@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
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1529 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as React$1 from 'react';
|
|
3
|
+
import { ReactNode, JSX } from 'react';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { ControllerInputEvent, ConnectionRole, ConnectionStatus, RunMode, GameState, PlayerProfile, ServerToClientEvents, ClientToServerEvents, ControllerStatePayload, RoomCode, ControllerInputPayload, HapticSignalPayload, ToastSignalPayload } from './protocol.js';
|
|
6
|
+
export { AIRJAM_PROXY_PREFIX, AirJamProxyMessage, ClientLoadUiPayload, ControllerJoinAck, ControllerJoinPayload, ControllerJoinedNotice, ControllerLeavePayload, ControllerLeftNotice, ControllerStateMessage, ControllerSystemPayload, ControllerWelcomePayload, ErrorCode, HostJoinAsChildPayload, HostLeftNotice, HostRegisterSystemPayload, HostRegistrationAck, HostRegistrationPayload, InterServerEvents, PlaySoundEventPayload, PlaySoundPayload, ProxyInputMessage, ProxyMessageBase, ProxyReadyMessage, ProxyStateMessage, RoomReadyNotice, ServerErrorPayload, SignalPayload, SignalType, SocketData, SystemLaunchGameAck, SystemLaunchGamePayload, controllerInputSchema, controllerJoinSchema, controllerLeaveSchema, controllerStateSchema, controllerSystemSchema, hostJoinAsChildSchema, hostRegisterSystemSchema, hostRegistrationSchema, roomCodeSchema, systemLaunchGameSchema } from './protocol.js';
|
|
7
|
+
import * as zustand from 'zustand';
|
|
8
|
+
import { StoreApi } from 'zustand';
|
|
9
|
+
import { Socket } from 'socket.io-client';
|
|
10
|
+
import * as class_variance_authority_types from 'class-variance-authority/types';
|
|
11
|
+
import { VariantProps } from 'class-variance-authority';
|
|
12
|
+
import * as SliderPrimitive from '@radix-ui/react-slider';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @module InputManager
|
|
16
|
+
* @description Internal class for managing controller input with validation and latching.
|
|
17
|
+
*
|
|
18
|
+
* The InputManager is created internally by the AirJamProvider when input configuration
|
|
19
|
+
* is provided. It handles:
|
|
20
|
+
*
|
|
21
|
+
* 1. **Input Buffering** - Stores the latest input for each controller
|
|
22
|
+
* 2. **Validation** - Validates input against a Zod schema (optional)
|
|
23
|
+
* 3. **Latching** - Ensures rapid button presses and stick flicks are never missed
|
|
24
|
+
*
|
|
25
|
+
* ## What is Latching?
|
|
26
|
+
*
|
|
27
|
+
* Game loops typically run at 60fps, but network events may arrive between frames.
|
|
28
|
+
* Without latching, a quick button tap might be missed if it starts and ends between
|
|
29
|
+
* two consecutive `getInput()` calls.
|
|
30
|
+
*
|
|
31
|
+
* **Latching solves this by:**
|
|
32
|
+
* - Boolean fields: Keeping `true` values until consumed
|
|
33
|
+
* - Vector fields: Keeping non-zero values for one frame after release
|
|
34
|
+
*
|
|
35
|
+
* ## Example: Without Latching (Bug)
|
|
36
|
+
* ```
|
|
37
|
+
* Frame 1: getInput() → action: false
|
|
38
|
+
* [Network: action: true arrives, then action: false arrives]
|
|
39
|
+
* Frame 2: getInput() → action: false ← Tap missed!
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* ## Example: With Latching (Fixed)
|
|
43
|
+
* ```
|
|
44
|
+
* Frame 1: getInput() → action: false
|
|
45
|
+
* [Network: action: true arrives, then action: false arrives]
|
|
46
|
+
* Frame 2: getInput() → action: true ← Tap captured!
|
|
47
|
+
* Frame 3: getInput() → action: false ← Consumed, resets
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @internal This class is not exported publicly. Use hooks to access input.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Configuration for input handling with optional validation and latching.
|
|
55
|
+
*
|
|
56
|
+
* Provided to `AirJamProvider` to enable typed, validated, and latched input.
|
|
57
|
+
*
|
|
58
|
+
* @template TSchema - Zod schema type for input validation
|
|
59
|
+
*
|
|
60
|
+
* @example Basic schema validation
|
|
61
|
+
* ```ts
|
|
62
|
+
* const config: InputConfig = {
|
|
63
|
+
* schema: z.object({
|
|
64
|
+
* vector: z.object({ x: z.number(), y: z.number() }),
|
|
65
|
+
* action: z.boolean(),
|
|
66
|
+
* }),
|
|
67
|
+
* };
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @example With latching for button and stick
|
|
71
|
+
* ```ts
|
|
72
|
+
* const config: InputConfig = {
|
|
73
|
+
* schema: gameInputSchema,
|
|
74
|
+
* latch: {
|
|
75
|
+
* booleanFields: ["action", "ability", "jump"],
|
|
76
|
+
* vectorFields: ["vector", "aim"],
|
|
77
|
+
* },
|
|
78
|
+
* };
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
interface InputConfig<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
82
|
+
/**
|
|
83
|
+
* Zod schema for input validation and type inference.
|
|
84
|
+
*
|
|
85
|
+
* When provided:
|
|
86
|
+
* - Incoming input is validated against the schema
|
|
87
|
+
* - Invalid input returns `undefined` (with console warning)
|
|
88
|
+
* - Return type is inferred from the schema
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* schema: z.object({
|
|
93
|
+
* vector: z.object({ x: z.number(), y: z.number() }),
|
|
94
|
+
* action: z.boolean(),
|
|
95
|
+
* ability: z.boolean(),
|
|
96
|
+
* timestamp: z.number(),
|
|
97
|
+
* })
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
schema?: TSchema;
|
|
101
|
+
/**
|
|
102
|
+
* Latching configuration to ensure rapid taps and flicks are never missed.
|
|
103
|
+
*
|
|
104
|
+
* Latching "holds" boolean true values and non-zero vectors until the next
|
|
105
|
+
* `getInput()` call, preventing missed inputs between game frames.
|
|
106
|
+
*/
|
|
107
|
+
latch?: {
|
|
108
|
+
/**
|
|
109
|
+
* Boolean field names that should be latched.
|
|
110
|
+
*
|
|
111
|
+
* When a boolean field is latched:
|
|
112
|
+
* - If it becomes `true`, it stays `true` until consumed by `getInput()`
|
|
113
|
+
* - After consumption, it resets to the actual current value
|
|
114
|
+
*
|
|
115
|
+
* Use for: buttons, triggers, action inputs
|
|
116
|
+
*
|
|
117
|
+
* @example ["action", "ability", "jump", "fire"]
|
|
118
|
+
*/
|
|
119
|
+
booleanFields?: string[];
|
|
120
|
+
/**
|
|
121
|
+
* Vector field names that should be latched.
|
|
122
|
+
*
|
|
123
|
+
* When a vector field is latched:
|
|
124
|
+
* - Non-zero vectors are kept for one frame after returning to zero
|
|
125
|
+
* - Ensures quick stick flicks register in the game loop
|
|
126
|
+
*
|
|
127
|
+
* Use for: joysticks, d-pads, directional inputs
|
|
128
|
+
*
|
|
129
|
+
* @example ["vector", "movement", "aim"]
|
|
130
|
+
*/
|
|
131
|
+
vectorFields?: string[];
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Internal class that manages input buffering, validation, and latching.
|
|
136
|
+
*
|
|
137
|
+
* Created automatically by `AirJamProvider` when input configuration is provided.
|
|
138
|
+
* Access input via `useGetInput()` or `useAirJamHost().getInput()`.
|
|
139
|
+
*
|
|
140
|
+
* @template TSchema - Zod schema type for input validation
|
|
141
|
+
*
|
|
142
|
+
* @internal Not exported publicly. Use hooks to access input.
|
|
143
|
+
*
|
|
144
|
+
* @example How it's used internally
|
|
145
|
+
* ```ts
|
|
146
|
+
* // In AirJamProvider
|
|
147
|
+
* const inputManager = new InputManager({
|
|
148
|
+
* schema: gameInputSchema,
|
|
149
|
+
* latch: { booleanFields: ["action"] },
|
|
150
|
+
* });
|
|
151
|
+
*
|
|
152
|
+
* // In useAirJamHost (socket handler)
|
|
153
|
+
* socket.on("server:input", (payload) => {
|
|
154
|
+
* inputManager.handleInput(payload);
|
|
155
|
+
* });
|
|
156
|
+
*
|
|
157
|
+
* // In game loop (via useGetInput)
|
|
158
|
+
* const input = inputManager.getInput(playerId);
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
declare class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
162
|
+
private inputBuffer;
|
|
163
|
+
private latchState;
|
|
164
|
+
private config;
|
|
165
|
+
private roomId?;
|
|
166
|
+
constructor(config?: InputConfig<TSchema>, roomId?: string);
|
|
167
|
+
/**
|
|
168
|
+
* Handles incoming input events from the socket.
|
|
169
|
+
* Should be called when 'server:input' event is received.
|
|
170
|
+
*/
|
|
171
|
+
handleInput(payload: ControllerInputEvent): void;
|
|
172
|
+
/**
|
|
173
|
+
* Gets input for a specific controller.
|
|
174
|
+
* Returns validated and typed input if schema is provided, otherwise raw Record.
|
|
175
|
+
* If latching is configured, applies latching logic.
|
|
176
|
+
*/
|
|
177
|
+
getInput(controllerId: string): z.infer<TSchema> | undefined;
|
|
178
|
+
/**
|
|
179
|
+
* Clears input state for a controller (e.g., when player leaves).
|
|
180
|
+
*/
|
|
181
|
+
clearInput(controllerId: string): void;
|
|
182
|
+
/**
|
|
183
|
+
* Updates the room ID filter.
|
|
184
|
+
*/
|
|
185
|
+
setRoomId(roomId: string | undefined): void;
|
|
186
|
+
/**
|
|
187
|
+
* Internal method to apply latching logic.
|
|
188
|
+
*/
|
|
189
|
+
private getLatched;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
interface AirJamStore {
|
|
193
|
+
role: ConnectionRole | null;
|
|
194
|
+
roomId: string | null;
|
|
195
|
+
controllerId: string | null;
|
|
196
|
+
connectionStatus: ConnectionStatus;
|
|
197
|
+
mode: RunMode;
|
|
198
|
+
gameState: GameState;
|
|
199
|
+
stateMessage?: string;
|
|
200
|
+
players: PlayerProfile[];
|
|
201
|
+
lastError?: string;
|
|
202
|
+
registeredRoomId: string | null;
|
|
203
|
+
setRole: (role: ConnectionRole | null) => void;
|
|
204
|
+
setRoomId: (roomId: string | null) => void;
|
|
205
|
+
setControllerId: (controllerId: string | null) => void;
|
|
206
|
+
setStatus: (status: ConnectionStatus) => void;
|
|
207
|
+
setMode: (mode: RunMode) => void;
|
|
208
|
+
setGameState: (state: GameState) => void;
|
|
209
|
+
setStateMessage: (message?: string) => void;
|
|
210
|
+
toggleGameState: () => void;
|
|
211
|
+
setError: (message?: string) => void;
|
|
212
|
+
upsertPlayer: (player: PlayerProfile) => void;
|
|
213
|
+
removePlayer: (playerId: string) => void;
|
|
214
|
+
resetPlayers: () => void;
|
|
215
|
+
resetGameState: () => void;
|
|
216
|
+
setRegisteredRoomId: (roomId: string | null) => void;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Factory function to create a new AirJamStore instance.
|
|
220
|
+
* Each AirJamProvider creates its own store for multi-instance support.
|
|
221
|
+
*/
|
|
222
|
+
declare const createAirJamStore: () => StoreApi<AirJamStore>;
|
|
223
|
+
|
|
224
|
+
type AirJamSocket = Socket<ServerToClientEvents, ClientToServerEvents>;
|
|
225
|
+
/**
|
|
226
|
+
* Manages socket connections per role within a provider instance.
|
|
227
|
+
* Unlike the global singleton, each SocketManager is scoped to its provider.
|
|
228
|
+
*/
|
|
229
|
+
declare class SocketManager {
|
|
230
|
+
private sockets;
|
|
231
|
+
private serverUrl;
|
|
232
|
+
constructor(serverUrl?: string);
|
|
233
|
+
/**
|
|
234
|
+
* Get or create a socket for the specified role.
|
|
235
|
+
* Sockets are lazily created and cached per role.
|
|
236
|
+
*/
|
|
237
|
+
getSocket(role: ConnectionRole): AirJamSocket;
|
|
238
|
+
/**
|
|
239
|
+
* Disconnect a socket by role.
|
|
240
|
+
* NOTE: We intentionally do NOT delete the socket from cache.
|
|
241
|
+
* This ensures all hooks using getSocket() get the same instance,
|
|
242
|
+
* even during React Strict Mode remounts. The socket can be reconnected
|
|
243
|
+
* by calling socket.connect() again.
|
|
244
|
+
*/
|
|
245
|
+
disconnect(role: ConnectionRole): void;
|
|
246
|
+
/**
|
|
247
|
+
* Disconnect all sockets managed by this instance
|
|
248
|
+
*/
|
|
249
|
+
disconnectAll(): void;
|
|
250
|
+
/**
|
|
251
|
+
* Get the resolved server URL
|
|
252
|
+
*/
|
|
253
|
+
getServerUrl(): string;
|
|
254
|
+
/**
|
|
255
|
+
* Resolve server URL with fallbacks
|
|
256
|
+
*/
|
|
257
|
+
private resolveServerUrl;
|
|
258
|
+
private normalizeOrigin;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Resolved configuration for the AirJam SDK.
|
|
263
|
+
* Created from AirJamProviderProps with environment variable fallbacks applied.
|
|
264
|
+
*/
|
|
265
|
+
interface AirJamConfig {
|
|
266
|
+
/** WebSocket server URL. Falls back to env vars or window location */
|
|
267
|
+
serverUrl?: string;
|
|
268
|
+
/** API key for production authentication */
|
|
269
|
+
apiKey?: string;
|
|
270
|
+
/** Path for controller page (default: /joypad) */
|
|
271
|
+
controllerPath: string;
|
|
272
|
+
/** Maximum players allowed in a room */
|
|
273
|
+
maxPlayers: number;
|
|
274
|
+
/** Public host for controller URLs (optional) */
|
|
275
|
+
publicHost?: string;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Props for the AirJamProvider component.
|
|
279
|
+
*
|
|
280
|
+
* @template TSchema - Zod schema type for input validation (inferred from input.schema)
|
|
281
|
+
*
|
|
282
|
+
* @example Minimal configuration (uses environment variables)
|
|
283
|
+
* ```tsx
|
|
284
|
+
* <AirJamProvider>
|
|
285
|
+
* <App />
|
|
286
|
+
* </AirJamProvider>
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* @example Full configuration
|
|
290
|
+
* ```tsx
|
|
291
|
+
* <AirJamProvider
|
|
292
|
+
* serverUrl="wss://your-server.com"
|
|
293
|
+
* apiKey="your-api-key"
|
|
294
|
+
* controllerPath="/controller"
|
|
295
|
+
* maxPlayers={4}
|
|
296
|
+
* input={{
|
|
297
|
+
* schema: myInputSchema,
|
|
298
|
+
* latch: { booleanFields: ["fire"], vectorFields: ["move"] },
|
|
299
|
+
* }}
|
|
300
|
+
* >
|
|
301
|
+
* <App />
|
|
302
|
+
* </AirJamProvider>
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
interface AirJamProviderProps<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
306
|
+
/** React children to render within the provider */
|
|
307
|
+
children: ReactNode;
|
|
308
|
+
/**
|
|
309
|
+
* WebSocket server URL for AirJam connections.
|
|
310
|
+
* Falls back to VITE_AIR_JAM_SERVER_URL or NEXT_PUBLIC_AIR_JAM_SERVER_URL environment variables.
|
|
311
|
+
*/
|
|
312
|
+
serverUrl?: string;
|
|
313
|
+
/**
|
|
314
|
+
* API key for production authentication.
|
|
315
|
+
* Falls back to VITE_AIR_JAM_API_KEY or NEXT_PUBLIC_AIR_JAM_API_KEY environment variables.
|
|
316
|
+
*/
|
|
317
|
+
apiKey?: string;
|
|
318
|
+
/**
|
|
319
|
+
* URL path for the controller page on your app.
|
|
320
|
+
* Controllers will be directed to this path with ?room=XXXX query parameter.
|
|
321
|
+
* @default "/joypad"
|
|
322
|
+
*/
|
|
323
|
+
controllerPath?: string;
|
|
324
|
+
/**
|
|
325
|
+
* Maximum number of players allowed in a room.
|
|
326
|
+
* @default 8
|
|
327
|
+
*/
|
|
328
|
+
maxPlayers?: number;
|
|
329
|
+
/**
|
|
330
|
+
* Public hostname for controller URLs.
|
|
331
|
+
* Useful when your app is behind a proxy or has a different public URL.
|
|
332
|
+
*/
|
|
333
|
+
publicHost?: string;
|
|
334
|
+
/**
|
|
335
|
+
* Input handling configuration including Zod schema validation and latching.
|
|
336
|
+
* When provided, enables typed input with automatic validation and latch support.
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* ```ts
|
|
340
|
+
* input={{
|
|
341
|
+
* schema: z.object({
|
|
342
|
+
* vector: z.object({ x: z.number(), y: z.number() }),
|
|
343
|
+
* action: z.boolean(),
|
|
344
|
+
* ability: z.boolean(),
|
|
345
|
+
* }),
|
|
346
|
+
* latch: {
|
|
347
|
+
* booleanFields: ["action", "ability"], // Latch button presses
|
|
348
|
+
* vectorFields: ["vector"], // Latch stick flicks
|
|
349
|
+
* },
|
|
350
|
+
* }}
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
353
|
+
input?: InputConfig<TSchema>;
|
|
354
|
+
}
|
|
355
|
+
interface AirJamContextValue {
|
|
356
|
+
/** Resolved configuration with env fallbacks */
|
|
357
|
+
config: AirJamConfig;
|
|
358
|
+
/** Context-bound Zustand store */
|
|
359
|
+
store: ReturnType<typeof createAirJamStore>;
|
|
360
|
+
/** Socket manager for this provider instance */
|
|
361
|
+
socketManager: SocketManager;
|
|
362
|
+
/** Get socket for a specific role */
|
|
363
|
+
getSocket: (role: ConnectionRole) => AirJamSocket;
|
|
364
|
+
/** Disconnect socket for a specific role */
|
|
365
|
+
disconnectSocket: (role: ConnectionRole) => void;
|
|
366
|
+
/** InputManager instance (created from input config if provided) */
|
|
367
|
+
inputManager: InputManager | null;
|
|
368
|
+
}
|
|
369
|
+
declare const AirJamContext: React$1.Context<AirJamContextValue | null>;
|
|
370
|
+
/**
|
|
371
|
+
* Root provider component for the AirJam SDK.
|
|
372
|
+
*
|
|
373
|
+
* Wrap your application (or game component tree) with this provider to enable
|
|
374
|
+
* AirJam functionality. The provider manages WebSocket connections, global state,
|
|
375
|
+
* and input processing.
|
|
376
|
+
*
|
|
377
|
+
* **Key responsibilities:**
|
|
378
|
+
* - Creates and manages WebSocket connections to the AirJam server
|
|
379
|
+
* - Provides a Zustand store for connection state (players, game state, etc.)
|
|
380
|
+
* - Creates an InputManager for typed input validation and latching
|
|
381
|
+
* - Resolves configuration from props and environment variables
|
|
382
|
+
*
|
|
383
|
+
* @template TSchema - Zod schema for input validation (inferred from props)
|
|
384
|
+
*
|
|
385
|
+
* @example Basic usage
|
|
386
|
+
* ```tsx
|
|
387
|
+
* import { AirJamProvider } from "@air-jam/sdk";
|
|
388
|
+
*
|
|
389
|
+
* export const App = () => (
|
|
390
|
+
* <AirJamProvider>
|
|
391
|
+
* <Routes>
|
|
392
|
+
* <Route path="/" element={<HostView />} />
|
|
393
|
+
* <Route path="/joypad" element={<ControllerView />} />
|
|
394
|
+
* </Routes>
|
|
395
|
+
* </AirJamProvider>
|
|
396
|
+
* );
|
|
397
|
+
* ```
|
|
398
|
+
*
|
|
399
|
+
* @example With typed input and latching
|
|
400
|
+
* ```tsx
|
|
401
|
+
* import { AirJamProvider } from "@air-jam/sdk";
|
|
402
|
+
* import { z } from "zod";
|
|
403
|
+
*
|
|
404
|
+
* const gameInputSchema = z.object({
|
|
405
|
+
* vector: z.object({ x: z.number(), y: z.number() }),
|
|
406
|
+
* action: z.boolean(),
|
|
407
|
+
* ability: z.boolean(),
|
|
408
|
+
* timestamp: z.number(),
|
|
409
|
+
* });
|
|
410
|
+
*
|
|
411
|
+
* export const App = () => (
|
|
412
|
+
* <AirJamProvider
|
|
413
|
+
* input={{
|
|
414
|
+
* schema: gameInputSchema,
|
|
415
|
+
* latch: {
|
|
416
|
+
* booleanFields: ["action", "ability"],
|
|
417
|
+
* vectorFields: ["vector"],
|
|
418
|
+
* },
|
|
419
|
+
* }}
|
|
420
|
+
* >
|
|
421
|
+
* <GameRoutes />
|
|
422
|
+
* </AirJamProvider>
|
|
423
|
+
* );
|
|
424
|
+
* ```
|
|
425
|
+
*/
|
|
426
|
+
declare const AirJamProvider: <TSchema extends z.ZodSchema = z.ZodSchema>({ children, serverUrl, apiKey, controllerPath, maxPlayers, publicHost, input, }: AirJamProviderProps<TSchema>) => react_jsx_runtime.JSX.Element;
|
|
427
|
+
/**
|
|
428
|
+
* Access the raw AirJam context value.
|
|
429
|
+
*
|
|
430
|
+
* This is a low-level hook that provides direct access to the context.
|
|
431
|
+
* Most applications should use the higher-level hooks instead:
|
|
432
|
+
* - `useAirJamHost()` - For host/game functionality
|
|
433
|
+
* - `useAirJamController()` - For controller functionality
|
|
434
|
+
* - `useGetInput()` - For input access without re-renders
|
|
435
|
+
* - `useSendSignal()` - For sending signals without re-renders
|
|
436
|
+
*
|
|
437
|
+
* @throws Error if used outside of an AirJamProvider
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```tsx
|
|
441
|
+
* const { config, store, inputManager } = useAirJamContext();
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
declare const useAirJamContext: () => AirJamContextValue;
|
|
445
|
+
/**
|
|
446
|
+
* Access the resolved AirJam configuration.
|
|
447
|
+
*
|
|
448
|
+
* Returns the configuration with all environment variable fallbacks resolved.
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* ```tsx
|
|
452
|
+
* const config = useAirJamConfig();
|
|
453
|
+
* console.log(config.serverUrl, config.maxPlayers);
|
|
454
|
+
* ```
|
|
455
|
+
*/
|
|
456
|
+
declare const useAirJamConfig: () => AirJamConfig;
|
|
457
|
+
/**
|
|
458
|
+
* Access AirJam store state with a selector.
|
|
459
|
+
*
|
|
460
|
+
* Uses Zustand's useShallow for optimal re-render behavior.
|
|
461
|
+
* Only re-renders when the selected values actually change.
|
|
462
|
+
*
|
|
463
|
+
* @param selector - Function to select state from the store
|
|
464
|
+
* @returns The selected state value
|
|
465
|
+
*
|
|
466
|
+
* @example
|
|
467
|
+
* ```tsx
|
|
468
|
+
* const { players, gameState } = useAirJamState((state) => ({
|
|
469
|
+
* players: state.players,
|
|
470
|
+
* gameState: state.gameState,
|
|
471
|
+
* }));
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
declare const useAirJamState: <T>(selector: (state: AirJamStore) => T) => T;
|
|
475
|
+
/**
|
|
476
|
+
* Get a socket instance for the specified role.
|
|
477
|
+
*
|
|
478
|
+
* Returns the same socket instance across renders (stable reference).
|
|
479
|
+
* The socket is managed by the SocketManager and shared across hooks.
|
|
480
|
+
*
|
|
481
|
+
* @param role - Either "host" or "controller"
|
|
482
|
+
* @returns The Socket.IO socket instance
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* ```tsx
|
|
486
|
+
* const socket = useAirJamSocket("host");
|
|
487
|
+
* socket.emit("custom:event", { data: "value" });
|
|
488
|
+
* ```
|
|
489
|
+
*/
|
|
490
|
+
declare const useAirJamSocket: (role: ConnectionRole) => AirJamSocket;
|
|
491
|
+
|
|
492
|
+
type SoundCategory = "music" | "sfx";
|
|
493
|
+
interface SoundConfig {
|
|
494
|
+
src: string[];
|
|
495
|
+
volume?: number;
|
|
496
|
+
loop?: boolean;
|
|
497
|
+
html5?: boolean;
|
|
498
|
+
sprite?: {
|
|
499
|
+
[key: string]: [number, number];
|
|
500
|
+
};
|
|
501
|
+
category?: SoundCategory;
|
|
502
|
+
}
|
|
503
|
+
type SoundManifest = Record<string, SoundConfig>;
|
|
504
|
+
/**
|
|
505
|
+
* Auto-detect sound category based on config heuristics
|
|
506
|
+
* - If explicitly set, use it
|
|
507
|
+
* - If loop=true and html5=true, likely music
|
|
508
|
+
* - Otherwise, default to sfx
|
|
509
|
+
*/
|
|
510
|
+
declare function detectSoundCategory(config: SoundConfig): SoundCategory;
|
|
511
|
+
interface PlayOptions {
|
|
512
|
+
remote?: boolean;
|
|
513
|
+
target?: string;
|
|
514
|
+
volume?: number;
|
|
515
|
+
loop?: boolean;
|
|
516
|
+
sprite?: string;
|
|
517
|
+
pitch?: number;
|
|
518
|
+
}
|
|
519
|
+
declare class AudioManager<T extends string = string> {
|
|
520
|
+
private sounds;
|
|
521
|
+
private categories;
|
|
522
|
+
private manifest;
|
|
523
|
+
private _muted;
|
|
524
|
+
private socket;
|
|
525
|
+
private roomId;
|
|
526
|
+
private activeSoundIds;
|
|
527
|
+
private volumeUnsubscribe;
|
|
528
|
+
constructor(manifest?: SoundManifest);
|
|
529
|
+
/**
|
|
530
|
+
* Cleanup method to unsubscribe from volume changes
|
|
531
|
+
*/
|
|
532
|
+
destroy(): void;
|
|
533
|
+
/**
|
|
534
|
+
* Get the category for a sound (with auto-detection)
|
|
535
|
+
*/
|
|
536
|
+
getCategory(soundId: string): SoundCategory;
|
|
537
|
+
setSocket(socket: Socket<ServerToClientEvents, ClientToServerEvents> | null, roomId?: string): void;
|
|
538
|
+
/**
|
|
539
|
+
* Initialize audio context (must be called on user interaction)
|
|
540
|
+
*/
|
|
541
|
+
init(): void;
|
|
542
|
+
/**
|
|
543
|
+
* Check if audio context is ready to play sounds
|
|
544
|
+
*/
|
|
545
|
+
isReady(): boolean;
|
|
546
|
+
load(manifest: SoundManifest): void;
|
|
547
|
+
play(id: T, options?: PlayOptions): number | null;
|
|
548
|
+
private playLocal;
|
|
549
|
+
private playRemote;
|
|
550
|
+
private isHost;
|
|
551
|
+
/**
|
|
552
|
+
* Play a sound at a specific 3D position
|
|
553
|
+
*/
|
|
554
|
+
playSpatial(id: T, pos: {
|
|
555
|
+
x: number;
|
|
556
|
+
y: number;
|
|
557
|
+
z: number;
|
|
558
|
+
}, sprite?: string): number | null;
|
|
559
|
+
stop(id?: T, soundId?: number): void;
|
|
560
|
+
volume(vol: number, id?: T, soundId?: number): void;
|
|
561
|
+
mute(muted: boolean, id?: T, soundId?: number): void;
|
|
562
|
+
isMuted(): boolean;
|
|
563
|
+
/**
|
|
564
|
+
* Update the listener position for spatial audio
|
|
565
|
+
*/
|
|
566
|
+
setListenerPos(x: number, y: number, z: number): void;
|
|
567
|
+
/**
|
|
568
|
+
* Update the listener orientation for spatial audio
|
|
569
|
+
*/
|
|
570
|
+
setListenerOrientation(x: number, y: number, z: number, xUp: number, yUp: number, zUp: number): void;
|
|
571
|
+
}
|
|
572
|
+
declare const createAudioManager: <T extends string = string>(manifest?: SoundManifest) => AudioManager<T>;
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Hook to use audio with a sound manifest
|
|
576
|
+
* Creates a singleton AudioManager for the manifest and handles socket injection
|
|
577
|
+
* @param manifest The sound manifest configuration
|
|
578
|
+
* @returns The AudioManager instance with type-safe sound IDs
|
|
579
|
+
*/
|
|
580
|
+
declare function useAudio<M extends SoundManifest>(manifest: M): AudioManager<keyof M & string>;
|
|
581
|
+
declare const AudioProvider: ({ children, manager, }: {
|
|
582
|
+
children: React.ReactNode;
|
|
583
|
+
manager: AudioManager<any>;
|
|
584
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
585
|
+
declare const useAudioLegacy: <T extends string = string>() => AudioManager<T>;
|
|
586
|
+
/**
|
|
587
|
+
* Hook to create a stable AudioManager instance
|
|
588
|
+
*/
|
|
589
|
+
declare const useAudioManager: <T extends string = string>(manifest: SoundManifest) => AudioManager<T>;
|
|
590
|
+
|
|
591
|
+
interface VolumeSettings {
|
|
592
|
+
masterVolume: number;
|
|
593
|
+
musicVolume: number;
|
|
594
|
+
sfxVolume: number;
|
|
595
|
+
setMasterVolume: (volume: number) => void;
|
|
596
|
+
setMusicVolume: (volume: number) => void;
|
|
597
|
+
setSfxVolume: (volume: number) => void;
|
|
598
|
+
getEffectiveVolume: (category: "music" | "sfx") => number;
|
|
599
|
+
}
|
|
600
|
+
declare const useVolumeStore: zustand.UseBoundStore<zustand.StoreApi<VolumeSettings>>;
|
|
601
|
+
|
|
602
|
+
interface AirJamOverlayProps {
|
|
603
|
+
roomId: string;
|
|
604
|
+
joinUrl: string;
|
|
605
|
+
connectionStatus: ConnectionStatus;
|
|
606
|
+
players: PlayerProfile[];
|
|
607
|
+
lastError?: string;
|
|
608
|
+
gameState: GameState;
|
|
609
|
+
onTogglePlayPause?: () => void;
|
|
610
|
+
isChildMode?: boolean;
|
|
611
|
+
}
|
|
612
|
+
declare const AirJamOverlay: ({ roomId, joinUrl, connectionStatus, players, lastError, gameState, onTogglePlayPause, isChildMode, }: AirJamOverlayProps) => JSX.Element | null;
|
|
613
|
+
|
|
614
|
+
type OrientationRequirement = "portrait" | "landscape" | "any";
|
|
615
|
+
interface ControllerShellProps {
|
|
616
|
+
roomId?: string | null;
|
|
617
|
+
connectionStatus: ConnectionStatus;
|
|
618
|
+
requiredOrientation?: OrientationRequirement;
|
|
619
|
+
children: ReactNode;
|
|
620
|
+
gameState?: "paused" | "playing";
|
|
621
|
+
onTogglePlayPause?: () => void;
|
|
622
|
+
onReconnect?: (roomCode: string) => void;
|
|
623
|
+
onRefresh?: () => void;
|
|
624
|
+
customActions?: ReactNode;
|
|
625
|
+
}
|
|
626
|
+
declare const ControllerShell: ({ roomId, connectionStatus, requiredOrientation, children, gameState, onTogglePlayPause, onReconnect, onRefresh, customActions, }: ControllerShellProps) => JSX.Element;
|
|
627
|
+
|
|
628
|
+
declare const buttonVariants: (props?: ({
|
|
629
|
+
variant?: "default" | "link" | "destructive" | "secondary" | "outline" | "ghost" | null | undefined;
|
|
630
|
+
size?: "default" | "sm" | "lg" | "icon" | "icon-sm" | "icon-lg" | null | undefined;
|
|
631
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
632
|
+
declare function Button({ className, variant, size, asChild, ...props }: React$1.ComponentProps<"button"> & VariantProps<typeof buttonVariants> & {
|
|
633
|
+
asChild?: boolean;
|
|
634
|
+
}): react_jsx_runtime.JSX.Element;
|
|
635
|
+
|
|
636
|
+
declare function Slider({ className, defaultValue, value, min, max, ...props }: React$1.ComponentProps<typeof SliderPrimitive.Root>): react_jsx_runtime.JSX.Element;
|
|
637
|
+
|
|
638
|
+
interface VolumeControlsProps {
|
|
639
|
+
className?: string;
|
|
640
|
+
compact?: boolean;
|
|
641
|
+
}
|
|
642
|
+
declare function VolumeControls({ className, compact }: VolumeControlsProps): react_jsx_runtime.JSX.Element;
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Core Air Jam constants used across the SDK
|
|
646
|
+
*/
|
|
647
|
+
/**
|
|
648
|
+
* Default server port for Air Jam server
|
|
649
|
+
*/
|
|
650
|
+
declare const DEFAULT_SERVER_PORT = 4000;
|
|
651
|
+
/**
|
|
652
|
+
* Default path for controller endpoints
|
|
653
|
+
*/
|
|
654
|
+
declare const DEFAULT_CONTROLLER_PATH = "/joypad";
|
|
655
|
+
/**
|
|
656
|
+
* Input debounce time in milliseconds
|
|
657
|
+
* Used to prevent rapid input events from overwhelming the system
|
|
658
|
+
*/
|
|
659
|
+
declare const INPUT_DEBOUNCE_MS = 200;
|
|
660
|
+
/**
|
|
661
|
+
* Toggle debounce time in milliseconds
|
|
662
|
+
* Used for game state toggles to prevent accidental double-toggles
|
|
663
|
+
*/
|
|
664
|
+
declare const TOGGLE_DEBOUNCE_MS = 300;
|
|
665
|
+
/**
|
|
666
|
+
* Socket.IO configuration
|
|
667
|
+
*/
|
|
668
|
+
declare const SOCKET_CONFIG: {
|
|
669
|
+
/**
|
|
670
|
+
* Ping interval in milliseconds
|
|
671
|
+
*/
|
|
672
|
+
readonly PING_INTERVAL: 2000;
|
|
673
|
+
/**
|
|
674
|
+
* Ping timeout in milliseconds
|
|
675
|
+
*/
|
|
676
|
+
readonly PING_TIMEOUT: 5000;
|
|
677
|
+
};
|
|
678
|
+
/**
|
|
679
|
+
* Maximum players allowed in a room by default
|
|
680
|
+
*/
|
|
681
|
+
declare const DEFAULT_MAX_PLAYERS = 8;
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Air Jam Socket.IO event names
|
|
685
|
+
* Centralized to ensure consistency and type safety
|
|
686
|
+
*/
|
|
687
|
+
/**
|
|
688
|
+
* Host events
|
|
689
|
+
*/
|
|
690
|
+
declare const HostEvents: {
|
|
691
|
+
/**
|
|
692
|
+
* Register as a master host (standalone mode)
|
|
693
|
+
*/
|
|
694
|
+
readonly REGISTER: "host:register";
|
|
695
|
+
/**
|
|
696
|
+
* Register as the system/arcade master host
|
|
697
|
+
*/
|
|
698
|
+
readonly REGISTER_SYSTEM: "host:registerSystem";
|
|
699
|
+
/**
|
|
700
|
+
* Join as a child host (game launched from arcade)
|
|
701
|
+
*/
|
|
702
|
+
readonly JOIN_AS_CHILD: "host:joinAsChild";
|
|
703
|
+
};
|
|
704
|
+
/**
|
|
705
|
+
* Controller events
|
|
706
|
+
*/
|
|
707
|
+
declare const ControllerEvents: {
|
|
708
|
+
/**
|
|
709
|
+
* Controller joins a room
|
|
710
|
+
*/
|
|
711
|
+
readonly JOIN: "controller:join";
|
|
712
|
+
/**
|
|
713
|
+
* Controller sends input
|
|
714
|
+
*/
|
|
715
|
+
readonly INPUT: "controller:input";
|
|
716
|
+
/**
|
|
717
|
+
* Controller leaves a room
|
|
718
|
+
*/
|
|
719
|
+
readonly LEAVE: "controller:leave";
|
|
720
|
+
/**
|
|
721
|
+
* Controller sends game state update
|
|
722
|
+
*/
|
|
723
|
+
readonly STATE: "controller:state";
|
|
724
|
+
/**
|
|
725
|
+
* Controller sends system command
|
|
726
|
+
*/
|
|
727
|
+
readonly SYSTEM: "controller:system";
|
|
728
|
+
};
|
|
729
|
+
/**
|
|
730
|
+
* System events (Arcade-specific)
|
|
731
|
+
*/
|
|
732
|
+
declare const SystemEvents: {
|
|
733
|
+
/**
|
|
734
|
+
* Arcade launches a game
|
|
735
|
+
*/
|
|
736
|
+
readonly LAUNCH_GAME: "system:launchGame";
|
|
737
|
+
/**
|
|
738
|
+
* Arcade closes a game
|
|
739
|
+
*/
|
|
740
|
+
readonly CLOSE_GAME: "system:closeGame";
|
|
741
|
+
};
|
|
742
|
+
/**
|
|
743
|
+
* Server events (server -> client)
|
|
744
|
+
*/
|
|
745
|
+
declare const ServerEvents: {
|
|
746
|
+
/**
|
|
747
|
+
* Welcome message with room/controller info
|
|
748
|
+
*/
|
|
749
|
+
readonly WELCOME: "server:welcome";
|
|
750
|
+
/**
|
|
751
|
+
* Room is ready
|
|
752
|
+
*/
|
|
753
|
+
readonly ROOM_READY: "server:roomReady";
|
|
754
|
+
/**
|
|
755
|
+
* Controller joined notification
|
|
756
|
+
*/
|
|
757
|
+
readonly CONTROLLER_JOINED: "server:controllerJoined";
|
|
758
|
+
/**
|
|
759
|
+
* Controller left notification
|
|
760
|
+
*/
|
|
761
|
+
readonly CONTROLLER_LEFT: "server:controllerLeft";
|
|
762
|
+
/**
|
|
763
|
+
* Host left notification
|
|
764
|
+
*/
|
|
765
|
+
readonly HOST_LEFT: "server:hostLeft";
|
|
766
|
+
/**
|
|
767
|
+
* Server error
|
|
768
|
+
*/
|
|
769
|
+
readonly ERROR: "server:error";
|
|
770
|
+
/**
|
|
771
|
+
* Play sound on client
|
|
772
|
+
*/
|
|
773
|
+
readonly PLAY_SOUND: "server:playSound";
|
|
774
|
+
};
|
|
775
|
+
/**
|
|
776
|
+
* Client events (server -> controller shell)
|
|
777
|
+
*/
|
|
778
|
+
declare const ClientEvents: {
|
|
779
|
+
/**
|
|
780
|
+
* Load a game UI in the controller iframe
|
|
781
|
+
*/
|
|
782
|
+
readonly LOAD_UI: "client:loadUi";
|
|
783
|
+
/**
|
|
784
|
+
* Unload the game UI from the controller iframe
|
|
785
|
+
*/
|
|
786
|
+
readonly UNLOAD_UI: "client:unloadUi";
|
|
787
|
+
/**
|
|
788
|
+
* Update game state display
|
|
789
|
+
*/
|
|
790
|
+
readonly STATE: "client:state";
|
|
791
|
+
};
|
|
792
|
+
/**
|
|
793
|
+
* Child events (server -> child host)
|
|
794
|
+
*/
|
|
795
|
+
declare const ChildEvents: {
|
|
796
|
+
/**
|
|
797
|
+
* Server requests child to close
|
|
798
|
+
*/
|
|
799
|
+
readonly CLOSE: "child:close";
|
|
800
|
+
};
|
|
801
|
+
/**
|
|
802
|
+
* All events combined for easy reference
|
|
803
|
+
*/
|
|
804
|
+
declare const Events: {
|
|
805
|
+
readonly Host: {
|
|
806
|
+
/**
|
|
807
|
+
* Register as a master host (standalone mode)
|
|
808
|
+
*/
|
|
809
|
+
readonly REGISTER: "host:register";
|
|
810
|
+
/**
|
|
811
|
+
* Register as the system/arcade master host
|
|
812
|
+
*/
|
|
813
|
+
readonly REGISTER_SYSTEM: "host:registerSystem";
|
|
814
|
+
/**
|
|
815
|
+
* Join as a child host (game launched from arcade)
|
|
816
|
+
*/
|
|
817
|
+
readonly JOIN_AS_CHILD: "host:joinAsChild";
|
|
818
|
+
};
|
|
819
|
+
readonly Controller: {
|
|
820
|
+
/**
|
|
821
|
+
* Controller joins a room
|
|
822
|
+
*/
|
|
823
|
+
readonly JOIN: "controller:join";
|
|
824
|
+
/**
|
|
825
|
+
* Controller sends input
|
|
826
|
+
*/
|
|
827
|
+
readonly INPUT: "controller:input";
|
|
828
|
+
/**
|
|
829
|
+
* Controller leaves a room
|
|
830
|
+
*/
|
|
831
|
+
readonly LEAVE: "controller:leave";
|
|
832
|
+
/**
|
|
833
|
+
* Controller sends game state update
|
|
834
|
+
*/
|
|
835
|
+
readonly STATE: "controller:state";
|
|
836
|
+
/**
|
|
837
|
+
* Controller sends system command
|
|
838
|
+
*/
|
|
839
|
+
readonly SYSTEM: "controller:system";
|
|
840
|
+
};
|
|
841
|
+
readonly System: {
|
|
842
|
+
/**
|
|
843
|
+
* Arcade launches a game
|
|
844
|
+
*/
|
|
845
|
+
readonly LAUNCH_GAME: "system:launchGame";
|
|
846
|
+
/**
|
|
847
|
+
* Arcade closes a game
|
|
848
|
+
*/
|
|
849
|
+
readonly CLOSE_GAME: "system:closeGame";
|
|
850
|
+
};
|
|
851
|
+
readonly Server: {
|
|
852
|
+
/**
|
|
853
|
+
* Welcome message with room/controller info
|
|
854
|
+
*/
|
|
855
|
+
readonly WELCOME: "server:welcome";
|
|
856
|
+
/**
|
|
857
|
+
* Room is ready
|
|
858
|
+
*/
|
|
859
|
+
readonly ROOM_READY: "server:roomReady";
|
|
860
|
+
/**
|
|
861
|
+
* Controller joined notification
|
|
862
|
+
*/
|
|
863
|
+
readonly CONTROLLER_JOINED: "server:controllerJoined";
|
|
864
|
+
/**
|
|
865
|
+
* Controller left notification
|
|
866
|
+
*/
|
|
867
|
+
readonly CONTROLLER_LEFT: "server:controllerLeft";
|
|
868
|
+
/**
|
|
869
|
+
* Host left notification
|
|
870
|
+
*/
|
|
871
|
+
readonly HOST_LEFT: "server:hostLeft";
|
|
872
|
+
/**
|
|
873
|
+
* Server error
|
|
874
|
+
*/
|
|
875
|
+
readonly ERROR: "server:error";
|
|
876
|
+
/**
|
|
877
|
+
* Play sound on client
|
|
878
|
+
*/
|
|
879
|
+
readonly PLAY_SOUND: "server:playSound";
|
|
880
|
+
};
|
|
881
|
+
readonly Client: {
|
|
882
|
+
/**
|
|
883
|
+
* Load a game UI in the controller iframe
|
|
884
|
+
*/
|
|
885
|
+
readonly LOAD_UI: "client:loadUi";
|
|
886
|
+
/**
|
|
887
|
+
* Unload the game UI from the controller iframe
|
|
888
|
+
*/
|
|
889
|
+
readonly UNLOAD_UI: "client:unloadUi";
|
|
890
|
+
/**
|
|
891
|
+
* Update game state display
|
|
892
|
+
*/
|
|
893
|
+
readonly STATE: "client:state";
|
|
894
|
+
};
|
|
895
|
+
readonly Child: {
|
|
896
|
+
/**
|
|
897
|
+
* Server requests child to close
|
|
898
|
+
*/
|
|
899
|
+
readonly CLOSE: "child:close";
|
|
900
|
+
};
|
|
901
|
+
};
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* Options for configuring the controller connection.
|
|
905
|
+
*
|
|
906
|
+
* @example Basic usage (room from URL)
|
|
907
|
+
* ```tsx
|
|
908
|
+
* // URL: https://yourgame.com/joypad?room=ABCD
|
|
909
|
+
* const controller = useAirJamController();
|
|
910
|
+
* // Automatically joins room ABCD
|
|
911
|
+
* ```
|
|
912
|
+
*
|
|
913
|
+
* @example With nickname
|
|
914
|
+
* ```tsx
|
|
915
|
+
* const controller = useAirJamController({
|
|
916
|
+
* nickname: playerName,
|
|
917
|
+
* onState: (state) => {
|
|
918
|
+
* if (state.gameState === "playing") startGame();
|
|
919
|
+
* },
|
|
920
|
+
* });
|
|
921
|
+
* ```
|
|
922
|
+
*/
|
|
923
|
+
interface AirJamControllerOptions {
|
|
924
|
+
/**
|
|
925
|
+
* Room ID (4-character code) to join.
|
|
926
|
+
* Can also be provided via URL query parameter: `?room=XXXX`
|
|
927
|
+
* The URL parameter takes precedence if both are provided.
|
|
928
|
+
*/
|
|
929
|
+
roomId?: string;
|
|
930
|
+
/**
|
|
931
|
+
* Player nickname to display in the game.
|
|
932
|
+
* If not provided, the server assigns a random label.
|
|
933
|
+
*/
|
|
934
|
+
nickname?: string;
|
|
935
|
+
/**
|
|
936
|
+
* Controller ID for this instance.
|
|
937
|
+
* Auto-generated if not provided. Persists across reconnects.
|
|
938
|
+
*/
|
|
939
|
+
controllerId?: string;
|
|
940
|
+
/**
|
|
941
|
+
* Force connection even in non-standard modes (e.g., iframe).
|
|
942
|
+
* @default false
|
|
943
|
+
*/
|
|
944
|
+
forceConnect?: boolean;
|
|
945
|
+
/**
|
|
946
|
+
* Called when the host sends a state update (game state, messages, etc.)
|
|
947
|
+
*/
|
|
948
|
+
onState?: (state: ControllerStatePayload) => void;
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Return type of useAirJamController hook.
|
|
952
|
+
*
|
|
953
|
+
* Provides state and functions for a mobile/web controller interface.
|
|
954
|
+
*/
|
|
955
|
+
interface AirJamControllerApi {
|
|
956
|
+
/** Room code this controller is connected to (or attempting to join) */
|
|
957
|
+
roomId: RoomCode | null;
|
|
958
|
+
/** This controller's unique ID */
|
|
959
|
+
controllerId: string | null;
|
|
960
|
+
/** Current connection status */
|
|
961
|
+
connectionStatus: ConnectionStatus;
|
|
962
|
+
/** Last error message, if any */
|
|
963
|
+
lastError?: string;
|
|
964
|
+
/** Current game state (paused or playing) */
|
|
965
|
+
gameState: GameState;
|
|
966
|
+
/** Optional message from the host (e.g., "Get Ready!") */
|
|
967
|
+
stateMessage?: string;
|
|
968
|
+
/**
|
|
969
|
+
* Send input to the host.
|
|
970
|
+
*
|
|
971
|
+
* @example Sending joystick and button input
|
|
972
|
+
* ```ts
|
|
973
|
+
* controller.sendInput({
|
|
974
|
+
* vector: { x: joystickX, y: joystickY },
|
|
975
|
+
* action: isFirePressed,
|
|
976
|
+
* ability: isAbilityPressed,
|
|
977
|
+
* timestamp: Date.now(),
|
|
978
|
+
* });
|
|
979
|
+
* ```
|
|
980
|
+
*/
|
|
981
|
+
sendInput: (input: ControllerInputPayload) => boolean;
|
|
982
|
+
/**
|
|
983
|
+
* Send a system command to the host.
|
|
984
|
+
*
|
|
985
|
+
* @example Toggle pause
|
|
986
|
+
* ```ts
|
|
987
|
+
* controller.sendSystemCommand("toggle_pause");
|
|
988
|
+
* ```
|
|
989
|
+
*/
|
|
990
|
+
sendSystemCommand: (command: "exit" | "ready" | "toggle_pause") => void;
|
|
991
|
+
/**
|
|
992
|
+
* Update the player nickname.
|
|
993
|
+
* Changes take effect on next connection.
|
|
994
|
+
*/
|
|
995
|
+
setNickname: (value: string) => void;
|
|
996
|
+
/** Force reconnection to the room */
|
|
997
|
+
reconnect: () => void;
|
|
998
|
+
/** List of all connected players in the room */
|
|
999
|
+
players: PlayerProfile[];
|
|
1000
|
+
/** Raw Socket.IO socket instance (null if not connected) */
|
|
1001
|
+
socket: AirJamSocket | null;
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Hook for building mobile/web controllers that connect to AirJam hosts.
|
|
1005
|
+
*
|
|
1006
|
+
* This hook handles all the complexity of connecting a mobile controller to a game:
|
|
1007
|
+
* - Automatic room joining from URL parameters
|
|
1008
|
+
* - Input sending with validation
|
|
1009
|
+
* - Haptic feedback on signal receipt (vibration)
|
|
1010
|
+
* - Game state synchronization
|
|
1011
|
+
* - Auto-reconnection on disconnect
|
|
1012
|
+
*
|
|
1013
|
+
* **Typical usage flow:**
|
|
1014
|
+
* 1. Player scans QR code on game screen
|
|
1015
|
+
* 2. Opens controller URL with ?room=XXXX parameter
|
|
1016
|
+
* 3. Controller auto-connects to the game session
|
|
1017
|
+
* 4. Player uses joystick/buttons to send input
|
|
1018
|
+
* 5. Receives haptic feedback on game events
|
|
1019
|
+
*
|
|
1020
|
+
* @param options - Configuration options
|
|
1021
|
+
* @returns Controller API with state and functions
|
|
1022
|
+
*
|
|
1023
|
+
* @example Basic controller setup
|
|
1024
|
+
* ```tsx
|
|
1025
|
+
* const ControllerView = () => {
|
|
1026
|
+
* const controller = useAirJamController();
|
|
1027
|
+
*
|
|
1028
|
+
* const handleJoystickMove = (x: number, y: number) => {
|
|
1029
|
+
* controller.sendInput({
|
|
1030
|
+
* vector: { x, y },
|
|
1031
|
+
* action: false,
|
|
1032
|
+
* timestamp: Date.now(),
|
|
1033
|
+
* });
|
|
1034
|
+
* };
|
|
1035
|
+
*
|
|
1036
|
+
* const handleFirePress = () => {
|
|
1037
|
+
* controller.sendInput({
|
|
1038
|
+
* vector: { x: 0, y: 0 },
|
|
1039
|
+
* action: true,
|
|
1040
|
+
* timestamp: Date.now(),
|
|
1041
|
+
* });
|
|
1042
|
+
* };
|
|
1043
|
+
*
|
|
1044
|
+
* if (controller.connectionStatus !== "connected") {
|
|
1045
|
+
* return <div>Connecting to room {controller.roomId}...</div>;
|
|
1046
|
+
* }
|
|
1047
|
+
*
|
|
1048
|
+
* return (
|
|
1049
|
+
* <div>
|
|
1050
|
+
* <Joystick onMove={handleJoystickMove} />
|
|
1051
|
+
* <FireButton onPress={handleFirePress} />
|
|
1052
|
+
* <p>Game: {controller.gameState}</p>
|
|
1053
|
+
* </div>
|
|
1054
|
+
* );
|
|
1055
|
+
* };
|
|
1056
|
+
* ```
|
|
1057
|
+
*
|
|
1058
|
+
* @example With state callback
|
|
1059
|
+
* ```tsx
|
|
1060
|
+
* const controller = useAirJamController({
|
|
1061
|
+
* onState: (state) => {
|
|
1062
|
+
* if (state.message) {
|
|
1063
|
+
* showNotification(state.message);
|
|
1064
|
+
* }
|
|
1065
|
+
* },
|
|
1066
|
+
* });
|
|
1067
|
+
* ```
|
|
1068
|
+
*/
|
|
1069
|
+
declare const useAirJamController: (options?: AirJamControllerOptions) => AirJamControllerApi;
|
|
1070
|
+
|
|
1071
|
+
/**
|
|
1072
|
+
* Options for configuring the host connection.
|
|
1073
|
+
*
|
|
1074
|
+
* @example Basic usage with callbacks
|
|
1075
|
+
* ```tsx
|
|
1076
|
+
* const host = useAirJamHost({
|
|
1077
|
+
* onPlayerJoin: (player) => {
|
|
1078
|
+
* console.log(`${player.label} joined!`);
|
|
1079
|
+
* spawnPlayerShip(player.id);
|
|
1080
|
+
* },
|
|
1081
|
+
* onPlayerLeave: (controllerId) => {
|
|
1082
|
+
* console.log(`Player ${controllerId} left`);
|
|
1083
|
+
* removePlayerShip(controllerId);
|
|
1084
|
+
* },
|
|
1085
|
+
* });
|
|
1086
|
+
* ```
|
|
1087
|
+
*
|
|
1088
|
+
* @example With custom room ID
|
|
1089
|
+
* ```tsx
|
|
1090
|
+
* const host = useAirJamHost({
|
|
1091
|
+
* roomId: "GAME1", // Custom 4-character room code
|
|
1092
|
+
* maxPlayers: 4, // Override provider's maxPlayers
|
|
1093
|
+
* });
|
|
1094
|
+
* ```
|
|
1095
|
+
*/
|
|
1096
|
+
interface AirJamHostOptions {
|
|
1097
|
+
/**
|
|
1098
|
+
* Room ID (4-character code) to use for this session.
|
|
1099
|
+
* If not provided, a random room code will be generated.
|
|
1100
|
+
* Can also be set via URL query parameter: `?room=XXXX`
|
|
1101
|
+
*/
|
|
1102
|
+
roomId?: string;
|
|
1103
|
+
/**
|
|
1104
|
+
* Called when a player successfully joins the room.
|
|
1105
|
+
* Use this to spawn player entities, update UI, etc.
|
|
1106
|
+
*/
|
|
1107
|
+
onPlayerJoin?: (player: PlayerProfile) => void;
|
|
1108
|
+
/**
|
|
1109
|
+
* Called when a player leaves the room (disconnects or exits).
|
|
1110
|
+
* Use this to remove player entities, handle cleanup.
|
|
1111
|
+
*/
|
|
1112
|
+
onPlayerLeave?: (controllerId: string) => void;
|
|
1113
|
+
/**
|
|
1114
|
+
* Called when the child window closes in Arcade mode.
|
|
1115
|
+
* Only relevant when running as part of the AirJam Platform.
|
|
1116
|
+
*/
|
|
1117
|
+
onChildClose?: () => void;
|
|
1118
|
+
/**
|
|
1119
|
+
* Force connection even in non-standard modes.
|
|
1120
|
+
* @default true
|
|
1121
|
+
*/
|
|
1122
|
+
forceConnect?: boolean;
|
|
1123
|
+
/**
|
|
1124
|
+
* Override the API key from provider.
|
|
1125
|
+
* Useful for per-game API keys in multi-game setups.
|
|
1126
|
+
*/
|
|
1127
|
+
apiKey?: string;
|
|
1128
|
+
/**
|
|
1129
|
+
* Override the maximum players from provider.
|
|
1130
|
+
* @default 8
|
|
1131
|
+
*/
|
|
1132
|
+
maxPlayers?: number;
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Return type of useAirJamHost hook.
|
|
1136
|
+
*
|
|
1137
|
+
* Provides all the state and functions needed to run a multiplayer game session.
|
|
1138
|
+
*
|
|
1139
|
+
* @template TSchema - Zod schema type for input (inferred from provider)
|
|
1140
|
+
*/
|
|
1141
|
+
interface AirJamHostApi<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
1142
|
+
/** The room code for this session (e.g., "ABCD") */
|
|
1143
|
+
roomId: RoomCode;
|
|
1144
|
+
/** Full URL for controllers to join (display as QR code) */
|
|
1145
|
+
joinUrl: string;
|
|
1146
|
+
/** Current connection status to the server */
|
|
1147
|
+
connectionStatus: ConnectionStatus;
|
|
1148
|
+
/** List of currently connected players */
|
|
1149
|
+
players: PlayerProfile[];
|
|
1150
|
+
/** Last error message, if any */
|
|
1151
|
+
lastError?: string;
|
|
1152
|
+
/** Current run mode (standalone, arcade, platform) */
|
|
1153
|
+
mode: RunMode;
|
|
1154
|
+
/** Current game state (paused or playing) */
|
|
1155
|
+
gameState: GameState;
|
|
1156
|
+
/** Toggle between paused and playing states */
|
|
1157
|
+
toggleGameState: () => void;
|
|
1158
|
+
/**
|
|
1159
|
+
* Send state update to all connected controllers.
|
|
1160
|
+
* Use for syncing game state, messages, etc.
|
|
1161
|
+
*/
|
|
1162
|
+
sendState: (state: ControllerStatePayload) => boolean;
|
|
1163
|
+
/**
|
|
1164
|
+
* Send a signal (haptic feedback or toast) to controllers.
|
|
1165
|
+
*
|
|
1166
|
+
* @example Send haptic to specific player
|
|
1167
|
+
* ```ts
|
|
1168
|
+
* host.sendSignal("HAPTIC", { pattern: "heavy" }, playerId);
|
|
1169
|
+
* ```
|
|
1170
|
+
*
|
|
1171
|
+
* @example Send toast to all players
|
|
1172
|
+
* ```ts
|
|
1173
|
+
* host.sendSignal("TOAST", {
|
|
1174
|
+
* title: "Round Start!",
|
|
1175
|
+
* message: "Game begins in 3 seconds",
|
|
1176
|
+
* });
|
|
1177
|
+
* ```
|
|
1178
|
+
*/
|
|
1179
|
+
sendSignal: {
|
|
1180
|
+
(type: "HAPTIC", payload: HapticSignalPayload, targetId?: string): void;
|
|
1181
|
+
(type: "TOAST", payload: ToastSignalPayload, targetId?: string): void;
|
|
1182
|
+
};
|
|
1183
|
+
/** Reconnect to the server */
|
|
1184
|
+
reconnect: () => void;
|
|
1185
|
+
/** Raw Socket.IO socket instance for advanced usage */
|
|
1186
|
+
socket: AirJamSocket;
|
|
1187
|
+
/** Whether running in child/arcade mode */
|
|
1188
|
+
isChildMode: boolean;
|
|
1189
|
+
/**
|
|
1190
|
+
* Get the latest input from a specific controller.
|
|
1191
|
+
*
|
|
1192
|
+
* Returns validated, typed input based on the schema provided to AirJamProvider.
|
|
1193
|
+
* If latching is configured, automatically handles latch logic (button presses
|
|
1194
|
+
* and stick flicks are "held" until consumed).
|
|
1195
|
+
*
|
|
1196
|
+
* @example In a game loop
|
|
1197
|
+
* ```ts
|
|
1198
|
+
* useFrame(() => {
|
|
1199
|
+
* players.forEach((player) => {
|
|
1200
|
+
* const input = host.getInput(player.id);
|
|
1201
|
+
* if (input?.action) {
|
|
1202
|
+
* fireWeapon(player.id);
|
|
1203
|
+
* }
|
|
1204
|
+
* movePlayer(player.id, input?.vector ?? { x: 0, y: 0 });
|
|
1205
|
+
* });
|
|
1206
|
+
* });
|
|
1207
|
+
* ```
|
|
1208
|
+
*/
|
|
1209
|
+
getInput: (controllerId: string) => z.infer<TSchema> | undefined;
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Primary hook for host/game functionality.
|
|
1213
|
+
*
|
|
1214
|
+
* Connects to the AirJam server as a host and provides everything needed
|
|
1215
|
+
* to manage a multiplayer game session. Must be used within an AirJamProvider.
|
|
1216
|
+
*
|
|
1217
|
+
* **Features:**
|
|
1218
|
+
* - Automatic room creation and management
|
|
1219
|
+
* - Real-time player join/leave events
|
|
1220
|
+
* - Typed input with validation and latching
|
|
1221
|
+
* - Haptic feedback and toast notifications
|
|
1222
|
+
* - Game state synchronization
|
|
1223
|
+
*
|
|
1224
|
+
* @template TSchema - Zod schema for input validation (from provider)
|
|
1225
|
+
* @param options - Configuration options for the host
|
|
1226
|
+
* @returns API object with state and functions
|
|
1227
|
+
*
|
|
1228
|
+
* @example Basic usage
|
|
1229
|
+
* ```tsx
|
|
1230
|
+
* const HostView = () => {
|
|
1231
|
+
* const host = useAirJamHost({
|
|
1232
|
+
* onPlayerJoin: (player) => console.log(`${player.label} joined`),
|
|
1233
|
+
* onPlayerLeave: (id) => console.log(`${id} left`),
|
|
1234
|
+
* });
|
|
1235
|
+
*
|
|
1236
|
+
* return (
|
|
1237
|
+
* <div>
|
|
1238
|
+
* <h1>Room: {host.roomId}</h1>
|
|
1239
|
+
* <QRCode value={host.joinUrl} />
|
|
1240
|
+
* <p>Players: {host.players.length}</p>
|
|
1241
|
+
* <button onClick={host.toggleGameState}>
|
|
1242
|
+
* {host.gameState === "playing" ? "Pause" : "Play"}
|
|
1243
|
+
* </button>
|
|
1244
|
+
* </div>
|
|
1245
|
+
* );
|
|
1246
|
+
* };
|
|
1247
|
+
* ```
|
|
1248
|
+
*
|
|
1249
|
+
* @example Reading input in a game loop
|
|
1250
|
+
* ```tsx
|
|
1251
|
+
* const GameScene = () => {
|
|
1252
|
+
* const host = useAirJamHost();
|
|
1253
|
+
*
|
|
1254
|
+
* useFrame(() => {
|
|
1255
|
+
* host.players.forEach((player) => {
|
|
1256
|
+
* const input = host.getInput(player.id);
|
|
1257
|
+
* if (!input) return;
|
|
1258
|
+
*
|
|
1259
|
+
* // Move player based on joystick
|
|
1260
|
+
* movePlayer(player.id, input.vector);
|
|
1261
|
+
*
|
|
1262
|
+
* // Handle button press (auto-latched)
|
|
1263
|
+
* if (input.action) {
|
|
1264
|
+
* playerShoot(player.id);
|
|
1265
|
+
* }
|
|
1266
|
+
* });
|
|
1267
|
+
* });
|
|
1268
|
+
*
|
|
1269
|
+
* return <GameCanvas />;
|
|
1270
|
+
* };
|
|
1271
|
+
* ```
|
|
1272
|
+
*
|
|
1273
|
+
* @example Sending haptic feedback
|
|
1274
|
+
* ```tsx
|
|
1275
|
+
* const handleHit = (playerId: string) => {
|
|
1276
|
+
* host.sendSignal("HAPTIC", { pattern: "heavy" }, playerId);
|
|
1277
|
+
* };
|
|
1278
|
+
* ```
|
|
1279
|
+
*/
|
|
1280
|
+
declare const useAirJamHost: <TSchema extends z.ZodSchema = z.ZodSchema>(options?: AirJamHostOptions) => AirJamHostApi<TSchema>;
|
|
1281
|
+
|
|
1282
|
+
interface AirJamShellOptions {
|
|
1283
|
+
/** Room ID to join */
|
|
1284
|
+
roomId?: string;
|
|
1285
|
+
/** Player nickname */
|
|
1286
|
+
nickname?: string;
|
|
1287
|
+
/** Controller ID (auto-generated if not provided) */
|
|
1288
|
+
controllerId?: string;
|
|
1289
|
+
}
|
|
1290
|
+
interface AirJamShellApi {
|
|
1291
|
+
roomId: RoomCode | null;
|
|
1292
|
+
controllerId: string | null;
|
|
1293
|
+
connectionStatus: ConnectionStatus;
|
|
1294
|
+
lastError?: string;
|
|
1295
|
+
activeUrl: string | null;
|
|
1296
|
+
players: PlayerProfile[];
|
|
1297
|
+
gameState: GameState;
|
|
1298
|
+
sendInput: (input: ControllerInputPayload) => boolean;
|
|
1299
|
+
sendSystemCommand: (command: "exit" | "ready" | "toggle_pause") => void;
|
|
1300
|
+
}
|
|
1301
|
+
declare const useAirJamShell: (options?: AirJamShellOptions) => AirJamShellApi;
|
|
1302
|
+
|
|
1303
|
+
/**
|
|
1304
|
+
* Lightweight hook for accessing controller input without store subscriptions.
|
|
1305
|
+
*
|
|
1306
|
+
* Returns a stable `getInput` function that retrieves the latest input for a
|
|
1307
|
+
* given controller ID. Unlike `useAirJamHost()`, this hook does NOT subscribe
|
|
1308
|
+
* to the store, so it won't cause re-renders when connection state changes.
|
|
1309
|
+
*
|
|
1310
|
+
* **Requirements:**
|
|
1311
|
+
* - Must be used within an `AirJamProvider`
|
|
1312
|
+
* - Input configuration must be provided to the provider
|
|
1313
|
+
*
|
|
1314
|
+
* @template TSchema - Zod schema type for input (from provider)
|
|
1315
|
+
* @returns A stable function that retrieves input for a controller ID
|
|
1316
|
+
*
|
|
1317
|
+
* @example In a game loop (React Three Fiber)
|
|
1318
|
+
* ```tsx
|
|
1319
|
+
* const Ship = ({ playerId }: { playerId: string }) => {
|
|
1320
|
+
* const getInput = useGetInput<typeof gameInputSchema>();
|
|
1321
|
+
* const meshRef = useRef<THREE.Mesh>(null);
|
|
1322
|
+
*
|
|
1323
|
+
* useFrame(() => {
|
|
1324
|
+
* const input = getInput(playerId);
|
|
1325
|
+
* if (!input || !meshRef.current) return;
|
|
1326
|
+
*
|
|
1327
|
+
* // Move ship based on joystick
|
|
1328
|
+
* meshRef.current.position.x += input.vector.x * SPEED;
|
|
1329
|
+
* meshRef.current.position.y += input.vector.y * SPEED;
|
|
1330
|
+
*
|
|
1331
|
+
* // Fire if action button pressed (auto-latched)
|
|
1332
|
+
* if (input.action) {
|
|
1333
|
+
* fireLaser();
|
|
1334
|
+
* }
|
|
1335
|
+
* });
|
|
1336
|
+
*
|
|
1337
|
+
* return <mesh ref={meshRef}>...</mesh>;
|
|
1338
|
+
* };
|
|
1339
|
+
* ```
|
|
1340
|
+
*
|
|
1341
|
+
* @example In a regular React component
|
|
1342
|
+
* ```tsx
|
|
1343
|
+
* const InputDebugger = ({ playerId }: { playerId: string }) => {
|
|
1344
|
+
* const getInput = useGetInput();
|
|
1345
|
+
* const [display, setDisplay] = useState<string>("");
|
|
1346
|
+
*
|
|
1347
|
+
* useEffect(() => {
|
|
1348
|
+
* const interval = setInterval(() => {
|
|
1349
|
+
* const input = getInput(playerId);
|
|
1350
|
+
* setDisplay(JSON.stringify(input, null, 2));
|
|
1351
|
+
* }, 100);
|
|
1352
|
+
* return () => clearInterval(interval);
|
|
1353
|
+
* }, [playerId, getInput]);
|
|
1354
|
+
*
|
|
1355
|
+
* return <pre>{display}</pre>;
|
|
1356
|
+
* };
|
|
1357
|
+
* ```
|
|
1358
|
+
*/
|
|
1359
|
+
declare const useGetInput: <TSchema extends z.ZodSchema = z.ZodSchema>() => ((controllerId: string) => z.infer<TSchema> | undefined);
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
* Function signature for sending signals to controllers.
|
|
1363
|
+
*
|
|
1364
|
+
* Overloaded to provide proper typing for each signal type.
|
|
1365
|
+
*/
|
|
1366
|
+
interface SendSignalFn {
|
|
1367
|
+
/**
|
|
1368
|
+
* Send haptic (vibration) feedback to a controller.
|
|
1369
|
+
*
|
|
1370
|
+
* @param type - Must be "HAPTIC"
|
|
1371
|
+
* @param payload - Haptic configuration with pattern
|
|
1372
|
+
* @param targetId - Controller ID (omit to send to all)
|
|
1373
|
+
*
|
|
1374
|
+
* @example Send heavy vibration to specific player
|
|
1375
|
+
* ```ts
|
|
1376
|
+
* sendSignal("HAPTIC", { pattern: "heavy" }, playerId);
|
|
1377
|
+
* ```
|
|
1378
|
+
*
|
|
1379
|
+
* @example Send custom vibration pattern to all
|
|
1380
|
+
* ```ts
|
|
1381
|
+
* sendSignal("HAPTIC", {
|
|
1382
|
+
* pattern: "custom",
|
|
1383
|
+
* sequence: [50, 100, 50, 100, 200],
|
|
1384
|
+
* });
|
|
1385
|
+
* ```
|
|
1386
|
+
*/
|
|
1387
|
+
(type: "HAPTIC", payload: HapticSignalPayload, targetId?: string): void;
|
|
1388
|
+
/**
|
|
1389
|
+
* Send a toast notification to a controller.
|
|
1390
|
+
*
|
|
1391
|
+
* @param type - Must be "TOAST"
|
|
1392
|
+
* @param payload - Toast content with title and message
|
|
1393
|
+
* @param targetId - Controller ID (omit to send to all)
|
|
1394
|
+
*
|
|
1395
|
+
* @example Send achievement notification
|
|
1396
|
+
* ```ts
|
|
1397
|
+
* sendSignal("TOAST", {
|
|
1398
|
+
* title: "Achievement Unlocked!",
|
|
1399
|
+
* message: "First blood",
|
|
1400
|
+
* variant: "success",
|
|
1401
|
+
* }, playerId);
|
|
1402
|
+
* ```
|
|
1403
|
+
*/
|
|
1404
|
+
(type: "TOAST", payload: ToastSignalPayload, targetId?: string): void;
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Lightweight hook for sending signals without store subscriptions.
|
|
1408
|
+
*
|
|
1409
|
+
* Returns a stable `sendSignal` function that can send haptic feedback
|
|
1410
|
+
* or toast notifications to controllers. Unlike `useAirJamHost()`, this
|
|
1411
|
+
* hook does NOT subscribe to the store, preventing re-renders.
|
|
1412
|
+
*
|
|
1413
|
+
* **Haptic patterns:**
|
|
1414
|
+
* - `light` - Quick tap (10ms)
|
|
1415
|
+
* - `medium` - Standard vibration (30ms)
|
|
1416
|
+
* - `heavy` - Strong pulse pattern
|
|
1417
|
+
* - `success` - Short double tap
|
|
1418
|
+
* - `failure` - Rapid pulses
|
|
1419
|
+
* - `custom` - Custom sequence array
|
|
1420
|
+
*
|
|
1421
|
+
* @returns A stable sendSignal function
|
|
1422
|
+
*
|
|
1423
|
+
* @example In a collision handler
|
|
1424
|
+
* ```tsx
|
|
1425
|
+
* const Laser = ({ ownerId }: { ownerId: string }) => {
|
|
1426
|
+
* const sendSignal = useSendSignal();
|
|
1427
|
+
*
|
|
1428
|
+
* const handleHit = (targetId: string) => {
|
|
1429
|
+
* // Vibrate the player who got hit
|
|
1430
|
+
* sendSignal("HAPTIC", { pattern: "heavy" }, targetId);
|
|
1431
|
+
*
|
|
1432
|
+
* // Light vibration for the shooter
|
|
1433
|
+
* sendSignal("HAPTIC", { pattern: "light" }, ownerId);
|
|
1434
|
+
* };
|
|
1435
|
+
*
|
|
1436
|
+
* // ... collision detection logic
|
|
1437
|
+
* };
|
|
1438
|
+
* ```
|
|
1439
|
+
*
|
|
1440
|
+
* @example For collectible pickups
|
|
1441
|
+
* ```tsx
|
|
1442
|
+
* const Collectible = () => {
|
|
1443
|
+
* const sendSignal = useSendSignal();
|
|
1444
|
+
*
|
|
1445
|
+
* const handleCollect = (playerId: string) => {
|
|
1446
|
+
* sendSignal("HAPTIC", { pattern: "success" }, playerId);
|
|
1447
|
+
* sendSignal("TOAST", {
|
|
1448
|
+
* title: "+10 Points",
|
|
1449
|
+
* variant: "default",
|
|
1450
|
+
* }, playerId);
|
|
1451
|
+
* };
|
|
1452
|
+
* };
|
|
1453
|
+
* ```
|
|
1454
|
+
*
|
|
1455
|
+
* @example Game-wide announcements
|
|
1456
|
+
* ```tsx
|
|
1457
|
+
* const GameManager = () => {
|
|
1458
|
+
* const sendSignal = useSendSignal();
|
|
1459
|
+
*
|
|
1460
|
+
* const announceRoundStart = () => {
|
|
1461
|
+
* // No targetId = broadcast to all controllers
|
|
1462
|
+
* sendSignal("TOAST", {
|
|
1463
|
+
* title: "Round Start!",
|
|
1464
|
+
* message: "Get ready to fight",
|
|
1465
|
+
* variant: "default",
|
|
1466
|
+
* });
|
|
1467
|
+
* };
|
|
1468
|
+
* };
|
|
1469
|
+
* ```
|
|
1470
|
+
*/
|
|
1471
|
+
declare const useSendSignal: () => SendSignalFn;
|
|
1472
|
+
|
|
1473
|
+
declare const generateRoomCode: () => string;
|
|
1474
|
+
declare const generateControllerId: () => string;
|
|
1475
|
+
|
|
1476
|
+
/**
|
|
1477
|
+
* Gets the local network IP address using WebRTC.
|
|
1478
|
+
* Filters out virtual network interfaces and prefers real network IPs.
|
|
1479
|
+
* Returns null if unable to determine the IP.
|
|
1480
|
+
*/
|
|
1481
|
+
declare const getLocalNetworkIp: () => Promise<string | null>;
|
|
1482
|
+
|
|
1483
|
+
/**
|
|
1484
|
+
* Centralized URL builder service for Air Jam.
|
|
1485
|
+
* Handles IP detection, URL normalization, and construction of all URL types.
|
|
1486
|
+
*/
|
|
1487
|
+
declare class UrlBuilder {
|
|
1488
|
+
private cachedLocalIp;
|
|
1489
|
+
private ipDetectionPromise;
|
|
1490
|
+
/**
|
|
1491
|
+
* Get the local network IP address with caching.
|
|
1492
|
+
* First call triggers detection, subsequent calls return cached value.
|
|
1493
|
+
*/
|
|
1494
|
+
getLocalIp(): Promise<string | null>;
|
|
1495
|
+
/**
|
|
1496
|
+
* Clear the cached IP address.
|
|
1497
|
+
* Useful for testing or when network changes are detected.
|
|
1498
|
+
*/
|
|
1499
|
+
clearCache(): void;
|
|
1500
|
+
/**
|
|
1501
|
+
* Normalize a URL for mobile access.
|
|
1502
|
+
* Replaces localhost with the local network IP.
|
|
1503
|
+
*/
|
|
1504
|
+
normalizeForMobile(url: string): Promise<string>;
|
|
1505
|
+
/**
|
|
1506
|
+
* Build a controller URL with room code.
|
|
1507
|
+
* Automatically uses local IP when running on localhost.
|
|
1508
|
+
*/
|
|
1509
|
+
buildControllerUrl(roomId: RoomCode, options?: {
|
|
1510
|
+
path?: string;
|
|
1511
|
+
host?: string;
|
|
1512
|
+
}): Promise<string>;
|
|
1513
|
+
/**
|
|
1514
|
+
* Resolve the server URL.
|
|
1515
|
+
* Uses explicit URL if provided, otherwise infers from window location.
|
|
1516
|
+
*/
|
|
1517
|
+
resolveServerUrl(explicit?: string): string;
|
|
1518
|
+
/**
|
|
1519
|
+
* Check if a hostname is localhost.
|
|
1520
|
+
*/
|
|
1521
|
+
private isLocalhost;
|
|
1522
|
+
/**
|
|
1523
|
+
* Normalize an origin string to ensure it has a protocol.
|
|
1524
|
+
*/
|
|
1525
|
+
private normalizeOrigin;
|
|
1526
|
+
}
|
|
1527
|
+
declare const urlBuilder: UrlBuilder;
|
|
1528
|
+
|
|
1529
|
+
export { type AirJamConfig, AirJamContext, type AirJamContextValue, type AirJamControllerApi, type AirJamControllerOptions, type AirJamHostApi, type AirJamHostOptions, AirJamOverlay, AirJamProvider, type AirJamProviderProps, type AirJamShellApi, type AirJamSocket, AudioManager, AudioProvider, Button, ChildEvents, ClientEvents, ClientToServerEvents, ConnectionRole, ConnectionStatus, ControllerEvents, ControllerInputEvent, ControllerInputPayload, ControllerShell, ControllerStatePayload, DEFAULT_CONTROLLER_PATH, DEFAULT_MAX_PLAYERS, DEFAULT_SERVER_PORT, Events, GameState, HapticSignalPayload, HostEvents, INPUT_DEBOUNCE_MS, type PlayOptions, PlayerProfile, RoomCode, RunMode, SOCKET_CONFIG, type SendSignalFn, ServerEvents, ServerToClientEvents, Slider, SocketManager, type SoundCategory, type SoundConfig, type SoundManifest, SystemEvents, TOGGLE_DEBOUNCE_MS, ToastSignalPayload, UrlBuilder, VolumeControls, buttonVariants, createAudioManager, detectSoundCategory, generateControllerId, generateRoomCode, getLocalNetworkIp, urlBuilder, useAirJamConfig, useAirJamContext, useAirJamController, useAirJamHost, useAirJamShell, useAirJamSocket, useAirJamState, useAudio, useAudioLegacy, useAudioManager, useGetInput, useSendSignal, useVolumeStore };
|