mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 09:18:11 +00:00
1b76c46bc5
GameRecords also now include PlayerStats
54 lines
1.1 KiB
TypeScript
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;
|
|
}
|
|
}
|