update id to use i64 instead of u32
authorGeoffrey Allott <geoffrey@allott.email>
Sat, 27 Feb 2021 15:24:14 +0000 (15:24 +0000)
committerGeoffrey Allott <geoffrey@allott.email>
Sat, 27 Feb 2021 15:24:14 +0000 (15:24 +0000)
src/api.rs
src/client.rs
src/dealer.rs
src/game/chatroom.rs
src/game/mod.rs
src/game/poker.rs
src/game/whist.rs
src/server.rs

index 27b566d261f17987aa2def1953b768ecdaf3c39a..b45f21b0f8b23dbaf56a912b819885623128bcd8 100644 (file)
@@ -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<GameSummary> },
     JoinLobbyFailure { reason: String },
index c7b0f1c507558741207e6d4e605fd5f76a83876a..a06201676ccaacf02b6e80cff513303936620ad7 100644 (file)
@@ -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 },
 }
 
index bcd7ed17129cc769f66b47f7faa5122b779fc27f..1a9c90c6143ec26e544e1e16c14b708734355d74 100644 (file)
@@ -20,7 +20,7 @@ pub struct DealerState {
 }
 
 impl Dealer {
-    pub async fn new(mut server: ServerState, id: u32) -> RedisResult<Self> {
+    pub async fn new(mut server: ServerState, id: i64) -> RedisResult<Self> {
         let mut interests = HashSet::new();
         interests.insert(ClientInterest::Game{id});
         server.register_interests(interests).await;
index d218a7f57f338cc78fe46acbb953ddbfd686021f..113ef7728d2878caa8fbbe9b6dd4f4c1b97604fd 100644 (file)
@@ -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<Username>,
 }
 
 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
     }
 
index f4e74008a39c3a9ebca67fd93ab6437a9f395ebc..b0fd05f890da46a299220859d5bf9ae925d7c2e7 100644 (file)
@@ -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<Username>;
     fn actions_len(&self) -> usize;
     fn validate_action(&self, action: UserAction) -> Result<ValidatedUserAction, ActionError>;
@@ -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
     }
 }
index ba08f22ba43a48b07385f4ccdba62319a3470eb2..52a90cf6378a623a50dcbdb348704b42896635f5 100644 (file)
@@ -1,79 +1,38 @@
-pub enum TexasHoldEm {
-    NotYetStarted {
-        seats: Seats,
-        stacks: HashMap<Username, u32>,
-    }
-    Dealing {
-        dealer: Username,
-        hands: HashMap<Username, HashSet<Card>>,
-        deck: HashSet<Card>,
-        seats: Seats,
-        stacks: HashMap<Username, u32>,
-    }
-    PostSmallBlind {
-        dealer: Username,
-        action: Username,
-        hands: HashMap<Username, HashSet<Card>>,
-        deck: HashSet<Card>,
-        seats: Seats,
-        bets: HashMap<Username, u32>,
-        players: HashSet<Username>,
-        stacks: HashMap<Username, u32>,
-    }
-    PostBigBlind {
-        dealer: Username,
-        action: Username,
-        hands: HashMap<Username, HashSet<Card>>,
-        deck: HashSet<Card>,
-        seats: Seats,
-        bets: HashMap<Username, u32>,
-        players: HashSet<Username>,
-        stacks: HashMap<Username, u32>,
-    }
-    PreFlopBetting {
-        dealer: Username,
-        action: Username,
-        hands: HashMap<Username, HashSet<Card>>,
-        deck: HashSet<Card>,
-        seats: Seats,
-        pot: u32,
-        bets: HashMap<Username, u32>,
-        players: HashSet<Username>,
-        stacks: HashMap<Username, u32>,
-    }
-    DealFirstFlopCard {
-        dealer: Username,
-        action: Username,
-        flop: [Card; 1],
-        hands: HashMap<Username, HashSet<Card>>,
-        deck: HashSet<Card>,
-        seats: Seats,
-        pot: u32,
-        players: HashSet<Username>,
-        stacks: HashMap<Username, u32>,
-    }
-    DealSecondFlopCard {
-        dealer: Username,
-        action: Username,
-        flop: [Card; 2],
-        hands: HashMap<Username, HashSet<Card>>,
-        deck: HashSet<Card>,
-        seats: Seats,
-        pot: u32,
-        players: HashSet<Username>,
-        stacks: HashMap<Username, u32>,
-    }
-    DealThirdFlopCard {
-        dealer: Username,
-        action: Username,
-        flop: [Card; 3],
-        hands: HashMap<Username, HashSet<Card>>,
-        deck: HashSet<Card>,
-        seats: Seats,
-        pot: u32,
-        players: HashSet<Username>,
-        stacks: HashMap<Username, u32>,
-    }
-    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<Username, u64>,
+    hands: HashMap<Username, HashSet<Card>>,
+    deck: HashSet<Card>,
+    dealer: Username,
+    action: Username,
+    bets: HashMap<Username, u64>,
+    players: HashSet<Username>,
+    pot: u64,
+    community: HashSet<Card>,
 }
index f4d4ecd7798518b7b968877a31efed96f2a2997b..411fee797f4d0ac54c34891caca904c3bb033243 100644 (file)
@@ -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
     }
 
index 455c98b787a76d887d4fe8c4f4d5b6109a099c93..e4ca70fca6b6011b26e2cc531b6b20f1ec8441ff 100644 (file)
@@ -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<u32> {
-        let id = self.redis.incr("game:next_id", 1u32).await?;
+    pub async fn create_game(&mut self, settings: GameSettings) -> RedisResult<i64> {
+        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<Vec<GameSummary>> {
         debug!("game_list(from: {})", from);
-        let games: Vec<u32> = self.redis.lrange("game:list", from as isize, -1).await?;
+        let games: Vec<i64> = 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<Vec<ValidatedUserAction>> {
+    pub async fn game_state(&mut self, id: i64, from: usize) -> RedisResult<Vec<ValidatedUserAction>> {
         let key = game_actions_key(id);
         let actions: Vec<AsJson<ValidatedUserAction>> = 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<GameSummary> {
+    pub async fn game_summary(&mut self, id: i64) -> RedisResult<GameSummary> {
         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<ActionStatus> {
+    pub async fn take_action(&mut self, id: i64, len: usize, action: &ValidatedUserAction) -> RedisResult<ActionStatus> {
         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