Use board placement editor for Smuggler poaching
This commit is contained in:
parent
958d967998
commit
875f636da9
5 changed files with 237 additions and 4 deletions
|
|
@ -1628,6 +1628,7 @@ function applyMove(state, m, p, historical = false) {
|
|||
const custom = {
|
||||
assign: "assign",
|
||||
bonusAssignAll: "bonusAssign",
|
||||
poachAll: "poach",
|
||||
produce: "produce",
|
||||
store: "store",
|
||||
smallWharf: "ship",
|
||||
|
|
@ -2056,6 +2057,39 @@ function applyMove(state, m, p, historical = false) {
|
|||
remaining: Math.min(3, total(s.register) - s.players.length),
|
||||
};
|
||||
break;
|
||||
case "poachAll": {
|
||||
assert(
|
||||
Array.isArray(m.placements) &&
|
||||
m.placements.length <=
|
||||
Math.min(
|
||||
3,
|
||||
t.remaining,
|
||||
Math.max(0, total(s.register) - s.players.length),
|
||||
),
|
||||
"Too many poached workers",
|
||||
);
|
||||
for (const placement of m.placements) {
|
||||
assert(placement && typeof placement === "object");
|
||||
const { kind, target } = placement;
|
||||
assert(
|
||||
["w", "c"].includes(kind) && s.register[kind] > 0,
|
||||
"Unavailable register worker",
|
||||
);
|
||||
assert(destinations(a).includes(target), "Invalid poach slot");
|
||||
s.register[kind]--;
|
||||
tileRef(a, target)[kind]++;
|
||||
t.remaining--;
|
||||
event(s, "poachWorker", p, {
|
||||
kind,
|
||||
target,
|
||||
targetId: tileRef(a, target).id,
|
||||
phase: t.kind,
|
||||
});
|
||||
}
|
||||
// Confirmation ends placement even if the player chose fewer than the limit.
|
||||
endPoach(s, t);
|
||||
break;
|
||||
}
|
||||
case "poachWorker":
|
||||
s.register[m.kind]--;
|
||||
tileRef(a, m.target)[m.kind]++;
|
||||
|
|
@ -2086,7 +2120,7 @@ function applyMove(state, m, p, historical = false) {
|
|||
default:
|
||||
assert(false);
|
||||
}
|
||||
if (!["role", "plant", "bonusAssignAll"].includes(m.type))
|
||||
if (!["role", "plant", "bonusAssignAll", "poachAll"].includes(m.type))
|
||||
if (!["achievementChoose", "achievementDraft"].includes(m.type))
|
||||
event(s, m.type, p, { ...m, ...extra });
|
||||
s.history.push({ p, move: clone(m) });
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/puerto-rico",
|
||||
"version": "0.3.15",
|
||||
"version": "0.3.16",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Puerto Rico 1897 Special Edition for Boardgamers Space",
|
||||
|
|
|
|||
|
|
@ -1526,6 +1526,83 @@ try {
|
|||
type: "bonusAssignAll",
|
||||
targets: ["e1", "b0"],
|
||||
});
|
||||
const poachPlacement = structuredClone(bonusPlacement);
|
||||
poachPlacement.tasks = [{ kind: "poach", p: 0, remaining: 3 }];
|
||||
poachPlacement.register = { w: 5, c: 1 };
|
||||
poachPlacement.players[0].buildings = [{ id: "largeSugar", w: 0, c: 1 }];
|
||||
await page.evaluate(
|
||||
(s) => host.emit("state", s),
|
||||
E.stripSecret(poachPlacement, 0),
|
||||
);
|
||||
await page.locator(".action-dock [data-actions]").click();
|
||||
const beforePoach = await page.evaluate(() => played.length);
|
||||
assert.equal(
|
||||
await page.locator('.placement-editor [data-poach-slot="e0"]').count(),
|
||||
0,
|
||||
);
|
||||
assert.equal(
|
||||
await page.locator('.placement-editor [data-poach-slot="b0"]').count(),
|
||||
2,
|
||||
);
|
||||
await page.locator('.placement-editor [data-tool="c"]').click();
|
||||
await page.locator('.placement-editor [data-poach-slot="e1"]').click();
|
||||
assert.ok(
|
||||
await page
|
||||
.locator('.placement-editor [data-poach-slot="b0"]')
|
||||
.first()
|
||||
.isDisabled(),
|
||||
"citizen supply exhausted",
|
||||
);
|
||||
await page.locator('.placement-editor [data-poach-slot="e1"]').click();
|
||||
assert.ok(
|
||||
await page
|
||||
.locator('.placement-editor [data-poach-slot="b0"]')
|
||||
.first()
|
||||
.isEnabled(),
|
||||
"draft removal returns citizen",
|
||||
);
|
||||
await page
|
||||
.locator('.placement-editor [data-poach-slot="b0"]')
|
||||
.first()
|
||||
.click();
|
||||
await page.locator('.placement-editor [data-tool="w"]').click();
|
||||
await page.locator('.placement-editor [data-poach-slot="e1"]').click();
|
||||
await page
|
||||
.locator('.placement-editor [data-poach-slot="b0"]')
|
||||
.last()
|
||||
.click();
|
||||
assert.equal(
|
||||
await page.locator('.placement-editor [data-poach-index="2"]').count(),
|
||||
1,
|
||||
);
|
||||
await page.locator('.placement-editor [data-poach-index="2"]').click();
|
||||
assert.equal(
|
||||
await page.evaluate(() => played.length),
|
||||
beforePoach,
|
||||
"poach edits remain local",
|
||||
);
|
||||
await page.locator(".pr-modal [data-confirm-poach]").click();
|
||||
assert.equal(await page.evaluate(() => played.length), beforePoach + 1);
|
||||
assert.deepEqual(await page.evaluate(() => played.at(-1)), {
|
||||
type: "poachAll",
|
||||
placements: [
|
||||
{ kind: "c", target: "b0" },
|
||||
{ kind: "w", target: "e1" },
|
||||
],
|
||||
});
|
||||
// Confirming zero is also an explicit early stop, not sequential pass/placement sends.
|
||||
poachPlacement.tasks[0].remaining = 2;
|
||||
poachPlacement.history.push({ p: 0, move: { type: "pass" } });
|
||||
await page.evaluate(
|
||||
(s) => host.emit("state", s),
|
||||
E.stripSecret(poachPlacement, 0),
|
||||
);
|
||||
await page.locator(".action-dock [data-actions]").click();
|
||||
await page.locator(".pr-modal [data-confirm-poach]").click();
|
||||
assert.deepEqual(await page.evaluate(() => played.at(-1)), {
|
||||
type: "poachAll",
|
||||
placements: [],
|
||||
});
|
||||
let undoPlacement = E.init(3, [], {}, "browser-placement-undo");
|
||||
undoPlacement = E.move(undoPlacement, { type: "role", id: "recruiter" }, 0);
|
||||
undoPlacement = E.move(undoPlacement, { type: "recruitBonus" }, 0);
|
||||
|
|
|
|||
70
test/poach-placement.test.js
Normal file
70
test/poach-placement.test.js
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import * as E from "../engine/index.js";
|
||||
function fixture(remaining = 3) {
|
||||
const s = E.init(3, ["smuggler", "citizens"], {}, "poach-draft");
|
||||
s.tasks = [
|
||||
{ kind: "poach", p: 0, remaining },
|
||||
{ kind: "role", p: 1 },
|
||||
];
|
||||
s.register = { w: 5, c: 3 };
|
||||
s.discardedPeople = { w: 0, c: 0 };
|
||||
s.players[0].estates = [
|
||||
{ id: "corn", w: 1, c: 0 },
|
||||
{ id: "fruit", w: 0, c: 0 },
|
||||
{ id: "forest", w: 0, c: 0 },
|
||||
];
|
||||
s.players[0].buildings = [{ id: "largeSugar", w: 0, c: 1 }];
|
||||
return s;
|
||||
}
|
||||
const w = (target) => ({ kind: "w", target });
|
||||
const c = (target) => ({ kind: "c", target });
|
||||
const move = (s, placements) => E.move(s, { type: "poachAll", placements }, 0);
|
||||
test("poach batch is atomic, keeps incumbents, and leaves mixed surplus discard separate", () => {
|
||||
const s = fixture();
|
||||
const n = move(s, [c("e1"), w("b0"), c("b0")]);
|
||||
assert.equal(n.history.length, s.history.length + 1);
|
||||
assert.deepEqual(n.register, { w: 4, c: 1 });
|
||||
assert.deepEqual(n.players[0].buildings[0], { id: "largeSugar", w: 1, c: 2 });
|
||||
assert.equal(n.players[0].estates[0].w, 1);
|
||||
assert.equal(n.tasks[0].kind, "poachDiscard");
|
||||
assert.deepEqual(n.discardedPeople, { w: 0, c: 0 });
|
||||
assert.equal(n.events.filter((e) => e.type === "poachWorker").length, 3);
|
||||
});
|
||||
test("poach may stop early or take none; legacy individual placement still works", () => {
|
||||
for (const placements of [[], [w("e1")]]) {
|
||||
const n = move(fixture(), placements);
|
||||
assert.equal(n.tasks[0].kind, "poachDiscard");
|
||||
}
|
||||
const s = E.move(fixture(), { type: "poachWorker", ...w("e1") }, 0);
|
||||
assert.equal(s.tasks[0].remaining, 2);
|
||||
assert.equal(move(s, [c("b0")]).tasks[0].kind, "poachDiscard");
|
||||
});
|
||||
test("poach rejects malformed, over-cap, unavailable, occupied and overflowing drafts without mutation", () => {
|
||||
const s = fixture();
|
||||
for (const placements of [
|
||||
null,
|
||||
{},
|
||||
[null],
|
||||
[{ kind: "x", target: "e1" }],
|
||||
[w("reserve")],
|
||||
[w("e99")],
|
||||
[w("e2")],
|
||||
[w("e0")],
|
||||
[w("e1"), c("e1")],
|
||||
[w("b0"), w("b0"), w("b0")],
|
||||
[w("e1"), w("b0"), w("b0"), w("b0")],
|
||||
]) {
|
||||
const before = structuredClone(s);
|
||||
assert.throws(() => move(s, placements));
|
||||
assert.deepEqual(s, before);
|
||||
}
|
||||
assert.throws(() => move(fixture(1), [w("e1"), w("b0")]));
|
||||
s.register = { w: 7, c: 1 };
|
||||
assert.throws(() => move(s, [c("e1"), c("b0")]));
|
||||
s.register = { w: 2, c: 2 };
|
||||
assert.throws(() => move(s, [w("e1"), c("b0")]));
|
||||
const n = move(s, [c("e1")]);
|
||||
assert.equal(n.tasks[0].kind, "role");
|
||||
assert.deepEqual(n.register, { w: 2, c: 1 });
|
||||
});
|
||||
56
viewer/ui.js
56
viewer/ui.js
|
|
@ -343,6 +343,10 @@ export function mountGame(
|
|||
}
|
||||
if (enabled && seat === task?.p && task?.kind === "bonusAssign")
|
||||
draft = { targets: [] };
|
||||
if (enabled && seat === task?.p && task?.kind === "poach") {
|
||||
draft = { placements: [] };
|
||||
tool = state.register.w ? "w" : "c";
|
||||
}
|
||||
if (enabled && seat === task?.p && task?.kind === "store")
|
||||
draft = autoStorage(state, seat);
|
||||
}
|
||||
|
|
@ -551,6 +555,8 @@ export function mountGame(
|
|||
.join("")}</div>`;
|
||||
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 === "poach")
|
||||
return `<div class="placement-editor"><div class="assignment-tools">${["w", "c"].map((k) => `<button data-tool="${k}" class="${tool === k ? "selected" : ""}" aria-pressed="${tool === k}" aria-label="${esc(t(k === "w" ? "workers" : "citizens"))}">${metric(k === "w" ? "worker" : "citizen", poachRemaining(k))}</button>`).join("")}<span>${draft.placements.length} / ${poachLimit()}</span></div>${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 - draft.targets.length)}</div>${playerAreas(state.players[seat])}</div><footer class="action-editor-footer">${confirmation()}</footer>`;
|
||||
if (task.kind === "store")
|
||||
|
|
@ -586,7 +592,27 @@ export function mountGame(
|
|||
)
|
||||
.join("")}</div>`;
|
||||
}
|
||||
function poachRemaining(kind) {
|
||||
return (
|
||||
state.register[kind] -
|
||||
draft.placements.filter((x) => x.kind === kind).length
|
||||
);
|
||||
}
|
||||
function poachLimit() {
|
||||
return Math.min(
|
||||
3,
|
||||
state.tasks[0].remaining,
|
||||
Math.max(0, state.register.w + state.register.c - state.players.length),
|
||||
);
|
||||
}
|
||||
function peopleSlots(key, x, n = 1) {
|
||||
const poach = draft && state.tasks[0]?.kind === "poach" && view === seat;
|
||||
const placements = poach
|
||||
? draft.placements
|
||||
.map((x, index) => ({ ...x, index }))
|
||||
.filter((x) => x.target === key)
|
||||
: [];
|
||||
|
||||
const bonus =
|
||||
draft && state.tasks[0]?.kind === "bonusAssign" && view === seat;
|
||||
const added = bonus
|
||||
|
|
@ -602,6 +628,10 @@ export function mountGame(
|
|||
? draft[key[0] === "e" ? "estates" : "buildings"][Number(key.slice(1))]
|
||||
: { ...x, w: x.w + added };
|
||||
return `<div class="worker-slots">${Array.from({ length: n }, (_, i) => {
|
||||
if (poach && i >= x.w + x.c) {
|
||||
const placed = placements[i - x.w - x.c];
|
||||
return `<button type="button" class="worker-slot ${placed?.kind ?? ""}" data-poach-slot="${key}" data-poach-index="${placed?.index ?? ""}" ${!allowed() || (!placed && (draft.placements.length >= poachLimit() || poachRemaining(tool) <= 0)) ? "disabled" : ""} aria-label="${esc(`${t(placed?.kind === "c" ? "citizens" : placed ? "workers" : "empty")} · ${t(x.id)}`)}">${placed ? icon(placed.kind === "c" ? "citizen" : "worker", 20) : "+"}</button>`;
|
||||
}
|
||||
const k = i < values.c ? "c" : i < values.c + values.w ? "w" : null;
|
||||
const unavailable =
|
||||
editable && !k && (tool === "erase" || remaining()[tool] === 0);
|
||||
|
|
@ -870,6 +900,8 @@ 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 === "poach")
|
||||
confirm = `<button class="primary" data-confirm-poach>${icon("check")} ${t("confirm")}</button>`;
|
||||
if (task.kind === "bonusAssign") {
|
||||
const a = state.players[seat];
|
||||
const spaces =
|
||||
|
|
@ -912,6 +944,7 @@ export function mountGame(
|
|||
"plant",
|
||||
"assign",
|
||||
"bonusAssign",
|
||||
"poach",
|
||||
"store",
|
||||
"achievementChoose",
|
||||
"achievementDraft",
|
||||
|
|
@ -924,6 +957,7 @@ export function mountGame(
|
|||
plant: "plantationChoices",
|
||||
assign: "editAssignment",
|
||||
bonusAssign: "editAssignment",
|
||||
poach: "editAssignment",
|
||||
store: "editStorage",
|
||||
achievementChoose: "achievements",
|
||||
achievementDraft: "achievements",
|
||||
|
|
@ -937,7 +971,7 @@ export function mountGame(
|
|||
: "";
|
||||
const confirm = confirmation();
|
||||
const passControl =
|
||||
pass && can
|
||||
pass && can && task.kind !== "poach"
|
||||
? moveButton(
|
||||
pass,
|
||||
t(task?.kind === "trade" && task.acted ? "finish" : "pass"),
|
||||
|
|
@ -990,7 +1024,7 @@ export function mountGame(
|
|||
}
|
||||
function openActions() {
|
||||
if (!allowed()) return;
|
||||
if (["assign", "bonusAssign"].includes(state.tasks[0].kind)) {
|
||||
if (["assign", "bonusAssign", "poach"].includes(state.tasks[0].kind)) {
|
||||
view = seat;
|
||||
render();
|
||||
}
|
||||
|
|
@ -1561,6 +1595,24 @@ export function mountGame(
|
|||
);
|
||||
return;
|
||||
}
|
||||
if (el.dataset.poachSlot) {
|
||||
if (el.dataset.poachIndex !== "")
|
||||
draft.placements.splice(Number(el.dataset.poachIndex), 1);
|
||||
else if (
|
||||
draft.placements.length < poachLimit() &&
|
||||
poachRemaining(tool) > 0
|
||||
)
|
||||
draft.placements.push({ kind: tool, target: el.dataset.poachSlot });
|
||||
render();
|
||||
return;
|
||||
}
|
||||
if (el.hasAttribute("data-confirm-poach")) {
|
||||
send({
|
||||
type: "poachAll",
|
||||
placements: draft.placements.map((x) => ({ ...x })),
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (el.dataset.bonusSlot) {
|
||||
const key = el.dataset.bonusSlot;
|
||||
if (el.dataset.bonusOccupied === "true")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue