Batch Publishing House recruitment and guard pending recruitment submissions

This commit is contained in:
coyotte508 2026-10-31 22:37:21 +01:00
commit 1180e7d7c9
8 changed files with 130 additions and 14 deletions

View file

@ -796,7 +796,19 @@ function phaseLegal(s, t) {
case "assembly":
return [{ type: "assembly" }, pass];
case "recruitBonus":
return [...(s.workerSupply > 0 ? [{ type: "recruitBonus" }] : []), pass];
return [
...(s.workerSupply > 0
? [
{
type: "recruitBonus",
...(!a.puertoma && Math.min(t.remaining, s.workerSupply) > 1
? { amount: Math.min(t.remaining, s.workerSupply) }
: {}),
},
]
: []),
pass,
];
case "villa":
return [
...(s.citizenSupply + s.workerSupply > 0 ? [{ type: "villa" }] : []),
@ -1622,6 +1634,12 @@ function applyMove(state, m, p, historical = false) {
} else
assert(
legal(s, p).some((x) => sameMove(x, m)) ||
// Older clients and recorded games claimed the privilege one worker at a time.
(t.kind === "recruitBonus" &&
m.type === "recruitBonus" &&
m.amount === undefined &&
Object.keys(m).length === 1 &&
s.workerSupply > 0) ||
// Preserve already-recorded payments from before mandatory quarry discounts.
(historical &&
t.kind === "build" &&
@ -1733,10 +1751,14 @@ function applyMove(state, m, p, historical = false) {
assert(gainPerson(s, p, s.citizenSupply ? "c" : "w"));
next();
break;
case "recruitBonus":
assert(gainPerson(s, p));
if (--t.remaining === 0) next();
case "recruitBonus": {
const amount = m.amount ?? 1;
for (let i = 0; i < amount; i++) assert(gainPerson(s, p));
extra.amount = amount;
t.remaining -= amount;
if (t.remaining === 0) next();
break;
}
case "recruit":
s.register[m.kind]--;
a.reserve[m.kind]++;

View file

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

View file

@ -90,7 +90,7 @@ for (const seed of ["ledger-one", "ledger-two", "ledger-three"])
].includes(e.type) &&
(e.kind ?? "w") === kind
)
n++;
n += e.amount ?? 1;
if (kind === "w")
n +=
(e.reward?.workers ?? 0) +

View file

@ -0,0 +1,48 @@
import test from "node:test";
import assert from "node:assert/strict";
import * as E from "../engine/index.js";
function state(supply = 10) {
const s = E.init(3, [], {}, "recruit-bonus");
s.role = "recruiter";
s.owner = 0;
s.players[0].buildings.push({ id: "publishingHouse", w: 1, c: 0 });
s.tasks = [
{ kind: "recruitBonus", p: 0, remaining: 2 },
{ kind: "assign", p: 0 },
{ kind: "assign", p: 1 },
];
s.workerSupply = supply;
return s;
}
test("Publishing House bonus workers are claimed in one move and logged together", () => {
const s = state(),
before = s.players[0].reserve.w;
const m = E.legal(s, 0).find((m) => m.type === "recruitBonus");
assert.deepEqual(m, { type: "recruitBonus", amount: 2 });
const next = E.move(s, m, 0);
assert.equal(next.players[0].reserve.w, before + 2);
assert.equal(next.workerSupply, s.workerSupply - 2);
assert.equal(next.history.length, s.history.length + 1);
assert.equal(
next.events.findLast((e) => e.type === "recruitBonus").amount,
2,
);
assert.ok(!next.tasks.some((t) => t.kind === "recruitBonus"));
assert.throws(() => E.move(s, { type: "recruitBonus", amount: 3 }, 0));
});
test("bonus claims respect supply and old one-worker submissions remain valid", () => {
const s = state();
const first = E.move(s, { type: "recruitBonus" }, 0);
assert.equal(first.players[0].reserve.w, s.players[0].reserve.w + 1);
assert.equal(first.tasks[0].remaining, 1);
const second = E.move(first, { type: "recruitBonus" }, 0);
assert.equal(second.players[0].reserve.w, s.players[0].reserve.w + 2);
const low = state(1);
assert.deepEqual(
E.legal(low, 0).find((m) => m.type === "recruitBonus"),
{ type: "recruitBonus" },
);
const result = E.move(low, { type: "recruitBonus" }, 0);
assert.equal(result.workerSupply, 0);
assert.equal(result.players[0].reserve.w, low.players[0].reserve.w + 1);
});

View file

@ -138,3 +138,30 @@ test("finished BGS saves are converted to a complete public viewer state", async
assert.equal(state.historyLength, E.logLength(raw));
assert.equal(state.history, undefined);
});
test("duplicate recruitment snapshots cannot release a pending move", async () => {
const { viewer } = fixture();
viewer.onPlayer({ index: 0 });
const before = { historyLength: 8, tasks: [{ kind: "recruit", p: 0 }] };
viewer.onState(before);
let done = false;
const request = viewer.move({ type: "recruit", kind: "w" }).then(() => {
done = true;
});
viewer.onMoveResult({ ok: true });
viewer.onState(before);
await Promise.resolve();
assert.equal(done, false);
await assert.rejects(
viewer.move({ type: "recruit", kind: "w" }),
/unavailable/,
);
viewer.onState({ ...before, historyLength: 9 });
await request;
assert.equal(done, true);
// A later turn can legitimately offer the same recruitment choice again.
const next = viewer.move({ type: "recruit", kind: "c" });
viewer.onState({ historyLength: 10, tasks: [{ kind: "assign", p: 0 }] });
await next;
viewer.destroy();
});

View file

@ -115,7 +115,7 @@ export function journalContent(state, t, { automatedSetup = false } = {}) {
case "villa":
case "bonusAssign":
case "poachWorker":
text = `${t("journalRecruit")} ${metric(e.kind === "c" ? "citizen" : "worker", 1)}${target(e)}`;
text = `${t("journalRecruit")} ${metric(e.kind === "c" ? "citizen" : "worker", e.amount ?? 1)}${target(e)}`;
break;
case "bohio":
text = `${t("journalMoveWorker")} ${metric(e.kind === "c" ? "citizen" : "worker", 1)}${target(e)}`;

View file

@ -44,7 +44,13 @@ export function viewerLifecycle(ctx, ui, chat) {
if (replaying || pending)
return Promise.reject(Error("Move unavailable"));
return new Promise((resolve, reject) => {
pending = { resolve, reject, move: m, player: playerIndex };
pending = {
resolve,
reject,
move: m,
player: playerIndex,
historyLength: live?.historyLength,
};
timer = setTimeout(() => {
settle(Error("No confirmation received. Please try again."));
ctx.fetchState();
@ -55,22 +61,28 @@ export function viewerLifecycle(ctx, ui, chat) {
chat,
onState(s) {
// BGS returns the raw saved engine state after the game ends.
if (s.finished && Array.isArray(s.history)) s = stripSecret(s, playerIndex);
if (s.finished && Array.isArray(s.history))
s = stripSecret(s, playerIndex);
live = s;
const awaitingPlacement =
const awaitingMove =
pending &&
((pending.move.type === "assign" &&
s.tasks.some((t) => t.kind === "assign" && t.p === pending.player)) ||
((["recruit", "recruitBonus"].includes(pending.move.type) &&
Number.isInteger(pending.historyLength) &&
s.historyLength <= pending.historyLength) ||
(pending.move.type === "assign" &&
s.tasks.some(
(t) => t.kind === "assign" && t.p === pending.player,
)) ||
(pending.move.type === "orderedPlacement" && !s.placementOrdered));
if (!replaying) {
position = Math.max(1, s.historyLength);
ui.render(s, !!awaitingPlacement);
ui.render(s, !!awaitingMove);
} else {
// An undo or branch replacement may shorten the host history.
if (position > s.historyLength) seek(s.historyLength);
info();
}
if (!awaitingPlacement) settle();
if (!awaitingMove) settle();
},
onThumbnail: () => ui.thumbnail(),
onPlayer: (p) => {

View file

@ -489,6 +489,13 @@ export function mountGame(
function actionButton(m) {
if (m.type === "produce")
return `${moveButton(m, `<span class="produce-label">${t("produce")}</span><span class="produce-goods">${goodsRow(m.goods)}</span>`, "check", "primary produce-action", t("produce"))}<button class="quiet production-custom" data-custom-production="${encode(m)}" title="${esc(t("selectQty"))}" aria-label="${esc(t("selectQty"))}" ${allowed() ? "" : "disabled"}>${icon("edit")}<span>${t("selectQty")}</span></button>`;
if (m.type === "recruitBonus")
return moveButton(
m,
`${t("claim")} ${metric("worker", m.amount ?? 1)}`,
"check",
"primary",
);
if (m.type === "role")
return roleCard(state.roles.find((r) => r.id === m.id));
const compact = compactAction(m);