Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ All notable changes to this project are documented in this file. The format is b

### Added

- "Find a game" in the hall: the bar's magnifier (or F, /, or Y on a controller) opens a panel with a
search box, "Continue playing" (the games this browser opened last, the list page's `arcade.recent`)
and "Surprise me". Picking one takes the visitor straight to that cabinet, facing it, after a short
fade (none with Reduce motion). The search finds exactly what the list page's search box finds for the
same words: each cabinet in `layout.json` carries the list's own search text (`search`). On a phone the
bar's Find and Sound become icons up to 480 px to make room.

- Several featured games, chosen in the registry: an optional `featured: <n>` per game in `games.yaml`
(a whole number from 1, its place among the featured games; two games may not share one), reported
by `/api/games` as `featured`. The hall's stage holds every featured game side by side in that order,
Expand Down
3 changes: 3 additions & 0 deletions src/arcade/hall.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ def layout(games, style=None):
"author": game.get("author"),
}
)
# What the hall's "Find a game" looks in: the list page's own search text,
# so both find the same games for the same words (hall-finder.js).
cabinets[-1]["search"] = filters.search_text(cabinets[-1])
return {
"room": room_for(len(ordered), style, featured),
"cabinet": {
Expand Down
18 changes: 12 additions & 6 deletions src/arcade/hall_assets/hall-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
/* The hall's controls, with no DOM and no engine, so the same code runs in
the page and under node (tests/browser/hall_controls_test.cjs):

HallControls.pad(gamepad) -> {forward, right, run, lookX, lookY, a, b, select}
from a "standard" mapping Gamepad
HallControls.edges(prev, now) -> {a, b, select}: true on the frame a button went down
HallControls.pad(gamepad) -> {forward, right, run, lookX, lookY, a, b, y, select, up, down}
from a "standard" mapping Gamepad (up/down: the walk
pushed past half way, for moving through a list)
HallControls.edges(prev, now) -> {a, b, y, select, up, down}: true on the frame it went down
HallControls.LOOK_RATE -> rad/s at full right-stick deflection
HallControls.settings.load(storage, fallback) -> the visitor's settings (never throws)
HallControls.settings.save(storage, s) -> true if they were kept
Expand All @@ -13,7 +14,8 @@
HallControls.project(view, p) -> where a point is on screen, or the edge to point from

The left stick (or the d-pad) walks; the right stick looks, and only the
visitor's own stick turns the view; A plays; B goes back. A stick inside
visitor's own stick turns the view; A plays; B goes back; Y finds a game
(hall-finder.js). A stick inside
its dead zone reads exactly 0, so a resting, slightly worn stick never
drifts the walk or turns the view on its own. */
(function (root) {
Expand Down Expand Up @@ -41,7 +43,7 @@
}

function pad(gp) {
const out = {forward: 0, right: 0, run: false, lookX: 0, lookY: 0, a: false, b: false, select: false};
const out = {forward: 0, right: 0, run: false, lookX: 0, lookY: 0, a: false, b: false, y: false, select: false, up: false, down: false};
if (!gp || gp.connected === false) return out;
const [lx, ly, lm] = deadzone(axis(gp, 0), axis(gp, 1));
// The d-pad (standard buttons 12-15) walks too.
Expand All @@ -56,13 +58,17 @@
out.lookY = ry * Math.abs(ry);
out.a = pressed(gp, 0);
out.b = pressed(gp, 1);
out.y = pressed(gp, 3);
out.select = pressed(gp, 8);
out.up = out.forward > 0.5;
out.down = out.forward < -0.5;
return out;
}

function edges(prev, now) {
prev = prev || {};
return {a: !!now.a && !prev.a, b: !!now.b && !prev.b, select: !!now.select && !prev.select};
const went = (k) => !!now[k] && !prev[k];
return {a: went("a"), b: went("b"), y: went("y"), select: went("select"), up: went("up"), down: went("down")};
}

/* The first connected gamepad in a navigator.getGamepads() list (it has
Expand Down
197 changes: 197 additions & 0 deletions src/arcade/hall_assets/hall-finder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
"use strict";
/* "Find a game": the hall's search box, its "Continue playing" row and its
"Surprise me", the three ways into a game the list page (landing.py) has.
Picking one takes the visitor to that cabinet (hall.js __hall.go): a short
fade and they stand in front of it, as /hall/?at=<slug> starts them.

The pure part runs under node too (tests/browser/hall_finder_test.cjs):

HallFinder.match(cabinets, q) -> the cabinets whose search text has every word of q,
in the list's order (by title), as the list page's
search box finds them (filters.search_text, words ANDed)
HallFinder.recent(cabinets, slugs) -> the deployed cabinets named in slugs, in that order
HallFinder.readRecent(storage) -> the list's "arcade.recent" slugs (never throws)
HallFinder.surprise(pool, rnd, avoid)-> one deployed cabinet of pool, not `avoid` when there
is any other; null for none

Opened by the bar's Find button, F or / on a keyboard, Y on a controller.
In it: type to narrow, the arrow keys (or the d-pad) move, Enter (or A)
goes, Esc (or B) closes. */
(function (root) {
const RECENT_KEY = "arcade.recent";

const title = (c) => String(c.title || c.slug || "");
const byTitle = (a, b) => title(a).toLowerCase().localeCompare(title(b).toLowerCase()) || String(a.slug).localeCompare(String(b.slug));

function words(q) { return String(q || "").toLowerCase().split(/\s+/).filter(Boolean); }

function match(cabinets, q) {
const ws = words(q);
return (cabinets || []).filter((c) => {
const text = String(c.search || `${c.title || ""} ${c.slug || ""}`).toLowerCase();
return ws.every((w) => text.indexOf(w) >= 0);
}).sort(byTitle);
}

function recent(cabinets, slugs) {
const by = new Map((cabinets || []).map((c) => [c.slug, c]));
const out = [];
for (const s of slugs || []) {
const c = by.get(s);
if (c && c.deployed && out.indexOf(c) < 0) out.push(c);
}
return out;
}

function readRecent(storage) {
try {
const list = JSON.parse((storage && storage.getItem(RECENT_KEY)) || "[]");
return Array.isArray(list) ? list.filter((s) => typeof s === "string") : [];
} catch (e) { return []; }
}

function surprise(pool, rnd, avoid) {
let live = (pool || []).filter((c) => c.deployed);
if (live.length > 1 && avoid) live = live.filter((c) => c.slug !== avoid);
if (!live.length) return null;
const r = typeof rnd === "function" ? rnd() : Math.random();
return live[Math.min(live.length - 1, Math.floor(r * live.length))];
}

const HallFinder = {match, recent, readRecent, surprise, RECENT_KEY};
if (typeof module !== "undefined" && module.exports) { module.exports = HallFinder; return; }
root.HallFinder = HallFinder;

/* ------------------------------------------------------------ the panel */
const $ = (id) => document.getElementById(id);
const panel = $("finder"), input = $("finder-q"), list = $("finder-list"), button = $("find-btn");
if (!panel || !input || !list || !button) return;
const store = (() => { try { return window.localStorage; } catch (e) { return null; } })();
const state = {open: false, shown: [], at: -1, went: null};
let opener = null;

const hall = () => window.__hall;
const cabinets = () => (hall() && hall().layout ? hall().layout.cabinets : []);
// A filtered hall (?tag=halloween) offers its lit cabinets first, as its list would.
function lit(c) {
const f = hall() && hall().filter;
return !f || f.lit.indexOf(c.slug) >= 0;
}
const esc = (s) => String(s).replace(/[&<>"]/g, (ch) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;"}[ch]));

function row(c, i) {
const meta = [c.author ? `by ${c.author}` : "", c.deployed ? `${c.plays || 0} plays` : "Coming soon"].filter(Boolean).join(" · ");
return `<li role="none"><button type="button" role="option" id="finder-o${i}" data-slug="${esc(c.slug)}"`
+ ` aria-selected="false"${c.deployed ? "" : ' class="soon"'}>`
+ `<span class="sw" style="background:rgb(${(c.colour || [0.5, 0.5, 0.5]).map((v) => Math.round(v * 255)).join(",")})"></span>`
+ `<span class="t">${esc(title(c))}${c.new ? ' <span class="badge">New</span>' : ""}</span>`
+ `<span class="m">${esc(meta)}</span></button></li>`;
}

function render() {
const q = input.value;
const all = cabinets();
let html = "", shown = [];
if (!words(q).length) {
const back = recent(all, readRecent(store));
if (back.length) {
html += '<li role="presentation" class="h">Continue playing</li>';
back.forEach((c) => { html += row(c, shown.length); shown.push(c); });
}
html += `<li role="presentation" class="h">${hall() && hall().filter ? "In this hall" : "Every game"}</li>`;
}
const found = match(all, q);
const ordered = found.filter(lit).concat(found.filter((c) => !lit(c)));
ordered.forEach((c) => { html += row(c, shown.length); shown.push(c); });
if (!found.length) html += `<li role="presentation" class="none">No game matches “${esc(q.trim())}”.</li>`;
list.innerHTML = html;
state.shown = shown;
$("finder-count").textContent = words(q).length ? `${found.length} found` : `${all.length} games`;
select(shown.length ? 0 : -1, false);
}

function select(i, scroll = true) {
const prev = list.querySelector('[aria-selected="true"]');
if (prev) prev.setAttribute("aria-selected", "false");
state.at = i;
const el = i >= 0 ? $(`finder-o${i}`) : null;
if (el) {
el.setAttribute("aria-selected", "true");
input.setAttribute("aria-activedescendant", el.id);
if (scroll) el.scrollIntoView({block: "nearest"});
} else input.removeAttribute("aria-activedescendant");
}
function move(d) {
if (!state.shown.length) return;
select(Math.max(0, Math.min(state.shown.length - 1, state.at + d)));
}

function open(on) {
if (on === state.open) return;
state.open = on;
panel.hidden = !on;
button.setAttribute("aria-expanded", String(on));
document.body.classList.toggle("finding", on);
if (on) {
opener = document.activeElement;
input.value = "";
render();
// Not on a touch screen at once: the keyboard would cover the list.
if (!matchMedia("(hover: none), (pointer: coarse)").matches) input.focus({preventScroll: true});
else panel.focus({preventScroll: true});
} else {
const back = opener && opener !== document.body && document.contains(opener) ? opener : $("hall");
if (back) back.focus({preventScroll: true});
}
}

function go(c) {
if (!c || !hall()) return;
open(false);
state.went = c.slug;
hall().go(c.slug);
}

button.addEventListener("click", () => open(!state.open));
$("finder-close").addEventListener("click", () => open(false));
$("finder-random").addEventListener("click", () => {
const pool = match(cabinets(), input.value).filter(lit);
go(surprise(pool.length ? pool : cabinets(), Math.random, hall() && hall().near));
});
input.addEventListener("input", render);
list.addEventListener("click", (e) => {
const b = e.target.closest("button[data-slug]");
if (b) go(cabinets().find((c) => c.slug === b.getAttribute("data-slug")));
});
panel.addEventListener("keydown", (e) => {
const k = e.key;
if (k === "Escape") { open(false); e.preventDefault(); e.stopPropagation(); return; }
if (k === "ArrowDown" || k === "ArrowUp") { move(k === "ArrowDown" ? 1 : -1); e.preventDefault(); e.stopPropagation(); return; }
if (k === "Enter" && document.activeElement === input) { go(state.shown[state.at]); e.preventDefault(); e.stopPropagation(); }
// Every other key is the panel's (typing), not the hall's walking.
e.stopPropagation();
});
// F or / anywhere in the hall (not while typing elsewhere) opens it.
window.addEventListener("keydown", (e) => {
if (state.open || e.ctrlKey || e.metaKey || e.altKey || e.repeat) return;
if (e.target && /^(INPUT|SELECT|TEXTAREA)$/.test(e.target.tagName)) return;
if (hall() && hall().playing) return;
if (e.key === "/" || e.key.toLowerCase() === "f") { open(true); e.preventDefault(); e.stopPropagation(); }
}, true);
// A click (or tap) outside the panel closes it, except on the button that toggles it.
document.addEventListener("pointerdown", (e) => {
if (state.open && !panel.contains(e.target) && !button.contains(e.target)) open(false);
}, true);

/* The controller, from hall.js each frame while the panel is open (or Y to
open it): {y, a, b, up, down} are presses this frame. */
function pad(p) {
if (p.y) { open(!state.open); return; }
if (!state.open) return;
if (p.b) open(false);
else if (p.a) go(state.shown[state.at]);
else if (p.up || p.down) move(p.down ? 1 : -1);
}

window.__hallFinder = {open, pad, state, get isOpen() { return state.open; }};
})(typeof window !== "undefined" ? window : globalThis);
59 changes: 59 additions & 0 deletions src/arcade/hall_assets/hall.css
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,62 @@ body.touch #bar #visible-pill { min-height: 44px; min-width: 44px; }
body.touch #bar #leave, body.touch #bar #sound { width: 44px; min-height: 44px; }
}
/* (The line saying where you are shows no text up to 480 px: see #bar #look above.) */

/* "Find a game" (hall-finder.js): the bar's magnifier, F, / or Y. A panel under
the bar with a search box, "Continue playing", every game and "Surprise me";
picking one takes the visitor to its cabinet. On a phone it fills the width
and the stick, card and play button step aside while it is open. */
#bar #find-btn { gap: 6px; }
#bar #find-btn svg { flex: none; }
@media (max-width: 480px) {
#bar { gap: 6px; }
#bar #find-btn, #bar #sound { padding: 0; width: 40px; justify-content: center; }
#bar #find-btn .label, #bar #sound .label { display: none; }
#bar #sound svg { display: block; }
body.touch #bar #find-btn, body.touch #bar #sound { width: 44px; min-height: 44px; }
/* With the pill too, "All games" is its arrow (its name stays, as aria-label). */
body.visible-me #bar #leave { padding: 0; width: 44px; justify-content: center; }
body.visible-me #bar #leave .label { display: none; }
}
/* Under 390 px there is no room for seven: with the pill, "All games" leaves the
bar for the finder ("Every game as a list"). */
@media (max-width: 389px) { body.visible-me #bar #leave { display: none; } }
#finder { position: fixed; z-index: 4; top: calc(max(8px, env(safe-area-inset-top)) + 52px);
right: max(12px, env(safe-area-inset-right)); width: min(420px, calc(100vw - 24px));
max-height: calc(100dvh - max(8px, env(safe-area-inset-top)) - 52px - max(16px, env(safe-area-inset-bottom)));
display: flex; flex-direction: column; gap: 8px; padding: 10px; border-radius: 12px; background: rgba(16, 12, 30, .92);
border: 1px solid var(--line); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); outline: none; }
#finder[hidden] { display: none; }
#finder .top { display: flex; gap: 8px; }
#finder-q { flex: 1; min-width: 0; min-height: 44px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line);
background: rgba(0, 0, 0, .35); color: var(--fg); font: inherit; font-size: 16px; }
#finder-q:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
#finder-close { flex: none; width: 44px; min-height: 44px; border-radius: 10px; border: 1px solid var(--line);
background: transparent; color: var(--fg); font-size: 22px; cursor: pointer; }
#finder .row { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
#finder-count { flex: 1; }
#finder-list-link { color: var(--muted); }
body:not(.visible-me) #finder-list-link { display: none; }
@media (min-width: 390px) { #finder-list-link { display: none; } }
#finder-random { min-height: 40px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--accent);
background: transparent; color: var(--fg); font-weight: 600; cursor: pointer; }
body.touch #finder-random { min-height: 44px; }
#finder-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; overscroll-behavior: contain; min-height: 0; }
#finder-list .h { padding: 8px 6px 4px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }
#finder-list .none { padding: 12px 6px; color: var(--muted); }
#finder-list button { display: grid; grid-template-columns: 10px 1fr; grid-template-rows: auto auto; column-gap: 10px;
width: 100%; min-height: 48px; padding: 6px 8px; border: 0; border-radius: 8px; background: transparent;
color: var(--fg); text-align: left; cursor: pointer; }
#finder-list button .sw { grid-row: 1 / 3; align-self: stretch; border-radius: 3px; }
#finder-list button .t { font-weight: 600; }
#finder-list button .m { color: var(--muted); font-size: 12px; }
#finder-list button.soon { opacity: .6; }
#finder-list button:hover, #finder-list button[aria-selected="true"] { background: rgba(169, 139, 255, .18); }
#finder-list button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
#finder-list .badge { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 999px; background: #ffd23f;
color: #1a1400; font-size: 11px; font-weight: 700; vertical-align: 1px; }
@media (max-width: 480px) { #finder { left: max(8px, env(safe-area-inset-left)); right: max(8px, env(safe-area-inset-right)); width: auto; } }
body.finding #stick, body.finding #prompt, body.touch.finding #card, body.finding #map { visibility: hidden; }
/* A slow device's bar sits at the bottom: the list ends above it. */
body:has(.sk-slow) #finder { max-height: calc(100dvh - max(8px, env(safe-area-inset-top)) - 52px - max(24px, env(safe-area-inset-bottom)) - 76px); }
body.touch:has(.sk-slow) #finder { max-height: calc(100dvh - max(8px, env(safe-area-inset-top)) - 52px - max(24px, env(safe-area-inset-bottom)) - 150px); }
Loading
Loading