@diffenderfer-games/hub 2.0.0-next.0 → 2.0.0-next.1
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -21
- package/README.md +191 -139
- package/dist/doctor/cli.mjs +49 -19
- package/dist/doctor/cli.mjs.map +1 -1
- package/dist/shared/client-version.gen.js +1 -1
- package/dist/shared/daily.js +1 -1
- package/dist/shared/globals.js +8 -3
- package/dist/shared/globals.js.map +1 -1
- package/dist/shared/index.d.ts +1 -1
- package/package.json +89 -89
- package/src/auto.ts +39 -39
- package/src/core/connection.ts +299 -299
- package/src/core/errors.ts +147 -147
- package/src/core/game-data.ts +38 -38
- package/src/core/hub-economy.ts +179 -179
- package/src/core/hub-progress.ts +205 -205
- package/src/core/hub.ts +131 -131
- package/src/core/menu-control.ts +61 -61
- package/src/core/offline/events.ts +50 -50
- package/src/core/offline/favorites.ts +21 -21
- package/src/core/offline/local-store.ts +261 -261
- package/src/core/offline/queue.ts +171 -171
- package/src/core/page.ts +74 -74
- package/src/core/rest/endpoint-types.ts +22 -22
- package/src/core/rest/rest-client.ts +152 -152
- package/src/core/storage/json-guards.ts +74 -74
- package/src/core/storage/key-value.ts +66 -66
- package/src/core/ticket.ts +43 -43
- package/src/core/types.ts +134 -134
- package/src/core/version/client-version.gen.ts +1 -1
- package/src/core/version/hub-version.ts +62 -62
- package/src/core/version/semver.ts +97 -97
- package/src/daily/daily-system.ts +161 -161
- package/src/daily/page-daily.ts +36 -36
- package/src/daily/types.ts +67 -67
- package/src/daily.ts +10 -10
- package/src/doctor/checks.ts +78 -76
- package/src/doctor/cli.ts +98 -106
- package/src/doctor/hub-version-fetch.ts +43 -0
- package/src/doctor/tsconfig.json +12 -12
- package/src/globals.ts +115 -110
- package/src/index.ts +50 -50
- package/src/input/device-state.ts +146 -146
- package/src/input/edges.ts +55 -55
- package/src/input/gamepad.ts +61 -61
- package/src/input/input-events.ts +32 -32
- package/src/input/input-registry.ts +164 -164
- package/src/input/input-system.ts +393 -393
- package/src/input/legacy-controls.ts +64 -64
- package/src/input/nav-state.ts +191 -191
- package/src/input/navigator.ts +171 -171
- package/src/input/on-screen-controls.ts +101 -101
- package/src/input/page-control.ts +43 -43
- package/src/input/page-input.ts +193 -193
- package/src/input/player-remaps.ts +77 -77
- package/src/input/source-values.ts +116 -116
- package/src/input/spatial.ts +47 -47
- package/src/input/types.ts +331 -331
- package/src/input/zero-config.ts +177 -177
- package/src/input.ts +22 -22
- package/src/mp.ts +10 -10
- package/src/overlay/controls-panel.ts +163 -163
- package/src/overlay/dom.ts +46 -46
- package/src/overlay/layout-clusters.ts +162 -162
- package/src/overlay/layout-geometry.ts +136 -136
- package/src/overlay/layout.ts +203 -203
- package/src/overlay/listener-scope.ts +23 -23
- package/src/overlay/on-screen-keyboard.ts +137 -137
- package/src/overlay/overlay.ts +208 -208
- package/src/overlay/styles.ts +9 -9
- package/src/overlay/virtual-controls.ts +262 -262
- package/src/race.ts +10 -10
- package/src/realtime/bridge.ts +121 -121
- package/src/realtime/cross-bundle.ts +29 -29
- package/src/realtime/fallback-notice.ts +48 -48
- package/src/realtime/hub-link.ts +65 -65
- package/src/realtime/latest-state.ts +33 -33
- package/src/realtime/multiplayer-forwarder.ts +28 -28
- package/src/realtime/multiplayer-types.ts +52 -52
- package/src/realtime/page-runtime.ts +60 -60
- package/src/realtime/race-forwarder.ts +59 -59
- package/src/realtime/race-types.ts +132 -132
- package/src/realtime/room-types.ts +125 -125
- package/src/realtime/rooms-forwarder.ts +79 -79
- package/src/realtime/runtime-api.ts +49 -49
- package/src/realtime/runtime-opener.ts +61 -61
- package/src/realtime/social-forwarder.ts +72 -72
- package/src/realtime/social-types.ts +78 -78
- package/src/realtime/types.ts +53 -53
- package/src/realtime/wire-types.ts +72 -72
- package/src/rooms.ts +10 -10
- package/src/social.ts +18 -18
- package/src/types.ts +13 -13
- package/src/uistack/page-stack.ts +45 -45
- package/src/uistack/play-suspension.ts +82 -82
- package/src/uistack/types.ts +76 -76
- package/src/uistack/ui-stack.ts +144 -144
package/src/core/hub.ts
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The `Hub` client: every REST method (in `hub-progress.ts` and
|
|
3
|
-
* `hub-economy.ts`), the hub's version, and the page-wide namespaces as lazy
|
|
4
|
-
* getters (`hub.input`, `hub.daily`, `hub.social`, `hub.mp`, `hub.rooms`,
|
|
5
|
-
* `hub.race`, ...), so v1 code such as `hub.input.define(...)` works unchanged.
|
|
6
|
-
*/
|
|
7
|
-
import type { HubVersion } from '@diffenderfer-games/hub-contract';
|
|
8
|
-
import { getDaily, type DailyHost, type DailySystem } from '../daily';
|
|
9
|
-
import { getInput, getUiStack, type InputSystem } from '../input';
|
|
10
|
-
import { mp } from '../mp';
|
|
11
|
-
import { race } from '../race';
|
|
12
|
-
import { rooms } from '../rooms';
|
|
13
|
-
import { linkRealtimeToHub, notify, presence, social } from '../social';
|
|
14
|
-
import { OfflineError, toHubError } from './errors';
|
|
15
|
-
import { HubEconomy } from './hub-economy';
|
|
16
|
-
import { hubMenu, type HubMenuControl } from './menu-control';
|
|
17
|
-
import { emitWindowEvent } from './page';
|
|
18
|
-
import { fetchGameServerTicket } from './ticket';
|
|
19
|
-
import { clientSupport, readHubVersion, type ClientSupport } from './version/hub-version';
|
|
20
|
-
import { HUB_CLIENT_VERSION } from './version/client-version.gen';
|
|
21
|
-
import type { HubOptions } from './types';
|
|
22
|
-
|
|
23
|
-
/** The hub client. Use the ready-made {@link hub}, or `createHub()` for another slug or API base. */
|
|
24
|
-
export class Hub extends HubEconomy {
|
|
25
|
-
/** This client's package version (sent as `x-hub-client`). */
|
|
26
|
-
readonly clientVersion: string = HUB_CLIENT_VERSION;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The hub's version and the client versions it supports (`GET /_api/version`),
|
|
30
|
-
* read once per page. `null` for a hub from before versioning.
|
|
31
|
-
*/
|
|
32
|
-
hubVersion(): Promise<HubVersion | null> {
|
|
33
|
-
return readHubVersion(this.rest);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** How the hub regards this client: `supported`, `deprecated`, `unsupported`, or `unknown` (an older hub). */
|
|
37
|
-
async clientSupport(): Promise<ClientSupport> {
|
|
38
|
-
return clientSupport(await this.hubVersion());
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The input system (keyboard, touch and gamepad): `hub.input.define({ ... })`,
|
|
43
|
-
* `hub.input.value('jump')`. Page-wide; remaps are saved locally and to the
|
|
44
|
-
* reserved `__controls` save slot.
|
|
45
|
-
*/
|
|
46
|
-
get input(): InputSystem {
|
|
47
|
-
return getInput({
|
|
48
|
-
slug: this.slug,
|
|
49
|
-
saveToCloud: (overrides) => {
|
|
50
|
-
void this.putSave('__controls', overrides).catch(() => undefined);
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Hub UI covering the game (menu, dialogs, chat): open/close events and `autoPause`. Page-wide. */
|
|
56
|
-
get overlay(): ReturnType<typeof getUiStack> {
|
|
57
|
-
return getUiStack();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Show or hide the injected hub menu button (page-wide). */
|
|
61
|
-
get menu(): HubMenuControl {
|
|
62
|
-
return hubMenu;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Daily challenges: call `hub.daily.define({ play })` once at load and
|
|
67
|
-
* `hub.daily.complete({ ... })` on finish. Page-wide, shared with the
|
|
68
|
-
* injected menu, which can start a clicked day on the same instance.
|
|
69
|
-
*/
|
|
70
|
-
get daily(): DailySystem {
|
|
71
|
-
return getDaily(this.dailyHost());
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Tell friends what the player is doing in this game. */
|
|
75
|
-
get presence(): typeof presence {
|
|
76
|
-
return presence;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Friends, players and the hub's social dialogs. */
|
|
80
|
-
get social(): typeof social {
|
|
81
|
-
return social;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Invites, launches, the leave guard and own-server tickets. */
|
|
85
|
-
get mp(): typeof mp {
|
|
86
|
-
return mp;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** Hub relay rooms, for games without a server. */
|
|
90
|
-
get rooms(): typeof rooms {
|
|
91
|
-
return rooms;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Toast behaviour during intense play. */
|
|
95
|
-
get notify(): typeof notify {
|
|
96
|
-
return notify;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Races: a single-player game becomes multiplayer by racing. */
|
|
100
|
-
get race(): typeof race {
|
|
101
|
-
return race;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** What the daily system needs from this client: its REST calls and window events. */
|
|
105
|
-
private dailyHost(): DailyHost {
|
|
106
|
-
return {
|
|
107
|
-
slug: this.slug,
|
|
108
|
-
status: () => this.rest.call('getGameDailyChallenges', this.gameFields()),
|
|
109
|
-
all: () => this.rest.call('listDailyChallengesForAllGames'),
|
|
110
|
-
start: (day) => this.rest.call('startDailyChallenge', { ...this.gameFields(), day }),
|
|
111
|
-
complete: (day, token, meta) => this.rest.call('completeDailyChallenge', { ...this.gameFields(), day, token, meta }),
|
|
112
|
-
emit: (eventName, detail) => {
|
|
113
|
-
emitWindowEvent(eventName, detail);
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
linkRealtimeToHub({
|
|
120
|
-
toHubError,
|
|
121
|
-
createOfflineError: (message) => new OfflineError(message),
|
|
122
|
-
getGameServerTicket: fetchGameServerTicket,
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
/** A ready-to-use client, with the slug and API base detected from the page. */
|
|
126
|
-
export const hub: Hub = new Hub();
|
|
127
|
-
|
|
128
|
-
/** Create a client for another slug or API base, or with other offline behaviour. */
|
|
129
|
-
export function createHub(options: HubOptions = {}): Hub {
|
|
130
|
-
return new Hub(options);
|
|
131
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The `Hub` client: every REST method (in `hub-progress.ts` and
|
|
3
|
+
* `hub-economy.ts`), the hub's version, and the page-wide namespaces as lazy
|
|
4
|
+
* getters (`hub.input`, `hub.daily`, `hub.social`, `hub.mp`, `hub.rooms`,
|
|
5
|
+
* `hub.race`, ...), so v1 code such as `hub.input.define(...)` works unchanged.
|
|
6
|
+
*/
|
|
7
|
+
import type { HubVersion } from '@diffenderfer-games/hub-contract';
|
|
8
|
+
import { getDaily, type DailyHost, type DailySystem } from '../daily';
|
|
9
|
+
import { getInput, getUiStack, type InputSystem } from '../input';
|
|
10
|
+
import { mp } from '../mp';
|
|
11
|
+
import { race } from '../race';
|
|
12
|
+
import { rooms } from '../rooms';
|
|
13
|
+
import { linkRealtimeToHub, notify, presence, social } from '../social';
|
|
14
|
+
import { OfflineError, toHubError } from './errors';
|
|
15
|
+
import { HubEconomy } from './hub-economy';
|
|
16
|
+
import { hubMenu, type HubMenuControl } from './menu-control';
|
|
17
|
+
import { emitWindowEvent } from './page';
|
|
18
|
+
import { fetchGameServerTicket } from './ticket';
|
|
19
|
+
import { clientSupport, readHubVersion, type ClientSupport } from './version/hub-version';
|
|
20
|
+
import { HUB_CLIENT_VERSION } from './version/client-version.gen';
|
|
21
|
+
import type { HubOptions } from './types';
|
|
22
|
+
|
|
23
|
+
/** The hub client. Use the ready-made {@link hub}, or `createHub()` for another slug or API base. */
|
|
24
|
+
export class Hub extends HubEconomy {
|
|
25
|
+
/** This client's package version (sent as `x-hub-client`). */
|
|
26
|
+
readonly clientVersion: string = HUB_CLIENT_VERSION;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The hub's version and the client versions it supports (`GET /_api/version`),
|
|
30
|
+
* read once per page. `null` for a hub from before versioning.
|
|
31
|
+
*/
|
|
32
|
+
hubVersion(): Promise<HubVersion | null> {
|
|
33
|
+
return readHubVersion(this.rest);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** How the hub regards this client: `supported`, `deprecated`, `unsupported`, or `unknown` (an older hub). */
|
|
37
|
+
async clientSupport(): Promise<ClientSupport> {
|
|
38
|
+
return clientSupport(await this.hubVersion());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The input system (keyboard, touch and gamepad): `hub.input.define({ ... })`,
|
|
43
|
+
* `hub.input.value('jump')`. Page-wide; remaps are saved locally and to the
|
|
44
|
+
* reserved `__controls` save slot.
|
|
45
|
+
*/
|
|
46
|
+
get input(): InputSystem {
|
|
47
|
+
return getInput({
|
|
48
|
+
slug: this.slug,
|
|
49
|
+
saveToCloud: (overrides) => {
|
|
50
|
+
void this.putSave('__controls', overrides).catch(() => undefined);
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Hub UI covering the game (menu, dialogs, chat): open/close events and `autoPause`. Page-wide. */
|
|
56
|
+
get overlay(): ReturnType<typeof getUiStack> {
|
|
57
|
+
return getUiStack();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Show or hide the injected hub menu button (page-wide). */
|
|
61
|
+
get menu(): HubMenuControl {
|
|
62
|
+
return hubMenu;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Daily challenges: call `hub.daily.define({ play })` once at load and
|
|
67
|
+
* `hub.daily.complete({ ... })` on finish. Page-wide, shared with the
|
|
68
|
+
* injected menu, which can start a clicked day on the same instance.
|
|
69
|
+
*/
|
|
70
|
+
get daily(): DailySystem {
|
|
71
|
+
return getDaily(this.dailyHost());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Tell friends what the player is doing in this game. */
|
|
75
|
+
get presence(): typeof presence {
|
|
76
|
+
return presence;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Friends, players and the hub's social dialogs. */
|
|
80
|
+
get social(): typeof social {
|
|
81
|
+
return social;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Invites, launches, the leave guard and own-server tickets. */
|
|
85
|
+
get mp(): typeof mp {
|
|
86
|
+
return mp;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Hub relay rooms, for games without a server. */
|
|
90
|
+
get rooms(): typeof rooms {
|
|
91
|
+
return rooms;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Toast behaviour during intense play. */
|
|
95
|
+
get notify(): typeof notify {
|
|
96
|
+
return notify;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Races: a single-player game becomes multiplayer by racing. */
|
|
100
|
+
get race(): typeof race {
|
|
101
|
+
return race;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** What the daily system needs from this client: its REST calls and window events. */
|
|
105
|
+
private dailyHost(): DailyHost {
|
|
106
|
+
return {
|
|
107
|
+
slug: this.slug,
|
|
108
|
+
status: () => this.rest.call('getGameDailyChallenges', this.gameFields()),
|
|
109
|
+
all: () => this.rest.call('listDailyChallengesForAllGames'),
|
|
110
|
+
start: (day) => this.rest.call('startDailyChallenge', { ...this.gameFields(), day }),
|
|
111
|
+
complete: (day, token, meta) => this.rest.call('completeDailyChallenge', { ...this.gameFields(), day, token, meta }),
|
|
112
|
+
emit: (eventName, detail) => {
|
|
113
|
+
emitWindowEvent(eventName, detail);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
linkRealtimeToHub({
|
|
120
|
+
toHubError,
|
|
121
|
+
createOfflineError: (message) => new OfflineError(message),
|
|
122
|
+
getGameServerTicket: fetchGameServerTicket,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/** A ready-to-use client, with the slug and API base detected from the page. */
|
|
126
|
+
export const hub: Hub = new Hub();
|
|
127
|
+
|
|
128
|
+
/** Create a client for another slug or API base, or with other offline behaviour. */
|
|
129
|
+
export function createHub(options: HubOptions = {}): Hub {
|
|
130
|
+
return new Hub(options);
|
|
131
|
+
}
|
package/src/core/menu-control.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `hub.menu`: show or hide the injected hub menu button. The menu
|
|
3
|
-
* (`/_hub/menu.js`) may load before or after the game, and the game may
|
|
4
|
-
* bundle its own client copy, so the shared state is a window global and a
|
|
5
|
-
* window event rather than an object:
|
|
6
|
-
*
|
|
7
|
-
* - `window.__HUB_MENU_VISIBLE__` (boolean; absent means visible);
|
|
8
|
-
* - `hub:menu-visibility` (`CustomEvent`, detail `{ visible }`).
|
|
9
|
-
*
|
|
10
|
-
* The menu reads the global on boot and listens for the event.
|
|
11
|
-
*/
|
|
12
|
-
import { emitWindowEvent, isBrowserPage } from './page';
|
|
13
|
-
|
|
14
|
-
declare global {
|
|
15
|
-
interface Window {
|
|
16
|
-
/** The last requested menu-button visibility (absent: visible). */
|
|
17
|
-
__HUB_MENU_VISIBLE__?: boolean;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Control over the injected hub menu button. */
|
|
22
|
-
export interface HubMenuControl {
|
|
23
|
-
/**
|
|
24
|
-
* Show (`true`) or hide (`false`) the menu button. Hidden means faded out,
|
|
25
|
-
* not focusable and `aria-hidden`; an open drawer stays open until closed,
|
|
26
|
-
* and the gamepad Select button still opens it. A small corner hotspot
|
|
27
|
-
* peeks it back for a few seconds on hover or tap, so players can always
|
|
28
|
-
* reach it.
|
|
29
|
-
*/
|
|
30
|
-
setVisible(isVisible: boolean): void;
|
|
31
|
-
/** Show the menu button. */
|
|
32
|
-
show(): void;
|
|
33
|
-
/** Hide the menu button. */
|
|
34
|
-
hide(): void;
|
|
35
|
-
/** The last requested visibility (`true` unless a game hid it). */
|
|
36
|
-
readonly visible: boolean;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** The page's menu control (one per page; every client copy shares the window state). */
|
|
40
|
-
export const hubMenu: HubMenuControl = {
|
|
41
|
-
setVisible(isVisible: boolean): void {
|
|
42
|
-
if (!isBrowserPage()) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const wasVisible = window.__HUB_MENU_VISIBLE__;
|
|
46
|
-
if (wasVisible === isVisible || (wasVisible === undefined && isVisible)) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
window.__HUB_MENU_VISIBLE__ = isVisible;
|
|
50
|
-
emitWindowEvent('hub:menu-visibility', { visible: isVisible });
|
|
51
|
-
},
|
|
52
|
-
show(): void {
|
|
53
|
-
hubMenu.setVisible(true);
|
|
54
|
-
},
|
|
55
|
-
hide(): void {
|
|
56
|
-
hubMenu.setVisible(false);
|
|
57
|
-
},
|
|
58
|
-
get visible(): boolean {
|
|
59
|
-
return !isBrowserPage() || window.__HUB_MENU_VISIBLE__ !== false;
|
|
60
|
-
},
|
|
61
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* `hub.menu`: show or hide the injected hub menu button. The menu
|
|
3
|
+
* (`/_hub/menu.js`) may load before or after the game, and the game may
|
|
4
|
+
* bundle its own client copy, so the shared state is a window global and a
|
|
5
|
+
* window event rather than an object:
|
|
6
|
+
*
|
|
7
|
+
* - `window.__HUB_MENU_VISIBLE__` (boolean; absent means visible);
|
|
8
|
+
* - `hub:menu-visibility` (`CustomEvent`, detail `{ visible }`).
|
|
9
|
+
*
|
|
10
|
+
* The menu reads the global on boot and listens for the event.
|
|
11
|
+
*/
|
|
12
|
+
import { emitWindowEvent, isBrowserPage } from './page';
|
|
13
|
+
|
|
14
|
+
declare global {
|
|
15
|
+
interface Window {
|
|
16
|
+
/** The last requested menu-button visibility (absent: visible). */
|
|
17
|
+
__HUB_MENU_VISIBLE__?: boolean;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Control over the injected hub menu button. */
|
|
22
|
+
export interface HubMenuControl {
|
|
23
|
+
/**
|
|
24
|
+
* Show (`true`) or hide (`false`) the menu button. Hidden means faded out,
|
|
25
|
+
* not focusable and `aria-hidden`; an open drawer stays open until closed,
|
|
26
|
+
* and the gamepad Select button still opens it. A small corner hotspot
|
|
27
|
+
* peeks it back for a few seconds on hover or tap, so players can always
|
|
28
|
+
* reach it.
|
|
29
|
+
*/
|
|
30
|
+
setVisible(isVisible: boolean): void;
|
|
31
|
+
/** Show the menu button. */
|
|
32
|
+
show(): void;
|
|
33
|
+
/** Hide the menu button. */
|
|
34
|
+
hide(): void;
|
|
35
|
+
/** The last requested visibility (`true` unless a game hid it). */
|
|
36
|
+
readonly visible: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The page's menu control (one per page; every client copy shares the window state). */
|
|
40
|
+
export const hubMenu: HubMenuControl = {
|
|
41
|
+
setVisible(isVisible: boolean): void {
|
|
42
|
+
if (!isBrowserPage()) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const wasVisible = window.__HUB_MENU_VISIBLE__;
|
|
46
|
+
if (wasVisible === isVisible || (wasVisible === undefined && isVisible)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
window.__HUB_MENU_VISIBLE__ = isVisible;
|
|
50
|
+
emitWindowEvent('hub:menu-visibility', { visible: isVisible });
|
|
51
|
+
},
|
|
52
|
+
show(): void {
|
|
53
|
+
hubMenu.setVisible(true);
|
|
54
|
+
},
|
|
55
|
+
hide(): void {
|
|
56
|
+
hubMenu.setVisible(false);
|
|
57
|
+
},
|
|
58
|
+
get visible(): boolean {
|
|
59
|
+
return !isBrowserPage() || window.__HUB_MENU_VISIBLE__ !== false;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Analytics events recorded while offline (`play`, `heartbeat`, custom
|
|
3
|
-
* events), kept under `hub:<slug>:events` and sent when the hub is back.
|
|
4
|
-
* The buffer is capped; heartbeats are dropped first, then the oldest.
|
|
5
|
-
*/
|
|
6
|
-
import { isRecord, optionalNumber, optionalString } from '../storage/json-guards';
|
|
7
|
-
|
|
8
|
-
/** The most events kept while offline. */
|
|
9
|
-
export const EVENT_BUFFER_CAP = 100;
|
|
10
|
-
|
|
11
|
-
/** One buffered event. */
|
|
12
|
-
export interface BufferedEvent {
|
|
13
|
-
/** The event type, as `recordGameEvent` sends it. */
|
|
14
|
-
readonly type: string;
|
|
15
|
-
/** When it happened (epoch ms). */
|
|
16
|
-
readonly ts: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** The well-formed events of a stored buffer. */
|
|
20
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a buffer read back from localStorage (possibly written by a v1 client)
|
|
21
|
-
export function readEvents(stored: unknown): BufferedEvent[] {
|
|
22
|
-
if (!Array.isArray(stored)) {
|
|
23
|
-
return [];
|
|
24
|
-
}
|
|
25
|
-
return stored.flatMap((entry): BufferedEvent[] => {
|
|
26
|
-
if (!isRecord(entry)) {
|
|
27
|
-
return [];
|
|
28
|
-
}
|
|
29
|
-
const type = optionalString(entry.type);
|
|
30
|
-
const ts = optionalNumber(entry.ts);
|
|
31
|
-
return type === undefined || ts === undefined ? [] : [{ type, ts }];
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** The buffer with one more event, trimmed to {@link EVENT_BUFFER_CAP}: oldest heartbeats go first, then the oldest events. */
|
|
36
|
-
export function appendEvent(events: readonly BufferedEvent[], incoming: BufferedEvent): BufferedEvent[] {
|
|
37
|
-
let buffered = [...events, incoming];
|
|
38
|
-
let excess = buffered.length - EVENT_BUFFER_CAP;
|
|
39
|
-
if (excess <= 0) {
|
|
40
|
-
return buffered;
|
|
41
|
-
}
|
|
42
|
-
buffered = buffered.filter((event) => {
|
|
43
|
-
if (excess > 0 && event.type === 'heartbeat') {
|
|
44
|
-
excess -= 1;
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
return true;
|
|
48
|
-
});
|
|
49
|
-
return buffered.length > EVENT_BUFFER_CAP ? buffered.slice(buffered.length - EVENT_BUFFER_CAP) : buffered;
|
|
50
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Analytics events recorded while offline (`play`, `heartbeat`, custom
|
|
3
|
+
* events), kept under `hub:<slug>:events` and sent when the hub is back.
|
|
4
|
+
* The buffer is capped; heartbeats are dropped first, then the oldest.
|
|
5
|
+
*/
|
|
6
|
+
import { isRecord, optionalNumber, optionalString } from '../storage/json-guards';
|
|
7
|
+
|
|
8
|
+
/** The most events kept while offline. */
|
|
9
|
+
export const EVENT_BUFFER_CAP = 100;
|
|
10
|
+
|
|
11
|
+
/** One buffered event. */
|
|
12
|
+
export interface BufferedEvent {
|
|
13
|
+
/** The event type, as `recordGameEvent` sends it. */
|
|
14
|
+
readonly type: string;
|
|
15
|
+
/** When it happened (epoch ms). */
|
|
16
|
+
readonly ts: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The well-formed events of a stored buffer. */
|
|
20
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a buffer read back from localStorage (possibly written by a v1 client)
|
|
21
|
+
export function readEvents(stored: unknown): BufferedEvent[] {
|
|
22
|
+
if (!Array.isArray(stored)) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
return stored.flatMap((entry): BufferedEvent[] => {
|
|
26
|
+
if (!isRecord(entry)) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
const type = optionalString(entry.type);
|
|
30
|
+
const ts = optionalNumber(entry.ts);
|
|
31
|
+
return type === undefined || ts === undefined ? [] : [{ type, ts }];
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The buffer with one more event, trimmed to {@link EVENT_BUFFER_CAP}: oldest heartbeats go first, then the oldest events. */
|
|
36
|
+
export function appendEvent(events: readonly BufferedEvent[], incoming: BufferedEvent): BufferedEvent[] {
|
|
37
|
+
let buffered = [...events, incoming];
|
|
38
|
+
let excess = buffered.length - EVENT_BUFFER_CAP;
|
|
39
|
+
if (excess <= 0) {
|
|
40
|
+
return buffered;
|
|
41
|
+
}
|
|
42
|
+
buffered = buffered.filter((event) => {
|
|
43
|
+
if (excess > 0 && event.type === 'heartbeat') {
|
|
44
|
+
excess -= 1;
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
});
|
|
49
|
+
return buffered.length > EVENT_BUFFER_CAP ? buffered.slice(buffered.length - EVENT_BUFFER_CAP) : buffered;
|
|
50
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Starred games are site-wide, not per game, so they live under one global
|
|
3
|
-
* key (`hub:favorites`, the v1 client's) rather than in a game's cache. The
|
|
4
|
-
* list mirrors the hub's so the menu still shows it offline.
|
|
5
|
-
*/
|
|
6
|
-
import { parseJsonText, stringsIn } from '../storage/json-guards';
|
|
7
|
-
import { createPageStore } from '../storage/key-value';
|
|
8
|
-
|
|
9
|
-
/** The key of the mirrored favorites list. */
|
|
10
|
-
export const FAVORITES_KEY = 'hub:favorites';
|
|
11
|
-
|
|
12
|
-
/** The mirrored favorite slugs (empty when none or unreadable). */
|
|
13
|
-
export function readLocalFavorites(): string[] {
|
|
14
|
-
const text = createPageStore().read(FAVORITES_KEY);
|
|
15
|
-
return text === null ? [] : stringsIn(parseJsonText(text));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** Mirror the hub's favorites list. */
|
|
19
|
-
export function writeLocalFavorites(games: readonly string[]): void {
|
|
20
|
-
createPageStore().write(FAVORITES_KEY, JSON.stringify(games));
|
|
21
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Starred games are site-wide, not per game, so they live under one global
|
|
3
|
+
* key (`hub:favorites`, the v1 client's) rather than in a game's cache. The
|
|
4
|
+
* list mirrors the hub's so the menu still shows it offline.
|
|
5
|
+
*/
|
|
6
|
+
import { parseJsonText, stringsIn } from '../storage/json-guards';
|
|
7
|
+
import { createPageStore } from '../storage/key-value';
|
|
8
|
+
|
|
9
|
+
/** The key of the mirrored favorites list. */
|
|
10
|
+
export const FAVORITES_KEY = 'hub:favorites';
|
|
11
|
+
|
|
12
|
+
/** The mirrored favorite slugs (empty when none or unreadable). */
|
|
13
|
+
export function readLocalFavorites(): string[] {
|
|
14
|
+
const text = createPageStore().read(FAVORITES_KEY);
|
|
15
|
+
return text === null ? [] : stringsIn(parseJsonText(text));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Mirror the hub's favorites list. */
|
|
19
|
+
export function writeLocalFavorites(games: readonly string[]): void {
|
|
20
|
+
createPageStore().write(FAVORITES_KEY, JSON.stringify(games));
|
|
21
|
+
}
|