@air-jam/sdk 0.1.1 → 0.1.3
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/dist/{chunk-7M5D3Y3L.js → chunk-B5DURUWT.js} +11 -3
- package/dist/chunk-B5DURUWT.js.map +1 -0
- package/dist/index.cjs +1733 -1783
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -66
- package/dist/index.d.ts +23 -66
- package/dist/index.js +1722 -1779
- package/dist/index.js.map +1 -1
- package/dist/protocol.cjs +10 -0
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.d.cts +19 -1
- package/dist/protocol.d.ts +19 -1
- package/dist/protocol.js +1 -1
- package/package.json +4 -1
- package/src/AirJamClient.ts +1 -552
- package/src/audio/audio-manager.ts +12 -1
- package/src/audio/volume-store.ts +47 -0
- package/src/components/controller-shell.tsx +201 -177
- package/src/components/{air-jam-overlay.tsx → host-shell.tsx} +59 -51
- package/src/components/qr-scanner-dialog.tsx +2 -2
- package/src/constants.ts +3 -2
- package/src/context/air-jam-context.tsx +3 -14
- package/src/hooks/use-air-jam-controller.ts +1 -1
- package/src/hooks/use-air-jam-host.ts +94 -49
- package/src/hooks/use-air-jam-shell.ts +36 -0
- package/src/index.ts +1 -3
- package/src/internal/input-manager.ts +28 -14
- package/src/protocol.ts +28 -0
- package/src/store/create-air-jam-store.ts +69 -7
- package/src/utils/url-builder.ts +3 -3
- package/dist/chunk-7M5D3Y3L.js.map +0 -1
- package/src/components/air-jam-debug.tsx +0 -77
- package/src/components/debug-state-dialog.tsx +0 -83
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from "lucide-react";
|
|
11
11
|
import QRCode from "qrcode";
|
|
12
12
|
import type { JSX } from "react";
|
|
13
|
-
import { useEffect, useMemo, useState } from "react";
|
|
13
|
+
import { useEffect, useMemo, useState, type ReactNode } from "react";
|
|
14
14
|
import { useAirJamHost } from "../hooks/use-air-jam-host";
|
|
15
15
|
import type { ConnectionStatus } from "../protocol";
|
|
16
16
|
import { PlayerAvatar } from "./player-avatar";
|
|
@@ -29,7 +29,11 @@ const statusCopy: Record<ConnectionStatus, string> = {
|
|
|
29
29
|
reconnecting: "Reconnecting…",
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
interface HostShellProps {
|
|
33
|
+
children: ReactNode;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const HostShell = ({ children }: HostShellProps): JSX.Element => {
|
|
33
37
|
const host = useAirJamHost();
|
|
34
38
|
const {
|
|
35
39
|
roomId,
|
|
@@ -149,64 +153,64 @@ export const AirJamOverlay = (): JSX.Element | null => {
|
|
|
149
153
|
|
|
150
154
|
// In Child Mode (Platform), we hide the overlay completely
|
|
151
155
|
// because the Platform handles the UI/Header.
|
|
152
|
-
if (isChildMode)
|
|
156
|
+
if (isChildMode) {
|
|
157
|
+
return <>{children}</>;
|
|
158
|
+
}
|
|
153
159
|
|
|
154
160
|
// Always render the navbar (unless child mode + playing)
|
|
155
161
|
return (
|
|
156
|
-
<div className="dark">
|
|
162
|
+
<div className="dark relative">
|
|
157
163
|
{/* Top Navbar - Always Visible */}
|
|
158
|
-
|
|
159
|
-
<div className="
|
|
160
|
-
<div className="
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
<div
|
|
164
|
-
<
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
</p>
|
|
171
|
-
</div>
|
|
164
|
+
<div className="pointer-events-none absolute inset-x-0 top-0 z-99999">
|
|
165
|
+
<div className="mx-auto w-full">
|
|
166
|
+
<div className="flex items-center justify-between px-4 py-2">
|
|
167
|
+
{/* Left: Room name */}
|
|
168
|
+
<div className="flex items-center gap-3">
|
|
169
|
+
<div>
|
|
170
|
+
<p className="text-muted-foreground text-xs tracking-[0.18em] uppercase">
|
|
171
|
+
Room
|
|
172
|
+
</p>
|
|
173
|
+
<p className="text-foreground text-lg font-semibold">
|
|
174
|
+
{roomId}
|
|
175
|
+
</p>
|
|
172
176
|
</div>
|
|
177
|
+
</div>
|
|
173
178
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
)}
|
|
187
|
-
</div>
|
|
188
|
-
)}
|
|
189
|
-
|
|
190
|
-
{/* Play/Pause button */}
|
|
191
|
-
<Button
|
|
192
|
-
type="button"
|
|
193
|
-
variant="ghost"
|
|
194
|
-
size="icon"
|
|
195
|
-
onClick={toggleGameState}
|
|
196
|
-
aria-label={gameState === "playing" ? "Pause" : "Play"}
|
|
197
|
-
className="h-8 w-8"
|
|
198
|
-
>
|
|
199
|
-
{gameState === "playing" ? (
|
|
200
|
-
<Pause className="h-4 w-4" />
|
|
201
|
-
) : (
|
|
202
|
-
<Play className="h-4 w-4" />
|
|
179
|
+
{/* Right: Avatar stack and play/pause */}
|
|
180
|
+
<div className="pointer-events-auto flex items-center gap-3">
|
|
181
|
+
{/* Avatar stack */}
|
|
182
|
+
{players.length > 0 && (
|
|
183
|
+
<div className="flex items-center -space-x-2">
|
|
184
|
+
{players.slice(0, 4).map((player) => (
|
|
185
|
+
<PlayerAvatar key={player.id} player={player} size="sm" />
|
|
186
|
+
))}
|
|
187
|
+
{players.length > 4 && (
|
|
188
|
+
<div className="border-background bg-muted flex h-8 w-8 items-center justify-center rounded-full border-2 text-xs font-medium">
|
|
189
|
+
+{players.length - 4}
|
|
190
|
+
</div>
|
|
203
191
|
)}
|
|
204
|
-
</
|
|
205
|
-
|
|
192
|
+
</div>
|
|
193
|
+
)}
|
|
194
|
+
|
|
195
|
+
{/* Play/Pause button */}
|
|
196
|
+
<Button
|
|
197
|
+
type="button"
|
|
198
|
+
variant="ghost"
|
|
199
|
+
size="icon"
|
|
200
|
+
onClick={toggleGameState}
|
|
201
|
+
aria-label={gameState === "playing" ? "Pause" : "Play"}
|
|
202
|
+
className="h-8 w-8"
|
|
203
|
+
>
|
|
204
|
+
{gameState === "playing" ? (
|
|
205
|
+
<Pause className="h-4 w-4" />
|
|
206
|
+
) : (
|
|
207
|
+
<Play className="h-4 w-4" />
|
|
208
|
+
)}
|
|
209
|
+
</Button>
|
|
206
210
|
</div>
|
|
207
211
|
</div>
|
|
208
212
|
</div>
|
|
209
|
-
|
|
213
|
+
</div>
|
|
210
214
|
|
|
211
215
|
{/* Paused Overlay - Only visible when paused */}
|
|
212
216
|
{gameState === "paused" && (
|
|
@@ -382,7 +386,7 @@ export const AirJamOverlay = (): JSX.Element | null => {
|
|
|
382
386
|
size="lg"
|
|
383
387
|
>
|
|
384
388
|
<Play className="mr-2 h-5 w-5" />
|
|
385
|
-
|
|
389
|
+
Play
|
|
386
390
|
</Button>
|
|
387
391
|
</div>
|
|
388
392
|
</div>
|
|
@@ -508,6 +512,10 @@ export const AirJamOverlay = (): JSX.Element | null => {
|
|
|
508
512
|
</div>
|
|
509
513
|
</div>
|
|
510
514
|
)}
|
|
515
|
+
|
|
516
|
+
{/* Game content - rendered as children */}
|
|
517
|
+
{children}
|
|
511
518
|
</div>
|
|
512
519
|
);
|
|
513
520
|
};
|
|
521
|
+
|
|
@@ -15,8 +15,8 @@ interface QRScannerDialogProps {
|
|
|
15
15
|
/**
|
|
16
16
|
* Extracts room code from a scanned URL or text.
|
|
17
17
|
* Supports formats like:
|
|
18
|
-
* - http://host/
|
|
19
|
-
* - https://host/
|
|
18
|
+
* - http://host/controller?room=CODE
|
|
19
|
+
* - https://host/controller?room=CODE
|
|
20
20
|
* - Just the room code itself
|
|
21
21
|
*/
|
|
22
22
|
const extractRoomCode = (text: string): string | null => {
|
package/src/constants.ts
CHANGED
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
export const DEFAULT_SERVER_PORT = 4000;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Standard path for controller endpoints.
|
|
12
|
+
* All Air Jam games use this convention - controller is always at {baseUrl}/controller
|
|
12
13
|
*/
|
|
13
|
-
export const
|
|
14
|
+
export const CONTROLLER_PATH = "/controller";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Input debounce time in milliseconds
|
|
@@ -54,7 +54,7 @@ import {
|
|
|
54
54
|
import type { z } from "zod";
|
|
55
55
|
import { useStore } from "zustand";
|
|
56
56
|
import { useShallow } from "zustand/react/shallow";
|
|
57
|
-
import {
|
|
57
|
+
import { DEFAULT_MAX_PLAYERS } from "../constants";
|
|
58
58
|
import { InputManager, type InputConfig } from "../internal/input-manager";
|
|
59
59
|
import type { ConnectionRole } from "../protocol";
|
|
60
60
|
import { createAirJamStore, type AirJamStore } from "../state/connection-store";
|
|
@@ -73,8 +73,6 @@ export interface AirJamConfig {
|
|
|
73
73
|
serverUrl?: string;
|
|
74
74
|
/** API key for production authentication */
|
|
75
75
|
apiKey?: string;
|
|
76
|
-
/** Path for controller page (default: /joypad) */
|
|
77
|
-
controllerPath: string;
|
|
78
76
|
/** Maximum players allowed in a room */
|
|
79
77
|
maxPlayers: number;
|
|
80
78
|
/** Public host for controller URLs (optional) */
|
|
@@ -98,7 +96,6 @@ export interface AirJamConfig {
|
|
|
98
96
|
* <AirJamProvider
|
|
99
97
|
* serverUrl="wss://your-server.com"
|
|
100
98
|
* apiKey="your-api-key"
|
|
101
|
-
* controllerPath="/controller"
|
|
102
99
|
* maxPlayers={4}
|
|
103
100
|
* input={{
|
|
104
101
|
* schema: myInputSchema,
|
|
@@ -124,12 +121,6 @@ export interface AirJamProviderProps<
|
|
|
124
121
|
* Falls back to VITE_AIR_JAM_API_KEY or NEXT_PUBLIC_AIR_JAM_API_KEY environment variables.
|
|
125
122
|
*/
|
|
126
123
|
apiKey?: string;
|
|
127
|
-
/**
|
|
128
|
-
* URL path for the controller page on your app.
|
|
129
|
-
* Controllers will be directed to this path with ?room=XXXX query parameter.
|
|
130
|
-
* @default "/joypad"
|
|
131
|
-
*/
|
|
132
|
-
controllerPath?: string;
|
|
133
124
|
/**
|
|
134
125
|
* Maximum number of players allowed in a room.
|
|
135
126
|
* @default 8
|
|
@@ -272,7 +263,7 @@ const getEnvApiKey = (): string | undefined => {
|
|
|
272
263
|
* <AirJamProvider>
|
|
273
264
|
* <Routes>
|
|
274
265
|
* <Route path="/" element={<HostView />} />
|
|
275
|
-
* <Route path="/
|
|
266
|
+
* <Route path="/controller" element={<ControllerView />} />
|
|
276
267
|
* </Routes>
|
|
277
268
|
* </AirJamProvider>
|
|
278
269
|
* );
|
|
@@ -309,7 +300,6 @@ export const AirJamProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
|
|
|
309
300
|
children,
|
|
310
301
|
serverUrl,
|
|
311
302
|
apiKey,
|
|
312
|
-
controllerPath = DEFAULT_CONTROLLER_PATH,
|
|
313
303
|
maxPlayers = DEFAULT_MAX_PLAYERS,
|
|
314
304
|
publicHost,
|
|
315
305
|
input,
|
|
@@ -319,11 +309,10 @@ export const AirJamProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
|
|
|
319
309
|
() => ({
|
|
320
310
|
serverUrl: serverUrl ?? getEnvServerUrl(),
|
|
321
311
|
apiKey: apiKey ?? getEnvApiKey(),
|
|
322
|
-
controllerPath,
|
|
323
312
|
maxPlayers,
|
|
324
313
|
publicHost,
|
|
325
314
|
}),
|
|
326
|
-
[serverUrl, apiKey,
|
|
315
|
+
[serverUrl, apiKey, maxPlayers, publicHost],
|
|
327
316
|
);
|
|
328
317
|
|
|
329
318
|
// Create InputManager from input config if provided
|
|
@@ -42,7 +42,7 @@ import { detectRunMode } from "../utils/mode";
|
|
|
42
42
|
*
|
|
43
43
|
* @example Basic usage (room from URL)
|
|
44
44
|
* ```tsx
|
|
45
|
-
* // URL: https://yourgame.com/
|
|
45
|
+
* // URL: https://yourgame.com/controller?room=ABCD
|
|
46
46
|
* const controller = useAirJamController();
|
|
47
47
|
* // Automatically joins room ABCD
|
|
48
48
|
* ```
|
|
@@ -34,10 +34,10 @@ import type {
|
|
|
34
34
|
import {
|
|
35
35
|
controllerStateSchema,
|
|
36
36
|
controllerSystemSchema,
|
|
37
|
-
|
|
37
|
+
hostCreateRoomSchema,
|
|
38
|
+
hostReconnectSchema,
|
|
38
39
|
roomCodeSchema,
|
|
39
40
|
} from "../protocol";
|
|
40
|
-
import { generateRoomCode } from "../utils/ids";
|
|
41
41
|
import { detectRunMode } from "../utils/mode";
|
|
42
42
|
import { urlBuilder } from "../utils/url-builder";
|
|
43
43
|
|
|
@@ -276,15 +276,24 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
276
276
|
// Always connect - forceConnect kept for API compatibility
|
|
277
277
|
const shouldConnect = true;
|
|
278
278
|
|
|
279
|
-
//
|
|
280
|
-
const
|
|
279
|
+
// Subscribe to store state to get roomId (server-issued for standalone mode)
|
|
280
|
+
const storeRoomId = useStore(store, (s) => s.roomId);
|
|
281
281
|
|
|
282
282
|
// Parse room ID from various sources
|
|
283
|
-
|
|
283
|
+
// For child mode: use arcadeParams.room (from URL)
|
|
284
|
+
// For standalone mode: use store.roomId (set after server responds)
|
|
285
|
+
const parsedRoomId = useMemo<RoomCode | null>(() => {
|
|
284
286
|
if (arcadeParams) {
|
|
287
|
+
// Child mode: room ID comes from URL params
|
|
285
288
|
return roomCodeSchema.parse(arcadeParams.room.toUpperCase());
|
|
286
289
|
}
|
|
287
290
|
|
|
291
|
+
// Standalone mode: use store.roomId (server-issued)
|
|
292
|
+
if (storeRoomId) {
|
|
293
|
+
return roomCodeSchema.parse(storeRoomId.toUpperCase());
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Fallback: check URL param or options (for reconnection scenarios)
|
|
288
297
|
if (typeof window !== "undefined") {
|
|
289
298
|
const params = new URLSearchParams(window.location.search);
|
|
290
299
|
const paramRoom = params.get("room");
|
|
@@ -298,15 +307,8 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
298
307
|
return roomCodeSchema.parse(options.roomId.toUpperCase());
|
|
299
308
|
}
|
|
300
309
|
|
|
301
|
-
return
|
|
302
|
-
}, [options.roomId,
|
|
303
|
-
|
|
304
|
-
// Update InputManager room ID when parsedRoomId changes
|
|
305
|
-
useEffect(() => {
|
|
306
|
-
if (inputManager) {
|
|
307
|
-
inputManager.setRoomId(parsedRoomId);
|
|
308
|
-
}
|
|
309
|
-
}, [inputManager, parsedRoomId]);
|
|
310
|
+
return null;
|
|
311
|
+
}, [options.roomId, storeRoomId, arcadeParams]);
|
|
310
312
|
|
|
311
313
|
const [joinUrl, setJoinUrl] = useState<string>("");
|
|
312
314
|
|
|
@@ -330,6 +332,7 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
330
332
|
players: state.players,
|
|
331
333
|
gameState: state.gameState,
|
|
332
334
|
mode: state.mode,
|
|
335
|
+
roomId: state.roomId,
|
|
333
336
|
})),
|
|
334
337
|
);
|
|
335
338
|
|
|
@@ -352,6 +355,8 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
352
355
|
return;
|
|
353
356
|
}
|
|
354
357
|
|
|
358
|
+
if (!parsedRoomId) return;
|
|
359
|
+
|
|
355
360
|
const payload = controllerSystemSchema.safeParse({
|
|
356
361
|
roomId: parsedRoomId,
|
|
357
362
|
command: "toggle_pause",
|
|
@@ -363,7 +368,7 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
363
368
|
|
|
364
369
|
const sendState = useCallback(
|
|
365
370
|
(state: ControllerStatePayload): boolean => {
|
|
366
|
-
if (!socket || !socket.connected) {
|
|
371
|
+
if (!socket || !socket.connected || !parsedRoomId) {
|
|
367
372
|
return false;
|
|
368
373
|
}
|
|
369
374
|
const payload = controllerStateSchema.safeParse({
|
|
@@ -407,14 +412,15 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
407
412
|
|
|
408
413
|
// Build controller URL
|
|
409
414
|
useEffect(() => {
|
|
415
|
+
if (!parsedRoomId) return; // Don't build URL until we have a room ID
|
|
416
|
+
|
|
410
417
|
(async () => {
|
|
411
418
|
const url = await urlBuilder.buildControllerUrl(parsedRoomId, {
|
|
412
|
-
path: config.controllerPath,
|
|
413
419
|
host: config.publicHost,
|
|
414
420
|
});
|
|
415
421
|
setJoinUrl(url);
|
|
416
422
|
})();
|
|
417
|
-
}, [parsedRoomId, config.
|
|
423
|
+
}, [parsedRoomId, config.publicHost, options.roomId]);
|
|
418
424
|
|
|
419
425
|
// Get setRegisteredRoomId from store
|
|
420
426
|
const setRegisteredRoomId = useStore(store, (s) => s.setRegisteredRoomId);
|
|
@@ -424,7 +430,11 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
424
430
|
const storeState = store.getState();
|
|
425
431
|
storeState.setMode(detectRunMode());
|
|
426
432
|
storeState.setRole("host");
|
|
427
|
-
|
|
433
|
+
// Don't set roomId here - it will be set after server responds
|
|
434
|
+
// Only set it if we have it (child mode or reconnection)
|
|
435
|
+
if (parsedRoomId) {
|
|
436
|
+
storeState.setRoomId(parsedRoomId);
|
|
437
|
+
}
|
|
428
438
|
storeState.setStatus("connecting");
|
|
429
439
|
storeState.setError(undefined);
|
|
430
440
|
|
|
@@ -436,14 +446,14 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
436
446
|
const registerHost = async () => {
|
|
437
447
|
const storeState = store.getState();
|
|
438
448
|
|
|
439
|
-
// Prevent duplicate registration
|
|
449
|
+
// Prevent duplicate registration
|
|
440
450
|
const currentRegisteredRoomId = store.getState().registeredRoomId;
|
|
441
|
-
if (currentRegisteredRoomId
|
|
451
|
+
if (currentRegisteredRoomId && socket.connected) {
|
|
442
452
|
return;
|
|
443
453
|
}
|
|
444
454
|
|
|
445
455
|
if (arcadeParams) {
|
|
446
|
-
// Child Mode - join as child
|
|
456
|
+
// Child Mode - join as child (unchanged)
|
|
447
457
|
const childRoomId = roomCodeSchema.parse(
|
|
448
458
|
arcadeParams.room.toUpperCase(),
|
|
449
459
|
);
|
|
@@ -463,28 +473,68 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
463
473
|
setRegisteredRoomId(childRoomId);
|
|
464
474
|
});
|
|
465
475
|
} else {
|
|
466
|
-
// Standalone Mode -
|
|
467
|
-
const
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
476
|
+
// Standalone Mode - use server-issued room IDs
|
|
477
|
+
const createNewRoom = () => {
|
|
478
|
+
const payload = hostCreateRoomSchema.parse({
|
|
479
|
+
maxPlayers: options.maxPlayers ?? config.maxPlayers,
|
|
480
|
+
apiKey: options.apiKey ?? config.apiKey,
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
socket.emit("host:createRoom", payload, (ack) => {
|
|
484
|
+
if (!ack.ok) {
|
|
485
|
+
storeState.setError(ack.message ?? "Failed to create room");
|
|
486
|
+
storeState.setStatus("disconnected");
|
|
487
|
+
setRegisteredRoomId(null);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (ack.roomId) {
|
|
492
|
+
storeState.setStatus("connected");
|
|
493
|
+
storeState.setRoomId(ack.roomId);
|
|
494
|
+
setRegisteredRoomId(ack.roomId);
|
|
495
|
+
|
|
496
|
+
// Save room ID to sessionStorage for reconnection on reload
|
|
497
|
+
if (typeof window !== "undefined") {
|
|
498
|
+
sessionStorage.setItem("airjam_room_id", ack.roomId);
|
|
499
|
+
}
|
|
500
|
+
} else {
|
|
501
|
+
storeState.setError("Server did not return room ID");
|
|
502
|
+
storeState.setStatus("disconnected");
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
};
|
|
472
506
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
507
|
+
// Check for saved room ID in sessionStorage (for reconnection)
|
|
508
|
+
if (typeof window !== "undefined") {
|
|
509
|
+
const savedRoomId = sessionStorage.getItem("airjam_room_id");
|
|
510
|
+
if (savedRoomId) {
|
|
511
|
+
// Attempt to reconnect to existing room
|
|
512
|
+
const reconnectPayload = hostReconnectSchema.parse({
|
|
513
|
+
roomId: savedRoomId,
|
|
514
|
+
apiKey: options.apiKey ?? config.apiKey,
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
socket.emit("host:reconnect", reconnectPayload, (ack) => {
|
|
518
|
+
if (ack.ok && ack.roomId) {
|
|
519
|
+
storeState.setStatus("connected");
|
|
520
|
+
storeState.setRoomId(ack.roomId);
|
|
521
|
+
setRegisteredRoomId(ack.roomId);
|
|
522
|
+
} else {
|
|
523
|
+
// Reconnection failed (room expired), create new room
|
|
524
|
+
if (typeof window !== "undefined") {
|
|
525
|
+
sessionStorage.removeItem("airjam_room_id");
|
|
526
|
+
}
|
|
527
|
+
createNewRoom();
|
|
528
|
+
}
|
|
529
|
+
});
|
|
484
530
|
} else {
|
|
485
|
-
|
|
531
|
+
// No saved room ID, create new room
|
|
532
|
+
createNewRoom();
|
|
486
533
|
}
|
|
487
|
-
}
|
|
534
|
+
} else {
|
|
535
|
+
// Not in browser environment, create new room
|
|
536
|
+
createNewRoom();
|
|
537
|
+
}
|
|
488
538
|
}
|
|
489
539
|
};
|
|
490
540
|
|
|
@@ -527,7 +577,7 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
527
577
|
};
|
|
528
578
|
|
|
529
579
|
const handleState = (payload: ControllerStateMessage): void => {
|
|
530
|
-
if (payload.roomId !== parsedRoomId) return;
|
|
580
|
+
if (!parsedRoomId || payload.roomId !== parsedRoomId) return;
|
|
531
581
|
|
|
532
582
|
const storeState = store.getState();
|
|
533
583
|
if (payload.state.gameState) {
|
|
@@ -556,15 +606,10 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
556
606
|
|
|
557
607
|
// If socket is already connected, register immediately
|
|
558
608
|
const currentRegisteredRoomId = store.getState().registeredRoomId;
|
|
559
|
-
if (socket.connected && currentRegisteredRoomId
|
|
609
|
+
if (socket.connected && !currentRegisteredRoomId) {
|
|
560
610
|
registerHost();
|
|
561
611
|
}
|
|
562
612
|
|
|
563
|
-
// Reset registered room when parsedRoomId changes
|
|
564
|
-
if (currentRegisteredRoomId && currentRegisteredRoomId !== parsedRoomId) {
|
|
565
|
-
setRegisteredRoomId(null);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
613
|
return () => {
|
|
569
614
|
socket.off("connect", handleConnect);
|
|
570
615
|
socket.off("disconnect", handleDisconnect);
|
|
@@ -580,8 +625,8 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
580
625
|
config.apiKey,
|
|
581
626
|
options.maxPlayers,
|
|
582
627
|
options.apiKey,
|
|
583
|
-
parsedRoomId,
|
|
584
628
|
arcadeParams,
|
|
629
|
+
parsedRoomId, // Include parsedRoomId since it's used in handleState callback
|
|
585
630
|
shouldConnect,
|
|
586
631
|
socket,
|
|
587
632
|
store,
|
|
@@ -606,7 +651,7 @@ export const useAirJamHost = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
|
606
651
|
);
|
|
607
652
|
|
|
608
653
|
return {
|
|
609
|
-
roomId: parsedRoomId,
|
|
654
|
+
roomId: parsedRoomId ?? ("" as RoomCode), // Return empty string as fallback if not yet available
|
|
610
655
|
joinUrl,
|
|
611
656
|
connectionStatus: connectionState.connectionStatus,
|
|
612
657
|
players: connectionState.players,
|
|
@@ -51,6 +51,10 @@ const getRoomFromLocation = (): string | null => {
|
|
|
51
51
|
return code ? code.toUpperCase() : null;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
+
// Throttling variables for logging
|
|
55
|
+
let lastSendInputLogTime = 0;
|
|
56
|
+
let lastSendInputFailLogTime = 0;
|
|
57
|
+
|
|
54
58
|
export const useAirJamShell = (
|
|
55
59
|
options: AirJamShellOptions = {},
|
|
56
60
|
): AirJamShellApi => {
|
|
@@ -256,6 +260,7 @@ export const useAirJamShell = (
|
|
|
256
260
|
socket.on("disconnect", handleDisconnect);
|
|
257
261
|
socket.on("server:welcome", handleWelcome);
|
|
258
262
|
socket.on("server:state", handleState);
|
|
263
|
+
|
|
259
264
|
socket.on("client:loadUi", handleLoadUi);
|
|
260
265
|
socket.on("client:unloadUi", handleUnloadUi);
|
|
261
266
|
socket.on("server:error", handleError);
|
|
@@ -280,12 +285,43 @@ export const useAirJamShell = (
|
|
|
280
285
|
const sendInput = useCallback(
|
|
281
286
|
(input: ControllerInputPayload): boolean => {
|
|
282
287
|
const storeState = store.getState();
|
|
288
|
+
const now = Date.now();
|
|
289
|
+
|
|
290
|
+
// Only log when there's actual user input (not just the loop sending zeros)
|
|
291
|
+
const hasActiveInput =
|
|
292
|
+
input.action === true ||
|
|
293
|
+
(typeof input.vector === "object" &&
|
|
294
|
+
input.vector !== null &&
|
|
295
|
+
(Math.abs((input.vector as { x?: number; y?: number }).x ?? 0) >
|
|
296
|
+
0.01 ||
|
|
297
|
+
Math.abs((input.vector as { x?: number; y?: number }).y ?? 0) >
|
|
298
|
+
0.01));
|
|
299
|
+
|
|
300
|
+
// Throttled logging - only log active input, once per second max
|
|
301
|
+
if (
|
|
302
|
+
hasActiveInput &&
|
|
303
|
+
(!lastSendInputLogTime || now - lastSendInputLogTime > 1000)
|
|
304
|
+
) {
|
|
305
|
+
lastSendInputLogTime = now;
|
|
306
|
+
}
|
|
283
307
|
|
|
284
308
|
if (!parsedRoomId || !storeState.controllerId || !socket) {
|
|
309
|
+
if (
|
|
310
|
+
!lastSendInputFailLogTime ||
|
|
311
|
+
now - lastSendInputFailLogTime > 1000
|
|
312
|
+
) {
|
|
313
|
+
lastSendInputFailLogTime = now;
|
|
314
|
+
}
|
|
285
315
|
storeState.setError("Not connected to a room");
|
|
286
316
|
return false;
|
|
287
317
|
}
|
|
288
318
|
if (!socket.connected) {
|
|
319
|
+
if (
|
|
320
|
+
!lastSendInputFailLogTime ||
|
|
321
|
+
now - lastSendInputFailLogTime > 1000
|
|
322
|
+
) {
|
|
323
|
+
lastSendInputFailLogTime = now;
|
|
324
|
+
}
|
|
289
325
|
return false;
|
|
290
326
|
}
|
|
291
327
|
|
package/src/index.ts
CHANGED
|
@@ -5,10 +5,8 @@ export * from "./context";
|
|
|
5
5
|
export * from "./audio";
|
|
6
6
|
|
|
7
7
|
// Components
|
|
8
|
-
export * from "./components/air-jam-debug";
|
|
9
|
-
export * from "./components/air-jam-overlay";
|
|
10
8
|
export * from "./components/controller-shell";
|
|
11
|
-
export * from "./components/
|
|
9
|
+
export * from "./components/host-shell";
|
|
12
10
|
export * from "./components/player-avatar";
|
|
13
11
|
export * from "./components/ui/button";
|
|
14
12
|
export * from "./components/ui/slider";
|
|
@@ -39,6 +39,9 @@
|
|
|
39
39
|
import type { z } from "zod";
|
|
40
40
|
import type { ControllerInputEvent } from "../protocol";
|
|
41
41
|
|
|
42
|
+
// Throttling for debug logging
|
|
43
|
+
let lastInputManagerLogTime = 0;
|
|
44
|
+
|
|
42
45
|
/**
|
|
43
46
|
* Configuration for input handling with optional validation and latching.
|
|
44
47
|
*
|
|
@@ -165,11 +168,9 @@ export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
165
168
|
private inputBuffer = new Map<string, Record<string, unknown>>();
|
|
166
169
|
private latchState = new Map<string, LatchState>();
|
|
167
170
|
private config: InputConfig<TSchema>;
|
|
168
|
-
private roomId?: string;
|
|
169
171
|
|
|
170
|
-
constructor(config: InputConfig<TSchema> = {}
|
|
172
|
+
constructor(config: InputConfig<TSchema> = {}) {
|
|
171
173
|
this.config = config;
|
|
172
|
-
this.roomId = roomId;
|
|
173
174
|
}
|
|
174
175
|
|
|
175
176
|
/**
|
|
@@ -177,13 +178,33 @@ export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
177
178
|
* Should be called when 'server:input' event is received.
|
|
178
179
|
*/
|
|
179
180
|
handleInput(payload: ControllerInputEvent): void {
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
181
|
+
// Note: We don't filter by roomId here because:
|
|
182
|
+
// 1. The server already routes input correctly to the right host
|
|
183
|
+
// 2. Multiple host instances might share the same InputManager instance
|
|
184
|
+
// 3. The roomId check was causing false rejections when hosts re-register
|
|
184
185
|
|
|
185
186
|
// Store raw input as-is (arbitrary structure)
|
|
186
187
|
this.inputBuffer.set(payload.controllerId, payload.input);
|
|
188
|
+
|
|
189
|
+
// Throttled logging - only log active input, once per second max
|
|
190
|
+
const now = Date.now();
|
|
191
|
+
const input = payload?.input;
|
|
192
|
+
const hasActiveInput =
|
|
193
|
+
input &&
|
|
194
|
+
(input.action === true ||
|
|
195
|
+
(typeof input.vector === "object" &&
|
|
196
|
+
input.vector !== null &&
|
|
197
|
+
(Math.abs((input.vector as { x?: number; y?: number }).x ?? 0) >
|
|
198
|
+
0.01 ||
|
|
199
|
+
Math.abs((input.vector as { x?: number; y?: number }).y ?? 0) >
|
|
200
|
+
0.01)));
|
|
201
|
+
|
|
202
|
+
if (
|
|
203
|
+
hasActiveInput &&
|
|
204
|
+
(!lastInputManagerLogTime || now - lastInputManagerLogTime > 1000)
|
|
205
|
+
) {
|
|
206
|
+
lastInputManagerLogTime = now;
|
|
207
|
+
}
|
|
187
208
|
}
|
|
188
209
|
|
|
189
210
|
/**
|
|
@@ -232,13 +253,6 @@ export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
232
253
|
this.latchState.delete(controllerId);
|
|
233
254
|
}
|
|
234
255
|
|
|
235
|
-
/**
|
|
236
|
-
* Updates the room ID filter.
|
|
237
|
-
*/
|
|
238
|
-
setRoomId(roomId: string | undefined): void {
|
|
239
|
-
this.roomId = roomId;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
256
|
/**
|
|
243
257
|
* Internal method to apply latching logic.
|
|
244
258
|
*/
|