diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 12:19:38 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 12:19:38 -0500 |
commit | 29dc8af0d9e01274eb1c520ea174e8a16f316562 (patch) | |
tree | 478037da70359321a1045181287594f910d325bf | |
parent | 8696dcc0afdc0896081405df13c887384500961b (diff) |
Insert new deals to db
-rw-r--r-- | server/src/table.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/server/src/table.rs b/server/src/table.rs index d2eff51..55fa8fb 100644 --- a/server/src/table.rs +++ b/server/src/table.rs @@ -6,7 +6,7 @@ use protocol::{ BiddingStatePlayerView, GameState, PlayStatePlayerView, TableState, }, card::Card, - simple_bots::{AlwaysPassBiddingBot, RandomPlayingBot}, + simple_bots::{AlwaysPassBiddingBot, RandomPlayingBot}, core::Deal, }; use rand::random; use sqlx::{PgPool, Postgres, Transaction}; @@ -117,6 +117,7 @@ pub async fn advance_play( pub struct DbTable { db: PgPool, + id: Uuid, pub inner: Box<dyn Table + Send>, } @@ -125,8 +126,8 @@ impl DbTable { let mut txn = db.begin().await?; if let Some(_) = sqlx::query!("select id from bridge_table where id = $1", id) - .fetch_optional(&mut txn) - .await? + .fetch_optional(&mut txn) + .await? { return Self::restore(db, txn, id).await; } @@ -138,6 +139,7 @@ impl DbTable { txn.commit().await?; Ok(Self { db, + id, inner: Box::new(InMemoryTable::new()), }) } @@ -150,6 +152,7 @@ impl DbTable { txn.rollback().await?; Ok(Self { db, + id, inner: Box::new(InMemoryTable::new()), }) } @@ -184,8 +187,17 @@ impl Table for DbTable { async fn new_deal( self: Box<Self>, ) -> Result<Box<dyn Table + Send>, BridgeError> { + let inner = self.inner.new_deal().await?; + + let deal: Deal = random(); + sqlx::query!(r#" + insert into table_boards (table_id, board_number, deal) + values ($1, $2, $3) + "#, self.id, 0, sqlx::types::Json(deal) as _) + .execute(&self.db).await?; + Ok(Box::new(Self { - inner: self.inner.new_deal().await?, + inner: inner, ..*self })) } |