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 19:39:40 -04:00
committed by GitHub
parent 369483b4ac
commit 70745faac4
119 changed files with 1428 additions and 1123 deletions
+1
View File
@@ -126,6 +126,7 @@ export async function readGameRecord(
Key: `${gameFolder}/${gameId}`, // Fixed - needed to include gameFolder
});
// Parse the response body
if (response.Body === undefined) return null;
const bodyContents = await response.Body.transformToString();
return JSON.parse(bodyContents) as GameRecord;
} catch (error) {
+3 -3
View File
@@ -16,7 +16,7 @@ export class GameManager {
}
public game(id: GameID): GameServer | null {
return this.games.get(id);
return this.games.get(id) ?? null;
}
addClient(client: Client, gameID: GameID, lastTurn: number): boolean {
@@ -62,7 +62,7 @@ export class GameManager {
const active = new Map<GameID, GameServer>();
for (const [id, game] of this.games) {
const phase = game.phase();
if (phase == GamePhase.Active) {
if (phase === GamePhase.Active) {
if (!game.hasStarted()) {
// Prestart tells clients to start loading the game.
game.prestart();
@@ -77,7 +77,7 @@ export class GameManager {
}
}
if (phase == GamePhase.Finished) {
if (phase === GamePhase.Finished) {
try {
game.end();
} catch (error) {
+36 -36
View File
@@ -42,13 +42,13 @@ export class GameServer {
// Used for record record keeping
private allClients: Map<ClientID, Client> = new Map();
private _hasStarted = false;
private _startTime: number = null;
private _startTime: number | null = null;
private endTurnIntervalID;
private lastPingUpdate = 0;
private winner: ClientSendWinnerMessage = null;
private winner: ClientSendWinnerMessage | null = null;
// This field is currently only filled at victory
private allPlayersStats: AllPlayersStats = {};
@@ -70,37 +70,37 @@ export class GameServer {
this.log = log_.child({ gameID: id });
}
public updateGameConfig(gameConfig: GameConfig): void {
if (gameConfig.gameMap != null) {
public updateGameConfig(gameConfig: Partial<GameConfig>): void {
if (gameConfig.gameMap !== undefined) {
this.gameConfig.gameMap = gameConfig.gameMap;
}
if (gameConfig.difficulty != null) {
if (gameConfig.difficulty !== undefined) {
this.gameConfig.difficulty = gameConfig.difficulty;
}
if (gameConfig.disableNPCs != null) {
if (gameConfig.disableNPCs !== undefined) {
this.gameConfig.disableNPCs = gameConfig.disableNPCs;
}
if (gameConfig.bots != null) {
if (gameConfig.bots !== undefined) {
this.gameConfig.bots = gameConfig.bots;
}
if (gameConfig.infiniteGold != null) {
if (gameConfig.infiniteGold !== undefined) {
this.gameConfig.infiniteGold = gameConfig.infiniteGold;
}
if (gameConfig.infiniteTroops != null) {
if (gameConfig.infiniteTroops !== undefined) {
this.gameConfig.infiniteTroops = gameConfig.infiniteTroops;
}
if (gameConfig.instantBuild != null) {
if (gameConfig.instantBuild !== undefined) {
this.gameConfig.instantBuild = gameConfig.instantBuild;
}
if (gameConfig.gameMode != null) {
if (gameConfig.gameMode !== undefined) {
this.gameConfig.gameMode = gameConfig.gameMode;
}
if (gameConfig.disabledUnits != null) {
if (gameConfig.disabledUnits !== undefined) {
this.gameConfig.disabledUnits = gameConfig.disabledUnits;
}
if (gameConfig.playerTeams != null) {
if (gameConfig.playerTeams !== undefined) {
this.gameConfig.playerTeams = gameConfig.playerTeams;
}
}
@@ -120,9 +120,9 @@ export class GameServer {
});
if (
this.gameConfig.gameType == GameType.Public &&
this.gameConfig.gameType === GameType.Public &&
this.activeClients.filter(
(c) => c.ip == client.ip && c.clientID != client.clientID,
(c) => c.ip === client.ip && c.clientID !== client.clientID,
).length >= 3
) {
this.log.warn("cannot add client, already have 3 ips", {
@@ -134,9 +134,9 @@ export class GameServer {
// Remove stale client if this is a reconnect
const existing = this.activeClients.find(
(c) => c.clientID == client.clientID,
(c) => c.clientID === client.clientID,
);
if (existing != null) {
if (existing !== undefined) {
if (client.persistentID !== existing.persistentID) {
this.log.error("persistent ids do not match", {
clientID: client.clientID,
@@ -149,7 +149,7 @@ export class GameServer {
}
existing.ws.removeAllListeners("message");
this.activeClients = this.activeClients.filter(
(c) => c.clientID != client.clientID,
(c) => c.clientID !== client.clientID,
);
}
this.activeClients.push(client);
@@ -161,14 +161,14 @@ export class GameServer {
"message",
gatekeeper.wsHandler(client.ip, async (message: string) => {
try {
let clientMsg: ClientMessage = null;
let clientMsg: ClientMessage | null = null;
try {
clientMsg = ClientMessageSchema.parse(JSON.parse(message));
} catch (error) {
throw Error(`error parsing schema for ${ipAnonymize(client.ip)}`);
}
if (clientMsg.type == "intent") {
if (clientMsg.intent.clientID != client.clientID) {
if (clientMsg.type === "intent") {
if (clientMsg.intent.clientID !== client.clientID) {
this.log.warn(
`client id mismatch, client: ${client.clientID}, intent: ${clientMsg.intent.clientID}`,
);
@@ -176,14 +176,14 @@ export class GameServer {
}
this.addIntent(clientMsg.intent);
}
if (clientMsg.type == "ping") {
if (clientMsg.type === "ping") {
this.lastPingUpdate = Date.now();
client.lastPing = Date.now();
}
if (clientMsg.type == "hash") {
if (clientMsg.type === "hash") {
client.hashes.set(clientMsg.turnNumber, clientMsg.hash);
}
if (clientMsg.type == "winner") {
if (clientMsg.type === "winner") {
this.winner = clientMsg;
this.allPlayersStats = clientMsg.allPlayersStats;
}
@@ -203,7 +203,7 @@ export class GameServer {
persistentID: client.persistentID,
});
this.activeClients = this.activeClients.filter(
(c) => c.clientID != client.clientID,
(c) => c.clientID !== client.clientID,
);
});
client.ws.on("error", (error: Error) => {
@@ -223,7 +223,7 @@ export class GameServer {
}
public startTime(): number {
if (this._startTime > 0) {
if (this._startTime !== null && this._startTime > 0) {
return this._startTime;
} else {
//game hasn't started yet, only works for public games
@@ -382,10 +382,10 @@ export class GameServer {
this.gameStartInfo,
playerRecords,
this.turns,
this._startTime,
this._startTime ?? 0,
Date.now(),
this.winner?.winner,
this.winner?.winnerType,
this.winner?.winner ?? null,
this.winner?.winnerType ?? null,
this.allPlayersStats,
),
);
@@ -421,7 +421,7 @@ export class GameServer {
phase(): GamePhase {
const now = Date.now();
const alive = [];
const alive: Client[] = [];
for (const client of this.activeClients) {
if (now - client.lastPing > 60_000) {
this.log.info("no pings received, terminating connection", {
@@ -444,9 +444,9 @@ export class GameServer {
}
const noRecentPings = now > this.lastPingUpdate + 20 * 1000;
const noActive = this.activeClients.length == 0;
const noActive = this.activeClients.length === 0;
if (this.gameConfig.gameType != GameType.Public) {
if (this.gameConfig.gameType !== GameType.Public) {
if (this._hasStarted) {
if (noActive && noRecentPings) {
this.log.info("private game complete", {
@@ -464,7 +464,7 @@ export class GameServer {
const msSinceCreation = now - this.createdAt;
const lessThanLifetime = msSinceCreation < this.config.gameCreationRate();
const notEnoughPlayers =
this.gameConfig.gameType == GameType.Public &&
this.gameConfig.gameType === GameType.Public &&
this.gameConfig.maxPlayers &&
this.activeClients.length < this.gameConfig.maxPlayers;
if (lessThanLifetime && notEnoughPlayers) {
@@ -498,7 +498,7 @@ export class GameServer {
}
public isPublic(): boolean {
return this.gameConfig.gameType == GameType.Public;
return this.gameConfig.gameType === GameType.Public;
}
public kickClient(clientID: ClientID): void {
@@ -530,7 +530,7 @@ export class GameServer {
if (this.activeClients.length <= 1) {
return;
}
if (this.turns.length % 10 != 0 || this.turns.length < 10) {
if (this.turns.length % 10 !== 0 || this.turns.length < 10) {
// Check hashes every 10 turns
return;
}
@@ -540,7 +540,7 @@ export class GameServer {
const { mostCommonHash, outOfSyncClients } =
this.findOutOfSyncClients(lastHashTurn);
if (outOfSyncClients.length == 0) {
if (outOfSyncClients.length === 0) {
this.turns[lastHashTurn].hash = mostCommonHash;
return;
}
+2 -2
View File
@@ -26,10 +26,10 @@ export interface Gatekeeper {
) => (message: string) => Promise<void>;
}
let gk: Gatekeeper = null;
let gk: Gatekeeper | null = null;
async function getGatekeeperCached(): Promise<Gatekeeper> {
if (gk != null) {
if (gk !== null) {
return gk;
}
return getGatekeeper().then((g) => {
+1 -1
View File
@@ -21,7 +21,7 @@ const loggerProvider = new LoggerProvider({
resource,
});
if (config.env() == GameEnv.Prod && config.otelEnabled()) {
if (config.env() === GameEnv.Prod && config.otelEnabled()) {
console.log("OTEL enabled");
// Configure OpenTelemetry endpoint with basic auth (if provided)
const headers = {};
+1 -1
View File
@@ -56,7 +56,7 @@ export class MapPlaylist {
infiniteGold: false,
infiniteTroops: false,
instantBuild: false,
disableNPCs: mode == GameMode.Team,
disableNPCs: mode === GameMode.Team,
disableNukes: false,
gameMode: mode,
playerTeams: numPlayerTeams,
+21 -3
View File
@@ -103,7 +103,7 @@ export async function startMaster() {
setInterval(
() =>
fetchLobbies().then((lobbies) => {
if (lobbies == 0) {
if (lobbies === 0) {
scheduleLobbies();
}
}),
@@ -194,7 +194,7 @@ app.post(
);
async function fetchLobbies(): Promise<number> {
const fetchPromises = [];
const fetchPromises: Promise<GameInfo | null>[] = [];
for (const gameID of publicLobbyIDs) {
const controller = new AbortController();
@@ -233,8 +233,26 @@ async function fetchLobbies(): Promise<number> {
});
lobbyInfos.forEach((l) => {
if (l.msUntilStart <= 250 || l.gameConfig.maxPlayers <= l.numClients) {
if (
"msUntilStart" in l &&
l.msUntilStart !== undefined &&
l.msUntilStart <= 250
) {
publicLobbyIDs.delete(l.gameID);
return;
}
if (
"gameConfig" in l &&
l.gameConfig !== undefined &&
"maxPlayers" in l.gameConfig &&
l.gameConfig.maxPlayers !== undefined &&
"numClients" in l &&
l.numClients !== undefined &&
l.gameConfig.maxPlayers <= l.numClients
) {
publicLobbyIDs.delete(l.gameID);
return;
}
});
+8 -8
View File
@@ -35,7 +35,7 @@ export function startWorker() {
const gm = new GameManager(config, log);
if (config.env() == GameEnv.Prod && config.otelEnabled()) {
if (config.env() === GameEnv.Prod && config.otelEnabled()) {
initWorkerMetrics(gm);
}
@@ -85,7 +85,7 @@ export function startWorker() {
const clientIP = req.ip || req.socket.remoteAddress || "unknown";
const gc = req.body?.gameConfig as GameConfig;
if (
gc?.gameType == GameType.Public &&
gc?.gameType === GameType.Public &&
req.headers[config.adminHeader()] !== config.adminToken()
) {
log.warn(
@@ -140,7 +140,7 @@ export function startWorker() {
gatekeeper.httpHandler(LimiterType.Put, async (req, res) => {
// TODO: only update public game if from local host
const lobbyID = req.params.id;
if (req.body.gameType == GameType.Public) {
if (req.body.gameType === GameType.Public) {
log.info(`cannot update game ${lobbyID} to public`);
return res.status(400).json({ error: "Cannot update public game" });
}
@@ -182,7 +182,7 @@ export function startWorker() {
gatekeeper.httpHandler(LimiterType.Get, async (req, res) => {
const lobbyId = req.params.id;
res.json({
exists: gm.game(lobbyId) != null,
exists: gm.game(lobbyId) !== null,
});
}),
);
@@ -191,7 +191,7 @@ export function startWorker() {
"/api/game/:id",
gatekeeper.httpHandler(LimiterType.Get, async (req, res) => {
const game = gm.game(req.params.id);
if (game == null) {
if (game === null) {
log.info(`lobby ${req.params.id} not found`);
return res.status(404).json({ error: "Game not found" });
}
@@ -213,8 +213,8 @@ export function startWorker() {
}
if (
config.env() != GameEnv.Dev &&
gameRecord.gitCommit != config.gitCommit()
config.env() !== GameEnv.Dev &&
gameRecord.gitCommit !== config.gitCommit()
) {
log.warn(
`git commit mismatch for game ${req.params.id}, expected ${config.gitCommit()}, got ${gameRecord.gitCommit}`,
@@ -295,7 +295,7 @@ export function startWorker() {
JSON.parse(message.toString()),
);
if (clientMsg.type == "join") {
if (clientMsg.type === "join") {
// Verify this worker should handle this game
const expectedWorkerId = config.workerIndex(clientMsg.gameID);
if (expectedWorkerId !== workerId) {