mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 09:18:11 +00:00
70745faac4
## Description: Improve type safety and runtime correctness by: 1. Enabling TypeScript's [strictNullChecks](https://www.typescriptlang.org/tsconfig/#strictNullChecks) compiler option. 2. Replacing all loose equality operators (`==` and `!=`) with strict equality operators (`===` and `!==`). 3. Cleaning up of type declarations, null handling logic, and equality expressions throughout the project. Currently, the code allows implicit assumptions that `null` and `undefined` are interchangeable, and relies on type-coercing equality checks that can introduce subtle bugs. These practices make it difficult to reason about when values may be absent and hinder the effectiveness of static analysis. Migrating to strict null checks and enforcing strict equality comparisons will clarify intent, reduce bugs, and make the codebase safer and easier to maintain. Fixes #466 ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> Co-authored-by: evanpelle <openfrontio@gmail.com>
97 lines
2.4 KiB
TypeScript
97 lines
2.4 KiB
TypeScript
import { GameEvent } from "../EventBus";
|
|
import {
|
|
ColoredTeams,
|
|
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 | null = null;
|
|
|
|
constructor() {}
|
|
|
|
init(mg: Game, ticks: number) {
|
|
this.mg = mg;
|
|
}
|
|
|
|
tick(ticks: number) {
|
|
if (ticks % 10 !== 0) {
|
|
return;
|
|
}
|
|
if (this.mg === null) throw new Error("Not initialized");
|
|
if (this.mg.config().gameConfig().gameMode === GameMode.FFA) {
|
|
this.checkWinnerFFA();
|
|
} else {
|
|
this.checkWinnerTeam();
|
|
}
|
|
}
|
|
|
|
checkWinnerFFA(): void {
|
|
if (this.mg === null) throw new Error("Not initialized");
|
|
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 {
|
|
if (this.mg === null) throw new Error("Not initialized");
|
|
const teamToTiles = new Map<Team, number>();
|
|
for (const player of this.mg.players()) {
|
|
const team = player.team();
|
|
// Sanity check, team should not be null here
|
|
if (team === null) continue;
|
|
teamToTiles.set(
|
|
team,
|
|
(teamToTiles.get(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()) {
|
|
if (max[0] === ColoredTeams.Bot) return;
|
|
this.mg.setWinner(max[0], this.mg.stats().stats());
|
|
console.log(`${max[0]} has won the game`);
|
|
this.active = false;
|
|
}
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
}
|