mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-10 11:54:35 +00:00
43d07ca85f
Tighten the package model around the determinism invariant: everything the
engine imports must itself be deterministic.
- Rename core-public -> engine-public (package, dir, `engine-public/*` alias,
all 118 import sites). It is the deterministic public surface the engine
depends on and that client/server also use.
- Move the pure formatting helpers (renderNumber/renderTroops) from shared
into engine-public/format — the engine uses them and they're deterministic.
- Drop the now-empty shared package (and its alias). `shared` was intended for
client/server-shared, engine-forbidden code; there is none yet, so it is
removed and can be re-added when such code appears.
Final graph: engine-public (leaf) <- engine <- {client, server};
client/server also -> engine-public.
Verified: tsc clean; 1364 + 65 tests pass; prod build succeeds; engine-public
is a leaf; engine has no client/server imports. Lockfile regenerated.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
290 lines
8.2 KiB
TypeScript
290 lines
8.2 KiB
TypeScript
import { DonateGoldExecution } from "engine/execution/DonateGoldExecution";
|
|
import { DonateTroopsExecution } from "engine/execution/DonateTroopExecution";
|
|
import { SpawnExecution } from "engine/execution/SpawnExecution";
|
|
import { PlayerInfo, PlayerType } from "engine/game/Game";
|
|
import { GameID } from "engine-public/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);
|
|
});
|
|
});
|
|
|
|
describe("Self donation prevention", () => {
|
|
it("Should evaluate isFriendly(this) to true but disallow donating to self", async () => {
|
|
const game = await setup("ocean_and_land", {
|
|
infiniteGold: false,
|
|
infiniteTroops: false,
|
|
donateGold: true,
|
|
donateTroops: true,
|
|
});
|
|
const gameID: GameID = "game_id";
|
|
|
|
// Create a player with team=0/null (default/FFA)
|
|
const playerInfo = new PlayerInfo(
|
|
"player_self",
|
|
PlayerType.Human,
|
|
null,
|
|
"self_id",
|
|
);
|
|
game.addPlayer(playerInfo);
|
|
|
|
const player = game.player(playerInfo.id);
|
|
const spawnA = game.ref(0, 10);
|
|
|
|
game.addExecution(new SpawnExecution(gameID, playerInfo, spawnA));
|
|
game.executeNextTick();
|
|
|
|
// Assert player.isFriendly(player) === true
|
|
expect(player.isFriendly(player)).toBe(true);
|
|
|
|
// Assert canDonateGold and canDonateTroops return false for self
|
|
expect(player.canDonateGold(player)).toBe(false);
|
|
expect(player.canDonateTroops(player)).toBe(false);
|
|
|
|
// Try executing DonateGoldExecution and DonateTroopsExecution on self
|
|
player.addGold(1000n);
|
|
player.addTroops(1000);
|
|
const goldBefore = player.gold();
|
|
const troopsBefore = player.troops();
|
|
|
|
game.addExecution(new DonateGoldExecution(player, player.id(), 500));
|
|
game.addExecution(new DonateTroopsExecution(player, player.id(), 500));
|
|
game.executeNextTick();
|
|
|
|
// Verify no changes occurred to gold or troops (execution failed/aborted)
|
|
expect(player.gold()).toBeGreaterThanOrEqual(goldBefore);
|
|
expect(player.troops()).toBeGreaterThanOrEqual(troopsBefore);
|
|
});
|
|
});
|