web_lib/offscreen_game/
selecting_hand.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
use std::collections::HashSet;

use mahjong_core::TileId;
use wasm_bindgen::prelude::wasm_bindgen;

use super::OffscreenGame;

#[derive(Clone)]
#[wasm_bindgen]
pub struct SelectingHandTile {
    pub(super) concealed: bool,
    pub(super) id: TileId,
    pub(super) set_id: Option<String>,
}

#[wasm_bindgen]
impl SelectingHandTile {
    #[wasm_bindgen(getter)]
    pub fn id(&self) -> TileId {
        self.id
    }

    #[wasm_bindgen(getter)]
    pub fn concealed(&self) -> bool {
        self.concealed
    }

    #[wasm_bindgen(getter)]
    pub fn set_id(&self) -> Option<String> {
        self.set_id.clone()
    }

    pub fn set_concealed(&mut self, concealed: bool, off_game: &mut OffscreenGame) {
        off_game
            .game
            .table
            .hands
            .0
            .iter_mut()
            .for_each(|(_, hand)| {
                hand.list.iter_mut().for_each(|t| {
                    if t.id == self.id {
                        t.concealed = concealed;
                    }
                });
            });
    }

    pub fn set_set_id(&mut self, set_id: Option<String>, off_game: &mut OffscreenGame) {
        off_game
            .game
            .table
            .hands
            .0
            .iter_mut()
            .for_each(|(_, hand)| {
                hand.list.iter_mut().for_each(|t| {
                    if t.id == self.id {
                        t.set_id = set_id.clone();
                    }
                });
            });
    }
}

#[wasm_bindgen]
pub struct SelectingHand {
    pub(super) tiles: Vec<SelectingHandTile>,
}

#[wasm_bindgen]
impl SelectingHand {
    #[wasm_bindgen(getter)]
    pub fn tiles(&self) -> Vec<SelectingHandTile> {
        self.tiles.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn sets_ids(&self) -> Vec<String> {
        let ids: HashSet<String> = self.tiles.iter().filter_map(|t| t.set_id.clone()).collect();

        ids.into_iter().collect()
    }
}