Files
OpenFrontIO/tests/Donate.test.ts
T
Aotumuri f1d162825e feat: remove spawn timer on singleplayer (#3199)
Resolves #1041 

## Description:

Remove the singleplayer spawn countdown so the game starts when the
player spawns, spawn nations immediately after player spawn, and align
game timer/max-timer timing with the new start point.

Added a singleplayer regression test for spawn-immunity timing
(GameImpl.test.ts) and updated spawn-phase loop tests to use gameType:
GameType.Public where singleplayer behavior is not under test (e.g.
MIRV/AI/Spawn/WinCheck-related suites), eliminating inSpawnPhase()
timeout hangs after the new singleplayer start logic.


https://github.com/user-attachments/assets/c07a585f-1153-490e-88ca-a91fc7ae5756

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:
aotumuri
2026-05-11 12:44:44 -07:00

242 lines
6.6 KiB
TypeScript

import { DonateGoldExecution } from "../src/core/execution/DonateGoldExecution";
import { DonateTroopsExecution } from "../src/core/execution/DonateTroopExecution";
import { SpawnExecution } from "../src/core/execution/SpawnExecution";
import { PlayerInfo, PlayerType } from "../src/core/game/Game";
import { GameID } from "../src/core/Schemas";
import { setup } from "./util/Setup";
describe("Donate troops to an ally", () => {
it("Troops should be successfully donated", async () => {
const gameID: GameID = "game_id";
const game = await setup("ocean_and_land", {
infiniteTroops: false,
donateTroops: true,
});
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(gameID, donorInfo, spawnA),
new SpawnExecution(gameID, recipientInfo, spawnB),
);
// donor sends alliance request to recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// recipient accepts the alliance request
if (allianceRequest) {
allianceRequest.accept();
}
// Ensure donor can actually donate the requested amount
donor.addTroops(6000);
const donorTroopsBefore = donor.troops();
const recipientTroopsBefore = recipient.troops();
game.addExecution(new DonateTroopsExecution(donor, recipientInfo.id, 5000));
for (let i = 0; i < 5; i++) {
game.executeNextTick();
}
expect(donor.troops() < donorTroopsBefore).toBe(true);
expect(recipient.troops() > recipientTroopsBefore).toBe(true);
});
});
describe("Donate gold to an ally", () => {
it("Gold should be successfully donated", async () => {
const game = await setup("ocean_and_land", {
infiniteGold: false,
donateGold: true,
});
const gameID: GameID = "game_id";
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(gameID, donorInfo, spawnA),
new SpawnExecution(gameID, recipientInfo, spawnB),
);
// donor sends alliance request to recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// recipient accepts the alliance request
if (allianceRequest) {
allianceRequest.accept();
}
game.executeNextTick();
// Ensure donor can actually donate the requested amount
donor.addGold(6000n);
const donorGoldBefore = donor.gold();
const recipientGoldBefore = recipient.gold();
game.addExecution(new DonateGoldExecution(donor, recipientInfo.id, 5000));
for (let i = 0; i < 5; i++) {
game.executeNextTick();
}
expect(donor.gold() < donorGoldBefore).toBe(true);
expect(recipient.gold() > recipientGoldBefore).toBe(true);
});
});
describe("Donate troops to a non ally", () => {
it("Troops should not be donated", async () => {
const game = await setup("ocean_and_land", {
infiniteTroops: false,
donateTroops: true,
});
const gameID: GameID = "game_id";
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(gameID, donorInfo, spawnA),
new SpawnExecution(gameID, recipientInfo, spawnB),
);
// Donor sends alliance request to Recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// Donor rejects the Recipient
if (allianceRequest) {
allianceRequest.reject();
}
const donorTroopsBefore = donor.troops();
const recipientTroopsBefore = recipient.troops();
game.addExecution(new DonateTroopsExecution(donor, recipientInfo.id, 5000));
game.executeNextTick();
// Troops should not be donated since they are not allies
expect(donor.troops() >= donorTroopsBefore).toBe(true);
expect(recipient.troops() >= recipientTroopsBefore).toBe(true);
});
});
describe("Donate Gold to a non ally", () => {
it("Gold should not be donated", async () => {
const game = await setup("ocean_and_land", {
infiniteGold: false,
donateGold: true,
});
const gameID: GameID = "game_id";
const donorInfo = new PlayerInfo(
"donor",
PlayerType.Human,
null,
"donor_id",
);
const recipientInfo = new PlayerInfo(
"recipient",
PlayerType.Human,
null,
"recipient_id",
);
game.addPlayer(donorInfo);
game.addPlayer(recipientInfo);
const donor = game.player(donorInfo.id);
const recipient = game.player(recipientInfo.id);
// Spawn both players
const spawnA = game.ref(0, 10);
const spawnB = game.ref(0, 15);
game.addExecution(
new SpawnExecution(gameID, donorInfo, spawnA),
new SpawnExecution(gameID, recipientInfo, spawnB),
);
// Donor sends alliance request to Recipient
const allianceRequest = donor.createAllianceRequest(recipient);
expect(allianceRequest).not.toBeNull();
// Donor rejects the Recipient
if (allianceRequest) {
allianceRequest.reject();
}
const donorGoldBefore = donor.gold();
const recipientGoldBefore = donor.gold();
game.addExecution(new DonateGoldExecution(donor, recipientInfo.id, 5000));
game.executeNextTick();
// Gold should not be donated since they are not allies
expect(donor.gold() >= donorGoldBefore).toBe(true);
expect(recipient.gold() >= recipientGoldBefore).toBe(true);
});
});