diff --git a/src/core/execution/WinCheckExecution.ts b/src/core/execution/WinCheckExecution.ts index 0cb8c1dabc..8e50bd3c87 100644 --- a/src/core/execution/WinCheckExecution.ts +++ b/src/core/execution/WinCheckExecution.ts @@ -106,9 +106,12 @@ export class WinCheckExecution implements Execution { const timeElapsed = this.mg.elapsedGameSeconds(); const numTilesWithoutFallout = this.mg.numLandTiles() - this.mg.numTilesWithFallout(); - if ( + const isTerritoryWin = + numTilesWithoutFallout > 0 && (max.numTilesOwned() / numTilesWithoutFallout) * 100 > - this.mg.config().percentageTilesOwnedToWin() || + this.mg.config().percentageTilesOwnedToWin(); + if ( + isTerritoryWin || (this.mg.config().gameConfig().maxTimerValue !== undefined && timeElapsed - this.mg.config().gameConfig().maxTimerValue! * 60 >= 0) || timeElapsed >= WinCheckExecution.HARD_TIME_LIMIT_SECONDS @@ -164,9 +167,12 @@ export class WinCheckExecution implements Execution { const timeElapsed = this.mg.elapsedGameSeconds(); const numTilesWithoutFallout = this.mg.numLandTiles() - this.mg.numTilesWithFallout(); - const percentage = (max[1] / numTilesWithoutFallout) * 100; + const isTerritoryWin = + numTilesWithoutFallout > 0 && + (max[1] / numTilesWithoutFallout) * 100 > + this.mg.config().percentageTilesOwnedToWin(); if ( - percentage > this.mg.config().percentageTilesOwnedToWin() || + isTerritoryWin || (this.mg.config().gameConfig().maxTimerValue !== undefined && timeElapsed - this.mg.config().gameConfig().maxTimerValue! * 60 >= 0) || timeElapsed >= WinCheckExecution.HARD_TIME_LIMIT_SECONDS diff --git a/tests/core/executions/WinCheckExecution.test.ts b/tests/core/executions/WinCheckExecution.test.ts index 798d1d5f99..669f6c3593 100644 --- a/tests/core/executions/WinCheckExecution.test.ts +++ b/tests/core/executions/WinCheckExecution.test.ts @@ -82,6 +82,38 @@ describe("WinCheckExecution", () => { expect(mg.setWinner).not.toHaveBeenCalled(); }); + it("should not set territory winner in FFA when non-fallout tiles is zero", () => { + const player = { + numTilesOwned: vi.fn(() => 10), + name: vi.fn(() => "P1"), + }; + mg.players = vi.fn(() => [player]); + mg.numLandTiles = vi.fn(() => 100); + mg.numTilesWithFallout = vi.fn(() => 100); + winCheck.checkWinnerFFA(); + expect(mg.setWinner).not.toHaveBeenCalled(); + }); + + it("should not set territory winner in Team mode when non-fallout tiles is zero", () => { + mg.config = vi.fn(() => ({ + gameConfig: vi.fn(() => ({ + gameMode: GameMode.Team, + })), + percentageTilesOwnedToWin: vi.fn(() => 50), + })); + const player = { + numTilesOwned: vi.fn(() => 10), + team: vi.fn(() => ColoredTeams.Red), + name: vi.fn(() => "P1"), + }; + mg.players = vi.fn(() => [player]); + mg.numLandTiles = vi.fn(() => 100); + mg.numTilesWithFallout = vi.fn(() => 100); + winCheck.init(mg, 0); + winCheck.checkWinnerTeam(); + expect(mg.setWinner).not.toHaveBeenCalled(); + }); + it("should return false for activeDuringSpawnPhase", () => { expect(winCheck.activeDuringSpawnPhase()).toBe(false); });