@air-jam/sdk 0.1.4 → 0.9.1
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/LICENSE +21 -0
- package/README.md +627 -57
- package/dist/agent-tooling-BSMGBix9.d.cts +123 -0
- package/dist/agent-tooling-BSMGBix9.d.ts +123 -0
- package/dist/agent-tooling.cjs +83 -0
- package/dist/agent-tooling.cjs.map +1 -0
- package/dist/agent-tooling.d.cts +2 -0
- package/dist/agent-tooling.d.ts +2 -0
- package/dist/agent-tooling.js +17 -0
- package/dist/agent-tooling.js.map +1 -0
- package/dist/arcade/bridge/controller.cjs +749 -0
- package/dist/arcade/bridge/controller.cjs.map +1 -0
- package/dist/arcade/bridge/controller.d.cts +85 -0
- package/dist/arcade/bridge/controller.d.ts +85 -0
- package/dist/arcade/bridge/controller.js +33 -0
- package/dist/arcade/bridge/controller.js.map +1 -0
- package/dist/arcade/bridge/host.cjs +421 -0
- package/dist/arcade/bridge/host.cjs.map +1 -0
- package/dist/arcade/bridge/host.d.cts +73 -0
- package/dist/arcade/bridge/host.d.ts +73 -0
- package/dist/arcade/bridge/host.js +25 -0
- package/dist/arcade/bridge/host.js.map +1 -0
- package/dist/arcade/bridge/iframe.cjs +453 -0
- package/dist/arcade/bridge/iframe.cjs.map +1 -0
- package/dist/arcade/bridge/iframe.d.cts +80 -0
- package/dist/arcade/bridge/iframe.d.ts +80 -0
- package/dist/arcade/bridge/iframe.js +28 -0
- package/dist/arcade/bridge/iframe.js.map +1 -0
- package/dist/arcade/bridge.cjs +1067 -0
- package/dist/arcade/bridge.cjs.map +1 -0
- package/dist/arcade/bridge.d.cts +11 -0
- package/dist/arcade/bridge.d.ts +11 -0
- package/dist/arcade/bridge.js +76 -0
- package/dist/arcade/bridge.js.map +1 -0
- package/dist/arcade/host.cjs +481 -0
- package/dist/arcade/host.cjs.map +1 -0
- package/dist/arcade/host.d.cts +19 -0
- package/dist/arcade/host.d.ts +19 -0
- package/dist/arcade/host.js +14 -0
- package/dist/arcade/host.js.map +1 -0
- package/dist/arcade/surface.cjs +113 -0
- package/dist/arcade/surface.cjs.map +1 -0
- package/dist/arcade/surface.d.cts +14 -0
- package/dist/arcade/surface.d.ts +14 -0
- package/dist/arcade/surface.js +27 -0
- package/dist/arcade/surface.js.map +1 -0
- package/dist/arcade/url.cjs +544 -0
- package/dist/arcade/url.cjs.map +1 -0
- package/dist/arcade/url.d.cts +68 -0
- package/dist/arcade/url.d.ts +68 -0
- package/dist/arcade/url.js +29 -0
- package/dist/arcade/url.js.map +1 -0
- package/dist/arcade-runtime-url-BW-MUAOY.d.cts +28 -0
- package/dist/arcade-runtime-url-l8qj01Pg.d.ts +28 -0
- package/dist/arcade-surface-identity-CQxDpn_w.d.cts +16 -0
- package/dist/arcade-surface-identity-CQxDpn_w.d.ts +16 -0
- package/dist/arcade.cjs +1737 -0
- package/dist/arcade.cjs.map +1 -0
- package/dist/arcade.d.cts +18 -0
- package/dist/arcade.d.ts +18 -0
- package/dist/arcade.js +123 -0
- package/dist/arcade.js.map +1 -0
- package/dist/capabilities.cjs +39 -0
- package/dist/capabilities.cjs.map +1 -0
- package/dist/capabilities.d.cts +31 -0
- package/dist/capabilities.d.ts +31 -0
- package/dist/capabilities.js +12 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/chunk-32NYL7XI.js +539 -0
- package/dist/chunk-32NYL7XI.js.map +1 -0
- package/dist/chunk-3USU7GY2.js +54 -0
- package/dist/chunk-3USU7GY2.js.map +1 -0
- package/dist/chunk-7UXSWCZZ.js +32 -0
- package/dist/chunk-7UXSWCZZ.js.map +1 -0
- package/dist/chunk-BE6Z4SIT.js +65 -0
- package/dist/chunk-BE6Z4SIT.js.map +1 -0
- package/dist/chunk-CABEUV65.js +184 -0
- package/dist/chunk-CABEUV65.js.map +1 -0
- package/dist/chunk-CEJ5RFSG.js +288 -0
- package/dist/chunk-CEJ5RFSG.js.map +1 -0
- package/dist/chunk-EDRQIOCV.js +25 -0
- package/dist/chunk-EDRQIOCV.js.map +1 -0
- package/dist/chunk-EVZJA6RN.js +140 -0
- package/dist/chunk-EVZJA6RN.js.map +1 -0
- package/dist/chunk-FAT2KJ3K.js +238 -0
- package/dist/chunk-FAT2KJ3K.js.map +1 -0
- package/dist/chunk-IKKDONQL.js +1 -0
- package/dist/chunk-IKKDONQL.js.map +1 -0
- package/dist/chunk-JEHV5C2X.js +249 -0
- package/dist/chunk-JEHV5C2X.js.map +1 -0
- package/dist/chunk-JO2HR5PV.js +43 -0
- package/dist/chunk-JO2HR5PV.js.map +1 -0
- package/dist/chunk-JTF47B56.js +1 -0
- package/dist/chunk-JTF47B56.js.map +1 -0
- package/dist/chunk-KXAFI5OP.js +193 -0
- package/dist/chunk-KXAFI5OP.js.map +1 -0
- package/dist/chunk-LIXPF4P6.js +22 -0
- package/dist/chunk-LIXPF4P6.js.map +1 -0
- package/dist/chunk-MPDMJIX5.js +1 -0
- package/dist/chunk-MPDMJIX5.js.map +1 -0
- package/dist/chunk-NCL6WJCL.js +91 -0
- package/dist/chunk-NCL6WJCL.js.map +1 -0
- package/dist/chunk-NUJBMV3O.js +1435 -0
- package/dist/chunk-NUJBMV3O.js.map +1 -0
- package/dist/chunk-TGRTEWYV.js +195 -0
- package/dist/chunk-TGRTEWYV.js.map +1 -0
- package/dist/chunk-UWJOIVON.js +369 -0
- package/dist/chunk-UWJOIVON.js.map +1 -0
- package/dist/chunk-VKBMZHMQ.js +216 -0
- package/dist/chunk-VKBMZHMQ.js.map +1 -0
- package/dist/chunk-W2VK36RO.js +11 -0
- package/dist/chunk-W2VK36RO.js.map +1 -0
- package/dist/chunk-W3YA5BWR.js +126 -0
- package/dist/chunk-W3YA5BWR.js.map +1 -0
- package/dist/chunk-X24K52DG.js +12 -0
- package/dist/chunk-X24K52DG.js.map +1 -0
- package/dist/chunk-YXHH3MQT.js +230 -0
- package/dist/chunk-YXHH3MQT.js.map +1 -0
- package/dist/chunk-ZLPL2R3R.js +183 -0
- package/dist/chunk-ZLPL2R3R.js.map +1 -0
- package/dist/chunk-ZTQ5SP4H.js +9 -0
- package/dist/chunk-ZTQ5SP4H.js.map +1 -0
- package/dist/controller-Bod8YYr1.d.cts +172 -0
- package/dist/controller-DxVchfiA.d.ts +172 -0
- package/dist/dev-log-events-CVAM29hx.d.cts +123 -0
- package/dist/dev-log-events-CVAM29hx.d.ts +123 -0
- package/dist/handshake-CC_LrK5g.d.cts +31 -0
- package/dist/handshake-CC_LrK5g.d.ts +31 -0
- package/dist/host-8hmq_Sgc.d.cts +109 -0
- package/dist/host-Cw-N6e-5.d.ts +109 -0
- package/dist/index.cjs +8730 -3426
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +676 -1070
- package/dist/index.d.ts +676 -1070
- package/dist/index.js +4892 -3196
- package/dist/index.js.map +1 -1
- package/dist/metadata.cjs +114 -0
- package/dist/metadata.cjs.map +1 -0
- package/dist/metadata.d.cts +95 -0
- package/dist/metadata.d.ts +95 -0
- package/dist/metadata.js +83 -0
- package/dist/metadata.js.map +1 -0
- package/dist/notices-CnGaaS8B.d.ts +38 -0
- package/dist/notices-NyDM5Mtz.d.cts +38 -0
- package/dist/platform-machine.cjs +425 -0
- package/dist/platform-machine.cjs.map +1 -0
- package/dist/platform-machine.d.cts +1183 -0
- package/dist/platform-machine.d.ts +1183 -0
- package/dist/platform-machine.js +309 -0
- package/dist/platform-machine.js.map +1 -0
- package/dist/platform-settings-cuor3i11.d.cts +48 -0
- package/dist/platform-settings-cuor3i11.d.ts +48 -0
- package/dist/prefabs.cjs +36 -0
- package/dist/prefabs.cjs.map +1 -0
- package/dist/prefabs.d.cts +49 -0
- package/dist/prefabs.d.ts +49 -0
- package/dist/prefabs.js +8 -0
- package/dist/prefabs.js.map +1 -0
- package/dist/preview.cjs +2620 -0
- package/dist/preview.cjs.map +1 -0
- package/dist/preview.d.cts +155 -0
- package/dist/preview.d.ts +155 -0
- package/dist/preview.js +1671 -0
- package/dist/preview.js.map +1 -0
- package/dist/protocol.cjs +621 -54
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.d.cts +76 -240
- package/dist/protocol.d.ts +76 -240
- package/dist/protocol.js +100 -2
- package/dist/protocol.js.map +1 -1
- package/dist/realtime-client-Cu8yVJFw.d.cts +15 -0
- package/dist/realtime-client-Cu8yVJFw.d.ts +15 -0
- package/dist/release.cjs +143 -0
- package/dist/release.cjs.map +1 -0
- package/dist/release.d.cts +64 -0
- package/dist/release.d.ts +64 -0
- package/dist/release.js +43 -0
- package/dist/release.js.map +1 -0
- package/dist/runtime-topology.cjs +405 -0
- package/dist/runtime-topology.cjs.map +1 -0
- package/dist/runtime-topology.d.cts +59 -0
- package/dist/runtime-topology.d.ts +59 -0
- package/dist/runtime-topology.js +31 -0
- package/dist/runtime-topology.js.map +1 -0
- package/dist/standard-match-phase-BucA5dYo.d.ts +44 -0
- package/dist/standard-match-phase-U2ra8J-0.d.cts +44 -0
- package/dist/sync-B6QsNrXz.d.cts +125 -0
- package/dist/sync-B6QsNrXz.d.ts +125 -0
- package/dist/ui.cjs +3061 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.cts +320 -0
- package/dist/ui.d.ts +320 -0
- package/dist/ui.js +1719 -0
- package/dist/ui.js.map +1 -0
- package/dist/url-policy-C89zxgw9.d.cts +9 -0
- package/dist/url-policy-C89zxgw9.d.ts +9 -0
- package/package.json +155 -10
- package/src/agent/agent-action.ts +304 -0
- package/src/agent/agent-contract.ts +180 -0
- package/src/agent-tooling.ts +20 -0
- package/src/arcade/bridge/controller.ts +15 -0
- package/src/arcade/bridge/host.ts +12 -0
- package/src/arcade/bridge/iframe.ts +61 -0
- package/src/arcade/bridge.ts +3 -0
- package/src/arcade/host.ts +1 -0
- package/src/arcade/surface.ts +4 -0
- package/src/arcade/url.ts +60 -0
- package/src/arcade.ts +4 -0
- package/src/audio/audio-manager.ts +162 -171
- package/src/audio/hooks.tsx +218 -97
- package/src/audio/index.ts +1 -1
- package/src/audio/music.tsx +139 -0
- package/src/capabilities/manifest.ts +71 -0
- package/src/capabilities.ts +12 -0
- package/src/components/connection-status-pill.tsx +55 -0
- package/src/components/controller-player-name-field.tsx +119 -0
- package/src/components/controller-primary-action.tsx +58 -0
- package/src/components/host-mute-button.tsx +49 -0
- package/src/components/join-qr-overlay.tsx +133 -0
- package/src/components/join-url-action-buttons.tsx +95 -0
- package/src/components/join-url-controls.tsx +58 -0
- package/src/components/join-url-field.tsx +52 -0
- package/src/components/lifecycle-action-group.tsx +129 -0
- package/src/components/player-avatar-strip.tsx +65 -0
- package/src/components/player-avatar.tsx +42 -13
- package/src/components/room-qr-code.tsx +125 -0
- package/src/components/runtime-shell-header.tsx +37 -0
- package/src/components/shell-classes.ts +19 -0
- package/src/components/surface-orientation-layout.ts +60 -0
- package/src/components/surface-viewport-layout.ts +268 -0
- package/src/components/surface-viewport.tsx +314 -0
- package/src/components/ui/button.tsx +4 -6
- package/src/components/ui/slider.tsx +1 -1
- package/src/components/volume-controls.tsx +65 -6
- package/src/context/air-jam-context.tsx +209 -161
- package/src/context/index.ts +2 -2
- package/src/context/session-providers.tsx +77 -0
- package/src/context/session-scope.ts +64 -0
- package/src/context/socket-manager.ts +6 -24
- package/src/contracts/v2/bridge.ts +261 -0
- package/src/contracts/v2/handshake.ts +40 -0
- package/src/contracts/v2/index.ts +2 -0
- package/src/dev/browser-log-sink.ts +912 -0
- package/src/diagnostics.ts +128 -0
- package/src/events.ts +7 -34
- package/src/hooks/internal/use-controller-runtime-api.ts +875 -0
- package/src/hooks/internal/use-host-runtime-api.ts +1051 -0
- package/src/hooks/internal/use-runtime-control-contract.ts +15 -0
- package/src/hooks/internal/use-runtime-inspection-contract.ts +16 -0
- package/src/hooks/internal/use-runtime-observability-contract.ts +24 -0
- package/src/hooks/use-air-jam-controller.ts +155 -475
- package/src/hooks/use-air-jam-host.ts +156 -485
- package/src/hooks/use-connection-status.ts +8 -0
- package/src/hooks/use-controller-lifecycle-intents.ts +27 -0
- package/src/hooks/use-controller-lifecycle-permissions.ts +36 -0
- package/src/hooks/use-controller-shell-status.ts +40 -0
- package/src/hooks/use-controller-tick.ts +65 -0
- package/src/hooks/use-controller-toasts.ts +116 -0
- package/src/hooks/use-get-input.ts +8 -5
- package/src/hooks/use-host-arcade-restore.ts +30 -0
- package/src/hooks/use-host-join-controls.ts +152 -0
- package/src/hooks/use-host-tick.ts +171 -0
- package/src/hooks/use-input-writer.ts +83 -0
- package/src/hooks/use-lifecycle-action-group-model.ts +98 -0
- package/src/hooks/use-players.ts +12 -0
- package/src/hooks/use-room.ts +22 -0
- package/src/hooks/use-send-signal.ts +6 -2
- package/src/index.ts +205 -31
- package/src/internal/input-manager.ts +195 -215
- package/src/lifecycle/index.ts +9 -0
- package/src/lifecycle/standard-match-phase.ts +32 -0
- package/src/metadata/manifest.ts +152 -0
- package/src/metadata.ts +12 -0
- package/src/platform-machine.ts +475 -0
- package/src/prefabs/types.ts +82 -0
- package/src/prefabs.ts +12 -0
- package/src/preview/host-workspace.tsx +59 -0
- package/src/preview/identity.ts +66 -0
- package/src/preview/layout.ts +223 -0
- package/src/preview/manager.ts +577 -0
- package/src/preview/messages.ts +26 -0
- package/src/preview/ownership.ts +81 -0
- package/src/preview/url.ts +117 -0
- package/src/preview/window.tsx +490 -0
- package/src/preview/workspace.tsx +678 -0
- package/src/preview.ts +42 -0
- package/src/protocol/arcade-platform-actions.ts +29 -0
- package/src/protocol/controller.ts +187 -0
- package/src/protocol/core.ts +23 -0
- package/src/protocol/dev-log-events.ts +222 -0
- package/src/protocol/errors.ts +21 -0
- package/src/protocol/host-grant.ts +185 -0
- package/src/protocol/host.ts +138 -0
- package/src/protocol/notices.ts +43 -0
- package/src/protocol/signals.ts +38 -0
- package/src/protocol/socket-events.ts +154 -0
- package/src/protocol/sync.ts +121 -0
- package/src/protocol/url-policy.ts +63 -0
- package/src/protocol.ts +12 -386
- package/src/qrcode.d.ts +16 -0
- package/src/release.ts +99 -0
- package/src/runtime/air-jam-config.ts +287 -0
- package/src/runtime/air-jam-error-boundary.tsx +672 -0
- package/src/runtime/arcade-bridge-request-surface.ts +15 -0
- package/src/runtime/arcade-runtime-url.ts +95 -0
- package/src/runtime/arcade-surface-identity.ts +16 -0
- package/src/runtime/contracts/control.ts +68 -0
- package/src/runtime/contracts/inspection.ts +165 -0
- package/src/runtime/contracts/observability.ts +94 -0
- package/src/runtime/controller-bridge.ts +451 -0
- package/src/runtime/controller-identity.ts +132 -0
- package/src/runtime/controller-presentation.ts +24 -0
- package/src/runtime/controller-realtime-client.ts +591 -0
- package/src/runtime/create-air-jam-app.tsx +260 -0
- package/src/runtime/dev-runtime-events.ts +36 -0
- package/src/runtime/embedded-runtime-adapters.ts +68 -0
- package/src/runtime/host-bridge.ts +388 -0
- package/src/runtime/host-realtime-client.ts +550 -0
- package/src/runtime/iframe-bridge.ts +135 -0
- package/src/runtime/realtime-client.ts +20 -0
- package/src/runtime/router-basename.ts +25 -0
- package/src/runtime/runtime-owner-contexts.tsx +4 -0
- package/src/runtime/runtime-session-params.ts +169 -0
- package/src/runtime/sdk-version.ts +6 -0
- package/src/runtime/session-runtimes.tsx +188 -0
- package/src/runtime/validate-arcade-bridge-attach.ts +14 -0
- package/src/runtime-control.ts +10 -0
- package/src/runtime-inspection.ts +14 -0
- package/src/runtime-observability.ts +9 -0
- package/src/runtime-topology.ts +569 -0
- package/src/settings/platform-settings-bridge.ts +336 -0
- package/src/settings/platform-settings-runtime.tsx +387 -0
- package/src/settings/platform-settings.ts +288 -0
- package/src/state/connection-store.ts +80 -12
- package/src/store/air-jam-store-domain-constants.ts +4 -0
- package/src/store/create-air-jam-store.ts +1094 -227
- package/src/styles.css +4 -38
- package/src/ui.ts +29 -0
- package/src/utils/is-rpc-serializable.ts +56 -0
- package/src/utils/player-avatar-url.ts +48 -0
- package/src/utils/url-builder.ts +7 -0
- package/dist/.tsbuildinfo +0 -1
- package/dist/chunk-B5DURUWT.js +0 -80
- package/dist/chunk-B5DURUWT.js.map +0 -1
- package/src/AirJamClient.ts +0 -224
- package/src/audio/volume-store.ts +0 -124
- package/src/components/controller-shell.tsx +0 -346
- package/src/components/host-shell.tsx +0 -520
- package/src/components/qr-scanner-dialog.tsx +0 -373
- package/src/components/ui/alert.tsx +0 -66
- package/src/components/ui/avatar.tsx +0 -46
- package/src/components/ui/badge.tsx +0 -47
- package/src/components/ui/card.tsx +0 -92
- package/src/components/ui/dialog.tsx +0 -122
- package/src/components/ui/tabs.tsx +0 -64
- package/src/hooks/use-air-jam-shell.ts +0 -379
- package/src/hooks/use-fullscreen.ts +0 -108
|
@@ -6,64 +6,66 @@
|
|
|
6
6
|
* all the functionality needed to manage a multiplayer session:
|
|
7
7
|
* - Room management (create/join rooms)
|
|
8
8
|
* - Player tracking (join/leave events, player list)
|
|
9
|
-
* - Input handling (
|
|
9
|
+
* - Input handling (typed and behavior-aware controller input)
|
|
10
10
|
* - Signaling (send haptic feedback, toast notifications to controllers)
|
|
11
|
-
* -
|
|
11
|
+
* - Runtime pause/play controls and controller state broadcasts
|
|
12
|
+
*
|
|
13
|
+
* **Standalone (default):** creates or reconnects a room from session storage / options.
|
|
14
|
+
*
|
|
15
|
+
* **Embedded child host:** when `aj_room` + `aj_cap` are present, the hook binds to that
|
|
16
|
+
* room and skips create-room; resolution is centralized in
|
|
17
|
+
* {@link ../runtime/embedded-runtime-adapters.readEmbeddedHostChildSession}.
|
|
12
18
|
*/
|
|
13
|
-
import {
|
|
19
|
+
import { useContext } from "react";
|
|
14
20
|
import type { z } from "zod";
|
|
15
21
|
import { useStore } from "zustand";
|
|
16
22
|
import { useShallow } from "zustand/react/shallow";
|
|
17
|
-
import { TOGGLE_DEBOUNCE_MS } from "../constants";
|
|
18
23
|
import { useAirJamContext } from "../context/air-jam-context";
|
|
19
|
-
import
|
|
24
|
+
import { useAssertSessionScope } from "../context/session-scope";
|
|
25
|
+
import { createAirJamDiagnosticError } from "../diagnostics";
|
|
20
26
|
import type {
|
|
21
27
|
ConnectionStatus,
|
|
22
|
-
|
|
23
|
-
ControllerStateMessage,
|
|
28
|
+
ControllerPresenceNotice,
|
|
24
29
|
ControllerStatePayload,
|
|
25
|
-
GameState,
|
|
26
30
|
HapticSignalPayload,
|
|
31
|
+
HostControllerActionAck,
|
|
32
|
+
HostRegistrationAck,
|
|
27
33
|
PlayerProfile,
|
|
28
34
|
RoomCode,
|
|
29
35
|
RunMode,
|
|
30
|
-
|
|
31
|
-
SignalType,
|
|
36
|
+
RuntimeState,
|
|
32
37
|
ToastSignalPayload,
|
|
33
38
|
} from "../protocol";
|
|
34
|
-
import {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
roomCodeSchema,
|
|
40
|
-
} from "../protocol";
|
|
41
|
-
import { detectRunMode } from "../utils/mode";
|
|
42
|
-
import { urlBuilder } from "../utils/url-builder";
|
|
39
|
+
import type { AirJamRealtimeClient } from "../runtime/realtime-client";
|
|
40
|
+
import { hostRuntimeContext } from "../runtime/runtime-owner-contexts";
|
|
41
|
+
import type { AirJamStore } from "../state/connection-store";
|
|
42
|
+
|
|
43
|
+
export type JoinUrlStatus = "loading" | "ready" | "unavailable";
|
|
43
44
|
|
|
44
45
|
/**
|
|
45
|
-
* Options for configuring the host
|
|
46
|
+
* Options for configuring the host runtime boundary.
|
|
46
47
|
*
|
|
47
|
-
* @example
|
|
48
|
+
* @example Runtime boundary usage with callbacks
|
|
48
49
|
* ```tsx
|
|
49
|
-
*
|
|
50
|
-
* onPlayerJoin
|
|
50
|
+
* <AirJamHostRuntime
|
|
51
|
+
* onPlayerJoin={(player) => {
|
|
51
52
|
* console.log(`${player.label} joined!`);
|
|
52
53
|
* spawnPlayerShip(player.id);
|
|
53
|
-
* }
|
|
54
|
-
* onPlayerLeave
|
|
54
|
+
* }}
|
|
55
|
+
* onPlayerLeave={(controllerId) => {
|
|
55
56
|
* console.log(`Player ${controllerId} left`);
|
|
56
57
|
* removePlayerShip(controllerId);
|
|
57
|
-
* }
|
|
58
|
-
*
|
|
58
|
+
* }}
|
|
59
|
+
* >
|
|
60
|
+
* <HostView />
|
|
61
|
+
* </AirJamHostRuntime>
|
|
59
62
|
* ```
|
|
60
63
|
*
|
|
61
64
|
* @example With custom room ID
|
|
62
65
|
* ```tsx
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* });
|
|
66
|
+
* <AirJamHostRuntime roomId="GAME1">
|
|
67
|
+
* <HostView />
|
|
68
|
+
* </AirJamHostRuntime>
|
|
67
69
|
* ```
|
|
68
70
|
*/
|
|
69
71
|
export interface AirJamHostOptions {
|
|
@@ -83,26 +85,6 @@ export interface AirJamHostOptions {
|
|
|
83
85
|
* Use this to remove player entities, handle cleanup.
|
|
84
86
|
*/
|
|
85
87
|
onPlayerLeave?: (controllerId: string) => void;
|
|
86
|
-
/**
|
|
87
|
-
* Called when the child window closes in Arcade mode.
|
|
88
|
-
* Only relevant when running as part of the AirJam Platform.
|
|
89
|
-
*/
|
|
90
|
-
onChildClose?: () => void;
|
|
91
|
-
/**
|
|
92
|
-
* Force connection even in non-standard modes.
|
|
93
|
-
* @default true
|
|
94
|
-
*/
|
|
95
|
-
forceConnect?: boolean;
|
|
96
|
-
/**
|
|
97
|
-
* Override the API key from provider.
|
|
98
|
-
* Useful for per-game API keys in multi-game setups.
|
|
99
|
-
*/
|
|
100
|
-
apiKey?: string;
|
|
101
|
-
/**
|
|
102
|
-
* Override the maximum players from provider.
|
|
103
|
-
* @default 8
|
|
104
|
-
*/
|
|
105
|
-
maxPlayers?: number;
|
|
106
88
|
}
|
|
107
89
|
|
|
108
90
|
/**
|
|
@@ -117,18 +99,26 @@ export interface AirJamHostApi<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
117
99
|
roomId: RoomCode;
|
|
118
100
|
/** Full URL for controllers to join (display as QR code) */
|
|
119
101
|
joinUrl: string;
|
|
102
|
+
/** Whether the join URL is still being resolved, ready to render, or unavailable */
|
|
103
|
+
joinUrlStatus: JoinUrlStatus;
|
|
120
104
|
/** Current connection status to the server */
|
|
121
105
|
connectionStatus: ConnectionStatus;
|
|
122
106
|
/** List of currently connected players */
|
|
123
107
|
players: PlayerProfile[];
|
|
108
|
+
/** Full controller-session roster for the current room, including source and lease state. */
|
|
109
|
+
controllers: ControllerPresenceNotice[];
|
|
124
110
|
/** Last error message, if any */
|
|
125
111
|
lastError?: string;
|
|
126
112
|
/** Current run mode (standalone, arcade, platform) */
|
|
127
113
|
mode: RunMode;
|
|
128
|
-
/** Current
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
|
|
114
|
+
/** Current runtime pause/play state */
|
|
115
|
+
runtimeState: RuntimeState;
|
|
116
|
+
/** Pause the runtime for every connected surface. */
|
|
117
|
+
pauseRuntime: () => void;
|
|
118
|
+
/** Resume the runtime for every connected surface. */
|
|
119
|
+
resumeRuntime: () => void;
|
|
120
|
+
/** Set the runtime pause/play state explicitly. */
|
|
121
|
+
setRuntimeState: (state: RuntimeState) => void;
|
|
132
122
|
/**
|
|
133
123
|
* Send state update to all connected controllers.
|
|
134
124
|
* Use for syncing game state, messages, etc.
|
|
@@ -156,16 +146,18 @@ export interface AirJamHostApi<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
156
146
|
};
|
|
157
147
|
/** Reconnect to the server */
|
|
158
148
|
reconnect: () => void;
|
|
159
|
-
/**
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
|
|
149
|
+
/** Remove one controller from the room immediately. */
|
|
150
|
+
removeController: (controllerId: string) => Promise<HostControllerActionAck>;
|
|
151
|
+
/** Tear down the current room and create a fresh empty room for this host. */
|
|
152
|
+
resetRoom: () => Promise<HostRegistrationAck>;
|
|
153
|
+
/** Realtime client for host events (socket-backed standalone, bridge-backed in arcade embeds) */
|
|
154
|
+
socket: AirJamRealtimeClient;
|
|
163
155
|
/**
|
|
164
156
|
* Get the latest input from a specific controller.
|
|
165
157
|
*
|
|
166
|
-
* Returns validated, typed input based on the schema provided to
|
|
167
|
-
*
|
|
168
|
-
*
|
|
158
|
+
* Returns validated, typed input based on the schema provided to the session provider.
|
|
159
|
+
* Input behavior defaults are tap-safe booleans (`pulse`) and latest vectors (`latest`),
|
|
160
|
+
* with optional per-field overrides.
|
|
169
161
|
*
|
|
170
162
|
* @example In a game loop
|
|
171
163
|
* ```ts
|
|
@@ -183,38 +175,86 @@ export interface AirJamHostApi<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
183
175
|
getInput: (controllerId: string) => z.infer<TSchema> | undefined;
|
|
184
176
|
}
|
|
185
177
|
|
|
178
|
+
export interface AirJamHostState {
|
|
179
|
+
roomId: RoomCode | null;
|
|
180
|
+
connectionStatus: ConnectionStatus;
|
|
181
|
+
players: PlayerProfile[];
|
|
182
|
+
controllers: ControllerPresenceNotice[];
|
|
183
|
+
lastError?: string;
|
|
184
|
+
mode: RunMode;
|
|
185
|
+
runtimeState: RuntimeState;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export type AirJamHostRuntimeControls<
|
|
189
|
+
TSchema extends z.ZodSchema = z.ZodSchema,
|
|
190
|
+
> = Pick<
|
|
191
|
+
AirJamHostApi<TSchema>,
|
|
192
|
+
| "roomId"
|
|
193
|
+
| "joinUrl"
|
|
194
|
+
| "joinUrlStatus"
|
|
195
|
+
| "pauseRuntime"
|
|
196
|
+
| "resumeRuntime"
|
|
197
|
+
| "setRuntimeState"
|
|
198
|
+
| "sendState"
|
|
199
|
+
| "sendSignal"
|
|
200
|
+
| "reconnect"
|
|
201
|
+
| "removeController"
|
|
202
|
+
| "resetRoom"
|
|
203
|
+
| "socket"
|
|
204
|
+
| "getInput"
|
|
205
|
+
>;
|
|
206
|
+
|
|
207
|
+
const toHostState = (state: AirJamStore): AirJamHostState => ({
|
|
208
|
+
roomId: state.roomId,
|
|
209
|
+
connectionStatus: state.connectionStatus,
|
|
210
|
+
players: state.players,
|
|
211
|
+
controllers: state.controllerSessions,
|
|
212
|
+
lastError: state.lastError,
|
|
213
|
+
mode: state.mode,
|
|
214
|
+
runtimeState: state.runtimeState,
|
|
215
|
+
});
|
|
216
|
+
|
|
186
217
|
/**
|
|
187
|
-
*
|
|
218
|
+
* Read the mounted host runtime API.
|
|
219
|
+
*
|
|
220
|
+
* Use this inside `AirJamHostRuntime` or another explicit host runtime boundary
|
|
221
|
+
* such as `airjam.Host`. Runtime ownership is mounted once at the boundary; child
|
|
222
|
+
* code reads from that runtime through this hook.
|
|
188
223
|
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
224
|
+
* This hook is runtime-aware:
|
|
225
|
+
* - standalone: creates/reconnects host rooms directly
|
|
226
|
+
* - arcade iframe runtime: auto-detects `aj_room` + `aj_cap` and bridges through the platform-owned host session
|
|
227
|
+
*
|
|
228
|
+
* This hook no longer creates host runtime side effects.
|
|
191
229
|
*
|
|
192
230
|
* **Features:**
|
|
193
231
|
* - Automatic room creation and management
|
|
194
232
|
* - Real-time player join/leave events
|
|
195
|
-
* - Typed input with validation and
|
|
233
|
+
* - Typed input with validation and behavior defaults
|
|
196
234
|
* - Haptic feedback and toast notifications
|
|
197
|
-
* -
|
|
235
|
+
* - Runtime state synchronization
|
|
198
236
|
*
|
|
199
237
|
* @template TSchema - Zod schema for input validation (from provider)
|
|
200
|
-
* @param options - Configuration options for the host
|
|
201
238
|
* @returns API object with state and functions
|
|
202
239
|
*
|
|
203
240
|
* @example Basic usage
|
|
204
241
|
* ```tsx
|
|
205
242
|
* const HostView = () => {
|
|
206
|
-
* const host = useAirJamHost(
|
|
207
|
-
* onPlayerJoin: (player) => console.log(`${player.label} joined`),
|
|
208
|
-
* onPlayerLeave: (id) => console.log(`${id} left`),
|
|
209
|
-
* });
|
|
243
|
+
* const host = useAirJamHost();
|
|
210
244
|
*
|
|
211
245
|
* return (
|
|
212
246
|
* <div>
|
|
213
247
|
* <h1>Room: {host.roomId}</h1>
|
|
214
248
|
* <QRCode value={host.joinUrl} />
|
|
215
249
|
* <p>Players: {host.players.length}</p>
|
|
216
|
-
* <button
|
|
217
|
-
* {
|
|
250
|
+
* <button
|
|
251
|
+
* onClick={
|
|
252
|
+
* host.runtimeState === "playing"
|
|
253
|
+
* ? host.pauseRuntime
|
|
254
|
+
* : host.resumeRuntime
|
|
255
|
+
* }
|
|
256
|
+
* >
|
|
257
|
+
* {host.runtimeState === "playing" ? "Pause" : "Resume"}
|
|
218
258
|
* </button>
|
|
219
259
|
* </div>
|
|
220
260
|
* );
|
|
@@ -234,7 +274,7 @@ export interface AirJamHostApi<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
234
274
|
* // Move player based on joystick
|
|
235
275
|
* movePlayer(player.id, input.vector);
|
|
236
276
|
*
|
|
237
|
-
* // Handle button press (
|
|
277
|
+
* // Handle button press (tap-safe pulse default)
|
|
238
278
|
* if (input.action) {
|
|
239
279
|
* playerShoot(player.id);
|
|
240
280
|
* }
|
|
@@ -252,418 +292,49 @@ export interface AirJamHostApi<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
|
252
292
|
* };
|
|
253
293
|
* ```
|
|
254
294
|
*/
|
|
255
|
-
export
|
|
256
|
-
|
|
257
|
-
): AirJamHostApi<TSchema
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return null;
|
|
272
|
-
}, []);
|
|
273
|
-
|
|
274
|
-
const isChildMode = !!arcadeParams;
|
|
275
|
-
|
|
276
|
-
// Always connect - forceConnect kept for API compatibility
|
|
277
|
-
const shouldConnect = true;
|
|
278
|
-
|
|
279
|
-
// Subscribe to store state to get roomId (server-issued for standalone mode)
|
|
280
|
-
const storeRoomId = useStore(store, (s) => s.roomId);
|
|
281
|
-
|
|
282
|
-
// Parse room ID from various sources
|
|
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>(() => {
|
|
286
|
-
if (arcadeParams) {
|
|
287
|
-
// Child mode: room ID comes from URL params
|
|
288
|
-
return roomCodeSchema.parse(arcadeParams.room.toUpperCase());
|
|
289
|
-
}
|
|
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)
|
|
297
|
-
if (typeof window !== "undefined") {
|
|
298
|
-
const params = new URLSearchParams(window.location.search);
|
|
299
|
-
const paramRoom = params.get("room");
|
|
300
|
-
if (paramRoom) {
|
|
301
|
-
const result = roomCodeSchema.safeParse(paramRoom.toUpperCase());
|
|
302
|
-
if (result.success) return result.data;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (options.roomId) {
|
|
307
|
-
return roomCodeSchema.parse(options.roomId.toUpperCase());
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return null;
|
|
311
|
-
}, [options.roomId, storeRoomId, arcadeParams]);
|
|
312
|
-
|
|
313
|
-
const [joinUrl, setJoinUrl] = useState<string>("");
|
|
314
|
-
|
|
315
|
-
// Keep callback refs stable
|
|
316
|
-
const onPlayerJoinRef = useRef(options.onPlayerJoin);
|
|
317
|
-
const onPlayerLeaveRef = useRef(options.onPlayerLeave);
|
|
318
|
-
const onChildCloseRef = useRef(options.onChildClose);
|
|
319
|
-
|
|
320
|
-
useEffect(() => {
|
|
321
|
-
onPlayerJoinRef.current = options.onPlayerJoin;
|
|
322
|
-
onPlayerLeaveRef.current = options.onPlayerLeave;
|
|
323
|
-
onChildCloseRef.current = options.onChildClose;
|
|
324
|
-
}, [options.onPlayerJoin, options.onPlayerLeave, options.onChildClose]);
|
|
325
|
-
|
|
326
|
-
// Subscribe to store state
|
|
327
|
-
const connectionState = useStore(
|
|
328
|
-
store,
|
|
329
|
-
useShallow((state) => ({
|
|
330
|
-
connectionStatus: state.connectionStatus,
|
|
331
|
-
lastError: state.lastError,
|
|
332
|
-
players: state.players,
|
|
333
|
-
gameState: state.gameState,
|
|
334
|
-
mode: state.mode,
|
|
335
|
-
roomId: state.roomId,
|
|
336
|
-
})),
|
|
337
|
-
);
|
|
338
|
-
|
|
339
|
-
// Get socket from context
|
|
340
|
-
const socket = useMemo(
|
|
341
|
-
() => (shouldConnect ? getSocket("host") : null),
|
|
342
|
-
[shouldConnect, getSocket],
|
|
343
|
-
);
|
|
344
|
-
|
|
345
|
-
const [lastToggle, setLastToggle] = useState(0);
|
|
346
|
-
|
|
347
|
-
const toggleGameState = useCallback(() => {
|
|
348
|
-
const now = Date.now();
|
|
349
|
-
if (now - lastToggle < TOGGLE_DEBOUNCE_MS) {
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
setLastToggle(now);
|
|
353
|
-
|
|
354
|
-
if (!socket || !socket.connected) {
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
if (!parsedRoomId) return;
|
|
359
|
-
|
|
360
|
-
const payload = controllerSystemSchema.safeParse({
|
|
361
|
-
roomId: parsedRoomId,
|
|
362
|
-
command: "toggle_pause",
|
|
363
|
-
});
|
|
364
|
-
if (payload.success) {
|
|
365
|
-
socket.emit("host:system", payload.data);
|
|
366
|
-
}
|
|
367
|
-
}, [socket, parsedRoomId, lastToggle]);
|
|
368
|
-
|
|
369
|
-
const sendState = useCallback(
|
|
370
|
-
(state: ControllerStatePayload): boolean => {
|
|
371
|
-
if (!socket || !socket.connected || !parsedRoomId) {
|
|
372
|
-
return false;
|
|
373
|
-
}
|
|
374
|
-
const payload = controllerStateSchema.safeParse({
|
|
375
|
-
roomId: parsedRoomId,
|
|
376
|
-
state,
|
|
377
|
-
});
|
|
378
|
-
if (!payload.success) {
|
|
379
|
-
return false;
|
|
380
|
-
}
|
|
381
|
-
socket.emit("host:state", payload.data);
|
|
382
|
-
return true;
|
|
383
|
-
},
|
|
384
|
-
[socket, parsedRoomId],
|
|
385
|
-
);
|
|
386
|
-
|
|
387
|
-
const sendSignal = useCallback(
|
|
388
|
-
(
|
|
389
|
-
type: SignalType,
|
|
390
|
-
payload: HapticSignalPayload | ToastSignalPayload,
|
|
391
|
-
targetId?: string,
|
|
392
|
-
): void => {
|
|
393
|
-
if (!socket || !socket.connected) {
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
const signal: SignalPayload = {
|
|
397
|
-
targetId,
|
|
398
|
-
type,
|
|
399
|
-
payload,
|
|
400
|
-
} as SignalPayload;
|
|
401
|
-
socket.emit("host:signal", signal);
|
|
402
|
-
},
|
|
403
|
-
[socket],
|
|
404
|
-
) as AirJamHostApi["sendSignal"];
|
|
405
|
-
|
|
406
|
-
const reconnect = useCallback(() => {
|
|
407
|
-
disconnectSocket("host");
|
|
408
|
-
if (socket) {
|
|
409
|
-
socket.connect();
|
|
410
|
-
}
|
|
411
|
-
}, [socket, disconnectSocket]);
|
|
412
|
-
|
|
413
|
-
// Build controller URL
|
|
414
|
-
useEffect(() => {
|
|
415
|
-
if (!parsedRoomId) return; // Don't build URL until we have a room ID
|
|
416
|
-
|
|
417
|
-
(async () => {
|
|
418
|
-
const url = await urlBuilder.buildControllerUrl(parsedRoomId, {
|
|
419
|
-
host: config.publicHost,
|
|
420
|
-
});
|
|
421
|
-
setJoinUrl(url);
|
|
422
|
-
})();
|
|
423
|
-
}, [parsedRoomId, config.publicHost, options.roomId]);
|
|
424
|
-
|
|
425
|
-
// Get setRegisteredRoomId from store
|
|
426
|
-
const setRegisteredRoomId = useStore(store, (s) => s.setRegisteredRoomId);
|
|
427
|
-
|
|
428
|
-
// Main connection effect
|
|
429
|
-
useEffect(() => {
|
|
430
|
-
const storeState = store.getState();
|
|
431
|
-
storeState.setMode(detectRunMode());
|
|
432
|
-
storeState.setRole("host");
|
|
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
|
-
}
|
|
438
|
-
storeState.setStatus("connecting");
|
|
439
|
-
storeState.setError(undefined);
|
|
440
|
-
|
|
441
|
-
if (!shouldConnect || !socket) {
|
|
442
|
-
storeState.setStatus("idle");
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
const registerHost = async () => {
|
|
447
|
-
const storeState = store.getState();
|
|
448
|
-
|
|
449
|
-
// Prevent duplicate registration
|
|
450
|
-
const currentRegisteredRoomId = store.getState().registeredRoomId;
|
|
451
|
-
if (currentRegisteredRoomId && socket.connected) {
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
if (arcadeParams) {
|
|
456
|
-
// Child Mode - join as child (unchanged)
|
|
457
|
-
const childRoomId = roomCodeSchema.parse(
|
|
458
|
-
arcadeParams.room.toUpperCase(),
|
|
459
|
-
);
|
|
460
|
-
const payload = {
|
|
461
|
-
roomId: childRoomId,
|
|
462
|
-
joinToken: arcadeParams.token,
|
|
463
|
-
};
|
|
464
|
-
socket.emit("host:joinAsChild", payload, (ack) => {
|
|
465
|
-
if (!ack.ok) {
|
|
466
|
-
storeState.setError(ack.message ?? "Failed to join as child");
|
|
467
|
-
storeState.setStatus("disconnected");
|
|
468
|
-
setRegisteredRoomId(null);
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
storeState.setStatus("connected");
|
|
472
|
-
storeState.setRoomId(childRoomId);
|
|
473
|
-
setRegisteredRoomId(childRoomId);
|
|
474
|
-
});
|
|
475
|
-
} else {
|
|
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
|
-
};
|
|
506
|
-
|
|
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
|
-
});
|
|
530
|
-
} else {
|
|
531
|
-
// No saved room ID, create new room
|
|
532
|
-
createNewRoom();
|
|
533
|
-
}
|
|
534
|
-
} else {
|
|
535
|
-
// Not in browser environment, create new room
|
|
536
|
-
createNewRoom();
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
};
|
|
540
|
-
|
|
541
|
-
const handleConnect = (): void => {
|
|
542
|
-
store.getState().setStatus("connected");
|
|
543
|
-
registerHost();
|
|
544
|
-
};
|
|
545
|
-
|
|
546
|
-
const handleDisconnect = (): void => {
|
|
547
|
-
store.getState().setStatus("disconnected");
|
|
548
|
-
};
|
|
549
|
-
|
|
550
|
-
const handleJoin = (payload: {
|
|
551
|
-
controllerId: string;
|
|
552
|
-
nickname?: string;
|
|
553
|
-
player?: PlayerProfile;
|
|
554
|
-
}): void => {
|
|
555
|
-
if (payload.player) {
|
|
556
|
-
store.getState().upsertPlayer(payload.player);
|
|
557
|
-
setTimeout(() => {
|
|
558
|
-
onPlayerJoinRef.current?.(payload.player!);
|
|
559
|
-
}, 0);
|
|
560
|
-
}
|
|
561
|
-
};
|
|
562
|
-
|
|
563
|
-
const handleLeave = (payload: { controllerId: string }): void => {
|
|
564
|
-
store.getState().removePlayer(payload.controllerId);
|
|
565
|
-
onPlayerLeaveRef.current?.(payload.controllerId);
|
|
566
|
-
};
|
|
567
|
-
|
|
568
|
-
const handleInput = (payload: ControllerInputEvent): void => {
|
|
569
|
-
// Handle input via InputManager from context (if configured in provider)
|
|
570
|
-
if (inputManager) {
|
|
571
|
-
inputManager.handleInput(payload);
|
|
572
|
-
}
|
|
573
|
-
};
|
|
574
|
-
|
|
575
|
-
const handleChildClose = (): void => {
|
|
576
|
-
onChildCloseRef.current?.();
|
|
577
|
-
};
|
|
578
|
-
|
|
579
|
-
const handleState = (payload: ControllerStateMessage): void => {
|
|
580
|
-
if (!parsedRoomId || payload.roomId !== parsedRoomId) return;
|
|
581
|
-
|
|
582
|
-
const storeState = store.getState();
|
|
583
|
-
if (payload.state.gameState) {
|
|
584
|
-
storeState.setGameState(payload.state.gameState);
|
|
585
|
-
}
|
|
586
|
-
if (payload.state.message !== undefined) {
|
|
587
|
-
storeState.setStateMessage(payload.state.message);
|
|
588
|
-
}
|
|
589
|
-
};
|
|
590
|
-
|
|
591
|
-
const handleError = (payload: { message: string }): void => {
|
|
592
|
-
store.getState().setError(payload.message);
|
|
593
|
-
};
|
|
594
|
-
|
|
595
|
-
socket.on("connect", handleConnect);
|
|
596
|
-
socket.on("disconnect", handleDisconnect);
|
|
597
|
-
socket.on("server:controllerJoined", handleJoin);
|
|
598
|
-
socket.on("server:controllerLeft", handleLeave);
|
|
599
|
-
socket.on("server:input", handleInput);
|
|
600
|
-
socket.on("server:error", handleError);
|
|
601
|
-
socket.on("server:closeChild", handleChildClose);
|
|
602
|
-
socket.on("server:state", handleState);
|
|
603
|
-
|
|
604
|
-
// Connect the socket
|
|
605
|
-
socket.connect();
|
|
606
|
-
|
|
607
|
-
// If socket is already connected, register immediately
|
|
608
|
-
const currentRegisteredRoomId = store.getState().registeredRoomId;
|
|
609
|
-
if (socket.connected && !currentRegisteredRoomId) {
|
|
610
|
-
registerHost();
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
return () => {
|
|
614
|
-
socket.off("connect", handleConnect);
|
|
615
|
-
socket.off("disconnect", handleDisconnect);
|
|
616
|
-
socket.off("server:controllerJoined", handleJoin);
|
|
617
|
-
socket.off("server:controllerLeft", handleLeave);
|
|
618
|
-
socket.off("server:input", handleInput);
|
|
619
|
-
socket.off("server:error", handleError);
|
|
620
|
-
socket.off("server:closeChild", handleChildClose);
|
|
621
|
-
socket.off("server:state", handleState);
|
|
622
|
-
};
|
|
623
|
-
}, [
|
|
624
|
-
config.maxPlayers,
|
|
625
|
-
config.apiKey,
|
|
626
|
-
options.maxPlayers,
|
|
627
|
-
options.apiKey,
|
|
628
|
-
arcadeParams,
|
|
629
|
-
parsedRoomId, // Include parsedRoomId since it's used in handleState callback
|
|
630
|
-
shouldConnect,
|
|
631
|
-
socket,
|
|
295
|
+
export function useAirJamHost<
|
|
296
|
+
TSchema extends z.ZodSchema = z.ZodSchema,
|
|
297
|
+
>(): AirJamHostApi<TSchema>;
|
|
298
|
+
export function useAirJamHost<TSelected>(
|
|
299
|
+
selector: (state: AirJamHostState) => TSelected,
|
|
300
|
+
): TSelected;
|
|
301
|
+
export function useAirJamHost<
|
|
302
|
+
TSchema extends z.ZodSchema = z.ZodSchema,
|
|
303
|
+
TSelected = AirJamHostApi<TSchema>,
|
|
304
|
+
>(
|
|
305
|
+
selector?: (state: AirJamHostState) => TSelected,
|
|
306
|
+
): AirJamHostApi<TSchema> | TSelected {
|
|
307
|
+
useAssertSessionScope("host", "useAirJamHost");
|
|
308
|
+
|
|
309
|
+
const { store } = useAirJamContext();
|
|
310
|
+
const selectedState = useStore(
|
|
632
311
|
store,
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
// Ensure we return a valid socket
|
|
638
|
-
const returnSocket = socket ?? getSocket("host");
|
|
639
|
-
|
|
640
|
-
// Create getInput function using InputManager from context
|
|
641
|
-
const getInput = useCallback(
|
|
642
|
-
(controllerId: string): z.infer<TSchema> | undefined => {
|
|
643
|
-
if (!inputManager) {
|
|
644
|
-
return undefined;
|
|
645
|
-
}
|
|
646
|
-
return inputManager.getInput(controllerId) as
|
|
647
|
-
| z.infer<TSchema>
|
|
648
|
-
| undefined;
|
|
649
|
-
},
|
|
650
|
-
[inputManager],
|
|
312
|
+
useShallow((state) => {
|
|
313
|
+
const hostState = toHostState(state);
|
|
314
|
+
return selector ? selector(hostState) : hostState;
|
|
315
|
+
}),
|
|
651
316
|
);
|
|
317
|
+
const runtime = useContext(
|
|
318
|
+
hostRuntimeContext,
|
|
319
|
+
) as AirJamHostRuntimeControls<TSchema> | null;
|
|
320
|
+
if (!runtime) {
|
|
321
|
+
throw createAirJamDiagnosticError(
|
|
322
|
+
"AJ_SCOPE_MISMATCH",
|
|
323
|
+
"useAirJamHost requires a mounted host runtime boundary. Wrap the host tree with <AirJamHostRuntime> or <airjam.Host> before reading host state.",
|
|
324
|
+
{
|
|
325
|
+
hookName: "useAirJamHost",
|
|
326
|
+
expectedScope: "host",
|
|
327
|
+
receivedScope: "host",
|
|
328
|
+
},
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (selector) {
|
|
333
|
+
return selectedState as TSelected;
|
|
334
|
+
}
|
|
652
335
|
|
|
653
336
|
return {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
connectionStatus: connectionState.connectionStatus,
|
|
657
|
-
players: connectionState.players,
|
|
658
|
-
lastError: connectionState.lastError,
|
|
659
|
-
mode: connectionState.mode,
|
|
660
|
-
gameState: connectionState.gameState,
|
|
661
|
-
toggleGameState,
|
|
662
|
-
sendState,
|
|
663
|
-
sendSignal,
|
|
664
|
-
reconnect,
|
|
665
|
-
socket: returnSocket,
|
|
666
|
-
isChildMode,
|
|
667
|
-
getInput,
|
|
337
|
+
...(selectedState as AirJamHostState),
|
|
338
|
+
...runtime,
|
|
668
339
|
};
|
|
669
|
-
}
|
|
340
|
+
}
|