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 | |
parent | 6b1f611e4ae1c719acd7d0d8dd5716dd0d52b255 (diff) |
Populate result after a board is played
-rw-r--r-- | protocol/src/bridge_engine.rs | 75 | ||||
-rw-r--r-- | protocol/src/lib.rs | 2 | ||||
-rw-r--r-- | protocol/src/move_result.rs (renamed from protocol/src/play_result.rs) | 0 | ||||
-rw-r--r-- | protocol/src/simple_bots.rs | 16 | ||||
-rw-r--r-- | server/src/play.rs | 4 | ||||
-rw-r--r-- | webapp/src/components/table.rs | 4 |
6 files changed, 54 insertions, 47 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 { 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/play_result.rs b/protocol/src/move_result.rs index 8dad6ef..8dad6ef 100644 --- a/protocol/src/play_result.rs +++ b/protocol/src/move_result.rs 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<J: Journal<TableUpdate>> Table<J> { } } - 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 { <p>{"An error occurred."}</p> }, Some(TableStatePlayerView::Game(game)) => self.view_game(ctx, game), - Some(TableStatePlayerView::Result(_)) => html! { + Some(TableStatePlayerView::Result(result)) => html! { <> - <p>{"A beautiful result."}</p> + <pre>{format!("{result:#?}")}</pre> <button onclick={ctx.link().callback(|_| Msg::RequestNewDeal)}> {"New deal"} </button> |