From 45ce66a29b3d1c49ef8e86b125701e89d58e8a4a Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 23 Dec 2022 16:16:35 -0500 Subject: Generalize table state to include the result of play --- webapp/src/components/table.rs | 80 +++++++++++++++++++++++++----------------- webapp/src/services.rs | 4 +-- 2 files changed, 49 insertions(+), 35 deletions(-) (limited to 'webapp') 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, + table_state: Option, } pub enum Msg { - TableStateUpdated(Result), + TableStateUpdated(Result), Bid(Bid), Play(Card), } @@ -42,7 +42,9 @@ pub enum Msg { impl OnlineTableInner { fn play(&mut self, ctx: &yew::Context, 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, + 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! { + <> +
+ {center} +
+

{format!("This is table {}", ctx.props().table.id)}

+ + + } + } } impl Component for OnlineTableInner { @@ -106,35 +140,15 @@ impl Component for OnlineTableInner { } fn view(&self, ctx: &yew::Context) -> 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! { - <> -
- {center} -
-

{format!("This is table {}", ctx.props().table.id)}

- - + Some(TableStatePlayerView::Unknown) => html! { +

{"An error occurred."}

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

{"A beautiful result."}

+ }, } } } diff --git a/webapp/src/services.rs b/webapp/src/services.rs index 212363f..43b1698 100644 --- a/webapp/src/services.rs +++ b/webapp/src/services.rs @@ -1,12 +1,12 @@ use anyhow::Context; use gloo_net::http::Request; -use protocol::{bridge_engine::{GameStatePlayerView, Bid}, Table, card::Card}; +use protocol::{bridge_engine::{TableStatePlayerView, Bid}, Table, card::Card}; use crate::utils::ok_json; pub async fn get_table_player_view( table: Table, -) -> Result { +) -> Result { let response = Request::get(&format!("/api/table/{}", table.id)) .send() .await -- cgit v1.2.3