@ksaa-nlp/gamehub-sdk 3.6.0 → 3.8.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/SPEC.md +148 -53
- package/dist/gamehub-sdk.js +192 -75
- package/dist/gamehub-sdk.js.map +3 -3
- package/dist/src/api-client.d.ts +6 -5
- package/dist/src/api-client.d.ts.map +1 -1
- package/dist/src/api-client.js +9 -8
- package/dist/src/api-client.js.map +1 -1
- package/dist/src/bridge.d.ts +10 -6
- package/dist/src/bridge.d.ts.map +1 -1
- package/dist/src/bridge.js +70 -56
- package/dist/src/bridge.js.map +1 -1
- package/dist/src/browser-entry.d.ts.map +1 -1
- package/dist/src/browser-entry.js +6 -5
- package/dist/src/browser-entry.js.map +1 -1
- package/dist/src/constants.d.ts +8 -1
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +8 -1
- package/dist/src/constants.js.map +1 -1
- package/dist/src/init.d.ts.map +1 -1
- package/dist/src/init.js +14 -9
- package/dist/src/init.js.map +1 -1
- package/dist/src/main.d.ts +16 -2
- package/dist/src/main.d.ts.map +1 -1
- package/dist/src/main.js +16 -0
- package/dist/src/main.js.map +1 -1
- package/dist/src/session.d.ts +7 -1
- package/dist/src/session.d.ts.map +1 -1
- package/dist/src/session.js +53 -1
- package/dist/src/session.js.map +1 -1
- package/dist/src/transport.d.ts +47 -0
- package/dist/src/transport.d.ts.map +1 -0
- package/dist/src/transport.js +102 -0
- package/dist/src/transport.js.map +1 -0
- package/dist/types/session.d.ts +42 -0
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/word.d.ts +147 -0
- package/dist/types/word.d.ts.map +1 -0
- package/dist/types/word.js +15 -0
- package/dist/types/word.js.map +1 -0
- package/package.json +1 -1
package/SPEC.md
CHANGED
|
@@ -26,7 +26,8 @@ The SDK is not coupled to any realtime transport. When the host attaches a multi
|
|
|
26
26
|
- Exchange a small set of `postMessage` envelopes with the host page (bootstrap, auth, ready, init, command).
|
|
27
27
|
- Hold a single in-memory session token and supply it to the GameHub REST API when submitting scores.
|
|
28
28
|
- Submit scores (`submitScore`, `submitMatchScore`) and league-match results (`submitMatchResult`).
|
|
29
|
-
- Fetch questions (`getQuestions`).
|
|
29
|
+
- Fetch questions (`getQuestions`) and words (`getWords`).
|
|
30
|
+
- Request the host's report dialog for a question (`reportQuestion`) or word (`reportWord`).
|
|
30
31
|
- Surface a bootstrap snapshot (`getBootstrap()`) including the opaque `multiplayer` block.
|
|
31
32
|
- Forward player-visible host commands (`pause`, `resume`) to optional `window.onGameHubPause` / `onGameHubResume` callbacks.
|
|
32
33
|
- Type definitions for authoring UGC server bundles (`./server` subpath, types-only at runtime).
|
|
@@ -49,23 +50,25 @@ The SDK is not coupled to any realtime transport. When the host attaches a multi
|
|
|
49
50
|
|
|
50
51
|
The package is laid out flat under `src/` and `types/`. No subdirectories.
|
|
51
52
|
|
|
52
|
-
| File
|
|
53
|
-
|
|
|
54
|
-
| `src/main.ts`
|
|
55
|
-
| `src/browser-entry.ts`
|
|
56
|
-
| `src/init.ts`
|
|
57
|
-
| `src/
|
|
58
|
-
| `src/
|
|
59
|
-
| `src/
|
|
60
|
-
| `src/
|
|
61
|
-
| `src/
|
|
62
|
-
| `src/
|
|
63
|
-
| `src/
|
|
64
|
-
| `
|
|
65
|
-
| `types/
|
|
66
|
-
| `types/
|
|
67
|
-
| `types/
|
|
68
|
-
| `
|
|
53
|
+
| File | Responsibility |
|
|
54
|
+
| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
55
|
+
| `src/main.ts` | Public entry. Defines the `Sdk` interface and `createSdk()` factory. Pure on import. |
|
|
56
|
+
| `src/browser-entry.ts` | The only file with side effects on import. Used by the IIFE bundle to attach `window.GameHubSDK`. |
|
|
57
|
+
| `src/init.ts` | `detectEnvironment`, `autoInit`, `ensureInitialized`, `installHostMessageListener`. |
|
|
58
|
+
| `src/transport.ts` | Host transport abstraction: `HostTransport` interface, `PostMessageTransport` (iframe ⇄ parent) and `NativeBridgeTransport` (`window.GameHubNativeHost`), plus `getTransport()` / `hasHost()` detection. See §6.7. |
|
|
59
|
+
| `src/bridge.ts` | Host protocol: builds the `GAMEHUB_SDK_*` envelopes, correlates request/response by `messageId`, send helper. Transport-independent — routes through `getTransport()`. |
|
|
60
|
+
| `src/session.ts` | `startSession`, `submitScore`, `submitMatchScore`, `submitMatchResult`, `getQuestions`, `getWords`. |
|
|
61
|
+
| `src/match-context.ts` | `fetchMatchContext` (HTTP `/leagues/<id>/matches/<id>`). |
|
|
62
|
+
| `src/api-client.ts` | `apiRequest`: thin `fetch` wrapper that prefixes the API base and attaches the Bearer token. |
|
|
63
|
+
| `src/state.ts` | `SdkState` interface + the singleton `state` object. All mutable state lives here. |
|
|
64
|
+
| `src/constants.ts` | `SDK_VERSION`, `MESSAGE_PREFIX`, `PARENT_REQUEST_TIMEOUT_MS`, `QUESTIONS_LIMIT_CAP`, `WORDS_LIMIT_CAP`. |
|
|
65
|
+
| `src/utils.ts` | `isBrowser`, `warn`, `error`, `unwrapResult`, `readQueryParam`. |
|
|
66
|
+
| `types/api.ts` | `ApiResponse`, `ApiRequestOptions`. |
|
|
67
|
+
| `types/bootstrap.ts` | `BootstrapMultiplayer`, `BootstrapConfig`, `BootstrapData`. |
|
|
68
|
+
| `types/session.ts` | `Question`, `QuestionOptions`, `ScoreMetadata`, `EventData`, `GameCompleteData`, `StartSessionResult`, `SubmitScoreResult`, `WordOptions`, `ReportWordData`, `ReportableWord` (re-exports `Word`). |
|
|
69
|
+
| `types/word.ts` | `Word` + sub-interfaces (`WordSense`, `WordForm`, `Lookup`, `Synset`, `FormRepresentation`, `DynamicProp`, …) — Words Bank feature. |
|
|
70
|
+
| `types/match.ts` | `MatchParticipantMember`, `MatchParticipant`, `MatchContext`, `MatchResultPayload`. |
|
|
71
|
+
| `src/server-entry.ts` | `./server` subpath. Types-only at runtime: `GuestModule`, `Ctx`, `PeerInfo`, `BroadcastOptions`, `EndMatchPayload`, `CreateOptions`. No imports from the browser SDK; safe for tooling that compiles the bundle on Node. |
|
|
69
72
|
| `src/server-dev-harness.ts` | `./server/dev` subpath. `runLocal(guest, opts)` returns a `LocalRoom` that simulates the colyseus host (`join`, `send`, `leave`, `advance`, etc.) and records every bridge call to `outbox`. Pure Node — does not import `isolated-vm` or anything browser. |
|
|
70
73
|
|
|
71
74
|
### 3.2 Singleton model
|
|
@@ -151,37 +154,46 @@ Slug must match `[a-z0-9-]+` (case-insensitive).
|
|
|
151
154
|
|
|
152
155
|
## 6. Bootstrap protocol
|
|
153
156
|
|
|
154
|
-
The SDK communicates with the host
|
|
157
|
+
The SDK communicates with the host through namespaced envelopes. All envelope types are `MESSAGE_PREFIX + '_' + name`, where `MESSAGE_PREFIX = 'GAMEHUB_SDK'`. The envelope shapes and the `messageId` correlation are **transport-independent**; the wire underneath is chosen by `getTransport()` (`src/transport.ts`):
|
|
158
|
+
|
|
159
|
+
- **`postmessage`** — the game runs in an `<iframe>` and exchanges envelopes with its parent window via `window.postMessage` (the classic embedding).
|
|
160
|
+
- **`native`** — the game is the top-level document in a native WebView (e.g. a Flutter app) and exchanges envelopes through an injected `window.GameHubNativeHost` object. See §6.7.
|
|
161
|
+
- **none** — standalone (dev harness, or served from the GameHub origin). `getTransport()` returns `null` and `hasHost()` is `false`; host round-trips resolve `null` and `apiRequest` falls back to a direct `fetch`.
|
|
162
|
+
|
|
163
|
+
The full taxonomy follows. It applies to **both** transports identically.
|
|
155
164
|
|
|
156
165
|
### 6.1 Message-type taxonomy
|
|
157
166
|
|
|
158
|
-
| Direction | Type (after prefix) | Purpose
|
|
159
|
-
| ---------- | ------------------- |
|
|
160
|
-
| SDK → host | `READY` | Fired once on script load when
|
|
161
|
-
| SDK → host | `BOOTSTRAP_REQUEST` | Asks the host for bootstrap config. Carries `{ messageId }`.
|
|
162
|
-
| host → SDK | `BOOTSTRAP` | Bootstrap config response. Carries `{ payload: BootstrapConfig, messageId? }`. Host may also push an unsolicited one any time.
|
|
163
|
-
| SDK → host | `AUTH_REQUEST` | Asks the host for an auth token. Carries `{ messageId }`.
|
|
164
|
-
| host → SDK | `AUTH_RESPONSE` | Auth response. Carries `{ token: string \| null, leagueId?, gameId?, matchId?, messageId }`.
|
|
165
|
-
| host → SDK | `SET_AUTH` | Unsolicited auth update. Carries `{ token }`. Triggers session start if not already running.
|
|
166
|
-
| host → SDK | `COMMAND` | Player-visible host command. Carries `{ command: 'pause' \| 'resume' }`. Routed to optional `window.onGameHubPause/Resume`.
|
|
167
|
-
| SDK → host | `INIT` | Fired after auto-init resolves. Carries `{ slug, auto: true }`.
|
|
168
|
-
| SDK → host | `SESSION_STARTED` | Fired when a session token is obtained. Carries `{ token }`.
|
|
169
|
-
| SDK → host | `SCORE_SUBMITTED` | Fired when a score is accepted. Carries `{ score, accepted, timeTaken }`.
|
|
170
|
-
| SDK → host | `GAME_COMPLETE` | Fired by `sdk.notifyComplete(data?)`. Carries `data` verbatim.
|
|
171
|
-
| SDK → host | `REQUEST_CLOSE` | Fired by `sdk.requestClose()`. Empty payload.
|
|
172
|
-
| SDK → host | `LOG_EVENT` | Fired by `sdk.logEvent(name, data?)`. Carries `{ event, data }`.
|
|
167
|
+
| Direction | Type (after prefix) | Purpose |
|
|
168
|
+
| ---------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
169
|
+
| SDK → host | `READY` | Fired once on script load when a host transport is present (`hasHost()`). Carries `{ version }`. |
|
|
170
|
+
| SDK → host | `BOOTSTRAP_REQUEST` | Asks the host for bootstrap config. Carries `{ messageId }`. |
|
|
171
|
+
| host → SDK | `BOOTSTRAP` | Bootstrap config response. Carries `{ payload: BootstrapConfig, messageId? }`. Host may also push an unsolicited one any time. |
|
|
172
|
+
| SDK → host | `AUTH_REQUEST` | Asks the host for an auth token. Carries `{ messageId }`. |
|
|
173
|
+
| host → SDK | `AUTH_RESPONSE` | Auth response. Carries `{ token: string \| null, leagueId?, gameId?, matchId?, messageId }`. |
|
|
174
|
+
| host → SDK | `SET_AUTH` | Unsolicited auth update. Carries `{ token }`. Triggers session start if not already running. |
|
|
175
|
+
| host → SDK | `COMMAND` | Player-visible host command. Carries `{ command: 'pause' \| 'resume' }`. Routed to optional `window.onGameHubPause/Resume`. |
|
|
176
|
+
| SDK → host | `INIT` | Fired after auto-init resolves. Carries `{ slug, auto: true }`. |
|
|
177
|
+
| SDK → host | `SESSION_STARTED` | Fired when a session token is obtained. Carries `{ token }`. |
|
|
178
|
+
| SDK → host | `SCORE_SUBMITTED` | Fired when a score is accepted. Carries `{ score, accepted, timeTaken }`. |
|
|
179
|
+
| SDK → host | `GAME_COMPLETE` | Fired by `sdk.notifyComplete(data?)`. Carries `data` verbatim. |
|
|
180
|
+
| SDK → host | `REQUEST_CLOSE` | Fired by `sdk.requestClose()`. Empty payload. |
|
|
181
|
+
| SDK → host | `LOG_EVENT` | Fired by `sdk.logEvent(name, data?)`. Carries `{ event, data }`. |
|
|
173
182
|
| SDK → host | `QUESTION_REPORT` | Fired by `sdk.reportQuestion(data)`. Carries `{ questionId?, prompt?, message?, questions? }`. With `questions` the host renders a question-picker dropdown; otherwise it reports the single `questionId`. |
|
|
174
|
-
| SDK → host | `
|
|
175
|
-
|
|
|
183
|
+
| SDK → host | `WORD_REPORT` | Fired by `sdk.reportWord(data)` (Words Bank feature). Carries `{ wordId?, lemma?, message?, words? }`. Same picker/single semantics as `QUESTION_REPORT`, relabeled to lemma. |
|
|
184
|
+
| SDK → host | `API_REQUEST` | Asks the host to proxy a backend HTTP call. Carries `{ messageId, endpoint, method, body }`. See §6.6. |
|
|
185
|
+
| host → SDK | `API_RESPONSE` | Result of a proxied call. Carries `{ messageId, ok: boolean, result?: ApiResponse, error?: string }`. See §6.6. |
|
|
176
186
|
|
|
177
187
|
Every SDK → host envelope also includes the resolved `gameSlug` as a top-level field (except `API_REQUEST`, whose payload is the proxied request).
|
|
178
188
|
|
|
179
189
|
### 6.2 Origin handling
|
|
180
190
|
|
|
181
|
-
The SDK fires the initial `BOOTSTRAP_REQUEST` with target origin `'*'` because it has not yet learned the parent's origin. The very first
|
|
191
|
+
Origin handling is a property of the **postMessage** transport only. The SDK fires the initial `BOOTSTRAP_REQUEST` with target origin `'*'` because it has not yet learned the parent's origin. The very first inbound reply records `event.origin` into `state.parentOrigin`. All subsequent SDK → host messages target that recorded origin. Calls before any inbound message fall back to `'*'`.
|
|
182
192
|
|
|
183
193
|
If `event.origin` changes mid-session (e.g. the host navigates between origins), `state.parentOrigin` is overwritten by the next inbound message. There is no origin allowlist; the SDK trusts whatever the parent reports. This is acceptable because the iframe contents are themselves served by GameHub.
|
|
184
194
|
|
|
195
|
+
On the **native** transport there is no web origin: inbound `HostMessage.origin` is `null`, `state.parentOrigin` stays `null`, and the transport ignores it when sending (it posts through `window.GameHubNativeHost`). The trust boundary is the native app itself, which owns the WebView and injects the bridge.
|
|
196
|
+
|
|
185
197
|
### 6.3 Request/response correlation
|
|
186
198
|
|
|
187
199
|
- Each SDK-initiated request includes a `messageId: string` (currently `Date.now().toString()`).
|
|
@@ -199,7 +211,7 @@ If `event.origin` changes mid-session (e.g. the host navigates between origins),
|
|
|
199
211
|
- Non-string and non-null → ignored (field left as-is).
|
|
200
212
|
- `multiplayer`:
|
|
201
213
|
- Explicit `null` → `null`.
|
|
202
|
-
- Object with `
|
|
214
|
+
- Object with a `reservation` object (string `name`, `sessionId`, `roomId` — the `SdkSeatReservation` shape) and a string `wsEndpoint` → only `wsEndpoint` is trimmed; the `reservation` object is stored as-is.
|
|
203
215
|
- Any other shape → ignored (field left as-is).
|
|
204
216
|
|
|
205
217
|
This lets the host clear fields explicitly (via `null`) without overwriting unset fields.
|
|
@@ -220,8 +232,8 @@ The SDK never issues a cross-origin request to the GameHub backend. When a game
|
|
|
220
232
|
|
|
221
233
|
`apiRequest(endpoint, options)` (`src/api-client.ts`) chooses its transport by context:
|
|
222
234
|
|
|
223
|
-
- **
|
|
224
|
-
- **Standalone** (dev harness, or a game served from the GameHub origin): the
|
|
235
|
+
- **Hosted** (`hasHost()` — iframe parent or native bridge): delegates to `requestApiFromParent(endpoint, options)` (`src/bridge.ts`). This sends `API_REQUEST` `{ messageId, endpoint, method, body }` to the host and awaits the matching `API_RESPONSE`. On the native transport the host is the native app, which performs the real HTTPS call and keeps the bearer token out of the WebView entirely.
|
|
236
|
+
- **Standalone** (dev harness, or a game served from the GameHub origin): the direct `fetch` path against `state.apiBase`, attaching `Authorization: Bearer <token>` (§7.2). This path is same-origin by construction, so CORS does not apply.
|
|
225
237
|
|
|
226
238
|
Protocol details:
|
|
227
239
|
|
|
@@ -232,6 +244,31 @@ Protocol details:
|
|
|
232
244
|
- Timeout is `HOST_API_TIMEOUT_MS` (15 000 ms), larger than the 2 s bootstrap/auth timeout because the round-trip includes the host's real network call. On timeout the promise rejects and the call degrades.
|
|
233
245
|
- `messageId` is `'api_' + Date.now() + '_' + <seq>`, where `<seq>` is a monotonic counter so concurrent proxied calls (e.g. session start + questions) never collide.
|
|
234
246
|
|
|
247
|
+
### 6.7 Native bridge transport
|
|
248
|
+
|
|
249
|
+
When a game is rendered in a native WebView rather than an iframe, the native app plays the host role. Because the game is the **top-level document** (`window.parent === window`), the postMessage transport is unavailable; the SDK instead talks to an injected `window.GameHubNativeHost` object.
|
|
250
|
+
|
|
251
|
+
**Detection.** `getTransport()` selects the native transport when `window.GameHubNativeHost.postMessage` is a function. This is checked **before** the iframe check, so a native host always wins. The native app must inject the object **before** the SDK script runs (e.g. a WebView user-script evaluated at document start), otherwise the SDK falls through to standalone.
|
|
252
|
+
|
|
253
|
+
**Contract.** The object is bidirectional:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
interface GameHubNativeHost {
|
|
257
|
+
// game → host. The SDK calls this with a JSON-stringified envelope.
|
|
258
|
+
postMessage(json: string): void;
|
|
259
|
+
// host → game. The SDK installs this; the native app invokes it (via the
|
|
260
|
+
// WebView's "evaluate JavaScript" channel) with one envelope, either as a
|
|
261
|
+
// JSON string or an already-parsed object.
|
|
262
|
+
receive?(envelope: string | object): void;
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
- The native app implements `postMessage` (typically forwarding to its WebView message channel — e.g. `flutter_inappwebview`'s `callHandler`).
|
|
267
|
+
- The SDK installs `receive` when the transport is constructed. The native app calls `window.GameHubNativeHost.receive(env)` to deliver every host → SDK envelope (`BOOTSTRAP`, `AUTH_RESPONSE`, `API_RESPONSE`, `SET_AUTH`, `COMMAND`).
|
|
268
|
+
- Envelope shapes, `messageId` correlation, and timeouts (§6.3) are identical to the postMessage transport. The native host must therefore echo the `messageId` on `BOOTSTRAP`/`AUTH_RESPONSE`/`API_RESPONSE` exactly as an iframe host does.
|
|
269
|
+
- The native host is responsible for the same protocol an iframe host implements: replying to `BOOTSTRAP_REQUEST`/`AUTH_REQUEST`, proxying `API_REQUEST` (performing the real HTTPS call natively — see §6.6), and reacting to one-way signals (`REQUEST_CLOSE`, `QUESTION_REPORT`, `WORD_REPORT`, `SCORE_SUBMITTED`, `GAME_COMPLETE`, `LOG_EVENT`).
|
|
270
|
+
- Multiplayer is unchanged: the host passes `bootstrap.multiplayer` `{ reservation, wsEndpoint }`, and the game opens its own Colyseus WebSocket from inside the WebView (§19.1). The SDK does not proxy realtime traffic over the bridge.
|
|
271
|
+
|
|
235
272
|
## 7. Authentication
|
|
236
273
|
|
|
237
274
|
### 7.1 Token sources
|
|
@@ -246,7 +283,7 @@ The SDK never reads the token from storage and never writes it. Each page load s
|
|
|
246
283
|
|
|
247
284
|
### 7.2 Sending the token
|
|
248
285
|
|
|
249
|
-
On the **standalone** transport, when `state.authToken` is non-null, `apiRequest` attaches `Authorization: Bearer <token>` to every outbound HTTP request. On the **host-proxied** transport (§6.6, the
|
|
286
|
+
On the **standalone** transport, when `state.authToken` is non-null, `apiRequest` attaches `Authorization: Bearer <token>` to every outbound HTTP request. On the **host-proxied** transport (§6.6, the hosted default — iframe or native) the host attaches the bearer header from its own auth context instead. A native host can therefore keep the token entirely outside the WebView. Either way the SDK does not parse the token, refresh it, or check its expiry. A 401 from the server is a normal failed response; the SDK surfaces the error and does not retry.
|
|
250
287
|
|
|
251
288
|
### 7.3 Token rotation
|
|
252
289
|
|
|
@@ -388,6 +425,43 @@ interface Question {
|
|
|
388
425
|
}
|
|
389
426
|
```
|
|
390
427
|
|
|
428
|
+
## 11bis. Words (Words Bank feature)
|
|
429
|
+
|
|
430
|
+
### 11bis.1 `getWords(options?)`
|
|
431
|
+
|
|
432
|
+
Mirrors `getQuestions`. Awaits `ensureInitialized()`; identifier is
|
|
433
|
+
`state.gameId || state.gameSlug` (returns `[]` if both null). Validates
|
|
434
|
+
`options.difficulty` and `options.limit` the same way (returns `[]` on
|
|
435
|
+
mismatch). GETs
|
|
436
|
+
`/games/<identifier>/words?limit=<n>&random=true[&gameId=…][&difficulty=…][&category=…][&tags=a,b][&profileId=…]`.
|
|
437
|
+
|
|
438
|
+
- `options.profileId` selects a game **profile** whose `wordPack` drives the
|
|
439
|
+
selection (the same profile that carries the question `pack`). Omitted →
|
|
440
|
+
bootstrap `profileId` → the game's `isDefault` profile.
|
|
441
|
+
- `options.tags` (a `string[]`) is sent comma-joined and ANDed (`$in`) on top of
|
|
442
|
+
the profile selection.
|
|
443
|
+
- `limit` is clamped to `WORDS_LIMIT_CAP` (**50**, lower than questions' 100
|
|
444
|
+
because a full-nested `Word` is heavier) and defaults to 50.
|
|
445
|
+
- Response shapes accepted: `Word[]` directly, `{ words: Word[] }`, else `[]`.
|
|
446
|
+
- Never throws — all failure modes resolve `[]`. No normalization (the runtime
|
|
447
|
+
route is a near-passthrough).
|
|
448
|
+
|
|
449
|
+
### 11bis.2 `Word` shape
|
|
450
|
+
|
|
451
|
+
The **full nested `PublicEntry` shape** (`lemma`, `senses[]`, `wordForms[]`,
|
|
452
|
+
`lemmaLanguage`, `relatedSenses[]`, …) plus exposed gamehub fields
|
|
453
|
+
`difficulty`/`category`/`tags`, with `_id` renamed to `id`. The embedded
|
|
454
|
+
reference objects (`Lookup`/`Synset`/`DynamicProperty`) reflect the
|
|
455
|
+
**audit-stripped** shape the server persists (no `_id`/`createdAt`/`isReviewed`/…).
|
|
456
|
+
Full field tables in `types/word.ts` and `specs/words-bank-feature.md` §3.
|
|
457
|
+
|
|
458
|
+
### 11bis.3 `reportWord(data)`
|
|
459
|
+
|
|
460
|
+
Mirrors `reportQuestion`: fires the `WORD_REPORT` postMessage with
|
|
461
|
+
`{ wordId?, lemma?, message?, words? }` for the host to render its
|
|
462
|
+
`ReportWordDialog`. No-ops outside the host iframe (the SDK never calls the
|
|
463
|
+
report endpoint itself). See §6.1.
|
|
464
|
+
|
|
391
465
|
## 12. Host command channel
|
|
392
466
|
|
|
393
467
|
`COMMAND` messages from the host dispatch to optional global handlers:
|
|
@@ -500,16 +574,17 @@ interface SdkState {
|
|
|
500
574
|
|
|
501
575
|
### 14.6 Host signalling
|
|
502
576
|
|
|
503
|
-
| Method | Returns | Postmessage emitted (after prefix)
|
|
504
|
-
| --------------------------------- | ------- |
|
|
505
|
-
| `notifyComplete(data?)` | `void` | `GAME_COMPLETE`
|
|
506
|
-
| `requestClose()` | `void` | `REQUEST_CLOSE`
|
|
507
|
-
| `logEvent(eventName, eventData?)` | `void` | `LOG_EVENT { event: eventName, data: eventData }`
|
|
577
|
+
| Method | Returns | Postmessage emitted (after prefix) |
|
|
578
|
+
| --------------------------------- | ------- | ---------------------------------------------------------------- |
|
|
579
|
+
| `notifyComplete(data?)` | `void` | `GAME_COMPLETE` |
|
|
580
|
+
| `requestClose()` | `void` | `REQUEST_CLOSE` |
|
|
581
|
+
| `logEvent(eventName, eventData?)` | `void` | `LOG_EVENT { event: eventName, data: eventData }` |
|
|
508
582
|
| `reportQuestion(data)` | `void` | `QUESTION_REPORT { questionId?, prompt?, message?, questions? }` |
|
|
509
583
|
|
|
510
584
|
`reportQuestion(data: ReportQuestionData)` asks the host to open its "report this question" dialog. The host owns the dialog (the SDK renders no DOM — see §19.3) and submits the report, collecting the reason, an optional message, and — for guests — name/email. Like the other host signals it no-ops outside an iframe.
|
|
511
585
|
|
|
512
586
|
Two modes:
|
|
587
|
+
|
|
513
588
|
- **Single question** — pass `questionId` (and optionally `prompt`). The dialog reports that one question.
|
|
514
589
|
- **Pick from a list** — pass `questions: ReportableQuestion[]` (each `{ questionId, prompt? }`). The host renders a dropdown so the player chooses which question to report; `questionId`, when also supplied, pre-selects an entry. The `questions` array is forwarded verbatim (each entry normalised to `{ questionId, prompt }`) on the `QUESTION_REPORT` envelope.
|
|
515
590
|
|
|
@@ -528,9 +603,18 @@ All types listed below are re-exported from the package root and form the public
|
|
|
528
603
|
### 15.1 Bootstrap
|
|
529
604
|
|
|
530
605
|
```ts
|
|
606
|
+
interface SdkSeatReservation {
|
|
607
|
+
name: string;
|
|
608
|
+
sessionId: string;
|
|
609
|
+
roomId: string;
|
|
610
|
+
processId?: string;
|
|
611
|
+
publicAddress?: string;
|
|
612
|
+
reconnectionToken?: string;
|
|
613
|
+
devMode?: boolean;
|
|
614
|
+
}
|
|
615
|
+
|
|
531
616
|
interface BootstrapMultiplayer {
|
|
532
|
-
|
|
533
|
-
reconnectionToken: string;
|
|
617
|
+
reservation: SdkSeatReservation;
|
|
534
618
|
wsEndpoint: string;
|
|
535
619
|
}
|
|
536
620
|
|
|
@@ -729,7 +813,7 @@ These rules exist because of a real prior regression where transport-related cle
|
|
|
729
813
|
### 19.3 No DOM injection
|
|
730
814
|
|
|
731
815
|
- The SDK must not create, modify, or query DOM elements outside reading `window.location` and the `<script>` tag list during environment detection.
|
|
732
|
-
-
|
|
816
|
+
- On the postMessage transport, the SDK must not register global event listeners outside the `message` listeners its transport installs (the ambient one from `installHostMessageListener()` plus the short-lived per-request handlers in `bridge.ts`, each removed on settle/timeout). On the native transport it touches only `window.GameHubNativeHost` (reading `postMessage`, installing `receive`) — no DOM and no `window` event listeners.
|
|
733
817
|
|
|
734
818
|
### 19.4 No storage
|
|
735
819
|
|
|
@@ -754,7 +838,7 @@ These rules exist because of a real prior regression where transport-related cle
|
|
|
754
838
|
|
|
755
839
|
### 20.3 Bundle budget
|
|
756
840
|
|
|
757
|
-
The IIFE bundle (`dist/gamehub-sdk.js`) is expected to remain under **
|
|
841
|
+
The IIFE bundle (`dist/gamehub-sdk.js`) is expected to remain under **10 KB gzip**, the figure that matters on the wire. The raw budget was historically **25 KB**; the native-bridge transport added in 3.8.0 (a deliberate new feature area — §6.7) pushed raw to ~26 KB. Current size: ~26 KB raw / ~5.9 KB gzip. Further growth should be a deliberate decision tied to a release note, and gzip remains the hard ceiling.
|
|
758
842
|
|
|
759
843
|
### 20.4 No runtime dependencies
|
|
760
844
|
|
|
@@ -781,6 +865,13 @@ The SDK does not currently ship a test suite, but every change should be verifia
|
|
|
781
865
|
- Replying with `AUTH_RESPONSE { token }` makes `isAuthenticated()` true.
|
|
782
866
|
- Posting `SET_AUTH { token }` after init opens a session and fires `SESSION_STARTED`.
|
|
783
867
|
|
|
868
|
+
### 21.3b Native bridge boot
|
|
869
|
+
|
|
870
|
+
- With `window.GameHubNativeHost = { postMessage(json) { … } }` injected before the SDK loads (and `window.parent === window`), `getTransport().kind === 'native'` and `hasHost()` is `true`.
|
|
871
|
+
- The SDK calls `GameHubNativeHost.postMessage` with a JSON-stringified `BOOTSTRAP_REQUEST` on boot. Calling `window.GameHubNativeHost.receive(JSON.stringify({ type: 'GAMEHUB_SDK_BOOTSTRAP', messageId, payload: { gameSlug } }))` (echoing the requested `messageId`) flips `getBootstrap().isBootstrapped` to `true`.
|
|
872
|
+
- `receive` accepts both a JSON string and an already-parsed object; malformed strings are ignored without throwing.
|
|
873
|
+
- `apiRequest` travels as an `API_REQUEST` over `postMessage`, and the native host's `API_RESPONSE` (delivered via `receive`) resolves/rejects it identically to the iframe path.
|
|
874
|
+
|
|
784
875
|
### 21.4 Score submission
|
|
785
876
|
|
|
786
877
|
- `submitScore(42, { level: 3 })` causes a POST to `/api/sessions/submit` with `{ token, score: 42, meta: { level: 3 } }` and Bearer auth. When embedded (the default), this travels as an `API_REQUEST` `{ endpoint: '/sessions/submit', method: 'POST', body: { … } }` and the **host** performs the POST with its own bearer auth (§6.6); standalone, the SDK POSTs directly.
|
|
@@ -816,8 +907,12 @@ The `./server` and `./server/dev` subpaths are separate from the browser SDK. Im
|
|
|
816
907
|
import type { GuestModule, Ctx, PeerInfo } from '@ksaa-nlp/gamehub-sdk/server';
|
|
817
908
|
|
|
818
909
|
const guest: GuestModule = {
|
|
819
|
-
onCreate(ctx, opts) {
|
|
820
|
-
|
|
910
|
+
onCreate(ctx, opts) {
|
|
911
|
+
ctx.state.round = 0;
|
|
912
|
+
},
|
|
913
|
+
onJoin(ctx, peer) {
|
|
914
|
+
ctx.broadcast('peer:joined', { peerId: peer.peerId });
|
|
915
|
+
},
|
|
821
916
|
onMessage(ctx, peer, type, payload) {
|
|
822
917
|
if (type === 'answer') ctx.send(peer.peerId, 'ack', { ok: true });
|
|
823
918
|
},
|
|
@@ -863,7 +958,7 @@ expect(room.outbox).toContainEqual({
|
|
|
863
958
|
- **Bootstrap.** The set of context the host gives the iframe at load: slug, IDs, auth, multiplayer block, display strings.
|
|
864
959
|
- **Session.** A server-issued token representing a single playthrough. Used to authenticate `submitScore`.
|
|
865
960
|
- **League match.** A scheduled game between two participants tracked by GameHub's league system. Has its own session endpoint that allows guest submitters.
|
|
866
|
-
- **Multiplayer block.** The opaque `{
|
|
961
|
+
- **Multiplayer block.** The opaque `{ reservation, wsEndpoint }` pair the host provides for the game to open its own realtime connection, where `reservation` is the Colyseus seat-reservation (`SdkSeatReservation`).
|
|
867
962
|
- **Host.** The GameHub-served page that frames the game in an iframe. Owns auth, matchmaking, and the bootstrap pipeline.
|
|
868
963
|
- **Game.** The iframe-hosted application that consumes this SDK.
|
|
869
964
|
- **Standalone.** A game loaded outside an iframe. `isInIframe()` returns false; the bootstrap pipeline is skipped.
|