This commit is contained in:
Jose Conde
2024-07-16 19:21:35 +02:00
parent 97b490c058
commit 2b54efb604
10 changed files with 73 additions and 47 deletions

View File

@ -6,10 +6,11 @@ import { getRandomSeed, uuid, wait, whileNot } from "../common/utilities";
import { MatchSessionState } from "./dto/MatchSessionState";
import { PlayerNotificationService } from '../server/services/PlayerNotificationService';
import seedrandom, { PRNG } from "seedrandom";
import { NetworkPlayer } from "./entities/player/NetworkPlayer";
import { PlayerHuman } from "./entities/player/PlayerHuman";
import { GameSummary } from "./dto/GameSummary";
import { PlayerMove } from "./entities/PlayerMove";
import { SessionService } from "../server/services/SessionService";
import { Score } from "../server/db/interfaces";
export class MatchSession {
@ -23,6 +24,7 @@ export class MatchSession {
private winnerIndex: number | null = null;
private clientsReady: string[] = [];
private gameSummaries: GameSummary[] = [];
private sessionService: SessionService = new SessionService();
id: string;
matchInProgress: boolean = false;
@ -148,22 +150,26 @@ export class MatchSession {
this.setScores(gameSummary || undefined);
this.checkMatchWinner();
this.resetPlayers();
if (!this.matchInProgress) {
this.status = 'end'
this.notificationService.sendEventToPlayers('server:match-finished', this.players, {
lastGame: gameSummary,
sessionState: this.getState(true),
});
} else {
this.status = 'waiting'
// await this.playerNotificationManager.notifyMatchState(this);
this.notificationService.sendEventToPlayers('server:game-finished', this.players, {
lastGame: gameSummary,
sessionState: this.getState(true)
});
this.waitingForPlayers = true;
this.startGame();
}
try {
if (!this.matchInProgress) {
this.status = 'end'
this.notificationService.sendEventToPlayers('server:match-finished', this.players, {
lastGame: gameSummary,
sessionState: this.getState(true),
});
} else {
this.status = 'waiting'
// await this.playerNotificationManager.notifyMatchState(this);
this.notificationService.sendEventToPlayers('server:game-finished', this.players, {
lastGame: gameSummary,
sessionState: this.getState(true)
});
this.waitingForPlayers = true;
this.startGame();
}
} finally {
this.sessionService.updateSession(this);
}
}
private startGame() {
@ -327,11 +333,11 @@ export class MatchSession {
};
}
getScoreBoardState() {
getScoreBoardState(): Score[] {
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 };
return { id, name, score } as Score;
});
}
}