mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-10 12:54:37 +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>
144 lines
4.4 KiB
TypeScript
144 lines
4.4 KiB
TypeScript
import { MoveWarshipExecution } from "engine/execution/MoveWarshipExecution";
|
|
import { WarshipExecution } from "engine/execution/WarshipExecution";
|
|
import {
|
|
Game,
|
|
Player,
|
|
PlayerInfo,
|
|
PlayerType,
|
|
UnitType,
|
|
} from "engine/game/Game";
|
|
import { setup } from "./util/Setup";
|
|
import { executeTicks } from "./util/utils";
|
|
|
|
const coastX = 7;
|
|
let game: Game;
|
|
let player1: Player;
|
|
let player2: Player;
|
|
|
|
describe("Warship multi-selection (MoveWarshipExecution)", () => {
|
|
beforeEach(async () => {
|
|
game = await setup(
|
|
"half_land_half_ocean",
|
|
{ infiniteGold: true, instantBuild: true },
|
|
[
|
|
new PlayerInfo("p1", PlayerType.Human, null, "p1"),
|
|
new PlayerInfo("p2", PlayerType.Human, null, "p2"),
|
|
],
|
|
);
|
|
player1 = game.player("p1");
|
|
player2 = game.player("p2");
|
|
});
|
|
|
|
test("moving multiple warships via array MoveWarshipExecution updates all patrol tiles", () => {
|
|
const w1 = player1.buildUnit(UnitType.Warship, game.ref(coastX + 1, 10), {
|
|
patrolTile: game.ref(coastX + 1, 10),
|
|
});
|
|
const w2 = player1.buildUnit(UnitType.Warship, game.ref(coastX + 2, 10), {
|
|
patrolTile: game.ref(coastX + 2, 10),
|
|
});
|
|
const w3 = player1.buildUnit(UnitType.Warship, game.ref(coastX + 3, 10), {
|
|
patrolTile: game.ref(coastX + 3, 10),
|
|
});
|
|
|
|
game.addExecution(new WarshipExecution(w1));
|
|
game.addExecution(new WarshipExecution(w2));
|
|
game.addExecution(new WarshipExecution(w3));
|
|
|
|
const sharedTarget = game.ref(coastX + 5, 15);
|
|
// Single execution with array of ids — the new unified API
|
|
game.addExecution(
|
|
new MoveWarshipExecution(
|
|
player1,
|
|
[w1.id(), w2.id(), w3.id()],
|
|
sharedTarget,
|
|
),
|
|
);
|
|
|
|
executeTicks(game, 5);
|
|
|
|
expect(w1.warshipState().patrolTile).toBe(sharedTarget);
|
|
expect(w2.warshipState().patrolTile).toBe(sharedTarget);
|
|
expect(w3.warshipState().patrolTile).toBe(sharedTarget);
|
|
});
|
|
|
|
test("moving multiple warships to different targets works independently", () => {
|
|
const w1 = player1.buildUnit(UnitType.Warship, game.ref(coastX + 1, 10), {
|
|
patrolTile: game.ref(coastX + 1, 10),
|
|
});
|
|
const w2 = player1.buildUnit(UnitType.Warship, game.ref(coastX + 2, 10), {
|
|
patrolTile: game.ref(coastX + 2, 10),
|
|
});
|
|
|
|
game.addExecution(new WarshipExecution(w1));
|
|
game.addExecution(new WarshipExecution(w2));
|
|
|
|
const target1 = game.ref(coastX + 3, 12);
|
|
const target2 = game.ref(coastX + 4, 14);
|
|
|
|
game.addExecution(new MoveWarshipExecution(player1, [w1.id()], target1));
|
|
game.addExecution(new MoveWarshipExecution(player1, [w2.id()], target2));
|
|
|
|
executeTicks(game, 5);
|
|
|
|
expect(w1.warshipState().patrolTile).toBe(target1);
|
|
expect(w2.warshipState().patrolTile).toBe(target2);
|
|
});
|
|
|
|
test("enemy cannot move player's warships via MoveWarshipExecution", () => {
|
|
const originalTile = game.ref(coastX + 1, 10);
|
|
const w1 = player1.buildUnit(UnitType.Warship, originalTile, {
|
|
patrolTile: originalTile,
|
|
});
|
|
game.addExecution(new WarshipExecution(w1));
|
|
|
|
new MoveWarshipExecution(player2, [w1.id()], game.ref(coastX + 5, 15)).init(
|
|
game,
|
|
0,
|
|
);
|
|
|
|
expect(w1.warshipState().patrolTile).toBe(originalTile);
|
|
});
|
|
|
|
test("MoveWarshipExecution on destroyed warship does not throw", () => {
|
|
const w1 = player1.buildUnit(UnitType.Warship, game.ref(coastX + 1, 10), {
|
|
patrolTile: game.ref(coastX + 1, 10),
|
|
});
|
|
w1.delete();
|
|
|
|
const exec = new MoveWarshipExecution(
|
|
player1,
|
|
[w1.id()],
|
|
game.ref(coastX + 5, 15),
|
|
);
|
|
expect(() => exec.init(game, 0)).not.toThrow();
|
|
expect(exec.isActive()).toBe(false);
|
|
});
|
|
|
|
test("batch move does not affect warships owned by other players", () => {
|
|
const p1tile = game.ref(coastX + 1, 10);
|
|
const p2tile = game.ref(coastX + 2, 10);
|
|
|
|
const w1 = player1.buildUnit(UnitType.Warship, p1tile, {
|
|
patrolTile: p1tile,
|
|
});
|
|
const w2 = player2.buildUnit(UnitType.Warship, p2tile, {
|
|
patrolTile: p2tile,
|
|
});
|
|
|
|
game.addExecution(new WarshipExecution(w1));
|
|
game.addExecution(new WarshipExecution(w2));
|
|
|
|
const target = game.ref(coastX + 5, 15);
|
|
|
|
// player1 sends both IDs — but w2 belongs to player2
|
|
game.addExecution(
|
|
new MoveWarshipExecution(player1, [w1.id(), w2.id()], target),
|
|
);
|
|
|
|
executeTicks(game, 5);
|
|
|
|
expect(w1.warshipState().patrolTile).toBe(target);
|
|
expect(w2.warshipState().patrolTile).toBe(p2tile); // unchanged — wrong owner
|
|
});
|
|
});
|