This commit is contained in:
Jose Conde
2024-07-07 23:23:49 +02:00
parent a974f576b3
commit f67c262b0e
24 changed files with 369 additions and 211 deletions

View File

@ -4,7 +4,7 @@ import { PlayerInterface } from "./entities/player/PlayerInterface";
import { LoggingService } from "../common/LoggingService";
import { getRandomSeed, uuid, wait } from "../common/utilities";
import { MatchSessionState } from "./dto/MatchSessionState";
import { PlayerNotificationManager } from './PlayerNotificationManager';
import { PlayerNotificationService } from '../server/services/PlayerNotificationService';
import seedrandom, { PRNG } from "seedrandom";
import { NetworkPlayer } from "./entities/player/NetworkPlayer";
import { PlayerHuman } from "./entities/player/PlayerHuman";
@ -16,11 +16,11 @@ export class MatchSession {
private waitingForPlayers: boolean = true;
private waitingSeconds: number = 0;
private logger: LoggingService = new LoggingService();
private playerNotificationManager = new PlayerNotificationManager();
private playerNotificationManager = new PlayerNotificationService();
id: string;
matchInProgress: boolean = false;
matchWinner: PlayerInterface | null = null;
matchWinner?: PlayerInterface = undefined;
maxPlayers: number = 4;
mode: string = 'classic';
players: PlayerInterface[] = [];
@ -31,20 +31,19 @@ export class MatchSession {
sessionInProgress: boolean = false;
state: string = 'created'
constructor(public creator: PlayerInterface, public name?: string) {
constructor(public creator: PlayerInterface, public name?: string, seed?: string) {
this.seed = seed || getRandomSeed();
this.id = uuid();
this.name = name || `Game ${this.id}`;
this.addPlayer(creator);
this.addPlayer(creator);
this.creator = creator;
this.logger.info(`Match session created by: ${creator.name}`);
this.logger.info(`Match session ID: ${this.id}`);
this.logger.info(`Match session name: ${this.name}`);
this.logger.info(`Points to win: ${this.pointsToWin}`);
this.sessionInProgress = true;
this.matchInProgress = false;
this.playerNotificationManager.notifyMatchState(this);
this.playerNotificationManager.notifyPlayersState(this.players);
}
get numPlayers() {
@ -77,14 +76,17 @@ export class MatchSession {
this.state = 'started'
this.logger.info(`Game #${gameNumber} started`);
// this.game.reset()
await this.currentGame.start();
const gameSummary = await this.currentGame.start();
this.logger.debug('gameSummary :>> ', gameSummary);
this.setScores();
this.checkMatchWinner();
this.resetReadiness();
this.resetPlayers();
this.state = 'waiting'
await this.playerNotificationManager.notifyMatchState(this);
this.playerNotificationManager.sendEventToPlayers('game-finished', this.players);
await this.checkHumanPlayersReady();
if (this.matchInProgress) {
await this.checkHumanPlayersReady();
}
}
this.state = 'end'
// await this.game.start();
@ -103,10 +105,10 @@ export class MatchSession {
}, 1000);
});
}
resetReadiness() {
resetPlayers() {
this.players.forEach(player => {
player.ready = false
player.reset()
});
}
@ -114,7 +116,10 @@ export class MatchSession {
const scores = Array.from(this.scoreboard.values());
const maxScore = Math.max(...scores);
if (maxScore >= this.pointsToWin) {
this.matchWinner = this.players.find(player => this.scoreboard.get(player.id) === maxScore)!;
this.matchWinner = this.players.find(player => this.scoreboard.get(player.name) === maxScore);
if (!this.matchWinner) {
throw new Error('Match winner not found');
}
this.logger.info(`Match winner: ${this.matchWinner.name} with ${maxScore} points`);
this.matchInProgress = false;
}
@ -122,17 +127,19 @@ export class MatchSession {
resetScoreboard() {
this.scoreboard = new Map();
this.players.forEach(player => {
this.scoreboard.set(player.id, 0);
this.scoreboard.set(player.name, 0);
});
}
setScores() {
const totalPips = this.currentGame?.players.reduce((acc, player) => acc + player.pipsCount(), 0);
const totalPips = this.currentGame?.players.reduce((acc, player) => acc + player.pipsCount(), 0) || 0;
if (this.currentGame && this.currentGame.winner !== null) {
const winner = this.currentGame.winner;
this.scoreboard.set(winner.id, this.scoreboard.get(winner.id)! + totalPips!);
const currentPips = this.scoreboard.get(winner.name) || 0;
this.logger.debug (`${winner.name} has ${currentPips} points`);
this.scoreboard.set(winner.name, currentPips + totalPips);
if (winner.teamedWith !== null) {
this.scoreboard.set(winner.teamedWith.id, this.scoreboard.get(winner.teamedWith.id)! + totalPips!);
this.scoreboard.set(winner.teamedWith.name, currentPips + totalPips);
}
}
}
@ -181,9 +188,7 @@ export class MatchSession {
return player;
}
async start(seed?: string) {
this.seed = seed || getRandomSeed();
console.log('seed :>> ', this.seed);
async start() {
if (this.matchInProgress) {
throw new Error("Game already in progress");
}
@ -207,8 +212,9 @@ export class MatchSession {
this.logger.info(`${player.name} joined the game!`);
}
setPlayerReady(user: string) {
const player = this.players.find(player => player.name === user);
setPlayerReady(userId: string) {
this.logger.debug(userId)
const player = this.players.find(player => player.id === userId);
if (!player) {
throw new Error("Player not found");
}
@ -242,7 +248,7 @@ export class MatchSession {
mode: this.mode,
pointsToWin: this.pointsToWin,
status: this.sessionInProgress ? 'in progress' : 'waiting',
scoreboard: this.scoreboard,
scoreboard: [...this.scoreboard.entries()],
matchWinner: this.matchWinner?.getState() || null,
matchInProgress: this.matchInProgress
};