From 45ce66a29b3d1c49ef8e86b125701e89d58e8a4a Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 23 Dec 2022 16:16:35 -0500 Subject: Generalize table state to include the result of play --- protocol/src/bridge_engine.rs | 76 ++++++++++++++++++++++++++++++++++++------- protocol/src/play_result.rs | 23 +++++++------ 2 files changed, 78 insertions(+), 21 deletions(-) (limited to 'protocol/src') 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, 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 for MoveResult { + fn into(self) -> TableState { + match self { + MoveResult::Current(game) => TableState::Game(game), + MoveResult::Next(result) => TableState::Result(result), + } + } +} + +impl Into 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); diff --git a/protocol/src/play_result.rs b/protocol/src/play_result.rs index 21961ee..2dba276 100644 --- a/protocol/src/play_result.rs +++ b/protocol/src/play_result.rs @@ -1,20 +1,23 @@ -pub enum MoveResult { - Stay(Stay), - Go(Go), +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +pub enum MoveResult { + Current(Current), + Next(Next), } -impl MoveResult { - pub fn stay(self) -> Result { +impl MoveResult { + pub fn current(self) -> Result { match self { - MoveResult::Stay(o) => Ok(o), - MoveResult::Go(_) => Err(anyhow::anyhow!("Not in stay state")), + MoveResult::Current(o) => Ok(o), + MoveResult::Next(_) => Err(anyhow::anyhow!("Not in current state")), } } - pub fn go(self) -> Result { + pub fn next(self) -> Result { match self { - MoveResult::Go(f) => Ok(f), - MoveResult::Stay(_) => Err(anyhow::anyhow!("Not in go state")), + MoveResult::Next(f) => Ok(f), + MoveResult::Current(_) => Err(anyhow::anyhow!("Not in next state")), } } } -- cgit v1.2.3