@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
|
@@ -0,0 +1,1051 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
import { z as zod } from "zod";
|
|
4
|
+
import { useStore } from "zustand";
|
|
5
|
+
import { useAirJamContext } from "../../context/air-jam-context";
|
|
6
|
+
import {
|
|
7
|
+
useAssertSessionScope,
|
|
8
|
+
useClaimSessionRuntimeOwner,
|
|
9
|
+
} from "../../context/session-scope";
|
|
10
|
+
import { updateDevBrowserLogContext } from "../../dev/browser-log-sink";
|
|
11
|
+
import { emitAirJamDiagnostic } from "../../diagnostics";
|
|
12
|
+
import type {
|
|
13
|
+
ControllerInputEvent,
|
|
14
|
+
ControllerJoinedNotice,
|
|
15
|
+
ControllerPresenceNotice,
|
|
16
|
+
ControllerPrivilegedCapability,
|
|
17
|
+
ControllerStateMessage,
|
|
18
|
+
ControllerStatePayload,
|
|
19
|
+
HapticSignalPayload,
|
|
20
|
+
HostBootstrapAck,
|
|
21
|
+
HostBootstrapPayload,
|
|
22
|
+
HostRegistrationAck,
|
|
23
|
+
PlayerProfile,
|
|
24
|
+
RoomCode,
|
|
25
|
+
RuntimeState,
|
|
26
|
+
SignalPayload,
|
|
27
|
+
SignalType,
|
|
28
|
+
ToastSignalPayload,
|
|
29
|
+
} from "../../protocol";
|
|
30
|
+
import {
|
|
31
|
+
AIRJAM_DEV_LOG_EVENTS,
|
|
32
|
+
controllerStateSchema,
|
|
33
|
+
ErrorCode,
|
|
34
|
+
hostBootstrapSchema,
|
|
35
|
+
hostCreateRoomSchema,
|
|
36
|
+
hostReconnectSchema,
|
|
37
|
+
hostRemoveControllerSchema,
|
|
38
|
+
hostResetRoomSchema,
|
|
39
|
+
roomCodeSchema,
|
|
40
|
+
} from "../../protocol";
|
|
41
|
+
import type { PlayerUpdatedNotice } from "../../protocol/notices";
|
|
42
|
+
import { emitAirJamDevRuntimeEvent } from "../../runtime/dev-runtime-events";
|
|
43
|
+
import { readEmbeddedHostChildSession } from "../../runtime/embedded-runtime-adapters";
|
|
44
|
+
import { getHostRealtimeClient } from "../../runtime/host-realtime-client";
|
|
45
|
+
import type { AirJamRealtimeClient } from "../../runtime/realtime-client";
|
|
46
|
+
import { detectRunMode } from "../../utils/mode";
|
|
47
|
+
import { urlBuilder } from "../../utils/url-builder";
|
|
48
|
+
import type {
|
|
49
|
+
AirJamHostApi,
|
|
50
|
+
AirJamHostOptions,
|
|
51
|
+
AirJamHostRuntimeControls,
|
|
52
|
+
JoinUrlStatus,
|
|
53
|
+
} from "../use-air-jam-host";
|
|
54
|
+
|
|
55
|
+
const HOST_BOOTSTRAP_TIMEOUT_MESSAGE =
|
|
56
|
+
"Host bootstrap timed out. The deployed Air Jam server may be out of sync with this client.";
|
|
57
|
+
|
|
58
|
+
export const useHostRuntimeApi = <TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
59
|
+
options: AirJamHostOptions,
|
|
60
|
+
hookName: string,
|
|
61
|
+
): AirJamHostRuntimeControls<TSchema> => {
|
|
62
|
+
useAssertSessionScope("host", hookName);
|
|
63
|
+
useClaimSessionRuntimeOwner("host-runtime", hookName);
|
|
64
|
+
|
|
65
|
+
const { config, store, getSocket, disconnectSocket, inputManager } =
|
|
66
|
+
useAirJamContext();
|
|
67
|
+
|
|
68
|
+
const embeddedHost = useMemo(() => readEmbeddedHostChildSession(), []);
|
|
69
|
+
|
|
70
|
+
const shouldConnect = true;
|
|
71
|
+
const storeRoomId = useStore(store, (s) => s.roomId);
|
|
72
|
+
|
|
73
|
+
const parsedRoomId = useMemo<RoomCode | null>(() => {
|
|
74
|
+
if (embeddedHost) {
|
|
75
|
+
return embeddedHost.roomId;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (storeRoomId) {
|
|
79
|
+
return roomCodeSchema.parse(storeRoomId.toUpperCase());
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (typeof window !== "undefined") {
|
|
83
|
+
const params = new URLSearchParams(window.location.search);
|
|
84
|
+
const paramRoom = params.get("room");
|
|
85
|
+
if (paramRoom) {
|
|
86
|
+
const result = roomCodeSchema.safeParse(paramRoom.toUpperCase());
|
|
87
|
+
if (result.success) return result.data;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (options.roomId) {
|
|
92
|
+
return roomCodeSchema.parse(options.roomId.toUpperCase());
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return null;
|
|
96
|
+
}, [options.roomId, storeRoomId, embeddedHost]);
|
|
97
|
+
|
|
98
|
+
const [controllerCapability, setControllerCapability] =
|
|
99
|
+
useState<ControllerPrivilegedCapability | null>(null);
|
|
100
|
+
|
|
101
|
+
const joinUrlBuildKey = useMemo(
|
|
102
|
+
() =>
|
|
103
|
+
parsedRoomId
|
|
104
|
+
? `${parsedRoomId}\0${config.publicHost ?? ""}\0${controllerCapability?.token ?? ""}`
|
|
105
|
+
: null,
|
|
106
|
+
[config.publicHost, controllerCapability?.token, parsedRoomId],
|
|
107
|
+
);
|
|
108
|
+
const [computedJoinUrl, setComputedJoinUrl] = useState<{
|
|
109
|
+
key: string | null;
|
|
110
|
+
url: string;
|
|
111
|
+
error: boolean;
|
|
112
|
+
}>({
|
|
113
|
+
key: null,
|
|
114
|
+
url: "",
|
|
115
|
+
error: false,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const onPlayerJoinRef = useRef(options.onPlayerJoin);
|
|
119
|
+
const onPlayerLeaveRef = useRef(options.onPlayerLeave);
|
|
120
|
+
const parsedRoomIdRef = useRef<RoomCode | null>(parsedRoomId);
|
|
121
|
+
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
onPlayerJoinRef.current = options.onPlayerJoin;
|
|
124
|
+
onPlayerLeaveRef.current = options.onPlayerLeave;
|
|
125
|
+
}, [options.onPlayerJoin, options.onPlayerLeave]);
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
parsedRoomIdRef.current = parsedRoomId;
|
|
129
|
+
}, [parsedRoomId]);
|
|
130
|
+
|
|
131
|
+
const socket = useMemo<AirJamRealtimeClient | null>(
|
|
132
|
+
() =>
|
|
133
|
+
shouldConnect ? getHostRealtimeClient((role) => getSocket(role)) : null,
|
|
134
|
+
[shouldConnect, getSocket],
|
|
135
|
+
);
|
|
136
|
+
const setRegisteredRoomId = useStore(store, (s) => s.setRegisteredRoomId);
|
|
137
|
+
|
|
138
|
+
const hydrateHostRoster = useCallback(
|
|
139
|
+
(players?: PlayerProfile[], controllers?: ControllerPresenceNotice[]) => {
|
|
140
|
+
const latestState = store.getState();
|
|
141
|
+
latestState.resetPlayers();
|
|
142
|
+
players?.forEach((player) => {
|
|
143
|
+
latestState.upsertPlayer(player);
|
|
144
|
+
});
|
|
145
|
+
latestState.resetControllerSessions();
|
|
146
|
+
controllers?.forEach((controller) => {
|
|
147
|
+
latestState.upsertControllerSession(controller);
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
[store],
|
|
151
|
+
);
|
|
152
|
+
const canEmitAuthoritativeHostState = useCallback(
|
|
153
|
+
(roomId: RoomCode | null): roomId is RoomCode => {
|
|
154
|
+
if (!socket || !socket.connected || !roomId) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const latestState = store.getState();
|
|
159
|
+
return (
|
|
160
|
+
latestState.registeredRoomId === roomId &&
|
|
161
|
+
latestState.hostArcadeRestore.phase === "idle"
|
|
162
|
+
);
|
|
163
|
+
},
|
|
164
|
+
[socket, store],
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const emitHostRuntimeEvent = useCallback(
|
|
168
|
+
({
|
|
169
|
+
event,
|
|
170
|
+
level = "info",
|
|
171
|
+
message,
|
|
172
|
+
roomId,
|
|
173
|
+
data,
|
|
174
|
+
}: {
|
|
175
|
+
event: (typeof AIRJAM_DEV_LOG_EVENTS.runtime)[keyof typeof AIRJAM_DEV_LOG_EVENTS.runtime];
|
|
176
|
+
level?: "info" | "warn" | "error";
|
|
177
|
+
message: string;
|
|
178
|
+
roomId?: string;
|
|
179
|
+
data?: Record<string, unknown>;
|
|
180
|
+
}) => {
|
|
181
|
+
emitAirJamDevRuntimeEvent({
|
|
182
|
+
event,
|
|
183
|
+
level,
|
|
184
|
+
message,
|
|
185
|
+
role: "host",
|
|
186
|
+
roomId,
|
|
187
|
+
data,
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
[],
|
|
191
|
+
);
|
|
192
|
+
const lastObservedStateVersionRef = useRef<number | null>(null);
|
|
193
|
+
const emittedInvariantKeysRef = useRef<Set<string>>(new Set());
|
|
194
|
+
const emitInvariantOnce = useCallback(
|
|
195
|
+
({
|
|
196
|
+
code,
|
|
197
|
+
roomId,
|
|
198
|
+
data,
|
|
199
|
+
message,
|
|
200
|
+
}: {
|
|
201
|
+
code: string;
|
|
202
|
+
roomId?: string;
|
|
203
|
+
data?: Record<string, unknown>;
|
|
204
|
+
message: string;
|
|
205
|
+
}) => {
|
|
206
|
+
const key = `${roomId ?? "unknown"}:${code}`;
|
|
207
|
+
if (emittedInvariantKeysRef.current.has(key)) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
emittedInvariantKeysRef.current.add(key);
|
|
211
|
+
emitHostRuntimeEvent({
|
|
212
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.invariantViolation,
|
|
213
|
+
level: "warn",
|
|
214
|
+
message,
|
|
215
|
+
roomId,
|
|
216
|
+
data: {
|
|
217
|
+
code,
|
|
218
|
+
...data,
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
[emitHostRuntimeEvent],
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const sendState = useCallback(
|
|
226
|
+
(state: ControllerStatePayload): boolean => {
|
|
227
|
+
const activeSocket = socket;
|
|
228
|
+
const activeRoomId = parsedRoomIdRef.current;
|
|
229
|
+
if (!activeSocket || !canEmitAuthoritativeHostState(activeRoomId)) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
const payload = controllerStateSchema.safeParse({
|
|
233
|
+
roomId: activeRoomId,
|
|
234
|
+
state,
|
|
235
|
+
});
|
|
236
|
+
if (!payload.success) {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
activeSocket.emit("host:state", payload.data);
|
|
240
|
+
return true;
|
|
241
|
+
},
|
|
242
|
+
[canEmitAuthoritativeHostState, socket],
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
const setRuntimeState = useCallback(
|
|
246
|
+
(runtimeState: RuntimeState): void => {
|
|
247
|
+
const activeRoomId = parsedRoomIdRef.current;
|
|
248
|
+
if (store.getState().runtimeState === runtimeState) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (!canEmitAuthoritativeHostState(activeRoomId)) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
sendState({ runtimeState });
|
|
255
|
+
},
|
|
256
|
+
[canEmitAuthoritativeHostState, sendState, store],
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
const pauseRuntime = useCallback((): void => {
|
|
260
|
+
setRuntimeState("paused");
|
|
261
|
+
}, [setRuntimeState]);
|
|
262
|
+
|
|
263
|
+
const resumeRuntime = useCallback((): void => {
|
|
264
|
+
setRuntimeState("playing");
|
|
265
|
+
}, [setRuntimeState]);
|
|
266
|
+
|
|
267
|
+
const sendSignal = useCallback(
|
|
268
|
+
(
|
|
269
|
+
type: SignalType,
|
|
270
|
+
payload: HapticSignalPayload | ToastSignalPayload,
|
|
271
|
+
targetId?: string,
|
|
272
|
+
): void => {
|
|
273
|
+
if (!socket || !socket.connected) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const signal: SignalPayload = {
|
|
277
|
+
targetId,
|
|
278
|
+
type,
|
|
279
|
+
payload,
|
|
280
|
+
} as SignalPayload;
|
|
281
|
+
socket.emit("host:signal", signal);
|
|
282
|
+
},
|
|
283
|
+
[socket],
|
|
284
|
+
) as AirJamHostApi["sendSignal"];
|
|
285
|
+
|
|
286
|
+
const reconnect = useCallback(() => {
|
|
287
|
+
socket?.disconnect();
|
|
288
|
+
if (!embeddedHost) {
|
|
289
|
+
disconnectSocket("host");
|
|
290
|
+
}
|
|
291
|
+
if (socket) {
|
|
292
|
+
socket.connect();
|
|
293
|
+
}
|
|
294
|
+
}, [socket, embeddedHost, disconnectSocket]);
|
|
295
|
+
|
|
296
|
+
const removeController = useCallback(
|
|
297
|
+
async (controllerId: string) => {
|
|
298
|
+
const activeRoomId = parsedRoomIdRef.current;
|
|
299
|
+
if (!socket || !activeRoomId) {
|
|
300
|
+
return {
|
|
301
|
+
ok: false,
|
|
302
|
+
message: "Host is not connected to a room.",
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const payload = hostRemoveControllerSchema.parse({
|
|
307
|
+
roomId: activeRoomId,
|
|
308
|
+
controllerId,
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
return socket.emitWithAck<{
|
|
312
|
+
ok: boolean;
|
|
313
|
+
message?: string;
|
|
314
|
+
code?: string;
|
|
315
|
+
}>("host:removeController", payload);
|
|
316
|
+
},
|
|
317
|
+
[socket],
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
const resetRoom = useCallback(async (): Promise<HostRegistrationAck> => {
|
|
321
|
+
const activeRoomId = parsedRoomIdRef.current;
|
|
322
|
+
if (!socket || !activeRoomId) {
|
|
323
|
+
return {
|
|
324
|
+
ok: false,
|
|
325
|
+
message: "Host is not connected to a room.",
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const payload = hostResetRoomSchema.parse({
|
|
330
|
+
roomId: activeRoomId,
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
emitHostRuntimeEvent({
|
|
334
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.hostResetRoomRequested,
|
|
335
|
+
message: "Host requested local room reset",
|
|
336
|
+
roomId: activeRoomId,
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
const ack = await socket.emitWithAck<HostRegistrationAck>(
|
|
340
|
+
"host:resetRoom",
|
|
341
|
+
payload,
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
if (!ack.ok || !ack.roomId) {
|
|
345
|
+
return ack;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const latestState = store.getState();
|
|
349
|
+
latestState.setStatus("connected");
|
|
350
|
+
latestState.setRoomId(ack.roomId);
|
|
351
|
+
latestState.setError(undefined);
|
|
352
|
+
latestState.clearHostArcadeRestore();
|
|
353
|
+
latestState.resetRuntimeState();
|
|
354
|
+
hydrateHostRoster(ack.players, ack.controllers);
|
|
355
|
+
setRegisteredRoomId(ack.roomId);
|
|
356
|
+
setControllerCapability(ack.controllerCapability ?? null);
|
|
357
|
+
|
|
358
|
+
if (typeof window !== "undefined") {
|
|
359
|
+
sessionStorage.setItem("airjam_room_id", ack.roomId);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return ack;
|
|
363
|
+
}, [
|
|
364
|
+
emitHostRuntimeEvent,
|
|
365
|
+
hydrateHostRoster,
|
|
366
|
+
setRegisteredRoomId,
|
|
367
|
+
socket,
|
|
368
|
+
store,
|
|
369
|
+
]);
|
|
370
|
+
|
|
371
|
+
useEffect(() => {
|
|
372
|
+
if (embeddedHost?.joinUrl) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (!parsedRoomId || !joinUrlBuildKey) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
let cancelled = false;
|
|
380
|
+
|
|
381
|
+
void (async () => {
|
|
382
|
+
try {
|
|
383
|
+
const url = await urlBuilder.buildControllerUrl(parsedRoomId, {
|
|
384
|
+
host: config.publicHost,
|
|
385
|
+
capabilityToken: controllerCapability?.token,
|
|
386
|
+
});
|
|
387
|
+
if (!cancelled) {
|
|
388
|
+
setComputedJoinUrl({
|
|
389
|
+
key: joinUrlBuildKey,
|
|
390
|
+
url,
|
|
391
|
+
error: false,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
} catch {
|
|
395
|
+
if (!cancelled) {
|
|
396
|
+
setComputedJoinUrl({
|
|
397
|
+
key: joinUrlBuildKey,
|
|
398
|
+
url: "",
|
|
399
|
+
error: true,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
})();
|
|
404
|
+
|
|
405
|
+
return () => {
|
|
406
|
+
cancelled = true;
|
|
407
|
+
};
|
|
408
|
+
}, [
|
|
409
|
+
parsedRoomId,
|
|
410
|
+
embeddedHost?.joinUrl,
|
|
411
|
+
config.publicHost,
|
|
412
|
+
controllerCapability?.token,
|
|
413
|
+
joinUrlBuildKey,
|
|
414
|
+
]);
|
|
415
|
+
|
|
416
|
+
const joinUrl = embeddedHost?.joinUrl
|
|
417
|
+
? embeddedHost.joinUrl
|
|
418
|
+
: computedJoinUrl.key === joinUrlBuildKey
|
|
419
|
+
? computedJoinUrl.url
|
|
420
|
+
: "";
|
|
421
|
+
|
|
422
|
+
const joinUrlStatus: JoinUrlStatus = embeddedHost?.joinUrl
|
|
423
|
+
? "ready"
|
|
424
|
+
: !joinUrlBuildKey
|
|
425
|
+
? "loading"
|
|
426
|
+
: computedJoinUrl.key !== joinUrlBuildKey
|
|
427
|
+
? "loading"
|
|
428
|
+
: computedJoinUrl.error
|
|
429
|
+
? "unavailable"
|
|
430
|
+
: computedJoinUrl.url
|
|
431
|
+
? "ready"
|
|
432
|
+
: "loading";
|
|
433
|
+
|
|
434
|
+
const reconnectRetryTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(
|
|
435
|
+
null,
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
const setDevHostTraceId = useCallback((traceId?: string) => {
|
|
439
|
+
updateDevBrowserLogContext({ traceId });
|
|
440
|
+
}, []);
|
|
441
|
+
|
|
442
|
+
useEffect(() => {
|
|
443
|
+
updateDevBrowserLogContext({
|
|
444
|
+
role: "host",
|
|
445
|
+
roomId: parsedRoomId ?? undefined,
|
|
446
|
+
});
|
|
447
|
+
}, [parsedRoomId]);
|
|
448
|
+
|
|
449
|
+
useEffect(() => {
|
|
450
|
+
return () => {
|
|
451
|
+
updateDevBrowserLogContext({
|
|
452
|
+
role: undefined,
|
|
453
|
+
roomId: undefined,
|
|
454
|
+
traceId: undefined,
|
|
455
|
+
});
|
|
456
|
+
};
|
|
457
|
+
}, []);
|
|
458
|
+
|
|
459
|
+
const hostGrantResponseSchema = useMemo(
|
|
460
|
+
() =>
|
|
461
|
+
zod.object({
|
|
462
|
+
hostGrant: zod.string().min(1),
|
|
463
|
+
}),
|
|
464
|
+
[],
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
useEffect(() => {
|
|
468
|
+
const storeState = store.getState();
|
|
469
|
+
const initialRoomId = parsedRoomIdRef.current;
|
|
470
|
+
storeState.setMode(detectRunMode());
|
|
471
|
+
storeState.setRole("host");
|
|
472
|
+
if (initialRoomId) {
|
|
473
|
+
storeState.setRoomId(initialRoomId);
|
|
474
|
+
}
|
|
475
|
+
storeState.setStatus("connecting");
|
|
476
|
+
storeState.setError(undefined);
|
|
477
|
+
lastObservedStateVersionRef.current = null;
|
|
478
|
+
emittedInvariantKeysRef.current.clear();
|
|
479
|
+
|
|
480
|
+
if (!shouldConnect || !socket) {
|
|
481
|
+
storeState.setStatus("idle");
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const registerHost = async () => {
|
|
486
|
+
if (embeddedHost) {
|
|
487
|
+
const childRoomId = embeddedHost.roomId;
|
|
488
|
+
const latestState = store.getState();
|
|
489
|
+
latestState.setStatus("connected");
|
|
490
|
+
latestState.setRoomId(childRoomId);
|
|
491
|
+
hydrateHostRoster();
|
|
492
|
+
setRegisteredRoomId(childRoomId);
|
|
493
|
+
setControllerCapability(null);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const resolveBootstrapPayload =
|
|
498
|
+
async (): Promise<HostBootstrapPayload> => {
|
|
499
|
+
if (!config.hostGrantEndpoint) {
|
|
500
|
+
return hostBootstrapSchema.parse({
|
|
501
|
+
appId: config.appId,
|
|
502
|
+
hostSessionKind: config.hostSessionKind,
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const response = await fetch(config.hostGrantEndpoint, {
|
|
507
|
+
method: "POST",
|
|
508
|
+
credentials: "include",
|
|
509
|
+
headers: {
|
|
510
|
+
"content-type": "application/json",
|
|
511
|
+
},
|
|
512
|
+
body: JSON.stringify(config.appId ? { appId: config.appId } : {}),
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
if (!response.ok) {
|
|
516
|
+
throw new Error(`Failed to fetch host grant (${response.status})`);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const parsed = hostGrantResponseSchema.safeParse(
|
|
520
|
+
await response.json(),
|
|
521
|
+
);
|
|
522
|
+
if (!parsed.success) {
|
|
523
|
+
throw new Error("Invalid host grant response");
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
return hostBootstrapSchema.parse({
|
|
527
|
+
hostGrant: parsed.data.hostGrant,
|
|
528
|
+
hostSessionKind: config.hostSessionKind,
|
|
529
|
+
});
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
let bootstrapPayload: HostBootstrapPayload;
|
|
533
|
+
try {
|
|
534
|
+
bootstrapPayload = await resolveBootstrapPayload();
|
|
535
|
+
} catch (error) {
|
|
536
|
+
const latestState = store.getState();
|
|
537
|
+
const message =
|
|
538
|
+
error instanceof Error
|
|
539
|
+
? error.message
|
|
540
|
+
: "Failed to resolve host bootstrap grant";
|
|
541
|
+
emitAirJamDiagnostic({
|
|
542
|
+
code: "AJ_HOST_BOOTSTRAP_FAILED",
|
|
543
|
+
severity: "error",
|
|
544
|
+
message,
|
|
545
|
+
details: {
|
|
546
|
+
stage: "resolve_bootstrap_payload",
|
|
547
|
+
hasAppId: Boolean(config.appId),
|
|
548
|
+
hasHostGrantEndpoint: Boolean(config.hostGrantEndpoint),
|
|
549
|
+
},
|
|
550
|
+
});
|
|
551
|
+
latestState.setError(message);
|
|
552
|
+
latestState.setStatus("disconnected");
|
|
553
|
+
latestState.clearHostArcadeRestore();
|
|
554
|
+
setRegisteredRoomId(null);
|
|
555
|
+
setDevHostTraceId(undefined);
|
|
556
|
+
setControllerCapability(null);
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
let bootstrapAck: HostBootstrapAck;
|
|
561
|
+
try {
|
|
562
|
+
bootstrapAck = await socket.emitWithAck<HostBootstrapAck>(
|
|
563
|
+
"host:bootstrap",
|
|
564
|
+
bootstrapPayload,
|
|
565
|
+
);
|
|
566
|
+
} catch (error) {
|
|
567
|
+
const latestState = store.getState();
|
|
568
|
+
const message =
|
|
569
|
+
error instanceof Error &&
|
|
570
|
+
error.message.includes(
|
|
571
|
+
'Timed out waiting for acknowledgement for realtime event "host:bootstrap".',
|
|
572
|
+
)
|
|
573
|
+
? HOST_BOOTSTRAP_TIMEOUT_MESSAGE
|
|
574
|
+
: error instanceof Error
|
|
575
|
+
? error.message
|
|
576
|
+
: "Failed to authorize host";
|
|
577
|
+
emitAirJamDiagnostic({
|
|
578
|
+
code: "AJ_HOST_BOOTSTRAP_FAILED",
|
|
579
|
+
severity: "error",
|
|
580
|
+
message,
|
|
581
|
+
details: {
|
|
582
|
+
stage: "bootstrap_ack_timeout",
|
|
583
|
+
hasAppId: Boolean(config.appId),
|
|
584
|
+
hasHostGrantEndpoint: Boolean(config.hostGrantEndpoint),
|
|
585
|
+
},
|
|
586
|
+
});
|
|
587
|
+
latestState.setError(message);
|
|
588
|
+
latestState.setStatus("disconnected");
|
|
589
|
+
latestState.clearHostArcadeRestore();
|
|
590
|
+
setRegisteredRoomId(null);
|
|
591
|
+
setDevHostTraceId(undefined);
|
|
592
|
+
setControllerCapability(null);
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (!bootstrapAck.ok) {
|
|
597
|
+
const latestState = store.getState();
|
|
598
|
+
const message = bootstrapAck.message ?? "Failed to authorize host";
|
|
599
|
+
emitAirJamDiagnostic({
|
|
600
|
+
code: "AJ_HOST_BOOTSTRAP_FAILED",
|
|
601
|
+
severity: "error",
|
|
602
|
+
message,
|
|
603
|
+
details: {
|
|
604
|
+
stage: "bootstrap_ack",
|
|
605
|
+
ackCode: bootstrapAck.code,
|
|
606
|
+
hasAppId: Boolean(config.appId),
|
|
607
|
+
hasHostGrantEndpoint: Boolean(config.hostGrantEndpoint),
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
latestState.setError(message);
|
|
611
|
+
latestState.setStatus("disconnected");
|
|
612
|
+
latestState.clearHostArcadeRestore();
|
|
613
|
+
setRegisteredRoomId(null);
|
|
614
|
+
setDevHostTraceId(undefined);
|
|
615
|
+
setControllerCapability(null);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
setDevHostTraceId(bootstrapAck.traceId);
|
|
620
|
+
|
|
621
|
+
const createNewRoom = (
|
|
622
|
+
reason: "bootstrap" | "reconnect_fallback" = "bootstrap",
|
|
623
|
+
details?: Record<string, unknown>,
|
|
624
|
+
) => {
|
|
625
|
+
const latestState = store.getState();
|
|
626
|
+
latestState.clearHostArcadeRestore();
|
|
627
|
+
latestState.resetPlayers();
|
|
628
|
+
setRegisteredRoomId(null);
|
|
629
|
+
setControllerCapability(null);
|
|
630
|
+
const payload = hostCreateRoomSchema.parse({
|
|
631
|
+
maxPlayers: config.maxPlayers,
|
|
632
|
+
});
|
|
633
|
+
emitHostRuntimeEvent({
|
|
634
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.hostCreateRoomRequested,
|
|
635
|
+
message: "Host requested room creation",
|
|
636
|
+
data: {
|
|
637
|
+
reason,
|
|
638
|
+
maxPlayers: payload.maxPlayers,
|
|
639
|
+
...details,
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
socket.emit("host:createRoom", payload, (ack: HostRegistrationAck) => {
|
|
644
|
+
const latestState = store.getState();
|
|
645
|
+
if (!ack.ok) {
|
|
646
|
+
latestState.setError(ack.message ?? "Failed to create room");
|
|
647
|
+
latestState.setStatus("disconnected");
|
|
648
|
+
setRegisteredRoomId(null);
|
|
649
|
+
setDevHostTraceId(undefined);
|
|
650
|
+
setControllerCapability(null);
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (ack.roomId) {
|
|
655
|
+
latestState.setStatus("connected");
|
|
656
|
+
latestState.setRoomId(ack.roomId);
|
|
657
|
+
latestState.clearHostArcadeRestore();
|
|
658
|
+
hydrateHostRoster(ack.players, ack.controllers);
|
|
659
|
+
setRegisteredRoomId(ack.roomId);
|
|
660
|
+
setControllerCapability(ack.controllerCapability ?? null);
|
|
661
|
+
|
|
662
|
+
if (typeof window !== "undefined") {
|
|
663
|
+
sessionStorage.setItem("airjam_room_id", ack.roomId);
|
|
664
|
+
}
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
latestState.setError("Server did not return room ID");
|
|
669
|
+
latestState.setStatus("disconnected");
|
|
670
|
+
setDevHostTraceId(undefined);
|
|
671
|
+
setControllerCapability(null);
|
|
672
|
+
});
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
if (typeof window !== "undefined") {
|
|
676
|
+
const savedRoomId = sessionStorage.getItem("airjam_room_id");
|
|
677
|
+
if (savedRoomId) {
|
|
678
|
+
const reconnectPayload = hostReconnectSchema.parse({
|
|
679
|
+
roomId: savedRoomId,
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
const maxReconnectAttempts = 12;
|
|
683
|
+
const reconnectRetryDelayMs = 250;
|
|
684
|
+
|
|
685
|
+
const attemptReconnect = (attempt: number) => {
|
|
686
|
+
store.getState().setHostArcadeRestore({
|
|
687
|
+
phase: "awaiting_ack",
|
|
688
|
+
session: null,
|
|
689
|
+
});
|
|
690
|
+
emitHostRuntimeEvent({
|
|
691
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.hostReconnectRequested,
|
|
692
|
+
message: "Host requested room reconnect",
|
|
693
|
+
roomId: reconnectPayload.roomId,
|
|
694
|
+
data: {
|
|
695
|
+
attempt,
|
|
696
|
+
source: "session_storage_restore",
|
|
697
|
+
},
|
|
698
|
+
});
|
|
699
|
+
socket.emit(
|
|
700
|
+
"host:reconnect",
|
|
701
|
+
reconnectPayload,
|
|
702
|
+
(ack: HostRegistrationAck) => {
|
|
703
|
+
const latestState = store.getState();
|
|
704
|
+
if (ack.ok && ack.roomId) {
|
|
705
|
+
latestState.setStatus("connected");
|
|
706
|
+
latestState.setRoomId(ack.roomId);
|
|
707
|
+
hydrateHostRoster(ack.players, ack.controllers);
|
|
708
|
+
setControllerCapability(ack.controllerCapability ?? null);
|
|
709
|
+
latestState.setHostArcadeRestore(
|
|
710
|
+
ack.arcadeSession
|
|
711
|
+
? {
|
|
712
|
+
phase: "pending_restore",
|
|
713
|
+
session: ack.arcadeSession,
|
|
714
|
+
}
|
|
715
|
+
: {
|
|
716
|
+
phase: "idle",
|
|
717
|
+
session: null,
|
|
718
|
+
},
|
|
719
|
+
);
|
|
720
|
+
setRegisteredRoomId(ack.roomId);
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
if (
|
|
725
|
+
ack.code === ErrorCode.ALREADY_CONNECTED &&
|
|
726
|
+
attempt < maxReconnectAttempts
|
|
727
|
+
) {
|
|
728
|
+
emitHostRuntimeEvent({
|
|
729
|
+
event:
|
|
730
|
+
AIRJAM_DEV_LOG_EVENTS.runtime.hostReconnectRetryScheduled,
|
|
731
|
+
message: "Host reconnect retry scheduled",
|
|
732
|
+
roomId: reconnectPayload.roomId,
|
|
733
|
+
data: {
|
|
734
|
+
attempt,
|
|
735
|
+
nextAttempt: attempt + 1,
|
|
736
|
+
retryDelayMs: reconnectRetryDelayMs,
|
|
737
|
+
ackCode: ack.code,
|
|
738
|
+
},
|
|
739
|
+
});
|
|
740
|
+
reconnectRetryTimeoutRef.current = setTimeout(() => {
|
|
741
|
+
reconnectRetryTimeoutRef.current = null;
|
|
742
|
+
attemptReconnect(attempt + 1);
|
|
743
|
+
}, reconnectRetryDelayMs);
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
latestState.clearHostArcadeRestore();
|
|
748
|
+
if (typeof window !== "undefined") {
|
|
749
|
+
sessionStorage.removeItem("airjam_room_id");
|
|
750
|
+
}
|
|
751
|
+
setControllerCapability(null);
|
|
752
|
+
createNewRoom("reconnect_fallback", {
|
|
753
|
+
attempt,
|
|
754
|
+
ackCode: ack.code,
|
|
755
|
+
ackMessage: ack.message,
|
|
756
|
+
});
|
|
757
|
+
},
|
|
758
|
+
);
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
attemptReconnect(0);
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
createNewRoom();
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
const handleConnect = (): void => {
|
|
770
|
+
emitHostRuntimeEvent({
|
|
771
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.socketConnected,
|
|
772
|
+
message: "Host socket connected",
|
|
773
|
+
roomId: parsedRoomIdRef.current ?? undefined,
|
|
774
|
+
data: {
|
|
775
|
+
socketId: socket.id,
|
|
776
|
+
connected: socket.connected,
|
|
777
|
+
},
|
|
778
|
+
});
|
|
779
|
+
store.getState().setStatus("connecting");
|
|
780
|
+
void registerHost();
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
const handleDisconnect = (reason: string): void => {
|
|
784
|
+
emitHostRuntimeEvent({
|
|
785
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.socketDisconnected,
|
|
786
|
+
message: "Host socket disconnected",
|
|
787
|
+
roomId: parsedRoomIdRef.current ?? undefined,
|
|
788
|
+
data: {
|
|
789
|
+
socketId: socket.id,
|
|
790
|
+
reason,
|
|
791
|
+
},
|
|
792
|
+
});
|
|
793
|
+
store.getState().setStatus("disconnected");
|
|
794
|
+
store.getState().resetPlayers();
|
|
795
|
+
store.getState().resetRuntimeState();
|
|
796
|
+
lastObservedStateVersionRef.current = null;
|
|
797
|
+
setRegisteredRoomId(null);
|
|
798
|
+
setDevHostTraceId(undefined);
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
const handleConnectError = (error: Error): void => {
|
|
802
|
+
emitAirJamDevRuntimeEvent({
|
|
803
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.socketConnectError,
|
|
804
|
+
level: "warn",
|
|
805
|
+
message: "Host socket connect error",
|
|
806
|
+
role: "host",
|
|
807
|
+
roomId: parsedRoomIdRef.current ?? undefined,
|
|
808
|
+
data: {
|
|
809
|
+
message: error.message,
|
|
810
|
+
name: error.name,
|
|
811
|
+
},
|
|
812
|
+
});
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
const handleJoin = (payload: ControllerJoinedNotice): void => {
|
|
816
|
+
store.getState().upsertControllerSession(payload);
|
|
817
|
+
if (!payload.player) {
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
store.getState().upsertPlayer(payload.player);
|
|
821
|
+
setTimeout(() => {
|
|
822
|
+
onPlayerJoinRef.current?.(payload.player!);
|
|
823
|
+
}, 0);
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
const handleLeave = (payload: { controllerId: string }): void => {
|
|
827
|
+
const latestState = store.getState();
|
|
828
|
+
latestState.removePlayer(payload.controllerId);
|
|
829
|
+
latestState.removeControllerSession(payload.controllerId);
|
|
830
|
+
onPlayerLeaveRef.current?.(payload.controllerId);
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
const handlePlayerUpdated = (payload: PlayerUpdatedNotice): void => {
|
|
834
|
+
const latestState = store.getState();
|
|
835
|
+
latestState.upsertPlayer(payload.player);
|
|
836
|
+
const existingSession = latestState.controllerSessions.find(
|
|
837
|
+
(controller) => controller.controllerId === payload.player.id,
|
|
838
|
+
);
|
|
839
|
+
if (existingSession) {
|
|
840
|
+
latestState.upsertControllerSession({
|
|
841
|
+
...existingSession,
|
|
842
|
+
player: payload.player,
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
const handleInput = (payload: ControllerInputEvent): void => {
|
|
848
|
+
if (inputManager) {
|
|
849
|
+
inputManager.handleInput(payload);
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
const handleState = (payload: ControllerStateMessage): void => {
|
|
854
|
+
const activeRoomId = parsedRoomIdRef.current;
|
|
855
|
+
if (!activeRoomId || payload.roomId !== activeRoomId) return;
|
|
856
|
+
|
|
857
|
+
emitHostRuntimeEvent({
|
|
858
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.hostStateReceived,
|
|
859
|
+
message: "Host received state update",
|
|
860
|
+
roomId: payload.roomId,
|
|
861
|
+
data: {
|
|
862
|
+
runtimeState: payload.state.runtimeState,
|
|
863
|
+
orientation: payload.state.orientation,
|
|
864
|
+
stateVersion: payload.state.stateVersion,
|
|
865
|
+
hasMessage: payload.state.message !== undefined,
|
|
866
|
+
},
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
const latestState = store.getState();
|
|
870
|
+
const previousRuntimeState = latestState.runtimeState;
|
|
871
|
+
const nextRuntimeState =
|
|
872
|
+
payload.state.runtimeState ?? previousRuntimeState;
|
|
873
|
+
const incomingVersion = payload.state.stateVersion;
|
|
874
|
+
const previousVersion = lastObservedStateVersionRef.current;
|
|
875
|
+
if (typeof incomingVersion === "number") {
|
|
876
|
+
if (previousVersion === null) {
|
|
877
|
+
emitHostRuntimeEvent({
|
|
878
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.stateVersionReceived,
|
|
879
|
+
message: "Host received initial room state version",
|
|
880
|
+
roomId: payload.roomId,
|
|
881
|
+
data: {
|
|
882
|
+
stateVersion: incomingVersion,
|
|
883
|
+
relation: "initial",
|
|
884
|
+
},
|
|
885
|
+
});
|
|
886
|
+
} else if (incomingVersion <= previousVersion) {
|
|
887
|
+
emitHostRuntimeEvent({
|
|
888
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.stateVersionReceived,
|
|
889
|
+
level: "warn",
|
|
890
|
+
message: "Host received non-monotonic room state version",
|
|
891
|
+
roomId: payload.roomId,
|
|
892
|
+
data: {
|
|
893
|
+
stateVersion: incomingVersion,
|
|
894
|
+
previousStateVersion: previousVersion,
|
|
895
|
+
relation: "non_monotonic",
|
|
896
|
+
},
|
|
897
|
+
});
|
|
898
|
+
emitInvariantOnce({
|
|
899
|
+
code: "state_version_non_monotonic",
|
|
900
|
+
roomId: payload.roomId,
|
|
901
|
+
message:
|
|
902
|
+
"Received non-monotonic room state version in host runtime",
|
|
903
|
+
data: {
|
|
904
|
+
stateVersion: incomingVersion,
|
|
905
|
+
previousStateVersion: previousVersion,
|
|
906
|
+
},
|
|
907
|
+
});
|
|
908
|
+
} else if (incomingVersion !== previousVersion + 1) {
|
|
909
|
+
emitHostRuntimeEvent({
|
|
910
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.stateVersionReceived,
|
|
911
|
+
message: "Host detected room state version gap",
|
|
912
|
+
roomId: payload.roomId,
|
|
913
|
+
data: {
|
|
914
|
+
stateVersion: incomingVersion,
|
|
915
|
+
previousStateVersion: previousVersion,
|
|
916
|
+
relation: "gap",
|
|
917
|
+
},
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
lastObservedStateVersionRef.current =
|
|
921
|
+
previousVersion === null
|
|
922
|
+
? incomingVersion
|
|
923
|
+
: Math.max(previousVersion, incomingVersion);
|
|
924
|
+
}
|
|
925
|
+
if (
|
|
926
|
+
typeof incomingVersion === "number" &&
|
|
927
|
+
nextRuntimeState !== previousRuntimeState
|
|
928
|
+
) {
|
|
929
|
+
emitHostRuntimeEvent({
|
|
930
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.phaseTransition,
|
|
931
|
+
message: "Host runtime phase transition",
|
|
932
|
+
roomId: payload.roomId,
|
|
933
|
+
data: {
|
|
934
|
+
from: previousRuntimeState,
|
|
935
|
+
to: nextRuntimeState,
|
|
936
|
+
source: "server_state",
|
|
937
|
+
stateVersion: incomingVersion,
|
|
938
|
+
},
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
if (payload.state.runtimeState) {
|
|
942
|
+
latestState.setRuntimeState(payload.state.runtimeState);
|
|
943
|
+
}
|
|
944
|
+
if (payload.state.orientation) {
|
|
945
|
+
latestState.setControllerOrientation(payload.state.orientation);
|
|
946
|
+
}
|
|
947
|
+
if (payload.state.message !== undefined) {
|
|
948
|
+
latestState.setStateMessage(payload.state.message);
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
const handleError = (payload: { message: string }): void => {
|
|
953
|
+
store.getState().setError(payload.message);
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
socket.on("connect", handleConnect);
|
|
957
|
+
socket.on("disconnect", handleDisconnect);
|
|
958
|
+
socket.on("connect_error", handleConnectError);
|
|
959
|
+
socket.on("server:controllerJoined", handleJoin);
|
|
960
|
+
socket.on("server:controllerLeft", handleLeave);
|
|
961
|
+
socket.on("server:playerUpdated", handlePlayerUpdated);
|
|
962
|
+
socket.on("server:input", handleInput);
|
|
963
|
+
socket.on("server:error", handleError);
|
|
964
|
+
socket.on("server:state", handleState);
|
|
965
|
+
|
|
966
|
+
if (socket.connected) {
|
|
967
|
+
handleConnect();
|
|
968
|
+
} else {
|
|
969
|
+
socket.connect();
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
return () => {
|
|
973
|
+
if (reconnectRetryTimeoutRef.current) {
|
|
974
|
+
clearTimeout(reconnectRetryTimeoutRef.current);
|
|
975
|
+
reconnectRetryTimeoutRef.current = null;
|
|
976
|
+
}
|
|
977
|
+
socket.off("connect", handleConnect);
|
|
978
|
+
socket.off("disconnect", handleDisconnect);
|
|
979
|
+
socket.off("connect_error", handleConnectError);
|
|
980
|
+
socket.off("server:controllerJoined", handleJoin);
|
|
981
|
+
socket.off("server:controllerLeft", handleLeave);
|
|
982
|
+
socket.off("server:playerUpdated", handlePlayerUpdated);
|
|
983
|
+
socket.off("server:input", handleInput);
|
|
984
|
+
socket.off("server:error", handleError);
|
|
985
|
+
socket.off("server:state", handleState);
|
|
986
|
+
setDevHostTraceId(undefined);
|
|
987
|
+
};
|
|
988
|
+
}, [
|
|
989
|
+
config.maxPlayers,
|
|
990
|
+
config.appId,
|
|
991
|
+
config.hostGrantEndpoint,
|
|
992
|
+
config.hostSessionKind,
|
|
993
|
+
embeddedHost,
|
|
994
|
+
shouldConnect,
|
|
995
|
+
socket,
|
|
996
|
+
store,
|
|
997
|
+
inputManager,
|
|
998
|
+
setRegisteredRoomId,
|
|
999
|
+
setDevHostTraceId,
|
|
1000
|
+
hostGrantResponseSchema,
|
|
1001
|
+
emitHostRuntimeEvent,
|
|
1002
|
+
emitInvariantOnce,
|
|
1003
|
+
hydrateHostRoster,
|
|
1004
|
+
]);
|
|
1005
|
+
|
|
1006
|
+
const getInput = useCallback(
|
|
1007
|
+
(controllerId: string): z.infer<TSchema> | undefined => {
|
|
1008
|
+
if (!inputManager) {
|
|
1009
|
+
return undefined;
|
|
1010
|
+
}
|
|
1011
|
+
return inputManager.getInput(controllerId) as
|
|
1012
|
+
| z.infer<TSchema>
|
|
1013
|
+
| undefined;
|
|
1014
|
+
},
|
|
1015
|
+
[inputManager],
|
|
1016
|
+
);
|
|
1017
|
+
|
|
1018
|
+
return useMemo(
|
|
1019
|
+
() => ({
|
|
1020
|
+
roomId: parsedRoomId ?? ("" as RoomCode),
|
|
1021
|
+
joinUrl,
|
|
1022
|
+
joinUrlStatus,
|
|
1023
|
+
pauseRuntime,
|
|
1024
|
+
resumeRuntime,
|
|
1025
|
+
setRuntimeState,
|
|
1026
|
+
sendState,
|
|
1027
|
+
sendSignal,
|
|
1028
|
+
reconnect,
|
|
1029
|
+
removeController,
|
|
1030
|
+
resetRoom,
|
|
1031
|
+
socket: socket ?? getHostRealtimeClient((role) => getSocket(role)),
|
|
1032
|
+
getInput,
|
|
1033
|
+
}),
|
|
1034
|
+
[
|
|
1035
|
+
getInput,
|
|
1036
|
+
getSocket,
|
|
1037
|
+
joinUrl,
|
|
1038
|
+
joinUrlStatus,
|
|
1039
|
+
parsedRoomId,
|
|
1040
|
+
pauseRuntime,
|
|
1041
|
+
reconnect,
|
|
1042
|
+
removeController,
|
|
1043
|
+
resetRoom,
|
|
1044
|
+
resumeRuntime,
|
|
1045
|
+
sendSignal,
|
|
1046
|
+
sendState,
|
|
1047
|
+
setRuntimeState,
|
|
1048
|
+
socket,
|
|
1049
|
+
],
|
|
1050
|
+
);
|
|
1051
|
+
};
|