diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-30 09:29:12 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-30 09:29:12 -0500 |
commit | 83ffcc667999879197508aba0d1f910ca7cb000b (patch) | |
tree | f4eb596e5cbd6957f50a642b4e85160db0448969 /protocol/src/bridge_engine.rs | |
parent | 6b1f611e4ae1c719acd7d0d8dd5716dd0d52b255 (diff) |
Populate result after a board is played
Diffstat (limited to 'protocol/src/bridge_engine.rs')
-rw-r--r-- | protocol/src/bridge_engine.rs | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/protocol/src/bridge_engine.rs b/protocol/src/bridge_engine.rs index a3aa12e..708d9ae 100644 --- a/protocol/src/bridge_engine.rs +++ b/protocol/src/bridge_engine.rs @@ -1,6 +1,6 @@ use crate::{ card::{make_deck, sort_cards, Card, RankOrder, Suit}, - play_result::MoveResult, + move_result::MoveResult, }; use anyhow::{anyhow, bail}; use log::info; @@ -670,26 +670,41 @@ impl PlayState { self.playing_deal.current_player() } - pub fn play(self, card: Card) -> Result<PlayStateResult, anyhow::Error> { + pub fn play( + self, + card: Card, + ) -> Result<MoveResult<PlayState, PlayResult>, anyhow::Error> { Ok(match self.playing_deal.play(card)? { - MoveResult::Current(playing_deal) => { - PlayStateResult::InProgress(Self { - playing_deal, - ..self - }) + MoveResult::Current(playing_deal) => MoveResult::Current(Self { + playing_deal, + ..self + }), + MoveResult::Next(tricks) => { + MoveResult::Next(PlayResult::Played(PlayedResult { + bidding: self.bidding, + tricks, + })) } - MoveResult::Next(_) => PlayStateResult::PlayFinished(PlayResult), }) } } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct PlayResult; +pub struct PassedOutResult { + pub deal: Deal, + pub bidding: Bidding, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct PlayedResult { + pub bidding: Bidding, + pub tricks: Vec<Trick>, +} -#[derive(Debug)] -pub enum PlayStateResult { - InProgress(PlayState), - PlayFinished(PlayResult), +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub enum PlayResult { + PassedOut(PassedOutResult), + Played(PlayedResult), } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -759,7 +774,7 @@ impl GameState { pub fn bid( self, bid: Bid, - ) -> Result<MoveResult<GameState, GameResult>, anyhow::Error> { + ) -> Result<MoveResult<GameState, PlayResult>, anyhow::Error> { let BiddingState { dealer, deal, @@ -773,8 +788,11 @@ impl GameState { bidding, })) } - BiddingResult::Contract(None, _bidding) => { - MoveResult::Next(GameResult) + BiddingResult::Contract(None, bidding) => { + MoveResult::Next(PlayResult::PassedOut(PassedOutResult { + deal, + bidding + })) } BiddingResult::Contract(Some(contract), bidding) => { MoveResult::Current(GameState::Play(PlayState::new( @@ -787,27 +805,16 @@ impl GameState { pub fn play( self, card: Card, - ) -> Result<MoveResult<GameState, GameResult>, anyhow::Error> { + ) -> Result<MoveResult<GameState, PlayResult>, anyhow::Error> { Ok(match self.play_state()?.clone().play(card)? { - PlayStateResult::InProgress(play_state) => { + MoveResult::Current(play_state) => { MoveResult::Current(play_state.into()) } - PlayStateResult::PlayFinished(result) => { - MoveResult::Next(result.into()) - } + MoveResult::Next(result) => MoveResult::Next(result.into()), }) } } -#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] -pub struct GameResult; - -impl From<PlayResult> for GameResult { - fn from(_val: PlayResult) -> GameResult { - GameResult - } -} - #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct Deal { pub north: Vec<Card>, @@ -943,7 +950,7 @@ impl GameStatePlayerView { pub enum TableState { Unknown, Game(GameState), - Result(GameResult), + Result(PlayResult), } impl Default for TableState { @@ -952,8 +959,8 @@ impl Default for TableState { } } -impl From<MoveResult<GameState, GameResult>> for TableState { - fn from(val: MoveResult<GameState, GameResult>) -> Self { +impl From<MoveResult<GameState, PlayResult>> for TableState { + fn from(val: MoveResult<GameState, PlayResult>) -> Self { match val { MoveResult::Current(game) => TableState::Game(game), MoveResult::Next(result) => TableState::Result(result), @@ -971,7 +978,7 @@ impl From<GameState> for TableState { pub enum TableStatePlayerView { Unknown, Game(GameStatePlayerView), - Result(GameResult), + Result(PlayResult), } impl TableStatePlayerView { |