|
| 1 | +import { WinCheckExecution } from "../../../src/core/execution/WinCheckExecution"; |
| 2 | +import { GameMode } from "../../../src/core/game/Game"; |
| 3 | +import { setup } from "../../util/Setup"; |
| 4 | + |
| 5 | +describe("WinCheckExecution", () => { |
| 6 | + let mg: any; |
| 7 | + let winCheck: WinCheckExecution; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + mg = await setup("big_plains", { |
| 11 | + infiniteGold: true, |
| 12 | + gameMode: GameMode.FFA, |
| 13 | + maxTimerValue: 5, |
| 14 | + instantBuild: true, |
| 15 | + }); |
| 16 | + mg.setWinner = jest.fn(); |
| 17 | + winCheck = new WinCheckExecution(); |
| 18 | + winCheck.init(mg, 0); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should call checkWinnerFFA in FFA mode", () => { |
| 22 | + const spy = jest.spyOn(winCheck as any, "checkWinnerFFA"); |
| 23 | + winCheck.tick(10); |
| 24 | + expect(spy).toHaveBeenCalled(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should call checkWinnerTeam in non-FFA mode", () => { |
| 28 | + mg.config = jest.fn(() => ({ |
| 29 | + gameConfig: jest.fn(() => ({ |
| 30 | + maxTimerValue: 5, |
| 31 | + gameMode: GameMode.Team, |
| 32 | + })), |
| 33 | + percentageTilesOwnedToWin: jest.fn(() => 50), |
| 34 | + })); |
| 35 | + winCheck.init(mg, 0); |
| 36 | + const spy = jest.spyOn(winCheck as any, "checkWinnerTeam"); |
| 37 | + winCheck.tick(10); |
| 38 | + expect(spy).toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should set winner in FFA if percentage is reached", () => { |
| 42 | + const player = { |
| 43 | + numTilesOwned: jest.fn(() => 81), |
| 44 | + name: jest.fn(() => "P1"), |
| 45 | + }; |
| 46 | + mg.players = jest.fn(() => [player]); |
| 47 | + mg.numLandTiles = jest.fn(() => 100); |
| 48 | + mg.numTilesWithFallout = jest.fn(() => 0); |
| 49 | + winCheck.checkWinnerFFA(); |
| 50 | + expect(mg.setWinner).toHaveBeenCalledWith(player, expect.anything()); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should set winner in FFA if timer is 0", () => { |
| 54 | + const player = { |
| 55 | + numTilesOwned: jest.fn(() => 10), |
| 56 | + name: jest.fn(() => "P1"), |
| 57 | + }; |
| 58 | + mg.players = jest.fn(() => [player]); |
| 59 | + mg.numLandTiles = jest.fn(() => 100); |
| 60 | + mg.numTilesWithFallout = jest.fn(() => 0); |
| 61 | + mg.stats = jest.fn(() => ({ stats: () => ({ mocked: true }) })); |
| 62 | + // Advance ticks until timeElapsed (in seconds) >= maxTimerValue * 60 |
| 63 | + // timeElapsed = (ticks - numSpawnPhaseTurns) / 10 => |
| 64 | + // ticks >= numSpawnPhaseTurns + maxTimerValue * 600 |
| 65 | + const threshold = |
| 66 | + mg.config().numSpawnPhaseTurns() + |
| 67 | + (mg.config().gameConfig().maxTimerValue ?? 0) * 600; |
| 68 | + while (mg.ticks() < threshold) { |
| 69 | + mg.executeNextTick(); |
| 70 | + } |
| 71 | + winCheck.checkWinnerFFA(); |
| 72 | + expect(mg.setWinner).toHaveBeenCalledWith(player, expect.any(Object)); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should not set winner if no players", () => { |
| 76 | + mg.players = jest.fn(() => []); |
| 77 | + winCheck.checkWinnerFFA(); |
| 78 | + expect(mg.setWinner).not.toHaveBeenCalled(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should return false for activeDuringSpawnPhase", () => { |
| 82 | + expect(winCheck.activeDuringSpawnPhase()).toBe(false); |
| 83 | + }); |
| 84 | +}); |
0 commit comments