web_lib/offscreen_game/
selecting_hand.rs

1use std::collections::HashSet;
2
3use mahjong_core::TileId;
4use wasm_bindgen::prelude::wasm_bindgen;
5
6use super::OffscreenGame;
7
8#[derive(Clone)]
9#[wasm_bindgen]
10pub struct SelectingHandTile {
11    pub(super) concealed: bool,
12    pub(super) id: TileId,
13    pub(super) set_id: Option<String>,
14}
15
16#[wasm_bindgen]
17impl SelectingHandTile {
18    #[wasm_bindgen(getter)]
19    pub fn id(&self) -> TileId {
20        self.id
21    }
22
23    #[wasm_bindgen(getter)]
24    pub fn concealed(&self) -> bool {
25        self.concealed
26    }
27
28    #[wasm_bindgen(getter)]
29    pub fn set_id(&self) -> Option<String> {
30        self.set_id.clone()
31    }
32
33    pub fn set_concealed(&mut self, concealed: bool, off_game: &mut OffscreenGame) {
34        off_game
35            .game
36            .table
37            .hands
38            .0
39            .iter_mut()
40            .for_each(|(_, hand)| {
41                hand.list.iter_mut().for_each(|t| {
42                    if t.id == self.id {
43                        t.concealed = concealed;
44                    }
45                });
46            });
47    }
48
49    pub fn set_set_id(&mut self, set_id: Option<String>, off_game: &mut OffscreenGame) {
50        off_game
51            .game
52            .table
53            .hands
54            .0
55            .iter_mut()
56            .for_each(|(_, hand)| {
57                hand.list.iter_mut().for_each(|t| {
58                    if t.id == self.id {
59                        t.set_id = set_id.clone();
60                    }
61                });
62            });
63    }
64}
65
66#[wasm_bindgen]
67pub struct SelectingHand {
68    pub(super) tiles: Vec<SelectingHandTile>,
69}
70
71#[wasm_bindgen]
72impl SelectingHand {
73    #[wasm_bindgen(getter)]
74    pub fn tiles(&self) -> Vec<SelectingHandTile> {
75        self.tiles.clone()
76    }
77
78    #[wasm_bindgen(getter)]
79    pub fn sets_ids(&self) -> Vec<String> {
80        let ids: HashSet<String> = self.tiles.iter().filter_map(|t| t.set_id.clone()).collect();
81
82        ids.into_iter().collect()
83    }
84}