service_contracts/
lib.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#![deny(clippy::use_self, clippy::shadow_unrelated)]
use std::fmt::{self, Display};
use ts_rs::TS;

use mahjong_core::{
    deck::DeckContent, game::GameVersion, game_summary::GameSummary, hand::SetIdContent, Game,
    GameId, Hand, Hands, PlayerId, TileId,
};
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
pub use service_player::{ServicePlayer, ServicePlayerGame, ServicePlayerSummary};

mod service_player;

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct GameSettings {
    pub ai_enabled: bool,
    pub auto_sort_players: FxHashSet<PlayerId>,
    pub auto_stop_claim_meld: FxHashSet<PlayerId>,
    pub dead_wall: bool,
    pub discard_wait_ms: Option<i32>,
    pub fixed_settings: bool,
    pub last_discard_time: i128,
}

impl Default for GameSettings {
    fn default() -> Self {
        Self {
            ai_enabled: true,
            auto_sort_players: FxHashSet::default(),
            auto_stop_claim_meld: FxHashSet::default(),
            dead_wall: false,
            discard_wait_ms: Some(1000),
            fixed_settings: false,
            last_discard_time: 0,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct ServiceGame {
    pub created_at: i64,
    pub game: Game,
    pub players: FxHashMap<PlayerId, ServicePlayer>,
    pub settings: GameSettings,
    pub updated_at: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct GameSettingsSummary {
    pub ai_enabled: bool,
    pub auto_sort: bool,
    pub auto_stop_claim_meld: bool,
    pub dead_wall: bool,
    pub discard_wait_ms: Option<i32>,
    pub fixed_settings: bool,
    pub last_discard_time: String,
}

impl GameSettingsSummary {
    pub fn from_game_settings(settings: &GameSettings, player_id: &PlayerId) -> Self {
        Self {
            ai_enabled: settings.ai_enabled,
            auto_sort: settings.auto_sort_players.iter().any(|p| p == player_id),
            auto_stop_claim_meld: settings.auto_stop_claim_meld.iter().any(|p| p == player_id),
            dead_wall: settings.dead_wall,
            discard_wait_ms: settings.discard_wait_ms,
            fixed_settings: settings.fixed_settings,
            last_discard_time: settings.last_discard_time.to_string(),
        }
    }

    pub fn to_game_settings(&self, player_id: &PlayerId, settings: &GameSettings) -> GameSettings {
        let mut new_settings = settings.clone();

        if self.auto_sort {
            new_settings.auto_sort_players.insert(player_id.clone());
        } else {
            new_settings.auto_sort_players.remove(player_id);
        }

        if self.auto_stop_claim_meld {
            new_settings.auto_stop_claim_meld.insert(player_id.clone());
        } else {
            new_settings.auto_stop_claim_meld.remove(player_id);
        }

        new_settings.ai_enabled = self.ai_enabled;
        new_settings.discard_wait_ms = self.discard_wait_ms;
        new_settings.fixed_settings = self.fixed_settings;
        new_settings.last_discard_time = self.last_discard_time.parse().unwrap_or(0);

        new_settings
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct ServiceGameSummary {
    pub game_summary: GameSummary,
    pub players: FxHashMap<PlayerId, ServicePlayerSummary>,
    pub settings: GameSettingsSummary,
}

impl ServiceGame {
    pub fn get_ai_players(&self) -> FxHashSet<PlayerId> {
        self.players
            .iter()
            .filter(|(_, player)| player.is_ai)
            .map(|(id, _)| id.clone())
            .collect::<FxHashSet<PlayerId>>()
    }
}

impl ServiceGameSummary {
    pub fn from_service_game(game: &ServiceGame, player_id: &PlayerId) -> Option<Self> {
        let game_summary = GameSummary::from_game(&game.game, player_id);

        game_summary.as_ref()?;

        let game_summary = game_summary.unwrap();

        let players: FxHashMap<PlayerId, ServicePlayerSummary> = game
            .players
            .clone()
            .into_iter()
            .map(|(id, player)| {
                (
                    id,
                    ServicePlayerSummary {
                        id: player.id,
                        name: player.name,
                    },
                )
            })
            .collect();

        Some(Self {
            game_summary,
            players,
            settings: GameSettingsSummary::from_game_settings(&game.settings, player_id),
        })
    }

    pub fn get_turn_player(&self) -> Option<ServicePlayerSummary> {
        let player_id = self.game_summary.players.0[self.game_summary.round.player_index].clone();

        self.players.get(&player_id).cloned()
    }

    pub fn get_dealer_player(&self) -> Option<ServicePlayerSummary> {
        let player_id =
            self.game_summary.players.0[self.game_summary.round.dealer_player_index].clone();

        self.players.get(&player_id).cloned()
    }
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SocketMessage {
    GameUpdate(ServiceGame),
    GameSummaryUpdate(ServiceGameSummary),
    ListRooms,
    Name(String),
    PlayerLeft,
    PlayerJoined,
}

#[derive(Serialize, Deserialize, TS)]
#[ts(export)]
pub struct WebSocketQuery {
    pub game_id: GameId,
    pub player_id: Option<PlayerId>,
    pub token: String,
}

pub type AdminGetGamesResponse = Vec<ServicePlayerGame>;

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
#[serde(tag = "type")]
pub enum Queries {
    UserBreakMeld {
        game_id: GameId,
        player_id: PlayerId,
        set_id: SetIdContent,
    },
    UserCreateGame {
        ai_player_names: Option<Vec<String>>,
        auto_sort_own: Option<bool>,
        dead_wall: Option<bool>,
        player_id: PlayerId,
    },
    UserCreateMeld {
        game_id: GameId,
        is_concealed: bool,
        is_upgrade: bool,
        player_id: PlayerId,
        tiles: FxHashSet<TileId>,
    },
    UserDiscardTile {
        game_id: GameId,
        tile_id: TileId,
    },
    UserDrawTile {
        game_id: GameId,
        game_version: GameVersion,
        player_id: PlayerId,
    },
    UserGetDashboard,
    UserMovePlayer {
        game_id: GameId,
        player_id: PlayerId,
    },
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserGetDashboardResponse {
    pub auth_info: AuthInfoSummary,
    pub player: DashboardPlayer,
    pub player_games: Vec<DashboardGame>,
    pub player_total_score: i32,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
#[serde(tag = "type")]
pub enum QueriesResponses {
    UserBreakMeld { game: ServiceGameSummary },
    UserCreateGame { game: ServiceGameSummary },
    UserCreateMeld { game: ServiceGameSummary },
    UserDiscardTile { game: ServiceGameSummary },
    UserDrawTile { game: ServiceGameSummary },
    UserGetDashboard { dashboard: UserGetDashboardResponse },
    UserMovePlayer { game: ServiceGameSummary },
}

pub type AdminPostDrawTileResponse = Hand;

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct AdminPostCreateMeldRequest {
    pub is_concealed: bool,
    pub is_upgrade: bool,
    pub player_id: String,
    pub tiles: FxHashSet<TileId>,
}
pub type AdminPostCreateMeldResponse = Hand;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdminPostBreakMeldRequest {
    pub player_id: String,
    pub set_id: SetIdContent,
}
pub type AdminPostBreakMeldResponse = Hand;

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct AdminPostDiscardTileRequest {
    pub tile_id: TileId,
}
pub type AdminPostDiscardTileResponse = ServiceGame;

pub type AdminPostMovePlayerRequest = ();

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct AdminPostMovePlayerResponse(pub ServiceGame);

pub type AdminPostSortHandsRequest = ();

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct AdminPostSortHandsResponse(pub Hands);

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct AdminPostClaimTileRequest {
    pub player_id: PlayerId,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct AdminPostClaimTileResponse(pub ServiceGame);

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserLoadGameQuery {
    pub player_id: PlayerId,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct UserGetLoadGameResponse(pub ServiceGameSummary);

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSortHandRequest {
    pub game_version: GameVersion,
    pub player_id: PlayerId,
    pub tiles: Option<Vec<TileId>>,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSortHandResponse(pub ServiceGameSummary);

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct AdminPostSayMahjongRequest {
    pub player_id: PlayerId,
}
pub type AdminPostSayMahjongResponse = ServiceGame;

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct AdminPostAIContinueRequest {
    pub draw: Option<bool>,
}
#[derive(Deserialize, Serialize)]
pub struct AdminPostAIContinueResponse {
    pub service_game: ServiceGame,
    pub changed: bool,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostAIContinueRequest {
    pub player_id: PlayerId,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostAIContinueResponse {
    pub service_game_summary: ServiceGameSummary,
    pub changed: bool,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostClaimTileRequest {
    pub player_id: PlayerId,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostClaimTileResponse(pub ServiceGameSummary);

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSayMahjongRequest {
    pub player_id: PlayerId,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSayMahjongResponse(pub ServiceGameSummary);

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSetGameSettingsRequest {
    pub player_id: PlayerId,
    pub settings: GameSettingsSummary,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSetGameSettingsResponse(pub ServiceGameSummary);

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostJoinGameResponse(pub PlayerId);

#[derive(Deserialize, Serialize, Debug, TS)]
#[ts(export)]
pub struct UserPostSetAuthRequest {
    pub username: String,
    pub password: String,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSetAuthResponse {
    pub token: String,
}

#[derive(Deserialize, Serialize, Debug, TS)]
#[ts(export)]
pub struct UserPostSetAuthAnonRequest {
    pub id_token: String,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostSetAuthAnonResponse {
    pub token: String,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostPassRoundRequest {
    pub player_id: PlayerId,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPostPassRoundResponse(pub ServiceGameSummary);

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserGetInfoResponse {
    pub name: String,
    pub total_score: i32,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct DashboardPlayer {
    pub id: String,
    pub name: String,
    pub created_at: String,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct DashboardGame {
    pub id: String,
    pub created_at: String,
    pub updated_at: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, TS)]
#[ts(export)]
pub enum AuthProvider {
    Anonymous,
    Email,
    Github,
}

#[derive(Deserialize, Serialize, Clone, TS)]
#[ts(export)]
pub struct AuthInfoSummary {
    pub provider: AuthProvider,
    pub username: Option<String>,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPatchInfoRequest {
    pub name: String,
}
#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct UserPatchInfoResponse {
    pub name: String,
    pub total_score: i32,
}

#[derive(Deserialize, Serialize, TS)]
#[ts(export)]
pub struct GetDeckResponse(pub DeckContent);

impl Display for AuthProvider {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let result = match self {
            Self::Anonymous => "anonymous".to_string(),
            Self::Email => "email".to_string(),
            Self::Github => "github".to_string(),
        };

        write!(f, "{}", result)
    }
}