@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-progress.ts
CHANGED
|
@@ -1,205 +1,205 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Accounts and the player's progress in this game: sign-in, the profile,
|
|
3
|
-
* saves, stats and leaderboards. Reads fall back to the offline cache; writes
|
|
4
|
-
* apply to the cache at once and are queued while offline. Method names are
|
|
5
|
-
* the v1 client's, unchanged.
|
|
6
|
-
*/
|
|
7
|
-
import type { Board, Json, Stats } from '@diffenderfer-games/hub-contract';
|
|
8
|
-
import { HubConnection } from './connection';
|
|
9
|
-
import { toGameData, toStoredJson, type GamePayload } from './game-data';
|
|
10
|
-
import type { ProfilePatch, QueuedScoreOptions } from './offline/queue';
|
|
11
|
-
import type { LeaderboardResult, MeResponse, Profile, Save, SaveMeta, SubmitScoreOptions, SubmitScoreResult, User } from './types';
|
|
12
|
-
|
|
13
|
-
/** A signed-in user, as the auth calls answer. */
|
|
14
|
-
export interface AuthResponse {
|
|
15
|
-
/** The signed-in user. */
|
|
16
|
-
readonly user: User;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** A stat's new value, as the stat calls answer. */
|
|
20
|
-
export interface StatResult {
|
|
21
|
-
/** The stat key. */
|
|
22
|
-
readonly key: string;
|
|
23
|
-
/** Its value after the update. */
|
|
24
|
-
readonly value: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** A profile change with the game's own data typed as `TData`. */
|
|
28
|
-
export interface ProfileUpdate<TData extends GamePayload = Json> {
|
|
29
|
-
/** Shown to other players; `null` clears it. */
|
|
30
|
-
readonly displayName?: string | null;
|
|
31
|
-
/** The game's own profile data. */
|
|
32
|
-
readonly data?: TData;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** A profile with the game's own data typed as `TData`. */
|
|
36
|
-
function typedProfile<TData>(profile: Profile | null): Profile<TData> | null {
|
|
37
|
-
return profile === null ? null : { ...profile, data: toGameData<TData>(profile.data) };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** A save with the game's own data typed as `TData`. */
|
|
41
|
-
function typedSave<TData>(save: Save | null): Save<TData> | null {
|
|
42
|
-
return save === null ? null : { ...save, data: toGameData<TData>(save.data) };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Accounts, profile, saves, stats and leaderboards. */
|
|
46
|
-
export class HubProgress extends HubConnection {
|
|
47
|
-
/** The current user (or `null`) and the games they have a profile in. */
|
|
48
|
-
me(): Promise<MeResponse> {
|
|
49
|
-
return this.withFallback(() => this.rest.call('getCurrentUser'), () => {
|
|
50
|
-
const profile = this.store.getProfile();
|
|
51
|
-
return { user: this.store.getUser(), profiles: profile === null ? [] : [{ game: profile.game, displayName: profile.displayName }] };
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Make sure a session exists, creating a guest (a random name, no
|
|
57
|
-
* password) when the player isn't signed in, so progress saves without a
|
|
58
|
-
* sign-in. Idempotent. Offline, a local guest is used.
|
|
59
|
-
*/
|
|
60
|
-
ensureGuest(): Promise<AuthResponse> {
|
|
61
|
-
return this.withFallback<AuthResponse>(() => this.rest.call('ensureGuestSession'), () => {
|
|
62
|
-
const known = this.store.getUser();
|
|
63
|
-
if (known !== null) {
|
|
64
|
-
return { user: known };
|
|
65
|
-
}
|
|
66
|
-
const guest: User = { id: 0, username: 'Guest-local', guest: true };
|
|
67
|
-
this.store.setUser(guest);
|
|
68
|
-
return { user: guest };
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Turn the current guest into a registered account (the same identity, so
|
|
74
|
-
* all progress carries over). Rejects with 409 when already registered or
|
|
75
|
-
* the name is taken; the player should `login` instead.
|
|
76
|
-
*/
|
|
77
|
-
claim(username: string, password: string): Promise<AuthResponse> {
|
|
78
|
-
return this.withFallback<AuthResponse>(() => this.rest.call('claimGuestAccount', { username, password }), () => this.signInLocally({ id: 0, username, guest: false }));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Create an account and sign in. (Offline: a local user is stored.) */
|
|
82
|
-
signup(username: string, password: string): Promise<AuthResponse> {
|
|
83
|
-
return this.withFallback<AuthResponse>(() => this.rest.call('signUpWithPassword', { username, password }), () => this.signInLocally({ id: 0, username }));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Sign in. (Offline: a local user is stored so signed-in flows work.) */
|
|
87
|
-
login(username: string, password: string): Promise<AuthResponse> {
|
|
88
|
-
return this.withFallback<AuthResponse>(() => this.rest.call('logInWithPassword', { username, password }), () => this.signInLocally({ id: 0, username }));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Sign out (ends the session shared by every game). */
|
|
92
|
-
logout(): Promise<{ ok: true }> {
|
|
93
|
-
return this.withFallback(() => this.rest.call('logOut'), () => {
|
|
94
|
-
this.store.clearUser();
|
|
95
|
-
return { ok: true } as const;
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private signInLocally(user: User): AuthResponse {
|
|
100
|
-
this.store.setUser(user);
|
|
101
|
-
return { user };
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** This game's profile for the current user, or `profile: null`. `TData` is the game's own data type. */
|
|
105
|
-
getProfile<TData = Json>(): Promise<{ profile: Profile<TData> | null }> {
|
|
106
|
-
return this.withFallback(
|
|
107
|
-
async () => ({ profile: typedProfile<TData>((await this.rest.call('getGameProfile', this.gameFields())).profile) }),
|
|
108
|
-
() => ({ profile: typedProfile<TData>(this.store.getProfile()) }),
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Create or update this game's profile for the current user. */
|
|
113
|
-
setProfile<TData extends GamePayload = Json>(patch: ProfileUpdate<TData>): Promise<{ profile: Profile<TData> }> {
|
|
114
|
-
const queued: ProfilePatch = {};
|
|
115
|
-
if (patch.displayName !== undefined) {
|
|
116
|
-
queued.displayName = patch.displayName;
|
|
117
|
-
}
|
|
118
|
-
if (patch.data !== undefined) {
|
|
119
|
-
queued.data = toStoredJson(patch.data);
|
|
120
|
-
}
|
|
121
|
-
return this.mutateQueued({ kind: 'setProfile', patch: queued }, () => {
|
|
122
|
-
const stored = this.store.setProfile(this.slug ?? 'app', patch.displayName ?? null, queued.data ?? {});
|
|
123
|
-
return { profile: { ...stored, data: toGameData<TData>(stored.data) } };
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** Metadata for every save slot (no payloads), newest first. */
|
|
128
|
-
listSaves(): Promise<{ saves: SaveMeta[] }> {
|
|
129
|
-
return this.withFallback(() => this.rest.call('listSaves', this.gameFields()), () => ({ saves: this.store.listSaves() }));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** One slot's contents, or `save: null` when the slot is empty. `TData` is the game's own save type. */
|
|
133
|
-
getSave<TData = Json>(slot: string): Promise<{ save: Save<TData> | null }> {
|
|
134
|
-
return this.withFallback(
|
|
135
|
-
async () => ({ save: typedSave<TData>((await this.rest.call('getSave', { ...this.gameFields(), slot })).save) }),
|
|
136
|
-
() => ({ save: typedSave<TData>(this.store.getSave(slot)) }),
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** Create or overwrite a save slot. `saveData` is any JSON up to 64 KB. */
|
|
141
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- kept so v1 calls such as putSave<MySave>(...) still compile
|
|
142
|
-
putSave<TData extends GamePayload = Json>(slot: string, saveData: TData, label?: string): Promise<{ ok: true; updatedAt: number }> {
|
|
143
|
-
const stored = toStoredJson(saveData);
|
|
144
|
-
return this.mutateQueued({ kind: 'putSave', slot, data: stored, label: label ?? null }, () => ({ ok: true as const, updatedAt: this.store.putSave(slot, stored, label ?? null) }));
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Delete a save slot. */
|
|
148
|
-
deleteSave(slot: string): Promise<{ ok: true }> {
|
|
149
|
-
return this.mutateQueued({ kind: 'deleteSave', slot }, () => {
|
|
150
|
-
this.store.deleteSave(slot);
|
|
151
|
-
return { ok: true as const };
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Numeric stats for the current user, or another player's via `userId`. */
|
|
156
|
-
getStats(userId?: number): Promise<{ stats: Stats }> {
|
|
157
|
-
return this.withFallback(
|
|
158
|
-
() => this.rest.call('getGameStats', { ...this.gameFields(), ...(userId === undefined ? {} : { user: userId }) }),
|
|
159
|
-
() => ({ stats: this.store.getStats() }),
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Overwrite a stat. */
|
|
164
|
-
setStat(key: string, value: number): Promise<StatResult> {
|
|
165
|
-
return this.mutateQueued({ kind: 'setStat', key, value }, () => ({ key, value: this.store.updateStat(key, value, 'set') }));
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** Add to a stat (creating it at `amount` when absent). */
|
|
169
|
-
incrStat(key: string, amount = 1): Promise<StatResult> {
|
|
170
|
-
return this.mutateQueued({ kind: 'incrStat', key, amount }, () => ({ key, value: this.store.updateStat(key, amount, 'incr') }));
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/** Keep the larger of the stored and the given value. */
|
|
174
|
-
maxStat(key: string, value: number): Promise<StatResult> {
|
|
175
|
-
return this.mutateQueued({ kind: 'maxStat', key, value }, () => ({ key, value: this.store.updateStat(key, value, 'max') }));
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** Submit a score. The board is created on first use; only a player's best score sticks. */
|
|
179
|
-
submitScore<TMeta extends GamePayload = Json>(boardKey: string, score: number, opts: SubmitScoreOptions<TMeta> = {}): Promise<SubmitScoreResult> {
|
|
180
|
-
const queued: QueuedScoreOptions = {
|
|
181
|
-
...(opts.title === undefined ? {} : { title: opts.title }),
|
|
182
|
-
...(opts.sort === undefined ? {} : { sort: opts.sort }),
|
|
183
|
-
...(opts.meta === undefined ? {} : { meta: toStoredJson(opts.meta) }),
|
|
184
|
-
};
|
|
185
|
-
return this.mutateQueued({ kind: 'submitScore', key: boardKey, score, opts: queued }, () => ({ ...this.store.submitScore(boardKey, score, queued), rank: 1 }));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/** One board's ranked entries (top `limit`, default 50). Offline: only the player's own best. */
|
|
189
|
-
getLeaderboard(boardKey: string, limit = 50): Promise<LeaderboardResult> {
|
|
190
|
-
return this.withFallback(() => this.rest.call('getLeaderboard', { ...this.gameFields(), key: boardKey, limit }), () => {
|
|
191
|
-
const local = this.store.getBoard(boardKey);
|
|
192
|
-
if (local === null) {
|
|
193
|
-
return { board: { key: boardKey, title: boardKey, sortDir: 'desc' }, entries: [] };
|
|
194
|
-
}
|
|
195
|
-
const user = this.store.getUser();
|
|
196
|
-
return { board: local.board, entries: [{ rank: 1, userId: user?.id ?? 0, username: user?.username ?? 'you', score: local.best, meta: null }] };
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** The boards this game owns (keys and titles, no entries). */
|
|
201
|
-
listLeaderboards(): Promise<{ boards: Board[] }> {
|
|
202
|
-
return this.withFallback(() => this.rest.call('listGameLeaderboards', this.gameFields()), () => ({ boards: this.store.listBoards() }));
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Accounts and the player's progress in this game: sign-in, the profile,
|
|
3
|
+
* saves, stats and leaderboards. Reads fall back to the offline cache; writes
|
|
4
|
+
* apply to the cache at once and are queued while offline. Method names are
|
|
5
|
+
* the v1 client's, unchanged.
|
|
6
|
+
*/
|
|
7
|
+
import type { Board, Json, Stats } from '@diffenderfer-games/hub-contract';
|
|
8
|
+
import { HubConnection } from './connection';
|
|
9
|
+
import { toGameData, toStoredJson, type GamePayload } from './game-data';
|
|
10
|
+
import type { ProfilePatch, QueuedScoreOptions } from './offline/queue';
|
|
11
|
+
import type { LeaderboardResult, MeResponse, Profile, Save, SaveMeta, SubmitScoreOptions, SubmitScoreResult, User } from './types';
|
|
12
|
+
|
|
13
|
+
/** A signed-in user, as the auth calls answer. */
|
|
14
|
+
export interface AuthResponse {
|
|
15
|
+
/** The signed-in user. */
|
|
16
|
+
readonly user: User;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A stat's new value, as the stat calls answer. */
|
|
20
|
+
export interface StatResult {
|
|
21
|
+
/** The stat key. */
|
|
22
|
+
readonly key: string;
|
|
23
|
+
/** Its value after the update. */
|
|
24
|
+
readonly value: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A profile change with the game's own data typed as `TData`. */
|
|
28
|
+
export interface ProfileUpdate<TData extends GamePayload = Json> {
|
|
29
|
+
/** Shown to other players; `null` clears it. */
|
|
30
|
+
readonly displayName?: string | null;
|
|
31
|
+
/** The game's own profile data. */
|
|
32
|
+
readonly data?: TData;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A profile with the game's own data typed as `TData`. */
|
|
36
|
+
function typedProfile<TData>(profile: Profile | null): Profile<TData> | null {
|
|
37
|
+
return profile === null ? null : { ...profile, data: toGameData<TData>(profile.data) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A save with the game's own data typed as `TData`. */
|
|
41
|
+
function typedSave<TData>(save: Save | null): Save<TData> | null {
|
|
42
|
+
return save === null ? null : { ...save, data: toGameData<TData>(save.data) };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Accounts, profile, saves, stats and leaderboards. */
|
|
46
|
+
export class HubProgress extends HubConnection {
|
|
47
|
+
/** The current user (or `null`) and the games they have a profile in. */
|
|
48
|
+
me(): Promise<MeResponse> {
|
|
49
|
+
return this.withFallback(() => this.rest.call('getCurrentUser'), () => {
|
|
50
|
+
const profile = this.store.getProfile();
|
|
51
|
+
return { user: this.store.getUser(), profiles: profile === null ? [] : [{ game: profile.game, displayName: profile.displayName }] };
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Make sure a session exists, creating a guest (a random name, no
|
|
57
|
+
* password) when the player isn't signed in, so progress saves without a
|
|
58
|
+
* sign-in. Idempotent. Offline, a local guest is used.
|
|
59
|
+
*/
|
|
60
|
+
ensureGuest(): Promise<AuthResponse> {
|
|
61
|
+
return this.withFallback<AuthResponse>(() => this.rest.call('ensureGuestSession'), () => {
|
|
62
|
+
const known = this.store.getUser();
|
|
63
|
+
if (known !== null) {
|
|
64
|
+
return { user: known };
|
|
65
|
+
}
|
|
66
|
+
const guest: User = { id: 0, username: 'Guest-local', guest: true };
|
|
67
|
+
this.store.setUser(guest);
|
|
68
|
+
return { user: guest };
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Turn the current guest into a registered account (the same identity, so
|
|
74
|
+
* all progress carries over). Rejects with 409 when already registered or
|
|
75
|
+
* the name is taken; the player should `login` instead.
|
|
76
|
+
*/
|
|
77
|
+
claim(username: string, password: string): Promise<AuthResponse> {
|
|
78
|
+
return this.withFallback<AuthResponse>(() => this.rest.call('claimGuestAccount', { username, password }), () => this.signInLocally({ id: 0, username, guest: false }));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Create an account and sign in. (Offline: a local user is stored.) */
|
|
82
|
+
signup(username: string, password: string): Promise<AuthResponse> {
|
|
83
|
+
return this.withFallback<AuthResponse>(() => this.rest.call('signUpWithPassword', { username, password }), () => this.signInLocally({ id: 0, username }));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Sign in. (Offline: a local user is stored so signed-in flows work.) */
|
|
87
|
+
login(username: string, password: string): Promise<AuthResponse> {
|
|
88
|
+
return this.withFallback<AuthResponse>(() => this.rest.call('logInWithPassword', { username, password }), () => this.signInLocally({ id: 0, username }));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Sign out (ends the session shared by every game). */
|
|
92
|
+
logout(): Promise<{ ok: true }> {
|
|
93
|
+
return this.withFallback(() => this.rest.call('logOut'), () => {
|
|
94
|
+
this.store.clearUser();
|
|
95
|
+
return { ok: true } as const;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private signInLocally(user: User): AuthResponse {
|
|
100
|
+
this.store.setUser(user);
|
|
101
|
+
return { user };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** This game's profile for the current user, or `profile: null`. `TData` is the game's own data type. */
|
|
105
|
+
getProfile<TData = Json>(): Promise<{ profile: Profile<TData> | null }> {
|
|
106
|
+
return this.withFallback(
|
|
107
|
+
async () => ({ profile: typedProfile<TData>((await this.rest.call('getGameProfile', this.gameFields())).profile) }),
|
|
108
|
+
() => ({ profile: typedProfile<TData>(this.store.getProfile()) }),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Create or update this game's profile for the current user. */
|
|
113
|
+
setProfile<TData extends GamePayload = Json>(patch: ProfileUpdate<TData>): Promise<{ profile: Profile<TData> }> {
|
|
114
|
+
const queued: ProfilePatch = {};
|
|
115
|
+
if (patch.displayName !== undefined) {
|
|
116
|
+
queued.displayName = patch.displayName;
|
|
117
|
+
}
|
|
118
|
+
if (patch.data !== undefined) {
|
|
119
|
+
queued.data = toStoredJson(patch.data);
|
|
120
|
+
}
|
|
121
|
+
return this.mutateQueued({ kind: 'setProfile', patch: queued }, () => {
|
|
122
|
+
const stored = this.store.setProfile(this.slug ?? 'app', patch.displayName ?? null, queued.data ?? {});
|
|
123
|
+
return { profile: { ...stored, data: toGameData<TData>(stored.data) } };
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Metadata for every save slot (no payloads), newest first. */
|
|
128
|
+
listSaves(): Promise<{ saves: SaveMeta[] }> {
|
|
129
|
+
return this.withFallback(() => this.rest.call('listSaves', this.gameFields()), () => ({ saves: this.store.listSaves() }));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** One slot's contents, or `save: null` when the slot is empty. `TData` is the game's own save type. */
|
|
133
|
+
getSave<TData = Json>(slot: string): Promise<{ save: Save<TData> | null }> {
|
|
134
|
+
return this.withFallback(
|
|
135
|
+
async () => ({ save: typedSave<TData>((await this.rest.call('getSave', { ...this.gameFields(), slot })).save) }),
|
|
136
|
+
() => ({ save: typedSave<TData>(this.store.getSave(slot)) }),
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Create or overwrite a save slot. `saveData` is any JSON up to 64 KB. */
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- kept so v1 calls such as putSave<MySave>(...) still compile
|
|
142
|
+
putSave<TData extends GamePayload = Json>(slot: string, saveData: TData, label?: string): Promise<{ ok: true; updatedAt: number }> {
|
|
143
|
+
const stored = toStoredJson(saveData);
|
|
144
|
+
return this.mutateQueued({ kind: 'putSave', slot, data: stored, label: label ?? null }, () => ({ ok: true as const, updatedAt: this.store.putSave(slot, stored, label ?? null) }));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Delete a save slot. */
|
|
148
|
+
deleteSave(slot: string): Promise<{ ok: true }> {
|
|
149
|
+
return this.mutateQueued({ kind: 'deleteSave', slot }, () => {
|
|
150
|
+
this.store.deleteSave(slot);
|
|
151
|
+
return { ok: true as const };
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Numeric stats for the current user, or another player's via `userId`. */
|
|
156
|
+
getStats(userId?: number): Promise<{ stats: Stats }> {
|
|
157
|
+
return this.withFallback(
|
|
158
|
+
() => this.rest.call('getGameStats', { ...this.gameFields(), ...(userId === undefined ? {} : { user: userId }) }),
|
|
159
|
+
() => ({ stats: this.store.getStats() }),
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Overwrite a stat. */
|
|
164
|
+
setStat(key: string, value: number): Promise<StatResult> {
|
|
165
|
+
return this.mutateQueued({ kind: 'setStat', key, value }, () => ({ key, value: this.store.updateStat(key, value, 'set') }));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Add to a stat (creating it at `amount` when absent). */
|
|
169
|
+
incrStat(key: string, amount = 1): Promise<StatResult> {
|
|
170
|
+
return this.mutateQueued({ kind: 'incrStat', key, amount }, () => ({ key, value: this.store.updateStat(key, amount, 'incr') }));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Keep the larger of the stored and the given value. */
|
|
174
|
+
maxStat(key: string, value: number): Promise<StatResult> {
|
|
175
|
+
return this.mutateQueued({ kind: 'maxStat', key, value }, () => ({ key, value: this.store.updateStat(key, value, 'max') }));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Submit a score. The board is created on first use; only a player's best score sticks. */
|
|
179
|
+
submitScore<TMeta extends GamePayload = Json>(boardKey: string, score: number, opts: SubmitScoreOptions<TMeta> = {}): Promise<SubmitScoreResult> {
|
|
180
|
+
const queued: QueuedScoreOptions = {
|
|
181
|
+
...(opts.title === undefined ? {} : { title: opts.title }),
|
|
182
|
+
...(opts.sort === undefined ? {} : { sort: opts.sort }),
|
|
183
|
+
...(opts.meta === undefined ? {} : { meta: toStoredJson(opts.meta) }),
|
|
184
|
+
};
|
|
185
|
+
return this.mutateQueued({ kind: 'submitScore', key: boardKey, score, opts: queued }, () => ({ ...this.store.submitScore(boardKey, score, queued), rank: 1 }));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** One board's ranked entries (top `limit`, default 50). Offline: only the player's own best. */
|
|
189
|
+
getLeaderboard(boardKey: string, limit = 50): Promise<LeaderboardResult> {
|
|
190
|
+
return this.withFallback(() => this.rest.call('getLeaderboard', { ...this.gameFields(), key: boardKey, limit }), () => {
|
|
191
|
+
const local = this.store.getBoard(boardKey);
|
|
192
|
+
if (local === null) {
|
|
193
|
+
return { board: { key: boardKey, title: boardKey, sortDir: 'desc' }, entries: [] };
|
|
194
|
+
}
|
|
195
|
+
const user = this.store.getUser();
|
|
196
|
+
return { board: local.board, entries: [{ rank: 1, userId: user?.id ?? 0, username: user?.username ?? 'you', score: local.best, meta: null }] };
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The boards this game owns (keys and titles, no entries). */
|
|
201
|
+
listLeaderboards(): Promise<{ boards: Board[] }> {
|
|
202
|
+
return this.withFallback(() => this.rest.call('listGameLeaderboards', this.gameFields()), () => ({ boards: this.store.listBoards() }));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
}
|