@usions/sdk 2.21.0 → 2.24.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/package.json +1 -1
- package/src/browser.js +961 -166
- package/src/modules/backend-channel.js +41 -8
- package/src/modules/core.js +59 -8
- package/src/modules/errors.js +49 -4
- package/src/modules/game-core.js +26 -17
- package/src/modules/game-direct.js +14 -7
- package/src/modules/game-methods.js +114 -29
- package/src/modules/game-netcode.js +3 -0
- package/src/modules/game-resilience.js +245 -0
- package/src/modules/matchmaking.js +92 -13
- package/src/modules/misc.js +7 -2
- package/src/modules/netcode/index.js +4 -2
- package/src/modules/netcode/interest-grid.js +227 -0
- package/src/modules/wallet.js +12 -5
- package/types/index.d.ts +164 -6
package/src/modules/wallet.js
CHANGED
|
@@ -21,13 +21,17 @@ export function createWalletModule(Usion) {
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Get current wallet balance
|
|
24
|
+
* @param {object} [opts]
|
|
25
|
+
* @param {boolean} [opts.fresh] - Bypass the cache and re-query the host.
|
|
26
|
+
* Use after out-of-band balance changes (e.g. a server-side settle).
|
|
24
27
|
* @returns {Promise<number>} Balance in credits
|
|
25
28
|
*/
|
|
26
|
-
getBalance: function() {
|
|
29
|
+
getBalance: function(opts) {
|
|
27
30
|
const self = this;
|
|
31
|
+
const fresh = !!(opts && opts.fresh);
|
|
28
32
|
|
|
29
33
|
// If we have cached balance, return it
|
|
30
|
-
if (self._balance !== null) {
|
|
34
|
+
if (!fresh && self._balance !== null) {
|
|
31
35
|
return Promise.resolve(self._balance);
|
|
32
36
|
}
|
|
33
37
|
|
|
@@ -94,11 +98,14 @@ export function createWalletModule(Usion) {
|
|
|
94
98
|
settled = true;
|
|
95
99
|
cleanup();
|
|
96
100
|
// Update cached balance from the authoritative new balance, else
|
|
97
|
-
// best-effort subtract
|
|
101
|
+
// best-effort subtract now and re-query the host so the cache
|
|
102
|
+
// converges on the real value (an estimate would otherwise stick
|
|
103
|
+
// until reload).
|
|
98
104
|
if (response && response.newBalance !== undefined) {
|
|
99
105
|
self._balance = response.newBalance;
|
|
100
|
-
} else
|
|
101
|
-
self._balance -= amount;
|
|
106
|
+
} else {
|
|
107
|
+
if (self._balance !== null) self._balance -= amount;
|
|
108
|
+
self.getBalance({ fresh: true }).catch(function() { /* keep estimate */ });
|
|
102
109
|
}
|
|
103
110
|
resolve(response);
|
|
104
111
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -97,7 +97,8 @@ export interface FileStorageModule {
|
|
|
97
97
|
// ─── Wallet ──────────────────────────────────────────────────────
|
|
98
98
|
|
|
99
99
|
export interface WalletModule {
|
|
100
|
-
|
|
100
|
+
/** Cached after the first call; pass `{ fresh: true }` to re-query the host. */
|
|
101
|
+
getBalance(opts?: { fresh?: boolean }): Promise<number>;
|
|
101
102
|
hasCredits(amount: number): Promise<boolean>;
|
|
102
103
|
requestPayment(amount: number, reason: string, data?: PaymentOptions): Promise<PaymentResponse>;
|
|
103
104
|
onBalanceChange(callback: (balance: number) => void): UnsubscribeFn;
|
|
@@ -172,6 +173,51 @@ export interface DirectConnectionConfig {
|
|
|
172
173
|
apiUrl?: string;
|
|
173
174
|
token?: string;
|
|
174
175
|
protocolVersion?: string;
|
|
176
|
+
/**
|
|
177
|
+
* Liveness watchdog tuning, or `false` to disable probing/dead detection.
|
|
178
|
+
* Defaults: probe after 4 s of silence, declare dead after 12 s. (SDK ≥ 2.24)
|
|
179
|
+
*/
|
|
180
|
+
liveness?: DirectLivenessOptions | false;
|
|
181
|
+
/**
|
|
182
|
+
* Hold realtime 'input' frames (latest only) while the socket send buffer
|
|
183
|
+
* exceeds this many bytes; drained as the buffer clears. Control frames
|
|
184
|
+
* (join/action/ping/…) always send. 0 or `false` disables. Default 8192.
|
|
185
|
+
* (SDK ≥ 2.24)
|
|
186
|
+
*/
|
|
187
|
+
backpressureBytes?: number | false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Liveness watchdog options for connectDirect. (SDK ≥ 2.24) */
|
|
191
|
+
export interface DirectLivenessOptions {
|
|
192
|
+
/** Silence before an RTT probe ping, ms (default 4000, clamped ≥ 1000). */
|
|
193
|
+
probeAfterMs?: number;
|
|
194
|
+
/** Silence before the socket is declared dead and re-dialed, ms (default 12000, clamped ≥ probeAfterMs + 2000). */
|
|
195
|
+
deadAfterMs?: number;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Coarse connection quality. 'dead' = liveness-declared dead or disconnected. */
|
|
199
|
+
export type NetworkQuality = 'good' | 'fair' | 'poor' | 'dead';
|
|
200
|
+
|
|
201
|
+
/** Snapshot returned by game.getNetworkStats(). (SDK ≥ 2.24) */
|
|
202
|
+
export interface NetworkStats {
|
|
203
|
+
transport: 'direct' | 'relay';
|
|
204
|
+
connectionState: GameConnectionState;
|
|
205
|
+
quality: NetworkQuality;
|
|
206
|
+
/** Smoothed round-trip time in ms (EWMA); null until something measured it. */
|
|
207
|
+
rtt: number | null;
|
|
208
|
+
/** Smoothed RTT variation in ms; null until something measured it. */
|
|
209
|
+
jitter: number | null;
|
|
210
|
+
/** ms since the last inbound direct-socket message; null in relay mode. */
|
|
211
|
+
lastInboundAgeMs: number | null;
|
|
212
|
+
/** Direct-socket send-buffer bytes; null when there is no direct socket. */
|
|
213
|
+
bufferedAmount: number | null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Payload of game.onNetworkQuality — fired on quality transitions only. */
|
|
217
|
+
export interface NetworkQualityChange {
|
|
218
|
+
quality: NetworkQuality;
|
|
219
|
+
prev: NetworkQuality;
|
|
220
|
+
stats: NetworkStats;
|
|
175
221
|
}
|
|
176
222
|
|
|
177
223
|
export interface PlayerJoinedData {
|
|
@@ -185,6 +231,9 @@ export interface PlayerJoinedData {
|
|
|
185
231
|
export interface PlayerLeftData {
|
|
186
232
|
room_id: string;
|
|
187
233
|
player_id: string;
|
|
234
|
+
/** Authoritative roster (mirrors PlayerJoinedData) — reconcile membership from either event. */
|
|
235
|
+
player_ids: string[];
|
|
236
|
+
reason?: string;
|
|
188
237
|
}
|
|
189
238
|
|
|
190
239
|
export interface StateUpdateData {
|
|
@@ -274,6 +323,20 @@ export interface GameInviteResult {
|
|
|
274
323
|
invited: string[];
|
|
275
324
|
}
|
|
276
325
|
|
|
326
|
+
/** Options for game.joinWorld(). */
|
|
327
|
+
export interface JoinWorldOptions {
|
|
328
|
+
/** Service to place into (defaults to the current service). */
|
|
329
|
+
serviceId?: string;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Result of game.joinWorld(). */
|
|
333
|
+
export interface WorldJoinResult {
|
|
334
|
+
/** The world room this player was placed into (also set on game.roomId). */
|
|
335
|
+
roomId: string;
|
|
336
|
+
/** Players in the world at placement time (this player included). */
|
|
337
|
+
playerIds: string[];
|
|
338
|
+
}
|
|
339
|
+
|
|
277
340
|
/** Unified connection-state machine, identical across all transports. */
|
|
278
341
|
export type GameConnectionState = 'connected' | 'disconnected' | 'rejoining' | 'reconnected';
|
|
279
342
|
|
|
@@ -358,6 +421,15 @@ export interface GameModule {
|
|
|
358
421
|
// transitions so rejoining clients can restore instantly (the host
|
|
359
422
|
// need not be the one to call it).
|
|
360
423
|
join(roomId?: string): Promise<GameJoinResult>;
|
|
424
|
+
/**
|
|
425
|
+
* Place this player into a drop-in/drop-out WORLD room (services tagged
|
|
426
|
+
* `world`). Placement rides the matchmaking channel (works embedded AND
|
|
427
|
+
* standalone): the platform returns the world you're already in, else
|
|
428
|
+
* backfills a world with space, else creates a new one. Sets `game.roomId`,
|
|
429
|
+
* so the usual `connect()`+`join()` / `connectDirect()` flow works
|
|
430
|
+
* unchanged afterwards. Rejects with MATCH_TIMEOUT after 15s. (SDK ≥ 2.23)
|
|
431
|
+
*/
|
|
432
|
+
joinWorld(opts?: JoinWorldOptions): Promise<WorldJoinResult>;
|
|
361
433
|
leave(): void;
|
|
362
434
|
action(actionType: string, actionData?: Record<string, any>, opts?: GameActionOptions): Promise<ActionResult>;
|
|
363
435
|
realtime(actionType: string, actionData?: Record<string, any>): void;
|
|
@@ -495,6 +567,8 @@ export interface GameModule {
|
|
|
495
567
|
createLagCompensator(opts?: LagCompensatorOptions): LagCompensator;
|
|
496
568
|
/** Deterministic lockstep (inputs-only sim + free replays). */
|
|
497
569
|
createLockstep(opts: LockstepOptions): Lockstep;
|
|
570
|
+
/** Spatial hash for area-of-interest filtering in world rooms. (SDK ≥ 2.23) */
|
|
571
|
+
createInterestGrid(opts?: InterestGridOptions): InterestGrid;
|
|
498
572
|
/** Inject latency/jitter/loss locally to test under bad networks (null = off). */
|
|
499
573
|
simulateNetwork(opts: NetworkSimOptions | null): NetworkSim | void;
|
|
500
574
|
|
|
@@ -502,6 +576,21 @@ export interface GameModule {
|
|
|
502
576
|
ping(): Promise<number | null>;
|
|
503
577
|
/** Latest smoothed round-trip time in ms (null until first ping). */
|
|
504
578
|
getRtt(): number | null;
|
|
579
|
+
|
|
580
|
+
// ─── Network resilience (SDK ≥ 2.24) ─────────────────────────
|
|
581
|
+
/**
|
|
582
|
+
* Cheap snapshot of connection health: transport, connection state,
|
|
583
|
+
* quality, smoothed rtt/jitter, age of the last inbound direct frame,
|
|
584
|
+
* and buffered (unsent) bytes. Works in every transport; relay mode
|
|
585
|
+
* reports rtt/jitter null unless game.ping() measured them.
|
|
586
|
+
*/
|
|
587
|
+
getNetworkStats(): NetworkStats;
|
|
588
|
+
/**
|
|
589
|
+
* Observe network-quality TRANSITIONS ('good'/'fair'/'poor'/'dead') —
|
|
590
|
+
* fires only when the quality level changes, with { quality, prev, stats }.
|
|
591
|
+
* Drive a connection-quality indicator or degrade send rates from here.
|
|
592
|
+
*/
|
|
593
|
+
onNetworkQuality(callback: (change: NetworkQualityChange) => void): UnsubscribeFn;
|
|
505
594
|
}
|
|
506
595
|
|
|
507
596
|
// ─── Netcode toolkit ──────────────────────────────────────────────
|
|
@@ -837,6 +926,43 @@ export class WebTransportConnection {
|
|
|
837
926
|
close(): void;
|
|
838
927
|
}
|
|
839
928
|
|
|
929
|
+
// ─── Interest grid (area-of-interest spatial hash, SDK ≥ 2.23) ───
|
|
930
|
+
|
|
931
|
+
export interface InterestGridOptions {
|
|
932
|
+
/** Cell size in world units (default 256); pick ≈ the typical query radius. */
|
|
933
|
+
cellSize?: number;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/** Per-observer visibility diff from InterestGrid.observe(). */
|
|
937
|
+
export interface InterestGridObservation {
|
|
938
|
+
/** Ids visible now that were not visible on this observer's previous observe(). */
|
|
939
|
+
entered: Array<string | number>;
|
|
940
|
+
/** Ids visible on the previous observe() that no longer are. */
|
|
941
|
+
left: Array<string | number>;
|
|
942
|
+
/** All ids currently within the radius. */
|
|
943
|
+
visible: Array<string | number>;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Spatial hash for area-of-interest filtering: O(cells touched) proximity
|
|
948
|
+
* queries plus per-observer enter/leave diffs. Pure JS (browser and Node) —
|
|
949
|
+
* the AOI building block for world-scale rooms.
|
|
950
|
+
*/
|
|
951
|
+
export interface InterestGrid {
|
|
952
|
+
/** Insert an entity or move an existing one. */
|
|
953
|
+
upsert(id: string | number, x: number, y: number): void;
|
|
954
|
+
/** Remove an entity (unknown ids are a no-op). */
|
|
955
|
+
remove(id: string | number): void;
|
|
956
|
+
/** Entity ids within `radius` of (x, y) — a true circle test. */
|
|
957
|
+
query(x: number, y: number, radius: number): Array<string | number>;
|
|
958
|
+
/** query() diffed against this observer's previous call. */
|
|
959
|
+
observe(observerId: string | number, x: number, y: number, radius: number): InterestGridObservation;
|
|
960
|
+
/** Free an observer's tracking state (call when a player leaves). */
|
|
961
|
+
dropObserver(observerId: string | number): void;
|
|
962
|
+
/** Number of entities in the grid. */
|
|
963
|
+
size(): number;
|
|
964
|
+
}
|
|
965
|
+
|
|
840
966
|
export interface NetcodeModule {
|
|
841
967
|
SnapshotInterpolation: typeof SnapshotInterpolation;
|
|
842
968
|
Vault: typeof Vault;
|
|
@@ -854,6 +980,8 @@ export interface NetcodeModule {
|
|
|
854
980
|
quantize<T = any>(value: T, precision?: number): T;
|
|
855
981
|
encode(value: any): Uint8Array;
|
|
856
982
|
decode(buf: ArrayBuffer | Uint8Array): any;
|
|
983
|
+
/** Spatial hash for area-of-interest filtering in world rooms. (SDK ≥ 2.23) */
|
|
984
|
+
createInterestGrid(opts?: InterestGridOptions): InterestGrid;
|
|
857
985
|
}
|
|
858
986
|
|
|
859
987
|
// ─── Results ────────────────────────────────────────────────────
|
|
@@ -901,7 +1029,15 @@ export interface UsionSDK {
|
|
|
901
1029
|
config: UsionConfig;
|
|
902
1030
|
|
|
903
1031
|
// Lifecycle
|
|
904
|
-
|
|
1032
|
+
/**
|
|
1033
|
+
* Initialize the SDK. The callback fires when the host's INIT config
|
|
1034
|
+
* arrives; every call also returns a Promise of the same config, so
|
|
1035
|
+
* `const config = await Usion.init()` works. `opts.timeout` (ms) rejects
|
|
1036
|
+
* the promise with UsionError(INIT_TIMEOUT) when no INIT arrives in time
|
|
1037
|
+
* (embedded but host silent). (promise/timeout: SDK ≥ 2.22)
|
|
1038
|
+
*/
|
|
1039
|
+
init(callback?: (config: UsionConfig) => void, opts?: { timeout?: number }): Promise<UsionConfig>;
|
|
1040
|
+
init(opts: { timeout?: number }): Promise<UsionConfig>;
|
|
905
1041
|
exit(options?: { backCount?: number }): void;
|
|
906
1042
|
|
|
907
1043
|
// Theme & Locale
|
|
@@ -946,8 +1082,8 @@ export interface UsionSDK {
|
|
|
946
1082
|
// Logging
|
|
947
1083
|
log(msg: string): void;
|
|
948
1084
|
|
|
949
|
-
// Generic event listener
|
|
950
|
-
on(type: string, callback: (data: any) => void):
|
|
1085
|
+
// Generic event listener (returns an unsubscribe function)
|
|
1086
|
+
on(type: string, callback: (data: any) => void): UnsubscribeFn;
|
|
951
1087
|
|
|
952
1088
|
// UI helpers
|
|
953
1089
|
setLoading(btn: HTMLElement | string, loading: boolean): void;
|
|
@@ -1064,8 +1200,14 @@ export interface CloudModule extends CloudScope {
|
|
|
1064
1200
|
export interface Match { roomId: string; players: string[]; serviceId?: string; }
|
|
1065
1201
|
|
|
1066
1202
|
export interface MatchmakingModule {
|
|
1067
|
-
/**
|
|
1068
|
-
|
|
1203
|
+
/**
|
|
1204
|
+
* Join the quick-match queue; resolves when paired with online strangers.
|
|
1205
|
+
* `opts.timeout` (ms) bounds the wait: on expiry the queue is left and the
|
|
1206
|
+
* promise rejects with UsionError(MATCH_TIMEOUT). A newer find() rejects
|
|
1207
|
+
* the previous one with SUPERSEDED; cancel() rejects it with CANCELLED.
|
|
1208
|
+
* (timeout: SDK ≥ 2.22)
|
|
1209
|
+
*/
|
|
1210
|
+
find(serviceId?: string, opts?: { size?: number; timeout?: number }): Promise<Match>;
|
|
1069
1211
|
/** Leave the queue / stop waiting. */
|
|
1070
1212
|
cancel(): Promise<any>;
|
|
1071
1213
|
/** Called whenever a match is found. */
|
|
@@ -1120,16 +1262,30 @@ export declare const ERROR_CODES: {
|
|
|
1120
1262
|
readonly NO_ROOM: 'NO_ROOM';
|
|
1121
1263
|
readonly ROOM_NOT_FOUND: 'ROOM_NOT_FOUND';
|
|
1122
1264
|
readonly NOT_PARTICIPANT: 'NOT_PARTICIPANT';
|
|
1265
|
+
readonly NOT_IN_ROOM: 'NOT_IN_ROOM';
|
|
1123
1266
|
readonly NOT_AUTHORITY: 'NOT_AUTHORITY';
|
|
1124
1267
|
readonly NOT_AUTHENTICATED: 'NOT_AUTHENTICATED';
|
|
1125
1268
|
readonly JOIN_TIMEOUT: 'JOIN_TIMEOUT';
|
|
1126
1269
|
readonly CONNECT_TIMEOUT: 'CONNECT_TIMEOUT';
|
|
1270
|
+
readonly INIT_TIMEOUT: 'INIT_TIMEOUT';
|
|
1271
|
+
readonly MATCH_TIMEOUT: 'MATCH_TIMEOUT';
|
|
1127
1272
|
readonly STATE_TOO_LARGE: 'STATE_TOO_LARGE';
|
|
1128
1273
|
readonly INVALID_STATE: 'INVALID_STATE';
|
|
1274
|
+
readonly STALE_STATE: 'STALE_STATE';
|
|
1129
1275
|
readonly INVALID_NEXT_TURN: 'INVALID_NEXT_TURN';
|
|
1276
|
+
readonly INVALID_INPUT: 'INVALID_INPUT';
|
|
1277
|
+
readonly NOT_FOUND: 'NOT_FOUND';
|
|
1278
|
+
readonly QUOTA_EXCEEDED: 'QUOTA_EXCEEDED';
|
|
1279
|
+
readonly VALUE_TOO_LARGE: 'VALUE_TOO_LARGE';
|
|
1280
|
+
readonly LOBBY_FULL: 'LOBBY_FULL';
|
|
1281
|
+
readonly LOBBY_CLOSED: 'LOBBY_CLOSED';
|
|
1282
|
+
readonly WORLD_FULL: 'WORLD_FULL';
|
|
1283
|
+
readonly CONFLICT: 'CONFLICT';
|
|
1130
1284
|
readonly RATE_LIMITED: 'RATE_LIMITED';
|
|
1131
1285
|
readonly REQUEST_TIMEOUT: 'REQUEST_TIMEOUT';
|
|
1132
1286
|
readonly QUEUE_FULL: 'QUEUE_FULL';
|
|
1287
|
+
readonly CANCELLED: 'CANCELLED';
|
|
1288
|
+
readonly SUPERSEDED: 'SUPERSEDED';
|
|
1133
1289
|
readonly UNSUPPORTED: 'UNSUPPORTED';
|
|
1134
1290
|
readonly UNKNOWN: 'UNKNOWN';
|
|
1135
1291
|
};
|
|
@@ -1139,6 +1295,8 @@ export type UsionErrorCode = keyof typeof ERROR_CODES;
|
|
|
1139
1295
|
export declare class UsionError extends Error {
|
|
1140
1296
|
readonly name: 'UsionError';
|
|
1141
1297
|
readonly code: UsionErrorCode;
|
|
1298
|
+
/** Seconds until a RATE_LIMITED call may be retried (when the server sent it). */
|
|
1299
|
+
readonly retryAfter?: number;
|
|
1142
1300
|
constructor(code: UsionErrorCode | string, message?: string);
|
|
1143
1301
|
}
|
|
1144
1302
|
|