mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-10 13:04:36 +00:00
a7f992e9b0
Restructure the single src/ tree into an npm-workspaces monorepo under
packages/, rename core -> engine, extract a types-only core-public layer,
and break the pre-existing engine -> client dependency cycle.
Structure (packages/):
core-public public API/wire schemas + shared enums (clean leaf)
shared framework-agnostic helpers (clean leaf)
engine deterministic simulation (was src/core)
client rendering/UI (was src/client)
server coordination (was src/server)
Dependency DAG: engine -> {core-public, shared}; client -> {core-public,
shared, engine}; server -> {core-public, engine}.
- npm workspaces: root package.json workspaces + per-package package.json;
tsconfig.base.json holds shared options + path aliases
(core-public/* shared/* engine/* client/* server/*) resolved uniformly by
tsc, Vite (resolve.tsconfigPaths), Vitest, and tsx. Lockfile regenerated.
- core-public: moved Schemas/ApiSchemas/CosmeticSchemas/StatsSchemas/
ClanApiSchemas/WorkerSchemas/Base64/PatternDecoder; extracted the enums
(GameTypes), GameEvent type, emoji table, and GraphicsOverrides schema.
Engine re-exports the moved enums/types so existing imports keep working.
- Broke engine -> client cycle:
- renderNumber/renderTroops -> shared/format
- NameBoxCalculator moved into engine
- username validation returns translation key + params; client translates
- applyStateUpdate moved to client (operates on the render-only PlayerState)
- Config/UnitGrid/execution-Util/GameImpl now use structural read
interfaces (engine/game/ReadViews: PlayerLike/UnitLike/GameLike) instead
of importing client view classes; client imports view classes from a new
client/view barrel; deleted the engine/game/GameView re-export shim.
- Build/deploy updated: vite.config, index.html, eslint, Dockerfile
(copies packages/ + tsconfig.base.json before npm ci), .vscode, tests.
Verified: tsc --noEmit clean; 1364 + 65 tests pass; production vite build
succeeds; engine has zero client/server imports; core-public and shared are
dependency leaves.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
156 lines
4.0 KiB
TypeScript
156 lines
4.0 KiB
TypeScript
import { AiAttackBehavior } from "engine/execution/utils/AiAttackBehavior";
|
|
import { Game, Player, PlayerInfo, PlayerType } from "engine/game/Game";
|
|
import { PseudoRandom } from "engine/PseudoRandom";
|
|
import { setup } from "./util/Setup";
|
|
|
|
describe("Ai Attack Behavior", () => {
|
|
let game: Game;
|
|
let bot: Player;
|
|
let human: Player;
|
|
let attackBehavior: AiAttackBehavior;
|
|
|
|
// Helper function for basic test setup
|
|
async function setupTestEnvironment() {
|
|
const testGame = await setup("big_plains", {
|
|
infiniteGold: true,
|
|
instantBuild: true,
|
|
infiniteTroops: true,
|
|
});
|
|
|
|
// Add players
|
|
const botInfo = new PlayerInfo(
|
|
"bot_test",
|
|
PlayerType.Bot,
|
|
null,
|
|
"bot_test",
|
|
);
|
|
const humanInfo = new PlayerInfo(
|
|
"human_test",
|
|
PlayerType.Human,
|
|
null,
|
|
"human_test",
|
|
);
|
|
testGame.addPlayer(botInfo);
|
|
testGame.addPlayer(humanInfo);
|
|
|
|
const testBot = testGame.player("bot_test");
|
|
const testHuman = testGame.player("human_test");
|
|
|
|
// Assign territories
|
|
let landTileCount = 0;
|
|
testGame.map().forEachTile((tile) => {
|
|
if (!testGame.map().isLand(tile)) return;
|
|
(landTileCount++ % 2 === 0 ? testBot : testHuman).conquer(tile);
|
|
});
|
|
|
|
// Add troops
|
|
testBot.addTroops(5000);
|
|
testHuman.addTroops(5000);
|
|
|
|
const behavior = new AiAttackBehavior(
|
|
new PseudoRandom(42),
|
|
testGame,
|
|
testBot,
|
|
0.5,
|
|
0.5,
|
|
0.2,
|
|
);
|
|
|
|
return { testGame, testBot, testHuman, behavior };
|
|
}
|
|
|
|
// Helper functions for tile assignment
|
|
function assignAlternatingLandTiles(
|
|
game: Game,
|
|
players: Player[],
|
|
totalTiles: number,
|
|
) {
|
|
let assigned = 0;
|
|
game.map().forEachTile((tile) => {
|
|
if (assigned >= totalTiles) return;
|
|
if (!game.map().isLand(tile)) return;
|
|
const player = players[assigned % players.length];
|
|
player.conquer(tile);
|
|
assigned++;
|
|
});
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
const env = await setupTestEnvironment();
|
|
game = env.testGame;
|
|
bot = env.testBot;
|
|
human = env.testHuman;
|
|
attackBehavior = env.behavior;
|
|
});
|
|
|
|
test("bot cannot attack allied player", () => {
|
|
// Form alliance (bot creates request to human)
|
|
const allianceRequest = bot.createAllianceRequest(human);
|
|
allianceRequest?.accept();
|
|
|
|
expect(bot.isAlliedWith(human)).toBe(true);
|
|
|
|
// Count attacks before attempting attack
|
|
const attacksBefore = bot.outgoingAttacks().length;
|
|
|
|
// Attempt attack (should be blocked)
|
|
attackBehavior.sendAttack(human);
|
|
|
|
// Execute a few ticks to process the attacks
|
|
for (let i = 0; i < 5; i++) {
|
|
game.executeNextTick();
|
|
}
|
|
|
|
expect(bot.isAlliedWith(human)).toBe(true);
|
|
expect(human.incomingAttacks()).toHaveLength(0);
|
|
// Should be same number of attacks (no new attack created)
|
|
expect(bot.outgoingAttacks()).toHaveLength(attacksBefore);
|
|
});
|
|
|
|
test("nation cannot attack allied player", () => {
|
|
// Create nation
|
|
const nationInfo = new PlayerInfo(
|
|
"nation_test",
|
|
PlayerType.Nation,
|
|
null,
|
|
"nation_test",
|
|
);
|
|
game.addPlayer(nationInfo);
|
|
const nation = game.player("nation_test");
|
|
|
|
// Use helper for tile assignment
|
|
assignAlternatingLandTiles(game, [bot, human, nation], 21); // 21 to ensure each gets 7 tiles
|
|
|
|
nation.addTroops(1000);
|
|
|
|
const nationBehavior = new AiAttackBehavior(
|
|
new PseudoRandom(42),
|
|
game,
|
|
nation,
|
|
0.5,
|
|
0.5,
|
|
0.2,
|
|
);
|
|
|
|
// Alliance between nation and human
|
|
const allianceRequest = nation.createAllianceRequest(human);
|
|
allianceRequest?.accept();
|
|
|
|
expect(nation.isAlliedWith(human)).toBe(true);
|
|
|
|
const attacksBefore = nation.outgoingAttacks().length;
|
|
nation.addTroops(50_000);
|
|
|
|
// Nation tries to attack ally (should be blocked)
|
|
nationBehavior.sendAttack(human);
|
|
|
|
// Execute a few ticks to process the attacks
|
|
for (let i = 0; i < 5; i++) {
|
|
game.executeNextTick();
|
|
}
|
|
|
|
expect(nation.isAlliedWith(human)).toBe(true);
|
|
expect(nation.outgoingAttacks()).toHaveLength(attacksBefore);
|
|
});
|
|
});
|