From af78c26a939ddf4f4a81d4e5fde22ccc709baf74 Mon Sep 17 00:00:00 2001 From: Jose Conde Date: Tue, 16 Jul 2024 02:15:40 +0200 Subject: [PATCH] progress --- src/common/utilities.ts | 18 +++++---- src/game/DominoesGame.ts | 2 +- src/game/MatchSession.ts | 61 +++++++++++++++++-------------- src/game/dto/MatchSessionState.ts | 2 +- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/common/utilities.ts b/src/common/utilities.ts index 3c36d7f..1f1841b 100644 --- a/src/common/utilities.ts +++ b/src/common/utilities.ts @@ -32,18 +32,20 @@ export const whileNotUndefined = async (fn: Function, maxQueries: number = 20, m }); } -export const whileNot = async (fn: Function, maxQueries: number = 20, millis: number = 500): Promise => { +export const whileNot = async (fn: Function, secondsToWait: number = 120): Promise => { return new Promise(async (resolve, reject) => { let result: boolean = false; - while (result === false) { - await wait(millis); - result = fn() - if (maxQueries-- < 0) { + let maxQueries = secondsToWait; + const interval = setInterval(() => { + result = fn(); + if (result === true) { + clearInterval(interval); + resolve(); + } else if (maxQueries-- < 0) { + clearInterval(interval); reject() - return; } - } - resolve(); + }, 1000); }); } diff --git a/src/game/DominoesGame.ts b/src/game/DominoesGame.ts index 67cb6dd..8585620 100644 --- a/src/game/DominoesGame.ts +++ b/src/game/DominoesGame.ts @@ -5,7 +5,7 @@ import { PlayerMove } from "./entities/PlayerMove"; import { PlayerInterface } from "./entities/player/PlayerInterface"; import { Tile } from "./entities/Tile"; import { LoggingService } from "../common/LoggingService"; -import { printBoard, printLine, uuid, wait, whileNot, whileNotUndefined } from '../common/utilities'; +import { printBoard, printLine, uuid, whileNot } from '../common/utilities'; import { PlayerNotificationService } from '../server/services/PlayerNotificationService'; import { GameState } from './dto/GameState'; import { PlayerHuman } from './entities/player/PlayerHuman'; diff --git a/src/game/MatchSession.ts b/src/game/MatchSession.ts index bda4d56..788f589 100644 --- a/src/game/MatchSession.ts +++ b/src/game/MatchSession.ts @@ -81,7 +81,7 @@ export class MatchSession { } } - async checkAllClientsReadyBeforeStart() { + async checkAllClientsReady() { try { if (this.currentGame) { const conditionFn = () => { @@ -145,9 +145,6 @@ export class MatchSession { private continueMatch(gameSummary: GameSummary) { this.gameSummaries.push(gameSummary); this.winnerIndex = this.players.findIndex(player => player.id === gameSummary?.winner?.id); - if (this.winnerIndex !== null) { - this.currentGame?.setForcedInitialPlayerIndex(this.winnerIndex); - } this.setScores(gameSummary || undefined); this.checkMatchWinner(); this.resetPlayers(); @@ -170,15 +167,30 @@ export class MatchSession { } private startGame() { - this.gameNumber += 1; - this.logger.info(`Game #${this.gameNumber} started`); - this.currentGame = new DominoesGame(this.players, this.rng); - this.currentGame.on('game-over', (gameSummary: GameSummary) => { - this.gameInProgress = false; - this.continueMatch(gameSummary); - }); - this.logger.info(`Waiting for ${this.numHumanPlayers} clients to be ready`); - this.checkAllClientsReadyBeforeStart(); + try { + this.gameNumber += 1; + this.logger.info(`Game #${this.gameNumber} started`); + this.currentGame = new DominoesGame(this.players, this.rng); + if (this.winnerIndex !== null) { + this.currentGame.setForcedInitialPlayerIndex(this.winnerIndex); + } + this.currentGame.on('game-over', (gameSummary: GameSummary) => { + this.gameInProgress = false; + this.continueMatch(gameSummary); + }); + this.logger.info(`Waiting for ${this.numHumanPlayers} clients to be ready`); + this.checkAllClientsReady(); + } catch (error: any) { + this.logger.error(error); + this.matchInProgress = false; + this.status = 'error' + + this.notificationService.sendEventToPlayers('server:game-error', this.players, { + matchState: this.getState(), + error: error.message || error, + }) + + } } private async startMatch(seed: string) { @@ -197,19 +209,6 @@ export class MatchSession { this.status = 'error' } } - - // async checkHumanPlayersReady() { - // this.logger.info('Waiting for human players to be ready'); - // return new Promise((resolve) => { - // const interval = setInterval(() => { - // this.logger.debug(`Human players ready: ${this.numPlayersReady}/${this.numHumanPlayers}`) - // if (this.numPlayersReady === this.numHumanPlayers) { - // clearInterval(interval); - // resolve(true); - // } - // }, 1000); - // }); - // } resetPlayers() { this.players.forEach(player => { @@ -321,10 +320,18 @@ export class MatchSession { mode: this.mode, pointsToWin: this.pointsToWin, status: this.sessionInProgress ? 'in progress' : 'waiting', - scoreboard: [...this.scoreboard.entries()], + scoreboard: this.getScoreBoardState(), matchWinner: this.matchWinner?.getState() || null, matchInProgress: this.matchInProgress, gameSummaries: this.gameSummaries, }; } + + getScoreBoardState() { + return Array.from(this.scoreboard).map(([name, score]) => { + const player = this.players.find(player => player.name === name); + const id = player?.id || name; + return { id, name, score }; + }); + } } \ No newline at end of file diff --git a/src/game/dto/MatchSessionState.ts b/src/game/dto/MatchSessionState.ts index d762c4b..14aea07 100644 --- a/src/game/dto/MatchSessionState.ts +++ b/src/game/dto/MatchSessionState.ts @@ -15,7 +15,7 @@ export interface MatchSessionState { maxPlayers: number; numPlayers: number; waitingSeconds: number; - scoreboard: [string, number][]; + scoreboard: { id: string, name: string; score: number; }[]; matchWinner: PlayerDto | null; matchInProgress: boolean; playersReady: number,