diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-22 19:09:17 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-22 19:09:17 -0500 |
commit | 58aa2e4764f90528270ae5b9a61de576260e5483 (patch) | |
tree | b8b9ce071637fa66335108e7430cce849f872933 /server | |
parent | a60500b47c81d15c9b970b58b1c871821dbe934a (diff) |
Fixes to the engine enough that the random bot can finish a game
Diffstat (limited to 'server')
-rw-r--r-- | server/Cargo.toml | 1 | ||||
-rw-r--r-- | server/src/main.rs | 10 | ||||
-rw-r--r-- | server/src/play.rs | 48 |
3 files changed, 48 insertions, 11 deletions
diff --git a/server/Cargo.toml b/server/Cargo.toml index 9d5f1a8..8d8407c 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -31,3 +31,4 @@ cookie = "0.16.1" time = "0.1.44" async-trait = "0.1.57" rand = "0.8.4" +env_logger = "0.10.0" diff --git a/server/src/main.rs b/server/src/main.rs index 735258f..03ae3e3 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -296,3 +296,13 @@ async fn login(cookies: Cookies, extension: ContextExtension) -> Redirect { cookies.add(cookie); Redirect::temporary(auth_url.as_str()) } + +#[cfg(test)] +mod tests { + use env_logger::Env; + + pub fn test_setup() { + dotenv::dotenv().ok(); + let _ =env_logger::Builder::from_env(Env::default().default_filter_or("info")).is_test(true).try_init(); + } +} diff --git a/server/src/play.rs b/server/src/play.rs index 974ec58..ffb7407 100644 --- a/server/src/play.rs +++ b/server/src/play.rs @@ -1,5 +1,3 @@ -use std::mem; - use async_trait::async_trait; use protocol::{ bot::{BiddingBot, PlayingBot}, @@ -178,6 +176,15 @@ impl<J: Journal<TableUpdate>> Table<J> { } } + pub fn result(&self) -> Result<&GameResult, BridgeError> { + match &self.state { + TableState::Result(r) => Ok(r), + _ => Err(BridgeError::InvalidRequest( + "no result".to_string(), + )), + } + } + async fn init(&mut self) -> Result<(), BridgeError> { self.insert_and_apply(TableUpdate::NewDeal { deal: random(), @@ -219,7 +226,10 @@ impl<J: Journal<TableUpdate>> Table<J> { self.state = GameState::new(deal, dealer).into(); Ok(()) } - TableUpdate::Play(_) => todo!(), + TableUpdate::Play(card) => { + self.state = self.game()?.clone().play(card)?.into(); + Ok(()) + }, } } @@ -246,7 +256,7 @@ impl<J: Journal<TableUpdate>> Table<J> { Ok(()) } - pub async fn new_or_replay(mut journal: J) -> Result<Self, BridgeError> { + pub async fn new_or_replay(journal: J) -> Result<Self, BridgeError> { let mut table = Self { journal, state: Default::default(), @@ -295,9 +305,11 @@ pub async fn advance_play<J: Journal<TableUpdate>>( #[cfg(test)] mod test { - use std::cell::RefCell; - + use protocol::{bridge_engine::{Raise, ContractLevel}, card::Suit}; use serde_json::json; + use tracing::info; + + use crate::tests::test_setup; use super::*; @@ -313,10 +325,6 @@ mod test { seq: -1, } } - - pub fn reset(&mut self) { - self.seq = -1; - } } #[async_trait] @@ -380,10 +388,28 @@ mod test { // } #[tokio::test] - async fn test_advance_play() { + async fn test_advance_play_once() { let mut t1 = Table::new(TestJournal::new()).await.unwrap(); let player = t1.game().unwrap().current_player(); advance_play(&mut t1).await.unwrap(); assert_ne!(player, t1.game().unwrap().current_player()); } + + #[tokio::test] + async fn test_advance_play_until_result() { + test_setup(); + let mut t1 = Table::new(TestJournal::new()).await.unwrap(); + assert!(t1.game().is_ok()); + let raise1c = Raise { + level: ContractLevel::One, + suit: Some(Suit::Club), + }; + // Make sure the game doesn't get passed out. + t1.bid(Bid::Raise(raise1c)).await.unwrap(); + while t1.game().is_ok() { + info!("Game is: {:#?}", t1.game()); + advance_play(&mut t1).await.unwrap(); + } + assert!(t1.result().is_ok()); + } } |