@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.
Files changed (98) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +21 -21
  3. package/README.md +191 -139
  4. package/dist/doctor/cli.mjs +49 -19
  5. package/dist/doctor/cli.mjs.map +1 -1
  6. package/dist/shared/client-version.gen.js +1 -1
  7. package/dist/shared/daily.js +1 -1
  8. package/dist/shared/globals.js +8 -3
  9. package/dist/shared/globals.js.map +1 -1
  10. package/dist/shared/index.d.ts +1 -1
  11. package/package.json +89 -89
  12. package/src/auto.ts +39 -39
  13. package/src/core/connection.ts +299 -299
  14. package/src/core/errors.ts +147 -147
  15. package/src/core/game-data.ts +38 -38
  16. package/src/core/hub-economy.ts +179 -179
  17. package/src/core/hub-progress.ts +205 -205
  18. package/src/core/hub.ts +131 -131
  19. package/src/core/menu-control.ts +61 -61
  20. package/src/core/offline/events.ts +50 -50
  21. package/src/core/offline/favorites.ts +21 -21
  22. package/src/core/offline/local-store.ts +261 -261
  23. package/src/core/offline/queue.ts +171 -171
  24. package/src/core/page.ts +74 -74
  25. package/src/core/rest/endpoint-types.ts +22 -22
  26. package/src/core/rest/rest-client.ts +152 -152
  27. package/src/core/storage/json-guards.ts +74 -74
  28. package/src/core/storage/key-value.ts +66 -66
  29. package/src/core/ticket.ts +43 -43
  30. package/src/core/types.ts +134 -134
  31. package/src/core/version/client-version.gen.ts +1 -1
  32. package/src/core/version/hub-version.ts +62 -62
  33. package/src/core/version/semver.ts +97 -97
  34. package/src/daily/daily-system.ts +161 -161
  35. package/src/daily/page-daily.ts +36 -36
  36. package/src/daily/types.ts +67 -67
  37. package/src/daily.ts +10 -10
  38. package/src/doctor/checks.ts +78 -76
  39. package/src/doctor/cli.ts +98 -106
  40. package/src/doctor/hub-version-fetch.ts +43 -0
  41. package/src/doctor/tsconfig.json +12 -12
  42. package/src/globals.ts +115 -110
  43. package/src/index.ts +50 -50
  44. package/src/input/device-state.ts +146 -146
  45. package/src/input/edges.ts +55 -55
  46. package/src/input/gamepad.ts +61 -61
  47. package/src/input/input-events.ts +32 -32
  48. package/src/input/input-registry.ts +164 -164
  49. package/src/input/input-system.ts +393 -393
  50. package/src/input/legacy-controls.ts +64 -64
  51. package/src/input/nav-state.ts +191 -191
  52. package/src/input/navigator.ts +171 -171
  53. package/src/input/on-screen-controls.ts +101 -101
  54. package/src/input/page-control.ts +43 -43
  55. package/src/input/page-input.ts +193 -193
  56. package/src/input/player-remaps.ts +77 -77
  57. package/src/input/source-values.ts +116 -116
  58. package/src/input/spatial.ts +47 -47
  59. package/src/input/types.ts +331 -331
  60. package/src/input/zero-config.ts +177 -177
  61. package/src/input.ts +22 -22
  62. package/src/mp.ts +10 -10
  63. package/src/overlay/controls-panel.ts +163 -163
  64. package/src/overlay/dom.ts +46 -46
  65. package/src/overlay/layout-clusters.ts +162 -162
  66. package/src/overlay/layout-geometry.ts +136 -136
  67. package/src/overlay/layout.ts +203 -203
  68. package/src/overlay/listener-scope.ts +23 -23
  69. package/src/overlay/on-screen-keyboard.ts +137 -137
  70. package/src/overlay/overlay.ts +208 -208
  71. package/src/overlay/styles.ts +9 -9
  72. package/src/overlay/virtual-controls.ts +262 -262
  73. package/src/race.ts +10 -10
  74. package/src/realtime/bridge.ts +121 -121
  75. package/src/realtime/cross-bundle.ts +29 -29
  76. package/src/realtime/fallback-notice.ts +48 -48
  77. package/src/realtime/hub-link.ts +65 -65
  78. package/src/realtime/latest-state.ts +33 -33
  79. package/src/realtime/multiplayer-forwarder.ts +28 -28
  80. package/src/realtime/multiplayer-types.ts +52 -52
  81. package/src/realtime/page-runtime.ts +60 -60
  82. package/src/realtime/race-forwarder.ts +59 -59
  83. package/src/realtime/race-types.ts +132 -132
  84. package/src/realtime/room-types.ts +125 -125
  85. package/src/realtime/rooms-forwarder.ts +79 -79
  86. package/src/realtime/runtime-api.ts +49 -49
  87. package/src/realtime/runtime-opener.ts +61 -61
  88. package/src/realtime/social-forwarder.ts +72 -72
  89. package/src/realtime/social-types.ts +78 -78
  90. package/src/realtime/types.ts +53 -53
  91. package/src/realtime/wire-types.ts +72 -72
  92. package/src/rooms.ts +10 -10
  93. package/src/social.ts +18 -18
  94. package/src/types.ts +13 -13
  95. package/src/uistack/page-stack.ts +45 -45
  96. package/src/uistack/play-suspension.ts +82 -82
  97. package/src/uistack/types.ts +76 -76
  98. package/src/uistack/ui-stack.ts +144 -144
