From e6933ac48848b598f870ff5a9d96a0336728620b Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sat, 3 Dec 2022 13:49:54 -0500 Subject: Display player turn in webapp --- protocol/src/bridge_engine.rs | 19 ++++++++++++++----- protocol/src/simple_bots.rs | 2 +- webapp/src/components/game.rs | 43 +++++++++++++++++++++++++++--------------- webapp/src/components/table.rs | 22 +++++++-------------- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/protocol/src/bridge_engine.rs b/protocol/src/bridge_engine.rs index 6b3cf7d..fa5c995 100644 --- a/protocol/src/bridge_engine.rs +++ b/protocol/src/bridge_engine.rs @@ -426,6 +426,10 @@ impl Bidding { self.dealer.many_next(self.bids.len() - 4) } + pub fn current_bidder(&self) -> Player { + self.dealer.many_next(self.bids.len()) + } + pub fn highest_bid(&self) -> Option { for bid in self.bids.iter().rev() { if let Some(raise) = bid.as_raise() { @@ -526,11 +530,11 @@ impl BiddingStatePlayerView { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct PlayState { - dealer: Player, - deal: Deal, - contract: Contract, - bidding: Bidding, - playing_deal: DealInPlay, + pub dealer: Player, + pub deal: Deal, + pub contract: Contract, + pub bidding: Bidding, + pub playing_deal: DealInPlay, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -696,10 +700,15 @@ mod tests { fn bidding() { crate::tests::test_setup(); let bidding = Bidding::new(Player::South); + assert_eq!(bidding.current_bidder(), Player::South); let bidding = as_bidding(bidding.bid(Bid::Pass).unwrap()); + assert_eq!(bidding.current_bidder(), Player::West); let bidding = as_bidding(bidding.bid(Bid::Raise("1♦".parse().unwrap())).unwrap()); + assert_eq!(bidding.current_bidder(), Player::North); let bidding = as_bidding(bidding.bid(Bid::Pass).unwrap()); + assert_eq!(bidding.current_bidder(), Player::East); let bidding = as_bidding(bidding.bid(Bid::Pass).unwrap()); + assert_eq!(bidding.current_bidder(), Player::South); let contract = as_contract(bidding.bid(Bid::Pass).unwrap()); assert_eq!( Some(Contract { diff --git a/protocol/src/simple_bots.rs b/protocol/src/simple_bots.rs index ffa1be2..7997418 100644 --- a/protocol/src/simple_bots.rs +++ b/protocol/src/simple_bots.rs @@ -6,7 +6,7 @@ struct AlwaysPassBiddingBot {} #[async_trait] impl BiddingBot for AlwaysPassBiddingBot { - async fn bid(&self, bidding: &BiddingStatePlayerView) -> Bid { + async fn bid(&self, _bidding: &BiddingStatePlayerView) -> Bid { Bid::Pass } } diff --git a/webapp/src/components/game.rs b/webapp/src/components/game.rs index c5e2602..9d45095 100644 --- a/webapp/src/components/game.rs +++ b/webapp/src/components/game.rs @@ -1,6 +1,8 @@ -use protocol::bridge_engine::{DealInPlay, DealInPlayResult, Player, GameState, deal}; use crate::components::{Bidding, Hand, ShowBid, TrickInPlay, TricksPlayed}; use log::{error, info}; +use protocol::bridge_engine::{ + deal, BiddingState, DealInPlay, DealInPlayResult, GameState, PlayState, Player, +}; use yew::prelude::*; fn init_state() -> GameState { @@ -21,22 +23,27 @@ pub fn game() -> Html { let on_card_clicked = { let state = state.clone(); Callback::from(move |card| { - if let GameState::Play { + if let GameState::Play(PlayState { dealer, playing_deal, contract, bidding, - } = (*state).clone() + deal, + .. + }) = (*state).clone() { info!("Card clicked: {}", card); match playing_deal.play(card) { Err(err) => error!("Could not play card: {:?}", err), - Ok(DealInPlayResult::InProgress(playing_deal)) => state.set(GameState::Play { - dealer, - playing_deal, - contract, - bidding, - }), + Ok(DealInPlayResult::InProgress(playing_deal)) => { + state.set(GameState::Play(PlayState { + dealer, + playing_deal, + contract, + bidding, + deal, + })) + } Ok(DealInPlayResult::PlayFinished(_tricks)) => todo!(), }; } @@ -44,19 +51,24 @@ pub fn game() -> Html { }; let center = match &*state { - GameState::Bidding { dealer, deal, bidding } => { + GameState::Bidding(BiddingState { + dealer, + deal, + bidding, + }) => { let on_contract = { let state = state.clone(); let dealer = dealer.clone(); let deal = deal.clone(); Callback::from(move |(contract, bidding)| { state.set(match contract { - Some(contract) => GameState::Play { + Some(contract) => GameState::Play(PlayState { dealer, + deal: deal.clone(), playing_deal: DealInPlay::new(dealer, deal.clone()), contract, bidding, - }, + }), None => GameState::PassedOut { dealer: dealer, deal: deal.clone(), @@ -69,12 +81,13 @@ pub fn game() -> Html { } } - GameState::Play { + GameState::Play(PlayState { + deal, dealer, playing_deal, contract: _, bidding: _, - } => { + }) => { html! { <> @@ -88,7 +101,7 @@ pub fn game() -> Html { html! { <>