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) => {
|
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) {
|
||||||
|
clearInterval(interval);
|
||||||
|
resolve();
|
||||||
|
} else if (maxQueries-- < 0) {
|
||||||
|
clearInterval(interval);
|
||||||
reject()
|
reject()
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}, 1000);
|
||||||
resolve();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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';
|
||||||
|
@ -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() {
|
||||||
this.gameNumber += 1;
|
try {
|
||||||
this.logger.info(`Game #${this.gameNumber} started`);
|
this.gameNumber += 1;
|
||||||
this.currentGame = new DominoesGame(this.players, this.rng);
|
this.logger.info(`Game #${this.gameNumber} started`);
|
||||||
this.currentGame.on('game-over', (gameSummary: GameSummary) => {
|
this.currentGame = new DominoesGame(this.players, this.rng);
|
||||||
this.gameInProgress = false;
|
if (this.winnerIndex !== null) {
|
||||||
this.continueMatch(gameSummary);
|
this.currentGame.setForcedInitialPlayerIndex(this.winnerIndex);
|
||||||
});
|
}
|
||||||
this.logger.info(`Waiting for ${this.numHumanPlayers} clients to be ready`);
|
this.currentGame.on('game-over', (gameSummary: GameSummary) => {
|
||||||
this.checkAllClientsReadyBeforeStart();
|
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) {
|
private async startMatch(seed: string) {
|
||||||
@ -197,19 +209,6 @@ export class MatchSession {
|
|||||||
this.status = 'error'
|
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() {
|
resetPlayers() {
|
||||||
this.players.forEach(player => {
|
this.players.forEach(player => {
|
||||||
@ -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 };
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user