1use lazy_static::lazy_static;
2use serde::{Deserialize, Serialize};
3use ts_rs::TS;
4
5use crate::{
6 game::Players, table::BonusTiles, Board, Dragon, DragonTile, DrawWall, Flower, FlowerTile,
7 Hand, Hands, HandsMap, Season, SeasonTile, Suit, SuitTile, Table, Tile, TileId, Wind, WindTile,
8};
9
10pub type DeckContent = Vec<Tile>;
11
12fn create_deck_content() -> DeckContent {
13 let suits_set = vec![Suit::Bamboo, Suit::Dots, Suit::Characters];
14 let winds_set = vec![Wind::East, Wind::North, Wind::South, Wind::West];
15 let dragons_set = vec![Dragon::Green, Dragon::Red, Dragon::White];
16 let seasons_set = vec![
17 Season::Spring,
18 Season::Summer,
19 Season::Autumn,
20 Season::Winter,
21 ];
22 let flowers_set = vec![
23 Flower::Bamboo,
24 Flower::Chrysanthemum,
25 Flower::Orchid,
26 Flower::Plum,
27 ];
28
29 let mut deck_list: Vec<Tile> = vec![];
30
31 for flower in flowers_set {
32 deck_list.push(Tile::Flower(FlowerTile {
33 id: 0,
34 value: flower,
35 }));
36 }
37
38 for season in seasons_set {
39 deck_list.push(Tile::Season(SeasonTile {
40 id: 0,
41 value: season,
42 }));
43 }
44
45 for _ in 0..4 {
46 for suit in suits_set.clone() {
47 for value in 1..10 {
48 let suit_tile = SuitTile { id: 0, value, suit };
49 let tile = Tile::Suit(suit_tile);
50
51 deck_list.push(tile);
52 }
53 }
54
55 for wind in winds_set.clone() {
56 let wind_tile = WindTile { id: 0, value: wind };
57 let tile = Tile::Wind(wind_tile);
58
59 deck_list.push(tile);
60 }
61
62 for dragon in dragons_set.clone() {
63 let dragon_tile = DragonTile {
64 id: 0,
65 value: dragon,
66 };
67 let tile = Tile::Dragon(dragon_tile);
68
69 deck_list.push(tile);
70 }
71 }
72
73 let mut deck: DeckContent = vec![];
74
75 deck_list.iter().enumerate().for_each(|(index, tile)| {
76 let mut tile = tile.clone();
77 tile.set_id(index);
78 deck.push(tile);
79 });
80
81 deck
82}
83
84lazy_static! {
85 pub static ref DEFAULT_DECK: Deck = Deck(create_deck_content());
86}
87
88#[derive(Clone, Debug, Serialize, Deserialize, TS)]
89#[ts(export)]
90pub struct Deck(pub DeckContent);
91
92impl Deck {
93 pub fn find_tile_without_id(tile: Tile) -> Tile {
94 let all_tiles = DEFAULT_DECK.0.clone();
95
96 all_tiles
97 .iter()
98 .find(|t| tile.is_same_content(t))
99 .unwrap()
100 .clone()
101 }
102
103 pub fn create_table(&self, players: &Players) -> Table {
104 let Self(deck_content) = self;
105 let mut ids: Vec<usize> = vec![];
106 for id in 0..deck_content.len() {
107 ids.push(id);
108 }
109 let draw_wall = DrawWall::new(ids);
110
111 let hands_map = players
112 .iter()
113 .map(|player| {
114 let hand = Hand::default();
115 (player.clone(), hand)
116 })
117 .collect::<HandsMap>();
118
119 let bonus_tiles = BonusTiles::default();
120
121 Table {
122 board: Board::default(),
123 draw_wall,
124 bonus_tiles,
125 hands: Hands(hands_map),
126 }
127 }
128
129 pub fn get_sure(&self, id: TileId) -> &Tile {
130 &self.0[id]
131 }
132}