mahjong_core/
meld.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::{
    deck::DEFAULT_DECK, round::TileClaimed, HandTile, PlayerId, SetId, Suit, SuitTile, Tile, TileId,
};
use serde::{Deserialize, Serialize};
use ts_rs::TS;

pub type PlayerDiff = Option<i32>;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TS)]
#[ts(export)]
pub enum MeldType {
    Chow,
    Kong,
    Pair,
    Pung,
}

impl MeldType {
    pub fn from_tiles(tiles: &[TileId]) -> Option<Self> {
        if tiles.len() < 2 || tiles.len() > 4 {
            return None;
        }

        let tiles = tiles
            .iter()
            .map(|t| &DEFAULT_DECK.0[*t])
            .collect::<Vec<&Tile>>();

        let opts = SetCheckOpts {
            board_tile_player_diff: None,
            claimed_tile: None,
            sub_hand: &tiles,
        };

        if get_is_pung(&opts) {
            return Some(Self::Pung);
        }

        if get_is_chow(&opts) {
            return Some(Self::Chow);
        }

        if get_is_kong(&opts) {
            return Some(Self::Kong);
        }

        if get_is_pair(opts.sub_hand) {
            return Some(Self::Pair);
        }

        None
    }
}

#[derive(Debug, Clone)]
pub struct SetCheckOpts<'a> {
    pub board_tile_player_diff: PlayerDiff,
    pub claimed_tile: Option<TileId>,
    pub sub_hand: &'a [&'a Tile],
}

pub fn get_is_pung(opts: &SetCheckOpts) -> bool {
    if opts.sub_hand.len() != 3 {
        return false;
    }

    let mut last_tile = opts.sub_hand[0];
    if last_tile.is_bonus() {
        return false;
    }

    for tile_index in 1..3 {
        let tile = opts.sub_hand[tile_index];

        if !tile.is_same_content(last_tile) {
            return false;
        }

        last_tile = tile;
    }

    true
}

// This approach is used for performance
const DUMMY_SUIT: SuitTile = SuitTile {
    id: 0,
    value: 0,
    suit: crate::Suit::Dots,
};

pub fn get_is_chow(opts: &SetCheckOpts) -> bool {
    if opts.sub_hand.len() != 3 {
        return false;
    };

    if let Some(board_tile_player_diff) = opts.board_tile_player_diff {
        if let Some(claimed_tile) = opts.claimed_tile {
            if board_tile_player_diff != 1 {
                let has_same_claimed_tile =
                    opts.sub_hand.iter().any(|t| t.get_id() == claimed_tile);

                if has_same_claimed_tile {
                    return false;
                }
            }
        }
    }

    let mut suit_tiles: [&SuitTile; 3] = [&DUMMY_SUIT, &DUMMY_SUIT, &DUMMY_SUIT];
    let mut suit: Option<Suit> = None;

    for (idx, tile) in opts.sub_hand.iter().enumerate() {
        match tile {
            Tile::Suit(suit_tile) => {
                if suit.is_some() {
                    if Some(suit_tile.suit) != suit {
                        return false;
                    }
                } else {
                    suit = Some(suit_tile.suit);
                }
                suit_tiles[idx] = suit_tile
            }
            _ => {
                return false;
            }
        }
    }

    suit_tiles.sort_by(|a, b| a.value.cmp(&b.value));

    let mut last_tile = suit_tiles[0];

    for tile in suit_tiles.iter().skip(1).take(2) {
        if last_tile.value + 1 != tile.value {
            return false;
        }

        last_tile = tile;
    }

    true
}

pub fn get_is_kong(opts: &SetCheckOpts) -> bool {
    if opts.sub_hand.len() != 4 {
        return false;
    }

    let mut last_tile = opts.sub_hand[0];
    if last_tile.is_bonus() {
        return false;
    }

    for tile_index in 1..4 {
        let tile = opts.sub_hand[tile_index];

        if !tile.is_same_content(last_tile) {
            return false;
        }

        last_tile = tile;
    }

    true
}

pub fn get_tile_claimed_id_for_user(
    player_id: &PlayerId,
    tile_claimed: &TileClaimed,
) -> Option<TileId> {
    if tile_claimed.is_none() {
        return None;
    }

    let tile_claimed = tile_claimed.clone().unwrap();

    tile_claimed.clone().by?;

    if tile_claimed.by.unwrap() == *player_id {
        return Some(tile_claimed.id);
    }

    None
}

pub struct RemoveMeldOpts {
    hand: Vec<HandTile>,
    set_id: SetId,
}

pub fn remove_meld(opts: RemoveMeldOpts) {
    let mut meld_tiles = opts
        .hand
        .iter()
        .filter(|h| h.set_id == opts.set_id)
        .cloned()
        .collect::<Vec<HandTile>>();

    for meld_tile in meld_tiles.clone() {
        if !meld_tile.concealed {
            return;
        }
    }

    meld_tiles.iter_mut().for_each(|t| {
        t.set_id = None;
    });
}

pub fn get_is_pair(hand: &[&Tile]) -> bool {
    if hand.len() != 2 {
        return false;
    }

    hand[0].is_same_content(hand[1])
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TS)]
#[ts(export)]
pub struct PossibleMeld {
    pub discard_tile: Option<TileId>,
    pub is_concealed: bool,
    pub is_mahjong: bool,
    pub is_upgrade: bool,
    pub player_id: PlayerId,
    pub tiles: Vec<TileId>,
}

impl PossibleMeld {
    pub fn sort_tiles(&mut self) {
        self.tiles.sort_by(|a, b| {
            let tile_a = &DEFAULT_DECK.0[*a];
            let tile_b = &DEFAULT_DECK.0[*b];

            tile_a.cmp_custom(tile_b)
        });
    }
}