@air-jam/sdk 0.1.1 → 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/index.cjs CHANGED
@@ -49,7 +49,7 @@ var SliderPrimitive__namespace = /*#__PURE__*/_interopNamespace(SliderPrimitive)
49
49
 
50
50
  // src/constants.ts
51
51
  var DEFAULT_SERVER_PORT = 4e3;
52
- var DEFAULT_CONTROLLER_PATH = "/joypad";
52
+ var CONTROLLER_PATH = "/controller";
53
53
  var INPUT_DEBOUNCE_MS = 200;
54
54
  var TOGGLE_DEBOUNCE_MS = 300;
55
55
  var SOCKET_CONFIG = {
@@ -63,26 +63,21 @@ var SOCKET_CONFIG = {
63
63
  PING_TIMEOUT: 5e3
64
64
  };
65
65
  var DEFAULT_MAX_PLAYERS = 8;
66
-
67
- // src/internal/input-manager.ts
68
66
  var InputManager = class {
69
67
  inputBuffer = /* @__PURE__ */ new Map();
70
68
  latchState = /* @__PURE__ */ new Map();
71
69
  config;
72
- roomId;
73
- constructor(config = {}, roomId) {
70
+ constructor(config = {}) {
74
71
  this.config = config;
75
- this.roomId = roomId;
76
72
  }
77
73
  /**
78
74
  * Handles incoming input events from the socket.
79
75
  * Should be called when 'server:input' event is received.
80
76
  */
81
77
  handleInput(payload) {
82
- if (this.roomId && payload.roomId !== this.roomId) {
83
- return;
84
- }
85
78
  this.inputBuffer.set(payload.controllerId, payload.input);
79
+ const input = payload?.input;
80
+ input && (input.action === true || typeof input.vector === "object" && input.vector !== null && (Math.abs(input.vector.x ?? 0) > 0.01 || Math.abs(input.vector.y ?? 0) > 0.01));
86
81
  }
87
82
  /**
88
83
  * Gets input for a specific controller.
@@ -121,12 +116,6 @@ var InputManager = class {
121
116
  this.inputBuffer.delete(controllerId);
122
117
  this.latchState.delete(controllerId);
123
118
  }
124
- /**
125
- * Updates the room ID filter.
126
- */
127
- setRoomId(roomId) {
128
- this.roomId = roomId;
129
- }
130
119
  /**
131
120
  * Internal method to apply latching logic.
132
121
  */
@@ -350,7 +339,6 @@ var AirJamProvider = ({
350
339
  children,
351
340
  serverUrl,
352
341
  apiKey,
353
- controllerPath = DEFAULT_CONTROLLER_PATH,
354
342
  maxPlayers = DEFAULT_MAX_PLAYERS,
355
343
  publicHost,
356
344
  input
@@ -359,11 +347,10 @@ var AirJamProvider = ({
359
347
  () => ({
360
348
  serverUrl: serverUrl ?? getEnvServerUrl(),
361
349
  apiKey: apiKey ?? getEnvApiKey(),
362
- controllerPath,
363
350
  maxPlayers,
364
351
  publicHost
365
352
  }),
366
- [serverUrl, apiKey, controllerPath, maxPlayers, publicHost]
353
+ [serverUrl, apiKey, maxPlayers, publicHost]
367
354
  );
368
355
  const inputManager = React.useMemo(() => {
369
356
  if (!input) {
@@ -480,6 +467,22 @@ var useVolumeStore = zustand.create((set, get) => ({
480
467
  return masterVolume * categoryVolume;
481
468
  }
482
469
  }));
470
+ var setupParentSettingsListener = () => {
471
+ if (typeof window === "undefined") return;
472
+ const isInIframe = window.parent !== window;
473
+ if (!isInIframe) return;
474
+ const handleMessage = (event) => {
475
+ if (event.source !== window.parent) return;
476
+ if (event.data?.type !== "AIRJAM_SETTINGS_SYNC") return;
477
+ const { masterVolume, musicVolume, sfxVolume } = event.data.payload;
478
+ const store = useVolumeStore.getState();
479
+ if (typeof masterVolume === "number") store.setMasterVolume(masterVolume);
480
+ if (typeof musicVolume === "number") store.setMusicVolume(musicVolume);
481
+ if (typeof sfxVolume === "number") store.setSfxVolume(sfxVolume);
482
+ };
483
+ window.addEventListener("message", handleMessage);
484
+ };
485
+ setupParentSettingsListener();
483
486
 
484
487
  // src/audio/audio-manager.ts
485
488
  function detectSoundCategory(config) {
@@ -548,7 +551,12 @@ var AudioManager = class {
548
551
  if (roomId) this.roomId = roomId;
549
552
  }
550
553
  /**
551
- * Initialize audio context (must be called on user interaction)
554
+ * Resume audio context if suspended. This is automatically called by play(),
555
+ * so you typically don't need to call this manually.
556
+ *
557
+ * Note: Due to browser autoplay policies, audio context starts suspended
558
+ * and can only be resumed in response to a user gesture. The useAudio hook
559
+ * handles this automatically, and play() also calls init() internally.
552
560
  */
553
561
  init() {
554
562
  if (howler.Howler.ctx && howler.Howler.ctx.state === "suspended") {
@@ -592,6 +600,7 @@ var AudioManager = class {
592
600
  sprite,
593
601
  pitch
594
602
  } = options || {};
603
+ this.init();
595
604
  if (remote) {
596
605
  this.playRemote(id, target, volume, loop);
597
606
  return null;
@@ -649,6 +658,7 @@ var AudioManager = class {
649
658
  * Play a sound at a specific 3D position
650
659
  */
651
660
  playSpatial(id, pos, sprite) {
661
+ this.init();
652
662
  const soundId = this.playLocal(id, void 0, void 0, sprite);
653
663
  if (soundId !== null) {
654
664
  const sound = this.sounds.get(id);
@@ -1083,31 +1093,16 @@ var hostJoinAsChildSchema = zod.z.object({
1083
1093
  roomId: roomCodeSchema,
1084
1094
  joinToken: zod.z.string()
1085
1095
  });
1096
+ var hostCreateRoomSchema = zod.z.object({
1097
+ maxPlayers: zod.z.number().int().min(1).max(16).default(8),
1098
+ apiKey: zod.z.string().optional()
1099
+ });
1100
+ var hostReconnectSchema = zod.z.object({
1101
+ roomId: roomCodeSchema,
1102
+ apiKey: zod.z.string().optional()
1103
+ });
1086
1104
  var AIRJAM_PROXY_PREFIX = "AIRJAM_";
1087
1105
 
1088
- // src/utils/ids.ts
1089
- var alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
1090
- var generateRoomCode = () => {
1091
- const array = new Uint32Array(4);
1092
- if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
1093
- crypto.getRandomValues(array);
1094
- } else {
1095
- for (let i = 0; i < array.length; i += 1) {
1096
- array[i] = Math.floor(Math.random() * alphabet.length);
1097
- }
1098
- }
1099
- const code = Array.from(
1100
- array,
1101
- (value) => alphabet[value % alphabet.length]
1102
- ).join("");
1103
- return roomCodeSchema.parse(code);
1104
- };
1105
- var generateControllerId = () => {
1106
- const stamp = Math.floor(Date.now() % 1e5).toString().padStart(5, "0");
1107
- const random = alphabet[Math.floor(Math.random() * alphabet.length)];
1108
- return `C${random}${stamp}`;
1109
- };
1110
-
1111
1106
  // src/utils/mode.ts
1112
1107
  var detectRunMode = () => {
1113
1108
  if (typeof window === "undefined") {
@@ -1271,10 +1266,11 @@ var UrlBuilder = class {
1271
1266
  }
1272
1267
  /**
1273
1268
  * Build a controller URL with room code.
1269
+ * Controller is always at {baseUrl}/controller (standardized convention).
1274
1270
  * Automatically uses local IP when running on localhost.
1275
1271
  */
1276
1272
  async buildControllerUrl(roomId, options) {
1277
- const path = options?.path || DEFAULT_CONTROLLER_PATH;
1273
+ const path = CONTROLLER_PATH;
1278
1274
  if (options?.host) {
1279
1275
  const url2 = new URL(path, this.normalizeOrigin(options.host));
1280
1276
  url2.searchParams.set("room", roomId);
@@ -1349,11 +1345,14 @@ var useAirJamHost = (options = {}) => {
1349
1345
  }, []);
1350
1346
  const isChildMode = !!arcadeParams;
1351
1347
  const shouldConnect = true;
1352
- const [fallbackRoomId] = React.useState(() => generateRoomCode());
1348
+ const storeRoomId = zustand.useStore(store, (s) => s.roomId);
1353
1349
  const parsedRoomId = React.useMemo(() => {
1354
1350
  if (arcadeParams) {
1355
1351
  return roomCodeSchema.parse(arcadeParams.room.toUpperCase());
1356
1352
  }
1353
+ if (storeRoomId) {
1354
+ return roomCodeSchema.parse(storeRoomId.toUpperCase());
1355
+ }
1357
1356
  if (typeof window !== "undefined") {
1358
1357
  const params = new URLSearchParams(window.location.search);
1359
1358
  const paramRoom = params.get("room");
@@ -1365,13 +1364,8 @@ var useAirJamHost = (options = {}) => {
1365
1364
  if (options.roomId) {
1366
1365
  return roomCodeSchema.parse(options.roomId.toUpperCase());
1367
1366
  }
1368
- return fallbackRoomId;
1369
- }, [options.roomId, fallbackRoomId, arcadeParams]);
1370
- React.useEffect(() => {
1371
- if (inputManager) {
1372
- inputManager.setRoomId(parsedRoomId);
1373
- }
1374
- }, [inputManager, parsedRoomId]);
1367
+ return null;
1368
+ }, [options.roomId, storeRoomId, arcadeParams]);
1375
1369
  const [joinUrl, setJoinUrl] = React.useState("");
1376
1370
  const onPlayerJoinRef = React.useRef(options.onPlayerJoin);
1377
1371
  const onPlayerLeaveRef = React.useRef(options.onPlayerLeave);
@@ -1388,7 +1382,8 @@ var useAirJamHost = (options = {}) => {
1388
1382
  lastError: state.lastError,
1389
1383
  players: state.players,
1390
1384
  gameState: state.gameState,
1391
- mode: state.mode
1385
+ mode: state.mode,
1386
+ roomId: state.roomId
1392
1387
  }))
1393
1388
  );
1394
1389
  const socket = React.useMemo(
@@ -1405,6 +1400,7 @@ var useAirJamHost = (options = {}) => {
1405
1400
  if (!socket || !socket.connected) {
1406
1401
  return;
1407
1402
  }
1403
+ if (!parsedRoomId) return;
1408
1404
  const payload = controllerSystemSchema.safeParse({
1409
1405
  roomId: parsedRoomId,
1410
1406
  command: "toggle_pause"
@@ -1415,7 +1411,7 @@ var useAirJamHost = (options = {}) => {
1415
1411
  }, [socket, parsedRoomId, lastToggle]);
1416
1412
  const sendState = React.useCallback(
1417
1413
  (state) => {
1418
- if (!socket || !socket.connected) {
1414
+ if (!socket || !socket.connected || !parsedRoomId) {
1419
1415
  return false;
1420
1416
  }
1421
1417
  const payload = controllerStateSchema.safeParse({
@@ -1451,20 +1447,22 @@ var useAirJamHost = (options = {}) => {
1451
1447
  }
1452
1448
  }, [socket, disconnectSocket]);
1453
1449
  React.useEffect(() => {
1450
+ if (!parsedRoomId) return;
1454
1451
  (async () => {
1455
1452
  const url = await urlBuilder.buildControllerUrl(parsedRoomId, {
1456
- path: config.controllerPath,
1457
1453
  host: config.publicHost
1458
1454
  });
1459
1455
  setJoinUrl(url);
1460
1456
  })();
1461
- }, [parsedRoomId, config.controllerPath, config.publicHost]);
1457
+ }, [parsedRoomId, config.publicHost, options.roomId]);
1462
1458
  const setRegisteredRoomId = zustand.useStore(store, (s) => s.setRegisteredRoomId);
1463
1459
  React.useEffect(() => {
1464
1460
  const storeState = store.getState();
1465
1461
  storeState.setMode(detectRunMode());
1466
1462
  storeState.setRole("host");
1467
- storeState.setRoomId(parsedRoomId);
1463
+ if (parsedRoomId) {
1464
+ storeState.setRoomId(parsedRoomId);
1465
+ }
1468
1466
  storeState.setStatus("connecting");
1469
1467
  storeState.setError(void 0);
1470
1468
  if (!socket) {
@@ -1474,7 +1472,7 @@ var useAirJamHost = (options = {}) => {
1474
1472
  const registerHost = async () => {
1475
1473
  const storeState2 = store.getState();
1476
1474
  const currentRegisteredRoomId2 = store.getState().registeredRoomId;
1477
- if (currentRegisteredRoomId2 === parsedRoomId && socket.connected) {
1475
+ if (currentRegisteredRoomId2 && socket.connected) {
1478
1476
  return;
1479
1477
  }
1480
1478
  if (arcadeParams) {
@@ -1497,26 +1495,56 @@ var useAirJamHost = (options = {}) => {
1497
1495
  setRegisteredRoomId(childRoomId);
1498
1496
  });
1499
1497
  } else {
1500
- const payload = hostRegistrationSchema.parse({
1501
- roomId: parsedRoomId,
1502
- maxPlayers: options.maxPlayers ?? config.maxPlayers,
1503
- apiKey: options.apiKey ?? config.apiKey
1504
- });
1505
- socket.emit("host:register", payload, (ack) => {
1506
- if (!ack.ok) {
1507
- storeState2.setError(ack.message ?? "Failed to register host");
1508
- storeState2.setStatus("disconnected");
1509
- setRegisteredRoomId(null);
1510
- return;
1511
- }
1512
- storeState2.setStatus("connected");
1513
- if (ack.roomId) {
1514
- storeState2.setRoomId(ack.roomId);
1515
- setRegisteredRoomId(ack.roomId);
1498
+ const createNewRoom = () => {
1499
+ const payload = hostCreateRoomSchema.parse({
1500
+ maxPlayers: options.maxPlayers ?? config.maxPlayers,
1501
+ apiKey: options.apiKey ?? config.apiKey
1502
+ });
1503
+ socket.emit("host:createRoom", payload, (ack) => {
1504
+ if (!ack.ok) {
1505
+ storeState2.setError(ack.message ?? "Failed to create room");
1506
+ storeState2.setStatus("disconnected");
1507
+ setRegisteredRoomId(null);
1508
+ return;
1509
+ }
1510
+ if (ack.roomId) {
1511
+ storeState2.setStatus("connected");
1512
+ storeState2.setRoomId(ack.roomId);
1513
+ setRegisteredRoomId(ack.roomId);
1514
+ if (typeof window !== "undefined") {
1515
+ sessionStorage.setItem("airjam_room_id", ack.roomId);
1516
+ }
1517
+ } else {
1518
+ storeState2.setError("Server did not return room ID");
1519
+ storeState2.setStatus("disconnected");
1520
+ }
1521
+ });
1522
+ };
1523
+ if (typeof window !== "undefined") {
1524
+ const savedRoomId = sessionStorage.getItem("airjam_room_id");
1525
+ if (savedRoomId) {
1526
+ const reconnectPayload = hostReconnectSchema.parse({
1527
+ roomId: savedRoomId,
1528
+ apiKey: options.apiKey ?? config.apiKey
1529
+ });
1530
+ socket.emit("host:reconnect", reconnectPayload, (ack) => {
1531
+ if (ack.ok && ack.roomId) {
1532
+ storeState2.setStatus("connected");
1533
+ storeState2.setRoomId(ack.roomId);
1534
+ setRegisteredRoomId(ack.roomId);
1535
+ } else {
1536
+ if (typeof window !== "undefined") {
1537
+ sessionStorage.removeItem("airjam_room_id");
1538
+ }
1539
+ createNewRoom();
1540
+ }
1541
+ });
1516
1542
  } else {
1517
- setRegisteredRoomId(parsedRoomId);
1543
+ createNewRoom();
1518
1544
  }
1519
- });
1545
+ } else {
1546
+ createNewRoom();
1547
+ }
1520
1548
  }
1521
1549
  };
1522
1550
  const handleConnect = () => {
@@ -1547,7 +1575,7 @@ var useAirJamHost = (options = {}) => {
1547
1575
  onChildCloseRef.current?.();
1548
1576
  };
1549
1577
  const handleState = (payload) => {
1550
- if (payload.roomId !== parsedRoomId) return;
1578
+ if (!parsedRoomId || payload.roomId !== parsedRoomId) return;
1551
1579
  const storeState2 = store.getState();
1552
1580
  if (payload.state.gameState) {
1553
1581
  storeState2.setGameState(payload.state.gameState);
@@ -1569,12 +1597,9 @@ var useAirJamHost = (options = {}) => {
1569
1597
  socket.on("server:state", handleState);
1570
1598
  socket.connect();
1571
1599
  const currentRegisteredRoomId = store.getState().registeredRoomId;
1572
- if (socket.connected && currentRegisteredRoomId !== parsedRoomId) {
1600
+ if (socket.connected && !currentRegisteredRoomId) {
1573
1601
  registerHost();
1574
1602
  }
1575
- if (currentRegisteredRoomId && currentRegisteredRoomId !== parsedRoomId) {
1576
- setRegisteredRoomId(null);
1577
- }
1578
1603
  return () => {
1579
1604
  socket.off("connect", handleConnect);
1580
1605
  socket.off("disconnect", handleDisconnect);
@@ -1590,8 +1615,9 @@ var useAirJamHost = (options = {}) => {
1590
1615
  config.apiKey,
1591
1616
  options.maxPlayers,
1592
1617
  options.apiKey,
1593
- parsedRoomId,
1594
1618
  arcadeParams,
1619
+ parsedRoomId,
1620
+ // Include parsedRoomId since it's used in handleState callback
1595
1621
  shouldConnect,
1596
1622
  socket,
1597
1623
  store,
@@ -1609,7 +1635,8 @@ var useAirJamHost = (options = {}) => {
1609
1635
  [inputManager]
1610
1636
  );
1611
1637
  return {
1612
- roomId: parsedRoomId,
1638
+ roomId: parsedRoomId ?? "",
1639
+ // Return empty string as fallback if not yet available
1613
1640
  joinUrl,
1614
1641
  connectionStatus: connectionState.connectionStatus,
1615
1642
  players: connectionState.players,
@@ -2273,7 +2300,7 @@ var AirJamOverlay = () => {
2273
2300
  size: "lg",
2274
2301
  children: [
2275
2302
  /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Play, { className: "mr-2 h-5 w-5" }),
2276
- "Start Game"
2303
+ "Play"
2277
2304
  ]
2278
2305
  }
2279
2306
  ) })
@@ -2747,7 +2774,7 @@ var describeStatus = (status) => {
2747
2774
  }
2748
2775
  };
2749
2776
  var orientationMatches = (required) => {
2750
- if (required === "any") return true;
2777
+ if (!required) return true;
2751
2778
  if (typeof window === "undefined" || !window.matchMedia) return true;
2752
2779
  const matcher = window.matchMedia(`(orientation: ${required})`);
2753
2780
  return matcher.matches;
@@ -2755,7 +2782,7 @@ var orientationMatches = (required) => {
2755
2782
  var ControllerShell = ({
2756
2783
  roomId,
2757
2784
  connectionStatus,
2758
- requiredOrientation = "landscape",
2785
+ forceOrientation,
2759
2786
  children,
2760
2787
  gameState = "paused",
2761
2788
  onTogglePlayPause,
@@ -2765,7 +2792,7 @@ var ControllerShell = ({
2765
2792
  }) => {
2766
2793
  const isChildMode = React.useMemo(() => detectRunMode() === "platform", []);
2767
2794
  const [isOrientationOk, setOrientationOk] = React.useState(
2768
- () => orientationMatches(requiredOrientation)
2795
+ () => orientationMatches(forceOrientation)
2769
2796
  );
2770
2797
  const [isScannerOpen, setIsScannerOpen] = React.useState(false);
2771
2798
  const { isFullscreen, toggleFullscreen } = useFullscreen();
@@ -2786,16 +2813,17 @@ var ControllerShell = ({
2786
2813
  return found || null;
2787
2814
  }, [controllerId, players, connectionStatus]);
2788
2815
  React.useEffect(() => {
2789
- if (requiredOrientation === "any") {
2816
+ if (!forceOrientation) {
2817
+ setOrientationOk(true);
2790
2818
  return;
2791
2819
  }
2792
- const media = window.matchMedia(`(orientation: ${requiredOrientation})`);
2820
+ const media = window.matchMedia(`(orientation: ${forceOrientation})`);
2793
2821
  const handleChange = () => setOrientationOk(media.matches);
2794
2822
  handleChange();
2795
2823
  media.addEventListener("change", handleChange);
2796
2824
  return () => media.removeEventListener("change", handleChange);
2797
- }, [requiredOrientation]);
2798
- const orientationOk = requiredOrientation === "any" ? true : isOrientationOk;
2825
+ }, [forceOrientation]);
2826
+ const orientationOk = !forceOrientation ? true : isOrientationOk;
2799
2827
  const statusDotColor = React.useMemo(() => {
2800
2828
  switch (connectionStatus) {
2801
2829
  case "connected":
@@ -2814,7 +2842,7 @@ var ControllerShell = ({
2814
2842
  /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-card-foreground text-xl font-semibold", children: "Rotate your device" }),
2815
2843
  /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "text-muted-foreground mt-2 text-sm", children: [
2816
2844
  "This game is best experienced in ",
2817
- requiredOrientation,
2845
+ forceOrientation,
2818
2846
  " ",
2819
2847
  "orientation."
2820
2848
  ] })
@@ -2834,36 +2862,43 @@ var ControllerShell = ({
2834
2862
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "dark", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-background text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden select-none", children: [
2835
2863
  /* @__PURE__ */ jsxRuntime.jsxs("header", { className: "pointer-events-none sticky top-0 z-50 flex items-center justify-between border-b px-6 py-2", children: [
2836
2864
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "pointer-events-auto flex items-center gap-3", children: [
2837
- typeof window !== "undefined" && (currentPlayer || controllerId) && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "relative", title: describeStatus(connectionStatus), children: [
2838
- currentPlayer ? /* @__PURE__ */ jsxRuntime.jsx(PlayerAvatar, { player: currentPlayer, size: "sm" }) : controllerId ? /* @__PURE__ */ jsxRuntime.jsx(
2839
- PlayerAvatar,
2840
- {
2841
- player: {
2842
- id: controllerId,
2843
- label: "Player"
2844
- },
2845
- size: "sm"
2846
- }
2847
- ) : null,
2848
- /* @__PURE__ */ jsxRuntime.jsx(
2849
- "span",
2850
- {
2851
- className: "absolute right-0 bottom-0 h-3 w-3 rounded-full bg-black",
2852
- "aria-hidden": "true"
2853
- }
2854
- ),
2855
- /* @__PURE__ */ jsxRuntime.jsx(
2856
- "span",
2857
- {
2858
- className: cn(
2859
- "absolute right-0.5 bottom-0.5 h-2 w-2 rounded-full",
2860
- statusDotColor,
2861
- connectionStatus === "connected" && "animate-pulse"
2865
+ typeof window !== "undefined" && (currentPlayer || controllerId) && /* @__PURE__ */ jsxRuntime.jsxs(
2866
+ "div",
2867
+ {
2868
+ className: "relative",
2869
+ title: describeStatus(connectionStatus),
2870
+ children: [
2871
+ currentPlayer ? /* @__PURE__ */ jsxRuntime.jsx(PlayerAvatar, { player: currentPlayer, size: "sm" }) : controllerId ? /* @__PURE__ */ jsxRuntime.jsx(
2872
+ PlayerAvatar,
2873
+ {
2874
+ player: {
2875
+ id: controllerId,
2876
+ label: "Player"
2877
+ },
2878
+ size: "sm"
2879
+ }
2880
+ ) : null,
2881
+ /* @__PURE__ */ jsxRuntime.jsx(
2882
+ "span",
2883
+ {
2884
+ className: "absolute right-0 bottom-0 h-3 w-3 rounded-full bg-black",
2885
+ "aria-hidden": "true"
2886
+ }
2862
2887
  ),
2863
- "aria-label": describeStatus(connectionStatus)
2864
- }
2865
- )
2866
- ] }),
2888
+ /* @__PURE__ */ jsxRuntime.jsx(
2889
+ "span",
2890
+ {
2891
+ className: cn(
2892
+ "absolute right-0.5 bottom-0.5 h-2 w-2 rounded-full",
2893
+ statusDotColor,
2894
+ connectionStatus === "connected" && "animate-pulse"
2895
+ ),
2896
+ "aria-label": describeStatus(connectionStatus)
2897
+ }
2898
+ )
2899
+ ]
2900
+ }
2901
+ ),
2867
2902
  /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
2868
2903
  /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground text-xs tracking-[0.24em] uppercase", children: "Room" }),
2869
2904
  /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-foreground text-lg font-semibold", children: typeof window !== "undefined" ? roomId ?? "N/A" : "N/A" })
@@ -2930,13 +2965,12 @@ var ControllerShell = ({
2930
2965
  )
2931
2966
  ] })
2932
2967
  ] }),
2933
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "pointer-events-none fixed right-4 bottom-4 z-50", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "pointer-events-auto", children: /* @__PURE__ */ jsxRuntime.jsx(VolumeControls, { compact: true }) }) }),
2934
2968
  /* @__PURE__ */ jsxRuntime.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__ */ jsxRuntime.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: [
2935
- !orientationOk && /* @__PURE__ */ jsxRuntime.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: [
2969
+ !orientationOk && forceOrientation && /* @__PURE__ */ jsxRuntime.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: [
2936
2970
  /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-card-foreground text-xl font-semibold", children: "Rotate your device" }),
2937
2971
  /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "text-muted-foreground mt-2 text-sm", children: [
2938
2972
  "This game is best experienced in ",
2939
- requiredOrientation,
2973
+ forceOrientation,
2940
2974
  " ",
2941
2975
  "orientation."
2942
2976
  ] })
@@ -3072,6 +3106,31 @@ var Events = {
3072
3106
  Client: ClientEvents,
3073
3107
  Child: ChildEvents
3074
3108
  };
3109
+
3110
+ // src/utils/ids.ts
3111
+ var alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
3112
+ var generateRoomCode = () => {
3113
+ const array = new Uint32Array(4);
3114
+ if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
3115
+ crypto.getRandomValues(array);
3116
+ } else {
3117
+ for (let i = 0; i < array.length; i += 1) {
3118
+ array[i] = Math.floor(Math.random() * alphabet.length);
3119
+ }
3120
+ }
3121
+ const code = Array.from(
3122
+ array,
3123
+ (value) => alphabet[value % alphabet.length]
3124
+ ).join("");
3125
+ return roomCodeSchema.parse(code);
3126
+ };
3127
+ var generateControllerId = () => {
3128
+ const stamp = Math.floor(Date.now() % 1e5).toString().padStart(5, "0");
3129
+ const random = alphabet[Math.floor(Math.random() * alphabet.length)];
3130
+ return `C${random}${stamp}`;
3131
+ };
3132
+
3133
+ // src/hooks/use-air-jam-controller.ts
3075
3134
  var getRoomFromLocation = () => {
3076
3135
  if (typeof window === "undefined") {
3077
3136
  return null;
@@ -3580,6 +3639,7 @@ var useAirJamShell = (options = {}) => {
3580
3639
  const sendInput = React.useCallback(
3581
3640
  (input) => {
3582
3641
  const storeState = store.getState();
3642
+ input.action === true || typeof input.vector === "object" && input.vector !== null && (Math.abs(input.vector.x ?? 0) > 0.01 || Math.abs(input.vector.y ?? 0) > 0.01);
3583
3643
  if (!parsedRoomId || !storeState.controllerId || !socket) {
3584
3644
  storeState.setError("Not connected to a room");
3585
3645
  return false;
@@ -3676,6 +3736,7 @@ function createAirJamStore2(initializer) {
3676
3736
  }
3677
3737
  };
3678
3738
  });
3739
+ const gameUiUnloadedRef = { current: false };
3679
3740
  const useSyncedStore = (selector = (s) => s) => {
3680
3741
  const slice = store(selector);
3681
3742
  const { getSocket } = useAirJamContext();
@@ -3683,6 +3744,10 @@ function createAirJamStore2(initializer) {
3683
3744
  const roomId = useAirJamState((state) => state.roomId);
3684
3745
  const controllerId = useAirJamState((state) => state.controllerId);
3685
3746
  const socket = role === "host" ? getSocket("host") : getSocket("controller");
3747
+ const socketRef = React.useRef(socket);
3748
+ React.useEffect(() => {
3749
+ socketRef.current = socket;
3750
+ }, [socket]);
3686
3751
  React.useEffect(() => {
3687
3752
  if (!socket || !roomId || !role) {
3688
3753
  return;
@@ -3706,6 +3771,7 @@ function createAirJamStore2(initializer) {
3706
3771
  };
3707
3772
  }
3708
3773
  if (role === "controller") {
3774
+ gameUiUnloadedRef.current = false;
3709
3775
  const handleSync = (payload) => {
3710
3776
  const { data } = payload;
3711
3777
  const currentState = store.getState();
@@ -3717,9 +3783,27 @@ function createAirJamStore2(initializer) {
3717
3783
  }
3718
3784
  };
3719
3785
  socket.on("airjam:state_sync", handleSync);
3720
- return () => socket.off("airjam:state_sync", handleSync);
3786
+ const handleUnloadUi = () => {
3787
+ gameUiUnloadedRef.current = true;
3788
+ socket.off("airjam:state_sync", handleSync);
3789
+ };
3790
+ const handleLoadUi = () => {
3791
+ gameUiUnloadedRef.current = false;
3792
+ };
3793
+ socket.on("client:loadUi", handleLoadUi);
3794
+ const handleDisconnect = () => {
3795
+ gameUiUnloadedRef.current = true;
3796
+ };
3797
+ socket.on("disconnect", handleDisconnect);
3798
+ socket.on("client:unloadUi", handleUnloadUi);
3799
+ return () => {
3800
+ socket.off("airjam:state_sync", handleSync);
3801
+ socket.off("client:loadUi", handleLoadUi);
3802
+ socket.off("client:unloadUi", handleUnloadUi);
3803
+ socket.off("disconnect", handleDisconnect);
3804
+ };
3721
3805
  }
3722
- }, [socket, role, roomId]);
3806
+ }, [socket, role, roomId, socket?.id]);
3723
3807
  if (role === "controller" && socket && roomId) {
3724
3808
  let originalActions = null;
3725
3809
  if (typeof slice === "object" && slice !== null && "actions" in slice) {
@@ -3740,13 +3824,26 @@ function createAirJamStore2(initializer) {
3740
3824
  const proxyActions = {};
3741
3825
  Object.keys(originalActions).forEach((key) => {
3742
3826
  proxyActions[key] = (...args) => {
3827
+ if (gameUiUnloadedRef.current) {
3828
+ console.warn(
3829
+ `[AirJamStore] Action "${key}" blocked: Game UI unloaded.`
3830
+ );
3831
+ return;
3832
+ }
3833
+ const activeSocket = socketRef.current;
3834
+ if (!activeSocket || !activeSocket.connected) {
3835
+ console.warn(
3836
+ `[AirJamStore] Action "${key}" blocked: Socket disconnected.`
3837
+ );
3838
+ return;
3839
+ }
3743
3840
  if (!controllerId) {
3744
3841
  console.warn(
3745
3842
  "[AirJamStore] Cannot send action RPC: controllerId not set"
3746
3843
  );
3747
3844
  return;
3748
3845
  }
3749
- socket.emit("controller:action_rpc", {
3846
+ activeSocket.emit("controller:action_rpc", {
3750
3847
  roomId,
3751
3848
  actionName: key,
3752
3849
  args,
@@ -3774,11 +3871,11 @@ exports.AirJamProvider = AirJamProvider;
3774
3871
  exports.AudioManager = AudioManager;
3775
3872
  exports.AudioProvider = AudioProvider;
3776
3873
  exports.Button = Button;
3874
+ exports.CONTROLLER_PATH = CONTROLLER_PATH;
3777
3875
  exports.ChildEvents = ChildEvents;
3778
3876
  exports.ClientEvents = ClientEvents;
3779
3877
  exports.ControllerEvents = ControllerEvents;
3780
3878
  exports.ControllerShell = ControllerShell;
3781
- exports.DEFAULT_CONTROLLER_PATH = DEFAULT_CONTROLLER_PATH;
3782
3879
  exports.DEFAULT_MAX_PLAYERS = DEFAULT_MAX_PLAYERS;
3783
3880
  exports.DEFAULT_SERVER_PORT = DEFAULT_SERVER_PORT;
3784
3881
  exports.DebugStateDialog = DebugStateDialog;
@@ -3807,7 +3904,9 @@ exports.detectSoundCategory = detectSoundCategory;
3807
3904
  exports.generateControllerId = generateControllerId;
3808
3905
  exports.generateRoomCode = generateRoomCode;
3809
3906
  exports.getLocalNetworkIp = getLocalNetworkIp;
3907
+ exports.hostCreateRoomSchema = hostCreateRoomSchema;
3810
3908
  exports.hostJoinAsChildSchema = hostJoinAsChildSchema;
3909
+ exports.hostReconnectSchema = hostReconnectSchema;
3811
3910
  exports.hostRegisterSystemSchema = hostRegisterSystemSchema;
3812
3911
  exports.hostRegistrationSchema = hostRegistrationSchema;
3813
3912
  exports.roomCodeSchema = roomCodeSchema;