This commit is contained in:
Jose Conde
2024-07-16 02:15:40 +02:00
parent 7741b07d60
commit af78c26a93
4 changed files with 46 additions and 37 deletions

View File

@ -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 };
});
}
}