From 83ffcc667999879197508aba0d1f910ca7cb000b Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 30 Dec 2022 09:29:12 -0500 Subject: Populate result after a board is played --- protocol/src/bridge_engine.rs | 75 +++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 34 deletions(-) (limited to 'protocol/src/bridge_engine.rs') 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 { + pub fn play( + self, + card: Card, + ) -> Result, 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, +} -#[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, anyhow::Error> { + ) -> Result, 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, anyhow::Error> { + ) -> Result, 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 for GameResult { - fn from(_val: PlayResult) -> GameResult { - GameResult - } -} - #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct Deal { pub north: Vec, @@ -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> for TableState { - fn from(val: MoveResult) -> Self { +impl From> for TableState { + fn from(val: MoveResult) -> Self { match val { MoveResult::Current(game) => TableState::Game(game), MoveResult::Next(result) => TableState::Result(result), @@ -971,7 +978,7 @@ impl From for TableState { pub enum TableStatePlayerView { Unknown, Game(GameStatePlayerView), - Result(GameResult), + Result(PlayResult), } impl TableStatePlayerView { -- cgit v1.2.3