Files
OpenFrontIO/src/core/execution/WinCheckExecution.ts
T
ilan schemoul 1b76c46bc5 feat: remove LocalPersistantStats so we locally save GameRecords
GameRecords also now include PlayerStats
2025-03-08 17:39:41 +01:00

54 lines
1.1 KiB
TypeScript

import { EventBus, GameEvent } from "../EventBus";
import { Execution, Game, Player, PlayerID } from "../game/Game";
export class WinEvent implements GameEvent {
constructor(public readonly winner: Player) {}
}
export class WinCheckExecution implements Execution {
private active = true;
private mg: Game;
constructor() {}
init(mg: Game, ticks: number) {
this.mg = mg;
}
tick(ticks: number) {
if (ticks % 10 != 0) {
return;
}
const sorted = this.mg
.players()
.sort((a, b) => b.numTilesOwned() - a.numTilesOwned());
if (sorted.length == 0) {
return;
}
const max = sorted[0];
const numTilesWithoutFallout =
this.mg.numLandTiles() - this.mg.numTilesWithFallout();
if (
(max.numTilesOwned() / numTilesWithoutFallout) * 100 >
this.mg.config().percentageTilesOwnedToWin()
) {
this.mg.setWinner(max, this.mg.stats().stats());
console.log(`${max.name()} has won the game`);
this.active = false;
}
}
owner(): Player {
return null;
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}