diff --git a/package.json b/package.json index c14e8dc..35a361f 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test:tutorial:browser": "node test/tutorial-browser.mjs && node test/tutorial-walkthrough-browser.mjs" }, "dependencies": { - "@boardgamers/protocol": "0.6.2", + "@boardgamers/protocol": "0.8.1", "@noble/hashes": "2.0.1", "morphdom": "2.7.8" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2dc8fde..6a4034b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@boardgamers/protocol': - specifier: 0.6.2 - version: 0.6.2 + specifier: 0.8.1 + version: 0.8.1 '@noble/hashes': specifier: 2.0.1 version: 2.0.1 @@ -30,8 +30,8 @@ importers: packages: - '@boardgamers/protocol@0.6.2': - resolution: {integrity: sha512-6FKgLsB4guCzNww1rkanhrsLHIdQVajDs1B4qNsYcgtUobTBRM2VPt9T4mlc8ZDX4JDQltvJVHuK7XDZ1J556A==} + '@boardgamers/protocol@0.8.1': + resolution: {integrity: sha512-BZNj+wof1aR1nSybRMqmQ+yAVNR3ZZy+mczIz2KbXSLXMLL3Jv21yi50CRbJ2ipdkPiPatcSP0nJj+xJbFgIfQ==} '@esbuild/aix-ppc64@0.25.12': resolution: {integrity: sha512-Hhmwd6CInZ3dwpuGTF8fJG6yoWmsToE+vYgD4nytZVxcu1ulHpUQRAB1UJ8+N1Am3Mz4+xOByoQoSZf4D+CpkA==} @@ -226,7 +226,7 @@ packages: snapshots: - '@boardgamers/protocol@0.6.2': + '@boardgamers/protocol@0.8.1': dependencies: zod: 4.6.5 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 13e8649..1a54af5 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,4 +1,4 @@ allowBuilds: esbuild: true minimumReleaseAgeExclude: - - "@boardgamers/protocol@0.6.1 || 0.6.2" + - "@boardgamers/protocol@0.6.1 || 0.6.2 || 0.8.1" diff --git a/test/player-colors.test.js b/test/player-colors.test.js new file mode 100644 index 0000000..8a0e2d9 --- /dev/null +++ b/test/player-colors.test.js @@ -0,0 +1,26 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { init } from "../engine/index.js"; +import { + playerColorState, + playerColorInk, + playerColorText, +} from "../viewer/player-colors.js"; + +test("viewer colours preserve engine state and accessibility defaults", () => { + const original = init(3, [], {}, "colour-test"); + const saved = structuredClone(original); + const view = playerColorState(original, ["#12abcd", "url(x)"]); + assert.equal(view.players[0].color, "#12abcd"); + assert.equal(view.players[1].color, original.players[1].color); + assert.deepEqual(original, saved); + assert.deepEqual(playerColorState(original, ["#12abcd"], true), original); + assert.deepEqual(playerColorState(original, []), original); +}); + +test("white and black custom colours keep readable labels", () => { + assert.equal(playerColorText("#ffffff"), "#000"); + assert.equal(playerColorText("#000000"), "#fff"); + assert.notEqual(playerColorInk("#ffffff"), "#ffffff"); + assert.equal(playerColorInk("#000000"), "#000000"); +}); diff --git a/viewer/activity.js b/viewer/activity.js index 1f965d9..88b7a10 100644 --- a/viewer/activity.js +++ b/viewer/activity.js @@ -1,3 +1,4 @@ +import { playerColorInk } from "./player-colors.js"; import { assets } from "./assets.js"; import { createRenderer } from "./dom.js"; import { tilePreviewSvg } from "./board.js"; @@ -73,7 +74,11 @@ export function mountActivity( author.className = "chat-author"; author.dataset.bgsPlayer = index; const color = state?.players[index]?.color ?? COLORS[index]; - if (color) author.style.setProperty("--chat-player-color", color); + if (color) + author.style.setProperty( + "--chat-player-ink", + playerColorInk(color), + ); const avatar = avatarForPlayer?.(index); if (avatar) { const img = document.createElement("img"); @@ -136,7 +141,7 @@ export function mountActivity( const text = (source) => esc(translateText(source, lang)); const playerName = (p) => state?.players[p] - ? `${esc(state.players[p].name)}` + ? `${esc(state.players[p].name)}` : ""; const chip = (html, kind = "") => `${html}`; diff --git a/viewer/board.js b/viewer/board.js index f80ac1c..fb53dec 100644 --- a/viewer/board.js +++ b/viewer/board.js @@ -157,7 +157,14 @@ export function boardSvg( svg += `${esc(help)}`; svg += huts[i] !== undefined - ? svgIcon("build", x, y, 30, COLORS[huts[i]], help) + ? svgIcon( + "build", + x, + y, + 30, + s.players[huts[i]]?.color ?? COLORS[huts[i]], + help, + ) : ``; if (colorBlind && huts[i] !== undefined) svg += `${symbols[huts[i]]}`; diff --git a/viewer/player-colors.js b/viewer/player-colors.js new file mode 100644 index 0000000..b8f296f --- /dev/null +++ b/viewer/player-colors.js @@ -0,0 +1,32 @@ +export function playerColorState(state, preferred = [], colorBlind = false) { + if (!state) return state; + return { + ...state, + players: state.players.map((player, index) => ({ + ...player, + color: + !colorBlind && /^#[a-f0-9]{6}$/i.test(preferred[index] ?? "") + ? preferred[index] + : player.color, + })), + }; +} + +const rgb = (hex) => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)); +const luminance = (values) => + values.reduce((total, channel, i) => { + const c = channel / 255; + return ( + total + + (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4) * + [0.2126, 0.7152, 0.0722][i] + ); + }, 0); +export const playerColorText = (hex) => + luminance(rgb(hex)) > 0.179 ? "#000" : "#fff"; +export function playerColorInk(hex) { + let values = rgb(hex); + while (luminance(values) > 0.13) + values = values.map((v) => Math.floor(v * 0.8)); + return "#" + values.map((v) => v.toString(16).padStart(2, "0")).join(""); +} diff --git a/viewer/style.css b/viewer/style.css index d7c6eac..f1900e9 100644 --- a/viewer/style.css +++ b/viewer/style.css @@ -268,7 +268,7 @@ min-width: 20px; height: 16px; background: var(--player); - color: #fff; + color: var(--player-text, #fff); border-radius: 8px; font-size: 10px; font-weight: 750; @@ -709,7 +709,7 @@ max-width: 160px; } .vanuatu .player-card .profile { - color: color-mix(in srgb, var(--player) 60%, black); + color: var(--player-ink, #244e53); } .vanuatu .player-supplies .icon-build { color: var(--player); @@ -731,7 +731,7 @@ } .vanuatu .chat-panel .chat-author[data-bgs-player] { /* Darken the player palette for readable names on the cream chat surface. */ - color: color-mix(in srgb, var(--chat-player-color, #244e53) 60%, black); + color: var(--chat-player-ink, #244e53); } .vanuatu .chat-panel .chat-author { display: inline-flex; @@ -1859,3 +1859,12 @@ body:has(.vanuatu) { transform: translate(1px, 1px); } } + +.vanuatu .supporter-badge { + display: inline-block; + width: 14px; + height: 14px; + margin-left: 4px; + vertical-align: -2px; + flex-shrink: 0; +} diff --git a/viewer/ui.js b/viewer/ui.js index 9f074ff..93ce959 100644 --- a/viewer/ui.js +++ b/viewer/ui.js @@ -1,4 +1,9 @@ import { blockingHtml, blockingText } from "./blocking.js"; +import { + playerColorState, + playerColorText, + playerColorInk, +} from "./player-colors.js"; import { FIRST_HELP, RETRIEVE_HELP } from "./learning.js"; import { EXTEND_WHO, EXTEND_HOW, REMAINING_TILES } from "./learning.js"; import { createRenderer } from "./dom.js"; @@ -61,6 +66,17 @@ export function mountGame(target, options = {}) { dialog = root.querySelector("dialog"), feedback = root.querySelector(".feedback"); let avatars = []; + let appearance = {}; + const playerBadge = (i) => { + const badge = appearance.players?.[i]?.pro && appearance.supporterBadge; + return badge + ? `${esc(badge.label)}` + : ""; + }; + let playerColors = []; + let rawState; + const displayState = (state) => + playerColorState(state, playerColors, colorBlind); let s, player, enabled = true, @@ -240,7 +256,7 @@ export function mountGame(target, options = {}) { } function playerCard(p, i) { const c = CHARACTERS[p.character]; - return `
${btn(`${avatars[i] ? `` : ""}${colorBlind ? symbols[i] + " " : ""}${esc(p.name)}`, `data-bgs-player="${i}" title="${esc(p.name)}"`, "profile")} ${s.first === i ? btn(icon("first", translateText(FIRST_HELP, lang)), `data-first-help aria-label="${t("first")}"`, "icon-button") : ""}${token("point", p.score)}
${token("coin", p.money)}${p.fish.length ? p.fish.map((n) => token("fish", n)).join("") : token("fish", 0)}${p.treasures.length ? p.treasures.map((n) => token("explore", n)).join("") : token("explore", 0)}${token("build", p.hutsLeft)}
${c ? `` : ""}${i === player ? `
${legal().some((m) => m.type === "treasure") ? btn(`${icon("explore")} → ${icon("coin")}`, `data-free="treasure" title="${t("treasure")}"`) : ""}${legal().some((m) => m.type === "beg") ? btn(`${icon("point")} → ${icon("coin")}`, `data-free="beg" title="${t("beg")}"`) : ""}
` : ""}
`; + return `
${btn(`${avatars[i] ? `` : ""}${colorBlind ? symbols[i] + " " : ""}${esc(p.name)}${playerBadge(i)}`, `data-bgs-player="${i}" title="${esc(p.name)}"`, "profile")} ${s.first === i ? btn(icon("first", translateText(FIRST_HELP, lang)), `data-first-help aria-label="${t("first")}"`, "icon-button") : ""}${token("point", p.score)}
${token("coin", p.money)}${p.fish.length ? p.fish.map((n) => token("fish", n)).join("") : token("fish", 0)}${p.treasures.length ? p.treasures.map((n) => token("explore", n)).join("") : token("explore", 0)}${token("build", p.hutsLeft)}
${c ? `` : ""}${i === player ? `
${legal().some((m) => m.type === "treasure") ? btn(`${icon("explore")} → ${icon("coin")}`, `data-free="treasure" title="${t("treasure")}"`) : ""}${legal().some((m) => m.type === "beg") ? btn(`${icon("point")} → ${icon("coin")}`, `data-free="beg" title="${t("beg")}"`) : ""}
` : ""}
`; } function dock() { const planning = ["plan", "neutral"].includes(s.phase) && working().length; @@ -293,7 +309,7 @@ export function mountGame(target, options = {}) { const stacks = s.players .map((p, i) => p.markers[a] - ? `${colorBlind ? symbols[i] : ""}${p.markers[a]}` + ? `${colorBlind ? symbols[i] : ""}${p.markers[a]}` : "", ) .join(""); @@ -747,7 +763,7 @@ export function mountGame(target, options = {}) { .sort((a, b) => b.score - a.score) .map( (p) => - `
${esc(p.name)}${token("point", p.score)}${ + `
${esc(p.name)}${token("point", p.score)}${ p.final ? `${Object.entries(p.final) .filter(([, v]) => v !== 0) @@ -837,7 +853,8 @@ export function mountGame(target, options = {}) { render(state) { const next = `${state.historyLength ?? state.round + ":" + state.phase}:${player}`; const key = JSON.stringify(state); - s = state; + rawState = state; + s = displayState(state); if (stateKey === key && next === revision) return; stateKey = key; if (next !== revision) { @@ -868,10 +885,17 @@ export function mountGame(target, options = {}) { render(); }, setPreferences(p) { - const previous = `${analysis}:${colorBlind}:${lang}`; + const previous = `${analysis}:${colorBlind}:${lang}:${playerColors.join()}:${JSON.stringify(appearance)}`; + appearance = p.bgs ?? {}; chatNotifications = p.chatNotifications !== false; analysis = p.analysis === true; colorBlind = p.colorBlind === true; + playerColors = Array.isArray(p.bgs?.playerColors) + ? p.bgs.playerColors.map((c) => + typeof c === "string" && /^#[a-f0-9]{6}$/i.test(c) ? c : undefined, + ) + : []; + s = displayState(rawState); sound = p.sound === true; moveSound.setEnabled(sound); for (const [name, value] of Object.entries({ colorBlind, sound })) { @@ -887,7 +911,11 @@ export function mountGame(target, options = {}) { activity.setLanguage(lang); } } - if (previous !== `${analysis}:${colorBlind}:${lang}`) render(); + if ( + previous !== + `${analysis}:${colorBlind}:${lang}:${playerColors.join()}:${JSON.stringify(appearance)}` + ) + render(); else updateUnread(); }, destroy() {