@ksaa-nlp/gamehub-sdk 0.1.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/README.md +546 -0
- package/dist/gamehub-sdk.d.ts +694 -0
- package/dist/gamehub-sdk.js +1693 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.mjs +6 -0
- package/package.json +31 -0
- package/scripts/emit-module-entry.mjs +96 -0
- package/src/api-client.ts +33 -0
- package/src/bridge.ts +312 -0
- package/src/constants.ts +24 -0
- package/src/init.ts +335 -0
- package/src/main.ts +287 -0
- package/src/match-context.ts +52 -0
- package/src/multiplayer.ts +525 -0
- package/src/session.ts +240 -0
- package/src/state.ts +78 -0
- package/src/utils.ts +68 -0
- package/tsconfig.json +33 -0
- package/types/api.ts +21 -0
- package/types/bootstrap.ts +65 -0
- package/types/match.ts +41 -0
- package/types/multiplayer.ts +118 -0
- package/types/session.ts +67 -0
package/README.md
ADDED
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
# GameHub SDK v3.1.0 — Official Developer Guide
|
|
2
|
+
|
|
3
|
+
GameHub SDK is the browser SDK used by GameHub-hosted games to communicate with the platform for authentication, sessions, score submission, league matches, questions, and multiplayer rooms.
|
|
4
|
+
|
|
5
|
+
The SDK is distributed as a package for modern game builds and is also auto-injected by GameHub when a ZIP game is uploaded.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
### Package-based games
|
|
10
|
+
|
|
11
|
+
Use this for React, Angular, Vue, Phaser, Vite, Webpack, or any build pipeline that can bundle npm packages.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install gamehub-sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import GameHubSDK from 'gamehub-sdk';
|
|
19
|
+
|
|
20
|
+
await GameHubSDK.ready();
|
|
21
|
+
await GameHubSDK.submitScore(1500);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The package attaches the same singleton SDK instance to `window.GameHubSDK`, so package and global usage point to the same runtime object.
|
|
25
|
+
|
|
26
|
+
### GameHub ZIP uploads
|
|
27
|
+
|
|
28
|
+
When you upload a ZIP game, GameHub injects the SDK into the entry HTML automatically:
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<script src="/sdk/gamehub-sdk.js" data-game-slug="your-game-slug"></script>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Do not add your own wrapper layer around the SDK. Use the public methods directly and wait for `ready()`.
|
|
35
|
+
|
|
36
|
+
### Local standalone testing
|
|
37
|
+
|
|
38
|
+
If you run the game outside GameHub and are not bundling the npm package, add this script tag only for local testing:
|
|
39
|
+
|
|
40
|
+
```html
|
|
41
|
+
<script src="https://gamesbeta.ksaa.gov.sa/sdk/gamehub-sdk.js"></script>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Remove the tag before uploading if your build already imports the package or if GameHub will inject it.
|
|
45
|
+
|
|
46
|
+
## Required boot pattern
|
|
47
|
+
|
|
48
|
+
There is no `init()` call. The SDK auto-detects game context from the script tag, URL, and host `postMessage` bootstrap payload.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import GameHubSDK from 'gamehub-sdk';
|
|
52
|
+
|
|
53
|
+
await GameHubSDK.ready();
|
|
54
|
+
|
|
55
|
+
const bootstrap = GameHubSDK.getBootstrap();
|
|
56
|
+
console.log({
|
|
57
|
+
version: GameHubSDK.version,
|
|
58
|
+
gameSlug: bootstrap.gameSlug,
|
|
59
|
+
leagueId: bootstrap.leagueId,
|
|
60
|
+
matchId: bootstrap.matchId,
|
|
61
|
+
peerId: bootstrap.peerId,
|
|
62
|
+
currentPlayer: bootstrap.currentPlayer,
|
|
63
|
+
players: bootstrap.players,
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
For non-bundled games:
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
await window.GameHubSDK.ready();
|
|
71
|
+
const bootstrap = window.GameHubSDK.getBootstrap();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Bootstrap data
|
|
75
|
+
|
|
76
|
+
`GameHubSDK.getBootstrap()` returns the current runtime state:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
{
|
|
80
|
+
gameSlug: string | null;
|
|
81
|
+
authToken: string | null;
|
|
82
|
+
leagueId: string | null;
|
|
83
|
+
gameId: string | null;
|
|
84
|
+
matchId: string | null;
|
|
85
|
+
match: MatchContext | null;
|
|
86
|
+
participant1Id: string | null;
|
|
87
|
+
participant2Id: string | null;
|
|
88
|
+
participant1MemberIds: string[];
|
|
89
|
+
participant2MemberIds: string[];
|
|
90
|
+
shareId: string | null;
|
|
91
|
+
roomCode: string | null;
|
|
92
|
+
playerName: string | null;
|
|
93
|
+
peerId: string | null;
|
|
94
|
+
currentPlayer: BootstrapPlayer | null;
|
|
95
|
+
maxPlayers: number | null;
|
|
96
|
+
players: BootstrapPlayer[] | null;
|
|
97
|
+
room: BootstrapRoom | null;
|
|
98
|
+
isBootstrapped: boolean;
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Use `currentPlayer` to identify the local player when the host supplies a multiplayer player list.
|
|
103
|
+
|
|
104
|
+
## Normal score submission
|
|
105
|
+
|
|
106
|
+
Call `submitScore` when a standalone game round ends:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
const result = await GameHubSDK.submitScore(score, {
|
|
110
|
+
mode: 'single',
|
|
111
|
+
correctAnswers: 8,
|
|
112
|
+
totalQuestions: 10,
|
|
113
|
+
durationMs: 92000,
|
|
114
|
+
level: 3,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
if (result.skipped) {
|
|
118
|
+
// In an online room, this peer is not the host. The host will submit.
|
|
119
|
+
} else if (!result.success) {
|
|
120
|
+
console.error(result.error);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`submitScore` returns:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
{
|
|
128
|
+
success: boolean;
|
|
129
|
+
accepted?: boolean;
|
|
130
|
+
timeTaken?: number;
|
|
131
|
+
error?: string;
|
|
132
|
+
skipped?: boolean;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface MpMessage<T = unknown> {
|
|
136
|
+
roomCode: string;
|
|
137
|
+
fromPeerId: string;
|
|
138
|
+
eventName: string;
|
|
139
|
+
payload: T;
|
|
140
|
+
seq: number;
|
|
141
|
+
sentAt: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
interface Window {
|
|
145
|
+
GameHubSDK: {
|
|
146
|
+
version: string;
|
|
147
|
+
submitScore(
|
|
148
|
+
score: number,
|
|
149
|
+
meta?: Record<string, unknown>
|
|
150
|
+
): Promise<GameHubSDKResult>;
|
|
151
|
+
submitMatchScore(
|
|
152
|
+
score: number,
|
|
153
|
+
meta?: Record<string, unknown>
|
|
154
|
+
): Promise<GameHubSDKResult>;
|
|
155
|
+
submitMatchResult(result: {
|
|
156
|
+
player1Score?: number;
|
|
157
|
+
player2Score?: number;
|
|
158
|
+
participant1Score?: number;
|
|
159
|
+
participant2Score?: number;
|
|
160
|
+
winnerParticipantId?: string;
|
|
161
|
+
winnerId?: string;
|
|
162
|
+
}): Promise<GameHubSDKResult>;
|
|
163
|
+
playAgain(): Promise<{ success: boolean; token?: string; error?: string }>;
|
|
164
|
+
ready(): Promise<boolean>;
|
|
165
|
+
isAuthenticated(): boolean;
|
|
166
|
+
hasActiveSession(): boolean;
|
|
167
|
+
notifyComplete(data?: Record<string, unknown>): void;
|
|
168
|
+
requestClose(): void;
|
|
169
|
+
logEvent(name: string, data?: Record<string, unknown>): void;
|
|
170
|
+
getBootstrap(): {
|
|
171
|
+
gameSlug?: string | null;
|
|
172
|
+
authToken?: string | null;
|
|
173
|
+
leagueId?: string | null;
|
|
174
|
+
gameId?: string | null;
|
|
175
|
+
matchId?: string | null;
|
|
176
|
+
participant1Id?: string | null;
|
|
177
|
+
participant2Id?: string | null;
|
|
178
|
+
participant1MemberIds?: string[];
|
|
179
|
+
participant2MemberIds?: string[];
|
|
180
|
+
peerId?: string | null;
|
|
181
|
+
playerName?: string | null;
|
|
182
|
+
room?: {
|
|
183
|
+
roomCode: string | null;
|
|
184
|
+
shareId: string | null;
|
|
185
|
+
maxPlayers: number | null;
|
|
186
|
+
} | null;
|
|
187
|
+
isBootstrapped?: boolean;
|
|
188
|
+
};
|
|
189
|
+
getGameSlug(): string | null;
|
|
190
|
+
isInIframe(): boolean;
|
|
191
|
+
|
|
192
|
+
multiplayer: {
|
|
193
|
+
createRoom(opts?: { maxPlayers?: number; passcode?: string }): Promise<{
|
|
194
|
+
success: boolean;
|
|
195
|
+
roomCode?: string;
|
|
196
|
+
shareId?: string;
|
|
197
|
+
shareUrl?: string;
|
|
198
|
+
peerId?: string;
|
|
199
|
+
roomToken?: string;
|
|
200
|
+
error?: string;
|
|
201
|
+
}>;
|
|
202
|
+
joinRoom(
|
|
203
|
+
roomCode: string,
|
|
204
|
+
opts?: { passcode?: string; displayName?: string; asViewer?: boolean }
|
|
205
|
+
): Promise<{
|
|
206
|
+
success: boolean;
|
|
207
|
+
roomCode?: string;
|
|
208
|
+
peerId?: string;
|
|
209
|
+
error?: string;
|
|
210
|
+
}>;
|
|
211
|
+
joinRoomByLink(
|
|
212
|
+
link: string,
|
|
213
|
+
opts?: { passcode?: string; displayName?: string; asViewer?: boolean }
|
|
214
|
+
): Promise<{
|
|
215
|
+
success: boolean;
|
|
216
|
+
roomCode?: string;
|
|
217
|
+
peerId?: string;
|
|
218
|
+
error?: string;
|
|
219
|
+
}>;
|
|
220
|
+
leaveRoom(reason?: string): Promise<void>;
|
|
221
|
+
send(eventName: string, payload?: Record<string, unknown>): boolean;
|
|
222
|
+
broadcast(eventName: string, payload?: Record<string, unknown>): boolean;
|
|
223
|
+
on(
|
|
224
|
+
eventName: string,
|
|
225
|
+
handler: (msg: MpMessage | Record<string, unknown>) => void
|
|
226
|
+
): () => void;
|
|
227
|
+
getRoom(): {
|
|
228
|
+
roomCode: string;
|
|
229
|
+
shareId: string;
|
|
230
|
+
peerId: string;
|
|
231
|
+
role: string;
|
|
232
|
+
} | null;
|
|
233
|
+
getPeers(): Array<{
|
|
234
|
+
peerId: string;
|
|
235
|
+
role?: string;
|
|
236
|
+
displayName?: string;
|
|
237
|
+
}>;
|
|
238
|
+
isConnected(): boolean;
|
|
239
|
+
isHost(): boolean;
|
|
240
|
+
isViewer(): boolean;
|
|
241
|
+
getRole(): string | null;
|
|
242
|
+
selfId(): string | null;
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Inside a multiplayer room, only the host can write scores. Guests and viewers receive `{ success: true, skipped: true }` without a network write.
|
|
249
|
+
|
|
250
|
+
## League match integration
|
|
251
|
+
|
|
252
|
+
When a game is opened from a league match, the host supplies `leagueId`, `gameId`, and `matchId`. The SDK uses these values to start a scoped match session.
|
|
253
|
+
|
|
254
|
+
Fetch participant names and team members:
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
await GameHubSDK.ready();
|
|
258
|
+
|
|
259
|
+
const match = await GameHubSDK.getMatchContext();
|
|
260
|
+
if (match) {
|
|
261
|
+
renderSideA(match.participant1.name, match.participant1.members);
|
|
262
|
+
renderSideB(match.participant2.name, match.participant2.members);
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
`MatchContext`:
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
{
|
|
270
|
+
id: string;
|
|
271
|
+
leagueId: string;
|
|
272
|
+
gameId: string;
|
|
273
|
+
participant1: {
|
|
274
|
+
participantId: string;
|
|
275
|
+
name?: string;
|
|
276
|
+
members?: Array<{ leaguePlayerId: string; fullname?: string; avatar?: string }>;
|
|
277
|
+
};
|
|
278
|
+
participant2: {
|
|
279
|
+
participantId: string;
|
|
280
|
+
name?: string;
|
|
281
|
+
members?: Array<{ leaguePlayerId: string; fullname?: string; avatar?: string }>;
|
|
282
|
+
};
|
|
283
|
+
status: string;
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Submit the match winner and scores:
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
await GameHubSDK.submitMatchResult({
|
|
291
|
+
winnerId: match.participant1.participantId,
|
|
292
|
+
scores: {
|
|
293
|
+
[match.participant1.participantId]: 12,
|
|
294
|
+
[match.participant2.participantId]: 8,
|
|
295
|
+
},
|
|
296
|
+
reason: 'normal_finish',
|
|
297
|
+
details: {
|
|
298
|
+
durationMs: 94000,
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
For a match-specific score without a full bracket result:
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
await GameHubSDK.submitMatchScore(1200, {
|
|
307
|
+
correctAnswers: 10,
|
|
308
|
+
totalQuestions: 12,
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
In multiplayer league matches, the host should submit the match result. If shared game code calls `submitMatchResult` from a guest or viewer, the SDK returns `skipped`.
|
|
313
|
+
|
|
314
|
+
## Questions
|
|
315
|
+
|
|
316
|
+
Fetch published questions for the active game:
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
const questions = await GameHubSDK.getQuestions({
|
|
320
|
+
difficulty: 'medium',
|
|
321
|
+
category: 'science',
|
|
322
|
+
limit: 20,
|
|
323
|
+
});
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
The SDK uses `gameId` when provided by the host. Otherwise it falls back to the game slug.
|
|
327
|
+
|
|
328
|
+
Question shape:
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
{
|
|
332
|
+
id: string;
|
|
333
|
+
difficulty?: 'easy' | 'medium' | 'hard' | string;
|
|
334
|
+
category?: string;
|
|
335
|
+
prompt: string;
|
|
336
|
+
choices: string[];
|
|
337
|
+
answer: string | number;
|
|
338
|
+
explanation?: string;
|
|
339
|
+
points: number;
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
Invalid options or API failures return an empty array.
|
|
344
|
+
|
|
345
|
+
## Multiplayer model
|
|
346
|
+
|
|
347
|
+
GameHub multiplayer is host-authoritative:
|
|
348
|
+
|
|
349
|
+
| Concern | Owner |
|
|
350
|
+
| -------------------------------- | --------------------------------------------- |
|
|
351
|
+
| Random grids, seeds, decks | Host |
|
|
352
|
+
| Turn order and round advancement | Host |
|
|
353
|
+
| Click/move validation | Host |
|
|
354
|
+
| Player input | Local player sends intent |
|
|
355
|
+
| Final score and match result | Host only |
|
|
356
|
+
| Timer | Host broadcasts or sends an absolute deadline |
|
|
357
|
+
|
|
358
|
+
Guests send intent with `send`. The host validates and publishes resolved state with `broadcast`.
|
|
359
|
+
|
|
360
|
+
### Create and join
|
|
361
|
+
|
|
362
|
+
```ts
|
|
363
|
+
const room = await GameHubSDK.multiplayer.createRoom({
|
|
364
|
+
maxPlayers: 2,
|
|
365
|
+
passcode: '1234',
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
console.log(room.roomCode, room.shareUrl);
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
```ts
|
|
372
|
+
await GameHubSDK.multiplayer.joinRoom('AB12CD', {
|
|
373
|
+
passcode: '1234',
|
|
374
|
+
displayName: 'Player 2',
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
await GameHubSDK.multiplayer.joinRoomByLink(window.location.href);
|
|
378
|
+
|
|
379
|
+
// Read-only guest:
|
|
380
|
+
await GameHubSDK.multiplayer.joinRoom('AB12CD', { asViewer: true });
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Send and broadcast
|
|
384
|
+
### SDK auto-join
|
|
385
|
+
|
|
386
|
+
The SDK **auto-joins on boot** when it can detect a pending room — you don't need to wire any of this yourself:
|
|
387
|
+
|
|
388
|
+
| Signal | Where it comes from |
|
|
389
|
+
| ------------------------------------------------------------ | ---------------------------------------------------------------------- |
|
|
390
|
+
| `?mp=<shareId>` on the page URL | QR scan or shared link — SDK auto-calls `joinRoomByLink`. |
|
|
391
|
+
| `?roomCode=<CODE>&playerName=<name>` | Host UI forwarded this after `join-token` — SDK auto-calls `joinRoom`. |
|
|
392
|
+
| `GAMEHUB_SDK_BOOTSTRAP` postMessage (`shareId` / `roomCode`) | Host page pushed them into the iframe. |
|
|
393
|
+
|
|
394
|
+
By the time `await GameHubSDK.ready()` resolves, your `getBootstrap()` already contains `peerId` and `room: { roomCode, shareId, maxPlayers }`, and the socket is connecting. **The bootstrap does not contain the peer list or live room state** — those would be stale by the time you read them. Use `multiplayer.getRoom()`, `multiplayer.getPeers()`, and listen to `connected` / `room_joined` / `peer_joined` / `peer_left` for live data.
|
|
395
|
+
|
|
396
|
+
### Send and receive game events
|
|
397
|
+
|
|
398
|
+
```javascript
|
|
399
|
+
// Any peer can send intent — host and guests both use this for inputs.
|
|
400
|
+
GameHubSDK.multiplayer.send('cell_clicked', { x: 3, y: 5 });
|
|
401
|
+
|
|
402
|
+
```ts
|
|
403
|
+
const mp = GameHubSDK.multiplayer;
|
|
404
|
+
|
|
405
|
+
function onCellClick(cellId: string) {
|
|
406
|
+
mp.send('cell_click', { cellId });
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
mp.on('cell_click', ({ fromPeerId, payload }) => {
|
|
410
|
+
if (!mp.isHost()) return;
|
|
411
|
+
const result = resolveMove(fromPeerId, payload.cellId);
|
|
412
|
+
mp.broadcast('cell_resolved', result);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
mp.on('cell_resolved', ({ payload }) => {
|
|
416
|
+
applyResolvedMove(payload);
|
|
417
|
+
});
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
`broadcast` is host-only. It returns `false` for guests and viewers.
|
|
421
|
+
|
|
422
|
+
### Role helpers
|
|
423
|
+
|
|
424
|
+
```ts
|
|
425
|
+
GameHubSDK.multiplayer.getRole(); // 'host' | 'player' | 'viewer' | null
|
|
426
|
+
GameHubSDK.multiplayer.isHost();
|
|
427
|
+
GameHubSDK.multiplayer.isViewer();
|
|
428
|
+
GameHubSDK.multiplayer.selfId();
|
|
429
|
+
GameHubSDK.multiplayer.getRoom();
|
|
430
|
+
GameHubSDK.multiplayer.getPeers();
|
|
431
|
+
GameHubSDK.multiplayer.isConnected();
|
|
432
|
+
GameHubSDK.multiplayer.leaveRoom();
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
### Events
|
|
436
|
+
|
|
437
|
+
Subscribe with `GameHubSDK.multiplayer.on(eventName, handler)`. Use `'*'` to observe all events.
|
|
438
|
+
|
|
439
|
+
| Event | Payload | Meaning |
|
|
440
|
+
| -------------- | ------------------------------------------------- | --------------------------- |
|
|
441
|
+
| `connected` | `{ roomCode, peerId }` | WebSocket opened |
|
|
442
|
+
| `disconnected` | `{ roomCode }` | WebSocket closed |
|
|
443
|
+
| `reconnecting` | `{ attempts, delayMs }` | SDK is reconnecting |
|
|
444
|
+
| `room_joined` | `{ peerId, role, peers }` | Local peer joined |
|
|
445
|
+
| `peer_joined` | `{ peerId, role, ... }` | Another peer joined |
|
|
446
|
+
| `peer_left` | `{ peerId }` | A peer left |
|
|
447
|
+
| `room_closed` | `{ reason }` | Host/server closed the room |
|
|
448
|
+
| `room_left` | `{ reason }` | Local peer left |
|
|
449
|
+
| `error` | `{ code }` | Server rejected a frame |
|
|
450
|
+
| `message` | `{ eventName, payload, fromPeerId, seq, sentAt }` | Any custom game event |
|
|
451
|
+
|
|
452
|
+
Custom game events are also emitted by their own name:
|
|
453
|
+
|
|
454
|
+
```ts
|
|
455
|
+
mp.on('round_resolved', ({ payload, fromPeerId, seq }) => {
|
|
456
|
+
applyRound(payload);
|
|
457
|
+
});
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
The SDK drops late or duplicate game-event frames using the server-provided `seq` per sending peer.
|
|
461
|
+
|
|
462
|
+
### Auto-join
|
|
463
|
+
|
|
464
|
+
The SDK auto-joins when it sees one of these signals before `ready()` resolves:
|
|
465
|
+
|
|
466
|
+
| Signal | Source |
|
|
467
|
+
| ------------------------------------ | --------------------------- |
|
|
468
|
+
| `?mp=<shareId>` | Share link or QR scan |
|
|
469
|
+
| `?roomCode=<CODE>&playerName=<name>` | Host page forwarding a code |
|
|
470
|
+
| Bootstrap `shareId` / `roomCode` | Parent page postMessage |
|
|
471
|
+
|
|
472
|
+
## Public API reference
|
|
473
|
+
|
|
474
|
+
Core:
|
|
475
|
+
|
|
476
|
+
- `version: string`
|
|
477
|
+
- `ready(): Promise<boolean>`
|
|
478
|
+
- `isInitialized(): boolean`
|
|
479
|
+
- `isAuthenticated(): boolean`
|
|
480
|
+
- `hasActiveSession(): boolean`
|
|
481
|
+
- `startSession(): Promise<StartSessionResult>`
|
|
482
|
+
- `submitScore(score, meta?): Promise<SubmitScoreResult>`
|
|
483
|
+
- `submitMatchScore(score, meta?): Promise<SubmitScoreResult>`
|
|
484
|
+
- `submitMatchResult(result?): Promise<SubmitScoreResult>`
|
|
485
|
+
- `playAgain(): Promise<{ success: boolean; token: string | null }>`
|
|
486
|
+
- `notifyComplete(data?): void`
|
|
487
|
+
- `requestClose(): void`
|
|
488
|
+
- `logEvent(eventName, eventData?): void`
|
|
489
|
+
- `getGameSlug(): string | null`
|
|
490
|
+
- `isInIframe(): boolean`
|
|
491
|
+
- `getBootstrap(): BootstrapData`
|
|
492
|
+
- `getMatchContext(): Promise<MatchContext | null>`
|
|
493
|
+
- `getQuestions(options?): Promise<Question[]>`
|
|
494
|
+
|
|
495
|
+
Multiplayer:
|
|
496
|
+
|
|
497
|
+
- `multiplayer.createRoom(options?)`
|
|
498
|
+
- `multiplayer.joinRoom(roomCode, options?)`
|
|
499
|
+
- `multiplayer.joinRoomByLink(link, options?)`
|
|
500
|
+
- `multiplayer.leaveRoom(reason?)`
|
|
501
|
+
- `multiplayer.send(eventName, payload?)`
|
|
502
|
+
- `multiplayer.broadcast(eventName, payload?)`
|
|
503
|
+
- `multiplayer.on(eventName, handler)`
|
|
504
|
+
- `multiplayer.getRoom()`
|
|
505
|
+
- `multiplayer.getPeers()`
|
|
506
|
+
- `multiplayer.isConnected()`
|
|
507
|
+
- `multiplayer.getRole()`
|
|
508
|
+
- `multiplayer.isHost()`
|
|
509
|
+
- `multiplayer.isViewer()`
|
|
510
|
+
- `multiplayer.selfId()`
|
|
511
|
+
|
|
512
|
+
## Pause and resume
|
|
513
|
+
|
|
514
|
+
If the host pauses/resumes the iframe, define these optional handlers:
|
|
515
|
+
|
|
516
|
+
```js
|
|
517
|
+
window.onGameHubPause = function () {
|
|
518
|
+
pauseGame();
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
window.onGameHubResume = function () {
|
|
522
|
+
resumeGame();
|
|
523
|
+
};
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
## Common issues
|
|
527
|
+
|
|
528
|
+
### `window.GameHubSDK` is undefined
|
|
529
|
+
|
|
530
|
+
The SDK is not loaded. Import `gamehub-sdk` in a bundled app, use the local testing script tag, or test inside a GameHub-uploaded game.
|
|
531
|
+
|
|
532
|
+
### Score returns `skipped`
|
|
533
|
+
|
|
534
|
+
The current peer is not the multiplayer host, or it joined as a viewer. This is expected and prevents duplicate writes.
|
|
535
|
+
|
|
536
|
+
### Match context is `null`
|
|
537
|
+
|
|
538
|
+
The game was not launched with `leagueId` and `matchId`, or the match endpoint could not resolve the match. Fall back to normal game UI.
|
|
539
|
+
|
|
540
|
+
### Questions return `[]`
|
|
541
|
+
|
|
542
|
+
No published questions matched the filters, options were invalid, or the API request failed. Treat this as the empty/degraded state.
|
|
543
|
+
|
|
544
|
+
### Guest state differs from host
|
|
545
|
+
|
|
546
|
+
Move all random generation and final state transitions to the host. Guests should send intent only and wait for `broadcast`ed resolved events.
|