@usions/sdk 2.24.2 → 3.0.0-next.7
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 +45 -277
- package/dist/browser.global.js +1972 -0
- package/dist/browser.global.js.map +1 -0
- package/dist/index.d.ts +337 -0
- package/dist/index.js +1955 -0
- package/dist/index.js.map +1 -0
- package/dist/testing.d.ts +23 -0
- package/dist/testing.js +63 -0
- package/dist/testing.js.map +1 -0
- package/dist/types-G075L8Nl.d.ts +83 -0
- package/package.json +25 -46
- package/src/browser.js +0 -6725
- package/src/index.js +0 -10
- package/src/modules/back-button.js +0 -26
- package/src/modules/backend-channel.js +0 -109
- package/src/modules/bot.js +0 -52
- package/src/modules/chat.js +0 -35
- package/src/modules/cloud.js +0 -67
- package/src/modules/core.js +0 -424
- package/src/modules/errors.js +0 -120
- package/src/modules/file-storage.js +0 -46
- package/src/modules/game-ack.js +0 -32
- package/src/modules/game-core.js +0 -278
- package/src/modules/game-direct.js +0 -267
- package/src/modules/game-methods.js +0 -782
- package/src/modules/game-netcode.js +0 -349
- package/src/modules/game-proxy.js +0 -184
- package/src/modules/game-reliability.js +0 -278
- package/src/modules/game-resilience.js +0 -245
- package/src/modules/game-socket.js +0 -214
- package/src/modules/index.js +0 -78
- package/src/modules/leaderboard.js +0 -46
- package/src/modules/lobby.js +0 -103
- package/src/modules/matchmaking.js +0 -140
- package/src/modules/misc.js +0 -152
- package/src/modules/netcode/binary.js +0 -113
- package/src/modules/netcode/delta.js +0 -236
- package/src/modules/netcode/index.js +0 -43
- package/src/modules/netcode/interest-grid.js +0 -227
- package/src/modules/netcode/interpolation.js +0 -235
- package/src/modules/netcode/lagcomp.js +0 -104
- package/src/modules/netcode/lockstep.js +0 -103
- package/src/modules/netcode/mesh-network.js +0 -103
- package/src/modules/netcode/mesh.js +0 -188
- package/src/modules/netcode/netsim.js +0 -77
- package/src/modules/netcode/ping.js +0 -69
- package/src/modules/netcode/prediction.js +0 -113
- package/src/modules/netcode/sender.js +0 -102
- package/src/modules/netcode/webtransport.js +0 -195
- package/src/modules/notify.js +0 -70
- package/src/modules/permissions.js +0 -157
- package/src/modules/results.js +0 -34
- package/src/modules/session.js +0 -62
- package/src/modules/storage.js +0 -64
- package/src/modules/ui.js +0 -111
- package/src/modules/user.js +0 -61
- package/src/modules/wallet.js +0 -253
- package/types/browser.d.ts +0 -5
- package/types/index.d.ts +0 -1308
package/types/index.d.ts
DELETED
|
@@ -1,1308 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion Mini App SDK v2.1 — TypeScript Declarations
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// ─── Config & Init ───────────────────────────────────────────────
|
|
6
|
-
|
|
7
|
-
export interface UsionConfig {
|
|
8
|
-
userId?: string;
|
|
9
|
-
userName?: string;
|
|
10
|
-
userAvatar?: string;
|
|
11
|
-
authToken?: string;
|
|
12
|
-
sessionId?: string;
|
|
13
|
-
sessionData?: Record<string, any>;
|
|
14
|
-
balance?: number;
|
|
15
|
-
results?: SavedResult[];
|
|
16
|
-
theme?: 'light' | 'dark';
|
|
17
|
-
language?: string;
|
|
18
|
-
socketUrl?: string;
|
|
19
|
-
/** HTTP/3 WebTransport endpoint for direct-mode games (lowest latency). */
|
|
20
|
-
webTransportUrl?: string;
|
|
21
|
-
roomId?: string;
|
|
22
|
-
playerIds?: string[];
|
|
23
|
-
serviceId?: string;
|
|
24
|
-
serviceName?: string;
|
|
25
|
-
apiUrl?: string;
|
|
26
|
-
connectionMode?: 'platform' | 'direct';
|
|
27
|
-
/**
|
|
28
|
-
* Internal path the host opened this app at — set when the user taps a
|
|
29
|
-
* notification carrying a `path`. Read via `Usion.getLaunchParams().path`.
|
|
30
|
-
*/
|
|
31
|
-
launchPath?: string;
|
|
32
|
-
/** Referral code the app was opened with, if any. */
|
|
33
|
-
ref?: string;
|
|
34
|
-
/**
|
|
35
|
-
* How the app was launched: `'single'` (from Explore / the Game hub, solo)
|
|
36
|
-
* or `'multiplayer'` (from a game invite in a chat). Read via
|
|
37
|
-
* `Usion.getLaunchParams().mode`. (SDK ≥ 2.18)
|
|
38
|
-
*/
|
|
39
|
-
mode?: 'single' | 'multiplayer';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// ─── Payment ─────────────────────────────────────────────────────
|
|
43
|
-
|
|
44
|
-
export interface PaymentResponse {
|
|
45
|
-
success: boolean;
|
|
46
|
-
reason?: string;
|
|
47
|
-
newBalance?: number;
|
|
48
|
-
receiptToken?: string;
|
|
49
|
-
transactionId?: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface PaymentOptions {
|
|
53
|
-
/**
|
|
54
|
-
* Optional idempotency key. When set, the host + backend dedupe on it so any
|
|
55
|
-
* retry of the SAME intent (network error, reload, double-tap) reuses the
|
|
56
|
-
* SAME charge instead of debiting again, and returns the same receiptToken.
|
|
57
|
-
* Omit for the legacy one-shot behavior.
|
|
58
|
-
*/
|
|
59
|
-
idempotencyKey?: string;
|
|
60
|
-
[key: string]: any;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// ─── User ────────────────────────────────────────────────────────
|
|
64
|
-
|
|
65
|
-
export interface UserProfile {
|
|
66
|
-
id: string;
|
|
67
|
-
name: string;
|
|
68
|
-
avatar: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export interface UserModule {
|
|
72
|
-
getId(): string | null;
|
|
73
|
-
getName(): string | null;
|
|
74
|
-
getAvatar(): string | null;
|
|
75
|
-
getToken(): string | null;
|
|
76
|
-
getProfile(): Promise<UserProfile>;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// ─── Storage ─────────────────────────────────────────────────────
|
|
80
|
-
|
|
81
|
-
export interface StorageModule {
|
|
82
|
-
get(key: string): Promise<any>;
|
|
83
|
-
set(key: string, value: any): Promise<void>;
|
|
84
|
-
remove(key: string): Promise<void>;
|
|
85
|
-
clear(): Promise<void>;
|
|
86
|
-
keys(): Promise<string[]>;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// ─── File Storage ───────────────────────────────────────────────
|
|
90
|
-
|
|
91
|
-
export interface FileStorageModule {
|
|
92
|
-
set(key: string, base64Data: string, mimeType: string): Promise<void>;
|
|
93
|
-
get(key: string): Promise<{ base64Data: string; mimeType: string } | null>;
|
|
94
|
-
remove(key: string): Promise<void>;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ─── Wallet ──────────────────────────────────────────────────────
|
|
98
|
-
|
|
99
|
-
export interface WalletModule {
|
|
100
|
-
/** Cached after the first call; pass `{ fresh: true }` to re-query the host. */
|
|
101
|
-
getBalance(opts?: { fresh?: boolean }): Promise<number>;
|
|
102
|
-
hasCredits(amount: number): Promise<boolean>;
|
|
103
|
-
requestPayment(amount: number, reason: string, data?: PaymentOptions): Promise<PaymentResponse>;
|
|
104
|
-
onBalanceChange(callback: (balance: number) => void): UnsubscribeFn;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// ─── Session ─────────────────────────────────────────────────────
|
|
108
|
-
|
|
109
|
-
export interface SessionModule {
|
|
110
|
-
getId(): string | null;
|
|
111
|
-
getData(key?: string): any;
|
|
112
|
-
setData(keyOrData: string | Record<string, any>, value?: any): void;
|
|
113
|
-
clear(): void;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// ─── Chat ────────────────────────────────────────────────────────
|
|
117
|
-
|
|
118
|
-
export interface ChatCreationResult {
|
|
119
|
-
chatId: string;
|
|
120
|
-
peerName: string;
|
|
121
|
-
peerUsername: string;
|
|
122
|
-
peerAvatar: string;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export interface ChatModule {
|
|
126
|
-
sendMessage(recipientId: string, message: string): Promise<{ success: boolean; reason?: string }>;
|
|
127
|
-
createPersonalChat(peerUserId: string): Promise<ChatCreationResult>;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// ─── Share ────────────────────────────────────────────────────────
|
|
131
|
-
|
|
132
|
-
export interface ShareMediaItem {
|
|
133
|
-
type: 'image' | 'video' | 'audio';
|
|
134
|
-
url: string;
|
|
135
|
-
thumbnailUrl?: string;
|
|
136
|
-
width?: number;
|
|
137
|
-
height?: number;
|
|
138
|
-
duration?: number;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export interface ShareData {
|
|
142
|
-
text?: string;
|
|
143
|
-
audioUrl?: string;
|
|
144
|
-
imageUrl?: string;
|
|
145
|
-
videoUrl?: string;
|
|
146
|
-
thumbnailUrl?: string;
|
|
147
|
-
width?: number;
|
|
148
|
-
height?: number;
|
|
149
|
-
duration?: number;
|
|
150
|
-
media?: ShareMediaItem[];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// ─── Game ────────────────────────────────────────────────────────
|
|
154
|
-
|
|
155
|
-
export interface GameJoinResult {
|
|
156
|
-
room_id: string;
|
|
157
|
-
player_id: string;
|
|
158
|
-
sequence?: number;
|
|
159
|
-
error?: string;
|
|
160
|
-
message?: string;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export interface ActionResult {
|
|
164
|
-
success: boolean;
|
|
165
|
-
/** False when a v2 proxy host accepted the command but cannot acknowledge it. */
|
|
166
|
-
acknowledged?: boolean;
|
|
167
|
-
sequence?: number;
|
|
168
|
-
error?: string;
|
|
169
|
-
message?: string;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export interface DirectConnectionConfig {
|
|
173
|
-
roomId?: string;
|
|
174
|
-
serviceId?: string;
|
|
175
|
-
apiUrl?: string;
|
|
176
|
-
token?: string;
|
|
177
|
-
protocolVersion?: string;
|
|
178
|
-
/**
|
|
179
|
-
* Liveness watchdog tuning, or `false` to disable probing/dead detection.
|
|
180
|
-
* Defaults: probe after 4 s of silence, declare dead after 12 s. (SDK ≥ 2.24)
|
|
181
|
-
*/
|
|
182
|
-
liveness?: DirectLivenessOptions | false;
|
|
183
|
-
/**
|
|
184
|
-
* Hold realtime 'input' frames (latest only) while the socket send buffer
|
|
185
|
-
* exceeds this many bytes; drained as the buffer clears. Control frames
|
|
186
|
-
* (join/action/ping/…) always send. 0 or `false` disables. Default 8192.
|
|
187
|
-
* (SDK ≥ 2.24)
|
|
188
|
-
*/
|
|
189
|
-
backpressureBytes?: number | false;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/** Liveness watchdog options for connectDirect. (SDK ≥ 2.24) */
|
|
193
|
-
export interface DirectLivenessOptions {
|
|
194
|
-
/** Silence before an RTT probe ping, ms (default 4000, clamped ≥ 1000). */
|
|
195
|
-
probeAfterMs?: number;
|
|
196
|
-
/** Silence before the socket is declared dead and re-dialed, ms (default 12000, clamped ≥ probeAfterMs + 2000). */
|
|
197
|
-
deadAfterMs?: number;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** Coarse connection quality. 'dead' = liveness-declared dead or disconnected. */
|
|
201
|
-
export type NetworkQuality = 'good' | 'fair' | 'poor' | 'dead';
|
|
202
|
-
|
|
203
|
-
/** Snapshot returned by game.getNetworkStats(). (SDK ≥ 2.24) */
|
|
204
|
-
export interface NetworkStats {
|
|
205
|
-
transport: 'direct' | 'relay';
|
|
206
|
-
connectionState: GameConnectionState;
|
|
207
|
-
quality: NetworkQuality;
|
|
208
|
-
/** Smoothed round-trip time in ms (EWMA); null until something measured it. */
|
|
209
|
-
rtt: number | null;
|
|
210
|
-
/** Smoothed RTT variation in ms; null until something measured it. */
|
|
211
|
-
jitter: number | null;
|
|
212
|
-
/** ms since the last inbound direct-socket message; null in relay mode. */
|
|
213
|
-
lastInboundAgeMs: number | null;
|
|
214
|
-
/** Direct-socket send-buffer bytes; null when there is no direct socket. */
|
|
215
|
-
bufferedAmount: number | null;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Payload of game.onNetworkQuality — fired on quality transitions only. */
|
|
219
|
-
export interface NetworkQualityChange {
|
|
220
|
-
quality: NetworkQuality;
|
|
221
|
-
prev: NetworkQuality;
|
|
222
|
-
stats: NetworkStats;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export interface PlayerJoinedData {
|
|
226
|
-
room_id: string;
|
|
227
|
-
player_id: string;
|
|
228
|
-
player_ids: string[];
|
|
229
|
-
status?: string;
|
|
230
|
-
game_state?: Record<string, any>;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
export interface PlayerLeftData {
|
|
234
|
-
room_id: string;
|
|
235
|
-
player_id: string;
|
|
236
|
-
/** Authoritative roster (mirrors PlayerJoinedData) — reconcile membership from either event. */
|
|
237
|
-
player_ids: string[];
|
|
238
|
-
reason?: string;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
export interface StateUpdateData {
|
|
242
|
-
room_id: string;
|
|
243
|
-
game_state?: Record<string, any>;
|
|
244
|
-
current_turn?: string;
|
|
245
|
-
status?: string;
|
|
246
|
-
sequence?: number;
|
|
247
|
-
[key: string]: any;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export interface GameActionData {
|
|
251
|
-
room_id: string;
|
|
252
|
-
player_id: string;
|
|
253
|
-
action_type: string;
|
|
254
|
-
action_data: Record<string, any>;
|
|
255
|
-
sequence?: number;
|
|
256
|
-
/** Present when the sender passed { nextTurn } — the server-remembered turn. */
|
|
257
|
-
current_turn?: string;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/** Returned by event registrations; call to remove the handler. */
|
|
261
|
-
export type UnsubscribeFn = () => void;
|
|
262
|
-
|
|
263
|
-
export interface GameActionOptions {
|
|
264
|
-
/**
|
|
265
|
-
* Player ID whose turn comes after this action. The server stores it (no
|
|
266
|
-
* game logic — a relay that remembers) and returns it as current_turn in
|
|
267
|
-
* join acks and game:sync, so turn state survives reconnects.
|
|
268
|
-
*/
|
|
269
|
-
nextTurn?: string;
|
|
270
|
-
/**
|
|
271
|
-
* Hold this action while disconnected and send it (in order) once the
|
|
272
|
-
* connection recovers, instead of rejecting with NOT_CONNECTED — "your
|
|
273
|
-
* move is saved and sends when you're back". For turn-based games only;
|
|
274
|
-
* never queue realtime-style inputs. Queue cap 20 (then QUEUE_FULL).
|
|
275
|
-
*/
|
|
276
|
-
queueOffline?: boolean;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export interface PlayerConnectionData {
|
|
280
|
-
room_id: string;
|
|
281
|
-
player_id: string;
|
|
282
|
-
/**
|
|
283
|
-
* connected — player (re)joined the room.
|
|
284
|
-
* reconnecting — player's connection dropped; grace window running (~15s).
|
|
285
|
-
* gone — player did not return before the grace/heartbeat expiry.
|
|
286
|
-
*/
|
|
287
|
-
state: 'connected' | 'reconnecting' | 'gone';
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
export interface GameFinishedData {
|
|
291
|
-
room_id: string;
|
|
292
|
-
winner_ids: string[];
|
|
293
|
-
reason?: string;
|
|
294
|
-
forfeiter?: string;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export interface SyncData {
|
|
298
|
-
room_id: string;
|
|
299
|
-
actions: GameActionData[];
|
|
300
|
-
game_state: Record<string, any>;
|
|
301
|
-
current_turn?: string | null;
|
|
302
|
-
sequence: number;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
export interface RematchData {
|
|
306
|
-
room_id: string;
|
|
307
|
-
player_id: string;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/** Options for game.invite(). */
|
|
311
|
-
export interface GameInviteOptions {
|
|
312
|
-
/** Cap the room at this many players (otherwise the game's max_players). */
|
|
313
|
-
maxPlayers?: number;
|
|
314
|
-
/** Max ms to wait for the user to finish in the picker (default 120000). */
|
|
315
|
-
timeout?: number;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/** Result of game.invite(). */
|
|
319
|
-
export interface GameInviteResult {
|
|
320
|
-
/** True if the user sent at least one invite. */
|
|
321
|
-
success: boolean;
|
|
322
|
-
/** The room invitees will join (you are host); null if nothing was sent. */
|
|
323
|
-
roomId: string | null;
|
|
324
|
-
/** User/group ids the invite was sent to. */
|
|
325
|
-
invited: string[];
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/** Options for game.joinWorld(). */
|
|
329
|
-
export interface JoinWorldOptions {
|
|
330
|
-
/** Service to place into (defaults to the current service). */
|
|
331
|
-
serviceId?: string;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/** Result of game.joinWorld(). */
|
|
335
|
-
export interface WorldJoinResult {
|
|
336
|
-
/** The world room this player was placed into (also set on game.roomId). */
|
|
337
|
-
roomId: string;
|
|
338
|
-
/** Players in the world at placement time (this player included). */
|
|
339
|
-
playerIds: string[];
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/** Unified connection-state machine, identical across all transports. */
|
|
343
|
-
export type GameConnectionState = 'connected' | 'disconnected' | 'rejoining' | 'reconnected';
|
|
344
|
-
|
|
345
|
-
/** Payload of game.onReconnected — fires once after a reconnect's resync. */
|
|
346
|
-
export interface ReconnectedInfo {
|
|
347
|
-
/** The server checkpoint from the resync (may be empty). */
|
|
348
|
-
state: Record<string, any> | null;
|
|
349
|
-
/** Highest action sequence after the resync. */
|
|
350
|
-
lastSequence: number;
|
|
351
|
-
/** True when recovery came through a game:sync payload. */
|
|
352
|
-
viaSync: boolean;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/** An action as seen by a syncedState reducer. */
|
|
356
|
-
export interface SyncedStateAction {
|
|
357
|
-
playerId: string;
|
|
358
|
-
type: string;
|
|
359
|
-
data: any;
|
|
360
|
-
/** Authoritative sequence; undefined only for direct-mode local applies. */
|
|
361
|
-
sequence: number | undefined;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/** Options for game.syncedState(). */
|
|
365
|
-
export interface SyncedStateOptions<S = Record<string, any>> {
|
|
366
|
-
/** Pure reducer applied to every action; default shallow-merges action.data. */
|
|
367
|
-
reduce?: (state: S, action: SyncedStateAction) => S;
|
|
368
|
-
/** Authority checkpoint cadence in actions (1 = every action; 0 disables). */
|
|
369
|
-
checkpointEvery?: number;
|
|
370
|
-
/** Who checkpoints: 'host' (player_ids[0], default) or 'all' participants. */
|
|
371
|
-
authority?: 'host' | 'all';
|
|
372
|
-
/** Default action type used by commit(data) (default 'update'). */
|
|
373
|
-
type?: string;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/** Reconnect-safe authoritative shared state (see game.syncedState). */
|
|
377
|
-
export interface SyncedState<S = Record<string, any>> {
|
|
378
|
-
/** Current state value. */
|
|
379
|
-
get(): S;
|
|
380
|
-
/** Commit with the default action type. Applied exactly once via the echo. */
|
|
381
|
-
commit(data: Record<string, any>): Promise<ActionResult>;
|
|
382
|
-
/** Commit a named action; opts forwarded to game.action (nextTurn, …). */
|
|
383
|
-
commit(type: string, data?: Record<string, any>, opts?: GameActionOptions): Promise<ActionResult>;
|
|
384
|
-
/** Subscribe to changes: cb(state, reason). Returns an unsubscribe fn. */
|
|
385
|
-
onChange(callback: (state: S, reason: 'action' | 'recover') => void): UnsubscribeFn;
|
|
386
|
-
/** Whether this client is the checkpointing authority. */
|
|
387
|
-
isAuthority(): boolean;
|
|
388
|
-
/** Force a server checkpoint now (no-op result in direct mode). */
|
|
389
|
-
checkpoint(): Promise<{ success: boolean; error?: string; code?: UsionErrorCode }>;
|
|
390
|
-
/** Sequence of the last action applied into this state. */
|
|
391
|
-
getSequence(): number;
|
|
392
|
-
/** Detach all listeners; the instance stops receiving updates. */
|
|
393
|
-
destroy(): void;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
export interface GameModule {
|
|
397
|
-
// Connection
|
|
398
|
-
connect(socketUrl?: string, token?: string): Promise<void>;
|
|
399
|
-
connectDirect(config?: DirectConnectionConfig): Promise<void>;
|
|
400
|
-
disconnect(): void;
|
|
401
|
-
isConnected(): boolean;
|
|
402
|
-
/**
|
|
403
|
-
* Whether the app was launched multiplayer (from a chat game invite) vs
|
|
404
|
-
* single-player (from Explore / the Game hub). Wrapper over
|
|
405
|
-
* `Usion.getLaunchParams().mode === 'multiplayer'`. (SDK ≥ 2.18)
|
|
406
|
-
*/
|
|
407
|
-
isMultiplayer(): boolean;
|
|
408
|
-
|
|
409
|
-
// Room & actions
|
|
410
|
-
//
|
|
411
|
-
// RELIABILITY CONTRACT (what the platform guarantees / what your game must do):
|
|
412
|
-
// - Every action is echoed back to the sender with the authoritative
|
|
413
|
-
// sequence number. Apply game state ONLY in onAction — never
|
|
414
|
-
// optimistically on send — so all clients apply identical actions in
|
|
415
|
-
// identical order. The SDK deduplicates by sequence, so each action is
|
|
416
|
-
// delivered exactly once even across reconnect replays.
|
|
417
|
-
// - On a connection blip the host/SDK rejoins the room and replays missed
|
|
418
|
-
// actions via onSync automatically. Use onDisconnect/onReconnect to
|
|
419
|
-
// pause input and show a "reconnecting" indicator.
|
|
420
|
-
// - For turn-based games, pass { nextTurn } on each move and trust
|
|
421
|
-
// current_turn from join/sync instead of deriving the turn locally.
|
|
422
|
-
// - Any participant should checkpoint via setState() at meaningful
|
|
423
|
-
// transitions so rejoining clients can restore instantly (the host
|
|
424
|
-
// need not be the one to call it).
|
|
425
|
-
join(roomId?: string): Promise<GameJoinResult>;
|
|
426
|
-
/**
|
|
427
|
-
* Place this player into a drop-in/drop-out WORLD room (services tagged
|
|
428
|
-
* `world`). Placement rides the matchmaking channel (works embedded AND
|
|
429
|
-
* standalone): the platform returns the world you're already in, else
|
|
430
|
-
* backfills a world with space, else creates a new one. Sets `game.roomId`,
|
|
431
|
-
* so the usual `connect()`+`join()` / `connectDirect()` flow works
|
|
432
|
-
* unchanged afterwards. Rejects with MATCH_TIMEOUT after 15s. (SDK ≥ 2.23)
|
|
433
|
-
*/
|
|
434
|
-
joinWorld(opts?: JoinWorldOptions): Promise<WorldJoinResult>;
|
|
435
|
-
leave(): void;
|
|
436
|
-
action(actionType: string, actionData?: Record<string, any>, opts?: GameActionOptions): Promise<ActionResult>;
|
|
437
|
-
realtime(actionType: string, actionData?: Record<string, any>): void;
|
|
438
|
-
requestSync(lastSequence?: number): void;
|
|
439
|
-
requestRematch(): void;
|
|
440
|
-
forfeit(): Promise<{ success: boolean; error?: string }>;
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* Invite friends to play in your room. Opens the host's friend/group picker
|
|
444
|
-
* (recent chats + username search + your groups, multi-select); each pick gets
|
|
445
|
-
* a game-invite card in their chat, and anyone who taps it joins THIS room
|
|
446
|
-
* (your game gets `onPlayerJoined`). Works for any multiplayer-capable game —
|
|
447
|
-
* if launched solo with no room, the host creates one with you as host and
|
|
448
|
-
* this joins you to it. Embedded only; standalone resolves `{success:false}`.
|
|
449
|
-
* Never rejects. (SDK ≥ 2.19)
|
|
450
|
-
*/
|
|
451
|
-
invite(opts?: GameInviteOptions): Promise<GameInviteResult>;
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* Checkpoint authoritative game state on the server. Any participant in the
|
|
455
|
-
* room may call this (not just player_ids[0]/host), so the snapshot stays
|
|
456
|
-
* fresh even while the host is backgrounded. (Re)joining clients receive the
|
|
457
|
-
* latest checkpoint as game_state in the join ack and in game:sync. Max 64 KB
|
|
458
|
-
* serialized.
|
|
459
|
-
*/
|
|
460
|
-
setState(state: Record<string, any>): Promise<{ success: boolean; error?: string; code?: UsionErrorCode }>;
|
|
461
|
-
|
|
462
|
-
// Persisted state — survives iframe unmount/remount.
|
|
463
|
-
// Keyed by (player_id, room_id) in localStorage. Schema is up to the game.
|
|
464
|
-
saveState<T = any>(state: T): boolean;
|
|
465
|
-
loadState<T = any>(): T | null;
|
|
466
|
-
clearState(): void;
|
|
467
|
-
|
|
468
|
-
// Forward a debug snapshot to the parent. Rendered as a top-right
|
|
469
|
-
// overlay over the iframe when the host page is opened with `?debug=1`.
|
|
470
|
-
debug(payload: Record<string, any>): void;
|
|
471
|
-
|
|
472
|
-
// Event handlers
|
|
473
|
-
onJoined(callback: (data: GameJoinResult) => void): UnsubscribeFn;
|
|
474
|
-
onPlayerJoined(callback: (data: PlayerJoinedData) => void): UnsubscribeFn;
|
|
475
|
-
onPlayerLeft(callback: (data: PlayerLeftData) => void): UnsubscribeFn;
|
|
476
|
-
onStateUpdate(callback: (data: StateUpdateData) => void): UnsubscribeFn;
|
|
477
|
-
onSync(callback: (data: SyncData) => void): UnsubscribeFn;
|
|
478
|
-
onAction(callback: (data: GameActionData) => void): UnsubscribeFn;
|
|
479
|
-
onRealtime(callback: (data: Record<string, any>) => void): UnsubscribeFn;
|
|
480
|
-
onGameFinished(callback: (data: GameFinishedData) => void): UnsubscribeFn;
|
|
481
|
-
onGameRestarted(callback: (data: Record<string, any>) => void): UnsubscribeFn;
|
|
482
|
-
onError(callback: (data: { message: string; code?: string }) => void): UnsubscribeFn;
|
|
483
|
-
onRematchRequest(callback: (data: RematchData) => void): UnsubscribeFn;
|
|
484
|
-
onDisconnect(callback: (reason: string) => void): UnsubscribeFn;
|
|
485
|
-
onReconnect(callback: (attemptNumber: number) => void): UnsubscribeFn;
|
|
486
|
-
onConnectionError(callback: (error: Error) => void): UnsubscribeFn;
|
|
487
|
-
/** Peer connection lifecycle: connected / reconnecting (grace) / gone. */
|
|
488
|
-
onPlayerConnection(callback: (data: PlayerConnectionData) => void): UnsubscribeFn;
|
|
489
|
-
/**
|
|
490
|
-
* Fired when the host assigns this app a room AFTER launch — the solo→host
|
|
491
|
-
* promotion path. The user opened the game solo (launch mode 'single'), then
|
|
492
|
-
* shared it from the host's top-bar Share button; the host created a room and
|
|
493
|
-
* the SDK has already updated `getLaunchParams().roomId`/`.mode` and is
|
|
494
|
-
* connecting+joining. Use this to flip a solo game into its multiplayer /
|
|
495
|
-
* hosting UI; `onJoined` fires right after the join completes.
|
|
496
|
-
*/
|
|
497
|
-
onRoomAssigned(callback: (data: { roomId: string }) => void): UnsubscribeFn;
|
|
498
|
-
|
|
499
|
-
// ─── Reconnection lifecycle (SDK ≥ 2.21; documented since 2.16) ──────────
|
|
500
|
-
/** Highest action sequence seen (joins, syncs, live actions). */
|
|
501
|
-
getLastSequence(): number;
|
|
502
|
-
/** Highest action sequence applied locally; trails while catching up. */
|
|
503
|
-
getLastAppliedSequence(): number;
|
|
504
|
-
/** Current connection state, synchronously. */
|
|
505
|
-
getConnectionState(): GameConnectionState;
|
|
506
|
-
/**
|
|
507
|
-
* Observe connection-state transitions ('connected' → 'disconnected' →
|
|
508
|
-
* 'rejoining' → 'reconnected' → 'connected') — identical across the socket,
|
|
509
|
-
* embedded-proxy, and direct transports. One hook to drive a
|
|
510
|
-
* "Reconnecting…" overlay and gate input.
|
|
511
|
-
*/
|
|
512
|
-
onConnectionState(callback: (state: GameConnectionState) => void): UnsubscribeFn;
|
|
513
|
-
/**
|
|
514
|
-
* Fires once per reconnect, after the resync completes. Restore local
|
|
515
|
-
* state here — it does NOT fire for a manually-requested sync.
|
|
516
|
-
*/
|
|
517
|
-
onReconnected(callback: (info: ReconnectedInfo) => void): UnsubscribeFn;
|
|
518
|
-
/**
|
|
519
|
-
* Reconnect-safe authoritative shared state. Commits are sequenced actions
|
|
520
|
-
* applied through `reduce` on every client in the same order (deduped by
|
|
521
|
-
* sequence); the authority (player_ids[0] by default) auto-checkpoints via
|
|
522
|
-
* setState, and (re)joining clients recover automatically (load checkpoint,
|
|
523
|
-
* replay the un-checkpointed tail). Use for state that must survive a
|
|
524
|
-
* disconnect — unlike replicate() (realtime, lossy). Degrades to
|
|
525
|
-
* local-apply in direct mode (no platform checkpoint there).
|
|
526
|
-
*/
|
|
527
|
-
syncedState<S = Record<string, any>>(initial: S, opts?: SyncedStateOptions<S>): SyncedState<S>;
|
|
528
|
-
|
|
529
|
-
/**
|
|
530
|
-
* Register an ADDITIONAL event listener. Unlike the onX methods this
|
|
531
|
-
* supports multiple listeners per event, can be called before connect(),
|
|
532
|
-
* and works in every transport. Accepts internal names ('action'), wire
|
|
533
|
-
* names ('game:action'), or snake_case ('player_joined').
|
|
534
|
-
* @returns Unsubscribe function.
|
|
535
|
-
*/
|
|
536
|
-
on(event: string, callback: (data: any) => void): UnsubscribeFn;
|
|
537
|
-
|
|
538
|
-
// ─── Netcode helpers (low-latency realtime) ──────────────────
|
|
539
|
-
// JSON state delta compression.
|
|
540
|
-
diff(prev: any, next: any): any | undefined;
|
|
541
|
-
patch(base: any, delta: any): any;
|
|
542
|
-
/** Round numeric fields to a fixed precision (snapshot compression). */
|
|
543
|
-
quantize<T = any>(value: T, precision?: number): T;
|
|
544
|
-
/** Compact binary codec (use on direct/mesh transports). */
|
|
545
|
-
encode(value: any): Uint8Array;
|
|
546
|
-
decode(buf: ArrayBuffer | Uint8Array): any;
|
|
547
|
-
|
|
548
|
-
/** Snapshot interpolation buffer (adaptive buffer + capped extrapolation). */
|
|
549
|
-
createInterpolation(opts?: InterpolationOptions): SnapshotInterpolation;
|
|
550
|
-
/** Client-side prediction + reconciliation + error smoothing. */
|
|
551
|
-
createPredictor<S = any, I = any>(opts: PredictorOptions<S, I>): Predictor<S, I>;
|
|
552
|
-
/** Fixed-rate outbound coalescer (defaults to sending via game.realtime). */
|
|
553
|
-
createSender(opts?: SenderOptions): Coalescer;
|
|
554
|
-
/** Sequence-guarded, delta-compressed snapshot sender. Pair with createSnapshotReceiver. */
|
|
555
|
-
createSnapshotSender(opts?: SnapshotSenderOptions): SnapshotSender;
|
|
556
|
-
/** Receiver for createSnapshotSender (drops stale/out-of-order frames). */
|
|
557
|
-
createSnapshotReceiver(opts?: SnapshotReceiverOptions): SnapshotReceiver;
|
|
558
|
-
/** One-line WebRTC P2P (2-peer) setup; signaling over realtime. */
|
|
559
|
-
createMesh(opts: MeshOptions): MeshConnection;
|
|
560
|
-
/** N-peer full mesh; signaling routed per-peer over realtime. */
|
|
561
|
-
createMeshNetwork(opts?: MeshNetworkOptions): MeshNetwork;
|
|
562
|
-
/** WebTransport (HTTP/3) connection — lowest-latency client-server datagrams. */
|
|
563
|
-
createWebTransport(opts?: WebTransportOptions): WebTransportConnection;
|
|
564
|
-
/** Declarative replication (host): mutate the object, it auto-syncs. */
|
|
565
|
-
replicate<T = any>(obj: T, opts?: ReplicateOptions): Replicated<T>;
|
|
566
|
-
/** Declarative replication (client): receive + (optionally) interpolate. */
|
|
567
|
-
replica<S = any>(opts?: ReplicaOptions): Replica<S>;
|
|
568
|
-
/** Server-side lag compensation (history + rewind) for fair hit-resolution. */
|
|
569
|
-
createLagCompensator(opts?: LagCompensatorOptions): LagCompensator;
|
|
570
|
-
/** Deterministic lockstep (inputs-only sim + free replays). */
|
|
571
|
-
createLockstep(opts: LockstepOptions): Lockstep;
|
|
572
|
-
/** Spatial hash for area-of-interest filtering in world rooms. (SDK ≥ 2.23) */
|
|
573
|
-
createInterestGrid(opts?: InterestGridOptions): InterestGrid;
|
|
574
|
-
/** Inject latency/jitter/loss locally to test under bad networks (null = off). */
|
|
575
|
-
simulateNetwork(opts: NetworkSimOptions | null): NetworkSim | void;
|
|
576
|
-
|
|
577
|
-
/** Measure round-trip time once (single outstanding probe); ms or null. */
|
|
578
|
-
ping(): Promise<number | null>;
|
|
579
|
-
/** Latest smoothed round-trip time in ms (null until first ping). */
|
|
580
|
-
getRtt(): number | null;
|
|
581
|
-
|
|
582
|
-
// ─── Network resilience (SDK ≥ 2.24) ─────────────────────────
|
|
583
|
-
/**
|
|
584
|
-
* Cheap snapshot of connection health: transport, connection state,
|
|
585
|
-
* quality, smoothed rtt/jitter, age of the last inbound direct frame,
|
|
586
|
-
* and buffered (unsent) bytes. Works in every transport; relay mode
|
|
587
|
-
* reports rtt/jitter null unless game.ping() measured them.
|
|
588
|
-
*/
|
|
589
|
-
getNetworkStats(): NetworkStats;
|
|
590
|
-
/**
|
|
591
|
-
* Observe network-quality TRANSITIONS ('good'/'fair'/'poor'/'dead') —
|
|
592
|
-
* fires only when the quality level changes, with { quality, prev, stats }.
|
|
593
|
-
* Drive a connection-quality indicator or degrade send rates from here.
|
|
594
|
-
*/
|
|
595
|
-
onNetworkQuality(callback: (change: NetworkQualityChange) => void): UnsubscribeFn;
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
// ─── Netcode toolkit ──────────────────────────────────────────────
|
|
599
|
-
|
|
600
|
-
export interface NetcodeEntity {
|
|
601
|
-
id: string | number;
|
|
602
|
-
[key: string]: any;
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
export interface Snapshot {
|
|
606
|
-
id?: string;
|
|
607
|
-
time?: number;
|
|
608
|
-
state: NetcodeEntity[] | { [group: string]: NetcodeEntity[] };
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
export interface InterpolationOptions {
|
|
612
|
-
/** Expected snapshot rate; sets the default buffer (default 20). */
|
|
613
|
-
serverFps?: number;
|
|
614
|
-
/** Fixed interpolation delay in ms (default ≈ 3 server frames). */
|
|
615
|
-
bufferMs?: number;
|
|
616
|
-
/** Grow the buffer with measured jitter (Source cl_interp_ratio style). */
|
|
617
|
-
adaptive?: boolean;
|
|
618
|
-
/** Adaptive buffer clamps. */
|
|
619
|
-
minBufferMs?: number;
|
|
620
|
-
maxBufferMs?: number;
|
|
621
|
-
/** Max forward extrapolation on buffer underrun, ms (0 = off; Source caps ~250). */
|
|
622
|
-
extrapolationMs?: number;
|
|
623
|
-
/** Render against snapshot.time (server clock domain) instead of arrival time. */
|
|
624
|
-
serverTime?: boolean;
|
|
625
|
-
/** Snapshot history depth (default 120). */
|
|
626
|
-
maxSize?: number;
|
|
627
|
-
/** Clock source override (default Date.now). */
|
|
628
|
-
now?: () => number;
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
export class SnapshotInterpolation {
|
|
632
|
-
constructor(opts?: InterpolationOptions);
|
|
633
|
-
getBufferMs(): number;
|
|
634
|
-
setBufferMs(ms: number): void;
|
|
635
|
-
getJitter(): number;
|
|
636
|
-
add(snapshot: Snapshot | NetcodeEntity[] | { [group: string]: NetcodeEntity[] }): void;
|
|
637
|
-
/** Interpolated entities for the render instant, or null if no data yet. */
|
|
638
|
-
calc(keys: string, group?: string): NetcodeEntity[] | null;
|
|
639
|
-
vault: Vault;
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
export class Vault {
|
|
643
|
-
constructor(maxSize?: number);
|
|
644
|
-
add(snapshot: { time: number; state: any }): void;
|
|
645
|
-
readonly size: number;
|
|
646
|
-
setMaxSize(n: number): void;
|
|
647
|
-
latest(): { time: number; state: any } | null;
|
|
648
|
-
clear(): void;
|
|
649
|
-
straddle(time: number): [any, any];
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
export interface PredictorSmoothOptions {
|
|
653
|
-
keys?: string | string[];
|
|
654
|
-
rate?: number;
|
|
655
|
-
snapTo?: number;
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
export interface PredictorOptions<S = any, I = any> {
|
|
659
|
-
apply: (state: S, input: I) => S;
|
|
660
|
-
initialState?: S;
|
|
661
|
-
/** Smoothly blend correction errors instead of snapping (Overwatch style). */
|
|
662
|
-
smooth?: PredictorSmoothOptions | string;
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
export class Predictor<S = any, I = any> {
|
|
666
|
-
constructor(opts: PredictorOptions<S, I>);
|
|
667
|
-
readonly state: S;
|
|
668
|
-
readonly pending: Array<{ seq: number; input: I }>;
|
|
669
|
-
readonly lastSeq: number;
|
|
670
|
-
predict(input: I): { seq: number; state: S };
|
|
671
|
-
reconcile(serverState: S, ackedSeq: number): S;
|
|
672
|
-
/** Render state: corrected + decaying error offset. Call once per frame. */
|
|
673
|
-
view(rate?: number): S;
|
|
674
|
-
reset(state?: S): void;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
export interface CoalescerOptions {
|
|
678
|
-
hz?: number;
|
|
679
|
-
onFlush: (entries: Array<{ type: string; data: any }>) => void;
|
|
680
|
-
autoStart?: boolean;
|
|
681
|
-
setInterval?: (fn: () => void, ms: number) => any;
|
|
682
|
-
clearInterval?: (handle: any) => void;
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
export class Coalescer {
|
|
686
|
-
constructor(opts: CoalescerOptions);
|
|
687
|
-
readonly running: boolean;
|
|
688
|
-
/** Latest-wins: only the newest data for `type` is sent next flush. */
|
|
689
|
-
queue(type: string, data: any): void;
|
|
690
|
-
/** Buffered: every value for `type` is kept and sent next flush. */
|
|
691
|
-
append(type: string, data: any): void;
|
|
692
|
-
drain(): Array<{ type: string; data: any }>;
|
|
693
|
-
flush(): void;
|
|
694
|
-
start(): void;
|
|
695
|
-
stop(): void;
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
export interface SenderOptions {
|
|
699
|
-
hz?: number;
|
|
700
|
-
autoStart?: boolean;
|
|
701
|
-
send?: (type: string, data: any) => void;
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
export interface SnapshotSenderOptions extends SenderOptions {
|
|
705
|
-
/** realtime action_type to send under (default 'state'). */
|
|
706
|
-
channel?: string;
|
|
707
|
-
/** Use delta compression with periodic keyframes (default true). */
|
|
708
|
-
delta?: boolean;
|
|
709
|
-
/** Send a full keyframe every N deltas so lost deltas self-heal (default 30). */
|
|
710
|
-
keyframeEvery?: number;
|
|
711
|
-
/** Quantize numeric fields to this many decimals before diffing. */
|
|
712
|
-
precision?: number;
|
|
713
|
-
/** Binary-encode the wire payload: true (built-in) or a custom encoder. */
|
|
714
|
-
encode?: boolean | ((payload: any) => any);
|
|
715
|
-
/** Auto-read state each tick from this getter (basis for game.replicate). */
|
|
716
|
-
source?: () => any;
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
export interface ReplicateOptions extends SnapshotSenderOptions {}
|
|
720
|
-
|
|
721
|
-
export interface Replicated<T = any> {
|
|
722
|
-
state: T;
|
|
723
|
-
flush(): void;
|
|
724
|
-
start(): void;
|
|
725
|
-
stop(): void;
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
export interface ReplicaOptions {
|
|
729
|
-
channel?: string;
|
|
730
|
-
decode?: boolean | ((buf: any) => any);
|
|
731
|
-
/** Interpolation keys ('x y') or an InterpolationOptions object to smooth entities. */
|
|
732
|
-
interpolate?: string | InterpolationOptions;
|
|
733
|
-
keys?: string;
|
|
734
|
-
group?: string;
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
export interface Replica<S = any> {
|
|
738
|
-
readonly state: S;
|
|
739
|
-
view(): any;
|
|
740
|
-
onChange(cb: (state: S) => void): void;
|
|
741
|
-
stop(): void;
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
export interface NetworkSimOptions {
|
|
745
|
-
latencyMs?: number;
|
|
746
|
-
jitterMs?: number;
|
|
747
|
-
lossPct?: number;
|
|
748
|
-
dupPct?: number;
|
|
749
|
-
setTimeout?: (fn: () => void, ms: number) => any;
|
|
750
|
-
clearTimeout?: (handle: any) => void;
|
|
751
|
-
random?: () => number;
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
export class NetworkSim {
|
|
755
|
-
constructor(opts?: NetworkSimOptions);
|
|
756
|
-
latencyMs: number; jitterMs: number; lossPct: number; dupPct: number;
|
|
757
|
-
set(opts: NetworkSimOptions): NetworkSim;
|
|
758
|
-
wrap<F extends (...args: any[]) => any>(fn: F): F;
|
|
759
|
-
flush(): void;
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
export interface LockstepOptions {
|
|
763
|
-
playerId: string;
|
|
764
|
-
players: string[];
|
|
765
|
-
step: (frame: number, inputs: Record<string, any>) => void;
|
|
766
|
-
send?: (msg: { frame: number; playerId: string; input: any }) => void;
|
|
767
|
-
inputDelay?: number;
|
|
768
|
-
idleInput?: any;
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
export class Lockstep {
|
|
772
|
-
constructor(opts: LockstepOptions);
|
|
773
|
-
readonly frame: number;
|
|
774
|
-
readonly players: string[];
|
|
775
|
-
submit(input: any): number;
|
|
776
|
-
receive(msg: { frame: number; playerId: string; input: any }): void;
|
|
777
|
-
tick(): number;
|
|
778
|
-
isStalled(): boolean;
|
|
779
|
-
getReplay(): Array<{ frame: number; inputs: Record<string, any> }>;
|
|
780
|
-
static replay(log: Array<{ frame: number; inputs: Record<string, any> }>, step: (frame: number, inputs: Record<string, any>) => void): void;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
export interface LagCompensatorOptions {
|
|
784
|
-
historyMs?: number;
|
|
785
|
-
maxSize?: number;
|
|
786
|
-
now?: () => number;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
export class LagCompensator {
|
|
790
|
-
constructor(opts?: LagCompensatorOptions);
|
|
791
|
-
readonly size: number;
|
|
792
|
-
record(entities: Array<{ id: string | number; [k: string]: any }>, time?: number): void;
|
|
793
|
-
rewind(time: number, keys?: string[]): Record<string, any>;
|
|
794
|
-
rewindForClient(rttMs: number, interpBufferMs: number, opts?: { maxRewindMs?: number; keys?: string[] }): Record<string, any>;
|
|
795
|
-
clear(): void;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
export interface SnapshotSender {
|
|
799
|
-
send(state: any): void;
|
|
800
|
-
flush(): void;
|
|
801
|
-
start(): void;
|
|
802
|
-
stop(): void;
|
|
803
|
-
reset(): void;
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
export interface SnapshotReceiverOptions {
|
|
807
|
-
/** Binary-decode incoming payloads: true (built-in) or a custom decoder. */
|
|
808
|
-
decode?: boolean | ((buf: any) => any);
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
export interface SnapshotReceiver {
|
|
812
|
-
/** Apply a message from the sender; returns the reconstructed state. */
|
|
813
|
-
receive<S = any>(msg: any): S;
|
|
814
|
-
readonly state: any;
|
|
815
|
-
readonly stats: { appliedSeq: number; baseSeq: number; dropped: number };
|
|
816
|
-
reset(): void;
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
export interface PingMeterOptions {
|
|
820
|
-
alpha?: number;
|
|
821
|
-
now?: () => number;
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
export class PingMeter {
|
|
825
|
-
constructor(opts?: PingMeterOptions);
|
|
826
|
-
readonly rtt: number | null;
|
|
827
|
-
readonly latency: number | null;
|
|
828
|
-
readonly jitter: number;
|
|
829
|
-
readonly last: number | null;
|
|
830
|
-
begin(): number;
|
|
831
|
-
end(id: number): number | null;
|
|
832
|
-
sample(rttMs: number): number | null;
|
|
833
|
-
reset(): void;
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
export interface IceServerConfig {
|
|
837
|
-
stun?: string;
|
|
838
|
-
turn?: string;
|
|
839
|
-
turnUsername?: string;
|
|
840
|
-
turnCredential?: string;
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
export interface MeshOptions {
|
|
844
|
-
role: 'host' | 'guest';
|
|
845
|
-
/** realtime action_type used for signaling (default 'signal'). */
|
|
846
|
-
signalChannel?: string;
|
|
847
|
-
iceServers?: RTCIceServer[];
|
|
848
|
-
/** Drop stale/out-of-order frames on the unreliable channel (default true). */
|
|
849
|
-
sequenced?: boolean;
|
|
850
|
-
/** Recover via ICE restart on connection failure (host; default true). */
|
|
851
|
-
autoReconnect?: boolean;
|
|
852
|
-
maxRestarts?: number;
|
|
853
|
-
RTCPeerConnection?: typeof RTCPeerConnection;
|
|
854
|
-
setTimeout?: (fn: () => void, ms: number) => any;
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
export class MeshConnection {
|
|
858
|
-
constructor(opts: MeshOptions & { sendSignal: (payload: any) => void });
|
|
859
|
-
/** Build an iceServers array with optional TURN relay. */
|
|
860
|
-
static iceServers(cfg?: IceServerConfig): RTCIceServer[];
|
|
861
|
-
readonly connected: boolean;
|
|
862
|
-
readonly role: 'host' | 'guest';
|
|
863
|
-
onOpen: (() => void) | null;
|
|
864
|
-
onMessage: ((data: any, channel: 'unreliable' | 'reliable') => void) | null;
|
|
865
|
-
onClose: (() => void) | null;
|
|
866
|
-
onError: ((err: any) => void) | null;
|
|
867
|
-
onStateChange: ((state: string) => void) | null;
|
|
868
|
-
start(): Promise<void>;
|
|
869
|
-
handleSignal(payload: any): Promise<void>;
|
|
870
|
-
send(data: any): boolean;
|
|
871
|
-
sendReliable(data: any): boolean;
|
|
872
|
-
close(): void;
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
export interface MeshNetworkOptions {
|
|
876
|
-
selfId?: string;
|
|
877
|
-
signalChannel?: string;
|
|
878
|
-
iceServers?: RTCIceServer[];
|
|
879
|
-
sequenced?: boolean;
|
|
880
|
-
autoReconnect?: boolean;
|
|
881
|
-
RTCPeerConnection?: typeof RTCPeerConnection;
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
export class MeshNetwork {
|
|
885
|
-
constructor(opts: MeshNetworkOptions & { selfId: string; sendSignal: (toPeerId: string, payload: any) => void });
|
|
886
|
-
readonly selfId: string;
|
|
887
|
-
readonly peerIds: string[];
|
|
888
|
-
readonly connectedCount: number;
|
|
889
|
-
peer(peerId: string): MeshConnection | null;
|
|
890
|
-
addPeer(peerId: string): Promise<MeshConnection | null>;
|
|
891
|
-
setRoster(peerIds: string[]): Promise<void>;
|
|
892
|
-
removePeer(peerId: string): void;
|
|
893
|
-
handleSignal(fromPeerId: string, payload: any): Promise<void>;
|
|
894
|
-
send(peerId: string, data: any): boolean;
|
|
895
|
-
sendReliable(peerId: string, data: any): boolean;
|
|
896
|
-
broadcast(data: any): void;
|
|
897
|
-
broadcastReliable(data: any): void;
|
|
898
|
-
onPeerOpen: ((peerId: string) => void) | null;
|
|
899
|
-
onPeerClose: ((peerId: string) => void) | null;
|
|
900
|
-
onMessage: ((peerId: string, data: any, channel: 'unreliable' | 'reliable') => void) | null;
|
|
901
|
-
onError: ((peerId: string, err: any) => void) | null;
|
|
902
|
-
close(): void;
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
export interface WebTransportOptions {
|
|
906
|
-
/** https:// HTTP/3 endpoint (defaults to Usion.config.webTransportUrl). */
|
|
907
|
-
url?: string;
|
|
908
|
-
/** For self-signed dev certificates. */
|
|
909
|
-
serverCertificateHashes?: Array<{ algorithm: string; value: BufferSource }>;
|
|
910
|
-
/** Drop stale/out-of-order datagrams (default true). */
|
|
911
|
-
sequenced?: boolean;
|
|
912
|
-
/** Injectable WebTransport constructor (tests). */
|
|
913
|
-
WebTransport?: any;
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
export class WebTransportConnection {
|
|
917
|
-
constructor(opts: WebTransportOptions & { url: string });
|
|
918
|
-
readonly connected: boolean;
|
|
919
|
-
onOpen: (() => void) | null;
|
|
920
|
-
onMessage: ((data: any, channel: 'datagram' | 'reliable') => void) | null;
|
|
921
|
-
onClose: (() => void) | null;
|
|
922
|
-
onError: ((err: any) => void) | null;
|
|
923
|
-
connect(): Promise<void>;
|
|
924
|
-
/** Send over the unreliable datagram channel (sequenced). */
|
|
925
|
-
send(data: any): boolean;
|
|
926
|
-
/** Send over the reliable ordered stream. */
|
|
927
|
-
sendReliable(data: any): boolean;
|
|
928
|
-
close(): void;
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
// ─── Interest grid (area-of-interest spatial hash, SDK ≥ 2.23) ───
|
|
932
|
-
|
|
933
|
-
export interface InterestGridOptions {
|
|
934
|
-
/** Cell size in world units (default 256); pick ≈ the typical query radius. */
|
|
935
|
-
cellSize?: number;
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
/** Per-observer visibility diff from InterestGrid.observe(). */
|
|
939
|
-
export interface InterestGridObservation {
|
|
940
|
-
/** Ids visible now that were not visible on this observer's previous observe(). */
|
|
941
|
-
entered: Array<string | number>;
|
|
942
|
-
/** Ids visible on the previous observe() that no longer are. */
|
|
943
|
-
left: Array<string | number>;
|
|
944
|
-
/** All ids currently within the radius. */
|
|
945
|
-
visible: Array<string | number>;
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
/**
|
|
949
|
-
* Spatial hash for area-of-interest filtering: O(cells touched) proximity
|
|
950
|
-
* queries plus per-observer enter/leave diffs. Pure JS (browser and Node) —
|
|
951
|
-
* the AOI building block for world-scale rooms.
|
|
952
|
-
*/
|
|
953
|
-
export interface InterestGrid {
|
|
954
|
-
/** Insert an entity or move an existing one. */
|
|
955
|
-
upsert(id: string | number, x: number, y: number): void;
|
|
956
|
-
/** Remove an entity (unknown ids are a no-op). */
|
|
957
|
-
remove(id: string | number): void;
|
|
958
|
-
/** Entity ids within `radius` of (x, y) — a true circle test. */
|
|
959
|
-
query(x: number, y: number, radius: number): Array<string | number>;
|
|
960
|
-
/** query() diffed against this observer's previous call. */
|
|
961
|
-
observe(observerId: string | number, x: number, y: number, radius: number): InterestGridObservation;
|
|
962
|
-
/** Free an observer's tracking state (call when a player leaves). */
|
|
963
|
-
dropObserver(observerId: string | number): void;
|
|
964
|
-
/** Number of entities in the grid. */
|
|
965
|
-
size(): number;
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
export interface NetcodeModule {
|
|
969
|
-
SnapshotInterpolation: typeof SnapshotInterpolation;
|
|
970
|
-
Vault: typeof Vault;
|
|
971
|
-
Predictor: typeof Predictor;
|
|
972
|
-
Coalescer: typeof Coalescer;
|
|
973
|
-
PingMeter: typeof PingMeter;
|
|
974
|
-
MeshConnection: typeof MeshConnection;
|
|
975
|
-
MeshNetwork: typeof MeshNetwork;
|
|
976
|
-
WebTransportConnection: typeof WebTransportConnection;
|
|
977
|
-
NetworkSim: typeof NetworkSim;
|
|
978
|
-
Lockstep: typeof Lockstep;
|
|
979
|
-
LagCompensator: typeof LagCompensator;
|
|
980
|
-
diff(prev: any, next: any): any | undefined;
|
|
981
|
-
patch(base: any, delta: any): any;
|
|
982
|
-
quantize<T = any>(value: T, precision?: number): T;
|
|
983
|
-
encode(value: any): Uint8Array;
|
|
984
|
-
decode(buf: ArrayBuffer | Uint8Array): any;
|
|
985
|
-
/** Spatial hash for area-of-interest filtering in world rooms. (SDK ≥ 2.23) */
|
|
986
|
-
createInterestGrid(opts?: InterestGridOptions): InterestGrid;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
// ─── Results ────────────────────────────────────────────────────
|
|
990
|
-
|
|
991
|
-
export interface SavedResult {
|
|
992
|
-
id: string;
|
|
993
|
-
data: string;
|
|
994
|
-
metadata?: {
|
|
995
|
-
thumbnail_url?: string;
|
|
996
|
-
title?: string;
|
|
997
|
-
type?: string;
|
|
998
|
-
};
|
|
999
|
-
created_at?: string;
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
// ─── Bot ────────────────────────────────────────────────────────
|
|
1003
|
-
|
|
1004
|
-
export interface BotMessage {
|
|
1005
|
-
id: string;
|
|
1006
|
-
content: string;
|
|
1007
|
-
content_type?: string;
|
|
1008
|
-
components?: any[];
|
|
1009
|
-
sender_id: string;
|
|
1010
|
-
}
|
|
1011
|
-
|
|
1012
|
-
export interface BotModule {
|
|
1013
|
-
callAction(action: string, data?: Record<string, any>): Promise<any>;
|
|
1014
|
-
sendMessage(text: string): void;
|
|
1015
|
-
updateContext(ctx: Record<string, any>): void;
|
|
1016
|
-
close(result?: any): void;
|
|
1017
|
-
onMessage(callback: (message: BotMessage) => void): UnsubscribeFn;
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
// ─── Selection Grid ──────────────────────────────────────────────
|
|
1021
|
-
|
|
1022
|
-
export interface SelectionGrid {
|
|
1023
|
-
getSelected(): string | null;
|
|
1024
|
-
clear(): void;
|
|
1025
|
-
}
|
|
1026
|
-
|
|
1027
|
-
// ─── Main SDK ────────────────────────────────────────────────────
|
|
1028
|
-
|
|
1029
|
-
export interface UsionSDK {
|
|
1030
|
-
version: string;
|
|
1031
|
-
config: UsionConfig;
|
|
1032
|
-
|
|
1033
|
-
// Lifecycle
|
|
1034
|
-
/**
|
|
1035
|
-
* Initialize the SDK. The callback fires when the host's INIT config
|
|
1036
|
-
* arrives; every call also returns a Promise of the same config, so
|
|
1037
|
-
* `const config = await Usion.init()` works. `opts.timeout` (ms) rejects
|
|
1038
|
-
* the promise with UsionError(INIT_TIMEOUT) when no INIT arrives in time
|
|
1039
|
-
* (embedded but host silent). (promise/timeout: SDK ≥ 2.22)
|
|
1040
|
-
*/
|
|
1041
|
-
init(callback?: (config: UsionConfig) => void, opts?: { timeout?: number }): Promise<UsionConfig>;
|
|
1042
|
-
init(opts: { timeout?: number }): Promise<UsionConfig>;
|
|
1043
|
-
exit(options?: { backCount?: number }): void;
|
|
1044
|
-
|
|
1045
|
-
// Theme & Locale
|
|
1046
|
-
getTheme(): 'light' | 'dark';
|
|
1047
|
-
getLanguage(): string;
|
|
1048
|
-
|
|
1049
|
-
/**
|
|
1050
|
-
* Launch parameters the host opened this app with. `path` is the deep-link
|
|
1051
|
-
* target (e.g. from a tapped `Usion.notify` notification); route to it after
|
|
1052
|
-
* init so the user lands on the right screen. `mode` is `'single'` (opened
|
|
1053
|
-
* from Explore / the Game hub, solo) or `'multiplayer'` (opened from a game
|
|
1054
|
-
* invite in a chat) — branch game setup on it. (mode: SDK ≥ 2.18)
|
|
1055
|
-
*/
|
|
1056
|
-
getLaunchParams(): { path: string | null; ref: string | null; roomId: string | null; mode: 'single' | 'multiplayer' };
|
|
1057
|
-
|
|
1058
|
-
// Payments
|
|
1059
|
-
requestPayment(amount: number, reason: string, data?: PaymentOptions): Promise<PaymentResponse>;
|
|
1060
|
-
|
|
1061
|
-
// Service communication
|
|
1062
|
-
submit(data: Record<string, any>): void;
|
|
1063
|
-
error(message: string): void;
|
|
1064
|
-
|
|
1065
|
-
// Results (server-side persistence)
|
|
1066
|
-
saveResult(data: string, metadata?: { thumbnail_url?: string; title?: string; type?: string; [key: string]: unknown }): Promise<SavedResult>;
|
|
1067
|
-
deleteResult(resultId: string): Promise<void>;
|
|
1068
|
-
getResults(): SavedResult[];
|
|
1069
|
-
|
|
1070
|
-
// Back button
|
|
1071
|
-
claimBackButton(callback: () => void): void;
|
|
1072
|
-
releaseBackButton(): void;
|
|
1073
|
-
|
|
1074
|
-
// Download
|
|
1075
|
-
download(url: string, filename?: string): Promise<{ success?: boolean; error?: string }>;
|
|
1076
|
-
|
|
1077
|
-
// Sharing
|
|
1078
|
-
share(contentType: 'audio' | 'image' | 'video' | 'text' | 'mixed', data: ShareData): void;
|
|
1079
|
-
shareToFeed(contentType: 'text' | 'image' | 'video' | 'audio' | 'mixed', data: {
|
|
1080
|
-
text?: string;
|
|
1081
|
-
media?: Array<{ type: 'image' | 'video' | 'audio'; url: string; thumbnailUrl?: string; width?: number; height?: number; duration?: number }>;
|
|
1082
|
-
}): Promise<{ success: boolean; postId?: string; shareUrl?: string }>;
|
|
1083
|
-
|
|
1084
|
-
// Logging
|
|
1085
|
-
log(msg: string): void;
|
|
1086
|
-
|
|
1087
|
-
// Generic event listener (returns an unsubscribe function)
|
|
1088
|
-
on(type: string, callback: (data: any) => void): UnsubscribeFn;
|
|
1089
|
-
|
|
1090
|
-
// UI helpers
|
|
1091
|
-
setLoading(btn: HTMLElement | string, loading: boolean): void;
|
|
1092
|
-
toggle(el: HTMLElement | string, show: boolean): void;
|
|
1093
|
-
charCount(input: HTMLElement | string, counter: HTMLElement | string, max: number): void;
|
|
1094
|
-
selectionGrid(
|
|
1095
|
-
containerSelector: string,
|
|
1096
|
-
itemSelector: string,
|
|
1097
|
-
onChange: (selected: string, item: HTMLElement) => void
|
|
1098
|
-
): SelectionGrid;
|
|
1099
|
-
|
|
1100
|
-
/** SDK version (injected from package.json at build time). */
|
|
1101
|
-
version: string;
|
|
1102
|
-
|
|
1103
|
-
/**
|
|
1104
|
-
* Live diagnostics snapshot: version, transport, connection + sequence
|
|
1105
|
-
* state. game.debug() attaches this automatically as payload._diag.
|
|
1106
|
-
*/
|
|
1107
|
-
diagnostics(): {
|
|
1108
|
-
version: string;
|
|
1109
|
-
transport: 'socket' | 'proxy' | 'direct' | 'none';
|
|
1110
|
-
connected: boolean;
|
|
1111
|
-
joined: boolean;
|
|
1112
|
-
roomId: string | null;
|
|
1113
|
-
playerId: string | null;
|
|
1114
|
-
lastSequence: number;
|
|
1115
|
-
lastActionApplied: number;
|
|
1116
|
-
rejoining: boolean;
|
|
1117
|
-
};
|
|
1118
|
-
|
|
1119
|
-
// Errors — branch on err.code (stable), never on message text.
|
|
1120
|
-
UsionError: typeof UsionError;
|
|
1121
|
-
ERROR_CODES: typeof ERROR_CODES;
|
|
1122
|
-
|
|
1123
|
-
// Modules
|
|
1124
|
-
user: UserModule;
|
|
1125
|
-
storage: StorageModule;
|
|
1126
|
-
fileStorage: FileStorageModule;
|
|
1127
|
-
wallet: WalletModule;
|
|
1128
|
-
session: SessionModule;
|
|
1129
|
-
chat: ChatModule;
|
|
1130
|
-
bot: BotModule;
|
|
1131
|
-
game: GameModule;
|
|
1132
|
-
lobby: LobbyModule;
|
|
1133
|
-
leaderboard: LeaderboardModule;
|
|
1134
|
-
matchmaking: MatchmakingModule;
|
|
1135
|
-
cloud: CloudModule;
|
|
1136
|
-
notify: NotifyModule;
|
|
1137
|
-
permissions: PermissionsModule;
|
|
1138
|
-
netcode: NetcodeModule;
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
/**
|
|
1142
|
-
* Send notifications to the CURRENT user that reopen this app (in-app banner
|
|
1143
|
-
* when online, OS push when offline). `path` deep-links to a screen, read back
|
|
1144
|
-
* via `Usion.getLaunchParams().path`. Rate-limited per user per service.
|
|
1145
|
-
*/
|
|
1146
|
-
export interface NotifyModule {
|
|
1147
|
-
/** Notify the current user. */
|
|
1148
|
-
send(opts: { title: string; body: string; path?: string; serviceId?: string }): Promise<{ success: boolean; delivered?: string }>;
|
|
1149
|
-
/** Mute/unmute notifications from this app for the current user. */
|
|
1150
|
-
setMuted(muted: boolean, opts?: { serviceId?: string }): Promise<{ success: boolean; muted: boolean }>;
|
|
1151
|
-
/** Whether the current user has muted this app's notifications. */
|
|
1152
|
-
isMuted(opts?: { serviceId?: string }): Promise<boolean>;
|
|
1153
|
-
}
|
|
1154
|
-
|
|
1155
|
-
/** Result of a permission request: per-key state + whether ALL were granted. */
|
|
1156
|
-
export interface PermissionResult {
|
|
1157
|
-
/** True only if every requested permission ended granted. */
|
|
1158
|
-
granted: boolean;
|
|
1159
|
-
/** Map of each requested permission key to its resulting state. */
|
|
1160
|
-
permissions: Record<string, boolean>;
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
/**
|
|
1164
|
-
* Ask the user to allow capabilities (the host shows a modal — like asking for
|
|
1165
|
-
* money). The platform enforces grants: e.g. `notify.send` is dropped until
|
|
1166
|
-
* `notifications` is granted. Embedded feature; standalone resolves "not
|
|
1167
|
-
* granted". Permission keys today: `'notifications'`.
|
|
1168
|
-
*/
|
|
1169
|
-
export interface PermissionsModule {
|
|
1170
|
-
/** Prompt the user to allow one or more permissions. */
|
|
1171
|
-
request(permissions: string | string[], opts?: { reason?: string }): Promise<PermissionResult>;
|
|
1172
|
-
/** Read current state without prompting; map of key -> granted. */
|
|
1173
|
-
query(permissions?: string | string[]): Promise<Record<string, boolean>>;
|
|
1174
|
-
/** Convenience: is a single permission currently granted? */
|
|
1175
|
-
has(permission: string): Promise<boolean>;
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
/** Scoped server-persisted KV operations (per-user or shared). */
|
|
1179
|
-
export interface CloudScope {
|
|
1180
|
-
/** Get a value; resolves to null when the key doesn't exist. */
|
|
1181
|
-
get(key: string, opts?: { serviceId?: string }): Promise<any>;
|
|
1182
|
-
/** Set a JSON-serializable value (max 64 KB). Keys: 1-128 chars of A-Za-z0-9_.-:/ */
|
|
1183
|
-
set(key: string, value: any, opts?: { serviceId?: string }): Promise<{ success: boolean; size: number }>;
|
|
1184
|
-
/** Remove a key. */
|
|
1185
|
-
remove(key: string, opts?: { serviceId?: string }): Promise<{ success: boolean; removed: boolean }>;
|
|
1186
|
-
/** List keys in this scope's bucket. */
|
|
1187
|
-
keys(opts?: { serviceId?: string }): Promise<string[]>;
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
/**
|
|
1191
|
-
* Server-persisted cloud storage. Per-user scope on the module itself
|
|
1192
|
-
* (cloud saves synced across devices); `shared` is one bucket per app that
|
|
1193
|
-
* all users read/write. Quotas: 64 KB/value, 200 keys + 1 MB per bucket.
|
|
1194
|
-
*/
|
|
1195
|
-
export interface CloudModule extends CloudScope {
|
|
1196
|
-
shared: CloudScope & {
|
|
1197
|
-
/** Atomically increment a shared numeric value (default delta 1). Resolves to the new value. */
|
|
1198
|
-
incr(key: string, delta?: number, opts?: { serviceId?: string }): Promise<number>;
|
|
1199
|
-
};
|
|
1200
|
-
}
|
|
1201
|
-
|
|
1202
|
-
export interface Match { roomId: string; players: string[]; serviceId?: string; }
|
|
1203
|
-
|
|
1204
|
-
export interface MatchmakingModule {
|
|
1205
|
-
/**
|
|
1206
|
-
* Join the quick-match queue; resolves when paired with online strangers.
|
|
1207
|
-
* `opts.timeout` (ms) bounds the wait: on expiry the queue is left and the
|
|
1208
|
-
* promise rejects with UsionError(MATCH_TIMEOUT). A newer find() rejects
|
|
1209
|
-
* the previous one with SUPERSEDED; cancel() rejects it with CANCELLED.
|
|
1210
|
-
* (timeout: SDK ≥ 2.22)
|
|
1211
|
-
*/
|
|
1212
|
-
find(serviceId?: string, opts?: { size?: number; timeout?: number }): Promise<Match>;
|
|
1213
|
-
/** Leave the queue / stop waiting. */
|
|
1214
|
-
cancel(): Promise<any>;
|
|
1215
|
-
/** Called whenever a match is found. */
|
|
1216
|
-
onMatch(cb: (match: Match) => void): void;
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
export interface LeaderboardEntry {
|
|
1220
|
-
user_id: string;
|
|
1221
|
-
name?: string | null;
|
|
1222
|
-
avatar?: string | null;
|
|
1223
|
-
score: number;
|
|
1224
|
-
rank: number;
|
|
1225
|
-
is_me: boolean;
|
|
1226
|
-
metadata?: any;
|
|
1227
|
-
}
|
|
1228
|
-
|
|
1229
|
-
export interface LeaderboardModule {
|
|
1230
|
-
/** Submit a score (best is kept per the service's order config). Opt-in per game. */
|
|
1231
|
-
submit(score: number, metadata?: any, opts?: { serviceId?: string }): Promise<{ success: boolean; score: number; best: number; rank: number; updated: boolean }>;
|
|
1232
|
-
/** Leaderboard of your accepted friends (plus you). */
|
|
1233
|
-
friends(opts?: { serviceId?: string; limit?: number }): Promise<LeaderboardEntry[]>;
|
|
1234
|
-
/** Global top N. */
|
|
1235
|
-
top(opts?: { serviceId?: string; limit?: number }): Promise<LeaderboardEntry[]>;
|
|
1236
|
-
/** Your own score + global rank. */
|
|
1237
|
-
me(opts?: { serviceId?: string }): Promise<{ score: number | null; rank: number | null; total: number; metadata?: any }>;
|
|
1238
|
-
}
|
|
1239
|
-
|
|
1240
|
-
export interface LobbyMember { id: string; name?: string; ready: boolean; }
|
|
1241
|
-
export interface LobbyState { code: string | null; host: string | null; status: string | null; members: LobbyMember[]; }
|
|
1242
|
-
|
|
1243
|
-
export interface LobbyModule {
|
|
1244
|
-
readonly state: LobbyState;
|
|
1245
|
-
onUpdate(cb: (state: { code: string; host: string; status: string; members: LobbyMember[] }) => void): void;
|
|
1246
|
-
onStarted(cb: (data: { room_id: string; by: string; player_ids?: string[] }) => void): void;
|
|
1247
|
-
create(opts?: { maxPlayers?: number; public?: boolean }): Promise<{ code: string }>;
|
|
1248
|
-
join(code: string): Promise<{ code: string }>;
|
|
1249
|
-
leave(): Promise<void>;
|
|
1250
|
-
setReady(ready?: boolean): Promise<any>;
|
|
1251
|
-
allReady(): boolean;
|
|
1252
|
-
isHost(): boolean;
|
|
1253
|
-
start(roomId: string): Promise<any>;
|
|
1254
|
-
queue(serviceId: string, opts?: { conversationId?: string }): Promise<any>;
|
|
1255
|
-
}
|
|
1256
|
-
|
|
1257
|
-
/**
|
|
1258
|
-
* Stable machine-readable error codes. Part of the public API: codes are
|
|
1259
|
-
* never removed within a major version. Messages may change — branch on
|
|
1260
|
-
* `err.code`, never on message text.
|
|
1261
|
-
*/
|
|
1262
|
-
export declare const ERROR_CODES: {
|
|
1263
|
-
readonly NOT_CONNECTED: 'NOT_CONNECTED';
|
|
1264
|
-
readonly NO_ROOM: 'NO_ROOM';
|
|
1265
|
-
readonly ROOM_NOT_FOUND: 'ROOM_NOT_FOUND';
|
|
1266
|
-
readonly NOT_PARTICIPANT: 'NOT_PARTICIPANT';
|
|
1267
|
-
readonly NOT_IN_ROOM: 'NOT_IN_ROOM';
|
|
1268
|
-
readonly NOT_AUTHORITY: 'NOT_AUTHORITY';
|
|
1269
|
-
readonly NOT_AUTHENTICATED: 'NOT_AUTHENTICATED';
|
|
1270
|
-
readonly JOIN_TIMEOUT: 'JOIN_TIMEOUT';
|
|
1271
|
-
readonly CONNECT_TIMEOUT: 'CONNECT_TIMEOUT';
|
|
1272
|
-
readonly INIT_TIMEOUT: 'INIT_TIMEOUT';
|
|
1273
|
-
readonly MATCH_TIMEOUT: 'MATCH_TIMEOUT';
|
|
1274
|
-
readonly STATE_TOO_LARGE: 'STATE_TOO_LARGE';
|
|
1275
|
-
readonly INVALID_STATE: 'INVALID_STATE';
|
|
1276
|
-
readonly STALE_STATE: 'STALE_STATE';
|
|
1277
|
-
readonly INVALID_NEXT_TURN: 'INVALID_NEXT_TURN';
|
|
1278
|
-
readonly INVALID_INPUT: 'INVALID_INPUT';
|
|
1279
|
-
readonly NOT_FOUND: 'NOT_FOUND';
|
|
1280
|
-
readonly QUOTA_EXCEEDED: 'QUOTA_EXCEEDED';
|
|
1281
|
-
readonly VALUE_TOO_LARGE: 'VALUE_TOO_LARGE';
|
|
1282
|
-
readonly LOBBY_FULL: 'LOBBY_FULL';
|
|
1283
|
-
readonly LOBBY_CLOSED: 'LOBBY_CLOSED';
|
|
1284
|
-
readonly WORLD_FULL: 'WORLD_FULL';
|
|
1285
|
-
readonly CONFLICT: 'CONFLICT';
|
|
1286
|
-
readonly RATE_LIMITED: 'RATE_LIMITED';
|
|
1287
|
-
readonly REQUEST_TIMEOUT: 'REQUEST_TIMEOUT';
|
|
1288
|
-
readonly QUEUE_FULL: 'QUEUE_FULL';
|
|
1289
|
-
readonly CANCELLED: 'CANCELLED';
|
|
1290
|
-
readonly SUPERSEDED: 'SUPERSEDED';
|
|
1291
|
-
readonly UNSUPPORTED: 'UNSUPPORTED';
|
|
1292
|
-
readonly UNKNOWN: 'UNKNOWN';
|
|
1293
|
-
};
|
|
1294
|
-
|
|
1295
|
-
export type UsionErrorCode = keyof typeof ERROR_CODES;
|
|
1296
|
-
|
|
1297
|
-
export declare class UsionError extends Error {
|
|
1298
|
-
readonly name: 'UsionError';
|
|
1299
|
-
readonly code: UsionErrorCode;
|
|
1300
|
-
/** Seconds until a RATE_LIMITED call may be retried (when the server sent it). */
|
|
1301
|
-
readonly retryAfter?: number;
|
|
1302
|
-
constructor(code: UsionErrorCode | string, message?: string);
|
|
1303
|
-
}
|
|
1304
|
-
|
|
1305
|
-
declare const Usion: UsionSDK;
|
|
1306
|
-
|
|
1307
|
-
export { Usion };
|
|
1308
|
-
export default Usion;
|