working flow

This commit is contained in:
Jose Conde
2024-07-14 21:41:38 +02:00
parent 5f117667a4
commit 7741b07d60
27 changed files with 534 additions and 147 deletions

View File

@ -22,6 +22,7 @@ export class MatchSession {
private notificationService = new PlayerNotificationService();
private winnerIndex: number | null = null;
private clientsReady: string[] = [];
private gameSummaries: GameSummary[] = [];
id: string;
matchInProgress: boolean = false;
@ -35,7 +36,7 @@ export class MatchSession {
scoreboard: Map<string, number> = new Map();
seed!: string
sessionInProgress: boolean = false;
state: string = 'created'
status: string = 'created'
constructor(public creator: PlayerInterface, public name?: string, seed?: string) {
this.seed = seed || getRandomSeed();
@ -71,6 +72,13 @@ export class MatchSession {
this.logger.trace(`Client ${userId} is ready`)
this.clientsReady.push(userId);
}
this.logger.trace(`${this.clientsReady.length}`);
}
setCurrentGameClientReady(userId: string) {
if (this.currentGame) {
this.currentGame.setClientReady(userId);
}
}
async checkAllClientsReadyBeforeStart() {
@ -80,13 +88,15 @@ export class MatchSession {
this.logger.trace(`Clients ready: ${this.clientsReady.length}/${this.numHumanPlayers}`);
return this.clientsReady.length === this.numHumanPlayers
}
await whileNot(conditionFn, 10);
await whileNot(conditionFn, 50);
this.logger.info(`Game #${this.gameNumber} started`);
this.currentGame.start();
this.gameInProgress = true;
this.clientsReady = [];
}
} catch (error) {
this.logger.error(error, 'Error starting game');
throw new Error('Error starting game (checkAllClientsReadyBeforeStart)');
}
}
@ -95,6 +105,8 @@ export class MatchSession {
}
playerMove(move: any) {
this.logger.trace('Handling player move (playerMove)');
this.logger.trace(`${this.clientsReady.length}`);
if (this.currentGame) {
if ((move === null) || (move === undefined) || move.type === 'pass') {
this.currentGame.finishTurn(null);
@ -131,6 +143,7 @@ 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);
@ -139,19 +152,20 @@ export class MatchSession {
this.checkMatchWinner();
this.resetPlayers();
if (!this.matchInProgress) {
this.state = 'end'
this.status = 'end'
this.notificationService.sendEventToPlayers('server:match-finished', this.players, {
lastGame: gameSummary,
sessionState: this.getState(),
});
} else {
this.state = 'waiting'
this.status = 'waiting'
// await this.playerNotificationManager.notifyMatchState(this);
this.notificationService.sendEventToPlayers('server:game-finished', this.players, {
lastGame: gameSummary,
sessionState: this.getState()
});
this.waitingForPlayers = true;
this.waitingForPlayers = true;
this.startGame();
}
}
@ -169,7 +183,7 @@ export class MatchSession {
private async startMatch(seed: string) {
try {
this.state = 'in-game'
this.status = 'in-game'
this.rng = seedrandom(seed);
this.resetScoreboard()
this.gameNumber = 0;
@ -180,7 +194,7 @@ export class MatchSession {
} catch (error) {
this.logger.error(error);
this.matchInProgress = false;
this.state = 'error'
this.status = 'error'
}
}
@ -296,11 +310,7 @@ export class MatchSession {
id: this.id,
name: this.name!,
creator: this.creator.id,
players: this.players.map(player =>( {
id: player.id,
name: player.name,
ready: player.ready,
})),
players: this.players.map(player => player.getState()),
playersReady: this.numPlayersReady,
sessionInProgress: this.sessionInProgress,
maxPlayers: this.maxPlayers,
@ -313,7 +323,8 @@ export class MatchSession {
status: this.sessionInProgress ? 'in progress' : 'waiting',
scoreboard: [...this.scoreboard.entries()],
matchWinner: this.matchWinner?.getState() || null,
matchInProgress: this.matchInProgress
matchInProgress: this.matchInProgress,
gameSummaries: this.gameSummaries,
};
}
}