@air-jam/sdk 0.1.0 → 0.1.2
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-XSXI3RIS.js → chunk-B5DURUWT.js} +11 -3
- package/dist/chunk-B5DURUWT.js.map +1 -0
- package/dist/index.cjs +2346 -1931
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +97 -41
- package/dist/index.d.ts +97 -41
- package/dist/index.js +2401 -2001
- 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 +42 -1
- package/dist/protocol.d.ts +42 -1
- package/dist/protocol.js +1 -1
- package/package.json +5 -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/air-jam-debug.tsx +77 -0
- package/src/components/air-jam-overlay.tsx +243 -186
- package/src/components/controller-shell.tsx +174 -182
- package/src/components/debug-state-dialog.tsx +83 -0
- package/src/components/player-avatar.tsx +64 -0
- package/src/components/qr-scanner-dialog.tsx +2 -2
- package/src/components/ui/button.tsx +1 -1
- package/src/components/ui/dialog.tsx +122 -0
- 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 +6 -0
- package/src/internal/input-manager.ts +28 -14
- package/src/protocol.ts +55 -0
- package/src/store/create-air-jam-store.ts +274 -0
- package/src/store/index.ts +1 -0
- package/src/utils/url-builder.ts +3 -3
- package/dist/chunk-XSXI3RIS.js.map +0 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
|
2
|
+
import { X } from "lucide-react";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { cn } from "../../utils/cn";
|
|
6
|
+
|
|
7
|
+
const Dialog = DialogPrimitive.Root;
|
|
8
|
+
|
|
9
|
+
const DialogTrigger = DialogPrimitive.Trigger;
|
|
10
|
+
|
|
11
|
+
const DialogPortal = DialogPrimitive.Portal;
|
|
12
|
+
|
|
13
|
+
const DialogClose = DialogPrimitive.Close;
|
|
14
|
+
|
|
15
|
+
const DialogOverlay = React.forwardRef<
|
|
16
|
+
React.ElementRef<typeof DialogPrimitive.Overlay>,
|
|
17
|
+
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
|
|
18
|
+
>(({ className, ...props }, ref) => (
|
|
19
|
+
<DialogPrimitive.Overlay
|
|
20
|
+
ref={ref}
|
|
21
|
+
className={cn(
|
|
22
|
+
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-99999 bg-black/80",
|
|
23
|
+
className,
|
|
24
|
+
)}
|
|
25
|
+
{...props}
|
|
26
|
+
/>
|
|
27
|
+
));
|
|
28
|
+
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
|
|
29
|
+
|
|
30
|
+
const DialogContent = React.forwardRef<
|
|
31
|
+
React.ElementRef<typeof DialogPrimitive.Content>,
|
|
32
|
+
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
|
|
33
|
+
>(({ className, children, ...props }, ref) => (
|
|
34
|
+
<DialogPortal>
|
|
35
|
+
<div className="dark">
|
|
36
|
+
<DialogOverlay />
|
|
37
|
+
<DialogPrimitive.Content
|
|
38
|
+
ref={ref}
|
|
39
|
+
className={cn(
|
|
40
|
+
"bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] fixed top-[50%] left-[50%] z-99999 w-full translate-x-[-50%] translate-y-[-50%] gap-4 rounded-xl border border-white p-6 shadow-lg duration-200",
|
|
41
|
+
className,
|
|
42
|
+
)}
|
|
43
|
+
{...props}
|
|
44
|
+
>
|
|
45
|
+
{children}
|
|
46
|
+
<DialogPrimitive.Close className="text-foreground ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground absolute top-4 right-4 rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-none disabled:pointer-events-none">
|
|
47
|
+
<X className="h-4 w-4" />
|
|
48
|
+
<span className="sr-only">Close</span>
|
|
49
|
+
</DialogPrimitive.Close>
|
|
50
|
+
</DialogPrimitive.Content>
|
|
51
|
+
</div>
|
|
52
|
+
</DialogPortal>
|
|
53
|
+
));
|
|
54
|
+
DialogContent.displayName = DialogPrimitive.Content.displayName;
|
|
55
|
+
|
|
56
|
+
const DialogHeader = ({
|
|
57
|
+
className,
|
|
58
|
+
...props
|
|
59
|
+
}: React.HTMLAttributes<HTMLDivElement>) => (
|
|
60
|
+
<div
|
|
61
|
+
className={cn(
|
|
62
|
+
"flex flex-col space-y-1.5 text-center sm:text-left",
|
|
63
|
+
className,
|
|
64
|
+
)}
|
|
65
|
+
{...props}
|
|
66
|
+
/>
|
|
67
|
+
);
|
|
68
|
+
DialogHeader.displayName = "DialogHeader";
|
|
69
|
+
|
|
70
|
+
const DialogFooter = ({
|
|
71
|
+
className,
|
|
72
|
+
...props
|
|
73
|
+
}: React.HTMLAttributes<HTMLDivElement>) => (
|
|
74
|
+
<div
|
|
75
|
+
className={cn(
|
|
76
|
+
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
|
|
77
|
+
className,
|
|
78
|
+
)}
|
|
79
|
+
{...props}
|
|
80
|
+
/>
|
|
81
|
+
);
|
|
82
|
+
DialogFooter.displayName = "DialogFooter";
|
|
83
|
+
|
|
84
|
+
const DialogTitle = React.forwardRef<
|
|
85
|
+
React.ElementRef<typeof DialogPrimitive.Title>,
|
|
86
|
+
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
|
|
87
|
+
>(({ className, ...props }, ref) => (
|
|
88
|
+
<DialogPrimitive.Title
|
|
89
|
+
ref={ref}
|
|
90
|
+
className={cn(
|
|
91
|
+
"text-lg leading-none font-semibold tracking-tight",
|
|
92
|
+
className,
|
|
93
|
+
)}
|
|
94
|
+
{...props}
|
|
95
|
+
/>
|
|
96
|
+
));
|
|
97
|
+
DialogTitle.displayName = DialogPrimitive.Title.displayName;
|
|
98
|
+
|
|
99
|
+
const DialogDescription = React.forwardRef<
|
|
100
|
+
React.ElementRef<typeof DialogPrimitive.Description>,
|
|
101
|
+
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
|
|
102
|
+
>(({ className, ...props }, ref) => (
|
|
103
|
+
<DialogPrimitive.Description
|
|
104
|
+
ref={ref}
|
|
105
|
+
className={cn("text-muted-foreground text-sm", className)}
|
|
106
|
+
{...props}
|
|
107
|
+
/>
|
|
108
|
+
));
|
|
109
|
+
DialogDescription.displayName = DialogPrimitive.Description.displayName;
|
|
110
|
+
|
|
111
|
+
export {
|
|
112
|
+
Dialog,
|
|
113
|
+
DialogClose,
|
|
114
|
+
DialogContent,
|
|
115
|
+
DialogDescription,
|
|
116
|
+
DialogFooter,
|
|
117
|
+
DialogHeader,
|
|
118
|
+
DialogOverlay,
|
|
119
|
+
DialogPortal,
|
|
120
|
+
DialogTitle,
|
|
121
|
+
DialogTrigger,
|
|
122
|
+
};
|
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,8 +5,11 @@ export * from "./context";
|
|
|
5
5
|
export * from "./audio";
|
|
6
6
|
|
|
7
7
|
// Components
|
|
8
|
+
export * from "./components/air-jam-debug";
|
|
8
9
|
export * from "./components/air-jam-overlay";
|
|
9
10
|
export * from "./components/controller-shell";
|
|
11
|
+
export * from "./components/debug-state-dialog";
|
|
12
|
+
export * from "./components/player-avatar";
|
|
10
13
|
export * from "./components/ui/button";
|
|
11
14
|
export * from "./components/ui/slider";
|
|
12
15
|
export * from "./components/volume-controls";
|
|
@@ -27,6 +30,9 @@ export * from "./hooks/use-send-signal";
|
|
|
27
30
|
// Protocol types
|
|
28
31
|
export * from "./protocol";
|
|
29
32
|
|
|
33
|
+
// Store
|
|
34
|
+
export * from "./store/create-air-jam-store";
|
|
35
|
+
|
|
30
36
|
// Utilities
|
|
31
37
|
export * from "./utils/ids";
|
|
32
38
|
export * from "./utils/network-ip";
|
|
@@ -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
|
*/
|