@hadoku/game-ui 0.1.44 → 0.1.46
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/dist/app/GameAppShell.d.ts +23 -0
- package/dist/app/GameAppShell.js +51 -0
- package/dist/embed/bound-room.d.ts +44 -0
- package/dist/embed/bound-room.js +87 -0
- package/dist/registered.d.ts +6 -2
- package/dist/registered.js +14 -3
- package/dist/room/RoomShell.d.ts +18 -4
- package/dist/room/RoomShell.js +59 -14
- package/dist/room/whoami.d.ts +17 -3
- package/dist/room/whoami.js +25 -3
- package/dist/screens/route.d.ts +6 -24
- package/dist/screens/route.js +18 -111
- package/dist/screens/router.d.ts +53 -0
- package/dist/screens/router.js +133 -0
- package/dist/sound/index.d.ts +6 -0
- package/dist/sound/index.js +14 -1
- package/package.json +7 -7
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type AppHeaderProps } from '@wolffm/task-ui-components';
|
|
3
|
+
import { type Who } from '../room/whoami.js';
|
|
4
|
+
/** What the frame resolved, for the game's own layout. */
|
|
5
|
+
export interface GameShell {
|
|
6
|
+
/** The viewer, or null while asked and when the fleet could not say. */
|
|
7
|
+
who: Who | null;
|
|
8
|
+
narrow: boolean;
|
|
9
|
+
touchFirst: boolean;
|
|
10
|
+
isDarkTheme: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface GameAppShellProps {
|
|
13
|
+
/** The `<main>`'s class: the game's own container (`bomberman-container`). */
|
|
14
|
+
className: string;
|
|
15
|
+
children: (shell: GameShell) => ReactNode;
|
|
16
|
+
}
|
|
17
|
+
export declare function GameAppShell(props: GameAppShellProps): import("react").JSX.Element;
|
|
18
|
+
/**
|
|
19
|
+
* The fleet's `<AppHeader>`, handed the identity the page already resolved, so the settings popout
|
|
20
|
+
* does not fetch whoami a second time. An identity HINT, never a way to present a tier the edge did
|
|
21
|
+
* not assign: every gate that matters is enforced server-side off the edge-stamped tier.
|
|
22
|
+
*/
|
|
23
|
+
export declare function GameHeader(props: Omit<AppHeaderProps, 'settingsProps'>): import("react").JSX.Element;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/*
|
|
3
|
+
* A game's standalone page frame: everything a game's `App.tsx` would otherwise copy.
|
|
4
|
+
*
|
|
5
|
+
* Three things resolve ONCE, here, and are handed down:
|
|
6
|
+
*
|
|
7
|
+
* the theme `<HadokuThemeRoot>` owns the theme, its persistence and the picker. A game adds no
|
|
8
|
+
* `useTheme` of its own: the fleet's apps each carried a copy once, and they drifted
|
|
9
|
+
* into real bugs.
|
|
10
|
+
* the identity one `/session/whoami` (`../room/whoami.ts`), handed to every `useWhoami` below
|
|
11
|
+
* through `WhoProvider`: the header's settings popout (`GameHeader`) and the room
|
|
12
|
+
* chrome (`../room/RoomShell.tsx`) read the same answer.
|
|
13
|
+
* the platform `usePlatform`'s `narrow` and `touchFirst`, rendered as `data-narrow` /
|
|
14
|
+
* `data-touch-first` on the `<main>` from the first paint, so the game's CSS keys
|
|
15
|
+
* off the same source as its JS. (`usePlatform` stamps them itself, but in an effect
|
|
16
|
+
* that runs on a change, and on the first render the `<main>` does not exist yet:
|
|
17
|
+
* the skeleton holds the theme's first resolve. On a phone the stamp found no element
|
|
18
|
+
* and never came back.)
|
|
19
|
+
*
|
|
20
|
+
* The skeleton holds the page through the theme's first resolve, so it never flashes the wrong
|
|
21
|
+
* palette. Inside, the game lays out its own column: `children` is called with what was resolved.
|
|
22
|
+
*
|
|
23
|
+
* This is the standalone page only. Embedded in a host's page the room and the theme are the host's,
|
|
24
|
+
* and a game draws none of this (`@hadoku/ui-shell/embed`'s `mountEmbed`).
|
|
25
|
+
*/
|
|
26
|
+
import { useRef } from 'react';
|
|
27
|
+
import { AppHeader, LoadingSkeleton } from '@wolffm/task-ui-components';
|
|
28
|
+
import { HadokuThemeRoot, useHadokuTheme, usePlatform } from '@wolffm/themes';
|
|
29
|
+
import { useResolvedWhoami, useWhoami, WhoProvider } from "../room/whoami.js";
|
|
30
|
+
export function GameAppShell(props) {
|
|
31
|
+
const containerRef = useRef(null);
|
|
32
|
+
return (_jsx(HadokuThemeRoot, { containerRef: containerRef, children: _jsx(ShellInner, { containerRef: containerRef, ...props }) }));
|
|
33
|
+
}
|
|
34
|
+
function ShellInner({ containerRef, className, children }) {
|
|
35
|
+
const { isDarkTheme, isThemeReady, isInitialThemeLoad } = useHadokuTheme();
|
|
36
|
+
const { narrow, touchFirst } = usePlatform({ containerRef });
|
|
37
|
+
const who = useResolvedWhoami();
|
|
38
|
+
if (isInitialThemeLoad && !isThemeReady)
|
|
39
|
+
return _jsx(LoadingSkeleton, { isDarkTheme: isDarkTheme });
|
|
40
|
+
return (_jsx(WhoProvider, { who: who, children: _jsx("main", { ref: containerRef, className: className, "data-dark-theme": isDarkTheme ? 'true' : 'false', "data-narrow": narrow ? '' : undefined, "data-touch-first": touchFirst ? '' : undefined, children: children({ who: who === 'pending' ? null : who, narrow, touchFirst, isDarkTheme }) }) }));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The fleet's `<AppHeader>`, handed the identity the page already resolved, so the settings popout
|
|
44
|
+
* does not fetch whoami a second time. An identity HINT, never a way to present a tier the edge did
|
|
45
|
+
* not assign: every gate that matters is enforced server-side off the edge-stamped tier.
|
|
46
|
+
*/
|
|
47
|
+
export function GameHeader(props) {
|
|
48
|
+
const who = useWhoami();
|
|
49
|
+
const viewer = who === 'pending' ? null : who;
|
|
50
|
+
return _jsx(AppHeader, { ...props, settingsProps: viewer === null ? undefined : { userType: viewer.tier, name: viewer.name } });
|
|
51
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { HadokuThemeValue } from '@wolffm/themes';
|
|
2
|
+
import { type Fetch } from '../http.js';
|
|
3
|
+
export type BindOutcome = {
|
|
4
|
+
ok: true;
|
|
5
|
+
roomId: string;
|
|
6
|
+
} | {
|
|
7
|
+
ok: false;
|
|
8
|
+
message: string;
|
|
9
|
+
};
|
|
10
|
+
/** Which of this game's rooms a host room plays in. Never throws: a failure is an outcome the embed renders. */
|
|
11
|
+
export type BindRoom = (hostRoom: string) => Promise<BindOutcome>;
|
|
12
|
+
/** The game server's own answer, over `fetchImpl`. */
|
|
13
|
+
export declare function bindWatchpartyRoom(fetchImpl?: Fetch): BindRoom;
|
|
14
|
+
export type Binding = {
|
|
15
|
+
state: 'binding';
|
|
16
|
+
} | {
|
|
17
|
+
state: 'bound';
|
|
18
|
+
roomId: string;
|
|
19
|
+
} | {
|
|
20
|
+
state: 'failed';
|
|
21
|
+
message: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The room `hostRoom` plays in, asked of `bind` whenever `hostRoom` changes. With `skip` the host's
|
|
25
|
+
* room key IS the room: `bind` is never called, and the answer is derived rather than stored, so it
|
|
26
|
+
* cannot drift from `skip` or `hostRoom` changing.
|
|
27
|
+
*/
|
|
28
|
+
export declare function useBoundRoom(hostRoom: string, bind: BindRoom, skip?: boolean): Binding;
|
|
29
|
+
/**
|
|
30
|
+
* A route that is not the bound room goes to it: `go` is the game's own navigation. An effect,
|
|
31
|
+
* never a write during render. `go` should be stable (a module-level function).
|
|
32
|
+
*/
|
|
33
|
+
export declare function ToRoom({ roomId, go }: {
|
|
34
|
+
roomId: string;
|
|
35
|
+
go: (roomId: string) => void;
|
|
36
|
+
}): null;
|
|
37
|
+
/** Whether a fleet theme name is one of the dark ones. */
|
|
38
|
+
export declare function isDarkThemeName(theme: string): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* The theme context for a tree whose theme is the HOST's: anything below reading `useHadokuTheme()`
|
|
41
|
+
* reads the host's theme rather than throwing. Changing it is the host's, so `setTheme` is inert and
|
|
42
|
+
* there is no picker.
|
|
43
|
+
*/
|
|
44
|
+
export declare function hostThemeValue(theme: string): HadokuThemeValue;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The room a game's embed plays in, inside a host's room (watchparty's game slot).
|
|
3
|
+
*
|
|
4
|
+
* The game's own server answers which of its rooms a host room plays in (`POST /api/rooms/watchparty`,
|
|
5
|
+
* `@hadoku/game-server`'s room-core route), deriving it from the host's room id, so every member's
|
|
6
|
+
* bundle lands at the same table without any of them choosing it. The host's room id is opaque here:
|
|
7
|
+
* the server hashes it, so its shape need not fit a game's room-id rules.
|
|
8
|
+
*
|
|
9
|
+
* A host that runs the match itself (a `MatchChannel` in the bundle context) needs no binding: its
|
|
10
|
+
* room key IS the room (`skip`).
|
|
11
|
+
*/
|
|
12
|
+
import { useEffect, useState } from 'react';
|
|
13
|
+
import { THEME_FAMILIES } from '@wolffm/themes';
|
|
14
|
+
import { ROOM_ID_RE } from '@wolffm/platform-wire/room-feed';
|
|
15
|
+
import { apiUrl, bodyOf, isRecord, pageFetch, sendJson, serverMessage } from "../http.js";
|
|
16
|
+
/** The game server's own answer, over `fetchImpl`. */
|
|
17
|
+
export function bindWatchpartyRoom(fetchImpl = pageFetch) {
|
|
18
|
+
return async (hostRoom) => {
|
|
19
|
+
let res;
|
|
20
|
+
try {
|
|
21
|
+
res = await sendJson(apiUrl('rooms/watchparty'), 'POST', { room: hostRoom }, fetchImpl);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return { ok: false, message: 'Could not reach the server.' };
|
|
25
|
+
}
|
|
26
|
+
const body = await bodyOf(res);
|
|
27
|
+
if (!res.ok)
|
|
28
|
+
return { ok: false, message: `Could not open this room’s table: ${serverMessage(body, res.status)}.` };
|
|
29
|
+
const roomId = isRecord(body) ? body.id : undefined;
|
|
30
|
+
if (typeof roomId !== 'string' || !ROOM_ID_RE.test(roomId))
|
|
31
|
+
return { ok: false, message: 'The server answered with something that is not a room.' };
|
|
32
|
+
return { ok: true, roomId };
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The room `hostRoom` plays in, asked of `bind` whenever `hostRoom` changes. With `skip` the host's
|
|
37
|
+
* room key IS the room: `bind` is never called, and the answer is derived rather than stored, so it
|
|
38
|
+
* cannot drift from `skip` or `hostRoom` changing.
|
|
39
|
+
*/
|
|
40
|
+
export function useBoundRoom(hostRoom, bind, skip = false) {
|
|
41
|
+
const [binding, setBinding] = useState({ state: 'binding' });
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (skip)
|
|
44
|
+
return;
|
|
45
|
+
let live = true;
|
|
46
|
+
setBinding({ state: 'binding' });
|
|
47
|
+
void bind(hostRoom).then((outcome) => {
|
|
48
|
+
if (!live)
|
|
49
|
+
return;
|
|
50
|
+
setBinding(outcome.ok ? { state: 'bound', roomId: outcome.roomId } : { state: 'failed', message: outcome.message });
|
|
51
|
+
});
|
|
52
|
+
return () => {
|
|
53
|
+
live = false;
|
|
54
|
+
};
|
|
55
|
+
}, [hostRoom, bind, skip]);
|
|
56
|
+
return skip ? { state: 'bound', roomId: hostRoom } : binding;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A route that is not the bound room goes to it: `go` is the game's own navigation. An effect,
|
|
60
|
+
* never a write during render. `go` should be stable (a module-level function).
|
|
61
|
+
*/
|
|
62
|
+
export function ToRoom({ roomId, go }) {
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
go(roomId);
|
|
65
|
+
}, [roomId, go]);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
/** Whether a fleet theme name is one of the dark ones. */
|
|
69
|
+
export function isDarkThemeName(theme) {
|
|
70
|
+
return theme === 'dark' || theme.endsWith('-dark');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The theme context for a tree whose theme is the HOST's: anything below reading `useHadokuTheme()`
|
|
74
|
+
* reads the host's theme rather than throwing. Changing it is the host's, so `setTheme` is inert and
|
|
75
|
+
* there is no picker.
|
|
76
|
+
*/
|
|
77
|
+
export function hostThemeValue(theme) {
|
|
78
|
+
return {
|
|
79
|
+
theme,
|
|
80
|
+
setTheme: () => undefined,
|
|
81
|
+
themeFamilies: [...THEME_FAMILIES],
|
|
82
|
+
getThemeIcon: () => null,
|
|
83
|
+
isDarkTheme: isDarkThemeName(theme),
|
|
84
|
+
isThemeReady: true,
|
|
85
|
+
isInitialThemeLoad: false,
|
|
86
|
+
};
|
|
87
|
+
}
|
package/dist/registered.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A part the game hands a generic file once, from its composition root (cardtable's `./composition.tsx`).
|
|
3
|
+
* Read before then, it throws rather than guess — unless the part has an honest default (`fallback`),
|
|
4
|
+
* built on first read, which a game that registers nothing gets.
|
|
5
|
+
*/
|
|
6
|
+
export declare function registered<T>(name: string, fallback?: () => T): {
|
|
3
7
|
set(value: T): void;
|
|
4
8
|
get(): T;
|
|
5
9
|
};
|
package/dist/registered.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A part the game hands a generic file once, from its composition root (cardtable's `./composition.tsx`).
|
|
3
|
+
* Read before then, it throws rather than guess — unless the part has an honest default (`fallback`),
|
|
4
|
+
* built on first read, which a game that registers nothing gets.
|
|
5
|
+
*/
|
|
6
|
+
export function registered(name, fallback) {
|
|
3
7
|
let value;
|
|
4
8
|
const missing = () => { throw new Error(`${name} was never registered: ./composition.tsx's composeGame() registers it`); };
|
|
5
|
-
return {
|
|
9
|
+
return {
|
|
10
|
+
set: (v) => void (value = v),
|
|
11
|
+
get: () => {
|
|
12
|
+
if (value === undefined && fallback !== undefined)
|
|
13
|
+
value = fallback();
|
|
14
|
+
return value ?? missing();
|
|
15
|
+
},
|
|
16
|
+
};
|
|
6
17
|
}
|
package/dist/room/RoomShell.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { type ReactNode } from 'react';
|
|
2
|
+
import type { RoomChrome as RoomChromeComponent } from '@wolffm/watchparty-ui/room-chrome';
|
|
2
3
|
import '@wolffm/watchparty-ui/room-chrome.css';
|
|
3
4
|
import { type Theme } from '@wolffm/themes';
|
|
5
|
+
type RoomChromeType = typeof RoomChromeComponent;
|
|
6
|
+
/** Starts loading the chrome's chunk, once per page; a front door may call it to fetch it early. */
|
|
7
|
+
export declare function loadRoomChrome(): Promise<RoomChromeType>;
|
|
4
8
|
/**
|
|
5
9
|
* The page's resolved theme, handed to the chrome as-is.
|
|
6
10
|
*
|
|
@@ -81,7 +85,7 @@ export declare function roomKeyFor(id: string): string | null;
|
|
|
81
85
|
* path, `/cardtable[-ppe]/socket.io/`, from its pathname, so passing the
|
|
82
86
|
* bare base is enough — no `roomSocketPath` override needed.
|
|
83
87
|
*/
|
|
84
|
-
export declare function
|
|
88
|
+
export declare function appOrigin(): string;
|
|
85
89
|
/**
|
|
86
90
|
* Wraps `children` in watchparty's room when there is a room to be in.
|
|
87
91
|
*
|
|
@@ -100,10 +104,12 @@ export declare function cardtableOrigin(): string;
|
|
|
100
104
|
* into a node that moves into the chrome when it draws and out when it goes. That
|
|
101
105
|
* node and the slot holding it are both `.chrome-slot` (`display: contents`), and
|
|
102
106
|
* the chrome's rules for its content's own child reach through exactly those two
|
|
103
|
-
* (`@hadoku/ui-shell`'s `chrome.css`
|
|
104
|
-
*
|
|
107
|
+
* (`@hadoku/ui-shell`'s `chrome.css`; `display: contents` is also set on them here, for a game
|
|
108
|
+
* that loads no such sheet). While whoami or the chrome's chunk is in flight on a room's route,
|
|
109
|
+
* before they have first mounted, they wait for it, showing `fallback`: their first frame is already
|
|
110
|
+
* inside. After that, they show bare while the chrome is not drawn.
|
|
105
111
|
*/
|
|
106
|
-
export declare function RoomShell({ roomId, collapsible, inert, children, }: {
|
|
112
|
+
export declare function RoomShell({ roomId, collapsible, inert, fallback, requireIdentified, children, }: {
|
|
107
113
|
/** The room this viewer is in: the chat and roster the chrome joins, and where their avatar is asserted. */
|
|
108
114
|
roomId: string | null;
|
|
109
115
|
/** `false` keeps the chat open with no Hide chat button — the room's lobby,
|
|
@@ -112,5 +118,13 @@ export declare function RoomShell({ roomId, collapsible, inert, children, }: {
|
|
|
112
118
|
collapsible?: boolean;
|
|
113
119
|
/** Behind a question the page must answer first (`./SessionPrompt.tsx`): the chat cannot be reached. */
|
|
114
120
|
inert?: boolean;
|
|
121
|
+
/** What a room's route shows while it waits, before its content first mounts. Default: nothing. */
|
|
122
|
+
fallback?: ReactNode;
|
|
123
|
+
/**
|
|
124
|
+
* No chrome for a viewer the fleet could not name (`ANON_USER_ID`): the room's server turns an
|
|
125
|
+
* anonymous join away, and a chat column that can only fail reads as a broken room. Default off.
|
|
126
|
+
*/
|
|
127
|
+
requireIdentified?: boolean;
|
|
115
128
|
children: ReactNode;
|
|
116
129
|
}): import("react").JSX.Element;
|
|
130
|
+
export {};
|
package/dist/room/RoomShell.js
CHANGED
|
@@ -19,7 +19,7 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
|
|
|
19
19
|
* ## The room is ours; only the art is theirs
|
|
20
20
|
*
|
|
21
21
|
* `roomOrigin` points the chrome's whole room socket — join, roster, identity,
|
|
22
|
-
* chat — at `
|
|
22
|
+
* chat — at `appOrigin()`: this app's own `/room` namespace
|
|
23
23
|
* (`games/magic/server/src/room/serve.ts`), behind cardtable's member gate, with the
|
|
24
24
|
* roster read from the same presence tracker our seats use. Watchparty never
|
|
25
25
|
* hears of a cardtable room, so it neither counts one nor holds a deploy for
|
|
@@ -44,10 +44,10 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
|
|
|
44
44
|
* and the table renders alone, because a room that cannot say who you are is
|
|
45
45
|
* worse than no room.
|
|
46
46
|
*/
|
|
47
|
-
import { useCallback, useRef, useState } from 'react';
|
|
47
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
48
48
|
import { createPortal } from 'react-dom';
|
|
49
|
-
import { RoomChrome } from '@wolffm/watchparty-ui/room-chrome';
|
|
50
49
|
import '@wolffm/watchparty-ui/room-chrome.css';
|
|
50
|
+
import { ANON_USER_ID } from '@wolffm/prefs-client';
|
|
51
51
|
import { useHadokuTheme } from '@wolffm/themes';
|
|
52
52
|
import { ROOM_ID_RE } from '@wolffm/platform-wire/room-feed';
|
|
53
53
|
import { appId } from '@wolffm/ui-shell/app-id';
|
|
@@ -59,6 +59,43 @@ import { useRoomFrame } from '@wolffm/ui-shell/chrome/shell';
|
|
|
59
59
|
// of one origin is how the chrome and the faces come to disagree about it.
|
|
60
60
|
import { watchpartyOrigin } from "./avatar.js";
|
|
61
61
|
import { useWhoami } from "./whoami.js";
|
|
62
|
+
/*
|
|
63
|
+
* Their chrome, in its own chunk: only a room needs it, and a game's front door is lighter by the
|
|
64
|
+
* whole of it. Loaded as soon as a RoomShell first draws (one per page, from the app's root), so by
|
|
65
|
+
* the time a room's route is shown it is usually here; until it is, the room waits exactly as it
|
|
66
|
+
* waits for whoami.
|
|
67
|
+
*/
|
|
68
|
+
let chromeLoad = null;
|
|
69
|
+
let loadedChrome = null;
|
|
70
|
+
/** Starts loading the chrome's chunk, once per page; a front door may call it to fetch it early. */
|
|
71
|
+
export function loadRoomChrome() {
|
|
72
|
+
chromeLoad ??= import('@wolffm/watchparty-ui/room-chrome').then((m) => (loadedChrome = m.RoomChrome), (err) => {
|
|
73
|
+
// The next RoomShell to draw asks again.
|
|
74
|
+
chromeLoad = null;
|
|
75
|
+
throw err;
|
|
76
|
+
});
|
|
77
|
+
return chromeLoad;
|
|
78
|
+
}
|
|
79
|
+
/** The chrome's component once its chunk is here, else null. */
|
|
80
|
+
function useRoomChrome() {
|
|
81
|
+
const [chrome, setChrome] = useState(() => loadedChrome);
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
if (chrome !== null)
|
|
84
|
+
return;
|
|
85
|
+
let live = true;
|
|
86
|
+
loadRoomChrome().then((loaded) => {
|
|
87
|
+
if (live)
|
|
88
|
+
setChrome(() => loaded);
|
|
89
|
+
}, (err) => {
|
|
90
|
+
// The room renders without its chat: a degraded room, not an outage.
|
|
91
|
+
console.error('room chrome: its chunk did not load', err);
|
|
92
|
+
});
|
|
93
|
+
return () => {
|
|
94
|
+
live = false;
|
|
95
|
+
};
|
|
96
|
+
}, [chrome]);
|
|
97
|
+
return chrome;
|
|
98
|
+
}
|
|
62
99
|
/**
|
|
63
100
|
* The page's resolved theme, handed to the chrome as-is.
|
|
64
101
|
*
|
|
@@ -145,7 +182,7 @@ export function roomKeyFor(id) {
|
|
|
145
182
|
* path, `/cardtable[-ppe]/socket.io/`, from its pathname, so passing the
|
|
146
183
|
* bare base is enough — no `roomSocketPath` override needed.
|
|
147
184
|
*/
|
|
148
|
-
export function
|
|
185
|
+
export function appOrigin() {
|
|
149
186
|
const base = import.meta.env?.BASE_URL;
|
|
150
187
|
const prefix = typeof base === 'string' && base !== '' ? base : '/';
|
|
151
188
|
return `${globalThis.location.origin}${prefix}`.replace(/\/$/, '');
|
|
@@ -168,11 +205,14 @@ export function cardtableOrigin() {
|
|
|
168
205
|
* into a node that moves into the chrome when it draws and out when it goes. That
|
|
169
206
|
* node and the slot holding it are both `.chrome-slot` (`display: contents`), and
|
|
170
207
|
* the chrome's rules for its content's own child reach through exactly those two
|
|
171
|
-
* (`@hadoku/ui-shell`'s `chrome.css`
|
|
172
|
-
*
|
|
208
|
+
* (`@hadoku/ui-shell`'s `chrome.css`; `display: contents` is also set on them here, for a game
|
|
209
|
+
* that loads no such sheet). While whoami or the chrome's chunk is in flight on a room's route,
|
|
210
|
+
* before they have first mounted, they wait for it, showing `fallback`: their first frame is already
|
|
211
|
+
* inside. After that, they show bare while the chrome is not drawn.
|
|
173
212
|
*/
|
|
174
|
-
export function RoomShell({ roomId, collapsible = true, inert = false, children, }) {
|
|
213
|
+
export function RoomShell({ roomId, collapsible = true, inert = false, fallback = null, requireIdentified = false, children, }) {
|
|
175
214
|
const who = useWhoami();
|
|
215
|
+
const RoomChrome = useRoomChrome();
|
|
176
216
|
// Always inside `AppChrome`'s `HadokuThemeRoot` — see chrome/AppChrome.tsx,
|
|
177
217
|
// which mounts this component. `useHadokuTheme()` throws with no provider
|
|
178
218
|
// on purpose (a silent fallback is the bug this removes), so a caller that
|
|
@@ -182,15 +222,18 @@ export function RoomShell({ roomId, collapsible = true, inert = false, children,
|
|
|
182
222
|
const roomFrameRef = useRoomFrame();
|
|
183
223
|
const [host] = useState(createHost);
|
|
184
224
|
const mounted = useRef(false);
|
|
185
|
-
const
|
|
186
|
-
|
|
225
|
+
const unknown = who === null || (requireIdentified && who !== 'pending' && who.userId === ANON_USER_ID);
|
|
226
|
+
const waiting = key !== null && !unknown && (who === 'pending' || RoomChrome === null);
|
|
227
|
+
// Waiting before the first mount shows the fallback; after it, the content stays where it is.
|
|
228
|
+
const holding = waiting && !mounted.current;
|
|
229
|
+
if (!holding)
|
|
187
230
|
mounted.current = true;
|
|
188
231
|
// One keyed element in every branch below, so React keeps the children; last, so the slot has
|
|
189
232
|
// taken the host in before their layout effects run.
|
|
190
233
|
const content = mounted.current ? createPortal(children, host, 'content') : null;
|
|
191
|
-
if (
|
|
192
|
-
return _jsx(_Fragment, { children:
|
|
193
|
-
if (key === null || who ===
|
|
234
|
+
if (holding)
|
|
235
|
+
return _jsx(_Fragment, { children: fallback });
|
|
236
|
+
if (key === null || unknown || who === 'pending' || RoomChrome === null)
|
|
194
237
|
return (_jsxs(_Fragment, { children: [_jsx(Slot, { host: host }, "bare"), content] }));
|
|
195
238
|
return (
|
|
196
239
|
/*
|
|
@@ -205,18 +248,20 @@ export function RoomShell({ roomId, collapsible = true, inert = false, children,
|
|
|
205
248
|
* `route.kind !== 'room'`), never the room lobby (Settled 10: "the room
|
|
206
249
|
* lobby is unchanged").
|
|
207
250
|
*/
|
|
208
|
-
_jsxs(_Fragment, { children: [_jsx("div", { className: "room-shell", "data-testid": "room-shell", "data-collapsible": collapsible ? '' : undefined, inert: inert || undefined, children: _jsxs(RoomChrome, { roomKey: key, serverOrigin: watchpartyOrigin(), roomOrigin:
|
|
251
|
+
_jsxs(_Fragment, { children: [_jsx("div", { className: "room-shell", "data-testid": "room-shell", "data-collapsible": collapsible ? '' : undefined, inert: inert || undefined, children: _jsxs(RoomChrome, { roomKey: key, serverOrigin: watchpartyOrigin(), roomOrigin: appOrigin(), userType: who.tier, userId: who.userId, theme: watchpartyTheme(theme), collapsible: collapsible, children: [_jsx(Slot, { host: host }), _jsx("span", { hidden: true, ref: roomFrameRef })] }) }, "chrome"), content] }));
|
|
209
252
|
}
|
|
210
253
|
/** The node {@link RoomShell}'s children are drawn into: it takes no box of its own, so they lay out as its parent's. */
|
|
211
254
|
function createHost() {
|
|
212
255
|
const host = document.createElement('div');
|
|
213
256
|
host.className = 'chrome-slot';
|
|
257
|
+
host.style.display = 'contents';
|
|
214
258
|
return host;
|
|
215
259
|
}
|
|
260
|
+
const SLOT_STYLE = { display: 'contents' };
|
|
216
261
|
/** Where the children stand now: the host is moved in when this mounts. */
|
|
217
262
|
function Slot({ host }) {
|
|
218
263
|
const place = useCallback((slot) => {
|
|
219
264
|
slot?.appendChild(host);
|
|
220
265
|
}, [host]);
|
|
221
|
-
return _jsx("div", { ref: place, className: "chrome-slot" });
|
|
266
|
+
return _jsx("div", { ref: place, className: "chrome-slot", style: SLOT_STYLE });
|
|
222
267
|
}
|
package/dist/room/whoami.d.ts
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
1
2
|
import { whoami as hadokuWhoami } from '@wolffm/prefs-client';
|
|
2
3
|
export interface Who {
|
|
3
4
|
userId: string;
|
|
4
5
|
/** The fleet's own tier list, so a tier it adds (`member`) is not a type error here. */
|
|
5
|
-
tier:
|
|
6
|
+
tier: WhoamiAnswer['userType'];
|
|
6
7
|
/** The viewer's display name. Null when the fleet has none for them. */
|
|
7
8
|
name: string | null;
|
|
8
9
|
}
|
|
10
|
+
type WhoamiAnswer = Awaited<ReturnType<typeof hadokuWhoami>>;
|
|
11
|
+
/** The viewer, `null` when the fleet could not say, `'pending'` while it is asked. */
|
|
12
|
+
export type WhoState = Who | null | 'pending';
|
|
13
|
+
export declare const registerWhoamiTier: (value: (r: Pick<WhoamiAnswer, "userType" | "verified">) => Who["tier"]) => void;
|
|
9
14
|
/**
|
|
10
15
|
* The viewer's tier as cardtable treats it: a member the operator has verified IS a friend here (the
|
|
11
16
|
* operator, 2026-11-05: *"for cardtable they will be equivalent"*). The server's `tierOf` makes the
|
|
12
17
|
* same call for every gate; this is the tier the fleet chrome is handed to show.
|
|
13
18
|
*/
|
|
14
|
-
export declare function tierOfWhoami(r: Pick<
|
|
15
|
-
|
|
19
|
+
export declare function tierOfWhoami(r: Pick<WhoamiAnswer, 'userType' | 'verified'>): Who['tier'];
|
|
20
|
+
/** Hands `who` to every `useWhoami` below, which then asks nothing of its own. */
|
|
21
|
+
export declare function WhoProvider({ who, children }: {
|
|
22
|
+
who: WhoState;
|
|
23
|
+
children: ReactNode;
|
|
24
|
+
}): import("react").FunctionComponentElement<import("react").ProviderProps<WhoState | undefined>>;
|
|
25
|
+
/** The viewer: the app shell's answer when there is one above, else this hook's own. */
|
|
26
|
+
export declare function useWhoami(): WhoState;
|
|
27
|
+
/** Asks the fleet once, while `enabled`. */
|
|
28
|
+
export declare function useResolvedWhoami(enabled?: boolean): WhoState;
|
|
29
|
+
export {};
|
package/dist/room/whoami.js
CHANGED
|
@@ -10,8 +10,15 @@
|
|
|
10
10
|
* Nothing here invents an identity: a failed answer is `null`, and the caller
|
|
11
11
|
* decides what a table with nobody known at it looks like.
|
|
12
12
|
*/
|
|
13
|
-
import { useEffect, useState } from 'react';
|
|
13
|
+
import { createContext, createElement, useContext, useEffect, useState } from 'react';
|
|
14
14
|
import { whoami as hadokuWhoami } from '@wolffm/prefs-client';
|
|
15
|
+
import { registered } from "../registered.js";
|
|
16
|
+
/**
|
|
17
|
+
* How a game reads the fleet's tier for the viewer. Default: as the fleet answered it. Cardtable's
|
|
18
|
+
* composition root registers {@link tierOfWhoami}.
|
|
19
|
+
*/
|
|
20
|
+
const whoamiTier = registered('whoami tier', () => (r) => r.userType);
|
|
21
|
+
export const registerWhoamiTier = whoamiTier.set;
|
|
15
22
|
/**
|
|
16
23
|
* The viewer's tier as cardtable treats it: a member the operator has verified IS a friend here (the
|
|
17
24
|
* operator, 2026-11-05: *"for cardtable they will be equivalent"*). The server's `tierOf` makes the
|
|
@@ -20,9 +27,24 @@ import { whoami as hadokuWhoami } from '@wolffm/prefs-client';
|
|
|
20
27
|
export function tierOfWhoami(r) {
|
|
21
28
|
return r.userType === 'member' && r.verified === true ? 'friend' : r.userType;
|
|
22
29
|
}
|
|
30
|
+
/** The page's one resolution, when an app shell above has made it (`../app/GameAppShell.tsx`). */
|
|
31
|
+
const WhoContext = createContext(undefined);
|
|
32
|
+
/** Hands `who` to every `useWhoami` below, which then asks nothing of its own. */
|
|
33
|
+
export function WhoProvider({ who, children }) {
|
|
34
|
+
return createElement(WhoContext.Provider, { value: who }, children);
|
|
35
|
+
}
|
|
36
|
+
/** The viewer: the app shell's answer when there is one above, else this hook's own. */
|
|
23
37
|
export function useWhoami() {
|
|
38
|
+
const given = useContext(WhoContext);
|
|
39
|
+
const own = useResolvedWhoami(given === undefined);
|
|
40
|
+
return given ?? own;
|
|
41
|
+
}
|
|
42
|
+
/** Asks the fleet once, while `enabled`. */
|
|
43
|
+
export function useResolvedWhoami(enabled = true) {
|
|
24
44
|
const [who, setWho] = useState('pending');
|
|
25
45
|
useEffect(() => {
|
|
46
|
+
if (!enabled)
|
|
47
|
+
return;
|
|
26
48
|
let live = true;
|
|
27
49
|
// Same-origin on purpose. `whoami()` defaults to the edge-router origin,
|
|
28
50
|
// which in production IS this page's origin — but hard-coding it makes the
|
|
@@ -34,7 +56,7 @@ export function useWhoami() {
|
|
|
34
56
|
.then((r) => {
|
|
35
57
|
if (!live)
|
|
36
58
|
return;
|
|
37
|
-
setWho({ userId: r.userId, tier:
|
|
59
|
+
setWho({ userId: r.userId, tier: whoamiTier.get()(r), name: r.name ?? null });
|
|
38
60
|
})
|
|
39
61
|
.catch(() => {
|
|
40
62
|
if (live)
|
|
@@ -43,6 +65,6 @@ export function useWhoami() {
|
|
|
43
65
|
return () => {
|
|
44
66
|
live = false;
|
|
45
67
|
};
|
|
46
|
-
}, []);
|
|
68
|
+
}, [enabled]);
|
|
47
69
|
return who;
|
|
48
70
|
}
|
package/dist/screens/route.d.ts
CHANGED
|
@@ -110,34 +110,16 @@ export declare function canonicalAddress(location: {
|
|
|
110
110
|
search: string;
|
|
111
111
|
hash: string;
|
|
112
112
|
}, base: string): string | null;
|
|
113
|
-
|
|
114
|
-
export declare function canonicalizeAddress(): void;
|
|
113
|
+
export declare const useRoute: () => Route, currentRoute: () => Route, routeInMemory: (start: Route) => () => void, canonicalizeAddress: () => void;
|
|
115
114
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
115
|
+
* Go somewhere: a history entry standalone (`replace` swaps the current one, for a screen that
|
|
116
|
+
* forwards on its own), the in-memory route embedded.
|
|
118
117
|
*/
|
|
119
|
-
export declare
|
|
118
|
+
export declare const navigate: (route: Route, options?: {
|
|
119
|
+
replace?: boolean;
|
|
120
|
+
}) => void;
|
|
120
121
|
/** The page an invite link is built on: the app's own base, standalone or embedded (`routeUrl`). */
|
|
121
122
|
export declare function appLocation(): {
|
|
122
123
|
origin: string;
|
|
123
124
|
pathname: string;
|
|
124
125
|
};
|
|
125
|
-
/**
|
|
126
|
-
* Go somewhere.
|
|
127
|
-
*
|
|
128
|
-
* Writing the route's address with `history.pushState` is what puts it in the address bar and in
|
|
129
|
-
* the back/forward history; the browser dispatches nothing for that, so this tells the subscribers
|
|
130
|
-
* itself. Back and Forward arrive as `popstate` (and `hashchange`), which `moved` handles.
|
|
131
|
-
*
|
|
132
|
-
* `replace` swaps the current history entry instead of adding one, for a
|
|
133
|
-
* screen that forwards on its own: Back from where it forwarded to must not
|
|
134
|
-
* land on it again and be forwarded straight back. Arriving at the route this page already
|
|
135
|
-
* shows only corrects its address, and adds no entry.
|
|
136
|
-
*/
|
|
137
|
-
export declare function navigate(route: Route, { replace }?: {
|
|
138
|
-
replace?: boolean;
|
|
139
|
-
}): void;
|
|
140
|
-
/** The route this page is on now, read once: what `useRoute` renders, for a caller outside React. */
|
|
141
|
-
export declare function currentRoute(): Route;
|
|
142
|
-
/** The current route, re-rendering the caller when it changes. */
|
|
143
|
-
export declare function useRoute(): Route;
|
package/dist/screens/route.js
CHANGED
|
@@ -65,12 +65,12 @@
|
|
|
65
65
|
* `parseRoute`, `routeHash`, `routeAddress`, `routeUrl` and `canonicalAddress`
|
|
66
66
|
* take their inputs as arguments and touch no globals, so the interesting behaviour — what a mangled hash means,
|
|
67
67
|
* what an invite URL looks like under the `/cardtable/` prefix — is testable
|
|
68
|
-
* without a DOM.
|
|
68
|
+
* without a DOM. The browser half is `./router.ts`'s, shared by every game: only it and
|
|
69
69
|
* `appLocation` know `window` exists — and `routeInMemory` is how the embed
|
|
70
|
-
* (D21) tells
|
|
70
|
+
* (D21) tells it the address bar is not the app's.
|
|
71
71
|
*/
|
|
72
|
-
import { useMemo, useSyncExternalStore } from 'react';
|
|
73
72
|
import { registered } from "../registered.js";
|
|
73
|
+
import { createHashRouter } from "./router.js";
|
|
74
74
|
/** Whether `#/embed/<catalogue>/<id>/<view>` is one of the game's pages; any other is the lobby. */
|
|
75
75
|
const embedAddress = registered('embed address');
|
|
76
76
|
export const registerEmbedAddress = embedAddress.set;
|
|
@@ -241,119 +241,26 @@ export function canonicalAddress(location, base) {
|
|
|
241
241
|
const next = `${want.path}${location.search}${want.hash}`;
|
|
242
242
|
return next === `${location.pathname}${location.search}${location.hash}` ? null : next;
|
|
243
243
|
}
|
|
244
|
-
|
|
245
|
-
export function canonicalizeAddress() {
|
|
246
|
-
const next = canonicalAddress(globalThis.location, import.meta.env.BASE_URL);
|
|
247
|
-
if (next !== null)
|
|
248
|
-
globalThis.history.replaceState(globalThis.history.state, '', next);
|
|
249
|
-
}
|
|
250
|
-
/* ── the browser half ────────────────────────────────────────────────── */
|
|
251
|
-
const listeners = new Set();
|
|
244
|
+
/* ── the browser half: `./router.ts`, with a room's page at its path ── */
|
|
252
245
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
* there would rewrite THEIR page's route and land in their history. So the
|
|
257
|
-
* embed holds its route here instead, and `navigate` / `useRoute` read and
|
|
258
|
-
* write this rather than `location.hash`. One per page, like everything else
|
|
259
|
-
* the chrome owns ("exactly one chrome per page", `chrome/AppChrome.tsx`).
|
|
246
|
+
* Embedded in a watchparty room (D21) the URL is watchparty's: a hash written there would rewrite
|
|
247
|
+
* THEIR page's route and land in their history. So the embed holds its route in memory
|
|
248
|
+
* (`routeInMemory`), and `navigate` / `useRoute` read and write that rather than the address bar.
|
|
260
249
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
notify();
|
|
270
|
-
}
|
|
271
|
-
/** Whether `hashchange` and `popstate` are subscribed — only ever while the address bar is ours. */
|
|
272
|
-
let listening = false;
|
|
273
|
-
function syncHashListener() {
|
|
274
|
-
const want = inMemory === null && listeners.size > 0;
|
|
275
|
-
if (want === listening)
|
|
276
|
-
return;
|
|
277
|
-
listening = want;
|
|
278
|
-
for (const event of ['hashchange', 'popstate']) {
|
|
279
|
-
if (want)
|
|
280
|
-
globalThis.addEventListener(event, moved);
|
|
281
|
-
else
|
|
282
|
-
globalThis.removeEventListener(event, moved);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
function subscribeToHash(listener) {
|
|
286
|
-
listeners.add(listener);
|
|
287
|
-
syncHashListener();
|
|
288
|
-
return () => {
|
|
289
|
-
listeners.delete(listener);
|
|
290
|
-
syncHashListener();
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
/** The current route as its hash: one string, so the snapshot compares by value. */
|
|
294
|
-
function currentHash() {
|
|
295
|
-
if (inMemory !== null)
|
|
296
|
-
return inMemory.hash;
|
|
297
|
-
const location = globalThis.location;
|
|
298
|
-
if (location === undefined)
|
|
299
|
-
return '';
|
|
300
|
-
return routeHash(locationRoute(location, import.meta.env.BASE_URL));
|
|
301
|
-
}
|
|
250
|
+
const router = createHashRouter({
|
|
251
|
+
parse: parseRoute,
|
|
252
|
+
toHash: routeHash,
|
|
253
|
+
paths: { toAddress: routeAddress, fromLocation: locationRoute, canonical: canonicalAddress },
|
|
254
|
+
});
|
|
255
|
+
export const { useRoute, currentRoute, routeInMemory,
|
|
256
|
+
/** Puts this page on its route's own address, before anything reads the route and after each move. */
|
|
257
|
+
canonicalize: canonicalizeAddress, } = router;
|
|
302
258
|
/**
|
|
303
|
-
*
|
|
304
|
-
*
|
|
259
|
+
* Go somewhere: a history entry standalone (`replace` swaps the current one, for a screen that
|
|
260
|
+
* forwards on its own), the in-memory route embedded.
|
|
305
261
|
*/
|
|
306
|
-
export
|
|
307
|
-
const mine = { hash: routeHash(start) };
|
|
308
|
-
inMemory = mine;
|
|
309
|
-
syncHashListener();
|
|
310
|
-
notify();
|
|
311
|
-
return () => {
|
|
312
|
-
if (inMemory !== mine)
|
|
313
|
-
return;
|
|
314
|
-
inMemory = null;
|
|
315
|
-
syncHashListener();
|
|
316
|
-
};
|
|
317
|
-
}
|
|
262
|
+
export const navigate = router.navigate;
|
|
318
263
|
/** The page an invite link is built on: the app's own base, standalone or embedded (`routeUrl`). */
|
|
319
264
|
export function appLocation() {
|
|
320
265
|
return { origin: globalThis.location?.origin ?? '', pathname: import.meta.env?.BASE_URL ?? '/' };
|
|
321
266
|
}
|
|
322
|
-
/**
|
|
323
|
-
* Go somewhere.
|
|
324
|
-
*
|
|
325
|
-
* Writing the route's address with `history.pushState` is what puts it in the address bar and in
|
|
326
|
-
* the back/forward history; the browser dispatches nothing for that, so this tells the subscribers
|
|
327
|
-
* itself. Back and Forward arrive as `popstate` (and `hashchange`), which `moved` handles.
|
|
328
|
-
*
|
|
329
|
-
* `replace` swaps the current history entry instead of adding one, for a
|
|
330
|
-
* screen that forwards on its own: Back from where it forwarded to must not
|
|
331
|
-
* land on it again and be forwarded straight back. Arriving at the route this page already
|
|
332
|
-
* shows only corrects its address, and adds no entry.
|
|
333
|
-
*/
|
|
334
|
-
export function navigate(route, { replace = false } = {}) {
|
|
335
|
-
if (inMemory !== null)
|
|
336
|
-
inMemory.hash = routeHash(route);
|
|
337
|
-
else {
|
|
338
|
-
const location = globalThis.location;
|
|
339
|
-
const { path, hash } = routeAddress(route, import.meta.env.BASE_URL);
|
|
340
|
-
const next = `${path}${location.search}${hash}`;
|
|
341
|
-
if (next !== `${location.pathname}${location.search}${location.hash}`) {
|
|
342
|
-
const same = currentHash() === routeHash(route);
|
|
343
|
-
if (replace || same)
|
|
344
|
-
globalThis.history.replaceState(globalThis.history.state, '', next);
|
|
345
|
-
else
|
|
346
|
-
globalThis.history.pushState(null, '', next);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
notify();
|
|
350
|
-
}
|
|
351
|
-
/** The route this page is on now, read once: what `useRoute` renders, for a caller outside React. */
|
|
352
|
-
export function currentRoute() {
|
|
353
|
-
return parseRoute(currentHash());
|
|
354
|
-
}
|
|
355
|
-
/** The current route, re-rendering the caller when it changes. */
|
|
356
|
-
export function useRoute() {
|
|
357
|
-
const hash = useSyncExternalStore(subscribeToHash, currentHash, () => '');
|
|
358
|
-
return useMemo(() => parseRoute(hash), [hash]);
|
|
359
|
-
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** The page's address, as much of it as a route is read from. */
|
|
2
|
+
export interface RouteLocation {
|
|
3
|
+
pathname: string;
|
|
4
|
+
search: string;
|
|
5
|
+
hash: string;
|
|
6
|
+
}
|
|
7
|
+
/** Where a route lives when some routes are paths, not hashes. */
|
|
8
|
+
export interface RoutePaths<R> {
|
|
9
|
+
/** Where `route` lives under `base`: its path, and its hash (`''` for none). */
|
|
10
|
+
toAddress(route: R, base: string): {
|
|
11
|
+
path: string;
|
|
12
|
+
hash: string;
|
|
13
|
+
};
|
|
14
|
+
/** The route an address names under `base`. */
|
|
15
|
+
fromLocation(location: Pick<RouteLocation, 'pathname' | 'hash'>, base: string): R;
|
|
16
|
+
/** The address this page should be moved to, or null when it is already there. */
|
|
17
|
+
canonical?(location: RouteLocation, base: string): string | null;
|
|
18
|
+
}
|
|
19
|
+
export interface HashRouterOptions<R> {
|
|
20
|
+
/** A hash (`#/room/x`, tolerant of mangling) → its route. Unknown is the game's front door. */
|
|
21
|
+
parse(hash: string): R;
|
|
22
|
+
/** The inverse of `parse`: `parse(toHash(r))` is `r`. */
|
|
23
|
+
toHash(route: R): string;
|
|
24
|
+
/** Only for a game with path-addressed routes; see the header. */
|
|
25
|
+
paths?: RoutePaths<R>;
|
|
26
|
+
/** The app's base (`/bomberman/`). Default: the bundler's `BASE_URL`. */
|
|
27
|
+
base?: () => string;
|
|
28
|
+
}
|
|
29
|
+
export interface HashRouter<R> {
|
|
30
|
+
/** The current route, re-rendering the caller when it changes. */
|
|
31
|
+
useRoute(): R;
|
|
32
|
+
/** The route now, read once, for a caller outside React. */
|
|
33
|
+
currentRoute(): R;
|
|
34
|
+
/**
|
|
35
|
+
* Go somewhere. Standalone this adds a history entry, so Back works; `replace` swaps the current
|
|
36
|
+
* one instead, for a screen that forwards on its own. Arriving where the page already is adds none.
|
|
37
|
+
*/
|
|
38
|
+
navigate(route: R, options?: {
|
|
39
|
+
replace?: boolean;
|
|
40
|
+
}): void;
|
|
41
|
+
/** Route in memory from here on, starting at `start`. The returned function gives the address bar back. */
|
|
42
|
+
routeInMemory(start: R): () => void;
|
|
43
|
+
/** Puts the page on its route's own address (`paths.canonical`); a no-op for a hash-only router. */
|
|
44
|
+
canonicalize(): void;
|
|
45
|
+
/** Whether the route is in memory now (an embed is mounted). */
|
|
46
|
+
inMemory(): boolean;
|
|
47
|
+
/** Subscribes to route changes; returns the unsubscribe. */
|
|
48
|
+
subscribe(listener: () => void): () => void;
|
|
49
|
+
parse(hash: string): R;
|
|
50
|
+
toHash(route: R): string;
|
|
51
|
+
}
|
|
52
|
+
/** A router for `R`. Call once per app, at module level, and export what it returns. */
|
|
53
|
+
export declare function createHashRouter<R>({ parse, toHash, paths, base }: HashRouterOptions<R>): HashRouter<R>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A game's router: a route type `R` the game defines, and the one mechanism every game shares for
|
|
3
|
+
* where that route lives.
|
|
4
|
+
*
|
|
5
|
+
* The game hands over two pure functions — `parse(hash) → R` and `toHash(R) → '#/…'`, a round trip —
|
|
6
|
+
* and gets back `useRoute`, `navigate` and the rest. Standalone the route IS the page's address; inside
|
|
7
|
+
* a host's page (the embed, watchparty's game slot) the address bar is the host's, and nothing here may
|
|
8
|
+
* write to it: `routeInMemory` moves the route into this router for the life of the mount, and
|
|
9
|
+
* `useRoute` / `navigate` read and write that instead.
|
|
10
|
+
*
|
|
11
|
+
* ## The hash, unless the game says a route lives at a path
|
|
12
|
+
*
|
|
13
|
+
* By default every route lives in the hash, on whatever path the page is on. A game whose links must
|
|
14
|
+
* be visible to the server (cardtable's room page, whose address is a path so a pasted link unfurls)
|
|
15
|
+
* passes `paths`: where a route lives under the app's base, which route an address names, and the
|
|
16
|
+
* address a page should be moved to (`canonical`). The router then writes with `history.pushState`
|
|
17
|
+
* and puts a stray address right on every move.
|
|
18
|
+
*
|
|
19
|
+
* ## One owner of the route at a time
|
|
20
|
+
*
|
|
21
|
+
* A router is a module singleton, and the embed mounts once per page. A second `routeInMemory` while
|
|
22
|
+
* one is active throws, because two embeds sharing one route would navigate each other.
|
|
23
|
+
*/
|
|
24
|
+
import { useMemo, useSyncExternalStore } from 'react';
|
|
25
|
+
function bundlerBase() {
|
|
26
|
+
const base = import.meta.env?.BASE_URL;
|
|
27
|
+
return typeof base === 'string' && base !== '' ? base : '/';
|
|
28
|
+
}
|
|
29
|
+
/** A router for `R`. Call once per app, at module level, and export what it returns. */
|
|
30
|
+
export function createHashRouter({ parse, toHash, paths, base = bundlerBase }) {
|
|
31
|
+
const listeners = new Set();
|
|
32
|
+
let memory = null;
|
|
33
|
+
let listening = false;
|
|
34
|
+
const notify = () => {
|
|
35
|
+
for (const listener of [...listeners])
|
|
36
|
+
listener();
|
|
37
|
+
};
|
|
38
|
+
const canonicalize = () => {
|
|
39
|
+
const location = globalThis.location;
|
|
40
|
+
if (paths?.canonical === undefined || location === undefined)
|
|
41
|
+
return;
|
|
42
|
+
const next = paths.canonical(location, base());
|
|
43
|
+
if (next !== null)
|
|
44
|
+
globalThis.history.replaceState(globalThis.history.state, '', next);
|
|
45
|
+
};
|
|
46
|
+
/** The address bar moved on its own (Back, Forward, a `#` link): correct it, then re-read it. */
|
|
47
|
+
const moved = () => {
|
|
48
|
+
canonicalize();
|
|
49
|
+
notify();
|
|
50
|
+
};
|
|
51
|
+
// Subscribed to the window only while the address bar is ours and somebody is listening.
|
|
52
|
+
const syncWindow = () => {
|
|
53
|
+
const want = memory === null && listeners.size > 0;
|
|
54
|
+
if (want === listening)
|
|
55
|
+
return;
|
|
56
|
+
listening = want;
|
|
57
|
+
for (const event of ['hashchange', 'popstate']) {
|
|
58
|
+
if (want)
|
|
59
|
+
globalThis.addEventListener(event, moved);
|
|
60
|
+
else
|
|
61
|
+
globalThis.removeEventListener(event, moved);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const subscribe = (listener) => {
|
|
65
|
+
listeners.add(listener);
|
|
66
|
+
syncWindow();
|
|
67
|
+
return () => {
|
|
68
|
+
listeners.delete(listener);
|
|
69
|
+
syncWindow();
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
/** The current route as its hash: one string, so the snapshot compares by value. */
|
|
73
|
+
const currentHash = () => {
|
|
74
|
+
if (memory !== null)
|
|
75
|
+
return memory.hash;
|
|
76
|
+
const location = globalThis.location;
|
|
77
|
+
if (location === undefined)
|
|
78
|
+
return '';
|
|
79
|
+
return toHash(paths === undefined ? parse(location.hash) : paths.fromLocation(location, base()));
|
|
80
|
+
};
|
|
81
|
+
const addressOf = (route, location) => {
|
|
82
|
+
if (paths === undefined)
|
|
83
|
+
return `${location.pathname}${location.search}${toHash(route)}`;
|
|
84
|
+
const { path, hash } = paths.toAddress(route, base());
|
|
85
|
+
return `${path}${location.search}${hash}`;
|
|
86
|
+
};
|
|
87
|
+
const navigate = (route, { replace = false } = {}) => {
|
|
88
|
+
if (memory !== null)
|
|
89
|
+
memory.hash = toHash(route);
|
|
90
|
+
else {
|
|
91
|
+
const location = globalThis.location;
|
|
92
|
+
const next = addressOf(route, location);
|
|
93
|
+
if (next !== `${location.pathname}${location.search}${location.hash}`) {
|
|
94
|
+
const same = currentHash() === toHash(route);
|
|
95
|
+
if (replace || same)
|
|
96
|
+
globalThis.history.replaceState(globalThis.history.state, '', next);
|
|
97
|
+
else
|
|
98
|
+
globalThis.history.pushState(null, '', next);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
notify();
|
|
102
|
+
};
|
|
103
|
+
const routeInMemory = (start) => {
|
|
104
|
+
if (memory !== null)
|
|
105
|
+
throw new Error('route: already routing in memory — one embed per page');
|
|
106
|
+
const mine = { hash: toHash(start) };
|
|
107
|
+
memory = mine;
|
|
108
|
+
syncWindow();
|
|
109
|
+
notify();
|
|
110
|
+
return () => {
|
|
111
|
+
if (memory !== mine)
|
|
112
|
+
return;
|
|
113
|
+
memory = null;
|
|
114
|
+
syncWindow();
|
|
115
|
+
notify();
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
const useRoute = () => {
|
|
119
|
+
const hash = useSyncExternalStore(subscribe, currentHash, () => '');
|
|
120
|
+
return useMemo(() => parse(hash), [hash]);
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
useRoute,
|
|
124
|
+
currentRoute: () => parse(currentHash()),
|
|
125
|
+
navigate,
|
|
126
|
+
routeInMemory,
|
|
127
|
+
canonicalize,
|
|
128
|
+
inMemory: () => memory !== null,
|
|
129
|
+
subscribe,
|
|
130
|
+
parse,
|
|
131
|
+
toHash,
|
|
132
|
+
};
|
|
133
|
+
}
|
package/dist/sound/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { type SoundConfig } from '@wolffm/ui-shell/sound';
|
|
2
|
+
/**
|
|
3
|
+
* A game with no sound of its own registers nothing and gets {@link SILENT_SOUND}: no roles, a client
|
|
4
|
+
* nothing asks anything of. A role nobody has assigned plays nothing, so that is the whole of what a
|
|
5
|
+
* real registry would do for it.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SILENT_SOUND: SoundConfig<string>;
|
|
2
8
|
export declare const registerSound: (config: SoundConfig<string>) => void;
|
|
3
9
|
export declare const setRolePlayer: (player: import("@wolffm/ui-shell/sound").RolePlayer | null) => void, resetRolePlayerForTests: () => void, takeUiTally: () => Record<string, number>, holdMatchQuiet: () => () => void, playRole: (role: string) => void, playSoundEvents: (events: readonly import("@wolffm/ui-shell/sound").DelayedSoundEvent[]) => void, setTrackNames: (names: Readonly<Record<string, string>>) => void, getMusicTrack: () => import("@wolffm/ui-shell/sound").MusicTrack | null, subscribeMusicTrack: (fn: () => void) => () => void, setScreenMusic: (role: string | null) => void, setMatchMusic: (roles: readonly string[] | null) => void, roleHandler: (role: string) => () => void;
|
|
4
10
|
/**
|
package/dist/sound/index.js
CHANGED
|
@@ -14,7 +14,20 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { createSound } from '@wolffm/ui-shell/sound';
|
|
16
16
|
import { registered } from "../registered.js";
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* A game with no sound of its own registers nothing and gets {@link SILENT_SOUND}: no roles, a client
|
|
19
|
+
* nothing asks anything of. A role nobody has assigned plays nothing, so that is the whole of what a
|
|
20
|
+
* real registry would do for it.
|
|
21
|
+
*/
|
|
22
|
+
export const SILENT_SOUND = {
|
|
23
|
+
registry: { roles: new Map(), fallback: {}, reversed: {} },
|
|
24
|
+
client: {
|
|
25
|
+
takeUrl: (file) => file,
|
|
26
|
+
loadAssignments: () => Promise.resolve({ cells: {}, gains: {}, names: {}, versions: {}, pack: '' }),
|
|
27
|
+
loadPack: () => Promise.resolve(undefined),
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
const sound = registered('sound', () => createSound(SILENT_SOUND));
|
|
18
31
|
export const registerSound = (config) => sound.set(createSound(config));
|
|
19
32
|
/** Each name calls the registered instance when it is called, so an importer can bind it before the composition root runs. */
|
|
20
33
|
const live = new Proxy({}, { get: (_, name) => (...args) => sound.get()[name](...args) });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/game-ui",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.46",
|
|
4
|
+
"gitHead": "1e3606fe6ef84d205f62af383b2a248512158e54",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "The browser parts any hosted game shares, above ui-shell: the room shell and session, seats and looks, the room route, the front page's generic pieces (gate, polls, gallery, patch notes), the bundle slot and relay channel a game's client mounts in, settings and trim screens, the HTTP and socket helpers, and the generic components (modal, popover, avatar, action button, radio cards). Knows no game; Magic's UI (games/magic/ui) plugs in, composed by the site (apps/ui). MIT; imports only react, react-dom, socket.io-client, @wolffm/* and the platform packages.",
|
|
@@ -49,14 +49,14 @@
|
|
|
49
49
|
"react-dom": "^19.2.8"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.
|
|
53
|
-
"@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.
|
|
52
|
+
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.46",
|
|
53
|
+
"@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.46",
|
|
54
54
|
"@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.1.27",
|
|
55
|
-
"@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.
|
|
56
|
-
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.
|
|
55
|
+
"@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.111",
|
|
56
|
+
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.111",
|
|
57
57
|
"@wolffm/task-ui-components": "npm:@hadoku/task-ui-components@^4.23.35",
|
|
58
58
|
"@wolffm/themes": "npm:@hadoku/themes@^5.14.4",
|
|
59
|
-
"@wolffm/watchparty-ui": "npm:@hadoku/watchparty-ui@^0.45.
|
|
59
|
+
"@wolffm/watchparty-ui": "npm:@hadoku/watchparty-ui@^0.45.111",
|
|
60
60
|
"socket.io-client": "^4.8.3",
|
|
61
61
|
"zod": "4.5.4"
|
|
62
62
|
},
|