@@ -1,179 +1,179 @@
1
- /**
2
- * Analytics, the catalog, favorites, records, inventory and trades. Analytics
3
- * calls are fire-and-forget and never throw (telemetry must not break a game);
4
- * offline they are buffered and sent when the hub is back. Method names are
5
- * the v1 client's, unchanged.
6
- */
7
- import { HubProgress } from './hub-progress';
8
- import { readLocalFavorites, writeLocalFavorites } from './offline/favorites';
9
- import { HubError } from './errors';
10
- import type { EndpointFields, EndpointReply } from './rest/endpoint-types';
11
- import type { GameAnalytics, GameInfo, GlobalAnalytics, InventorySnapshot } from './types';
12
-
13
- /** Goods to grant: item key to quantity, and coins. */
14
- export type GrantBundle = Omit<EndpointFields<'grantInventoryItems'>, 'slug'>;
15
-
16
- /** Goods taken or given in an exchange or a trade. */
17
- export type TradeBundle = EndpointFields<'exchangeInventoryItems'>['take'];
18
-
19
- /** A trade offer's id, as a number or its text. */
20
- export type TradeId = number | string;
21
-
22
- /** Extras for creating or countering a trade. */
23
- export interface TradeNoteOptions {
24
- /** A short note to the other player (moderated). */
25
- readonly note?: string;
26
- }
27
-
28
- /** Which trades `listTrades` lists. */
29
- export type TradeListOptions = Omit<EndpointFields<'listMyTrades'>, 'slug'>;
30
-
31
- /** Analytics, catalog, favorites, records, inventory and trades. */
32
- export class HubEconomy extends HubProgress {
33
- /** Record a "play". Call once when the game starts. */
34
- recordPlay(): Promise<void> {
35
- return this.event('play');
36
- }
37
-
38
- /** Record a "heartbeat". Call periodically while the game is active. */
39
- heartbeat(): Promise<void> {
40
- return this.event('heartbeat');
41
- }
42
-
43
- /** Record a custom event. Never throws; offline, events are buffered and sent on reconnect. */
44
- async event(type: string): Promise<void> {
45
- if (this.slug === null) {
46
- return;
47
- }
48
- if (this.isOffline) {
49
- this.store.bufferEvent(type);
50
- this.notifyStatus();
51
- return;
52
- }
53
- try {
54
- await this.rest.call('recordGameEvent', { ...this.gameFields(), type });
55
- } catch (failure) {
56
- if (failure instanceof HubError && failure.isOffline) {
57
- this.setReachable(false);
58
- this.store.bufferEvent(type);
59
- }
60
- }
61
- }
62
-
63
- /** This game's play counts. */
64
- analytics(): Promise<GameAnalytics> {
65
- return this.withFallback(() => this.rest.call('getGameAnalytics', this.gameFields()), () => ({ slug: this.slug ?? 'app', totalPlays: 0, uniquePlayers: 0, activeNow: 0 }));
66
- }
67
-
68
- /** Catalog-wide play counts with a per-game breakdown. */
69
- globalAnalytics(): Promise<GlobalAnalytics> {
70
- return this.withFallback(() => this.rest.call('getSiteAnalytics'), () => ({ global: { totalPlays: 0, uniquePlayers: 0, activeNow: 0 }, games: [] }));
71
- }
72
-
73
- /** The listed games, for "jump to another game" navigation. */
74
- listGames(): Promise<{ games: GameInfo[] }> {
75
- return this.withFallback(() => this.rest.call('listGames'), () => ({ games: [] }));
76
- }
77
-
78
- /** Every board of every game, each with its top `top` entries (default 5). */
79
- allLeaderboards(top = 5): Promise<EndpointReply<'listTopOfEveryLeaderboard'>> {
80
- return this.withFallback(() => this.rest.call('listTopOfEveryLeaderboard', { top }), () => ({ games: [] }));
81
- }
82
-
83
- /**
84
- * The starred games (slugs), shown first in the menu's game list. Kept per
85
- * account by the hub and mirrored to `localStorage`, so it shows offline.
86
- */
87
- favorites(): Promise<{ games: string[] }> {
88
- return this.withFallback(async () => {
89
- const reply = await this.rest.call('listFavoriteGames');
90
- writeLocalFavorites(reply.games);
91
- return reply;
92
- }, () => ({ games: readLocalFavorites() }));
93
- }
94
-
95
- /** Star (`isOn = true`) or unstar a game. Needs a connection. */
96
- async setFavorite(slug: string, isOn: boolean): Promise<{ games: string[] }> {
97
- const reply = await this.rest.call('setGameFavorite', { slug, on: isOn });
98
- writeLocalFavorites(reply.games);
99
- return reply;
100
- }
101
-
102
- /** The player's leaderboard standings, grouped by game (every game, or one via `forSlug`). Empty offline or signed out. */
103
- myRecords(forSlug?: string): Promise<EndpointReply<'listMyLeaderboardRecords'>> {
104
- return this.withFallback(
105
- () => this.rest.call('listMyLeaderboardRecords', forSlug === undefined || forSlug === '' ? {} : { game: forSlug }),
106
- () => ({ games: [] }),
107
- );
108
- }
109
-
110
- /** This game's inventory and coin balance for the current user. */
111
- getInventory(): Promise<InventorySnapshot> {
112
- return this.withFallback(() => this.rest.call('getMyInventory', this.gameFields()), () => ({ items: [], coins: 0 }));
113
- }
114
-
115
- /** Another player's available holdings (to compose a trade). */
116
- playerInventory(userId: number): Promise<InventorySnapshot> {
117
- return this.withFallback(() => this.rest.call('getPlayerInventory', { ...this.gameFields(), userId }), () => ({ items: [], coins: 0 }));
118
- }
119
-
120
- /** Add goods to the player (gameplay rewards); answers the new holdings. */
121
- grant(bundle: GrantBundle): Promise<EndpointReply<'grantInventoryItems'>> {
122
- return this.rest.call('grantInventoryItems', { ...this.gameFields(), ...bundle });
123
- }
124
-
125
- /** An atomic swap with the house: remove `take`, add `give`. */
126
- exchange(take?: TradeBundle, give?: TradeBundle): Promise<EndpointReply<'exchangeInventoryItems'>> {
127
- return this.rest.call('exchangeInventoryItems', { ...this.gameFields(), ...(take === undefined ? {} : { take }), ...(give === undefined ? {} : { give }) });
128
- }
129
-
130
- /** Sell items to the house for coins. */
131
- sell(items: Record<string, number>, coins: number): Promise<EndpointReply<'exchangeInventoryItems'>> {
132
- return this.exchange({ items }, { coins });
133
- }
134
-
135
- /** Buy items from the house with coins. */
136
- buy(coins: number, items: Record<string, number>): Promise<EndpointReply<'exchangeInventoryItems'>> {
137
- return this.exchange({ coins }, { items });
138
- }
139
-
140
- /** Wipe the player's progress in this game (saves, stats, inventory and coins). */
141
- resetGame(): Promise<{ ok: true }> {
142
- return this.rest.call('resetMyGameProgress', this.gameFields());
143
- }
144
-
145
- /** Players this player can trade with in this game. */
146
- listPlayers(): Promise<EndpointReply<'listTradePartners'>> {
147
- return this.withFallback(() => this.rest.call('listTradePartners', this.gameFields()), () => ({ players: [] }));
148
- }
149
-
150
- /** The player's trades (`box`: `incoming` or `outgoing`; `status` filters). */
151
- listTrades(opts: TradeListOptions = {}): Promise<EndpointReply<'listMyTrades'>> {
152
- return this.withFallback(() => this.rest.call('listMyTrades', { ...this.gameFields(), ...opts }), () => ({ offers: [] }));
153
- }
154
-
155
- /** Offer a trade to another player (by id or name). */
156
- createTrade(toUser: number | string, give?: TradeBundle, want?: TradeBundle, opts: TradeNoteOptions = {}): Promise<EndpointReply<'createTrade'>> {
157
- return this.rest.call('createTrade', { ...this.gameFields(), toUser: String(toUser), ...(give === undefined ? {} : { give }), ...(want === undefined ? {} : { want }), ...opts });
158
- }
159
-
160
- /** Accept a trade offered to the player. */
161
- acceptTrade(id: TradeId): Promise<EndpointReply<'acceptTrade'>> {
162
- return this.rest.call('acceptTrade', { ...this.gameFields(), id: Number(id) });
163
- }
164
-
165
- /** Decline a trade offered to the player. */
166
- declineTrade(id: TradeId): Promise<EndpointReply<'declineTrade'>> {
167
- return this.rest.call('declineTrade', { ...this.gameFields(), id: Number(id) });
168
- }
169
-
170
- /** Cancel a trade the player offered. */
171
- cancelTrade(id: TradeId): Promise<EndpointReply<'cancelTrade'>> {
172
- return this.rest.call('cancelTrade', { ...this.gameFields(), id: Number(id) });
173
- }
174
-
175
- /** Answer a trade with a counter-offer. */
176
- counterTrade(id: TradeId, give?: TradeBundle, want?: TradeBundle, opts: TradeNoteOptions = {}): Promise<EndpointReply<'counterTradeOffer'>> {
177
- return this.rest.call('counterTradeOffer', { ...this.gameFields(), id: Number(id), ...(give === undefined ? {} : { give }), ...(want === undefined ? {} : { want }), ...opts });
178
- }
179
- }
1
+ /**
2
+ * Analytics, the catalog, favorites, records, inventory and trades. Analytics
3
+ * calls are fire-and-forget and never throw (telemetry must not break a game);
4
+ * offline they are buffered and sent when the hub is back. Method names are
5
+ * the v1 client's, unchanged.
6
+ */
7
+ import { HubProgress } from './hub-progress';
8
+ import { readLocalFavorites, writeLocalFavorites } from './offline/favorites';
9
+ import { HubError } from './errors';
10
+ import type { EndpointFields, EndpointReply } from './rest/endpoint-types';
11
+ import type { GameAnalytics, GameInfo, GlobalAnalytics, InventorySnapshot } from './types';
12
+
13
+ /** Goods to grant: item key to quantity, and coins. */
14
+ export type GrantBundle = Omit<EndpointFields<'grantInventoryItems'>, 'slug'>;
15
+
16
+ /** Goods taken or given in an exchange or a trade. */
17
+ export type TradeBundle = EndpointFields<'exchangeInventoryItems'>['take'];
18
+
19
+ /** A trade offer's id, as a number or its text. */
20
+ export type TradeId = number | string;
21
+
22
+ /** Extras for creating or countering a trade. */
23
+ export interface TradeNoteOptions {
24
+ /** A short note to the other player (moderated). */
25
+ readonly note?: string;
26
+ }
27
+
28
+ /** Which trades `listTrades` lists. */
29
+ export type TradeListOptions = Omit<EndpointFields<'listMyTrades'>, 'slug'>;
30
+
31
+ /** Analytics, catalog, favorites, records, inventory and trades. */
32
+ export class HubEconomy extends HubProgress {
33
+ /** Record a "play". Call once when the game starts. */
34
+ recordPlay(): Promise<void> {
35
+ return this.event('play');
36
+ }
37
+
38
+ /** Record a "heartbeat". Call periodically while the game is active. */
39
+ heartbeat(): Promise<void> {
40
+ return this.event('heartbeat');
41
+ }
42
+
43
+ /** Record a custom event. Never throws; offline, events are buffered and sent on reconnect. */
44
+ async event(type: string): Promise<void> {
45
+ if (this.slug === null) {
46
+ return;
47
+ }
48
+ if (this.isOffline) {
49
+ this.store.bufferEvent(type);
50
+ this.notifyStatus();
51
+ return;
52
+ }
53
+ try {
54
+ await this.rest.call('recordGameEvent', { ...this.gameFields(), type });
55
+ } catch (failure) {
56
+ if (failure instanceof HubError && failure.isOffline) {
57
+ this.setReachable(false);
58
+ this.store.bufferEvent(type);
59
+ }
60
+ }
61
+ }
62
+
63
+ /** This game's play counts. */
64
+ analytics(): Promise<GameAnalytics> {
65
+ return this.withFallback(() => this.rest.call('getGameAnalytics', this.gameFields()), () => ({ slug: this.slug ?? 'app', totalPlays: 0, uniquePlayers: 0, activeNow: 0 }));
66
+ }
67
+
68
+ /** Catalog-wide play counts with a per-game breakdown. */
69
+ globalAnalytics(): Promise<GlobalAnalytics> {
70
+ return this.withFallback(() => this.rest.call('getSiteAnalytics'), () => ({ global: { totalPlays: 0, uniquePlayers: 0, activeNow: 0 }, games: [] }));
71
+ }
72
+
73
+ /** The listed games, for "jump to another game" navigation. */
74
+ listGames(): Promise<{ games: GameInfo[] }> {
75
+ return this.withFallback(() => this.rest.call('listGames'), () => ({ games: [] }));
76
+ }
77
+
78
+ /** Every board of every game, each with its top `top` entries (default 5). */
79
+ allLeaderboards(top = 5): Promise<EndpointReply<'listTopOfEveryLeaderboard'>> {
80
+ return this.withFallback(() => this.rest.call('listTopOfEveryLeaderboard', { top }), () => ({ games: [] }));
81
+ }
82
+
83
+ /**
84
+ * The starred games (slugs), shown first in the menu's game list. Kept per
85
+ * account by the hub and mirrored to `localStorage`, so it shows offline.
86
+ */
87
+ favorites(): Promise<{ games: string[] }> {
88
+ return this.withFallback(async () => {
89
+ const reply = await this.rest.call('listFavoriteGames');
90
+ writeLocalFavorites(reply.games);
91
+ return reply;
92
+ }, () => ({ games: readLocalFavorites() }));
93
+ }
94
+
95
+ /** Star (`isOn = true`) or unstar a game. Needs a connection. */
96
+ async setFavorite(slug: string, isOn: boolean): Promise<{ games: string[] }> {
97
+ const reply = await this.rest.call('setGameFavorite', { slug, on: isOn });
98
+ writeLocalFavorites(reply.games);
99
+ return reply;
100
+ }
101
+
102
+ /** The player's leaderboard standings, grouped by game (every game, or one via `forSlug`). Empty offline or signed out. */
103
+ myRecords(forSlug?: string): Promise<EndpointReply<'listMyLeaderboardRecords'>> {
104
+ return this.withFallback(
105
+ () => this.rest.call('listMyLeaderboardRecords', forSlug === undefined || forSlug === '' ? {} : { game: forSlug }),
106
+ () => ({ games: [] }),
107
+ );
108
+ }
109
+
110
+ /** This game's inventory and coin balance for the current user. */
111
+ getInventory(): Promise<InventorySnapshot> {
112
+ return this.withFallback(() => this.rest.call('getMyInventory', this.gameFields()), () => ({ items: [], coins: 0 }));
113
+ }
114
+
115
+ /** Another player's available holdings (to compose a trade). */
116
+ playerInventory(userId: number): Promise<InventorySnapshot> {
117
+ return this.withFallback(() => this.rest.call('getPlayerInventory', { ...this.gameFields(), userId }), () => ({ items: [], coins: 0 }));
118
+ }
119
+
120
+ /** Add goods to the player (gameplay rewards); answers the new holdings. */
121
+ grant(bundle: GrantBundle): Promise<EndpointReply<'grantInventoryItems'>> {
122
+ return this.rest.call('grantInventoryItems', { ...this.gameFields(), ...bundle });
123
+ }
124
+
125
+ /** An atomic swap with the house: remove `take`, add `give`. */
126
+ exchange(take?: TradeBundle, give?: TradeBundle): Promise<EndpointReply<'exchangeInventoryItems'>> {
127
+ return this.rest.call('exchangeInventoryItems', { ...this.gameFields(), ...(take === undefined ? {} : { take }), ...(give === undefined ? {} : { give }) });
128
+ }
129
+
130
+ /** Sell items to the house for coins. */
131
+ sell(items: Record<string, number>, coins: number): Promise<EndpointReply<'exchangeInventoryItems'>> {
132
+ return this.exchange({ items }, { coins });
133
+ }
134
+
135
+ /** Buy items from the house with coins. */
136
+ buy(coins: number, items: Record<string, number>): Promise<EndpointReply<'exchangeInventoryItems'>> {
137
+ return this.exchange({ coins }, { items });
138
+ }
139
+
140
+ /** Wipe the player's progress in this game (saves, stats, inventory and coins). */
141
+ resetGame(): Promise<{ ok: true }> {
142
+ return this.rest.call('resetMyGameProgress', this.gameFields());
143
+ }
144
+
145
+ /** Players this player can trade with in this game. */
146
+ listPlayers(): Promise<EndpointReply<'listTradePartners'>> {
147
+ return this.withFallback(() => this.rest.call('listTradePartners', this.gameFields()), () => ({ players: [] }));
148
+ }
149
+
150
+ /** The player's trades (`box`: `incoming` or `outgoing`; `status` filters). */
151
+ listTrades(opts: TradeListOptions = {}): Promise<EndpointReply<'listMyTrades'>> {
152
+ return this.withFallback(() => this.rest.call('listMyTrades', { ...this.gameFields(), ...opts }), () => ({ offers: [] }));
153
+ }
154
+
155
+ /** Offer a trade to another player (by id or name). */
156
+ createTrade(toUser: number | string, give?: TradeBundle, want?: TradeBundle, opts: TradeNoteOptions = {}): Promise<EndpointReply<'createTrade'>> {
157
+ return this.rest.call('createTrade', { ...this.gameFields(), toUser: String(toUser), ...(give === undefined ? {} : { give }), ...(want === undefined ? {} : { want }), ...opts });
158
+ }
159
+
160
+ /** Accept a trade offered to the player. */
161
+ acceptTrade(id: TradeId): Promise<EndpointReply<'acceptTrade'>> {
162
+ return this.rest.call('acceptTrade', { ...this.gameFields(), id: Number(id) });
163
+ }
164
+
165
+ /** Decline a trade offered to the player. */
166
+ declineTrade(id: TradeId): Promise<EndpointReply<'declineTrade'>> {
167
+ return this.rest.call('declineTrade', { ...this.gameFields(), id: Number(id) });
168
+ }
169
+
170
+ /** Cancel a trade the player offered. */
171
+ cancelTrade(id: TradeId): Promise<EndpointReply<'cancelTrade'>> {
172
+ return this.rest.call('cancelTrade', { ...this.gameFields(), id: Number(id) });
173
+ }
174
+
175
+ /** Answer a trade with a counter-offer. */
176
+ counterTrade(id: TradeId, give?: TradeBundle, want?: TradeBundle, opts: TradeNoteOptions = {}): Promise<EndpointReply<'counterTradeOffer'>> {
177
+ return this.rest.call('counterTradeOffer', { ...this.gameFields(), id: Number(id), ...(give === undefined ? {} : { give }), ...(want === undefined ? {} : { want }), ...opts });
178
+ }
179
+ }