Require quarry discounts and simplify building payment choices
This commit is contained in:
parent
940c4edf07
commit
7ace1689fd
6 changed files with 102 additions and 17 deletions
|
|
@ -359,7 +359,7 @@ export function cost(s, p, id, move = {}) {
|
|||
: 0;
|
||||
return move.pay ?? Math.max(0, base - discount);
|
||||
}
|
||||
function buildingPayments(s, p, id) {
|
||||
function buildingPayments(s, p, id, legacyQuarries = false) {
|
||||
const a = s.players[p],
|
||||
b = B[id];
|
||||
const base =
|
||||
|
|
@ -390,7 +390,9 @@ function buildingPayments(s, p, id) {
|
|||
? 1
|
||||
: 0
|
||||
: 0;
|
||||
let reductions = Array.from({ length: quarries + 1 }, (_, i) => i);
|
||||
let reductions = legacyQuarries
|
||||
? Array.from({ length: quarries + 1 }, (_, i) => i)
|
||||
: [quarries];
|
||||
for (const bonus of [privilege, lumber, notary])
|
||||
if (bonus)
|
||||
reductions = [...new Set(reductions.flatMap((n) => [n, n + bonus]))];
|
||||
|
|
@ -414,7 +416,7 @@ function workerSources(a) {
|
|||
.map((kind) => ({ key, kind })),
|
||||
);
|
||||
}
|
||||
export function buildOptions(s, p) {
|
||||
export function buildOptions(s, p, legacyQuarries = false) {
|
||||
if (s.players[p].puertoma) return P.buildings(s, p);
|
||||
const a = s.players[p],
|
||||
out = [];
|
||||
|
|
@ -435,9 +437,9 @@ export function buildOptions(s, p) {
|
|||
const short = cost(s, p, id) - a.coins;
|
||||
if (short <= 0) {
|
||||
out.push({ type: "build", id });
|
||||
// Quarry reductions and role Advantages may be declined (rulebook pp. 9, 12).
|
||||
// Role Advantages and building functions may be declined; quarries always apply.
|
||||
// Explicit higher Coin payments let Big Spender use this legal choice.
|
||||
for (const pay of buildingPayments(s, p, id))
|
||||
for (const pay of buildingPayments(s, p, id, legacyQuarries))
|
||||
if (pay > cost(s, p, id) && pay <= a.coins)
|
||||
out.push({ type: "build", id, pay });
|
||||
continue;
|
||||
|
|
@ -1410,7 +1412,7 @@ export function resolveForcedActions(state) {
|
|||
}
|
||||
throw Error("Forced action loop");
|
||||
}
|
||||
function applyMove(state, m, p) {
|
||||
function applyMove(state, m, p, historical = false) {
|
||||
assert(m && typeof m === "object" && !Array.isArray(m));
|
||||
assert(JSON.stringify(m).length <= 8192, "Move payload is too large");
|
||||
assert(integer(p) && !!state.players[p], "Invalid player");
|
||||
|
|
@ -1448,7 +1450,12 @@ function applyMove(state, m, p) {
|
|||
new Set(m.decline).size === m.decline.length &&
|
||||
m.decline.every((id) => optionalPoints(s, p, m).includes(id)),
|
||||
);
|
||||
} else assert(legal(s, p).some((x) => sameMove(x, m)));
|
||||
} else assert(
|
||||
legal(s, p).some((x) => sameMove(x, m)) ||
|
||||
// Preserve already-recorded payments from before mandatory quarry discounts.
|
||||
(historical && t.kind === "build" &&
|
||||
buildOptions(s, p, true).some((x) => sameMove(x, m))),
|
||||
);
|
||||
if (m.target)
|
||||
extra.targetId =
|
||||
m.target === "reserve" ? "reserve" : tileRef(a, m.target).id;
|
||||
|
|
@ -1988,7 +1995,7 @@ export function replay(s, options = {}) {
|
|||
assert(integer(to) && to >= 1 && to <= logLength(s));
|
||||
const c = s.config;
|
||||
let r = init(c.n, c.expansions, c.options, c.seed);
|
||||
for (const h of s.history.slice(0, to - 1)) r = applyMove(r, h.move, h.p);
|
||||
for (const h of s.history.slice(0, to - 1)) r = applyMove(r, h.move, h.p, true);
|
||||
r.players.forEach((p, i) => {
|
||||
p.name = s.players[i].name;
|
||||
p.bot = s.players[i].bot;
|
||||
|
|
@ -2007,7 +2014,7 @@ export function logSlice(
|
|||
for (let i = Math.max(0, start); i <= Math.min(end, logLength(s) - 1); i++) {
|
||||
frames.push(stripSecret(position, player));
|
||||
const h = s.history[i];
|
||||
if (h && i < end) position = applyMove(position, h.move, h.p);
|
||||
if (h && i < end) position = applyMove(position, h.move, h.p, true);
|
||||
}
|
||||
return { frames };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/puerto-rico",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.3",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Puerto Rico 1897 Special Edition for Boardgamers Space",
|
||||
|
|
|
|||
|
|
@ -186,9 +186,9 @@ test("completing a card reveals it and scores only at game end without VP tokens
|
|||
"masterCollector",
|
||||
);
|
||||
});
|
||||
test("Big Spender can decline discounts, while arbitrary overpayment is rejected", () => {
|
||||
test("Big Spender can decline the privilege but must retain quarry discounts", () => {
|
||||
let s = fresh();
|
||||
s.players[0].coins = 10;
|
||||
s.players[0].coins = 6;
|
||||
s.players[0].estates = Array.from({ length: 4 }, () => tile("quarry", 1));
|
||||
s.players[0].achievements = [
|
||||
{ id: "bigSpender", completed: false },
|
||||
|
|
@ -196,14 +196,16 @@ test("Big Spender can decline discounts, while arbitrary overpayment is rejected
|
|||
];
|
||||
s = E.move(s, { type: "role", id: "builder" }, 0);
|
||||
assert.equal(E.cost(s, 0, "fireStation"), 5);
|
||||
assert(E.legal(s, 0).some((m) => m.id === "fireStation" && m.pay === 10));
|
||||
assert(E.legal(s, 0).some((m) => m.id === "fireStation" && m.pay === 6));
|
||||
s.players[0].coins = 10;
|
||||
assert.throws(() =>
|
||||
E.move(s, { type: "build", id: "fireStation", pay: 11 }, 0),
|
||||
E.move(s, { type: "build", id: "fireStation", pay: 10 }, 0),
|
||||
);
|
||||
const r = E.move(s, { type: "build", id: "fireStation", pay: 10 }, 0);
|
||||
s.players[0].coins = 6;
|
||||
const r = E.move(s, { type: "build", id: "fireStation", pay: 6 }, 0);
|
||||
assert.equal(r.players[0].coins, 0);
|
||||
assert.equal(r.players[0].achievements[0].completed, true);
|
||||
assert.equal(r.players[0].achievements[1].completed, false);
|
||||
assert.equal(r.players[0].achievements[1].completed, true);
|
||||
});
|
||||
test("Puertoma scores every humans incomplete cards, never the completed cards", () => {
|
||||
const s = E.init(
|
||||
|
|
|
|||
61
test/building-payment.test.js
Normal file
61
test/building-payment.test.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { showBuildingPayment } from "../viewer/building-payment.js";
|
||||
|
||||
test("higher prices appear only when they complete an unfinished Big Spender", () => {
|
||||
const player = {
|
||||
coins: 10,
|
||||
achievements: [{ id: "bigSpender", completed: false }],
|
||||
};
|
||||
const move = { type: "build", id: "fireStation", pay: 10 };
|
||||
assert.equal(showBuildingPayment(player, move), true);
|
||||
assert.equal(showBuildingPayment(player, { ...move, pay: 9 }), false);
|
||||
assert.equal(
|
||||
showBuildingPayment(player, { ...move, id: "smallFruit" }),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
showBuildingPayment({ ...player, achievements: [] }, move),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
showBuildingPayment(
|
||||
{ ...player, achievements: [{ id: "bigSpender", completed: true }] },
|
||||
move,
|
||||
),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
showBuildingPayment(player, { type: "build", id: "fireStation" }),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("historical quarry overpayments remain replayable but cannot be submitted now", async () => {
|
||||
const E = await import("../engine/index.js");
|
||||
let s = E.init(3, [], {}, "historic-payment");
|
||||
for (let n = 0; n < 200 && !s.finished; n++) {
|
||||
const p = E.currentPlayer(s);
|
||||
if (s.tasks[0].kind === "build") {
|
||||
const legal = E.legal(s, p);
|
||||
const old = E.buildOptions(s, p, true).find(
|
||||
(m) =>
|
||||
m.pay && !legal.some((x) => JSON.stringify(m) === JSON.stringify(x)),
|
||||
);
|
||||
if (old) {
|
||||
assert.throws(() => E.move(s, old, p));
|
||||
const saved = { ...s, history: [...s.history, { p, move: old }] };
|
||||
const replayed = E.replay(saved);
|
||||
assert.equal(replayed.players[p].coins, s.players[p].coins - old.pay);
|
||||
assert.ok(replayed.players[p].buildings.some((b) => b.id === old.id));
|
||||
assert.equal(
|
||||
E.logSlice(saved, { player: p }).frames.length,
|
||||
E.logLength(saved),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
s = E.moveAI(s, p);
|
||||
}
|
||||
assert.fail("Expected a historical quarry payment fixture");
|
||||
});
|
||||
12
viewer/building-payment.js
Normal file
12
viewer/building-payment.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { BUILDINGS } from "../engine/catalog.js";
|
||||
|
||||
// Keep the minimum payment visible. Extra payments matter only for Big Spender.
|
||||
export function showBuildingPayment(player, move) {
|
||||
return (
|
||||
move.type !== "build" ||
|
||||
move.pay === undefined ||
|
||||
(BUILDINGS[move.id]?.size === 2 &&
|
||||
move.pay === player?.coins &&
|
||||
player.achievements?.some((c) => c.id === "bigSpender" && !c.completed))
|
||||
);
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
import { showBuildingPayment } from "./building-payment.js";
|
||||
import { FORCE_DARK_THEME } from "./theme.js";
|
||||
import { mergePreferences, imageUrl } from "./platform.js";
|
||||
import { achievementProgress } from "./achievement-progress.js";
|
||||
|
|
@ -427,6 +428,7 @@ export function mountGame(
|
|||
return state.legal.filter(
|
||||
(m) =>
|
||||
m.type !== "bohio" &&
|
||||
showBuildingPayment(state.players[seat], m) &&
|
||||
(m.type !== "pass" || m.decline?.length) &&
|
||||
// Zero production has the same bonus choices as passing.
|
||||
!(m.type === "produce" && !GOODS.some((g) => m.goods[g])),
|
||||
|
|
@ -980,7 +982,8 @@ export function mountGame(
|
|||
: null;
|
||||
const b = B[id],
|
||||
moves = state.legal.filter(
|
||||
(m) => ["build", "draft"].includes(m.type) && m.id === id,
|
||||
(m) => ["build", "draft"].includes(m.type) && m.id === id &&
|
||||
showBuildingPayment(state.players[seat], m),
|
||||
);
|
||||
show(
|
||||
t(id),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue