mahjong_core/game/
definition.rs

1use super::Players;
2use crate::{macros::derive_game_common, round::Round, Score, Table, TileId};
3use std::{
4    fmt::{Display, Formatter},
5    str::FromStr,
6};
7use ts_rs::TS;
8
9derive_game_common! {
10#[derive(PartialEq, Eq, TS, Copy)]
11pub enum GamePhase {
12    Beginning,
13    Charleston,
14    DecidingDealer,
15    End,
16    InitialDraw,
17    InitialShuffle,
18    Playing,
19    WaitingPlayers,
20}}
21
22derive_game_common! {
23#[derive(PartialEq, Eq, TS)]
24#[ts(export)]
25pub enum GameStyle {
26    HongKong,
27}}
28
29pub type GameId = String;
30pub type GameVersion = String;
31
32derive_game_common! {
33#[derive(TS)]
34#[ts(export)]
35pub struct Game {
36    pub id: GameId,
37    pub name: String,
38    pub phase: GamePhase,
39    pub players: Players,
40    pub round: Round,
41    pub score: Score,
42    pub table: Table,
43    pub version: GameVersion,
44    pub style: GameStyle,
45}}
46
47derive_game_common! {
48#[derive(PartialEq, Eq)]
49pub enum DrawTileResult {
50    AlreadyDrawn,
51    Bonus(TileId),
52    Normal(TileId),
53    WallExhausted,
54}}
55
56impl Game {
57    pub fn get_players_num(style: &GameStyle) -> usize {
58        match style {
59            GameStyle::HongKong => 4,
60        }
61    }
62}
63
64impl Display for GamePhase {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        match self {
67            Self::Beginning => write!(f, "Beginning"),
68            Self::Charleston => write!(f, "Charleston"),
69            Self::DecidingDealer => write!(f, "Deciding Dealer"),
70            Self::End => write!(f, "End"),
71            Self::InitialDraw => write!(f, "Initial Draw"),
72            Self::InitialShuffle => write!(f, "Initial Shuffle"),
73            Self::Playing => write!(f, "Playing"),
74            Self::WaitingPlayers => write!(f, "Waiting Players"),
75        }
76    }
77}
78
79const STYLE_HONG_KONG: &str = "Hong Kong";
80
81impl Display for GameStyle {
82    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83        match self {
84            Self::HongKong => f.write_str(STYLE_HONG_KONG),
85        }
86    }
87}
88
89impl FromStr for GameStyle {
90    type Err = ();
91
92    fn from_str(input: &str) -> Result<Self, Self::Err> {
93        match input {
94            STYLE_HONG_KONG => Ok(Self::HongKong),
95            _ => Err(()),
96        }
97    }
98}
99
100impl Default for GameStyle {
101    fn default() -> Self {
102        Self::HongKong
103    }
104}
105
106impl GameStyle {
107    pub fn tiles_after_claim(&self) -> usize {
108        match self {
109            Self::HongKong => 14,
110        }
111    }
112    pub fn max_consecutive_same_seats(&self) -> usize {
113        match self {
114            Self::HongKong => 3,
115        }
116    }
117}