mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-08 00:40:39 +00:00
Enable strictNullChecks, eqeqeq (#436)
## 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>
This commit is contained in:
@@ -15,10 +15,10 @@ import { ParabolaPathFinder } from "../pathfinding/PathFinding";
|
||||
import { PseudoRandom } from "../PseudoRandom";
|
||||
|
||||
export class NukeExecution implements Execution {
|
||||
private player: Player;
|
||||
private active = true;
|
||||
private mg: Game;
|
||||
private nuke: Unit;
|
||||
private player: Player | null = null;
|
||||
private mg: Game | null = null;
|
||||
private nuke: Unit | null = null;
|
||||
|
||||
private random: PseudoRandom;
|
||||
private pathFinder: ParabolaPathFinder;
|
||||
@@ -27,7 +27,7 @@ export class NukeExecution implements Execution {
|
||||
private type: NukeType,
|
||||
private senderID: PlayerID,
|
||||
private dst: TileRef,
|
||||
private src?: TileRef,
|
||||
private src?: TileRef | null,
|
||||
private speed: number = -1,
|
||||
private waitTicks = 0,
|
||||
) {}
|
||||
@@ -42,28 +42,37 @@ export class NukeExecution implements Execution {
|
||||
this.mg = mg;
|
||||
this.player = mg.player(this.senderID);
|
||||
this.random = new PseudoRandom(ticks);
|
||||
if (this.speed == -1) {
|
||||
if (this.speed === -1) {
|
||||
this.speed = this.mg.config().defaultNukeSpeed();
|
||||
}
|
||||
this.pathFinder = new ParabolaPathFinder(mg);
|
||||
}
|
||||
|
||||
public target(): Player | TerraNullius {
|
||||
if (this.mg === null) {
|
||||
throw new Error("Not initialized");
|
||||
}
|
||||
return this.mg.owner(this.dst);
|
||||
}
|
||||
|
||||
private tilesToDestroy(): Set<TileRef> {
|
||||
if (this.mg === null || this.nuke === null) {
|
||||
throw new Error("Not initialized");
|
||||
}
|
||||
const magnitude = this.mg.config().nukeMagnitudes(this.nuke.type());
|
||||
const rand = new PseudoRandom(this.mg.ticks());
|
||||
const inner2 = magnitude.inner * magnitude.inner;
|
||||
const outer2 = magnitude.outer * magnitude.outer;
|
||||
return this.mg.bfs(this.dst, (_, n: TileRef) => {
|
||||
const d2 = this.mg.euclideanDistSquared(this.dst, n);
|
||||
const d2 = this.mg?.euclideanDistSquared(this.dst, n) ?? 0;
|
||||
return d2 <= outer2 && (d2 <= inner2 || rand.chance(2));
|
||||
});
|
||||
}
|
||||
|
||||
private breakAlliances(toDestroy: Set<TileRef>) {
|
||||
if (this.mg === null || this.player === null || this.nuke === null) {
|
||||
throw new Error("Not initialized");
|
||||
}
|
||||
const attacked = new Map<Player, number>();
|
||||
for (const tile of toDestroy) {
|
||||
const owner = this.mg.owner(tile);
|
||||
@@ -74,13 +83,13 @@ export class NukeExecution implements Execution {
|
||||
}
|
||||
|
||||
for (const [other, tilesDestroyed] of attacked) {
|
||||
if (tilesDestroyed > 100 && this.nuke.type() != UnitType.MIRVWarhead) {
|
||||
if (tilesDestroyed > 100 && this.nuke.type() !== UnitType.MIRVWarhead) {
|
||||
// Mirv warheads shouldn't break alliances
|
||||
const alliance = this.player.allianceWith(other);
|
||||
if (alliance != null) {
|
||||
if (alliance !== null) {
|
||||
this.player.breakAlliance(alliance);
|
||||
}
|
||||
if (other != this.player) {
|
||||
if (other !== this.player) {
|
||||
other.updateRelation(this.player, -100);
|
||||
}
|
||||
}
|
||||
@@ -88,9 +97,13 @@ export class NukeExecution implements Execution {
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
if (this.nuke == null) {
|
||||
if (this.mg === null || this.player === null) {
|
||||
throw new Error("Not initialized");
|
||||
}
|
||||
|
||||
if (this.nuke === null) {
|
||||
const spawn = this.src ?? this.player.canBuild(this.type, this.dst);
|
||||
if (spawn == false) {
|
||||
if (spawn === false) {
|
||||
consolex.warn(`cannot build Nuke`);
|
||||
this.active = false;
|
||||
return;
|
||||
@@ -98,14 +111,14 @@ export class NukeExecution implements Execution {
|
||||
this.pathFinder.computeControlPoints(
|
||||
spawn,
|
||||
this.dst,
|
||||
this.type != UnitType.MIRVWarhead,
|
||||
this.type !== UnitType.MIRVWarhead,
|
||||
);
|
||||
this.nuke = this.player.buildUnit(this.type, spawn, {
|
||||
detonationDst: this.dst,
|
||||
});
|
||||
if (this.mg.hasOwner(this.dst)) {
|
||||
const target = this.mg.owner(this.dst) as Player;
|
||||
if (this.type == UnitType.AtomBomb) {
|
||||
if (this.type === UnitType.AtomBomb) {
|
||||
this.mg.displayIncomingUnit(
|
||||
this.nuke.id(),
|
||||
`${this.player.name()} - atom bomb inbound`,
|
||||
@@ -113,7 +126,7 @@ export class NukeExecution implements Execution {
|
||||
target.id(),
|
||||
);
|
||||
}
|
||||
if (this.type == UnitType.HydrogenBomb) {
|
||||
if (this.type === UnitType.HydrogenBomb) {
|
||||
this.mg.displayIncomingUnit(
|
||||
this.nuke.id(),
|
||||
`${this.player.name()} - hydrogen bomb inbound`,
|
||||
@@ -164,6 +177,9 @@ export class NukeExecution implements Execution {
|
||||
}
|
||||
|
||||
private detonate() {
|
||||
if (this.mg === null || this.nuke === null) {
|
||||
throw new Error("Not initialized");
|
||||
}
|
||||
const magnitude = this.mg.config().nukeMagnitudes(this.nuke.type());
|
||||
const toDestroy = this.tilesToDestroy();
|
||||
this.breakAlliances(toDestroy);
|
||||
@@ -183,15 +199,17 @@ export class NukeExecution implements Execution {
|
||||
.nukeDeathFactor(owner.workers(), owner.numTilesOwned()),
|
||||
);
|
||||
owner.outgoingAttacks().forEach((attack) => {
|
||||
const deaths = this.mg
|
||||
.config()
|
||||
.nukeDeathFactor(attack.troops(), owner.numTilesOwned());
|
||||
const deaths =
|
||||
this.mg
|
||||
?.config()
|
||||
.nukeDeathFactor(attack.troops(), owner.numTilesOwned()) ?? 0;
|
||||
attack.setTroops(attack.troops() - deaths);
|
||||
});
|
||||
owner.units(UnitType.TransportShip).forEach((attack) => {
|
||||
const deaths = this.mg
|
||||
.config()
|
||||
.nukeDeathFactor(attack.troops(), owner.numTilesOwned());
|
||||
const deaths =
|
||||
this.mg
|
||||
?.config()
|
||||
.nukeDeathFactor(attack.troops(), owner.numTilesOwned()) ?? 0;
|
||||
attack.setTroops(attack.troops() - deaths);
|
||||
});
|
||||
}
|
||||
@@ -204,10 +222,10 @@ export class NukeExecution implements Execution {
|
||||
const outer2 = magnitude.outer * magnitude.outer;
|
||||
for (const unit of this.mg.units()) {
|
||||
if (
|
||||
unit.type() != UnitType.AtomBomb &&
|
||||
unit.type() != UnitType.HydrogenBomb &&
|
||||
unit.type() != UnitType.MIRVWarhead &&
|
||||
unit.type() != UnitType.MIRV
|
||||
unit.type() !== UnitType.AtomBomb &&
|
||||
unit.type() !== UnitType.HydrogenBomb &&
|
||||
unit.type() !== UnitType.MIRVWarhead &&
|
||||
unit.type() !== UnitType.MIRV
|
||||
) {
|
||||
if (this.mg.euclideanDistSquared(this.dst, unit.tile()) < outer2) {
|
||||
unit.delete();
|
||||
@@ -219,6 +237,9 @@ export class NukeExecution implements Execution {
|
||||
}
|
||||
|
||||
owner(): Player {
|
||||
if (this.player === null) {
|
||||
throw new Error("Not initialized");
|
||||
}
|
||||
return this.player;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user