mahjong_service/
game_wrapper.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
use crate::{
    ai_wrapper::AIWrapper,
    http_server::{DataSocketServer, DataStorage},
    service_error::{ResponseCommon, ServiceError},
    socket::{MahjongWebsocketSession, SocketClientMessage},
    time::get_timestamp,
};
use actix_web::{web, HttpResponse};
use mahjong_core::{
    game::{DrawTileResult, GameVersion},
    hand::SetIdContent,
    Game, GamePhase, PlayerId, Players, TileId,
};
use rustc_hash::{FxHashMap, FxHashSet};
use service_contracts::{
    AdminPostAIContinueRequest, AdminPostAIContinueResponse, AdminPostBreakMeldRequest,
    AdminPostBreakMeldResponse, AdminPostClaimTileResponse, AdminPostCreateMeldRequest,
    AdminPostCreateMeldResponse, AdminPostDiscardTileResponse, AdminPostDrawTileResponse,
    AdminPostMovePlayerResponse, AdminPostSayMahjongResponse, GameSettings, GameSettingsSummary,
    ServiceGame, ServiceGameSummary, ServicePlayer, SocketMessage, UserPostAIContinueRequest,
    UserPostAIContinueResponse, UserPostJoinGameResponse, UserPostPassRoundResponse,
    UserPostSayMahjongResponse, UserPostSetGameSettingsResponse, UserPostSortHandResponse,
};
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::debug;

pub struct GameWrapper<'a> {
    service_game: ServiceGame,
    socket_server: DataSocketServer,
    storage: &'a DataStorage,
}

#[derive(Default)]
pub struct CreateGameOpts<'a> {
    pub ai_player_names: Option<&'a Vec<String>>,
    pub auto_sort_own: Option<&'a bool>,
    pub dead_wall: Option<&'a bool>,
    pub player_id: Option<&'a PlayerId>,
}

impl<'a> GameWrapper<'a> {
    async fn from_storage_base(
        storage: &'a DataStorage,
        game_id: &String,
        socket_server: DataSocketServer,
        game_version: Option<&'a GameVersion>,
        use_cache: bool,
    ) -> Result<GameWrapper<'a>, ServiceError> {
        let game = storage.get_game(&game_id.to_string(), use_cache).await;

        if game.is_err() {
            return Err(ServiceError::ErrorLoadingGame);
        }

        let game_content = game.unwrap();

        if game_content.is_none() {
            return Err(ServiceError::GameNotFound);
        }

        let service_game = game_content.unwrap();

        if game_version.is_some() && service_game.game.version != *game_version.unwrap() {
            return Err(ServiceError::GameVersionMismatch);
        }

