diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-23 11:29:16 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-23 11:29:16 -0500 |
commit | 868703627bfd27925a53fcfbdd3dbeef831660c8 (patch) | |
tree | 50071b25b723ed5e61a96c67183c0413297c4775 | |
parent | 0308132c21003af01764b9129f39cbb08d8ec551 (diff) |
Add api endpoint for playing cards at a table
-rw-r--r-- | server/src/main.rs | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/server/src/main.rs b/server/src/main.rs index ab5cdcc..969bc25 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -9,7 +9,7 @@ use axum::{ routing::{delete, get, post}, Json, Router, }; -use protocol::bridge_engine::{Bid, GameStatePlayerView, Player}; +use protocol::{bridge_engine::{Bid, GameStatePlayerView, Player}, card::Card}; use protocol::{Table, UserInfo}; use server::ServerState; use tower_cookies::{Cookie, CookieManagerLayer, Cookies}; @@ -113,7 +113,7 @@ async fn main() { .route("/api/table", delete(leave_table)) .route("/api/table/:id", get(get_table_view)) .route("/api/table/:id/bid", post(post_bid)) - // .route("/api/user/table", get(user_table)) + .route("/api/table/:id/play", post(post_play)) .route("/api/login", get(login)) .route(auth::LOGIN_CALLBACK, get(login_callback)) .layer(CookieManagerLayer::new()) @@ -173,22 +173,23 @@ async fn post_bid( info!("Getting table state for {id:}"); let jnl = DbJournal::new(state.db.clone(), id); let mut table = play::Table::replay(jnl).await?; - if !table.game()?.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() != player_position { - return Err(BridgeError::InvalidRequest(format!( - "It is not {player_position:?} to play" - ))); - } table.bid(bid).await?; Ok(Json(())) } +async fn post_play( + _session: AuthenticatedSession, + State(state): ServerState, + Path(id): Path<Uuid>, + Json(card): Json<Card>, +) -> Result<Json<()>, BridgeError> { + info!("Getting table state for {id:}"); + let jnl = DbJournal::new(state.db.clone(), id); + let mut table = play::Table::replay(jnl).await?; + table.play(card).await?; + Ok(Json(())) +} + async fn leave_table( session: AuthenticatedSession, State(state): ServerState, |