Skip to content
Closed
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
5 changes: 5 additions & 0 deletions src/core/execution/alliance/AllianceExtensionExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Player,
PlayerID,
} from "../../game/Game";
import { ALLIANCE_BLOCK_TICKS } from "../../game/constants";

export class AllianceExtensionExecution implements Execution {
constructor(
Expand All @@ -13,6 +14,10 @@ export class AllianceExtensionExecution implements Execution {
) {}

init(mg: Game, ticks: number): void {
if (mg.ticks() >= ALLIANCE_BLOCK_TICKS) {
return;
}

if (!mg.hasPlayer(this.toID)) {
console.warn(
`[AllianceExtensionExecution] Player ${this.toID} not found`,
Expand Down
5 changes: 5 additions & 0 deletions src/core/game/PlayerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "../Util";
import { sanitizeUsername } from "../validations/username";
import { AttackImpl } from "./AttackImpl";
import { ALLIANCE_BLOCK_TICKS } from "./constants";
import {
Alliance,
AllianceRequest,
Expand Down Expand Up @@ -388,6 +389,10 @@ export class PlayerImpl implements Player {
}

canSendAllianceRequest(other: Player): boolean {
if (this.mg.ticks() >= ALLIANCE_BLOCK_TICKS) {
return false;
}

if (other === this) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/game/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Game timing constants
// Block alliance requests/renewals after 40 minutes of game time
// 40 minutes = 2400 seconds = 24,000 ticks (10 ticks per second)
export const ALLIANCE_BLOCK_TICKS = 40 * 60 * 10; // 24,000 ticks
44 changes: 44 additions & 0 deletions tests/AllianceExtensionExecution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AllianceExtensionExecution } from "../src/core/execution/alliance/Allia
import { AllianceRequestExecution } from "../src/core/execution/alliance/AllianceRequestExecution";
import { AllianceRequestReplyExecution } from "../src/core/execution/alliance/AllianceRequestReplyExecution";
import { Game, MessageType, Player, PlayerType } from "../src/core/game/Game";
import { ALLIANCE_BLOCK_TICKS } from "../src/core/game/constants";
import { playerInfo, setup } from "./util/Setup";

let game: Game;
Expand Down Expand Up @@ -165,4 +166,47 @@ describe("AllianceExtensionExecution", () => {

displayMessageSpy.mockRestore();
});

test("Blocks alliance renewal messages after 40 minutes", () => {
jest.spyOn(player1, "canSendAllianceRequest").mockReturnValue(true);
jest.spyOn(player2, "isAlive").mockReturnValue(true);
jest.spyOn(player1, "isAlive").mockReturnValue(true);

// Create alliance between player1 and player2
game.addExecution(new AllianceRequestExecution(player1, player2.id()));
game.executeNextTick();
game.executeNextTick();

game.addExecution(
new AllianceRequestReplyExecution(player1.id(), player2, true),
);
game.executeNextTick();
game.executeNextTick();

expect(player1.allianceWith(player2)).toBeTruthy();
expect(player2.allianceWith(player1)).toBeTruthy();

// Advance game to 40 minutes (24,000 ticks)
// 40 minutes = 2400 seconds = 24,000 ticks (10 ticks per second)
const currentTicks = game.ticks();
const ticksToAdvance = ALLIANCE_BLOCK_TICKS - currentTicks;

for (let i = 0; i < ticksToAdvance; i++) {
game.executeNextTick();
}

expect(game.ticks()).toBeGreaterThanOrEqual(ALLIANCE_BLOCK_TICKS);

// Spy on displayMessage to verify it's NOT called
const displayMessageSpy = jest.spyOn(game, "displayMessage");

// Player1 tries to request renewal after 40 minutes
game.addExecution(new AllianceExtensionExecution(player1, player2.id()));
game.executeNextTick();

// Verify no renewal message was sent
expect(displayMessageSpy).not.toHaveBeenCalled();

displayMessageSpy.mockRestore();
});
});
25 changes: 25 additions & 0 deletions tests/AllianceRequestExecution.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AllianceRequestExecution } from "../src/core/execution/alliance/AllianceRequestExecution";
import { AllianceRequestReplyExecution } from "../src/core/execution/alliance/AllianceRequestReplyExecution";
import { NukeExecution } from "../src/core/execution/NukeExecution";
import { ALLIANCE_BLOCK_TICKS } from "../src/core/game/constants";
import { Game, Player, PlayerType, UnitType } from "../src/core/game/Game";
import { playerInfo, setup } from "./util/Setup";
import { constructionExecution } from "./util/utils";
Expand Down Expand Up @@ -100,4 +101,28 @@ describe("AllianceRequestExecution", () => {
expect(player1.isAlliedWith(player2)).toBeFalsy();
expect(player2.isAlliedWith(player1)).toBeFalsy();
});

test("Blocks new alliance requests after 40 minutes", () => {
// Advance game to 40 minutes (24,000 ticks)
// 40 minutes = 2400 seconds = 24,000 ticks (10 ticks per second)
const currentTicks = game.ticks();
const ticksToAdvance = ALLIANCE_BLOCK_TICKS - currentTicks;

for (let i = 0; i < ticksToAdvance; i++) {
game.executeNextTick();
}

expect(game.ticks()).toBeGreaterThanOrEqual(ALLIANCE_BLOCK_TICKS);

// Try to send alliance request after 40 minutes
game.addExecution(new AllianceRequestExecution(player1, player2.id()));
game.executeNextTick();
game.executeNextTick();

// Verify no alliance request was created
expect(player1.outgoingAllianceRequests().length).toBe(0);
expect(player2.incomingAllianceRequests().length).toBe(0);
expect(player1.isAlliedWith(player2)).toBeFalsy();
expect(player2.isAlliedWith(player1)).toBeFalsy();
});
});
15 changes: 15 additions & 0 deletions tests/PlayerImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PlayerType,
UnitType,
} from "../src/core/game/Game";
import { ALLIANCE_BLOCK_TICKS } from "../src/core/game/constants";
import { setup } from "./util/Setup";

let game: Game;
Expand Down Expand Up @@ -91,4 +92,18 @@ describe("PlayerImpl", () => {
}
expect(other.canSendAllianceRequest(player)).toBe(false);
});

test("Can't send alliance requests after 40 minutes", () => {
// Advance game to 40 minutes (24,000 ticks)
// 40 minutes = 2400 seconds = 24,000 ticks (10 ticks per second)
const currentTicks = game.ticks();
const ticksToAdvance = ALLIANCE_BLOCK_TICKS - currentTicks;

for (let i = 0; i < ticksToAdvance; i++) {
game.executeNextTick();
}

expect(game.ticks()).toBeGreaterThanOrEqual(ALLIANCE_BLOCK_TICKS);
expect(player.canSendAllianceRequest(other)).toBe(false);
});
});
Loading