@air-jam/sdk 0.1.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 +87 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/chunk-XSXI3RIS.js +72 -0
- package/dist/chunk-XSXI3RIS.js.map +1 -0
- package/dist/index.cjs +3514 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1529 -0
- package/dist/index.d.ts +1529 -0
- package/dist/index.js +3366 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.cjs +85 -0
- package/dist/protocol.cjs.map +1 -0
- package/dist/protocol.d.cts +242 -0
- package/dist/protocol.d.ts +242 -0
- package/dist/protocol.js +3 -0
- package/dist/protocol.js.map +1 -0
- package/package.json +81 -0
- package/src/AirJamClient.ts +775 -0
- package/src/audio/audio-manager.ts +395 -0
- package/src/audio/hooks.tsx +140 -0
- package/src/audio/index.ts +3 -0
- package/src/audio/volume-store.ts +77 -0
- package/src/components/air-jam-overlay.tsx +456 -0
- package/src/components/controller-shell.tsx +329 -0
- package/src/components/qr-scanner-dialog.tsx +373 -0
- package/src/components/ui/alert.tsx +66 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +47 -0
- package/src/components/ui/button.tsx +61 -0
- package/src/components/ui/card.tsx +92 -0
- package/src/components/ui/slider.tsx +61 -0
- package/src/components/ui/tabs.tsx +64 -0
- package/src/components/volume-controls.tsx +93 -0
- package/src/constants.ts +45 -0
- package/src/context/air-jam-context.tsx +478 -0
- package/src/context/index.ts +2 -0
- package/src/context/socket-manager.ts +98 -0
- package/src/events.ts +151 -0
- package/src/hooks/use-air-jam-controller.ts +608 -0
- package/src/hooks/use-air-jam-host.ts +624 -0
- package/src/hooks/use-air-jam-shell.ts +343 -0
- package/src/hooks/use-fullscreen.ts +108 -0
- package/src/hooks/use-get-input.ts +97 -0
- package/src/hooks/use-send-signal.ts +164 -0
- package/src/index.ts +33 -0
- package/src/internal/input-manager.ts +362 -0
- package/src/protocol.ts +331 -0
- package/src/state/connection-store.ts +84 -0
- package/src/styles.css +128 -0
- package/src/types/browser.ts +42 -0
- package/src/utils/cn.ts +4 -0
- package/src/utils/ids.ts +31 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/mode.ts +13 -0
- package/src/utils/network-ip.ts +145 -0
- package/src/utils/url-builder.ts +170 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3366 @@
|
|
|
1
|
+
import { roomCodeSchema, controllerInputSchema, controllerSystemSchema, controllerStateSchema, controllerJoinSchema, hostRegistrationSchema } from './chunk-XSXI3RIS.js';
|
|
2
|
+
export { AIRJAM_PROXY_PREFIX, ErrorCode, controllerInputSchema, controllerJoinSchema, controllerLeaveSchema, controllerStateSchema, controllerSystemSchema, hostJoinAsChildSchema, hostRegisterSystemSchema, hostRegistrationSchema, roomCodeSchema, systemLaunchGameSchema } from './chunk-XSXI3RIS.js';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { createContext, useMemo, useEffect, useCallback, useContext, useRef, useState } from 'react';
|
|
5
|
+
import { create, useStore } from 'zustand';
|
|
6
|
+
import { useShallow } from 'zustand/react/shallow';
|
|
7
|
+
import { io } from 'socket.io-client';
|
|
8
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
9
|
+
import { Howler, Howl } from 'howler';
|
|
10
|
+
import { Volume2, Music, Headphones, Pause, Play, QrCode, MonitorSmartphone, Settings, RefreshCw, Minimize, Maximize } from 'lucide-react';
|
|
11
|
+
import QRCode from 'qrcode';
|
|
12
|
+
import { cva } from 'class-variance-authority';
|
|
13
|
+
import { clsx } from 'clsx';
|
|
14
|
+
import { twMerge } from 'tailwind-merge';
|
|
15
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
16
|
+
import * as TabsPrimitive from '@radix-ui/react-tabs';
|
|
17
|
+
import * as SliderPrimitive from '@radix-ui/react-slider';
|
|
18
|
+
import { Html5Qrcode } from 'html5-qrcode';
|
|
19
|
+
|
|
20
|
+
// src/constants.ts
|
|
21
|
+
var DEFAULT_SERVER_PORT = 4e3;
|
|
22
|
+
var DEFAULT_CONTROLLER_PATH = "/joypad";
|
|
23
|
+
var INPUT_DEBOUNCE_MS = 200;
|
|
24
|
+
var TOGGLE_DEBOUNCE_MS = 300;
|
|
25
|
+
var SOCKET_CONFIG = {
|
|
26
|
+
/**
|
|
27
|
+
* Ping interval in milliseconds
|
|
28
|
+
*/
|
|
29
|
+
PING_INTERVAL: 2e3,
|
|
30
|
+
/**
|
|
31
|
+
* Ping timeout in milliseconds
|
|
32
|
+
*/
|
|
33
|
+
PING_TIMEOUT: 5e3
|
|
34
|
+
};
|
|
35
|
+
var DEFAULT_MAX_PLAYERS = 8;
|
|
36
|
+
|
|
37
|
+
// src/internal/input-manager.ts
|
|
38
|
+
var InputManager = class {
|
|
39
|
+
inputBuffer = /* @__PURE__ */ new Map();
|
|
40
|
+
latchState = /* @__PURE__ */ new Map();
|
|
41
|
+
config;
|
|
42
|
+
roomId;
|
|
43
|
+
constructor(config = {}, roomId) {
|
|
44
|
+
this.config = config;
|
|
45
|
+
this.roomId = roomId;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Handles incoming input events from the socket.
|
|
49
|
+
* Should be called when 'server:input' event is received.
|
|
50
|
+
*/
|
|
51
|
+
handleInput(payload) {
|
|
52
|
+
if (this.roomId && payload.roomId !== this.roomId) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
this.inputBuffer.set(payload.controllerId, payload.input);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets input for a specific controller.
|
|
59
|
+
* Returns validated and typed input if schema is provided, otherwise raw Record.
|
|
60
|
+
* If latching is configured, applies latching logic.
|
|
61
|
+
*/
|
|
62
|
+
getInput(controllerId) {
|
|
63
|
+
const rawInput = this.inputBuffer.get(controllerId);
|
|
64
|
+
if (!rawInput) {
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
let validatedInput = rawInput;
|
|
68
|
+
if (this.config.schema) {
|
|
69
|
+
const result = this.config.schema.safeParse(rawInput);
|
|
70
|
+
if (!result.success) {
|
|
71
|
+
console.warn(
|
|
72
|
+
`[InputManager] Invalid input for controller ${controllerId}:`,
|
|
73
|
+
result.error
|
|
74
|
+
);
|
|
75
|
+
return void 0;
|
|
76
|
+
}
|
|
77
|
+
validatedInput = result.data;
|
|
78
|
+
}
|
|
79
|
+
if (this.config.latch) {
|
|
80
|
+
return this.getLatched(
|
|
81
|
+
controllerId,
|
|
82
|
+
validatedInput
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return validatedInput;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Clears input state for a controller (e.g., when player leaves).
|
|
89
|
+
*/
|
|
90
|
+
clearInput(controllerId) {
|
|
91
|
+
this.inputBuffer.delete(controllerId);
|
|
92
|
+
this.latchState.delete(controllerId);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Updates the room ID filter.
|
|
96
|
+
*/
|
|
97
|
+
setRoomId(roomId) {
|
|
98
|
+
this.roomId = roomId;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Internal method to apply latching logic.
|
|
102
|
+
*/
|
|
103
|
+
getLatched(controllerId, rawInput) {
|
|
104
|
+
const { booleanFields = [], vectorFields = [] } = this.config.latch;
|
|
105
|
+
const state = this.latchState.get(controllerId);
|
|
106
|
+
const rawHash = JSON.stringify(rawInput);
|
|
107
|
+
const rawChanged = rawHash !== state?.lastRawHash;
|
|
108
|
+
if (!rawChanged && state) {
|
|
109
|
+
const resetLatched = { ...rawInput };
|
|
110
|
+
const resetLatchedRecord = resetLatched;
|
|
111
|
+
const actualRawRecord2 = rawInput;
|
|
112
|
+
for (const field of booleanFields) {
|
|
113
|
+
const rawValue = actualRawRecord2[field];
|
|
114
|
+
resetLatchedRecord[field] = rawValue ?? false;
|
|
115
|
+
}
|
|
116
|
+
for (const field of vectorFields) {
|
|
117
|
+
const rawValue = actualRawRecord2[field];
|
|
118
|
+
if (rawValue && typeof rawValue === "object" && !Array.isArray(rawValue)) {
|
|
119
|
+
const rawVec = rawValue;
|
|
120
|
+
if (typeof rawVec.x === "number" && typeof rawVec.y === "number") {
|
|
121
|
+
resetLatchedRecord[field] = rawVec;
|
|
122
|
+
} else {
|
|
123
|
+
resetLatchedRecord[field] = { x: 0, y: 0 };
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
resetLatchedRecord[field] = { x: 0, y: 0 };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
this.latchState.set(controllerId, {
|
|
130
|
+
raw: rawInput,
|
|
131
|
+
latched: resetLatched,
|
|
132
|
+
lastRawHash: rawHash
|
|
133
|
+
});
|
|
134
|
+
return resetLatched;
|
|
135
|
+
}
|
|
136
|
+
const prevLatched = state?.latched ?? {};
|
|
137
|
+
const latched = { ...rawInput };
|
|
138
|
+
const latchedRecord = latched;
|
|
139
|
+
const actualRawRecord = rawInput;
|
|
140
|
+
const prevLatchedRecord = prevLatched;
|
|
141
|
+
for (const field of booleanFields) {
|
|
142
|
+
const rawValue = actualRawRecord[field];
|
|
143
|
+
const prevValue = prevLatchedRecord[field];
|
|
144
|
+
if (typeof rawValue === "boolean") {
|
|
145
|
+
latchedRecord[field] = rawValue || prevValue === true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
for (const field of vectorFields) {
|
|
149
|
+
const rawValue = actualRawRecord[field];
|
|
150
|
+
const prevValue = prevLatchedRecord[field];
|
|
151
|
+
if (rawValue && typeof rawValue === "object" && !Array.isArray(rawValue)) {
|
|
152
|
+
const rawVec = rawValue;
|
|
153
|
+
const prevVec = prevValue;
|
|
154
|
+
if (typeof rawVec.x === "number" && typeof rawVec.y === "number") {
|
|
155
|
+
const isRawActive = rawVec.x !== 0 || rawVec.y !== 0;
|
|
156
|
+
const isPrevActive = prevVec && typeof prevVec.x === "number" && typeof prevVec.y === "number" && (prevVec.x !== 0 || prevVec.y !== 0);
|
|
157
|
+
if (isRawActive) {
|
|
158
|
+
latchedRecord[field] = rawVec;
|
|
159
|
+
} else if (isPrevActive) {
|
|
160
|
+
latchedRecord[field] = prevVec;
|
|
161
|
+
} else {
|
|
162
|
+
latchedRecord[field] = { x: 0, y: 0 };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
this.latchState.set(controllerId, {
|
|
168
|
+
raw: rawInput,
|
|
169
|
+
latched,
|
|
170
|
+
lastRawHash: rawHash
|
|
171
|
+
});
|
|
172
|
+
return latched;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
var createAirJamStore = () => create((set) => ({
|
|
176
|
+
role: null,
|
|
177
|
+
roomId: null,
|
|
178
|
+
controllerId: null,
|
|
179
|
+
connectionStatus: "idle",
|
|
180
|
+
mode: "standalone",
|
|
181
|
+
gameState: "paused",
|
|
182
|
+
stateMessage: void 0,
|
|
183
|
+
players: [],
|
|
184
|
+
lastError: void 0,
|
|
185
|
+
registeredRoomId: null,
|
|
186
|
+
setRole: (role) => set({ role }),
|
|
187
|
+
setRoomId: (roomId) => set({ roomId }),
|
|
188
|
+
setControllerId: (controllerId) => set({ controllerId }),
|
|
189
|
+
setStatus: (connectionStatus) => set({ connectionStatus }),
|
|
190
|
+
setMode: (mode) => set({ mode }),
|
|
191
|
+
setGameState: (gameState) => set({ gameState }),
|
|
192
|
+
setStateMessage: (stateMessage) => set({ stateMessage }),
|
|
193
|
+
toggleGameState: () => set((state) => ({
|
|
194
|
+
gameState: state.gameState === "paused" ? "playing" : "paused"
|
|
195
|
+
})),
|
|
196
|
+
setError: (message) => set({ lastError: message }),
|
|
197
|
+
upsertPlayer: (player) => set((state) => {
|
|
198
|
+
const existingIndex = state.players.findIndex(
|
|
199
|
+
(entry) => entry.id === player.id
|
|
200
|
+
);
|
|
201
|
+
if (existingIndex >= 0) {
|
|
202
|
+
const nextPlayers = [...state.players];
|
|
203
|
+
nextPlayers[existingIndex] = player;
|
|
204
|
+
return { players: nextPlayers };
|
|
205
|
+
}
|
|
206
|
+
return { players: [...state.players, player] };
|
|
207
|
+
}),
|
|
208
|
+
removePlayer: (playerId) => set((state) => ({
|
|
209
|
+
players: state.players.filter((player) => player.id !== playerId)
|
|
210
|
+
})),
|
|
211
|
+
resetPlayers: () => set({ players: [] }),
|
|
212
|
+
resetGameState: () => set({ gameState: "paused", stateMessage: void 0 }),
|
|
213
|
+
setRegisteredRoomId: (roomId) => set({ registeredRoomId: roomId })
|
|
214
|
+
}));
|
|
215
|
+
var SocketManager = class {
|
|
216
|
+
sockets = {};
|
|
217
|
+
serverUrl;
|
|
218
|
+
constructor(serverUrl) {
|
|
219
|
+
this.serverUrl = this.resolveServerUrl(serverUrl);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get or create a socket for the specified role.
|
|
223
|
+
* Sockets are lazily created and cached per role.
|
|
224
|
+
*/
|
|
225
|
+
getSocket(role) {
|
|
226
|
+
const existing = this.sockets[role];
|
|
227
|
+
if (existing) {
|
|
228
|
+
return existing;
|
|
229
|
+
}
|
|
230
|
+
const socket = io(this.serverUrl, {
|
|
231
|
+
autoConnect: false,
|
|
232
|
+
transports: ["websocket"],
|
|
233
|
+
query: { role }
|
|
234
|
+
});
|
|
235
|
+
this.sockets[role] = socket;
|
|
236
|
+
return socket;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Disconnect a socket by role.
|
|
240
|
+
* NOTE: We intentionally do NOT delete the socket from cache.
|
|
241
|
+
* This ensures all hooks using getSocket() get the same instance,
|
|
242
|
+
* even during React Strict Mode remounts. The socket can be reconnected
|
|
243
|
+
* by calling socket.connect() again.
|
|
244
|
+
*/
|
|
245
|
+
disconnect(role) {
|
|
246
|
+
const socket = this.sockets[role];
|
|
247
|
+
if (socket) {
|
|
248
|
+
socket.disconnect();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Disconnect all sockets managed by this instance
|
|
253
|
+
*/
|
|
254
|
+
disconnectAll() {
|
|
255
|
+
for (const role of Object.keys(this.sockets)) {
|
|
256
|
+
this.disconnect(role);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Get the resolved server URL
|
|
261
|
+
*/
|
|
262
|
+
getServerUrl() {
|
|
263
|
+
return this.serverUrl;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Resolve server URL with fallbacks
|
|
267
|
+
*/
|
|
268
|
+
resolveServerUrl(explicit) {
|
|
269
|
+
if (explicit) {
|
|
270
|
+
return this.normalizeOrigin(explicit);
|
|
271
|
+
}
|
|
272
|
+
if (typeof window !== "undefined") {
|
|
273
|
+
const url = new URL(window.location.href);
|
|
274
|
+
url.port = String(DEFAULT_SERVER_PORT);
|
|
275
|
+
return url.origin;
|
|
276
|
+
}
|
|
277
|
+
return `http://localhost:${DEFAULT_SERVER_PORT}`;
|
|
278
|
+
}
|
|
279
|
+
normalizeOrigin(origin) {
|
|
280
|
+
if (!origin.includes("://")) {
|
|
281
|
+
return `http://${origin}`;
|
|
282
|
+
}
|
|
283
|
+
return origin;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
var AirJamContext = createContext(null);
|
|
287
|
+
var getViteEnv = () => {
|
|
288
|
+
try {
|
|
289
|
+
const meta = import.meta;
|
|
290
|
+
if (meta && typeof meta.env === "object") {
|
|
291
|
+
return meta.env;
|
|
292
|
+
}
|
|
293
|
+
} catch {
|
|
294
|
+
}
|
|
295
|
+
return void 0;
|
|
296
|
+
};
|
|
297
|
+
var getEnvServerUrl = () => {
|
|
298
|
+
const viteEnv = getViteEnv();
|
|
299
|
+
if (viteEnv?.VITE_AIR_JAM_SERVER_URL) {
|
|
300
|
+
return viteEnv.VITE_AIR_JAM_SERVER_URL;
|
|
301
|
+
}
|
|
302
|
+
if (typeof process !== "undefined" && process.env) {
|
|
303
|
+
const nextUrl = process.env.NEXT_PUBLIC_AIR_JAM_SERVER_URL;
|
|
304
|
+
if (nextUrl) return nextUrl;
|
|
305
|
+
}
|
|
306
|
+
return void 0;
|
|
307
|
+
};
|
|
308
|
+
var getEnvApiKey = () => {
|
|
309
|
+
const viteEnv = getViteEnv();
|
|
310
|
+
if (viteEnv?.VITE_AIR_JAM_API_KEY) {
|
|
311
|
+
return viteEnv.VITE_AIR_JAM_API_KEY;
|
|
312
|
+
}
|
|
313
|
+
if (typeof process !== "undefined" && process.env) {
|
|
314
|
+
const nextKey = process.env.NEXT_PUBLIC_AIR_JAM_API_KEY;
|
|
315
|
+
if (nextKey) return nextKey;
|
|
316
|
+
}
|
|
317
|
+
return void 0;
|
|
318
|
+
};
|
|
319
|
+
var AirJamProvider = ({
|
|
320
|
+
children,
|
|
321
|
+
serverUrl,
|
|
322
|
+
apiKey,
|
|
323
|
+
controllerPath = DEFAULT_CONTROLLER_PATH,
|
|
324
|
+
maxPlayers = DEFAULT_MAX_PLAYERS,
|
|
325
|
+
publicHost,
|
|
326
|
+
input
|
|
327
|
+
}) => {
|
|
328
|
+
const config = useMemo(
|
|
329
|
+
() => ({
|
|
330
|
+
serverUrl: serverUrl ?? getEnvServerUrl(),
|
|
331
|
+
apiKey: apiKey ?? getEnvApiKey(),
|
|
332
|
+
controllerPath,
|
|
333
|
+
maxPlayers,
|
|
334
|
+
publicHost
|
|
335
|
+
}),
|
|
336
|
+
[serverUrl, apiKey, controllerPath, maxPlayers, publicHost]
|
|
337
|
+
);
|
|
338
|
+
const inputManager = useMemo(() => {
|
|
339
|
+
if (!input) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
return new InputManager(input);
|
|
343
|
+
}, [input]);
|
|
344
|
+
const store = useMemo(() => createAirJamStore(), []);
|
|
345
|
+
const socketManager = useMemo(
|
|
346
|
+
() => new SocketManager(config.serverUrl),
|
|
347
|
+
// Only recreate if serverUrl changes - intentionally omitting config
|
|
348
|
+
[config.serverUrl]
|
|
349
|
+
);
|
|
350
|
+
useEffect(() => {
|
|
351
|
+
return () => {
|
|
352
|
+
socketManager.disconnectAll();
|
|
353
|
+
};
|
|
354
|
+
}, [socketManager]);
|
|
355
|
+
const getSocket = useCallback(
|
|
356
|
+
(role) => socketManager.getSocket(role),
|
|
357
|
+
[socketManager]
|
|
358
|
+
);
|
|
359
|
+
const disconnectSocket = useCallback(
|
|
360
|
+
(role) => socketManager.disconnect(role),
|
|
361
|
+
[socketManager]
|
|
362
|
+
);
|
|
363
|
+
const contextValue = useMemo(
|
|
364
|
+
() => ({
|
|
365
|
+
config,
|
|
366
|
+
store,
|
|
367
|
+
socketManager,
|
|
368
|
+
getSocket,
|
|
369
|
+
disconnectSocket,
|
|
370
|
+
inputManager
|
|
371
|
+
}),
|
|
372
|
+
[config, store, socketManager, getSocket, disconnectSocket, inputManager]
|
|
373
|
+
);
|
|
374
|
+
return /* @__PURE__ */ jsx(AirJamContext.Provider, { value: contextValue, children });
|
|
375
|
+
};
|
|
376
|
+
var useAirJamContext = () => {
|
|
377
|
+
const context = useContext(AirJamContext);
|
|
378
|
+
if (!context) {
|
|
379
|
+
throw new Error(
|
|
380
|
+
"useAirJamContext must be used within an AirJamProvider. Wrap your app or component tree with <AirJamProvider>."
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
return context;
|
|
384
|
+
};
|
|
385
|
+
var useAirJamConfig = () => {
|
|
386
|
+
return useAirJamContext().config;
|
|
387
|
+
};
|
|
388
|
+
var useAirJamState = (selector) => {
|
|
389
|
+
const { store } = useAirJamContext();
|
|
390
|
+
return useStore(store, useShallow(selector));
|
|
391
|
+
};
|
|
392
|
+
var useAirJamSocket = (role) => {
|
|
393
|
+
const { getSocket } = useAirJamContext();
|
|
394
|
+
return useMemo(() => getSocket(role), [getSocket, role]);
|
|
395
|
+
};
|
|
396
|
+
var STORAGE_KEY = "air-jam-volume-settings";
|
|
397
|
+
var loadFromStorage = () => {
|
|
398
|
+
if (typeof window === "undefined") return {};
|
|
399
|
+
try {
|
|
400
|
+
const stored = localStorage.getItem(STORAGE_KEY);
|
|
401
|
+
if (stored) {
|
|
402
|
+
const parsed = JSON.parse(stored);
|
|
403
|
+
return {
|
|
404
|
+
masterVolume: parsed.masterVolume ?? 1,
|
|
405
|
+
musicVolume: parsed.musicVolume ?? 0.8,
|
|
406
|
+
sfxVolume: parsed.sfxVolume ?? 1
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
} catch {
|
|
410
|
+
}
|
|
411
|
+
return {};
|
|
412
|
+
};
|
|
413
|
+
var saveToStorage = (state) => {
|
|
414
|
+
if (typeof window === "undefined") return;
|
|
415
|
+
try {
|
|
416
|
+
localStorage.setItem(
|
|
417
|
+
STORAGE_KEY,
|
|
418
|
+
JSON.stringify({
|
|
419
|
+
masterVolume: state.masterVolume,
|
|
420
|
+
musicVolume: state.musicVolume,
|
|
421
|
+
sfxVolume: state.sfxVolume
|
|
422
|
+
})
|
|
423
|
+
);
|
|
424
|
+
} catch {
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
var initialValues = loadFromStorage();
|
|
428
|
+
var useVolumeStore = create((set, get) => ({
|
|
429
|
+
masterVolume: initialValues.masterVolume ?? 1,
|
|
430
|
+
musicVolume: initialValues.musicVolume ?? 0.8,
|
|
431
|
+
sfxVolume: initialValues.sfxVolume ?? 1,
|
|
432
|
+
setMasterVolume: (volume) => {
|
|
433
|
+
const clamped = Math.max(0, Math.min(1, volume));
|
|
434
|
+
set({ masterVolume: clamped });
|
|
435
|
+
saveToStorage({ ...get(), masterVolume: clamped });
|
|
436
|
+
},
|
|
437
|
+
setMusicVolume: (volume) => {
|
|
438
|
+
const clamped = Math.max(0, Math.min(1, volume));
|
|
439
|
+
set({ musicVolume: clamped });
|
|
440
|
+
saveToStorage({ ...get(), musicVolume: clamped });
|
|
441
|
+
},
|
|
442
|
+
setSfxVolume: (volume) => {
|
|
443
|
+
const clamped = Math.max(0, Math.min(1, volume));
|
|
444
|
+
set({ sfxVolume: clamped });
|
|
445
|
+
saveToStorage({ ...get(), sfxVolume: clamped });
|
|
446
|
+
},
|
|
447
|
+
getEffectiveVolume: (category) => {
|
|
448
|
+
const { masterVolume, musicVolume, sfxVolume } = get();
|
|
449
|
+
const categoryVolume = category === "music" ? musicVolume : sfxVolume;
|
|
450
|
+
return masterVolume * categoryVolume;
|
|
451
|
+
}
|
|
452
|
+
}));
|
|
453
|
+
|
|
454
|
+
// src/audio/audio-manager.ts
|
|
455
|
+
function detectSoundCategory(config) {
|
|
456
|
+
if (config.category) {
|
|
457
|
+
return config.category;
|
|
458
|
+
}
|
|
459
|
+
if (config.loop && config.html5) {
|
|
460
|
+
return "music";
|
|
461
|
+
}
|
|
462
|
+
return "sfx";
|
|
463
|
+
}
|
|
464
|
+
var AudioManager = class {
|
|
465
|
+
sounds = /* @__PURE__ */ new Map();
|
|
466
|
+
categories = /* @__PURE__ */ new Map();
|
|
467
|
+
manifest = {};
|
|
468
|
+
_muted = false;
|
|
469
|
+
socket = null;
|
|
470
|
+
roomId = null;
|
|
471
|
+
activeSoundIds = /* @__PURE__ */ new Map();
|
|
472
|
+
volumeUnsubscribe = null;
|
|
473
|
+
constructor(manifest) {
|
|
474
|
+
const initialState = useVolumeStore.getState();
|
|
475
|
+
const updateAllSounds = (state) => {
|
|
476
|
+
this.activeSoundIds.forEach((info, howlSoundId) => {
|
|
477
|
+
const sound = this.sounds.get(info.soundId);
|
|
478
|
+
if (sound) {
|
|
479
|
+
const config = this.manifest[info.soundId];
|
|
480
|
+
const baseVolume = config?.volume ?? 1;
|
|
481
|
+
const effectiveVolume = state.getEffectiveVolume(info.category);
|
|
482
|
+
const finalVolume = baseVolume * effectiveVolume;
|
|
483
|
+
sound.volume(finalVolume, howlSoundId);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
this.sounds.forEach((sound, soundId) => {
|
|
487
|
+
const category = this.categories.get(soundId) ?? "sfx";
|
|
488
|
+
const config = this.manifest[soundId];
|
|
489
|
+
const baseVolume = config?.volume ?? 1;
|
|
490
|
+
const effectiveVolume = state.getEffectiveVolume(category);
|
|
491
|
+
const newVolume = baseVolume * effectiveVolume;
|
|
492
|
+
sound.volume(newVolume);
|
|
493
|
+
});
|
|
494
|
+
};
|
|
495
|
+
this.volumeUnsubscribe = useVolumeStore.subscribe(updateAllSounds);
|
|
496
|
+
if (manifest) {
|
|
497
|
+
this.load(manifest);
|
|
498
|
+
updateAllSounds(initialState);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Cleanup method to unsubscribe from volume changes
|
|
503
|
+
*/
|
|
504
|
+
destroy() {
|
|
505
|
+
if (this.volumeUnsubscribe) {
|
|
506
|
+
this.volumeUnsubscribe();
|
|
507
|
+
this.volumeUnsubscribe = null;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Get the category for a sound (with auto-detection)
|
|
512
|
+
*/
|
|
513
|
+
getCategory(soundId) {
|
|
514
|
+
return this.categories.get(soundId) ?? "sfx";
|
|
515
|
+
}
|
|
516
|
+
setSocket(socket, roomId) {
|
|
517
|
+
this.socket = socket;
|
|
518
|
+
if (roomId) this.roomId = roomId;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Initialize audio context (must be called on user interaction)
|
|
522
|
+
*/
|
|
523
|
+
init() {
|
|
524
|
+
if (Howler.ctx && Howler.ctx.state === "suspended") {
|
|
525
|
+
Howler.ctx.resume();
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Check if audio context is ready to play sounds
|
|
530
|
+
*/
|
|
531
|
+
isReady() {
|
|
532
|
+
return Howler.ctx !== null && Howler.ctx.state === "running";
|
|
533
|
+
}
|
|
534
|
+
load(manifest) {
|
|
535
|
+
this.manifest = { ...this.manifest, ...manifest };
|
|
536
|
+
const volumeStore = useVolumeStore.getState();
|
|
537
|
+
Object.entries(manifest).forEach(([key, config]) => {
|
|
538
|
+
if (this.sounds.has(key)) return;
|
|
539
|
+
const category = detectSoundCategory(config);
|
|
540
|
+
this.categories.set(key, category);
|
|
541
|
+
const baseVolume = config.volume ?? 1;
|
|
542
|
+
const effectiveVolume = volumeStore.getEffectiveVolume(category);
|
|
543
|
+
const initialVolume = baseVolume * effectiveVolume;
|
|
544
|
+
const sound = new Howl({
|
|
545
|
+
src: config.src,
|
|
546
|
+
volume: initialVolume,
|
|
547
|
+
// Use effective volume from the start
|
|
548
|
+
loop: config.loop ?? false,
|
|
549
|
+
html5: config.html5 ?? false,
|
|
550
|
+
sprite: config.sprite,
|
|
551
|
+
preload: true
|
|
552
|
+
});
|
|
553
|
+
this.sounds.set(key, sound);
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
play(id, options) {
|
|
557
|
+
const {
|
|
558
|
+
remote = false,
|
|
559
|
+
target,
|
|
560
|
+
volume,
|
|
561
|
+
loop,
|
|
562
|
+
sprite,
|
|
563
|
+
pitch
|
|
564
|
+
} = options || {};
|
|
565
|
+
if (remote) {
|
|
566
|
+
this.playRemote(id, target, volume, loop);
|
|
567
|
+
return null;
|
|
568
|
+
} else {
|
|
569
|
+
return this.playLocal(id, volume, loop, sprite, pitch);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
playLocal(id, volume, loop, sprite, pitch) {
|
|
573
|
+
const sound = this.sounds.get(id);
|
|
574
|
+
if (!sound) {
|
|
575
|
+
console.warn(`Sound "${id}" not found`);
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
const category = this.getCategory(id);
|
|
579
|
+
const config = this.manifest[id];
|
|
580
|
+
const baseVolume = config?.volume ?? 1;
|
|
581
|
+
const volumeStore = useVolumeStore.getState();
|
|
582
|
+
const effectiveVolume = volumeStore.getEffectiveVolume(category);
|
|
583
|
+
const finalVolume = volume !== void 0 ? volume * effectiveVolume : baseVolume * effectiveVolume;
|
|
584
|
+
const soundId = sound.play(sprite);
|
|
585
|
+
this.activeSoundIds.set(soundId, { soundId: id, category });
|
|
586
|
+
sound.once("end", () => {
|
|
587
|
+
this.activeSoundIds.delete(soundId);
|
|
588
|
+
});
|
|
589
|
+
sound.volume(finalVolume, soundId);
|
|
590
|
+
if (loop !== void 0) sound.loop(loop, soundId);
|
|
591
|
+
if (pitch !== void 0) sound.rate(pitch, soundId);
|
|
592
|
+
return soundId;
|
|
593
|
+
}
|
|
594
|
+
playRemote(id, target, volume, loop) {
|
|
595
|
+
if (!this.socket || !this.roomId) return;
|
|
596
|
+
if (this.isHost()) {
|
|
597
|
+
this.socket.emit("host:play_sound", {
|
|
598
|
+
roomId: this.roomId,
|
|
599
|
+
targetControllerId: target,
|
|
600
|
+
// If undefined, broadcasts to all
|
|
601
|
+
soundId: id,
|
|
602
|
+
volume,
|
|
603
|
+
loop
|
|
604
|
+
});
|
|
605
|
+
} else {
|
|
606
|
+
this.socket.emit("controller:play_sound", {
|
|
607
|
+
roomId: this.roomId,
|
|
608
|
+
soundId: id,
|
|
609
|
+
volume,
|
|
610
|
+
loop
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
isHost() {
|
|
615
|
+
const query = this.socket?.io?.opts?.query;
|
|
616
|
+
return query && typeof query === "object" && "role" in query && query.role === "host" || false;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Play a sound at a specific 3D position
|
|
620
|
+
*/
|
|
621
|
+
playSpatial(id, pos, sprite) {
|
|
622
|
+
const soundId = this.playLocal(id, void 0, void 0, sprite);
|
|
623
|
+
if (soundId !== null) {
|
|
624
|
+
const sound = this.sounds.get(id);
|
|
625
|
+
if (sound) {
|
|
626
|
+
sound.pos(pos.x, pos.y, pos.z, soundId);
|
|
627
|
+
sound.pannerAttr(
|
|
628
|
+
{
|
|
629
|
+
panningModel: "HRTF",
|
|
630
|
+
refDistance: 1,
|
|
631
|
+
maxDistance: 1e3,
|
|
632
|
+
rolloffFactor: 1,
|
|
633
|
+
distanceModel: "inverse"
|
|
634
|
+
},
|
|
635
|
+
soundId
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return soundId;
|
|
640
|
+
}
|
|
641
|
+
stop(id, soundId) {
|
|
642
|
+
if (id) {
|
|
643
|
+
const sound = this.sounds.get(id);
|
|
644
|
+
sound?.stop(soundId);
|
|
645
|
+
if (soundId !== void 0) {
|
|
646
|
+
this.activeSoundIds.delete(soundId);
|
|
647
|
+
} else {
|
|
648
|
+
this.activeSoundIds.forEach((info, howlSoundId) => {
|
|
649
|
+
if (info.soundId === id) {
|
|
650
|
+
this.activeSoundIds.delete(howlSoundId);
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
} else {
|
|
655
|
+
Howler.stop();
|
|
656
|
+
this.activeSoundIds.clear();
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
volume(vol, id, soundId) {
|
|
660
|
+
if (id) {
|
|
661
|
+
const sound = this.sounds.get(id);
|
|
662
|
+
if (sound) {
|
|
663
|
+
if (soundId !== void 0) {
|
|
664
|
+
sound.volume(vol, soundId);
|
|
665
|
+
} else {
|
|
666
|
+
sound.volume(vol);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
} else {
|
|
670
|
+
Howler.volume(vol);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
mute(muted, id, soundId) {
|
|
674
|
+
if (id) {
|
|
675
|
+
const sound = this.sounds.get(id);
|
|
676
|
+
sound?.mute(muted, soundId);
|
|
677
|
+
} else {
|
|
678
|
+
this._muted = muted;
|
|
679
|
+
Howler.mute(muted);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
isMuted() {
|
|
683
|
+
return this._muted;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Update the listener position for spatial audio
|
|
687
|
+
*/
|
|
688
|
+
setListenerPos(x, y, z) {
|
|
689
|
+
Howler.pos(x, y, z);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Update the listener orientation for spatial audio
|
|
693
|
+
*/
|
|
694
|
+
setListenerOrientation(x, y, z, xUp, yUp, zUp) {
|
|
695
|
+
Howler.orientation(x, y, z, xUp, yUp, zUp);
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
var createAudioManager = (manifest) => new AudioManager(manifest);
|
|
699
|
+
var managerCache = /* @__PURE__ */ new WeakMap();
|
|
700
|
+
var globalInitialized = false;
|
|
701
|
+
function useAudio(manifest) {
|
|
702
|
+
const { store, getSocket } = useAirJamContext();
|
|
703
|
+
const roomId = useStore(store, (state) => state.roomId);
|
|
704
|
+
const role = useStore(store, (state) => state.role);
|
|
705
|
+
const initRef = useRef(false);
|
|
706
|
+
let manager = managerCache.get(manifest);
|
|
707
|
+
if (!manager) {
|
|
708
|
+
manager = new AudioManager(manifest);
|
|
709
|
+
managerCache.set(manifest, manager);
|
|
710
|
+
}
|
|
711
|
+
useEffect(() => {
|
|
712
|
+
if (role && roomId) {
|
|
713
|
+
const socket = getSocket(role);
|
|
714
|
+
manager.setSocket(socket, roomId);
|
|
715
|
+
}
|
|
716
|
+
}, [manager, role, roomId, getSocket]);
|
|
717
|
+
useEffect(() => {
|
|
718
|
+
if (initRef.current || globalInitialized) return;
|
|
719
|
+
const handleInteraction = () => {
|
|
720
|
+
manager.init();
|
|
721
|
+
globalInitialized = true;
|
|
722
|
+
initRef.current = true;
|
|
723
|
+
window.removeEventListener("click", handleInteraction);
|
|
724
|
+
window.removeEventListener("touchstart", handleInteraction);
|
|
725
|
+
window.removeEventListener("keydown", handleInteraction);
|
|
726
|
+
};
|
|
727
|
+
window.addEventListener("click", handleInteraction);
|
|
728
|
+
window.addEventListener("touchstart", handleInteraction);
|
|
729
|
+
window.addEventListener("keydown", handleInteraction);
|
|
730
|
+
return () => {
|
|
731
|
+
window.removeEventListener("click", handleInteraction);
|
|
732
|
+
window.removeEventListener("touchstart", handleInteraction);
|
|
733
|
+
window.removeEventListener("keydown", handleInteraction);
|
|
734
|
+
};
|
|
735
|
+
}, [manager]);
|
|
736
|
+
return manager;
|
|
737
|
+
}
|
|
738
|
+
var LegacyAudioContext = createContext(null);
|
|
739
|
+
var AudioProvider = ({
|
|
740
|
+
children,
|
|
741
|
+
manager
|
|
742
|
+
}) => {
|
|
743
|
+
const { store, getSocket } = useAirJamContext();
|
|
744
|
+
const roomId = useStore(store, (state) => state.roomId);
|
|
745
|
+
const role = useStore(store, (state) => state.role);
|
|
746
|
+
useEffect(() => {
|
|
747
|
+
if (role && roomId) {
|
|
748
|
+
const socket = getSocket(role);
|
|
749
|
+
manager.setSocket(socket, roomId);
|
|
750
|
+
}
|
|
751
|
+
}, [manager, role, roomId, getSocket]);
|
|
752
|
+
useEffect(() => {
|
|
753
|
+
const handleInteraction = () => {
|
|
754
|
+
manager.init();
|
|
755
|
+
window.removeEventListener("click", handleInteraction);
|
|
756
|
+
window.removeEventListener("touchstart", handleInteraction);
|
|
757
|
+
window.removeEventListener("keydown", handleInteraction);
|
|
758
|
+
};
|
|
759
|
+
window.addEventListener("click", handleInteraction);
|
|
760
|
+
window.addEventListener("touchstart", handleInteraction);
|
|
761
|
+
window.addEventListener("keydown", handleInteraction);
|
|
762
|
+
return () => {
|
|
763
|
+
window.removeEventListener("click", handleInteraction);
|
|
764
|
+
window.removeEventListener("touchstart", handleInteraction);
|
|
765
|
+
window.removeEventListener("keydown", handleInteraction);
|
|
766
|
+
};
|
|
767
|
+
}, [manager]);
|
|
768
|
+
return /* @__PURE__ */ jsx(LegacyAudioContext.Provider, { value: manager, children });
|
|
769
|
+
};
|
|
770
|
+
var useAudioLegacy = () => {
|
|
771
|
+
const manager = useContext(LegacyAudioContext);
|
|
772
|
+
if (!manager) {
|
|
773
|
+
throw new Error("useAudioLegacy must be used within an AudioProvider");
|
|
774
|
+
}
|
|
775
|
+
return manager;
|
|
776
|
+
};
|
|
777
|
+
var useAudioManager = (manifest) => {
|
|
778
|
+
const managerRef = useRef(null);
|
|
779
|
+
if (!managerRef.current) {
|
|
780
|
+
managerRef.current = new AudioManager(manifest);
|
|
781
|
+
}
|
|
782
|
+
return managerRef.current;
|
|
783
|
+
};
|
|
784
|
+
var cn = (...inputs) => twMerge(clsx(inputs));
|
|
785
|
+
var alertVariants = cva(
|
|
786
|
+
"relative w-full rounded-lg border px-4 py-3 text-sm grid has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] grid-cols-[0_1fr] has-[>svg]:gap-x-3 gap-y-0.5 items-start [&>svg]:size-4 [&>svg]:translate-y-0.5 [&>svg]:text-current",
|
|
787
|
+
{
|
|
788
|
+
variants: {
|
|
789
|
+
variant: {
|
|
790
|
+
default: "bg-card text-card-foreground",
|
|
791
|
+
destructive: "text-destructive bg-card [&>svg]:text-current *:data-[slot=alert-description]:text-destructive/90"
|
|
792
|
+
}
|
|
793
|
+
},
|
|
794
|
+
defaultVariants: {
|
|
795
|
+
variant: "default"
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
);
|
|
799
|
+
function Alert({
|
|
800
|
+
className,
|
|
801
|
+
variant,
|
|
802
|
+
...props
|
|
803
|
+
}) {
|
|
804
|
+
return /* @__PURE__ */ jsx(
|
|
805
|
+
"div",
|
|
806
|
+
{
|
|
807
|
+
"data-slot": "alert",
|
|
808
|
+
role: "alert",
|
|
809
|
+
className: cn(alertVariants({ variant }), className),
|
|
810
|
+
...props
|
|
811
|
+
}
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
function AlertDescription({
|
|
815
|
+
className,
|
|
816
|
+
...props
|
|
817
|
+
}) {
|
|
818
|
+
return /* @__PURE__ */ jsx(
|
|
819
|
+
"div",
|
|
820
|
+
{
|
|
821
|
+
"data-slot": "alert-description",
|
|
822
|
+
className: cn(
|
|
823
|
+
"text-muted-foreground col-start-2 grid justify-items-start gap-1 text-sm [&_p]:leading-relaxed",
|
|
824
|
+
className
|
|
825
|
+
),
|
|
826
|
+
...props
|
|
827
|
+
}
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
var Avatar = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(
|
|
831
|
+
"div",
|
|
832
|
+
{
|
|
833
|
+
ref,
|
|
834
|
+
className: cn(
|
|
835
|
+
"relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full",
|
|
836
|
+
className
|
|
837
|
+
),
|
|
838
|
+
...props
|
|
839
|
+
}
|
|
840
|
+
));
|
|
841
|
+
Avatar.displayName = "Avatar";
|
|
842
|
+
var AvatarImage = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(
|
|
843
|
+
"img",
|
|
844
|
+
{
|
|
845
|
+
ref,
|
|
846
|
+
className: cn("aspect-square h-full w-full", className),
|
|
847
|
+
...props
|
|
848
|
+
}
|
|
849
|
+
));
|
|
850
|
+
AvatarImage.displayName = "AvatarImage";
|
|
851
|
+
var AvatarFallback = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(
|
|
852
|
+
"div",
|
|
853
|
+
{
|
|
854
|
+
ref,
|
|
855
|
+
className: cn(
|
|
856
|
+
"bg-muted flex h-full w-full items-center justify-center rounded-full",
|
|
857
|
+
className
|
|
858
|
+
),
|
|
859
|
+
...props
|
|
860
|
+
}
|
|
861
|
+
));
|
|
862
|
+
AvatarFallback.displayName = "AvatarFallback";
|
|
863
|
+
var badgeVariants = cva(
|
|
864
|
+
"inline-flex items-center justify-center rounded-full border px-2 py-0.5 text-xs font-medium w-fit whitespace-nowrap shrink-0 [&>svg]:size-3 gap-1 [&>svg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden",
|
|
865
|
+
{
|
|
866
|
+
variants: {
|
|
867
|
+
variant: {
|
|
868
|
+
default: "border-transparent bg-primary text-primary-foreground [a&]:hover:bg-primary/90",
|
|
869
|
+
secondary: "border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90",
|
|
870
|
+
destructive: "border-transparent bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
|
|
871
|
+
outline: "text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground"
|
|
872
|
+
}
|
|
873
|
+
},
|
|
874
|
+
defaultVariants: {
|
|
875
|
+
variant: "default"
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
);
|
|
879
|
+
function Badge({
|
|
880
|
+
className,
|
|
881
|
+
variant,
|
|
882
|
+
asChild = false,
|
|
883
|
+
...props
|
|
884
|
+
}) {
|
|
885
|
+
const Comp = asChild ? Slot : "span";
|
|
886
|
+
return /* @__PURE__ */ jsx(
|
|
887
|
+
Comp,
|
|
888
|
+
{
|
|
889
|
+
"data-slot": "badge",
|
|
890
|
+
className: cn(badgeVariants({ variant }), className),
|
|
891
|
+
...props
|
|
892
|
+
}
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
var buttonVariants = cva(
|
|
896
|
+
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-all cursor-pointer disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 shrink-0 [&_svg]:shrink-0 outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
|
897
|
+
{
|
|
898
|
+
variants: {
|
|
899
|
+
variant: {
|
|
900
|
+
default: "bg-primary text-primary-foreground hover:bg-primary/90",
|
|
901
|
+
destructive: "bg-destructive text-white hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
|
|
902
|
+
outline: "border bg-background shadow-xs hover:bg-accent hover:text-accent-foreground dark:bg-input/30 dark:border-input dark:hover:bg-input/50",
|
|
903
|
+
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
904
|
+
ghost: "hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50",
|
|
905
|
+
link: "text-primary underline-offset-4 hover:underline"
|
|
906
|
+
},
|
|
907
|
+
size: {
|
|
908
|
+
default: "h-9 px-4 py-2 has-[>svg]:px-3",
|
|
909
|
+
sm: "h-8 rounded-md gap-1.5 px-3 has-[>svg]:px-2.5",
|
|
910
|
+
lg: "h-10 rounded-md px-6 has-[>svg]:px-4",
|
|
911
|
+
icon: "size-9",
|
|
912
|
+
"icon-sm": "size-8",
|
|
913
|
+
"icon-lg": "size-10"
|
|
914
|
+
}
|
|
915
|
+
},
|
|
916
|
+
defaultVariants: {
|
|
917
|
+
variant: "default",
|
|
918
|
+
size: "default"
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
);
|
|
922
|
+
function Button({
|
|
923
|
+
className,
|
|
924
|
+
variant,
|
|
925
|
+
size,
|
|
926
|
+
asChild = false,
|
|
927
|
+
...props
|
|
928
|
+
}) {
|
|
929
|
+
const Comp = asChild ? Slot : "button";
|
|
930
|
+
return /* @__PURE__ */ jsx(
|
|
931
|
+
Comp,
|
|
932
|
+
{
|
|
933
|
+
"data-slot": "button",
|
|
934
|
+
className: cn(buttonVariants({ variant, size, className })),
|
|
935
|
+
...props
|
|
936
|
+
}
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
function Card({ className, ...props }) {
|
|
940
|
+
return /* @__PURE__ */ jsx(
|
|
941
|
+
"div",
|
|
942
|
+
{
|
|
943
|
+
"data-slot": "card",
|
|
944
|
+
className: cn(
|
|
945
|
+
"bg-card text-card-foreground flex flex-col gap-6 rounded-xl border py-6 shadow-sm",
|
|
946
|
+
className
|
|
947
|
+
),
|
|
948
|
+
...props
|
|
949
|
+
}
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
function CardHeader({ className, ...props }) {
|
|
953
|
+
return /* @__PURE__ */ jsx(
|
|
954
|
+
"div",
|
|
955
|
+
{
|
|
956
|
+
"data-slot": "card-header",
|
|
957
|
+
className: cn(
|
|
958
|
+
"@container/card-header grid auto-rows-min grid-rows-[auto_auto] items-start gap-2 px-6 has-data-[slot=card-action]:grid-cols-[1fr_auto] [.border-b]:pb-6",
|
|
959
|
+
className
|
|
960
|
+
),
|
|
961
|
+
...props
|
|
962
|
+
}
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
function CardTitle({ className, ...props }) {
|
|
966
|
+
return /* @__PURE__ */ jsx(
|
|
967
|
+
"div",
|
|
968
|
+
{
|
|
969
|
+
"data-slot": "card-title",
|
|
970
|
+
className: cn("leading-none font-semibold", className),
|
|
971
|
+
...props
|
|
972
|
+
}
|
|
973
|
+
);
|
|
974
|
+
}
|
|
975
|
+
function CardDescription({ className, ...props }) {
|
|
976
|
+
return /* @__PURE__ */ jsx(
|
|
977
|
+
"div",
|
|
978
|
+
{
|
|
979
|
+
"data-slot": "card-description",
|
|
980
|
+
className: cn("text-muted-foreground text-sm", className),
|
|
981
|
+
...props
|
|
982
|
+
}
|
|
983
|
+
);
|
|
984
|
+
}
|
|
985
|
+
function CardContent({ className, ...props }) {
|
|
986
|
+
return /* @__PURE__ */ jsx(
|
|
987
|
+
"div",
|
|
988
|
+
{
|
|
989
|
+
"data-slot": "card-content",
|
|
990
|
+
className: cn("px-6", className),
|
|
991
|
+
...props
|
|
992
|
+
}
|
|
993
|
+
);
|
|
994
|
+
}
|
|
995
|
+
function Tabs({
|
|
996
|
+
className,
|
|
997
|
+
...props
|
|
998
|
+
}) {
|
|
999
|
+
return /* @__PURE__ */ jsx(
|
|
1000
|
+
TabsPrimitive.Root,
|
|
1001
|
+
{
|
|
1002
|
+
"data-slot": "tabs",
|
|
1003
|
+
className: cn("flex flex-col gap-2", className),
|
|
1004
|
+
...props
|
|
1005
|
+
}
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
function TabsList({
|
|
1009
|
+
className,
|
|
1010
|
+
...props
|
|
1011
|
+
}) {
|
|
1012
|
+
return /* @__PURE__ */ jsx(
|
|
1013
|
+
TabsPrimitive.List,
|
|
1014
|
+
{
|
|
1015
|
+
"data-slot": "tabs-list",
|
|
1016
|
+
className: cn(
|
|
1017
|
+
"bg-muted text-muted-foreground inline-flex h-9 w-fit items-center justify-center rounded-lg p-[3px]",
|
|
1018
|
+
className
|
|
1019
|
+
),
|
|
1020
|
+
...props
|
|
1021
|
+
}
|
|
1022
|
+
);
|
|
1023
|
+
}
|
|
1024
|
+
function TabsTrigger({
|
|
1025
|
+
className,
|
|
1026
|
+
...props
|
|
1027
|
+
}) {
|
|
1028
|
+
return /* @__PURE__ */ jsx(
|
|
1029
|
+
TabsPrimitive.Trigger,
|
|
1030
|
+
{
|
|
1031
|
+
"data-slot": "tabs-trigger",
|
|
1032
|
+
className: cn(
|
|
1033
|
+
"data-[state=active]:bg-background dark:data-[state=active]:text-foreground focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:outline-ring dark:data-[state=active]:border-input dark:data-[state=active]:bg-input/30 text-foreground dark:text-muted-foreground inline-flex h-[calc(100%-1px)] flex-1 cursor-pointer items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-[color,box-shadow] focus-visible:ring-[3px] focus-visible:outline-1 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:shadow-sm [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
|
1034
|
+
className
|
|
1035
|
+
),
|
|
1036
|
+
...props
|
|
1037
|
+
}
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
function TabsContent({
|
|
1041
|
+
className,
|
|
1042
|
+
...props
|
|
1043
|
+
}) {
|
|
1044
|
+
return /* @__PURE__ */ jsx(
|
|
1045
|
+
TabsPrimitive.Content,
|
|
1046
|
+
{
|
|
1047
|
+
"data-slot": "tabs-content",
|
|
1048
|
+
className: cn("flex-1 outline-none", className),
|
|
1049
|
+
...props
|
|
1050
|
+
}
|
|
1051
|
+
);
|
|
1052
|
+
}
|
|
1053
|
+
function Slider({
|
|
1054
|
+
className,
|
|
1055
|
+
defaultValue,
|
|
1056
|
+
value,
|
|
1057
|
+
min = 0,
|
|
1058
|
+
max = 100,
|
|
1059
|
+
...props
|
|
1060
|
+
}) {
|
|
1061
|
+
const _values = React.useMemo(
|
|
1062
|
+
() => Array.isArray(value) ? value : Array.isArray(defaultValue) ? defaultValue : [min, max],
|
|
1063
|
+
[value, defaultValue, min, max]
|
|
1064
|
+
);
|
|
1065
|
+
return /* @__PURE__ */ jsxs(
|
|
1066
|
+
SliderPrimitive.Root,
|
|
1067
|
+
{
|
|
1068
|
+
"data-slot": "slider",
|
|
1069
|
+
defaultValue,
|
|
1070
|
+
value,
|
|
1071
|
+
min,
|
|
1072
|
+
max,
|
|
1073
|
+
className: cn(
|
|
1074
|
+
"relative flex w-full cursor-pointer touch-none items-center select-none data-disabled:opacity-50 data-[orientation=vertical]:h-full data-[orientation=vertical]:min-h-44 data-[orientation=vertical]:w-auto data-[orientation=vertical]:flex-col",
|
|
1075
|
+
className
|
|
1076
|
+
),
|
|
1077
|
+
...props,
|
|
1078
|
+
children: [
|
|
1079
|
+
/* @__PURE__ */ jsx(
|
|
1080
|
+
SliderPrimitive.Track,
|
|
1081
|
+
{
|
|
1082
|
+
"data-slot": "slider-track",
|
|
1083
|
+
className: cn(
|
|
1084
|
+
"bg-muted relative grow overflow-hidden rounded-full data-[orientation=horizontal]:h-1.5 data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-1.5"
|
|
1085
|
+
),
|
|
1086
|
+
children: /* @__PURE__ */ jsx(
|
|
1087
|
+
SliderPrimitive.Range,
|
|
1088
|
+
{
|
|
1089
|
+
"data-slot": "slider-range",
|
|
1090
|
+
className: cn(
|
|
1091
|
+
"bg-primary absolute data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full"
|
|
1092
|
+
)
|
|
1093
|
+
}
|
|
1094
|
+
)
|
|
1095
|
+
}
|
|
1096
|
+
),
|
|
1097
|
+
Array.from({ length: _values.length }, (_, index) => /* @__PURE__ */ jsx(
|
|
1098
|
+
SliderPrimitive.Thumb,
|
|
1099
|
+
{
|
|
1100
|
+
"data-slot": "slider-thumb",
|
|
1101
|
+
className: "border-primary ring-ring/50 block size-4 shrink-0 cursor-pointer rounded-full border bg-white shadow-sm transition-[color,box-shadow] hover:ring-4 focus-visible:ring-4 focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-50"
|
|
1102
|
+
},
|
|
1103
|
+
index
|
|
1104
|
+
))
|
|
1105
|
+
]
|
|
1106
|
+
}
|
|
1107
|
+
);
|
|
1108
|
+
}
|
|
1109
|
+
function VolumeControls({ className, compact }) {
|
|
1110
|
+
const {
|
|
1111
|
+
masterVolume,
|
|
1112
|
+
musicVolume,
|
|
1113
|
+
sfxVolume,
|
|
1114
|
+
setMasterVolume,
|
|
1115
|
+
setMusicVolume,
|
|
1116
|
+
setSfxVolume
|
|
1117
|
+
} = useVolumeStore();
|
|
1118
|
+
return /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col gap-3", compact && "gap-1", className), children: [
|
|
1119
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
1120
|
+
/* @__PURE__ */ jsx(Volume2, { className: "text-muted-foreground h-4 w-4" }),
|
|
1121
|
+
/* @__PURE__ */ jsx("span", { className: "text-sm font-medium", children: "Volume" })
|
|
1122
|
+
] }),
|
|
1123
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
|
|
1124
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
1125
|
+
/* @__PURE__ */ jsx("label", { className: "text-muted-foreground text-xs", children: "Master" }),
|
|
1126
|
+
/* @__PURE__ */ jsxs("span", { className: "text-muted-foreground font-mono text-xs", children: [
|
|
1127
|
+
Math.round(masterVolume * 100),
|
|
1128
|
+
"%"
|
|
1129
|
+
] })
|
|
1130
|
+
] }),
|
|
1131
|
+
/* @__PURE__ */ jsx(
|
|
1132
|
+
Slider,
|
|
1133
|
+
{
|
|
1134
|
+
value: [masterVolume * 100],
|
|
1135
|
+
onValueChange: (values) => setMasterVolume(values[0] / 100),
|
|
1136
|
+
min: 0,
|
|
1137
|
+
max: 100,
|
|
1138
|
+
step: 1,
|
|
1139
|
+
className: "w-full",
|
|
1140
|
+
"aria-label": "Master volume"
|
|
1141
|
+
}
|
|
1142
|
+
)
|
|
1143
|
+
] }),
|
|
1144
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
|
|
1145
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
1146
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
|
|
1147
|
+
/* @__PURE__ */ jsx(Music, { className: "text-muted-foreground h-3 w-3" }),
|
|
1148
|
+
/* @__PURE__ */ jsx("label", { className: "text-muted-foreground text-xs", children: "Music" })
|
|
1149
|
+
] }),
|
|
1150
|
+
/* @__PURE__ */ jsxs("span", { className: "text-muted-foreground font-mono text-xs", children: [
|
|
1151
|
+
Math.round(musicVolume * 100),
|
|
1152
|
+
"%"
|
|
1153
|
+
] })
|
|
1154
|
+
] }),
|
|
1155
|
+
/* @__PURE__ */ jsx(
|
|
1156
|
+
Slider,
|
|
1157
|
+
{
|
|
1158
|
+
value: [musicVolume * 100],
|
|
1159
|
+
onValueChange: (values) => setMusicVolume(values[0] / 100),
|
|
1160
|
+
min: 0,
|
|
1161
|
+
max: 100,
|
|
1162
|
+
step: 1,
|
|
1163
|
+
className: "w-full",
|
|
1164
|
+
"aria-label": "Music volume"
|
|
1165
|
+
}
|
|
1166
|
+
)
|
|
1167
|
+
] }),
|
|
1168
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
|
|
1169
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
1170
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
|
|
1171
|
+
/* @__PURE__ */ jsx(Headphones, { className: "text-muted-foreground h-3 w-3" }),
|
|
1172
|
+
/* @__PURE__ */ jsx("label", { className: "text-muted-foreground text-xs", children: "SFX" })
|
|
1173
|
+
] }),
|
|
1174
|
+
/* @__PURE__ */ jsxs("span", { className: "text-muted-foreground font-mono text-xs", children: [
|
|
1175
|
+
Math.round(sfxVolume * 100),
|
|
1176
|
+
"%"
|
|
1177
|
+
] })
|
|
1178
|
+
] }),
|
|
1179
|
+
/* @__PURE__ */ jsx(
|
|
1180
|
+
Slider,
|
|
1181
|
+
{
|
|
1182
|
+
value: [sfxVolume * 100],
|
|
1183
|
+
onValueChange: (values) => setSfxVolume(values[0] / 100),
|
|
1184
|
+
min: 0,
|
|
1185
|
+
max: 100,
|
|
1186
|
+
step: 1,
|
|
1187
|
+
className: "w-full",
|
|
1188
|
+
"aria-label": "SFX volume"
|
|
1189
|
+
}
|
|
1190
|
+
)
|
|
1191
|
+
] })
|
|
1192
|
+
] });
|
|
1193
|
+
}
|
|
1194
|
+
var statusCopy = {
|
|
1195
|
+
idle: "Idle",
|
|
1196
|
+
connecting: "Waiting for server\u2026",
|
|
1197
|
+
connected: "Ready for controllers",
|
|
1198
|
+
disconnected: "Disconnected",
|
|
1199
|
+
reconnecting: "Reconnecting\u2026"
|
|
1200
|
+
};
|
|
1201
|
+
var getPlayerAvatarUrl = (playerId) => {
|
|
1202
|
+
return `https://api.dicebear.com/9.x/adventurer-neutral/svg?seed=${encodeURIComponent(
|
|
1203
|
+
playerId
|
|
1204
|
+
)}`;
|
|
1205
|
+
};
|
|
1206
|
+
var AirJamOverlay = ({
|
|
1207
|
+
roomId,
|
|
1208
|
+
joinUrl,
|
|
1209
|
+
connectionStatus,
|
|
1210
|
+
players,
|
|
1211
|
+
lastError,
|
|
1212
|
+
gameState,
|
|
1213
|
+
onTogglePlayPause,
|
|
1214
|
+
isChildMode = false
|
|
1215
|
+
}) => {
|
|
1216
|
+
const [qrUrl, setQrUrl] = useState(null);
|
|
1217
|
+
const [qrError, setQrError] = useState(null);
|
|
1218
|
+
useEffect(() => {
|
|
1219
|
+
if (isChildMode) return;
|
|
1220
|
+
let mounted = true;
|
|
1221
|
+
QRCode.toDataURL(joinUrl, {
|
|
1222
|
+
margin: 1,
|
|
1223
|
+
color: {
|
|
1224
|
+
dark: "#0f172a",
|
|
1225
|
+
light: "#ffffff"
|
|
1226
|
+
},
|
|
1227
|
+
width: 320
|
|
1228
|
+
}).then((value) => {
|
|
1229
|
+
if (mounted) {
|
|
1230
|
+
setQrUrl(value);
|
|
1231
|
+
setQrError(null);
|
|
1232
|
+
}
|
|
1233
|
+
}).catch((err) => {
|
|
1234
|
+
if (mounted) {
|
|
1235
|
+
setQrError(err.message);
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1238
|
+
return () => {
|
|
1239
|
+
mounted = false;
|
|
1240
|
+
};
|
|
1241
|
+
}, [joinUrl, isChildMode]);
|
|
1242
|
+
const connectionVariant = useMemo(() => {
|
|
1243
|
+
switch (connectionStatus) {
|
|
1244
|
+
case "connected":
|
|
1245
|
+
return "default";
|
|
1246
|
+
case "connecting":
|
|
1247
|
+
case "reconnecting":
|
|
1248
|
+
return "secondary";
|
|
1249
|
+
default:
|
|
1250
|
+
return "destructive";
|
|
1251
|
+
}
|
|
1252
|
+
}, [connectionStatus]);
|
|
1253
|
+
if (isChildMode) return null;
|
|
1254
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1255
|
+
!isChildMode && /* @__PURE__ */ jsx("div", { className: "pointer-events-none absolute inset-x-0 top-0 z-99999", children: /* @__PURE__ */ jsx("div", { className: "mx-auto w-full", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between px-4 py-2", children: [
|
|
1256
|
+
/* @__PURE__ */ jsx("div", { className: "flex items-center gap-3", children: /* @__PURE__ */ jsxs("div", { children: [
|
|
1257
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs tracking-[0.18em] uppercase", children: "Room" }),
|
|
1258
|
+
/* @__PURE__ */ jsx("p", { className: "text-foreground text-lg font-semibold", children: roomId })
|
|
1259
|
+
] }) }),
|
|
1260
|
+
/* @__PURE__ */ jsxs("div", { className: "pointer-events-auto flex items-center gap-3", children: [
|
|
1261
|
+
players.length > 0 && /* @__PURE__ */ jsxs("div", { className: "flex items-center -space-x-2", children: [
|
|
1262
|
+
players.slice(0, 4).map((player) => /* @__PURE__ */ jsx(
|
|
1263
|
+
Avatar,
|
|
1264
|
+
{
|
|
1265
|
+
className: "h-8 w-8 border-2",
|
|
1266
|
+
style: {
|
|
1267
|
+
borderColor: player.color || "hsl(var(--border))"
|
|
1268
|
+
},
|
|
1269
|
+
children: /* @__PURE__ */ jsx(
|
|
1270
|
+
AvatarImage,
|
|
1271
|
+
{
|
|
1272
|
+
src: getPlayerAvatarUrl(player.id),
|
|
1273
|
+
alt: player.label
|
|
1274
|
+
}
|
|
1275
|
+
)
|
|
1276
|
+
},
|
|
1277
|
+
player.id
|
|
1278
|
+
)),
|
|
1279
|
+
players.length > 4 && /* @__PURE__ */ jsxs("div", { className: "border-background bg-muted flex h-8 w-8 items-center justify-center rounded-full border-2 text-xs font-medium", children: [
|
|
1280
|
+
"+",
|
|
1281
|
+
players.length - 4
|
|
1282
|
+
] })
|
|
1283
|
+
] }),
|
|
1284
|
+
onTogglePlayPause && /* @__PURE__ */ jsx(
|
|
1285
|
+
Button,
|
|
1286
|
+
{
|
|
1287
|
+
type: "button",
|
|
1288
|
+
variant: "ghost",
|
|
1289
|
+
size: "icon",
|
|
1290
|
+
onClick: onTogglePlayPause,
|
|
1291
|
+
"aria-label": gameState === "playing" ? "Pause" : "Play",
|
|
1292
|
+
className: "h-8 w-8",
|
|
1293
|
+
children: gameState === "playing" ? /* @__PURE__ */ jsx(Pause, { className: "h-4 w-4" }) : /* @__PURE__ */ jsx(Play, { className: "h-4 w-4" })
|
|
1294
|
+
}
|
|
1295
|
+
)
|
|
1296
|
+
] })
|
|
1297
|
+
] }) }) }),
|
|
1298
|
+
gameState === "paused" && /* @__PURE__ */ jsx("div", { className: "pointer-events-none fixed inset-0 z-99998 flex items-center justify-center p-4", children: /* @__PURE__ */ jsx("div", { className: "pointer-events-auto w-full max-w-2xl", children: /* @__PURE__ */ jsx(Card, { className: "bg-card/20 flex h-[80vh] flex-col overflow-hidden border py-0 shadow-lg backdrop-blur-sm", children: /* @__PURE__ */ jsxs(
|
|
1299
|
+
Tabs,
|
|
1300
|
+
{
|
|
1301
|
+
defaultValue: isChildMode ? "settings" : "room-code",
|
|
1302
|
+
className: "flex h-full w-full flex-col gap-0",
|
|
1303
|
+
children: [
|
|
1304
|
+
/* @__PURE__ */ jsxs(TabsList, { className: "border-border grid w-full shrink-0 grid-cols-2 rounded-none border-b bg-transparent p-0", children: [
|
|
1305
|
+
!isChildMode && /* @__PURE__ */ jsxs(
|
|
1306
|
+
TabsTrigger,
|
|
1307
|
+
{
|
|
1308
|
+
value: "room-code",
|
|
1309
|
+
className: "hover:bg-background/50 data-[state=active]:bg-background/50 flex items-center gap-2 rounded-none border-none bg-transparent",
|
|
1310
|
+
children: [
|
|
1311
|
+
/* @__PURE__ */ jsx(QrCode, { className: "h-4 w-4" }),
|
|
1312
|
+
"Room Code"
|
|
1313
|
+
]
|
|
1314
|
+
}
|
|
1315
|
+
),
|
|
1316
|
+
isChildMode && /* @__PURE__ */ jsxs(
|
|
1317
|
+
TabsTrigger,
|
|
1318
|
+
{
|
|
1319
|
+
value: "info",
|
|
1320
|
+
className: "hover:bg-background/50 data-[state=active]:bg-background/50 flex items-center gap-2 rounded-none border-none bg-transparent",
|
|
1321
|
+
children: [
|
|
1322
|
+
/* @__PURE__ */ jsx(MonitorSmartphone, { className: "h-4 w-4" }),
|
|
1323
|
+
"Platform"
|
|
1324
|
+
]
|
|
1325
|
+
}
|
|
1326
|
+
),
|
|
1327
|
+
/* @__PURE__ */ jsxs(
|
|
1328
|
+
TabsTrigger,
|
|
1329
|
+
{
|
|
1330
|
+
value: "settings",
|
|
1331
|
+
className: "hover:bg-background/50 data-[state=active]:bg-background/50 flex items-center gap-2 rounded-none border-none bg-transparent",
|
|
1332
|
+
children: [
|
|
1333
|
+
/* @__PURE__ */ jsx(Settings, { className: "h-4 w-4" }),
|
|
1334
|
+
"Settings"
|
|
1335
|
+
]
|
|
1336
|
+
}
|
|
1337
|
+
)
|
|
1338
|
+
] }),
|
|
1339
|
+
!isChildMode && /* @__PURE__ */ jsx(
|
|
1340
|
+
TabsContent,
|
|
1341
|
+
{
|
|
1342
|
+
value: "room-code",
|
|
1343
|
+
className: "mt-0 min-h-0 flex-1 overflow-y-auto",
|
|
1344
|
+
children: /* @__PURE__ */ jsxs("div", { className: "space-y-6 pb-6", children: [
|
|
1345
|
+
/* @__PURE__ */ jsxs(CardHeader, { className: "pt-6 text-center", children: [
|
|
1346
|
+
/* @__PURE__ */ jsx(CardTitle, { className: "text-4xl font-bold tracking-wider", children: roomId }),
|
|
1347
|
+
/* @__PURE__ */ jsx(
|
|
1348
|
+
Badge,
|
|
1349
|
+
{
|
|
1350
|
+
variant: connectionVariant,
|
|
1351
|
+
className: "mx-auto mt-3",
|
|
1352
|
+
children: statusCopy[connectionStatus]
|
|
1353
|
+
}
|
|
1354
|
+
)
|
|
1355
|
+
] }),
|
|
1356
|
+
/* @__PURE__ */ jsx(CardContent, { className: "space-y-6", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center space-y-4", children: [
|
|
1357
|
+
/* @__PURE__ */ jsx(CardDescription, { className: "text-xs tracking-[0.18em] uppercase", children: "Join URL" }),
|
|
1358
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-center font-mono text-xs break-all", children: joinUrl }),
|
|
1359
|
+
/* @__PURE__ */ jsx("div", { className: "border-border bg-card w-48 overflow-hidden rounded-lg border-2 shadow-md", children: qrUrl ? /* @__PURE__ */ jsx(
|
|
1360
|
+
"img",
|
|
1361
|
+
{
|
|
1362
|
+
src: qrUrl,
|
|
1363
|
+
alt: `Join room ${roomId}`,
|
|
1364
|
+
className: "w-full rounded bg-white"
|
|
1365
|
+
}
|
|
1366
|
+
) : /* @__PURE__ */ jsx("div", { className: "text-muted-foreground flex h-48 items-center justify-center text-sm", children: qrError ? `QR failed: ${qrError}` : "Generating QR code\u2026" }) }),
|
|
1367
|
+
/* @__PURE__ */ jsx("div", { className: "w-full", children: players.length === 0 ? /* @__PURE__ */ jsx("p", { className: "text-muted-foreground py-4 text-center text-sm", children: "Waiting for controllers to join\u2026" }) : /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
|
|
1368
|
+
/* @__PURE__ */ jsxs("p", { className: "text-foreground text-center text-sm font-medium", children: [
|
|
1369
|
+
"Connected Players (",
|
|
1370
|
+
players.length,
|
|
1371
|
+
")"
|
|
1372
|
+
] }),
|
|
1373
|
+
/* @__PURE__ */ jsx("ul", { className: "flex flex-wrap items-center justify-center gap-4", children: players.map((player) => /* @__PURE__ */ jsxs(
|
|
1374
|
+
"li",
|
|
1375
|
+
{
|
|
1376
|
+
className: "flex flex-col items-center gap-2",
|
|
1377
|
+
children: [
|
|
1378
|
+
/* @__PURE__ */ jsx(
|
|
1379
|
+
"img",
|
|
1380
|
+
{
|
|
1381
|
+
src: getPlayerAvatarUrl(player.id),
|
|
1382
|
+
alt: player.label,
|
|
1383
|
+
className: "bg-secondary/30 h-16 w-16 rounded-full border-4 shadow-md",
|
|
1384
|
+
style: {
|
|
1385
|
+
borderColor: player.color || "hsl(var(--border))"
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
),
|
|
1389
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-0.5", children: [
|
|
1390
|
+
/* @__PURE__ */ jsx("span", { className: "text-card-foreground max-w-[100px] truncate text-center text-sm font-medium", children: player.label }),
|
|
1391
|
+
/* @__PURE__ */ jsx("span", { className: "text-muted-foreground font-mono text-xs", children: player.id.slice(0, 8) })
|
|
1392
|
+
] })
|
|
1393
|
+
]
|
|
1394
|
+
},
|
|
1395
|
+
player.id
|
|
1396
|
+
)) })
|
|
1397
|
+
] }) }),
|
|
1398
|
+
lastError && /* @__PURE__ */ jsx(Alert, { variant: "destructive", className: "w-full", children: /* @__PURE__ */ jsx(AlertDescription, { children: lastError }) }),
|
|
1399
|
+
onTogglePlayPause && /* @__PURE__ */ jsx("div", { className: "pt-2", children: /* @__PURE__ */ jsxs(
|
|
1400
|
+
Button,
|
|
1401
|
+
{
|
|
1402
|
+
type: "button",
|
|
1403
|
+
onClick: onTogglePlayPause,
|
|
1404
|
+
size: "lg",
|
|
1405
|
+
children: [
|
|
1406
|
+
/* @__PURE__ */ jsx(Play, { className: "mr-2 h-5 w-5" }),
|
|
1407
|
+
"Start Game"
|
|
1408
|
+
]
|
|
1409
|
+
}
|
|
1410
|
+
) })
|
|
1411
|
+
] }) })
|
|
1412
|
+
] })
|
|
1413
|
+
}
|
|
1414
|
+
),
|
|
1415
|
+
isChildMode && /* @__PURE__ */ jsx(
|
|
1416
|
+
TabsContent,
|
|
1417
|
+
{
|
|
1418
|
+
value: "info",
|
|
1419
|
+
className: "mt-0 min-h-0 flex-1 overflow-y-auto",
|
|
1420
|
+
children: /* @__PURE__ */ jsxs("div", { className: "space-y-6 pb-6", children: [
|
|
1421
|
+
/* @__PURE__ */ jsxs(CardHeader, { className: "pt-6 text-center", children: [
|
|
1422
|
+
/* @__PURE__ */ jsx(CardTitle, { className: "text-2xl font-bold tracking-wider", children: "Connected to Platform" }),
|
|
1423
|
+
/* @__PURE__ */ jsx(CardDescription, { className: "mt-2", children: "This game is running inside the Air Jam Platform." })
|
|
1424
|
+
] }),
|
|
1425
|
+
/* @__PURE__ */ jsx(CardContent, { className: "space-y-6", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center space-y-4", children: [
|
|
1426
|
+
/* @__PURE__ */ jsx("div", { className: "w-full", children: /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
|
|
1427
|
+
/* @__PURE__ */ jsxs("p", { className: "text-foreground text-center text-sm font-medium", children: [
|
|
1428
|
+
"Connected Players (",
|
|
1429
|
+
players.length,
|
|
1430
|
+
")"
|
|
1431
|
+
] }),
|
|
1432
|
+
/* @__PURE__ */ jsx("ul", { className: "flex flex-wrap items-center justify-center gap-4", children: players.map((player) => /* @__PURE__ */ jsxs(
|
|
1433
|
+
"li",
|
|
1434
|
+
{
|
|
1435
|
+
className: "flex flex-col items-center gap-2",
|
|
1436
|
+
children: [
|
|
1437
|
+
/* @__PURE__ */ jsx(
|
|
1438
|
+
"img",
|
|
1439
|
+
{
|
|
1440
|
+
src: getPlayerAvatarUrl(player.id),
|
|
1441
|
+
alt: player.label,
|
|
1442
|
+
className: "bg-secondary/30 h-16 w-16 rounded-full border-4 shadow-md",
|
|
1443
|
+
style: {
|
|
1444
|
+
borderColor: player.color || "hsl(var(--border))"
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
),
|
|
1448
|
+
/* @__PURE__ */ jsx("div", { className: "flex flex-col items-center gap-0.5", children: /* @__PURE__ */ jsx("span", { className: "text-card-foreground max-w-[100px] truncate text-center text-sm font-medium", children: player.label }) })
|
|
1449
|
+
]
|
|
1450
|
+
},
|
|
1451
|
+
player.id
|
|
1452
|
+
)) })
|
|
1453
|
+
] }) }),
|
|
1454
|
+
onTogglePlayPause && /* @__PURE__ */ jsx("div", { className: "pt-2", children: /* @__PURE__ */ jsxs(
|
|
1455
|
+
Button,
|
|
1456
|
+
{
|
|
1457
|
+
type: "button",
|
|
1458
|
+
onClick: onTogglePlayPause,
|
|
1459
|
+
size: "lg",
|
|
1460
|
+
children: [
|
|
1461
|
+
/* @__PURE__ */ jsx(Play, { className: "mr-2 h-5 w-5" }),
|
|
1462
|
+
"Resume Game"
|
|
1463
|
+
]
|
|
1464
|
+
}
|
|
1465
|
+
) })
|
|
1466
|
+
] }) })
|
|
1467
|
+
] })
|
|
1468
|
+
}
|
|
1469
|
+
),
|
|
1470
|
+
/* @__PURE__ */ jsx(
|
|
1471
|
+
TabsContent,
|
|
1472
|
+
{
|
|
1473
|
+
value: "settings",
|
|
1474
|
+
className: "mt-0 min-h-0 flex-1 overflow-y-auto",
|
|
1475
|
+
children: /* @__PURE__ */ jsx(CardContent, { className: "pt-6 pb-6", children: /* @__PURE__ */ jsxs("div", { className: "space-y-6", children: [
|
|
1476
|
+
/* @__PURE__ */ jsx("div", { className: "space-y-4", children: /* @__PURE__ */ jsx(VolumeControls, {}) }),
|
|
1477
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-4 border-t pt-4", children: [
|
|
1478
|
+
/* @__PURE__ */ jsx("h3", { className: "text-sm font-semibold", children: "Connection" }),
|
|
1479
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
|
|
1480
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
1481
|
+
/* @__PURE__ */ jsx("span", { className: "text-muted-foreground text-sm", children: "Status" }),
|
|
1482
|
+
/* @__PURE__ */ jsx(Badge, { variant: connectionVariant, children: statusCopy[connectionStatus] })
|
|
1483
|
+
] }),
|
|
1484
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
1485
|
+
/* @__PURE__ */ jsx("span", { className: "text-muted-foreground text-sm", children: "Room ID" }),
|
|
1486
|
+
/* @__PURE__ */ jsx("span", { className: "font-mono text-sm", children: roomId })
|
|
1487
|
+
] }),
|
|
1488
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
1489
|
+
/* @__PURE__ */ jsx("span", { className: "text-muted-foreground text-sm", children: "Players" }),
|
|
1490
|
+
/* @__PURE__ */ jsx("span", { className: "text-sm font-medium", children: players.length })
|
|
1491
|
+
] })
|
|
1492
|
+
] })
|
|
1493
|
+
] }),
|
|
1494
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-4 border-t pt-4", children: [
|
|
1495
|
+
/* @__PURE__ */ jsx("h3", { className: "text-sm font-semibold", children: "Display" }),
|
|
1496
|
+
/* @__PURE__ */ jsx("div", { className: "space-y-2", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
1497
|
+
/* @__PURE__ */ jsx("span", { className: "text-muted-foreground text-sm", children: "Game State" }),
|
|
1498
|
+
/* @__PURE__ */ jsx(Badge, { variant: "outline", className: "capitalize", children: gameState })
|
|
1499
|
+
] }) })
|
|
1500
|
+
] })
|
|
1501
|
+
] }) })
|
|
1502
|
+
}
|
|
1503
|
+
)
|
|
1504
|
+
]
|
|
1505
|
+
}
|
|
1506
|
+
) }) }) })
|
|
1507
|
+
] });
|
|
1508
|
+
};
|
|
1509
|
+
var useFullscreen = () => {
|
|
1510
|
+
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
1511
|
+
useEffect(() => {
|
|
1512
|
+
const handleFullscreenChange = () => {
|
|
1513
|
+
const doc = document;
|
|
1514
|
+
setIsFullscreen(
|
|
1515
|
+
!!(document.fullscreenElement || doc.webkitFullscreenElement || doc.mozFullScreenElement || doc.msFullscreenElement)
|
|
1516
|
+
);
|
|
1517
|
+
};
|
|
1518
|
+
document.addEventListener("fullscreenchange", handleFullscreenChange);
|
|
1519
|
+
document.addEventListener("webkitfullscreenchange", handleFullscreenChange);
|
|
1520
|
+
document.addEventListener("mozfullscreenchange", handleFullscreenChange);
|
|
1521
|
+
document.addEventListener("msfullscreenchange", handleFullscreenChange);
|
|
1522
|
+
return () => {
|
|
1523
|
+
document.removeEventListener("fullscreenchange", handleFullscreenChange);
|
|
1524
|
+
document.removeEventListener(
|
|
1525
|
+
"webkitfullscreenchange",
|
|
1526
|
+
handleFullscreenChange
|
|
1527
|
+
);
|
|
1528
|
+
document.removeEventListener(
|
|
1529
|
+
"mozfullscreenchange",
|
|
1530
|
+
handleFullscreenChange
|
|
1531
|
+
);
|
|
1532
|
+
document.removeEventListener(
|
|
1533
|
+
"msfullscreenchange",
|
|
1534
|
+
handleFullscreenChange
|
|
1535
|
+
);
|
|
1536
|
+
};
|
|
1537
|
+
}, []);
|
|
1538
|
+
const exitFullscreen = useCallback(async () => {
|
|
1539
|
+
try {
|
|
1540
|
+
const doc = document;
|
|
1541
|
+
if (document.fullscreenElement || doc.webkitFullscreenElement || doc.mozFullScreenElement || doc.msFullscreenElement) {
|
|
1542
|
+
if (document.exitFullscreen) {
|
|
1543
|
+
await document.exitFullscreen();
|
|
1544
|
+
} else if (doc.webkitExitFullscreen) {
|
|
1545
|
+
await doc.webkitExitFullscreen();
|
|
1546
|
+
} else if (doc.mozCancelFullScreen) {
|
|
1547
|
+
await doc.mozCancelFullScreen();
|
|
1548
|
+
} else if (doc.msExitFullscreen) {
|
|
1549
|
+
await doc.msExitFullscreen();
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
} catch (error) {
|
|
1553
|
+
console.error("Error exiting fullscreen:", error);
|
|
1554
|
+
}
|
|
1555
|
+
}, []);
|
|
1556
|
+
const enterFullscreen = useCallback(async (element) => {
|
|
1557
|
+
try {
|
|
1558
|
+
const elem = element || document.documentElement;
|
|
1559
|
+
if (elem.requestFullscreen) {
|
|
1560
|
+
await elem.requestFullscreen();
|
|
1561
|
+
} else if (elem.webkitRequestFullscreen) {
|
|
1562
|
+
await elem.webkitRequestFullscreen();
|
|
1563
|
+
} else if (elem.mozRequestFullScreen) {
|
|
1564
|
+
await elem.mozRequestFullScreen();
|
|
1565
|
+
} else if (elem.msRequestFullscreen) {
|
|
1566
|
+
await elem.msRequestFullscreen();
|
|
1567
|
+
}
|
|
1568
|
+
} catch (error) {
|
|
1569
|
+
console.error("Error entering fullscreen:", error);
|
|
1570
|
+
}
|
|
1571
|
+
}, []);
|
|
1572
|
+
const toggleFullscreen = useCallback(
|
|
1573
|
+
async (element) => {
|
|
1574
|
+
if (isFullscreen) {
|
|
1575
|
+
await exitFullscreen();
|
|
1576
|
+
} else {
|
|
1577
|
+
await enterFullscreen(element);
|
|
1578
|
+
}
|
|
1579
|
+
},
|
|
1580
|
+
[isFullscreen, exitFullscreen, enterFullscreen]
|
|
1581
|
+
);
|
|
1582
|
+
return {
|
|
1583
|
+
isFullscreen,
|
|
1584
|
+
enterFullscreen,
|
|
1585
|
+
exitFullscreen,
|
|
1586
|
+
toggleFullscreen
|
|
1587
|
+
};
|
|
1588
|
+
};
|
|
1589
|
+
|
|
1590
|
+
// src/utils/mode.ts
|
|
1591
|
+
var detectRunMode = () => {
|
|
1592
|
+
if (typeof window === "undefined") {
|
|
1593
|
+
return "standalone";
|
|
1594
|
+
}
|
|
1595
|
+
try {
|
|
1596
|
+
return window.self !== window.top ? "platform" : "standalone";
|
|
1597
|
+
} catch {
|
|
1598
|
+
return "platform";
|
|
1599
|
+
}
|
|
1600
|
+
};
|
|
1601
|
+
|
|
1602
|
+
// src/utils/network-ip.ts
|
|
1603
|
+
var isVirtualNetwork = (ip) => {
|
|
1604
|
+
if (ip.startsWith("192.168.64.") || ip.startsWith("192.168.65.")) {
|
|
1605
|
+
return true;
|
|
1606
|
+
}
|
|
1607
|
+
if (ip.startsWith("172.17.")) {
|
|
1608
|
+
return true;
|
|
1609
|
+
}
|
|
1610
|
+
if (ip.startsWith("172.18.") || ip.startsWith("172.19.")) {
|
|
1611
|
+
return true;
|
|
1612
|
+
}
|
|
1613
|
+
if (ip.startsWith("169.254.")) {
|
|
1614
|
+
return true;
|
|
1615
|
+
}
|
|
1616
|
+
return false;
|
|
1617
|
+
};
|
|
1618
|
+
var scoreIp = (ip) => {
|
|
1619
|
+
if (ip.startsWith("192.168.")) {
|
|
1620
|
+
const parts = ip.split(".");
|
|
1621
|
+
const thirdOctet = parseInt(parts[2] || "0", 10);
|
|
1622
|
+
if (thirdOctet < 64) {
|
|
1623
|
+
return 100 - thirdOctet;
|
|
1624
|
+
}
|
|
1625
|
+
return 10;
|
|
1626
|
+
}
|
|
1627
|
+
if (ip.startsWith("10.")) {
|
|
1628
|
+
return 50;
|
|
1629
|
+
}
|
|
1630
|
+
if (/^172\.(1[6-9]|2[0-9]|3[01])\./.test(ip)) {
|
|
1631
|
+
return 30;
|
|
1632
|
+
}
|
|
1633
|
+
return 0;
|
|
1634
|
+
};
|
|
1635
|
+
var getLocalNetworkIp = () => {
|
|
1636
|
+
return new Promise((resolve) => {
|
|
1637
|
+
if (typeof window === "undefined" || typeof RTCPeerConnection === "undefined") {
|
|
1638
|
+
resolve(null);
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
const pc = new RTCPeerConnection({
|
|
1642
|
+
iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
|
|
1643
|
+
});
|
|
1644
|
+
const candidates = [];
|
|
1645
|
+
let candidateGatheringComplete = false;
|
|
1646
|
+
const resolveOnce = (ip) => {
|
|
1647
|
+
pc.close();
|
|
1648
|
+
resolve(ip);
|
|
1649
|
+
};
|
|
1650
|
+
const timeout = setTimeout(() => {
|
|
1651
|
+
if (candidateGatheringComplete) {
|
|
1652
|
+
pickBestCandidate();
|
|
1653
|
+
} else {
|
|
1654
|
+
setTimeout(() => {
|
|
1655
|
+
pickBestCandidate();
|
|
1656
|
+
}, 500);
|
|
1657
|
+
}
|
|
1658
|
+
}, 3e3);
|
|
1659
|
+
const pickBestCandidate = () => {
|
|
1660
|
+
if (candidates.length === 0) {
|
|
1661
|
+
resolveOnce(null);
|
|
1662
|
+
return;
|
|
1663
|
+
}
|
|
1664
|
+
candidates.sort((a, b) => b.score - a.score);
|
|
1665
|
+
const best = candidates[0];
|
|
1666
|
+
clearTimeout(timeout);
|
|
1667
|
+
resolveOnce(best.ip);
|
|
1668
|
+
};
|
|
1669
|
+
pc.onicecandidate = (event) => {
|
|
1670
|
+
if (event.candidate) {
|
|
1671
|
+
const candidate = event.candidate.candidate;
|
|
1672
|
+
const match = candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3})/);
|
|
1673
|
+
if (match) {
|
|
1674
|
+
const ip = match[1];
|
|
1675
|
+
if (ip === "127.0.0.1") {
|
|
1676
|
+
return;
|
|
1677
|
+
}
|
|
1678
|
+
const isPrivate = ip.startsWith("192.168.") || ip.startsWith("10.") || /^172\.(1[6-9]|2[0-9]|3[01])\./.test(ip);
|
|
1679
|
+
if (isPrivate && !isVirtualNetwork(ip)) {
|
|
1680
|
+
const score = scoreIp(ip);
|
|
1681
|
+
candidates.push({ ip, score });
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
} else {
|
|
1685
|
+
candidateGatheringComplete = true;
|
|
1686
|
+
setTimeout(() => {
|
|
1687
|
+
pickBestCandidate();
|
|
1688
|
+
}, 200);
|
|
1689
|
+
}
|
|
1690
|
+
};
|
|
1691
|
+
pc.createDataChannel("");
|
|
1692
|
+
pc.createOffer().then((offer) => pc.setLocalDescription(offer)).catch(() => {
|
|
1693
|
+
clearTimeout(timeout);
|
|
1694
|
+
resolveOnce(null);
|
|
1695
|
+
});
|
|
1696
|
+
});
|
|
1697
|
+
};
|
|
1698
|
+
|
|
1699
|
+
// src/utils/url-builder.ts
|
|
1700
|
+
var UrlBuilder = class {
|
|
1701
|
+
cachedLocalIp = null;
|
|
1702
|
+
ipDetectionPromise = null;
|
|
1703
|
+
/**
|
|
1704
|
+
* Get the local network IP address with caching.
|
|
1705
|
+
* First call triggers detection, subsequent calls return cached value.
|
|
1706
|
+
*/
|
|
1707
|
+
async getLocalIp() {
|
|
1708
|
+
if (this.cachedLocalIp !== null) {
|
|
1709
|
+
return this.cachedLocalIp;
|
|
1710
|
+
}
|
|
1711
|
+
if (this.ipDetectionPromise !== null) {
|
|
1712
|
+
return this.ipDetectionPromise;
|
|
1713
|
+
}
|
|
1714
|
+
this.ipDetectionPromise = getLocalNetworkIp().then((ip) => {
|
|
1715
|
+
this.cachedLocalIp = ip;
|
|
1716
|
+
this.ipDetectionPromise = null;
|
|
1717
|
+
return ip;
|
|
1718
|
+
});
|
|
1719
|
+
return this.ipDetectionPromise;
|
|
1720
|
+
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Clear the cached IP address.
|
|
1723
|
+
* Useful for testing or when network changes are detected.
|
|
1724
|
+
*/
|
|
1725
|
+
clearCache() {
|
|
1726
|
+
this.cachedLocalIp = null;
|
|
1727
|
+
this.ipDetectionPromise = null;
|
|
1728
|
+
}
|
|
1729
|
+
/**
|
|
1730
|
+
* Normalize a URL for mobile access.
|
|
1731
|
+
* Replaces localhost with the local network IP.
|
|
1732
|
+
*/
|
|
1733
|
+
async normalizeForMobile(url) {
|
|
1734
|
+
if (typeof window === "undefined") return url;
|
|
1735
|
+
try {
|
|
1736
|
+
const urlObj = new URL(url);
|
|
1737
|
+
if (urlObj.hostname === "localhost" || urlObj.hostname === "127.0.0.1") {
|
|
1738
|
+
const localIp = await this.getLocalIp();
|
|
1739
|
+
if (localIp) {
|
|
1740
|
+
urlObj.hostname = localIp;
|
|
1741
|
+
return urlObj.toString();
|
|
1742
|
+
}
|
|
1743
|
+
urlObj.hostname = window.location.hostname;
|
|
1744
|
+
return urlObj.toString();
|
|
1745
|
+
}
|
|
1746
|
+
return url;
|
|
1747
|
+
} catch {
|
|
1748
|
+
return url;
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
/**
|
|
1752
|
+
* Build a controller URL with room code.
|
|
1753
|
+
* Automatically uses local IP when running on localhost.
|
|
1754
|
+
*/
|
|
1755
|
+
async buildControllerUrl(roomId, options) {
|
|
1756
|
+
const path = options?.path || DEFAULT_CONTROLLER_PATH;
|
|
1757
|
+
if (options?.host) {
|
|
1758
|
+
const url2 = new URL(path, this.normalizeOrigin(options.host));
|
|
1759
|
+
url2.searchParams.set("room", roomId);
|
|
1760
|
+
return url2.toString();
|
|
1761
|
+
}
|
|
1762
|
+
let base;
|
|
1763
|
+
if (typeof window === "undefined") {
|
|
1764
|
+
base = `http://localhost:${DEFAULT_SERVER_PORT}`;
|
|
1765
|
+
} else {
|
|
1766
|
+
const currentUrl = new URL(window.location.href);
|
|
1767
|
+
if (!this.isLocalhost(currentUrl.hostname)) {
|
|
1768
|
+
base = currentUrl.origin;
|
|
1769
|
+
} else {
|
|
1770
|
+
const localIp = await this.getLocalIp();
|
|
1771
|
+
if (localIp) {
|
|
1772
|
+
const port = currentUrl.port;
|
|
1773
|
+
base = port ? `http://${localIp}:${port}` : `http://${localIp}`;
|
|
1774
|
+
} else {
|
|
1775
|
+
base = currentUrl.origin;
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
const url = new URL(path, base);
|
|
1780
|
+
url.searchParams.set("room", roomId);
|
|
1781
|
+
return url.toString();
|
|
1782
|
+
}
|
|
1783
|
+
/**
|
|
1784
|
+
* Resolve the server URL.
|
|
1785
|
+
* Uses explicit URL if provided, otherwise infers from window location.
|
|
1786
|
+
*/
|
|
1787
|
+
resolveServerUrl(explicit) {
|
|
1788
|
+
if (explicit) {
|
|
1789
|
+
return this.normalizeOrigin(explicit);
|
|
1790
|
+
}
|
|
1791
|
+
if (typeof window !== "undefined") {
|
|
1792
|
+
const url = new URL(window.location.href);
|
|
1793
|
+
url.port = String(DEFAULT_SERVER_PORT);
|
|
1794
|
+
return url.origin;
|
|
1795
|
+
}
|
|
1796
|
+
return `http://localhost:${DEFAULT_SERVER_PORT}`;
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* Check if a hostname is localhost.
|
|
1800
|
+
*/
|
|
1801
|
+
isLocalhost(hostname) {
|
|
1802
|
+
return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]";
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Normalize an origin string to ensure it has a protocol.
|
|
1806
|
+
*/
|
|
1807
|
+
normalizeOrigin(origin) {
|
|
1808
|
+
if (!origin.includes("://")) {
|
|
1809
|
+
return `http://${origin}`;
|
|
1810
|
+
}
|
|
1811
|
+
return origin;
|
|
1812
|
+
}
|
|
1813
|
+
};
|
|
1814
|
+
var urlBuilder = new UrlBuilder();
|
|
1815
|
+
var extractRoomCode = (text) => {
|
|
1816
|
+
try {
|
|
1817
|
+
const url = new URL(text);
|
|
1818
|
+
const roomParam = url.searchParams.get("room");
|
|
1819
|
+
if (roomParam) {
|
|
1820
|
+
const parsed2 = roomCodeSchema.safeParse(roomParam.toUpperCase());
|
|
1821
|
+
if (parsed2.success) {
|
|
1822
|
+
return parsed2.data;
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
} catch {
|
|
1826
|
+
}
|
|
1827
|
+
const parsed = roomCodeSchema.safeParse(text.trim().toUpperCase());
|
|
1828
|
+
if (parsed.success) {
|
|
1829
|
+
return parsed.data;
|
|
1830
|
+
}
|
|
1831
|
+
return null;
|
|
1832
|
+
};
|
|
1833
|
+
var QRScannerDialog = ({
|
|
1834
|
+
isOpen,
|
|
1835
|
+
onClose,
|
|
1836
|
+
onScan,
|
|
1837
|
+
currentRoomId
|
|
1838
|
+
}) => {
|
|
1839
|
+
const [code, setCode] = useState("");
|
|
1840
|
+
const [isCameraAvailable, setIsCameraAvailable] = useState(false);
|
|
1841
|
+
const [joinUrl, setJoinUrl] = useState("");
|
|
1842
|
+
const [copied, setCopied] = useState(false);
|
|
1843
|
+
const scannerRef = useRef(null);
|
|
1844
|
+
const inputRef = useRef(null);
|
|
1845
|
+
const prevIsOpenRef = useRef(false);
|
|
1846
|
+
useEffect(() => {
|
|
1847
|
+
if (isOpen && !prevIsOpenRef.current) {
|
|
1848
|
+
const initialCode = currentRoomId || "";
|
|
1849
|
+
setTimeout(() => {
|
|
1850
|
+
setIsCameraAvailable(false);
|
|
1851
|
+
setCode(initialCode);
|
|
1852
|
+
inputRef.current?.focus();
|
|
1853
|
+
inputRef.current?.select();
|
|
1854
|
+
}, 0);
|
|
1855
|
+
}
|
|
1856
|
+
prevIsOpenRef.current = isOpen;
|
|
1857
|
+
}, [isOpen, currentRoomId]);
|
|
1858
|
+
useEffect(() => {
|
|
1859
|
+
const buildUrl = async () => {
|
|
1860
|
+
const trimmed = code.trim().toUpperCase();
|
|
1861
|
+
const parsed = roomCodeSchema.safeParse(trimmed);
|
|
1862
|
+
if (parsed.success) {
|
|
1863
|
+
try {
|
|
1864
|
+
const url = await urlBuilder.buildControllerUrl(parsed.data);
|
|
1865
|
+
setJoinUrl(url);
|
|
1866
|
+
} catch {
|
|
1867
|
+
setJoinUrl("");
|
|
1868
|
+
}
|
|
1869
|
+
} else {
|
|
1870
|
+
setJoinUrl("");
|
|
1871
|
+
}
|
|
1872
|
+
};
|
|
1873
|
+
buildUrl();
|
|
1874
|
+
}, [code]);
|
|
1875
|
+
useEffect(() => {
|
|
1876
|
+
if (!isOpen) {
|
|
1877
|
+
setTimeout(() => {
|
|
1878
|
+
setIsCameraAvailable(false);
|
|
1879
|
+
}, 0);
|
|
1880
|
+
if (scannerRef.current) {
|
|
1881
|
+
const scanner = scannerRef.current;
|
|
1882
|
+
scanner.stop().catch(() => {
|
|
1883
|
+
}).finally(() => {
|
|
1884
|
+
try {
|
|
1885
|
+
scanner.clear();
|
|
1886
|
+
} catch {
|
|
1887
|
+
}
|
|
1888
|
+
});
|
|
1889
|
+
scannerRef.current = null;
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
}, [isOpen]);
|
|
1893
|
+
useEffect(() => {
|
|
1894
|
+
if (!isOpen) {
|
|
1895
|
+
return;
|
|
1896
|
+
}
|
|
1897
|
+
const containerId = "qr-reader";
|
|
1898
|
+
let scanner = null;
|
|
1899
|
+
let scannerRunning = false;
|
|
1900
|
+
let mounted = true;
|
|
1901
|
+
const startScanning = async () => {
|
|
1902
|
+
try {
|
|
1903
|
+
const container = document.getElementById(containerId);
|
|
1904
|
+
if (!container) {
|
|
1905
|
+
return;
|
|
1906
|
+
}
|
|
1907
|
+
const rect = container.getBoundingClientRect();
|
|
1908
|
+
if (rect.width === 0 || rect.height === 0) {
|
|
1909
|
+
return;
|
|
1910
|
+
}
|
|
1911
|
+
scanner = new Html5Qrcode(containerId);
|
|
1912
|
+
scannerRef.current = scanner;
|
|
1913
|
+
let cameraId = null;
|
|
1914
|
+
try {
|
|
1915
|
+
const devices = await Html5Qrcode.getCameras();
|
|
1916
|
+
if (devices && devices.length > 0) {
|
|
1917
|
+
const backCamera = devices.find(
|
|
1918
|
+
(d) => d.label.toLowerCase().includes("back") || d.label.toLowerCase().includes("rear")
|
|
1919
|
+
);
|
|
1920
|
+
const envCamera = devices.find((d) => {
|
|
1921
|
+
const label = d.label.toLowerCase();
|
|
1922
|
+
return label.includes("environment") || label.includes("facing back");
|
|
1923
|
+
});
|
|
1924
|
+
cameraId = (envCamera || backCamera || devices[0])?.id || null;
|
|
1925
|
+
}
|
|
1926
|
+
} catch {
|
|
1927
|
+
}
|
|
1928
|
+
const config = cameraId ? { deviceId: { exact: cameraId } } : { facingMode: "environment" };
|
|
1929
|
+
await scanner.start(
|
|
1930
|
+
config,
|
|
1931
|
+
{
|
|
1932
|
+
fps: 10,
|
|
1933
|
+
qrbox: (viewfinderWidth, viewfinderHeight) => {
|
|
1934
|
+
const minEdge = Math.min(viewfinderWidth, viewfinderHeight);
|
|
1935
|
+
const qrboxSize = Math.min(250, minEdge * 0.8);
|
|
1936
|
+
return {
|
|
1937
|
+
width: qrboxSize,
|
|
1938
|
+
height: qrboxSize
|
|
1939
|
+
};
|
|
1940
|
+
},
|
|
1941
|
+
aspectRatio: 1
|
|
1942
|
+
},
|
|
1943
|
+
(decodedText) => {
|
|
1944
|
+
if (!mounted) return;
|
|
1945
|
+
const roomCode = extractRoomCode(decodedText);
|
|
1946
|
+
if (roomCode) {
|
|
1947
|
+
setCode(roomCode);
|
|
1948
|
+
scannerRunning = false;
|
|
1949
|
+
scanner?.stop().catch(() => {
|
|
1950
|
+
}).finally(() => {
|
|
1951
|
+
if (mounted) {
|
|
1952
|
+
setIsCameraAvailable(false);
|
|
1953
|
+
}
|
|
1954
|
+
});
|
|
1955
|
+
}
|
|
1956
|
+
},
|
|
1957
|
+
() => {
|
|
1958
|
+
}
|
|
1959
|
+
);
|
|
1960
|
+
scannerRunning = true;
|
|
1961
|
+
if (mounted) {
|
|
1962
|
+
setIsCameraAvailable(true);
|
|
1963
|
+
}
|
|
1964
|
+
} catch {
|
|
1965
|
+
if (mounted) {
|
|
1966
|
+
setIsCameraAvailable(false);
|
|
1967
|
+
}
|
|
1968
|
+
if (scanner) {
|
|
1969
|
+
try {
|
|
1970
|
+
scanner.clear();
|
|
1971
|
+
} catch {
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
scannerRef.current = null;
|
|
1975
|
+
}
|
|
1976
|
+
};
|
|
1977
|
+
const timeoutId = setTimeout(() => {
|
|
1978
|
+
startScanning();
|
|
1979
|
+
}, 200);
|
|
1980
|
+
return () => {
|
|
1981
|
+
mounted = false;
|
|
1982
|
+
clearTimeout(timeoutId);
|
|
1983
|
+
if (scanner && scannerRunning) {
|
|
1984
|
+
scanner.stop().catch(() => {
|
|
1985
|
+
}).finally(() => {
|
|
1986
|
+
try {
|
|
1987
|
+
scanner?.clear();
|
|
1988
|
+
} catch {
|
|
1989
|
+
}
|
|
1990
|
+
});
|
|
1991
|
+
} else if (scanner) {
|
|
1992
|
+
try {
|
|
1993
|
+
scanner.clear();
|
|
1994
|
+
} catch {
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
scannerRef.current = null;
|
|
1998
|
+
};
|
|
1999
|
+
}, [isOpen]);
|
|
2000
|
+
const handleSubmit = () => {
|
|
2001
|
+
const trimmed = code.trim().toUpperCase();
|
|
2002
|
+
const parsed = roomCodeSchema.safeParse(trimmed);
|
|
2003
|
+
if (parsed.success) {
|
|
2004
|
+
onScan(parsed.data);
|
|
2005
|
+
onClose();
|
|
2006
|
+
}
|
|
2007
|
+
};
|
|
2008
|
+
const handleInputFocus = (e) => {
|
|
2009
|
+
e.target.select();
|
|
2010
|
+
};
|
|
2011
|
+
const handleInputChange = (e) => {
|
|
2012
|
+
setCode(e.target.value.toUpperCase());
|
|
2013
|
+
setCopied(false);
|
|
2014
|
+
};
|
|
2015
|
+
const handleCopyUrl = async () => {
|
|
2016
|
+
if (!joinUrl) return;
|
|
2017
|
+
try {
|
|
2018
|
+
await navigator.clipboard.writeText(joinUrl);
|
|
2019
|
+
setCopied(true);
|
|
2020
|
+
setTimeout(() => setCopied(false), 2e3);
|
|
2021
|
+
} catch {
|
|
2022
|
+
const textArea = document.createElement("textarea");
|
|
2023
|
+
textArea.value = joinUrl;
|
|
2024
|
+
textArea.style.position = "fixed";
|
|
2025
|
+
textArea.style.opacity = "0";
|
|
2026
|
+
document.body.appendChild(textArea);
|
|
2027
|
+
textArea.select();
|
|
2028
|
+
try {
|
|
2029
|
+
document.execCommand("copy");
|
|
2030
|
+
setCopied(true);
|
|
2031
|
+
setTimeout(() => setCopied(false), 2e3);
|
|
2032
|
+
} catch {
|
|
2033
|
+
}
|
|
2034
|
+
document.body.removeChild(textArea);
|
|
2035
|
+
}
|
|
2036
|
+
};
|
|
2037
|
+
if (!isOpen) {
|
|
2038
|
+
return null;
|
|
2039
|
+
}
|
|
2040
|
+
const isValid = code.trim().length > 0;
|
|
2041
|
+
return /* @__PURE__ */ jsxs("div", { className: "bg-background fixed inset-0 z-50 flex flex-col items-center justify-center", children: [
|
|
2042
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col items-center justify-center", children: [
|
|
2043
|
+
isCameraAvailable && /* @__PURE__ */ jsx("div", { className: "relative flex aspect-square w-full items-center justify-center overflow-hidden p-6", children: /* @__PURE__ */ jsx(
|
|
2044
|
+
"div",
|
|
2045
|
+
{
|
|
2046
|
+
id: "qr-reader",
|
|
2047
|
+
className: "h-full w-full overflow-hidden rounded-lg bg-black"
|
|
2048
|
+
}
|
|
2049
|
+
) }),
|
|
2050
|
+
/* @__PURE__ */ jsx("div", { className: "w-full max-w-md px-6", children: /* @__PURE__ */ jsx(
|
|
2051
|
+
"input",
|
|
2052
|
+
{
|
|
2053
|
+
ref: inputRef,
|
|
2054
|
+
type: "text",
|
|
2055
|
+
value: code,
|
|
2056
|
+
onChange: handleInputChange,
|
|
2057
|
+
onFocus: handleInputFocus,
|
|
2058
|
+
onKeyDown: (e) => {
|
|
2059
|
+
if (e.key === "Enter" && isValid) {
|
|
2060
|
+
handleSubmit();
|
|
2061
|
+
}
|
|
2062
|
+
},
|
|
2063
|
+
placeholder: "Enter room code",
|
|
2064
|
+
className: "border-border bg-background focus:ring-primary focus:border-primary w-full rounded-lg border-2 px-6 py-4 text-center font-mono text-2xl focus:ring-2 focus:outline-none",
|
|
2065
|
+
autoComplete: "off",
|
|
2066
|
+
autoCapitalize: "characters"
|
|
2067
|
+
}
|
|
2068
|
+
) }),
|
|
2069
|
+
joinUrl && /* @__PURE__ */ jsx("div", { className: "mt-4 w-full max-w-md px-6 pb-2", children: /* @__PURE__ */ jsx(
|
|
2070
|
+
"button",
|
|
2071
|
+
{
|
|
2072
|
+
type: "button",
|
|
2073
|
+
onClick: handleCopyUrl,
|
|
2074
|
+
className: "text-muted-foreground hover:text-foreground bg-muted/50 hover:bg-muted w-full rounded-lg px-4 py-3 text-center font-mono text-xs break-all transition-colors",
|
|
2075
|
+
children: copied ? "\u2713 Copied!" : joinUrl
|
|
2076
|
+
}
|
|
2077
|
+
) })
|
|
2078
|
+
] }),
|
|
2079
|
+
/* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-md gap-4 px-6 pb-6", children: [
|
|
2080
|
+
/* @__PURE__ */ jsx(
|
|
2081
|
+
Button,
|
|
2082
|
+
{
|
|
2083
|
+
type: "button",
|
|
2084
|
+
variant: "outline",
|
|
2085
|
+
onClick: onClose,
|
|
2086
|
+
className: "flex-1 py-6",
|
|
2087
|
+
children: "Cancel"
|
|
2088
|
+
}
|
|
2089
|
+
),
|
|
2090
|
+
/* @__PURE__ */ jsx(
|
|
2091
|
+
Button,
|
|
2092
|
+
{
|
|
2093
|
+
type: "button",
|
|
2094
|
+
onClick: handleSubmit,
|
|
2095
|
+
disabled: !isValid,
|
|
2096
|
+
className: "flex-1 py-6",
|
|
2097
|
+
children: "Connect"
|
|
2098
|
+
}
|
|
2099
|
+
)
|
|
2100
|
+
] })
|
|
2101
|
+
] });
|
|
2102
|
+
};
|
|
2103
|
+
var getPlayerAvatarUrl2 = (playerId) => {
|
|
2104
|
+
return `https://api.dicebear.com/9.x/adventurer-neutral/svg?seed=${encodeURIComponent(
|
|
2105
|
+
playerId
|
|
2106
|
+
)}`;
|
|
2107
|
+
};
|
|
2108
|
+
var describeStatus = (status) => {
|
|
2109
|
+
switch (status) {
|
|
2110
|
+
case "connected":
|
|
2111
|
+
return "Connected to host";
|
|
2112
|
+
case "connecting":
|
|
2113
|
+
return "Connecting\u2026";
|
|
2114
|
+
case "reconnecting":
|
|
2115
|
+
return "Trying to reconnect\u2026";
|
|
2116
|
+
case "disconnected":
|
|
2117
|
+
return "Disconnected";
|
|
2118
|
+
case "idle":
|
|
2119
|
+
default:
|
|
2120
|
+
return "Idle";
|
|
2121
|
+
}
|
|
2122
|
+
};
|
|
2123
|
+
var orientationMatches = (required) => {
|
|
2124
|
+
if (required === "any") return true;
|
|
2125
|
+
if (typeof window === "undefined" || !window.matchMedia) return true;
|
|
2126
|
+
const matcher = window.matchMedia(`(orientation: ${required})`);
|
|
2127
|
+
return matcher.matches;
|
|
2128
|
+
};
|
|
2129
|
+
var ControllerShell = ({
|
|
2130
|
+
roomId,
|
|
2131
|
+
connectionStatus,
|
|
2132
|
+
requiredOrientation = "landscape",
|
|
2133
|
+
children,
|
|
2134
|
+
gameState = "paused",
|
|
2135
|
+
onTogglePlayPause,
|
|
2136
|
+
onReconnect,
|
|
2137
|
+
onRefresh,
|
|
2138
|
+
customActions
|
|
2139
|
+
}) => {
|
|
2140
|
+
const isChildMode = useMemo(() => detectRunMode() === "platform", []);
|
|
2141
|
+
const [isOrientationOk, setOrientationOk] = useState(
|
|
2142
|
+
() => orientationMatches(requiredOrientation)
|
|
2143
|
+
);
|
|
2144
|
+
const [isScannerOpen, setIsScannerOpen] = useState(false);
|
|
2145
|
+
const { isFullscreen, toggleFullscreen } = useFullscreen();
|
|
2146
|
+
const { store } = useAirJamContext();
|
|
2147
|
+
const { controllerId, players } = useStore(
|
|
2148
|
+
store,
|
|
2149
|
+
useShallow(
|
|
2150
|
+
(state) => ({
|
|
2151
|
+
controllerId: state.controllerId,
|
|
2152
|
+
players: state.players
|
|
2153
|
+
})
|
|
2154
|
+
)
|
|
2155
|
+
);
|
|
2156
|
+
const currentPlayer = useMemo(() => {
|
|
2157
|
+
if (!controllerId) return null;
|
|
2158
|
+
const found = players.find((p) => p.id === controllerId);
|
|
2159
|
+
if (!found && connectionStatus === "connected" && players.length > 0) ;
|
|
2160
|
+
return found || null;
|
|
2161
|
+
}, [controllerId, players, connectionStatus]);
|
|
2162
|
+
useEffect(() => {
|
|
2163
|
+
if (requiredOrientation === "any") {
|
|
2164
|
+
return;
|
|
2165
|
+
}
|
|
2166
|
+
const media = window.matchMedia(`(orientation: ${requiredOrientation})`);
|
|
2167
|
+
const handleChange = () => setOrientationOk(media.matches);
|
|
2168
|
+
handleChange();
|
|
2169
|
+
media.addEventListener("change", handleChange);
|
|
2170
|
+
return () => media.removeEventListener("change", handleChange);
|
|
2171
|
+
}, [requiredOrientation]);
|
|
2172
|
+
const orientationOk = requiredOrientation === "any" ? true : isOrientationOk;
|
|
2173
|
+
const statusDotColor = useMemo(() => {
|
|
2174
|
+
switch (connectionStatus) {
|
|
2175
|
+
case "connected":
|
|
2176
|
+
return "bg-green-500";
|
|
2177
|
+
case "connecting":
|
|
2178
|
+
case "reconnecting":
|
|
2179
|
+
return "bg-yellow-500";
|
|
2180
|
+
case "disconnected":
|
|
2181
|
+
default:
|
|
2182
|
+
return "bg-red-500";
|
|
2183
|
+
}
|
|
2184
|
+
}, [connectionStatus]);
|
|
2185
|
+
if (isChildMode) {
|
|
2186
|
+
return /* @__PURE__ */ jsx("div", { className: "text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden bg-transparent select-none", children: /* @__PURE__ */ jsxs("main", { className: "relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4", children: [
|
|
2187
|
+
!orientationOk && /* @__PURE__ */ jsxs("div", { className: "bg-background/95 absolute inset-0 z-50 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm", children: [
|
|
2188
|
+
/* @__PURE__ */ jsx("p", { className: "text-card-foreground text-xl font-semibold", children: "Rotate your device" }),
|
|
2189
|
+
/* @__PURE__ */ jsxs("p", { className: "text-muted-foreground mt-2 text-sm", children: [
|
|
2190
|
+
"This game is best experienced in ",
|
|
2191
|
+
requiredOrientation,
|
|
2192
|
+
" ",
|
|
2193
|
+
"orientation."
|
|
2194
|
+
] })
|
|
2195
|
+
] }),
|
|
2196
|
+
/* @__PURE__ */ jsx(
|
|
2197
|
+
"div",
|
|
2198
|
+
{
|
|
2199
|
+
className: cn(
|
|
2200
|
+
"h-full w-full select-none",
|
|
2201
|
+
!orientationOk && "pointer-events-none opacity-30"
|
|
2202
|
+
),
|
|
2203
|
+
children
|
|
2204
|
+
}
|
|
2205
|
+
)
|
|
2206
|
+
] }) });
|
|
2207
|
+
}
|
|
2208
|
+
return /* @__PURE__ */ jsxs("div", { className: "bg-background text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden select-none", children: [
|
|
2209
|
+
/* @__PURE__ */ jsxs("header", { className: "pointer-events-none sticky top-0 z-50 flex items-center justify-between border-b px-6 py-2", children: [
|
|
2210
|
+
/* @__PURE__ */ jsxs("div", { className: "pointer-events-auto flex items-center gap-3", children: [
|
|
2211
|
+
typeof window !== "undefined" && (currentPlayer || controllerId) && /* @__PURE__ */ jsxs("div", { className: "relative", title: describeStatus(connectionStatus), children: [
|
|
2212
|
+
/* @__PURE__ */ jsx(
|
|
2213
|
+
Avatar,
|
|
2214
|
+
{
|
|
2215
|
+
className: "h-8 w-8 border-2",
|
|
2216
|
+
style: {
|
|
2217
|
+
borderColor: currentPlayer?.color || "hsl(var(--border))"
|
|
2218
|
+
},
|
|
2219
|
+
children: /* @__PURE__ */ jsx(
|
|
2220
|
+
AvatarImage,
|
|
2221
|
+
{
|
|
2222
|
+
src: getPlayerAvatarUrl2(
|
|
2223
|
+
currentPlayer?.id || controllerId || ""
|
|
2224
|
+
),
|
|
2225
|
+
alt: currentPlayer?.label || "Player"
|
|
2226
|
+
}
|
|
2227
|
+
)
|
|
2228
|
+
}
|
|
2229
|
+
),
|
|
2230
|
+
/* @__PURE__ */ jsx(
|
|
2231
|
+
"span",
|
|
2232
|
+
{
|
|
2233
|
+
className: "absolute right-0 bottom-0 h-3 w-3 rounded-full bg-black",
|
|
2234
|
+
"aria-hidden": "true"
|
|
2235
|
+
}
|
|
2236
|
+
),
|
|
2237
|
+
/* @__PURE__ */ jsx(
|
|
2238
|
+
"span",
|
|
2239
|
+
{
|
|
2240
|
+
className: cn(
|
|
2241
|
+
"absolute right-0.5 bottom-0.5 h-2 w-2 rounded-full",
|
|
2242
|
+
statusDotColor,
|
|
2243
|
+
connectionStatus === "connected" && "animate-pulse"
|
|
2244
|
+
),
|
|
2245
|
+
"aria-label": describeStatus(connectionStatus)
|
|
2246
|
+
}
|
|
2247
|
+
)
|
|
2248
|
+
] }),
|
|
2249
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
2250
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs tracking-[0.24em] uppercase", children: "Room" }),
|
|
2251
|
+
/* @__PURE__ */ jsx("p", { className: "text-foreground text-lg font-semibold", children: typeof window !== "undefined" ? roomId ?? "N/A" : "N/A" })
|
|
2252
|
+
] })
|
|
2253
|
+
] }),
|
|
2254
|
+
/* @__PURE__ */ jsxs("div", { className: "pointer-events-auto flex items-center gap-3", children: [
|
|
2255
|
+
customActions,
|
|
2256
|
+
onRefresh && roomId && /* @__PURE__ */ jsx(
|
|
2257
|
+
Button,
|
|
2258
|
+
{
|
|
2259
|
+
type: "button",
|
|
2260
|
+
variant: "outline",
|
|
2261
|
+
size: "icon",
|
|
2262
|
+
onClick: onRefresh,
|
|
2263
|
+
disabled: connectionStatus === "connecting" || connectionStatus === "reconnecting",
|
|
2264
|
+
"aria-label": "Reconnect",
|
|
2265
|
+
title: "Reconnect to room",
|
|
2266
|
+
children: /* @__PURE__ */ jsx(
|
|
2267
|
+
RefreshCw,
|
|
2268
|
+
{
|
|
2269
|
+
className: cn(
|
|
2270
|
+
"h-5 w-5",
|
|
2271
|
+
(connectionStatus === "connecting" || connectionStatus === "reconnecting") && "animate-spin"
|
|
2272
|
+
)
|
|
2273
|
+
}
|
|
2274
|
+
)
|
|
2275
|
+
}
|
|
2276
|
+
),
|
|
2277
|
+
onReconnect && /* @__PURE__ */ jsx(
|
|
2278
|
+
Button,
|
|
2279
|
+
{
|
|
2280
|
+
type: "button",
|
|
2281
|
+
variant: "outline",
|
|
2282
|
+
size: "icon",
|
|
2283
|
+
onClick: () => setIsScannerOpen(true),
|
|
2284
|
+
"aria-label": "Scan QR code to reconnect",
|
|
2285
|
+
title: "Scan QR code to reconnect",
|
|
2286
|
+
children: /* @__PURE__ */ jsx(QrCode, { className: "h-5 w-5" })
|
|
2287
|
+
}
|
|
2288
|
+
),
|
|
2289
|
+
onTogglePlayPause && /* @__PURE__ */ jsx(
|
|
2290
|
+
Button,
|
|
2291
|
+
{
|
|
2292
|
+
type: "button",
|
|
2293
|
+
variant: "outline",
|
|
2294
|
+
size: "icon",
|
|
2295
|
+
onClick: onTogglePlayPause,
|
|
2296
|
+
"aria-label": gameState === "playing" ? "Pause" : "Play",
|
|
2297
|
+
title: gameState === "playing" ? "Pause" : "Play",
|
|
2298
|
+
children: gameState === "playing" ? /* @__PURE__ */ jsx(Pause, { className: "h-5 w-5" }) : /* @__PURE__ */ jsx(Play, { className: "h-5 w-5" })
|
|
2299
|
+
}
|
|
2300
|
+
),
|
|
2301
|
+
/* @__PURE__ */ jsx(
|
|
2302
|
+
Button,
|
|
2303
|
+
{
|
|
2304
|
+
type: "button",
|
|
2305
|
+
variant: "outline",
|
|
2306
|
+
size: "icon",
|
|
2307
|
+
onClick: () => toggleFullscreen(),
|
|
2308
|
+
"aria-label": isFullscreen ? "Exit fullscreen" : "Enter fullscreen",
|
|
2309
|
+
title: isFullscreen ? "Exit fullscreen" : "Enter fullscreen",
|
|
2310
|
+
children: isFullscreen ? /* @__PURE__ */ jsx(Minimize, { className: "h-5 w-5" }) : /* @__PURE__ */ jsx(Maximize, { className: "h-5 w-5" })
|
|
2311
|
+
}
|
|
2312
|
+
)
|
|
2313
|
+
] })
|
|
2314
|
+
] }),
|
|
2315
|
+
/* @__PURE__ */ jsx("div", { className: "pointer-events-none fixed right-4 bottom-4 z-50", children: /* @__PURE__ */ jsx("div", { className: "pointer-events-auto", children: /* @__PURE__ */ jsx(VolumeControls, { compact: true }) }) }),
|
|
2316
|
+
/* @__PURE__ */ jsx("main", { className: "relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4", children: /* @__PURE__ */ jsxs("div", { className: "bg-card/95 absolute inset-0 z-10 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm", children: [
|
|
2317
|
+
!orientationOk && /* @__PURE__ */ jsxs("div", { className: "bg-background/95 absolute inset-0 z-10 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm", children: [
|
|
2318
|
+
/* @__PURE__ */ jsx("p", { className: "text-card-foreground text-xl font-semibold", children: "Rotate your device" }),
|
|
2319
|
+
/* @__PURE__ */ jsxs("p", { className: "text-muted-foreground mt-2 text-sm", children: [
|
|
2320
|
+
"This game is best experienced in ",
|
|
2321
|
+
requiredOrientation,
|
|
2322
|
+
" ",
|
|
2323
|
+
"orientation."
|
|
2324
|
+
] })
|
|
2325
|
+
] }),
|
|
2326
|
+
/* @__PURE__ */ jsx(
|
|
2327
|
+
"div",
|
|
2328
|
+
{
|
|
2329
|
+
className: cn(
|
|
2330
|
+
"h-full w-full select-none",
|
|
2331
|
+
!orientationOk && "pointer-events-none opacity-30"
|
|
2332
|
+
),
|
|
2333
|
+
children
|
|
2334
|
+
}
|
|
2335
|
+
)
|
|
2336
|
+
] }) }),
|
|
2337
|
+
onReconnect && /* @__PURE__ */ jsx(
|
|
2338
|
+
QRScannerDialog,
|
|
2339
|
+
{
|
|
2340
|
+
isOpen: isScannerOpen,
|
|
2341
|
+
onClose: () => setIsScannerOpen(false),
|
|
2342
|
+
onScan: (roomCode) => {
|
|
2343
|
+
onReconnect(roomCode);
|
|
2344
|
+
setIsScannerOpen(false);
|
|
2345
|
+
},
|
|
2346
|
+
currentRoomId: roomId
|
|
2347
|
+
}
|
|
2348
|
+
)
|
|
2349
|
+
] });
|
|
2350
|
+
};
|
|
2351
|
+
|
|
2352
|
+
// src/events.ts
|
|
2353
|
+
var HostEvents = {
|
|
2354
|
+
/**
|
|
2355
|
+
* Register as a master host (standalone mode)
|
|
2356
|
+
*/
|
|
2357
|
+
REGISTER: "host:register",
|
|
2358
|
+
/**
|
|
2359
|
+
* Register as the system/arcade master host
|
|
2360
|
+
*/
|
|
2361
|
+
REGISTER_SYSTEM: "host:registerSystem",
|
|
2362
|
+
/**
|
|
2363
|
+
* Join as a child host (game launched from arcade)
|
|
2364
|
+
*/
|
|
2365
|
+
JOIN_AS_CHILD: "host:joinAsChild"
|
|
2366
|
+
};
|
|
2367
|
+
var ControllerEvents = {
|
|
2368
|
+
/**
|
|
2369
|
+
* Controller joins a room
|
|
2370
|
+
*/
|
|
2371
|
+
JOIN: "controller:join",
|
|
2372
|
+
/**
|
|
2373
|
+
* Controller sends input
|
|
2374
|
+
*/
|
|
2375
|
+
INPUT: "controller:input",
|
|
2376
|
+
/**
|
|
2377
|
+
* Controller leaves a room
|
|
2378
|
+
*/
|
|
2379
|
+
LEAVE: "controller:leave",
|
|
2380
|
+
/**
|
|
2381
|
+
* Controller sends game state update
|
|
2382
|
+
*/
|
|
2383
|
+
STATE: "controller:state",
|
|
2384
|
+
/**
|
|
2385
|
+
* Controller sends system command
|
|
2386
|
+
*/
|
|
2387
|
+
SYSTEM: "controller:system"
|
|
2388
|
+
};
|
|
2389
|
+
var SystemEvents = {
|
|
2390
|
+
/**
|
|
2391
|
+
* Arcade launches a game
|
|
2392
|
+
*/
|
|
2393
|
+
LAUNCH_GAME: "system:launchGame",
|
|
2394
|
+
/**
|
|
2395
|
+
* Arcade closes a game
|
|
2396
|
+
*/
|
|
2397
|
+
CLOSE_GAME: "system:closeGame"
|
|
2398
|
+
};
|
|
2399
|
+
var ServerEvents = {
|
|
2400
|
+
/**
|
|
2401
|
+
* Welcome message with room/controller info
|
|
2402
|
+
*/
|
|
2403
|
+
WELCOME: "server:welcome",
|
|
2404
|
+
/**
|
|
2405
|
+
* Room is ready
|
|
2406
|
+
*/
|
|
2407
|
+
ROOM_READY: "server:roomReady",
|
|
2408
|
+
/**
|
|
2409
|
+
* Controller joined notification
|
|
2410
|
+
*/
|
|
2411
|
+
CONTROLLER_JOINED: "server:controllerJoined",
|
|
2412
|
+
/**
|
|
2413
|
+
* Controller left notification
|
|
2414
|
+
*/
|
|
2415
|
+
CONTROLLER_LEFT: "server:controllerLeft",
|
|
2416
|
+
/**
|
|
2417
|
+
* Host left notification
|
|
2418
|
+
*/
|
|
2419
|
+
HOST_LEFT: "server:hostLeft",
|
|
2420
|
+
/**
|
|
2421
|
+
* Server error
|
|
2422
|
+
*/
|
|
2423
|
+
ERROR: "server:error",
|
|
2424
|
+
/**
|
|
2425
|
+
* Play sound on client
|
|
2426
|
+
*/
|
|
2427
|
+
PLAY_SOUND: "server:playSound"
|
|
2428
|
+
};
|
|
2429
|
+
var ClientEvents = {
|
|
2430
|
+
/**
|
|
2431
|
+
* Load a game UI in the controller iframe
|
|
2432
|
+
*/
|
|
2433
|
+
LOAD_UI: "client:loadUi",
|
|
2434
|
+
/**
|
|
2435
|
+
* Unload the game UI from the controller iframe
|
|
2436
|
+
*/
|
|
2437
|
+
UNLOAD_UI: "client:unloadUi",
|
|
2438
|
+
/**
|
|
2439
|
+
* Update game state display
|
|
2440
|
+
*/
|
|
2441
|
+
STATE: "client:state"
|
|
2442
|
+
};
|
|
2443
|
+
var ChildEvents = {
|
|
2444
|
+
/**
|
|
2445
|
+
* Server requests child to close
|
|
2446
|
+
*/
|
|
2447
|
+
CLOSE: "child:close"
|
|
2448
|
+
};
|
|
2449
|
+
var Events = {
|
|
2450
|
+
Host: HostEvents,
|
|
2451
|
+
Controller: ControllerEvents,
|
|
2452
|
+
System: SystemEvents,
|
|
2453
|
+
Server: ServerEvents,
|
|
2454
|
+
Client: ClientEvents,
|
|
2455
|
+
Child: ChildEvents
|
|
2456
|
+
};
|
|
2457
|
+
|
|
2458
|
+
// src/utils/ids.ts
|
|
2459
|
+
var alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
2460
|
+
var generateRoomCode = () => {
|
|
2461
|
+
const array = new Uint32Array(4);
|
|
2462
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
2463
|
+
crypto.getRandomValues(array);
|
|
2464
|
+
} else {
|
|
2465
|
+
for (let i = 0; i < array.length; i += 1) {
|
|
2466
|
+
array[i] = Math.floor(Math.random() * alphabet.length);
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
const code = Array.from(
|
|
2470
|
+
array,
|
|
2471
|
+
(value) => alphabet[value % alphabet.length]
|
|
2472
|
+
).join("");
|
|
2473
|
+
return roomCodeSchema.parse(code);
|
|
2474
|
+
};
|
|
2475
|
+
var generateControllerId = () => {
|
|
2476
|
+
const stamp = Math.floor(Date.now() % 1e5).toString().padStart(5, "0");
|
|
2477
|
+
const random = alphabet[Math.floor(Math.random() * alphabet.length)];
|
|
2478
|
+
return `C${random}${stamp}`;
|
|
2479
|
+
};
|
|
2480
|
+
|
|
2481
|
+
// src/hooks/use-air-jam-controller.ts
|
|
2482
|
+
var getRoomFromLocation = () => {
|
|
2483
|
+
if (typeof window === "undefined") {
|
|
2484
|
+
return null;
|
|
2485
|
+
}
|
|
2486
|
+
const params = new URLSearchParams(window.location.search);
|
|
2487
|
+
const code = params.get("room");
|
|
2488
|
+
return code ? code.toUpperCase() : null;
|
|
2489
|
+
};
|
|
2490
|
+
var useAirJamController = (options = {}) => {
|
|
2491
|
+
const { store, getSocket, disconnectSocket } = useAirJamContext();
|
|
2492
|
+
const nicknameRef = useRef(options.nickname ?? "");
|
|
2493
|
+
const subControllerParams = useMemo(() => {
|
|
2494
|
+
if (typeof window === "undefined") return null;
|
|
2495
|
+
const params = new URLSearchParams(window.location.search);
|
|
2496
|
+
const room = params.get("aj_room");
|
|
2497
|
+
const controllerId2 = params.get("aj_controller_id");
|
|
2498
|
+
if (room && controllerId2) {
|
|
2499
|
+
return { room, controllerId: controllerId2 };
|
|
2500
|
+
}
|
|
2501
|
+
return null;
|
|
2502
|
+
}, []);
|
|
2503
|
+
const parsedRoomId = useMemo(() => {
|
|
2504
|
+
const code = subControllerParams?.room ?? options.roomId ?? getRoomFromLocation();
|
|
2505
|
+
if (!code) return null;
|
|
2506
|
+
try {
|
|
2507
|
+
return roomCodeSchema.parse(code.toUpperCase());
|
|
2508
|
+
} catch {
|
|
2509
|
+
return null;
|
|
2510
|
+
}
|
|
2511
|
+
}, [options.roomId, subControllerParams]);
|
|
2512
|
+
const controllerId = useMemo(() => {
|
|
2513
|
+
if (subControllerParams?.controllerId) {
|
|
2514
|
+
return subControllerParams.controllerId;
|
|
2515
|
+
}
|
|
2516
|
+
if (options.controllerId) {
|
|
2517
|
+
return options.controllerId;
|
|
2518
|
+
}
|
|
2519
|
+
if (typeof window !== "undefined") {
|
|
2520
|
+
const params = new URLSearchParams(window.location.search);
|
|
2521
|
+
const urlControllerId = params.get("controllerId");
|
|
2522
|
+
if (urlControllerId) return urlControllerId;
|
|
2523
|
+
}
|
|
2524
|
+
return generateControllerId();
|
|
2525
|
+
}, [options.controllerId, subControllerParams]);
|
|
2526
|
+
const onStateRef = useRef(
|
|
2527
|
+
options.onState
|
|
2528
|
+
);
|
|
2529
|
+
useEffect(() => {
|
|
2530
|
+
onStateRef.current = options.onState;
|
|
2531
|
+
}, [options.onState]);
|
|
2532
|
+
const connectionState = useStore(
|
|
2533
|
+
store,
|
|
2534
|
+
useShallow((state) => ({
|
|
2535
|
+
connectionStatus: state.connectionStatus,
|
|
2536
|
+
lastError: state.lastError,
|
|
2537
|
+
controllerId: state.controllerId,
|
|
2538
|
+
players: state.players,
|
|
2539
|
+
gameState: state.gameState,
|
|
2540
|
+
stateMessage: state.stateMessage
|
|
2541
|
+
}))
|
|
2542
|
+
);
|
|
2543
|
+
const [reconnectKey, setReconnectKey] = useState(0);
|
|
2544
|
+
const reconnect = useCallback(() => {
|
|
2545
|
+
if (!parsedRoomId) return;
|
|
2546
|
+
disconnectSocket("controller");
|
|
2547
|
+
setReconnectKey((prev) => prev + 1);
|
|
2548
|
+
}, [parsedRoomId, disconnectSocket]);
|
|
2549
|
+
const isChildMode = useMemo(() => {
|
|
2550
|
+
if (typeof window === "undefined") return false;
|
|
2551
|
+
const params = new URLSearchParams(window.location.search);
|
|
2552
|
+
return params.get("airjam_mode") === "child";
|
|
2553
|
+
}, []);
|
|
2554
|
+
const shouldConnect = useMemo(() => {
|
|
2555
|
+
if (options.forceConnect) return true;
|
|
2556
|
+
if (subControllerParams) return true;
|
|
2557
|
+
if (typeof window !== "undefined") {
|
|
2558
|
+
const params = new URLSearchParams(window.location.search);
|
|
2559
|
+
return params.get("airjam_force_connect") === "true";
|
|
2560
|
+
}
|
|
2561
|
+
return false;
|
|
2562
|
+
}, [options.forceConnect, subControllerParams]);
|
|
2563
|
+
const canConnect = useMemo(
|
|
2564
|
+
() => !isChildMode || shouldConnect,
|
|
2565
|
+
[isChildMode, shouldConnect]
|
|
2566
|
+
);
|
|
2567
|
+
const socket = useMemo(
|
|
2568
|
+
() => canConnect && parsedRoomId ? getSocket("controller") : null,
|
|
2569
|
+
[canConnect, parsedRoomId, getSocket]
|
|
2570
|
+
);
|
|
2571
|
+
useEffect(() => {
|
|
2572
|
+
if (!socket) return;
|
|
2573
|
+
const handleSignal = (signal) => {
|
|
2574
|
+
if (signal.type !== "HAPTIC") return;
|
|
2575
|
+
if (typeof navigator === "undefined" || !navigator.vibrate) return;
|
|
2576
|
+
const payload = signal.payload;
|
|
2577
|
+
switch (payload.pattern) {
|
|
2578
|
+
case "light":
|
|
2579
|
+
navigator.vibrate(10);
|
|
2580
|
+
break;
|
|
2581
|
+
case "medium":
|
|
2582
|
+
navigator.vibrate(30);
|
|
2583
|
+
break;
|
|
2584
|
+
case "heavy":
|
|
2585
|
+
navigator.vibrate([50, 20, 50]);
|
|
2586
|
+
break;
|
|
2587
|
+
case "success":
|
|
2588
|
+
navigator.vibrate([10, 30, 10]);
|
|
2589
|
+
break;
|
|
2590
|
+
case "failure":
|
|
2591
|
+
navigator.vibrate([50, 50, 50, 50]);
|
|
2592
|
+
break;
|
|
2593
|
+
case "custom":
|
|
2594
|
+
if (Array.isArray(payload.sequence)) {
|
|
2595
|
+
navigator.vibrate(payload.sequence);
|
|
2596
|
+
} else if (typeof payload.sequence === "number") {
|
|
2597
|
+
navigator.vibrate(payload.sequence);
|
|
2598
|
+
}
|
|
2599
|
+
break;
|
|
2600
|
+
}
|
|
2601
|
+
};
|
|
2602
|
+
socket.on("server:signal", handleSignal);
|
|
2603
|
+
return () => {
|
|
2604
|
+
socket.off("server:signal", handleSignal);
|
|
2605
|
+
};
|
|
2606
|
+
}, [socket]);
|
|
2607
|
+
useEffect(() => {
|
|
2608
|
+
const storeState = store.getState();
|
|
2609
|
+
storeState.setMode(detectRunMode());
|
|
2610
|
+
storeState.setRole("controller");
|
|
2611
|
+
storeState.setRoomId(parsedRoomId);
|
|
2612
|
+
storeState.setStatus(parsedRoomId ? "connecting" : "idle");
|
|
2613
|
+
storeState.setError(void 0);
|
|
2614
|
+
if (!canConnect) {
|
|
2615
|
+
storeState.setStatus("connected");
|
|
2616
|
+
const handleMessage = (event) => {
|
|
2617
|
+
if (event.source !== window.parent) return;
|
|
2618
|
+
const data = event.data;
|
|
2619
|
+
if (data?.type === "AIRJAM_STATE") {
|
|
2620
|
+
const payload = data.payload;
|
|
2621
|
+
if (payload.gameState) storeState.setGameState(payload.gameState);
|
|
2622
|
+
if (payload.message !== void 0)
|
|
2623
|
+
storeState.setStateMessage(payload.message);
|
|
2624
|
+
onStateRef.current?.(payload);
|
|
2625
|
+
}
|
|
2626
|
+
};
|
|
2627
|
+
window.addEventListener("message", handleMessage);
|
|
2628
|
+
window.parent.postMessage({ type: "AIRJAM_READY" }, "*");
|
|
2629
|
+
return () => {
|
|
2630
|
+
window.removeEventListener("message", handleMessage);
|
|
2631
|
+
storeState.setStatus("disconnected");
|
|
2632
|
+
};
|
|
2633
|
+
}
|
|
2634
|
+
if (!parsedRoomId || !socket || !controllerId) {
|
|
2635
|
+
storeState.setStatus("idle");
|
|
2636
|
+
return;
|
|
2637
|
+
}
|
|
2638
|
+
storeState.setControllerId(controllerId);
|
|
2639
|
+
const handleConnect = () => {
|
|
2640
|
+
store.getState().setStatus("connected");
|
|
2641
|
+
if (subControllerParams) {
|
|
2642
|
+
return;
|
|
2643
|
+
}
|
|
2644
|
+
const payload = controllerJoinSchema.parse({
|
|
2645
|
+
roomId: parsedRoomId,
|
|
2646
|
+
controllerId,
|
|
2647
|
+
nickname: nicknameRef.current || void 0
|
|
2648
|
+
});
|
|
2649
|
+
socket.emit("controller:join", payload, (ack) => {
|
|
2650
|
+
const storeState2 = store.getState();
|
|
2651
|
+
if (!ack.ok) {
|
|
2652
|
+
storeState2.setError(ack.message ?? "Unable to join room");
|
|
2653
|
+
storeState2.setStatus("disconnected");
|
|
2654
|
+
return;
|
|
2655
|
+
}
|
|
2656
|
+
if (ack.controllerId) {
|
|
2657
|
+
storeState2.setControllerId(ack.controllerId);
|
|
2658
|
+
}
|
|
2659
|
+
storeState2.setStatus("connected");
|
|
2660
|
+
});
|
|
2661
|
+
};
|
|
2662
|
+
const handleDisconnect = () => {
|
|
2663
|
+
store.getState().setStatus("disconnected");
|
|
2664
|
+
};
|
|
2665
|
+
const handleWelcome = (payload) => {
|
|
2666
|
+
const storeState2 = store.getState();
|
|
2667
|
+
const storeRoomId = storeState2.roomId;
|
|
2668
|
+
if (storeRoomId && payload.roomId.toUpperCase() !== storeRoomId.toUpperCase()) {
|
|
2669
|
+
return;
|
|
2670
|
+
}
|
|
2671
|
+
if (!storeRoomId && payload.roomId) {
|
|
2672
|
+
storeState2.setRoomId(payload.roomId);
|
|
2673
|
+
}
|
|
2674
|
+
if (!payload.player) {
|
|
2675
|
+
storeState2.setError(
|
|
2676
|
+
"Welcome message received but no player profile included."
|
|
2677
|
+
);
|
|
2678
|
+
return;
|
|
2679
|
+
}
|
|
2680
|
+
storeState2.upsertPlayer(payload.player);
|
|
2681
|
+
};
|
|
2682
|
+
const handleState = (payload) => {
|
|
2683
|
+
if (payload.roomId !== parsedRoomId) return;
|
|
2684
|
+
const storeState2 = store.getState();
|
|
2685
|
+
if (payload.state.gameState) {
|
|
2686
|
+
storeState2.setGameState(payload.state.gameState);
|
|
2687
|
+
}
|
|
2688
|
+
if (payload.state.message !== void 0) {
|
|
2689
|
+
storeState2.setStateMessage(payload.state.message);
|
|
2690
|
+
}
|
|
2691
|
+
onStateRef.current?.(payload.state);
|
|
2692
|
+
};
|
|
2693
|
+
const handleHostLeft = (payload) => {
|
|
2694
|
+
const storeState2 = store.getState();
|
|
2695
|
+
storeState2.setError(payload.reason);
|
|
2696
|
+
storeState2.setStatus("disconnected");
|
|
2697
|
+
storeState2.resetGameState();
|
|
2698
|
+
setTimeout(() => {
|
|
2699
|
+
disconnectSocket("controller");
|
|
2700
|
+
setReconnectKey((prev) => prev + 1);
|
|
2701
|
+
}, 1e3);
|
|
2702
|
+
};
|
|
2703
|
+
const handleError = (payload) => {
|
|
2704
|
+
store.getState().setError(payload.message);
|
|
2705
|
+
};
|
|
2706
|
+
socket.on("connect", handleConnect);
|
|
2707
|
+
socket.on("disconnect", handleDisconnect);
|
|
2708
|
+
socket.on("server:welcome", handleWelcome);
|
|
2709
|
+
socket.on("server:state", handleState);
|
|
2710
|
+
socket.on("server:hostLeft", handleHostLeft);
|
|
2711
|
+
socket.on("server:error", handleError);
|
|
2712
|
+
socket.connect();
|
|
2713
|
+
return () => {
|
|
2714
|
+
socket.off("connect", handleConnect);
|
|
2715
|
+
socket.off("disconnect", handleDisconnect);
|
|
2716
|
+
socket.off("server:welcome", handleWelcome);
|
|
2717
|
+
socket.off("server:state", handleState);
|
|
2718
|
+
socket.off("server:hostLeft", handleHostLeft);
|
|
2719
|
+
socket.off("server:error", handleError);
|
|
2720
|
+
};
|
|
2721
|
+
}, [
|
|
2722
|
+
parsedRoomId,
|
|
2723
|
+
reconnectKey,
|
|
2724
|
+
canConnect,
|
|
2725
|
+
socket,
|
|
2726
|
+
controllerId,
|
|
2727
|
+
subControllerParams,
|
|
2728
|
+
store,
|
|
2729
|
+
disconnectSocket
|
|
2730
|
+
]);
|
|
2731
|
+
const setNickname = useCallback((value) => {
|
|
2732
|
+
nicknameRef.current = value;
|
|
2733
|
+
}, []);
|
|
2734
|
+
const sendInput = useCallback(
|
|
2735
|
+
(input) => {
|
|
2736
|
+
const storeState = store.getState();
|
|
2737
|
+
if (!canConnect) {
|
|
2738
|
+
if (typeof window !== "undefined") {
|
|
2739
|
+
window.parent.postMessage(
|
|
2740
|
+
{
|
|
2741
|
+
type: "AIRJAM_INPUT",
|
|
2742
|
+
payload: input
|
|
2743
|
+
},
|
|
2744
|
+
"*"
|
|
2745
|
+
);
|
|
2746
|
+
}
|
|
2747
|
+
return true;
|
|
2748
|
+
}
|
|
2749
|
+
if (!parsedRoomId || !storeState.controllerId || !socket) {
|
|
2750
|
+
storeState.setError("Not connected to a room");
|
|
2751
|
+
return false;
|
|
2752
|
+
}
|
|
2753
|
+
if (!socket.connected) {
|
|
2754
|
+
return false;
|
|
2755
|
+
}
|
|
2756
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
2757
|
+
storeState.setError("Input must be an object");
|
|
2758
|
+
return false;
|
|
2759
|
+
}
|
|
2760
|
+
const payload = controllerInputSchema.safeParse({
|
|
2761
|
+
roomId: parsedRoomId,
|
|
2762
|
+
controllerId: storeState.controllerId,
|
|
2763
|
+
input
|
|
2764
|
+
});
|
|
2765
|
+
if (!payload.success) {
|
|
2766
|
+
storeState.setError(payload.error.message);
|
|
2767
|
+
return false;
|
|
2768
|
+
}
|
|
2769
|
+
socket.emit("controller:input", payload.data);
|
|
2770
|
+
return true;
|
|
2771
|
+
},
|
|
2772
|
+
[parsedRoomId, canConnect, socket, store]
|
|
2773
|
+
);
|
|
2774
|
+
const sendSystemCommand = useCallback(
|
|
2775
|
+
(command) => {
|
|
2776
|
+
const storeState = store.getState();
|
|
2777
|
+
if (!canConnect) return;
|
|
2778
|
+
if (!parsedRoomId || !storeState.controllerId || !socket) return;
|
|
2779
|
+
if (!socket.connected) return;
|
|
2780
|
+
const payload = controllerSystemSchema.safeParse({
|
|
2781
|
+
roomId: parsedRoomId,
|
|
2782
|
+
command
|
|
2783
|
+
});
|
|
2784
|
+
if (payload.success) {
|
|
2785
|
+
socket.emit("controller:system", payload.data);
|
|
2786
|
+
}
|
|
2787
|
+
},
|
|
2788
|
+
[parsedRoomId, canConnect, socket, store]
|
|
2789
|
+
);
|
|
2790
|
+
return {
|
|
2791
|
+
roomId: parsedRoomId,
|
|
2792
|
+
controllerId: connectionState.controllerId,
|
|
2793
|
+
connectionStatus: connectionState.connectionStatus,
|
|
2794
|
+
lastError: connectionState.lastError,
|
|
2795
|
+
gameState: connectionState.gameState,
|
|
2796
|
+
stateMessage: connectionState.stateMessage,
|
|
2797
|
+
sendInput,
|
|
2798
|
+
sendSystemCommand,
|
|
2799
|
+
setNickname,
|
|
2800
|
+
reconnect,
|
|
2801
|
+
players: connectionState.players,
|
|
2802
|
+
socket
|
|
2803
|
+
};
|
|
2804
|
+
};
|
|
2805
|
+
var useAirJamHost = (options = {}) => {
|
|
2806
|
+
const { config, store, getSocket, disconnectSocket, inputManager } = useAirJamContext();
|
|
2807
|
+
const arcadeParams = useMemo(() => {
|
|
2808
|
+
if (typeof window === "undefined") return null;
|
|
2809
|
+
const params = new URLSearchParams(window.location.search);
|
|
2810
|
+
const room = params.get("aj_room");
|
|
2811
|
+
const token = params.get("aj_token");
|
|
2812
|
+
if (room && token) {
|
|
2813
|
+
return { room, token };
|
|
2814
|
+
}
|
|
2815
|
+
return null;
|
|
2816
|
+
}, []);
|
|
2817
|
+
const isChildMode = !!arcadeParams;
|
|
2818
|
+
const shouldConnect = true;
|
|
2819
|
+
const [fallbackRoomId] = useState(() => generateRoomCode());
|
|
2820
|
+
const parsedRoomId = useMemo(() => {
|
|
2821
|
+
if (arcadeParams) {
|
|
2822
|
+
return roomCodeSchema.parse(arcadeParams.room.toUpperCase());
|
|
2823
|
+
}
|
|
2824
|
+
if (typeof window !== "undefined") {
|
|
2825
|
+
const params = new URLSearchParams(window.location.search);
|
|
2826
|
+
const paramRoom = params.get("room");
|
|
2827
|
+
if (paramRoom) {
|
|
2828
|
+
const result = roomCodeSchema.safeParse(paramRoom.toUpperCase());
|
|
2829
|
+
if (result.success) return result.data;
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
if (options.roomId) {
|
|
2833
|
+
return roomCodeSchema.parse(options.roomId.toUpperCase());
|
|
2834
|
+
}
|
|
2835
|
+
return fallbackRoomId;
|
|
2836
|
+
}, [options.roomId, fallbackRoomId, arcadeParams]);
|
|
2837
|
+
useEffect(() => {
|
|
2838
|
+
if (inputManager) {
|
|
2839
|
+
inputManager.setRoomId(parsedRoomId);
|
|
2840
|
+
}
|
|
2841
|
+
}, [inputManager, parsedRoomId]);
|
|
2842
|
+
const [joinUrl, setJoinUrl] = useState("");
|
|
2843
|
+
const onPlayerJoinRef = useRef(options.onPlayerJoin);
|
|
2844
|
+
const onPlayerLeaveRef = useRef(options.onPlayerLeave);
|
|
2845
|
+
const onChildCloseRef = useRef(options.onChildClose);
|
|
2846
|
+
useEffect(() => {
|
|
2847
|
+
onPlayerJoinRef.current = options.onPlayerJoin;
|
|
2848
|
+
onPlayerLeaveRef.current = options.onPlayerLeave;
|
|
2849
|
+
onChildCloseRef.current = options.onChildClose;
|
|
2850
|
+
}, [options.onPlayerJoin, options.onPlayerLeave, options.onChildClose]);
|
|
2851
|
+
const connectionState = useStore(
|
|
2852
|
+
store,
|
|
2853
|
+
useShallow((state) => ({
|
|
2854
|
+
connectionStatus: state.connectionStatus,
|
|
2855
|
+
lastError: state.lastError,
|
|
2856
|
+
players: state.players,
|
|
2857
|
+
gameState: state.gameState,
|
|
2858
|
+
mode: state.mode
|
|
2859
|
+
}))
|
|
2860
|
+
);
|
|
2861
|
+
const socket = useMemo(
|
|
2862
|
+
() => getSocket("host") ,
|
|
2863
|
+
[shouldConnect, getSocket]
|
|
2864
|
+
);
|
|
2865
|
+
const [lastToggle, setLastToggle] = useState(0);
|
|
2866
|
+
const toggleGameState = useCallback(() => {
|
|
2867
|
+
const now = Date.now();
|
|
2868
|
+
if (now - lastToggle < TOGGLE_DEBOUNCE_MS) {
|
|
2869
|
+
return;
|
|
2870
|
+
}
|
|
2871
|
+
setLastToggle(now);
|
|
2872
|
+
if (!socket || !socket.connected) {
|
|
2873
|
+
return;
|
|
2874
|
+
}
|
|
2875
|
+
const payload = controllerSystemSchema.safeParse({
|
|
2876
|
+
roomId: parsedRoomId,
|
|
2877
|
+
command: "toggle_pause"
|
|
2878
|
+
});
|
|
2879
|
+
if (payload.success) {
|
|
2880
|
+
socket.emit("host:system", payload.data);
|
|
2881
|
+
}
|
|
2882
|
+
}, [socket, parsedRoomId, lastToggle]);
|
|
2883
|
+
const sendState = useCallback(
|
|
2884
|
+
(state) => {
|
|
2885
|
+
if (!socket || !socket.connected) {
|
|
2886
|
+
return false;
|
|
2887
|
+
}
|
|
2888
|
+
const payload = controllerStateSchema.safeParse({
|
|
2889
|
+
roomId: parsedRoomId,
|
|
2890
|
+
state
|
|
2891
|
+
});
|
|
2892
|
+
if (!payload.success) {
|
|
2893
|
+
return false;
|
|
2894
|
+
}
|
|
2895
|
+
socket.emit("host:state", payload.data);
|
|
2896
|
+
return true;
|
|
2897
|
+
},
|
|
2898
|
+
[socket, parsedRoomId]
|
|
2899
|
+
);
|
|
2900
|
+
const sendSignal = useCallback(
|
|
2901
|
+
(type, payload, targetId) => {
|
|
2902
|
+
if (!socket || !socket.connected) {
|
|
2903
|
+
return;
|
|
2904
|
+
}
|
|
2905
|
+
const signal = {
|
|
2906
|
+
targetId,
|
|
2907
|
+
type,
|
|
2908
|
+
payload
|
|
2909
|
+
};
|
|
2910
|
+
socket.emit("host:signal", signal);
|
|
2911
|
+
},
|
|
2912
|
+
[socket]
|
|
2913
|
+
);
|
|
2914
|
+
const reconnect = useCallback(() => {
|
|
2915
|
+
disconnectSocket("host");
|
|
2916
|
+
if (socket) {
|
|
2917
|
+
socket.connect();
|
|
2918
|
+
}
|
|
2919
|
+
}, [socket, disconnectSocket]);
|
|
2920
|
+
useEffect(() => {
|
|
2921
|
+
(async () => {
|
|
2922
|
+
const url = await urlBuilder.buildControllerUrl(parsedRoomId, {
|
|
2923
|
+
path: config.controllerPath,
|
|
2924
|
+
host: config.publicHost
|
|
2925
|
+
});
|
|
2926
|
+
setJoinUrl(url);
|
|
2927
|
+
})();
|
|
2928
|
+
}, [parsedRoomId, config.controllerPath, config.publicHost]);
|
|
2929
|
+
const setRegisteredRoomId = useStore(store, (s) => s.setRegisteredRoomId);
|
|
2930
|
+
useEffect(() => {
|
|
2931
|
+
const storeState = store.getState();
|
|
2932
|
+
storeState.setMode(detectRunMode());
|
|
2933
|
+
storeState.setRole("host");
|
|
2934
|
+
storeState.setRoomId(parsedRoomId);
|
|
2935
|
+
storeState.setStatus("connecting");
|
|
2936
|
+
storeState.setError(void 0);
|
|
2937
|
+
if (!socket) {
|
|
2938
|
+
storeState.setStatus("idle");
|
|
2939
|
+
return;
|
|
2940
|
+
}
|
|
2941
|
+
const registerHost = async () => {
|
|
2942
|
+
const storeState2 = store.getState();
|
|
2943
|
+
const currentRegisteredRoomId2 = store.getState().registeredRoomId;
|
|
2944
|
+
if (currentRegisteredRoomId2 === parsedRoomId && socket.connected) {
|
|
2945
|
+
return;
|
|
2946
|
+
}
|
|
2947
|
+
if (arcadeParams) {
|
|
2948
|
+
const childRoomId = roomCodeSchema.parse(
|
|
2949
|
+
arcadeParams.room.toUpperCase()
|
|
2950
|
+
);
|
|
2951
|
+
const payload = {
|
|
2952
|
+
roomId: childRoomId,
|
|
2953
|
+
joinToken: arcadeParams.token
|
|
2954
|
+
};
|
|
2955
|
+
socket.emit("host:joinAsChild", payload, (ack) => {
|
|
2956
|
+
if (!ack.ok) {
|
|
2957
|
+
storeState2.setError(ack.message ?? "Failed to join as child");
|
|
2958
|
+
storeState2.setStatus("disconnected");
|
|
2959
|
+
setRegisteredRoomId(null);
|
|
2960
|
+
return;
|
|
2961
|
+
}
|
|
2962
|
+
storeState2.setStatus("connected");
|
|
2963
|
+
storeState2.setRoomId(childRoomId);
|
|
2964
|
+
setRegisteredRoomId(childRoomId);
|
|
2965
|
+
});
|
|
2966
|
+
} else {
|
|
2967
|
+
const payload = hostRegistrationSchema.parse({
|
|
2968
|
+
roomId: parsedRoomId,
|
|
2969
|
+
maxPlayers: options.maxPlayers ?? config.maxPlayers,
|
|
2970
|
+
apiKey: options.apiKey ?? config.apiKey
|
|
2971
|
+
});
|
|
2972
|
+
socket.emit("host:register", payload, (ack) => {
|
|
2973
|
+
if (!ack.ok) {
|
|
2974
|
+
storeState2.setError(ack.message ?? "Failed to register host");
|
|
2975
|
+
storeState2.setStatus("disconnected");
|
|
2976
|
+
setRegisteredRoomId(null);
|
|
2977
|
+
return;
|
|
2978
|
+
}
|
|
2979
|
+
storeState2.setStatus("connected");
|
|
2980
|
+
if (ack.roomId) {
|
|
2981
|
+
storeState2.setRoomId(ack.roomId);
|
|
2982
|
+
setRegisteredRoomId(ack.roomId);
|
|
2983
|
+
} else {
|
|
2984
|
+
setRegisteredRoomId(parsedRoomId);
|
|
2985
|
+
}
|
|
2986
|
+
});
|
|
2987
|
+
}
|
|
2988
|
+
};
|
|
2989
|
+
const handleConnect = () => {
|
|
2990
|
+
store.getState().setStatus("connected");
|
|
2991
|
+
registerHost();
|
|
2992
|
+
};
|
|
2993
|
+
const handleDisconnect = () => {
|
|
2994
|
+
store.getState().setStatus("disconnected");
|
|
2995
|
+
};
|
|
2996
|
+
const handleJoin = (payload) => {
|
|
2997
|
+
if (payload.player) {
|
|
2998
|
+
store.getState().upsertPlayer(payload.player);
|
|
2999
|
+
setTimeout(() => {
|
|
3000
|
+
onPlayerJoinRef.current?.(payload.player);
|
|
3001
|
+
}, 0);
|
|
3002
|
+
}
|
|
3003
|
+
};
|
|
3004
|
+
const handleLeave = (payload) => {
|
|
3005
|
+
store.getState().removePlayer(payload.controllerId);
|
|
3006
|
+
onPlayerLeaveRef.current?.(payload.controllerId);
|
|
3007
|
+
};
|
|
3008
|
+
const handleInput = (payload) => {
|
|
3009
|
+
if (inputManager) {
|
|
3010
|
+
inputManager.handleInput(payload);
|
|
3011
|
+
}
|
|
3012
|
+
};
|
|
3013
|
+
const handleChildClose = () => {
|
|
3014
|
+
onChildCloseRef.current?.();
|
|
3015
|
+
};
|
|
3016
|
+
const handleState = (payload) => {
|
|
3017
|
+
if (payload.roomId !== parsedRoomId) return;
|
|
3018
|
+
const storeState2 = store.getState();
|
|
3019
|
+
if (payload.state.gameState) {
|
|
3020
|
+
storeState2.setGameState(payload.state.gameState);
|
|
3021
|
+
}
|
|
3022
|
+
if (payload.state.message !== void 0) {
|
|
3023
|
+
storeState2.setStateMessage(payload.state.message);
|
|
3024
|
+
}
|
|
3025
|
+
};
|
|
3026
|
+
const handleError = (payload) => {
|
|
3027
|
+
store.getState().setError(payload.message);
|
|
3028
|
+
};
|
|
3029
|
+
socket.on("connect", handleConnect);
|
|
3030
|
+
socket.on("disconnect", handleDisconnect);
|
|
3031
|
+
socket.on("server:controllerJoined", handleJoin);
|
|
3032
|
+
socket.on("server:controllerLeft", handleLeave);
|
|
3033
|
+
socket.on("server:input", handleInput);
|
|
3034
|
+
socket.on("server:error", handleError);
|
|
3035
|
+
socket.on("server:closeChild", handleChildClose);
|
|
3036
|
+
socket.on("server:state", handleState);
|
|
3037
|
+
socket.connect();
|
|
3038
|
+
const currentRegisteredRoomId = store.getState().registeredRoomId;
|
|
3039
|
+
if (socket.connected && currentRegisteredRoomId !== parsedRoomId) {
|
|
3040
|
+
registerHost();
|
|
3041
|
+
}
|
|
3042
|
+
if (currentRegisteredRoomId && currentRegisteredRoomId !== parsedRoomId) {
|
|
3043
|
+
setRegisteredRoomId(null);
|
|
3044
|
+
}
|
|
3045
|
+
return () => {
|
|
3046
|
+
socket.off("connect", handleConnect);
|
|
3047
|
+
socket.off("disconnect", handleDisconnect);
|
|
3048
|
+
socket.off("server:controllerJoined", handleJoin);
|
|
3049
|
+
socket.off("server:controllerLeft", handleLeave);
|
|
3050
|
+
socket.off("server:input", handleInput);
|
|
3051
|
+
socket.off("server:error", handleError);
|
|
3052
|
+
socket.off("server:closeChild", handleChildClose);
|
|
3053
|
+
socket.off("server:state", handleState);
|
|
3054
|
+
};
|
|
3055
|
+
}, [
|
|
3056
|
+
config.maxPlayers,
|
|
3057
|
+
config.apiKey,
|
|
3058
|
+
options.maxPlayers,
|
|
3059
|
+
options.apiKey,
|
|
3060
|
+
parsedRoomId,
|
|
3061
|
+
arcadeParams,
|
|
3062
|
+
shouldConnect,
|
|
3063
|
+
socket,
|
|
3064
|
+
store,
|
|
3065
|
+
inputManager,
|
|
3066
|
+
setRegisteredRoomId
|
|
3067
|
+
]);
|
|
3068
|
+
const returnSocket = socket ?? getSocket("host");
|
|
3069
|
+
const getInput = useCallback(
|
|
3070
|
+
(controllerId) => {
|
|
3071
|
+
if (!inputManager) {
|
|
3072
|
+
return void 0;
|
|
3073
|
+
}
|
|
3074
|
+
return inputManager.getInput(controllerId);
|
|
3075
|
+
},
|
|
3076
|
+
[inputManager]
|
|
3077
|
+
);
|
|
3078
|
+
return {
|
|
3079
|
+
roomId: parsedRoomId,
|
|
3080
|
+
joinUrl,
|
|
3081
|
+
connectionStatus: connectionState.connectionStatus,
|
|
3082
|
+
players: connectionState.players,
|
|
3083
|
+
lastError: connectionState.lastError,
|
|
3084
|
+
mode: connectionState.mode,
|
|
3085
|
+
gameState: connectionState.gameState,
|
|
3086
|
+
toggleGameState,
|
|
3087
|
+
sendState,
|
|
3088
|
+
sendSignal,
|
|
3089
|
+
reconnect,
|
|
3090
|
+
socket: returnSocket,
|
|
3091
|
+
isChildMode,
|
|
3092
|
+
getInput
|
|
3093
|
+
};
|
|
3094
|
+
};
|
|
3095
|
+
var getRoomFromLocation2 = () => {
|
|
3096
|
+
if (typeof window === "undefined") {
|
|
3097
|
+
return null;
|
|
3098
|
+
}
|
|
3099
|
+
const params = new URLSearchParams(window.location.search);
|
|
3100
|
+
const code = params.get("room");
|
|
3101
|
+
return code ? code.toUpperCase() : null;
|
|
3102
|
+
};
|
|
3103
|
+
var useAirJamShell = (options = {}) => {
|
|
3104
|
+
const { store, getSocket } = useAirJamContext();
|
|
3105
|
+
const nicknameRef = useRef(options.nickname ?? "Arcade Shell");
|
|
3106
|
+
const parsedRoomId = useMemo(() => {
|
|
3107
|
+
const code = options.roomId ?? getRoomFromLocation2();
|
|
3108
|
+
if (!code) return null;
|
|
3109
|
+
try {
|
|
3110
|
+
return roomCodeSchema.parse(code.toUpperCase());
|
|
3111
|
+
} catch {
|
|
3112
|
+
return null;
|
|
3113
|
+
}
|
|
3114
|
+
}, [options.roomId]);
|
|
3115
|
+
const controllerId = useMemo(() => {
|
|
3116
|
+
if (options.controllerId) return options.controllerId;
|
|
3117
|
+
if (typeof window !== "undefined") {
|
|
3118
|
+
const params = new URLSearchParams(window.location.search);
|
|
3119
|
+
const urlControllerId = params.get("controllerId");
|
|
3120
|
+
if (urlControllerId) return urlControllerId;
|
|
3121
|
+
}
|
|
3122
|
+
return generateControllerId();
|
|
3123
|
+
}, [options.controllerId]);
|
|
3124
|
+
const [activeUrl, setActiveUrl] = useState(null);
|
|
3125
|
+
const connectionState = useStore(
|
|
3126
|
+
store,
|
|
3127
|
+
useShallow((state) => ({
|
|
3128
|
+
connectionStatus: state.connectionStatus,
|
|
3129
|
+
lastError: state.lastError,
|
|
3130
|
+
controllerId: state.controllerId,
|
|
3131
|
+
players: state.players,
|
|
3132
|
+
gameState: state.gameState
|
|
3133
|
+
}))
|
|
3134
|
+
);
|
|
3135
|
+
const socket = useMemo(
|
|
3136
|
+
() => parsedRoomId ? getSocket("controller") : null,
|
|
3137
|
+
[parsedRoomId, getSocket]
|
|
3138
|
+
);
|
|
3139
|
+
useEffect(() => {
|
|
3140
|
+
if (!socket) return;
|
|
3141
|
+
const handleSignal = (signal) => {
|
|
3142
|
+
if (signal.type !== "HAPTIC") return;
|
|
3143
|
+
if (typeof navigator === "undefined" || !navigator.vibrate) return;
|
|
3144
|
+
const payload = signal.payload;
|
|
3145
|
+
switch (payload.pattern) {
|
|
3146
|
+
case "light":
|
|
3147
|
+
navigator.vibrate(10);
|
|
3148
|
+
break;
|
|
3149
|
+
case "medium":
|
|
3150
|
+
navigator.vibrate(30);
|
|
3151
|
+
break;
|
|
3152
|
+
case "heavy":
|
|
3153
|
+
navigator.vibrate([50, 20, 50]);
|
|
3154
|
+
break;
|
|
3155
|
+
case "success":
|
|
3156
|
+
navigator.vibrate([10, 30, 10]);
|
|
3157
|
+
break;
|
|
3158
|
+
case "failure":
|
|
3159
|
+
navigator.vibrate([50, 50, 50, 50]);
|
|
3160
|
+
break;
|
|
3161
|
+
case "custom":
|
|
3162
|
+
if (Array.isArray(payload.sequence)) {
|
|
3163
|
+
navigator.vibrate(payload.sequence);
|
|
3164
|
+
} else if (typeof payload.sequence === "number") {
|
|
3165
|
+
navigator.vibrate(payload.sequence);
|
|
3166
|
+
}
|
|
3167
|
+
break;
|
|
3168
|
+
}
|
|
3169
|
+
};
|
|
3170
|
+
socket.on("server:signal", handleSignal);
|
|
3171
|
+
return () => {
|
|
3172
|
+
socket.off("server:signal", handleSignal);
|
|
3173
|
+
};
|
|
3174
|
+
}, [socket]);
|
|
3175
|
+
useEffect(() => {
|
|
3176
|
+
const storeState = store.getState();
|
|
3177
|
+
storeState.setMode(detectRunMode());
|
|
3178
|
+
storeState.setRole("controller");
|
|
3179
|
+
storeState.setRoomId(parsedRoomId);
|
|
3180
|
+
storeState.setStatus(parsedRoomId ? "connecting" : "idle");
|
|
3181
|
+
storeState.setError(void 0);
|
|
3182
|
+
if (!parsedRoomId || !socket || !controllerId) {
|
|
3183
|
+
storeState.setStatus("idle");
|
|
3184
|
+
return;
|
|
3185
|
+
}
|
|
3186
|
+
storeState.setControllerId(controllerId);
|
|
3187
|
+
const handleConnect = () => {
|
|
3188
|
+
store.getState().setStatus("connected");
|
|
3189
|
+
const payload = controllerJoinSchema.parse({
|
|
3190
|
+
roomId: parsedRoomId,
|
|
3191
|
+
controllerId,
|
|
3192
|
+
nickname: nicknameRef.current || void 0
|
|
3193
|
+
});
|
|
3194
|
+
socket.emit("controller:join", payload, (ack) => {
|
|
3195
|
+
const storeState2 = store.getState();
|
|
3196
|
+
if (!ack.ok) {
|
|
3197
|
+
storeState2.setError(ack.message ?? "Unable to join room");
|
|
3198
|
+
storeState2.setStatus("disconnected");
|
|
3199
|
+
return;
|
|
3200
|
+
}
|
|
3201
|
+
if (ack.controllerId) {
|
|
3202
|
+
storeState2.setControllerId(ack.controllerId);
|
|
3203
|
+
}
|
|
3204
|
+
storeState2.setStatus("connected");
|
|
3205
|
+
});
|
|
3206
|
+
};
|
|
3207
|
+
const handleDisconnect = () => {
|
|
3208
|
+
store.getState().setStatus("disconnected");
|
|
3209
|
+
};
|
|
3210
|
+
const handleLoadUi = (payload) => {
|
|
3211
|
+
setActiveUrl(payload.url);
|
|
3212
|
+
};
|
|
3213
|
+
const handleUnloadUi = () => {
|
|
3214
|
+
setActiveUrl(null);
|
|
3215
|
+
};
|
|
3216
|
+
const handleWelcome = (payload) => {
|
|
3217
|
+
const storeState2 = store.getState();
|
|
3218
|
+
const storeRoomId = storeState2.roomId;
|
|
3219
|
+
if (storeRoomId && payload.roomId.toUpperCase() !== storeRoomId.toUpperCase()) {
|
|
3220
|
+
return;
|
|
3221
|
+
}
|
|
3222
|
+
if (!storeRoomId && payload.roomId) {
|
|
3223
|
+
storeState2.setRoomId(payload.roomId);
|
|
3224
|
+
}
|
|
3225
|
+
if (payload.player) {
|
|
3226
|
+
storeState2.upsertPlayer(payload.player);
|
|
3227
|
+
}
|
|
3228
|
+
};
|
|
3229
|
+
const handleState = (payload) => {
|
|
3230
|
+
if (payload.roomId !== parsedRoomId) return;
|
|
3231
|
+
const storeState2 = store.getState();
|
|
3232
|
+
if (payload.state.gameState) {
|
|
3233
|
+
storeState2.setGameState(payload.state.gameState);
|
|
3234
|
+
}
|
|
3235
|
+
if (payload.state.message !== void 0) {
|
|
3236
|
+
storeState2.setStateMessage(payload.state.message);
|
|
3237
|
+
}
|
|
3238
|
+
};
|
|
3239
|
+
const handleError = (payload) => {
|
|
3240
|
+
store.getState().setError(payload.message);
|
|
3241
|
+
};
|
|
3242
|
+
const handleMessage = (event) => {
|
|
3243
|
+
const storeState2 = store.getState();
|
|
3244
|
+
const data = event.data;
|
|
3245
|
+
if (data?.type === "AIRJAM_INPUT") {
|
|
3246
|
+
const input = data.payload;
|
|
3247
|
+
const payload = controllerInputSchema.safeParse({
|
|
3248
|
+
roomId: parsedRoomId,
|
|
3249
|
+
controllerId: storeState2.controllerId,
|
|
3250
|
+
input
|
|
3251
|
+
});
|
|
3252
|
+
if (payload.success) {
|
|
3253
|
+
socket.emit("controller:input", payload.data);
|
|
3254
|
+
}
|
|
3255
|
+
} else if (data?.type === "AIRJAM_READY") ;
|
|
3256
|
+
};
|
|
3257
|
+
socket.on("connect", handleConnect);
|
|
3258
|
+
socket.on("disconnect", handleDisconnect);
|
|
3259
|
+
socket.on("server:welcome", handleWelcome);
|
|
3260
|
+
socket.on("server:state", handleState);
|
|
3261
|
+
socket.on("client:loadUi", handleLoadUi);
|
|
3262
|
+
socket.on("client:unloadUi", handleUnloadUi);
|
|
3263
|
+
socket.on("server:error", handleError);
|
|
3264
|
+
window.addEventListener("message", handleMessage);
|
|
3265
|
+
socket.connect();
|
|
3266
|
+
return () => {
|
|
3267
|
+
socket.off("connect", handleConnect);
|
|
3268
|
+
socket.off("disconnect", handleDisconnect);
|
|
3269
|
+
socket.off("server:welcome", handleWelcome);
|
|
3270
|
+
socket.off("server:state", handleState);
|
|
3271
|
+
socket.off("client:loadUi", handleLoadUi);
|
|
3272
|
+
socket.off("client:unloadUi", handleUnloadUi);
|
|
3273
|
+
socket.off("server:error", handleError);
|
|
3274
|
+
window.removeEventListener("message", handleMessage);
|
|
3275
|
+
};
|
|
3276
|
+
}, [parsedRoomId, socket, controllerId, store]);
|
|
3277
|
+
const sendInput = useCallback(
|
|
3278
|
+
(input) => {
|
|
3279
|
+
const storeState = store.getState();
|
|
3280
|
+
if (!parsedRoomId || !storeState.controllerId || !socket) {
|
|
3281
|
+
storeState.setError("Not connected to a room");
|
|
3282
|
+
return false;
|
|
3283
|
+
}
|
|
3284
|
+
if (!socket.connected) {
|
|
3285
|
+
return false;
|
|
3286
|
+
}
|
|
3287
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
3288
|
+
storeState.setError("Input must be an object");
|
|
3289
|
+
return false;
|
|
3290
|
+
}
|
|
3291
|
+
const payload = controllerInputSchema.safeParse({
|
|
3292
|
+
roomId: parsedRoomId,
|
|
3293
|
+
controllerId: storeState.controllerId,
|
|
3294
|
+
input
|
|
3295
|
+
});
|
|
3296
|
+
if (!payload.success) {
|
|
3297
|
+
storeState.setError(payload.error.message);
|
|
3298
|
+
return false;
|
|
3299
|
+
}
|
|
3300
|
+
socket.emit("controller:input", payload.data);
|
|
3301
|
+
return true;
|
|
3302
|
+
},
|
|
3303
|
+
[parsedRoomId, socket, store]
|
|
3304
|
+
);
|
|
3305
|
+
const sendSystemCommand = useCallback(
|
|
3306
|
+
(command) => {
|
|
3307
|
+
const storeState = store.getState();
|
|
3308
|
+
if (!parsedRoomId || !storeState.controllerId || !socket) return;
|
|
3309
|
+
if (!socket.connected) return;
|
|
3310
|
+
const payload = controllerSystemSchema.safeParse({
|
|
3311
|
+
roomId: parsedRoomId,
|
|
3312
|
+
command
|
|
3313
|
+
});
|
|
3314
|
+
if (payload.success) {
|
|
3315
|
+
socket.emit("controller:system", payload.data);
|
|
3316
|
+
}
|
|
3317
|
+
},
|
|
3318
|
+
[parsedRoomId, socket, store]
|
|
3319
|
+
);
|
|
3320
|
+
return {
|
|
3321
|
+
roomId: parsedRoomId,
|
|
3322
|
+
controllerId: connectionState.controllerId,
|
|
3323
|
+
connectionStatus: connectionState.connectionStatus,
|
|
3324
|
+
lastError: connectionState.lastError,
|
|
3325
|
+
activeUrl,
|
|
3326
|
+
players: connectionState.players,
|
|
3327
|
+
gameState: connectionState.gameState,
|
|
3328
|
+
sendInput,
|
|
3329
|
+
sendSystemCommand
|
|
3330
|
+
};
|
|
3331
|
+
};
|
|
3332
|
+
var useGetInput = () => {
|
|
3333
|
+
const { inputManager } = useAirJamContext();
|
|
3334
|
+
return useCallback(
|
|
3335
|
+
(controllerId) => {
|
|
3336
|
+
if (!inputManager) {
|
|
3337
|
+
return void 0;
|
|
3338
|
+
}
|
|
3339
|
+
return inputManager.getInput(controllerId);
|
|
3340
|
+
},
|
|
3341
|
+
[inputManager]
|
|
3342
|
+
);
|
|
3343
|
+
};
|
|
3344
|
+
var useSendSignal = () => {
|
|
3345
|
+
const { getSocket } = useAirJamContext();
|
|
3346
|
+
const sendSignal = useCallback(
|
|
3347
|
+
(type, payload, targetId) => {
|
|
3348
|
+
const socket = getSocket("host");
|
|
3349
|
+
if (!socket || !socket.connected) {
|
|
3350
|
+
return;
|
|
3351
|
+
}
|
|
3352
|
+
const signal = {
|
|
3353
|
+
targetId,
|
|
3354
|
+
type,
|
|
3355
|
+
payload
|
|
3356
|
+
};
|
|
3357
|
+
socket.emit("host:signal", signal);
|
|
3358
|
+
},
|
|
3359
|
+
[getSocket]
|
|
3360
|
+
);
|
|
3361
|
+
return sendSignal;
|
|
3362
|
+
};
|
|
3363
|
+
|
|
3364
|
+
export { AirJamContext, AirJamOverlay, AirJamProvider, AudioManager, AudioProvider, Button, ChildEvents, ClientEvents, ControllerEvents, ControllerShell, DEFAULT_CONTROLLER_PATH, DEFAULT_MAX_PLAYERS, DEFAULT_SERVER_PORT, Events, HostEvents, INPUT_DEBOUNCE_MS, SOCKET_CONFIG, ServerEvents, Slider, SocketManager, SystemEvents, TOGGLE_DEBOUNCE_MS, UrlBuilder, VolumeControls, buttonVariants, createAudioManager, detectSoundCategory, generateControllerId, generateRoomCode, getLocalNetworkIp, urlBuilder, useAirJamConfig, useAirJamContext, useAirJamController, useAirJamHost, useAirJamShell, useAirJamSocket, useAirJamState, useAudio, useAudioLegacy, useAudioManager, useGetInput, useSendSignal, useVolumeStore };
|
|
3365
|
+
//# sourceMappingURL=index.js.map
|
|
3366
|
+
//# sourceMappingURL=index.js.map
|