@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
|
@@ -11,50 +11,56 @@
|
|
|
11
11
|
* Controllers automatically handle:
|
|
12
12
|
* - Room joining via URL parameter (?room=XXXX) or props
|
|
13
13
|
* - Haptic feedback (vibration patterns on signal receipt)
|
|
14
|
-
*
|
|
14
|
+
*
|
|
15
|
+
* **Standalone:** joins using `?room=` or `roomId` (and optional `controllerId`).
|
|
16
|
+
*
|
|
17
|
+
* **Embedded controller iframe:** when `aj_room` + `aj_controller_id` are present, binds as the
|
|
18
|
+
* platform-injected sub-controller; see
|
|
19
|
+
* {@link ../runtime/embedded-runtime-adapters.readEmbeddedControllerChildSession}.
|
|
15
20
|
*/
|
|
16
|
-
import {
|
|
21
|
+
import { useContext } from "react";
|
|
17
22
|
import { useStore } from "zustand";
|
|
18
23
|
import { useShallow } from "zustand/react/shallow";
|
|
19
24
|
import { useAirJamContext } from "../context/air-jam-context";
|
|
20
|
-
import
|
|
25
|
+
import { useAssertSessionScope } from "../context/session-scope";
|
|
26
|
+
import { createAirJamDiagnosticError } from "../diagnostics";
|
|
21
27
|
import type {
|
|
22
28
|
ConnectionStatus,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
ControllerLeaveAck,
|
|
30
|
+
ControllerOrientation,
|
|
31
|
+
ControllerRoomSettingsState,
|
|
32
|
+
ControllerStatePayload,
|
|
33
|
+
ControllerUpdatePlayerProfileAck,
|
|
26
34
|
PlayerProfile,
|
|
35
|
+
PlayerProfilePatch,
|
|
27
36
|
RoomCode,
|
|
28
|
-
|
|
29
|
-
} from "../protocol";
|
|
30
|
-
import {
|
|
31
|
-
controllerInputSchema,
|
|
32
|
-
controllerJoinSchema,
|
|
33
|
-
controllerSystemSchema,
|
|
34
|
-
roomCodeSchema,
|
|
35
|
-
type ControllerStatePayload,
|
|
37
|
+
RuntimeState,
|
|
36
38
|
} from "../protocol";
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
39
|
+
import type { AirJamRealtimeClient } from "../runtime/realtime-client";
|
|
40
|
+
import { controllerRuntimeContext } from "../runtime/runtime-owner-contexts";
|
|
41
|
+
import type { AirJamStore } from "../state/connection-store";
|
|
39
42
|
|
|
40
43
|
/**
|
|
41
|
-
* Options for configuring the controller
|
|
44
|
+
* Options for configuring the controller runtime boundary.
|
|
42
45
|
*
|
|
43
|
-
* @example Basic usage (room from URL)
|
|
46
|
+
* @example Basic runtime boundary usage (room from URL)
|
|
44
47
|
* ```tsx
|
|
45
48
|
* // URL: https://yourgame.com/controller?room=ABCD
|
|
46
|
-
*
|
|
47
|
-
*
|
|
49
|
+
* <AirJamControllerRuntime>
|
|
50
|
+
* <ControllerView />
|
|
51
|
+
* </AirJamControllerRuntime>
|
|
48
52
|
* ```
|
|
49
53
|
*
|
|
50
54
|
* @example With nickname
|
|
51
55
|
* ```tsx
|
|
52
|
-
*
|
|
53
|
-
* nickname
|
|
54
|
-
* onState
|
|
55
|
-
* if (state.
|
|
56
|
-
* }
|
|
57
|
-
*
|
|
56
|
+
* <AirJamControllerRuntime
|
|
57
|
+
* nickname={playerName}
|
|
58
|
+
* onState={(state) => {
|
|
59
|
+
* if (state.runtimeState === "playing") startGame();
|
|
60
|
+
* }}
|
|
61
|
+
* >
|
|
62
|
+
* <ControllerView />
|
|
63
|
+
* </AirJamControllerRuntime>
|
|
58
64
|
* ```
|
|
59
65
|
*/
|
|
60
66
|
export interface AirJamControllerOptions {
|
|
@@ -69,16 +75,20 @@ export interface AirJamControllerOptions {
|
|
|
69
75
|
* If not provided, the server assigns a random label.
|
|
70
76
|
*/
|
|
71
77
|
nickname?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Preset avatar id sent on join (optional).
|
|
80
|
+
*/
|
|
81
|
+
avatarId?: string;
|
|
72
82
|
/**
|
|
73
83
|
* Controller ID for this instance.
|
|
74
84
|
* Auto-generated if not provided. Persists across reconnects.
|
|
75
85
|
*/
|
|
76
86
|
controllerId?: string;
|
|
77
87
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
88
|
+
* Optional privileged controller capability token.
|
|
89
|
+
* Undefined falls back to `aj_controller_cap` from the URL; null explicitly disables URL token use.
|
|
80
90
|
*/
|
|
81
|
-
|
|
91
|
+
capabilityToken?: string | null;
|
|
82
92
|
/**
|
|
83
93
|
* Called when the host sends a state update (game state, messages, etc.)
|
|
84
94
|
*/
|
|
@@ -100,63 +110,100 @@ export interface AirJamControllerApi {
|
|
|
100
110
|
/** Last error message, if any */
|
|
101
111
|
lastError?: string;
|
|
102
112
|
/** Current game state (paused or playing) */
|
|
103
|
-
|
|
113
|
+
runtimeState: RuntimeState;
|
|
114
|
+
/** Intended controller presentation orientation for the active game UI. */
|
|
115
|
+
controllerOrientation: ControllerOrientation;
|
|
104
116
|
/** Optional message from the host (e.g., "Get Ready!") */
|
|
105
117
|
stateMessage?: string;
|
|
106
|
-
/**
|
|
107
|
-
|
|
108
|
-
*
|
|
109
|
-
* @example Sending joystick and button input
|
|
110
|
-
* ```ts
|
|
111
|
-
* controller.sendInput({
|
|
112
|
-
* vector: { x: joystickX, y: joystickY },
|
|
113
|
-
* action: isFirePressed,
|
|
114
|
-
* ability: isAbilityPressed,
|
|
115
|
-
* timestamp: Date.now(),
|
|
116
|
-
* });
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
sendInput: (input: ControllerInputPayload) => boolean;
|
|
118
|
+
/** Current host-owned room settings mirrored onto the controller. */
|
|
119
|
+
roomSettings: ControllerRoomSettingsState;
|
|
120
120
|
/**
|
|
121
121
|
* Send a system command to the host.
|
|
122
122
|
*
|
|
123
|
-
* @example
|
|
123
|
+
* @example Pause or resume
|
|
124
124
|
* ```ts
|
|
125
|
-
* controller.sendSystemCommand("
|
|
125
|
+
* controller.sendSystemCommand("pause");
|
|
126
|
+
* controller.sendSystemCommand("resume");
|
|
126
127
|
* ```
|
|
127
128
|
*/
|
|
128
|
-
sendSystemCommand: (command: "exit" | "
|
|
129
|
+
sendSystemCommand: (command: "exit" | "pause" | "resume") => void;
|
|
129
130
|
/**
|
|
130
|
-
* Update the player nickname.
|
|
131
|
-
* Changes take effect on next connection.
|
|
131
|
+
* Update the player nickname (local draft for next join).
|
|
132
132
|
*/
|
|
133
133
|
setNickname: (value: string) => void;
|
|
134
|
+
/**
|
|
135
|
+
* Update preset avatar id (local draft for next join).
|
|
136
|
+
*/
|
|
137
|
+
setAvatarId: (value: string) => void;
|
|
138
|
+
/**
|
|
139
|
+
* Patch display name and/or avatar on the server without reconnecting.
|
|
140
|
+
* In Arcade (embedded iframe), updates are forwarded through the controller bridge; the room
|
|
141
|
+
* still learns the new name via `server:playerUpdated` (ack payload is not round-tripped).
|
|
142
|
+
*/
|
|
143
|
+
updatePlayerProfile: (
|
|
144
|
+
patch: PlayerProfilePatch,
|
|
145
|
+
) => Promise<ControllerUpdatePlayerProfileAck>;
|
|
146
|
+
/** Explicitly leave the current room and release the controller binding. */
|
|
147
|
+
leave: () => Promise<ControllerLeaveAck>;
|
|
134
148
|
/** Force reconnection to the room */
|
|
135
149
|
reconnect: () => void;
|
|
136
150
|
/** List of all connected players in the room */
|
|
137
151
|
players: PlayerProfile[];
|
|
138
|
-
/**
|
|
139
|
-
|
|
152
|
+
/** This controller's current player profile when known. */
|
|
153
|
+
selfPlayer: PlayerProfile | null;
|
|
154
|
+
/** Realtime client for controller events (socket-backed standalone, bridge-backed in arcade embeds) */
|
|
155
|
+
socket: AirJamRealtimeClient | null;
|
|
140
156
|
}
|
|
141
157
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
158
|
+
export interface AirJamControllerState {
|
|
159
|
+
roomId: RoomCode | null;
|
|
160
|
+
controllerId: string | null;
|
|
161
|
+
connectionStatus: ConnectionStatus;
|
|
162
|
+
lastError?: string;
|
|
163
|
+
runtimeState: RuntimeState;
|
|
164
|
+
controllerOrientation: ControllerOrientation;
|
|
165
|
+
stateMessage?: string;
|
|
166
|
+
roomSettings: ControllerRoomSettingsState;
|
|
167
|
+
players: PlayerProfile[];
|
|
168
|
+
selfPlayer: PlayerProfile | null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export type AirJamControllerRuntimeControls = Pick<
|
|
172
|
+
AirJamControllerApi,
|
|
173
|
+
| "sendSystemCommand"
|
|
174
|
+
| "setNickname"
|
|
175
|
+
| "setAvatarId"
|
|
176
|
+
| "updatePlayerProfile"
|
|
177
|
+
| "leave"
|
|
178
|
+
| "reconnect"
|
|
179
|
+
| "socket"
|
|
180
|
+
>;
|
|
181
|
+
|
|
182
|
+
const toControllerState = (state: AirJamStore): AirJamControllerState => {
|
|
183
|
+
const selfPlayer = state.controllerId
|
|
184
|
+
? (state.players.find((player) => player.id === state.controllerId) ?? null)
|
|
185
|
+
: null;
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
roomId: state.roomId,
|
|
189
|
+
controllerId: state.controllerId,
|
|
190
|
+
connectionStatus: state.connectionStatus,
|
|
191
|
+
lastError: state.lastError,
|
|
192
|
+
runtimeState: state.runtimeState,
|
|
193
|
+
controllerOrientation: state.controllerOrientation,
|
|
194
|
+
stateMessage: state.stateMessage,
|
|
195
|
+
roomSettings: state.roomSettings,
|
|
196
|
+
players: state.players,
|
|
197
|
+
selfPlayer,
|
|
198
|
+
};
|
|
149
199
|
};
|
|
150
200
|
|
|
151
201
|
/**
|
|
152
|
-
*
|
|
202
|
+
* Read the mounted controller runtime API.
|
|
153
203
|
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* - Haptic feedback on signal receipt (vibration)
|
|
158
|
-
* - Game state synchronization
|
|
159
|
-
* - Auto-reconnection on disconnect
|
|
204
|
+
* Use this inside `AirJamControllerRuntime` or another explicit controller runtime
|
|
205
|
+
* boundary such as `airjam.Controller`. Runtime ownership is mounted once at the
|
|
206
|
+
* boundary; child code reads from that runtime through this hook.
|
|
160
207
|
*
|
|
161
208
|
* **Typical usage flow:**
|
|
162
209
|
* 1. Player scans QR code on game screen
|
|
@@ -165,7 +212,6 @@ const getRoomFromLocation = (): string | null => {
|
|
|
165
212
|
* 4. Player uses joystick/buttons to send input
|
|
166
213
|
* 5. Receives haptic feedback on game events
|
|
167
214
|
*
|
|
168
|
-
* @param options - Configuration options
|
|
169
215
|
* @returns Controller API with state and functions
|
|
170
216
|
*
|
|
171
217
|
* @example Basic controller setup
|
|
@@ -173,31 +219,14 @@ const getRoomFromLocation = (): string | null => {
|
|
|
173
219
|
* const ControllerView = () => {
|
|
174
220
|
* const controller = useAirJamController();
|
|
175
221
|
*
|
|
176
|
-
* const handleJoystickMove = (x: number, y: number) => {
|
|
177
|
-
* controller.sendInput({
|
|
178
|
-
* vector: { x, y },
|
|
179
|
-
* action: false,
|
|
180
|
-
* timestamp: Date.now(),
|
|
181
|
-
* });
|
|
182
|
-
* };
|
|
183
|
-
*
|
|
184
|
-
* const handleFirePress = () => {
|
|
185
|
-
* controller.sendInput({
|
|
186
|
-
* vector: { x: 0, y: 0 },
|
|
187
|
-
* action: true,
|
|
188
|
-
* timestamp: Date.now(),
|
|
189
|
-
* });
|
|
190
|
-
* };
|
|
191
|
-
*
|
|
192
222
|
* if (controller.connectionStatus !== "connected") {
|
|
193
223
|
* return <div>Connecting to room {controller.roomId}...</div>;
|
|
194
224
|
* }
|
|
195
225
|
*
|
|
196
226
|
* return (
|
|
197
227
|
* <div>
|
|
198
|
-
* <
|
|
199
|
-
* <
|
|
200
|
-
* <p>Game: {controller.gameState}</p>
|
|
228
|
+
* <p>Connected: {controller.controllerId}</p>
|
|
229
|
+
* <p>Game: {controller.runtimeState}</p>
|
|
201
230
|
* </div>
|
|
202
231
|
* );
|
|
203
232
|
* };
|
|
@@ -205,404 +234,55 @@ const getRoomFromLocation = (): string | null => {
|
|
|
205
234
|
*
|
|
206
235
|
* @example With state callback
|
|
207
236
|
* ```tsx
|
|
208
|
-
*
|
|
209
|
-
* onState
|
|
237
|
+
* <AirJamControllerRuntime
|
|
238
|
+
* onState={(state) => {
|
|
210
239
|
* if (state.message) {
|
|
211
240
|
* showNotification(state.message);
|
|
212
241
|
* }
|
|
213
|
-
* }
|
|
214
|
-
*
|
|
242
|
+
* }}
|
|
243
|
+
* >
|
|
244
|
+
* <ControllerView />
|
|
245
|
+
* </AirJamControllerRuntime>
|
|
215
246
|
* ```
|
|
216
247
|
*/
|
|
217
|
-
export
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
const params = new URLSearchParams(window.location.search);
|
|
229
|
-
const room = params.get("aj_room");
|
|
230
|
-
const controllerId = params.get("aj_controller_id");
|
|
231
|
-
if (room && controllerId) {
|
|
232
|
-
return { room, controllerId };
|
|
233
|
-
}
|
|
234
|
-
return null;
|
|
235
|
-
}, []);
|
|
236
|
-
|
|
237
|
-
// Parse room ID
|
|
238
|
-
const parsedRoomId = useMemo<RoomCode | null>(() => {
|
|
239
|
-
const code =
|
|
240
|
-
subControllerParams?.room ?? options.roomId ?? getRoomFromLocation();
|
|
241
|
-
if (!code) return null;
|
|
242
|
-
try {
|
|
243
|
-
return roomCodeSchema.parse(code.toUpperCase());
|
|
244
|
-
} catch {
|
|
245
|
-
return null;
|
|
246
|
-
}
|
|
247
|
-
}, [options.roomId, subControllerParams]);
|
|
248
|
-
|
|
249
|
-
// Generate or use provided controller ID
|
|
250
|
-
const controllerId = useMemo<string>(() => {
|
|
251
|
-
if (subControllerParams?.controllerId) {
|
|
252
|
-
return subControllerParams.controllerId;
|
|
253
|
-
}
|
|
254
|
-
if (options.controllerId) {
|
|
255
|
-
return options.controllerId;
|
|
256
|
-
}
|
|
257
|
-
if (typeof window !== "undefined") {
|
|
258
|
-
const params = new URLSearchParams(window.location.search);
|
|
259
|
-
const urlControllerId = params.get("controllerId");
|
|
260
|
-
if (urlControllerId) return urlControllerId;
|
|
261
|
-
}
|
|
262
|
-
return generateControllerId();
|
|
263
|
-
}, [options.controllerId, subControllerParams]);
|
|
264
|
-
|
|
265
|
-
const onStateRef = useRef<AirJamControllerOptions["onState"]>(
|
|
266
|
-
options.onState,
|
|
267
|
-
);
|
|
268
|
-
useEffect(() => {
|
|
269
|
-
onStateRef.current = options.onState;
|
|
270
|
-
}, [options.onState]);
|
|
271
|
-
|
|
272
|
-
// Subscribe to store state
|
|
273
|
-
const connectionState = useStore(
|
|
248
|
+
export function useAirJamController(): AirJamControllerApi;
|
|
249
|
+
export function useAirJamController<TSelected>(
|
|
250
|
+
selector: (state: AirJamControllerState) => TSelected,
|
|
251
|
+
): TSelected;
|
|
252
|
+
export function useAirJamController<TSelected>(
|
|
253
|
+
selector?: (state: AirJamControllerState) => TSelected,
|
|
254
|
+
): AirJamControllerApi | TSelected {
|
|
255
|
+
useAssertSessionScope("controller", "useAirJamController");
|
|
256
|
+
|
|
257
|
+
const { store } = useAirJamContext();
|
|
258
|
+
const selectedState = useStore(
|
|
274
259
|
store,
|
|
275
|
-
useShallow((state) =>
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
players: state.players,
|
|
280
|
-
gameState: state.gameState,
|
|
281
|
-
stateMessage: state.stateMessage,
|
|
282
|
-
})),
|
|
260
|
+
useShallow((state) => {
|
|
261
|
+
const controllerState = toControllerState(state);
|
|
262
|
+
return selector ? selector(controllerState) : controllerState;
|
|
263
|
+
}),
|
|
283
264
|
);
|
|
265
|
+
const runtime = useContext(
|
|
266
|
+
controllerRuntimeContext,
|
|
267
|
+
) as AirJamControllerRuntimeControls | null;
|
|
268
|
+
if (!runtime) {
|
|
269
|
+
throw createAirJamDiagnosticError(
|
|
270
|
+
"AJ_SCOPE_MISMATCH",
|
|
271
|
+
"useAirJamController requires a mounted controller runtime boundary. Wrap the controller tree with <AirJamControllerRuntime> or <airjam.Controller> before reading controller state.",
|
|
272
|
+
{
|
|
273
|
+
hookName: "useAirJamController",
|
|
274
|
+
expectedScope: "controller",
|
|
275
|
+
receivedScope: "controller",
|
|
276
|
+
},
|
|
277
|
+
);
|
|
278
|
+
}
|
|
284
279
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
const reconnect = useCallback(() => {
|
|
289
|
-
if (!parsedRoomId) return;
|
|
290
|
-
disconnectSocket("controller");
|
|
291
|
-
setReconnectKey((prev) => prev + 1);
|
|
292
|
-
}, [parsedRoomId, disconnectSocket]);
|
|
293
|
-
|
|
294
|
-
const isChildMode = useMemo(() => {
|
|
295
|
-
if (typeof window === "undefined") return false;
|
|
296
|
-
const params = new URLSearchParams(window.location.search);
|
|
297
|
-
return params.get("airjam_mode") === "child";
|
|
298
|
-
}, []);
|
|
299
|
-
|
|
300
|
-
const shouldConnect = useMemo(() => {
|
|
301
|
-
if (options.forceConnect) return true;
|
|
302
|
-
if (subControllerParams) return true;
|
|
303
|
-
if (typeof window !== "undefined") {
|
|
304
|
-
const params = new URLSearchParams(window.location.search);
|
|
305
|
-
return params.get("airjam_force_connect") === "true";
|
|
306
|
-
}
|
|
307
|
-
return false;
|
|
308
|
-
}, [options.forceConnect, subControllerParams]);
|
|
309
|
-
|
|
310
|
-
const canConnect = useMemo(
|
|
311
|
-
() => !isChildMode || shouldConnect,
|
|
312
|
-
[isChildMode, shouldConnect],
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
// Get socket from context (only if can connect and have room)
|
|
316
|
-
const socket = useMemo(
|
|
317
|
-
() => (canConnect && parsedRoomId ? getSocket("controller") : null),
|
|
318
|
-
[canConnect, parsedRoomId, getSocket],
|
|
319
|
-
);
|
|
320
|
-
|
|
321
|
-
// Automatically listen for haptic signals
|
|
322
|
-
useEffect(() => {
|
|
323
|
-
if (!socket) return;
|
|
324
|
-
|
|
325
|
-
const handleSignal = (signal: SignalPayload) => {
|
|
326
|
-
if (signal.type !== "HAPTIC") return;
|
|
327
|
-
|
|
328
|
-
// Browser compatibility check
|
|
329
|
-
if (typeof navigator === "undefined" || !navigator.vibrate) return;
|
|
330
|
-
|
|
331
|
-
// TypeScript now knows payload is HapticSignalPayload
|
|
332
|
-
const payload = signal.payload;
|
|
333
|
-
|
|
334
|
-
switch (payload.pattern) {
|
|
335
|
-
case "light":
|
|
336
|
-
navigator.vibrate(10);
|
|
337
|
-
break;
|
|
338
|
-
case "medium":
|
|
339
|
-
navigator.vibrate(30);
|
|
340
|
-
break;
|
|
341
|
-
case "heavy":
|
|
342
|
-
navigator.vibrate([50, 20, 50]); // Pulse
|
|
343
|
-
break;
|
|
344
|
-
case "success":
|
|
345
|
-
navigator.vibrate([10, 30, 10]);
|
|
346
|
-
break;
|
|
347
|
-
case "failure":
|
|
348
|
-
navigator.vibrate([50, 50, 50, 50]);
|
|
349
|
-
break;
|
|
350
|
-
case "custom":
|
|
351
|
-
if (Array.isArray(payload.sequence)) {
|
|
352
|
-
navigator.vibrate(payload.sequence);
|
|
353
|
-
} else if (typeof payload.sequence === "number") {
|
|
354
|
-
navigator.vibrate(payload.sequence);
|
|
355
|
-
}
|
|
356
|
-
break;
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
socket.on("server:signal", handleSignal);
|
|
361
|
-
return () => {
|
|
362
|
-
socket.off("server:signal", handleSignal);
|
|
363
|
-
};
|
|
364
|
-
}, [socket]);
|
|
365
|
-
|
|
366
|
-
// Main connection effect
|
|
367
|
-
useEffect(() => {
|
|
368
|
-
const storeState = store.getState();
|
|
369
|
-
storeState.setMode(detectRunMode());
|
|
370
|
-
storeState.setRole("controller");
|
|
371
|
-
storeState.setRoomId(parsedRoomId);
|
|
372
|
-
storeState.setStatus(parsedRoomId ? "connecting" : "idle");
|
|
373
|
-
storeState.setError(undefined);
|
|
374
|
-
|
|
375
|
-
if (!canConnect) {
|
|
376
|
-
// --- BRIDGE MODE (Iframe Child) ---
|
|
377
|
-
storeState.setStatus("connected");
|
|
378
|
-
|
|
379
|
-
const handleMessage = (event: MessageEvent) => {
|
|
380
|
-
if (event.source !== window.parent) return;
|
|
381
|
-
|
|
382
|
-
const data = event.data;
|
|
383
|
-
if (data?.type === "AIRJAM_STATE") {
|
|
384
|
-
const payload = data.payload as ControllerStatePayload;
|
|
385
|
-
if (payload.gameState) storeState.setGameState(payload.gameState);
|
|
386
|
-
if (payload.message !== undefined)
|
|
387
|
-
storeState.setStateMessage(payload.message);
|
|
388
|
-
onStateRef.current?.(payload);
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
window.addEventListener("message", handleMessage);
|
|
393
|
-
window.parent.postMessage({ type: "AIRJAM_READY" }, "*");
|
|
394
|
-
|
|
395
|
-
return () => {
|
|
396
|
-
window.removeEventListener("message", handleMessage);
|
|
397
|
-
storeState.setStatus("disconnected");
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// --- NORMAL MODE (Direct Socket Connection) ---
|
|
402
|
-
if (!parsedRoomId || !socket || !controllerId) {
|
|
403
|
-
storeState.setStatus("idle");
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
storeState.setControllerId(controllerId);
|
|
408
|
-
|
|
409
|
-
const handleConnect = (): void => {
|
|
410
|
-
store.getState().setStatus("connected");
|
|
411
|
-
|
|
412
|
-
if (subControllerParams) {
|
|
413
|
-
// Sub-Controller Mode - skip join
|
|
414
|
-
return;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
const payload = controllerJoinSchema.parse({
|
|
418
|
-
roomId: parsedRoomId,
|
|
419
|
-
controllerId,
|
|
420
|
-
nickname: nicknameRef.current || undefined,
|
|
421
|
-
});
|
|
422
|
-
socket.emit("controller:join", payload, (ack) => {
|
|
423
|
-
const storeState = store.getState();
|
|
424
|
-
if (!ack.ok) {
|
|
425
|
-
storeState.setError(ack.message ?? "Unable to join room");
|
|
426
|
-
storeState.setStatus("disconnected");
|
|
427
|
-
return;
|
|
428
|
-
}
|
|
429
|
-
if (ack.controllerId) {
|
|
430
|
-
storeState.setControllerId(ack.controllerId);
|
|
431
|
-
}
|
|
432
|
-
storeState.setStatus("connected");
|
|
433
|
-
});
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
const handleDisconnect = (): void => {
|
|
437
|
-
store.getState().setStatus("disconnected");
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
const handleWelcome = (payload: {
|
|
441
|
-
controllerId: string;
|
|
442
|
-
roomId: RoomCode;
|
|
443
|
-
player?: PlayerProfile;
|
|
444
|
-
}): void => {
|
|
445
|
-
const storeState = store.getState();
|
|
446
|
-
const storeRoomId = storeState.roomId;
|
|
447
|
-
if (
|
|
448
|
-
storeRoomId &&
|
|
449
|
-
payload.roomId.toUpperCase() !== storeRoomId.toUpperCase()
|
|
450
|
-
) {
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (!storeRoomId && payload.roomId) {
|
|
454
|
-
storeState.setRoomId(payload.roomId);
|
|
455
|
-
}
|
|
456
|
-
if (!payload.player) {
|
|
457
|
-
storeState.setError(
|
|
458
|
-
"Welcome message received but no player profile included.",
|
|
459
|
-
);
|
|
460
|
-
return;
|
|
461
|
-
}
|
|
462
|
-
storeState.upsertPlayer(payload.player);
|
|
463
|
-
};
|
|
464
|
-
|
|
465
|
-
const handleState = (payload: ControllerStateMessage): void => {
|
|
466
|
-
if (payload.roomId !== parsedRoomId) return;
|
|
467
|
-
|
|
468
|
-
const storeState = store.getState();
|
|
469
|
-
if (payload.state.gameState) {
|
|
470
|
-
storeState.setGameState(payload.state.gameState);
|
|
471
|
-
}
|
|
472
|
-
if (payload.state.message !== undefined) {
|
|
473
|
-
storeState.setStateMessage(payload.state.message);
|
|
474
|
-
}
|
|
475
|
-
onStateRef.current?.(payload.state);
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
const handleHostLeft = (payload: { reason: string }): void => {
|
|
479
|
-
const storeState = store.getState();
|
|
480
|
-
storeState.setError(payload.reason);
|
|
481
|
-
storeState.setStatus("disconnected");
|
|
482
|
-
storeState.resetGameState();
|
|
483
|
-
|
|
484
|
-
setTimeout(() => {
|
|
485
|
-
disconnectSocket("controller");
|
|
486
|
-
setReconnectKey((prev) => prev + 1);
|
|
487
|
-
}, 1000);
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
const handleError = (payload: { message: string }): void => {
|
|
491
|
-
store.getState().setError(payload.message);
|
|
492
|
-
};
|
|
493
|
-
|
|
494
|
-
socket.on("connect", handleConnect);
|
|
495
|
-
socket.on("disconnect", handleDisconnect);
|
|
496
|
-
socket.on("server:welcome", handleWelcome);
|
|
497
|
-
socket.on("server:state", handleState);
|
|
498
|
-
socket.on("server:hostLeft", handleHostLeft);
|
|
499
|
-
socket.on("server:error", handleError);
|
|
500
|
-
|
|
501
|
-
// Connect the socket
|
|
502
|
-
socket.connect();
|
|
503
|
-
|
|
504
|
-
return () => {
|
|
505
|
-
socket.off("connect", handleConnect);
|
|
506
|
-
socket.off("disconnect", handleDisconnect);
|
|
507
|
-
socket.off("server:welcome", handleWelcome);
|
|
508
|
-
socket.off("server:state", handleState);
|
|
509
|
-
socket.off("server:hostLeft", handleHostLeft);
|
|
510
|
-
socket.off("server:error", handleError);
|
|
511
|
-
};
|
|
512
|
-
}, [
|
|
513
|
-
parsedRoomId,
|
|
514
|
-
reconnectKey,
|
|
515
|
-
canConnect,
|
|
516
|
-
socket,
|
|
517
|
-
controllerId,
|
|
518
|
-
subControllerParams,
|
|
519
|
-
store,
|
|
520
|
-
disconnectSocket,
|
|
521
|
-
]);
|
|
522
|
-
|
|
523
|
-
const setNickname = useCallback((value: string) => {
|
|
524
|
-
nicknameRef.current = value;
|
|
525
|
-
}, []);
|
|
526
|
-
|
|
527
|
-
const sendInput = useCallback(
|
|
528
|
-
(input: ControllerInputPayload): boolean => {
|
|
529
|
-
const storeState = store.getState();
|
|
530
|
-
|
|
531
|
-
if (!canConnect) {
|
|
532
|
-
// Bridge Mode
|
|
533
|
-
if (typeof window !== "undefined") {
|
|
534
|
-
window.parent.postMessage(
|
|
535
|
-
{
|
|
536
|
-
type: "AIRJAM_INPUT",
|
|
537
|
-
payload: input,
|
|
538
|
-
},
|
|
539
|
-
"*",
|
|
540
|
-
);
|
|
541
|
-
}
|
|
542
|
-
return true;
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
if (!parsedRoomId || !storeState.controllerId || !socket) {
|
|
546
|
-
storeState.setError("Not connected to a room");
|
|
547
|
-
return false;
|
|
548
|
-
}
|
|
549
|
-
if (!socket.connected) {
|
|
550
|
-
return false;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
554
|
-
storeState.setError("Input must be an object");
|
|
555
|
-
return false;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
const payload = controllerInputSchema.safeParse({
|
|
559
|
-
roomId: parsedRoomId,
|
|
560
|
-
controllerId: storeState.controllerId,
|
|
561
|
-
input,
|
|
562
|
-
});
|
|
563
|
-
if (!payload.success) {
|
|
564
|
-
storeState.setError(payload.error.message);
|
|
565
|
-
return false;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
socket.emit("controller:input", payload.data);
|
|
569
|
-
return true;
|
|
570
|
-
},
|
|
571
|
-
[parsedRoomId, canConnect, socket, store],
|
|
572
|
-
);
|
|
573
|
-
|
|
574
|
-
const sendSystemCommand = useCallback(
|
|
575
|
-
(command: "exit" | "ready" | "toggle_pause") => {
|
|
576
|
-
const storeState = store.getState();
|
|
577
|
-
|
|
578
|
-
if (!canConnect) return;
|
|
579
|
-
if (!parsedRoomId || !storeState.controllerId || !socket) return;
|
|
580
|
-
if (!socket.connected) return;
|
|
581
|
-
|
|
582
|
-
const payload = controllerSystemSchema.safeParse({
|
|
583
|
-
roomId: parsedRoomId,
|
|
584
|
-
command,
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
if (payload.success) {
|
|
588
|
-
socket.emit("controller:system", payload.data);
|
|
589
|
-
}
|
|
590
|
-
},
|
|
591
|
-
[parsedRoomId, canConnect, socket, store],
|
|
592
|
-
);
|
|
280
|
+
if (selector) {
|
|
281
|
+
return selectedState as TSelected;
|
|
282
|
+
}
|
|
593
283
|
|
|
594
284
|
return {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
connectionStatus: connectionState.connectionStatus,
|
|
598
|
-
lastError: connectionState.lastError,
|
|
599
|
-
gameState: connectionState.gameState,
|
|
600
|
-
stateMessage: connectionState.stateMessage,
|
|
601
|
-
sendInput,
|
|
602
|
-
sendSystemCommand,
|
|
603
|
-
setNickname,
|
|
604
|
-
reconnect,
|
|
605
|
-
players: connectionState.players,
|
|
606
|
-
socket,
|
|
285
|
+
...(selectedState as AirJamControllerState),
|
|
286
|
+
...runtime,
|
|
607
287
|
};
|
|
608
|
-
}
|
|
288
|
+
}
|