From: Geoffrey Allott Date: Sat, 20 Mar 2021 20:37:44 +0000 (+0000) Subject: add game start time X-Git-Url: https://git.pointlesshacks.com/?a=commitdiff_plain;h=127e98b3f5c74324ce9ecd3824e99fb742ff2729;p=pokerwave.git add game start time --- diff --git a/src/game/mod.rs b/src/game/mod.rs index 3fea22f..552e0f2 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -115,3 +115,16 @@ impl GameSummary { self.id } } + +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +#[serde(untagged)] +enum StartCondition { + WhenFull, + AtTime(Timestamp), +} + +impl Default for StartCondition { + fn default() -> Self { + StartCondition::WhenFull + } +} diff --git a/src/game/poker/holdem.rs b/src/game/poker/holdem.rs index 7fab489..dd624fa 100644 --- a/src/game/poker/holdem.rs +++ b/src/game/poker/holdem.rs @@ -10,7 +10,7 @@ use crate::seats::Seats; use crate::username::Username; use crate::util::{max::IteratorMaxItems, timestamp::Timestamp}; -use super::super::{Action, ActionError, DealerAction, Game, UserAction, ValidatedUserAction}; +use super::super::{Action, ActionError, DealerAction, Game, StartCondition, UserAction, ValidatedUserAction}; use super::classify::rank_7_card_hand; @@ -47,6 +47,8 @@ pub struct TexasHoldEmSettings { round_length: Option, tournament_length: Option, action_timeout: Option, + #[serde(default)] + start_time: StartCondition, } impl TexasHoldEmSettings { @@ -480,13 +482,24 @@ impl Game for TexasHoldEm { let mut rng = self.rng.clone(); match self.state { State::NotStarted => { - if self.seats.players_len() == self.settings.max_players as usize { - // TODO - if let Some(username) = rng.choose_from(self.seats.player_set()) { - return DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::NextToDeal })); + match self.settings.start_time { + StartCondition::WhenFull => { + if self.seats.players_len() == self.settings.max_players as usize { + if let Some(username) = rng.choose_from(self.seats.player_set()) { + return DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::NextToDeal })); + } + } + DealerAction::WaitForPlayer + } + StartCondition::AtTime(start_time) => { + if timestamp >= start_time && self.seats.players_len() >= 2 { + if let Some(username) = rng.choose_from(self.seats.player_set()) { + return DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::NextToDeal })); + } + } + DealerAction::WaitUntil(start_time) } } - DealerAction::WaitForPlayer } State::Dealing => { if let Some(username) = self.receiver { @@ -658,6 +671,7 @@ mod tests { round_length: None, tournament_length: None, action_timeout: None, + start_time: StartCondition::WhenFull, }; assert_eq!(50, settings.level(0).small_blind); @@ -677,6 +691,7 @@ mod tests { round_length: Some(20 * 60 * 1000), tournament_length: Some(4 * 60 * 60 * 1000), action_timeout: None, + start_time: StartCondition::WhenFull, }; assert_eq!(25, settings.level(0).small_blind); diff --git a/src/game/whist.rs b/src/game/whist.rs index 262f1ad..912ac6a 100644 --- a/src/game/whist.rs +++ b/src/game/whist.rs @@ -6,7 +6,7 @@ use crate::seats::Seats; use crate::username::Username; use crate::util::{max::IteratorMaxItems, timestamp::Timestamp}; -use super::{Action, ActionError, DealerAction, Game, UserAction, ValidatedUserAction}; +use super::{Action, ActionError, DealerAction, Game, StartCondition, UserAction, ValidatedUserAction}; #[derive(Copy, Clone, Debug)] enum State { @@ -23,6 +23,8 @@ enum State { pub struct KnockOutWhistSettings { title: String, max_players: u32, + #[serde(default)] + start_time: StartCondition, } impl KnockOutWhistSettings { @@ -305,13 +307,24 @@ impl Game for KnockOutWhist { let mut rng = self.rng.clone(); match self.state { State::NotStarted => { - if self.seats.players_len() == self.settings.max_players as usize { - // TODO - if let Some(username) = rng.choose_from(self.seats.player_set()) { - return DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::NextToDeal })); + match self.settings.start_time { + StartCondition::WhenFull => { + if self.seats.players_len() == self.settings.max_players as usize { + if let Some(username) = rng.choose_from(self.seats.player_set()) { + return DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::NextToDeal })); + } + } + DealerAction::WaitForPlayer + } + StartCondition::AtTime(start_time) => { + if timestamp >= start_time && self.seats.players_len() >= 2 { + if let Some(username) = rng.choose_from(self.seats.player_set()) { + return DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::NextToDeal })); + } + } + DealerAction::WaitUntil(start_time) } } - DealerAction::WaitForPlayer } State::Dealing => { if let Some(username) = self.receiver { @@ -413,6 +426,24 @@ mod tests { } } + #[test] + fn start_game_at_time() { + let actions = r#"[ + {"timestamp":1616272415014,"username":"geoff","action":{"action":"Join","seat":0,"chips":0}}, + {"timestamp":1616272415018,"username":"kat","action":{"action":"Join","seat":1,"chips":0}}, + {"timestamp":1616280000000,"username":"geoff","action":{"action":"NextToDeal"}} + ]"#; + let actions = serde_json::from_str(actions).unwrap(); + + let settings = r#"{"format":"KnockOutWhist","title":"2-Player Knock-Out Whist Test","max_players":4,"start_time":1616280000000}"#; + let settings = serde_json::from_str(settings).unwrap(); + + let seed = r#"{"rng":"ChaCha20","seed":"0bc81b871dca272486f7c848a3c84102f501e934aaf87b311fc21e65327bf1f4"}"#; + let seed = serde_json::from_str(seed).unwrap(); + + test_game(actions, settings, seed); + } + #[test] fn complete_2_player_knock_out_whist() { let actions = r#"[