Show current production and bonus effects on owned buildings

This commit is contained in:
coyotte508 2026-10-26 21:07:03 +01:00
commit 18e6422805
4 changed files with 267 additions and 1 deletions

View file

@ -0,0 +1,76 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { buildingEffects } from "../viewer/building-effects.js";
const goods = { corn: 0, fruit: 0, sugar: 0, tobacco: 0, coffee: 0 };
function fixture() {
const p = {
reserve: { w: 1, c: 2 },
vp: 5,
goods: { ...goods },
estates: [
...Array.from({ length: 4 }, () => ({ id: "fruit", w: 1, c: 0 })),
{ id: "sugar", w: 1, c: 0 },
],
buildings: [
{ id: "smallFruit", w: 1, c: 0 },
{ id: "largeFruit", w: 2, c: 0 },
{ id: "largeSugar", w: 1, c: 0 },
{ id: "canal", w: 1, c: 0 },
{ id: "townSquare", w: 0, c: 0 },
{ id: "hiddenMarket", w: 1, c: 0 },
],
};
return {
players: [p],
expansions: [],
options: {},
supply: { corn: 10, fruit: 10, sugar: 10, tobacco: 10, coffee: 10 },
};
}
test("production is allocated once across depots and Canal is separate", () => {
const s = fixture();
let b = buildingEffects(s, 0);
assert.equal(b[0].items[0].value, 1);
assert.equal(b[1].items[0].value, 2);
assert.deepEqual(
b[3].items.map((x) => [x.icon, x.value]),
[
["fruit", 1],
["sugar", 1],
],
);
s.supply.fruit = 2;
b = buildingEffects(s, 0);
assert.equal(b[0].items[0].value + b[1].items[0].value, 2);
assert.equal(b[3].items[0].value, 0);
});
test("unstaffed score bonus is dimmed, without changing printed points or citizen count", () => {
const s = fixture();
assert.deepEqual(buildingEffects(s, 0)[4], {
enabled: false,
items: [{ icon: "vp", value: 2, sign: "+" }],
});
s.players[0].buildings[4].w = 1;
assert.equal(buildingEffects(s, 0)[4].enabled, true);
assert.equal(buildingEffects(s, 0)[4].items[0].value, 2);
});
test("Hidden Market counts usable payment categories and protects its own worker", () => {
const s = fixture();
assert.equal(buildingEffects(s, 0)[5].items[0].value, 2);
s.players[0].goods.fruit = 1;
assert.equal(buildingEffects(s, 0)[5].items[0].value, 3);
s.players[0].reserve = { w: 0, c: 0 };
s.players[0].estates = [];
s.players[0].buildings = s.players[0].buildings.slice(5);
assert.equal(buildingEffects(s, 0)[0].items[0].value, 2);
s.players[0].vp = null;
assert.equal(buildingEffects(s, 0)[0].items[0].value, "?");
});
test("empty production building shows available potential but stays inactive", () => {
const s = fixture();
s.players[0].buildings[1].w = 0;
const b = buildingEffects(s, 0);
assert.equal(b[1].enabled, false);
assert.equal(b[1].items[0].value, 3);
assert.equal(b[3].items[0].value, 0);
});

135
viewer/building-effects.js Normal file
View file

