diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-03 13:49:54 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-03 13:49:54 -0500 |
commit | e6933ac48848b598f870ff5a9d96a0336728620b (patch) | |
tree | 3c6b9e9c9a6100b1400dd1a02fc07fbb5803d7b7 | |
parent | 03ad7a9728c22a8016ade56f79049e32db5941ac (diff) |
Display player turn in webapp
-rw-r--r-- | protocol/src/bridge_engine.rs | 19 | ||||
-rw-r--r-- | protocol/src/simple_bots.rs | 2 | ||||
-rw-r--r-- | webapp/src/components/game.rs | 43 | ||||
-rw-r--r-- | 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<Raise> { 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 { <Bidding {on_contract} dealer={dealer.clone()} /> } } - GameState::Play { + GameState::Play(PlayState { + deal, dealer, playing_deal, contract: _, bidding: _, - } => { + }) => { html! { <> <TricksPlayed tricks={ playing_deal.tricks().clone() } /> @@ -88,7 +101,7 @@ pub fn game() -> Html { html! { <> <div class="nav"> - if let GameState::Play { contract, bidding, .. } = &*state { + if let GameState::Play(PlayState { contract, bidding, .. }) = &*state { <ShowBid contract={contract.clone()} bidding={bidding.clone()}/> } <button onclick={shuffle}>{ "Shuffle" }</button> diff --git a/webapp/src/components/table.rs b/webapp/src/components/table.rs index a285449..7c0b3a3 100644 --- a/webapp/src/components/table.rs +++ b/webapp/src/components/table.rs @@ -1,10 +1,10 @@ -use crate::components::{Hand, BiddingTable, BiddingBox}; +use crate::components::{BiddingBox, BiddingTable, Hand}; use crate::use_app_context; use crate::utils::ok_json; use anyhow::Context; use gloo_net::http::Request; use log::info; -use protocol::bridge_engine::GameStatePlayerView; +use protocol::bridge_engine::{GameStatePlayerView, BiddingState, BiddingStatePlayerView}; use yew::prelude::*; #[function_component(OnlineTable)] @@ -72,24 +72,16 @@ pub fn table(props: &TableProps) -> Html { }) }; let center = match &props.table { - GameStatePlayerView::Bidding { - bidding, - .. - } => html! { + GameStatePlayerView::Bidding(BiddingStatePlayerView { bidding, .. }) => html! { <> <BiddingTable bidding={bidding.clone()} /> <BiddingBox current_bid={bidding.highest_bid().clone()} { on_bid } /> + { format!("It is {:?} to bid", bidding.current_bidder()) } </> }, - GameStatePlayerView::PassedOut { - .. - } => todo!(), - GameStatePlayerView::Lead { - .. - } => todo!(), - GameStatePlayerView::Play { - .. - } => todo!(), + GameStatePlayerView::PassedOut { .. } => todo!(), + GameStatePlayerView::Lead { .. } => todo!(), + GameStatePlayerView::Play { .. } => todo!(), }; html! { <> |