From 006d904072a46734249b485713b44552268c125c Mon Sep 17 00:00:00 2001 From: Scott Anderson <662325+scottanderson@users.noreply.github.com> Date: Tue, 5 Aug 2025 14:20:50 -0400 Subject: [PATCH] Revert "Fix remaining errors and enable strict mode (#1628)" This reverts commit ad2598361b2f2b065d955bcad8a1ac29b35f7d3a. --- src/client/Main.ts | 1 - src/core/GameRunner.ts | 5 ++--- src/core/Schemas.ts | 6 +----- src/core/execution/FakeHumanExecution.ts | 2 +- src/core/execution/PlayerExecution.ts | 8 ++------ src/core/execution/SAMLauncherExecution.ts | 6 +----- src/core/game/BinaryLoaderGameMapLoader.ts | 4 +--- src/core/game/FetchGameMapLoader.ts | 4 +--- src/core/game/Game.ts | 12 +++++------- src/core/game/GameImpl.ts | 4 ++-- src/core/game/GameUpdates.ts | 2 +- src/core/game/GameView.ts | 4 ++-- src/core/game/PlayerImpl.ts | 4 ++-- src/core/game/UnitGrid.ts | 12 +++++------- src/core/utilities/Line.ts | 2 +- src/core/worker/Worker.worker.ts | 8 ++------ tests/UnitGrid.test.ts | 17 ++++++++++++----- tsconfig.json | 3 +-- 18 files changed, 42 insertions(+), 62 deletions(-) diff --git a/src/client/Main.ts b/src/client/Main.ts index 732a58c12..07555e520 100644 --- a/src/client/Main.ts +++ b/src/client/Main.ts @@ -64,7 +64,6 @@ declare global { // Extend the global interfaces to include your custom events interface DocumentEventMap { "join-lobby": CustomEvent; - "kick-player": CustomEvent; } } diff --git a/src/core/GameRunner.ts b/src/core/GameRunner.ts index 2e8201240..d2fd60c6e 100644 --- a/src/core/GameRunner.ts +++ b/src/core/GameRunner.ts @@ -4,7 +4,6 @@ import { Executor } from "./execution/ExecutionManager"; import { WinCheckExecution } from "./execution/WinCheckExecution"; import { AllPlayers, - Attack, Cell, Game, GameUpdates, @@ -36,7 +35,7 @@ export async function createGameRunner( gameStart: GameStartInfo, clientID: ClientID, mapLoader: GameMapLoader, - callBack: (gu: GameUpdateViewData | ErrorUpdate) => void, + callBack: (gu: GameUpdateViewData) => void, ): Promise { const config = await getConfig(gameStart.config, null); const gameMap = await loadGameMap(gameStart.config.gameMap, mapLoader); @@ -232,7 +231,7 @@ export class GameRunner { throw new Error(`player with id ${playerID} not found`); } - const condition = (a: Attack) => a.id() === attackID; + const condition = (a) => a.id() === attackID; const attack = player.outgoingAttacks().find(condition) ?? player.incomingAttacks().find(condition); diff --git a/src/core/Schemas.ts b/src/core/Schemas.ts index e59b06d28..23116bde1 100644 --- a/src/core/Schemas.ts +++ b/src/core/Schemas.ts @@ -215,11 +215,7 @@ export const RequiredPatternSchema = z new PatternDecoder(val, base64url.decode); return true; } catch (e) { - if (e instanceof Error) { - console.error(JSON.stringify(e.message, null, 2)); - } else { - console.error(String(e)); - } + console.error(JSON.stringify(e.message, null, 2)); return false; } }, diff --git a/src/core/execution/FakeHumanExecution.ts b/src/core/execution/FakeHumanExecution.ts index 828631757..b5920b5f7 100644 --- a/src/core/execution/FakeHumanExecution.ts +++ b/src/core/execution/FakeHumanExecution.ts @@ -350,7 +350,7 @@ export class FakeHumanExecution implements Execution { const dist = euclDistFN(tile, 25, false); let tileValue = targets .filter((unit) => dist(this.mg, unit.tile())) - .map((unit): number => { + .map((unit) => { switch (unit.type()) { case UnitType.City: return 25_000; diff --git a/src/core/execution/PlayerExecution.ts b/src/core/execution/PlayerExecution.ts index db021bd7f..913ea0c1d 100644 --- a/src/core/execution/PlayerExecution.ts +++ b/src/core/execution/PlayerExecution.ts @@ -1,7 +1,7 @@ import { Config } from "../configuration/Config"; import { Execution, Game, Player, UnitType } from "../game/Game"; import { GameImpl } from "../game/GameImpl"; -import { GameMap, TileRef } from "../game/GameMap"; +import { TileRef } from "../game/GameMap"; import { calculateBoundingBox, getMode, inscribed, simpleHash } from "../Util"; export class PlayerExecution implements Execution { @@ -190,11 +190,7 @@ export class PlayerExecution implements Execution { } const firstTile = cluster.values().next().value; - if (!firstTile) { - return; - } - - const filter = (_: GameMap, t: TileRef): boolean => + const filter = (_, t: TileRef): boolean => this.mg?.ownerID(t) === this.player?.smallID(); const tiles = this.mg.bfs(firstTile, filter); diff --git a/src/core/execution/SAMLauncherExecution.ts b/src/core/execution/SAMLauncherExecution.ts index 15891db07..484028aec 100644 --- a/src/core/execution/SAMLauncherExecution.ts +++ b/src/core/execution/SAMLauncherExecution.ts @@ -1,7 +1,6 @@ import { Execution, Game, - isUnit, MessageType, Player, Unit, @@ -81,9 +80,7 @@ class SAMTargetingSystem { [UnitType.AtomBomb, UnitType.HydrogenBomb], ({ unit }) => { return ( - isUnit(unit) && - unit.owner() !== this.player && - !this.player.isFriendly(unit.owner()) + unit.owner() !== this.player && !this.player.isFriendly(unit.owner()) ); }, ); @@ -215,7 +212,6 @@ export class SAMLauncherExecution implements Execution { this.MIRVWarheadSearchRadius, UnitType.MIRVWarhead, ({ unit }) => { - if (!isUnit(unit)) return false; if (unit.owner() === this.player) return false; if (this.player.isFriendly(unit.owner())) return false; const dst = unit.targetTile(); diff --git a/src/core/game/BinaryLoaderGameMapLoader.ts b/src/core/game/BinaryLoaderGameMapLoader.ts index 31e058026..2107b6faa 100644 --- a/src/core/game/BinaryLoaderGameMapLoader.ts +++ b/src/core/game/BinaryLoaderGameMapLoader.ts @@ -31,9 +31,7 @@ export class BinaryLoaderGameMapLoader implements GameMapLoader { return cachedMap; } - const key = Object.keys(GameMapType).find( - (k) => GameMapType[k as keyof typeof GameMapType] === map, - ); + const key = Object.keys(GameMapType).find((k) => GameMapType[k] === map); const fileName = key?.toLowerCase(); const mapData = { diff --git a/src/core/game/FetchGameMapLoader.ts b/src/core/game/FetchGameMapLoader.ts index cfde1360f..5d6c72367 100644 --- a/src/core/game/FetchGameMapLoader.ts +++ b/src/core/game/FetchGameMapLoader.ts @@ -17,9 +17,7 @@ export class FetchGameMapLoader implements GameMapLoader { return cachedMap; } - const key = Object.keys(GameMapType).find( - (k) => GameMapType[k as keyof typeof GameMapType] === map, - ); + const key = Object.keys(GameMapType).find((k) => GameMapType[k] === map); const fileName = key?.toLowerCase(); if (!fileName) { diff --git a/src/core/game/Game.ts b/src/core/game/Game.ts index 67af289d9..4ac4b18ff 100644 --- a/src/core/game/Game.ts +++ b/src/core/game/Game.ts @@ -9,7 +9,6 @@ import { } from "./GameUpdates"; import { RailNetwork } from "./RailNetwork"; import { Stats } from "./Stats"; -import { UnitPredicate } from "./UnitGrid"; export type PlayerID = string; export type Tick = number; @@ -390,10 +389,9 @@ export class PlayerInfo { } } -export function isUnit(unit: unknown): unit is Unit { +export function isUnit(unit: Unit | UnitParams): unit is Unit { return ( - unit && - typeof unit === "object" && + unit !== undefined && "isUnit" in unit && typeof unit.isUnit === "function" && unit.isUnit() @@ -666,12 +664,12 @@ export interface Game extends GameMap { searchRange: number, type: UnitType, playerId: PlayerID, - ): boolean; + ); nearbyUnits( tile: TileRef, searchRange: number, types: UnitType | UnitType[], - predicate?: UnitPredicate, + predicate?: (value: { unit: Unit; distSquared: number }) => boolean, ): Array<{ unit: Unit; distSquared: number }>; addExecution(...exec: Execution[]): void; @@ -707,7 +705,7 @@ export interface Game extends GameMap { addUpdate(update: GameUpdate): void; railNetwork(): RailNetwork; - conquerPlayer(conqueror: Player, conquered: Player): void; + conquerPlayer(conqueror: Player, conquered: Player); } export interface PlayerActions { diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index b85073e75..1d6349608 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -40,7 +40,7 @@ import { Stats } from "./Stats"; import { StatsImpl } from "./StatsImpl"; import { assignTeams } from "./TeamAssignment"; import { TerraNulliusImpl } from "./TerraNulliusImpl"; -import { UnitGrid, UnitPredicate } from "./UnitGrid"; +import { UnitGrid } from "./UnitGrid"; export function createGame( humans: PlayerInfo[], @@ -758,7 +758,7 @@ export class GameImpl implements Game { tile: TileRef, searchRange: number, types: UnitType | UnitType[], - predicate?: UnitPredicate, + predicate?: (value: { unit: Unit; distSquared: number }) => boolean, ): Array<{ unit: Unit; distSquared: number }> { return this.unitGrid.nearbyUnits( tile, diff --git a/src/core/game/GameUpdates.ts b/src/core/game/GameUpdates.ts index 565960479..daddeba69 100644 --- a/src/core/game/GameUpdates.ts +++ b/src/core/game/GameUpdates.ts @@ -18,7 +18,7 @@ export interface GameUpdateViewData { tick: number; updates: GameUpdates; packedTileUpdates: BigUint64Array; - playerNameViewData: Record; + playerNameViewData: Record; } export interface ErrorUpdate { diff --git a/src/core/game/GameView.ts b/src/core/game/GameView.ts index 34dc7e005..b5a33ef99 100644 --- a/src/core/game/GameView.ts +++ b/src/core/game/GameView.ts @@ -34,7 +34,7 @@ import { } from "./GameUpdates"; import { TerrainMapData } from "./TerrainMapLoader"; import { TerraNulliusImpl } from "./TerraNulliusImpl"; -import { UnitGrid, UnitPredicate } from "./UnitGrid"; +import { UnitGrid } from "./UnitGrid"; import { UserSettings } from "./UserSettings"; const userSettings: UserSettings = new UserSettings(); @@ -476,7 +476,7 @@ export class GameView implements GameMap { tile: TileRef, searchRange: number, types: UnitType | UnitType[], - predicate?: UnitPredicate, + predicate?: (value: { unit: UnitView; distSquared: number }) => boolean, ): Array<{ unit: UnitView; distSquared: number }> { return this.unitGrid.nearbyUnits( tile, diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index 6e038e455..f9d121a6d 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -213,9 +213,9 @@ export class PlayerImpl implements Player { return this._units.filter((u) => ts.has(u.type())); } - private numUnitsConstructed: Partial> = {}; + private numUnitsConstructed: number[] = []; private recordUnitConstructed(type: UnitType): void { - if (this.numUnitsConstructed[type] !== undefined) { + if (type in this.numUnitsConstructed) { this.numUnitsConstructed[type]++; } else { this.numUnitsConstructed[type] = 1; diff --git a/src/core/game/UnitGrid.ts b/src/core/game/UnitGrid.ts index 15617c752..027f1b33f 100644 --- a/src/core/game/UnitGrid.ts +++ b/src/core/game/UnitGrid.ts @@ -2,11 +2,6 @@ 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>[][]; private readonly cellSize = 100; @@ -135,8 +130,11 @@ export class UnitGrid { nearbyUnits( tile: TileRef, searchRange: number, - types: readonly UnitType[] | UnitType, - predicate?: UnitPredicate, + types: UnitType | UnitType[], + predicate?: (value: { + unit: Unit | UnitView; + distSquared: number; + }) => boolean, ): Array<{ unit: Unit | UnitView; distSquared: number }> { const nearby: Array<{ unit: Unit | UnitView; distSquared: number }> = []; const { startGridX, endGridX, startGridY, endGridY } = this.getCellsInRange( diff --git a/src/core/utilities/Line.ts b/src/core/utilities/Line.ts index 67024e9c6..e8c673533 100644 --- a/src/core/utilities/Line.ts +++ b/src/core/utilities/Line.ts @@ -124,7 +124,7 @@ export class DistanceBasedBezierCurve extends CubicBezierCurve { /** * Precompute all points spaced @p pixelSpacing apart */ - computeAllPoints(pixelSpacing: number, precision: number): void { + computeAllPoints(pixelSpacing: number, precision): void { this.cachedPoints = []; this.totalDistance = 0; this.currentIndex = 0; diff --git a/src/core/worker/Worker.worker.ts b/src/core/worker/Worker.worker.ts index 1014968fb..29e67f214 100644 --- a/src/core/worker/Worker.worker.ts +++ b/src/core/worker/Worker.worker.ts @@ -1,7 +1,7 @@ import version from "../../../resources/version.txt"; import { createGameRunner, GameRunner } from "../GameRunner"; import { FetchGameMapLoader } from "../game/FetchGameMapLoader"; -import { ErrorUpdate, GameUpdateViewData } from "../game/GameUpdates"; +import { GameUpdateViewData } from "../game/GameUpdates"; import { AttackAveragePositionResultMessage, InitializedMessage, @@ -17,11 +17,7 @@ const ctx: Worker = self as any; let gameRunner: Promise | null = null; const mapLoader = new FetchGameMapLoader(`/maps`, version); -function gameUpdate(gu: GameUpdateViewData | ErrorUpdate) { - // skip if ErrorUpdate - if (!("updates" in gu)) { - return; - } +function gameUpdate(gu: GameUpdateViewData) { sendMessage({ type: "game_update", gameUpdate: gu, diff --git a/tests/UnitGrid.test.ts b/tests/UnitGrid.test.ts index bd6629910..9a0869ace 100644 --- a/tests/UnitGrid.test.ts +++ b/tests/UnitGrid.test.ts @@ -29,7 +29,7 @@ async function nearbyUnits( unitPosX: number, rangeCheck: number, range: number, - unitTypes: readonly UnitType[], + unitTypes: UnitType[], ) { const game = await setup(mapName, { infiniteGold: true, instantBuild: true }); const grid = new UnitGrid(game.map()); @@ -51,7 +51,7 @@ describe("Unit Grid range tests", () => { ["plains", 0, 10, 11, false], // Exactly 1px outside ["big_plains", 0, 198, 42, true], // Inside huge range ["big_plains", 0, 198, 199, false], // Exactly 1px outside huge range - ] as const; + ]; describe("Is unit in range", () => { test.each(hasUnitCases)( @@ -77,18 +77,25 @@ describe("Unit Grid range tests", () => { ["plains", 0, 10, 11, [UnitType.DefensePost], 0], // 1px outside ["big_plains", 0, 198, 42, [UnitType.TradeShip], 1], // Inside huge range ["big_plains", 0, 198, 199, [UnitType.TransportShip], 0], // 1px outside - ] as const; + ]; describe("Retrieve all units in range", () => { test.each(unitsInRangeCases)( "on %p map, look if unit at position %p with a range of %p is in range of %p position, returns %p", - async (mapName, unitPosX, range, rangeCheck, units, expectedResult) => { + async ( + mapName: string, + unitPosX: number, + range: number, + rangeCheck: number, + units: UnitType[], + expectedResult: number, + ) => { const result = await nearbyUnits( mapName, unitPosX, rangeCheck, range, - units, // remove readonly + units, ); expect(result.length).toBe(expectedResult); }, diff --git a/tsconfig.json b/tsconfig.json index 6de45daab..7c73290f3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,8 +21,7 @@ "resolveJsonModule": true, "strictNullChecks": true, "useDefineForClassFields": false, - "strictPropertyInitialization": false, - "strict": true + "strictPropertyInitialization": false }, "include": [ "src/**/*",