fix(viewer): reconcile completed federation plans
This commit is contained in:
parent
d8f540d2f9
commit
5299cc30f3
4 changed files with 228 additions and 17 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@gaia-project/viewer",
|
||||
"version": "5.22.20",
|
||||
"version": "5.22.21",
|
||||
"description": "Functions to visualize the map and boards for gaia project inside html canvas",
|
||||
"contributors": [
|
||||
"coyotte508",
|
||||
|
|
|
|||
|
|
@ -2003,6 +2003,40 @@ describe("Game", () => {
|
|||
vm.$destroy();
|
||||
});
|
||||
|
||||
it("repairs and persists a stale federation after reloading without replaying the next research twice", () => {
|
||||
const live = new Engine(SETUP_MOVES);
|
||||
const federation = "terrans federation 1A4,1A5,1B2,2A1,9A8,9A9 ship-fed-range. build m 8A6.";
|
||||
const recorded = federation.replace("ship-fed-range.", "ship-fed-range using area2: 1, area3: 1.");
|
||||
const moveIndex = live.moveHistory.length;
|
||||
live.moveHistory.push(recorded, "terrans up nav (0 ⇒ 1).");
|
||||
const nav = { kind: "move", move: "terrans up nav." };
|
||||
window.localStorage.setItem(
|
||||
"analysis-mode::0",
|
||||
JSON.stringify({
|
||||
lines: [[{ kind: "move", move: federation }, nav, nav]],
|
||||
active: 0,
|
||||
baseRound: 1,
|
||||
baseMoveCount: live.moveHistory.length,
|
||||
baseMove: live.moveHistory[live.moveHistory.length - 1],
|
||||
})
|
||||
);
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
const vm = mountAsSeat(0, live);
|
||||
vm.enterAnalysisMode();
|
||||
expect(vm.analysisEntries).to.deep.equal([nav]);
|
||||
expect(vm.analysisAppliedCount).to.equal(1);
|
||||
expect(vm.analysisPlayed[0]).to.deep.equal([
|
||||
{ move: recorded, moveIndex },
|
||||
{ move: "terrans up nav (0 ⇒ 1).", moveIndex: moveIndex + 1 },
|
||||
]);
|
||||
const saved = JSON.parse(window.localStorage.getItem("analysis-mode::0"));
|
||||
expect(saved.lines).to.deep.equal([[nav]]);
|
||||
expect(saved.played[0]).to.have.length(2);
|
||||
vm.$el.remove();
|
||||
vm.$destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it("can start a blank variation without copying moves or played history", () => {
|
||||
const vm = mountAsSeat(0);
|
||||
vm.enterAnalysisMode();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,13 @@
|
|||
import Engine, { AuctionVariant, Command, Faction, Phase, Planet, Resource, Round } from "@gaia-project/engine";
|
||||
import Engine, {
|
||||
AuctionVariant,
|
||||
Building,
|
||||
Command,
|
||||
Faction,
|
||||
Phase,
|
||||
Planet,
|
||||
Resource,
|
||||
Round,
|
||||
} from "@gaia-project/engine";
|
||||
import { expect } from "chai";
|
||||
import { passedChargeGame } from "../testing/passed-charge";
|
||||
import type { AnalysisEntry, MoveCost } from "./analysis";
|
||||
|
|
@ -417,6 +426,151 @@ describe("dropPlayedAnalysisPrefix", () => {
|
|||
).to.deep.equal([nav]);
|
||||
});
|
||||
|
||||
const federation = "terrans federation 1A4,1A5,1B2,2A1,9A8,9A9 ship-fed-range. build m 8A6.";
|
||||
const loggedFederation =
|
||||
"terrans federation 1A4,1A5,1B2,2A1,9A8,9A9 ship-fed-range using area2: 1, area3: 1. build m 8A6. (0/1/10/0 ⇒ 0/0/9/0)";
|
||||
|
||||
it("matches the Devious-basket-7886 federation despite its power-payment annotation", () => {
|
||||
const charge: AnalysisEntry = { kind: "adjust", charge: 2 };
|
||||
const result = dropPlayedAnalysisPrefix(engine, [charge, move(federation), nav], 0, [loggedFederation]);
|
||||
expect(result.entries).to.deep.equal([nav]);
|
||||
expect(result.dropped).to.equal(1);
|
||||
expect(result.played[0].move).to.equal(loggedFederation);
|
||||
});
|
||||
|
||||
it("matches federation hex sets regardless of order or coordinate aliases, including sector centres", () => {
|
||||
const hexes = ["1A4", "1B2", "1C"];
|
||||
const coordinates = hexes.map((hex) => {
|
||||
const { q, r } = engine.map.parse(hex);
|
||||
return `${q}x${r}`;
|
||||
});
|
||||
const planned = move(`terrans federation ${hexes.join(",")} fed3.`);
|
||||
const result = dropPlayedAnalysisPrefix(engine, [planned, nav], 0, [
|
||||
`terrans federation ${coordinates.reverse().join(",")} fed3 using area1: 1.`,
|
||||
]);
|
||||
expect(result.entries).to.deep.equal([nav]);
|
||||
});
|
||||
|
||||
it("keeps federation alternatives with a different hex, token, follow-up build or conversion", () => {
|
||||
for (const alternative of [
|
||||
federation.replace("1A4", "1A3"),
|
||||
federation.replace("ship-fed-range", "fed3"),
|
||||
federation.replace("8A6", "8A7"),
|
||||
federation.replace(". build m 8A6.", "."),
|
||||
federation.replace("terrans ", "terrans spend 1q for 1o. "),
|
||||
]) {
|
||||
const entry = move(alternative);
|
||||
expect(dropPlayedAnalysisPrefix(engine, [entry], 0, [loggedFederation]).entries).to.deep.equal([entry]);
|
||||
}
|
||||
expect(
|
||||
dropPlayedAnalysisPrefix(engine, [move(federation)], 0, [loggedFederation.replace("terrans", "nevlas")]).dropped
|
||||
).to.equal(0);
|
||||
});
|
||||
|
||||
it("repairs a federation saved past its actual move and consumes subsequent turns only once", () => {
|
||||
const live = new Engine(SETUP_MOVES);
|
||||
const base = live.moveHistory.length;
|
||||
live.moveHistory.push("terrans up eco (0 ⇒ 1).", loggedFederation, "nevlas up nav.", "terrans up nav.");
|
||||
const previousPlayed = [{ move: live.moveHistory[base], moveIndex: base }];
|
||||
const result = dropPlayedAnalysisPrefix(
|
||||
live,
|
||||
[move(federation), nav, nav],
|
||||
0,
|
||||
[],
|
||||
live.moveHistory.length,
|
||||
previousPlayed
|
||||
);
|
||||
expect(result.entries).to.deep.equal([nav]);
|
||||
expect(result.played).to.deep.equal([
|
||||
{ move: loggedFederation, moveIndex: base + 1 },
|
||||
{ move: "terrans up nav.", moveIndex: base + 3 },
|
||||
]);
|
||||
const reopened = dropPlayedAnalysisPrefix(live, result.entries, 0, [], live.moveHistory.length, [
|
||||
...previousPlayed,
|
||||
...result.played,
|
||||
]);
|
||||
expect(reopened.entries).to.deep.equal([nav]);
|
||||
expect(reopened.played).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("recovers across the saved base without consuming a subsequent research twice", () => {
|
||||
const live = new Engine(SETUP_MOVES);
|
||||
const federationIndex = live.moveHistory.length;
|
||||
live.moveHistory.push(loggedFederation, "nevlas up nav.");
|
||||
const base = live.moveHistory.length;
|
||||
const newMoves = ["terrans up nav.", "nevlas charge 1."];
|
||||
live.moveHistory.push(...newMoves);
|
||||
const result = dropPlayedAnalysisPrefix(live, [move(federation), nav, nav], 0, newMoves, base);
|
||||
expect(result.entries).to.deep.equal([nav]);
|
||||
expect(result.played).to.deep.equal([
|
||||
{ move: loggedFederation, moveIndex: federationIndex },
|
||||
{ move: newMoves[0], moveIndex: base },
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves a fresh Ivits plan that legally federates the same hexes and token again", () => {
|
||||
const live = new Engine(["init 2 randomSeed", "p1 faction ivits", "p2 faction terrans"]);
|
||||
live.phase = Phase.RoundMove;
|
||||
live.round = 1;
|
||||
live.turnOrder = [0];
|
||||
live.currentPlayer = 0;
|
||||
const player = live.players[0];
|
||||
player.data.qics = 10;
|
||||
const coords = ["0x0", "1x0", "2x0", "0x1", "1x1", "2x1"];
|
||||
const buildings = [
|
||||
Building.PlanetaryInstitute,
|
||||
Building.Academy1,
|
||||
Building.ResearchLab,
|
||||
Building.ResearchLab,
|
||||
Building.TradingStation,
|
||||
Building.TradingStation,
|
||||
];
|
||||
coords.forEach((coord, index) => {
|
||||
const hex = live.map.getS(coord);
|
||||
hex.data.planet = Planet.Terra;
|
||||
hex.data.player = 0;
|
||||
hex.data.building = buildings[index];
|
||||
player.data.occupied.push(hex);
|
||||
player.data.buildings[buildings[index]]++;
|
||||
});
|
||||
live.clearAvailableCommands();
|
||||
const entry = move(`ivits federation ${coords.join(",")} fed2.`);
|
||||
live.move(entry.move);
|
||||
expect(player.data.federationCount).to.equal(1);
|
||||
const base = live.moveHistory.length;
|
||||
expect(dropPlayedAnalysisPrefix(live, [entry], 0, [], base).entries).to.deep.equal([entry]);
|
||||
live.move(entry.move);
|
||||
expect(player.data.federationCount).to.equal(2);
|
||||
expect(dropPlayedAnalysisPrefix(live, [entry], 0, live.moveHistory.slice(base), base).entries).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("recovers a stale federation even when it was the first planned turn", () => {
|
||||
const live = new Engine(SETUP_MOVES);
|
||||
live.moveHistory.push(loggedFederation);
|
||||
const result = dropPlayedAnalysisPrefix(live, [move(federation), nav], 0, [], live.moveHistory.length);
|
||||
expect(result.entries).to.deep.equal([nav]);
|
||||
expect(result.played[0]).to.deep.equal({ move: loggedFederation, moveIndex: live.moveHistory.length - 1 });
|
||||
});
|
||||
|
||||
it("does not recover an alternative, an already-retired federation or a divergent played history", () => {
|
||||
const live = new Engine(SETUP_MOVES);
|
||||
const base = live.moveHistory.length;
|
||||
live.moveHistory.push(loggedFederation, "terrans up nav.");
|
||||
const entry = move(federation);
|
||||
const different = move(federation.replace("8A6", "8A7"));
|
||||
expect(dropPlayedAnalysisPrefix(live, [different], 0, [], live.moveHistory.length).entries).to.deep.equal([
|
||||
different,
|
||||
]);
|
||||
for (const previousPlayed of [
|
||||
[{ move: loggedFederation, moveIndex: base }],
|
||||
[{ move: "terrans up gaia.", moveIndex: base - 1 }],
|
||||
]) {
|
||||
expect(
|
||||
dropPlayedAnalysisPrefix(live, [entry], 0, [], live.moveHistory.length, previousPlayed).entries
|
||||
).to.deep.equal([entry]);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the whole turn when only one command matches", () => {
|
||||
const turn = move("terrans spend 1q for 1o. up nav.");
|
||||
expect(dropPlayedAnalysisPrefix(engine, [turn], 0, ["terrans up nav."]).entries).to.deep.equal([turn]);
|
||||
|
|
|
|||
|
|
@ -793,24 +793,24 @@ export function ownMoveCount(engine: Engine, moves: string[], seat: number): num
|
|||
function analysisMoveKey(engine: Engine, move: string): string {
|
||||
return JSON.stringify(
|
||||
parseCommands(move).map(({ command, args }) => {
|
||||
if (command === Command.Build) args = args.slice(0, 2);
|
||||
// Federation logs append "using areaN: ..." after the hexes and token, not another command.
|
||||
if (command === Command.Build || command === Command.FormFederation) args = args.slice(0, 2);
|
||||
if (command === Command.Pass || command === Command.UpgradeResearch) args = args.slice(0, 1);
|
||||
return [
|
||||
command,
|
||||
...args.map((arg) =>
|
||||
arg
|
||||
.split(",")
|
||||
.map((token) => {
|
||||
if (!/^(?:-?\d+x-?\d+|\d+[AB]\d+|IS\d+|DS\d+_\d+)$/.test(token)) return token;
|
||||
try {
|
||||
const { q, r } = engine.map.parse(token);
|
||||
return `${q}x${r}`;
|
||||
} catch {
|
||||
return token;
|
||||
}
|
||||
})
|
||||
.join(",")
|
||||
),
|
||||
...args.map((arg, index) => {
|
||||
const tokens = arg.split(",").map((token) => {
|
||||
if (!/^(?:-?\d+x-?\d+|\d+(?:[AB]\d+|C)|IS\d+|DS\d+_\d+)$/.test(token)) return token;
|
||||
try {
|
||||
const { q, r } = engine.map.parse(token);
|
||||
return `${q}x${r}`;
|
||||
} catch {
|
||||
return token;
|
||||
}
|
||||
});
|
||||
if (command === Command.FormFederation && index === 0) tokens.sort();
|
||||
return tokens.join(",");
|
||||
}),
|
||||
];
|
||||
})
|
||||
);
|
||||
|
|
@ -851,6 +851,29 @@ export function dropPlayedAnalysisPrefix(
|
|||
playedMoves = [...engine.moveHistory.slice(lastPlayed.moveIndex + 1, firstMoveIndex), ...playedMoves];
|
||||
firstMoveIndex = lastPlayed.moveIndex + 1;
|
||||
}
|
||||
// A failed federation match in older viewers still advanced the saved base. Recover from the
|
||||
// exact real turn, never from illegality alone. Ivits can federate the same hexes/token again,
|
||||
// so only new history can prove their move was played. Other factions cannot reuse those hexes.
|
||||
if (
|
||||
firstEntry?.kind === "move" &&
|
||||
engine.players[seat]?.faction !== Faction.Ivits &&
|
||||
moveBelongsToSeat(engine, firstEntry.move, seat) &&
|
||||
parseCommands(firstEntry.move).some(({ command }) => command === Command.FormFederation) &&
|
||||
(!lastPlayed || engine.moveHistory[lastPlayed.moveIndex] === (lastPlayed.historyMove ?? lastPlayed.move))
|
||||
) {
|
||||
const key = analysisMoveKey(engine, firstEntry.move);
|
||||
const recoveredIndex = engine.moveHistory.findIndex(
|
||||
(move, index) =>
|
||||
index > (lastPlayed?.moveIndex ?? -1) &&
|
||||
index < firstMoveIndex &&
|
||||
moveBelongsToSeat(engine, move, seat) &&
|
||||
analysisMoveKey(engine, move) === key
|
||||
);
|
||||
if (recoveredIndex !== -1) {
|
||||
playedMoves = [...engine.moveHistory.slice(recoveredIndex, firstMoveIndex), ...playedMoves];
|
||||
firstMoveIndex = recoveredIndex;
|
||||
}
|
||||
}
|
||||
const skipped = (engine.automation?.plans[seat]?.runs ?? []).flatMap((run) =>
|
||||
run.steps.filter((step) => step.status === "skipped" && step.moveIndex !== undefined)
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue