Allow conflict-free Craftsman production and bonuses concurrently
This commit is contained in:
parent
09f6070e36
commit
6493461574
4 changed files with 235 additions and 13 deletions
|
|
@ -928,7 +928,7 @@ function optionalPoints(s, p, m) {
|
|||
out.push("publishingHouse");
|
||||
}
|
||||
if (
|
||||
s.tasks[0]?.kind === "produce" &&
|
||||
playerTask(s, p)?.kind === "produce" &&
|
||||
["produce", "pass"].includes(m.type) &&
|
||||
citizen(a, "chapel")
|
||||
)
|
||||
|
|
@ -952,17 +952,65 @@ function assignmentForced(a) {
|
|||
!(w && c)
|
||||
);
|
||||
}
|
||||
// Production commutes only when every remaining maximum (including both
|
||||
// Publishing House bonuses) fits the supply. Festival rewards can add workers
|
||||
// and change later production, so keep any potentially completing phase ordered.
|
||||
function parallelProductionTasks(s) {
|
||||
if (
|
||||
!s.production ||
|
||||
s.puertoma ||
|
||||
!["produce", "produceBonus"].includes(s.tasks[0]?.kind)
|
||||
)
|
||||
return [];
|
||||
const end = s.tasks.findIndex(
|
||||
(t) => !["produce", "produceBonus"].includes(t.kind),
|
||||
);
|
||||
const tasks = s.tasks.slice(0, end < 0 ? undefined : end);
|
||||
const demand = counts();
|
||||
const possible = s.production.map((out) => ({ ...out }));
|
||||
for (const t of tasks.filter((t) => t.kind === "produce")) {
|
||||
const output = production(s.players[t.p]);
|
||||
for (const g of GOODS) {
|
||||
demand[g] += output[g];
|
||||
possible[t.p][g] += output[g];
|
||||
}
|
||||
}
|
||||
for (const t of tasks.filter((t) => t.kind === "produceBonus"))
|
||||
for (const g of GOODS)
|
||||
if (possible[t.p][g] > 0) {
|
||||
demand[g] += t.remaining;
|
||||
possible[t.p][g] += t.remaining;
|
||||
}
|
||||
if (GOODS.some((g) => demand[g] > s.supply[g])) return [];
|
||||
const preview = { ...s, production: possible };
|
||||
if (
|
||||
tasks.some(
|
||||
(t) => completedFestivals(preview, t.p, { type: "produce" }).length,
|
||||
)
|
||||
)
|
||||
return [];
|
||||
return tasks.filter(
|
||||
(t, i) =>
|
||||
tasks.findIndex((other) => other.p === t.p) === i &&
|
||||
(t.kind !== "produceBonus" ||
|
||||
GOODS.some((g) => s.production[t.p][g] > 0 && s.supply[g] > 0)),
|
||||
);
|
||||
}
|
||||
function playerTask(s, p) {
|
||||
return (
|
||||
(!s.placementOrdered && placementTasks(s).find((t) => t.p === p)) ||
|
||||
parallelProductionTasks(s).find((t) => t.p === p) ||
|
||||
s.tasks[0]
|
||||
);
|
||||
}
|
||||
export function currentPlayers(s) {
|
||||
if (s.finished) return [];
|
||||
const placements = !s.placementOrdered && placementTasks(s);
|
||||
return placements?.length
|
||||
? placements.map((t) => t.p)
|
||||
const placements = (!s.placementOrdered && placementTasks(s)) || [];
|
||||
const concurrent = placements.length
|
||||
? placements
|
||||
: parallelProductionTasks(s);
|
||||
return concurrent.length
|
||||
? concurrent.map((t) => t.p)
|
||||
: [s.tasks[0]?.p].filter(Number.isInteger);
|
||||
}
|
||||
export function legal(s, p = currentPlayer(s)) {
|
||||
|
|
@ -1361,8 +1409,9 @@ export function canUndo(s, p) {
|
|||
(old) => old.id === f.id && old.claimed !== undefined,
|
||||
),
|
||||
) &&
|
||||
currentPlayers(before).length === 1 &&
|
||||
currentPlayer(before) === p &&
|
||||
(last.move.type === "assign"
|
||||
? currentPlayers(before).length === 1 && currentPlayer(before) === p
|
||||
: currentPlayers(before).includes(p)) &&
|
||||
JSON.stringify(before.bag) === JSON.stringify(s.bag) &&
|
||||
JSON.stringify(before.random) === JSON.stringify(s.random)
|
||||
);
|
||||
|
|
@ -2036,12 +2085,8 @@ export function stripSecret(state, p) {
|
|||
s.canUndo = Number.isInteger(p) && canUndo(state, p);
|
||||
s.currentPlayers = currentPlayers(state);
|
||||
const ownTask = Number.isInteger(p) ? playerTask(state, p) : null;
|
||||
if (
|
||||
ownTask?.kind === "assign" &&
|
||||
ownTask.p === p &&
|
||||
ownTask !== state.tasks[0]
|
||||
) {
|
||||
const index = s.tasks.findIndex((t) => t.kind === "assign" && t.p === p);
|
||||
if (ownTask && ownTask.p === p && ownTask !== state.tasks[0]) {
|
||||
const index = state.tasks.indexOf(ownTask);
|
||||
s.tasks.unshift(...s.tasks.splice(index, 1));
|
||||
}
|
||||
s.legal = Number.isInteger(p) ? legal(state, p) : [];
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/puerto-rico",
|
||||
"version": "0.3.8",
|
||||
"version": "0.3.9",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Puerto Rico 1897 Special Edition for Boardgamers Space",
|
||||
|
|
|
|||
|
|
@ -1167,6 +1167,42 @@ try {
|
|||
await page.locator(".placement-editor [data-slot]").count(),
|
||||
"withdrawn placement can be edited",
|
||||
);
|
||||
await page.keyboard.press("Escape");
|
||||
const earlyCraftsman = structuredClone(original);
|
||||
earlyCraftsman.role = "craftsman";
|
||||
earlyCraftsman.owner = 0;
|
||||
earlyCraftsman.festivals = [];
|
||||
earlyCraftsman.tasks = [
|
||||
{ kind: "produce", p: 1 },
|
||||
{ kind: "produce", p: 2 },
|
||||
{ kind: "produceBonus", p: 0, remaining: 1 },
|
||||
{ kind: "phaseEnd" },
|
||||
];
|
||||
earlyCraftsman.production = earlyCraftsman.players.map(() => ({
|
||||
corn: 0,
|
||||
fruit: 0,
|
||||
sugar: 0,
|
||||
tobacco: 0,
|
||||
coffee: 0,
|
||||
}));
|
||||
earlyCraftsman.production[0].corn = 1;
|
||||
earlyCraftsman.supply.corn = 10;
|
||||
earlyCraftsman.players.forEach((p) => {
|
||||
p.estates = [{ id: "corn", w: 1, c: 0 }];
|
||||
p.buildings = [];
|
||||
});
|
||||
await page.evaluate(
|
||||
(s) => {
|
||||
host.emit("player", { index: 0 });
|
||||
host.emit("state", s);
|
||||
},
|
||||
E.stripSecret(earlyCraftsman, 0),
|
||||
);
|
||||
await page.locator('.action-dock [data-move*="produceBonus"]').click();
|
||||
assert.deepEqual(await page.evaluate(() => played.at(-1)), {
|
||||
type: "produceBonus",
|
||||
good: "corn",
|
||||
});
|
||||
assert.deepEqual(errors, [], `No browser errors at ${width}`);
|
||||
console.log(
|
||||
`Browser ${width}px: actions, allocation, layout, journal, chat and scrolling OK`,
|
||||
|
|
|
|||
141
test/parallel-production.test.js
Normal file
141
test/parallel-production.test.js
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
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";
|
||||
function fixture() {
|
||||
const s = E.init(3, [], {}, "parallel-output");
|
||||
s.role = "craftsman";
|
||||
s.owner = 0;
|
||||
s.tasks = [0, 1, 2].map((p) => ({ kind: "produce", p }));
|
||||
s.tasks.push(
|
||||
{ kind: "produceBonus", p: 0, remaining: 1 },
|
||||
{ kind: "phaseEnd" },
|
||||
);
|
||||
s.production = s.players.map(() => ({
|
||||
corn: 0,
|
||||
fruit: 0,
|
||||
sugar: 0,
|
||||
tobacco: 0,
|
||||
coffee: 0,
|
||||
}));
|
||||
s.players.forEach((p) => {
|
||||
p.estates = [{ id: "corn", w: 1, c: 0 }];
|
||||
p.buildings = [];
|
||||
});
|
||||
s.festivals = [];
|
||||
return s;
|
||||
}
|
||||
const produce = (s, p) =>
|
||||
E.move(
|
||||
s,
|
||||
E.legal(s, p).find((m) => m.type === "produce"),
|
||||
p,
|
||||
);
|
||||
test("production and the chooser's bonus can run concurrently without changing outcomes", () => {
|
||||
const start = fixture();
|
||||
assert.deepEqual(E.currentPlayers(start), [0, 1, 2]);
|
||||
assert.deepEqual(host.currentPlayer(start), [0, 1, 2]);
|
||||
let early = produce(start, 0);
|
||||
assert.deepEqual(E.currentPlayers(early), [1, 2, 0]);
|
||||
assert.equal(E.stripSecret(early, 0).tasks[0].kind, "produceBonus");
|
||||
early = E.move(early, { type: "produceBonus", good: "corn" }, 0);
|
||||
early = produce(early, 2);
|
||||
early = produce(early, 1);
|
||||
let ordered = produce(start, 0);
|
||||
ordered = produce(ordered, 1);
|
||||
ordered = produce(ordered, 2);
|
||||
ordered = E.move(ordered, { type: "produceBonus", good: "corn" }, 0);
|
||||
assert.deepEqual(early.players, ordered.players);
|
||||
assert.deepEqual(early.supply, ordered.supply);
|
||||
assert.deepEqual(early.tasks, ordered.tasks);
|
||||
});
|
||||
test("scarce goods keep production and bonuses in order, including two bonus goods", () => {
|
||||
let s = fixture();
|
||||
s.supply.corn = 3;
|
||||
assert.deepEqual(E.currentPlayers(s), [0]);
|
||||
s = produce(s, 0);
|
||||
assert.deepEqual(E.currentPlayers(s), [1]);
|
||||
assert.throws(() => E.move(s, { type: "produceBonus", good: "corn" }, 0));
|
||||
s = fixture();
|
||||
s.tasks[3].remaining = 2;
|
||||
s.supply.corn = 4;
|
||||
assert.deepEqual(E.currentPlayers(s), [0]);
|
||||
s.supply.corn = 5;
|
||||
assert.deepEqual(E.currentPlayers(s), [0, 1, 2]);
|
||||
assert.equal(
|
||||
E.legal(s, 0).some((m) => m.type === "produceBonus"),
|
||||
false,
|
||||
);
|
||||
});
|
||||
test("potential Festival completion keeps the entire production phase ordered", () => {
|
||||
const s = fixture();
|
||||
s.festivals = [
|
||||
{
|
||||
id: "crop",
|
||||
goal: "produce",
|
||||
amount: 2,
|
||||
targets: { goods: ["corn"], estates: [] },
|
||||
reward: { workers: 1 },
|
||||
},
|
||||
];
|
||||
assert.deepEqual(
|
||||
E.currentPlayers(s),
|
||||
[0],
|
||||
"chooser's bonus could complete it",
|
||||
);
|
||||
s.festivals[0].targets.goods = ["coffee"];
|
||||
assert.deepEqual(
|
||||
E.currentPlayers(s),
|
||||
[0, 1, 2],
|
||||
"unreachable objectives do not block parallelism",
|
||||
);
|
||||
s.festivals[0].targets.goods = ["corn"];
|
||||
s.festivals[0].amount = 1;
|
||||
assert.deepEqual(
|
||||
E.currentPlayers(s),
|
||||
[0],
|
||||
"competing producers must resolve in order",
|
||||
);
|
||||
s.festivals[0].claimed = 2;
|
||||
assert.deepEqual(E.currentPlayers(s), [0, 1, 2]);
|
||||
});
|
||||
test("a pending Festival bonus worker placement is a barrier", () => {
|
||||
const s = fixture();
|
||||
s.tasks.unshift({ kind: "bonusAssign", p: 0, remaining: 1 });
|
||||
assert.deepEqual(E.currentPlayers(s), [0]);
|
||||
assert.equal(
|
||||
E.legal(s, 1).some((m) => m.type === "produce"),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("out-of-order Craftsman actions replay from an ordinary game history", () => {
|
||||
let s = E.init(3, [], {}, "replay-parallel-craftsman");
|
||||
s = E.move(s, { type: "role", id: "recruiter" }, 0);
|
||||
s = E.move(s, { type: "recruitBonus" }, 0);
|
||||
while (s.tasks[0].kind === "assign") {
|
||||
const p = E.currentPlayer(s);
|
||||
s = E.move(s, { type: "assign", ...E.autoAssignment(s, p) }, p);
|
||||
}
|
||||
const owner = E.currentPlayer(s);
|
||||
s = E.move(s, { type: "role", id: "craftsman" }, owner);
|
||||
for (let i = 0; i < 10 && s.tasks[0].kind !== "role"; i++) {
|
||||
const p = E.currentPlayers(s).at(-1);
|
||||
s = E.move(s, E.legal(s, p)[0], p);
|
||||
}
|
||||
assert.equal(s.tasks[0].kind, "role");
|
||||
assert.deepEqual(E.replay(s), s);
|
||||
});
|
||||
|
||||
test("early bonus choices do not repeat normal-production Chapel effects", () => {
|
||||
let s = fixture();
|
||||
s.players[0].buildings = [{ id: "chapel", c: 1, w: 0 }];
|
||||
s = produce(s, 0);
|
||||
assert.equal(
|
||||
E.legal(s, 0).some((m) => m.decline?.includes("chapel")),
|
||||
false,
|
||||
);
|
||||
const points = s.players[0].vp;
|
||||
s = E.move(s, { type: "produceBonus", good: "corn" }, 0);
|
||||
assert.equal(s.players[0].vp, points);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue