From 0d9ba3facabbda24eeab35ccead84bd56164a929 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Sat, 27 Feb 2021 15:24:14 +0000 Subject: [PATCH] update id to use i64 instead of u32 --- src/api.rs | 8 +-- src/client.rs | 2 +- src/dealer.rs | 2 +- src/game/chatroom.rs | 6 +-- src/game/mod.rs | 8 +-- src/game/poker.rs | 115 ++++++++++++++----------------------------- src/game/whist.rs | 6 +-- src/server.rs | 16 +++--- 8 files changed, 61 insertions(+), 102 deletions(-) diff --git a/src/api.rs b/src/api.rs index 27b566d..b45f21b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -5,8 +5,8 @@ use crate::username::Username; #[derive(Debug, Clone, Deserialize)] pub enum Scope { Global, - Game { id: u32 }, - Hand { id: u32 }, + Game { id: i64 }, + Hand { id: i64 }, Player { username: String }, } @@ -21,7 +21,7 @@ pub enum ClientMessage { Logout, CreateGame { settings: GameSettings }, JoinLobby { filter: String }, - JoinGame { id: u32 }, + JoinGame { id: i64 }, TakeAction { action: Action }, SendMessage { scope: Scope, message: String }, LeaveGame, @@ -42,7 +42,7 @@ pub enum ServerMessage { ChangeNicknameSuccess, ChangeNicknameFailure { reason: String }, LogoutSuccess, - CreateGameSuccess { id: u32 }, + CreateGameSuccess { id: i64 }, CreateGameFailure { reason: String }, JoinLobbySuccess { games: Vec }, JoinLobbyFailure { reason: String }, diff --git a/src/client.rs b/src/client.rs index c7b0f1c..a062016 100644 --- a/src/client.rs +++ b/src/client.rs @@ -25,7 +25,7 @@ pub enum ClientState { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ClientInterest { GameList, - Game { id: u32 }, + Game { id: i64 }, User { username: Username }, } diff --git a/src/dealer.rs b/src/dealer.rs index bcd7ed1..1a9c90c 100644 --- a/src/dealer.rs +++ b/src/dealer.rs @@ -20,7 +20,7 @@ pub struct DealerState { } impl Dealer { - pub async fn new(mut server: ServerState, id: u32) -> RedisResult { + pub async fn new(mut server: ServerState, id: i64) -> RedisResult { let mut interests = HashSet::new(); interests.insert(ClientInterest::Game{id}); server.register_interests(interests).await; diff --git a/src/game/chatroom.rs b/src/game/chatroom.rs index d218a7f..113ef77 100644 --- a/src/game/chatroom.rs +++ b/src/game/chatroom.rs @@ -20,14 +20,14 @@ pub struct ChatroomSettings { #[derive(Debug, Clone)] pub struct Chatroom { - id: u32, + id: i64, settings: ChatroomSettings, messages: Vec<(Username, ChatroomAction)>, users: HashSet, } impl Chatroom { - pub fn new(id: u32, settings: ChatroomSettings) -> Self { + pub fn new(id: i64, settings: ChatroomSettings) -> Self { Chatroom { id, settings, @@ -38,7 +38,7 @@ impl Chatroom { } impl Game for Chatroom { - fn id(&self) -> u32 { + fn id(&self) -> i64 { self.id } diff --git a/src/game/mod.rs b/src/game/mod.rs index f4e7400..b0fd05f 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -13,7 +13,7 @@ use self::whist::{KnockOutWhist, KnockOutWhistSettings}; pub use self::action::{Action, ActionError, UserAction, ValidatedUserAction}; pub trait Game : Debug + CloneBox + Send + Sync { - fn id(&self) -> u32; + fn id(&self) -> i64; fn players(&self) -> HashSet; fn actions_len(&self) -> usize; fn validate_action(&self, action: UserAction) -> Result; @@ -79,16 +79,16 @@ impl GameList { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct GameSummary { - id: u32, + id: i64, settings: GameSettings, } impl GameSummary { - pub fn new(id: u32, settings: GameSettings) -> Self { + pub fn new(id: i64, settings: GameSettings) -> Self { Self{id, settings} } - pub fn id(&self) -> u32 { + pub fn id(&self) -> i64 { self.id } } diff --git a/src/game/poker.rs b/src/game/poker.rs index ba08f22..52a90cf 100644 --- a/src/game/poker.rs +++ b/src/game/poker.rs @@ -1,79 +1,38 @@ -pub enum TexasHoldEm { - NotYetStarted { - seats: Seats, - stacks: HashMap, - } - Dealing { - dealer: Username, - hands: HashMap>, - deck: HashSet, - seats: Seats, - stacks: HashMap, - } - PostSmallBlind { - dealer: Username, - action: Username, - hands: HashMap>, - deck: HashSet, - seats: Seats, - bets: HashMap, - players: HashSet, - stacks: HashMap, - } - PostBigBlind { - dealer: Username, - action: Username, - hands: HashMap>, - deck: HashSet, - seats: Seats, - bets: HashMap, - players: HashSet, - stacks: HashMap, - } - PreFlopBetting { - dealer: Username, - action: Username, - hands: HashMap>, - deck: HashSet, - seats: Seats, - pot: u32, - bets: HashMap, - players: HashSet, - stacks: HashMap, - } - DealFirstFlopCard { - dealer: Username, - action: Username, - flop: [Card; 1], - hands: HashMap>, - deck: HashSet, - seats: Seats, - pot: u32, - players: HashSet, - stacks: HashMap, - } - DealSecondFlopCard { - dealer: Username, - action: Username, - flop: [Card; 2], - hands: HashMap>, - deck: HashSet, - seats: Seats, - pot: u32, - players: HashSet, - stacks: HashMap, - } - DealThirdFlopCard { - dealer: Username, - action: Username, - flop: [Card; 3], - hands: HashMap>, - deck: HashSet, - seats: Seats, - pot: u32, - players: HashSet, - stacks: HashMap, - } - PostFlopBetting { - } +pub enum TexasHoldEmState { + NotStarted, + Dealing, + PostingSmallBlind, + PostingBigBlind, + PreFlopBetting, + DealingFlop, + PostFlopBetting, + DealingTurn, + TurnBetting, + DealingRiver, + RiverBetting, + Completed, +} + +pub struct TexasHoldEmSettings { + title: String, + max_players: u32, + blinds: u64, + starting_stack: u64, +} + +pub struct TexasHoldEm { + id: i64, + settings: TexasHoldEmSettings, + actions_len: usize, + state: TexasHoldEmState, + seats: Seats, + stacks: HashMap, + hands: HashMap>, + deck: HashSet, + dealer: Username, + action: Username, + bets: HashMap, + players: HashSet, + pot: u64, + community: HashSet, } diff --git a/src/game/whist.rs b/src/game/whist.rs index f4d4ecd..411fee7 100644 --- a/src/game/whist.rs +++ b/src/game/whist.rs @@ -28,7 +28,7 @@ pub struct KnockOutWhistSettings { #[derive(Clone, Debug)] pub struct KnockOutWhist { - id: u32, + id: i64, settings: KnockOutWhistSettings, actions_len: usize, state: KnockOutWhistState, @@ -49,7 +49,7 @@ pub struct KnockOutWhist { } impl KnockOutWhist { - pub fn new(id: u32, settings: KnockOutWhistSettings) -> Self { + pub fn new(id: i64, settings: KnockOutWhistSettings) -> Self { Self { id, settings, @@ -101,7 +101,7 @@ impl KnockOutWhist { } impl Game for KnockOutWhist { - fn id(&self) -> u32 { + fn id(&self) -> i64 { self.id } diff --git a/src/server.rs b/src/server.rs index 455c98b..e4ca70f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -113,11 +113,11 @@ fn user_key(username: Username) -> String { format!("user:{}", username) } -fn game_settings_key(id: u32) -> String { +fn game_settings_key(id: i64) -> String { format!("game:{}:settings", id) } -fn game_actions_key(id: u32) -> String { +fn game_actions_key(id: i64) -> String { format!("game:{}:actions", id) } @@ -152,8 +152,8 @@ impl ServerState { } } - pub async fn create_game(&mut self, settings: GameSettings) -> RedisResult { - let id = self.redis.incr("game:next_id", 1u32).await?; + pub async fn create_game(&mut self, settings: GameSettings) -> RedisResult { + let id = self.redis.incr("game:next_id", 1).await?; let key = game_settings_key(id); let () = self.redis.set(key, AsJson(settings)).await?; let () = self.redis.rpush("game:list", id).await?; @@ -162,7 +162,7 @@ impl ServerState { pub async fn game_list(&mut self, from: usize) -> RedisResult> { debug!("game_list(from: {})", from); - let games: Vec = self.redis.lrange("game:list", from as isize, -1).await?; + let games: Vec = self.redis.lrange("game:list", from as isize, -1).await?; let mut summaries = Vec::with_capacity(games.len()); for id in games { match self.game_summary(id).await { @@ -173,18 +173,18 @@ impl ServerState { Ok(summaries) } - pub async fn game_state(&mut self, id: u32, from: usize) -> RedisResult> { + pub async fn game_state(&mut self, id: i64, from: usize) -> RedisResult> { let key = game_actions_key(id); let actions: Vec> = self.redis.lrange(&key, from as isize, -1).await?; Ok(actions.into_iter().map(AsJson::get).collect()) } - pub async fn game_summary(&mut self, id: u32) -> RedisResult { + pub async fn game_summary(&mut self, id: i64) -> RedisResult { let key = game_settings_key(id); self.redis.get(key).await.map(AsJson::get).map(|settings| GameSummary::new(id, settings)) } - pub async fn take_action(&mut self, id: u32, len: usize, action: &ValidatedUserAction) -> RedisResult { + pub async fn take_action(&mut self, id: i64, len: usize, action: &ValidatedUserAction) -> RedisResult { let key = game_actions_key(id); debug!("take_action: EVAL {{TAKE_ACTION_LUA_SCRIPT}} 1 {} {} {:?}", key, len, action); self.take_action_script.key(key).arg(len).arg(AsJson(action)).invoke_async(&mut self.redis).await -- 2.34.1