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 +++++++++++++++++++++++------------------- protocol/src/lib.rs | 2 +- protocol/src/move_result.rs | 23 +++++++++++++ protocol/src/play_result.rs | 23 ------------- protocol/src/simple_bots.rs | 16 ++++----- server/src/play.rs | 4 +-- webapp/src/components/table.rs | 4 +-- 7 files changed, 77 insertions(+), 70 deletions(-) create mode 100644 protocol/src/move_result.rs delete mode 100644 protocol/src/play_result.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 { diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 8ae9a36..f828be5 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -3,7 +3,7 @@ use uuid::Uuid; pub mod bot; pub mod bridge_engine; pub mod card; -pub mod play_result; +pub mod move_result; pub mod simple_bots; #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] diff --git a/protocol/src/move_result.rs b/protocol/src/move_result.rs new file mode 100644 index 0000000..8dad6ef --- /dev/null +++ b/protocol/src/move_result.rs @@ -0,0 +1,23 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +pub enum MoveResult { + Current(Current), + Next(Next), +} + +impl MoveResult { + pub fn current(self) -> Result { + match self { + MoveResult::Current(o) => Ok(o), + MoveResult::Next(_) => Err(anyhow::anyhow!("Not in current state")), + } + } + + pub fn next(self) -> Result { + match self { + MoveResult::Next(f) => Ok(f), + MoveResult::Current(_) => Err(anyhow::anyhow!("Not in next state")), + } + } +} diff --git a/protocol/src/play_result.rs b/protocol/src/play_result.rs deleted file mode 100644 index 8dad6ef..0000000 --- a/protocol/src/play_result.rs +++ /dev/null @@ -1,23 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] -pub enum MoveResult { - Current(Current), - Next(Next), -} - -impl MoveResult { - pub fn current(self) -> Result { - match self { - MoveResult::Current(o) => Ok(o), - MoveResult::Next(_) => Err(anyhow::anyhow!("Not in current state")), - } - } - - pub fn next(self) -> Result { - match self { - MoveResult::Next(f) => Ok(f), - MoveResult::Current(_) => Err(anyhow::anyhow!("Not in next state")), - } - } -} diff --git a/protocol/src/simple_bots.rs b/protocol/src/simple_bots.rs index ed1cd14..45d1a5b 100644 --- a/protocol/src/simple_bots.rs +++ b/protocol/src/simple_bots.rs @@ -46,13 +46,13 @@ impl PlayingBot for RandomPlayingBot { #[cfg(test)] mod tests { use std::str::FromStr; + use crate::move_result::MoveResult; use super::*; use crate::{ bridge_engine::{ Bidding, BiddingState, BiddingStatePlayerView, Contract, - ContractLevel, ContractModifier, Deal, PlayState, PlayStateResult, - Player, Raise, + ContractLevel, ContractModifier, Deal, PlayState, Player, Raise, }, card::Suit, }; @@ -75,8 +75,8 @@ mod tests { info!("South state: {south_state:#?}"); let play_state = match play_state.play(card1).unwrap() { - PlayStateResult::InProgress(p) => p, - PlayStateResult::PlayFinished(_) => { + MoveResult::Current(p) => p, + MoveResult::Next(_) => { panic!("game should not be over") } }; @@ -92,8 +92,8 @@ mod tests { assert_eq!(card1.suit(), card2.suit()); let _play_state = match play_state.play(card2).unwrap() { - PlayStateResult::InProgress(p) => p, - PlayStateResult::PlayFinished(_) => { + MoveResult::Current(p) => p, + MoveResult::Next(_) => { panic!("game should not be over") } }; @@ -159,8 +159,8 @@ mod tests { async fn play_until_completion() { crate::tests::test_setup(); let bot = RandomPlayingBot {}; - let mut result = PlayStateResult::InProgress(example_play_state()); - while let PlayStateResult::InProgress(play_state) = result { + let mut result = MoveResult::Current(example_play_state()); + while let MoveResult::Current(play_state) = result { info!("Play state: {play_state:#?}"); let player_state = PlayStatePlayerView::from_play_state( &play_state, diff --git a/server/src/play.rs b/server/src/play.rs index d15d64c..a825ece 100644 --- a/server/src/play.rs +++ b/server/src/play.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use protocol::{ bot::{BiddingBot, PlayingBot}, bridge_engine::{ - Bid, BiddingStatePlayerView, Deal, GameResult, GameState, + Bid, BiddingStatePlayerView, Deal, PlayResult, GameState, PlayStatePlayerView, Player, TableState, }, card::Card, @@ -154,7 +154,7 @@ impl> Table { } } - pub fn result(&self) -> Result<&GameResult, BridgeError> { + pub fn result(&self) -> Result<&PlayResult, BridgeError> { match &self.state { TableState::Result(r) => Ok(r), _ => Err(BridgeError::InvalidRequest("no result".to_string())), diff --git a/webapp/src/components/table.rs b/webapp/src/components/table.rs index d5bca4f..b9fe45d 100644 --- a/webapp/src/components/table.rs +++ b/webapp/src/components/table.rs @@ -162,9 +162,9 @@ impl Component for OnlineTableInner {

{"An error occurred."}

}, Some(TableStatePlayerView::Game(game)) => self.view_game(ctx, game), - Some(TableStatePlayerView::Result(_)) => html! { + Some(TableStatePlayerView::Result(result)) => html! { <> -

{"A beautiful result."}

+
{format!("{result:#?}")}
-- cgit v1.2.3