Skip to content

Commit

Permalink
scores
Browse files Browse the repository at this point in the history
  • Loading branch information
LasmGratel committed Aug 11, 2021
1 parent cc482eb commit 97210a0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ License under GPL-3.0.
- [X] 网络通信
- [X] 服务端
- [X] 控制台客户端
- [ ] 可游玩
- [X] 可游玩
- [ ] 积分结算
- [ ] 匹配大厅
- [ ] 图形界面客户端
2 changes: 1 addition & 1 deletion cardgame-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn run_network_thread(
GameError::WrongRule => {
println!("你出的牌不满足当前规则");
}
GameError::Win(player) => {
GameError::Win(player, player_type, score) => {
println!("{} 赢了。", player);
*client_state.lock().unwrap() = ClientState::Idle;
}
Expand Down
11 changes: 6 additions & 5 deletions cardgame-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ use crate::server_lobby::ServerLobby;

pub mod server_lobby;

pub fn get_room_by_endpoint(endpoint: &Endpoint) {

}

pub fn main() {
// Create a node, the main message-io entity. It is divided in 2 parts:
// The 'handler', used to make actions (connect, send messages, signals, stop the node...)
Expand Down Expand Up @@ -232,7 +228,12 @@ pub fn main() {
}
}
Err(e) => {
send_to_client(&S2CMessage::GameErr(e));
match e {
GameError::Win(user, player_type, score) => {
// TODO 结算并写入文件
}
_ => send_to_client(&S2CMessage::GameErr(e))
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cardgame-server/src/server_lobby.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cardgame::{Lobby, Room, Game, LobbyError, RoomState};
use cardgame::user::{User, UserId};
use cardgame::user::UserId;
use std::collections::HashMap;

pub struct ServerLobby {
Expand Down
54 changes: 36 additions & 18 deletions cardgame/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use rand::Rng;
use regex::Regex;
use serde::{Serialize, Deserialize};
use std::slice::Iter;
use crate::user::UserId;

/// 基础积分
const BASE_POINTS: u32 = 100;

#[derive(PartialEq, Eq)]
pub enum GameState {
Expand Down Expand Up @@ -41,6 +45,8 @@ pub struct Game {
pub landlord_cards: Vec<Card>,

/// 积分倍率
/// 炸弹,火箭会*=2
/// 加倍 *=2,超级加倍 *=4
pub score_multiplier: u32,
}

Expand Down Expand Up @@ -88,6 +94,7 @@ impl Game {
}
}

#[cfg(debug_assertions)]
pub fn print_cards(&self) {
for p in self.players.iter() {
print!("{}: ", p.user);
Expand All @@ -98,10 +105,17 @@ impl Game {
}
}

#[cfg(not(debug_assertions))]
pub fn print_cards(&self) {}

#[cfg(debug_assertions)]
pub fn print_player(&self) {
println!("轮到 {} 出牌", self.current_player().user);
}

#[cfg(not(debug_assertions))]
pub fn print_player(&self) {}

pub fn landlord_player(&self) -> &Player {
&self.players[self.landlord_index]
}
Expand Down Expand Up @@ -133,24 +147,20 @@ impl Game {
Ok((&self.players[self.landlord_index], self.players.iter()))
}

pub fn win(&mut self) {
pub fn win(&mut self) -> GameError {
#[cfg(debug_assertions)]
match self.current_player().player_type {
PlayerType::Landlord => {
println!("{} 赢了!", self.current_player().user);
println!("地主赢了!");
}
PlayerType::Farmer => {
for p in self
.players
.iter()
.filter(|x| x.player_type == PlayerType::Farmer)
{
println!("{} 赢了!", self.current_player().user);
// TODO 结算
}
println!("农民赢了!");
}
}
self.print_cards();

self.state = GameState::WaitingForPlayers;
GameError::Win(self.current_player().user.clone(), self.current_player().player_type.clone(), BASE_POINTS * self.score_multiplier)
}

pub fn pass(&mut self) -> Result<String, GameError> {
Expand All @@ -172,16 +182,23 @@ impl Game {
if option.is_none() {
return Err(GameError::NoSuchCards);
}
print!("{} 出牌:", self.current_player().user);
for c in cards.iter() {
print!("[{}]", c.to_string());

#[cfg(debug_assertions)] {
print!("{} 出牌:", self.current_player().user);
for c in cards.iter() {
print!("[{}]", c.to_string());
}
println!();
}

// 炸弹积分翻倍
if rule.bomb_priority() == 1 || rule.bomb_priority() == 2 {
self.score_multiplier *= 2;
}
println!();

// 赢得胜利
if self.current_player().cards.is_empty() {
self.win();
return Err(GameError::Win(self.current_player().user.clone()));
return Err(self.win());
}

self.players[self.index].cards = option.unwrap().to_cards();
Expand Down Expand Up @@ -219,7 +236,7 @@ impl Game {

self.state = GameState::Running;

// 显示信息
// 显示调试信息
self.print_cards();
self.print_player();

Expand All @@ -232,7 +249,8 @@ pub enum GameError {
NotRunning, NotYourTurn, NoSuchCards, WrongRule,

/// 这把赢了
Win(String),
/// 参数:最后出掉牌的玩家,玩家类型,获得的积分
Win(UserId, PlayerType, u32),

/// 过你马呢
YourTurn
Expand Down
2 changes: 1 addition & 1 deletion cardgame/src/lobby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Room {
if self.state != RoomState::Ready {
Err(RoomError::NotReady)
} else {
self.game.start().map_err(|e| RoomError::NotReady) // TODO
self.game.start().map_err(|e| RoomError::NotReady) // TODO 处理错误
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cardgame/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::user::UserId;

/// 玩家类型
#[derive(PartialEq, Serialize, Deserialize)]
#[derive(PartialEq, Serialize, Deserialize, Clone)]
pub enum PlayerType {
/// 农民
Farmer,
Expand Down
2 changes: 1 addition & 1 deletion cardgame/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl UserManager {
let path = Path::new(&self.path).join(id);
let str = fs::read_to_string(path);
if str.is_ok() {
serde_json::from_str(&str.unwrap()).map_err(|e| ())
serde_json::from_str(&str.unwrap()).map_err(|_| ())
} else {
Err(())
}
Expand Down

0 comments on commit 97210a0

Please sign in to comment.