@ksaa-nlp/gamehub-sdk 0.1.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +211 -0
- package/MIGRATION.md +322 -0
- package/README.md +121 -182
- package/dist/gamehub-sdk.js +1194 -1670
- package/dist/gamehub-sdk.js.map +7 -0
- package/dist/src/api-client.d.ts +11 -0
- package/dist/src/api-client.d.ts.map +1 -0
- package/dist/src/api-client.js +27 -0
- package/dist/src/api-client.js.map +1 -0
- package/dist/src/bridge.d.ts +35 -0
- package/dist/src/bridge.d.ts.map +1 -0
- package/dist/src/bridge.js +280 -0
- package/dist/src/bridge.js.map +1 -0
- package/dist/src/browser-entry.d.ts +19 -0
- package/dist/src/browser-entry.d.ts.map +1 -0
- package/dist/src/browser-entry.js +49 -0
- package/dist/src/browser-entry.js.map +1 -0
- package/dist/src/constants.d.ts +18 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +18 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/init.d.ts +36 -0
- package/dist/src/init.d.ts.map +1 -0
- package/dist/src/init.js +317 -0
- package/dist/src/init.js.map +1 -0
- package/dist/src/main.d.ts +94 -0
- package/dist/src/main.d.ts.map +1 -0
- package/dist/src/main.js +126 -0
- package/dist/src/main.js.map +1 -0
- package/dist/src/match-context.d.ts +11 -0
- package/dist/src/match-context.d.ts.map +1 -0
- package/dist/src/match-context.js +51 -0
- package/dist/src/match-context.js.map +1 -0
- package/dist/src/multiplayer.d.ts +76 -0
- package/dist/src/multiplayer.d.ts.map +1 -0
- package/dist/src/multiplayer.js +439 -0
- package/dist/src/multiplayer.js.map +1 -0
- package/dist/src/session.d.ts +35 -0
- package/dist/src/session.d.ts.map +1 -0
- package/dist/src/session.js +206 -0
- package/dist/src/session.js.map +1 -0
- package/dist/src/state.d.ts +71 -0
- package/dist/src/state.d.ts.map +1 -0
- package/dist/src/state.js +55 -0
- package/dist/src/state.js.map +1 -0
- package/dist/src/utils.d.ts +36 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +73 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/types/api.d.ts +21 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/api.js +2 -0
- package/dist/types/api.js.map +1 -0
- package/{types/bootstrap.ts → dist/types/bootstrap.d.ts} +26 -29
- package/dist/types/bootstrap.d.ts.map +1 -0
- package/dist/types/bootstrap.js +2 -0
- package/dist/types/bootstrap.js.map +1 -0
- package/dist/types/match.d.ts +37 -0
- package/dist/types/match.d.ts.map +1 -0
- package/dist/types/match.js +2 -0
- package/dist/types/match.js.map +1 -0
- package/dist/types/multiplayer.d.ts +107 -0
- package/dist/types/multiplayer.d.ts.map +1 -0
- package/dist/types/multiplayer.js +2 -0
- package/dist/types/multiplayer.js.map +1 -0
- package/{types/session.ts → dist/types/session.d.ts} +24 -30
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/session.js +2 -0
- package/dist/types/session.js.map +1 -0
- package/package.json +34 -18
- package/dist/gamehub-sdk.d.ts +0 -694
- package/dist/index.d.ts +0 -56
- package/dist/index.mjs +0 -6
- package/scripts/emit-module-entry.mjs +0 -96
- package/src/api-client.ts +0 -33
- package/src/bridge.ts +0 -312
- package/src/constants.ts +0 -24
- package/src/init.ts +0 -335
- package/src/main.ts +0 -287
- package/src/match-context.ts +0 -52
- package/src/multiplayer.ts +0 -525
- package/src/session.ts +0 -240
- package/src/state.ts +0 -78
- package/src/utils.ts +0 -68
- package/tsconfig.json +0 -33
- package/types/api.ts +0 -21
- package/types/match.ts +0 -41
- package/types/multiplayer.ts +0 -118
package/dist/src/main.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public SDK module entry. Exports `createSdk` (the factory) and every
|
|
3
|
+
* public type. Importing this module has no side effects — it does not
|
|
4
|
+
* touch `window`, `document`, or run any boot logic.
|
|
5
|
+
*
|
|
6
|
+
* For the legacy `<script>`-tag distribution that auto-attaches
|
|
7
|
+
* `window.GameHubSDK` and runs `autoInit()`, see `./browser-entry.ts`.
|
|
8
|
+
*/
|
|
9
|
+
import { SDK_VERSION } from './constants.js';
|
|
10
|
+
import { state } from './state.js';
|
|
11
|
+
import { sendToParent } from './bridge.js';
|
|
12
|
+
import { ensureInitialized } from './init.js';
|
|
13
|
+
import { isBrowser } from './utils.js';
|
|
14
|
+
import * as Session from './session.js';
|
|
15
|
+
import * as MatchCtx from './match-context.js';
|
|
16
|
+
import * as Multiplayer from './multiplayer.js';
|
|
17
|
+
/**
|
|
18
|
+
* Assemble the public API object. Methods are thin adapters that route to
|
|
19
|
+
* the internal modules — keeping the surface area small and the internals
|
|
20
|
+
* freely refactorable.
|
|
21
|
+
*/
|
|
22
|
+
export function createSdk() {
|
|
23
|
+
return {
|
|
24
|
+
version: SDK_VERSION,
|
|
25
|
+
isInitialized() {
|
|
26
|
+
return state.isInitialized;
|
|
27
|
+
},
|
|
28
|
+
isAuthenticated() {
|
|
29
|
+
return !!state.authToken;
|
|
30
|
+
},
|
|
31
|
+
hasActiveSession() {
|
|
32
|
+
return !!state.sessionToken;
|
|
33
|
+
},
|
|
34
|
+
async startSession() {
|
|
35
|
+
await ensureInitialized();
|
|
36
|
+
if (state.sessionStartPromise)
|
|
37
|
+
return await state.sessionStartPromise;
|
|
38
|
+
return await Session.startSession();
|
|
39
|
+
},
|
|
40
|
+
submitScore(score, meta = {}) {
|
|
41
|
+
return Session.submitScore(score, meta);
|
|
42
|
+
},
|
|
43
|
+
submitMatchScore(score, meta = {}) {
|
|
44
|
+
return Session.submitMatchScore(score, meta);
|
|
45
|
+
},
|
|
46
|
+
submitMatchResult(result = {}) {
|
|
47
|
+
return Session.submitMatchResult(result);
|
|
48
|
+
},
|
|
49
|
+
async playAgain() {
|
|
50
|
+
// Same session — each play is appended as a new record in plays[].
|
|
51
|
+
return { success: true, token: state.sessionToken };
|
|
52
|
+
},
|
|
53
|
+
notifyComplete(data = {}) {
|
|
54
|
+
sendToParent('GAME_COMPLETE', data);
|
|
55
|
+
},
|
|
56
|
+
requestClose() {
|
|
57
|
+
sendToParent('REQUEST_CLOSE', {});
|
|
58
|
+
},
|
|
59
|
+
logEvent(eventName, eventData = {}) {
|
|
60
|
+
sendToParent('LOG_EVENT', {
|
|
61
|
+
event: eventName,
|
|
62
|
+
data: eventData,
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
getGameSlug() {
|
|
66
|
+
return state.gameSlug;
|
|
67
|
+
},
|
|
68
|
+
isInIframe() {
|
|
69
|
+
return isBrowser() && window.parent !== window;
|
|
70
|
+
},
|
|
71
|
+
async ready() {
|
|
72
|
+
return await ensureInitialized();
|
|
73
|
+
},
|
|
74
|
+
getBootstrap() {
|
|
75
|
+
return {
|
|
76
|
+
gameSlug: state.gameSlug,
|
|
77
|
+
authToken: state.authToken,
|
|
78
|
+
leagueId: state.leagueId,
|
|
79
|
+
gameId: state.gameId,
|
|
80
|
+
matchId: state.matchId,
|
|
81
|
+
match: state.matchContext,
|
|
82
|
+
participant1Id: state.participant1Id,
|
|
83
|
+
participant2Id: state.participant2Id,
|
|
84
|
+
participant1MemberIds: state.participant1MemberIds,
|
|
85
|
+
participant2MemberIds: state.participant2MemberIds,
|
|
86
|
+
playerName: state.bootstrapPlayerName,
|
|
87
|
+
peerId: state.bootstrapPeerId,
|
|
88
|
+
room: state.bootstrapRoomCode
|
|
89
|
+
? {
|
|
90
|
+
roomCode: state.bootstrapRoomCode,
|
|
91
|
+
shareId: state.bootstrapShareId,
|
|
92
|
+
maxPlayers: state.bootstrapMaxPlayers,
|
|
93
|
+
role: state.bootstrapRoomRole,
|
|
94
|
+
roomToken: state.bootstrapRoomToken,
|
|
95
|
+
wsUrl: state.bootstrapWsUrl,
|
|
96
|
+
}
|
|
97
|
+
: null,
|
|
98
|
+
isBootstrapped: state.bootstrapReceived,
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
async getMatchContext() {
|
|
102
|
+
await ensureInitialized();
|
|
103
|
+
return await MatchCtx.fetchMatchContext();
|
|
104
|
+
},
|
|
105
|
+
getQuestions(options = {}) {
|
|
106
|
+
return Session.getQuestions(options);
|
|
107
|
+
},
|
|
108
|
+
multiplayer: {
|
|
109
|
+
createRoom: Multiplayer.createRoom,
|
|
110
|
+
joinRoom: Multiplayer.joinRoom,
|
|
111
|
+
joinRoomByLink: Multiplayer.joinRoomByLink,
|
|
112
|
+
leaveRoom: Multiplayer.leaveRoom,
|
|
113
|
+
send: Multiplayer.send,
|
|
114
|
+
broadcast: Multiplayer.broadcast,
|
|
115
|
+
on: Multiplayer.on,
|
|
116
|
+
getRoom: Multiplayer.getRoom,
|
|
117
|
+
getPeers: Multiplayer.getPeers,
|
|
118
|
+
isConnected: Multiplayer.isConnected,
|
|
119
|
+
getRole: Multiplayer.getRole,
|
|
120
|
+
isHost: Multiplayer.isHost,
|
|
121
|
+
isViewer: Multiplayer.isViewer,
|
|
122
|
+
selfId: Multiplayer.selfId,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/main.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAC;AACxC,OAAO,KAAK,QAAQ,MAAM,oBAAoB,CAAC;AAC/C,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC;AAyJhD;;;;GAIG;AACH,MA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QAED,YAAY,CAAC,UAA2B,EAAE;YACxC,OAAO,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;QACvC,CAAC;QAED,WAAW,EAAE;YACX,UAAU,EAAE,WAAW,CAAC,UAAU;YAClC,QAAQ,EAAE,WAAW,CAAC,QAAQ;YAC9B,cAAc,EAAE,WAAW,CAAC,cAAc;YAC1C,SAAS,EAAE,WAAW,CAAC,SAAS;YAChC,IAAI,EAAE,WAAW,CAAC,IAAI;YACtB,SAAS,EAAE,WAAW,CAAC,SAAS;YAChC,EAAE,EAAE,WAAW,CAAC,EAAE;YAClB,OAAO,EAAE,WAAW,CAAC,OAAO;YAC5B,QAAQ,EAAE,WAAW,CAAC,QAAQ;YAC9B,WAAW,EAAE,WAAW,CAAC,WAAW;YACpC,OAAO,EAAE,WAAW,CAAC,OAAO;YAC5B,MAAM,EAAE,WAAW,CAAC,MAAM;YAC1B,QAAQ,EAAE,WAAW,CAAC,QAAQ;YAC9B,MAAM,EAAE,WAAW,CAAC,MAAM;SAC3B;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MatchContext } from '../types/match.js';
|
|
2
|
+
/**
|
|
3
|
+
* Lazily load the match context for a league game. Results are cached on
|
|
4
|
+
* `state.matchContext`; concurrent callers share a single in-flight request
|
|
5
|
+
* via `state.matchContextPromise`.
|
|
6
|
+
*
|
|
7
|
+
* Resolves `null` when required context (league or match id) is missing, or
|
|
8
|
+
* when the server returns an error — callers decide how to degrade.
|
|
9
|
+
*/
|
|
10
|
+
export declare function fetchMatchContext(): Promise<MatchContext | null>;
|
|
11
|
+
//# sourceMappingURL=match-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match-context.d.ts","sourceRoot":"","sources":["../../src/match-context.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAA0B,MAAM,mBAAmB,CAAC;AAE9E;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAwCtE"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { state } from './state.js';
|
|
2
|
+
import { apiRequest } from './api-client.js';
|
|
3
|
+
import { unwrapResult } from './utils.js';
|
|
4
|
+
/**
|
|
5
|
+
* Lazily load the match context for a league game. Results are cached on
|
|
6
|
+
* `state.matchContext`; concurrent callers share a single in-flight request
|
|
7
|
+
* via `state.matchContextPromise`.
|
|
8
|
+
*
|
|
9
|
+
* Resolves `null` when required context (league or match id) is missing, or
|
|
10
|
+
* when the server returns an error — callers decide how to degrade.
|
|
11
|
+
*/
|
|
12
|
+
export async function fetchMatchContext() {
|
|
13
|
+
if (!state.leagueId || !state.matchId)
|
|
14
|
+
return null;
|
|
15
|
+
if (state.matchContext)
|
|
16
|
+
return state.matchContext;
|
|
17
|
+
if (state.matchContextPromise)
|
|
18
|
+
return await state.matchContextPromise;
|
|
19
|
+
state.matchContextPromise = apiRequest('/leagues/' +
|
|
20
|
+
encodeURIComponent(state.leagueId) +
|
|
21
|
+
'/matches/' +
|
|
22
|
+
encodeURIComponent(state.matchId), { method: 'GET' })
|
|
23
|
+
.then((result) => {
|
|
24
|
+
const data = unwrapResult(result);
|
|
25
|
+
if (result?.success && data) {
|
|
26
|
+
state.matchContext = data;
|
|
27
|
+
state.participant1Id =
|
|
28
|
+
data.participant1?.participantId || state.participant1Id;
|
|
29
|
+
state.participant2Id =
|
|
30
|
+
data.participant2?.participantId || state.participant2Id;
|
|
31
|
+
state.participant1MemberIds = Array.isArray(data.participant1?.members)
|
|
32
|
+
? data.participant1.members
|
|
33
|
+
.map((member) => member?.leaguePlayerId)
|
|
34
|
+
.filter(Boolean)
|
|
35
|
+
: state.participant1MemberIds;
|
|
36
|
+
state.participant2MemberIds = Array.isArray(data.participant2?.members)
|
|
37
|
+
? data.participant2.members
|
|
38
|
+
.map((member) => member?.leaguePlayerId)
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
: state.participant2MemberIds;
|
|
41
|
+
return state.matchContext;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
})
|
|
45
|
+
.catch(() => null)
|
|
46
|
+
.finally(() => {
|
|
47
|
+
state.matchContextPromise = null;
|
|
48
|
+
});
|
|
49
|
+
return await state.matchContextPromise;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=match-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match-context.js","sourceRoot":"","sources":["../../src/match-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG1C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB;IACrC,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO;QAAE,OAAO,IAAI,CAAC;IACnD,IAAI,KAAK,CAAC,YAAY;QAAE,OAAO,KAAK,CAAC,YAAY,CAAC;IAClD,IAAI,KAAK,CAAC,mBAAmB;QAAE,OAAO,MAAM,KAAK,CAAC,mBAAmB,CAAC;IAEtE,KAAK,CAAC,mBAAmB,GAAG,UAAU,CACpC,WAAW;QACT,kBAAkB,CAAC,KAAK,CAAC,QAAQ,CAAC;QAClC,WAAW;QACX,kBAAkB,CAAC,KAAK,CAAC,OAAO,CAAC,EACnC,EAAE,MAAM,EAAE,KAAK,EAAE,CAClB;SACE,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;QACf,MAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;QAClC,IAAI,MAAM,EAAE,OAAO,IAAI,IAAI,EAAE,CAAC;YAC5B,KAAK,CAAC,YAAY,GAAG,IAAoB,CAAC;YAC1C,KAAK,CAAC,cAAc;gBAClB,IAAI,CAAC,YAAY,EAAE,aAAa,IAAI,KAAK,CAAC,cAAc,CAAC;YAC3D,KAAK,CAAC,cAAc;gBAClB,IAAI,CAAC,YAAY,EAAE,aAAa,IAAI,KAAK,CAAC,cAAc,CAAC;YAC3D,KAAK,CAAC,qBAAqB,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC;gBACrE,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO;qBACtB,GAAG,CAAC,CAAC,MAA8B,EAAE,EAAE,CAAC,MAAM,EAAE,cAAc,CAAC;qBAC/D,MAAM,CAAC,OAAO,CAAC;gBACpB,CAAC,CAAC,KAAK,CAAC,qBAAqB,CAAC;YAChC,KAAK,CAAC,qBAAqB,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC;gBACrE,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO;qBACtB,GAAG,CAAC,CAAC,MAA8B,EAAE,EAAE,CAAC,MAAM,EAAE,cAAc,CAAC;qBAC/D,MAAM,CAAC,OAAO,CAAC;gBACpB,CAAC,CAAC,KAAK,CAAC,qBAAqB,CAAC;YAChC,OAAO,KAAK,CAAC,YAAY,CAAC;QAC5B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;SACD,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;SACjB,OAAO,CAAC,GAAG,EAAE;QACZ,KAAK,CAAC,mBAAmB,GAAG,IAAI,CAAC;IACnC,CAAC,CAAC,CAAC;IAEL,OAAO,MAAM,KAAK,CAAC,mBAAmB,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multiplayer layer: room creation/join, WebSocket connection management,
|
|
3
|
+
* reconnection, and an event emitter for game code to listen on.
|
|
4
|
+
*/
|
|
5
|
+
import type { CreateRoomResult, JoinInfo, JoinOptions, JoinRoomResult, MpEventHandler, Peer, RoomInfo, RoomOptions } from '../types/multiplayer.js';
|
|
6
|
+
/**
|
|
7
|
+
* Dispatch an event to per-name listeners and the wildcard (`'*'`) listeners.
|
|
8
|
+
* Handler errors are caught and logged so a buggy listener can't break the
|
|
9
|
+
* socket loop.
|
|
10
|
+
*/
|
|
11
|
+
export declare function emit(eventName: string, payload: any): void;
|
|
12
|
+
/**
|
|
13
|
+
* Detach listeners from the current socket and close it. Idempotent.
|
|
14
|
+
* The caller is responsible for clearing `state.mpRoom` and related state —
|
|
15
|
+
* this function only touches the transport.
|
|
16
|
+
*/
|
|
17
|
+
export declare function clearSocket(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Open a WebSocket for the given join info and wire up the message/close
|
|
20
|
+
* handlers. Automatically reconnects with exponential backoff up to
|
|
21
|
+
* `MP_MAX_RECONNECT_ATTEMPTS` attempts.
|
|
22
|
+
*/
|
|
23
|
+
export declare function connect(joinInfo: JoinInfo): void;
|
|
24
|
+
/**
|
|
25
|
+
* Create a room for the current game and connect as the host.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createRoom(options?: RoomOptions): Promise<CreateRoomResult>;
|
|
28
|
+
/**
|
|
29
|
+
* Join an existing room by its short alphanumeric code.
|
|
30
|
+
*/
|
|
31
|
+
export declare function joinRoom(roomCode: string, options?: JoinOptions): Promise<JoinRoomResult>;
|
|
32
|
+
/**
|
|
33
|
+
* Join an existing room by its share ID or full share URL.
|
|
34
|
+
*/
|
|
35
|
+
export declare function joinRoomByLink(link: string, options?: JoinOptions): Promise<JoinRoomResult>;
|
|
36
|
+
/**
|
|
37
|
+
* Disconnect from the current room. If we're the host, also ask the server
|
|
38
|
+
* to close the room so stragglers get booted cleanly.
|
|
39
|
+
*/
|
|
40
|
+
export declare function leaveRoom(reason?: string): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Send a custom game event to every other peer in the room. Returns false
|
|
43
|
+
* when the socket isn't open or the current peer is a view-only guest.
|
|
44
|
+
*/
|
|
45
|
+
export declare function send(eventName: string, payload?: Record<string, any>): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Host-only variant of `send`. Silently no-ops (and logs a warning) if the
|
|
48
|
+
* current peer isn't the host. Use this for authoritative state updates —
|
|
49
|
+
* grid shuffles, turn advances, round resolution — so a guest that
|
|
50
|
+
* accidentally calls it can't corrupt the match.
|
|
51
|
+
*/
|
|
52
|
+
export declare function broadcast(eventName: string, payload?: Record<string, any>): boolean;
|
|
53
|
+
/** @returns the current peer's role (`host`, `player`, `viewer`, ...) or null. */
|
|
54
|
+
export declare function getRole(): string | null;
|
|
55
|
+
/** @returns true when the current peer is the room's authoritative host. */
|
|
56
|
+
export declare function isHost(): boolean;
|
|
57
|
+
/** @returns true when the current peer joined with `{ asViewer: true }`. */
|
|
58
|
+
export declare function isViewer(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Returns the peerId of the current player (self), or null if not joined.
|
|
61
|
+
* Convenience for filtering self-originated echoes inside a `message`
|
|
62
|
+
* listener: `if (msg.fromPeerId === selfId()) return;`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function selfId(): string | null;
|
|
65
|
+
/**
|
|
66
|
+
* Subscribe to a multiplayer event. Use `'*'` to receive every event.
|
|
67
|
+
* Returns an unsubscribe function.
|
|
68
|
+
*/
|
|
69
|
+
export declare function on(eventName: string, handler: MpEventHandler): () => void;
|
|
70
|
+
/** @returns a copy of the current room descriptor, or null if not in one. */
|
|
71
|
+
export declare function getRoom(): RoomInfo | null;
|
|
72
|
+
/** @returns every peer currently in the room (self included). */
|
|
73
|
+
export declare function getPeers(): Peer[];
|
|
74
|
+
/** @returns whether the WebSocket is currently open. */
|
|
75
|
+
export declare function isConnected(): boolean;
|
|
76
|
+
//# sourceMappingURL=multiplayer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiplayer.d.ts","sourceRoot":"","sources":["../../src/multiplayer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAeH,OAAO,KAAK,EACV,g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