diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-11-15 19:49:54 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-11-15 19:49:54 -0500 |
commit | b114fe7940e77090861ac9ba60f4d0b8caec8978 (patch) | |
tree | d5c7a1f2250a899719225642bf876a29316a30b0 /protocol/src | |
parent | 87ede1e2b367a997440626ad9f583600d7cc42fc (diff) |
Add journaling GameState
Diffstat (limited to 'protocol/src')
-rw-r--r-- | protocol/src/bridge_engine.rs | 60 | ||||
-rw-r--r-- | protocol/src/card.rs | 9 |
2 files changed, 54 insertions, 15 deletions
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<Card>, @@ -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<Trick>, @@ -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<Suit>, @@ -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<Bid>, @@ -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::*; diff --git a/protocol/src/card.rs b/protocol/src/card.rs index 621bae1..e27f29a 100644 --- a/protocol/src/card.rs +++ b/protocol/src/card.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; use anyhow::anyhow; use rand::prelude::SliceRandom; use rand::Rng; @@ -7,7 +8,7 @@ use strum::IntoEnumIterator; use strum_macros::EnumCount; use strum_macros::EnumIter; -#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter, EnumCount)] +#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter, EnumCount, Serialize, Deserialize)] pub enum Suit { Club, Diamond, @@ -15,7 +16,7 @@ pub enum Suit { Spade, } -#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter)] +#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter, Serialize, Deserialize)] pub enum Rank { Two = 2, Three, @@ -119,7 +120,7 @@ impl std::str::FromStr for Rank { } } -#[derive(PartialEq, Eq, Clone, Copy)] +#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub struct Card(pub Suit, pub Rank); impl fmt::Display for Card { @@ -240,7 +241,7 @@ mod tests { } } -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct Deal { pub north: Vec<Card>, pub west: Vec<Card>, |