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 @@ -68,6 +68,11 @@ export class WinCheckExecution implements Execution {
(this.mg.ticks() - this.mg.config().numSpawnPhaseTurns()) / 10;
const numTilesWithoutFallout =
this.mg.numLandTiles() - this.mg.numTilesWithFallout();
Comment on lines 69 to 70
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;
}
Comment on lines +72 to +74
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

Timer-based win conditions are suppressed when all tiles have fallout.

The early return before the combined if block bypasses all three win conditions — tile-percentage, maxTimerValue countdown, and HARD_TIME_LIMIT_SECONDS. The timer conditions have nothing to do with numTilesWithoutFallout, so a game where all tiles have fallout when the timer expires (or the hard limit fires) will never declare a winner. The server's hard kill then ends the game without a graceful winner, which defeats the purpose of HARD_TIME_LIMIT_SECONDS.

The guard should wrap only the tile-percentage term:

-    if (numTilesWithoutFallout === 0) {
-      return;
-    }
-
     if (
-      (max.numTilesOwned() / numTilesWithoutFallout) * 100 >
-        this.mg.config().percentageTilesOwnedToWin() ||
+      (numTilesWithoutFallout > 0 &&
+        (max.numTilesOwned() / numTilesWithoutFallout) * 100 >
+          this.mg.config().percentageTilesOwnedToWin()) ||
       (this.mg.config().gameConfig().maxTimerValue !== undefined &&
         timeElapsed - this.mg.config().gameConfig().maxTimerValue! * 60 >= 0) ||
       timeElapsed >= WinCheckExecution.HARD_TIME_LIMIT_SECONDS

Apply the same pattern in checkWinnerTeam (lines 113-115 and 117-119).

Also applies to: 113-115

🤖 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 72 - 74, In
WinCheckExecution.ts adjust the guard so timer-based win conditions still run
when all tiles have fallout: in the win-checking logic (functions checkWinner
and checkWinnerTeam) remove the early "if (numTilesWithoutFallout === 0)
return;" that short-circuits the whole combined if, and instead apply the
numTilesWithoutFallout === 0 check only to the tile-percentage term inside the
combined if (i.e., wrap the tile-percentage comparison with "&&
numTilesWithoutFallout > 0" or equivalent). Keep the other terms (maxTimerValue
countdown and HARD_TIME_LIMIT_SECONDS) evaluated normally so timer expiries can
still declare a winner.


if (
(max.numTilesOwned() / numTilesWithoutFallout) * 100 >
this.mg.config().percentageTilesOwnedToWin() ||
Expand Down Expand Up @@ -104,9 +109,14 @@ export class WinCheckExecution implements Execution {
(this.mg.ticks() - this.mg.config().numSpawnPhaseTurns()) / 10;
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 +118 to +119
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 @@ -87,6 +87,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