diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 11:00:58 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 11:00:58 -0500 |
commit | 7f7e45cc3be5ea065ffd69863c566accb8b45269 (patch) | |
tree | bcbcda503f1ab783d53e81aceaa7212f4b84a5e4 /server/src | |
parent | 33dfcd18252dd8a4845cd58b93bd177ab0dffde9 (diff) |
Add failing test for DbTable
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/table.rs | 61 |
1 files changed, 48 insertions, 13 deletions
diff --git a/server/src/table.rs b/server/src/table.rs index cc00327..7e49c7f 100644 --- a/server/src/table.rs +++ b/server/src/table.rs @@ -1,13 +1,15 @@ use async_trait::async_trait; use protocol::{ + actions::Bid, bot::{BiddingBot, PlayingBot}, bridge_engine::{ BiddingStatePlayerView, GameState, PlayStatePlayerView, TableState, }, card::Card, - simple_bots::{AlwaysPassBiddingBot, RandomPlayingBot}, actions::Bid, + simple_bots::{AlwaysPassBiddingBot, RandomPlayingBot}, }; use rand::random; +use sqlx::PgPool; use crate::error::BridgeError; @@ -17,12 +19,12 @@ pub trait Table { async fn bid( self: Box<Self>, bid: Bid, - ) -> Result<Box<dyn Table>, BridgeError>; + ) -> Result<Box<dyn Table + Send>, BridgeError>; async fn play( self: Box<Self>, card: Card, - ) -> Result<Box<dyn Table>, BridgeError>; - async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table>, BridgeError>; + ) -> Result<Box<dyn Table + Send>, BridgeError>; + async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError>; } pub struct InMemoryTable { @@ -46,7 +48,7 @@ impl Table for InMemoryTable { async fn bid( self: Box<Self>, bid: Bid, - ) -> Result<Box<dyn Table>, BridgeError> { + ) -> Result<Box<dyn Table + Send>, BridgeError> { let game = match self.state { TableState::Game(game) => game, _ => { @@ -60,7 +62,7 @@ impl Table for InMemoryTable { async fn play( self: Box<Self>, card: Card, - ) -> Result<Box<dyn Table>, BridgeError> { + ) -> Result<Box<dyn Table + Send>, BridgeError> { let game = match self.state { TableState::Game(game) => game, _ => { @@ -71,7 +73,7 @@ impl Table for InMemoryTable { Ok(Box::new(Self { state: game.into() })) } - async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table>, BridgeError> { + async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError> { Ok(Box::new(Self { state: GameState::new(random(), random()).into(), })) @@ -80,7 +82,7 @@ impl Table for InMemoryTable { pub async fn advance_play( table: Box<dyn Table>, -) -> Result<Box<dyn Table>, BridgeError> { +) -> Result<Box<dyn Table + Send>, BridgeError> { let game = match table.state() { TableState::Game(game) => game, _ => return Err(BridgeError::InvalidRequest("no game".to_string())), @@ -108,8 +110,41 @@ pub async fn advance_play( table } -// pub struct DbTable -// { -// db: PgPool, -// pub state: TableState, -// } +pub struct DbTable { + db: PgPool, + pub inner: Box<dyn Table + Send>, +} + +impl DbTable { + pub fn new(db: PgPool) -> Self { + Self { + db, + inner: Box::new(InMemoryTable::new()), + } + } +} + +#[async_trait] +impl Table for DbTable { + fn state(&self) -> &TableState { + self.inner.state() + } + + async fn bid( + self: Box<Self>, + bid: Bid, + ) -> Result<Box<dyn Table + Send>, BridgeError> { + Ok(Box::new(Self { inner: self.inner.bid(bid).await?, ..*self })) + } + + async fn play( + self: Box<Self>, + card: Card, + ) -> Result<Box<dyn Table + Send>, BridgeError> { + Ok(Box::new(Self { inner: self.inner.play(card).await?, ..*self })) + } + + async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError> { + Ok(Box::new(Self { inner: self.inner.new_deal().await?, ..*self })) + } +} |