progress
This commit is contained in:
parent
7741b07d60
commit
af78c26a93
@ -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) => {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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';
|
||||
|
@ -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 };
|
||||
});
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user