@ksaa-nlp/gamehub-sdk 0.6.0 → 0.7.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 +95 -0
- package/dist/gamehub-sdk.js +45 -14
- package/dist/gamehub-sdk.js.map +2 -2
- package/dist/src/constants.d.ts +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/main.d.ts +10 -1
- package/dist/src/main.d.ts.map +1 -1
- package/dist/src/main.js +1 -0
- package/dist/src/main.js.map +1 -1
- package/dist/src/multiplayer-match.d.ts +4 -2
- package/dist/src/multiplayer-match.d.ts.map +1 -1
- package/dist/src/multiplayer-match.js +41 -15
- package/dist/src/multiplayer-match.js.map +1 -1
- package/dist/src/multiplayer.d.ts +14 -0
- package/dist/src/multiplayer.d.ts.map +1 -1
- package/dist/src/multiplayer.js +34 -0
- package/dist/src/multiplayer.js.map +1 -1
- package/dist/src/state.d.ts +12 -0
- package/dist/src/state.d.ts.map +1 -1
- package/dist/src/state.js +2 -0
- package/dist/src/state.js.map +1 -1
- package/dist/types/multiplayer-match.d.ts +38 -6
- package/dist/types/multiplayer-match.d.ts.map +1 -1
- package/dist/types/multiplayer.d.ts +21 -2
- package/dist/types/multiplayer.d.ts.map +1 -1
- package/package.json +2 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,100 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.7.0 — 2026-04-30
|
|
4
|
+
|
|
5
|
+
Three Tier-1 transport features completing the multiplayer roadmap: targeted send, per-peer seq continuity across reconnect, and a server-clock helper. All three are additive — existing `multiplayer.send` / `broadcast` / `match` calls keep their current semantics.
|
|
6
|
+
|
|
7
|
+
### Targeted send (`SendOptions.to`)
|
|
8
|
+
|
|
9
|
+
`multiplayer.send(name, payload, { to: peerId | peerId[] })` and the matching `MatchBroadcastOptions.to` route a frame to a specific subset of peers in the room instead of fanning out. Use cases: a private host→guest hint, per-player power-up effects that shouldn't leak to a spectator, host correcting one guest's view.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
sdk.multiplayer.send('private_hint', payload, { to: guestPeerId });
|
|
13
|
+
ctx.broadcast('reveal_for_winner', payload, { to: [winnerId] });
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Server-side semantics:
|
|
17
|
+
- The sender does NOT receive their own frame as an echo unless their id is explicitly in the `to` list.
|
|
18
|
+
- Unknown peerIds are silently skipped (no error frame).
|
|
19
|
+
- `to` and `retain` are mutually exclusive — a targeted frame is per-definition not a room-wide snapshot, so the server drops `retain` when `to` is set. The match-API mirrors this client-side and also skips the local `apply` step on targeted broadcasts (targeting means "send state to specific peers", not "this is the new authoritative state").
|
|
20
|
+
|
|
21
|
+
### Per-peer seq continuity across reconnect
|
|
22
|
+
|
|
23
|
+
A peer's outbound `seq` counter now survives a disconnect+reconnect within `RECONNECT_GRACE_MS` (30s). Previously the counter reset to 0 on every new socket because it lived in `wsMeta` (a `WeakMap` keyed by socket); a reconnecting peer's first new message would have `seq=1`, which the SDK's `mpLastSeqByPeer` dedupe at the receiver would compare against the last value seen pre-disconnect (e.g. `seq=12`) and silently drop.
|
|
24
|
+
|
|
25
|
+
Server changes that fix this:
|
|
26
|
+
- The `multiplayer_peers` collection now stores `seq` per peer doc.
|
|
27
|
+
- `storePeer()` (called on every connect) reads the existing seq and seeds `wsMeta.state.seq` from it before any message is processed.
|
|
28
|
+
- The message handler captures `state.seq` into a local immediately after incrementing, then awaits `touchPeer(peerId, seq)` (persists seq + lastSeenAt) BEFORE publishing — so a crash between persist and publish leaves the DB at-least-as-recent as what receivers observed.
|
|
29
|
+
- The `ws.on('close')` handler no longer deletes the peer doc; it sets `lastSeenAt: 0` so `getPeers` immediately filters the peer out, but the `seq` value stays around for a reconnect to pick up.
|
|
30
|
+
|
|
31
|
+
Race fix as part of this: previously, three concurrent message handlers on the same socket could yield on `await touchPeer` interleaved, all read `state.seq` at the end, and emit identical seqs. The seq is now snapshotted into a local before any `await`.
|
|
32
|
+
|
|
33
|
+
### Server clock helper (`multiplayer.serverNow()`)
|
|
34
|
+
|
|
35
|
+
`sdk.multiplayer.serverNow()` returns a server-anchored timestamp in milliseconds. Use it — not `Date.now()` — for any timer math compared across peers (countdowns, deadlines, replay times). Peer device clocks routinely drift seconds apart; without this, two peers running the same `maxTimeSec - (Date.now() - serverStartMs)/1000` countdown can show different remaining times.
|
|
36
|
+
|
|
37
|
+
Implementation: every incoming frame's `sentAt - Date.now()` becomes a sample. The first sample sets the offset wholesale; subsequent samples blend via a 0.85 / 0.15 EMA so a single high-latency packet doesn't yank the clock around. Retained replays are excluded from offset updates (their `sentAt` is when the host originally pinned them, not the current network).
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
// Old: brittle across clock skew
|
|
41
|
+
const remaining = maxTimeSec - (Date.now() - serverStartMs) / 1000;
|
|
42
|
+
|
|
43
|
+
// New: stable
|
|
44
|
+
const remaining = maxTimeSec - (sdk.multiplayer.serverNow() - serverStartMs) / 1000;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Returns `Date.now()` exactly until the first frame arrives, so games can call it during boot without special-casing.
|
|
48
|
+
|
|
49
|
+
### Verified
|
|
50
|
+
|
|
51
|
+
- 23/23 realtime tests green (14 unit + 9 integration). New tests cover: targeted single-peer delivery, targeted multi-peer delivery, targeted-strips-retain, seq continuity across reconnect, peer-doc preserved on disconnect.
|
|
52
|
+
- SDK type-check + build clean.
|
|
53
|
+
|
|
54
|
+
### Wire format
|
|
55
|
+
|
|
56
|
+
Strictly additive on the wire. Inbound (client → server):
|
|
57
|
+
- `to?: string | string[]` — targeted recipients.
|
|
58
|
+
|
|
59
|
+
Server → client: unchanged. The server-stamped `sentAt` was already on every frame; v0.7 just consumes it on the SDK side.
|
|
60
|
+
|
|
61
|
+
### Unchanged
|
|
62
|
+
|
|
63
|
+
- `Sdk` interface beyond the new `multiplayer.serverNow()` method and the new `to` field on `SendOptions` / `MatchBroadcastOptions` (both optional).
|
|
64
|
+
- `BootstrapData`, `MpMessage`.
|
|
65
|
+
- All other multiplayer methods.
|
|
66
|
+
- The match API — adding retain/seq features here doesn't touch its semantics.
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## 0.6.1 — 2026-04-30
|
|
71
|
+
|
|
72
|
+
Three small, additive fixes that surfaced while porting link-game to the `multiplayer.match` API.
|
|
73
|
+
|
|
74
|
+
- **`MatchCtx.retained: boolean`.** Added a `retained` flag on the context passed into `apply` and `onStart` handlers. It's `true` only when the handler is running for a server-replayed retained frame (a peer reconnecting / joining late catching up on past state) and `false` for live broadcasts and host-initiated calls. Game code uses this to suppress one-shot side effects — sounds, confetti, win animations, draw dialogs on a delay — that would otherwise re-fire for state the peer didn't see live. Also: retained replays no longer trigger a host's `hostHandle` (the host already authored that frame; re-validating against its own snapshot is at best wasteful, at worst incorrect).
|
|
75
|
+
|
|
76
|
+
- **`Peer` identity fields.** The wire payload from the realtime server (`displayName`, `avatar`, `userId`, `guestId`) was already populated from JWT claims, but the public `Peer` type only exposed `peerId`/`role`/`metadata`. Adding the rest as optional fields lets games access player-name and avatar info without an `as any` cast. Optional because anonymous / passcode-joined peers may not carry all four.
|
|
77
|
+
|
|
78
|
+
- **`MatchEventMap` constraint relaxed to a self-mapped form.** `match<TMap, TStart>(...)` previously required `TMap extends Record<string, MatchScopedPayload>`, which TypeScript silently rejects for an `interface LinkMatchMap { ... }` declaration because interfaces with concrete keys don't have an index signature. Switched the constraint everywhere (the `match()` factory, `MatchOptions`, the `Sdk['multiplayer'].match` signature) to the self-mapped pattern `<TMap extends { [K in keyof TMap]: MatchScopedPayload }>`, which accepts both interface-shaped and type-alias-shaped event maps while still enforcing per-key payload safety. The `MatchEventMap` alias is retained as documentation; its docstring now explains why the actual generics use the self-mapped form.
|
|
79
|
+
|
|
80
|
+
### Why these are 0.6.1 rather than 0.6.0
|
|
81
|
+
|
|
82
|
+
These shipped together with the original `multiplayer.match` API (0.6.0) but emerged only when a real game tried to consume it. Bumping a patch version makes the relationship explicit: 0.6.0 is the API as designed; 0.6.1 is the API as a real game actually uses it.
|
|
83
|
+
|
|
84
|
+
### Verified
|
|
85
|
+
|
|
86
|
+
- SDK type-check + build clean (53.3 KB IIFE).
|
|
87
|
+
- link-game (Angular, `@ksaa-nlp/gamehub-sdk@0.6.x` consumer) builds clean against the new types.
|
|
88
|
+
- 18/18 realtime tests still green.
|
|
89
|
+
|
|
90
|
+
### Unchanged
|
|
91
|
+
|
|
92
|
+
- Wire protocol with the realtime server.
|
|
93
|
+
- `Sdk` interface beyond the new `MatchCtx.retained` / `Peer.{userId,guestId,displayName,avatar}` fields and the loosened `match<>` constraint (purely a relaxation, no existing code breaks).
|
|
94
|
+
- `BootstrapData`, `MpMessage`, `multiplayer.send` / `broadcast` / `on` semantics.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
3
98
|
## 0.6.0 — 2026-04-30
|
|
4
99
|
|
|
5
100
|
Adds **`multiplayer.match()`** — a host-authoritative match coordinator that owns the lifecycle every multiplayer game otherwise reinvents: peer-ready handshake, host-elected match start, intent → resolve routing, matchId scoping, and idempotent reapply of broadcasts (live and retained replay alike). Plus two helpers (`assignSeats`, `waitForPeers`) that complete the picture.
|
package/dist/gamehub-sdk.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
(() => {
|
|
3
3
|
// src/constants.ts
|
|
4
|
-
var SDK_VERSION = "0.
|
|
4
|
+
var SDK_VERSION = "0.7.0";
|
|
5
5
|
var MESSAGE_PREFIX = "GAMEHUB_SDK";
|
|
6
6
|
var PARENT_REQUEST_TIMEOUT_MS = 2e3;
|
|
7
7
|
var MP_MAX_RECONNECT_ATTEMPTS = 5;
|
|
@@ -52,7 +52,9 @@
|
|
|
52
52
|
mpPeers: /* @__PURE__ */ new Map(),
|
|
53
53
|
mpHandlers: /* @__PURE__ */ new Map(),
|
|
54
54
|
mpLastSeqByPeer: /* @__PURE__ */ new Map(),
|
|
55
|
-
mpViewerOverride: false
|
|
55
|
+
mpViewerOverride: false,
|
|
56
|
+
mpServerOffsetMs: 0,
|
|
57
|
+
mpServerOffsetSamples: 0
|
|
56
58
|
};
|
|
57
59
|
|
|
58
60
|
// src/utils.ts
|
|
@@ -394,6 +396,15 @@
|
|
|
394
396
|
return;
|
|
395
397
|
}
|
|
396
398
|
if (!message || !message.type) return;
|
|
399
|
+
if (typeof message.sentAt === "number" && message.retained !== true) {
|
|
400
|
+
const sample = message.sentAt - Date.now();
|
|
401
|
+
if (state.mpServerOffsetSamples === 0) {
|
|
402
|
+
state.mpServerOffsetMs = sample;
|
|
403
|
+
} else {
|
|
404
|
+
state.mpServerOffsetMs = 0.85 * state.mpServerOffsetMs + 0.15 * sample;
|
|
405
|
+
}
|
|
406
|
+
state.mpServerOffsetSamples += 1;
|
|
407
|
+
}
|
|
397
408
|
if (message.type === "system") {
|
|
398
409
|
if (message.eventName === "room_joined") {
|
|
399
410
|
state.mpPeers.clear();
|
|
@@ -645,6 +656,9 @@
|
|
|
645
656
|
frame.retain = true;
|
|
646
657
|
if (options.retainKey) frame.retainKey = options.retainKey;
|
|
647
658
|
}
|
|
659
|
+
if (options.to !== void 0) {
|
|
660
|
+
frame.to = options.to;
|
|
661
|
+
}
|
|
648
662
|
socket.send(JSON.stringify(frame));
|
|
649
663
|
return true;
|
|
650
664
|
}
|
|
@@ -690,6 +704,9 @@
|
|
|
690
704
|
const s = state.mpSocket;
|
|
691
705
|
return Boolean(s && s.readyState === WebSocket.OPEN);
|
|
692
706
|
}
|
|
707
|
+
function serverNow() {
|
|
708
|
+
return Date.now() + state.mpServerOffsetMs;
|
|
709
|
+
}
|
|
693
710
|
|
|
694
711
|
// src/match-context.ts
|
|
695
712
|
async function fetchMatchContext() {
|
|
@@ -1154,7 +1171,7 @@
|
|
|
1154
1171
|
function localPeers() {
|
|
1155
1172
|
return getPeers();
|
|
1156
1173
|
}
|
|
1157
|
-
function makeCtx() {
|
|
1174
|
+
function makeCtx(retained = false) {
|
|
1158
1175
|
return {
|
|
1159
1176
|
get matchId() {
|
|
1160
1177
|
return matchId ?? "";
|
|
@@ -1168,6 +1185,7 @@
|
|
|
1168
1185
|
get peers() {
|
|
1169
1186
|
return localPeers();
|
|
1170
1187
|
},
|
|
1188
|
+
retained,
|
|
1171
1189
|
peer(peerId) {
|
|
1172
1190
|
return localPeers().find((p) => p.peerId === peerId) ?? null;
|
|
1173
1191
|
},
|
|
@@ -1191,13 +1209,13 @@
|
|
|
1191
1209
|
}
|
|
1192
1210
|
return `${eventName}:${payload.matchId}:${seq ?? "local"}`;
|
|
1193
1211
|
}
|
|
1194
|
-
function maybeApply(eventName, def, payload, fromPeerId, seq) {
|
|
1212
|
+
function maybeApply(eventName, def, payload, fromPeerId, seq, retained) {
|
|
1195
1213
|
if (!def?.apply) return;
|
|
1196
1214
|
const key = idKeyFor(eventName, def, payload, seq);
|
|
1197
1215
|
if (appliedIdKeys.has(key)) return;
|
|
1198
1216
|
appliedIdKeys.add(key);
|
|
1199
1217
|
try {
|
|
1200
|
-
def.apply(makeCtx(), payload, fromPeerId);
|
|
1218
|
+
def.apply(makeCtx(retained), payload, fromPeerId);
|
|
1201
1219
|
} catch (err) {
|
|
1202
1220
|
error(`multiplayer.match apply('${eventName}') threw:`, err);
|
|
1203
1221
|
}
|
|
@@ -1254,9 +1272,12 @@
|
|
|
1254
1272
|
}
|
|
1255
1273
|
const payload = { ...payloadIn, matchId };
|
|
1256
1274
|
const def = eventDefFor(String(name));
|
|
1257
|
-
const
|
|
1258
|
-
|
|
1259
|
-
|
|
1275
|
+
const targeted = callerOptions.to !== void 0;
|
|
1276
|
+
if (!targeted) {
|
|
1277
|
+
const me = localSelfId() ?? "";
|
|
1278
|
+
maybeApply(String(name), def, payload, me, void 0, false);
|
|
1279
|
+
}
|
|
1280
|
+
const retain = !targeted && (callerOptions.retain ?? def?.retain ?? false);
|
|
1260
1281
|
let retainKey;
|
|
1261
1282
|
if (retain) {
|
|
1262
1283
|
if (callerOptions.retainKey) {
|
|
@@ -1275,11 +1296,18 @@
|
|
|
1275
1296
|
retainKey = String(name);
|
|
1276
1297
|
}
|
|
1277
1298
|
}
|
|
1278
|
-
const sendOptions =
|
|
1299
|
+
const sendOptions = {};
|
|
1300
|
+
if (retain) {
|
|
1301
|
+
sendOptions.retain = true;
|
|
1302
|
+
sendOptions.retainKey = retainKey;
|
|
1303
|
+
}
|
|
1304
|
+
if (targeted) {
|
|
1305
|
+
sendOptions.to = callerOptions.to;
|
|
1306
|
+
}
|
|
1279
1307
|
return broadcast(
|
|
1280
1308
|
String(name),
|
|
1281
1309
|
payload,
|
|
1282
|
-
sendOptions
|
|
1310
|
+
Object.keys(sendOptions).length ? sendOptions : void 0
|
|
1283
1311
|
);
|
|
1284
1312
|
}
|
|
1285
1313
|
function refreshWaiting() {
|
|
@@ -1336,8 +1364,9 @@
|
|
|
1336
1364
|
matchId = payload.matchId;
|
|
1337
1365
|
appliedIdKeys.clear();
|
|
1338
1366
|
setState("active");
|
|
1367
|
+
const retained = msg?.retained === true;
|
|
1339
1368
|
try {
|
|
1340
|
-
options.onStart(makeCtx(), payload);
|
|
1369
|
+
options.onStart(makeCtx(retained), payload);
|
|
1341
1370
|
} catch (err) {
|
|
1342
1371
|
error("multiplayer.match onStart threw:", err);
|
|
1343
1372
|
}
|
|
@@ -1360,10 +1389,11 @@
|
|
|
1360
1389
|
const def = eventDefFor(eventName);
|
|
1361
1390
|
const fromPeerId = msg?.fromPeerId ?? "";
|
|
1362
1391
|
const seq = msg?.seq;
|
|
1363
|
-
|
|
1392
|
+
const retained = msg?.retained === true;
|
|
1393
|
+
if (def?.hostHandle && localIsHost() && fromPeerId !== localSelfId() && !retained) {
|
|
1364
1394
|
try {
|
|
1365
1395
|
Promise.resolve(
|
|
1366
|
-
def.hostHandle(makeCtx(), payload, fromPeerId)
|
|
1396
|
+
def.hostHandle(makeCtx(false), payload, fromPeerId)
|
|
1367
1397
|
).catch(
|
|
1368
1398
|
(err) => error(
|
|
1369
1399
|
`multiplayer.match hostHandle('${eventName}') threw:`,
|
|
@@ -1375,7 +1405,7 @@
|
|
|
1375
1405
|
}
|
|
1376
1406
|
return;
|
|
1377
1407
|
}
|
|
1378
|
-
maybeApply(eventName, def, payload, fromPeerId, seq);
|
|
1408
|
+
maybeApply(eventName, def, payload, fromPeerId, seq, retained);
|
|
1379
1409
|
}
|
|
1380
1410
|
offHandlers.push(
|
|
1381
1411
|
on("peer_joined", () => {
|
|
@@ -1636,6 +1666,7 @@
|
|
|
1636
1666
|
isHost,
|
|
1637
1667
|
isViewer,
|
|
1638
1668
|
selfId,
|
|
1669
|
+
serverNow,
|
|
1639
1670
|
match: createMatch,
|
|
1640
1671
|
assignSeats,
|
|
1641
1672
|
waitForPeers
|