Fix remaining errors and enable strict mode (#1628)

## Description:

#1075 

Fixing all remaining type errors caused by strict mode and enable it.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I have read and accepted the CLA agreement (only required once).

## Please put your Discord username so you can be contacted if a bug or
regression is found:

azlod

---------

Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
This commit is contained in:
Antoine
2025-08-03 23:06:31 +00:00
committed by GitHub
co-authored by Scott Anderson
parent 7eb1bf732d
commit ad2598361b
18 changed files with 63 additions and 43 deletions
+3 -1
View File
@@ -31,7 +31,9 @@ export class BinaryLoaderGameMapLoader implements GameMapLoader {
return cachedMap;
}
const key = Object.keys(GameMapType).find((k) => GameMapType[k] === map);
const key = Object.keys(GameMapType).find(
(k) => GameMapType[k as keyof typeof GameMapType] === map,
);
const fileName = key?.toLowerCase();
const mapData = {
+3 -1
View File
@@ -17,7 +17,9 @@ export class FetchGameMapLoader implements GameMapLoader {
return cachedMap;
}
const key = Object.keys(GameMapType).find((k) => GameMapType[k] === map);
const key = Object.keys(GameMapType).find(
(k) => GameMapType[k as keyof typeof GameMapType] === map,
);
const fileName = key?.toLowerCase();
if (!fileName) {
+7 -5
View File
@@ -9,6 +9,7 @@ import {
} from "./GameUpdates";
import { RailNetwork } from "./RailNetwork";
import { Stats } from "./Stats";
import { UnitPredicate } from "./UnitGrid";
export type PlayerID = string;
export type Tick = number;
@@ -389,9 +390,10 @@ export class PlayerInfo {
}
}
export function isUnit(unit: Unit | UnitParams<UnitType>): unit is Unit {
export function isUnit(unit: unknown): unit is Unit {
return (
unit !== undefined &&
unit &&
typeof unit === "object" &&
"isUnit" in unit &&
typeof unit.isUnit === "function" &&
unit.isUnit()
@@ -662,12 +664,12 @@ export interface Game extends GameMap {
searchRange: number,
type: UnitType,
playerId: PlayerID,
);
): boolean;
nearbyUnits(
tile: TileRef,
searchRange: number,
types: UnitType | UnitType[],
predicate?: (value: { unit: Unit; distSquared: number }) => boolean,
predicate?: UnitPredicate,
): Array<{ unit: Unit; distSquared: number }>;
addExecution(...exec: Execution[]): void;
@@ -703,7 +705,7 @@ export interface Game extends GameMap {
addUpdate(update: GameUpdate): void;
railNetwork(): RailNetwork;
conquerPlayer(conqueror: Player, conquered: Player);
conquerPlayer(conqueror: Player, conquered: Player): void;
}
export interface PlayerActions {
+2 -2
View File
@@ -40,7 +40,7 @@ import { Stats } from "./Stats";
import { StatsImpl } from "./StatsImpl";
import { assignTeams } from "./TeamAssignment";
import { TerraNulliusImpl } from "./TerraNulliusImpl";
import { UnitGrid } from "./UnitGrid";
import { UnitGrid, UnitPredicate } from "./UnitGrid";
export function createGame(
humans: PlayerInfo[],
@@ -758,7 +758,7 @@ export class GameImpl implements Game {
tile: TileRef,
searchRange: number,
types: UnitType | UnitType[],
predicate?: (value: { unit: Unit; distSquared: number }) => boolean,
predicate?: UnitPredicate,
): Array<{ unit: Unit; distSquared: number }> {
return this.unitGrid.nearbyUnits(
tile,
+1 -1
View File
@@ -18,7 +18,7 @@ export interface GameUpdateViewData {
tick: number;
updates: GameUpdates;
packedTileUpdates: BigUint64Array;
playerNameViewData: Record<number, NameViewData>;
playerNameViewData: Record<string, NameViewData>;
}
export interface ErrorUpdate {
+2 -2
View File
@@ -34,7 +34,7 @@ import {
} from "./GameUpdates";
import { TerrainMapData } from "./TerrainMapLoader";
import { TerraNulliusImpl } from "./TerraNulliusImpl";
import { UnitGrid } from "./UnitGrid";
import { UnitGrid, UnitPredicate } from "./UnitGrid";
import { UserSettings } from "./UserSettings";
const userSettings: UserSettings = new UserSettings();
@@ -472,7 +472,7 @@ export class GameView implements GameMap {
tile: TileRef,
searchRange: number,
types: UnitType | UnitType[],
predicate?: (value: { unit: UnitView; distSquared: number }) => boolean,
predicate?: UnitPredicate,
): Array<{ unit: UnitView; distSquared: number }> {
return this.unitGrid.nearbyUnits(
tile,
+2 -2
View File
@@ -211,9 +211,9 @@ export class PlayerImpl implements Player {
return this._units.filter((u) => ts.has(u.type()));
}
private numUnitsConstructed: number[] = [];
private numUnitsConstructed: Partial<Record<UnitType, number>> = {};
private recordUnitConstructed(type: UnitType): void {
if (type in this.numUnitsConstructed) {
if (this.numUnitsConstructed[type] !== undefined) {
this.numUnitsConstructed[type]++;
} else {
this.numUnitsConstructed[type] = 1;
+7 -5
View File
@@ -2,6 +2,11 @@ import { PlayerID, Unit, UnitType } from "./Game";
import { GameMap, TileRef } from "./GameMap";
import { UnitView } from "./GameView";
export type UnitPredicate = (value: {
unit: Unit | UnitView;
distSquared: number;
}) => boolean;
export class UnitGrid {
private grid: Map<UnitType, Set<Unit | UnitView>>[][];
private readonly cellSize = 100;
@@ -130,11 +135,8 @@ export class UnitGrid {
nearbyUnits(
tile: TileRef,
searchRange: number,
types: UnitType | UnitType[],
predicate?: (value: {
unit: Unit | UnitView;
distSquared: number;
}) => boolean,
types: readonly UnitType[] | UnitType,
predicate?: UnitPredicate,
): Array<{ unit: Unit | UnitView; distSquared: number }> {
const nearby: Array<{ unit: Unit | UnitView; distSquared: number }> = [];
const { startGridX, endGridX, startGridY, endGridY } = this.getCellsInRange(