diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-23 16:16:35 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-23 16:18:41 -0500 |
commit | 45ce66a29b3d1c49ef8e86b125701e89d58e8a4a (patch) | |
tree | 14d67dfef5c2d5e1aa7e4100526973cbd0ba4c2b /protocol/src/bridge_engine.rs | |
parent | 82c447fbfe12ee76dbbdaa36b0de1343d3a91795 (diff) |
Generalize table state to include the result of play
Diffstat (limited to 'protocol/src/bridge_engine.rs')
-rw-r--r-- | protocol/src/bridge_engine.rs | 76 |
1 files changed, 65 insertions, 11 deletions
diff --git a/protocol/src/bridge_engine.rs b/protocol/src/bridge_engine.rs index ee43437..0c583f3 100644 --- a/protocol/src/bridge_engine.rs +++ b/protocol/src/bridge_engine.rs @@ -7,7 +7,6 @@ use log::{error, info}; use rand::{ distributions::Standard, prelude::{Distribution, SliceRandom}, - random, }; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -756,17 +755,17 @@ impl GameState { } = self.bidding()?.clone(); Ok(match bidding.bid(bid)? { BiddingResult::InProgress(bidding) => { - MoveResult::Stay(GameState::Bidding(BiddingState { + MoveResult::Current(GameState::Bidding(BiddingState { dealer, deal, bidding, })) } BiddingResult::Contract(None, _bidding) => { - MoveResult::Go(GameResult) + MoveResult::Next(GameResult) } BiddingResult::Contract(Some(contract), bidding) => { - MoveResult::Stay(GameState::Play(PlayState::new( + MoveResult::Current(GameState::Play(PlayState::new( deal, contract, bidding, ))) } @@ -779,10 +778,10 @@ impl GameState { ) -> Result<MoveResult<GameState, GameResult>, anyhow::Error> { Ok(match self.play_state()?.clone().play(card)? { PlayStateResult::InProgress(play_state) => { - MoveResult::Stay(play_state.into()) + MoveResult::Current(play_state.into()) } PlayStateResult::PlayFinished(result) => { - MoveResult::Go(result.into()) + MoveResult::Next(result.into()) } }) } @@ -870,7 +869,11 @@ impl PlayStatePlayerView { hand: player_position .get_cards(&play_state.playing_deal.deal) .clone(), - previous_trick: play_state.playing_deal.tricks_played.last().cloned(), + previous_trick: play_state + .playing_deal + .tricks_played + .last() + .cloned(), current_trick: play_state.playing_deal.in_progress.clone(), } } @@ -924,6 +927,56 @@ impl GameStatePlayerView { } } +#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] +pub enum TableState { + Unknown, + Game(GameState), + Result(GameResult), +} + +impl Default for TableState { + fn default() -> Self { + TableState::Unknown + } +} + +impl Into<TableState> for MoveResult<GameState, GameResult> { + fn into(self) -> TableState { + match self { + MoveResult::Current(game) => TableState::Game(game), + MoveResult::Next(result) => TableState::Result(result), + } + } +} + +impl Into<TableState> for GameState { + fn into(self) -> TableState { + TableState::Game(self) + } +} + +#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] +pub enum TableStatePlayerView { + Unknown, + Game(GameStatePlayerView), + Result(GameResult), +} + +impl TableStatePlayerView { + pub fn from_table_state( + table: &TableState, + player_position: Player, + ) -> Self { + match table { + TableState::Unknown => TableStatePlayerView::Unknown, + TableState::Game(g) => TableStatePlayerView::Game( + GameStatePlayerView::from_game_state(g, player_position), + ), + TableState::Result(r) => TableStatePlayerView::Result(r.clone()), + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -1167,19 +1220,20 @@ mod tests { info!("Start bidding with game state {game_state:#?}"); let raise = |s| Bid::Raise(Raise::from_str(s).unwrap()); - let game_state = game_state.bid(raise("1H")).unwrap().stay().unwrap(); + let game_state = + game_state.bid(raise("1H")).unwrap().current().unwrap(); let game_state = game_state .bid(Bid::Pass) .unwrap() - .stay() + .current() .unwrap() .bid(Bid::Pass) .unwrap() - .stay() + .current() .unwrap() .bid(Bid::Pass) .unwrap() - .stay() + .current() .unwrap(); info!("Start playing with game state {game_state:#?}"); assert_eq!(game_state.is_bidding(), false); |