@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/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Context & Provider (primary API)
|
|
2
|
+
export * from "./context";
|
|
3
|
+
|
|
4
|
+
// Audio
|
|
5
|
+
export * from "./audio";
|
|
6
|
+
|
|
7
|
+
// Components
|
|
8
|
+
export * from "./components/air-jam-overlay";
|
|
9
|
+
export * from "./components/controller-shell";
|
|
10
|
+
export * from "./components/ui/button";
|
|
11
|
+
export * from "./components/ui/slider";
|
|
12
|
+
export * from "./components/volume-controls";
|
|
13
|
+
|
|
14
|
+
// Constants
|
|
15
|
+
export * from "./constants";
|
|
16
|
+
|
|
17
|
+
// Events
|
|
18
|
+
export * from "./events";
|
|
19
|
+
|
|
20
|
+
// Hooks
|
|
21
|
+
export * from "./hooks/use-air-jam-controller";
|
|
22
|
+
export * from "./hooks/use-air-jam-host";
|
|
23
|
+
export * from "./hooks/use-air-jam-shell";
|
|
24
|
+
export * from "./hooks/use-get-input";
|
|
25
|
+
export * from "./hooks/use-send-signal";
|
|
26
|
+
|
|
27
|
+
// Protocol types
|
|
28
|
+
export * from "./protocol";
|
|
29
|
+
|
|
30
|
+
// Utilities
|
|
31
|
+
export * from "./utils/ids";
|
|
32
|
+
export * from "./utils/network-ip";
|
|
33
|
+
export * from "./utils/url-builder";
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module InputManager
|
|
3
|
+
* @description Internal class for managing controller input with validation and latching.
|
|
4
|
+
*
|
|
5
|
+
* The InputManager is created internally by the AirJamProvider when input configuration
|
|
6
|
+
* is provided. It handles:
|
|
7
|
+
*
|
|
8
|
+
* 1. **Input Buffering** - Stores the latest input for each controller
|
|
9
|
+
* 2. **Validation** - Validates input against a Zod schema (optional)
|
|
10
|
+
* 3. **Latching** - Ensures rapid button presses and stick flicks are never missed
|
|
11
|
+
*
|
|
12
|
+
* ## What is Latching?
|
|
13
|
+
*
|
|
14
|
+
* Game loops typically run at 60fps, but network events may arrive between frames.
|
|
15
|
+
* Without latching, a quick button tap might be missed if it starts and ends between
|
|
16
|
+
* two consecutive `getInput()` calls.
|
|
17
|
+
*
|
|
18
|
+
* **Latching solves this by:**
|
|
19
|
+
* - Boolean fields: Keeping `true` values until consumed
|
|
20
|
+
* - Vector fields: Keeping non-zero values for one frame after release
|
|
21
|
+
*
|
|
22
|
+
* ## Example: Without Latching (Bug)
|
|
23
|
+
* ```
|
|
24
|
+
* Frame 1: getInput() → action: false
|
|
25
|
+
* [Network: action: true arrives, then action: false arrives]
|
|
26
|
+
* Frame 2: getInput() → action: false ← Tap missed!
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* ## Example: With Latching (Fixed)
|
|
30
|
+
* ```
|
|
31
|
+
* Frame 1: getInput() → action: false
|
|
32
|
+
* [Network: action: true arrives, then action: false arrives]
|
|
33
|
+
* Frame 2: getInput() → action: true ← Tap captured!
|
|
34
|
+
* Frame 3: getInput() → action: false ← Consumed, resets
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @internal This class is not exported publicly. Use hooks to access input.
|
|
38
|
+
*/
|
|
39
|
+
import type { z } from "zod";
|
|
40
|
+
import type { ControllerInputEvent } from "../protocol";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for input handling with optional validation and latching.
|
|
44
|
+
*
|
|
45
|
+
* Provided to `AirJamProvider` to enable typed, validated, and latched input.
|
|
46
|
+
*
|
|
47
|
+
* @template TSchema - Zod schema type for input validation
|
|
48
|
+
*
|
|
49
|
+
* @example Basic schema validation
|
|
50
|
+
* ```ts
|
|
51
|
+
* const config: InputConfig = {
|
|
52
|
+
* schema: z.object({
|
|
53
|
+
* vector: z.object({ x: z.number(), y: z.number() }),
|
|
54
|
+
* action: z.boolean(),
|
|
55
|
+
* }),
|
|
56
|
+
* };
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @example With latching for button and stick
|
|
60
|
+
* ```ts
|
|
61
|
+
* const config: InputConfig = {
|
|
62
|
+
* schema: gameInputSchema,
|
|
63
|
+
* latch: {
|
|
64
|
+
* booleanFields: ["action", "ability", "jump"],
|
|
65
|
+
* vectorFields: ["vector", "aim"],
|
|
66
|
+
* },
|
|
67
|
+
* };
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export interface InputConfig<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
71
|
+
/**
|
|
72
|
+
* Zod schema for input validation and type inference.
|
|
73
|
+
*
|
|
74
|
+
* When provided:
|
|
75
|
+
* - Incoming input is validated against the schema
|
|
76
|
+
* - Invalid input returns `undefined` (with console warning)
|
|
77
|
+
* - Return type is inferred from the schema
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* schema: z.object({
|
|
82
|
+
* vector: z.object({ x: z.number(), y: z.number() }),
|
|
83
|
+
* action: z.boolean(),
|
|
84
|
+
* ability: z.boolean(),
|
|
85
|
+
* timestamp: z.number(),
|
|
86
|
+
* })
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
schema?: TSchema;
|
|
90
|
+
/**
|
|
91
|
+
* Latching configuration to ensure rapid taps and flicks are never missed.
|
|
92
|
+
*
|
|
93
|
+
* Latching "holds" boolean true values and non-zero vectors until the next
|
|
94
|
+
* `getInput()` call, preventing missed inputs between game frames.
|
|
95
|
+
*/
|
|
96
|
+
latch?: {
|
|
97
|
+
/**
|
|
98
|
+
* Boolean field names that should be latched.
|
|
99
|
+
*
|
|
100
|
+
* When a boolean field is latched:
|
|
101
|
+
* - If it becomes `true`, it stays `true` until consumed by `getInput()`
|
|
102
|
+
* - After consumption, it resets to the actual current value
|
|
103
|
+
*
|
|
104
|
+
* Use for: buttons, triggers, action inputs
|
|
105
|
+
*
|
|
106
|
+
* @example ["action", "ability", "jump", "fire"]
|
|
107
|
+
*/
|
|
108
|
+
booleanFields?: string[];
|
|
109
|
+
/**
|
|
110
|
+
* Vector field names that should be latched.
|
|
111
|
+
*
|
|
112
|
+
* When a vector field is latched:
|
|
113
|
+
* - Non-zero vectors are kept for one frame after returning to zero
|
|
114
|
+
* - Ensures quick stick flicks register in the game loop
|
|
115
|
+
*
|
|
116
|
+
* Use for: joysticks, d-pads, directional inputs
|
|
117
|
+
*
|
|
118
|
+
* @example ["vector", "movement", "aim"]
|
|
119
|
+
*/
|
|
120
|
+
vectorFields?: string[];
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Internal state for tracking latch values per controller.
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
interface LatchState<TInput = Record<string, unknown>> {
|
|
129
|
+
/** The most recent raw input from the controller */
|
|
130
|
+
raw: TInput;
|
|
131
|
+
/** The latched input (with consumed values held) */
|
|
132
|
+
latched: TInput;
|
|
133
|
+
/** Hash of raw input to detect changes */
|
|
134
|
+
lastRawHash?: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Internal class that manages input buffering, validation, and latching.
|
|
139
|
+
*
|
|
140
|
+
* Created automatically by `AirJamProvider` when input configuration is provided.
|
|
141
|
+
* Access input via `useGetInput()` or `useAirJamHost().getInput()`.
|
|
142
|
+
*
|
|
143
|
+
* @template TSchema - Zod schema type for input validation
|
|
144
|
+
*
|
|
145
|
+
* @internal Not exported publicly. Use hooks to access input.
|
|
146
|
+
*
|
|
147
|
+
* @example How it's used internally
|
|
148
|
+
* ```ts
|
|
149
|
+
* // In AirJamProvider
|
|
150
|
+
* const inputManager = new InputManager({
|
|
151
|
+
* schema: gameInputSchema,
|
|
152
|
+
* latch: { booleanFields: ["action"] },
|
|
153
|
+
* });
|
|
154
|
+
*
|
|
155
|
+
* // In useAirJamHost (socket handler)
|
|
156
|
+
* socket.on("server:input", (payload) => {
|
|
157
|
+
* inputManager.handleInput(payload);
|
|
158
|
+
* });
|
|
159
|
+
*
|
|
160
|
+
* // In game loop (via useGetInput)
|
|
161
|
+
* const input = inputManager.getInput(playerId);
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
|
|
165
|
+
private inputBuffer = new Map<string, Record<string, unknown>>();
|
|
166
|
+
private latchState = new Map<string, LatchState>();
|
|
167
|
+
private config: InputConfig<TSchema>;
|
|
168
|
+
private roomId?: string;
|
|
169
|
+
|
|
170
|
+
constructor(config: InputConfig<TSchema> = {}, roomId?: string) {
|
|
171
|
+
this.config = config;
|
|
172
|
+
this.roomId = roomId;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Handles incoming input events from the socket.
|
|
177
|
+
* Should be called when 'server:input' event is received.
|
|
178
|
+
*/
|
|
179
|
+
handleInput(payload: ControllerInputEvent): void {
|
|
180
|
+
// Security/Sanity check: Only process inputs from our room
|
|
181
|
+
if (this.roomId && payload.roomId !== this.roomId) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Store raw input as-is (arbitrary structure)
|
|
186
|
+
this.inputBuffer.set(payload.controllerId, payload.input);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Gets input for a specific controller.
|
|
191
|
+
* Returns validated and typed input if schema is provided, otherwise raw Record.
|
|
192
|
+
* If latching is configured, applies latching logic.
|
|
193
|
+
*/
|
|
194
|
+
getInput(controllerId: string): z.infer<TSchema> | undefined {
|
|
195
|
+
const rawInput = this.inputBuffer.get(controllerId);
|
|
196
|
+
|
|
197
|
+
if (!rawInput) {
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Validate with schema if provided
|
|
202
|
+
let validatedInput: Record<string, unknown> = rawInput;
|
|
203
|
+
if (this.config.schema) {
|
|
204
|
+
const result = this.config.schema.safeParse(rawInput);
|
|
205
|
+
if (!result.success) {
|
|
206
|
+
// Validation failed - log error and return undefined
|
|
207
|
+
console.warn(
|
|
208
|
+
`[InputManager] Invalid input for controller ${controllerId}:`,
|
|
209
|
+
result.error,
|
|
210
|
+
);
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
validatedInput = result.data as Record<string, unknown>;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Apply latching if configured
|
|
217
|
+
if (this.config.latch) {
|
|
218
|
+
return this.getLatched(
|
|
219
|
+
controllerId,
|
|
220
|
+
validatedInput as Record<string, unknown>,
|
|
221
|
+
) as z.infer<TSchema>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return validatedInput as z.infer<TSchema>;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Clears input state for a controller (e.g., when player leaves).
|
|
229
|
+
*/
|
|
230
|
+
clearInput(controllerId: string): void {
|
|
231
|
+
this.inputBuffer.delete(controllerId);
|
|
232
|
+
this.latchState.delete(controllerId);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Updates the room ID filter.
|
|
237
|
+
*/
|
|
238
|
+
setRoomId(roomId: string | undefined): void {
|
|
239
|
+
this.roomId = roomId;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Internal method to apply latching logic.
|
|
244
|
+
*/
|
|
245
|
+
private getLatched<TInput extends Record<string, unknown>>(
|
|
246
|
+
controllerId: string,
|
|
247
|
+
rawInput: TInput,
|
|
248
|
+
): TInput | undefined {
|
|
249
|
+
const { booleanFields = [], vectorFields = [] } = this.config.latch!;
|
|
250
|
+
const state = this.latchState.get(controllerId);
|
|
251
|
+
|
|
252
|
+
// Simple hash to detect if raw input changed
|
|
253
|
+
const rawHash = JSON.stringify(rawInput);
|
|
254
|
+
const rawChanged = rawHash !== state?.lastRawHash;
|
|
255
|
+
|
|
256
|
+
// If raw hasn't changed and we have previous state, we already consumed
|
|
257
|
+
// Return raw (reset behavior - like old popInput did)
|
|
258
|
+
if (!rawChanged && state) {
|
|
259
|
+
// Reset latched to raw for next frame (consume behavior)
|
|
260
|
+
const resetLatched = { ...rawInput } as TInput;
|
|
261
|
+
const resetLatchedRecord = resetLatched as Record<string, unknown>;
|
|
262
|
+
const actualRawRecord = rawInput as Record<string, unknown>;
|
|
263
|
+
|
|
264
|
+
// Reset boolean fields to raw
|
|
265
|
+
for (const field of booleanFields) {
|
|
266
|
+
const rawValue = actualRawRecord[field];
|
|
267
|
+
resetLatchedRecord[field] = rawValue ?? false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Reset vector fields to raw
|
|
271
|
+
for (const field of vectorFields) {
|
|
272
|
+
const rawValue = actualRawRecord[field];
|
|
273
|
+
if (
|
|
274
|
+
rawValue &&
|
|
275
|
+
typeof rawValue === "object" &&
|
|
276
|
+
!Array.isArray(rawValue)
|
|
277
|
+
) {
|
|
278
|
+
const rawVec = rawValue as { x?: unknown; y?: unknown };
|
|
279
|
+
if (typeof rawVec.x === "number" && typeof rawVec.y === "number") {
|
|
280
|
+
resetLatchedRecord[field] = rawVec;
|
|
281
|
+
} else {
|
|
282
|
+
resetLatchedRecord[field] = { x: 0, y: 0 };
|
|
283
|
+
}
|
|
284
|
+
} else {
|
|
285
|
+
resetLatchedRecord[field] = { x: 0, y: 0 };
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Update state with reset values
|
|
290
|
+
this.latchState.set(controllerId, {
|
|
291
|
+
raw: rawInput,
|
|
292
|
+
latched: resetLatched,
|
|
293
|
+
lastRawHash: rawHash,
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
return resetLatched; // Return raw (consumed)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// New input or first call - calculate latched
|
|
300
|
+
const prevLatched = state?.latched ?? ({} as TInput);
|
|
301
|
+
|
|
302
|
+
const latched = { ...rawInput } as TInput;
|
|
303
|
+
const latchedRecord = latched as Record<string, unknown>;
|
|
304
|
+
const actualRawRecord = rawInput as Record<string, unknown>;
|
|
305
|
+
const prevLatchedRecord = prevLatched as Record<string, unknown>;
|
|
306
|
+
|
|
307
|
+
// Latch boolean fields
|
|
308
|
+
for (const field of booleanFields) {
|
|
309
|
+
const rawValue = actualRawRecord[field];
|
|
310
|
+
const prevValue = prevLatchedRecord[field];
|
|
311
|
+
|
|
312
|
+
if (typeof rawValue === "boolean") {
|
|
313
|
+
// Latch: true if currently true OR was true last frame
|
|
314
|
+
latchedRecord[field] = rawValue || prevValue === true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Latch vector fields (with zero detection)
|
|
319
|
+
for (const field of vectorFields) {
|
|
320
|
+
const rawValue = actualRawRecord[field];
|
|
321
|
+
const prevValue = prevLatchedRecord[field];
|
|
322
|
+
|
|
323
|
+
if (
|
|
324
|
+
rawValue &&
|
|
325
|
+
typeof rawValue === "object" &&
|
|
326
|
+
!Array.isArray(rawValue)
|
|
327
|
+
) {
|
|
328
|
+
const rawVec = rawValue as { x?: unknown; y?: unknown };
|
|
329
|
+
const prevVec = prevValue as { x?: unknown; y?: unknown } | undefined;
|
|
330
|
+
|
|
331
|
+
if (typeof rawVec.x === "number" && typeof rawVec.y === "number") {
|
|
332
|
+
const isRawActive = rawVec.x !== 0 || rawVec.y !== 0;
|
|
333
|
+
const isPrevActive =
|
|
334
|
+
prevVec &&
|
|
335
|
+
typeof prevVec.x === "number" &&
|
|
336
|
+
typeof prevVec.y === "number" &&
|
|
337
|
+
(prevVec.x !== 0 || prevVec.y !== 0);
|
|
338
|
+
|
|
339
|
+
if (isRawActive) {
|
|
340
|
+
// Currently active - use current value
|
|
341
|
+
latchedRecord[field] = rawVec;
|
|
342
|
+
} else if (isPrevActive) {
|
|
343
|
+
// Just released but was active - keep previous value (flick)
|
|
344
|
+
latchedRecord[field] = prevVec;
|
|
345
|
+
} else {
|
|
346
|
+
// Not active - use zero
|
|
347
|
+
latchedRecord[field] = { x: 0, y: 0 };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Store state for next frame
|
|
354
|
+
this.latchState.set(controllerId, {
|
|
355
|
+
raw: rawInput,
|
|
356
|
+
latched,
|
|
357
|
+
lastRawHash: rawHash,
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
return latched;
|
|
361
|
+
}
|
|
362
|
+
}
|