From c2145b91775be375779884a2a97365396923aba1 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Thu, 22 Dec 2022 11:36:48 -0500 Subject: Add typed journals --- server/src/main.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'server/src/main.rs') diff --git a/server/src/main.rs b/server/src/main.rs index 5b81fc0..35019c5 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -153,11 +153,20 @@ async fn get_table_view( let jnl = DbJournal::new(extension.db.clone(), id); let mut table = play::Table::new_or_replay(jnl).await?; info!("Advancing play"); - while table.game().current_player() != Some(player_position) { + while table + .game() + .ok_or(BridgeError::InvalidRequest( + "game not in progress".to_string(), + ))? + .current_player() + != player_position + { advance_play(&mut table).await?; } let response = Json(GameStatePlayerView::from_game_state( - table.game(), + table.game().ok_or(BridgeError::InvalidRequest( + "game not in progress".to_string(), + ))?, player_position, )); info!("Response: {response:#?}"); @@ -173,14 +182,27 @@ async fn post_bid( info!("Getting table state for {id:}"); let jnl = DbJournal::new(extension.db.clone(), id); let mut table = play::Table::replay(jnl).await?; - if !table.game().is_bidding() { + if !table + .game() + .ok_or(BridgeError::InvalidRequest( + "game not in progress".to_string(), + ))? + .is_bidding() + { return Err(BridgeError::InvalidRequest( "Posting a bid requires that the game is in the bidding phase" .to_string(), )); } let player_position = Player::South; - if table.game().current_player() != Some(player_position) { + if table + .game() + .ok_or(BridgeError::InvalidRequest( + "game not in progress".to_string(), + ))? + .current_player() + != player_position + { return Err(BridgeError::InvalidRequest(format!( "It is not {player_position:?} to play" ))); -- cgit v1.2.3