@air-jam/sdk 0.1.0
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/README.md +87 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/chunk-XSXI3RIS.js +72 -0
- package/dist/chunk-XSXI3RIS.js.map +1 -0
- package/dist/index.cjs +3514 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1529 -0
- package/dist/index.d.ts +1529 -0
- package/dist/index.js +3366 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.cjs +85 -0
- package/dist/protocol.cjs.map +1 -0
- package/dist/protocol.d.cts +242 -0
- package/dist/protocol.d.ts +242 -0
- package/dist/protocol.js +3 -0
- package/dist/protocol.js.map +1 -0
- package/package.json +81 -0
- package/src/AirJamClient.ts +775 -0
- package/src/audio/audio-manager.ts +395 -0
- package/src/audio/hooks.tsx +140 -0
- package/src/audio/index.ts +3 -0
- package/src/audio/volume-store.ts +77 -0
- package/src/components/air-jam-overlay.tsx +456 -0
- package/src/components/controller-shell.tsx +329 -0
- package/src/components/qr-scanner-dialog.tsx +373 -0
- package/src/components/ui/alert.tsx +66 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +47 -0
- package/src/components/ui/button.tsx +61 -0
- package/src/components/ui/card.tsx +92 -0
- package/src/components/ui/slider.tsx +61 -0
- package/src/components/ui/tabs.tsx +64 -0
- package/src/components/volume-controls.tsx +93 -0
- package/src/constants.ts +45 -0
- package/src/context/air-jam-context.tsx +478 -0
- package/src/context/index.ts +2 -0
- package/src/context/socket-manager.ts +98 -0
- package/src/events.ts +151 -0
- package/src/hooks/use-air-jam-controller.ts +608 -0
- package/src/hooks/use-air-jam-host.ts +624 -0
- package/src/hooks/use-air-jam-shell.ts +343 -0
- package/src/hooks/use-fullscreen.ts +108 -0
- package/src/hooks/use-get-input.ts +97 -0
- package/src/hooks/use-send-signal.ts +164 -0
- package/src/index.ts +33 -0
- package/src/internal/input-manager.ts +362 -0
- package/src/protocol.ts +331 -0
- package/src/state/connection-store.ts +84 -0
- package/src/styles.css +128 -0
- package/src/types/browser.ts +42 -0
- package/src/utils/cn.ts +4 -0
- package/src/utils/ids.ts +31 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/mode.ts +13 -0
- package/src/utils/network-ip.ts +145 -0
- package/src/utils/url-builder.ts +170 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { useStore } from "zustand";
|
|
3
|
+
import { useShallow } from "zustand/react/shallow";
|
|
4
|
+
import { useAirJamContext } from "../context/air-jam-context";
|
|
5
|
+
import type { AirJamSocket } from "../context/socket-manager";
|
|
6
|
+
import type {
|
|
7
|
+
ConnectionStatus,
|
|
8
|
+
ControllerInputPayload,
|
|
9
|
+
ControllerStateMessage,
|
|
10
|
+
GameState,
|
|
11
|
+
PlayerProfile,
|
|
12
|
+
RoomCode,
|
|
13
|
+
SignalPayload,
|
|
14
|
+
} from "../protocol";
|
|
15
|
+
import {
|
|
16
|
+
controllerInputSchema,
|
|
17
|
+
controllerJoinSchema,
|
|
18
|
+
controllerSystemSchema,
|
|
19
|
+
roomCodeSchema,
|
|
20
|
+
} from "../protocol";
|
|
21
|
+
import { generateControllerId } from "../utils/ids";
|
|
22
|
+
import { detectRunMode } from "../utils/mode";
|
|
23
|
+
|
|
24
|
+
interface AirJamShellOptions {
|
|
25
|
+
/** Room ID to join */
|
|
26
|
+
roomId?: string;
|
|
27
|
+
/** Player nickname */
|
|
28
|
+
nickname?: string;
|
|
29
|
+
/** Controller ID (auto-generated if not provided) */
|
|
30
|
+
controllerId?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AirJamShellApi {
|
|
34
|
+
roomId: RoomCode | null;
|
|
35
|
+
controllerId: string | null;
|
|
36
|
+
connectionStatus: ConnectionStatus;
|
|
37
|
+
lastError?: string;
|
|
38
|
+
activeUrl: string | null;
|
|
39
|
+
players: PlayerProfile[];
|
|
40
|
+
gameState: GameState;
|
|
41
|
+
sendInput: (input: ControllerInputPayload) => boolean;
|
|
42
|
+
sendSystemCommand: (command: "exit" | "ready" | "toggle_pause") => void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const getRoomFromLocation = (): string | null => {
|
|
46
|
+
if (typeof window === "undefined") {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const params = new URLSearchParams(window.location.search);
|
|
50
|
+
const code = params.get("room");
|
|
51
|
+
return code ? code.toUpperCase() : null;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const useAirJamShell = (
|
|
55
|
+
options: AirJamShellOptions = {},
|
|
56
|
+
): AirJamShellApi => {
|
|
57
|
+
// Get context
|
|
58
|
+
const { store, getSocket } = useAirJamContext();
|
|
59
|
+
|
|
60
|
+
const nicknameRef = useRef(options.nickname ?? "Arcade Shell");
|
|
61
|
+
|
|
62
|
+
// Parse room ID
|
|
63
|
+
const parsedRoomId = useMemo<RoomCode | null>(() => {
|
|
64
|
+
const code = options.roomId ?? getRoomFromLocation();
|
|
65
|
+
if (!code) return null;
|
|
66
|
+
try {
|
|
67
|
+
return roomCodeSchema.parse(code.toUpperCase());
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}, [options.roomId]);
|
|
72
|
+
|
|
73
|
+
// Generate or use provided controller ID
|
|
74
|
+
const controllerId = useMemo<string>(() => {
|
|
75
|
+
if (options.controllerId) return options.controllerId;
|
|
76
|
+
if (typeof window !== "undefined") {
|
|
77
|
+
const params = new URLSearchParams(window.location.search);
|
|
78
|
+
const urlControllerId = params.get("controllerId");
|
|
79
|
+
if (urlControllerId) return urlControllerId;
|
|
80
|
+
}
|
|
81
|
+
return generateControllerId();
|
|
82
|
+
}, [options.controllerId]);
|
|
83
|
+
|
|
84
|
+
const [activeUrl, setActiveUrl] = useState<string | null>(null);
|
|
85
|
+
|
|
86
|
+
// Subscribe to store state
|
|
87
|
+
const connectionState = useStore(
|
|
88
|
+
store,
|
|
89
|
+
useShallow((state) => ({
|
|
90
|
+
connectionStatus: state.connectionStatus,
|
|
91
|
+
lastError: state.lastError,
|
|
92
|
+
controllerId: state.controllerId,
|
|
93
|
+
players: state.players,
|
|
94
|
+
gameState: state.gameState,
|
|
95
|
+
})),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
// Get socket from context
|
|
99
|
+
const socket: AirJamSocket | null = useMemo(
|
|
100
|
+
() => (parsedRoomId ? getSocket("controller") : null),
|
|
101
|
+
[parsedRoomId, getSocket],
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
// Automatically listen for haptic signals
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (!socket) return;
|
|
107
|
+
|
|
108
|
+
const handleSignal = (signal: SignalPayload) => {
|
|
109
|
+
if (signal.type !== "HAPTIC") return;
|
|
110
|
+
|
|
111
|
+
// Browser compatibility check
|
|
112
|
+
if (typeof navigator === "undefined" || !navigator.vibrate) return;
|
|
113
|
+
|
|
114
|
+
// TypeScript now knows payload is HapticSignalPayload
|
|
115
|
+
const payload = signal.payload;
|
|
116
|
+
|
|
117
|
+
switch (payload.pattern) {
|
|
118
|
+
case "light":
|
|
119
|
+
navigator.vibrate(10);
|
|
120
|
+
break;
|
|
121
|
+
case "medium":
|
|
122
|
+
navigator.vibrate(30);
|
|
123
|
+
break;
|
|
124
|
+
case "heavy":
|
|
125
|
+
navigator.vibrate([50, 20, 50]); // Pulse
|
|
126
|
+
break;
|
|
127
|
+
case "success":
|
|
128
|
+
navigator.vibrate([10, 30, 10]);
|
|
129
|
+
break;
|
|
130
|
+
case "failure":
|
|
131
|
+
navigator.vibrate([50, 50, 50, 50]);
|
|
132
|
+
break;
|
|
133
|
+
case "custom":
|
|
134
|
+
if (Array.isArray(payload.sequence)) {
|
|
135
|
+
navigator.vibrate(payload.sequence);
|
|
136
|
+
} else if (typeof payload.sequence === "number") {
|
|
137
|
+
navigator.vibrate(payload.sequence);
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
socket.on("server:signal", handleSignal);
|
|
144
|
+
return () => {
|
|
145
|
+
socket.off("server:signal", handleSignal);
|
|
146
|
+
};
|
|
147
|
+
}, [socket]);
|
|
148
|
+
|
|
149
|
+
// Main connection effect
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
const storeState = store.getState();
|
|
152
|
+
storeState.setMode(detectRunMode());
|
|
153
|
+
storeState.setRole("controller");
|
|
154
|
+
storeState.setRoomId(parsedRoomId);
|
|
155
|
+
storeState.setStatus(parsedRoomId ? "connecting" : "idle");
|
|
156
|
+
storeState.setError(undefined);
|
|
157
|
+
|
|
158
|
+
if (!parsedRoomId || !socket || !controllerId) {
|
|
159
|
+
storeState.setStatus("idle");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
storeState.setControllerId(controllerId);
|
|
164
|
+
|
|
165
|
+
const handleConnect = (): void => {
|
|
166
|
+
store.getState().setStatus("connected");
|
|
167
|
+
|
|
168
|
+
const payload = controllerJoinSchema.parse({
|
|
169
|
+
roomId: parsedRoomId,
|
|
170
|
+
controllerId,
|
|
171
|
+
nickname: nicknameRef.current || undefined,
|
|
172
|
+
});
|
|
173
|
+
socket.emit("controller:join", payload, (ack) => {
|
|
174
|
+
const storeState = store.getState();
|
|
175
|
+
if (!ack.ok) {
|
|
176
|
+
storeState.setError(ack.message ?? "Unable to join room");
|
|
177
|
+
storeState.setStatus("disconnected");
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (ack.controllerId) {
|
|
181
|
+
storeState.setControllerId(ack.controllerId);
|
|
182
|
+
}
|
|
183
|
+
storeState.setStatus("connected");
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const handleDisconnect = (): void => {
|
|
188
|
+
store.getState().setStatus("disconnected");
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const handleLoadUi = (payload: { url: string }): void => {
|
|
192
|
+
setActiveUrl(payload.url);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const handleUnloadUi = (): void => {
|
|
196
|
+
setActiveUrl(null);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const handleWelcome = (payload: {
|
|
200
|
+
controllerId: string;
|
|
201
|
+
roomId: RoomCode;
|
|
202
|
+
player?: PlayerProfile;
|
|
203
|
+
}): void => {
|
|
204
|
+
const storeState = store.getState();
|
|
205
|
+
const storeRoomId = storeState.roomId;
|
|
206
|
+
if (
|
|
207
|
+
storeRoomId &&
|
|
208
|
+
payload.roomId.toUpperCase() !== storeRoomId.toUpperCase()
|
|
209
|
+
) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (!storeRoomId && payload.roomId) {
|
|
213
|
+
storeState.setRoomId(payload.roomId);
|
|
214
|
+
}
|
|
215
|
+
if (payload.player) {
|
|
216
|
+
storeState.upsertPlayer(payload.player);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const handleState = (payload: ControllerStateMessage): void => {
|
|
221
|
+
if (payload.roomId !== parsedRoomId) return;
|
|
222
|
+
|
|
223
|
+
const storeState = store.getState();
|
|
224
|
+
if (payload.state.gameState) {
|
|
225
|
+
storeState.setGameState(payload.state.gameState);
|
|
226
|
+
}
|
|
227
|
+
if (payload.state.message !== undefined) {
|
|
228
|
+
storeState.setStateMessage(payload.state.message);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const handleError = (payload: { message: string }): void => {
|
|
233
|
+
store.getState().setError(payload.message);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// Bridge logic for iframe communication
|
|
237
|
+
const handleMessage = (event: MessageEvent) => {
|
|
238
|
+
const storeState = store.getState();
|
|
239
|
+
const data = event.data;
|
|
240
|
+
if (data?.type === "AIRJAM_INPUT") {
|
|
241
|
+
const input = data.payload as ControllerInputPayload;
|
|
242
|
+
const payload = controllerInputSchema.safeParse({
|
|
243
|
+
roomId: parsedRoomId,
|
|
244
|
+
controllerId: storeState.controllerId,
|
|
245
|
+
input,
|
|
246
|
+
});
|
|
247
|
+
if (payload.success) {
|
|
248
|
+
socket.emit("controller:input", payload.data);
|
|
249
|
+
}
|
|
250
|
+
} else if (data?.type === "AIRJAM_READY") {
|
|
251
|
+
// Child is ready
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
socket.on("connect", handleConnect);
|
|
256
|
+
socket.on("disconnect", handleDisconnect);
|
|
257
|
+
socket.on("server:welcome", handleWelcome);
|
|
258
|
+
socket.on("server:state", handleState);
|
|
259
|
+
socket.on("client:loadUi", handleLoadUi);
|
|
260
|
+
socket.on("client:unloadUi", handleUnloadUi);
|
|
261
|
+
socket.on("server:error", handleError);
|
|
262
|
+
|
|
263
|
+
window.addEventListener("message", handleMessage);
|
|
264
|
+
|
|
265
|
+
// Connect the socket
|
|
266
|
+
socket.connect();
|
|
267
|
+
|
|
268
|
+
return () => {
|
|
269
|
+
socket.off("connect", handleConnect);
|
|
270
|
+
socket.off("disconnect", handleDisconnect);
|
|
271
|
+
socket.off("server:welcome", handleWelcome);
|
|
272
|
+
socket.off("server:state", handleState);
|
|
273
|
+
socket.off("client:loadUi", handleLoadUi);
|
|
274
|
+
socket.off("client:unloadUi", handleUnloadUi);
|
|
275
|
+
socket.off("server:error", handleError);
|
|
276
|
+
window.removeEventListener("message", handleMessage);
|
|
277
|
+
};
|
|
278
|
+
}, [parsedRoomId, socket, controllerId, store]);
|
|
279
|
+
|
|
280
|
+
const sendInput = useCallback(
|
|
281
|
+
(input: ControllerInputPayload): boolean => {
|
|
282
|
+
const storeState = store.getState();
|
|
283
|
+
|
|
284
|
+
if (!parsedRoomId || !storeState.controllerId || !socket) {
|
|
285
|
+
storeState.setError("Not connected to a room");
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
if (!socket.connected) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
293
|
+
storeState.setError("Input must be an object");
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const payload = controllerInputSchema.safeParse({
|
|
298
|
+
roomId: parsedRoomId,
|
|
299
|
+
controllerId: storeState.controllerId,
|
|
300
|
+
input,
|
|
301
|
+
});
|
|
302
|
+
if (!payload.success) {
|
|
303
|
+
storeState.setError(payload.error.message);
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
socket.emit("controller:input", payload.data);
|
|
308
|
+
return true;
|
|
309
|
+
},
|
|
310
|
+
[parsedRoomId, socket, store],
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
const sendSystemCommand = useCallback(
|
|
314
|
+
(command: "exit" | "ready" | "toggle_pause") => {
|
|
315
|
+
const storeState = store.getState();
|
|
316
|
+
|
|
317
|
+
if (!parsedRoomId || !storeState.controllerId || !socket) return;
|
|
318
|
+
if (!socket.connected) return;
|
|
319
|
+
|
|
320
|
+
const payload = controllerSystemSchema.safeParse({
|
|
321
|
+
roomId: parsedRoomId,
|
|
322
|
+
command,
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
if (payload.success) {
|
|
326
|
+
socket.emit("controller:system", payload.data);
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
[parsedRoomId, socket, store],
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
return {
|
|
333
|
+
roomId: parsedRoomId,
|
|
334
|
+
controllerId: connectionState.controllerId,
|
|
335
|
+
connectionStatus: connectionState.connectionStatus,
|
|
336
|
+
lastError: connectionState.lastError,
|
|
337
|
+
activeUrl,
|
|
338
|
+
players: connectionState.players,
|
|
339
|
+
gameState: connectionState.gameState,
|
|
340
|
+
sendInput,
|
|
341
|
+
sendSystemCommand,
|
|
342
|
+
};
|
|
343
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
DocumentWithFullscreen,
|
|
4
|
+
ElementWithFullscreen,
|
|
5
|
+
} from "../types/browser";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hook to manage fullscreen state and transitions
|
|
9
|
+
* Supports standard and vendor-prefixed fullscreen APIs
|
|
10
|
+
*/
|
|
11
|
+
export const useFullscreen = () => {
|
|
12
|
+
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
const handleFullscreenChange = () => {
|
|
16
|
+
const doc = document as DocumentWithFullscreen;
|
|
17
|
+
setIsFullscreen(
|
|
18
|
+
!!(
|
|
19
|
+
document.fullscreenElement ||
|
|
20
|
+
doc.webkitFullscreenElement ||
|
|
21
|
+
doc.mozFullScreenElement ||
|
|
22
|
+
doc.msFullscreenElement
|
|
23
|
+
),
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
document.addEventListener("fullscreenchange", handleFullscreenChange);
|
|
28
|
+
document.addEventListener("webkitfullscreenchange", handleFullscreenChange);
|
|
29
|
+
document.addEventListener("mozfullscreenchange", handleFullscreenChange);
|
|
30
|
+
document.addEventListener("msfullscreenchange", handleFullscreenChange);
|
|
31
|
+
|
|
32
|
+
return () => {
|
|
33
|
+
document.removeEventListener("fullscreenchange", handleFullscreenChange);
|
|
34
|
+
document.removeEventListener(
|
|
35
|
+
"webkitfullscreenchange",
|
|
36
|
+
handleFullscreenChange,
|
|
37
|
+
);
|
|
38
|
+
document.removeEventListener(
|
|
39
|
+
"mozfullscreenchange",
|
|
40
|
+
handleFullscreenChange,
|
|
41
|
+
);
|
|
42
|
+
document.removeEventListener(
|
|
43
|
+
"msfullscreenchange",
|
|
44
|
+
handleFullscreenChange,
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
const exitFullscreen = useCallback(async () => {
|
|
50
|
+
try {
|
|
51
|
+
const doc = document as DocumentWithFullscreen;
|
|
52
|
+
if (
|
|
53
|
+
document.fullscreenElement ||
|
|
54
|
+
doc.webkitFullscreenElement ||
|
|
55
|
+
doc.mozFullScreenElement ||
|
|
56
|
+
doc.msFullscreenElement
|
|
57
|
+
) {
|
|
58
|
+
if (document.exitFullscreen) {
|
|
59
|
+
await document.exitFullscreen();
|
|
60
|
+
} else if (doc.webkitExitFullscreen) {
|
|
61
|
+
await doc.webkitExitFullscreen();
|
|
62
|
+
} else if (doc.mozCancelFullScreen) {
|
|
63
|
+
await doc.mozCancelFullScreen();
|
|
64
|
+
} else if (doc.msExitFullscreen) {
|
|
65
|
+
await doc.msExitFullscreen();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error("Error exiting fullscreen:", error);
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
72
|
+
|
|
73
|
+
const enterFullscreen = useCallback(async (element?: HTMLElement) => {
|
|
74
|
+
try {
|
|
75
|
+
const elem = (element ||
|
|
76
|
+
document.documentElement) as ElementWithFullscreen;
|
|
77
|
+
if (elem.requestFullscreen) {
|
|
78
|
+
await elem.requestFullscreen();
|
|
79
|
+
} else if (elem.webkitRequestFullscreen) {
|
|
80
|
+
await elem.webkitRequestFullscreen();
|
|
81
|
+
} else if (elem.mozRequestFullScreen) {
|
|
82
|
+
await elem.mozRequestFullScreen();
|
|
83
|
+
} else if (elem.msRequestFullscreen) {
|
|
84
|
+
await elem.msRequestFullscreen();
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error("Error entering fullscreen:", error);
|
|
88
|
+
}
|
|
89
|
+
}, []);
|
|
90
|
+
|
|
91
|
+
const toggleFullscreen = useCallback(
|
|
92
|
+
async (element?: HTMLElement) => {
|
|
93
|
+
if (isFullscreen) {
|
|
94
|
+
await exitFullscreen();
|
|
95
|
+
} else {
|
|
96
|
+
await enterFullscreen(element);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
[isFullscreen, exitFullscreen, enterFullscreen],
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
isFullscreen,
|
|
104
|
+
enterFullscreen,
|
|
105
|
+
exitFullscreen,
|
|
106
|
+
toggleFullscreen,
|
|
107
|
+
};
|
|
108
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module useGetInput
|
|
3
|
+
* @description Lightweight hook for accessing controller input without triggering re-renders.
|
|
4
|
+
*
|
|
5
|
+
* This hook is designed for performance-critical code paths like game loops
|
|
6
|
+
* where you need to read input frequently without causing React re-renders.
|
|
7
|
+
*
|
|
8
|
+
* **Key features:**
|
|
9
|
+
* - No store subscriptions = no re-renders on state changes
|
|
10
|
+
* - Typed input based on provider schema
|
|
11
|
+
* - Automatic latching support (if configured)
|
|
12
|
+
* - Stable function reference across renders
|
|
13
|
+
*
|
|
14
|
+
* **When to use this vs useAirJamHost().getInput:**
|
|
15
|
+
* - Use `useGetInput()` in components that render frequently (game objects, particles)
|
|
16
|
+
* - Use `useAirJamHost().getInput` when you already need other host state
|
|
17
|
+
*/
|
|
18
|
+
import { useCallback } from "react";
|
|
19
|
+
import type { z } from "zod";
|
|
20
|
+
import { useAirJamContext } from "../context/air-jam-context";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Lightweight hook for accessing controller input without store subscriptions.
|
|
24
|
+
*
|
|
25
|
+
* Returns a stable `getInput` function that retrieves the latest input for a
|
|
26
|
+
* given controller ID. Unlike `useAirJamHost()`, this hook does NOT subscribe
|
|
27
|
+
* to the store, so it won't cause re-renders when connection state changes.
|
|
28
|
+
*
|
|
29
|
+
* **Requirements:**
|
|
30
|
+
* - Must be used within an `AirJamProvider`
|
|
31
|
+
* - Input configuration must be provided to the provider
|
|
32
|
+
*
|
|
33
|
+
* @template TSchema - Zod schema type for input (from provider)
|
|
34
|
+
* @returns A stable function that retrieves input for a controller ID
|
|
35
|
+
*
|
|
36
|
+
* @example In a game loop (React Three Fiber)
|
|
37
|
+
* ```tsx
|
|
38
|
+
* const Ship = ({ playerId }: { playerId: string }) => {
|
|
39
|
+
* const getInput = useGetInput<typeof gameInputSchema>();
|
|
40
|
+
* const meshRef = useRef<THREE.Mesh>(null);
|
|
41
|
+
*
|
|
42
|
+
* useFrame(() => {
|
|
43
|
+
* const input = getInput(playerId);
|
|
44
|
+
* if (!input || !meshRef.current) return;
|
|
45
|
+
*
|
|
46
|
+
* // Move ship based on joystick
|
|
47
|
+
* meshRef.current.position.x += input.vector.x * SPEED;
|
|
48
|
+
* meshRef.current.position.y += input.vector.y * SPEED;
|
|
49
|
+
*
|
|
50
|
+
* // Fire if action button pressed (auto-latched)
|
|
51
|
+
* if (input.action) {
|
|
52
|
+
* fireLaser();
|
|
53
|
+
* }
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* return <mesh ref={meshRef}>...</mesh>;
|
|
57
|
+
* };
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @example In a regular React component
|
|
61
|
+
* ```tsx
|
|
62
|
+
* const InputDebugger = ({ playerId }: { playerId: string }) => {
|
|
63
|
+
* const getInput = useGetInput();
|
|
64
|
+
* const [display, setDisplay] = useState<string>("");
|
|
65
|
+
*
|
|
66
|
+
* useEffect(() => {
|
|
67
|
+
* const interval = setInterval(() => {
|
|
68
|
+
* const input = getInput(playerId);
|
|
69
|
+
* setDisplay(JSON.stringify(input, null, 2));
|
|
70
|
+
* }, 100);
|
|
71
|
+
* return () => clearInterval(interval);
|
|
72
|
+
* }, [playerId, getInput]);
|
|
73
|
+
*
|
|
74
|
+
* return <pre>{display}</pre>;
|
|
75
|
+
* };
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export const useGetInput = <TSchema extends z.ZodSchema = z.ZodSchema>(): ((
|
|
79
|
+
controllerId: string,
|
|
80
|
+
) => z.infer<TSchema> | undefined) => {
|
|
81
|
+
const { inputManager } = useAirJamContext();
|
|
82
|
+
|
|
83
|
+
// Use useCallback to return a stable function reference
|
|
84
|
+
// Access InputManager directly from context (no store subscriptions)
|
|
85
|
+
return useCallback(
|
|
86
|
+
(controllerId: string): z.infer<TSchema> | undefined => {
|
|
87
|
+
if (!inputManager) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return inputManager.getInput(controllerId) as
|
|
92
|
+
| z.infer<TSchema>
|
|
93
|
+
| undefined;
|
|
94
|
+
},
|
|
95
|
+
[inputManager],
|
|
96
|
+
);
|
|
97
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module useSendSignal
|
|
3
|
+
* @description Lightweight hook for sending signals (haptics, toasts) without triggering re-renders.
|
|
4
|
+
*
|
|
5
|
+
* This hook is designed for components that need to send feedback signals to controllers
|
|
6
|
+
* without subscribing to the connection store. Perfect for game objects that trigger
|
|
7
|
+
* haptic feedback on collisions, pickups, or other events.
|
|
8
|
+
*
|
|
9
|
+
* **Supported signal types:**
|
|
10
|
+
* - `HAPTIC` - Vibration patterns on the controller device
|
|
11
|
+
* - `TOAST` - Visual notifications displayed on the controller
|
|
12
|
+
*
|
|
13
|
+
* **When to use this vs useAirJamHost().sendSignal:**
|
|
14
|
+
* - Use `useSendSignal()` in components that render frequently (projectiles, collectibles)
|
|
15
|
+
* - Use `useAirJamHost().sendSignal` when you already need other host state
|
|
16
|
+
*/
|
|
17
|
+
import { useCallback } from "react";
|
|
18
|
+
import { useAirJamContext } from "../context/air-jam-context";
|
|
19
|
+
import type {
|
|
20
|
+
HapticSignalPayload,
|
|
21
|
+
SignalPayload,
|
|
22
|
+
SignalType,
|
|
23
|
+
ToastSignalPayload,
|
|
24
|
+
} from "../protocol";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Function signature for sending signals to controllers.
|
|
28
|
+
*
|
|
29
|
+
* Overloaded to provide proper typing for each signal type.
|
|
30
|
+
*/
|
|
31
|
+
export interface SendSignalFn {
|
|
32
|
+
/**
|
|
33
|
+
* Send haptic (vibration) feedback to a controller.
|
|
34
|
+
*
|
|
35
|
+
* @param type - Must be "HAPTIC"
|
|
36
|
+
* @param payload - Haptic configuration with pattern
|
|
37
|
+
* @param targetId - Controller ID (omit to send to all)
|
|
38
|
+
*
|
|
39
|
+
* @example Send heavy vibration to specific player
|
|
40
|
+
* ```ts
|
|
41
|
+
* sendSignal("HAPTIC", { pattern: "heavy" }, playerId);
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @example Send custom vibration pattern to all
|
|
45
|
+
* ```ts
|
|
46
|
+
* sendSignal("HAPTIC", {
|
|
47
|
+
* pattern: "custom",
|
|
48
|
+
* sequence: [50, 100, 50, 100, 200],
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
(type: "HAPTIC", payload: HapticSignalPayload, targetId?: string): void;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Send a toast notification to a controller.
|
|
56
|
+
*
|
|
57
|
+
* @param type - Must be "TOAST"
|
|
58
|
+
* @param payload - Toast content with title and message
|
|
59
|
+
* @param targetId - Controller ID (omit to send to all)
|
|
60
|
+
*
|
|
61
|
+
* @example Send achievement notification
|
|
62
|
+
* ```ts
|
|
63
|
+
* sendSignal("TOAST", {
|
|
64
|
+
* title: "Achievement Unlocked!",
|
|
65
|
+
* message: "First blood",
|
|
66
|
+
* variant: "success",
|
|
67
|
+
* }, playerId);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
(type: "TOAST", payload: ToastSignalPayload, targetId?: string): void;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Lightweight hook for sending signals without store subscriptions.
|
|
75
|
+
*
|
|
76
|
+
* Returns a stable `sendSignal` function that can send haptic feedback
|
|
77
|
+
* or toast notifications to controllers. Unlike `useAirJamHost()`, this
|
|
78
|
+
* hook does NOT subscribe to the store, preventing re-renders.
|
|
79
|
+
*
|
|
80
|
+
* **Haptic patterns:**
|
|
81
|
+
* - `light` - Quick tap (10ms)
|
|
82
|
+
* - `medium` - Standard vibration (30ms)
|
|
83
|
+
* - `heavy` - Strong pulse pattern
|
|
84
|
+
* - `success` - Short double tap
|
|
85
|
+
* - `failure` - Rapid pulses
|
|
86
|
+
* - `custom` - Custom sequence array
|
|
87
|
+
*
|
|
88
|
+
* @returns A stable sendSignal function
|
|
89
|
+
*
|
|
90
|
+
* @example In a collision handler
|
|
91
|
+
* ```tsx
|
|
92
|
+
* const Laser = ({ ownerId }: { ownerId: string }) => {
|
|
93
|
+
* const sendSignal = useSendSignal();
|
|
94
|
+
*
|
|
95
|
+
* const handleHit = (targetId: string) => {
|
|
96
|
+
* // Vibrate the player who got hit
|
|
97
|
+
* sendSignal("HAPTIC", { pattern: "heavy" }, targetId);
|
|
98
|
+
*
|
|
99
|
+
* // Light vibration for the shooter
|
|
100
|
+
* sendSignal("HAPTIC", { pattern: "light" }, ownerId);
|
|
101
|
+
* };
|
|
102
|
+
*
|
|
103
|
+
* // ... collision detection logic
|
|
104
|
+
* };
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @example For collectible pickups
|
|
108
|
+
* ```tsx
|
|
109
|
+
* const Collectible = () => {
|
|
110
|
+
* const sendSignal = useSendSignal();
|
|
111
|
+
*
|
|
112
|
+
* const handleCollect = (playerId: string) => {
|
|
113
|
+
* sendSignal("HAPTIC", { pattern: "success" }, playerId);
|
|
114
|
+
* sendSignal("TOAST", {
|
|
115
|
+
* title: "+10 Points",
|
|
116
|
+
* variant: "default",
|
|
117
|
+
* }, playerId);
|
|
118
|
+
* };
|
|
119
|
+
* };
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @example Game-wide announcements
|
|
123
|
+
* ```tsx
|
|
124
|
+
* const GameManager = () => {
|
|
125
|
+
* const sendSignal = useSendSignal();
|
|
126
|
+
*
|
|
127
|
+
* const announceRoundStart = () => {
|
|
128
|
+
* // No targetId = broadcast to all controllers
|
|
129
|
+
* sendSignal("TOAST", {
|
|
130
|
+
* title: "Round Start!",
|
|
131
|
+
* message: "Get ready to fight",
|
|
132
|
+
* variant: "default",
|
|
133
|
+
* });
|
|
134
|
+
* };
|
|
135
|
+
* };
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export const useSendSignal = (): SendSignalFn => {
|
|
139
|
+
const { getSocket } = useAirJamContext();
|
|
140
|
+
|
|
141
|
+
// Get socket directly without subscribing to store state
|
|
142
|
+
// The socket reference is stable from SocketManager
|
|
143
|
+
const sendSignal = useCallback(
|
|
144
|
+
(
|
|
145
|
+
type: SignalType,
|
|
146
|
+
payload: HapticSignalPayload | ToastSignalPayload,
|
|
147
|
+
targetId?: string,
|
|
148
|
+
): void => {
|
|
149
|
+
const socket = getSocket("host");
|
|
150
|
+
if (!socket || !socket.connected) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const signal: SignalPayload = {
|
|
154
|
+
targetId,
|
|
155
|
+
type,
|
|
156
|
+
payload,
|
|
157
|
+
} as SignalPayload;
|
|
158
|
+
socket.emit("host:signal", signal);
|
|
159
|
+
},
|
|
160
|
+
[getSocket],
|
|
161
|
+
) as SendSignalFn;
|
|
162
|
+
|
|
163
|
+
return sendSignal;
|
|
164
|
+
};
|