From b114fe7940e77090861ac9ba60f4d0b8caec8978 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Tue, 15 Nov 2022 19:49:54 -0500 Subject: Add journaling GameState --- protocol/src/bridge_engine.rs | 60 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 11 deletions(-) (limited to 'protocol/src/bridge_engine.rs') diff --git a/protocol/src/bridge_engine.rs b/protocol/src/bridge_engine.rs index a914efe..e9c09f6 100644 --- a/protocol/src/bridge_engine.rs +++ b/protocol/src/bridge_engine.rs @@ -1,4 +1,5 @@ -use crate::card::{Card, Deal, Suit}; +use crate::card::{Card, Deal, Suit, RankOrder}; +use serde::{Deserialize, Serialize}; use anyhow::{anyhow, bail}; use log::{error, info}; use regex::Regex; @@ -8,7 +9,9 @@ use std::str::FromStr; use strum::{EnumCount, IntoEnumIterator}; use strum_macros::{EnumCount as EnumCountMacro, EnumIter, FromRepr}; -#[derive(PartialEq, Eq, Clone, Copy, Debug, FromRepr, EnumCountMacro)] +pub const SUIT_DISPLAY_ORDER: [Suit; 4] = [Suit::Diamond, Suit::Club, Suit::Heart, Suit::Spade]; + +#[derive(PartialEq, Eq, Clone, Copy, Debug, FromRepr, EnumCountMacro, Serialize, Deserialize)] #[repr(u8)] pub enum Player { West = 0, @@ -45,7 +48,7 @@ impl Player { } } -#[derive(PartialEq, Eq, Debug, Clone)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct Trick { pub leader: Player, pub cards_played: Vec, @@ -58,7 +61,7 @@ impl Trick { } } -#[derive(PartialEq, Eq, Debug, Clone)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct TurnInPlay { trick: Trick, } @@ -108,7 +111,7 @@ impl TurnInPlay { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct DealInPlay { deal: Deal, tricks_played: Vec, @@ -175,7 +178,7 @@ impl DealInPlay { } } -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, EnumIter)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, EnumIter, Serialize, Deserialize)] pub enum ContractLevel { One = 1, Two, @@ -215,7 +218,7 @@ impl FromStr for ContractLevel { } } -#[derive(PartialEq, Eq, Clone, Copy)] +#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub enum Bid { Pass, Double, @@ -266,7 +269,7 @@ impl FromStr for Bid { } } -#[derive(PartialEq, Eq, Clone, Copy)] +#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub struct Raise { pub level: ContractLevel, pub suit: Option, @@ -347,7 +350,7 @@ impl FromStr for Raise { } } -#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)] pub enum ContractModifier { None, Doubled, @@ -364,7 +367,7 @@ impl fmt::Display for ContractModifier { } } -#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)] pub struct Contract { declarer: Player, highest_bid: Raise, @@ -383,7 +386,7 @@ impl fmt::Display for Contract { } } -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct Bidding { pub dealer: Player, pub bids: Vec, @@ -469,6 +472,41 @@ impl BiddingResult { } } +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub enum GameState { + Bidding { + dealer: Player, + deal: Deal, + }, + PassedOut { + dealer: Player, + deal: Deal, + bidding: Bidding, + }, + Play { + playing_deal: DealInPlay, + contract: Contract, + bidding: Bidding, + }, +} + +impl GameState { + pub fn deal(&self) -> &Deal { + match self { + Self::Bidding { deal, .. } => deal, + Self::PassedOut { deal, .. } => deal, + Self::Play { playing_deal, .. } => &playing_deal.deal(), + } + } +} + +pub fn deal() -> Deal { + let mut rng = rand::thread_rng(); + let mut deal = crate::card::deal(&mut rng); + deal.sort(&SUIT_DISPLAY_ORDER, RankOrder::Descending); + deal +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3