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

@ -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<void> => { export const whileNot = async (fn: Function, secondsToWait: number = 120): Promise<void> => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let result: boolean = false; let result: boolean = false;
while (result === false) { let maxQueries = secondsToWait;
await wait(millis); const interval = setInterval(() => {
result = fn() result = fn();
if (maxQueries-- < 0) { if (result === true) {
reject() clearInterval(interval);
return;
}
}
resolve(); resolve();
} else if (maxQueries-- < 0) {
clearInterval(interval);
reject()
}
}, 1000);
}); });
} }

View File

@ -5,7 +5,7 @@ import { PlayerMove } from "./entities/PlayerMove";
import { PlayerInterface } from "./entities/player/PlayerInterface"; import { PlayerInterface } from "./entities/player/PlayerInterface";
import { Tile } from "./entities/Tile"; import { Tile } from "./entities/Tile";
import { LoggingService } from "../common/LoggingService"; 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 { PlayerNotificationService } from '../server/services/PlayerNotificationService';
import { GameState } from './dto/GameState'; import { GameState } from './dto/GameState';
import { PlayerHuman } from './entities/player/PlayerHuman'; import { PlayerHuman } from './entities/player/PlayerHuman';

View File

@ -81,7 +81,7 @@ export class MatchSession {
} }
} }
async checkAllClientsReadyBeforeStart() { async checkAllClientsReady() {
try { try {
if (this.currentGame) { if (this.currentGame) {
const conditionFn = () => { const conditionFn = () => {
@ -145,9 +145,6 @@ export class MatchSession {
private continueMatch(gameSummary: GameSummary) { private continueMatch(gameSummary: GameSummary) {
this.gameSummaries.push(gameSummary); this.gameSummaries.push(gameSummary);
this.winnerIndex = this.players.findIndex(player => player.id === gameSummary?.winner?.id); 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.setScores(gameSummary || undefined);
this.checkMatchWinner(); this.checkMatchWinner();
this.resetPlayers(); this.resetPlayers();
@ -170,15 +167,30 @@ export class MatchSession {
} }
private startGame() { private startGame() {
try {
this.gameNumber += 1; this.gameNumber += 1;
this.logger.info(`Game #${this.gameNumber} started`); this.logger.info(`Game #${this.gameNumber} started`);
this.currentGame = new DominoesGame(this.players, this.rng); 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.currentGame.on('game-over', (gameSummary: GameSummary) => {
this.gameInProgress = false; this.gameInProgress = false;
this.continueMatch(gameSummary); this.continueMatch(gameSummary);
}); });
this.logger.info(`Waiting for ${this.numHumanPlayers} clients to be ready`); this.logger.info(`Waiting for ${this.numHumanPlayers} clients to be ready`);
this.checkAllClientsReadyBeforeStart(); 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) { private async startMatch(seed: string) {
@ -198,19 +210,6 @@ export class MatchSession {
} }
} }
// 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() { resetPlayers() {
this.players.forEach(player => { this.players.forEach(player => {
player.reset() player.reset()
@ -321,10 +320,18 @@ export class MatchSession {
mode: this.mode, mode: this.mode,
pointsToWin: this.pointsToWin, pointsToWin: this.pointsToWin,
status: this.sessionInProgress ? 'in progress' : 'waiting', status: this.sessionInProgress ? 'in progress' : 'waiting',
scoreboard: [...this.scoreboard.entries()], scoreboard: this.getScoreBoardState(),
matchWinner: this.matchWinner?.getState() || null, matchWinner: this.matchWinner?.getState() || null,
matchInProgress: this.matchInProgress, matchInProgress: this.matchInProgress,
gameSummaries: this.gameSummaries, 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 };
});
}
} }

View File

@ -15,7 +15,7 @@ export interface MatchSessionState {
maxPlayers: number; maxPlayers: number;
numPlayers: number; numPlayers: number;
waitingSeconds: number; waitingSeconds: number;
scoreboard: [string, number][]; scoreboard: { id: string, name: string; score: number; }[];
matchWinner: PlayerDto | null; matchWinner: PlayerDto | null;
matchInProgress: boolean; matchInProgress: boolean;
playersReady: number, playersReady: number,