Improve handoff undo and confirm Festival worker placement as a batch
This commit is contained in:
parent
5aa255410c
commit
09f6070e36
8 changed files with 201 additions and 52 deletions
|
|
@ -53,18 +53,16 @@ export function move(s, m, p) {
|
|||
checkSeat(s, p);
|
||||
const next = announceCompletions(s, opponents(game.move(s, m, p)));
|
||||
delete next.liveUndo;
|
||||
// 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 ?? []);
|
||||
// Reopening a completed choice must not earn a second clock increment.
|
||||
// Keep this entitlement across other seats' independent placement updates.
|
||||
const editing = new Set(s.undoResubmitSeats ?? 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;
|
||||
if (!game.currentPlayers(s).includes(p)) editing.add(p);
|
||||
} else if (editing.delete(p)) next.liveUndo = true;
|
||||
delete next.placementUndoSeats;
|
||||
if (editing.size) next.undoResubmitSeats = [...editing];
|
||||
else delete next.undoResubmitSeats;
|
||||
return next;
|
||||
}
|
||||
export function moveAI(s, p = game.currentPlayer(s)) {
|
||||
|
|
|
|||
|
|
@ -1342,14 +1342,7 @@ export function canUndo(s, p) {
|
|||
if (s.tutorial) return false;
|
||||
if (placementUndoState(s, p)) return true;
|
||||
const last = s.history.at(-1);
|
||||
if (
|
||||
!last ||
|
||||
last.p !== p ||
|
||||
s.finished ||
|
||||
currentPlayers(s).length !== 1 ||
|
||||
currentPlayer(s) !== p
|
||||
)
|
||||
return false;
|
||||
if (!last || last.p !== p || s.finished) return false;
|
||||
const before = replay(s, logLength(s) - 1);
|
||||
return (
|
||||
!s.players.some((a, i) =>
|
||||
|
|
@ -1370,6 +1363,7 @@ export function canUndo(s, p) {
|
|||
) &&
|
||||
currentPlayers(before).length === 1 &&
|
||||
currentPlayer(before) === p &&
|
||||
JSON.stringify(before.bag) === JSON.stringify(s.bag) &&
|
||||
JSON.stringify(before.random) === JSON.stringify(s.random)
|
||||
);
|
||||
}
|
||||
|
|
@ -1479,6 +1473,7 @@ function applyMove(state, m, p, historical = false) {
|
|||
}
|
||||
const custom = {
|
||||
assign: "assign",
|
||||
bonusAssignAll: "bonusAssign",
|
||||
produce: "produce",
|
||||
store: "store",
|
||||
smallWharf: "ship",
|
||||
|
|
@ -1663,6 +1658,26 @@ function applyMove(state, m, p, historical = false) {
|
|||
next();
|
||||
break;
|
||||
}
|
||||
case "bonusAssignAll": {
|
||||
assert(Array.isArray(m.targets) && m.targets.length <= t.remaining);
|
||||
for (const target of m.targets) {
|
||||
assert(destinations(a).includes(target), "Invalid bonus worker slot");
|
||||
a.reserve.w--;
|
||||
tileRef(a, target).w++;
|
||||
t.remaining--;
|
||||
event(s, "bonusAssign", p, {
|
||||
target,
|
||||
targetId: tileRef(a, target).id,
|
||||
phase: t.kind,
|
||||
});
|
||||
}
|
||||
assert(
|
||||
t.remaining === 0 || destinations(a).length === 0,
|
||||
"Place all available bonus workers",
|
||||
);
|
||||
next();
|
||||
break;
|
||||
}
|
||||
case "bonusAssign":
|
||||
a.reserve.w--;
|
||||
tileRef(a, m.target).w++;
|
||||
|
|
@ -1907,7 +1922,7 @@ function applyMove(state, m, p, historical = false) {
|
|||
default:
|
||||
assert(false);
|
||||
}
|
||||
if (!["role", "plant"].includes(m.type))
|
||||
if (!["role", "plant", "bonusAssignAll"].includes(m.type))
|
||||
if (!["achievementChoose", "achievementDraft"].includes(m.type))
|
||||
event(s, m.type, p, { ...m, ...extra });
|
||||
s.history.push({ p, move: clone(m) });
|
||||
|
|
@ -1989,6 +2004,7 @@ export function stripSecret(state, p) {
|
|||
delete s.pendingMessages;
|
||||
delete s.liveUndo;
|
||||
delete s.placementUndoSeats;
|
||||
delete s.undoResubmitSeats;
|
||||
s.bagCount = s.bag.length;
|
||||
delete s.bag;
|
||||
delete s.discard;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/puerto-rico",
|
||||
"version": "0.3.7",
|
||||
"version": "0.3.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Puerto Rico 1897 Special Edition for Boardgamers Space",
|
||||
|
|
|
|||
37
test/bonus-placement.test.js
Normal file
37
test/bonus-placement.test.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import * as E from "../engine/index.js";
|
||||
function fixture() {
|
||||
const s = E.init(3, [], {}, "bonus-draft");
|
||||
s.tasks = [
|
||||
{ kind: "bonusAssign", p: 0, remaining: 2 },
|
||||
{ kind: "role", p: 1 },
|
||||
];
|
||||
s.players[0].reserve.w = 2;
|
||||
s.players[0].estates = [
|
||||
{ id: "corn", w: 1, c: 0 },
|
||||
{ id: "fruit", w: 0, c: 0 },
|
||||
];
|
||||
s.players[0].buildings = [{ id: "smallSugar", w: 0, c: 0 }];
|
||||
return s;
|
||||
}
|
||||
test("bonus placement submits all workers in one move and keeps existing workers fixed", () => {
|
||||
const s = fixture();
|
||||
const next = E.move(s, { type: "bonusAssignAll", targets: ["e1", "b0"] }, 0);
|
||||
assert.equal(next.history.length, s.history.length + 1);
|
||||
assert.equal(next.players[0].estates[0].w, 1);
|
||||
assert.equal(next.players[0].estates[1].w, 1);
|
||||
assert.equal(next.players[0].buildings[0].w, 1);
|
||||
assert.equal(next.players[0].reserve.w, 0);
|
||||
assert.equal(next.tasks[0].p, 1);
|
||||
for (const targets of [["e0", "b0"], ["e1", "e1"], ["e1"], []])
|
||||
assert.throws(() => E.move(s, { type: "bonusAssignAll", targets }, 0));
|
||||
assert.equal(s.players[0].reserve.w, 2);
|
||||
});
|
||||
test("bonus workers without available slots stay in reserve", () => {
|
||||
const s = fixture();
|
||||
s.players[0].buildings[0].w = 1;
|
||||
const next = E.move(s, { type: "bonusAssignAll", targets: ["e1"] }, 0);
|
||||
assert.equal(next.players[0].reserve.w, 1);
|
||||
assert.equal(next.tasks[0].p, 1);
|
||||
});
|
||||
|
|
@ -1107,29 +1107,37 @@ try {
|
|||
await page.locator(".placement-editor button.worker-slot").count(),
|
||||
2,
|
||||
);
|
||||
await page.locator(".placement-editor button.worker-slot").first().click();
|
||||
assert.deepEqual(await page.evaluate(() => played.at(-1)), {
|
||||
type: "bonusAssign",
|
||||
target: "e1",
|
||||
});
|
||||
const bonusAfter = E.move(
|
||||
bonusPlacement,
|
||||
{ type: "bonusAssign", target: "e1" },
|
||||
0,
|
||||
);
|
||||
await page.evaluate(
|
||||
(s) => host.emit("state", s),
|
||||
E.stripSecret(bonusAfter, 0),
|
||||
const movesBeforeBonus = await page.evaluate(() => played.length);
|
||||
await page.locator('.placement-editor [data-bonus-slot="e1"]').click();
|
||||
assert.equal(
|
||||
await page.evaluate(() => played.length),
|
||||
movesBeforeBonus,
|
||||
"slot clicks stay local",
|
||||
);
|
||||
assert.equal(
|
||||
await page.locator(".placement-editor button.worker-slot").count(),
|
||||
1,
|
||||
"bonus placement stays open for the remaining worker",
|
||||
await page.locator('.placement-editor [data-bonus-slot="e0"]').count(),
|
||||
0,
|
||||
"original worker is fixed",
|
||||
);
|
||||
await page.locator(".placement-editor button.worker-slot").click();
|
||||
await page.locator('.placement-editor [data-bonus-slot="e1"]').click();
|
||||
assert.equal(
|
||||
await page
|
||||
.locator('.placement-editor [data-bonus-slot="e1"]')
|
||||
.getAttribute("data-bonus-occupied"),
|
||||
"false",
|
||||
"new placement can be revised",
|
||||
);
|
||||
await page.locator('.placement-editor [data-bonus-slot="e1"]').click();
|
||||
assert.equal(
|
||||
await page.locator(".pr-modal [data-confirm-bonus]").isDisabled(),
|
||||
true,
|
||||
);
|
||||
await page.locator('.placement-editor [data-bonus-slot="b0"]').click();
|
||||
assert.equal(await page.evaluate(() => played.length), movesBeforeBonus);
|
||||
await page.locator(".pr-modal [data-confirm-bonus]").click();
|
||||
assert.deepEqual(await page.evaluate(() => played.at(-1)), {
|
||||
type: "bonusAssign",
|
||||
target: "b0",
|
||||
type: "bonusAssignAll",
|
||||
targets: ["e1", "b0"],
|
||||
});
|
||||
let undoPlacement = E.init(3, [], {}, "browser-placement-undo");
|
||||
undoPlacement = E.move(undoPlacement, { type: "role", id: "recruiter" }, 0);
|
||||
|
|
|
|||
|
|
@ -152,8 +152,8 @@ test("withdrawing and re-confirming placement do not award another clock increme
|
|||
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.deepEqual(s.undoResubmitSeats, [2]);
|
||||
assert.equal(host.stripSecret(s, 2).undoResubmitSeats, undefined);
|
||||
}
|
||||
assert.equal(s.history.length, recruit().history.length);
|
||||
});
|
||||
|
|
|
|||
69
test/undo-handoffs.test.js
Normal file
69
test/undo-handoffs.test.js
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import * as E from "../engine/index.js";
|
||||
import * as host from "../bgs/engine.js";
|
||||
|
||||
test("completed choices remain undoable until another player acts or new information is revealed", () => {
|
||||
let s = E.init(3, ["smuggler"], {}, "undo-handoffs");
|
||||
const covered = new Set();
|
||||
let blockedDraw = false;
|
||||
for (let i = 0; i < 350 && !s.finished; i++) {
|
||||
const p = E.currentPlayer(s);
|
||||
const after = E.moveAI(s);
|
||||
if (
|
||||
after.history.length === s.history.length + 1 &&
|
||||
JSON.stringify(s.bag) !== JSON.stringify(after.bag)
|
||||
) {
|
||||
assert.equal(
|
||||
E.canUndo(after, p),
|
||||
false,
|
||||
"drawing from a shuffled bag reveals information",
|
||||
);
|
||||
if (s.random === after.random) blockedDraw = true;
|
||||
}
|
||||
if (
|
||||
!covered.has(after.history.at(-1)?.move.type) &&
|
||||
after.history.length === s.history.length + 1 &&
|
||||
E.currentPlayer(after) !== p
|
||||
) {
|
||||
if (E.canUndo(after, p)) {
|
||||
const m = after.history.at(-1).move;
|
||||
covered.add(m.type);
|
||||
assert.equal(E.canMoveOutOfTurn(after, { type: "undo" }, p), true);
|
||||
assert.deepEqual(E.move(after, { type: "undo" }, p), s);
|
||||
if (!E.currentPlayers(after).includes(p)) {
|
||||
const undone = host.move(after, { type: "undo" }, p);
|
||||
assert.equal(host.isLiveUpdate(undone), true);
|
||||
const redone = host.move(undone, m, p);
|
||||
assert.equal(
|
||||
host.isLiveUpdate(redone),
|
||||
true,
|
||||
`${m.type} must not credit time twice`,
|
||||
);
|
||||
if (!after.finished) {
|
||||
const next = E.moveAI(after);
|
||||
if (next.history.at(-1).p !== p)
|
||||
assert.equal(E.canUndo(next, p), false);
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
s.random !== after.random ||
|
||||
JSON.stringify(s.bag) !== JSON.stringify(after.bag)
|
||||
)
|
||||
blockedDraw = true;
|
||||
}
|
||||
s = after;
|
||||
}
|
||||
for (const type of [
|
||||
"plant",
|
||||
"produce",
|
||||
"produceBonus",
|
||||
"build",
|
||||
"ship",
|
||||
"store",
|
||||
"hospital",
|
||||
"pass",
|
||||
])
|
||||
assert.ok(covered.has(type), `covered ${type}`);
|
||||
assert.equal(blockedDraw, true, "plantation draws still prevent undo");
|
||||
});
|
||||
47
viewer/ui.js
47
viewer/ui.js
|
|
@ -332,6 +332,8 @@ export function mountGame(
|
|||
};
|
||||
tool = a.reserve.w ? "w" : a.reserve.c ? "c" : "w";
|
||||
}
|
||||
if (enabled && seat === task?.p && task?.kind === "bonusAssign")
|
||||
draft = { targets: [] };
|
||||
if (enabled && seat === task?.p && task?.kind === "store")
|
||||
draft = autoStorage(state, seat);
|
||||
}
|
||||
|
|
@ -534,7 +536,7 @@ export function mountGame(
|
|||
if (task.kind === "assign")
|
||||
return `<div class="placement-editor">${assignmentTools()}${playerAreas(state.players[seat])}</div><footer class="action-editor-footer">${confirmation()}</footer>`;
|
||||
if (task.kind === "bonusAssign")
|
||||
return `<div class="placement-editor"><div class="assignment-tools">${metric("worker", task.remaining)}</div>${playerAreas(state.players[seat])}</div>`;
|
||||
return `<div class="placement-editor"><div class="assignment-tools">${metric("worker", task.remaining - draft.targets.length)}</div>${playerAreas(state.players[seat])}</div><footer class="action-editor-footer">${confirmation()}</footer>`;
|
||||
if (task.kind === "store")
|
||||
return `${storageEditor()}<footer class="action-editor-footer">${confirmation()}</footer>`;
|
||||
return `<div class="action-choices">${moves.map(actionButton).join("")}</div>`;
|
||||
|
|
@ -569,6 +571,11 @@ export function mountGame(
|
|||
.join("")}</div>`;
|
||||
}
|
||||
function peopleSlots(key, x, n = 1) {
|
||||
const bonus =
|
||||
draft && state.tasks[0]?.kind === "bonusAssign" && view === seat;
|
||||
const added = bonus
|
||||
? draft.targets.filter((target) => target === key).length
|
||||
: 0;
|
||||
const preview =
|
||||
draft &&
|
||||
state.tasks[0].kind === "assign" &&
|
||||
|
|
@ -577,22 +584,13 @@ export function mountGame(
|
|||
editable = preview && allowed(),
|
||||
values = preview
|
||||
? draft[key[0] === "e" ? "estates" : "buildings"][Number(key.slice(1))]
|
||||
: x;
|
||||
: { ...x, w: x.w + added };
|
||||
return `<div class="worker-slots">${Array.from({ length: n }, (_, i) => {
|
||||
const k = i < values.c ? "c" : i < values.c + values.w ? "w" : null;
|
||||
const unavailable =
|
||||
editable && !k && (tool === "erase" || remaining()[tool] === 0);
|
||||
const bonusMove =
|
||||
!k &&
|
||||
view === seat &&
|
||||
allowed() &&
|
||||
state.tasks[0]?.kind === "bonusAssign"
|
||||
? state.legal.find(
|
||||
(m) => m.type === "bonusAssign" && m.target === key,
|
||||
)
|
||||
: null;
|
||||
if (bonusMove)
|
||||
return `<button type="button" class="worker-slot" data-move="${encode(bonusMove)}" aria-label="${esc(`${t("workers")} · ${t(x.id)}`)}">+</button>`;
|
||||
if (bonus && i >= x.w + x.c)
|
||||
return `<button type="button" class="worker-slot ${k ?? ""}" data-bonus-slot="${key}" data-bonus-occupied="${!!k}" ${!allowed() || (!k && draft.targets.length >= state.tasks[0].remaining) ? "disabled" : ""} aria-label="${esc(`${t("workers")} · ${t(x.id)}`)}">${k ? icon("worker", 20) : "+"}</button>`;
|
||||
return editable
|
||||
? `<button class="worker-slot ${k ?? ""}" data-slot="${key}:${i}" ${unavailable ? "disabled" : ""} aria-label="${esc(`${t(k === "c" ? "citizens" : k === "w" ? "workers" : "empty")} · ${t(x.id)}`)}">${k ? icon(k === "c" ? "citizen" : "worker", 20) : unavailable ? "·" : "+"}</button>`
|
||||
: `<span class="worker-slot ${k ?? ""}">${k ? icon(k === "c" ? "citizen" : "worker", 20) : "·"}</span>`;
|
||||
|
|
@ -848,6 +846,16 @@ export function mountGame(
|
|||
valid = assigned === Math.min(capacity, assigned + rest.w + rest.c);
|
||||
confirm = `<button class="primary" data-confirm-assign ${valid ? "" : "disabled"}>${icon("check")} ${t("confirm")}</button>`;
|
||||
}
|
||||
if (task.kind === "bonusAssign") {
|
||||
const a = state.players[seat];
|
||||
const spaces =
|
||||
a.estates.reduce(
|
||||
(n, x) => n + (x.id === "forest" ? 0 : 1 - x.w - x.c),
|
||||
0,
|
||||
) + a.buildings.reduce((n, x) => n + B[x.id].workers - x.w - x.c, 0);
|
||||
const valid = draft.targets.length === Math.min(task.remaining, spaces);
|
||||
confirm = `<button class="primary" data-confirm-bonus ${valid ? "" : "disabled"}>${icon("check")} ${t("confirm")}</button>`;
|
||||
}
|
||||
if (task.kind === "store") {
|
||||
const n = sum(
|
||||
GOODS.filter((g) => !draft.types.includes(g)).map(
|
||||
|
|
@ -1472,6 +1480,19 @@ export function mountGame(
|
|||
);
|
||||
return;
|
||||
}
|
||||
if (el.dataset.bonusSlot) {
|
||||
const key = el.dataset.bonusSlot;
|
||||
if (el.dataset.bonusOccupied === "true")
|
||||
draft.targets.splice(draft.targets.lastIndexOf(key), 1);
|
||||
else if (draft.targets.length < state.tasks[0].remaining)
|
||||
draft.targets.push(key);
|
||||
render();
|
||||
return;
|
||||
}
|
||||
if (el.hasAttribute("data-confirm-bonus")) {
|
||||
send({ type: "bonusAssignAll", targets: [...draft.targets] });
|
||||
return;
|
||||
}
|
||||
if (el.hasAttribute("data-confirm-assign")) {
|
||||
send({ type: "assign", ...draft });
|
||||
return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue