mahjong_core/
lib.rs

1#![deny(clippy::use_self, clippy::shadow_unrelated)]
2pub use deck::Deck;
3pub use game::{Game, GameId, GamePhase, PlayerId, Players};
4pub use hand::{Hand, HandTile, SetId};
5pub use hand::{Hands, HandsMap};
6pub use score::{Score, ScoreItem, ScoreMap};
7use serde::{Deserialize, Serialize};
8use std::fmt::{self, Display, Formatter};
9use std::str::FromStr;
10pub use table::{Board, BonusTiles, DrawWall, DrawWallPlace, Table};
11pub use tile::{Tile, TileId};
12use ts_rs::TS;
13
14pub mod ai;
15pub mod deck;
16pub mod game;
17pub mod game_summary;
18pub mod hand;
19mod log;
20mod macros;
21pub mod meld;
22pub mod round;
23pub mod score;
24#[cfg(feature = "summary")]
25mod summary_view;
26mod table;
27#[cfg(test)]
28mod tests;
29pub mod tile;
30pub mod ui;
31
32#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, TS)]
33#[ts(export)]
34pub enum Suit {
35    Bamboo,
36    Characters,
37    Dots,
38}
39
40#[derive(Clone, Debug, Serialize, Deserialize, TS)]
41#[ts(export)]
42pub struct SuitTile {
43    pub id: TileId,
44    pub value: u32,
45    pub suit: Suit,
46}
47
48#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, TS)]
49#[ts(export)]
50pub enum Wind {
51    East,
52    North,
53    South,
54    West,
55}
56
57// Note that this order is reversed to the compass directions, since it is counter-clockwise
58pub const WINDS_ROUND_ORDER: &[Wind; 4] = &[Wind::East, Wind::South, Wind::West, Wind::North];
59pub const FLOWERS_ORDER: &[Flower] = &[
60    Flower::Plum,
61    Flower::Orchid,
62    Flower::Chrysanthemum,
63    Flower::Bamboo,
64];
65pub const SEASONS_ORDER: &[Season] = &[
66    Season::Spring,
67    Season::Summer,
68    Season::Autumn,
69    Season::Winter,
70];
71
72#[derive(Clone, Debug, Serialize, Deserialize, TS)]
73#[ts(export)]
74pub struct WindTile {
75    pub id: TileId,
76    pub value: Wind,
77}
78
79#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, TS)]
80#[ts(export)]
81pub enum Dragon {
82    Green,
83    Red,
84    White,
85}
86
87#[derive(Clone, Debug, Serialize, Deserialize, TS)]
88#[ts(export)]
89pub struct DragonTile {
90    pub id: TileId,
91    pub value: Dragon,
92}
93
94#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, TS)]
95#[ts(export)]
96pub enum Flower {
97    Bamboo,
98    Chrysanthemum,
99    Orchid,
100    Plum,
101}
102
103#[derive(Clone, Debug, Serialize, Deserialize, TS)]
104#[ts(export)]
105pub struct FlowerTile {
106    pub id: TileId,
107    pub value: Flower,
108}
109
110#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, TS)]
111#[ts(export)]
112pub enum Season {
113    Autumn,
114    Spring,
115    Summer,
116    Winter,
117}
118
119#[derive(Clone, Debug, Serialize, Deserialize, TS)]
120#[ts(export)]
121pub struct SeasonTile {
122    pub id: TileId,
123    pub value: Season,
124}
125
126impl FromStr for Wind {
127    type Err = ();
128
129    fn from_str(s: &str) -> Result<Self, Self::Err> {
130        match s {
131            "東" => Ok(Self::East),
132            "北" => Ok(Self::North),
133            "南" => Ok(Self::South),
134            "西" => Ok(Self::West),
135            _ => Err(()),
136        }
137    }
138}
139
140impl Display for Wind {
141    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
142        let wind_str = match self {
143            Self::East => "東",
144            Self::North => "北",
145            Self::South => "南",
146            Self::West => "西",
147        };
148        write!(f, "{}", wind_str)
149    }
150}