mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:10:42 +00:00
86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
import { EventBus, GameEvent } from "../EventBus";
|
|
import { Execution, Game, GameMode, Player, Team } 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;
|
|
}
|
|
if (this.mg.config().gameConfig().gameMode == GameMode.FFA) {
|
|
this.checkWinnerFFA();
|
|
} else {
|
|
this.checkWinnerTeam();
|
|
}
|
|
}
|
|
|
|
checkWinnerFFA(): void {
|
|
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;
|
|
}
|
|
}
|
|
|
|
checkWinnerTeam(): void {
|
|
const teamToTiles = new Map<Team, number>();
|
|
for (const player of this.mg.players()) {
|
|
teamToTiles.set(
|
|
player.team(),
|
|
(teamToTiles.get(player.team()) ?? 0) + player.numTilesOwned(),
|
|
);
|
|
}
|
|
const sorted = Array.from(teamToTiles.entries()).sort(
|
|
(a, b) => b[1] - a[1],
|
|
);
|
|
if (sorted.length == 0) {
|
|
return;
|
|
}
|
|
const max = sorted[0];
|
|
const numTilesWithoutFallout =
|
|
this.mg.numLandTiles() - this.mg.numTilesWithFallout();
|
|
const percentage = (max[1] / numTilesWithoutFallout) * 100;
|
|
if (percentage > this.mg.config().percentageTilesOwnedToWin()) {
|
|
this.mg.setWinner(max[0].name, this.mg.stats().stats());
|
|
console.log(`${max[0].name} has won the game`);
|
|
this.active = false;
|
|
}
|
|
}
|
|
owner(): Player {
|
|
return null;
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
}
|