@ -0,0 +1,135 @@
import { BUILDINGS as B, GOODS } from "../engine/catalog.js";
import { production, scoreBreakdown } from "../engine/index.js";
const total = (x) => (x.w ?? 0) + (x.c ?? 0);
const sum = (xs) => xs.reduce((a, b) => a + b, 0);
const scoring = new Set([
"fireStation",
"residence",
"fortress",
"customsHouse",
"cityHall",
"cathedral",
"townSquare",
]);
// These are current capacities/bonuses, not rewards already earned.
export function buildingEffects(state, seat, player = state.players[seat]) {
if (player.puertoma) return [];
const farms = Object.fromEntries(
GOODS.map((g) => [
g,
sum(player.estates.filter((e) => e.id === g).map(total)),
]),
);
const remaining = Object.fromEntries(
GOODS.map((g) => [g, Math.min(farms[g], state.supply[g])]),
);
const citizens =
(player.reserve.c ?? 0) +
sum([...player.estates, ...player.buildings].map((x) => x.c ?? 0));
const capacity = production(player);
const available = Object.fromEntries(
GOODS.map((g) => [g, Math.min(capacity[g], state.supply[g])]),
);
const allocated = new Map();
player.buildings.forEach((b, i) => {
const g = B[b.id].good;
if (GOODS.includes(g) && total(b)) {
const n = Math.min(remaining[g], total(b));
allocated.set(i, n);
remaining[g] -= n;
}
});
return player.buildings.map((b, i) => {
const enabled = total(b) > 0;
const items = [];
const add = (icon, value, sign = "+") => items.push({ icon, value, sign });
const g = B[b.id].good;
if (GOODS.includes(g))
add(
g,
enabled ? allocated.get(i) : Math.min(remaining[g], B[b.id].workers),
);
else if (scoring.has(b.id)) {
if (b.id === "customsHouse" && player.vp == null) add("vp", "?");
else {
const preview = {
...player,
buildings: player.buildings.map((x, j) =>
j === i ? { ...x, w: enabled ? x.w : 1 } : x,
),
};
const s = {
...state,
players: state.players.map((p, j) => (j === seat ? preview : p)),
};
let n = scoreBreakdown(s, seat).bonus[b.id] ?? 0;
if (b.id === "fortress")
n = Math.floor(
(total(player.reserve) +
sum([...player.estates, ...player.buildings].map(total))) /
3,
);
add("vp", n);
}
} else if (b.id === "canal") {
for (const [good, id] of [
["fruit", "largeFruit"],
["sugar", "largeSugar"],
]) {
const large = player.buildings.find((x) => x.id === id);
if (!large) continue;
const base = Math.min(
farms[good],
sum(player.buildings.filter((x) => B[x.id].good === good).map(total)),
);
add(
good,
total(large) > 0 && base > 0 && state.supply[good] > base ? 1 : 0,
);
}
} else if (b.id === "hiddenMarket") {
const worker =
total(player.reserve) +
sum(player.estates.map(total)) +
sum(player.buildings.filter((x) => x !== b).map(total)) >
0;
const goods = GOODS.some((g) => player.goods[g] > 0);
add(
"coin",
player.vp == null
? "?"
: Number(worker) + Number(goods) + Number(player.vp > 0),
"−",
);
} else if (b.id === "smallMarket") add("coin", 1);
else if (b.id === "largeMarket") add("coin", 2);
else if (b.id === "harbor") add("vp", 1);
else if (b.id === "chapel") add(b.c ? "vp" : "coin", 1);
else if (b.id === "tailorShop")
add("coin", Math.min(citizens, state.options.tailorLimit ? 3 : Infinity));
else if (b.id === "factory")
add(
"coin",
[0, 0, 1, 2, 3, 5][GOODS.filter((g) => available[g] > 0).length],
);
else if (b.id === "distillery")
add(
"coin",
Math.max(
0,
...GOODS.filter((g) => g !== "corn").map((g) => available[g] - 1),
),
);
else if (b.id === "lumberyard")
add(
"coin",
Math.floor(player.estates.filter((e) => e.id === "forest").length / 2),
"−",
);
else if (b.id === "notary") add("coin", b.c ? 2 : 1, "−");
else if (b.id === "assemblyHall")
add("vp", sum(GOODS.map((g) => Math.floor(player.goods[g] / 2))));
return { enabled, items };
});
}

View file

