Allow withdrawing parallel placements while earlier players decide

This commit is contained in:
coyotte508 2026-10-28 22:06:55 +01:00
commit 5aa255410c
6 changed files with 151 additions and 10 deletions

View file

@ -53,7 +53,18 @@ export function move(s, m, p) {
checkSeat(s, p);
const next = announceCompletions(s, opponents(game.move(s, m, p)));
delete next.liveUndo;
if (m?.type === "undo") next.liveUndo = true;
// A withdrawn placement already earned its increment. Re-confirmation is
// also a live update, even when another player moves between the two.
const editing = new Set(s.placementUndoSeats ?? []);
if (m?.type === "undo") {
next.liveUndo = true;
if (s.tasks[0]?.kind === "assign" && !game.currentPlayers(s).includes(p))
editing.add(p);
}
if (m?.type === "assign" && editing.delete(p)) next.liveUndo = true;
if (next.tasks[0]?.kind !== "assign") editing.clear();
if (editing.size) next.placementUndoSeats = [...editing];
else delete next.placementUndoSeats;
return next;
}
export function moveAI(s, p = game.currentPlayer(s)) {
@ -140,7 +151,7 @@ export function messages(s) {
return { messages: s.pendingMessages ?? [], data };
}
// Undo updates the saved position without awarding another clock increment.
// Undo and replacement placements update the position without another increment.
export function isLiveUpdate(s) {
return s.liveUndo === true;
}

View file

@ -1303,8 +1303,44 @@ function sameMove(a, b) {
: x;
return JSON.stringify(normalize(a)) === JSON.stringify(normalize(b));
}
// Withdraw only this seat's independent placement while an earlier seat is
// still deciding. Never rewind another player's move or a revealed reward.
function placementUndoState(s, p) {
if (s.tutorial || s.finished || s.placementOrdered || !Number.isInteger(p))
return null;
const tasks = placementTasks(s);
const rank = (seat) => (seat - s.owner + s.players.length) % s.players.length;
if (
!tasks.length ||
tasks.some((t) => t.p === p) ||
!tasks.some((t) => rank(t.p) < rank(p))
)
return null;
const index = s.history.findLastIndex((h) => h.p === p);
if (
index < (s.roleHistoryStart ?? 0) ||
s.history[index]?.move.type !== "assign" ||
s.history.slice(index + 1).some((h) => h.move.type !== "assign")
)
return null;
const candidate = replay({
...s,
history: s.history.filter((_, i) => i !== index),
});
const revealed = (state) =>
JSON.stringify([
state.players.map((a) =>
(a.achievements ?? []).filter((c) => c.completed),
),
(state.festivals ?? []).filter((f) => f.claimed !== undefined),
]);
if (revealed(candidate) !== revealed(s) || candidate.random !== s.random)
return null;
return candidate;
}
export function canUndo(s, p) {
if (s.tutorial) return false;
if (placementUndoState(s, p)) return true;
const last = s.history.at(-1);
if (
!last ||
@ -1340,7 +1376,7 @@ export function canUndo(s, p) {
export function move(state, m, p) {
if (m?.type === "undo") {
assert(canUndo(state, p), "This move can no longer be undone");
return replay(state, logLength(state) - 1);
return placementUndoState(state, p) ?? replay(state, logLength(state) - 1);
}
return resolveForcedActions(applyMove(state, m, p));
}
@ -1884,7 +1920,10 @@ export function currentPlayer(s) {
return s.finished ? undefined : s.tasks[0]?.p;
}
export function canMoveOutOfTurn(s, m, p) {
return m?.type === "bohio" && legal(s, p).some((x) => sameMove(x, m));
return (
(m?.type === "undo" && canUndo(s, p)) ||
(m?.type === "bohio" && legal(s, p).some((x) => sameMove(x, m)))
);
}
export function ended(s) {
return s.finished;
@ -1949,6 +1988,7 @@ export function stripSecret(state, p) {
delete s.history;
delete s.pendingMessages;
delete s.liveUndo;
delete s.placementUndoSeats;
s.bagCount = s.bag.length;
delete s.bag;
delete s.discard;

View file

@ -1,6 +1,6 @@
{
"name": "@boardgamers/puerto-rico",
"version": "0.3.6",
"version": "0.3.7",
"private": true,
"type": "module",
"description": "Puerto Rico 1897 Special Edition for Boardgamers Space",

View file

@ -1131,6 +1131,34 @@ try {
type: "bonusAssign",
target: "b0",
});
let undoPlacement = E.init(3, [], {}, "browser-placement-undo");
undoPlacement = E.move(undoPlacement, { type: "role", id: "recruiter" }, 0);
undoPlacement = E.move(undoPlacement, { type: "recruitBonus" }, 0);
undoPlacement = E.move(
undoPlacement,
{ type: "assign", ...E.autoAssignment(undoPlacement, 1) },
1,
);
await page.evaluate(
(s) => {
host.emit("player", { index: 1 });
host.emit("state", s);
},
E.stripSecret(undoPlacement, 1),
);
await page.locator(".action-dock .dock-undo").click();
assert.deepEqual(await page.evaluate(() => played.at(-1)), {
type: "undo",
});
await page.evaluate(
(s) => host.emit("state", s),
E.stripSecret(E.move(undoPlacement, { type: "undo" }, 1), 1),
);
await page.locator(".action-dock [data-actions]").click();
assert.ok(
await page.locator(".placement-editor [data-slot]").count(),
"withdrawn placement can be edited",
);
assert.deepEqual(errors, [], `No browser errors at ${width}`);
console.log(
`Browser ${width}px: actions, allocation, layout, journal, chat and scrolling OK`,

View file

@ -108,3 +108,62 @@ test("BGS currentPlayer exposes every simultaneous placement seat", async () =>
const s = recruit();
assert.deepEqual(host.currentPlayer(s), [0, 1, 2]);
});
test("a later seat can withdraw its placement without undoing another seat", () => {
const start = recruit();
let s = E.move(start, { type: "assign", ...E.autoAssignment(start, 1) }, 1);
s = E.move(s, { type: "assign", ...E.autoAssignment(s, 2) }, 2);
assert.equal(E.canUndo(s, 1), true);
assert.equal(E.canMoveOutOfTurn(s, { type: "undo" }, 1), true);
const restored = E.move(s, { type: "undo" }, 1);
assert.deepEqual(restored.players[1], start.players[1]);
assert.deepEqual(restored.players[2], s.players[2]);
assert.deepEqual(E.currentPlayers(restored), [0, 1]);
assert.equal(restored.history.length, s.history.length - 1);
assert.deepEqual(E.replay(restored), restored);
assert.equal(E.canUndo(restored, 1), false);
const firstDone = E.move(
restored,
{ type: "assign", ...E.autoAssignment(restored, 0) },
0,
);
assert.equal(E.canUndo(firstDone, 2), true, "seat 1 still precedes seat 2");
const finished = E.move(
firstDone,
{ type: "assign", ...E.autoAssignment(firstDone, 1) },
1,
);
assert.equal(E.canUndo(finished, 2), false);
});
test("an earlier seat cannot withdraw after only later seats remain", () => {
const start = recruit();
const s = E.move(start, { type: "assign", ...E.autoAssignment(start, 0) }, 0);
assert.equal(E.canUndo(s, 0), false);
assert.equal(E.canMoveOutOfTurn(s, { type: "undo" }, 0), false);
assert.throws(() => E.move(s, { type: "undo" }, 0));
});
test("withdrawing and re-confirming placement do not award another clock increment", async () => {
const host = await import("../bgs/engine.js");
let s = recruit();
for (let i = 0; i < 3; i++) {
s = host.move(s, { type: "assign", ...E.autoAssignment(s, 2) }, 2);
assert.equal(host.isLiveUpdate(s), i > 0);
s = host.move(s, { type: "undo" }, 2);
assert.equal(host.isLiveUpdate(s), true);
assert.deepEqual(s.placementUndoSeats, [2]);
assert.equal(host.stripSecret(s, 2).placementUndoSeats, undefined);
}
assert.equal(s.history.length, recruit().history.length);
});
test("placement undo never retracts a revealed achievement or Festival reward", () => {
const start = recruit();
const s = E.move(start, { type: "assign", ...E.autoAssignment(start, 2) }, 2);
s.players[2].achievements = [{ id: "revealed", completed: true }];
assert.equal(E.canUndo(s, 2), false);
s.players[2].achievements = [];
s.festivals = [{ id: "claimed", claimed: 2 }];
assert.equal(E.canUndo(s, 2), false);
});

View file

@ -254,7 +254,7 @@ export function mountGame(
.filter(Boolean)
.join(" · ");
const bonus = rewardPreview(m);
return `<button type="button" class="move ${classes}" data-move="${encode(m)}" ${hint ? `title="${esc(hint)}" ${description ? `aria-label="${esc(hint)}"` : ""}` : ""} ${allowed() || (enabled && !pending && m.type === "bohio") ? "" : "disabled"}>${pictogram ? icon(pictogram, 26) : ""}<span>${label}${bonus}</span></button>`;
return `<button type="button" class="move ${classes}" data-move="${encode(m)}" ${hint ? `title="${esc(hint)}" ${description ? `aria-label="${esc(hint)}"` : ""}` : ""} ${allowed() || (enabled && !pending && (m.type === "bohio" || (m.type === "undo" && state.canUndo))) ? "" : "disabled"}>${pictogram ? icon(pictogram, 26) : ""}<span>${label}${bonus}</span></button>`;
};
function rewardPreview(move) {
const festival = festivalRewards(state, seat, move);
@ -918,7 +918,7 @@ export function mountGame(
)
: "";
const undo =
state.canUndo && can
state.canUndo && enabled
? moveButton({ type: "undo" }, t("undo"), "undo", "quiet dock-undo")
: "";
const controls = special + undo + choices + confirm + passControl + order;
@ -1273,8 +1273,9 @@ export function mountGame(
!(
enabled &&
!pending &&
m.type === "bohio" &&
state.legal.some((x) => JSON.stringify(x) === JSON.stringify(m))
((m.type === "undo" && state.canUndo) ||
(m.type === "bohio" &&
state.legal.some((x) => JSON.stringify(x) === JSON.stringify(m))))
)
)
return;
@ -1417,7 +1418,9 @@ export function mountGame(
!el.hasAttribute("data-bohio") &&
!(
el.dataset.move &&
JSON.parse(decodeURIComponent(el.dataset.move)).type === "bohio"
["bohio", ...(state.canUndo ? ["undo"] : [])].includes(
JSON.parse(decodeURIComponent(el.dataset.move)).type,
)
)
)
return;