        Ok(Self {
            storage,
            service_game,
            socket_server,
        })
    }

    pub async fn from_storage(
        storage: &'a DataStorage,
        game_id: &String,
        socket_server: DataSocketServer,
        game_version: Option<&'a GameVersion>,
    ) -> Result<GameWrapper<'a>, ServiceError> {
        Self::from_storage_base(storage, game_id, socket_server, game_version, true).await
    }

    pub async fn from_storage_no_cache(
        storage: &'a DataStorage,
        game_id: &web::Path<String>,
        socket_server: DataSocketServer,
        game_version: Option<&'a GameVersion>,
    ) -> Result<GameWrapper<'a>, ServiceError> {
        Self::from_storage_base(storage, game_id, socket_server, game_version, false).await
    }

    pub async fn from_new_game(
        storage: &'a DataStorage,
        socket_server: DataSocketServer,
        opts: &'a CreateGameOpts<'a>,
    ) -> Result<GameWrapper<'a>, ServiceError> {
        let service_player = if let Some(player_id) = opts.player_id {
            let player_content = storage
                .get_player(player_id)
                .await
                .map_err(|_| {
                    debug!("Player not found with error");
                    ServiceError::Custom("Player not found")
                })?
                .ok_or_else(|| {
                    debug!("Player not found with none");
                    ServiceError::Custom("Player not found")
                })?;

            Some(player_content)
        } else {
            None
        };
        let service_game = create_game(&service_player, opts);

        Ok(Self {
            storage,
            service_game,
            socket_server,
        })
    }

    pub async fn handle_admin_say_mahjong(&mut self, player_id: &PlayerId) -> ResponseCommon {
        self.service_game
            .game
            .say_mahjong(player_id)
            .map_err(|_| ServiceError::Custom("Error saying mahjong"))?;

        self.sync_game_updated();

        let response: &AdminPostSayMahjongResponse = &self.service_game;

        self.save_and_return(response, "Error saying mahjong").await
    }

    pub async fn handle_user_say_mahjong(&mut self, player_id: &PlayerId) -> ResponseCommon {
        self.service_game
            .game
            .say_mahjong(player_id)
            .map_err(|_| ServiceError::Custom("Error saying mahjong"))?;

        self.sync_game_updated();

        let game = ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();
        let response = UserPostSayMahjongResponse(game);

        self.save_and_return(&response, "Error saying mahjong")
            .await
    }

    fn sync_game(&self) {
        let socket_server = loop {
            if let Ok(srv) = self.socket_server.lock() {
                break srv.clone();
            }
            std::thread::sleep(std::time::Duration::from_millis(1));
        };
        socket_server.do_send(SocketClientMessage {
            id: rand::random(),
            msg: SocketMessage::GameUpdate(self.service_game.clone()),
            room: MahjongWebsocketSession::get_room_id(&self.service_game.game.id, None),
        });

        for player in self.service_game.game.players.iter() {
            let game_summary =
                ServiceGameSummary::from_service_game(&self.service_game, player).unwrap();

            socket_server.do_send(SocketClientMessage {
                id: rand::random(),
                msg: SocketMessage::GameSummaryUpdate(game_summary),
                room: MahjongWebsocketSession::get_room_id(
                    &self.service_game.game.id,
                    Some(player),
                ),
            });
        }

        std::mem::drop(socket_server);
    }

    async fn save_and_return<A>(&self, data: A, err_msg: &'static str) -> ResponseCommon
    where
        A: serde::Serialize,
    {
        self.storage
            .save_game(&self.service_game)
            .await
            .map_err(|_| ServiceError::Custom(err_msg))?;

        self.sync_game();

        Ok(HttpResponse::Ok().json(data))
    }

    async fn save_and_return_2<A>(&self, data: A, err_msg: &'static str) -> Result<A, ServiceError>
    where
        A: serde::Serialize,
    {
        self.storage
            .save_game(&self.service_game)
            .await
            .map_err(|_| ServiceError::Custom(err_msg))?;

        self.sync_game();

        Ok(data)
    }

    pub async fn handle_admin_new_game(&self) -> ResponseCommon {
        self.save_and_return(&self.service_game, "Error creating game")
            .await
    }

    pub async fn handle_user_new_game(
        &self,
        player_id: &PlayerId,
    ) -> Result<ServiceGameSummary, ServiceError> {
        let game_summary = ServiceGameSummary::from_service_game(&self.service_game, player_id)
            .ok_or(ServiceError::Custom("Error creating game"))?;

        self.save_and_return_2(game_summary, "Error creating game")
            .await
    }

    pub fn user_load_game(&self, player_id: &PlayerId) -> ResponseCommon {
        match ServiceGameSummary::from_service_game(&self.service_game, player_id) {
            None => Err(ServiceError::ErrorLoadingGame.into()),
            Some(summary) => Ok(HttpResponse::Ok().json(summary)),
        }
    }

    pub async fn handle_sort_hands(&mut self) -> ResponseCommon {
        for player in self.service_game.game.players.iter() {
            let hand = self
                .service_game
                .game
                .table
                .hands
                .0
                .get_mut(player)
                .unwrap();
            hand.sort_default();
        }

        self.sync_game_updated();

        self.save_and_return(&self.service_game.game.table.hands, "Error sorting hands")
            .await
    }

    pub async fn handle_user_set_game_settings(
        &mut self,
        player_id: &PlayerId,
        settings: &GameSettingsSummary,
    ) -> ResponseCommon {
        if settings.fixed_settings {
            return Ok(HttpResponse::BadRequest().body("Cannot change fixed settings"));
        }

        let existing_settings = self.service_game.settings.clone();
        self.service_game.settings = settings.to_game_settings(player_id, &existing_settings);

        let game_summary =
            ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();
        let response = UserPostSetGameSettingsResponse(game_summary);

        self.sync_game_updated();

        self.save_and_return(response, "Error setting game settings")
            .await
    }

    pub async fn handle_admin_draw_tile(&mut self) -> ResponseCommon {
        self.service_game.game.draw_tile_from_wall();

        self.sync_game_updated();

        let current_player_id = self
            .service_game
            .game
            .get_current_player()
            .ok_or(ServiceError::Custom("Error getting current player"))?;

        let hand = self
            .service_game
            .game
            .table
            .hands
            .0
            .get(&current_player_id)
            .unwrap();

        let response: AdminPostDrawTileResponse = hand.clone();

        self.save_and_return(&response, "Error when drawing tile")
            .await
    }

    pub async fn handle_user_draw_tile(
        &mut self,
        player_id: &PlayerId,
    ) -> Result<ServiceGameSummary, ServiceError> {
        let current_player = self
            .service_game
            .game
            .get_current_player()
            .ok_or(ServiceError::Custom("Error getting current player"))?;

        if &current_player != player_id {
            return Err(ServiceError::Custom("Not your turn"));
        }

        let tile_drawn = self.service_game.game.draw_tile_from_wall();

        match tile_drawn {
            DrawTileResult::WallExhausted | DrawTileResult::AlreadyDrawn => {
                return Err(ServiceError::Custom("Error when drawing tile"));
            }
            DrawTileResult::Normal(_) | DrawTileResult::Bonus(_) => {}
        }

        self.sync_game_updated();

        let game = ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();

        self.save_and_return_2(game, "Error when drawing tile")
            .await
    }

    pub async fn handle_user_pass_round(&mut self, player_id: &PlayerId) -> ResponseCommon {
        self.service_game
            .game
            .pass_null_round()
            .map_err(|_| ServiceError::Custom("Error passing round"))?;

        self.sync_game_updated();

        let game_summary =
            ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();
        let response = UserPostPassRoundResponse(game_summary);

        self.save_and_return(&response, "Error passing round").await
    }

    pub async fn handle_admin_ai_continue(
        &mut self,
        body: &AdminPostAIContinueRequest,
    ) -> ResponseCommon {
        let mut standard_ai = AIWrapper::new(&mut self.service_game, body.draw);

        let mut global_changed = false;

        loop {
            let changed = standard_ai.play_action().changed;
            if !global_changed {
                global_changed = changed;
            }
            if !changed {
                break;
            }
        }

        self.sync_game_updated();

        let response: AdminPostAIContinueResponse = AdminPostAIContinueResponse {
            service_game: self.service_game.to_owned(),
            changed: global_changed,
        };

        self.save_and_return(response, "Error with AI action").await
    }

    pub async fn handle_user_ai_continue(
        &mut self,
        body: &UserPostAIContinueRequest,
    ) -> ResponseCommon {
        let mut standard_ai = AIWrapper::new(&mut self.service_game, None);

        let mut global_changed = false;

        loop {
            let changed = standard_ai.play_action().changed;

            if !global_changed {
                global_changed = changed;
            }

            let is_after_discard = standard_ai.get_is_after_discard();

            if is_after_discard {
                break;
            }

            if !changed {
                break;
            }
        }

        self.sync_game_updated();

        let response = UserPostAIContinueResponse {
            service_game_summary: ServiceGameSummary::from_service_game(
                &self.service_game,
                &body.player_id,
            )
            .unwrap(),
            changed: global_changed,
        };

        self.save_and_return(response, "Error with AI action").await
    }

    pub async fn handle_server_ai_continue(&mut self) -> ResponseCommon {
        if !self.service_game.settings.ai_enabled {
            // This response is not used
            return Ok(HttpResponse::BadRequest().body("AI disabled"));
        }

        let mut standard_ai = AIWrapper::new(&mut self.service_game, None);

        standard_ai.play_action();

        self.sync_game_updated();

        self.save_and_return(&self.service_game.game, "Error with AI action")
            .await
    }

    pub async fn handle_user_move_player(
        &mut self,
        player_id: &PlayerId,
    ) -> Result<ServiceGameSummary, ServiceError> {
        let current_player = self
            .service_game
            .game
            .get_current_player()
            .ok_or(ServiceError::Custom("Error getting current player"))?;

        let are_more_real = self.service_game.game.players.iter().any(|p_id| {
            let id = p_id.to_owned();
            let player = self.service_game.players.get(&id).unwrap();
            !player.is_ai && p_id != player_id
        });

        if are_more_real && current_player != player_id.clone() {
            return Err(ServiceError::Custom("Not your turn"));
        }

        self.service_game
            .game
            .round
            .next_turn(&self.service_game.game.table.hands)
            .map_err(|_| ServiceError::Custom("Error moving player"))?;

        self.sync_game_updated();

        let game_summary =
            ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();

        self.save_and_return_2(game_summary, "Error moving player")
            .await
    }

    pub async fn handle_discard_tile_admin(&mut self, tile_id: &TileId) -> ResponseCommon {
        let now_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis();

        self.service_game
            .game
            .discard_tile_to_board(tile_id)
            .unwrap_or_default();

        let mut game = self.service_game.clone();

        game.settings.last_discard_time = now_time as i128;

        self.sync_game_updated();

        let response: AdminPostDiscardTileResponse = game;
        self.save_and_return(&response, "Error when discarding the tile")
            .await
    }

    pub async fn handle_discard_tile_user(
        &mut self,
        tile_id: &TileId,
    ) -> Result<ServiceGameSummary, ServiceError> {
        let now_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis();

        self.service_game
            .game
            .discard_tile_to_board(tile_id)
            .unwrap_or_default();

        let mut game = self.service_game.clone();

        game.settings.last_discard_time = now_time as i128;

        self.sync_game_updated();

        let player_id = self
            .service_game
            .game
            .get_current_player()
            .ok_or(ServiceError::Custom("Error getting current player"))?;

        let game_summary = ServiceGameSummary::from_service_game(&game, &player_id).unwrap();

        self.save_and_return_2(game_summary, "Error when discarding the tile")
            .await
    }

    pub async fn handle_admin_break_meld(
        &mut self,
        body: &AdminPostBreakMeldRequest,
    ) -> ResponseCommon {
        self.service_game
            .game
            .break_meld(&body.player_id, &body.set_id)
            .map_err(|_| ServiceError::Custom("Error when breaking meld"))?;

        self.sync_game_updated();

        let hand = self
            .service_game
            .game
            .table
            .hands
            .0
            .get(&body.player_id)
            .unwrap();

        let response: AdminPostBreakMeldResponse = hand.clone();

        self.save_and_return(&response, "Error when breaking meld")
            .await
    }

    pub async fn handle_user_break_meld(
        &mut self,
        player_id: &PlayerId,
        set_id: &SetIdContent,
    ) -> Result<ServiceGameSummary, ServiceError> {
        self.service_game
            .game
            .break_meld(player_id, set_id)
            .map_err(|_| ServiceError::Custom("Error when breaking meld"))?;

        self.sync_game_updated();

        let game = ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();

        self.save_and_return_2(game, "Error when breaking meld")
            .await
    }

    pub async fn handle_admin_create_meld(
        &mut self,
        body: &AdminPostCreateMeldRequest,
    ) -> ResponseCommon {
        self.service_game
            .game
            .create_meld(
                &body.player_id,
                &body.tiles.clone().into_iter().collect::<Vec<TileId>>(),
                body.is_upgrade,
                body.is_concealed,
            )
            .map_err(|_| ServiceError::Custom("Error when creating meld"))?;

        self.sync_game_updated();

        let hand = self
            .service_game
            .game
            .table
            .hands
            .0
            .get(&body.player_id)
            .unwrap();
        let response: AdminPostCreateMeldResponse = hand.clone();

        self.save_and_return(&response, "Error when creating meld")
            .await
    }

    pub async fn handle_user_create_meld(
        &mut self,
        player_id: &PlayerId,
        tiles: &FxHashSet<TileId>,
        is_upgrade: bool,
        is_concealed: bool,
    ) -> Result<ServiceGameSummary, ServiceError> {
        self.service_game
            .game
            .create_meld(
                player_id,
                &tiles.clone().into_iter().collect::<Vec<TileId>>(),
                is_upgrade,
                is_concealed,
            )
            .map_err(|_| ServiceError::Custom("Error when creating meld"))?;

        self.sync_game_updated();

        let game = ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();

        self.save_and_return_2(game, "Error when creating meld")
            .await
    }

    pub async fn handle_admin_move_player(&mut self) -> ResponseCommon {
        let success = self
            .service_game
            .game
            .round
            .next_turn(&self.service_game.game.table.hands);

        match success {
            Ok(_) => {
                self.sync_game_updated();

                let response = AdminPostMovePlayerResponse(self.service_game.clone());

                self.save_and_return(response, "Error moving player").await
            }
            Err(_) => Ok(HttpResponse::BadRequest().body("Error when moving player")),
        }
    }

    pub async fn handle_user_sort_hand(
        &mut self,
        player_id: &PlayerId,
        tiles: &Option<Vec<TileId>>,
    ) -> ResponseCommon {
        let hand = self
            .service_game
            .game
            .table
            .hands
            .0
            .get_mut(player_id)
            .unwrap();

        if tiles.is_none() {
            hand.sort_default();
        } else {
            hand.sort_by_tiles(tiles.as_ref().unwrap())
                .map_err(|_| ServiceError::Custom("Error sorting hand"))?;
        }

        self.sync_game_updated();

        let game_summary =
            ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();
        let response = UserPostSortHandResponse(game_summary);

        debug!("Sorted hand for player: {:?}", player_id);

        self.save_and_return(&response, "Error sorting hand").await
    }

    pub async fn handle_admin_claim_tile(&mut self, player_id: &PlayerId) -> ResponseCommon {
        let success = self.service_game.game.claim_tile(player_id);

        if success {
            self.sync_game_updated();
            let response = AdminPostClaimTileResponse(self.service_game.to_owned());

            self.save_and_return(response, "Error claiming tile").await
        } else {
            Ok(HttpResponse::BadRequest().body("Error claiming tile"))
        }
    }

    pub async fn handle_user_claim_tile(&mut self, player_id: &PlayerId) -> ResponseCommon {
        let success = self.service_game.game.claim_tile(player_id);

        if success {
            self.sync_game_updated();

            let response =
                ServiceGameSummary::from_service_game(&self.service_game, player_id).unwrap();

            self.save_and_return(response, "Error claiming tile").await
        } else {
            Ok(HttpResponse::BadRequest().body("Error claiming tile"))
        }
    }

    pub fn get_current_player_id(&self) -> Result<PlayerId, ServiceError> {
        self.service_game
            .game
            .get_current_player()
            .ok_or(ServiceError::Custom("No current player"))
    }

    pub async fn handle_user_join_game(&mut self, player_id: &PlayerId) -> ResponseCommon {
        if self.service_game.game.players.0.contains(player_id) {
            return Err(ServiceError::Custom("Player already in game").into());
        }

        if self.service_game.game.players.len() >= 4 {
            return Err(ServiceError::Custom("Game is full").into());
        }

        if self.service_game.game.phase != GamePhase::WaitingPlayers {
            return Err(ServiceError::Custom("Game is empty").into());
        }

        self.service_game.game.players.push(player_id.clone());

        if !self.service_game.settings.auto_sort_players.is_empty() {
            self.service_game
                .settings
                .auto_sort_players
                .insert(player_id.clone());
        }

        self.sync_game_updated();

        let response = UserPostJoinGameResponse(player_id.clone());

        self.save_and_return(response, "Error joining game").await
    }

    fn sync_game_updated(&mut self) {
        self.service_game.updated_at = get_timestamp();
        self.service_game.game.update_version();
    }
}

fn create_game(player: &Option<ServicePlayer>, opts: &CreateGameOpts) -> ServiceGame {
    let mut players = Players::default();

    debug!("Going to create new game players");

    let mut game = Game {
        name: "Custom Game".to_string(),
        ..Game::new(None)
    };

    game.update_id(None);

    if player.is_some() {
        players.push(player.as_ref().unwrap().id.clone());
    }

    for _ in opts.ai_player_names.unwrap_or(&vec![]).iter() {
        let id = Players::new_player();

        players.push(id);
    }

    game.players = players;
    let mut players_set = FxHashMap::<String, ServicePlayer>::default();

    debug!("Going to add players to game");
    let empty_player_names = vec![];
    let player_names = opts.ai_player_names.unwrap_or(&empty_player_names);

    let timestamp = get_timestamp();

    for (index, game_player) in game.players.iter().enumerate() {
        if index == 0 && player.is_some() {
            let player_clone = player.as_ref().unwrap().clone();
            players_set.insert(player_clone.id.clone(), player_clone);
        } else {
            let default_name = format!("Player {}", index);
            let service_player = ServicePlayer {
                created_at: timestamp.to_string(),
                id: game_player.clone(),
                is_ai: true,
                name: player_names.get(index + 1).unwrap_or(&default_name).clone(),
            };
            players_set.insert(game_player.clone(), service_player);
        }
    }

    let mut settings = GameSettings::default();

    if let Some(dead_wall) = opts.dead_wall {
        settings.dead_wall = *dead_wall;
    }

    if let Some(player) = player {
        if let Some(auto_sort_own) = opts.auto_sort_own {
            if *auto_sort_own {
                settings.auto_sort_players.insert(player.id.clone());
            }
        }
        settings.auto_stop_claim_meld.insert(player.id.clone());
    }

    ServiceGame {
        created_at: timestamp,
        game,
        players: players_set,
        settings,
        updated_at: timestamp,
    }
}