@hadoku/ui-shell 0.0.0-stage → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/app-id/index.d.ts +4 -0
- package/dist/app-id/index.js +14 -0
- package/dist/chrome/AppChrome.d.ts +9 -0
- package/dist/chrome/AppChrome.js +201 -0
- package/dist/chrome/chrome.css +336 -0
- package/dist/chrome/index.d.ts +3 -0
- package/dist/chrome/index.js +3 -0
- package/dist/chrome/matchMedia.d.ts +2 -0
- package/dist/chrome/matchMedia.js +51 -0
- package/dist/chrome/shell.d.ts +17 -0
- package/dist/chrome/shell.js +47 -0
- package/dist/chrome/trim.css +334 -0
- package/dist/components/Reveal.d.ts +20 -0
- package/dist/components/Reveal.js +38 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +7 -0
- package/dist/components/reveal.css +110 -0
- package/dist/embed/index.d.ts +1 -0
- package/dist/embed/index.js +1 -0
- package/dist/embed/styles.d.ts +4 -0
- package/dist/embed/styles.js +49 -0
- package/dist/motion/index.d.ts +6 -0
- package/dist/motion/index.js +8 -0
- package/dist/settings/index.d.ts +1 -0
- package/dist/settings/index.js +1 -0
- package/dist/settings/ui-scale.d.ts +45 -0
- package/dist/settings/ui-scale.js +142 -0
- package/dist/sound/GainSlider.d.ts +6 -0
- package/dist/sound/GainSlider.js +15 -0
- package/dist/sound/index.d.ts +65 -0
- package/dist/sound/index.js +204 -0
- package/dist/sound/mix.d.ts +69 -0
- package/dist/sound/mix.js +117 -0
- package/dist/sound/mixer.d.ts +37 -0
- package/dist/sound/mixer.js +301 -0
- package/dist/sound/mute.d.ts +62 -0
- package/dist/sound/mute.js +250 -0
- package/dist/sound/queue.d.ts +32 -0
- package/dist/sound/queue.js +80 -0
- package/dist/theme/index.d.ts +18 -0
- package/dist/theme/index.js +41 -0
- package/dist/trim/index.d.ts +2 -0
- package/dist/trim/index.js +2 -0
- package/dist/trim/siteTrim.d.ts +23 -0
- package/dist/trim/siteTrim.js +91 -0
- package/dist/trim/trim.d.ts +72 -0
- package/dist/trim/trim.js +63 -0
- package/dist/usage/index.d.ts +32 -0
- package/dist/usage/index.js +84 -0
- package/package.json +70 -4
- package/README.md +0 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the viewer asked for less motion. Any game's board can read this
|
|
3
|
+
* without caring where the question lives; cardtable's own `play/motion.ts`
|
|
4
|
+
* re-exports it so none of its 20+ importers moved.
|
|
5
|
+
*/
|
|
6
|
+
export function prefersReducedMotion() {
|
|
7
|
+
return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ui-scale.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ui-scale.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** The range, in steps of 5%. Under 80% the text (11px authored) drops below ~9px; over 125% the
|
|
2
|
+
* side column and the prompt bars take so much of a 1280×800 table that the battlefields fall to
|
|
3
|
+
* their two-row minimum before a game has begun. */
|
|
4
|
+
export declare const UI_SCALE: {
|
|
5
|
+
readonly min: 0.8;
|
|
6
|
+
readonly max: 1.25;
|
|
7
|
+
readonly step: 0.05;
|
|
8
|
+
};
|
|
9
|
+
/** A saved or dragged value, inside the range and on a step; anything unreadable is 1. */
|
|
10
|
+
export declare function clampUiScale(value: number): number;
|
|
11
|
+
export declare function scaleCssValue(value: string): string;
|
|
12
|
+
/** A length a component writes inline (an icon's `size`), in the same units the stylesheets are rewritten to. */
|
|
13
|
+
export declare function uiLength(length: `${number}${'px' | 'rem'}`): string;
|
|
14
|
+
interface CssNode {
|
|
15
|
+
type?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
parent?: unknown;
|
|
18
|
+
}
|
|
19
|
+
/** The PostCSS plugin both builds run (`vite.config.ts`, `vite.embed.config.ts`). Idempotent. */
|
|
20
|
+
export declare const uiScaleCss: {
|
|
21
|
+
postcssPlugin: string;
|
|
22
|
+
Declaration(decl: {
|
|
23
|
+
prop: string;
|
|
24
|
+
value: string;
|
|
25
|
+
parent?: CssNode;
|
|
26
|
+
source?: {
|
|
27
|
+
input: {
|
|
28
|
+
file?: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}): void;
|
|
32
|
+
};
|
|
33
|
+
/** The element our tokens resolve on: the shadow host when `node` is inside one, else the document's root. */
|
|
34
|
+
export declare function scaleRootOf(node: Node): HTMLElement;
|
|
35
|
+
/**
|
|
36
|
+
* A length a component computes with, authored in px at 100% as its stylesheet would be, as the
|
|
37
|
+
* stylesheets draw it now: times the scale on `node`'s root. Every px constant a layout measures
|
|
38
|
+
* against reads through this, so it follows the slider with the CSS it stands beside.
|
|
39
|
+
*/
|
|
40
|
+
export declare function uiPx(px: number, node?: Node | null): number;
|
|
41
|
+
export declare function rememberedUiScale(): number | null;
|
|
42
|
+
export declare function rememberUiScale(scale: number): void;
|
|
43
|
+
/** Write the scale on `root`; at 1 nothing is written, and the stylesheet's own default stands. */
|
|
44
|
+
export declare function applyUiScale(root: HTMLElement, scale: number): void;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { appId } from "../app-id/index.js";
|
|
2
|
+
/*
|
|
3
|
+
* The UI scale (the operator, 2026-10-21: "add a slider to allow scaling the size of the UI").
|
|
4
|
+
*
|
|
5
|
+
* ONE number, `--ui-scale`, and every length the app's own stylesheets author is a multiple of it:
|
|
6
|
+
* `uiScaleCss` rewrites each `Npx` as `calc(N * var(--ui-px))` and each `Nrem` as
|
|
7
|
+
* `calc(N * var(--ui-rem))` at build time, and `styles.css` defines the two units from the scale.
|
|
8
|
+
* Everything the board MEASURES (`play/zone-box.ts`, `play/card-size.ts`) follows from those
|
|
9
|
+
* lengths, so a card, a column, a prompt and a font all scale together and the fit-to-the-window
|
|
10
|
+
* rules that size the table go on fitting it.
|
|
11
|
+
*
|
|
12
|
+
* Not CSS `zoom` and not a `transform` on the root: either one puts the page's boxes in a
|
|
13
|
+
* different space from the px its gestures write back (`getBoundingClientRect()` is zoomed, a
|
|
14
|
+
* `left: …px` written from it is zoomed again), and a hundred places on the board do exactly that.
|
|
15
|
+
* Here a px is a px: the scale changes what the stylesheet asks for, not the coordinate space.
|
|
16
|
+
*
|
|
17
|
+
* Where it is written: the element our tokens resolve on — the document's root standalone, the
|
|
18
|
+
* shadow host embedded (the embed build rewrites `:root` as `:host`). A custom property's `var()`
|
|
19
|
+
* is resolved where it is declared, so a scale written any lower would miss every token `:root`
|
|
20
|
+
* declares.
|
|
21
|
+
*/
|
|
22
|
+
/** The range, in steps of 5%. Under 80% the text (11px authored) drops below ~9px; over 125% the
|
|
23
|
+
* side column and the prompt bars take so much of a 1280×800 table that the battlefields fall to
|
|
24
|
+
* their two-row minimum before a game has begun. */
|
|
25
|
+
export const UI_SCALE = { min: 0.8, max: 1.25, step: 0.05 };
|
|
26
|
+
/** A saved or dragged value, inside the range and on a step; anything unreadable is 1. */
|
|
27
|
+
export function clampUiScale(value) {
|
|
28
|
+
if (!Number.isFinite(value))
|
|
29
|
+
return 1;
|
|
30
|
+
const stepped = Math.round(value / UI_SCALE.step) * UI_SCALE.step;
|
|
31
|
+
return Math.round(Math.min(UI_SCALE.max, Math.max(UI_SCALE.min, stepped)) * 100) / 100;
|
|
32
|
+
}
|
|
33
|
+
/** Strings and `url(…)`s pass through untouched; only what is between them is rewritten. */
|
|
34
|
+
const OPAQUE = /("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|url\([^)]*\))/;
|
|
35
|
+
/** A px or rem length that is a whole token: not part of a name (`--gap-4px`), a colour or a number. */
|
|
36
|
+
const LENGTH = /(?<![\w.#-])(-?)(\d+\.?\d*|\.\d+)(px|rem)(?![\w%-])/g;
|
|
37
|
+
export function scaleCssValue(value) {
|
|
38
|
+
return value
|
|
39
|
+
.split(OPAQUE)
|
|
40
|
+
.map((part, i) => i % 2 === 1
|
|
41
|
+
? part
|
|
42
|
+
: part.replace(LENGTH, (whole, sign, n, unit) => Number(n) === 0 ? whole : `calc(${sign}${n} * var(--ui-${unit}))`))
|
|
43
|
+
.join('');
|
|
44
|
+
}
|
|
45
|
+
/** A length a component writes inline (an icon's `size`), in the same units the stylesheets are rewritten to. */
|
|
46
|
+
export function uiLength(length) {
|
|
47
|
+
return scaleCssValue(length);
|
|
48
|
+
}
|
|
49
|
+
/** The units and the scale are defined in px and rem; they are the one place that may be. */
|
|
50
|
+
const OWN_UNITS = new Set(['--ui-scale', '--ui-px', '--ui-rem']);
|
|
51
|
+
/** Descriptors, not properties: they take no `var()`. */
|
|
52
|
+
const DESCRIPTOR_RULES = new Set(['property', 'font-face', 'page', 'counter-style', 'font-palette-values']);
|
|
53
|
+
function inAtRule(node, names) {
|
|
54
|
+
for (let n = node; n !== undefined; n = n.parent) {
|
|
55
|
+
if (n.type === 'atrule' && n.name !== undefined && names.has(n.name.toLowerCase()))
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const KEYFRAMES = new Set(['keyframes']);
|
|
61
|
+
/** Only the app's own sheets: a workspace package's `src` counts the same as `apps/<app>/src`, and so does a
|
|
62
|
+
* game's (`games/<game>/ui/src`, `games/<game>/packages/<pkg>/src`) — never the fleet's chrome or the symbol
|
|
63
|
+
* packs' vendor CSS, which keep their authored sizes. */
|
|
64
|
+
function ownSheet(file) {
|
|
65
|
+
return file !== undefined && OWN_SRC.test(file) && !/[\\/]node_modules[\\/]/.test(file);
|
|
66
|
+
}
|
|
67
|
+
const OWN_SRC = /[\\/](?:apps[\\/][^\\/]+|packages[\\/][^\\/]+|games[\\/][^\\/]+[\\/](?:ui|packages[\\/][^\\/]+))[\\/]src[\\/]/;
|
|
68
|
+
/** The PostCSS plugin both builds run (`vite.config.ts`, `vite.embed.config.ts`). Idempotent. */
|
|
69
|
+
export const uiScaleCss = {
|
|
70
|
+
postcssPlugin: 'cardtable-ui-scale',
|
|
71
|
+
Declaration(decl) {
|
|
72
|
+
if (OWN_UNITS.has(decl.prop) || !ownSheet(decl.source?.input.file) || inAtRule(decl.parent, DESCRIPTOR_RULES))
|
|
73
|
+
return;
|
|
74
|
+
const next = scaleCssValue(decl.value);
|
|
75
|
+
if (next === decl.value)
|
|
76
|
+
return;
|
|
77
|
+
// Firefox plays a registered property's keyframes that hold a `var()` forwards on every
|
|
78
|
+
// iteration, never back, so an `alternate` loop snaps from its end to its start (`card.css`'s
|
|
79
|
+
// flyers, the operator, 2026-10-23). Such a keyframe animates a plain number, and the length is
|
|
80
|
+
// made where it is drawn (`calc(var(--x) * 1px)`), which this rewrites like any other.
|
|
81
|
+
if (decl.prop.startsWith('--') && inAtRule(decl.parent, KEYFRAMES)) {
|
|
82
|
+
throw new Error(`${decl.source?.input.file ?? 'a stylesheet'}: a keyframe sets ${decl.prop} to a length (${decl.value}); animate a number and multiply by 1px where it is used`);
|
|
83
|
+
}
|
|
84
|
+
decl.value = next;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
/** The element our tokens resolve on: the shadow host when `node` is inside one, else the document's root. */
|
|
88
|
+
export function scaleRootOf(node) {
|
|
89
|
+
const root = node.getRootNode();
|
|
90
|
+
if (typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot && root.host instanceof HTMLElement)
|
|
91
|
+
return root.host;
|
|
92
|
+
return node.ownerDocument?.documentElement ?? document.documentElement;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A length a component computes with, authored in px at 100% as its stylesheet would be, as the
|
|
96
|
+
* stylesheets draw it now: times the scale on `node`'s root. Every px constant a layout measures
|
|
97
|
+
* against reads through this, so it follows the slider with the CSS it stands beside.
|
|
98
|
+
*/
|
|
99
|
+
export function uiPx(px, node) {
|
|
100
|
+
const at = node ?? (typeof document === 'undefined' ? null : document.documentElement);
|
|
101
|
+
if (at == null)
|
|
102
|
+
return px;
|
|
103
|
+
const scale = parseFloat(getComputedStyle(scaleRootOf(at)).getPropertyValue('--ui-scale'));
|
|
104
|
+
return Number.isFinite(scale) && scale > 0 ? px * scale : px;
|
|
105
|
+
}
|
|
106
|
+
/** Per game (`<appId>:ui-scale`); for cardtable, the literal key stays `cardtable:ui-scale`. */
|
|
107
|
+
function rememberedKey() {
|
|
108
|
+
return `${appId()}:ui-scale`;
|
|
109
|
+
}
|
|
110
|
+
/*
|
|
111
|
+
* The scale last applied in this browser, so the first frame is drawn at it. The saved scale is only
|
|
112
|
+
* known once prefs have answered (an identity read, then the prefs read), and a page drawn at 100%
|
|
113
|
+
* until then grew a tenth for a player at 110% as its content landed — the operator's "zoom in" on the
|
|
114
|
+
* front page (2026-11-01, `e2e/front-skeleton.spec.ts`). `null` when nothing is remembered.
|
|
115
|
+
* Wrapped as `../front/shape.ts` is: a private window or a blocked store still draws the page.
|
|
116
|
+
*/
|
|
117
|
+
export function rememberedUiScale() {
|
|
118
|
+
try {
|
|
119
|
+
const raw = globalThis.localStorage?.getItem(rememberedKey()) ?? null;
|
|
120
|
+
const value = raw === null ? Number.NaN : Number(raw);
|
|
121
|
+
return Number.isFinite(value) ? clampUiScale(value) : null;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export function rememberUiScale(scale) {
|
|
128
|
+
try {
|
|
129
|
+
globalThis.localStorage?.setItem(rememberedKey(), String(clampUiScale(scale)));
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// Not remembered: the next page is drawn at 100% until prefs answer, as it was before.
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** Write the scale on `root`; at 1 nothing is written, and the stylesheet's own default stands. */
|
|
136
|
+
export function applyUiScale(root, scale) {
|
|
137
|
+
const value = clampUiScale(scale);
|
|
138
|
+
if (value === 1)
|
|
139
|
+
root.style.removeProperty('--ui-scale');
|
|
140
|
+
else
|
|
141
|
+
root.style.setProperty('--ui-scale', String(value));
|
|
142
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/*
|
|
3
|
+
* A cell's gain, 0..2 (1 is as recorded). It moves freely while dragged and
|
|
4
|
+
* commits once on release, so a drag is one save rather than fifty.
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect, useState } from 'react';
|
|
7
|
+
export function GainSlider({ label, value, onCommit }) {
|
|
8
|
+
const [local, setLocal] = useState(value);
|
|
9
|
+
useEffect(() => setLocal(value), [value]);
|
|
10
|
+
const commit = () => {
|
|
11
|
+
if (local !== value)
|
|
12
|
+
onCommit(local);
|
|
13
|
+
};
|
|
14
|
+
return (_jsxs("span", { className: "sound-gain", children: [_jsx("input", { type: "range", min: 0, max: 2, step: 0.05, value: local, "aria-label": `${label} gain`, onChange: (e) => setLocal(Number(e.target.value)), onPointerUp: commit, onKeyUp: commit, onBlur: commit }), _jsx("output", { children: local.toFixed(2) })] }));
|
|
15
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export * from './mix.js';
|
|
2
|
+
export * from './mixer.js';
|
|
3
|
+
export * from './mute.js';
|
|
4
|
+
export * from './queue.js';
|
|
5
|
+
export { GainSlider, type GainSliderProps } from './GainSlider.js';
|
|
6
|
+
import { type RoleRegistry, type SoundEvent } from './mix.js';
|
|
7
|
+
import { type MusicChoice } from './mixer.js';
|
|
8
|
+
export interface RolePlayer {
|
|
9
|
+
play(event: SoundEvent): unknown;
|
|
10
|
+
loop?(role: MusicChoice, pick?: (role: string) => string | null): void;
|
|
11
|
+
nowPlaying?(): {
|
|
12
|
+
role: string;
|
|
13
|
+
file: string;
|
|
14
|
+
} | null;
|
|
15
|
+
/** A cell's takes, as assigned. */
|
|
16
|
+
takes?(cell: string): readonly string[];
|
|
17
|
+
}
|
|
18
|
+
/** What loops now, and every track its role could play instead: sound settings' track picker. */
|
|
19
|
+
export interface MusicTrack {
|
|
20
|
+
role: string;
|
|
21
|
+
file: string;
|
|
22
|
+
tracks: readonly {
|
|
23
|
+
file: string;
|
|
24
|
+
name: string;
|
|
25
|
+
}[];
|
|
26
|
+
}
|
|
27
|
+
/** A sound event that starts a little after the moment it belongs to. */
|
|
28
|
+
export interface DelayedSoundEvent extends SoundEvent {
|
|
29
|
+
delayMs?: number;
|
|
30
|
+
}
|
|
31
|
+
/** What a game's server answers for role-cell assignments: cells, gains, music-take names, `takeUrl`-pinning versions, and the cue pack's version (empty: no cues). */
|
|
32
|
+
export interface SoundAssignments {
|
|
33
|
+
cells: Readonly<Record<string, readonly string[]>>;
|
|
34
|
+
gains: Readonly<Record<string, number>>;
|
|
35
|
+
names: Readonly<Record<string, string>>;
|
|
36
|
+
versions: Readonly<Record<string, string>>;
|
|
37
|
+
pack: string;
|
|
38
|
+
}
|
|
39
|
+
/** What `createSound` needs from the game's server. */
|
|
40
|
+
export interface SoundClient {
|
|
41
|
+
takeUrl(file: string, version?: string): string;
|
|
42
|
+
loadAssignments(): Promise<SoundAssignments>;
|
|
43
|
+
loadPack(pack: string): Promise<ReadonlyMap<string, ArrayBuffer> | undefined>;
|
|
44
|
+
}
|
|
45
|
+
export interface SoundConfig<R extends string> {
|
|
46
|
+
registry: RoleRegistry;
|
|
47
|
+
client: SoundClient;
|
|
48
|
+
/** `ui`-bus roles already logged one by one elsewhere: left out of `takeUiTally`. Default: none. */
|
|
49
|
+
individuallyLogged?: ReadonlySet<R>;
|
|
50
|
+
}
|
|
51
|
+
/** A game's sound, wired to its own role registry and server client. */
|
|
52
|
+
export declare function createSound<R extends string>(config: SoundConfig<R>): {
|
|
53
|
+
setRolePlayer: (player: RolePlayer | null) => void;
|
|
54
|
+
resetRolePlayerForTests: () => void;
|
|
55
|
+
takeUiTally: () => Record<string, number>;
|
|
56
|
+
holdMatchQuiet: () => () => void;
|
|
57
|
+
playRole: (role: R) => void;
|
|
58
|
+
playSoundEvents: (events: readonly DelayedSoundEvent[]) => void;
|
|
59
|
+
setTrackNames: (names: Readonly<Record<string, string>>) => void;
|
|
60
|
+
getMusicTrack: () => MusicTrack | null;
|
|
61
|
+
subscribeMusicTrack: (fn: () => void) => () => void;
|
|
62
|
+
setScreenMusic: (role: R | null) => void;
|
|
63
|
+
setMatchMusic: (roles: readonly R[] | null) => void;
|
|
64
|
+
roleHandler: (role: R) => () => void;
|
|
65
|
+
};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The generic sound mixer — WebAudio playback, the mix planner, the mute
|
|
3
|
+
* store, the queue's mechanics and the gain slider, re-exported below, no
|
|
4
|
+
* role names among them (`./mix.ts`'s header says why) — plus
|
|
5
|
+
* `createSound<R>`, which a game wires its own role registry and server
|
|
6
|
+
* client into: `R` outside it is a compile error on `playRole`; a role the
|
|
7
|
+
* registry does not hold plays nothing at runtime regardless — the
|
|
8
|
+
* planner's own `unknown-role` skip, never re-checked here.
|
|
9
|
+
*/
|
|
10
|
+
export * from "./mix.js";
|
|
11
|
+
export * from "./mixer.js";
|
|
12
|
+
export * from "./mute.js";
|
|
13
|
+
export * from "./queue.js";
|
|
14
|
+
export { GainSlider } from "./GainSlider.js";
|
|
15
|
+
import { configureSoundRoles } from "./mix.js";
|
|
16
|
+
import { createMixer } from "./mixer.js";
|
|
17
|
+
import { getLevel, getMusicPick, isMuted, subscribeSound } from "./mute.js";
|
|
18
|
+
/** Where there is no WebAudio (jsdom, an old browser) nothing is built and nothing is fetched. */
|
|
19
|
+
const SILENT = { play: () => undefined };
|
|
20
|
+
function hasWebAudio() {
|
|
21
|
+
const g = globalThis;
|
|
22
|
+
return g.AudioContext !== undefined || g.webkitAudioContext !== undefined;
|
|
23
|
+
}
|
|
24
|
+
/** A game's sound, wired to its own role registry and server client. */
|
|
25
|
+
export function createSound(config) {
|
|
26
|
+
configureSoundRoles(config.registry);
|
|
27
|
+
const { registry, client } = config;
|
|
28
|
+
const individuallyLogged = config.individuallyLogged ?? new Set();
|
|
29
|
+
let injected = null;
|
|
30
|
+
let production = null;
|
|
31
|
+
/** The screen's music (`null`: a silent screen), and the match's while one is on it. */
|
|
32
|
+
let screenMusic = null;
|
|
33
|
+
let matchMusic = null;
|
|
34
|
+
/** Holds out against a match's own sound: while any is held, its moments and music stay off, the page's `ui` cues still play. Never persisted. */
|
|
35
|
+
let quietHolds = 0;
|
|
36
|
+
let unsubscribeSound = null;
|
|
37
|
+
let trackNames = {};
|
|
38
|
+
let musicTrack = null;
|
|
39
|
+
const trackListeners = new Set();
|
|
40
|
+
const uiTally = new Map();
|
|
41
|
+
function setRolePlayer(player) {
|
|
42
|
+
injected = player;
|
|
43
|
+
}
|
|
44
|
+
function resetRolePlayerForTests() {
|
|
45
|
+
injected = null;
|
|
46
|
+
production = null;
|
|
47
|
+
screenMusic = null;
|
|
48
|
+
matchMusic = null;
|
|
49
|
+
quietHolds = 0;
|
|
50
|
+
trackNames = {};
|
|
51
|
+
musicTrack = null;
|
|
52
|
+
trackListeners.clear();
|
|
53
|
+
uiTally.clear();
|
|
54
|
+
unsubscribeSound?.();
|
|
55
|
+
unsubscribeSound = null;
|
|
56
|
+
}
|
|
57
|
+
function player() {
|
|
58
|
+
if (injected !== null)
|
|
59
|
+
return injected;
|
|
60
|
+
if (production === null) {
|
|
61
|
+
if (!hasWebAudio())
|
|
62
|
+
return SILENT;
|
|
63
|
+
let versions = {};
|
|
64
|
+
const mixer = createMixer((f) => client.takeUrl(f, versions[f]));
|
|
65
|
+
production = mixer;
|
|
66
|
+
applyLevels();
|
|
67
|
+
unsubscribeSound ??= subscribeSound(applyPrefs);
|
|
68
|
+
void client.loadAssignments().then(async (a) => {
|
|
69
|
+
versions = a.versions;
|
|
70
|
+
mixer.planner.setAssignments(a.cells, a.gains);
|
|
71
|
+
setTrackNames(a.names);
|
|
72
|
+
applyMusic();
|
|
73
|
+
mixer.preload(await client.loadPack(a.pack));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return production;
|
|
77
|
+
}
|
|
78
|
+
function takeUiTally() {
|
|
79
|
+
const out = Object.fromEntries(uiTally);
|
|
80
|
+
uiTally.clear();
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
function playEvent(event) {
|
|
84
|
+
if (registry.roles.get(event.role)?.bus === 'ui' && !individuallyLogged.has(event.role)) {
|
|
85
|
+
uiTally.set(event.role, (uiTally.get(event.role) ?? 0) + 1);
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
if (isMuted())
|
|
89
|
+
return;
|
|
90
|
+
player().play(event);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// Sound is decoration. Nothing it does is worth breaking a click over.
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function holdMatchQuiet() {
|
|
97
|
+
quietHolds += 1;
|
|
98
|
+
applyMusic();
|
|
99
|
+
let released = false;
|
|
100
|
+
return () => {
|
|
101
|
+
if (released)
|
|
102
|
+
return;
|
|
103
|
+
released = true;
|
|
104
|
+
quietHolds -= 1;
|
|
105
|
+
applyMusic();
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function playRole(role) {
|
|
109
|
+
if (quietHolds > 0 && registry.roles.get(role)?.bus !== 'ui')
|
|
110
|
+
return;
|
|
111
|
+
playEvent({ role });
|
|
112
|
+
}
|
|
113
|
+
function playSoundEvents(events) {
|
|
114
|
+
if (quietHolds > 0)
|
|
115
|
+
return;
|
|
116
|
+
for (const { delayMs, ...event } of events) {
|
|
117
|
+
if (delayMs === undefined || delayMs <= 0)
|
|
118
|
+
playEvent(event);
|
|
119
|
+
else
|
|
120
|
+
setTimeout(() => playEvent(event), delayMs);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function applyLevels() {
|
|
124
|
+
production?.setLevels({ volume: getLevel('volume'), music: getLevel('music') });
|
|
125
|
+
}
|
|
126
|
+
function applyPrefs() {
|
|
127
|
+
applyLevels();
|
|
128
|
+
applyMusic();
|
|
129
|
+
}
|
|
130
|
+
function applyMusic() {
|
|
131
|
+
try {
|
|
132
|
+
const wanted = screenMusic === null ? null : ((quietHolds > 0 ? null : matchMusic) ?? screenMusic);
|
|
133
|
+
const p = player();
|
|
134
|
+
p.loop?.(isMuted() ? null : wanted, getMusicPick);
|
|
135
|
+
publishTrack(p);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// Music is decoration too.
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function publishTrack(p) {
|
|
142
|
+
const now = p.nowPlaying?.() ?? null;
|
|
143
|
+
const files = now === null ? [] : (p.takes?.(`${now.role}|play`) ?? [now.file]);
|
|
144
|
+
const next = now === null ? null : { ...now, tracks: files.map((file) => ({ file, name: trackNames[file] ?? file.split('/')[0] })) };
|
|
145
|
+
const same = next === musicTrack ||
|
|
146
|
+
(next !== null &&
|
|
147
|
+
musicTrack !== null &&
|
|
148
|
+
next.role === musicTrack.role &&
|
|
149
|
+
next.file === musicTrack.file &&
|
|
150
|
+
next.tracks.length === musicTrack.tracks.length &&
|
|
151
|
+
next.tracks.every((t, i) => t.file === musicTrack.tracks[i].file && t.name === musicTrack.tracks[i].name));
|
|
152
|
+
if (same)
|
|
153
|
+
return;
|
|
154
|
+
musicTrack = next;
|
|
155
|
+
for (const fn of trackListeners)
|
|
156
|
+
fn();
|
|
157
|
+
}
|
|
158
|
+
function setTrackNames(names) {
|
|
159
|
+
trackNames = names;
|
|
160
|
+
if (production !== null || injected !== null)
|
|
161
|
+
publishTrack(player());
|
|
162
|
+
}
|
|
163
|
+
function getMusicTrack() {
|
|
164
|
+
return musicTrack;
|
|
165
|
+
}
|
|
166
|
+
function subscribeMusicTrack(fn) {
|
|
167
|
+
trackListeners.add(fn);
|
|
168
|
+
return () => {
|
|
169
|
+
trackListeners.delete(fn);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function setScreenMusic(role) {
|
|
173
|
+
screenMusic = role;
|
|
174
|
+
unsubscribeSound ??= subscribeSound(applyPrefs);
|
|
175
|
+
applyMusic();
|
|
176
|
+
}
|
|
177
|
+
function setMatchMusic(roles) {
|
|
178
|
+
matchMusic = roles;
|
|
179
|
+
applyMusic();
|
|
180
|
+
}
|
|
181
|
+
const handlers = new Map();
|
|
182
|
+
function roleHandler(role) {
|
|
183
|
+
const existing = handlers.get(role);
|
|
184
|
+
if (existing !== undefined)
|
|
185
|
+
return existing;
|
|
186
|
+
const fn = () => playRole(role);
|
|
187
|
+
handlers.set(role, fn);
|
|
188
|
+
return fn;
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
setRolePlayer,
|
|
192
|
+
resetRolePlayerForTests,
|
|
193
|
+
takeUiTally,
|
|
194
|
+
holdMatchQuiet,
|
|
195
|
+
playRole,
|
|
196
|
+
playSoundEvents,
|
|
197
|
+
setTrackNames,
|
|
198
|
+
getMusicTrack,
|
|
199
|
+
subscribeMusicTrack,
|
|
200
|
+
setScreenMusic,
|
|
201
|
+
setMatchMusic,
|
|
202
|
+
roleHandler,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type Seat = 'me' | 'opp';
|
|
2
|
+
/** The mixer bus a sound plays on. The layer cell always plays on `colour`. */
|
|
3
|
+
export type SoundBus = 'ui' | 'game' | 'colour' | 'music';
|
|
4
|
+
/** What the planner needs about one role: everything else is the game's business. */
|
|
5
|
+
export interface MixerRole {
|
|
6
|
+
readonly bus: Exclude<SoundBus, 'colour'>;
|
|
7
|
+
/** Briefly lowers every other sound while it plays. */
|
|
8
|
+
readonly duck?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** A game's sound-role data, injected rather than imported: a role's bus/duck, its fallback (empty cell) and reversed (no cell of its own) chains. */
|
|
11
|
+
export interface RoleRegistry {
|
|
12
|
+
readonly roles: ReadonlyMap<string, MixerRole>;
|
|
13
|
+
readonly fallback: Readonly<Record<string, string>>;
|
|
14
|
+
readonly reversed: Readonly<Record<string, string>>;
|
|
15
|
+
}
|
|
16
|
+
/** The registry a bare `createMixPlanner()` builds against, for a call site that leans on its default. The game sets this once, with its real roles, before such a call; new code prefers `createSound`. */
|
|
17
|
+
export declare function configureSoundRoles(registry: RoleRegistry): void;
|
|
18
|
+
export interface SoundEvent {
|
|
19
|
+
role: string;
|
|
20
|
+
/** A layer cell, laid over the role's own: the game decides what it means. */
|
|
21
|
+
colourCell?: string | null;
|
|
22
|
+
seat?: Seat;
|
|
23
|
+
/** How heavy it is, 0 to 1. Louder and lower the heavier; absent, as recorded. */
|
|
24
|
+
intensity?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface MixSettings {
|
|
27
|
+
bus: Record<SoundBus, number>;
|
|
28
|
+
/** Multiplier on the opponent's game and layer cells. */
|
|
29
|
+
opponent: number;
|
|
30
|
+
collapseMs: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Interface sounds sit low: they fire constantly, and one as loud as the
|
|
34
|
+
* game is one a player mutes within a minute.
|
|
35
|
+
*/
|
|
36
|
+
export declare const DEFAULT_MIX: MixSettings;
|
|
37
|
+
export interface PlannedVoice {
|
|
38
|
+
cell: string;
|
|
39
|
+
file: string;
|
|
40
|
+
bus: SoundBus;
|
|
41
|
+
gain: number;
|
|
42
|
+
duck: boolean;
|
|
43
|
+
/** Played backwards: the event named a role in the registry's `reversed` map. */
|
|
44
|
+
reverse?: true;
|
|
45
|
+
/** Its playback rate when not 1: a heavier event's voice is lower (`intensityVoice`). */
|
|
46
|
+
rate?: number;
|
|
47
|
+
}
|
|
48
|
+
export type SkipReason = 'empty' | 'collapsed' | 'opponent-ui' | 'unknown-role';
|
|
49
|
+
export interface PlanResult {
|
|
50
|
+
voices: PlannedVoice[];
|
|
51
|
+
skipped: {
|
|
52
|
+
cell: string;
|
|
53
|
+
reason: SkipReason;
|
|
54
|
+
}[];
|
|
55
|
+
}
|
|
56
|
+
export interface MixPlanner {
|
|
57
|
+
setAssignments(cells: Readonly<Record<string, readonly string[]>>, gains: Readonly<Record<string, number>>): void;
|
|
58
|
+
plan(event: SoundEvent, nowMs: number, settings: MixSettings): PlanResult;
|
|
59
|
+
/** One cell's level as stored, 1 when unset. */
|
|
60
|
+
gainOf(cell: string): number;
|
|
61
|
+
/** Whether a cell has any take to play. Plans nothing. */
|
|
62
|
+
has(cell: string): boolean;
|
|
63
|
+
/** A cell's takes, as assigned. */
|
|
64
|
+
takes(cell: string): readonly string[];
|
|
65
|
+
/** Every take of every cell off the music bus, once each, sorted: what the mixer decodes ahead of time. */
|
|
66
|
+
cueTakes(): string[];
|
|
67
|
+
}
|
|
68
|
+
/** A planner against the game's real roles (`configureSoundRoles`), or against none until something configures one. New code prefers `createSound`. */
|
|
69
|
+
export declare function createMixPlanner(rng?: () => number): MixPlanner;
|