@ksaa-nlp/gamehub-sdk 0.1.0 → 0.4.0
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 +211 -0
- package/MIGRATION.md +322 -0
- package/README.md +121 -182
- package/dist/gamehub-sdk.js +1194 -1670
- package/dist/gamehub-sdk.js.map +7 -0
- package/dist/src/api-client.d.ts +11 -0
- package/dist/src/api-client.d.ts.map +1 -0
- package/dist/src/api-client.js +27 -0
- package/dist/src/api-client.js.map +1 -0
- package/dist/src/bridge.d.ts +35 -0
- package/dist/src/bridge.d.ts.map +1 -0
- package/dist/src/bridge.js +280 -0
- package/dist/src/bridge.js.map +1 -0
- package/dist/src/browser-entry.d.ts +19 -0
- package/dist/src/browser-entry.d.ts.map +1 -0
- package/dist/src/browser-entry.js +49 -0
- package/dist/src/browser-entry.js.map +1 -0
- package/dist/src/constants.d.ts +18 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +18 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/init.d.ts +36 -0
- package/dist/src/init.d.ts.map +1 -0
- package/dist/src/init.js +317 -0
- package/dist/src/init.js.map +1 -0
- package/dist/src/main.d.ts +94 -0
- package/dist/src/main.d.ts.map +1 -0
- package/dist/src/main.js +126 -0
- package/dist/src/main.js.map +1 -0
- package/dist/src/match-context.d.ts +11 -0
- package/dist/src/match-context.d.ts.map +1 -0
- package/dist/src/match-context.js +51 -0
- package/dist/src/match-context.js.map +1 -0
- package/dist/src/multiplayer.d.ts +76 -0
- package/dist/src/multiplayer.d.ts.map +1 -0
- package/dist/src/multiplayer.js +439 -0
- package/dist/src/multiplayer.js.map +1 -0
- package/dist/src/session.d.ts +35 -0
- package/dist/src/session.d.ts.map +1 -0
- package/dist/src/session.js +206 -0
- package/dist/src/session.js.map +1 -0
- package/dist/src/state.d.ts +71 -0
- package/dist/src/state.d.ts.map +1 -0
- package/dist/src/state.js +55 -0
- package/dist/src/state.js.map +1 -0
- package/dist/src/utils.d.ts +36 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +73 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/types/api.d.ts +21 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/api.js +2 -0
- package/dist/types/api.js.map +1 -0
- package/{types/bootstrap.ts → dist/types/bootstrap.d.ts} +26 -29
- package/dist/types/bootstrap.d.ts.map +1 -0
- package/dist/types/bootstrap.js +2 -0
- package/dist/types/bootstrap.js.map +1 -0
- package/dist/types/match.d.ts +37 -0
- package/dist/types/match.d.ts.map +1 -0
- package/dist/types/match.js +2 -0
- package/dist/types/match.js.map +1 -0
- package/dist/types/multiplayer.d.ts +107 -0
- package/dist/types/multiplayer.d.ts.map +1 -0
- package/dist/types/multiplayer.js +2 -0
- package/dist/types/multiplayer.js.map +1 -0
- package/{types/session.ts → dist/types/session.d.ts} +24 -30
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/session.js +2 -0
- package/dist/types/session.js.map +1 -0
- package/package.json +34 -18
- package/dist/gamehub-sdk.d.ts +0 -694
- package/dist/index.d.ts +0 -56
- package/dist/index.mjs +0 -6
- package/scripts/emit-module-entry.mjs +0 -96
- package/src/api-client.ts +0 -33
- package/src/bridge.ts +0 -312
- package/src/constants.ts +0 -24
- package/src/init.ts +0 -335
- package/src/main.ts +0 -287
- package/src/match-context.ts +0 -52
- package/src/multiplayer.ts +0 -525
- package/src/session.ts +0 -240
- package/src/state.ts +0 -78
- package/src/utils.ts +0 -68
- package/tsconfig.json +0 -33
- package/types/api.ts +0 -21
- package/types/match.ts +0 -41
- package/types/multiplayer.ts +0 -118
package/src/init.ts
DELETED
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SDK discovery + auto-initialisation.
|
|
3
|
-
*
|
|
4
|
-
* Detects the API base URL (from the `<script>` tag or the page location),
|
|
5
|
-
* the active game slug, and the league/match context from the URL. Then
|
|
6
|
-
* exchanges a few postMessages with the host to pick up an auth token and the
|
|
7
|
-
* full bootstrap payload before opening a session and auto-joining any
|
|
8
|
-
* pending multiplayer room.
|
|
9
|
-
*/
|
|
10
|
-
namespace GameHub.Init {
|
|
11
|
-
/**
|
|
12
|
-
* One-off environment detection. Should be called exactly once at SDK boot
|
|
13
|
-
* time, before any other Init function.
|
|
14
|
-
*/
|
|
15
|
-
export function detectEnvironment(): void {
|
|
16
|
-
const S = GameHub.State;
|
|
17
|
-
S.sdkOrigin = window.location.origin;
|
|
18
|
-
|
|
19
|
-
// Detect when the game is being served under /games/<slug>/ or
|
|
20
|
-
// /uploaded-games/<slug>/ — this lets us infer both the slug and the
|
|
21
|
-
// mount point (which we use to build the API base URL).
|
|
22
|
-
const runtimePathMatch = window.location.pathname.match(
|
|
23
|
-
/^(.*)\/(?:uploaded-games|games)\/([a-z0-9-]+)(?:\/|$)/i
|
|
24
|
-
);
|
|
25
|
-
let detectedSlug: string | null = null;
|
|
26
|
-
if (runtimePathMatch) {
|
|
27
|
-
S.runtimeBasePath = runtimePathMatch[1] || '';
|
|
28
|
-
detectedSlug = runtimePathMatch[2] || null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Pull league/match context off the URL if present. The host may still
|
|
32
|
-
// override these later via postMessage.
|
|
33
|
-
S.leagueId = GameHub.Utils.readQueryParam('leagueId');
|
|
34
|
-
S.gameId = GameHub.Utils.readQueryParam('gameId');
|
|
35
|
-
S.matchId = GameHub.Utils.readQueryParam('matchId');
|
|
36
|
-
S.participant1Id = GameHub.Utils.readQueryParam('participant1Id');
|
|
37
|
-
S.participant2Id = GameHub.Utils.readQueryParam('participant2Id');
|
|
38
|
-
S.participant1MemberIds = GameHub.Utils.readCsvQueryParam(
|
|
39
|
-
'participant1MemberIds'
|
|
40
|
-
);
|
|
41
|
-
S.participant2MemberIds = GameHub.Utils.readCsvQueryParam(
|
|
42
|
-
'participant2MemberIds'
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
// Locate our own <script> tag so we can honour `data-game-slug` and
|
|
46
|
-
// resolve the path prefix (needed when served from a subpath like /app).
|
|
47
|
-
const scripts = document.getElementsByTagName('script');
|
|
48
|
-
for (let i = 0; i < scripts.length; i++) {
|
|
49
|
-
const src = scripts[i].src;
|
|
50
|
-
if (!src.includes('gamehub-sdk.js')) continue;
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
const parsedSrc = new URL(src, window.location.origin);
|
|
54
|
-
S.sdkOrigin = parsedSrc.origin;
|
|
55
|
-
const match = parsedSrc.pathname.match(/(.*)\/sdk\/gamehub-sdk\.js$/);
|
|
56
|
-
S.sdkPathPrefix = match ? match[1] || '' : '';
|
|
57
|
-
} catch {
|
|
58
|
-
const match = src.match(/(.*)\/sdk\/gamehub-sdk\.js/);
|
|
59
|
-
if (match) {
|
|
60
|
-
const fallbackUrl = new URL(match[1], window.location.origin);
|
|
61
|
-
S.sdkOrigin = fallbackUrl.origin;
|
|
62
|
-
S.sdkPathPrefix = fallbackUrl.pathname || '';
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const slugAttr = scripts[i].getAttribute('data-game-slug');
|
|
67
|
-
if (slugAttr) detectedSlug = slugAttr;
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Final fallback: look for a games slug in the path even if we didn't
|
|
72
|
-
// match the full pattern earlier.
|
|
73
|
-
if (!detectedSlug) {
|
|
74
|
-
const pathMatch = window.location.pathname.match(
|
|
75
|
-
/\/(?:uploaded-games|games)\/([a-z0-9-]+)\//i
|
|
76
|
-
);
|
|
77
|
-
if (pathMatch) detectedSlug = pathMatch[1];
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const effectiveBasePath = S.sdkPathPrefix || S.runtimeBasePath || '';
|
|
81
|
-
S.apiBase = S.sdkOrigin + effectiveBasePath + '/api';
|
|
82
|
-
S.gameSlug = detectedSlug;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Returns the share ID we should auto-join on boot, if any. Bootstrap takes
|
|
87
|
-
* priority over URL params so hosts can override a stale link.
|
|
88
|
-
*/
|
|
89
|
-
function getPendingShareId(): string | null {
|
|
90
|
-
if (GameHub.State.bootstrapShareId) return GameHub.State.bootstrapShareId;
|
|
91
|
-
return GameHub.Utils.readQueryParam('mp');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Same as {@link getPendingShareId} but for room codes. */
|
|
95
|
-
function getPendingRoomCode(): string | null {
|
|
96
|
-
if (GameHub.State.bootstrapRoomCode) return GameHub.State.bootstrapRoomCode;
|
|
97
|
-
const roomCode = GameHub.Utils.readQueryParam('roomCode');
|
|
98
|
-
return roomCode ? roomCode.trim().toUpperCase() : null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function getPendingRoomRole(): string | null {
|
|
102
|
-
if (GameHub.State.bootstrapRoomRole) return GameHub.State.bootstrapRoomRole;
|
|
103
|
-
const role = GameHub.Utils.readQueryParam('roomRole');
|
|
104
|
-
return role ? role.trim() : null;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function getPendingRoomToken(): string | null {
|
|
108
|
-
if (GameHub.State.bootstrapRoomToken)
|
|
109
|
-
return GameHub.State.bootstrapRoomToken;
|
|
110
|
-
const roomToken = GameHub.Utils.readQueryParam('roomToken');
|
|
111
|
-
return roomToken ? roomToken.trim() : null;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function getPendingWsUrl(): string | null {
|
|
115
|
-
if (GameHub.State.bootstrapWsUrl) return GameHub.State.bootstrapWsUrl;
|
|
116
|
-
const wsUrl = GameHub.Utils.readQueryParam('wsUrl');
|
|
117
|
-
return wsUrl ? wsUrl.trim() : null;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** Same as {@link getPendingShareId} but for the host-preferred display name. */
|
|
121
|
-
function getPendingPlayerName(): string | undefined {
|
|
122
|
-
if (GameHub.State.bootstrapPlayerName)
|
|
123
|
-
return GameHub.State.bootstrapPlayerName;
|
|
124
|
-
const playerName = GameHub.Utils.readQueryParam('playerName');
|
|
125
|
-
return playerName ? playerName.trim() : undefined;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Resolve the peerId for "this player" — bootstrap wins over the URL so the
|
|
130
|
-
* host can correct a stale link. Populated opportunistically so games know
|
|
131
|
-
* their assigned peerId before the multiplayer WS has joined.
|
|
132
|
-
*/
|
|
133
|
-
function seedPeerIdFromUrl(): void {
|
|
134
|
-
const S = GameHub.State;
|
|
135
|
-
if (S.bootstrapPeerId) return;
|
|
136
|
-
const fromUrl = GameHub.Utils.readQueryParam('peerId');
|
|
137
|
-
if (fromUrl) S.bootstrapPeerId = fromUrl.trim();
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function resumePendingRoomFromBootstrap(): boolean {
|
|
141
|
-
const S = GameHub.State;
|
|
142
|
-
const roomCode = getPendingRoomCode();
|
|
143
|
-
const roomToken = getPendingRoomToken();
|
|
144
|
-
const wsUrl = getPendingWsUrl();
|
|
145
|
-
const peerId =
|
|
146
|
-
S.bootstrapPeerId ||
|
|
147
|
-
GameHub.Utils.readQueryParam('peerId')?.trim() ||
|
|
148
|
-
null;
|
|
149
|
-
|
|
150
|
-
if (!S.gameSlug || !roomCode || !roomToken || !wsUrl || !peerId) {
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const role = getPendingRoomRole() || 'player';
|
|
155
|
-
S.mpViewerOverride = role === 'viewer';
|
|
156
|
-
S.mpRoom = {
|
|
157
|
-
roomCode,
|
|
158
|
-
shareId: S.bootstrapShareId || '',
|
|
159
|
-
peerId,
|
|
160
|
-
role,
|
|
161
|
-
gameSlug: S.gameSlug,
|
|
162
|
-
roomToken,
|
|
163
|
-
wsUrl,
|
|
164
|
-
};
|
|
165
|
-
S.mpLastJoinRequest = {
|
|
166
|
-
roomCode,
|
|
167
|
-
roomToken,
|
|
168
|
-
wsUrl,
|
|
169
|
-
gameSlug: S.gameSlug,
|
|
170
|
-
peerId,
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
GameHub.Multiplayer.connect(S.mpLastJoinRequest);
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async function autoJoinPendingShare(): Promise<void> {
|
|
178
|
-
const shareId = getPendingShareId();
|
|
179
|
-
if (!shareId) return;
|
|
180
|
-
if (GameHub.State.lastAutoJoinShareId === shareId) return;
|
|
181
|
-
|
|
182
|
-
try {
|
|
183
|
-
GameHub.State.lastAutoJoinShareId = shareId;
|
|
184
|
-
await GameHub.Multiplayer.joinRoomByLink(shareId, {
|
|
185
|
-
displayName: getPendingPlayerName(),
|
|
186
|
-
});
|
|
187
|
-
} catch (err: any) {
|
|
188
|
-
GameHub.State.lastAutoJoinShareId = null;
|
|
189
|
-
GameHub.Utils.warn('Auto join by link failed: ' + (err?.message || err));
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async function autoJoinPendingRoomCode(): Promise<void> {
|
|
194
|
-
const roomCode = getPendingRoomCode();
|
|
195
|
-
if (!roomCode) return;
|
|
196
|
-
if (GameHub.State.lastAutoJoinRoomCode === roomCode) return;
|
|
197
|
-
|
|
198
|
-
try {
|
|
199
|
-
GameHub.State.lastAutoJoinRoomCode = roomCode;
|
|
200
|
-
if (resumePendingRoomFromBootstrap()) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
await GameHub.Multiplayer.joinRoom(roomCode, {
|
|
204
|
-
displayName: getPendingPlayerName(),
|
|
205
|
-
});
|
|
206
|
-
} catch (err: any) {
|
|
207
|
-
GameHub.State.lastAutoJoinRoomCode = null;
|
|
208
|
-
GameHub.Utils.warn(
|
|
209
|
-
'Auto join by room code failed: ' + (err?.message || err)
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Orchestrates the full SDK boot sequence:
|
|
216
|
-
* 1. Request bootstrap config from the host (seeds slug + context).
|
|
217
|
-
* 2. Exchange for an auth token if we don't already have one.
|
|
218
|
-
* 3. Notify the host that we've booted, then fetch match context.
|
|
219
|
-
* 4. Open a session if we can, and auto-join any pending multiplayer room.
|
|
220
|
-
*
|
|
221
|
-
* Safe to call multiple times — concurrent callers share a single promise.
|
|
222
|
-
*/
|
|
223
|
-
export async function autoInit(): Promise<boolean> {
|
|
224
|
-
const S = GameHub.State;
|
|
225
|
-
if (S.isInitialized || S.isAutoInitializing)
|
|
226
|
-
return S.autoInitPromise as Promise<boolean>;
|
|
227
|
-
|
|
228
|
-
S.isAutoInitializing = true;
|
|
229
|
-
|
|
230
|
-
S.autoInitPromise = (async () => {
|
|
231
|
-
try {
|
|
232
|
-
await GameHub.Bridge.requestBootstrapFromParent();
|
|
233
|
-
seedPeerIdFromUrl();
|
|
234
|
-
|
|
235
|
-
if (!S.gameSlug) {
|
|
236
|
-
GameHub.Utils.warn(
|
|
237
|
-
'Could not resolve game slug. Ensure the host page sends bootstrap config or serves the game under /games/{slug}.'
|
|
238
|
-
);
|
|
239
|
-
S.isAutoInitializing = false;
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (!S.authToken) {
|
|
244
|
-
S.authToken = await GameHub.Bridge.getAuthTokenFromParent();
|
|
245
|
-
}
|
|
246
|
-
S.isInitialized = true;
|
|
247
|
-
S.isAutoInitializing = false;
|
|
248
|
-
|
|
249
|
-
GameHub.Bridge.sendToParent('INIT', {
|
|
250
|
-
slug: S.gameSlug,
|
|
251
|
-
auto: true,
|
|
252
|
-
});
|
|
253
|
-
await GameHub.MatchCtx.fetchMatchContext();
|
|
254
|
-
|
|
255
|
-
if (S.authToken || (S.leagueId && S.gameId && S.matchId)) {
|
|
256
|
-
S.sessionStartPromise = GameHub.Session.startSession();
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
await autoJoinPendingShare();
|
|
260
|
-
await autoJoinPendingRoomCode();
|
|
261
|
-
return true;
|
|
262
|
-
} catch (err) {
|
|
263
|
-
GameHub.Utils.error('Auto-init failed:', err);
|
|
264
|
-
S.isAutoInitializing = false;
|
|
265
|
-
return false;
|
|
266
|
-
}
|
|
267
|
-
})();
|
|
268
|
-
|
|
269
|
-
return S.autoInitPromise;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Ensures init has at least started; callers `await` this before touching
|
|
274
|
-
* any state that depends on bootstrap / auth having resolved.
|
|
275
|
-
*/
|
|
276
|
-
export async function ensureInitialized(): Promise<boolean> {
|
|
277
|
-
const S = GameHub.State;
|
|
278
|
-
if (S.isInitialized) return true;
|
|
279
|
-
if (S.autoInitPromise) return await S.autoInitPromise;
|
|
280
|
-
return await autoInit();
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Wires up the ambient message listener the host uses to push updates into
|
|
285
|
-
* the running SDK (fresh bootstrap, rotated auth, pause/resume commands).
|
|
286
|
-
*/
|
|
287
|
-
export function installHostMessageListener(): void {
|
|
288
|
-
window.addEventListener('message', (event: MessageEvent) => {
|
|
289
|
-
if (!event.data || !event.data.type) return;
|
|
290
|
-
|
|
291
|
-
const type = event.data.type;
|
|
292
|
-
const S = GameHub.State;
|
|
293
|
-
|
|
294
|
-
if (
|
|
295
|
-
type === GameHub.MESSAGE_PREFIX + '_BOOTSTRAP' &&
|
|
296
|
-
event.data.payload
|
|
297
|
-
) {
|
|
298
|
-
S.parentOrigin = event.origin;
|
|
299
|
-
S.bootstrapReceived = true;
|
|
300
|
-
GameHub.Bridge.mergeBootstrapConfig(event.data.payload);
|
|
301
|
-
|
|
302
|
-
if (S.isInitialized && S.bootstrapShareId && !S.mpRoom) {
|
|
303
|
-
autoJoinPendingShare();
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
if (type === GameHub.MESSAGE_PREFIX + '_SET_AUTH') {
|
|
308
|
-
S.authToken = event.data.token;
|
|
309
|
-
S.parentOrigin = event.origin;
|
|
310
|
-
|
|
311
|
-
if (
|
|
312
|
-
S.gameSlug &&
|
|
313
|
-
!S.sessionToken &&
|
|
314
|
-
!S.sessionStartPromise &&
|
|
315
|
-
(S.authToken || (S.leagueId && S.gameId && S.matchId))
|
|
316
|
-
) {
|
|
317
|
-
S.sessionStartPromise = GameHub.Session.startSession();
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (type === GameHub.MESSAGE_PREFIX + '_COMMAND') {
|
|
322
|
-
const command = event.data.command;
|
|
323
|
-
const w = window as any;
|
|
324
|
-
if (command === 'pause' && typeof w.onGameHubPause === 'function') {
|
|
325
|
-
w.onGameHubPause();
|
|
326
|
-
} else if (
|
|
327
|
-
command === 'resume' &&
|
|
328
|
-
typeof w.onGameHubResume === 'function'
|
|
329
|
-
) {
|
|
330
|
-
w.onGameHubResume();
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
}
|
package/src/main.ts
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Public SDK surface — the one and only object that game code interacts with.
|
|
3
|
-
*
|
|
4
|
-
* Games load this as a plain `<script>` tag, so we expose ourselves via
|
|
5
|
-
* `window.GameHubSDK`. We also emit a `module.exports` assignment for the
|
|
6
|
-
* rare case of CommonJS-style consumption in a build tool, but CommonJS is
|
|
7
|
-
* not the primary distribution channel.
|
|
8
|
-
*/
|
|
9
|
-
namespace GameHub.Public {
|
|
10
|
-
/**
|
|
11
|
-
* Shape of {@link window.GameHubSDK}. Documented separately from the
|
|
12
|
-
* implementation so the public API is easy to scan.
|
|
13
|
-
*/
|
|
14
|
-
export interface Sdk {
|
|
15
|
-
readonly version: string;
|
|
16
|
-
|
|
17
|
-
/** @returns true after auto-init has successfully resolved. */
|
|
18
|
-
isInitialized(): boolean;
|
|
19
|
-
/** @returns true when an auth token is currently known. */
|
|
20
|
-
isAuthenticated(): boolean;
|
|
21
|
-
/** @returns true when a live session token has been issued. */
|
|
22
|
-
hasActiveSession(): boolean;
|
|
23
|
-
|
|
24
|
-
/** Manually start a session (auto-init already attempts this). */
|
|
25
|
-
startSession(): Promise<StartSessionResult>;
|
|
26
|
-
|
|
27
|
-
/** Submit a final score for the current session. */
|
|
28
|
-
submitScore(
|
|
29
|
-
score: number,
|
|
30
|
-
meta?: ScoreMetadata
|
|
31
|
-
): Promise<SubmitScoreResult>;
|
|
32
|
-
|
|
33
|
-
/** Submit a score attached to the active league match. */
|
|
34
|
-
submitMatchScore(
|
|
35
|
-
score: number,
|
|
36
|
-
meta?: ScoreMetadata
|
|
37
|
-
): Promise<SubmitScoreResult>;
|
|
38
|
-
|
|
39
|
-
/** Submit a full match result (winner, scores, etc.). */
|
|
40
|
-
submitMatchResult(result?: MatchResultPayload): Promise<SubmitScoreResult>;
|
|
41
|
-
|
|
42
|
-
/** Signal the host that the game is ready to begin another round. */
|
|
43
|
-
playAgain(): Promise<{ success: boolean; token: string | null }>;
|
|
44
|
-
|
|
45
|
-
/** Tell the host the game has finished (shows completion UI). */
|
|
46
|
-
notifyComplete(data?: GameCompleteData): void;
|
|
47
|
-
|
|
48
|
-
/** Ask the host to close the game/iframe and return to the lobby. */
|
|
49
|
-
requestClose(): void;
|
|
50
|
-
|
|
51
|
-
/** Emit a custom analytics event to the host. */
|
|
52
|
-
logEvent(eventName: string, eventData?: EventData): void;
|
|
53
|
-
|
|
54
|
-
/** @returns the resolved game slug, or null if we never found one. */
|
|
55
|
-
getGameSlug(): string | null;
|
|
56
|
-
|
|
57
|
-
/** @returns whether the SDK is running inside an iframe. */
|
|
58
|
-
isInIframe(): boolean;
|
|
59
|
-
|
|
60
|
-
/** Awaitable barrier — resolves when the SDK has booted. */
|
|
61
|
-
ready(): Promise<boolean>;
|
|
62
|
-
|
|
63
|
-
/** @returns a snapshot of bootstrap-sourced state for debugging. */
|
|
64
|
-
getBootstrap(): BootstrapData;
|
|
65
|
-
|
|
66
|
-
/** Fetch the match context (lazy; cached after first success). */
|
|
67
|
-
getMatchContext(): Promise<MatchContext | null>;
|
|
68
|
-
|
|
69
|
-
/** Fetch questions for quiz-style games. Returns [] on failure. */
|
|
70
|
-
getQuestions(options?: QuestionOptions): Promise<Question[]>;
|
|
71
|
-
|
|
72
|
-
/** Multiplayer sub-API. */
|
|
73
|
-
multiplayer: {
|
|
74
|
-
createRoom(options?: RoomOptions): Promise<CreateRoomResult>;
|
|
75
|
-
joinRoom(
|
|
76
|
-
roomCode: string,
|
|
77
|
-
options?: JoinOptions
|
|
78
|
-
): Promise<JoinRoomResult>;
|
|
79
|
-
joinRoomByLink(
|
|
80
|
-
link: string,
|
|
81
|
-
options?: JoinOptions
|
|
82
|
-
): Promise<JoinRoomResult>;
|
|
83
|
-
leaveRoom(reason?: string): Promise<void>;
|
|
84
|
-
/** Send a custom event to every peer in the room. Blocked for viewers. */
|
|
85
|
-
send(eventName: string, payload?: Record<string, any>): boolean;
|
|
86
|
-
/**
|
|
87
|
-
* Host-only send. No-ops for guests and viewers. Use this for
|
|
88
|
-
* authoritative state updates (grid shuffles, turn changes, round end).
|
|
89
|
-
*/
|
|
90
|
-
broadcast(eventName: string, payload?: Record<string, any>): boolean;
|
|
91
|
-
on(eventName: string, handler: MpEventHandler): () => void;
|
|
92
|
-
getRoom(): RoomInfo | null;
|
|
93
|
-
getPeers(): Peer[];
|
|
94
|
-
isConnected(): boolean;
|
|
95
|
-
/** @returns the peer's role (`host`, `player`, `viewer`) or null. */
|
|
96
|
-
getRole(): string | null;
|
|
97
|
-
/** @returns true if this peer is the authoritative host. */
|
|
98
|
-
isHost(): boolean;
|
|
99
|
-
/** @returns true if this peer joined as a view-only guest. */
|
|
100
|
-
isViewer(): boolean;
|
|
101
|
-
/** @returns the peerId of the local player (self), or null. */
|
|
102
|
-
selfId(): string | null;
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Assemble the public API object. Methods are thin adapters that route to
|
|
108
|
-
* the internal `GameHub.*` namespaces — keeping the surface area small and
|
|
109
|
-
* the internals freely refactorable.
|
|
110
|
-
*/
|
|
111
|
-
export function create(): Sdk {
|
|
112
|
-
return {
|
|
113
|
-
version: GameHub.SDK_VERSION,
|
|
114
|
-
|
|
115
|
-
isInitialized(): boolean {
|
|
116
|
-
return GameHub.State.isInitialized;
|
|
117
|
-
},
|
|
118
|
-
|
|
119
|
-
isAuthenticated(): boolean {
|
|
120
|
-
return !!GameHub.State.authToken;
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
hasActiveSession(): boolean {
|
|
124
|
-
return !!GameHub.State.sessionToken;
|
|
125
|
-
},
|
|
126
|
-
|
|
127
|
-
async startSession(): Promise<StartSessionResult> {
|
|
128
|
-
await GameHub.Init.ensureInitialized();
|
|
129
|
-
if (GameHub.State.sessionStartPromise)
|
|
130
|
-
return await GameHub.State.sessionStartPromise;
|
|
131
|
-
return await GameHub.Session.startSession();
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
submitScore(
|
|
135
|
-
score: number,
|
|
136
|
-
meta: ScoreMetadata = {}
|
|
137
|
-
): Promise<SubmitScoreResult> {
|
|
138
|
-
return GameHub.Session.submitScore(score, meta);
|
|
139
|
-
},
|
|
140
|
-
|
|
141
|
-
submitMatchScore(
|
|
142
|
-
score: number,
|
|
143
|
-
meta: ScoreMetadata = {}
|
|
144
|
-
): Promise<SubmitScoreResult> {
|
|
145
|
-
return GameHub.Session.submitMatchScore(score, meta);
|
|
146
|
-
},
|
|
147
|
-
|
|
148
|
-
submitMatchResult(
|
|
149
|
-
result: MatchResultPayload = {}
|
|
150
|
-
): Promise<SubmitScoreResult> {
|
|
151
|
-
return GameHub.Session.submitMatchResult(result);
|
|
152
|
-
},
|
|
153
|
-
|
|
154
|
-
async playAgain(): Promise<{ success: boolean; token: string | null }> {
|
|
155
|
-
// Same session — each play is appended as a new record in plays[].
|
|
156
|
-
return { success: true, token: GameHub.State.sessionToken };
|
|
157
|
-
},
|
|
158
|
-
|
|
159
|
-
notifyComplete(data: GameCompleteData = {}): void {
|
|
160
|
-
GameHub.Bridge.sendToParent('GAME_COMPLETE', data);
|
|
161
|
-
},
|
|
162
|
-
|
|
163
|
-
requestClose(): void {
|
|
164
|
-
GameHub.Bridge.sendToParent('REQUEST_CLOSE', {});
|
|
165
|
-
},
|
|
166
|
-
|
|
167
|
-
logEvent(eventName: string, eventData: EventData = {}): void {
|
|
168
|
-
GameHub.Bridge.sendToParent('LOG_EVENT', {
|
|
169
|
-
event: eventName,
|
|
170
|
-
data: eventData,
|
|
171
|
-
});
|
|
172
|
-
},
|
|
173
|
-
|
|
174
|
-
getGameSlug(): string | null {
|
|
175
|
-
return GameHub.State.gameSlug;
|
|
176
|
-
},
|
|
177
|
-
|
|
178
|
-
isInIframe(): boolean {
|
|
179
|
-
return window.parent !== window;
|
|
180
|
-
},
|
|
181
|
-
|
|
182
|
-
async ready(): Promise<boolean> {
|
|
183
|
-
return await GameHub.Init.ensureInitialized();
|
|
184
|
-
},
|
|
185
|
-
|
|
186
|
-
getBootstrap(): BootstrapData {
|
|
187
|
-
const S = GameHub.State;
|
|
188
|
-
return {
|
|
189
|
-
gameSlug: S.gameSlug,
|
|
190
|
-
authToken: S.authToken,
|
|
191
|
-
leagueId: S.leagueId,
|
|
192
|
-
gameId: S.gameId,
|
|
193
|
-
matchId: S.matchId,
|
|
194
|
-
match: S.matchContext,
|
|
195
|
-
participant1Id: S.participant1Id,
|
|
196
|
-
participant2Id: S.participant2Id,
|
|
197
|
-
participant1MemberIds: S.participant1MemberIds,
|
|
198
|
-
participant2MemberIds: S.participant2MemberIds,
|
|
199
|
-
playerName: S.bootstrapPlayerName,
|
|
200
|
-
peerId: S.bootstrapPeerId,
|
|
201
|
-
room: S.bootstrapRoomCode
|
|
202
|
-
? {
|
|
203
|
-
roomCode: S.bootstrapRoomCode,
|
|
204
|
-
shareId: S.bootstrapShareId,
|
|
205
|
-
maxPlayers: S.bootstrapMaxPlayers,
|
|
206
|
-
role: S.bootstrapRoomRole,
|
|
207
|
-
roomToken: S.bootstrapRoomToken,
|
|
208
|
-
wsUrl: S.bootstrapWsUrl,
|
|
209
|
-
}
|
|
210
|
-
: null,
|
|
211
|
-
isBootstrapped: S.bootstrapReceived,
|
|
212
|
-
};
|
|
213
|
-
},
|
|
214
|
-
|
|
215
|
-
async getMatchContext(): Promise<MatchContext | null> {
|
|
216
|
-
await GameHub.Init.ensureInitialized();
|
|
217
|
-
return await GameHub.MatchCtx.fetchMatchContext();
|
|
218
|
-
},
|
|
219
|
-
|
|
220
|
-
getQuestions(options: QuestionOptions = {}): Promise<Question[]> {
|
|
221
|
-
return GameHub.Session.getQuestions(options);
|
|
222
|
-
},
|
|
223
|
-
|
|
224
|
-
multiplayer: {
|
|
225
|
-
createRoom: GameHub.Multiplayer.createRoom,
|
|
226
|
-
joinRoom: GameHub.Multiplayer.joinRoom,
|
|
227
|
-
joinRoomByLink: GameHub.Multiplayer.joinRoomByLink,
|
|
228
|
-
leaveRoom: GameHub.Multiplayer.leaveRoom,
|
|
229
|
-
send: GameHub.Multiplayer.send,
|
|
230
|
-
broadcast: GameHub.Multiplayer.broadcast,
|
|
231
|
-
on: GameHub.Multiplayer.on,
|
|
232
|
-
getRoom: GameHub.Multiplayer.getRoom,
|
|
233
|
-
getPeers: GameHub.Multiplayer.getPeers,
|
|
234
|
-
isConnected: GameHub.Multiplayer.isConnected,
|
|
235
|
-
getRole: GameHub.Multiplayer.getRole,
|
|
236
|
-
isHost: GameHub.Multiplayer.isHost,
|
|
237
|
-
isViewer: GameHub.Multiplayer.isViewer,
|
|
238
|
-
selfId: GameHub.Multiplayer.selfId,
|
|
239
|
-
},
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Boot sequence. Runs unconditionally as the script is loaded — everything
|
|
246
|
-
* before this point has only declared helpers/state. Wrapping in an IIFE
|
|
247
|
-
* keeps the few locals we create here out of the global scope.
|
|
248
|
-
*/
|
|
249
|
-
(function bootstrapSdk(): void {
|
|
250
|
-
'use strict';
|
|
251
|
-
|
|
252
|
-
GameHub.Init.detectEnvironment();
|
|
253
|
-
GameHub.Init.installHostMessageListener();
|
|
254
|
-
|
|
255
|
-
const sdk = GameHub.Public.create();
|
|
256
|
-
(window as any).GameHubSDK = sdk;
|
|
257
|
-
|
|
258
|
-
// CommonJS interop for consumers that `require()` this file through a
|
|
259
|
-
// bundler. Guarded so the global-first pattern still works in the browser.
|
|
260
|
-
const anyModule: any =
|
|
261
|
-
typeof (globalThis as any).module !== 'undefined'
|
|
262
|
-
? (globalThis as any).module
|
|
263
|
-
: undefined;
|
|
264
|
-
if (anyModule && anyModule.exports) {
|
|
265
|
-
anyModule.exports = sdk;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// Announce presence to the host as soon as the script runs.
|
|
269
|
-
if (window.parent !== window) {
|
|
270
|
-
GameHub.Bridge.sendToParent('READY', { version: GameHub.SDK_VERSION });
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Kick off auto-init if we have any signal we're embedded in GameHub: either
|
|
274
|
-
// we detected a slug locally, or we're inside an iframe that might feed us
|
|
275
|
-
// one. Waits for DOMContentLoaded so script tags declared after us are
|
|
276
|
-
// available for the slug-detection scan.
|
|
277
|
-
const hasBootSignal = !!GameHub.State.gameSlug || window.parent !== window;
|
|
278
|
-
if (hasBootSignal) {
|
|
279
|
-
if (document.readyState === 'loading') {
|
|
280
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
281
|
-
GameHub.Init.autoInit();
|
|
282
|
-
});
|
|
283
|
-
} else {
|
|
284
|
-
GameHub.Init.autoInit();
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
})();
|
package/src/match-context.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
namespace GameHub.MatchCtx {
|
|
2
|
-
/**
|
|
3
|
-
* Lazily load the match context for a league game. Results are cached on
|
|
4
|
-
* {@link GameHub.State.matchContext}; concurrent callers share a single
|
|
5
|
-
* in-flight request via {@link GameHub.State.matchContextPromise}.
|
|
6
|
-
*
|
|
7
|
-
* Resolves `null` when required context (league or match id) is missing, or
|
|
8
|
-
* when the server returns an error — callers decide how to degrade.
|
|
9
|
-
*/
|
|
10
|
-
export async function fetchMatchContext(): Promise<MatchContext | null> {
|
|
11
|
-
const S = GameHub.State;
|
|
12
|
-
if (!S.leagueId || !S.matchId) return null;
|
|
13
|
-
if (S.matchContext) return S.matchContext;
|
|
14
|
-
if (S.matchContextPromise) return await S.matchContextPromise;
|
|
15
|
-
|
|
16
|
-
S.matchContextPromise = GameHub.Api.apiRequest(
|
|
17
|
-
'/leagues/' +
|
|
18
|
-
encodeURIComponent(S.leagueId) +
|
|
19
|
-
'/matches/' +
|
|
20
|
-
encodeURIComponent(S.matchId),
|
|
21
|
-
{ method: 'GET' }
|
|
22
|
-
)
|
|
23
|
-
.then((result) => {
|
|
24
|
-
const data = GameHub.Utils.unwrapResult(result);
|
|
25
|
-
if (result?.success && data) {
|
|
26
|
-
S.matchContext = data as MatchContext;
|
|
27
|
-
S.participant1Id =
|
|
28
|
-
data.participant1?.participantId || S.participant1Id;
|
|
29
|
-
S.participant2Id =
|
|
30
|
-
data.participant2?.participantId || S.participant2Id;
|
|
31
|
-
S.participant1MemberIds = Array.isArray(data.participant1?.members)
|
|
32
|
-
? data.participant1.members
|
|
33
|
-
.map((member: MatchParticipantMember) => member?.leaguePlayerId)
|
|
34
|
-
.filter(Boolean)
|
|
35
|
-
: S.participant1MemberIds;
|
|
36
|
-
S.participant2MemberIds = Array.isArray(data.participant2?.members)
|
|
37
|
-
? data.participant2.members
|
|
38
|
-
.map((member: MatchParticipantMember) => member?.leaguePlayerId)
|
|
39
|
-
.filter(Boolean)
|
|
40
|
-
: S.participant2MemberIds;
|
|
41
|
-
return S.matchContext;
|
|
42
|
-
}
|
|
43
|
-
return null;
|
|
44
|
-
})
|
|
45
|
-
.catch(() => null)
|
|
46
|
-
.finally(() => {
|
|
47
|
-
S.matchContextPromise = null;
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
return await S.matchContextPromise;
|
|
51
|
-
}
|
|
52
|
-
}
|