Show the Activities picker as a two-column Score ledger ordered by recently played or A to Z

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-12 10:35:28 -08:00
1 parent ba19f79f1c
commit fde92ef4d2
11 files changed
+453 -99

No files matched your search

+36
View File
@@ -64,6 +64,42 @@ export const bestKeys = (entry) => (entry.courses ? entry.courses.map((c) => bes
// leaves it at 0.
export const memoKey = (id) => "activityMemo" + cap(id);
// The looks.json key that holds when a game was last picked, in epoch
// milliseconds: activityPlayedSnake. Set on pick (screens/activities.js
// pick()), never by a score; 0 means never picked.
export const playedKey = (id) => "activityPlayed" + cap(id);
// Splits the game list into the games the person has played and the ones not
// played yet, for the picker's rows. Returns { played, unplayed }, arrays of
// the `list` entries themselves. A game is played when it has a played time,
// is the last pick, or has a best score on any course. `unplayed` is always
// A to Z. `order` "az" sorts `played` A to Z; anything else (the default
// "recent") sorts it newest pick first, the last pick ahead of games with no
// time, ties A to Z. Keys for games not in `list` are ignored.
export function orderGames(list, looks, order) {
const byTitle = (a, b) => a.title.localeCompare(b.title);
const timeOf = (e) => {
const n = Number(looks[playedKey(e.id)]);
return Number.isFinite(n) && n > 0 ? n : 0;
};
const isPlayed = (e) => timeOf(e) > 0 || e.id === looks.lastActivity || bestKeys(e).some((key) => looks[key] > 0);
const played = list.filter(isPlayed);
const unplayed = list.filter((e) => !isPlayed(e)).sort(byTitle);
if (order === "az") return { played: played.sort(byTitle), unplayed };
played.sort((a, b) => {
const ta = timeOf(a);
const tb = timeOf(b);
if (ta !== tb) return tb - ta;
if (ta === 0) {
const la = a.id === looks.lastActivity;
const lb = b.id === looks.lastActivity;
if (la !== lb) return la ? -1 : 1;
}
return byTitle(a, b);
});
return { played, unplayed };
}
// The looks.json key that holds a game's music volume, 0 to 100:
// activityMusicDrive. Only a game whose entry sets `music` has one.
export const musicKey = (id) => "activityMusic" + cap(id);
+13 -3
View File
@@ -9,7 +9,7 @@ import { normalizeRole, ROLE_NOT_SET } from "./starters.js";
import { bootData, hooks } from "./state.js";
import { el } from "./render.js";
import { tellApp } from "./app-bridge.js";
import { ACTIVITIES, bestKeys, memoKey, musicKey, musicLevel } from "./activities.js";
import { ACTIVITIES, bestKeys, memoKey, musicKey, musicLevel, playedKey } from "./activities.js";
const $ = (id) => document.getElementById(id);
@@ -49,6 +49,11 @@ export const DEFAULT_LOOKS = {
...Object.fromEntries(ACTIVITIES.map((a) => [memoKey(a.id), 0])),
// And, for a game with music, its music volume (activityMusicDrive), 0 to 100.
...Object.fromEntries(ACTIVITIES.filter((a) => a.music !== undefined).map((a) => [musicKey(a.id), a.music])),
// And when each game was last picked (activityPlayedSnake, epoch ms, 0 for
// never) and the picker's order, "recent" or "az". Neither is cleared by
// Reset best scores or Back to default.
...Object.fromEntries(ACTIVITIES.map((a) => [playedKey(a.id), 0])),
activityOrder: "recent",
};
export let looks = { ...DEFAULT_LOOKS };
@@ -79,8 +84,11 @@ export function adoptLooks(saved) {
}
const memo = Number(looks[memoKey(a.id)]);
looks[memoKey(a.id)] = Number.isFinite(memo) && memo > 0 ? Math.floor(memo) : 0;
const played = Number(looks[playedKey(a.id)]);
looks[playedKey(a.id)] = Number.isFinite(played) && played > 0 ? Math.floor(played) : 0;
if (a.music !== undefined) looks[musicKey(a.id)] = Math.round(musicLevel(a, looks[musicKey(a.id)]) * 100);
}
if (!["recent", "az"].includes(looks.activityOrder)) looks.activityOrder = DEFAULT_LOOKS.activityOrder;
if (saved && typeof saved === "object" && Object.hasOwn(saved, "role")) looks.role = normalizeRole(saved.role);
else delete looks.role;
}
@@ -286,7 +294,8 @@ export function renderSwatches() {
// "Back to default": every look returns to its default, but the role is
// marked as asked, the welcome stays seen, and the Activities' best scores
// and last pick stay (only Reset best scores clears those). Given `keys` (one
// and last pick stay (only Reset best scores clears those), and so do the
// played times and the picker's order (nothing clears those). Given `keys` (one
// Preferences tab's, prefsResetScope() in prefs.js), only those change: the
// role is cleared but marked as asked, the rest take their defaults, and
// every other key stays as it is.
@@ -295,10 +304,11 @@ export function resetLooks(keys) {
for (const key of keys) looks[key] = key === "role" ? ROLE_NOT_SET : DEFAULT_LOOKS[key];
return;
}
const kept = { lastActivity: looks.lastActivity };
const kept = { lastActivity: looks.lastActivity, activityOrder: looks.activityOrder };
for (const a of ACTIVITIES) {
for (const key of bestKeys(a)) kept[key] = looks[key];
kept[memoKey(a.id)] = looks[memoKey(a.id)];
kept[playedKey(a.id)] = looks[playedKey(a.id)];
}
looks = { ...DEFAULT_LOOKS, ...kept, role: ROLE_NOT_SET, welcomeSeen: looks.welcomeSeen };
}
+119 -48
View File
@@ -3,7 +3,7 @@
// Small games for a long wait. The list of games is activities.js; each game
// is its own module under activities/, loaded with import() the first time it
// is picked, and answers the calls the list's header names. The window opens
// on a picker of game cards and plays one game at a time. This file owns
// on a picker of game rows and plays one game at a time. This file owns
// everything around a game: the dialog, the picker, the one frame timer, the
// keys and the pauses. A game never runs a timer of its own, so a paused game cannot move:
// pausing is this file not ticking plus the game's own paused state. Nothing
@@ -15,7 +15,7 @@ import { finish, myTurn } from "../selectors.js";
import { looks, saveLooks } from "../looks.js";
import { el, lightDismiss, SVG_NS } from "../render.js";
import { tellApp } from "../app-bridge.js";
import { ACTIVITIES, bestKey, bestKeys, memoKey, musicKey, musicLevel } from "../activities.js";
import { ACTIVITIES, bestKey, bestKeys, memoKey, musicKey, musicLevel, orderGames, playedKey } from "../activities.js";
import { hum as playHum, hushTones as hushSounds, tone as playTone, unlockTones } from "../tones.js";
import { hushMusic, music as playMusic } from "../music.js";
@@ -97,7 +97,7 @@ const games = new Map();
// A load in flight per game id, so two quick opens share one import().
const loading = new Map();
let currentId = null;
// What the window shows: "picker", the page of game cards, "courses", the
// What the window shows: "picker", the page of game rows, "courses", the
// dashboard of a game with courses (Mini golf's six), or "game", the board of
// the game on screen. With one game listed there is no picker.
let view = "game";
@@ -399,6 +399,7 @@ function resetBest() {
return;
}
disarmReset();
// Best keys only: the played times and the picker's order stay.
for (const entry of list) for (const key of bestKeys(entry)) looks[key] = 0;
saveLooks();
paintOverlay();
@@ -758,7 +759,7 @@ async function showGame(id) {
/* ------------------------------------------------------------- picker */
// Each card's cover, drawn as a little SVG scene: [tag, attributes] pairs on
// Each row's cover, drawn as a little SVG scene: [tag, attributes] pairs on
// a 120 by 72 box. A `cv-` class carries one of the page's tokens
// (styles/activities.css), since the page's policy refuses a style attribute.
// A game with no cover of its own gets the plain one.
@@ -1084,53 +1085,114 @@ function cover(id) {
return svg;
}
// The round a card's game has waiting, if any: only the game last played can
// The round a row's game has waiting, if any: only the game last played can
// have one, since picking another lets it go.
const pausedRound = (id) => {
const g = games.get(id);
return g && g.state === "paused" ? g : null;
};
// The picker: a card per game with its cover, its line, its best and, for the
// game last played, whether a round of it is waiting.
// The "N games" count the Order bar shows.
const countText = (n) => `${n} game${n === 1 ? "" : "s"}`;
// A row's best chip: a muted label and a value. Mini golf counts the courses
// it has a best on; a game with no best yet reads "No best yet" with no value.
function bestChip(entry) {
const chip = el("span", "activities-row-best");
const label = (text) => chip.append(el("span", "activities-row-best-label", text));
const value = (text) => chip.append(el("span", "activities-row-best-value", text));
if (entry.courses) {
const n = entry.courses.filter((c) => best(entry.id, c)).length;
if (n) value(`${n} of ${entry.courses.length} courses`);
else label(`${entry.courses.length} courses to play`);
return chip;
}
const n = best(entry.id);
if (!n) {
chip.classList.add("none");
label("No best yet");
return chip;
}
label(lowerIsBetter.has(entry.id) ? "Best round" : "Best");
chip.append(" ");
value(n.toLocaleString("en-US"));
return chip;
}
// One row of the picker: the cover, the title with its tag, the line about the
// game and its best chip. The tag says whether a round is waiting, else
// whether this was the last game played.
function gameRow(entry) {
const b = el("button", "activities-row");
b.type = "button";
b.dataset.activity = entry.id;
const art = el("span", "activities-cover");
art.append(cover(entry.id));
const head = el("span", "activities-row-head", "");
head.append(el("span", "activities-row-title", entry.title));
const waiting = pausedRound(entry.id);
const tag = waiting ? `Paused at ${Number(waiting.score).toLocaleString("en-US")}` : entry.id === looks.lastActivity ? "Last played" : "";
if (tag) head.append(el("span", `activities-row-tag${waiting ? " waiting" : ""}`, tag));
const text = el("span", "activities-row-text");
text.append(head, el("span", "activities-row-blurb", entry.blurb || ""));
b.append(art, text, bestChip(entry));
b.addEventListener("click", () => pick(entry.id));
return b;
}
// The picker: an Order bar (Recently played or A to Z, and the game count),
// then one grid of game rows, with the games never picked under a Not played
// yet heading that spans both columns.
function drawPicker() {
const picker = $("activities-picker");
if (!hasPicker()) return picker.replaceChildren();
const cards = el("div", "activities-cards");
cards.setAttribute("role", "group");
cards.setAttribute("aria-label", "Pick a game");
for (const entry of list) {
const b = el("button", "activities-card");
b.type = "button";
b.dataset.activity = entry.id;
const art = el("span", "activities-cover");
art.append(cover(entry.id));
const body = el("span", "activities-card-body");
const n = best(entry.id);
// A game with courses counts the courses it has a best on.
const played = entry.courses ? entry.courses.filter((c) => best(entry.id, c)).length : 0;
const bestText = entry.courses
? played ? `Best on ${played} of ${entry.courses.length} courses` : `${entry.courses.length} courses to play`
: n ? `${lowerIsBetter.has(entry.id) ? "Best round" : "Best"}: ${n}` : "No best yet";
body.append(el("span", "activities-card-title", entry.title), el("span", "activities-card-blurb", entry.blurb || ""), el("span", "activities-card-best", bestText));
b.append(art, body);
const waiting = pausedRound(entry.id);
const tag = waiting ? `Round paused, score ${waiting.score}` : entry.id === looks.lastActivity ? "Last played" : "";
if (tag) b.append(el("span", `activities-card-tag${waiting ? " waiting" : ""}`, tag));
b.addEventListener("click", () => pick(entry.id));
cards.append(b);
const order = looks.activityOrder === "az" ? "az" : "recent";
const { played, unplayed } = orderGames(list, looks, order);
const bar = el("div", "activities-order");
bar.append(el("span", "activities-order-label", "Order"));
for (const [key, text] of [["recent", "Recently played"], ["az", "A to Z"]]) {
const pill = el("button", "activities-order-pill", text);
pill.type = "button";
pill.dataset.order = key;
pill.setAttribute("aria-pressed", String(order === key));
pill.addEventListener("click", () => setOrder(key));
bar.append(pill);
}
const parts = [cards];
bar.append(el("span", "activities-count", countText(list.length)));
const rows = el("div", "activities-rows");
rows.setAttribute("role", "group");
rows.setAttribute("aria-label", "Pick a game");
rows.append(...played.map(gameRow));
if (unplayed.length) {
const group = el("div", "activities-group", "Not played yet");
group.setAttribute("role", "heading");
group.setAttribute("aria-level", "3");
group.append(" ", el("span", "activities-group-count", String(unplayed.length)));
rows.append(group, ...unplayed.map(gameRow));
}
const parts = [bar, rows];
if (list.some((e) => pausedRound(e.id))) parts.push(el("p", "help activities-picker-note", "Picking a different game ends the paused round."));
picker.replaceChildren(...parts);
}
// An Order button pressed: keep the order, draw the picker again and put
// focus back on the button, which the redraw replaced. The pressed one does nothing.
function setOrder(order) {
if ((looks.activityOrder === "az" ? "az" : "recent") === order) return;
looks.activityOrder = order;
saveLooks();
drawPicker();
const pill = $("activities-picker").querySelector(`[data-order="${order}"]`);
if (pill) pill.focus({ preventScroll: true });
}
// The game to put focus on when the picker shows: the one with a round
// waiting, else the last one played, else the first.
// waiting, else the last one played; none means the first row drawn.
function focusId() {
if (currentId && list.some((e) => e.id === currentId)) return currentId;
const waiting = list.find((e) => pausedRound(e.id));
if (waiting) return waiting.id;
if (list.some((e) => e.id === looks.lastActivity)) return looks.lastActivity;
return list.length ? list[0].id : null;
return null;
}
function showPicker() {
@@ -1142,7 +1204,8 @@ function showPicker() {
paintTitle();
drawPicker();
layout();
const card = $("activities-picker").querySelector(`[data-activity="${focusId()}"]`);
const id = focusId();
const card = (id && $("activities-picker").querySelector(`[data-activity="${id}"]`)) || $("activities-picker").querySelector(".activities-row");
if (card) card.focus({ preventScroll: true });
}
@@ -1150,10 +1213,9 @@ function showPicker() {
// one picked, so only one game is ever loaded with a round in it. A game
// with courses opens on its dashboard.
function pick(id) {
if (looks.lastActivity !== id) {
looks.lastActivity = id;
saveLooks();
}
looks.lastActivity = id;
looks[playedKey(id)] = Date.now();
saveLooks();
if (currentId && currentId !== id) {
pauseAll("closed");
games.delete(currentId);
@@ -1248,16 +1310,25 @@ function playCourse(id, course) {
showGame(id);
}
// The arrows walk the cards: left and right one at a time, up and down a row.
// The arrows walk the rows and the course cards by position: left and right
// one at a time through the list, up and down to the nearest button in the
// next line above or below (closest left edge), staying put at the first or
// last line. The group heading is no button, so it is never focused. An Order
// button keeps its arrows, as a normal tab stop.
function moveFocus(dir) {
const cards = [...$("activities-picker").querySelectorAll(".activities-card")];
const active = document.activeElement;
if (active instanceof Element && active.classList.contains("activities-order-pill")) return;
const cards = [...$("activities-picker").querySelectorAll(".activities-row, .activities-card")];
if (!cards.length) return;
const at = cards.indexOf(document.activeElement);
const at = cards.indexOf(active);
if (at < 0) return cards[0].focus();
const perRow = Math.max(1, cards.filter((c) => c.offsetTop === cards[0].offsetTop).length);
const step = { left: -1, right: 1, up: -perRow, down: perRow }[dir];
const next = cards[Math.min(cards.length - 1, Math.max(0, at + step))];
next.focus();
if (dir === "left" || dir === "right") return cards[Math.min(cards.length - 1, Math.max(0, at + (dir === "left" ? -1 : 1)))].focus();
const tops = [...new Set(cards.map((c) => c.offsetTop))].sort((a, b) => a - b);
const line = tops.indexOf(cards[at].offsetTop) + (dir === "up" ? -1 : 1);
if (line < 0 || line >= tops.length) return;
const left = cards[at].offsetLeft;
const near = cards.filter((c) => c.offsetTop === tops[line]).sort((a, b) => Math.abs(a.offsetLeft - left) - Math.abs(b.offsetLeft - left))[0];
near.focus();
}
/* ------------------------------------------------------------ opening */
@@ -1344,8 +1415,8 @@ function onGameKey(e, key) {
e.stopPropagation();
return full ? toggleFull() : closeActivities();
}
// On the picker and a course dashboard the arrows walk the cards, and
// space and Enter press one.
// On the picker and a course dashboard the arrows walk the rows and
// cards, and space and Enter press one.
if (view === "picker" || view === "courses") {
if (Object.hasOwn(KEYS, key) && KEYS[key] !== "action") {
e.preventDefault();
+85 -3
View File
@@ -175,14 +175,16 @@
.activities-banner .btn { flex: none; }
.activities-banner .btn[hidden] { display: none; }
/* The picker: a page of game cards in place of the board, shown once two or
more games are listed. Each card is one button: a cover scene, the title,
/* The picker: a page of game rows in place of the board, shown once two or
more games are listed. Each row is one button: a cover scene, the title,
a line about the game, its best, and a tag for the last one played. */
.activities-picker {
flex: 1 1 auto;
min-height: 0;
overflow: auto;
padding: 4px 2px 8px;
/* The rows' columns follow the picker's own width, not the window's. */
container-type: inline-size;
}
.activities-picker[hidden] { display: none; }
/* Cards keep one width and wrap, and each row (a short last one too) sits in
@@ -255,6 +257,85 @@
}
.activities-card-tag.waiting { border-color: var(--accent-line); background: var(--accent-soft); color: var(--ink); }
.activities-picker-note { margin: 12px 0 0; }
/* The game picker's ledger: an Order bar, then one grid of compact rows (one
column, two from about 640px of picker width). The Not played yet heading
spans both columns, so the arrows walk one grid. */
.activities-order { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin: 0 0 12px; }
.activities-order-label { color: var(--ink-3); font-size: 0.85em; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.activities-order-pill {
padding: 3px 12px;
border: 1px solid var(--rule-2);
border-radius: 999px;
background: var(--panel);
color: var(--ink-2);
font: inherit;
font-size: 0.9em;
font-weight: 600;
cursor: pointer;
}
.activities-order-pill:hover { border-color: var(--accent-line); color: var(--ink); }
.activities-order-pill[aria-pressed="true"] { border-color: var(--accent); background: var(--accent); color: var(--accent-ink); }
.activities-order-pill:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.activities-count { margin-left: auto; color: var(--ink-3); font-size: 0.9em; font-weight: 600; font-variant-numeric: tabular-nums; }
.activities-rows { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
@container (min-width: 640px) {
.activities-rows { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.activities-row {
display: grid;
grid-template-columns: 96px minmax(0, 1fr) auto;
align-items: center;
gap: 12px;
padding: 8px 12px 8px 8px;
border: 1px solid transparent;
border-radius: 10px;
background: var(--panel);
color: var(--ink);
font: inherit;
text-align: left;
cursor: pointer;
transition: border-color 120ms ease-out, box-shadow 120ms ease-out, transform 120ms ease-out;
}
.activities-row:hover,
.activities-row:focus-visible { border-color: var(--accent-line); background: var(--panel-2); }
.activities-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.activities-row .activities-cover { border: 1px solid var(--rule-2); border-radius: 6px; overflow: hidden; }
.activities-row-text { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.activities-row-head { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 8px; }
.activities-row-title { font-size: 1.05em; font-weight: 800; }
.activities-row-blurb { color: var(--ink-2); font-size: 0.92em; line-height: 1.35; }
.activities-row-tag {
padding: 1px 8px;
border: 1px solid var(--rule-2);
border-radius: 999px;
color: var(--ink-2);
font-size: 0.78em;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
.activities-row-tag.waiting { border-color: var(--accent-line); background: var(--accent-soft); color: var(--ink); }
.activities-row-best {
padding: 3px 10px;
border: 1px solid var(--rule-2);
border-radius: 999px;
background: var(--panel-2);
color: var(--ink-3);
font-size: 0.85em;
font-weight: 600;
white-space: nowrap;
font-variant-numeric: tabular-nums;
}
.activities-row-best-value { color: var(--ink); font-weight: 800; }
.activities-row-best.none { border-color: transparent; background: transparent; }
.activities-group {
grid-column: 1 / -1;
margin-top: 6px;
padding-top: 10px;
border-top: 1px solid var(--rule);
font-weight: 800;
}
.activities-group-count { color: var(--ink-3); font-weight: 600; font-variant-numeric: tabular-nums; }
/* All games sits at the far left of the actions, away from Close. */
.modal.activities .activities-back { margin-right: auto; }
.modal.activities .activities-back[hidden] { display: none; }
@@ -264,7 +345,8 @@
.modal.activities .activities-music input { width: 110px; accent-color: var(--accent); }
.modal.activities .activities-music:has(input:disabled) { opacity: 0.55; }
@media (prefers-reduced-motion: no-preference) {
.activities-card:hover { transform: translateY(-2px); }
.activities-card:hover,
.activities-row:hover { transform: translateY(-2px); }
}
/* The board: fills the free space and centers the canvas, with the state