diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-23 16:16:35 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-23 16:18:41 -0500 |
commit | 45ce66a29b3d1c49ef8e86b125701e89d58e8a4a (patch) | |
tree | 14d67dfef5c2d5e1aa7e4100526973cbd0ba4c2b /webapp/src/components | |
parent | 82c447fbfe12ee76dbbdaa36b0de1343d3a91795 (diff) |
Generalize table state to include the result of play
Diffstat (limited to 'webapp/src/components')
-rw-r--r-- | webapp/src/components/table.rs | 80 |
1 files changed, 47 insertions, 33 deletions
diff --git a/webapp/src/components/table.rs b/webapp/src/components/table.rs index e1ec1a1..9702793 100644 --- a/webapp/src/components/table.rs +++ b/webapp/src/components/table.rs @@ -4,8 +4,8 @@ use crate::{services, use_app_context}; use futures::FutureExt; use log::info; use protocol::bridge_engine::{ - Bid, BiddingStatePlayerView, GameStatePlayerView, - PlayStatePlayerView, Player, + Bid, BiddingStatePlayerView, GameStatePlayerView, PlayStatePlayerView, + Player, TableStatePlayerView, }; use protocol::card::Card; use yew::prelude::*; @@ -30,11 +30,11 @@ pub struct OnlineTableInnerProps { } struct OnlineTableInner { - table_state: Option<GameStatePlayerView>, + table_state: Option<TableStatePlayerView>, } pub enum Msg { - TableStateUpdated(Result<GameStatePlayerView, anyhow::Error>), + TableStateUpdated(Result<TableStatePlayerView, anyhow::Error>), Bid(Bid), Play(Card), } @@ -42,7 +42,9 @@ pub enum Msg { impl OnlineTableInner { fn play(&mut self, ctx: &yew::Context<Self>, card: Card) { let _play_state = match &self.table_state { - Some(GameStatePlayerView::Playing(play_state)) => play_state, + Some(TableStatePlayerView::Game(GameStatePlayerView::Playing( + play_state, + ))) => play_state, _ => { info!( "Cannot play card with table state: {:#?}", @@ -61,6 +63,38 @@ impl OnlineTableInner { .map(Msg::TableStateUpdated), ); } + + fn view_game( + &self, + ctx: &yew::Context<Self>, + game: &GameStatePlayerView, + ) -> Html { + let center = match game { + GameStatePlayerView::Bidding(bidding) => { + bidding_view(bidding, ctx.link().callback(Msg::Bid)) + } + GameStatePlayerView::Playing(playing) => { + playing_view(playing, ctx.link().callback(Msg::Play)) + } + }; + + let leave_table = { + let ctx = ctx.props().app_ctx.clone(); + Callback::from(move |_| ctx.leave_table()) + }; + + html! { + <> + <div class="game-layout"> + {center} + </div> + <p>{format!("This is table {}", ctx.props().table.id)}</p> + <button onclick={leave_table}> + { "Leave table" } + </button> + </> + } + } } impl Component for OnlineTableInner { @@ -106,35 +140,15 @@ impl Component for OnlineTableInner { } fn view(&self, ctx: &yew::Context<Self>) -> Html { - let table_state = match &self.table_state { - Some(x) => x, + match &self.table_state { None => return loading(), - }; - - let center = match table_state { - GameStatePlayerView::Bidding(bidding) => { - bidding_view(bidding, ctx.link().callback(Msg::Bid)) - } - GameStatePlayerView::Playing(playing) => { - playing_view(playing, ctx.link().callback(Msg::Play)) - } - }; - - let leave_table = { - let ctx = ctx.props().app_ctx.clone(); - Callback::from(move |_| ctx.leave_table()) - }; - - html! { - <> - <div class="game-layout"> - {center} - </div> - <p>{format!("This is table {}", ctx.props().table.id)}</p> - <button onclick={leave_table}> - { "Leave table" } - </button> - </> + Some(TableStatePlayerView::Unknown) => html! { + <p>{"An error occurred."}</p> + }, + Some(TableStatePlayerView::Game(game)) => self.view_game(ctx, game), + Some(TableStatePlayerView::Result(result)) => html! { + <p>{"A beautiful result."}</p> + }, } } } |