diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2023-01-07 16:33:46 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2023-01-07 16:33:46 -0500 |
commit | 879276a057d4805014fef0218a4cbafdd2349b64 (patch) | |
tree | 6a9ed26672b3a8a6007b6a7fc3b098593e7eafd1 /server/src/table.rs | |
parent | 1f52e2e448b464e95530cab9e1b7d9177ada3279 (diff) |
Insert boards in the db using correct keys
Diffstat (limited to 'server/src/table.rs')
-rw-r--r-- | server/src/table.rs | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/server/src/table.rs b/server/src/table.rs index 1dc0fb7..370e2e1 100644 --- a/server/src/table.rs +++ b/server/src/table.rs @@ -16,7 +16,9 @@ use crate::error::BridgeError; #[async_trait] pub trait Table { + fn board_number(&self) -> usize; fn state(&self) -> &TableState; + async fn bid( self: Box<Self>, bid: Bid, @@ -25,19 +27,23 @@ pub trait Table { self: Box<Self>, card: Card, ) -> Result<Box<Self>, BridgeError>; - async fn new_deal( + async fn set_board( self: Box<Self>, + board_number: usize, + deal: Deal, ) -> Result<Box<Self>, BridgeError>; } pub struct InMemoryTable { pub state: TableState, + board_number: usize, } impl InMemoryTable { pub fn new() -> Self { Self { state: TableState::Unknown, + board_number: 0, } } } @@ -48,6 +54,10 @@ impl Table for InMemoryTable { &self.state } + fn board_number(&self) -> usize { + self.board_number + } + async fn bid( self: Box<Self>, bid: Bid, @@ -59,7 +69,7 @@ impl Table for InMemoryTable { } }; let game = game.bid(bid)?; - Ok(Box::new(Self { state: game.into() })) + Ok(Box::new(Self { state: game.into(), ..*self })) } async fn play( @@ -73,14 +83,17 @@ impl Table for InMemoryTable { } }; let game = game.play(card)?; - Ok(Box::new(Self { state: game.into() })) + Ok(Box::new(Self { state: game.into(), ..*self })) } - async fn new_deal( + async fn set_board( self: Box<Self>, + board_number: usize, + deal: Deal, ) -> Result<Box<Self>, BridgeError> { Ok(Box::new(Self { - state: GameState::new(random(), random()).into(), + state: GameState::new(deal).into(), + board_number, })) } } @@ -165,6 +178,10 @@ impl<Inner: Table + Send> Table for DbTable<Inner> { self.inner.state() } + fn board_number(&self) -> usize { + self.inner.board_number() + } + async fn bid( self: Box<Self>, bid: Bid, @@ -185,16 +202,18 @@ impl<Inner: Table + Send> Table for DbTable<Inner> { })) } - async fn new_deal( + async fn set_board( self: Box<Self>, + board_number: usize, + deal: Deal, ) -> Result<Box<Self>, BridgeError> { - let inner = self.inner.new_deal().await?; + let inner = self.inner.set_board(board_number, 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 _) + "#, self.id, board_number as i64, sqlx::types::Json(deal) as _) .execute(&self.db).await?; Ok(Box::new(Self { |