Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/core/execution/WinCheckExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export class WinCheckExecution implements Execution {
const timeElapsed = this.mg.elapsedGameSeconds();
const numTilesWithoutFallout =
this.mg.numLandTiles() - this.mg.numTilesWithFallout();
Comment on lines 68 to 69
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why nut just do:

if numTilesWithoutFallout === this.mg.numLandTiles() {
return
}


if (numTilesWithoutFallout <= 0) {
return;
}

if (
(max.numTilesOwned() / numTilesWithoutFallout) * 100 >
this.mg.config().percentageTilesOwnedToWin() ||
Expand Down Expand Up @@ -102,9 +107,14 @@ export class WinCheckExecution implements Execution {
const timeElapsed = this.mg.elapsedGameSeconds();
const numTilesWithoutFallout =
this.mg.numLandTiles() - this.mg.numTilesWithFallout();
const percentage = (max[1] / numTilesWithoutFallout) * 100;

if (numTilesWithoutFallout <= 0) {
return;
}

if (
percentage > this.mg.config().percentageTilesOwnedToWin() ||
(max[1] / numTilesWithoutFallout) * 100 >
this.mg.config().percentageTilesOwnedToWin() ||
Comment on lines +116 to +117
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Floating-point division on a changed line violates the src/core/ determinism guideline.

Line 118 still computes (max[1] / numTilesWithoutFallout) * 100, which is floating-point division. The PR description said this would be replaced with integer cross-multiplication, but it was not. The coding guideline for src/core/ requires avoiding floating-point math to keep simulation deterministic.

Replace with an integer comparison (no division, no floats):

-      (max[1] / numTilesWithoutFallout) * 100 >
-        this.mg.config().percentageTilesOwnedToWin() ||
+      max[1] * 100 >
+        this.mg.config().percentageTilesOwnedToWin() * numTilesWithoutFallout ||

As per coding guidelines: "Ensure deterministic behavior in src/core/ by using seeded PRNG and avoiding floating-point math."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
(max[1] / numTilesWithoutFallout) * 100 >
this.mg.config().percentageTilesOwnedToWin() ||
max[1] * 100 >
this.mg.config().percentageTilesOwnedToWin() * numTilesWithoutFallout ||
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/execution/WinCheckExecution.ts` around lines 118 - 119, The
comparison uses floating-point division in WinCheckExecution (the expression
"(max[1] / numTilesWithoutFallout) * 100") which violates src/core/ determinism
rules; replace it with an integer cross-multiplication to avoid floats: compare
max[1] * 100 against this.mg.config().percentageTilesOwnedToWin() *
numTilesWithoutFallout (using the same identifiers max, numTilesWithoutFallout
and this.mg.config().percentageTilesOwnedToWin()) so the logic is identical but
uses only integer arithmetic.

(this.mg.config().gameConfig().maxTimerValue !== undefined &&
timeElapsed - this.mg.config().gameConfig().maxTimerValue! * 60 >= 0) ||
timeElapsed >= WinCheckExecution.HARD_TIME_LIMIT_SECONDS
Expand Down
47 changes: 47 additions & 0 deletions tests/core/executions/WinCheckExecution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,53 @@ describe("WinCheckExecution", () => {
it("should return false for activeDuringSpawnPhase", () => {
expect(winCheck.activeDuringSpawnPhase()).toBe(false);
});

it("should not set winner via tile percentage when all land tiles have fallout (FFA)", () => {
const player = {
numTilesOwned: vi.fn(() => 100),
name: vi.fn(() => "P1"),
};
mg.players = vi.fn(() => [player]);
mg.numLandTiles = vi.fn(() => 100);
mg.numTilesWithFallout = vi.fn(() => 100);
mg.config = vi.fn(() => ({
gameConfig: vi.fn(() => ({
gameMode: GameMode.FFA,
maxTimerValue: undefined,
})),
percentageTilesOwnedToWin: vi.fn(() => 80),
numSpawnPhaseTurns: vi.fn(() => 0),
}));
mg.ticks = vi.fn(() => 0);
mg.stats = vi.fn(() => ({ stats: () => ({}) }));
winCheck.init(mg, 0);
winCheck.checkWinnerFFA();
expect(mg.setWinner).not.toHaveBeenCalled();
});

it("should not set winner via tile percentage when all land tiles have fallout (Team)", () => {
const player = {
numTilesOwned: vi.fn(() => 100),
name: vi.fn(() => "P1"),
team: vi.fn(() => "Red"),
};
mg.players = vi.fn(() => [player]);
mg.numLandTiles = vi.fn(() => 100);
mg.numTilesWithFallout = vi.fn(() => 100);
mg.config = vi.fn(() => ({
gameConfig: vi.fn(() => ({
gameMode: GameMode.Team,
maxTimerValue: undefined,
})),
percentageTilesOwnedToWin: vi.fn(() => 80),
numSpawnPhaseTurns: vi.fn(() => 0),
}));
mg.ticks = vi.fn(() => 0);
mg.stats = vi.fn(() => ({ stats: () => ({}) }));
winCheck.init(mg, 0);
winCheck.checkWinnerTeam();
expect(mg.setWinner).not.toHaveBeenCalled();
});
});

describe("WinCheckExecution - Nation Winners", () => {
Expand Down
Loading