mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 17:46:46 +00:00
70745faac4
## 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>
145 lines
3.9 KiB
TypeScript
145 lines
3.9 KiB
TypeScript
import { SpawnExecution } from "../src/core/execution/SpawnExecution";
|
|
import {
|
|
Game,
|
|
Player,
|
|
PlayerInfo,
|
|
PlayerType,
|
|
UnitType,
|
|
} from "../src/core/game/Game";
|
|
import { setup } from "./util/Setup";
|
|
import { constructionExecution } from "./util/utils";
|
|
|
|
const coastX = 7;
|
|
let game: Game;
|
|
let player1: Player;
|
|
let player2: Player;
|
|
|
|
describe("Warship", () => {
|
|
beforeEach(async () => {
|
|
game = await setup("half_land_half_ocean", {
|
|
infiniteGold: true,
|
|
instantBuild: true,
|
|
});
|
|
const player_1_info = new PlayerInfo(
|
|
"us",
|
|
"boat dude",
|
|
PlayerType.Human,
|
|
null,
|
|
"player_1_id",
|
|
);
|
|
game.addPlayer(player_1_info);
|
|
const player_2_info = new PlayerInfo(
|
|
"us",
|
|
"boat dude",
|
|
PlayerType.Human,
|
|
null,
|
|
"player_2_id",
|
|
);
|
|
game.addPlayer(player_2_info);
|
|
|
|
game.addExecution(
|
|
new SpawnExecution(
|
|
game.player(player_1_info.id).info(),
|
|
game.ref(coastX, 10),
|
|
),
|
|
new SpawnExecution(
|
|
game.player(player_2_info.id).info(),
|
|
game.ref(coastX, 15),
|
|
),
|
|
);
|
|
|
|
while (game.inSpawnPhase()) {
|
|
game.executeNextTick();
|
|
}
|
|
|
|
player1 = game.player(player_1_info.id);
|
|
player2 = game.player(player_2_info.id);
|
|
});
|
|
|
|
test("Warship heals only if player has port", async () => {
|
|
const maxHealth = game.config().unitInfo(UnitType.Warship).maxHealth;
|
|
if (typeof maxHealth !== "number") {
|
|
expect(typeof maxHealth).toBe("number");
|
|
throw new Error("unreachable");
|
|
}
|
|
|
|
const port = player1.buildUnit(UnitType.Port, game.ref(coastX, 10), {});
|
|
const warship = player1.buildUnit(
|
|
UnitType.Warship,
|
|
game.ref(coastX + 1, 10),
|
|
{},
|
|
);
|
|
|
|
game.executeNextTick();
|
|
|
|
expect(warship.health()).toBe(maxHealth);
|
|
warship.modifyHealth(-10);
|
|
expect(warship.health()).toBe(maxHealth - 10);
|
|
game.executeNextTick();
|
|
expect(warship.health()).toBe(maxHealth - 9);
|
|
|
|
port.delete();
|
|
|
|
game.executeNextTick();
|
|
expect(warship.health()).toBe(maxHealth - 9);
|
|
});
|
|
|
|
test("Warship captures trade if player has port", async () => {
|
|
constructionExecution(game, player1.id(), coastX, 10, UnitType.Port);
|
|
constructionExecution(game, player1.id(), coastX + 1, 10, UnitType.Warship);
|
|
// Warship need one more tick (for warship exec to actually build warship)
|
|
game.executeNextTick();
|
|
expect(player1.units(UnitType.Warship)).toHaveLength(1);
|
|
expect(player1.units(UnitType.Port)).toHaveLength(1);
|
|
|
|
const dstPort = player2.buildUnit(
|
|
UnitType.Port,
|
|
game.ref(coastX + 2, 10),
|
|
{},
|
|
);
|
|
|
|
// Cannot buildExec with trade ship as it's not buildable (but
|
|
// we can obviously directly add it to the player)
|
|
const tradeShip = player2.buildUnit(
|
|
UnitType.TradeShip,
|
|
game.ref(coastX + 1, 7),
|
|
{
|
|
dstPort,
|
|
},
|
|
);
|
|
|
|
expect(tradeShip.owner().id()).toBe(player2.id());
|
|
// Let plenty of time for A* to execute
|
|
for (let i = 0; i < 10; i++) {
|
|
game.executeNextTick();
|
|
}
|
|
expect(tradeShip.owner().id()).toBe(player1.id());
|
|
});
|
|
|
|
test("Warship do not capture trade if player has no port", async () => {
|
|
constructionExecution(game, player1.id(), coastX, 10, UnitType.Port);
|
|
constructionExecution(game, player1.id(), coastX + 1, 10, UnitType.Warship);
|
|
expect(player1.units(UnitType.Warship)).toHaveLength(1);
|
|
|
|
const [dstPort] = player1.units(UnitType.Port);
|
|
|
|
player1.units(UnitType.Port)[0].delete();
|
|
// Cannot buildExec with trade ship as it's not buildable (but
|
|
// we can obviously directly add it to the player)
|
|
const tradeShip = player2.buildUnit(
|
|
UnitType.TradeShip,
|
|
game.ref(coastX + 1, 11),
|
|
{
|
|
dstPort,
|
|
},
|
|
);
|
|
|
|
expect(tradeShip.owner().id()).toBe(player2.id());
|
|
// Let plenty of time for A* to execute
|
|
for (let i = 0; i < 10; i++) {
|
|
game.executeNextTick();
|
|
}
|
|
expect(tradeShip.owner().id()).toBe(player2.id());
|
|
});
|
|
});
|