@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/README.md
CHANGED
|
@@ -1,27 +1,45 @@
|
|
|
1
|
-
# GameHub SDK
|
|
1
|
+
# GameHub SDK v0.4.0 — Official Developer Guide
|
|
2
2
|
|
|
3
|
-
GameHub SDK is
|
|
3
|
+
GameHub SDK is an ESM SDK used by GameHub-hosted games to communicate with the platform for authentication, sessions, score submission, league matches, questions, and multiplayer rooms.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
It runs in any modern JavaScript environment — browser, Node, Angular Universal / SSR, Vite, webpack, esbuild, Bun. It also ships a self-contained IIFE bundle for plain `<script>`-tag use.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
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.
|
|
9
|
+
### Package-based games (Angular, React, Vue, Phaser, Vite, webpack, ...)
|
|
12
10
|
|
|
13
11
|
```bash
|
|
14
|
-
npm install gamehub-sdk
|
|
12
|
+
npm install @ksaa-nlp/gamehub-sdk
|
|
15
13
|
```
|
|
16
14
|
|
|
17
15
|
```ts
|
|
18
|
-
import
|
|
16
|
+
import { createSdk, type Sdk } from '@ksaa-nlp/gamehub-sdk';
|
|
17
|
+
|
|
18
|
+
const sdk: Sdk = createSdk();
|
|
19
|
+
|
|
20
|
+
await sdk.ready();
|
|
21
|
+
await sdk.submitScore(1500);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Importing the package has **no side effects** — `createSdk()` builds an SDK instance you control. Calling any method that needs platform state (e.g. `ready()`, `submitScore()`) lazily triggers the bootstrap handshake the first time it's needed.
|
|
25
|
+
|
|
26
|
+
The package is **ESM-only**. Modern Node (≥18), Angular CLI, Vite, webpack 5+, esbuild, Bun, and Rollup all consume ESM natively. Tooling that requires CJS `require()` needs a transpile step or a dynamic `import()`.
|
|
27
|
+
|
|
28
|
+
### Server-side rendering (Angular Universal, Next-style SSR, Node services)
|
|
29
|
+
|
|
30
|
+
The package is **safe to import on the server** — no `window` / `document` access happens at evaluation time. Methods called server-side return sensible defaults instead of throwing:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
const sdk = createSdk();
|
|
19
34
|
|
|
20
|
-
|
|
21
|
-
|
|
35
|
+
// Safe on the server — no ReferenceError, no exception.
|
|
36
|
+
sdk.isInIframe(); // false
|
|
37
|
+
sdk.getBootstrap().isBootstrapped; // false
|
|
38
|
+
sdk.notifyComplete({ score: 0 }); // silently no-ops
|
|
39
|
+
await sdk.ready(); // resolves to false
|
|
22
40
|
```
|
|
23
41
|
|
|
24
|
-
|
|
42
|
+
You don't need `isPlatformBrowser` / `typeof window` gates around the import or around `createSdk()`.
|
|
25
43
|
|
|
26
44
|
### GameHub ZIP uploads
|
|
27
45
|
|
|
@@ -31,7 +49,7 @@ When you upload a ZIP game, GameHub injects the SDK into the entry HTML automati
|
|
|
31
49
|
<script src="/sdk/gamehub-sdk.js" data-game-slug="your-game-slug"></script>
|
|
32
50
|
```
|
|
33
51
|
|
|
34
|
-
Do not add your own wrapper layer around the SDK. Use the public methods directly and wait for `ready()`.
|
|
52
|
+
This loads the IIFE bundle, attaches `window.GameHubSDK`, and runs the auto-init handshake on `DOMContentLoaded`. Do not add your own wrapper layer around the SDK. Use the public methods directly and wait for `ready()`.
|
|
35
53
|
|
|
36
54
|
### Local standalone testing
|
|
37
55
|
|
|
@@ -45,26 +63,26 @@ Remove the tag before uploading if your build already imports the package or if
|
|
|
45
63
|
|
|
46
64
|
## Required boot pattern
|
|
47
65
|
|
|
48
|
-
There is no `init()` call. The SDK auto-detects game context from the script tag, URL, and host `postMessage` bootstrap payload.
|
|
66
|
+
There is no separate `init()` call. The SDK auto-detects game context from the script tag (or your import), URL, and host `postMessage` bootstrap payload — `await sdk.ready()` runs the handshake and resolves when it's done.
|
|
49
67
|
|
|
50
68
|
```ts
|
|
51
|
-
import
|
|
69
|
+
import { createSdk } from '@ksaa-nlp/gamehub-sdk';
|
|
52
70
|
|
|
53
|
-
|
|
71
|
+
const sdk = createSdk();
|
|
72
|
+
await sdk.ready();
|
|
54
73
|
|
|
55
|
-
const bootstrap =
|
|
74
|
+
const bootstrap = sdk.getBootstrap();
|
|
56
75
|
console.log({
|
|
57
|
-
version:
|
|
76
|
+
version: sdk.version,
|
|
58
77
|
gameSlug: bootstrap.gameSlug,
|
|
59
78
|
leagueId: bootstrap.leagueId,
|
|
60
79
|
matchId: bootstrap.matchId,
|
|
61
80
|
peerId: bootstrap.peerId,
|
|
62
|
-
|
|
63
|
-
players: bootstrap.players,
|
|
81
|
+
room: bootstrap.room,
|
|
64
82
|
});
|
|
65
83
|
```
|
|
66
84
|
|
|
67
|
-
For
|
|
85
|
+
For `<script>`-tag games:
|
|
68
86
|
|
|
69
87
|
```js
|
|
70
88
|
await window.GameHubSDK.ready();
|
|
@@ -73,7 +91,7 @@ const bootstrap = window.GameHubSDK.getBootstrap();
|
|
|
73
91
|
|
|
74
92
|
## Bootstrap data
|
|
75
93
|
|
|
76
|
-
`
|
|
94
|
+
`sdk.getBootstrap()` returns the current runtime state:
|
|
77
95
|
|
|
78
96
|
```ts
|
|
79
97
|
{
|
|
@@ -87,26 +105,21 @@ const bootstrap = window.GameHubSDK.getBootstrap();
|
|
|
87
105
|
participant2Id: string | null;
|
|
88
106
|
participant1MemberIds: string[];
|
|
89
107
|
participant2MemberIds: string[];
|
|
90
|
-
shareId: string | null;
|
|
91
|
-
roomCode: string | null;
|
|
92
108
|
playerName: string | null;
|
|
93
109
|
peerId: string | null;
|
|
94
|
-
|
|
95
|
-
maxPlayers: number | null;
|
|
96
|
-
players: BootstrapPlayer[] | null;
|
|
97
|
-
room: BootstrapRoom | null;
|
|
110
|
+
room: BootstrapRoom | null; // null for non-multiplayer sessions
|
|
98
111
|
isBootstrapped: boolean;
|
|
99
112
|
}
|
|
100
113
|
```
|
|
101
114
|
|
|
102
|
-
|
|
115
|
+
For live peer/room state, use `sdk.multiplayer.getRoom()`, `sdk.multiplayer.getPeers()`, and the `peer_joined` / `peer_left` events — bootstrap intentionally doesn't expose live counts because they'd be stale by the time you read them.
|
|
103
116
|
|
|
104
|
-
##
|
|
117
|
+
## Score submission
|
|
105
118
|
|
|
106
119
|
Call `submitScore` when a standalone game round ends:
|
|
107
120
|
|
|
108
121
|
```ts
|
|
109
|
-
const result = await
|
|
122
|
+
const result = await sdk.submitScore(score, {
|
|
110
123
|
mode: 'single',
|
|
111
124
|
correctAnswers: 8,
|
|
112
125
|
totalQuestions: 10,
|
|
@@ -121,7 +134,7 @@ if (result.skipped) {
|
|
|
121
134
|
}
|
|
122
135
|
```
|
|
123
136
|
|
|
124
|
-
`submitScore` returns
|
|
137
|
+
`submitScore` returns a `SubmitScoreResult`:
|
|
125
138
|
|
|
126
139
|
```ts
|
|
127
140
|
{
|
|
@@ -131,118 +144,6 @@ if (result.skipped) {
|
|
|
131
144
|
error?: string;
|
|
132
145
|
skipped?: boolean;
|
|
133
146
|
}
|
|
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
147
|
```
|
|
247
148
|
|
|
248
149
|
Inside a multiplayer room, only the host can write scores. Guests and viewers receive `{ success: true, skipped: true }` without a network write.
|
|
@@ -254,9 +155,9 @@ When a game is opened from a league match, the host supplies `leagueId`, `gameId
|
|
|
254
155
|
Fetch participant names and team members:
|
|
255
156
|
|
|
256
157
|
```ts
|
|
257
|
-
await
|
|
158
|
+
await sdk.ready();
|
|
258
159
|
|
|
259
|
-
const match = await
|
|
160
|
+
const match = await sdk.getMatchContext();
|
|
260
161
|
if (match) {
|
|
261
162
|
renderSideA(match.participant1.name, match.participant1.members);
|
|
262
163
|
renderSideB(match.participant2.name, match.participant2.members);
|
|
@@ -287,7 +188,7 @@ if (match) {
|
|
|
287
188
|
Submit the match winner and scores:
|
|
288
189
|
|
|
289
190
|
```ts
|
|
290
|
-
await
|
|
191
|
+
await sdk.submitMatchResult({
|
|
291
192
|
winnerId: match.participant1.participantId,
|
|
292
193
|
scores: {
|
|
293
194
|
[match.participant1.participantId]: 12,
|
|
@@ -303,7 +204,7 @@ await GameHubSDK.submitMatchResult({
|
|
|
303
204
|
For a match-specific score without a full bracket result:
|
|
304
205
|
|
|
305
206
|
```ts
|
|
306
|
-
await
|
|
207
|
+
await sdk.submitMatchScore(1200, {
|
|
307
208
|
correctAnswers: 10,
|
|
308
209
|
totalQuestions: 12,
|
|
309
210
|
});
|
|
@@ -316,7 +217,7 @@ In multiplayer league matches, the host should submit the match result. If share
|
|
|
316
217
|
Fetch published questions for the active game:
|
|
317
218
|
|
|
318
219
|
```ts
|
|
319
|
-
const questions = await
|
|
220
|
+
const questions = await sdk.getQuestions({
|
|
320
221
|
difficulty: 'medium',
|
|
321
222
|
category: 'science',
|
|
322
223
|
limit: 20,
|
|
@@ -360,7 +261,7 @@ Guests send intent with `send`. The host validates and publishes resolved state
|
|
|
360
261
|
### Create and join
|
|
361
262
|
|
|
362
263
|
```ts
|
|
363
|
-
const room = await
|
|
264
|
+
const room = await sdk.multiplayer.createRoom({
|
|
364
265
|
maxPlayers: 2,
|
|
365
266
|
passcode: '1234',
|
|
366
267
|
});
|
|
@@ -369,21 +270,20 @@ console.log(room.roomCode, room.shareUrl);
|
|
|
369
270
|
```
|
|
370
271
|
|
|
371
272
|
```ts
|
|
372
|
-
await
|
|
273
|
+
await sdk.multiplayer.joinRoom('AB12CD', {
|
|
373
274
|
passcode: '1234',
|
|
374
275
|
displayName: 'Player 2',
|
|
375
276
|
});
|
|
376
277
|
|
|
377
|
-
await
|
|
278
|
+
await sdk.multiplayer.joinRoomByLink(window.location.href);
|
|
378
279
|
|
|
379
280
|
// Read-only guest:
|
|
380
|
-
await
|
|
281
|
+
await sdk.multiplayer.joinRoom('AB12CD', { asViewer: true });
|
|
381
282
|
```
|
|
382
283
|
|
|
383
|
-
###
|
|
384
|
-
### SDK auto-join
|
|
284
|
+
### Auto-join on boot
|
|
385
285
|
|
|
386
|
-
The SDK
|
|
286
|
+
The SDK auto-joins when it can detect a pending room — you don't need to wire any of this yourself:
|
|
387
287
|
|
|
388
288
|
| Signal | Where it comes from |
|
|
389
289
|
| ------------------------------------------------------------ | ---------------------------------------------------------------------- |
|
|
@@ -391,16 +291,14 @@ The SDK **auto-joins on boot** when it can detect a pending room — you don't n
|
|
|
391
291
|
| `?roomCode=<CODE>&playerName=<name>` | Host UI forwarded this after `join-token` — SDK auto-calls `joinRoom`. |
|
|
392
292
|
| `GAMEHUB_SDK_BOOTSTRAP` postMessage (`shareId` / `roomCode`) | Host page pushed them into the iframe. |
|
|
393
293
|
|
|
394
|
-
By the time `await
|
|
294
|
+
By the time `await sdk.ready()` resolves, your `getBootstrap()` already contains `peerId` and `room`, 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
295
|
|
|
396
296
|
### Send and receive game events
|
|
397
297
|
|
|
398
|
-
|
|
399
|
-
// Any peer can send intent — host and guests both use this for inputs.
|
|
400
|
-
GameHubSDK.multiplayer.send('cell_clicked', { x: 3, y: 5 });
|
|
298
|
+
Guests send intent with `send()`. The host validates and publishes resolved state with `broadcast()` — host-only; returns `false` for guests and viewers.
|
|
401
299
|
|
|
402
300
|
```ts
|
|
403
|
-
const mp =
|
|
301
|
+
const mp = sdk.multiplayer;
|
|
404
302
|
|
|
405
303
|
function onCellClick(cellId: string) {
|
|
406
304
|
mp.send('cell_click', { cellId });
|
|
@@ -417,24 +315,22 @@ mp.on('cell_resolved', ({ payload }) => {
|
|
|
417
315
|
});
|
|
418
316
|
```
|
|
419
317
|
|
|
420
|
-
`broadcast` is host-only. It returns `false` for guests and viewers.
|
|
421
|
-
|
|
422
318
|
### Role helpers
|
|
423
319
|
|
|
424
320
|
```ts
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
321
|
+
sdk.multiplayer.getRole(); // 'host' | 'player' | 'viewer' | null
|
|
322
|
+
sdk.multiplayer.isHost();
|
|
323
|
+
sdk.multiplayer.isViewer();
|
|
324
|
+
sdk.multiplayer.selfId();
|
|
325
|
+
sdk.multiplayer.getRoom();
|
|
326
|
+
sdk.multiplayer.getPeers();
|
|
327
|
+
sdk.multiplayer.isConnected();
|
|
328
|
+
sdk.multiplayer.leaveRoom();
|
|
433
329
|
```
|
|
434
330
|
|
|
435
331
|
### Events
|
|
436
332
|
|
|
437
|
-
Subscribe with `
|
|
333
|
+
Subscribe with `sdk.multiplayer.on(eventName, handler)`. Use `'*'` to observe all events.
|
|
438
334
|
|
|
439
335
|
| Event | Payload | Meaning |
|
|
440
336
|
| -------------- | ------------------------------------------------- | --------------------------- |
|
|
@@ -459,16 +355,6 @@ mp.on('round_resolved', ({ payload, fromPeerId, seq }) => {
|
|
|
459
355
|
|
|
460
356
|
The SDK drops late or duplicate game-event frames using the server-provided `seq` per sending peer.
|
|
461
357
|
|
|
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
358
|
## Public API reference
|
|
473
359
|
|
|
474
360
|
Core:
|
|
@@ -509,6 +395,34 @@ Multiplayer:
|
|
|
509
395
|
- `multiplayer.isViewer()`
|
|
510
396
|
- `multiplayer.selfId()`
|
|
511
397
|
|
|
398
|
+
### Type imports
|
|
399
|
+
|
|
400
|
+
Every public type is exported as a named ESM type from the package root. Import what you need:
|
|
401
|
+
|
|
402
|
+
```ts
|
|
403
|
+
import type {
|
|
404
|
+
Sdk,
|
|
405
|
+
BootstrapData,
|
|
406
|
+
BootstrapRoom,
|
|
407
|
+
MatchContext,
|
|
408
|
+
MatchParticipant,
|
|
409
|
+
MatchResultPayload,
|
|
410
|
+
RoomInfo,
|
|
411
|
+
Peer,
|
|
412
|
+
RoomOptions,
|
|
413
|
+
JoinOptions,
|
|
414
|
+
CreateRoomResult,
|
|
415
|
+
JoinRoomResult,
|
|
416
|
+
ScoreMetadata,
|
|
417
|
+
Question,
|
|
418
|
+
QuestionOptions,
|
|
419
|
+
StartSessionResult,
|
|
420
|
+
SubmitScoreResult,
|
|
421
|
+
MpMessage,
|
|
422
|
+
MpEventHandler,
|
|
423
|
+
} from '@ksaa-nlp/gamehub-sdk';
|
|
424
|
+
```
|
|
425
|
+
|
|
512
426
|
## Pause and resume
|
|
513
427
|
|
|
514
428
|
If the host pauses/resumes the iframe, define these optional handlers:
|
|
@@ -527,7 +441,27 @@ window.onGameHubResume = function () {
|
|
|
527
441
|
|
|
528
442
|
### `window.GameHubSDK` is undefined
|
|
529
443
|
|
|
530
|
-
|
|
444
|
+
You're on the `<script>`-tag path but the bundle didn't load. Either:
|
|
445
|
+
|
|
446
|
+
- The script tag is missing from your HTML — re-add `<script src="/sdk/gamehub-sdk.js">` (or the local-testing CDN URL).
|
|
447
|
+
- You're inside a bundled app and never imported the SDK — import `@ksaa-nlp/gamehub-sdk` and call `createSdk()` instead. The npm path doesn't attach `window.GameHubSDK`; that's only for the script-tag flow.
|
|
448
|
+
|
|
449
|
+
### `import sdk from '@ksaa-nlp/gamehub-sdk'` fails or returns `undefined`
|
|
450
|
+
|
|
451
|
+
The default-import singleton was removed in v0.2.0. Use the named factory:
|
|
452
|
+
|
|
453
|
+
```ts
|
|
454
|
+
import { createSdk } from '@ksaa-nlp/gamehub-sdk';
|
|
455
|
+
const sdk = createSdk();
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
### `ReferenceError: window is not defined` at import time
|
|
459
|
+
|
|
460
|
+
You're on a version older than v0.3.0. Upgrade to v0.4.0 or later — importing the package no longer touches `window` / `document`. If you still see this on a current version, something is importing the script-tag entry (`@ksaa-nlp/gamehub-sdk/browser` or `dist/gamehub-sdk.js`) on the server. Use the default `.` entry instead.
|
|
461
|
+
|
|
462
|
+
### `Cannot use import statement outside a module` / CJS `require()` fails
|
|
463
|
+
|
|
464
|
+
The package is ESM-only. Set `"type": "module"` in your `package.json`, or use a dynamic `import()` from CJS code.
|
|
531
465
|
|
|
532
466
|
### Score returns `skipped`
|
|
533
467
|
|
|
@@ -544,3 +478,8 @@ No published questions matched the filters, options were invalid, or the API req
|
|
|
544
478
|
### Guest state differs from host
|
|
545
479
|
|
|
546
480
|
Move all random generation and final state transitions to the host. Guests should send intent only and wait for `broadcast`ed resolved events.
|
|
481
|
+
|
|
482
|
+
## Versions
|
|
483
|
+
|
|
484
|
+
- [CHANGELOG.md](./CHANGELOG.md) — release notes for every version.
|
|
485
|
+
- [MIGRATION.md](./MIGRATION.md) — upgrading from v0.1 / v0.2.x / v0.3.x.
|