@ksaa-nlp/gamehub-sdk 4.3.0 → 5.0.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 +75 -46
- package/SPEC.md +130 -59
- package/dist/gamehub-sdk.js +15 -30
- package/dist/gamehub-sdk.js.map +3 -3
- package/dist/src/bot-driver.d.ts +54 -0
- package/dist/src/bot-driver.d.ts.map +1 -0
- package/dist/src/bot-driver.js +104 -0
- package/dist/src/bot-driver.js.map +1 -0
- package/dist/src/bridge.d.ts.map +1 -1
- package/dist/src/bridge.js +0 -35
- package/dist/src/bridge.js.map +1 -1
- package/dist/src/constants.d.ts +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/init.js +7 -7
- package/dist/src/init.js.map +1 -1
- package/dist/src/main.d.ts +1 -1
- package/dist/src/main.d.ts.map +1 -1
- package/dist/src/main.js +3 -4
- package/dist/src/main.js.map +1 -1
- package/dist/src/multiplayer-client.d.ts +50 -0
- package/dist/src/multiplayer-client.d.ts.map +1 -0
- package/dist/src/multiplayer-client.js +268 -0
- package/dist/src/multiplayer-client.js.map +1 -0
- package/dist/src/multiplayer-entry.d.ts +9 -0
- package/dist/src/multiplayer-entry.d.ts.map +1 -0
- package/dist/src/multiplayer-entry.js +9 -0
- package/dist/src/multiplayer-entry.js.map +1 -0
- package/dist/src/multiplayer-session.d.ts.map +1 -1
- package/dist/src/multiplayer-session.js +5 -1
- package/dist/src/multiplayer-session.js.map +1 -1
- package/dist/src/multiplayer.d.ts.map +1 -1
- package/dist/src/multiplayer.js +2 -1
- package/dist/src/multiplayer.js.map +1 -1
- package/dist/src/server-entry.d.ts +10 -4
- package/dist/src/server-entry.d.ts.map +1 -1
- package/dist/src/server-entry.js +7 -5
- package/dist/src/server-entry.js.map +1 -1
- package/dist/src/session.d.ts.map +1 -1
- package/dist/src/session.js +3 -2
- package/dist/src/session.js.map +1 -1
- package/dist/src/state.d.ts +1 -2
- package/dist/src/state.d.ts.map +1 -1
- package/dist/src/state.js +0 -1
- package/dist/src/state.js.map +1 -1
- package/dist/types/bootstrap.d.ts +6 -45
- package/dist/types/bootstrap.d.ts.map +1 -1
- package/dist/types/multiplayer-session.d.ts +13 -7
- package/dist/types/multiplayer-session.d.ts.map +1 -1
- 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 +17 -0
- package/dist/vendor/colyseus-schemas/common.js +12 -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 +176 -0
- package/dist/vendor/gamehub-multiplayer-contracts/index.js +287 -0
- package/package.json +33 -3
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/** Plain-data v2 wire contract. This entry must remain dependency and side-effect free. */
|
|
2
|
+
export declare const LAUNCH_VERSION: 2;
|
|
3
|
+
export declare const MULTIPLAYER_CAPABILITY: "multiplayer.launch.v2";
|
|
4
|
+
export declare const MULTIPLAYER_LOBBY_CAPABILITY: "multiplayer.lobby.v2";
|
|
5
|
+
export type LobbyMode = 'platform' | 'game';
|
|
6
|
+
export declare const CONTROL_TIMEOUT_MS = 10000;
|
|
7
|
+
export declare const ARRIVAL_WINDOW_MS = 90000;
|
|
8
|
+
export declare const RECOVERY_GRACE_MS = 60000;
|
|
9
|
+
export declare const RESERVATION_LEASE_MS = 15000;
|
|
10
|
+
export type ParticipantKind = 'human' | 'bot';
|
|
11
|
+
export declare const BOT_LEVELS: readonly ["easy", "medium", "hard"];
|
|
12
|
+
export type BotLevel = (typeof BOT_LEVELS)[number];
|
|
13
|
+
/** Bots stored before levels existed play at `medium`. */
|
|
14
|
+
export declare const DEFAULT_BOT_LEVEL: BotLevel;
|
|
15
|
+
export declare const isBotLevel: (value: unknown) => value is BotLevel;
|
|
16
|
+
/** UGC bots are game-owned; quiz bots are played by the built-in engine. Relay has none. */
|
|
17
|
+
export declare const BOT_ENGINES: readonly Engine[];
|
|
18
|
+
export interface PlayerPolicy {
|
|
19
|
+
minPlayers: number;
|
|
20
|
+
maxPlayers: number;
|
|
21
|
+
minHumanPlayers: number;
|
|
22
|
+
maxBots: number;
|
|
23
|
+
maxSpectators: number;
|
|
24
|
+
}
|
|
25
|
+
/** Missing policy identifies an existing two-human v2 package/round. */
|
|
26
|
+
export declare function playerPolicy(binding: {
|
|
27
|
+
playerPolicy?: PlayerPolicy;
|
|
28
|
+
}): PlayerPolicy;
|
|
29
|
+
export declare function parsePlayerPolicy(manifest: Record<string, unknown>, ceiling?: number): PlayerPolicy;
|
|
30
|
+
export type SeatType = 'player' | 'spectator';
|
|
31
|
+
export type SessionPhase = 'waiting' | 'in_round' | 'completed' | 'closed';
|
|
32
|
+
export type RoundPhase = 'preparing' | 'loading' | 'playing' | 'finishing' | 'completed' | 'aborted' | 'interrupted';
|
|
33
|
+
export type Authority = 'server' | 'unverified';
|
|
34
|
+
export type Engine = 'ugc' | 'quiz-template' | 'relay';
|
|
35
|
+
export interface PackageBinding {
|
|
36
|
+
lobbyMode?: LobbyMode;
|
|
37
|
+
gameLobbySupported?: boolean;
|
|
38
|
+
playerPolicy?: PlayerPolicy;
|
|
39
|
+
gameId: string;
|
|
40
|
+
gameSlug: string;
|
|
41
|
+
ownerId: string;
|
|
42
|
+
versionId: string;
|
|
43
|
+
versionKey: string;
|
|
44
|
+
contentHash: string;
|
|
45
|
+
serverHash: string | null;
|
|
46
|
+
gameUrl: string;
|
|
47
|
+
engine: Engine;
|
|
48
|
+
protocol: 'gamehub.v2';
|
|
49
|
+
scorePolicy?: 'history-only' | 'account';
|
|
50
|
+
profileId?: string | null;
|
|
51
|
+
questions?: 'none' | 'required';
|
|
52
|
+
}
|
|
53
|
+
export interface Seat {
|
|
54
|
+
/** Omitted only on records created before participant support; means human. */
|
|
55
|
+
kind?: ParticipantKind;
|
|
56
|
+
/** Bot seats only; omitted on bots created before levels means `medium`. */
|
|
57
|
+
botLevel?: BotLevel;
|
|
58
|
+
seatId: string;
|
|
59
|
+
displayName: string;
|
|
60
|
+
type: SeatType;
|
|
61
|
+
ready: boolean;
|
|
62
|
+
online: boolean;
|
|
63
|
+
status: 'joined' | 'left' | 'removed';
|
|
64
|
+
}
|
|
65
|
+
export interface LaunchReference {
|
|
66
|
+
recoveryGraceMs?: number;
|
|
67
|
+
version: 2;
|
|
68
|
+
multiplayerSessionId: string;
|
|
69
|
+
roundId: string;
|
|
70
|
+
attemptId: string;
|
|
71
|
+
seatId: string;
|
|
72
|
+
hostInstanceId: string;
|
|
73
|
+
gameUrl: string;
|
|
74
|
+
scoreAuthority: Authority;
|
|
75
|
+
}
|
|
76
|
+
export interface Reservation {
|
|
77
|
+
name: string;
|
|
78
|
+
roomId: string;
|
|
79
|
+
sessionId: string;
|
|
80
|
+
processId?: string;
|
|
81
|
+
publicAddress?: string;
|
|
82
|
+
}
|
|
83
|
+
export declare const ERROR_CODES: readonly ["INVALID_REQUEST", "UNAUTHORIZED", "LOGIN_REQUIRED", "FORBIDDEN", "NOT_FOUND", "GAME_MISMATCH", "GAME_UNAVAILABLE", "NO_QUESTIONS", "PROTOCOL_UNSUPPORTED", "PASSCODE_REQUIRED", "INVALID_PASSCODE", "ROOM_FULL", "SEAT_REMOVED", "ROSTER_FROZEN", "NOT_HOST", "STALE_REVISION", "NOT_READY", "STALE_ATTEMPT", "STALE_GENERATION", "ALREADY_CONNECTED", "LAUNCH_PENDING", "LAUNCH_EXPIRED", "ARRIVAL_TIMEOUT", "RECOVERY_EXPIRED", "INTERRUPTED", "ROUND_TERMINAL", "RESULT_CONFLICT", "INVALID_RESULT", "RATE_LIMITED", "UNAVAILABLE", "CANCELLED", "STORAGE_UNAVAILABLE"];
|
|
84
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
85
|
+
export interface MultiplayerError {
|
|
86
|
+
code: ErrorCode;
|
|
87
|
+
retryable: boolean;
|
|
88
|
+
message: string;
|
|
89
|
+
phase?: RoundPhase | SessionPhase;
|
|
90
|
+
retryAfterMs?: number;
|
|
91
|
+
}
|
|
92
|
+
export declare class MultiplayerFault extends Error {
|
|
93
|
+
readonly code: ErrorCode;
|
|
94
|
+
readonly retryable: boolean;
|
|
95
|
+
readonly status: number;
|
|
96
|
+
constructor(code: ErrorCode, retryable?: boolean, status?: number);
|
|
97
|
+
toJSON(): MultiplayerError;
|
|
98
|
+
}
|
|
99
|
+
export type LaunchOutcome = {
|
|
100
|
+
status: 'reserved';
|
|
101
|
+
launch: LaunchReference;
|
|
102
|
+
reservation: Reservation;
|
|
103
|
+
wsEndpoint: string;
|
|
104
|
+
generation: number;
|
|
105
|
+
expiresAt: number;
|
|
106
|
+
} | {
|
|
107
|
+
status: 'recoverable' | 'terminal';
|
|
108
|
+
error: MultiplayerError;
|
|
109
|
+
};
|
|
110
|
+
export interface SessionView {
|
|
111
|
+
/** Derived public website URLs. Absent/null outside waiting or for invalid site configuration. */
|
|
112
|
+
invitations?: {
|
|
113
|
+
playerUrl: string;
|
|
114
|
+
spectatorUrl: string | null;
|
|
115
|
+
} | null;
|
|
116
|
+
/** Trusted host telemetry identity, excluded from the game lobby projection. */
|
|
117
|
+
gameId?: string;
|
|
118
|
+
/** Frozen presentation choice; old sessions retain the platform lobby. */
|
|
119
|
+
lobbyMode?: LobbyMode;
|
|
120
|
+
lobbyGameUrl?: string;
|
|
121
|
+
playerPolicy?: PlayerPolicy;
|
|
122
|
+
version: 2;
|
|
123
|
+
multiplayerSessionId: string;
|
|
124
|
+
roomCode: string;
|
|
125
|
+
gameSlug: string;
|
|
126
|
+
phase: SessionPhase;
|
|
127
|
+
revision: number;
|
|
128
|
+
hostSeatId: string;
|
|
129
|
+
ownSeatId: string;
|
|
130
|
+
rematchAllowed?: boolean;
|
|
131
|
+
/** Trusted host metadata; never part of the game-owned launch reference. */
|
|
132
|
+
contentOrigin?: string | null;
|
|
133
|
+
contentReady?: boolean;
|
|
134
|
+
seats: Seat[];
|
|
135
|
+
round: null | {
|
|
136
|
+
roundId: string;
|
|
137
|
+
revision?: number;
|
|
138
|
+
phase: RoundPhase;
|
|
139
|
+
reason: string | null;
|
|
140
|
+
projection?: 'pending' | 'applied' | 'rejected';
|
|
141
|
+
projectionReason?: string | null;
|
|
142
|
+
result: CanonicalResult | null;
|
|
143
|
+
launch: LaunchReference;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export interface ResultProposal {
|
|
147
|
+
version: 2;
|
|
148
|
+
scores: Record<string, number>;
|
|
149
|
+
winnerSeatIds: string[];
|
|
150
|
+
reason: 'win' | 'draw' | 'forfeit' | 'abort';
|
|
151
|
+
metadata?: Record<string, string | number | boolean | null>;
|
|
152
|
+
}
|
|
153
|
+
export interface CanonicalResult extends ResultProposal {
|
|
154
|
+
participants?: Array<{
|
|
155
|
+
seatId: string;
|
|
156
|
+
displayName: string;
|
|
157
|
+
kind: ParticipantKind;
|
|
158
|
+
}>;
|
|
159
|
+
roundId: string;
|
|
160
|
+
multiplayerSessionId: string;
|
|
161
|
+
authority: Authority;
|
|
162
|
+
package: PackageBinding;
|
|
163
|
+
startedAt: number;
|
|
164
|
+
endedAt: number;
|
|
165
|
+
}
|
|
166
|
+
export declare const terminalRound: (phase: RoundPhase) => boolean;
|
|
167
|
+
export declare function canTransition(from: RoundPhase, to: RoundPhase): boolean;
|
|
168
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
169
|
+
export declare function isBoundedId(value: unknown): value is string;
|
|
170
|
+
export declare function isLaunchReference(value: unknown): value is LaunchReference;
|
|
171
|
+
export declare function isLaunchOutcome(value: unknown): value is LaunchOutcome;
|
|
172
|
+
export declare function parseResultProposal(value: unknown, participantSeatIds: readonly string[]): ResultProposal;
|
|
173
|
+
/** Stable, bounded canonical JSON; callers validate their domain payload first. */
|
|
174
|
+
export declare function canonicalJson(value: unknown): string;
|
|
175
|
+
export declare function normalizeError(error: unknown): MultiplayerError;
|
|
176
|
+
export declare function errorMessage(code: ErrorCode): string;
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/** Plain-data v2 wire contract. This entry must remain dependency and side-effect free. */
|
|
2
|
+
export const LAUNCH_VERSION = 2;
|
|
3
|
+
export const MULTIPLAYER_CAPABILITY = 'multiplayer.launch.v2';
|
|
4
|
+
export const MULTIPLAYER_LOBBY_CAPABILITY = 'multiplayer.lobby.v2';
|
|
5
|
+
export const CONTROL_TIMEOUT_MS = 10000;
|
|
6
|
+
export const ARRIVAL_WINDOW_MS = 90000;
|
|
7
|
+
export const RECOVERY_GRACE_MS = 60000;
|
|
8
|
+
export const RESERVATION_LEASE_MS = 15000;
|
|
9
|
+
export const BOT_LEVELS = ['easy', 'medium', 'hard'];
|
|
10
|
+
/** Bots stored before levels existed play at `medium`. */
|
|
11
|
+
export const DEFAULT_BOT_LEVEL = 'medium';
|
|
12
|
+
export const isBotLevel = (value) => BOT_LEVELS.includes(value);
|
|
13
|
+
/** UGC bots are game-owned; quiz bots are played by the built-in engine. Relay has none. */
|
|
14
|
+
export const BOT_ENGINES = ['ugc', 'quiz-template'];
|
|
15
|
+
/** Missing policy identifies an existing two-human v2 package/round. */
|
|
16
|
+
export function playerPolicy(binding) {
|
|
17
|
+
return (binding.playerPolicy ?? {
|
|
18
|
+
minPlayers: 2,
|
|
19
|
+
maxPlayers: 2,
|
|
20
|
+
minHumanPlayers: 1,
|
|
21
|
+
maxBots: 0,
|
|
22
|
+
maxSpectators: 16,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export function parsePlayerPolicy(manifest, ceiling = 256) {
|
|
26
|
+
const minPlayers = manifest.minPlayers;
|
|
27
|
+
const maxPlayers = manifest.maxPlayers;
|
|
28
|
+
const minHumanPlayers = (manifest.minHumanPlayers ?? 1);
|
|
29
|
+
const maxBots = (manifest.maxBots ?? 0);
|
|
30
|
+
if (![minPlayers, maxPlayers, minHumanPlayers, maxBots].every(Number.isSafeInteger) ||
|
|
31
|
+
minPlayers < 1 ||
|
|
32
|
+
maxPlayers < minPlayers ||
|
|
33
|
+
maxPlayers > ceiling ||
|
|
34
|
+
minHumanPlayers < 1 ||
|
|
35
|
+
minHumanPlayers > maxPlayers ||
|
|
36
|
+
maxBots < 0 ||
|
|
37
|
+
maxBots > maxPlayers - minHumanPlayers ||
|
|
38
|
+
typeof manifest.spectators !== 'boolean' ||
|
|
39
|
+
(maxBots > 0 && !BOT_ENGINES.includes(manifest.engine)))
|
|
40
|
+
throw new MultiplayerFault('PROTOCOL_UNSUPPORTED');
|
|
41
|
+
return {
|
|
42
|
+
minPlayers,
|
|
43
|
+
maxPlayers,
|
|
44
|
+
minHumanPlayers,
|
|
45
|
+
maxBots,
|
|
46
|
+
maxSpectators: manifest.spectators ? 16 : 0,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export const ERROR_CODES = [
|
|
50
|
+
'INVALID_REQUEST',
|
|
51
|
+
'UNAUTHORIZED',
|
|
52
|
+
'LOGIN_REQUIRED',
|
|
53
|
+
'FORBIDDEN',
|
|
54
|
+
'NOT_FOUND',
|
|
55
|
+
'GAME_MISMATCH',
|
|
56
|
+
'GAME_UNAVAILABLE',
|
|
57
|
+
'NO_QUESTIONS',
|
|
58
|
+
'PROTOCOL_UNSUPPORTED',
|
|
59
|
+
'PASSCODE_REQUIRED',
|
|
60
|
+
'INVALID_PASSCODE',
|
|
61
|
+
'ROOM_FULL',
|
|
62
|
+
'SEAT_REMOVED',
|
|
63
|
+
'ROSTER_FROZEN',
|
|
64
|
+
'NOT_HOST',
|
|
65
|
+
'STALE_REVISION',
|
|
66
|
+
'NOT_READY',
|
|
67
|
+
'STALE_ATTEMPT',
|
|
68
|
+
'STALE_GENERATION',
|
|
69
|
+
'ALREADY_CONNECTED',
|
|
70
|
+
'LAUNCH_PENDING',
|
|
71
|
+
'LAUNCH_EXPIRED',
|
|
72
|
+
'ARRIVAL_TIMEOUT',
|
|
73
|
+
'RECOVERY_EXPIRED',
|
|
74
|
+
'INTERRUPTED',
|
|
75
|
+
'ROUND_TERMINAL',
|
|
76
|
+
'RESULT_CONFLICT',
|
|
77
|
+
'INVALID_RESULT',
|
|
78
|
+
'RATE_LIMITED',
|
|
79
|
+
'UNAVAILABLE',
|
|
80
|
+
'CANCELLED',
|
|
81
|
+
'STORAGE_UNAVAILABLE',
|
|
82
|
+
];
|
|
83
|
+
export class MultiplayerFault extends Error {
|
|
84
|
+
constructor(code, retryable = false, status = 409) {
|
|
85
|
+
super(errorMessage(code));
|
|
86
|
+
this.code = code;
|
|
87
|
+
this.retryable = retryable;
|
|
88
|
+
this.status = status;
|
|
89
|
+
}
|
|
90
|
+
toJSON() {
|
|
91
|
+
return {
|
|
92
|
+
code: this.code,
|
|
93
|
+
message: this.message,
|
|
94
|
+
retryable: this.retryable,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export const terminalRound = (phase) => ['completed', 'aborted', 'interrupted'].includes(phase);
|
|
99
|
+
const transitions = {
|
|
100
|
+
preparing: ['loading', 'aborted'],
|
|
101
|
+
loading: ['playing', 'aborted'],
|
|
102
|
+
playing: ['finishing', 'interrupted'],
|
|
103
|
+
finishing: ['completed', 'interrupted'],
|
|
104
|
+
completed: [],
|
|
105
|
+
aborted: [],
|
|
106
|
+
interrupted: [],
|
|
107
|
+
};
|
|
108
|
+
export function canTransition(from, to) {
|
|
109
|
+
return transitions[from]?.includes(to) ?? false;
|
|
110
|
+
}
|
|
111
|
+
export function isRecord(value) {
|
|
112
|
+
return (!!value &&
|
|
113
|
+
typeof value === 'object' &&
|
|
114
|
+
!Array.isArray(value) &&
|
|
115
|
+
(Object.getPrototypeOf(value) === Object.prototype ||
|
|
116
|
+
Object.getPrototypeOf(value) === null));
|
|
117
|
+
}
|
|
118
|
+
export function isBoundedId(value) {
|
|
119
|
+
return typeof value === 'string' && /^[a-zA-Z0-9_-]{1,160}$/.test(value);
|
|
120
|
+
}
|
|
121
|
+
export function isLaunchReference(value) {
|
|
122
|
+
if (!isRecord(value) ||
|
|
123
|
+
value.version !== 2 ||
|
|
124
|
+
(value.recoveryGraceMs !== undefined &&
|
|
125
|
+
(!Number.isSafeInteger(value.recoveryGraceMs) ||
|
|
126
|
+
Number(value.recoveryGraceMs) < 1000 ||
|
|
127
|
+
Number(value.recoveryGraceMs) > 120000)))
|
|
128
|
+
return false;
|
|
129
|
+
return ([
|
|
130
|
+
'multiplayerSessionId',
|
|
131
|
+
'roundId',
|
|
132
|
+
'attemptId',
|
|
133
|
+
'seatId',
|
|
134
|
+
'hostInstanceId',
|
|
135
|
+
].every((k) => isBoundedId(value[k])) &&
|
|
136
|
+
typeof value.gameUrl === 'string' &&
|
|
137
|
+
value.gameUrl.startsWith('/games/') &&
|
|
138
|
+
value.gameUrl.length < 2048 &&
|
|
139
|
+
(value.scoreAuthority === 'server' || value.scoreAuthority === 'unverified'));
|
|
140
|
+
}
|
|
141
|
+
export function isLaunchOutcome(value) {
|
|
142
|
+
if (!isRecord(value))
|
|
143
|
+
return false;
|
|
144
|
+
if (value.status === 'reserved')
|
|
145
|
+
return (isLaunchReference(value.launch) &&
|
|
146
|
+
isRecord(value.reservation) &&
|
|
147
|
+
['name', 'roomId', 'sessionId'].every((k) => isBoundedId(value.reservation[k])) &&
|
|
148
|
+
typeof value.wsEndpoint === 'string' &&
|
|
149
|
+
/^wss?:\/\//.test(value.wsEndpoint) &&
|
|
150
|
+
value.wsEndpoint.length < 2048 &&
|
|
151
|
+
Number.isSafeInteger(value.generation) &&
|
|
152
|
+
Number(value.generation) > 0 &&
|
|
153
|
+
Number.isSafeInteger(value.expiresAt));
|
|
154
|
+
return ((value.status === 'terminal' || value.status === 'recoverable') &&
|
|
155
|
+
isRecord(value.error) &&
|
|
156
|
+
ERROR_CODES.includes(value.error.code) &&
|
|
157
|
+
typeof value.error.retryable === 'boolean' &&
|
|
158
|
+
typeof value.error.message === 'string' &&
|
|
159
|
+
value.error.message.length <= 1000);
|
|
160
|
+
}
|
|
161
|
+
export function parseResultProposal(value, participantSeatIds) {
|
|
162
|
+
if (!isRecord(value) ||
|
|
163
|
+
value.version !== 2 ||
|
|
164
|
+
!isRecord(value.scores) ||
|
|
165
|
+
!Array.isArray(value.winnerSeatIds) ||
|
|
166
|
+
!['win', 'draw', 'forfeit', 'abort'].includes(String(value.reason)))
|
|
167
|
+
throw new MultiplayerFault('INVALID_RESULT');
|
|
168
|
+
const allowed = new Set(participantSeatIds);
|
|
169
|
+
if (allowed.size !== participantSeatIds.length || !allowed.size)
|
|
170
|
+
throw new MultiplayerFault('INVALID_RESULT');
|
|
171
|
+
const scores = {};
|
|
172
|
+
for (const [seat, score] of Object.entries(value.scores)) {
|
|
173
|
+
if (!allowed.has(seat) ||
|
|
174
|
+
typeof score !== 'number' ||
|
|
175
|
+
!Number.isFinite(score) ||
|
|
176
|
+
Math.abs(score) > 1000000000)
|
|
177
|
+
throw new MultiplayerFault('INVALID_RESULT');
|
|
178
|
+
scores[seat] = score;
|
|
179
|
+
}
|
|
180
|
+
if (Object.keys(scores).length !== allowed.size ||
|
|
181
|
+
new Set(value.winnerSeatIds).size !== value.winnerSeatIds.length ||
|
|
182
|
+
value.winnerSeatIds.some((id) => typeof id !== 'string' || !allowed.has(id)) ||
|
|
183
|
+
(value.reason === 'draw' && value.winnerSeatIds.length !== 0) ||
|
|
184
|
+
(value.reason === 'win' && value.winnerSeatIds.length === 0))
|
|
185
|
+
throw new MultiplayerFault('INVALID_RESULT');
|
|
186
|
+
let metadata;
|
|
187
|
+
if (value.metadata !== undefined) {
|
|
188
|
+
if (!isRecord(value.metadata) ||
|
|
189
|
+
JSON.stringify(value.metadata).length > 4096 ||
|
|
190
|
+
Object.keys(value.metadata).length > 32 ||
|
|
191
|
+
Object.values(value.metadata).some((v) => (v !== null && !['string', 'number', 'boolean'].includes(typeof v)) ||
|
|
192
|
+
(typeof v === 'number' && !Number.isFinite(v))))
|
|
193
|
+
throw new MultiplayerFault('INVALID_RESULT');
|
|
194
|
+
metadata = value.metadata;
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
version: 2,
|
|
198
|
+
scores,
|
|
199
|
+
winnerSeatIds: [...value.winnerSeatIds].sort(),
|
|
200
|
+
reason: value.reason,
|
|
201
|
+
...(metadata ? { metadata } : {}),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/** Stable, bounded canonical JSON; callers validate their domain payload first. */
|
|
205
|
+
export function canonicalJson(value) {
|
|
206
|
+
if (Array.isArray(value))
|
|
207
|
+
return '[' + value.map(canonicalJson).join(',') + ']';
|
|
208
|
+
if (isRecord(value))
|
|
209
|
+
return ('{' +
|
|
210
|
+
Object.keys(value)
|
|
211
|
+
.sort()
|
|
212
|
+
.map((k) => JSON.stringify(k) + ':' + canonicalJson(value[k]))
|
|
213
|
+
.join(',') +
|
|
214
|
+
'}');
|
|
215
|
+
const encoded = JSON.stringify(value);
|
|
216
|
+
if (encoded === undefined)
|
|
217
|
+
throw new MultiplayerFault('INVALID_REQUEST');
|
|
218
|
+
return encoded;
|
|
219
|
+
}
|
|
220
|
+
export function normalizeError(error) {
|
|
221
|
+
if (error instanceof MultiplayerFault)
|
|
222
|
+
return error.toJSON();
|
|
223
|
+
const numeric = {
|
|
224
|
+
4210: 'ROUND_TERMINAL',
|
|
225
|
+
4211: 'SEAT_REMOVED',
|
|
226
|
+
4212: 'STALE_GENERATION',
|
|
227
|
+
4213: 'ALREADY_CONNECTED',
|
|
228
|
+
4214: 'RECOVERY_EXPIRED',
|
|
229
|
+
4215: 'ARRIVAL_TIMEOUT',
|
|
230
|
+
4216: 'INTERRUPTED',
|
|
231
|
+
4217: 'GAME_UNAVAILABLE',
|
|
232
|
+
4218: 'INVALID_RESULT',
|
|
233
|
+
4219: 'NOT_READY',
|
|
234
|
+
4220: 'LOGIN_REQUIRED',
|
|
235
|
+
4221: 'UNAVAILABLE',
|
|
236
|
+
522: 'LAUNCH_EXPIRED',
|
|
237
|
+
};
|
|
238
|
+
const raw = isRecord(error)
|
|
239
|
+
? error
|
|
240
|
+
: error instanceof Error
|
|
241
|
+
? {
|
|
242
|
+
code: error.code,
|
|
243
|
+
message: error.message,
|
|
244
|
+
}
|
|
245
|
+
: {};
|
|
246
|
+
const code = typeof raw.code === 'number'
|
|
247
|
+
? numeric[raw.code]
|
|
248
|
+
: ERROR_CODES.includes(raw.code)
|
|
249
|
+
? raw.code
|
|
250
|
+
: undefined;
|
|
251
|
+
return {
|
|
252
|
+
code: code ?? 'UNAVAILABLE',
|
|
253
|
+
message: code
|
|
254
|
+
? errorMessage(code)
|
|
255
|
+
: 'Connection unavailable. Check your network and retry.',
|
|
256
|
+
retryable: !code ||
|
|
257
|
+
['LAUNCH_EXPIRED', 'UNAVAILABLE', 'LAUNCH_PENDING'].includes(code),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
export function errorMessage(code) {
|
|
261
|
+
const messages = {
|
|
262
|
+
UNAUTHORIZED: 'Sign in, or rejoin with this host’s original guest session.',
|
|
263
|
+
LOGIN_REQUIRED: 'Sign in to play or spectate this game.',
|
|
264
|
+
GAME_MISMATCH: 'This invitation belongs to a different game. Open its invitation link.',
|
|
265
|
+
GAME_UNAVAILABLE: 'This game or its approved package is unavailable. Return to the lobby.',
|
|
266
|
+
NO_QUESTIONS: 'The selected profile has no published questions. Add questions or choose another profile.',
|
|
267
|
+
PROTOCOL_UNSUPPORTED: 'This game and host do not support the same multiplayer version.',
|
|
268
|
+
PASSCODE_REQUIRED: 'Enter the room passcode to join, including as a spectator.',
|
|
269
|
+
INVALID_PASSCODE: 'The passcode is incorrect. Check it and retry.',
|
|
270
|
+
ROOM_FULL: 'There are no free seats of this type.',
|
|
271
|
+
NOT_HOST: 'Only the current lobby host can start or rematch.',
|
|
272
|
+
NOT_READY: 'Meet the required player counts and have every human player join and mark ready before starting.',
|
|
273
|
+
STALE_REVISION: 'The lobby changed. Refresh its status and retry.',
|
|
274
|
+
ROSTER_FROZEN: 'This round has already selected its players. Join as a spectator or wait for a rematch.',
|
|
275
|
+
LAUNCH_PENDING: 'Your launch is being prepared. Retry shortly.',
|
|
276
|
+
LAUNCH_EXPIRED: 'The reservation expired. Request a fresh launch.',
|
|
277
|
+
ALREADY_CONNECTED: 'This seat is already connected in another tab. Return to that tab.',
|
|
278
|
+
ARRIVAL_TIMEOUT: 'A required player did not arrive in time. Return to the lobby and rematch.',
|
|
279
|
+
RECOVERY_EXPIRED: 'The reconnect window ended. Return to the lobby for a new round.',
|
|
280
|
+
SEAT_REMOVED: 'This seat has left or was removed and cannot reconnect.',
|
|
281
|
+
INTERRUPTED: 'The round was interrupted before its result was saved. Return to the lobby.',
|
|
282
|
+
ROUND_TERMINAL: 'This round has ended. Return to the lobby.',
|
|
283
|
+
RATE_LIMITED: 'Too many requests. Wait a moment and retry.',
|
|
284
|
+
UNAVAILABLE: 'The service is temporarily unavailable. Retry or leave the session.',
|
|
285
|
+
};
|
|
286
|
+
return messages[code] ?? code;
|
|
287
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ksaa-nlp/gamehub-sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "ESM SDK that lets GameHub-hosted games talk to the platform (auth, sessions, scores, bootstrap pipeline). Includes types and a Node-only dev harness for authoring UGC server bundles (`./server`, `./server/dev`).
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "ESM SDK that lets GameHub-hosted games talk to the platform (auth, sessions, scores, bootstrap pipeline). The main entry and IIFE bundle are transport-agnostic; the opt-in `./multiplayer` subpath adds the game-owned Colyseus connection for v2 rounds (optional peers @colyseus/sdk and @colyseus/schema). Includes types and a Node-only dev harness for authoring UGC server bundles (`./server`, `./server/dev`).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/src/main.js",
|
|
7
7
|
"module": "./dist/src/main.js",
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
"import": "./dist/src/browser-entry.js",
|
|
18
18
|
"default": "./dist/src/browser-entry.js"
|
|
19
19
|
},
|
|
20
|
+
"./multiplayer": {
|
|
21
|
+
"types": "./dist/src/multiplayer-entry.d.ts",
|
|
22
|
+
"import": "./dist/src/multiplayer-entry.js",
|
|
23
|
+
"default": "./dist/src/multiplayer-entry.js"
|
|
24
|
+
},
|
|
20
25
|
"./server": {
|
|
21
26
|
"types": "./dist/src/server-entry.d.ts",
|
|
22
27
|
"import": "./dist/src/server-entry.js",
|
|
@@ -30,6 +35,16 @@
|
|
|
30
35
|
"./gamehub-sdk.js": "./dist/gamehub-sdk.js",
|
|
31
36
|
"./package.json": "./package.json"
|
|
32
37
|
},
|
|
38
|
+
"imports": {
|
|
39
|
+
"#multiplayer-contracts": {
|
|
40
|
+
"types": "./dist/vendor/gamehub-multiplayer-contracts/index.d.ts",
|
|
41
|
+
"default": "./dist/vendor/gamehub-multiplayer-contracts/index.js"
|
|
42
|
+
},
|
|
43
|
+
"#colyseus-schemas/*": {
|
|
44
|
+
"types": "./dist/vendor/colyseus-schemas/*.d.ts",
|
|
45
|
+
"default": "./dist/vendor/colyseus-schemas/*.js"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
33
48
|
"sideEffects": [
|
|
34
49
|
"./dist/src/browser-entry.js",
|
|
35
50
|
"./dist/gamehub-sdk.js"
|
|
@@ -41,7 +56,8 @@
|
|
|
41
56
|
],
|
|
42
57
|
"scripts": {
|
|
43
58
|
"format": "prettier --write \"**/*.{ts,tsx,js,jsx,md,json}\"",
|
|
44
|
-
"build:
|
|
59
|
+
"build:vendor": "npm --prefix ../gamehub-multiplayer-contracts run build && npm --prefix ../colyseus-schemas run build && node scripts/vendor-workspace.mjs",
|
|
60
|
+
"build:tsc": "npm run build:vendor && tsc -p ./tsconfig.json",
|
|
45
61
|
"build:bundle": "esbuild src/browser-entry.ts --bundle --format=iife --platform=browser --target=es2020 --outfile=dist/gamehub-sdk.js --sourcemap",
|
|
46
62
|
"build": "npm run build:tsc && npm run build:bundle",
|
|
47
63
|
"test": "npm run build:tsc && node --test test/*.test.mjs",
|
|
@@ -50,7 +66,21 @@
|
|
|
50
66
|
},
|
|
51
67
|
"author": "KSAANLP",
|
|
52
68
|
"license": "ISC",
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"@colyseus/schema": "^5.0.36",
|
|
71
|
+
"@colyseus/sdk": "^0.18.5"
|
|
72
|
+
},
|
|
73
|
+
"peerDependenciesMeta": {
|
|
74
|
+
"@colyseus/schema": {
|
|
75
|
+
"optional": true
|
|
76
|
+
},
|
|
77
|
+
"@colyseus/sdk": {
|
|
78
|
+
"optional": true
|
|
79
|
+
}
|
|
80
|
+
},
|
|
53
81
|
"devDependencies": {
|
|
82
|
+
"@colyseus/schema": "^5.0.36",
|
|
83
|
+
"@colyseus/sdk": "^0.18.5",
|
|
54
84
|
"esbuild": "^0.25.0",
|
|
55
85
|
"typescript": "^6.0.3"
|
|
56
86
|
}
|