@ksaa-nlp/gamehub-react-native-sdk 0.0.0-stage → 1.3.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 +315 -2
- package/SPEC.md +294 -0
- package/dist/src/answers.d.ts +5 -0
- package/dist/src/answers.d.ts.map +1 -0
- package/dist/src/answers.js +32 -0
- package/dist/src/answers.js.map +1 -0
- package/dist/src/api-client.d.ts +7 -0
- package/dist/src/api-client.d.ts.map +1 -0
- package/dist/src/api-client.js +123 -0
- package/dist/src/api-client.js.map +1 -0
- package/dist/src/config.d.ts +15 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +45 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/constants.d.ts +6 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +6 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/content.d.ts +7 -0
- package/dist/src/content.d.ts.map +1 -0
- package/dist/src/content.js +70 -0
- package/dist/src/content.js.map +1 -0
- package/dist/src/host-events.d.ts +7 -0
- package/dist/src/host-events.d.ts.map +1 -0
- package/dist/src/host-events.js +20 -0
- package/dist/src/host-events.js.map +1 -0
- package/dist/src/identity.d.ts +10 -0
- package/dist/src/identity.d.ts.map +1 -0
- package/dist/src/identity.js +27 -0
- package/dist/src/identity.js.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +75 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/launch.d.ts +16 -0
- package/dist/src/launch.d.ts.map +1 -0
- package/dist/src/launch.js +115 -0
- package/dist/src/launch.js.map +1 -0
- package/dist/src/loading.d.ts +8 -0
- package/dist/src/loading.d.ts.map +1 -0
- package/dist/src/loading.js +96 -0
- package/dist/src/loading.js.map +1 -0
- package/dist/src/lobby-request.d.ts +4 -0
- package/dist/src/lobby-request.d.ts.map +1 -0
- package/dist/src/lobby-request.js +46 -0
- package/dist/src/lobby-request.js.map +1 -0
- package/dist/src/lobby.d.ts +34 -0
- package/dist/src/lobby.d.ts.map +1 -0
- package/dist/src/lobby.js +311 -0
- package/dist/src/lobby.js.map +1 -0
- package/dist/src/match-context.d.ts +10 -0
- package/dist/src/match-context.d.ts.map +1 -0
- package/dist/src/match-context.js +41 -0
- package/dist/src/match-context.js.map +1 -0
- package/dist/src/multiplayer-client.d.ts +27 -0
- package/dist/src/multiplayer-client.d.ts.map +1 -0
- package/dist/src/multiplayer-client.js +163 -0
- package/dist/src/multiplayer-client.js.map +1 -0
- package/dist/src/multiplayer-react.d.ts +4 -0
- package/dist/src/multiplayer-react.d.ts.map +1 -0
- package/dist/src/multiplayer-react.js +8 -0
- package/dist/src/multiplayer-react.js.map +1 -0
- package/dist/src/multiplayer-storage.d.ts +22 -0
- package/dist/src/multiplayer-storage.d.ts.map +1 -0
- package/dist/src/multiplayer-storage.js +65 -0
- package/dist/src/multiplayer-storage.js.map +1 -0
- package/dist/src/multiplayer-types.d.ts +56 -0
- package/dist/src/multiplayer-types.d.ts.map +1 -0
- package/dist/src/multiplayer-types.js +2 -0
- package/dist/src/multiplayer-types.js.map +1 -0
- package/dist/src/multiplayer.d.ts +5 -0
- package/dist/src/multiplayer.d.ts.map +1 -0
- package/dist/src/multiplayer.js +4 -0
- package/dist/src/multiplayer.js.map +1 -0
- package/dist/src/react.d.ts +12 -0
- package/dist/src/react.d.ts.map +1 -0
- package/dist/src/react.js +24 -0
- package/dist/src/react.js.map +1 -0
- package/dist/src/score.d.ts +9 -0
- package/dist/src/score.d.ts.map +1 -0
- package/dist/src/score.js +168 -0
- package/dist/src/score.js.map +1 -0
- package/dist/src/session-view.d.ts +4 -0
- package/dist/src/session-view.d.ts.map +1 -0
- package/dist/src/session-view.js +276 -0
- package/dist/src/session-view.js.map +1 -0
- package/dist/src/session.d.ts +10 -0
- package/dist/src/session.d.ts.map +1 -0
- package/dist/src/session.js +221 -0
- package/dist/src/session.js.map +1 -0
- package/dist/src/settings.d.ts +4 -0
- package/dist/src/settings.d.ts.map +1 -0
- package/dist/src/settings.js +40 -0
- package/dist/src/settings.js.map +1 -0
- package/dist/src/types.d.ts +137 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils.d.ts +5 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +16 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/vendor/colyseus-schemas/clock.d.ts +15 -0
- package/dist/vendor/colyseus-schemas/clock.js +67 -0
- package/dist/vendor/colyseus-schemas/common.d.ts +35 -0
- package/dist/vendor/colyseus-schemas/common.js +15 -0
- package/dist/vendor/colyseus-schemas/multiplayer.d.ts +8 -0
- package/dist/vendor/colyseus-schemas/multiplayer.js +26 -0
- package/dist/vendor/colyseus-schemas/ugc.d.ts +53 -0
- package/dist/vendor/colyseus-schemas/ugc.js +124 -0
- package/dist/vendor/gamehub-multiplayer-contracts/index.d.ts +200 -0
- package/dist/vendor/gamehub-multiplayer-contracts/index.js +289 -0
- package/dist/vendor/loading.d.ts +6 -0
- package/dist/vendor/loading.d.ts.map +1 -0
- package/dist/vendor/loading.js +2 -0
- package/dist/vendor/loading.js.map +1 -0
- package/dist/vendor/match.d.ts +37 -0
- package/dist/vendor/match.d.ts.map +1 -0
- package/dist/vendor/match.js +2 -0
- package/dist/vendor/match.js.map +1 -0
- package/dist/vendor/multiplayer-client.d.ts +52 -0
- package/dist/vendor/multiplayer-client.d.ts.map +1 -0
- package/dist/vendor/multiplayer-client.js +286 -0
- package/dist/vendor/multiplayer-client.js.map +1 -0
- package/dist/vendor/multiplayer-session.d.ts +82 -0
- package/dist/vendor/multiplayer-session.d.ts.map +1 -0
- package/dist/vendor/multiplayer-session.js +2 -0
- package/dist/vendor/multiplayer-session.js.map +1 -0
- package/dist/vendor/question.d.ts +35 -0
- package/dist/vendor/question.d.ts.map +1 -0
- package/dist/vendor/question.js +20 -0
- package/dist/vendor/question.js.map +1 -0
- package/dist/vendor/session.d.ts +174 -0
- package/dist/vendor/session.d.ts.map +1 -0
- package/dist/vendor/session.js +2 -0
- package/dist/vendor/session.js.map +1 -0
- package/dist/vendor/word.d.ts +147 -0
- package/dist/vendor/word.d.ts.map +1 -0
- package/dist/vendor/word.js +15 -0
- package/dist/vendor/word.js.map +1 -0
- package/package.json +90 -4
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { ERROR_CODES, isLaunchOutcome, normalizeError, } from '#multiplayer-contracts';
|
|
2
|
+
import { freeze } from './utils.js';
|
|
3
|
+
/** Bound launch reservation requests; no sockets, polling or credential persistence. */
|
|
4
|
+
export class NativeLaunch {
|
|
5
|
+
constructor(config, slug, transport, currentLaunch, getGeneration) {
|
|
6
|
+
this.config = config;
|
|
7
|
+
this.slug = slug;
|
|
8
|
+
this.transport = transport;
|
|
9
|
+
this.currentLaunch = currentLaunch;
|
|
10
|
+
this.getGeneration = getGeneration;
|
|
11
|
+
this.launching = null;
|
|
12
|
+
this.request = () => {
|
|
13
|
+
const fail = (code, message, retryable = false) => ({
|
|
14
|
+
status: retryable ? 'recoverable' : 'terminal',
|
|
15
|
+
error: { code, message, retryable },
|
|
16
|
+
});
|
|
17
|
+
if (!this.config)
|
|
18
|
+
return Promise.resolve(fail('PROTOCOL_UNSUPPORTED', 'Configure native multiplayer first'));
|
|
19
|
+
const launch = this.currentLaunch();
|
|
20
|
+
if (!launch)
|
|
21
|
+
return Promise.resolve(fail('STALE_ATTEMPT', 'No current round'));
|
|
22
|
+
if (this.launching)
|
|
23
|
+
return this.launching;
|
|
24
|
+
const generation = this.getGeneration();
|
|
25
|
+
const config = this.config;
|
|
26
|
+
const task = Promise.resolve()
|
|
27
|
+
.then(async () => {
|
|
28
|
+
const before = this.currentLaunch();
|
|
29
|
+
if (generation !== this.getGeneration() ||
|
|
30
|
+
!before ||
|
|
31
|
+
[
|
|
32
|
+
'multiplayerSessionId',
|
|
33
|
+
'roundId',
|
|
34
|
+
'attemptId',
|
|
35
|
+
'seatId',
|
|
36
|
+
'hostInstanceId',
|
|
37
|
+
].some((k) => before[k] !==
|
|
38
|
+
launch[k]))
|
|
39
|
+
return fail('STALE_ATTEMPT', 'Launch context changed');
|
|
40
|
+
let response;
|
|
41
|
+
try {
|
|
42
|
+
response = await this.transport(`/multiplayer/sessions/${encodeURIComponent(launch.multiplayerSessionId)}/launch`, {
|
|
43
|
+
gameSlug: this.slug,
|
|
44
|
+
hostInstanceId: config.hostInstanceId,
|
|
45
|
+
roundId: launch.roundId,
|
|
46
|
+
}, config.resumeToken);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return fail('UNAVAILABLE', 'Launch unavailable', true);
|
|
50
|
+
}
|
|
51
|
+
const current = this.currentLaunch();
|
|
52
|
+
if (generation !== this.getGeneration() ||
|
|
53
|
+
!current ||
|
|
54
|
+
[
|
|
55
|
+
'multiplayerSessionId',
|
|
56
|
+
'roundId',
|
|
57
|
+
'attemptId',
|
|
58
|
+
'seatId',
|
|
59
|
+
'hostInstanceId',
|
|
60
|
+
].some((k) => current[k] !==
|
|
61
|
+
launch[k]))
|
|
62
|
+
return fail('STALE_ATTEMPT', 'Launch context changed');
|
|
63
|
+
if (!response.success) {
|
|
64
|
+
const e = response.error;
|
|
65
|
+
if (e?.code &&
|
|
66
|
+
ERROR_CODES.includes(e.code))
|
|
67
|
+
return fail(e.code, e.message ?? e.code, e.retryable ?? normalizeError(e).retryable);
|
|
68
|
+
return fail('UNAVAILABLE', 'Launch unavailable', true);
|
|
69
|
+
}
|
|
70
|
+
const outcome = response.data;
|
|
71
|
+
if (!isLaunchOutcome(outcome))
|
|
72
|
+
return fail('PROTOCOL_UNSUPPORTED', 'Invalid launch response');
|
|
73
|
+
if (outcome.status === 'reserved' &&
|
|
74
|
+
(outcome.expiresAt <= Date.now() ||
|
|
75
|
+
[
|
|
76
|
+
'multiplayerSessionId',
|
|
77
|
+
'roundId',
|
|
78
|
+
'attemptId',
|
|
79
|
+
'seatId',
|
|
80
|
+
'hostInstanceId',
|
|
81
|
+
'gameUrl',
|
|
82
|
+
'scoreAuthority',
|
|
83
|
+
].some((k) => outcome.launch[k] !==
|
|
84
|
+
launch[k])))
|
|
85
|
+
return fail('STALE_ATTEMPT', 'Stale or unbound reservation');
|
|
86
|
+
return freeze(JSON.parse(JSON.stringify(outcome)));
|
|
87
|
+
})
|
|
88
|
+
.finally(() => {
|
|
89
|
+
if (this.launching === task)
|
|
90
|
+
this.launching = null;
|
|
91
|
+
})
|
|
92
|
+
.then((outcome) => {
|
|
93
|
+
const current = this.currentLaunch();
|
|
94
|
+
if (generation !== this.getGeneration() ||
|
|
95
|
+
!current ||
|
|
96
|
+
[
|
|
97
|
+
'multiplayerSessionId',
|
|
98
|
+
'roundId',
|
|
99
|
+
'attemptId',
|
|
100
|
+
'seatId',
|
|
101
|
+
'hostInstanceId',
|
|
102
|
+
].some((k) => current[k] !==
|
|
103
|
+
launch[k]))
|
|
104
|
+
return fail('STALE_ATTEMPT', 'Launch context changed');
|
|
105
|
+
return outcome;
|
|
106
|
+
});
|
|
107
|
+
this.launching = task;
|
|
108
|
+
return task;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
reset() {
|
|
112
|
+
this.launching = null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=launch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"launch.js","sourceRoot":"","sources":["../../src/launch.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,eAAe,EACf,cAAc,GAIf,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAQpC,wFAAwF;AACxF,MAAM,OAAO,YAAY;IAEvB,YACmB,MAA4C,EAC5C,IAAY,EACZ,SAAyB,EACzB,aAA2C,EAC3C,aAA2B;QAJ3B,WAAM,GAAN,MAAM,CAAsC;QAC5C,SAAI,GAAJ,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AAO,CAAC,CAA0B,CAAC;wBACnC,MAAM,CAAC,CAA0B,CAAC,CACrC;oBAED,OAAO,IAAI,CAAC,eAAe,EAAE,wBAAwB,CAAC,CAAC;gBACzD,OAAO,OAAO,CAAC;YACjB,CAAC,CAAC,CAAC;YACL,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;IAtIC,CAAC;IACJ,KAAK;QACH,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IACxB,CAAC;CAoIF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GameLoadingLaunch, GameLoadingState, NativeSdk } from './types.js';
|
|
2
|
+
/** Presentation state only. No sockets, polling, readiness or gameplay ownership. */
|
|
3
|
+
export declare function createLoading(currentLaunch: () => GameLoadingLaunch | null, publish: (state: GameLoadingState) => void): {
|
|
4
|
+
methods: Pick<NativeSdk, "setLoading" | "getLoading" | "onLoadingChange">;
|
|
5
|
+
reconcile(): void;
|
|
6
|
+
reset(): void;
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=loading.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loading.d.ts","sourceRoot":"","sources":["../../src/loading.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACV,MAAM,YAAY,CAAC;AAwBpB,qFAAqF;AACrF,wBAAgB,aAAa,CAC3B,aAAa,EAAE,MAAM,iBAAiB,GAAG,IAAI,EAC7C,OAAO,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI;;;;EAmF3C"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const identityKeys = [
|
|
2
|
+
'multiplayerSessionId',
|
|
3
|
+
'roundId',
|
|
4
|
+
'attemptId',
|
|
5
|
+
'seatId',
|
|
6
|
+
'hostInstanceId',
|
|
7
|
+
];
|
|
8
|
+
function launchKey(launch) {
|
|
9
|
+
if (!launch ||
|
|
10
|
+
!identityKeys.every((key) => typeof launch[key] === 'string' &&
|
|
11
|
+
/^[a-zA-Z0-9_-]{1,160}$/.test(launch[key])))
|
|
12
|
+
return null;
|
|
13
|
+
return identityKeys.map((key) => launch[key]).join(':');
|
|
14
|
+
}
|
|
15
|
+
/** Presentation state only. No sockets, polling, readiness or gameplay ownership. */
|
|
16
|
+
export function createLoading(currentLaunch, publish) {
|
|
17
|
+
let game = null;
|
|
18
|
+
let multiplayer = null;
|
|
19
|
+
let multiplayerKey = null;
|
|
20
|
+
let retiredKey = null;
|
|
21
|
+
let snapshot = null;
|
|
22
|
+
const listeners = new Set();
|
|
23
|
+
function update() {
|
|
24
|
+
const states = [game, multiplayer];
|
|
25
|
+
const next = ['failed', 'reconnecting', 'loading', 'waiting-for-players'].find((state) => states.includes(state)) ?? null;
|
|
26
|
+
if (next === snapshot)
|
|
27
|
+
return;
|
|
28
|
+
snapshot = next;
|
|
29
|
+
try {
|
|
30
|
+
publish(next);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
/* Host presentation cannot break gameplay. */
|
|
34
|
+
}
|
|
35
|
+
for (const listener of [...listeners]) {
|
|
36
|
+
try {
|
|
37
|
+
listener(snapshot);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
/* Isolate consumer callbacks. */
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const methods = {
|
|
45
|
+
setLoading(state, launch) {
|
|
46
|
+
if (state !== null &&
|
|
47
|
+
!['loading', 'waiting-for-players', 'reconnecting', 'failed'].includes(state))
|
|
48
|
+
return;
|
|
49
|
+
if (launch !== undefined) {
|
|
50
|
+
const key = launchKey(launch);
|
|
51
|
+
if (!key || key === retiredKey || key !== launchKey(currentLaunch()))
|
|
52
|
+
return;
|
|
53
|
+
multiplayerKey = key;
|
|
54
|
+
multiplayer = state;
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
game = state;
|
|
58
|
+
update();
|
|
59
|
+
},
|
|
60
|
+
getLoading: () => snapshot,
|
|
61
|
+
onLoadingChange(listener) {
|
|
62
|
+
const subscription = (state) => listener(state);
|
|
63
|
+
listeners.add(subscription);
|
|
64
|
+
try {
|
|
65
|
+
subscription(snapshot);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
/* Isolate consumer callbacks. */
|
|
69
|
+
}
|
|
70
|
+
return () => {
|
|
71
|
+
listeners.delete(subscription);
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
methods,
|
|
77
|
+
reconcile() {
|
|
78
|
+
const key = launchKey(currentLaunch());
|
|
79
|
+
if (key !== multiplayerKey) {
|
|
80
|
+
multiplayerKey = null;
|
|
81
|
+
multiplayer = null;
|
|
82
|
+
update();
|
|
83
|
+
}
|
|
84
|
+
if (key !== retiredKey)
|
|
85
|
+
retiredKey = null;
|
|
86
|
+
},
|
|
87
|
+
reset() {
|
|
88
|
+
// Ignore late connection callbacks from a completed/closed/account-old round.
|
|
89
|
+
retiredKey = launchKey(currentLaunch());
|
|
90
|
+
game = multiplayer = null;
|
|
91
|
+
multiplayerKey = null;
|
|
92
|
+
update();
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=loading.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loading.js","sourceRoot":"","sources":["../../src/loading.ts"],"names":[],"mappings":"AAMA,MAAM,YAAY,GAAG;IACnB,sBAAsB;IACtB,SAAS;IACT,WAAW;IACX,QAAQ;IACR,gBAAgB;CACR,CAAC;AACX,SAAS,SAAS,CAChB,MAA4C;IAE5C,IACE,CAAC,MAAM;QACP,CAAC,YAAY,CAAC,KAAK,CACjB,CAAC,GAAG,EAAE,EAAE,CACN,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,QAAQ;YAC/B,wBAAwB,CAAC,IAAI,CAAC,MA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A8E;YAC9E,UAAU,GAAG,SAAS,CAAC,aAAa,EAAE,CAAC,CAAC;YACxC,IAAI,GAAG,WAAW,GAAG,IAAI,CAAC;YAC1B,cAAc,GAAG,IAAI,CAAC;YACtB,MAAM,EAAE,CAAC;QACX,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { MultiplayerSessionRequest } from './types.js';
|
|
2
|
+
/** Validate game-owned lobby inputs; authority fields are supplied by the SDK. */
|
|
3
|
+
export declare function validRequest(value: unknown): value is MultiplayerSessionRequest;
|
|
4
|
+
//# sourceMappingURL=lobby-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lobby-request.d.ts","sourceRoot":"","sources":["../../src/lobby-request.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAE5D,kFAAkF;AAClF,wBAAgB,YAAY,CAC1B,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,yBAAyB,CA+CpC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isBoundedId, isBotLevel, isRecord } from '#multiplayer-contracts';
|
|
2
|
+
/** Validate game-owned lobby inputs; authority fields are supplied by the SDK. */
|
|
3
|
+
export function validRequest(value) {
|
|
4
|
+
if (!isRecord(value) || typeof value.operation !== 'string')
|
|
5
|
+
return false;
|
|
6
|
+
const keys = {
|
|
7
|
+
create: ['operation', 'type', 'passcode'],
|
|
8
|
+
join: ['operation', 'roomCode', 'displayName', 'type', 'passcode'],
|
|
9
|
+
ready: ['operation', 'ready'],
|
|
10
|
+
'add-bot': ['operation', 'level'],
|
|
11
|
+
'remove-bot': ['operation', 'seatId'],
|
|
12
|
+
read: ['operation'],
|
|
13
|
+
start: ['operation'],
|
|
14
|
+
leave: ['operation'],
|
|
15
|
+
rematch: ['operation'],
|
|
16
|
+
};
|
|
17
|
+
const allowed = Object.hasOwnProperty.call(keys, value.operation)
|
|
18
|
+
? keys[value.operation]
|
|
19
|
+
: undefined;
|
|
20
|
+
if (!allowed || Object.keys(value).some((k) => !allowed.includes(k)))
|
|
21
|
+
return false;
|
|
22
|
+
if (value.operation === 'create' || value.operation === 'join') {
|
|
23
|
+
if (typeof value.type !== 'string' ||
|
|
24
|
+
!['player', 'spectator'].includes(value.type) ||
|
|
25
|
+
(value.passcode !== undefined &&
|
|
26
|
+
(typeof value.passcode !== 'string' ||
|
|
27
|
+
!value.passcode.length ||
|
|
28
|
+
value.passcode.length > 64)))
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
if (value.operation === 'join' &&
|
|
32
|
+
(typeof value.roomCode !== 'string' ||
|
|
33
|
+
!/^\d{6}$/.test(value.roomCode) ||
|
|
34
|
+
typeof value.displayName !== 'string' ||
|
|
35
|
+
!value.displayName.trim() ||
|
|
36
|
+
value.displayName.trim().length > 64))
|
|
37
|
+
return false;
|
|
38
|
+
if (value.operation === 'ready' && typeof value.ready !== 'boolean')
|
|
39
|
+
return false;
|
|
40
|
+
if (value.operation === 'remove-bot' && !isBoundedId(value.seatId))
|
|
41
|
+
return false;
|
|
42
|
+
return (value.operation !== 'add-bot' ||
|
|
43
|
+
value.level === undefined ||
|
|
44
|
+
isBotLevel(value.level));
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=lobby-request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lobby-request.js","sourceRoot":"","sources":["../../src/lobby-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAG3E,kFAAkF;AAClF,MAAM,UAAU,YAAY,CAC1B,KAAc;IAEd,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,SAAS,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC1E,MAAM,IAAI,GAA6B;QACrC,MAAM,EAAE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EhC,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IACE,KAAK,CAAC,SAAS,KAAK,MAAM;QAC1B,CAAC,OAAO,KAAK,CAAC,QAAQ,KAAK,QAAQ;YACjC,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;YAC/B,OAAO,KAAK,CAAC,WAAW,KAAK,QAAQ;YACrC,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE;YACzB,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,EAAE,CAAC;QAEvC,OAAO,KAAK,CAAC;IACf,IAAI,KAAK,CAAC,SAAS,KAAK,OAAO,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,SAAS;QACjE,OAAO,KAAK,CAAC;IACf,IAAI,KAAK,CAAC,SAAS,KAAK,YAAY,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC;QAChE,OAAO,KAAK,CAAC;IACf,OAAO,CACL,KAAK,CAAC,SAAS,KAAK,SAAS;QAC7B,KAAK,CAAC,KAAK,KAAK,SAAS;QACzB,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CACxB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type LaunchReference } from '#multiplayer-contracts';
|
|
2
|
+
import type { NativeMultiplayerOptions, NativeMultiplayerSessionState, NativeMultiplayerSessionResult, MultiplayerSessionRequest } from './types.js';
|
|
3
|
+
import { type LobbyTransport } from './launch.js';
|
|
4
|
+
export declare class NativeLobby {
|
|
5
|
+
private readonly slug;
|
|
6
|
+
private readonly profileId;
|
|
7
|
+
private readonly transport;
|
|
8
|
+
private readonly takeOwnership;
|
|
9
|
+
private readonly onUpdate?;
|
|
10
|
+
private state;
|
|
11
|
+
private listeners;
|
|
12
|
+
private generation;
|
|
13
|
+
private pending;
|
|
14
|
+
private readonly launchRequests;
|
|
15
|
+
private timer;
|
|
16
|
+
private sessionId;
|
|
17
|
+
private createKey;
|
|
18
|
+
private owned;
|
|
19
|
+
private leaving;
|
|
20
|
+
private readonly config?;
|
|
21
|
+
constructor(config: NativeMultiplayerOptions | undefined, slug: string, profileId: string | undefined, transport: LobbyTransport, takeOwnership: () => void, onUpdate?: (() => void) | undefined);
|
|
22
|
+
owns: () => boolean;
|
|
23
|
+
snapshot: () => NativeMultiplayerSessionState;
|
|
24
|
+
launch: () => LaunchReference | null;
|
|
25
|
+
subscribe: (listener: (value: NativeMultiplayerSessionState) => void) => () => void;
|
|
26
|
+
private update;
|
|
27
|
+
private stopPoll;
|
|
28
|
+
private schedule;
|
|
29
|
+
reset(): void;
|
|
30
|
+
request: (input: MultiplayerSessionRequest) => Promise<NativeMultiplayerSessionResult>;
|
|
31
|
+
private malformed;
|
|
32
|
+
requestLaunch: () => Promise<import("#multiplayer-contracts").LaunchOutcome>;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=lobby.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lobby.d.ts","sourceRoot":"","sources":["../../src/lobby.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,eAAe,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAEV,wBAAwB,EACxB,6BAA6B,EAC7B,8BAA8B,EAC9B,yBAAyB,EAC1B,MAAM,YAAY,CAAC;AAKpB,OAAO,EAAgB,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAehE,qBAAa,WAAW;IAsBpB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC;IAzB5B,OAAO,CAAC,KAAK,CAKV;IACH,OAAO,CAAC,SAAS,CAA6D;IAC9E,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,OAAO,CAGC;IAChB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAe;IAC9C,OAAO,CAAC,KAAK,CAA4C;IACzD,OAAO,CAAC,SAAS,CAAgB;IACjC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,KAAK,CAAU;IACvB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAA2B;gBAEjD,MAAM,EAAE,wBAAwB,GAAG,SAAS,EAC3B,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,SAAS,EAAE,cAAc,EACzB,aAAa,EAAE,MAAM,IAAI,EACzB,QAAQ,CAAC,GAAE,MAAM,IAAI,aAAA;IA6BxC,IAAI,gBAAoB;IACxB,QAAQ,sCAAoB;IAC5B,MAAM,QAAO,eAAe,GAAG,IAAI,CAUjC;IACF,SAAS,GAAI,UAAU,CAAC,KAAK,EAAE,6BAA6B,KAAK,IAAI,gBAcnE;IACF,OAAO,CAAC,MAAM;IAYd,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,QAAQ;IAShB,KAAK;IAgBL,OAAO,GACL,OAAO,yBAAyB,KAC/B,OAAO,CAAC,8BAA8B,CAAC,CAuNxC;IACF,OAAO,CAAC,SAAS;IAQjB,aAAa,gEAAuC;CACrD"}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { canonicalJson, isBoundedId, isRecord, } from '#multiplayer-contracts';
|
|
2
|
+
import { sessionView } from './session-view.js';
|
|
3
|
+
import { freeze, integer } from './utils.js';
|
|
4
|
+
import { validRequest } from './lobby-request.js';
|
|
5
|
+
import { NativeLaunch } from './launch.js';
|
|
6
|
+
const error = (code, message, retryable = false) => ({
|
|
7
|
+
code,
|
|
8
|
+
message,
|
|
9
|
+
retryable,
|
|
10
|
+
});
|
|
11
|
+
const failed = (code, message, retryable = false) => ({
|
|
12
|
+
success: false,
|
|
13
|
+
error: error(code, message, retryable),
|
|
14
|
+
});
|
|
15
|
+
export class NativeLobby {
|
|
16
|
+
constructor(config, slug, profileId, transport, takeOwnership, onUpdate) {
|
|
17
|
+
this.slug = slug;
|
|
18
|
+
this.profileId = profileId;
|
|
19
|
+
this.transport = transport;
|
|
20
|
+
this.takeOwnership = takeOwnership;
|
|
21
|
+
this.onUpdate = onUpdate;
|
|
22
|
+
this.state = freeze({
|
|
23
|
+
session: null,
|
|
24
|
+
busy: false,
|
|
25
|
+
error: null,
|
|
26
|
+
disconnected: false,
|
|
27
|
+
});
|
|
28
|
+
this.listeners = new Set();
|
|
29
|
+
this.generation = 0;
|
|
30
|
+
this.pending = null;
|
|
31
|
+
this.createKey = null;
|
|
32
|
+
this.leaving = false;
|
|
33
|
+
this.owns = () => this.owned;
|
|
34
|
+
this.snapshot = () => this.state;
|
|
35
|
+
this.launch = () => {
|
|
36
|
+
const session = this.state.session;
|
|
37
|
+
if (this.leaving ||
|
|
38
|
+
!session?.seats.some((s) => s.seatId === session.ownSeatId && s.status === 'joined'))
|
|
39
|
+
return null;
|
|
40
|
+
return session.round?.launch ?? null;
|
|
41
|
+
};
|
|
42
|
+
this.subscribe = (listener) => {
|
|
43
|
+
// Unique subscription wrappers allow the same callback to subscribe independently.
|
|
44
|
+
const subscription = (s) => listener(s);
|
|
45
|
+
this.listeners.add(subscription);
|
|
46
|
+
try {
|
|
47
|
+
subscription(this.state);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
/* Isolate app callbacks. */
|
|
51
|
+
}
|
|
52
|
+
this.schedule();
|
|
53
|
+
return () => {
|
|
54
|
+
this.listeners.delete(subscription);
|
|
55
|
+
if (!this.listeners.size)
|
|
56
|
+
this.stopPoll();
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
this.request = (input) => {
|
|
60
|
+
const config = this.config;
|
|
61
|
+
if (!config)
|
|
62
|
+
return Promise.resolve(failed('PROTOCOL_UNSUPPORTED', 'Configure native multiplayer first'));
|
|
63
|
+
if (!validRequest(input))
|
|
64
|
+
return Promise.resolve(failed('INVALID_REQUEST', 'Invalid lobby request'));
|
|
65
|
+
const normalized = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));
|
|
66
|
+
if (input.operation === 'join')
|
|
67
|
+
normalized.displayName = input.displayName.trim();
|
|
68
|
+
const key = canonicalJson(normalized);
|
|
69
|
+
if (this.pending)
|
|
70
|
+
return this.pending.key === key
|
|
71
|
+
? this.pending.task
|
|
72
|
+
: Promise.resolve(failed('BUSY', 'A lobby request is in progress', true));
|
|
73
|
+
const operation = input.operation;
|
|
74
|
+
if ((operation === 'create' || operation === 'join') && this.sessionId)
|
|
75
|
+
return Promise.resolve(failed('BUSY', 'Leave the current session before joining another'));
|
|
76
|
+
if (!['create', 'join', 'leave'].includes(operation) && !this.sessionId)
|
|
77
|
+
return Promise.resolve(failed('STALE_ATTEMPT', 'No multiplayer session'));
|
|
78
|
+
let body = {
|
|
79
|
+
gameSlug: this.slug,
|
|
80
|
+
hostInstanceId: config.hostInstanceId,
|
|
81
|
+
};
|
|
82
|
+
if (input.operation === 'create') {
|
|
83
|
+
if (!this.createKey) {
|
|
84
|
+
try {
|
|
85
|
+
this.createKey = config.createId();
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return Promise.resolve(failed('INVALID_REQUEST', 'createId failed'));
|
|
89
|
+
}
|
|
90
|
+
if (!isBoundedId(this.createKey)) {
|
|
91
|
+
this.createKey = null;
|
|
92
|
+
return Promise.resolve(failed('INVALID_REQUEST', 'createId must return a bounded secure ID'));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
body = {
|
|
96
|
+
...body,
|
|
97
|
+
key: this.createKey,
|
|
98
|
+
type: input.type,
|
|
99
|
+
...(input.passcode ? { passcode: input.passcode } : {}),
|
|
100
|
+
...(this.profileId ? { profileId: this.profileId } : {}),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
else if (input.operation === 'join') {
|
|
104
|
+
body = {
|
|
105
|
+
...body,
|
|
106
|
+
roomCode: input.roomCode,
|
|
107
|
+
displayName: input.displayName.trim(),
|
|
108
|
+
type: input.type,
|
|
109
|
+
...(input.passcode ? { passcode: input.passcode } : {}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
else if (input.operation === 'ready')
|
|
113
|
+
body.ready = input.ready;
|
|
114
|
+
else if (['start', 'rematch', 'add-bot', 'remove-bot'].includes(operation)) {
|
|
115
|
+
if (!this.state.session)
|
|
116
|
+
return Promise.resolve(failed('STALE_ATTEMPT', 'Read the session before mutating it'));
|
|
117
|
+
body.revision = this.state.session.revision;
|
|
118
|
+
if (input.operation === 'remove-bot')
|
|
119
|
+
body.seatId = input.seatId;
|
|
120
|
+
if (input.operation === 'add-bot' && input.level)
|
|
121
|
+
body.level = input.level;
|
|
122
|
+
}
|
|
123
|
+
if (!this.owned && operation !== 'leave') {
|
|
124
|
+
this.owned = true;
|
|
125
|
+
this.takeOwnership();
|
|
126
|
+
}
|
|
127
|
+
const generation = this.generation;
|
|
128
|
+
let completionGeneration = generation;
|
|
129
|
+
const bound = this.sessionId;
|
|
130
|
+
this.leaving = operation === 'leave';
|
|
131
|
+
const path = operation === 'create'
|
|
132
|
+
? '/multiplayer/sessions'
|
|
133
|
+
: operation === 'join'
|
|
134
|
+
? '/multiplayer/sessions/join'
|
|
135
|
+
: `/multiplayer/sessions/${encodeURIComponent(bound ?? '')}/${operation}`;
|
|
136
|
+
this.stopPoll();
|
|
137
|
+
// Publish busy after pending is installed below, so reentrant listeners cannot issue a second mutation.
|
|
138
|
+
const task = Promise.resolve()
|
|
139
|
+
.then(async () => {
|
|
140
|
+
if (generation !== this.generation)
|
|
141
|
+
return failed('IDENTITY_CHANGED', 'Authentication changed');
|
|
142
|
+
if (operation === 'leave' && !bound) {
|
|
143
|
+
completionGeneration = ++this.generation;
|
|
144
|
+
this.owned = false;
|
|
145
|
+
this.createKey = null;
|
|
146
|
+
this.update({
|
|
147
|
+
session: null,
|
|
148
|
+
busy: false,
|
|
149
|
+
error: null,
|
|
150
|
+
disconnected: false,
|
|
151
|
+
});
|
|
152
|
+
return { success: true, data: this.state };
|
|
153
|
+
}
|
|
154
|
+
let response;
|
|
155
|
+
try {
|
|
156
|
+
response = await this.transport(path, body, config.resumeToken);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
response = {
|
|
160
|
+
success: false,
|
|
161
|
+
error: {
|
|
162
|
+
code: 'UNAVAILABLE',
|
|
163
|
+
message: 'Lobby unavailable',
|
|
164
|
+
retryable: true,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
if (generation !== this.generation)
|
|
169
|
+
return failed('IDENTITY_CHANGED', 'Authentication changed');
|
|
170
|
+
if (!response.success) {
|
|
171
|
+
const e = response.error;
|
|
172
|
+
const failure = failed(e?.code ?? 'UNAVAILABLE', e?.message ?? 'Lobby unavailable', e?.retryable ?? e?.details?.terminal !== true);
|
|
173
|
+
if (!failure.success)
|
|
174
|
+
this.update({
|
|
175
|
+
error: failure.error,
|
|
176
|
+
disconnected: operation === 'read',
|
|
177
|
+
});
|
|
178
|
+
return failure;
|
|
179
|
+
}
|
|
180
|
+
if (operation === 'leave') {
|
|
181
|
+
if (!isRecord(response.data) || response.data.left !== true)
|
|
182
|
+
return this.malformed();
|
|
183
|
+
++this.generation;
|
|
184
|
+
completionGeneration = this.generation;
|
|
185
|
+
this.sessionId = null;
|
|
186
|
+
this.createKey = null;
|
|
187
|
+
this.owned = false;
|
|
188
|
+
this.launchRequests.reset();
|
|
189
|
+
this.update({
|
|
190
|
+
session: null,
|
|
191
|
+
busy: false,
|
|
192
|
+
error: null,
|
|
193
|
+
disconnected: false,
|
|
194
|
+
});
|
|
195
|
+
return { success: true, data: this.state };
|
|
196
|
+
}
|
|
197
|
+
const view = sessionView(response.data, this.slug, config.hostInstanceId);
|
|
198
|
+
if (!view ||
|
|
199
|
+
(bound && view.multiplayerSessionId !== bound) ||
|
|
200
|
+
(this.state.session &&
|
|
201
|
+
view.ownSeatId !== this.state.session.ownSeatId))
|
|
202
|
+
return this.malformed();
|
|
203
|
+
const previous = this.state.session;
|
|
204
|
+
if (previous &&
|
|
205
|
+
(view.revision < previous.revision ||
|
|
206
|
+
(view.round?.roundId === previous.round?.roundId &&
|
|
207
|
+
(view.round?.revision ?? 0) < (previous.round?.revision ?? 0)))) {
|
|
208
|
+
const failure = failed('STALE_REVISION', 'Lobby response is older than the current session', true);
|
|
209
|
+
if (!failure.success)
|
|
210
|
+
this.update({ error: failure.error });
|
|
211
|
+
return failure;
|
|
212
|
+
}
|
|
213
|
+
this.sessionId = view.multiplayerSessionId;
|
|
214
|
+
this.update({ session: view, error: null, disconnected: false });
|
|
215
|
+
return { success: true, data: this.state };
|
|
216
|
+
})
|
|
217
|
+
.finally(() => {
|
|
218
|
+
if (this.pending?.task === task) {
|
|
219
|
+
// A failed first admission has no bound session to own. Keep the
|
|
220
|
+
// create key for retries, but allow the caller to choose solo play.
|
|
221
|
+
if (!this.sessionId &&
|
|
222
|
+
(operation === 'create' || operation === 'join'))
|
|
223
|
+
this.owned = false;
|
|
224
|
+
this.pending = null;
|
|
225
|
+
this.leaving = false;
|
|
226
|
+
this.update({ busy: false });
|
|
227
|
+
this.schedule();
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
.then((result) => completionGeneration === this.generation
|
|
231
|
+
? result.success
|
|
232
|
+
? {
|
|
233
|
+
success: true,
|
|
234
|
+
data: freeze({ ...result.data, busy: false }),
|
|
235
|
+
}
|
|
236
|
+
: result
|
|
237
|
+
: failed('IDENTITY_CHANGED', 'Authentication changed'));
|
|
238
|
+
this.pending = { key, task };
|
|
239
|
+
this.update({ busy: true, error: null });
|
|
240
|
+
return task;
|
|
241
|
+
};
|
|
242
|
+
this.requestLaunch = () => this.launchRequests.request();
|
|
243
|
+
if (config) {
|
|
244
|
+
if (!isBoundedId(config.hostInstanceId) ||
|
|
245
|
+
typeof config.createId !== 'function' ||
|
|
246
|
+
!/^[a-z0-9-]{1,100}$/.test(slug) ||
|
|
247
|
+
(config.sessionId !== undefined && !isBoundedId(config.sessionId)) ||
|
|
248
|
+
(config.resumeToken !== undefined &&
|
|
249
|
+
!/^[A-Za-z0-9_-]{43,128}$/.test(config.resumeToken)) ||
|
|
250
|
+
(profileId !== undefined && !/^[a-f0-9]{24}$/.test(profileId)) ||
|
|
251
|
+
(config.pollIntervalMs !== undefined &&
|
|
252
|
+
(!integer(config.pollIntervalMs) ||
|
|
253
|
+
config.pollIntervalMs < 1000 ||
|
|
254
|
+
config.pollIntervalMs > 60000)))
|
|
255
|
+
throw new TypeError('Invalid multiplayer configuration');
|
|
256
|
+
this.config = { ...config };
|
|
257
|
+
}
|
|
258
|
+
this.sessionId = config?.sessionId ?? null;
|
|
259
|
+
this.owned = Boolean(this.sessionId);
|
|
260
|
+
this.launchRequests = new NativeLaunch(this.config, this.slug, this.transport, this.launch, () => this.generation);
|
|
261
|
+
}
|
|
262
|
+
update(change) {
|
|
263
|
+
this.state = freeze({ ...this.state, ...change });
|
|
264
|
+
// Internal lifecycle observers do not subscribe and must not start polling.
|
|
265
|
+
this.onUpdate?.();
|
|
266
|
+
for (const listener of [...this.listeners]) {
|
|
267
|
+
try {
|
|
268
|
+
listener(this.state);
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
/* Isolate app callbacks. */
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
stopPoll() {
|
|
276
|
+
clearTimeout(this.timer);
|
|
277
|
+
this.timer = undefined;
|
|
278
|
+
}
|
|
279
|
+
schedule() {
|
|
280
|
+
this.stopPoll();
|
|
281
|
+
if (!this.config || !this.listeners.size || !this.sessionId || this.pending)
|
|
282
|
+
return;
|
|
283
|
+
this.timer = setTimeout(() => {
|
|
284
|
+
this.timer = undefined;
|
|
285
|
+
void this.request({ operation: 'read' });
|
|
286
|
+
}, this.config.pollIntervalMs ?? 2500);
|
|
287
|
+
}
|
|
288
|
+
reset() {
|
|
289
|
+
++this.generation;
|
|
290
|
+
this.stopPoll();
|
|
291
|
+
this.pending = null;
|
|
292
|
+
this.launchRequests.reset();
|
|
293
|
+
this.sessionId = null;
|
|
294
|
+
this.createKey = null;
|
|
295
|
+
this.owned = false;
|
|
296
|
+
this.leaving = false;
|
|
297
|
+
this.update({
|
|
298
|
+
session: null,
|
|
299
|
+
busy: false,
|
|
300
|
+
error: null,
|
|
301
|
+
disconnected: false,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
malformed() {
|
|
305
|
+
const failure = failed('MALFORMED_RESPONSE', 'Invalid or unbound multiplayer response');
|
|
306
|
+
if (!failure.success)
|
|
307
|
+
this.update({ error: failure.error });
|
|
308
|
+
return failure;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
//# sourceMappingURL=lobby.js.map
|