Unify role cards and show recruitment previews and inline warnings
This commit is contained in:
parent
7ace1689fd
commit
bfbcf25275
5 changed files with 110 additions and 40 deletions
|
|
@ -366,6 +366,18 @@ try {
|
|||
() => document.documentElement.scrollWidth > innerWidth + 1,
|
||||
);
|
||||
assert.equal(await overflow(), false, `No page overflow at ${width}`);
|
||||
await page.locator(".action-dock [data-actions]").click();
|
||||
assert.ok(await page.locator(".role-picker .role").count() > 0);
|
||||
assert.ok(await page.locator(".role-choice-warning").count() > 0);
|
||||
assert.equal(await page.locator(".recruitment-shares > span").count(), original.players.length);
|
||||
assert.equal(
|
||||
await page.locator('.role-picker [data-role-id="recruiter"]').innerHTML(),
|
||||
await page.locator('.shared-board [data-role-id="recruiter"]').innerHTML(),
|
||||
"role picker reuses board role cards",
|
||||
);
|
||||
assert.equal(await overflow(), false);
|
||||
await page.screenshot({ path: `.local/qa/${width}-role-picker.png` });
|
||||
await page.keyboard.press("Escape");
|
||||
const craftsman = page.locator('[data-role-id="craftsman"]');
|
||||
assert.equal(await craftsman.locator(".role-warning").count(), 1);
|
||||
await craftsman.click();
|
||||
|
|
|
|||
21
test/recruitment-preview.test.js
Normal file
21
test/recruitment-preview.test.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { recruitmentShares, recruitmentBonus } from "../viewer/recruitment.js";
|
||||
|
||||
test("recruitment preview distributes the mixed register starting at the chooser", () => {
|
||||
const state = {
|
||||
players: Array.from({ length: 3 }, () => ({ buildings: [] })),
|
||||
register: { w: 3, c: 2 },
|
||||
workerSupply: 10,
|
||||
};
|
||||
assert.deepEqual(recruitmentShares(state, 1), [1, 2, 2]);
|
||||
assert.equal(recruitmentBonus(state, 1), 1);
|
||||
state.players[1].buildings.push({ id: "publishingHouse", w: 1, c: 0 });
|
||||
assert.equal(recruitmentBonus(state, 1), 2);
|
||||
state.workerSupply = 1;
|
||||
assert.equal(recruitmentBonus(state, 1), 1);
|
||||
state.workerSupply = 0;
|
||||
assert.equal(recruitmentBonus(state, 1), 0);
|
||||
state.register = { w: 0, c: 0 };
|
||||
assert.deepEqual(recruitmentShares(state, 1), [0, 0, 0]);
|
||||
});
|
||||
16
viewer/recruitment.js
Normal file
16
viewer/recruitment.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Distribution from the work register, before optional building/role bonuses.
|
||||
export function recruitmentShares(state, chooser) {
|
||||
const total = state.register.w + state.register.c;
|
||||
const n = state.players.length;
|
||||
return state.players.map(
|
||||
(_, i) =>
|
||||
Math.floor(total / n) + ((i - chooser + n) % n < total % n ? 1 : 0),
|
||||
);
|
||||
}
|
||||
|
||||
export function recruitmentBonus(state, chooser) {
|
||||
const publishing = state.players[chooser].buildings.some(
|
||||
(b) => b.id === "publishingHouse" && b.w + b.c > 0,
|
||||
);
|
||||
return Math.min(state.workerSupply, publishing ? 2 : 1);
|
||||
}
|
||||
|
|
@ -496,20 +496,6 @@ body:has(.pr-game) {
|
|||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
}
|
||||
.pr-game .coin-badge {
|
||||
position: absolute;
|
||||
right: -12px;
|
||||
top: -5px;
|
||||
background: #e4bd6c;
|
||||
color: #423212;
|
||||
border-radius: 50%;
|
||||
font-size: 11px;
|
||||
font-weight: bold;
|
||||
min-width: 19px;
|
||||
height: 19px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
}
|
||||
.pr-game .role small {
|
||||
max-width: 100%;
|
||||
overflow: hidden;
|
||||
|
|
@ -2791,3 +2777,39 @@ body:has(.pr-game) {
|
|||
align-items: center;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.pr-game .role-coins {
|
||||
position: absolute;
|
||||
inset-inline-start: calc(100% - 5px);
|
||||
top: -4px;
|
||||
white-space: nowrap;
|
||||
font-weight: bold;
|
||||
}
|
||||
.pr-game .role-coins .metric { gap: 1px; }
|
||||
.pr-game .role-coins .icon { width: 16px; height: 16px; }
|
||||
.pr-game .role-recruits { font-size: 11px; }
|
||||
.pr-game .role-recruits .icon { width: 15px; height: 15px; }
|
||||
.pr-game .role-picker { grid-template-columns: repeat(4, minmax(0, 1fr)); grid-auto-rows: auto; align-items: start; }
|
||||
.pr-game .role-choice { min-width: 0; }
|
||||
.pr-game .role-choice > .role { width: 100%; min-height: 82px; }
|
||||
.pr-game .role-choice-warning {
|
||||
font-size: 12px;
|
||||
line-height: 1.4;
|
||||
margin: 8px 0 0;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.pr-game .role-choice-warning > .icon { margin-inline-end: 4px; vertical-align: middle; }
|
||||
.pr-game .recruitment-preview { border-top: 1px solid var(--line); padding-top: 12px; }
|
||||
.pr-game .recruitment-preview h3 { display: flex; align-items: center; gap: 6px; }
|
||||
.pr-game .recruitment-shares { display: flex; flex-wrap: wrap; gap: 8px 16px; }
|
||||
.pr-game .recruitment-shares > span { display: flex; align-items: center; gap: 6px; min-width: 0; }
|
||||
.pr-game .recruitment-shares b { overflow-wrap: anywhere; }
|
||||
.pr-game .recruitment-preview p { font-size: 12px; margin-bottom: 0; }
|
||||
@media (max-width: 600px) {
|
||||
.pr-game .role-picker { grid-template-columns: repeat(2, minmax(0, 1fr)); }
|
||||
}
|
||||
.pr-game .role-recruits, .pr-game .recruitment-count, .pr-game .role-recruits > span {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 2px;
|
||||
}
|
||||
|
|
|
|||
51
viewer/ui.js
51
viewer/ui.js
|
|
@ -1,3 +1,4 @@
|
|||
import { recruitmentShares, recruitmentBonus } from "./recruitment.js";
|
||||
import { showBuildingPayment } from "./building-payment.js";
|
||||
import { FORCE_DARK_THEME } from "./theme.js";
|
||||
import { mergePreferences, imageUrl } from "./platform.js";
|
||||
|
|
@ -437,17 +438,7 @@ export function mountGame(
|
|||
function actionButton(m) {
|
||||
if (m.type === "produce")
|
||||
return `${moveButton(m, moveLabel(m), "craftsman", "primary")}<button class="quiet" data-custom-production="${encode(m)}" ${allowed() ? "" : "disabled"}>${t("selectQty")}</button>`;
|
||||
if (m.type === "role") {
|
||||
const role = state.roles.find((r) => r.id === m.id);
|
||||
const warning = state.roleWarnings?.[m.id];
|
||||
return moveButton(
|
||||
m,
|
||||
`${t(m.id)}${role.coins ? metric("coin", role.coins) : ""}${warning ? icon("warning", 18) : ""}`,
|
||||
m.id === "adventurer2" ? "adventurer" : m.id,
|
||||
"",
|
||||
warning ? `${t(m.id)} — ${t(warning.reason)}` : "",
|
||||
);
|
||||
}
|
||||
if (m.type === "role") return roleCard(state.roles.find((r) => r.id === m.id));
|
||||
const compact = compactAction(m);
|
||||
return compact
|
||||
? moveButton(m, compact[0], null, "compact-action", compact[1])
|
||||
|
|
@ -477,6 +468,8 @@ export function mountGame(
|
|||
if (!allowed()) return "";
|
||||
const task = state.tasks[0],
|
||||
moves = actionMoves();
|
||||
if (task.kind === "role")
|
||||
return `<div class="roles role-picker">${moves.map((m) => `<div class="role-choice">${actionButton(m)}${state.roleWarnings?.[m.id] ? `<p class="role-choice-warning">${icon("warning", 15)}${esc([...new Intl.Segmenter(t.locale, { granularity: "sentence" }).segment(t(state.roleWarnings[m.id].reason))][0].segment)}</p>` : ""}</div>`).join("")}</div>${moves.some((m) => m.id === "recruiter") ? recruitmentPreview() : ""}`;
|
||||
if (["achievementChoose", "achievementDraft"].includes(task.kind))
|
||||
return `<p>${t(task.kind)} · ${state.players[seat].achievements.length}/4</p><div class="achievement-choices">${moves
|
||||
.map((m) => {
|
||||
|
|
@ -614,25 +607,31 @@ export function mountGame(
|
|||
function plantationHelp(id) {
|
||||
return `${t(id)} — ${t(id === "quarry" ? "quarryHelp" : id === "forest" ? "forestHelp" : id === "corn" ? "cornHelp" : "plantationHelp")}`;
|
||||
}
|
||||
function recruitmentCount(n) {
|
||||
return `<span class="recruitment-count" title="${esc(t("recruitmentPool"))}">${icon(!state.register.w && state.register.c ? "citizen" : "worker", 16)}${state.register.w && state.register.c ? icon("citizen", 16) : ""}<b>${n}</b></span>`;
|
||||
}
|
||||
function roleCard(r) {
|
||||
const m = state.legal.find((m) => m.type === "role" && m.id === r.id),
|
||||
selected = r.taken !== null,
|
||||
current = !state.finished && state.tasks[0]?.kind !== "role" &&
|
||||
r.id === (state.role === "adventurer"
|
||||
? state.events.filter((e) => e.type === "role").at(-1)?.id : state.role),
|
||||
acting = allowed(),
|
||||
warning = m && acting ? state.roleWarnings?.[r.id] : null;
|
||||
return `<button data-role-id="${r.id}" class="role ${selected ? "taken" : ""} ${current ? "current" : ""} ${m ? "available" : ""}" ${current ? 'aria-current="step"' : ""} ${selected ? `title="${esc(t(r.id))} · ${esc(state.players[r.taken].name)}${current ? ` · ${esc(t("currentRole"))}` : ""}"` : warning ? `title="${esc(t(warning.reason))}" aria-label="${esc(t(r.id))} — ${esc(t(warning.reason))}"` : ""} ${m && acting ? `data-move="${encode(m)}"` : `data-role="${r.id}"`}>${current ? `<small class="role-status">${t("currentRole")}</small>` : ""}${warning ? `<span class="role-warning" aria-hidden="true">${icon("warning", 15)}</span>` : ""}<span class="role-icon">${icon(r.id === "adventurer2" ? "adventurer" : r.id, 32)}${r.coins ? `<span class="role-coins">${metric("coin", r.coins)}</span>` : ""}</span><b>${t(r.id)}</b>${r.id === "recruiter" && m ? `<span class="role-recruits">${recruitmentCount(recruitmentShares(state, seat)[seat])}${recruitmentBonus(state, seat) ? ` <span title="${esc(t("privilege"))}">+${metric("worker", recruitmentBonus(state, seat))}</span>` : ""}</span>` : ""}${selected ? playerMarker(r.taken) : ""}</button>`;
|
||||
}
|
||||
function recruitmentPreview() {
|
||||
const shares = recruitmentShares(state, seat);
|
||||
const owner = state.players[seat];
|
||||
const bonus = recruitmentBonus(state, seat);
|
||||
return `<section class="recruitment-preview"><h3>${icon("recruiter", 20)}${t("recruitmentPool")}</h3><div class="recruitment-shares">${state.players.map((p, i) => `<span>${playerMarker(i, true)}<b>${esc(p.name)}</b>${recruitmentCount(shares[i])}</span>`).join("")}</div>${bonus ? `<p>${t("privilege")} · ${esc(owner.name)} +${metric("worker", bonus)}</p>` : ""}</section>`;
|
||||
}
|
||||
function sharedBoard() {
|
||||
const festivalTag = desktopLayout.matches ? "section" : "details";
|
||||
const festivalHeadingTag = desktopLayout.matches ? "header" : "summary";
|
||||
const acting = allowed(),
|
||||
task = state.tasks[0];
|
||||
const currentRole =
|
||||
!state.finished && task && task.kind !== "role"
|
||||
? state.role === "adventurer"
|
||||
? state.events.filter((e) => e.type === "role").at(-1)?.id
|
||||
: state.role
|
||||
: null;
|
||||
const acting = allowed(), task = state.tasks[0];
|
||||
return `<section class="shared-board"><div class="roles">${state.roles
|
||||
.map((r) => {
|
||||
const m = state.legal.find((m) => m.type === "role" && m.id === r.id),
|
||||
selected = r.taken !== null,
|
||||
current = r.id === currentRole,
|
||||
warning = m && acting ? state.roleWarnings?.[r.id] : null;
|
||||
return `<button data-role-id="${r.id}" class="role ${selected ? "taken" : ""} ${current ? "current" : ""} ${m ? "available" : ""}" ${current ? 'aria-current="step"' : ""} ${selected ? `title="${esc(t(r.id))} · ${esc(state.players[r.taken].name)}${current ? ` · ${esc(t("currentRole"))}` : ""}"` : warning ? `title="${esc(t(warning.reason))}" aria-label="${esc(t(r.id))} — ${esc(t(warning.reason))}"` : ""} ${m && acting ? `data-move="${encode(m)}"` : `data-role="${r.id}"`}>${current ? `<small class="role-status">${t("currentRole")}</small>` : ""}${warning ? `<span class="role-warning" aria-hidden="true">${icon("warning", 15)}</span>` : ""}<span class="role-icon">${icon(r.id === "adventurer2" ? "adventurer" : r.id, 32)}${r.coins ? `<span class="coin-badge">${r.coins}</span>` : ""}</span><b>${t(r.id)}</b>${selected ? playerMarker(r.taken) : ""}</button>`;
|
||||
})
|
||||
.map(roleCard)
|
||||
.join(
|
||||
"",
|
||||
)}<button type="button" class="role role-help" data-roles-help title="${esc(t("rolesHelp"))}" aria-label="${esc(t("rolesHelp"))}">${icon("help", 32)}</button></div><div class="recruitment-pool" role="group" aria-label="${esc(t("recruitmentPool"))}"><strong>${icon("recruiter", 20)}${t("recruitmentPool")}</strong>${metric("worker", state.register.w, "registerWorkers")}${state.register.c ? metric("citizen", state.register.c, "registerCitizens") : ""}</div><div class="port panel"><header><h2>${t("harborTitle")}</h2><div>${metric("vp", Math.max(0, state.vpSupply), "vpSupply")}</div></header><div class="goods-supply" role="group" aria-label="${esc(t("goodsSupply"))}"><strong>${t("goodsSupply")}</strong>${GOODS.map((g) => `<span class="supply-good ${state.supply[g] === 0 ? "depleted" : ""}">${metric(g, state.supply[g])}</span>`).join("")}</div><div class="ships">${state.ships.map((sh, i) => `<article class="ship ${sh.amount === sh.capacity ? "full" : ""}">${shipGraphic(sh)}<div>${sh.good ? icon(sh.good, 25) : icon("ship", 25)}<b>${sh.amount}<small> / ${sh.capacity}</small></b></div></article>`).join("")}</div><div class="trade-row"><span>${icon("trader", 18)} ${t("tradingHouse")}</span>${Array.from({ length: 4 }, (_, i) => `<span class="trade-slot">${state.trade[i] ? icon(state.trade[i], 26) : "·"}</span>`).join("")}</div></div><section class="plantation-supply"><header>${icon("planter", 20)}<h3>${t("plantedOffer")}</h3></header><div class="estate-offer">${state.offer
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue