@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
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Just enough semver for version ranges the hub sends (`>=2.0.0-0 <3.0.0`,
|
|
3
|
-
* `<2.2.0`): comparators separated by spaces must all hold, `||` separates
|
|
4
|
-
* alternatives. Versions compare in plain semver order, prereleases included
|
|
5
|
-
* (`2.0.0-next.1 < 2.0.0`); unlike npm, a prerelease is not excluded from a
|
|
6
|
-
* range of another release line, which is why the hub writes `-0` floors.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/** A parsed `major.minor.patch[-prerelease]` version. */
|
|
10
|
-
export interface ParsedVersion {
|
|
11
|
-
/** `[major, minor, patch]`. */
|
|
12
|
-
readonly release: readonly [number, number, number];
|
|
13
|
-
/** The dot-separated prerelease identifiers (empty for a release). */
|
|
14
|
-
readonly prerelease: readonly string[];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const VERSION_PATTERN = /^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
18
|
-
|
|
19
|
-
/** Parse a version (`2`, `2.1`, `2.1.0`, `2.1.0-next.3`); `undefined` when it is not one. */
|
|
20
|
-
export function parseVersion(text: string): ParsedVersion | undefined {
|
|
21
|
-
const match = VERSION_PATTERN.exec(text.trim());
|
|
22
|
-
if (match === null) {
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
const [, major = '0', minor = '0', patch = '0', prerelease] = match;
|
|
26
|
-
return { release: [Number(major), Number(minor), Number(patch)], prerelease: prerelease === undefined ? [] : prerelease.split('.') };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Compare two prerelease identifiers: numbers by value and before words, words as text. */
|
|
30
|
-
function compareIdentifiers(first: string, second: string): number {
|
|
31
|
-
const isFirstNumeric = /^\d+$/.test(first);
|
|
32
|
-
const isSecondNumeric = /^\d+$/.test(second);
|
|
33
|
-
if (isFirstNumeric && isSecondNumeric) {
|
|
34
|
-
return Number(first) - Number(second);
|
|
35
|
-
}
|
|
36
|
-
if (isFirstNumeric !== isSecondNumeric) {
|
|
37
|
-
return isFirstNumeric ? -1 : 1;
|
|
38
|
-
}
|
|
39
|
-
return first < second ? -1 : first > second ? 1 : 0;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Negative when `first` is older, positive when newer, 0 when equal. */
|
|
43
|
-
export function compareVersions(first: ParsedVersion, second: ParsedVersion): number {
|
|
44
|
-
for (let i = 0; i < 3; i += 1) {
|
|
45
|
-
const difference = (first.release[i] ?? 0) - (second.release[i] ?? 0);
|
|
46
|
-
if (difference !== 0) {
|
|
47
|
-
return difference;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
if (first.prerelease.length === 0 || second.prerelease.length === 0) {
|
|
51
|
-
return second.prerelease.length - first.prerelease.length;
|
|
52
|
-
}
|
|
53
|
-
const length = Math.max(first.prerelease.length, second.prerelease.length);
|
|
54
|
-
for (let i = 0; i < length; i += 1) {
|
|
55
|
-
const firstIdentifier = first.prerelease[i];
|
|
56
|
-
const secondIdentifier = second.prerelease[i];
|
|
57
|
-
if (firstIdentifier === undefined || secondIdentifier === undefined) {
|
|
58
|
-
return firstIdentifier === undefined ? -1 : 1;
|
|
59
|
-
}
|
|
60
|
-
const difference = compareIdentifiers(firstIdentifier, secondIdentifier);
|
|
61
|
-
if (difference !== 0) {
|
|
62
|
-
return difference;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return 0;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const COMPARATOR_PATTERN = /^(>=|<=|>|<|=)?\s*(.+)$/;
|
|
69
|
-
|
|
70
|
-
/** Whether `version` meets one comparator (`>=2.0.0`); a malformed comparator never holds. */
|
|
71
|
-
function meetsComparator(version: ParsedVersion, comparator: string): boolean {
|
|
72
|
-
const match = COMPARATOR_PATTERN.exec(comparator);
|
|
73
|
-
const bound = match?.[2] === undefined ? undefined : parseVersion(match[2]);
|
|
74
|
-
if (match === null || bound === undefined) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
const order = compareVersions(version, bound);
|
|
78
|
-
switch (match[1]) {
|
|
79
|
-
case '>=': return order >= 0;
|
|
80
|
-
case '<=': return order <= 0;
|
|
81
|
-
case '>': return order > 0;
|
|
82
|
-
case '<': return order < 0;
|
|
83
|
-
default: return order === 0;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Whether `versionText` is in `range`; a malformed version or empty range never is. */
|
|
88
|
-
export function satisfiesRange(versionText: string, range: string): boolean {
|
|
89
|
-
const version = parseVersion(versionText);
|
|
90
|
-
if (version === undefined) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
return range.split('||').some((alternative) => {
|
|
94
|
-
const comparators = alternative.trim().split(/\s+/).filter((comparator) => comparator !== '');
|
|
95
|
-
return comparators.length > 0 && comparators.every((comparator) => meetsComparator(version, comparator));
|
|
96
|
-
});
|
|
97
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Just enough semver for version ranges the hub sends (`>=2.0.0-0 <3.0.0`,
|
|
3
|
+
* `<2.2.0`): comparators separated by spaces must all hold, `||` separates
|
|
4
|
+
* alternatives. Versions compare in plain semver order, prereleases included
|
|
5
|
+
* (`2.0.0-next.1 < 2.0.0`); unlike npm, a prerelease is not excluded from a
|
|
6
|
+
* range of another release line, which is why the hub writes `-0` floors.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** A parsed `major.minor.patch[-prerelease]` version. */
|
|
10
|
+
export interface ParsedVersion {
|
|
11
|
+
/** `[major, minor, patch]`. */
|
|
12
|
+
readonly release: readonly [number, number, number];
|
|
13
|
+
/** The dot-separated prerelease identifiers (empty for a release). */
|
|
14
|
+
readonly prerelease: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const VERSION_PATTERN = /^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
18
|
+
|
|
19
|
+
/** Parse a version (`2`, `2.1`, `2.1.0`, `2.1.0-next.3`); `undefined` when it is not one. */
|
|
20
|
+
export function parseVersion(text: string): ParsedVersion | undefined {
|
|
21
|
+
const match = VERSION_PATTERN.exec(text.trim());
|
|
22
|
+
if (match === null) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const [, major = '0', minor = '0', patch = '0', prerelease] = match;
|
|
26
|
+
return { release: [Number(major), Number(minor), Number(patch)], prerelease: prerelease === undefined ? [] : prerelease.split('.') };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Compare two prerelease identifiers: numbers by value and before words, words as text. */
|
|
30
|
+
function compareIdentifiers(first: string, second: string): number {
|
|
31
|
+
const isFirstNumeric = /^\d+$/.test(first);
|
|
32
|
+
const isSecondNumeric = /^\d+$/.test(second);
|
|
33
|
+
if (isFirstNumeric && isSecondNumeric) {
|
|
34
|
+
return Number(first) - Number(second);
|
|
35
|
+
}
|
|
36
|
+
if (isFirstNumeric !== isSecondNumeric) {
|
|
37
|
+
return isFirstNumeric ? -1 : 1;
|
|
38
|
+
}
|
|
39
|
+
return first < second ? -1 : first > second ? 1 : 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Negative when `first` is older, positive when newer, 0 when equal. */
|
|
43
|
+
export function compareVersions(first: ParsedVersion, second: ParsedVersion): number {
|
|
44
|
+
for (let i = 0; i < 3; i += 1) {
|
|
45
|
+
const difference = (first.release[i] ?? 0) - (second.release[i] ?? 0);
|
|
46
|
+
if (difference !== 0) {
|
|
47
|
+
return difference;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (first.prerelease.length === 0 || second.prerelease.length === 0) {
|
|
51
|
+
return second.prerelease.length - first.prerelease.length;
|
|
52
|
+
}
|
|
53
|
+
const length = Math.max(first.prerelease.length, second.prerelease.length);
|
|
54
|
+
for (let i = 0; i < length; i += 1) {
|
|
55
|
+
const firstIdentifier = first.prerelease[i];
|
|
56
|
+
const secondIdentifier = second.prerelease[i];
|
|
57
|
+
if (firstIdentifier === undefined || secondIdentifier === undefined) {
|
|
58
|
+
return firstIdentifier === undefined ? -1 : 1;
|
|
59
|
+
}
|
|
60
|
+
const difference = compareIdentifiers(firstIdentifier, secondIdentifier);
|
|
61
|
+
if (difference !== 0) {
|
|
62
|
+
return difference;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const COMPARATOR_PATTERN = /^(>=|<=|>|<|=)?\s*(.+)$/;
|
|
69
|
+
|
|
70
|
+
/** Whether `version` meets one comparator (`>=2.0.0`); a malformed comparator never holds. */
|
|
71
|
+
function meetsComparator(version: ParsedVersion, comparator: string): boolean {
|
|
72
|
+
const match = COMPARATOR_PATTERN.exec(comparator);
|
|
73
|
+
const bound = match?.[2] === undefined ? undefined : parseVersion(match[2]);
|
|
74
|
+
if (match === null || bound === undefined) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const order = compareVersions(version, bound);
|
|
78
|
+
switch (match[1]) {
|
|
79
|
+
case '>=': return order >= 0;
|
|
80
|
+
case '<=': return order <= 0;
|
|
81
|
+
case '>': return order > 0;
|
|
82
|
+
case '<': return order < 0;
|
|
83
|
+
default: return order === 0;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Whether `versionText` is in `range`; a malformed version or empty range never is. */
|
|
88
|
+
export function satisfiesRange(versionText: string, range: string): boolean {
|
|
89
|
+
const version = parseVersion(versionText);
|
|
90
|
+
if (version === undefined) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return range.split('||').some((alternative) => {
|
|
94
|
+
const comparators = alternative.trim().split(/\s+/).filter((comparator) => comparator !== '');
|
|
95
|
+
return comparators.length > 0 && comparators.every((comparator) => meetsComparator(version, comparator));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -1,161 +1,161 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Daily challenges, client side. A daily-enabled game calls
|
|
3
|
-
* `hub.daily.define({ play })` once at load; `play` receives a
|
|
4
|
-
* {@link DailyChallenge} and starts exactly that challenge. When the player
|
|
5
|
-
* finishes, the game calls `hub.daily.complete({ score, … })`.
|
|
6
|
-
*
|
|
7
|
-
* The system is a page-wide singleton shared by the game's client and the
|
|
8
|
-
* injected menu, so the menu can start a day on the instance the game
|
|
9
|
-
* registered `play` on. Deep links (`/<slug>/?daily=YYYY-MM-DD`) auto-start.
|
|
10
|
-
*/
|
|
11
|
-
import { readField } from '../realtime/cross-bundle';
|
|
12
|
-
import type { DailyAll, DailyChallenge, DailyCompleteResponse, DailyCompletionMeta, DailyDefinition, DailyHost, DailyStatus } from './types';
|
|
13
|
-
|
|
14
|
-
const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
15
|
-
|
|
16
|
-
/** The window event the injected menu fires to start a day on the game's instance. */
|
|
17
|
-
export const DAILY_START_EVENT = 'hub:daily-start';
|
|
18
|
-
|
|
19
|
-
/** A day key, or `null` when `candidate` isn't one. */
|
|
20
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a URL parameter or a window global set by the host page
|
|
21
|
-
function asDayKey(candidate: unknown): string | null {
|
|
22
|
-
return typeof candidate === 'string' && DAY_PATTERN.test(candidate) ? candidate : null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** The day the page was deep-linked to, if any. */
|
|
26
|
-
function readDeepLinkDay(): string | null {
|
|
27
|
-
// Prefer the value the host's injected snippet captured at parse time: it
|
|
28
|
-
// survives games that rewrite their own URL on boot.
|
|
29
|
-
const captured = typeof window === 'undefined' ? null : asDayKey(readField(window, '__hubDailyDeep'));
|
|
30
|
-
if (captured !== null || typeof location === 'undefined') {
|
|
31
|
-
return captured;
|
|
32
|
-
}
|
|
33
|
-
return asDayKey(new URL(location.href).searchParams.get('daily'));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** The message of a thrown value. */
|
|
37
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: anything a REST call can throw
|
|
38
|
-
function messageOf(error: unknown): string {
|
|
39
|
-
return error instanceof Error ? error.message : String(error);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Daily challenges (`hub.daily`). */
|
|
43
|
-
export class DailySystem {
|
|
44
|
-
/**
|
|
45
|
-
* The challenge in progress (set when a day starts, cleared on complete).
|
|
46
|
-
* A game's win handler checks it: `if (hub.daily.active) hub.daily.complete(…)`.
|
|
47
|
-
*/
|
|
48
|
-
active: DailyChallenge | null = null;
|
|
49
|
-
private readonly host: DailyHost;
|
|
50
|
-
private play: DailyDefinition['play'] | null = null;
|
|
51
|
-
private pendingDay: string | null = null;
|
|
52
|
-
private isRelaying = false;
|
|
53
|
-
|
|
54
|
-
constructor(host: DailyHost) {
|
|
55
|
-
this.host = host;
|
|
56
|
-
if (typeof window !== 'undefined') {
|
|
57
|
-
// The injected menu (another module instance) asks us to start a day.
|
|
58
|
-
window.addEventListener(DAILY_START_EVENT, (event) => {
|
|
59
|
-
if (this.isRelaying) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
const day = event instanceof CustomEvent ? readField(event.detail, 'day') : undefined;
|
|
63
|
-
if (typeof day === 'string') {
|
|
64
|
-
void this.startAndPlay(day);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** Whether a game registered a daily-challenge handler on this page. */
|
|
71
|
-
get isDefined(): boolean {
|
|
72
|
-
return this.play !== null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Register the game's challenge runner. Call once at load. If the page was
|
|
77
|
-
* deep-linked with `?daily=DATE` (or a start was requested before this),
|
|
78
|
-
* that day starts now.
|
|
79
|
-
*/
|
|
80
|
-
define(definition: DailyDefinition): void {
|
|
81
|
-
this.play = definition.play;
|
|
82
|
-
const day = readDeepLinkDay() ?? this.pendingDay;
|
|
83
|
-
this.pendingDay = null;
|
|
84
|
-
if (day !== null) {
|
|
85
|
-
void this.startAndPlay(day);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Fetch the signed token for `day`, make it active and call the game's
|
|
91
|
-
* `play`. Before `define`, the day is kept and played then, so the menu's
|
|
92
|
-
* own instance never fetches a token; it is also relayed to any other
|
|
93
|
-
* daily system on the page (a client copy that couldn't share this one).
|
|
94
|
-
*/
|
|
95
|
-
async startAndPlay(day: string): Promise<void> {
|
|
96
|
-
const play = this.play;
|
|
97
|
-
if (play === null) {
|
|
98
|
-
this.pendingDay = day;
|
|
99
|
-
this.relayStart(day);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
try {
|
|
103
|
-
const started = await this.host.start(day);
|
|
104
|
-
const challenge: DailyChallenge = { day: started.day, seed: started.seed, token: started.token, alreadyCompleted: started.alreadyCompleted };
|
|
105
|
-
this.active = challenge;
|
|
106
|
-
this.host.emit('hub:daily-started', { day: started.day, seed: started.seed, alreadyCompleted: started.alreadyCompleted });
|
|
107
|
-
play({ ...challenge });
|
|
108
|
-
} catch (error) {
|
|
109
|
-
this.host.emit('hub:daily-error', { day, error: messageOf(error) });
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Record completion of the active challenge with optional metadata.
|
|
115
|
-
* Resolves with the new completion count and rank, or `null` when no
|
|
116
|
-
* challenge is active or the hub refused.
|
|
117
|
-
*/
|
|
118
|
-
async complete(meta: DailyCompletionMeta = {}): Promise<DailyCompleteResponse | null> {
|
|
119
|
-
const challenge = this.active;
|
|
120
|
-
if (challenge === null) {
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
try {
|
|
124
|
-
const completed = await this.host.complete(challenge.day, challenge.token, meta);
|
|
125
|
-
this.active = null;
|
|
126
|
-
this.host.emit('hub:daily-complete', { day: challenge.day, count: completed.count, rank: completed.rank, meta });
|
|
127
|
-
return completed;
|
|
128
|
-
} catch (error) {
|
|
129
|
-
this.host.emit('hub:daily-error', { day: challenge.day, error: messageOf(error) });
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Ask the page's other daily systems to start `day` (this one ignores its own event). */
|
|
135
|
-
private relayStart(day: string): void {
|
|
136
|
-
if (typeof window === 'undefined') {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
this.isRelaying = true;
|
|
140
|
-
try {
|
|
141
|
-
window.dispatchEvent(new CustomEvent(DAILY_START_EVENT, { detail: { day } }));
|
|
142
|
-
} finally {
|
|
143
|
-
this.isRelaying = false;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Abandon the active challenge without recording it (e.g. the player quit). */
|
|
148
|
-
cancel(): void {
|
|
149
|
-
this.active = null;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** This game's window and the player's completions. */
|
|
153
|
-
status(): Promise<DailyStatus> {
|
|
154
|
-
return this.host.status();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** Every daily-enabled game and the global standings (for the home hub). */
|
|
158
|
-
all(): Promise<DailyAll> {
|
|
159
|
-
return this.host.all();
|
|
160
|
-
}
|
|
161
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Daily challenges, client side. A daily-enabled game calls
|
|
3
|
+
* `hub.daily.define({ play })` once at load; `play` receives a
|
|
4
|
+
* {@link DailyChallenge} and starts exactly that challenge. When the player
|
|
5
|
+
* finishes, the game calls `hub.daily.complete({ score, … })`.
|
|
6
|
+
*
|
|
7
|
+
* The system is a page-wide singleton shared by the game's client and the
|
|
8
|
+
* injected menu, so the menu can start a day on the instance the game
|
|
9
|
+
* registered `play` on. Deep links (`/<slug>/?daily=YYYY-MM-DD`) auto-start.
|
|
10
|
+
*/
|
|
11
|
+
import { readField } from '../realtime/cross-bundle';
|
|
12
|
+
import type { DailyAll, DailyChallenge, DailyCompleteResponse, DailyCompletionMeta, DailyDefinition, DailyHost, DailyStatus } from './types';
|
|
13
|
+
|
|
14
|
+
const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
15
|
+
|
|
16
|
+
/** The window event the injected menu fires to start a day on the game's instance. */
|
|
17
|
+
export const DAILY_START_EVENT = 'hub:daily-start';
|
|
18
|
+
|
|
19
|
+
/** A day key, or `null` when `candidate` isn't one. */
|
|
20
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a URL parameter or a window global set by the host page
|
|
21
|
+
function asDayKey(candidate: unknown): string | null {
|
|
22
|
+
return typeof candidate === 'string' && DAY_PATTERN.test(candidate) ? candidate : null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The day the page was deep-linked to, if any. */
|
|
26
|
+
function readDeepLinkDay(): string | null {
|
|
27
|
+
// Prefer the value the host's injected snippet captured at parse time: it
|
|
28
|
+
// survives games that rewrite their own URL on boot.
|
|
29
|
+
const captured = typeof window === 'undefined' ? null : asDayKey(readField(window, '__hubDailyDeep'));
|
|
30
|
+
if (captured !== null || typeof location === 'undefined') {
|
|
31
|
+
return captured;
|
|
32
|
+
}
|
|
33
|
+
return asDayKey(new URL(location.href).searchParams.get('daily'));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The message of a thrown value. */
|
|
37
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: anything a REST call can throw
|
|
38
|
+
function messageOf(error: unknown): string {
|
|
39
|
+
return error instanceof Error ? error.message : String(error);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Daily challenges (`hub.daily`). */
|
|
43
|
+
export class DailySystem {
|
|
44
|
+
/**
|
|
45
|
+
* The challenge in progress (set when a day starts, cleared on complete).
|
|
46
|
+
* A game's win handler checks it: `if (hub.daily.active) hub.daily.complete(…)`.
|
|
47
|
+
*/
|
|
48
|
+
active: DailyChallenge | null = null;
|
|
49
|
+
private readonly host: DailyHost;
|
|
50
|
+
private play: DailyDefinition['play'] | null = null;
|
|
51
|
+
private pendingDay: string | null = null;
|
|
52
|
+
private isRelaying = false;
|
|
53
|
+
|
|
54
|
+
constructor(host: DailyHost) {
|
|
55
|
+
this.host = host;
|
|
56
|
+
if (typeof window !== 'undefined') {
|
|
57
|
+
// The injected menu (another module instance) asks us to start a day.
|
|
58
|
+
window.addEventListener(DAILY_START_EVENT, (event) => {
|
|
59
|
+
if (this.isRelaying) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const day = event instanceof CustomEvent ? readField(event.detail, 'day') : undefined;
|
|
63
|
+
if (typeof day === 'string') {
|
|
64
|
+
void this.startAndPlay(day);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Whether a game registered a daily-challenge handler on this page. */
|
|
71
|
+
get isDefined(): boolean {
|
|
72
|
+
return this.play !== null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Register the game's challenge runner. Call once at load. If the page was
|
|
77
|
+
* deep-linked with `?daily=DATE` (or a start was requested before this),
|
|
78
|
+
* that day starts now.
|
|
79
|
+
*/
|
|
80
|
+
define(definition: DailyDefinition): void {
|
|
81
|
+
this.play = definition.play;
|
|
82
|
+
const day = readDeepLinkDay() ?? this.pendingDay;
|
|
83
|
+
this.pendingDay = null;
|
|
84
|
+
if (day !== null) {
|
|
85
|
+
void this.startAndPlay(day);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Fetch the signed token for `day`, make it active and call the game's
|
|
91
|
+
* `play`. Before `define`, the day is kept and played then, so the menu's
|
|
92
|
+
* own instance never fetches a token; it is also relayed to any other
|
|
93
|
+
* daily system on the page (a client copy that couldn't share this one).
|
|
94
|
+
*/
|
|
95
|
+
async startAndPlay(day: string): Promise<void> {
|
|
96
|
+
const play = this.play;
|
|
97
|
+
if (play === null) {
|
|
98
|
+
this.pendingDay = day;
|
|
99
|
+
this.relayStart(day);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
const started = await this.host.start(day);
|
|
104
|
+
const challenge: DailyChallenge = { day: started.day, seed: started.seed, token: started.token, alreadyCompleted: started.alreadyCompleted };
|
|
105
|
+
this.active = challenge;
|
|
106
|
+
this.host.emit('hub:daily-started', { day: started.day, seed: started.seed, alreadyCompleted: started.alreadyCompleted });
|
|
107
|
+
play({ ...challenge });
|
|
108
|
+
} catch (error) {
|
|
109
|
+
this.host.emit('hub:daily-error', { day, error: messageOf(error) });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Record completion of the active challenge with optional metadata.
|
|
115
|
+
* Resolves with the new completion count and rank, or `null` when no
|
|
116
|
+
* challenge is active or the hub refused.
|
|
117
|
+
*/
|
|
118
|
+
async complete(meta: DailyCompletionMeta = {}): Promise<DailyCompleteResponse | null> {
|
|
119
|
+
const challenge = this.active;
|
|
120
|
+
if (challenge === null) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
const completed = await this.host.complete(challenge.day, challenge.token, meta);
|
|
125
|
+
this.active = null;
|
|
126
|
+
this.host.emit('hub:daily-complete', { day: challenge.day, count: completed.count, rank: completed.rank, meta });
|
|
127
|
+
return completed;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
this.host.emit('hub:daily-error', { day: challenge.day, error: messageOf(error) });
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Ask the page's other daily systems to start `day` (this one ignores its own event). */
|
|
135
|
+
private relayStart(day: string): void {
|
|
136
|
+
if (typeof window === 'undefined') {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this.isRelaying = true;
|
|
140
|
+
try {
|
|
141
|
+
window.dispatchEvent(new CustomEvent(DAILY_START_EVENT, { detail: { day } }));
|
|
142
|
+
} finally {
|
|
143
|
+
this.isRelaying = false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Abandon the active challenge without recording it (e.g. the player quit). */
|
|
148
|
+
cancel(): void {
|
|
149
|
+
this.active = null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** This game's window and the player's completions. */
|
|
153
|
+
status(): Promise<DailyStatus> {
|
|
154
|
+
return this.host.status();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Every daily-enabled game and the global standings (for the home hub). */
|
|
158
|
+
all(): Promise<DailyAll> {
|
|
159
|
+
return this.host.all();
|
|
160
|
+
}
|
|
161
|
+
}
|
package/src/daily/page-daily.ts
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The page's one daily-challenge system (plan §4.4): the first bundle to
|
|
3
|
-
* create it wins, so the menu starts days on the instance the game defined.
|
|
4
|
-
*/
|
|
5
|
-
import { HUB_CLIENT_VERSION } from '../core/version/client-version.gen';
|
|
6
|
-
import { claimSingleton } from '../globals';
|
|
7
|
-
import { hasMethods } from '../realtime/cross-bundle';
|
|
8
|
-
import { DailySystem } from './daily-system';
|
|
9
|
-
import type { DailyHost } from './types';
|
|
10
|
-
|
|
11
|
-
/** The cross-bundle shape number of the `daily` singleton. Bump only on an incompatible change. */
|
|
12
|
-
export const DAILY_SINGLETON_API = 1;
|
|
13
|
-
|
|
14
|
-
/** Whether `candidate` is some bundle's daily system (v1 copies included). */
|
|
15
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the instance may come from another bundle
|
|
16
|
-
export function isDailySystem(candidate: unknown): candidate is DailySystem {
|
|
17
|
-
return hasMethods(candidate, ['define', 'startAndPlay', 'complete', 'cancel', 'status', 'all']);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let claimedDaily: DailySystem | null = null;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The page-wide daily system, created with `host` if no bundle has made one
|
|
24
|
-
* yet. On a shape mismatch the registered one is kept and this bundle gets a
|
|
25
|
-
* private instance. The answer is kept for this bundle.
|
|
26
|
-
*/
|
|
27
|
-
export function getDaily(host: DailyHost): DailySystem {
|
|
28
|
-
claimedDaily ??= claimSingleton({
|
|
29
|
-
name: 'daily',
|
|
30
|
-
version: HUB_CLIENT_VERSION,
|
|
31
|
-
api: DAILY_SINGLETON_API,
|
|
32
|
-
create: () => new DailySystem(host),
|
|
33
|
-
isInstance: isDailySystem,
|
|
34
|
-
});
|
|
35
|
-
return claimedDaily;
|
|
36
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The page's one daily-challenge system (plan §4.4): the first bundle to
|
|
3
|
+
* create it wins, so the menu starts days on the instance the game defined.
|
|
4
|
+
*/
|
|
5
|
+
import { HUB_CLIENT_VERSION } from '../core/version/client-version.gen';
|
|
6
|
+
import { claimSingleton } from '../globals';
|
|
7
|
+
import { hasMethods } from '../realtime/cross-bundle';
|
|
8
|
+
import { DailySystem } from './daily-system';
|
|
9
|
+
import type { DailyHost } from './types';
|
|
10
|
+
|
|
11
|
+
/** The cross-bundle shape number of the `daily` singleton. Bump only on an incompatible change. */
|
|
12
|
+
export const DAILY_SINGLETON_API = 1;
|
|
13
|
+
|
|
14
|
+
/** Whether `candidate` is some bundle's daily system (v1 copies included). */
|
|
15
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the instance may come from another bundle
|
|
16
|
+
export function isDailySystem(candidate: unknown): candidate is DailySystem {
|
|
17
|
+
return hasMethods(candidate, ['define', 'startAndPlay', 'complete', 'cancel', 'status', 'all']);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let claimedDaily: DailySystem | null = null;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The page-wide daily system, created with `host` if no bundle has made one
|
|
24
|
+
* yet. On a shape mismatch the registered one is kept and this bundle gets a
|
|
25
|
+
* private instance. The answer is kept for this bundle.
|
|
26
|
+
*/
|
|
27
|
+
export function getDaily(host: DailyHost): DailySystem {
|
|
28
|
+
claimedDaily ??= claimSingleton({
|
|
29
|
+
name: 'daily',
|
|
30
|
+
version: HUB_CLIENT_VERSION,
|
|
31
|
+
api: DAILY_SINGLETON_API,
|
|
32
|
+
create: () => new DailySystem(host),
|
|
33
|
+
isInstance: isDailySystem,
|
|
34
|
+
});
|
|
35
|
+
return claimedDaily;
|
|
36
|
+
}
|