mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-10 17:14:38 +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>
119 lines
3.8 KiB
TypeScript
119 lines
3.8 KiB
TypeScript
import { AttackExecution } from "engine/execution/AttackExecution";
|
|
import { Game, Player, PlayerInfo, PlayerType } from "engine/game/Game";
|
|
import { GOLD_INDEX_WAR, GOLD_INDEX_WORK } from "engine-public/StatsSchemas";
|
|
import { setup } from "./util/Setup";
|
|
|
|
let game: Game;
|
|
let player1: Player;
|
|
let player2: Player;
|
|
const player1Info = new PlayerInfo(
|
|
"player1",
|
|
PlayerType.Human,
|
|
"player1",
|
|
"player1",
|
|
);
|
|
const player2Info = new PlayerInfo(
|
|
"player2",
|
|
PlayerType.Human,
|
|
"player2",
|
|
"player2",
|
|
);
|
|
|
|
describe("AttackStats", () => {
|
|
beforeEach(async () => {
|
|
game = await setup("plains", { infiniteTroops: true }, [
|
|
player1Info,
|
|
player2Info,
|
|
]);
|
|
|
|
player1 = game.player("player1");
|
|
player2 = game.player("player2");
|
|
player1.conquer(game.ref(50, 50));
|
|
player2.conquer(game.ref(50, 51));
|
|
player2.addGold(100n);
|
|
game.stats().goldWork(player2, 100n);
|
|
});
|
|
|
|
test("should increase war gold stat when a player is eliminated", () => {
|
|
expect(player1.sharesBorderWith(player2)).toBeTruthy();
|
|
// Player2 must attack to be considered active (otherwise gold won't transfer)
|
|
game.addExecution(
|
|
new AttackExecution(1, player2, game.terraNullius().id()),
|
|
);
|
|
game.executeNextTick();
|
|
performAttack(game, player1, player2);
|
|
expectWarGoldStatIsIncreasedAfterKill(game, player1, player2);
|
|
});
|
|
|
|
test("should NOT increase war gold stat when a inactive player is eliminated", () => {
|
|
expect(player1.sharesBorderWith(player2)).toBeTruthy();
|
|
|
|
const attackerStatsBefore = game.stats().stats()[player1.clientID()!];
|
|
const warGoldBefore = attackerStatsBefore?.gold?.[GOLD_INDEX_WAR] ?? 0n;
|
|
|
|
performAttack(game, player1, player2);
|
|
|
|
const attackerStatsAfter = game.stats().stats()[player1.clientID()!];
|
|
const warGoldAfter = attackerStatsAfter?.gold?.[GOLD_INDEX_WAR] ?? 0n;
|
|
|
|
expect(warGoldAfter).toBe(warGoldBefore);
|
|
});
|
|
|
|
test("should increase war gold stat when elimination occurs via territory annexation", () => {
|
|
// Player2 must attack to be considered active (otherwise gold won't transfer)
|
|
game.addExecution(
|
|
new AttackExecution(1, player2, game.terraNullius().id()),
|
|
);
|
|
game.executeNextTick();
|
|
|
|
// Mark every tile on the map as owned by player1
|
|
for (let x = 0; x < game.map().width(); x++) {
|
|
for (let y = 0; y < game.map().height(); y++) {
|
|
player1.conquer(game.ref(x, y));
|
|
}
|
|
}
|
|
// Place tiles of player2 in the center of the map
|
|
const centerX = Math.round(game.map().width() / 2);
|
|
const centerY = Math.round(game.map().height() / 2);
|
|
for (let x = -20; x < 20; x++) {
|
|
for (let y = -20; y < 20; y++) {
|
|
player2.conquer(game.ref(centerX + x, centerY + y));
|
|
}
|
|
}
|
|
|
|
performAttack(game, player1, player2);
|
|
expectWarGoldStatIsIncreasedAfterKill(game, player1, player2);
|
|
});
|
|
});
|
|
|
|
function expectWarGoldStatIsIncreasedAfterKill(
|
|
game: Game,
|
|
attacker: Player,
|
|
defender: Player,
|
|
) {
|
|
// Verify that the defender was killed as a result of the attack
|
|
expect(attacker.isAlive()).toBeTruthy();
|
|
expect(defender.isAlive()).toBeFalsy();
|
|
|
|
const attackerStats = game.stats().stats()[attacker.clientID()!];
|
|
const defenderStats = game.stats().stats()[defender.clientID()!];
|
|
|
|
// Conqueror receives 50% of human defender's gold as war gold
|
|
expect(attackerStats?.gold?.[GOLD_INDEX_WAR]).toBeDefined();
|
|
expect(defenderStats?.gold?.[GOLD_INDEX_WORK]).toBeDefined();
|
|
expect(attackerStats?.gold?.[GOLD_INDEX_WAR]).toBe(
|
|
(defenderStats?.gold?.reduce((acc, g) => acc + g, 0n) ?? 0n) / 2n,
|
|
);
|
|
}
|
|
|
|
function performAttack(game: Game, attacker: Player, defender: Player) {
|
|
// Execute the attack
|
|
game.addExecution(
|
|
new AttackExecution(attacker.troops(), attacker, defender.id()),
|
|
);
|
|
// Wait for the attack to complete
|
|
do {
|
|
game.executeNextTick();
|
|
} while (attacker.outgoingAttacks().length > 0);
|
|
}
|