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:
Scott Anderson
2025-05-15 16:39:40 -07:00
committed by GitHub
co-authored by Scott Anderson evanpelle
parent 369483b4ac
commit 70745faac4
119 changed files with 1428 additions and 1123 deletions
+3 -3
View File
@@ -7,11 +7,11 @@ import { DevConfig, DevServerConfig } from "./DevConfig";
import { preprodConfig } from "./PreprodConfig";
import { prodConfig } from "./ProdConfig";
export let cachedSC: ServerConfig = null;
export let cachedSC: ServerConfig | null = null;
export async function getConfig(
gameConfig: GameConfig,
userSettings: UserSettings | null = null,
userSettings: UserSettings | null,
isReplay: boolean = false,
): Promise<Config> {
const sc = await getServerConfigFromClient();
@@ -45,7 +45,7 @@ export async function getServerConfigFromClient(): Promise<ServerConfig> {
return cachedSC;
}
export function getServerConfigFromServer(): ServerConfig {
const gameEnv = process.env.GAME_ENV;
const gameEnv = process.env.GAME_ENV ?? "dev";
return getServerConfig(gameEnv);
}
export function getServerConfig(gameEnv: string) {
+49 -40
View File
@@ -50,6 +50,7 @@ export abstract class DefaultServerConfig implements ServerConfig {
async jwkPublicKey(): Promise<JWK> {
if (this.publicKey) return this.publicKey;
const jwksUrl = this.jwtIssuer() + "/.well-known/jwks.json";
console.log(`Fetching JWKS from ${jwksUrl}`);
const response = await fetch(jwksUrl);
const jwks = JwksSchema.parse(await response.json());
this.publicKey = jwks.keys[0];
@@ -61,42 +62,42 @@ export abstract class DefaultServerConfig implements ServerConfig {
);
}
otelEndpoint(): string {
return process.env.OTEL_ENDPOINT;
return process.env.OTEL_ENDPOINT ?? "undefined";
}
otelUsername(): string {
return process.env.OTEL_USERNAME;
return process.env.OTEL_USERNAME ?? "undefined";
}
otelPassword(): string {
return process.env.OTEL_PASSWORD;
return process.env.OTEL_PASSWORD ?? "undefined";
}
region(): string {
if (this.env() == GameEnv.Dev) {
if (this.env() === GameEnv.Dev) {
return "dev";
}
return process.env.REGION;
return process.env.REGION ?? "undefined";
}
gitCommit(): string {
return process.env.GIT_COMMIT;
return process.env.GIT_COMMIT ?? "undefined";
}
r2Endpoint(): string {
return `https://${process.env.CF_ACCOUNT_ID}.r2.cloudflarestorage.com`;
}
r2AccessKey(): string {
return process.env.R2_ACCESS_KEY;
return process.env.R2_ACCESS_KEY ?? "undefined";
}
r2SecretKey(): string {
return process.env.R2_SECRET_KEY;
return process.env.R2_SECRET_KEY ?? "undefined";
}
r2Bucket(): string {
return process.env.R2_BUCKET;
return process.env.R2_BUCKET ?? "undefined";
}
adminHeader(): string {
return "x-admin-key";
}
adminToken(): string {
return process.env.ADMIN_TOKEN;
return process.env.ADMIN_TOKEN ?? "undefined";
}
abstract numWorkers(): number;
abstract env(): GameEnv;
@@ -159,13 +160,13 @@ export abstract class DefaultServerConfig implements ServerConfig {
return Math.random() < 0.5 ? 30 : 15;
}
// world belongs with the ~2 mils, but these amounts never made sense so I assume the insanity is intended.
if (map == GameMapType.World) {
if (map === GameMapType.World) {
return Math.random() < 0.2 ? 150 : 50;
}
// default return for non specified map
return Math.random() < 0.2 ? 50 : 20;
};
return Math.min(150, numPlayers() * (mode == GameMode.Team ? 2 : 1));
return Math.min(150, numPlayers() * (mode === GameMode.Team ? 2 : 1));
}
workerIndex(gameID: GameID): number {
@@ -186,7 +187,7 @@ export class DefaultConfig implements Config {
constructor(
private _serverConfig: ServerConfig,
private _gameConfig: GameConfig,
private _userSettings: UserSettings,
private _userSettings: UserSettings | null,
private _isReplay: boolean,
) {}
isReplay(): boolean {
@@ -219,7 +220,10 @@ export class DefaultConfig implements Config {
return this._serverConfig;
}
userSettings(): UserSettings | null {
userSettings(): UserSettings {
if (this._userSettings === null) {
throw new Error("userSettings is null");
}
return this._userSettings;
}
@@ -299,7 +303,7 @@ export class DefaultConfig implements Config {
case UnitType.Warship:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold()
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: Math.min(
1_000_000,
@@ -323,7 +327,7 @@ export class DefaultConfig implements Config {
case UnitType.Port:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold()
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: Math.min(
1_000_000,
@@ -338,19 +342,21 @@ export class DefaultConfig implements Config {
case UnitType.AtomBomb:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold() ? 0 : 750_000,
p.type() === PlayerType.Human && this.infiniteGold() ? 0 : 750_000,
territoryBound: false,
};
case UnitType.HydrogenBomb:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold() ? 0 : 5_000_000,
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: 5_000_000,
territoryBound: false,
};
case UnitType.MIRV:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold()
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: 25_000_000,
territoryBound: false,
@@ -368,14 +374,16 @@ export class DefaultConfig implements Config {
case UnitType.MissileSilo:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold() ? 0 : 1_000_000,
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: 1_000_000,
territoryBound: true,
constructionDuration: this.instantBuild() ? 0 : 10 * 10,
};
case UnitType.DefensePost:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold()
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: Math.min(
250_000,
@@ -389,7 +397,7 @@ export class DefaultConfig implements Config {
case UnitType.SAMLauncher:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold()
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: Math.min(
3_000_000,
@@ -403,7 +411,7 @@ export class DefaultConfig implements Config {
case UnitType.City:
return {
cost: (p: Player) =>
p.type() == PlayerType.Human && this.infiniteGold()
p.type() === PlayerType.Human && this.infiniteGold()
? 0
: Math.min(
1_000_000,
@@ -453,7 +461,7 @@ export class DefaultConfig implements Config {
}
percentageTilesOwnedToWin(): number {
if (this._gameConfig.gameMode == GameMode.Team) {
if (this._gameConfig.gameMode === GameMode.Team) {
return 95;
}
return 80;
@@ -462,13 +470,13 @@ export class DefaultConfig implements Config {
return 3;
}
numSpawnPhaseTurns(): number {
return this._gameConfig.gameType == GameType.Singleplayer ? 100 : 300;
return this._gameConfig.gameType === GameType.Singleplayer ? 100 : 300;
}
numBots(): number {
return this.bots();
}
theme(): Theme {
return this.userSettings().darkMode() ? pastelThemeDark : pastelTheme;
return this.userSettings()?.darkMode() ? pastelThemeDark : pastelTheme;
}
attackLogic(
@@ -507,7 +515,7 @@ export class DefaultConfig implements Config {
gm.config().defensePostRange(),
UnitType.DefensePost,
)) {
if (dp.unit.owner() == defender) {
if (dp.unit.owner() === defender) {
mag *= this.defensePostDefenseBonus();
speed *= this.defensePostDefenseBonus();
break;
@@ -523,14 +531,14 @@ export class DefaultConfig implements Config {
if (attacker.isPlayer() && defender.isPlayer()) {
if (
attacker.type() == PlayerType.Human &&
defender.type() == PlayerType.Bot
attacker.type() === PlayerType.Human &&
defender.type() === PlayerType.Bot
) {
mag *= 0.8;
}
if (
attacker.type() == PlayerType.FakeHuman &&
defender.type() == PlayerType.Bot
attacker.type() === PlayerType.FakeHuman &&
defender.type() === PlayerType.Bot
) {
mag *= 0.8;
}
@@ -563,7 +571,7 @@ export class DefaultConfig implements Config {
} else {
return {
attackerTroopLoss:
attacker.type() == PlayerType.Bot ? mag / 10 : mag / 5,
attacker.type() === PlayerType.Bot ? mag / 10 : mag / 5,
defenderTroopLoss: 0,
tilesPerTickUsed: within(
(2000 * Math.max(10, speed)) / attackTroops,
@@ -608,7 +616,7 @@ export class DefaultConfig implements Config {
}
attackAmount(attacker: Player, defender: Player | TerraNullius) {
if (attacker.type() == PlayerType.Bot) {
if (attacker.type() === PlayerType.Bot) {
return attacker.troops() / 20;
} else {
return attacker.troops() / 5;
@@ -616,10 +624,10 @@ export class DefaultConfig implements Config {
}
startManpower(playerInfo: PlayerInfo): number {
if (playerInfo.playerType == PlayerType.Bot) {
if (playerInfo.playerType === PlayerType.Bot) {
return 10_000;
}
if (playerInfo.playerType == PlayerType.FakeHuman) {
if (playerInfo.playerType === PlayerType.FakeHuman) {
switch (this._gameConfig.difficulty) {
case Difficulty.Easy:
return 2_500 * (playerInfo?.nation?.strength ?? 1);
@@ -636,16 +644,16 @@ export class DefaultConfig implements Config {
maxPopulation(player: Player | PlayerView): number {
const maxPop =
player.type() == PlayerType.Human && this.infiniteTroops()
player.type() === PlayerType.Human && this.infiniteTroops()
? 1_000_000_000
: 2 * (Math.pow(player.numTilesOwned(), 0.6) * 1000 + 50000) +
player.units(UnitType.City).length * this.cityPopulationIncrease();
if (player.type() == PlayerType.Bot) {
if (player.type() === PlayerType.Bot) {
return maxPop / 2;
}
if (player.type() == PlayerType.Human) {
if (player.type() === PlayerType.Human) {
return maxPop;
}
@@ -669,11 +677,11 @@ export class DefaultConfig implements Config {
const ratio = 1 - player.population() / max;
toAdd *= ratio;
if (player.type() == PlayerType.Bot) {
if (player.type() === PlayerType.Bot) {
toAdd *= 0.7;
}
if (player.type() == PlayerType.FakeHuman) {
if (player.type() === PlayerType.FakeHuman) {
switch (this._gameConfig.difficulty) {
case Difficulty.Easy:
toAdd *= 0.9;
@@ -721,6 +729,7 @@ export class DefaultConfig implements Config {
case UnitType.HydrogenBomb:
return { inner: 80, outer: 100 };
}
throw new Error(`Unknown nuke type: ${unitType}`);
}
defaultNukeSpeed(): number {
+1 -1
View File
@@ -44,7 +44,7 @@ export class DevConfig extends DefaultConfig {
constructor(
sc: ServerConfig,
gc: GameConfig,
us: UserSettings,
us: UserSettings | null,
isReplay: boolean,
) {
super(sc, gc, us, isReplay);
+8 -5
View File
@@ -65,20 +65,23 @@ export const pastelTheme = new (class implements Theme {
}
territoryColor(player: PlayerView): Colord {
if (player.team() !== null) {
return this.teamColor(player.team());
const team = player.team();
if (team !== null) {
return this.teamColor(team);
}
if (player.info().playerType == PlayerType.Human) {
if (player.info().playerType === PlayerType.Human) {
return humanColors[simpleHash(player.id()) % humanColors.length];
}
if (player.info().playerType == PlayerType.Bot) {
if (player.info().playerType === PlayerType.Bot) {
return botColors[simpleHash(player.id()) % botColors.length];
}
return territoryColors[simpleHash(player.id()) % territoryColors.length];
}
textColor(player: PlayerView): string {
return player.info().playerType == PlayerType.Human ? "#000000" : "#4D4D4D";
return player.info().playerType === PlayerType.Human
? "#000000"
: "#4D4D4D";
}
specialBuildingColor(player: PlayerView): Colord {
+8 -5
View File
@@ -65,20 +65,23 @@ export const pastelThemeDark = new (class implements Theme {
}
territoryColor(player: PlayerView): Colord {
if (player.team() !== null) {
return this.teamColor(player.team());
const team = player.team();
if (team !== null) {
return this.teamColor(team);
}
if (player.info().playerType == PlayerType.Human) {
if (player.info().playerType === PlayerType.Human) {
return humanColors[simpleHash(player.id()) % humanColors.length];
}
if (player.info().playerType == PlayerType.Bot) {
if (player.info().playerType === PlayerType.Bot) {
return botColors[simpleHash(player.id()) % botColors.length];
}
return territoryColors[simpleHash(player.id()) % territoryColors.length];
}
textColor(player: PlayerView): string {
return player.info().playerType == PlayerType.Human ? "#ffffff" : "#e6e6e6";
return player.info().playerType === PlayerType.Human
? "#ffffff"
: "#e6e6e6";
}
specialBuildingColor(player: PlayerView): Colord {