From ca0f1466c210433affbcea7e8f016e17d6dd5ac2 Mon Sep 17 00:00:00 2001 From: Jose Conde Date: Mon, 8 Jul 2024 10:21:56 +0200 Subject: [PATCH] flow --- src/common/LoggingService.ts | 6 +++++- src/game/DominoesGame.ts | 7 ++++++- src/game/MatchSession.ts | 7 +++++-- src/game/entities/player/PlayerAI.ts | 3 ++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/common/LoggingService.ts b/src/common/LoggingService.ts index c39cf29..e8c0b7b 100644 --- a/src/common/LoggingService.ts +++ b/src/common/LoggingService.ts @@ -120,7 +120,11 @@ export class LoggingService { } _getStringObject(data: any) { - return JSON.stringify(data, null, 2); + try { + return JSON.stringify(data, null, 2); + } catch (error) { + return data; + } } } \ No newline at end of file diff --git a/src/game/DominoesGame.ts b/src/game/DominoesGame.ts index d8a14c6..7d1e222 100644 --- a/src/game/DominoesGame.ts +++ b/src/game/DominoesGame.ts @@ -27,6 +27,7 @@ export class DominoesGame { handSize: number = 7; notificationManager: PlayerNotificationService = new PlayerNotificationService(); lastMove: PlayerMove | null = null; + forcedInitialPlayerIndex: number | null = null; constructor(public players: PlayerInterface[], seed: PRNG) { this.id = uuid(); @@ -43,6 +44,10 @@ export class DominoesGame { this.board.boneyard = this.generateTiles(); } + setForcedInitialPlayerIndex(index: number) { + this.forcedInitialPlayerIndex = index; + } + reset() { this.board.reset(); this.initializeGame(); @@ -174,7 +179,7 @@ export class DominoesGame { this.tileSelectionPhase = false; this.gameInProgress = true; - this.currentPlayerIndex = this.getStartingPlayerIndex(); + this.currentPlayerIndex = (this.forcedInitialPlayerIndex !== null) ? this.forcedInitialPlayerIndex : this.getStartingPlayerIndex(); printLine(`${this.players[this.currentPlayerIndex].name} is the starting player:`); while (!this.gameOver) { await this.playTurn(); diff --git a/src/game/MatchSession.ts b/src/game/MatchSession.ts index 779b256..41118c3 100644 --- a/src/game/MatchSession.ts +++ b/src/game/MatchSession.ts @@ -69,15 +69,18 @@ export class MatchSession { let gameNumber: number = 0; this.matchInProgress = true this.playerNotificationManager.notifyMatchState(this); + let winnerIndex: number | null = null; while (this.matchInProgress) { - this.currentGame = new DominoesGame(this.players, this.rng); + if (winnerIndex !== null) { + this.currentGame.setForcedInitialPlayerIndex(winnerIndex); + } gameNumber += 1; this.state = 'started' this.logger.info(`Game #${gameNumber} started`); // this.game.reset() const gameSummary = await this.currentGame.start(); - this.logger.debug('gameSummary :>> ', gameSummary); + winnerIndex = this.players.findIndex(player => player.id === gameSummary.winner?.id); this.setScores(); this.checkMatchWinner(); this.resetPlayers(); diff --git a/src/game/entities/player/PlayerAI.ts b/src/game/entities/player/PlayerAI.ts index 32ccd82..2729366 100644 --- a/src/game/entities/player/PlayerAI.ts +++ b/src/game/entities/player/PlayerAI.ts @@ -13,7 +13,8 @@ export class PlayerAI extends AbstractPlayer { } async makeMove(board: Board): Promise { - await wait(500); // Simulate thinking time + const rndWait = Math.floor(Math.random() * 1000) + 1000; + await wait(rndWait); // Simulate thinking time if (board.tiles.length === 0) { printLine('playing the first tile'); const highestPair = this.getHighestPair();