@usions/sdk 2.24.1 → 2.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +13 -8
- package/src/browser.js +223 -84
- package/src/modules/core.js +10 -1
- package/src/modules/game-ack.js +32 -0
- package/src/modules/game-methods.js +107 -78
- package/src/modules/game-proxy.js +2 -0
- package/src/modules/game-socket.js +3 -1
- package/src/modules/host-tabs.js +66 -0
- package/src/modules/index.js +4 -0
- package/src/modules/leaderboard.js +4 -4
- package/types/index.d.ts +26 -1
package/README.md
CHANGED
|
@@ -258,7 +258,7 @@ Enable on the service (`leaderboard: { enabled: true, order: 'desc' }`), then:
|
|
|
258
258
|
|
|
259
259
|
```javascript
|
|
260
260
|
await Usion.leaderboard.submit(1500, { level: 3 }); // best score is kept
|
|
261
|
-
const friends = await Usion.leaderboard.friends(); //
|
|
261
|
+
const friends = await Usion.leaderboard.friends(); // your accepted friends + you
|
|
262
262
|
const top = await Usion.leaderboard.top({ limit: 20 });// global top
|
|
263
263
|
const me = await Usion.leaderboard.me(); // { score, rank, total }
|
|
264
264
|
// entry: { user_id, name, avatar, score, rank, is_me, metadata }
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usions/sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.25.0",
|
|
4
4
|
"description": "Usion Mini App SDK for iframe games and services",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "src/
|
|
6
|
+
"main": "src/index.js",
|
|
7
7
|
"browser": "src/browser.js",
|
|
8
8
|
"types": "types/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./types/index.d.ts",
|
|
12
|
-
"import": "./src/
|
|
12
|
+
"import": "./src/index.js",
|
|
13
13
|
"browser": "./src/browser.js",
|
|
14
|
-
"default": "./src/
|
|
14
|
+
"default": "./src/index.js"
|
|
15
15
|
},
|
|
16
16
|
"./browser": {
|
|
17
17
|
"types": "./types/browser.d.ts",
|
|
@@ -23,13 +23,18 @@
|
|
|
23
23
|
"types/"
|
|
24
24
|
],
|
|
25
25
|
"scripts": {
|
|
26
|
-
"build": "rollup -c",
|
|
27
|
-
"build:copy": "npm run build &&
|
|
26
|
+
"build": "node scripts/sync-version.mjs && rollup -c",
|
|
27
|
+
"build:copy": "npm run build && node scripts/sync-assets.mjs",
|
|
28
28
|
"watch": "rollup -c --watch",
|
|
29
|
-
"test": "npm run build && vitest run && npm run test:types && npm run check:src && npm run check:size",
|
|
30
|
-
"prepublishOnly": "npm run build && vitest run && npm run test:types && npm run check:src && npm run check:size",
|
|
29
|
+
"test": "npm run build && vitest run && npm run test:types && npm run check:src && npm run check:version && npm run check:assets && npm run check:cache && npm run check:size && npm run test:package",
|
|
30
|
+
"prepublishOnly": "npm run build && vitest run && npm run test:types && npm run check:src && npm run check:version && npm run check:size && npm run test:package",
|
|
31
31
|
"test:types": "tsc -p test/tsconfig.json",
|
|
32
|
+
"test:package": "node scripts/test-package.mjs",
|
|
33
|
+
"check:assets": "node scripts/sync-assets.mjs --check",
|
|
34
|
+
"check:cache": "node scripts/check-cache-headers.mjs",
|
|
35
|
+
"check:consumers": "node scripts/check-v2-consumers.mjs",
|
|
32
36
|
"check:size": "node scripts/check-size.mjs",
|
|
37
|
+
"check:version": "node scripts/sync-version.mjs --check",
|
|
33
38
|
"docs:api": "typedoc --plugin typedoc-plugin-markdown --entryPoints types/index.d.ts --out ../../docs/sdk-api --readme none --tsconfig test/tsconfig.json",
|
|
34
39
|
"check:src": "tsc -p tsconfig.src.json"
|
|
35
40
|
},
|
package/src/browser.js
CHANGED
|
@@ -191,7 +191,7 @@ var Usion = (function () {
|
|
|
191
191
|
* Core Usion object with init, _post, _request
|
|
192
192
|
*/
|
|
193
193
|
const core = {
|
|
194
|
-
version: '2.
|
|
194
|
+
version: '2.25.0', // synced from package.json by scripts/sync-version.mjs
|
|
195
195
|
config: {},
|
|
196
196
|
_initialized: false,
|
|
197
197
|
_initCallback: null,
|
|
@@ -365,6 +365,15 @@ var Usion = (function () {
|
|
|
365
365
|
self.bot._messageHandler(data.message);
|
|
366
366
|
}
|
|
367
367
|
|
|
368
|
+
// Handle host tab navigation (hybrid tabbed services). Keep launchPath
|
|
369
|
+
// truthful so getLaunchParams().path always reflects the current tab.
|
|
370
|
+
if (data.type === 'HOST_NAVIGATE' && typeof data.path === 'string') {
|
|
371
|
+
if (self.config) self.config.launchPath = data.path;
|
|
372
|
+
if (self._hostNavigateCallback) {
|
|
373
|
+
self._hostNavigateCallback({ path: data.path, tab: data.tab || null });
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
368
377
|
// Handle backend server-push events relayed by the host (embedded mode).
|
|
369
378
|
if (data.type === 'BACKEND_EVENT' && data.event && self._backendHandlers) {
|
|
370
379
|
const h = self._backendHandlers[data.event];
|
|
@@ -1140,6 +1149,73 @@ var Usion = (function () {
|
|
|
1140
1149
|
};
|
|
1141
1150
|
}
|
|
1142
1151
|
|
|
1152
|
+
/**
|
|
1153
|
+
* Usion SDK Host Tabs — hybrid tabbed services (SDK ≥ 2.25)
|
|
1154
|
+
*
|
|
1155
|
+
* A hybrid service registers `tabs`: the platform renders its native bot chat
|
|
1156
|
+
* as one tab and this app's own pages as the others, inside ONE persistent
|
|
1157
|
+
* iframe/WebView. These methods are the two-way bridge with that host screen:
|
|
1158
|
+
*
|
|
1159
|
+
* - Host → app: `onHostNavigate(cb)` receives tab switches as `{ path, tab }`.
|
|
1160
|
+
* Subscribing tells the host it can navigate in place; apps that never
|
|
1161
|
+
* subscribe get remounted with the new path as `getLaunchParams().path`.
|
|
1162
|
+
* - App → host: `openChat({ prefill })` jumps to the native chat tab with the
|
|
1163
|
+
* composer pre-filled (the user still presses send themselves), and
|
|
1164
|
+
* `reportPath(path)` keeps the host's tab highlight in sync when the user
|
|
1165
|
+
* navigates inside the app.
|
|
1166
|
+
*/
|
|
1167
|
+
|
|
1168
|
+
function createHostTabsMethods(Usion) {
|
|
1169
|
+
return {
|
|
1170
|
+
/**
|
|
1171
|
+
* Subscribe to host tab navigation. Called when the user taps one of this
|
|
1172
|
+
* service's tabs in the host tab bar (or a deep link targets a tab).
|
|
1173
|
+
* Subscribing signals the host that this app navigates in place — without
|
|
1174
|
+
* a subscription the host reloads the app with the new launch path instead.
|
|
1175
|
+
* @param {function} callback - Called with { path, tab } (tab may be null)
|
|
1176
|
+
*/
|
|
1177
|
+
onHostNavigate: function(callback) {
|
|
1178
|
+
Usion._hostNavigateCallback = callback;
|
|
1179
|
+
Usion._post({ type: 'HOST_NAVIGATE_SUBSCRIBED' });
|
|
1180
|
+
},
|
|
1181
|
+
|
|
1182
|
+
/**
|
|
1183
|
+
* Unsubscribe from host tab navigation (the host falls back to remounting
|
|
1184
|
+
* the app on the next tab switch).
|
|
1185
|
+
*/
|
|
1186
|
+
offHostNavigate: function() {
|
|
1187
|
+
Usion._hostNavigateCallback = null;
|
|
1188
|
+
Usion._post({ type: 'HOST_NAVIGATE_UNSUBSCRIBED' });
|
|
1189
|
+
},
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Ask the host to switch to the native chat tab, optionally pre-filling
|
|
1193
|
+
* the message composer. The text is NEVER sent automatically — the user
|
|
1194
|
+
* reviews it and presses send. No-op outside a hybrid tabbed host screen.
|
|
1195
|
+
* @param {object} [opts]
|
|
1196
|
+
* @param {string} [opts.prefill] - Draft text for the chat composer
|
|
1197
|
+
*/
|
|
1198
|
+
openChat: function(opts) {
|
|
1199
|
+
opts = opts || {};
|
|
1200
|
+
Usion._post({
|
|
1201
|
+
type: 'OPEN_CHAT_TAB',
|
|
1202
|
+
prefill: typeof opts.prefill === 'string' ? opts.prefill : undefined
|
|
1203
|
+
});
|
|
1204
|
+
},
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* Report the app's current internal path so the host can keep its tab
|
|
1208
|
+
* highlight in sync with in-app navigation. Fire-and-forget; the host
|
|
1209
|
+
* never echoes a navigation back for a reported path.
|
|
1210
|
+
* @param {string} path - Current path inside the app (e.g. "/explore")
|
|
1211
|
+
*/
|
|
1212
|
+
reportPath: function(path) {
|
|
1213
|
+
if (typeof path !== 'string' || !path) return;
|
|
1214
|
+
Usion._post({ type: 'TAB_CHANGED', path: path });
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1143
1219
|
/**
|
|
1144
1220
|
* Usion SDK UI Utilities — DOM helpers for mini apps
|
|
1145
1221
|
*/
|
|
@@ -2042,12 +2118,14 @@ var Usion = (function () {
|
|
|
2042
2118
|
// membership and sync are restored — otherwise a stale move could
|
|
2043
2119
|
// go out before the client has caught up.
|
|
2044
2120
|
if (self.roomId) {
|
|
2121
|
+
// Join reports the server head, so capture our replay cursor first.
|
|
2122
|
+
var resumeSequence = self._lastSequence || 0;
|
|
2045
2123
|
self._joined = false;
|
|
2046
2124
|
self._joinPromise = null;
|
|
2047
2125
|
self._rejoinPromise = self.join(self.roomId)
|
|
2048
2126
|
.then(function() {
|
|
2049
2127
|
Usion.log('Reconnected - joined room ' + self.roomId);
|
|
2050
|
-
self.requestSync(
|
|
2128
|
+
self.requestSync(resumeSequence);
|
|
2051
2129
|
})
|
|
2052
2130
|
.catch(function(err) {
|
|
2053
2131
|
Usion.log('Rejoin failed: ' + (err && err.message ? err.message : String(err)));
|
|
@@ -2287,6 +2365,7 @@ var Usion = (function () {
|
|
|
2287
2365
|
self._proxyJoinResolve(data);
|
|
2288
2366
|
self._proxyJoinResolve = null;
|
|
2289
2367
|
}
|
|
2368
|
+
self._proxyJoinReject = null;
|
|
2290
2369
|
self._dispatch('joined', data);
|
|
2291
2370
|
break;
|
|
2292
2371
|
|
|
@@ -2296,6 +2375,7 @@ var Usion = (function () {
|
|
|
2296
2375
|
self._proxyJoinReject(toUsionError(data.message || 'Join failed'));
|
|
2297
2376
|
self._proxyJoinReject = null;
|
|
2298
2377
|
}
|
|
2378
|
+
self._proxyJoinResolve = null;
|
|
2299
2379
|
break;
|
|
2300
2380
|
|
|
2301
2381
|
case 'GAME_PLAYER_JOINED':
|
|
@@ -2359,11 +2439,51 @@ var Usion = (function () {
|
|
|
2359
2439
|
};
|
|
2360
2440
|
}
|
|
2361
2441
|
|
|
2442
|
+
const GAME_ACK_TIMEOUT_MS = 10000;
|
|
2443
|
+
const GAME_JOIN_TIMEOUT_MS = 15000;
|
|
2444
|
+
|
|
2445
|
+
function emitGameWithAck(socket, event, payload, timeoutMs, timeoutCode) {
|
|
2446
|
+
timeoutMs = timeoutMs || GAME_ACK_TIMEOUT_MS;
|
|
2447
|
+
timeoutCode = timeoutCode || ERROR_CODES.REQUEST_TIMEOUT;
|
|
2448
|
+
return new Promise(function(resolve, reject) {
|
|
2449
|
+
let settled = false;
|
|
2450
|
+
const timer = setTimeout(function() {
|
|
2451
|
+
if (settled) return;
|
|
2452
|
+
settled = true;
|
|
2453
|
+
reject(new UsionError(timeoutCode,
|
|
2454
|
+
timeoutCode === ERROR_CODES.JOIN_TIMEOUT ? 'Join timeout' : 'Game request timeout'));
|
|
2455
|
+
}, timeoutMs);
|
|
2456
|
+
|
|
2457
|
+
try {
|
|
2458
|
+
socket.emit(event, payload, function(response) {
|
|
2459
|
+
if (settled) return;
|
|
2460
|
+
settled = true;
|
|
2461
|
+
clearTimeout(timer);
|
|
2462
|
+
resolve(response || {});
|
|
2463
|
+
});
|
|
2464
|
+
} catch (err) {
|
|
2465
|
+
if (settled) return;
|
|
2466
|
+
settled = true;
|
|
2467
|
+
clearTimeout(timer);
|
|
2468
|
+
reject(err);
|
|
2469
|
+
}
|
|
2470
|
+
});
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2362
2473
|
/**
|
|
2363
2474
|
* Usion SDK Game Methods — join, leave, action, realtime, sync, etc.
|
|
2364
2475
|
*/
|
|
2365
2476
|
|
|
2366
2477
|
|
|
2478
|
+
function trackJoinPromise(game, promise) {
|
|
2479
|
+
game._joinPromise = promise;
|
|
2480
|
+
promise.catch(function() {
|
|
2481
|
+
game._joined = false;
|
|
2482
|
+
if (game._joinPromise === promise) game._joinPromise = null;
|
|
2483
|
+
});
|
|
2484
|
+
return promise;
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2367
2487
|
/**
|
|
2368
2488
|
* Add game action methods to game module
|
|
2369
2489
|
* @param {object} game - The game module object
|
|
@@ -2379,8 +2499,8 @@ var Usion = (function () {
|
|
|
2379
2499
|
const self = this;
|
|
2380
2500
|
roomId = roomId || Usion.config.roomId;
|
|
2381
2501
|
|
|
2382
|
-
//
|
|
2383
|
-
if (self.
|
|
2502
|
+
// Coalesce in-flight/successful joins; rejected joins are cleared below.
|
|
2503
|
+
if (self.roomId === roomId && self._joinPromise) {
|
|
2384
2504
|
return self._joinPromise;
|
|
2385
2505
|
}
|
|
2386
2506
|
|
|
@@ -2398,35 +2518,35 @@ var Usion = (function () {
|
|
|
2398
2518
|
|
|
2399
2519
|
// Proxy mode: send join request to parent
|
|
2400
2520
|
if (self._useProxy) {
|
|
2401
|
-
|
|
2521
|
+
const joinPromise = new Promise(function(resolve, reject) {
|
|
2522
|
+
const rejectJoin = function(err) { reject(err); };
|
|
2402
2523
|
self._proxyJoinResolve = resolve;
|
|
2403
|
-
self._proxyJoinReject =
|
|
2524
|
+
self._proxyJoinReject = rejectJoin;
|
|
2404
2525
|
Usion._post({ type: 'GAME_JOIN', room_id: roomId });
|
|
2405
2526
|
setTimeout(function() {
|
|
2406
|
-
if (
|
|
2527
|
+
if (self._proxyJoinReject === rejectJoin) {
|
|
2528
|
+
self._proxyJoinResolve = null;
|
|
2407
2529
|
self._proxyJoinReject = null;
|
|
2408
2530
|
reject(new UsionError(ERROR_CODES.JOIN_TIMEOUT, 'Join timeout'));
|
|
2409
2531
|
}
|
|
2410
|
-
},
|
|
2532
|
+
}, GAME_JOIN_TIMEOUT_MS);
|
|
2411
2533
|
});
|
|
2412
|
-
return self
|
|
2534
|
+
return trackJoinPromise(self, joinPromise);
|
|
2413
2535
|
}
|
|
2414
2536
|
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
self.socket.emit('game:join', { room_id: roomId }, function(response) {
|
|
2537
|
+
let joinPromise;
|
|
2538
|
+
if (!self.socket || !self.connected) {
|
|
2539
|
+
joinPromise = Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
2540
|
+
} else if (!roomId) {
|
|
2541
|
+
joinPromise = Promise.reject(new UsionError(ERROR_CODES.NO_ROOM, 'No room ID provided'));
|
|
2542
|
+
} else {
|
|
2543
|
+
joinPromise = emitGameWithAck(
|
|
2544
|
+
self.socket, 'game:join', { room_id: roomId },
|
|
2545
|
+
GAME_JOIN_TIMEOUT_MS, ERROR_CODES.JOIN_TIMEOUT
|
|
2546
|
+
).then(function(response) {
|
|
2427
2547
|
if (response.error) {
|
|
2428
2548
|
self._joined = false;
|
|
2429
|
-
|
|
2549
|
+
throw toUsionError(response);
|
|
2430
2550
|
} else {
|
|
2431
2551
|
self._joined = true;
|
|
2432
2552
|
if (response.sequence !== undefined) {
|
|
@@ -2440,12 +2560,12 @@ var Usion = (function () {
|
|
|
2440
2560
|
// the socket transport now does too, so onJoined / recovery
|
|
2441
2561
|
// logic behaves the same everywhere.
|
|
2442
2562
|
self._dispatch('joined', response);
|
|
2443
|
-
|
|
2563
|
+
return response;
|
|
2444
2564
|
}
|
|
2445
2565
|
});
|
|
2446
|
-
}
|
|
2566
|
+
}
|
|
2447
2567
|
|
|
2448
|
-
return self
|
|
2568
|
+
return trackJoinPromise(self, joinPromise);
|
|
2449
2569
|
};
|
|
2450
2570
|
|
|
2451
2571
|
/**
|
|
@@ -2543,10 +2663,14 @@ var Usion = (function () {
|
|
|
2543
2663
|
}
|
|
2544
2664
|
|
|
2545
2665
|
if (self._useProxy) {
|
|
2666
|
+
if (!self.connected) {
|
|
2667
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
2668
|
+
}
|
|
2546
2669
|
const proxyMsg = { type: 'GAME_ACTION', room_id: self.roomId, action_type: actionType, action_data: actionData };
|
|
2547
2670
|
if (nextTurn) proxyMsg.next_turn = nextTurn;
|
|
2548
2671
|
Usion._post(proxyMsg);
|
|
2549
|
-
|
|
2672
|
+
// v2 hosts only confirm handoff; completion arrives via onAction.
|
|
2673
|
+
return Promise.resolve({ success: true, acknowledged: false });
|
|
2550
2674
|
}
|
|
2551
2675
|
|
|
2552
2676
|
// Gate sends while a post-reconnect rejoin is in flight, so a stale
|
|
@@ -2577,29 +2701,22 @@ var Usion = (function () {
|
|
|
2577
2701
|
*/
|
|
2578
2702
|
game._emitAction = function(payload, retryOnNotInRoom) {
|
|
2579
2703
|
const self = this;
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
if (response && response.error) {
|
|
2704
|
+
if (!self.socket || !self.connected) {
|
|
2705
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
2706
|
+
}
|
|
2707
|
+
return emitGameWithAck(self.socket, 'game:action', payload)
|
|
2708
|
+
.then(function(response) {
|
|
2709
|
+
if (response.error) {
|
|
2587
2710
|
const ue = toUsionError(response);
|
|
2588
2711
|
if (retryOnNotInRoom && ue.code === ERROR_CODES.NOT_IN_ROOM) {
|
|
2589
|
-
self._autoRejoin()
|
|
2590
|
-
.then(function() { return self._emitAction(payload, false); })
|
|
2591
|
-
.then(resolve, reject);
|
|
2592
|
-
return;
|
|
2593
|
-
}
|
|
2594
|
-
reject(ue);
|
|
2595
|
-
} else {
|
|
2596
|
-
if (response && response.sequence !== undefined) {
|
|
2597
|
-
self._lastSequence = response.sequence;
|
|
2712
|
+
return self._autoRejoin()
|
|
2713
|
+
.then(function() { return self._emitAction(payload, false); });
|
|
2598
2714
|
}
|
|
2599
|
-
|
|
2715
|
+
throw ue;
|
|
2600
2716
|
}
|
|
2717
|
+
if (response.sequence !== undefined) self._lastSequence = response.sequence;
|
|
2718
|
+
return response;
|
|
2601
2719
|
});
|
|
2602
|
-
});
|
|
2603
2720
|
};
|
|
2604
2721
|
|
|
2605
2722
|
/**
|
|
@@ -2612,11 +2729,12 @@ var Usion = (function () {
|
|
|
2612
2729
|
if (self._rejoinPromise) return self._rejoinPromise;
|
|
2613
2730
|
if (!self.roomId || !self.socket || !self.connected) return Promise.resolve();
|
|
2614
2731
|
Usion.log('Server reports socket not in room - auto-rejoining ' + self.roomId);
|
|
2732
|
+
const resumeSequence = self._lastSequence || 0;
|
|
2615
2733
|
self._joined = false;
|
|
2616
2734
|
self._joinPromise = null;
|
|
2617
2735
|
self._rejoinPromise = self.join(self.roomId)
|
|
2618
2736
|
.then(function() {
|
|
2619
|
-
self.requestSync(
|
|
2737
|
+
self.requestSync(resumeSequence);
|
|
2620
2738
|
})
|
|
2621
2739
|
.catch(function(err) {
|
|
2622
2740
|
Usion.log('Auto-rejoin failed: ' + (err && err.message ? err.message : String(err)));
|
|
@@ -2665,18 +2783,43 @@ var Usion = (function () {
|
|
|
2665
2783
|
game._flushOfflineQueue = function() {
|
|
2666
2784
|
const self = this;
|
|
2667
2785
|
const queue = self._offlineQueue;
|
|
2668
|
-
if (!queue || !queue.length) return;
|
|
2669
|
-
self.
|
|
2786
|
+
if (!queue || !queue.length) return Promise.resolve();
|
|
2787
|
+
if (self._offlineFlushPromise) return self._offlineFlushPromise;
|
|
2788
|
+
if (!self.connected || (!self._useProxy && !self.directMode && !self._joined)) {
|
|
2789
|
+
return Promise.resolve();
|
|
2790
|
+
}
|
|
2670
2791
|
Usion.log('Flushing ' + queue.length + ' queued action(s) after reconnect');
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2792
|
+
|
|
2793
|
+
function sendNext() {
|
|
2794
|
+
if (!queue.length) return Promise.resolve();
|
|
2795
|
+
if (!self.connected || (!self._useProxy && !self.directMode && !self._joined)) {
|
|
2796
|
+
return Promise.resolve();
|
|
2797
|
+
}
|
|
2798
|
+
|
|
2799
|
+
const item = queue[0];
|
|
2800
|
+
return self.action(item.actionType, item.actionData, item.opts).then(function(result) {
|
|
2801
|
+
if (queue[0] === item) queue.shift();
|
|
2802
|
+
item.resolve(result);
|
|
2803
|
+
return sendNext();
|
|
2804
|
+
}, function(err) {
|
|
2805
|
+
const ue = toUsionError(err);
|
|
2806
|
+
// Preserve only commands known not to have reached a live room.
|
|
2807
|
+
if (ue.code === ERROR_CODES.NOT_CONNECTED || ue.code === ERROR_CODES.NOT_IN_ROOM) {
|
|
2808
|
+
return;
|
|
2809
|
+
}
|
|
2810
|
+
if (queue[0] === item) queue.shift();
|
|
2811
|
+
item.reject(err);
|
|
2812
|
+
return sendNext();
|
|
2678
2813
|
});
|
|
2679
|
-
}
|
|
2814
|
+
}
|
|
2815
|
+
|
|
2816
|
+
const flushPromise = Promise.resolve().then(sendNext);
|
|
2817
|
+
self._offlineFlushPromise = flushPromise;
|
|
2818
|
+
function clearFlush() {
|
|
2819
|
+
if (self._offlineFlushPromise === flushPromise) self._offlineFlushPromise = null;
|
|
2820
|
+
}
|
|
2821
|
+
flushPromise.then(clearFlush, clearFlush);
|
|
2822
|
+
return flushPromise;
|
|
2680
2823
|
};
|
|
2681
2824
|
|
|
2682
2825
|
/**
|
|
@@ -2722,24 +2865,20 @@ var Usion = (function () {
|
|
|
2722
2865
|
});
|
|
2723
2866
|
}
|
|
2724
2867
|
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
}
|
|
2730
|
-
self.socket.emit('game:set_state', {
|
|
2868
|
+
if (!self.socket || !self.connected) {
|
|
2869
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
2870
|
+
}
|
|
2871
|
+
return emitGameWithAck(self.socket, 'game:set_state', {
|
|
2731
2872
|
room_id: self.roomId,
|
|
2732
2873
|
state: state || {},
|
|
2733
2874
|
sequence: checkpointSeq
|
|
2734
|
-
}
|
|
2735
|
-
if (response
|
|
2875
|
+
}).then(function(response) {
|
|
2876
|
+
if (response.error) {
|
|
2736
2877
|
const ue = toUsionError(response);
|
|
2737
|
-
|
|
2738
|
-
} else {
|
|
2739
|
-
resolve(response || { success: true });
|
|
2878
|
+
return { success: false, error: ue.message, code: ue.code };
|
|
2740
2879
|
}
|
|
2880
|
+
return Object.keys(response).length ? response : { success: true };
|
|
2741
2881
|
});
|
|
2742
|
-
});
|
|
2743
2882
|
};
|
|
2744
2883
|
|
|
2745
2884
|
/**
|
|
@@ -2846,20 +2985,16 @@ var Usion = (function () {
|
|
|
2846
2985
|
return Promise.resolve({ success: true });
|
|
2847
2986
|
}
|
|
2848
2987
|
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
self.socket.emit('game:forfeit', { room_id: self.roomId }, function(response) {
|
|
2988
|
+
if (!self.socket || !self.connected) {
|
|
2989
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
2990
|
+
}
|
|
2991
|
+
return emitGameWithAck(self.socket, 'game:forfeit', { room_id: self.roomId })
|
|
2992
|
+
.then(function(response) {
|
|
2856
2993
|
if (response.error) {
|
|
2857
|
-
|
|
2858
|
-
} else {
|
|
2859
|
-
resolve(response);
|
|
2994
|
+
throw toUsionError(response);
|
|
2860
2995
|
}
|
|
2996
|
+
return response;
|
|
2861
2997
|
});
|
|
2862
|
-
});
|
|
2863
2998
|
};
|
|
2864
2999
|
|
|
2865
3000
|
/**
|
|
@@ -3106,9 +3241,10 @@ var Usion = (function () {
|
|
|
3106
3241
|
*/
|
|
3107
3242
|
game.isConnected = function() {
|
|
3108
3243
|
if (this.directMode) {
|
|
3109
|
-
return this.connected && this.directSocket && this.directSocket.readyState === WebSocket.OPEN;
|
|
3244
|
+
return !!(this.connected && this.directSocket && this.directSocket.readyState === WebSocket.OPEN);
|
|
3110
3245
|
}
|
|
3111
|
-
|
|
3246
|
+
if (this._useProxy) return !!this.connected;
|
|
3247
|
+
return !!(this.connected && this.socket && this.socket.connected);
|
|
3112
3248
|
};
|
|
3113
3249
|
}
|
|
3114
3250
|
|
|
@@ -5988,12 +6124,12 @@ var Usion = (function () {
|
|
|
5988
6124
|
* unified backend channel, so it works in standalone AND embedded games.
|
|
5989
6125
|
*
|
|
5990
6126
|
* await Usion.leaderboard.submit(1500); // your score
|
|
5991
|
-
* const friends = await Usion.leaderboard.friends(); //
|
|
6127
|
+
* const friends = await Usion.leaderboard.friends(); // your accepted friends + you
|
|
5992
6128
|
* const top = await Usion.leaderboard.top(); // global top N
|
|
5993
6129
|
* const me = await Usion.leaderboard.me(); // { score, rank, total }
|
|
5994
6130
|
*
|
|
5995
|
-
* The **friends** board is scoped to
|
|
5996
|
-
* plus yourself —
|
|
6131
|
+
* The **friends** board is scoped to your accepted friends (the mutual-consent
|
|
6132
|
+
* friend system) plus yourself — "see my friends' record scores".
|
|
5997
6133
|
* Entries: { user_id, name, avatar, score, rank, is_me, metadata }.
|
|
5998
6134
|
*/
|
|
5999
6135
|
function createLeaderboardModule(Usion) {
|
|
@@ -6007,7 +6143,7 @@ var Usion = (function () {
|
|
|
6007
6143
|
return Usion._backendEmit('lb:submit', { service_id: serviceId(opts), score: score, metadata: metadata != null ? metadata : null });
|
|
6008
6144
|
},
|
|
6009
6145
|
|
|
6010
|
-
/** Leaderboard of
|
|
6146
|
+
/** Leaderboard of your accepted friends (plus you). Returns entries[]. */
|
|
6011
6147
|
friends: function (opts) {
|
|
6012
6148
|
opts = opts || {};
|
|
6013
6149
|
return Usion._backendEmit('lb:friends', { service_id: serviceId(opts), limit: opts.limit || 50 })
|
|
@@ -6647,6 +6783,9 @@ var Usion = (function () {
|
|
|
6647
6783
|
// Attach back button methods directly on Usion
|
|
6648
6784
|
Object.assign(Usion, createBackButtonMethods(Usion));
|
|
6649
6785
|
|
|
6786
|
+
// Attach hybrid tabbed-service methods directly on Usion
|
|
6787
|
+
Object.assign(Usion, createHostTabsMethods(Usion));
|
|
6788
|
+
|
|
6650
6789
|
// Attach UI utilities directly on Usion
|
|
6651
6790
|
Object.assign(Usion, uiMethods);
|
|
6652
6791
|
|
package/src/modules/core.js
CHANGED
|
@@ -68,7 +68,7 @@ export function isTrustedMessageSource(event) {
|
|
|
68
68
|
* Core Usion object with init, _post, _request
|
|
69
69
|
*/
|
|
70
70
|
export const core = {
|
|
71
|
-
version: '
|
|
71
|
+
version: '2.25.0', // synced from package.json by scripts/sync-version.mjs
|
|
72
72
|
config: {},
|
|
73
73
|
_initialized: false,
|
|
74
74
|
_initCallback: null,
|
|
@@ -242,6 +242,15 @@ export const core = {
|
|
|
242
242
|
self.bot._messageHandler(data.message);
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
+
// Handle host tab navigation (hybrid tabbed services). Keep launchPath
|
|
246
|
+
// truthful so getLaunchParams().path always reflects the current tab.
|
|
247
|
+
if (data.type === 'HOST_NAVIGATE' && typeof data.path === 'string') {
|
|
248
|
+
if (self.config) self.config.launchPath = data.path;
|
|
249
|
+
if (self._hostNavigateCallback) {
|
|
250
|
+
self._hostNavigateCallback({ path: data.path, tab: data.tab || null });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
245
254
|
// Handle backend server-push events relayed by the host (embedded mode).
|
|
246
255
|
if (data.type === 'BACKEND_EVENT' && data.event && self._backendHandlers) {
|
|
247
256
|
const h = self._backendHandlers[data.event];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { UsionError, ERROR_CODES } from './errors.js';
|
|
2
|
+
|
|
3
|
+
export const GAME_ACK_TIMEOUT_MS = 10000;
|
|
4
|
+
export const GAME_JOIN_TIMEOUT_MS = 15000;
|
|
5
|
+
|
|
6
|
+
export function emitGameWithAck(socket, event, payload, timeoutMs, timeoutCode) {
|
|
7
|
+
timeoutMs = timeoutMs || GAME_ACK_TIMEOUT_MS;
|
|
8
|
+
timeoutCode = timeoutCode || ERROR_CODES.REQUEST_TIMEOUT;
|
|
9
|
+
return new Promise(function(resolve, reject) {
|
|
10
|
+
let settled = false;
|
|
11
|
+
const timer = setTimeout(function() {
|
|
12
|
+
if (settled) return;
|
|
13
|
+
settled = true;
|
|
14
|
+
reject(new UsionError(timeoutCode,
|
|
15
|
+
timeoutCode === ERROR_CODES.JOIN_TIMEOUT ? 'Join timeout' : 'Game request timeout'));
|
|
16
|
+
}, timeoutMs);
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
socket.emit(event, payload, function(response) {
|
|
20
|
+
if (settled) return;
|
|
21
|
+
settled = true;
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
resolve(response || {});
|
|
24
|
+
});
|
|
25
|
+
} catch (err) {
|
|
26
|
+
if (settled) return;
|
|
27
|
+
settled = true;
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
reject(err);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -3,6 +3,19 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { UsionError, toUsionError, ERROR_CODES } from './errors.js';
|
|
6
|
+
import {
|
|
7
|
+
emitGameWithAck,
|
|
8
|
+
GAME_JOIN_TIMEOUT_MS,
|
|
9
|
+
} from './game-ack.js';
|
|
10
|
+
|
|
11
|
+
function trackJoinPromise(game, promise) {
|
|
12
|
+
game._joinPromise = promise;
|
|
13
|
+
promise.catch(function() {
|
|
14
|
+
game._joined = false;
|
|
15
|
+
if (game._joinPromise === promise) game._joinPromise = null;
|
|
16
|
+
});
|
|
17
|
+
return promise;
|
|
18
|
+
}
|
|
6
19
|
|
|
7
20
|
/**
|
|
8
21
|
* Add game action methods to game module
|
|
@@ -19,8 +32,8 @@ export function applyGameMethods(game, Usion) {
|
|
|
19
32
|
const self = this;
|
|
20
33
|
roomId = roomId || Usion.config.roomId;
|
|
21
34
|
|
|
22
|
-
//
|
|
23
|
-
if (self.
|
|
35
|
+
// Coalesce in-flight/successful joins; rejected joins are cleared below.
|
|
36
|
+
if (self.roomId === roomId && self._joinPromise) {
|
|
24
37
|
return self._joinPromise;
|
|
25
38
|
}
|
|
26
39
|
|
|
@@ -38,35 +51,35 @@ export function applyGameMethods(game, Usion) {
|
|
|
38
51
|
|
|
39
52
|
// Proxy mode: send join request to parent
|
|
40
53
|
if (self._useProxy) {
|
|
41
|
-
|
|
54
|
+
const joinPromise = new Promise(function(resolve, reject) {
|
|
55
|
+
const rejectJoin = function(err) { reject(err); };
|
|
42
56
|
self._proxyJoinResolve = resolve;
|
|
43
|
-
self._proxyJoinReject =
|
|
57
|
+
self._proxyJoinReject = rejectJoin;
|
|
44
58
|
Usion._post({ type: 'GAME_JOIN', room_id: roomId });
|
|
45
59
|
setTimeout(function() {
|
|
46
|
-
if (
|
|
60
|
+
if (self._proxyJoinReject === rejectJoin) {
|
|
61
|
+
self._proxyJoinResolve = null;
|
|
47
62
|
self._proxyJoinReject = null;
|
|
48
63
|
reject(new UsionError(ERROR_CODES.JOIN_TIMEOUT, 'Join timeout'));
|
|
49
64
|
}
|
|
50
|
-
},
|
|
65
|
+
}, GAME_JOIN_TIMEOUT_MS);
|
|
51
66
|
});
|
|
52
|
-
return self
|
|
67
|
+
return trackJoinPromise(self, joinPromise);
|
|
53
68
|
}
|
|
54
69
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
self.socket.emit('game:join', { room_id: roomId }, function(response) {
|
|
70
|
+
let joinPromise;
|
|
71
|
+
if (!self.socket || !self.connected) {
|
|
72
|
+
joinPromise = Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
73
|
+
} else if (!roomId) {
|
|
74
|
+
joinPromise = Promise.reject(new UsionError(ERROR_CODES.NO_ROOM, 'No room ID provided'));
|
|
75
|
+
} else {
|
|
76
|
+
joinPromise = emitGameWithAck(
|
|
77
|
+
self.socket, 'game:join', { room_id: roomId },
|
|
78
|
+
GAME_JOIN_TIMEOUT_MS, ERROR_CODES.JOIN_TIMEOUT
|
|
79
|
+
).then(function(response) {
|
|
67
80
|
if (response.error) {
|
|
68
81
|
self._joined = false;
|
|
69
|
-
|
|
82
|
+
throw toUsionError(response);
|
|
70
83
|
} else {
|
|
71
84
|
self._joined = true;
|
|
72
85
|
if (response.sequence !== undefined) {
|
|
@@ -80,12 +93,12 @@ export function applyGameMethods(game, Usion) {
|
|
|
80
93
|
// the socket transport now does too, so onJoined / recovery
|
|
81
94
|
// logic behaves the same everywhere.
|
|
82
95
|
self._dispatch('joined', response);
|
|
83
|
-
|
|
96
|
+
return response;
|
|
84
97
|
}
|
|
85
98
|
});
|
|
86
|
-
}
|
|
99
|
+
}
|
|
87
100
|
|
|
88
|
-
return self
|
|
101
|
+
return trackJoinPromise(self, joinPromise);
|
|
89
102
|
};
|
|
90
103
|
|
|
91
104
|
/**
|
|
@@ -183,10 +196,14 @@ export function applyGameMethods(game, Usion) {
|
|
|
183
196
|
}
|
|
184
197
|
|
|
185
198
|
if (self._useProxy) {
|
|
199
|
+
if (!self.connected) {
|
|
200
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
201
|
+
}
|
|
186
202
|
const proxyMsg = { type: 'GAME_ACTION', room_id: self.roomId, action_type: actionType, action_data: actionData };
|
|
187
203
|
if (nextTurn) proxyMsg.next_turn = nextTurn;
|
|
188
204
|
Usion._post(proxyMsg);
|
|
189
|
-
|
|
205
|
+
// v2 hosts only confirm handoff; completion arrives via onAction.
|
|
206
|
+
return Promise.resolve({ success: true, acknowledged: false });
|
|
190
207
|
}
|
|
191
208
|
|
|
192
209
|
// Gate sends while a post-reconnect rejoin is in flight, so a stale
|
|
@@ -217,29 +234,22 @@ export function applyGameMethods(game, Usion) {
|
|
|
217
234
|
*/
|
|
218
235
|
game._emitAction = function(payload, retryOnNotInRoom) {
|
|
219
236
|
const self = this;
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (response && response.error) {
|
|
237
|
+
if (!self.socket || !self.connected) {
|
|
238
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
239
|
+
}
|
|
240
|
+
return emitGameWithAck(self.socket, 'game:action', payload)
|
|
241
|
+
.then(function(response) {
|
|
242
|
+
if (response.error) {
|
|
227
243
|
const ue = toUsionError(response);
|
|
228
244
|
if (retryOnNotInRoom && ue.code === ERROR_CODES.NOT_IN_ROOM) {
|
|
229
|
-
self._autoRejoin()
|
|
230
|
-
.then(function() { return self._emitAction(payload, false); })
|
|
231
|
-
.then(resolve, reject);
|
|
232
|
-
return;
|
|
245
|
+
return self._autoRejoin()
|
|
246
|
+
.then(function() { return self._emitAction(payload, false); });
|
|
233
247
|
}
|
|
234
|
-
|
|
235
|
-
} else {
|
|
236
|
-
if (response && response.sequence !== undefined) {
|
|
237
|
-
self._lastSequence = response.sequence;
|
|
238
|
-
}
|
|
239
|
-
resolve(response);
|
|
248
|
+
throw ue;
|
|
240
249
|
}
|
|
250
|
+
if (response.sequence !== undefined) self._lastSequence = response.sequence;
|
|
251
|
+
return response;
|
|
241
252
|
});
|
|
242
|
-
});
|
|
243
253
|
};
|
|
244
254
|
|
|
245
255
|
/**
|
|
@@ -252,11 +262,12 @@ export function applyGameMethods(game, Usion) {
|
|
|
252
262
|
if (self._rejoinPromise) return self._rejoinPromise;
|
|
253
263
|
if (!self.roomId || !self.socket || !self.connected) return Promise.resolve();
|
|
254
264
|
Usion.log('Server reports socket not in room - auto-rejoining ' + self.roomId);
|
|
265
|
+
const resumeSequence = self._lastSequence || 0;
|
|
255
266
|
self._joined = false;
|
|
256
267
|
self._joinPromise = null;
|
|
257
268
|
self._rejoinPromise = self.join(self.roomId)
|
|
258
269
|
.then(function() {
|
|
259
|
-
self.requestSync(
|
|
270
|
+
self.requestSync(resumeSequence);
|
|
260
271
|
})
|
|
261
272
|
.catch(function(err) {
|
|
262
273
|
Usion.log('Auto-rejoin failed: ' + (err && err.message ? err.message : String(err)));
|
|
@@ -305,18 +316,43 @@ export function applyGameMethods(game, Usion) {
|
|
|
305
316
|
game._flushOfflineQueue = function() {
|
|
306
317
|
const self = this;
|
|
307
318
|
const queue = self._offlineQueue;
|
|
308
|
-
if (!queue || !queue.length) return;
|
|
309
|
-
self.
|
|
319
|
+
if (!queue || !queue.length) return Promise.resolve();
|
|
320
|
+
if (self._offlineFlushPromise) return self._offlineFlushPromise;
|
|
321
|
+
if (!self.connected || (!self._useProxy && !self.directMode && !self._joined)) {
|
|
322
|
+
return Promise.resolve();
|
|
323
|
+
}
|
|
310
324
|
Usion.log('Flushing ' + queue.length + ' queued action(s) after reconnect');
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
325
|
+
|
|
326
|
+
function sendNext() {
|
|
327
|
+
if (!queue.length) return Promise.resolve();
|
|
328
|
+
if (!self.connected || (!self._useProxy && !self.directMode && !self._joined)) {
|
|
329
|
+
return Promise.resolve();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const item = queue[0];
|
|
333
|
+
return self.action(item.actionType, item.actionData, item.opts).then(function(result) {
|
|
334
|
+
if (queue[0] === item) queue.shift();
|
|
335
|
+
item.resolve(result);
|
|
336
|
+
return sendNext();
|
|
337
|
+
}, function(err) {
|
|
338
|
+
const ue = toUsionError(err);
|
|
339
|
+
// Preserve only commands known not to have reached a live room.
|
|
340
|
+
if (ue.code === ERROR_CODES.NOT_CONNECTED || ue.code === ERROR_CODES.NOT_IN_ROOM) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if (queue[0] === item) queue.shift();
|
|
344
|
+
item.reject(err);
|
|
345
|
+
return sendNext();
|
|
318
346
|
});
|
|
319
|
-
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const flushPromise = Promise.resolve().then(sendNext);
|
|
350
|
+
self._offlineFlushPromise = flushPromise;
|
|
351
|
+
function clearFlush() {
|
|
352
|
+
if (self._offlineFlushPromise === flushPromise) self._offlineFlushPromise = null;
|
|
353
|
+
}
|
|
354
|
+
flushPromise.then(clearFlush, clearFlush);
|
|
355
|
+
return flushPromise;
|
|
320
356
|
};
|
|
321
357
|
|
|
322
358
|
/**
|
|
@@ -362,24 +398,20 @@ export function applyGameMethods(game, Usion) {
|
|
|
362
398
|
});
|
|
363
399
|
}
|
|
364
400
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
self.socket.emit('game:set_state', {
|
|
401
|
+
if (!self.socket || !self.connected) {
|
|
402
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
403
|
+
}
|
|
404
|
+
return emitGameWithAck(self.socket, 'game:set_state', {
|
|
371
405
|
room_id: self.roomId,
|
|
372
406
|
state: state || {},
|
|
373
407
|
sequence: checkpointSeq
|
|
374
|
-
}
|
|
375
|
-
if (response
|
|
408
|
+
}).then(function(response) {
|
|
409
|
+
if (response.error) {
|
|
376
410
|
const ue = toUsionError(response);
|
|
377
|
-
|
|
378
|
-
} else {
|
|
379
|
-
resolve(response || { success: true });
|
|
411
|
+
return { success: false, error: ue.message, code: ue.code };
|
|
380
412
|
}
|
|
413
|
+
return Object.keys(response).length ? response : { success: true };
|
|
381
414
|
});
|
|
382
|
-
});
|
|
383
415
|
};
|
|
384
416
|
|
|
385
417
|
/**
|
|
@@ -486,20 +518,16 @@ export function applyGameMethods(game, Usion) {
|
|
|
486
518
|
return Promise.resolve({ success: true });
|
|
487
519
|
}
|
|
488
520
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
self.socket.emit('game:forfeit', { room_id: self.roomId }, function(response) {
|
|
521
|
+
if (!self.socket || !self.connected) {
|
|
522
|
+
return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED, 'Not connected'));
|
|
523
|
+
}
|
|
524
|
+
return emitGameWithAck(self.socket, 'game:forfeit', { room_id: self.roomId })
|
|
525
|
+
.then(function(response) {
|
|
496
526
|
if (response.error) {
|
|
497
|
-
|
|
498
|
-
} else {
|
|
499
|
-
resolve(response);
|
|
527
|
+
throw toUsionError(response);
|
|
500
528
|
}
|
|
529
|
+
return response;
|
|
501
530
|
});
|
|
502
|
-
});
|
|
503
531
|
};
|
|
504
532
|
|
|
505
533
|
/**
|
|
@@ -746,8 +774,9 @@ export function applyGameMethods(game, Usion) {
|
|
|
746
774
|
*/
|
|
747
775
|
game.isConnected = function() {
|
|
748
776
|
if (this.directMode) {
|
|
749
|
-
return this.connected && this.directSocket && this.directSocket.readyState === WebSocket.OPEN;
|
|
777
|
+
return !!(this.connected && this.directSocket && this.directSocket.readyState === WebSocket.OPEN);
|
|
750
778
|
}
|
|
751
|
-
|
|
779
|
+
if (this._useProxy) return !!this.connected;
|
|
780
|
+
return !!(this.connected && this.socket && this.socket.connected);
|
|
752
781
|
};
|
|
753
782
|
}
|
|
@@ -109,6 +109,7 @@ export function applyGameProxy(game, Usion) {
|
|
|
109
109
|
self._proxyJoinResolve(data);
|
|
110
110
|
self._proxyJoinResolve = null;
|
|
111
111
|
}
|
|
112
|
+
self._proxyJoinReject = null;
|
|
112
113
|
self._dispatch('joined', data);
|
|
113
114
|
break;
|
|
114
115
|
|
|
@@ -118,6 +119,7 @@ export function applyGameProxy(game, Usion) {
|
|
|
118
119
|
self._proxyJoinReject(toUsionError(data.message || 'Join failed'));
|
|
119
120
|
self._proxyJoinReject = null;
|
|
120
121
|
}
|
|
122
|
+
self._proxyJoinResolve = null;
|
|
121
123
|
break;
|
|
122
124
|
|
|
123
125
|
case 'GAME_PLAYER_JOINED':
|
|
@@ -75,12 +75,14 @@ export function applyGameSocket(game, Usion) {
|
|
|
75
75
|
// membership and sync are restored — otherwise a stale move could
|
|
76
76
|
// go out before the client has caught up.
|
|
77
77
|
if (self.roomId) {
|
|
78
|
+
// Join reports the server head, so capture our replay cursor first.
|
|
79
|
+
var resumeSequence = self._lastSequence || 0;
|
|
78
80
|
self._joined = false;
|
|
79
81
|
self._joinPromise = null;
|
|
80
82
|
self._rejoinPromise = self.join(self.roomId)
|
|
81
83
|
.then(function() {
|
|
82
84
|
Usion.log('Reconnected - joined room ' + self.roomId);
|
|
83
|
-
self.requestSync(
|
|
85
|
+
self.requestSync(resumeSequence);
|
|
84
86
|
})
|
|
85
87
|
.catch(function(err) {
|
|
86
88
|
Usion.log('Rejoin failed: ' + (err && err.message ? err.message : String(err)));
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usion SDK Host Tabs — hybrid tabbed services (SDK ≥ 2.25)
|
|
3
|
+
*
|
|
4
|
+
* A hybrid service registers `tabs`: the platform renders its native bot chat
|
|
5
|
+
* as one tab and this app's own pages as the others, inside ONE persistent
|
|
6
|
+
* iframe/WebView. These methods are the two-way bridge with that host screen:
|
|
7
|
+
*
|
|
8
|
+
* - Host → app: `onHostNavigate(cb)` receives tab switches as `{ path, tab }`.
|
|
9
|
+
* Subscribing tells the host it can navigate in place; apps that never
|
|
10
|
+
* subscribe get remounted with the new path as `getLaunchParams().path`.
|
|
11
|
+
* - App → host: `openChat({ prefill })` jumps to the native chat tab with the
|
|
12
|
+
* composer pre-filled (the user still presses send themselves), and
|
|
13
|
+
* `reportPath(path)` keeps the host's tab highlight in sync when the user
|
|
14
|
+
* navigates inside the app.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export function createHostTabsMethods(Usion) {
|
|
18
|
+
return {
|
|
19
|
+
/**
|
|
20
|
+
* Subscribe to host tab navigation. Called when the user taps one of this
|
|
21
|
+
* service's tabs in the host tab bar (or a deep link targets a tab).
|
|
22
|
+
* Subscribing signals the host that this app navigates in place — without
|
|
23
|
+
* a subscription the host reloads the app with the new launch path instead.
|
|
24
|
+
* @param {function} callback - Called with { path, tab } (tab may be null)
|
|
25
|
+
*/
|
|
26
|
+
onHostNavigate: function(callback) {
|
|
27
|
+
Usion._hostNavigateCallback = callback;
|
|
28
|
+
Usion._post({ type: 'HOST_NAVIGATE_SUBSCRIBED' });
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Unsubscribe from host tab navigation (the host falls back to remounting
|
|
33
|
+
* the app on the next tab switch).
|
|
34
|
+
*/
|
|
35
|
+
offHostNavigate: function() {
|
|
36
|
+
Usion._hostNavigateCallback = null;
|
|
37
|
+
Usion._post({ type: 'HOST_NAVIGATE_UNSUBSCRIBED' });
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Ask the host to switch to the native chat tab, optionally pre-filling
|
|
42
|
+
* the message composer. The text is NEVER sent automatically — the user
|
|
43
|
+
* reviews it and presses send. No-op outside a hybrid tabbed host screen.
|
|
44
|
+
* @param {object} [opts]
|
|
45
|
+
* @param {string} [opts.prefill] - Draft text for the chat composer
|
|
46
|
+
*/
|
|
47
|
+
openChat: function(opts) {
|
|
48
|
+
opts = opts || {};
|
|
49
|
+
Usion._post({
|
|
50
|
+
type: 'OPEN_CHAT_TAB',
|
|
51
|
+
prefill: typeof opts.prefill === 'string' ? opts.prefill : undefined
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Report the app's current internal path so the host can keep its tab
|
|
57
|
+
* highlight in sync with in-app navigation. Fire-and-forget; the host
|
|
58
|
+
* never echoes a navigation back for a reported path.
|
|
59
|
+
* @param {string} path - Current path inside the app (e.g. "/explore")
|
|
60
|
+
*/
|
|
61
|
+
reportPath: function(path) {
|
|
62
|
+
if (typeof path !== 'string' || !path) return;
|
|
63
|
+
Usion._post({ type: 'TAB_CHANGED', path: path });
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
package/src/modules/index.js
CHANGED
|
@@ -21,6 +21,7 @@ import { createChatModule } from './chat.js';
|
|
|
21
21
|
import { createBotModule } from './bot.js';
|
|
22
22
|
import { createResultsMethods } from './results.js';
|
|
23
23
|
import { createBackButtonMethods } from './back-button.js';
|
|
24
|
+
import { createHostTabsMethods } from './host-tabs.js';
|
|
24
25
|
import { uiMethods } from './ui.js';
|
|
25
26
|
import { miscMethods } from './misc.js';
|
|
26
27
|
import { createFileStorageModule } from './file-storage.js';
|
|
@@ -69,6 +70,9 @@ Object.assign(Usion, createResultsMethods(Usion));
|
|
|
69
70
|
// Attach back button methods directly on Usion
|
|
70
71
|
Object.assign(Usion, createBackButtonMethods(Usion));
|
|
71
72
|
|
|
73
|
+
// Attach hybrid tabbed-service methods directly on Usion
|
|
74
|
+
Object.assign(Usion, createHostTabsMethods(Usion));
|
|
75
|
+
|
|
72
76
|
// Attach UI utilities directly on Usion
|
|
73
77
|
Object.assign(Usion, uiMethods);
|
|
74
78
|
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* unified backend channel, so it works in standalone AND embedded games.
|
|
6
6
|
*
|
|
7
7
|
* await Usion.leaderboard.submit(1500); // your score
|
|
8
|
-
* const friends = await Usion.leaderboard.friends(); //
|
|
8
|
+
* const friends = await Usion.leaderboard.friends(); // your accepted friends + you
|
|
9
9
|
* const top = await Usion.leaderboard.top(); // global top N
|
|
10
10
|
* const me = await Usion.leaderboard.me(); // { score, rank, total }
|
|
11
11
|
*
|
|
12
|
-
* The **friends** board is scoped to
|
|
13
|
-
* plus yourself —
|
|
12
|
+
* The **friends** board is scoped to your accepted friends (the mutual-consent
|
|
13
|
+
* friend system) plus yourself — "see my friends' record scores".
|
|
14
14
|
* Entries: { user_id, name, avatar, score, rank, is_me, metadata }.
|
|
15
15
|
*/
|
|
16
16
|
export function createLeaderboardModule(Usion) {
|
|
@@ -24,7 +24,7 @@ export function createLeaderboardModule(Usion) {
|
|
|
24
24
|
return Usion._backendEmit('lb:submit', { service_id: serviceId(opts), score: score, metadata: metadata != null ? metadata : null });
|
|
25
25
|
},
|
|
26
26
|
|
|
27
|
-
/** Leaderboard of
|
|
27
|
+
/** Leaderboard of your accepted friends (plus you). Returns entries[]. */
|
|
28
28
|
friends: function (opts) {
|
|
29
29
|
opts = opts || {};
|
|
30
30
|
return Usion._backendEmit('lb:friends', { service_id: serviceId(opts), limit: opts.limit || 50 })
|
package/types/index.d.ts
CHANGED
|
@@ -162,6 +162,8 @@ export interface GameJoinResult {
|
|
|
162
162
|
|
|
163
163
|
export interface ActionResult {
|
|
164
164
|
success: boolean;
|
|
165
|
+
/** False when a v2 proxy host accepted the command but cannot acknowledge it. */
|
|
166
|
+
acknowledged?: boolean;
|
|
165
167
|
sequence?: number;
|
|
166
168
|
error?: string;
|
|
167
169
|
message?: string;
|
|
@@ -1069,6 +1071,29 @@ export interface UsionSDK {
|
|
|
1069
1071
|
claimBackButton(callback: () => void): void;
|
|
1070
1072
|
releaseBackButton(): void;
|
|
1071
1073
|
|
|
1074
|
+
// Hybrid tabbed services (SDK ≥ 2.25)
|
|
1075
|
+
/**
|
|
1076
|
+
* Subscribe to host tab navigation on a hybrid tabbed service. Called with
|
|
1077
|
+
* `{ path, tab }` when the user taps one of this service's tabs in the host
|
|
1078
|
+
* tab bar (or a deep link targets a tab). Subscribing signals the host that
|
|
1079
|
+
* the app navigates in place; apps that never subscribe are remounted with
|
|
1080
|
+
* the new path as `getLaunchParams().path` instead. (SDK ≥ 2.25)
|
|
1081
|
+
*/
|
|
1082
|
+
onHostNavigate(callback: (nav: { path: string; tab: string | null }) => void): void;
|
|
1083
|
+
/** Unsubscribe from host tab navigation. (SDK ≥ 2.25) */
|
|
1084
|
+
offHostNavigate(): void;
|
|
1085
|
+
/**
|
|
1086
|
+
* Ask the host to switch to the native chat tab, optionally pre-filling the
|
|
1087
|
+
* message composer. The text is never sent automatically — the user reviews
|
|
1088
|
+
* it and presses send. No-op outside a hybrid tabbed host screen. (SDK ≥ 2.25)
|
|
1089
|
+
*/
|
|
1090
|
+
openChat(opts?: { prefill?: string }): void;
|
|
1091
|
+
/**
|
|
1092
|
+
* Report the app's current internal path so the host keeps its tab highlight
|
|
1093
|
+
* in sync with in-app navigation. Fire-and-forget. (SDK ≥ 2.25)
|
|
1094
|
+
*/
|
|
1095
|
+
reportPath(path: string): void;
|
|
1096
|
+
|
|
1072
1097
|
// Download
|
|
1073
1098
|
download(url: string, filename?: string): Promise<{ success?: boolean; error?: string }>;
|
|
1074
1099
|
|
|
@@ -1227,7 +1252,7 @@ export interface LeaderboardEntry {
|
|
|
1227
1252
|
export interface LeaderboardModule {
|
|
1228
1253
|
/** Submit a score (best is kept per the service's order config). Opt-in per game. */
|
|
1229
1254
|
submit(score: number, metadata?: any, opts?: { serviceId?: string }): Promise<{ success: boolean; score: number; best: number; rank: number; updated: boolean }>;
|
|
1230
|
-
/** Leaderboard of
|
|
1255
|
+
/** Leaderboard of your accepted friends (plus you). */
|
|
1231
1256
|
friends(opts?: { serviceId?: string; limit?: number }): Promise<LeaderboardEntry[]>;
|
|
1232
1257
|
/** Global top N. */
|
|
1233
1258
|
top(opts?: { serviceId?: string; limit?: number }): Promise<LeaderboardEntry[]>;
|