@ -2831,3 +2831,35 @@ body:has(.pr-game) {
.pr-game .action-bonus { display: inline-flex; align-items: center; gap: 3px; margin-inline-start: 6px; white-space: nowrap; font-size: .85em; }
.pr-game .action-bonus .icon, .pr-game .action-bonus .score-token { width: 20px; height: 20px; }
.pr-game .building-face .owned-building-name {
flex: 1;
min-width: 0;
}
.pr-game .current-building-effect {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 2px 5px;
margin-left: auto;
max-width: 45%;
font-size: 12px;
}
.pr-game .current-building-effect > span {
display: inline-flex;
align-items: center;
white-space: nowrap;
gap: 1px;
}
.pr-game .current-building-effect .metric {
margin: 0;
gap: 2px;
}
.pr-game .current-building-effect .icon {
width: 18px;
height: 18px;
}
.pr-game .current-building-effect.inactive {
opacity: 0.4;
filter: grayscale(1);
}

View file

@ -1,3 +1,4 @@
import { buildingEffects } from "./building-effects.js";
import { actionBonuses, actionVictoryPoints } from "./action-bonuses.js";
import { recruitmentShares, recruitmentBonus } from "./recruitment.js";
import { showBuildingPayment } from "./building-payment.js";
@ -601,7 +602,29 @@ export function mountGame(
);
}
function playerAreas(a) {
return `<h3>${t("countryside")} <small>${a.estates.filter((x) => (!x.vpArea && x.id !== "quarry") || !a.puertoma).length}/${a.puertoma ? 10 : 12}</small></h3><div class="estates">${a.estates.map((x, i) => `<article class="estate ${x.id}" title="${esc(t(x.id))}">${icon(GOODS.includes(x.id) ? `field-${x.id}` : x.id, 36)}<span class="estate-name">${t(x.id)}</span>${x.id !== "forest" ? peopleSlots(`e${i}`, x, a.puertoma ? (x.vpArea ? 0 : estateCapacity(x.id)) : 1) : ""}</article>`).join("")}${Array.from({ length: Math.max(0, (a.puertoma ? 10 : 12) - a.estates.length) }, () => '<span class="estate empty-estate"></span>').join("")}</div><h3>${t("city")} <small>${a.puertoma ? a.buildings.length : citySize(a)}/${a.puertoma ? 8 : 12}</small></h3><div class="city-grid">${a.buildings.map((x, i) => `<article class="owned-building ${B[x.id].size === 2 ? "expanded" : ""}"><button type="button" data-building="${x.id}" class="building-face" title="${esc(x.id === "monument" ? t(x.id) : t.effect(x.id))}">${buildingPictogram(x.id, 28)}<span>${t(x.id)}</span></button><div class="owned-building-bottom">${a.puertoma ? `<small>${t("level")} ${x.level}</small>` : peopleSlots(`b${i}`, x, B[x.id].workers)}${pointsBadge(B[x.id].vp)}</div></article>`).join("")}${a.buildings.length ? "" : `<div class="empty-city">${town}<span>${t("emptyCity")}</span></div>`}</div>`;
const preview =
draft &&
state.tasks[0]?.kind === "assign" &&
view === seat &&
state.tasks[0]?.p === seat
? {
...a,
reserve: remaining(),
estates: a.estates.map((x, i) => ({ ...x, ...draft.estates[i] })),
buildings: a.buildings.map((x, i) => ({
...x,
...draft.buildings[i],
})),
}
: a;
const effects = buildingEffects(state, view, preview);
const effectBadge = (i) => {
const effect = effects[i];
if (!effect?.items.length) return "";
const label = `${t.effect(a.buildings[i].id)}${effect.enabled ? "" : ` · ${t("workerSpaces")}: 0/${B[a.buildings[i].id].workers}`}`;
return `<span class="current-building-effect ${effect.enabled ? "" : "inactive"}" title="${esc(label)}" aria-label="${esc(label)}">${effect.items.map((x) => `<span>${x.sign}${metric(x.icon, x.value)}</span>`).join("")}</span>`;
};
return `<h3>${t("countryside")} <small>${a.estates.filter((x) => (!x.vpArea && x.id !== "quarry") || !a.puertoma).length}/${a.puertoma ? 10 : 12}</small></h3><div class="estates">${a.estates.map((x, i) => `<article class="estate ${x.id}" title="${esc(t(x.id))}">${icon(GOODS.includes(x.id) ? `field-${x.id}` : x.id, 36)}<span class="estate-name">${t(x.id)}</span>${x.id !== "forest" ? peopleSlots(`e${i}`, x, a.puertoma ? (x.vpArea ? 0 : estateCapacity(x.id)) : 1) : ""}</article>`).join("")}${Array.from({ length: Math.max(0, (a.puertoma ? 10 : 12) - a.estates.length) }, () => '<span class="estate empty-estate"></span>').join("")}</div><h3>${t("city")} <small>${a.puertoma ? a.buildings.length : citySize(a)}/${a.puertoma ? 8 : 12}</small></h3><div class="city-grid">${a.buildings.map((x, i) => `<article class="owned-building ${B[x.id].size === 2 ? "expanded" : ""}"><button type="button" data-building="${x.id}" class="building-face" title="${esc(x.id === "monument" ? t(x.id) : t.effect(x.id))}">${buildingPictogram(x.id, 28)}<span class="owned-building-name">${t(x.id)}</span>${effectBadge(i)}</button><div class="owned-building-bottom">${a.puertoma ? `<small>${t("level")} ${x.level}</small>` : peopleSlots(`b${i}`, x, B[x.id].workers)}${pointsBadge(B[x.id].vp)}</div></article>`).join("")}${a.buildings.length ? "" : `<div class="empty-city">${town}<span>${t("emptyCity")}</span></div>`}</div>`;
}
function playerBoard() {
const a = state.players[view];