@ksaa-nlp/gamehub-sdk 0.9.0 → 3.6.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 +158 -373
- package/SPEC.md +869 -0
- package/dist/gamehub-sdk.js +168 -1191
- package/dist/gamehub-sdk.js.map +3 -3
- package/dist/src/api-client.d.ts +15 -5
- package/dist/src/api-client.d.ts.map +1 -1
- package/dist/src/api-client.js +21 -5
- package/dist/src/api-client.js.map +1 -1
- package/dist/src/bridge.d.ts +31 -14
- package/dist/src/bridge.d.ts.map +1 -1
- package/dist/src/bridge.js +148 -159
- package/dist/src/bridge.js.map +1 -1
- package/dist/src/constants.d.ts +8 -6
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +8 -6
- package/dist/src/constants.js.map +1 -1
- package/dist/src/init.d.ts +18 -12
- package/dist/src/init.d.ts.map +1 -1
- package/dist/src/init.js +23 -156
- package/dist/src/init.js.map +1 -1
- package/dist/src/main.d.ts +45 -85
- package/dist/src/main.d.ts.map +1 -1
- package/dist/src/main.js +33 -52
- package/dist/src/main.js.map +1 -1
- package/dist/src/match-context.d.ts.map +1 -1
- package/dist/src/match-context.js +0 -14
- package/dist/src/match-context.js.map +1 -1
- package/dist/src/server-dev-harness.d.ts +85 -0
- package/dist/src/server-dev-harness.d.ts.map +1 -0
- package/dist/src/server-dev-harness.js +233 -0
- package/dist/src/server-dev-harness.js.map +1 -0
- package/dist/src/server-entry.d.ts +127 -0
- package/dist/src/server-entry.d.ts.map +1 -0
- package/dist/src/server-entry.js +26 -0
- package/dist/src/server-entry.js.map +1 -0
- package/dist/src/session.d.ts +16 -9
- package/dist/src/session.d.ts.map +1 -1
- package/dist/src/session.js +38 -27
- package/dist/src/session.js.map +1 -1
- package/dist/src/state.d.ts +17 -55
- package/dist/src/state.d.ts.map +1 -1
- package/dist/src/state.js +13 -32
- package/dist/src/state.js.map +1 -1
- package/dist/src/utils.d.ts +3 -14
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +3 -31
- package/dist/src/utils.js.map +1 -1
- package/dist/types/bootstrap.d.ts +56 -36
- package/dist/types/bootstrap.d.ts.map +1 -1
- package/dist/types/question.d.ts +36 -0
- package/dist/types/question.d.ts.map +1 -0
- package/dist/types/question.js +21 -0
- package/dist/types/question.js.map +1 -0
- package/dist/types/session.d.ts +46 -20
- package/dist/types/session.d.ts.map +1 -1
- package/package.json +14 -4
- package/CHANGELOG.md +0 -545
- package/MIGRATION.md +0 -322
- package/dist/src/multiplayer-match.d.ts +0 -42
- package/dist/src/multiplayer-match.d.ts.map +0 -1
- package/dist/src/multiplayer-match.js +0 -551
- package/dist/src/multiplayer-match.js.map +0 -1
- package/dist/src/multiplayer.d.ts +0 -96
- package/dist/src/multiplayer.d.ts.map +0 -1
- package/dist/src/multiplayer.js +0 -536
- package/dist/src/multiplayer.js.map +0 -1
- package/dist/types/multiplayer-match.d.ts +0 -204
- package/dist/types/multiplayer-match.d.ts.map +0 -1
- package/dist/types/multiplayer-match.js +0 -15
- package/dist/types/multiplayer-match.js.map +0 -1
- package/dist/types/multiplayer.d.ts +0 -194
- package/dist/types/multiplayer.d.ts.map +0 -1
- package/dist/types/multiplayer.js +0 -2
- package/dist/types/multiplayer.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,485 +1,270 @@
|
|
|
1
|
-
# GameHub SDK
|
|
1
|
+
# GameHub SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The client library GameHub-hosted games use to talk to the platform: auth, sessions, score submission, questions, league matches, and the bootstrap pipeline that hands games a multiplayer reconnection token.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Transport-agnostic — no `@colyseus/sdk` dependency. Games that need realtime open their own socket.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
For exhaustive contract details see [SPEC.md](./SPEC.md). The README below is a hands-on tour.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
## Install
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
12
|
npm install @ksaa-nlp/gamehub-sdk
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
import { createSdk, type Sdk } from '@ksaa-nlp/gamehub-sdk';
|
|
17
|
-
|
|
18
|
-
const sdk: Sdk = createSdk();
|
|
15
|
+
Or drop the script in directly (auto-attaches `window.GameHubSDK`):
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
```html
|
|
18
|
+
<script src="/sdk/gamehub-sdk.js"></script>
|
|
22
19
|
```
|
|
23
20
|
|
|
24
|
-
|
|
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:
|
|
21
|
+
## 60-second tutorial
|
|
31
22
|
|
|
32
23
|
```ts
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
You don't need `isPlatformBrowser` / `typeof window` gates around the import or around `createSdk()`.
|
|
43
|
-
|
|
44
|
-
### GameHub ZIP uploads
|
|
45
|
-
|
|
46
|
-
When you upload a ZIP game, GameHub injects the SDK into the entry HTML automatically:
|
|
47
|
-
|
|
48
|
-
```html
|
|
49
|
-
<script src="/sdk/gamehub-sdk.js" data-game-slug="your-game-slug"></script>
|
|
50
|
-
```
|
|
24
|
+
import { createSdk } from '@ksaa-nlp/gamehub-sdk';
|
|
51
25
|
|
|
52
|
-
|
|
26
|
+
const sdk = createSdk();
|
|
27
|
+
await sdk.ready(); // resolves once the host bootstrap arrives
|
|
53
28
|
|
|
54
|
-
|
|
29
|
+
console.log('Playing:', sdk.getGameSlug());
|
|
30
|
+
console.log('User auth:', sdk.isAuthenticated());
|
|
55
31
|
|
|
56
|
-
|
|
32
|
+
// ... run the game ...
|
|
57
33
|
|
|
58
|
-
|
|
59
|
-
|
|
34
|
+
await sdk.submitScore(1234, { level: 3, duration: 42 });
|
|
35
|
+
sdk.notifyComplete({ score: 1234 });
|
|
60
36
|
```
|
|
61
37
|
|
|
62
|
-
|
|
38
|
+
That's the whole minimum game: `createSdk` → `ready` → `submitScore`. Everything else is opt-in.
|
|
63
39
|
|
|
64
|
-
##
|
|
40
|
+
## Boot lifecycle
|
|
65
41
|
|
|
66
|
-
|
|
42
|
+
The SDK auto-initialises against the host page on first await. You can drive it manually too:
|
|
67
43
|
|
|
68
44
|
```ts
|
|
69
|
-
import { createSdk } from '@ksaa-nlp/gamehub-sdk';
|
|
70
|
-
|
|
71
45
|
const sdk = createSdk();
|
|
72
|
-
await sdk.ready();
|
|
73
|
-
|
|
74
|
-
const bootstrap = sdk.getBootstrap();
|
|
75
|
-
console.log({
|
|
76
|
-
version: sdk.version,
|
|
77
|
-
gameSlug: bootstrap.gameSlug,
|
|
78
|
-
leagueId: bootstrap.leagueId,
|
|
79
|
-
matchId: bootstrap.matchId,
|
|
80
|
-
peerId: bootstrap.peerId,
|
|
81
|
-
room: bootstrap.room,
|
|
82
|
-
});
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
For `<script>`-tag games:
|
|
86
46
|
|
|
87
|
-
|
|
88
|
-
await
|
|
89
|
-
|
|
47
|
+
sdk.isInitialized(); // false — bootstrap hasn't arrived yet
|
|
48
|
+
await sdk.ready(); // does the postMessage handshake with the host
|
|
49
|
+
sdk.isInitialized(); // true
|
|
50
|
+
sdk.hasActiveSession(); // true if auth or league-match context is present
|
|
90
51
|
```
|
|
91
52
|
|
|
92
|
-
|
|
53
|
+
`ready()` is idempotent; you can call it from multiple places without worrying about races.
|
|
93
54
|
|
|
94
|
-
|
|
55
|
+
## Bootstrap snapshot
|
|
56
|
+
|
|
57
|
+
`getBootstrap()` returns everything the host told us about this game launch:
|
|
95
58
|
|
|
96
59
|
```ts
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
isBootstrapped: boolean;
|
|
112
|
-
}
|
|
60
|
+
const bs = sdk.getBootstrap();
|
|
61
|
+
|
|
62
|
+
bs.gameSlug; // 'my-quiz' — required, populated by host or URL
|
|
63
|
+
bs.authToken; // Bearer token; null for guests
|
|
64
|
+
bs.leagueId; // string | null — set inside a league context
|
|
65
|
+
bs.gameId; // string | null — set inside a league context
|
|
66
|
+
bs.matchId; // string | null — set inside a league match
|
|
67
|
+
bs.mode; // 'two_players_online' | 'two_players_local' | …
|
|
68
|
+
bs.playerName; // free-form display name
|
|
69
|
+
bs.playerCount; // free-form, e.g. '4'
|
|
70
|
+
bs.teamOneName; // free-form
|
|
71
|
+
bs.teamTwoName; // free-form
|
|
72
|
+
bs.multiplayer; // { roomCode, reconnectionToken, wsEndpoint } | null
|
|
73
|
+
bs.isBootstrapped; // true after the host has replied at least once
|
|
113
74
|
```
|
|
114
75
|
|
|
115
|
-
|
|
76
|
+
All fields default to `null`. Don't assume `gameSlug` exists before `ready()` resolves.
|
|
116
77
|
|
|
117
|
-
##
|
|
78
|
+
## Submitting scores
|
|
118
79
|
|
|
119
|
-
|
|
80
|
+
### Standalone (single-player)
|
|
120
81
|
|
|
121
82
|
```ts
|
|
122
83
|
const result = await sdk.submitScore(score, {
|
|
123
|
-
mode: 'single',
|
|
124
|
-
correctAnswers: 8,
|
|
125
|
-
totalQuestions: 10,
|
|
126
|
-
durationMs: 92000,
|
|
127
84
|
level: 3,
|
|
85
|
+
duration: 42,
|
|
86
|
+
custom: { combo: 7 },
|
|
128
87
|
});
|
|
129
88
|
|
|
130
|
-
if (result.
|
|
131
|
-
|
|
132
|
-
} else
|
|
89
|
+
if (result.success) {
|
|
90
|
+
console.log('accepted:', result.accepted, 'timeTaken:', result.timeTaken);
|
|
91
|
+
} else {
|
|
133
92
|
console.error(result.error);
|
|
134
93
|
}
|
|
135
94
|
```
|
|
136
95
|
|
|
137
|
-
`submitScore`
|
|
138
|
-
|
|
139
|
-
```ts
|
|
140
|
-
{
|
|
141
|
-
success: boolean;
|
|
142
|
-
accepted?: boolean;
|
|
143
|
-
timeTaken?: number;
|
|
144
|
-
error?: string;
|
|
145
|
-
skipped?: boolean;
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
Inside a multiplayer room, only the host can write scores. Guests and viewers receive `{ success: true, skipped: true }` without a network write.
|
|
96
|
+
`submitScore` opens a session automatically on first call. If the user isn't authenticated and there's no league-match context, it returns `{ success: false, error: '...' }` — it never throws.
|
|
150
97
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
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.
|
|
154
|
-
|
|
155
|
-
Fetch participant names and team members:
|
|
98
|
+
### League match (one-score scoring)
|
|
156
99
|
|
|
157
100
|
```ts
|
|
158
|
-
await sdk.
|
|
159
|
-
|
|
160
|
-
const match = await sdk.getMatchContext();
|
|
161
|
-
if (match) {
|
|
162
|
-
renderSideA(match.participant1.name, match.participant1.members);
|
|
163
|
-
renderSideB(match.participant2.name, match.participant2.members);
|
|
164
|
-
}
|
|
101
|
+
await sdk.submitMatchScore(score, { level: 3 });
|
|
165
102
|
```
|
|
166
103
|
|
|
167
|
-
`
|
|
104
|
+
Same shape as `submitScore`, but tags the submission with the current `matchId`/`leagueId`/`gameId` from bootstrap. Fails fast if `matchId` is missing.
|
|
168
105
|
|
|
169
|
-
|
|
170
|
-
{
|
|
171
|
-
id: string;
|
|
172
|
-
leagueId: string;
|
|
173
|
-
gameId: string;
|
|
174
|
-
participant1: {
|
|
175
|
-
participantId: string;
|
|
176
|
-
name?: string;
|
|
177
|
-
members?: Array<{ leaguePlayerId: string; fullname?: string; avatar?: string }>;
|
|
178
|
-
};
|
|
179
|
-
participant2: {
|
|
180
|
-
participantId: string;
|
|
181
|
-
name?: string;
|
|
182
|
-
members?: Array<{ leaguePlayerId: string; fullname?: string; avatar?: string }>;
|
|
183
|
-
};
|
|
184
|
-
status: string;
|
|
185
|
-
}
|
|
186
|
-
```
|
|
106
|
+
### League match (winner + per-participant scoring)
|
|
187
107
|
|
|
188
|
-
|
|
108
|
+
For matches whose leaderboard is derived from a result object (winner, side scores, reason):
|
|
189
109
|
|
|
190
110
|
```ts
|
|
111
|
+
const ctx = await sdk.getMatchContext();
|
|
112
|
+
if (!ctx) return; // not in a league match
|
|
113
|
+
|
|
191
114
|
await sdk.submitMatchResult({
|
|
192
|
-
winnerId:
|
|
115
|
+
winnerId: ctx.participant1.participantId,
|
|
193
116
|
scores: {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
},
|
|
197
|
-
reason: 'normal_finish',
|
|
198
|
-
details: {
|
|
199
|
-
durationMs: 94000,
|
|
117
|
+
player1Score: 5,
|
|
118
|
+
player2Score: 3,
|
|
200
119
|
},
|
|
120
|
+
reason: 'connection',
|
|
201
121
|
});
|
|
202
122
|
```
|
|
203
123
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
```ts
|
|
207
|
-
await sdk.submitMatchScore(1200, {
|
|
208
|
-
correctAnswers: 10,
|
|
209
|
-
totalQuestions: 12,
|
|
210
|
-
});
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
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`.
|
|
124
|
+
`submitMatchResult` posts a zero placeholder score with the payload in `meta.matchResult`; the server derives leaderboard entries from it.
|
|
214
125
|
|
|
215
126
|
## Questions
|
|
216
127
|
|
|
217
|
-
|
|
128
|
+
For quiz-style games:
|
|
218
129
|
|
|
219
130
|
```ts
|
|
220
131
|
const questions = await sdk.getQuestions({
|
|
221
132
|
difficulty: 'medium',
|
|
222
|
-
category: '
|
|
223
|
-
limit:
|
|
133
|
+
category: 'capitals',
|
|
134
|
+
limit: 25,
|
|
224
135
|
});
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
The SDK uses `gameId` when provided by the host. Otherwise it falls back to the game slug.
|
|
228
|
-
|
|
229
|
-
Question shape:
|
|
230
136
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
difficulty?: 'easy' | 'medium' | 'hard' | string;
|
|
235
|
-
category?: string;
|
|
236
|
-
prompt: string;
|
|
237
|
-
choices: string[];
|
|
238
|
-
answer: string | number;
|
|
239
|
-
explanation?: string;
|
|
240
|
-
points: number;
|
|
241
|
-
}
|
|
137
|
+
questions.forEach((q) => {
|
|
138
|
+
console.log(q.prompt, q.choices, '→', q.answer);
|
|
139
|
+
});
|
|
242
140
|
```
|
|
243
141
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
## Multiplayer model
|
|
142
|
+
`limit` is clamped to 100. Any failure (bad inputs, network error, malformed response) resolves to `[]` — there's no `error` field to check.
|
|
247
143
|
|
|
248
|
-
|
|
144
|
+
## Host signalling
|
|
249
145
|
|
|
250
|
-
|
|
251
|
-
| -------------------------------- | --------------------------------------------- |
|
|
252
|
-
| Random grids, seeds, decks | Host |
|
|
253
|
-
| Turn order and round advancement | Host |
|
|
254
|
-
| Click/move validation | Host |
|
|
255
|
-
| Player input | Local player sends intent |
|
|
256
|
-
| Final score and match result | Host only |
|
|
257
|
-
| Timer | Host broadcasts or sends an absolute deadline |
|
|
258
|
-
|
|
259
|
-
Guests send intent with `send`. The host validates and publishes resolved state with `broadcast`.
|
|
260
|
-
|
|
261
|
-
### Create and join
|
|
146
|
+
Tell the host page things:
|
|
262
147
|
|
|
263
148
|
```ts
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
});
|
|
149
|
+
sdk.notifyComplete({ score: 1234, status: 'win' }); // game finished
|
|
150
|
+
sdk.requestClose(); // return to lobby
|
|
151
|
+
sdk.logEvent('used_powerup', { kind: 'extra_time' }); // analytics breadcrumb
|
|
268
152
|
|
|
269
|
-
|
|
270
|
-
|
|
153
|
+
// Let the player flag the current question — the host opens a report dialog
|
|
154
|
+
sdk.reportQuestion({ questionId: q.id, prompt: q.prompt });
|
|
271
155
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
displayName: 'Player 2',
|
|
156
|
+
// …or let the player pick which question to report from a dropdown
|
|
157
|
+
sdk.reportQuestion({
|
|
158
|
+
questions: quiz.map((q) => ({ questionId: q.id, prompt: q.prompt })),
|
|
276
159
|
});
|
|
277
|
-
|
|
278
|
-
await sdk.multiplayer.joinRoomByLink(window.location.href);
|
|
279
|
-
|
|
280
|
-
// Read-only guest:
|
|
281
|
-
await sdk.multiplayer.joinRoom('AB12CD', { asViewer: true });
|
|
282
160
|
```
|
|
283
161
|
|
|
284
|
-
|
|
162
|
+
All of these are fire-and-forget — they just `postMessage` to the parent window. `reportQuestion` asks the host to open its report dialog (the host collects the reason + optional message, plus name/email for guests, and submits it). Pass `questions` instead of a single `questionId` to have the host render a dropdown so the player chooses which question to report.
|
|
285
163
|
|
|
286
|
-
|
|
164
|
+
## Pause/resume from the host
|
|
287
165
|
|
|
288
|
-
|
|
289
|
-
| ------------------------------------------------------------ | ---------------------------------------------------------------------- |
|
|
290
|
-
| `?mp=<shareId>` on the page URL | QR scan or shared link — SDK auto-calls `joinRoomByLink`. |
|
|
291
|
-
| `?roomCode=<CODE>&playerName=<name>` | Host UI forwarded this after `join-token` — SDK auto-calls `joinRoom`. |
|
|
292
|
-
| `GAMEHUB_SDK_BOOTSTRAP` postMessage (`shareId` / `roomCode`) | Host page pushed them into the iframe. |
|
|
166
|
+
The host can pause/resume the iframe by posting a command. Define globals to receive them:
|
|
293
167
|
|
|
294
|
-
|
|
168
|
+
```ts
|
|
169
|
+
window.onGameHubPause = () => pauseGameLoop();
|
|
170
|
+
window.onGameHubResume = () => resumeGameLoop();
|
|
171
|
+
```
|
|
295
172
|
|
|
296
|
-
|
|
173
|
+
Both are optional. The SDK doesn't pause anything itself; it just delivers the signal.
|
|
297
174
|
|
|
298
|
-
|
|
175
|
+
## Multiplayer
|
|
299
176
|
|
|
300
|
-
|
|
301
|
-
const mp = sdk.multiplayer;
|
|
177
|
+
The SDK does not open a WebSocket. The host has already created a Colyseus room before the iframe boots and passes the reconnection token in the bootstrap. Open it yourself:
|
|
302
178
|
|
|
303
|
-
|
|
304
|
-
|
|
179
|
+
```ts
|
|
180
|
+
import { Client } from '@colyseus/sdk';
|
|
181
|
+
import { UgcState } from '@ksaa-nlp/colyseus-schemas/ugc';
|
|
182
|
+
import { attachServerClock } from '@ksaa-nlp/colyseus-schemas/clock';
|
|
183
|
+
|
|
184
|
+
const { multiplayer } = sdk.getBootstrap();
|
|
185
|
+
if (!multiplayer) {
|
|
186
|
+
startSoloMode();
|
|
187
|
+
return;
|
|
305
188
|
}
|
|
306
189
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
const result = resolveMove(fromPeerId, payload.cellId);
|
|
310
|
-
mp.broadcast('cell_resolved', result);
|
|
311
|
-
});
|
|
190
|
+
const client = new Client(multiplayer.wsEndpoint);
|
|
191
|
+
const room = await client.reconnect<UgcState>(multiplayer.reconnectionToken);
|
|
312
192
|
|
|
313
|
-
|
|
314
|
-
applyResolvedMove(payload);
|
|
315
|
-
});
|
|
316
|
-
```
|
|
193
|
+
const serverNow = attachServerClock(room);
|
|
317
194
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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();
|
|
195
|
+
room.onStateChange((state) => render(state));
|
|
196
|
+
room.onMessage('fx:answer', (msg) => playSound(msg));
|
|
197
|
+
room.send('answer:submit', { answer: 'B' });
|
|
329
198
|
```
|
|
330
199
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
Subscribe with `sdk.multiplayer.on(eventName, handler)`. Use `'*'` to observe all events.
|
|
200
|
+
The returned `room` is a stock `@colyseus/sdk` `Room` — see <https://docs.colyseus.io/client/room/> for its full API. Install `@colyseus/sdk`, `@colyseus/schema`, and `@ksaa-nlp/colyseus-schemas` in your game; the SDK doesn't pull them in.
|
|
334
201
|
|
|
335
|
-
|
|
336
|
-
| -------------- | ------------------------------------------------- | --------------------------- |
|
|
337
|
-
| `connected` | `{ roomCode, peerId }` | WebSocket opened |
|
|
338
|
-
| `disconnected` | `{ roomCode }` | WebSocket closed |
|
|
339
|
-
| `reconnecting` | `{ attempts, delayMs }` | SDK is reconnecting |
|
|
340
|
-
| `room_joined` | `{ peerId, role, peers }` | Local peer joined |
|
|
341
|
-
| `peer_joined` | `{ peerId, role, ... }` | Another peer joined |
|
|
342
|
-
| `peer_left` | `{ peerId }` | A peer left |
|
|
343
|
-
| `room_closed` | `{ reason }` | Host/server closed the room |
|
|
344
|
-
| `room_left` | `{ reason }` | Local peer left |
|
|
345
|
-
| `error` | `{ code }` | Server rejected a frame |
|
|
346
|
-
| `message` | `{ eventName, payload, fromPeerId, seq, sentAt }` | Any custom game event |
|
|
347
|
-
|
|
348
|
-
Custom game events are also emitted by their own name:
|
|
202
|
+
## API summary
|
|
349
203
|
|
|
350
204
|
```ts
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
205
|
+
const sdk = createSdk();
|
|
206
|
+
|
|
207
|
+
// Lifecycle
|
|
208
|
+
sdk.version; // '3.1.0'
|
|
209
|
+
sdk.ready(); // Promise<boolean>
|
|
210
|
+
sdk.isInitialized(); // boolean
|
|
211
|
+
sdk.isAuthenticated(); // boolean
|
|
212
|
+
sdk.hasActiveSession(); // boolean
|
|
213
|
+
sdk.startSession(); // Promise<StartSessionResult>
|
|
214
|
+
|
|
215
|
+
// Scores
|
|
216
|
+
sdk.submitScore(score, meta?); // Promise<SubmitScoreResult>
|
|
217
|
+
sdk.submitMatchScore(score, meta?); // Promise<SubmitScoreResult>
|
|
218
|
+
sdk.submitMatchResult({ winnerId, scores }); // Promise<SubmitScoreResult>
|
|
219
|
+
|
|
220
|
+
// League match
|
|
221
|
+
sdk.getMatchContext(); // Promise<MatchContext | null>
|
|
222
|
+
|
|
223
|
+
// Questions
|
|
224
|
+
sdk.getQuestions({ difficulty, category, limit }); // Promise<Question[]>
|
|
225
|
+
|
|
226
|
+
// Host signalling
|
|
227
|
+
sdk.notifyComplete(data?); // void
|
|
228
|
+
sdk.requestClose(); // void
|
|
229
|
+
sdk.logEvent(name, data?); // void
|
|
230
|
+
sdk.reportQuestion({ questionId?, prompt?, message?, questions? }); // void
|
|
231
|
+
|
|
232
|
+
// Context
|
|
233
|
+
sdk.getGameSlug(); // string | null
|
|
234
|
+
sdk.isInIframe(); // boolean
|
|
235
|
+
sdk.getBootstrap(); // BootstrapData
|
|
354
236
|
```
|
|
355
237
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
Core:
|
|
361
|
-
|
|
362
|
-
- `version: string`
|
|
363
|
-
- `ready(): Promise<boolean>`
|
|
364
|
-
- `isInitialized(): boolean`
|
|
365
|
-
- `isAuthenticated(): boolean`
|
|
366
|
-
- `hasActiveSession(): boolean`
|
|
367
|
-
- `startSession(): Promise<StartSessionResult>`
|
|
368
|
-
- `submitScore(score, meta?): Promise<SubmitScoreResult>`
|
|
369
|
-
- `submitMatchScore(score, meta?): Promise<SubmitScoreResult>`
|
|
370
|
-
- `submitMatchResult(result?): Promise<SubmitScoreResult>`
|
|
371
|
-
- `playAgain(): Promise<{ success: boolean; token: string | null }>`
|
|
372
|
-
- `notifyComplete(data?): void`
|
|
373
|
-
- `requestClose(): void`
|
|
374
|
-
- `logEvent(eventName, eventData?): void`
|
|
375
|
-
- `getGameSlug(): string | null`
|
|
376
|
-
- `isInIframe(): boolean`
|
|
377
|
-
- `getBootstrap(): BootstrapData`
|
|
378
|
-
- `getMatchContext(): Promise<MatchContext | null>`
|
|
379
|
-
- `getQuestions(options?): Promise<Question[]>`
|
|
380
|
-
|
|
381
|
-
Multiplayer:
|
|
382
|
-
|
|
383
|
-
- `multiplayer.createRoom(options?)`
|
|
384
|
-
- `multiplayer.joinRoom(roomCode, options?)`
|
|
385
|
-
- `multiplayer.joinRoomByLink(link, options?)`
|
|
386
|
-
- `multiplayer.leaveRoom(reason?)`
|
|
387
|
-
- `multiplayer.send(eventName, payload?)`
|
|
388
|
-
- `multiplayer.broadcast(eventName, payload?)`
|
|
389
|
-
- `multiplayer.on(eventName, handler)`
|
|
390
|
-
- `multiplayer.getRoom()`
|
|
391
|
-
- `multiplayer.getPeers()`
|
|
392
|
-
- `multiplayer.isConnected()`
|
|
393
|
-
- `multiplayer.getRole()`
|
|
394
|
-
- `multiplayer.isHost()`
|
|
395
|
-
- `multiplayer.isViewer()`
|
|
396
|
-
- `multiplayer.selfId()`
|
|
397
|
-
|
|
398
|
-
### Type imports
|
|
399
|
-
|
|
400
|
-
Every public type is exported as a named ESM type from the package root. Import what you need:
|
|
238
|
+
## TypeScript types
|
|
239
|
+
|
|
240
|
+
Every public type is re-exported from the package root:
|
|
401
241
|
|
|
402
242
|
```ts
|
|
403
243
|
import type {
|
|
404
244
|
Sdk,
|
|
405
245
|
BootstrapData,
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
MatchParticipant,
|
|
409
|
-
MatchResultPayload,
|
|
410
|
-
RoomInfo,
|
|
411
|
-
Peer,
|
|
412
|
-
RoomOptions,
|
|
413
|
-
JoinOptions,
|
|
414
|
-
CreateRoomResult,
|
|
415
|
-
JoinRoomResult,
|
|
246
|
+
BootstrapConfig,
|
|
247
|
+
BootstrapMultiplayer,
|
|
416
248
|
ScoreMetadata,
|
|
249
|
+
SubmitScoreResult,
|
|
250
|
+
StartSessionResult,
|
|
417
251
|
Question,
|
|
418
252
|
QuestionOptions,
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
253
|
+
GameCompleteData,
|
|
254
|
+
EventData,
|
|
255
|
+
MatchContext,
|
|
256
|
+
MatchParticipant,
|
|
257
|
+
MatchParticipantMember,
|
|
258
|
+
MatchResultPayload,
|
|
259
|
+
ApiResponse,
|
|
260
|
+
ApiRequestOptions,
|
|
423
261
|
} from '@ksaa-nlp/gamehub-sdk';
|
|
424
262
|
```
|
|
425
263
|
|
|
426
|
-
##
|
|
427
|
-
|
|
428
|
-
If the host pauses/resumes the iframe, define these optional handlers:
|
|
429
|
-
|
|
430
|
-
```js
|
|
431
|
-
window.onGameHubPause = function () {
|
|
432
|
-
pauseGame();
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
window.onGameHubResume = function () {
|
|
436
|
-
resumeGame();
|
|
437
|
-
};
|
|
438
|
-
```
|
|
439
|
-
|
|
440
|
-
## Common issues
|
|
441
|
-
|
|
442
|
-
### `window.GameHubSDK` is undefined
|
|
443
|
-
|
|
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.
|
|
465
|
-
|
|
466
|
-
### Score returns `skipped`
|
|
467
|
-
|
|
468
|
-
The current peer is not the multiplayer host, or it joined as a viewer. This is expected and prevents duplicate writes.
|
|
469
|
-
|
|
470
|
-
### Match context is `null`
|
|
471
|
-
|
|
472
|
-
The game was not launched with `leagueId` and `matchId`, or the match endpoint could not resolve the match. Fall back to normal game UI.
|
|
473
|
-
|
|
474
|
-
### Questions return `[]`
|
|
475
|
-
|
|
476
|
-
No published questions matched the filters, options were invalid, or the API request failed. Treat this as the empty/degraded state.
|
|
477
|
-
|
|
478
|
-
### Guest state differs from host
|
|
264
|
+
## Specification
|
|
479
265
|
|
|
480
|
-
|
|
266
|
+
[SPEC.md](./SPEC.md) is the authoritative contract — postMessage taxonomy, state model, REST endpoints, error semantics, boundary rules, versioning policy. Read that before changing SDK internals or adding a new feature area.
|
|
481
267
|
|
|
482
|
-
##
|
|
268
|
+
## License
|
|
483
269
|
|
|
484
|
-
|
|
485
|
-
- [MIGRATION.md](./MIGRATION.md) — upgrading from v0.1 / v0.2.x / v0.3.x.
|
|
270
|
+
ISC.
|