@air-jam/cli 0.0.0-stage → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +273 -2
- package/bin/airjam.mjs +66 -0
- package/dist/ai-pack.d.ts +44 -0
- package/dist/ai-pack.js +15 -0
- package/dist/ai-pack.js.map +1 -0
- package/dist/auth-profile.d.ts +5 -0
- package/dist/auth-profile.js +7 -0
- package/dist/auth-profile.js.map +1 -0
- package/dist/auth-sessions.d.ts +5 -0
- package/dist/auth-sessions.js +7 -0
- package/dist/auth-sessions.js.map +1 -0
- package/dist/chunk-7D4AYBAV.js +64 -0
- package/dist/chunk-7D4AYBAV.js.map +1 -0
- package/dist/chunk-J5MAU4QZ.js +102 -0
- package/dist/chunk-J5MAU4QZ.js.map +1 -0
- package/dist/chunk-LH7UO57C.js +23 -0
- package/dist/chunk-LH7UO57C.js.map +1 -0
- package/dist/chunk-N7AI7HZK.js +529 -0
- package/dist/chunk-N7AI7HZK.js.map +1 -0
- package/dist/chunk-PKTLVQXZ.js +82 -0
- package/dist/chunk-PKTLVQXZ.js.map +1 -0
- package/dist/chunk-SQE4NPZP.js +31 -0
- package/dist/chunk-SQE4NPZP.js.map +1 -0
- package/dist/chunk-U6V3HEBB.js +939 -0
- package/dist/chunk-U6V3HEBB.js.map +1 -0
- package/dist/chunk-VEHNFPEO.js +224 -0
- package/dist/chunk-VEHNFPEO.js.map +1 -0
- package/dist/development.d.ts +102 -0
- package/dist/development.js +33 -0
- package/dist/development.js.map +1 -0
- package/dist/documentation.d.ts +27 -0
- package/dist/documentation.js +7 -0
- package/dist/documentation.js.map +1 -0
- package/dist/game-analytics.d.ts +5 -0
- package/dist/game-analytics.js +7 -0
- package/dist/game-analytics.js.map +1 -0
- package/dist/game-app-id.d.ts +5 -0
- package/dist/game-app-id.js +7 -0
- package/dist/game-app-id.js.map +1 -0
- package/dist/game-media.d.ts +5 -0
- package/dist/game-media.js +7 -0
- package/dist/game-media.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2468 -0
- package/dist/index.js.map +1 -0
- package/dist/scaffold.d.ts +6 -0
- package/dist/scaffold.js +20 -0
- package/dist/scaffold.js.map +1 -0
- package/package.json +82 -3
- package/runtime/local-network.d.mts +6 -0
- package/runtime/local-network.mjs +9 -0
- package/runtime/public-assets.mjs +62 -0
- package/runtime/source-fence.mjs +182 -0
- package/runtime/vite-config.d.mts +43 -0
- package/runtime/vite-config.mjs +134 -0
- package/runtime/vite-dev.mjs +18 -0
- package/runtime/vite-https.d.mts +11 -0
- package/runtime/vite-https.mjs +40 -0
- package/template-assets/bootstrap/.claude/launch.json +12 -0
- package/template-assets/bootstrap/AGENTS.md +290 -0
- package/template-assets/bootstrap/CLAUDE.md +55 -0
- package/template-assets/bootstrap/_gitignore +6 -0
- package/template-assets/bootstrap/skills/airjam-docs-workflow/SKILL.md +49 -0
- package/template-assets/bootstrap/skills/airjam-mcp/SKILL.md +100 -0
- package/template-assets/bootstrap/skills/canvas-2d-gameplay/SKILL.md +47 -0
- package/template-assets/bootstrap/skills/controller-ui/SKILL.md +71 -0
- package/template-assets/bootstrap/skills/debug-and-test/SKILL.md +72 -0
- package/template-assets/bootstrap/skills/game-architecture/SKILL.md +102 -0
- package/template-assets/bootstrap/skills/game-state-and-rendering/SKILL.md +44 -0
- package/template-assets/bootstrap/skills/host-surface/SKILL.md +52 -0
- package/template-assets/bootstrap/skills/index.md +36 -0
- package/template-assets/bootstrap/skills/prefab-authoring/SKILL.md +58 -0
- package/template-assets/bootstrap/skills/r3f-3d-scene/SKILL.md +61 -0
- package/template-assets/documentation/for-agents/controller-ui/page.mdx +99 -0
- package/template-assets/documentation/for-agents/page.mdx +120 -0
- package/template-assets/documentation/for-agents/project-structure/page.mdx +170 -0
- package/template-assets/documentation/for-agents/state-and-rendering/page.mdx +77 -0
- package/template-assets/documentation/for-agents/supported-agents/page.mdx +18 -0
- package/template-assets/documentation/getting-started/debugging/page.mdx +164 -0
- package/template-assets/documentation/getting-started/dev-logs/page.mdx +169 -0
- package/template-assets/documentation/getting-started/game-ideas/page.mdx +114 -0
- package/template-assets/documentation/getting-started/introduction/page.mdx +200 -0
- package/template-assets/documentation/getting-started/migrating-legacy-games/page.mdx +673 -0
- package/template-assets/documentation/getting-started/quick-start/page.mdx +310 -0
- package/template-assets/documentation/how-it-works/architecture/page.mdx +230 -0
- package/template-assets/documentation/how-it-works/host-system/page.mdx +270 -0
- package/template-assets/documentation/manifest.json +500 -0
- package/template-assets/documentation/sdk/assets-and-theme/page.mdx +185 -0
- package/template-assets/documentation/sdk/hooks/page.mdx +435 -0
- package/template-assets/documentation/sdk/input-system/page.mdx +240 -0
- package/template-assets/documentation/sdk/networked-state/page.mdx +295 -0
- package/template-assets/documentation/sdk/settings/page.mdx +155 -0
- package/template-assets/documentation/sdk/ui-components/page.mdx +268 -0
- package/template-assets/managed/.airjam/ai-pack.json +209 -0
- package/template-assets/managed/docs/airjam/agent-gold-path.md +115 -0
- package/template-assets/managed/docs/airjam/agent-mcp.md +179 -0
- package/template-assets/managed/docs/airjam/canvas-2d-gameplay.md +82 -0
- package/template-assets/managed/docs/airjam/composition-shell-contract.md +87 -0
- package/template-assets/managed/docs/airjam/debug-and-testing.md +104 -0
- package/template-assets/managed/docs/airjam/development-loop.md +51 -0
- package/template-assets/managed/docs/airjam/docs-index.md +64 -0
- package/template-assets/managed/docs/airjam/generated/architecture.md +223 -0
- package/template-assets/managed/docs/airjam/generated/assets-and-theme.md +188 -0
- package/template-assets/managed/docs/airjam/generated/controller-ui.md +102 -0
- package/template-assets/managed/docs/airjam/generated/debugging-and-logs.md +165 -0
- package/template-assets/managed/docs/airjam/generated/for-agents.md +123 -0
- package/template-assets/managed/docs/airjam/generated/game-ideas.md +107 -0
- package/template-assets/managed/docs/airjam/generated/host-system.md +261 -0
- package/template-assets/managed/docs/airjam/generated/input-system.md +241 -0
- package/template-assets/managed/docs/airjam/generated/introduction.md +195 -0
- package/template-assets/managed/docs/airjam/generated/migrating-legacy-games.md +674 -0
- package/template-assets/managed/docs/airjam/generated/networked-state.md +298 -0
- package/template-assets/managed/docs/airjam/generated/project-structure.md +171 -0
- package/template-assets/managed/docs/airjam/generated/quick-start.md +311 -0
- package/template-assets/managed/docs/airjam/generated/sdk-hooks.md +430 -0
- package/template-assets/managed/docs/airjam/generated/settings.md +158 -0
- package/template-assets/managed/docs/airjam/generated/state-and-rendering.md +80 -0
- package/template-assets/managed/docs/airjam/generated/supported-agents.md +21 -0
- package/template-assets/managed/docs/airjam/generated/ui-components.md +265 -0
- package/template-assets/managed/docs/airjam/generated/unified-dev-logs.md +170 -0
- package/template-assets/managed/docs/airjam/iconography.md +55 -0
- package/template-assets/managed/docs/airjam/prefab-authoring.md +212 -0
- package/template-assets/managed/docs/airjam/preview-controllers.md +129 -0
- package/template-assets/managed/docs/airjam/r3f-3d-scene.md +163 -0
- package/template-assets/managed/docs/airjam/state-lanes-cookbook.md +101 -0
- package/template-assets/managed/docs/airjam/visual-system.md +103 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# Networked State
|
|
2
|
+
|
|
3
|
+
`createAirJamStore` is the canonical shared-state lane for Air Jam:
|
|
4
|
+
|
|
5
|
+
- Host is the only source of truth.
|
|
6
|
+
- Controllers dispatch actions through RPC.
|
|
7
|
+
- Host applies actions, then state sync is broadcast back.
|
|
8
|
+
|
|
9
|
+
Use this lane for replicated game state, not per-frame input.
|
|
10
|
+
|
|
11
|
+
A store is a plain object from `@air-jam/sdk/core`, so game logic can use it
|
|
12
|
+
without React. Components read it with `useAirJamStore`.
|
|
13
|
+
|
|
14
|
+
## Private per-player state
|
|
15
|
+
|
|
16
|
+
A store can declare one `privateField`: a map keyed by `controllerId`. Use it for
|
|
17
|
+
hidden hands, roles or shopping lists. The host keeps every entry. The room's
|
|
18
|
+
public state broadcast never contains this field; each phone receives only its
|
|
19
|
+
own entry through targeted delivery, including after a reconnect.
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { createAirJamStore } from "@air-jam/sdk/core";
|
|
23
|
+
import { useAirJamPrivateState } from "@air-jam/sdk";
|
|
24
|
+
|
|
25
|
+
type MarketState = {
|
|
26
|
+
round: number;
|
|
27
|
+
secrets: Record<string, { shoppingList: string[] }>;
|
|
28
|
+
actions: {};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const marketStore = createAirJamStore<MarketState, "secrets">(
|
|
32
|
+
() => ({ round: 1, secrets: {}, actions: {} }),
|
|
33
|
+
{ privateField: "secrets" },
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
function ShoppingList() {
|
|
37
|
+
const ownEntry = useAirJamPrivateState(marketStore);
|
|
38
|
+
return <p>{ownEntry?.shoppingList.join(", ") ?? "Waiting for your list"}</p>;
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
When supplying an explicit state type, name the private field as the second type
|
|
43
|
+
argument so the hook infers that entry even when the store has other maps.
|
|
44
|
+
Declare this store in the game just like a public store. The hook returns
|
|
45
|
+
`undefined` until this controller's entry arrives, and when the host removes it.
|
|
46
|
+
Host actions update the map normally. Only changed entries are sent; public-only
|
|
47
|
+
changes do not resend private data. Embedded controllers use the same private
|
|
48
|
+
lane through their MessagePort bridge.
|
|
49
|
+
|
|
50
|
+
Agent `projectSnapshot` receives the public fields and a private map containing
|
|
51
|
+
only the requested controller's entry (an empty map without a controller).
|
|
52
|
+
Host-authority `rawStores` and host restore retain the whole map. Keep private
|
|
53
|
+
values out of other public fields and explicitly author any end-of-round reveal.
|
|
54
|
+
Private state derives from the replicated store, so headless runs and replays use
|
|
55
|
+
the same authoritative state; it is not a separate RPC or an encrypted channel.
|
|
56
|
+
|
|
57
|
+
## Canonical Action Model
|
|
58
|
+
|
|
59
|
+
Actions always use this shape on the host:
|
|
60
|
+
|
|
61
|
+
1. First argument: `ctx` (identity, role, connected players and game time `now`)
|
|
62
|
+
2. Second argument: `payload` (either `undefined` or one typed plain-object payload)
|
|
63
|
+
|
|
64
|
+
Dispatch always goes through the store's `actions`:
|
|
65
|
+
|
|
66
|
+
1. `gameStore.actions.someAction()`
|
|
67
|
+
2. `gameStore.actions.someAction({ ...payload })`
|
|
68
|
+
|
|
69
|
+
No trailing `controllerId` arguments and no multi-argument action payloads.
|
|
70
|
+
No primitive, array, or event-like root payloads.
|
|
71
|
+
|
|
72
|
+
## Create a Store
|
|
73
|
+
|
|
74
|
+
This example uses a starter-friendly `stores/` folder shape.
|
|
75
|
+
The important contract is the action model and ownership boundary, not the exact filename.
|
|
76
|
+
|
|
77
|
+
```tsx filename="src/game/stores/game-store.ts"
|
|
78
|
+
import { createAirJamStore, type AirJamActionContext } from "@air-jam/sdk/core";
|
|
79
|
+
|
|
80
|
+
type Team = "team1" | "team2";
|
|
81
|
+
|
|
82
|
+
interface GameState {
|
|
83
|
+
phase: "lobby" | "playing";
|
|
84
|
+
teamAssignments: Record<string, Team>;
|
|
85
|
+
scores: { team1: number; team2: number };
|
|
86
|
+
actions: {
|
|
87
|
+
joinTeam: (ctx: AirJamActionContext, payload: { team: Team }) => void;
|
|
88
|
+
scorePoint: (ctx: AirJamActionContext, payload: { team: Team }) => void;
|
|
89
|
+
setPhase: (
|
|
90
|
+
ctx: AirJamActionContext,
|
|
91
|
+
payload: { phase: "lobby" | "playing" },
|
|
92
|
+
) => void;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const gameStore = createAirJamStore<GameState>((set) => ({
|
|
97
|
+
phase: "lobby",
|
|
98
|
+
teamAssignments: {},
|
|
99
|
+
scores: { team1: 0, team2: 0 },
|
|
100
|
+
actions: {
|
|
101
|
+
joinTeam: ({ actorId }, { team }) =>
|
|
102
|
+
set((state) => ({
|
|
103
|
+
teamAssignments: {
|
|
104
|
+
...state.teamAssignments,
|
|
105
|
+
[actorId]: team,
|
|
106
|
+
},
|
|
107
|
+
})),
|
|
108
|
+
scorePoint: (_ctx, { team }) =>
|
|
109
|
+
set((state) => ({
|
|
110
|
+
scores: {
|
|
111
|
+
...state.scores,
|
|
112
|
+
[team]: state.scores[team] + 1,
|
|
113
|
+
},
|
|
114
|
+
})),
|
|
115
|
+
setPhase: (_ctx, { phase }) => set({ phase }),
|
|
116
|
+
},
|
|
117
|
+
}));
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Declare the Store
|
|
121
|
+
|
|
122
|
+
The host and controllers sync a store only when the app declares it:
|
|
123
|
+
|
|
124
|
+
```ts filename="src/airjam.config.ts"
|
|
125
|
+
import { defineAirJamGame } from "@air-jam/sdk/core";
|
|
126
|
+
import { gameStore } from "./game/stores/game-store";
|
|
127
|
+
|
|
128
|
+
export const airjam = defineAirJamGame({
|
|
129
|
+
metadata: {
|
|
130
|
+
slug: "space-game",
|
|
131
|
+
name: "Space Game",
|
|
132
|
+
tagline: "Fly together and defend the fleet.",
|
|
133
|
+
category: "arcade",
|
|
134
|
+
minPlayers: 1,
|
|
135
|
+
maxPlayers: 4,
|
|
136
|
+
inputModalities: ["buttons", "touch"],
|
|
137
|
+
supportedSdkRange: "^1.0.0",
|
|
138
|
+
maintainer: { name: "Air Jam" },
|
|
139
|
+
version: 1,
|
|
140
|
+
},
|
|
141
|
+
controllerPath: "/controller",
|
|
142
|
+
stores: [gameStore],
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
A declared store syncs for as long as the host or controller runs, whether or
|
|
147
|
+
not any component is reading it. Reading a store the app does not declare
|
|
148
|
+
throws `AJ_STORE_NOT_DECLARED`.
|
|
149
|
+
|
|
150
|
+
## Use on Controller
|
|
151
|
+
|
|
152
|
+
This controller example matches the starter template surface layout.
|
|
153
|
+
It assumes your app has already mounted `airjam.Controller`.
|
|
154
|
+
|
|
155
|
+
```tsx filename="src/controller/index.tsx"
|
|
156
|
+
import { useAirJamController, useAirJamStore } from "@air-jam/sdk";
|
|
157
|
+
import { gameStore } from "../game/stores/game-store";
|
|
158
|
+
|
|
159
|
+
export const ControllerView = () => {
|
|
160
|
+
const controller = useAirJamController();
|
|
161
|
+
const teamAssignments = useAirJamStore(
|
|
162
|
+
gameStore,
|
|
163
|
+
(state) => state.teamAssignments,
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
const myTeam = controller.controllerId
|
|
167
|
+
? teamAssignments[controller.controllerId]
|
|
168
|
+
: null;
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<div>
|
|
172
|
+
<p>My team: {myTeam ?? "unassigned"}</p>
|
|
173
|
+
<button onClick={() => gameStore.actions.joinTeam({ team: "team1" })}>
|
|
174
|
+
Join Team 1
|
|
175
|
+
</button>
|
|
176
|
+
</div>
|
|
177
|
+
);
|
|
178
|
+
};
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Use on Host
|
|
182
|
+
|
|
183
|
+
This host example also matches the starter template surface layout.
|
|
184
|
+
It assumes your app has already mounted `airjam.Host`.
|
|
185
|
+
|
|
186
|
+
```tsx filename="src/host/index.tsx"
|
|
187
|
+
import { useAirJamStore } from "@air-jam/sdk";
|
|
188
|
+
import { gameStore } from "../game/stores/game-store";
|
|
189
|
+
|
|
190
|
+
export const HostView = () => {
|
|
191
|
+
const phase = useAirJamStore(gameStore, (state) => state.phase);
|
|
192
|
+
const scores = useAirJamStore(gameStore, (state) => state.scores);
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<div>
|
|
196
|
+
<p>Phase: {phase}</p>
|
|
197
|
+
<p>
|
|
198
|
+
{scores.team1} - {scores.team2}
|
|
199
|
+
</p>
|
|
200
|
+
<button onClick={() => gameStore.actions.setPhase({ phase: "playing" })}>
|
|
201
|
+
Start
|
|
202
|
+
</button>
|
|
203
|
+
<button onClick={() => gameStore.actions.scorePoint({ team: "team1" })}>
|
|
204
|
+
Team 1 +1
|
|
205
|
+
</button>
|
|
206
|
+
</div>
|
|
207
|
+
);
|
|
208
|
+
};
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Action Flow
|
|
212
|
+
|
|
213
|
+
1. Controller calls `actions.someAction()` or `actions.someAction({ ...payload })`.
|
|
214
|
+
2. The phone's session sends a request on the store's channel.
|
|
215
|
+
3. The room stamps it with the phone's seat, which becomes `ctx.actorId`.
|
|
216
|
+
4. Host executes action handler `(ctx, payload)`, updates state and answers.
|
|
217
|
+
5. Host publishes what changed; the room keeps the latest copy and sends each
|
|
218
|
+
phone the change, with only its own private entry.
|
|
219
|
+
6. If the phone's connection drops before the answer arrives, the action
|
|
220
|
+
resolves `unconfirmed`: it may or may not have run, and the state the phone
|
|
221
|
+
mirrors shows which.
|
|
222
|
+
|
|
223
|
+
## Rules
|
|
224
|
+
|
|
225
|
+
1. Keep all networked mutations inside `actions`.
|
|
226
|
+
2. Always dispatch via `store.actions`; do not call `store.getState().actions.*` directly.
|
|
227
|
+
3. Use one payload object per action for stable evolution.
|
|
228
|
+
4. Root payloads must be omitted or plain objects.
|
|
229
|
+
5. `T | undefined` payload unions are not valid roots. If an action has no payload, omit it entirely.
|
|
230
|
+
6. Nested values must stay RPC-serializable.
|
|
231
|
+
7. Use `ctx.actorId` for identity-aware actions (team join, ownership, etc.).
|
|
232
|
+
8. `ctx.actorId` is always the dispatcher identity. If host code dispatches through `store.actions`, then `ctx.actorId` is the host.
|
|
233
|
+
9. Read time as `ctx.now`, never `Date.now()`: it is game time, the simulation's when the game declares one, so headless runs and replays see the same time. Store deadlines in it and let the simulation act on them; phones show what the host writes to the store, such as whole seconds left, because they cannot read the host's clock.
|
|
234
|
+
10. If the host intentionally needs to run the same semantic player action as controller `X`, use `store.asPlayer("X")` instead of inventing a target payload for an actor-owned action.
|
|
235
|
+
11. Keep per-frame gameplay input in declared controls, read in simulation steps, not in this store.
|
|
236
|
+
12. For a host-only side effect of an action, such as a sound, use `useHostActionListener(store, listener)`.
|
|
237
|
+
|
|
238
|
+
## Accept And Reject Examples
|
|
239
|
+
|
|
240
|
+
If a store action returns `void`, Air Jam treats it as accepted.
|
|
241
|
+
|
|
242
|
+
Return `rejectAirJamAction(...)` when the semantic action should fail with a clear machine-readable reason, and return `acceptAirJamAction(result)` when the semantic action should succeed with explicit result data:
|
|
243
|
+
|
|
244
|
+
```tsx
|
|
245
|
+
import {
|
|
246
|
+
acceptAirJamAction,
|
|
247
|
+
createAirJamStore,
|
|
248
|
+
rejectAirJamAction,
|
|
249
|
+
} from "@air-jam/sdk/core";
|
|
250
|
+
|
|
251
|
+
export const gameStore = createAirJamStore((set, get) => ({
|
|
252
|
+
aliveByPlayerId: {} as Record<string, boolean>,
|
|
253
|
+
cooldownMsByPlayerId: {} as Record<string, number>,
|
|
254
|
+
actions: {
|
|
255
|
+
fire: ({ actorId }) => {
|
|
256
|
+
if (!actorId || get().aliveByPlayerId[actorId] === false)
|
|
257
|
+
return rejectAirJamAction("player_dead", "Dead players cannot fire.");
|
|
258
|
+
const cooldownMs = 4500;
|
|
259
|
+
set((state) => ({
|
|
260
|
+
cooldownMsByPlayerId: {
|
|
261
|
+
...state.cooldownMsByPlayerId,
|
|
262
|
+
[actorId]: cooldownMs,
|
|
263
|
+
},
|
|
264
|
+
}));
|
|
265
|
+
return acceptAirJamAction({ cooldownMs });
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
}));
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Use this pattern when the caller needs a real semantic outcome.
|
|
272
|
+
|
|
273
|
+
## Embedded Arcade Runtimes
|
|
274
|
+
|
|
275
|
+
When a game runs inside Arcade, `createAirJamStore` still works the same way in game code.
|
|
276
|
+
|
|
277
|
+
The SDK/runtime automatically resolves the correct replicated store domain from the embedded runtime context, so you should:
|
|
278
|
+
|
|
279
|
+
1. keep normal `createAirJamStore(...)` usage in games
|
|
280
|
+
2. not parse Arcade URL params in app/game code
|
|
281
|
+
3. not pick custom store domains in game code just to work when embedded
|
|
282
|
+
|
|
283
|
+
## API Summary
|
|
284
|
+
|
|
285
|
+
`createAirJamStore<T>(initializer)` from `@air-jam/sdk/core` returns a store to
|
|
286
|
+
declare in `defineAirJamGame({ stores })`. It provides:
|
|
287
|
+
|
|
288
|
+
1. `actions`, dispatching with `() => Promise` or `(payloadObject) => Promise` signatures.
|
|
289
|
+
2. `getState()` and `subscribe()` for logic outside React.
|
|
290
|
+
3. `asPlayer(controllerId)`, a host-only dispatch map for explicit player impersonation of semantic actions.
|
|
291
|
+
|
|
292
|
+
React reads it with `useAirJamStore(store, selector)` and listens to host-applied
|
|
293
|
+
actions with `useHostActionListener(store, listener)`.
|
|
294
|
+
|
|
295
|
+
`T` must include an `actions` object with host handlers `(ctx, payload) => void`, where `payload` is `undefined` or a plain object.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Game Settings
|
|
2
|
+
|
|
3
|
+
Declare the numbers, switches and choices a creator tunes while playtesting in
|
|
4
|
+
`airjam.config.ts`. The host owns their values and revision. A live update takes
|
|
5
|
+
effect between simulation steps and is recorded for exact replay. While a
|
|
6
|
+
simulation declared as a loader is still loading, updates are refused, so a
|
|
7
|
+
replay starts from the settings the simulation was created with.
|
|
8
|
+
|
|
9
|
+
## Declare settings in core
|
|
10
|
+
|
|
11
|
+
Keep the declaration in a leaf module so the simulation can import its type
|
|
12
|
+
without importing the game config back again.
|
|
13
|
+
|
|
14
|
+
```ts filename="src/game/contracts/settings.ts"
|
|
15
|
+
import {
|
|
16
|
+
defineAirJamSettings,
|
|
17
|
+
setting,
|
|
18
|
+
type AirJamSettingsValues,
|
|
19
|
+
} from "@air-jam/sdk/core";
|
|
20
|
+
|
|
21
|
+
export const pongSettings = defineAirJamSettings({
|
|
22
|
+
ballSpeed: setting.number({
|
|
23
|
+
default: 360,
|
|
24
|
+
min: 180,
|
|
25
|
+
max: 720,
|
|
26
|
+
step: 10,
|
|
27
|
+
live: true,
|
|
28
|
+
description: "Ball speed in pixels per second.",
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
31
|
+
export type PongSettings = AirJamSettingsValues<typeof pongSettings>;
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Pass it as `defineAirJamGame({ settings: pongSettings, ... })`. Remove the
|
|
35
|
+
module constants it replaces. A lobby choice players make, such as
|
|
36
|
+
`pointsToWin`, remains replicated store state.
|
|
37
|
+
|
|
38
|
+
Keys are lower camel case. `setting.number` requires `default`, `min` and
|
|
39
|
+
`max`, with an optional positive `step`; use `step: 1` for whole numbers.
|
|
40
|
+
Steps are multiples of zero. `setting.boolean` requires a boolean `default`.
|
|
41
|
+
`setting.choice` takes a nonempty string tuple in `options` and a `default`
|
|
42
|
+
from that tuple; its value type is the union of those strings.
|
|
43
|
+
Every kind accepts `description` and `live`, which defaults to false.
|
|
44
|
+
`defineAirJamSettings` validates defaults immediately. The description returned
|
|
45
|
+
by `airjam.inspect_game` includes these descriptors in `declaration.settings`.
|
|
46
|
+
|
|
47
|
+
## Read the current values
|
|
48
|
+
|
|
49
|
+
Use the settings type as the fourth simulation type parameter:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
defineAirJamSimulation<State, Input, Event, PongSettings>({
|
|
53
|
+
stepMs: 1000 / 60,
|
|
54
|
+
create: ({ settings }) => createState(settings.ballSpeed),
|
|
55
|
+
step: (state, { settings, stepMs }) => {
|
|
56
|
+
state.x += (settings.ballSpeed * stepMs) / 1000;
|
|
57
|
+
},
|
|
58
|
+
observe: (state, { settings }) => ({
|
|
59
|
+
x: state.x,
|
|
60
|
+
ballSpeed: settings.ballSpeed,
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Store actions read the same values through
|
|
66
|
+
`AirJamActionContext<PongSettings>.settings`. Host rendering calls
|
|
67
|
+
`useAirJamSettings(pongSettings)` from `@air-jam/sdk`; the hook subscribes to
|
|
68
|
+
the host runtime and returns the typed values. Controllers have no settings
|
|
69
|
+
hook. Copy any value a phone must display into replicated store state.
|
|
70
|
+
|
|
71
|
+
## Set initial values and tune a live room
|
|
72
|
+
|
|
73
|
+
`HostRuntimeOptions.settings` accepts a partial set of initial values and fills
|
|
74
|
+
the rest from defaults. `airjam.start_game_run` accepts the same `settings`
|
|
75
|
+
object when creating a new room, for browser and headless runs. Initial values
|
|
76
|
+
may include non-live settings. They are fixed for the host's lifetime.
|
|
77
|
+
|
|
78
|
+
Open a game session with host authority, then call:
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{
|
|
82
|
+
"tool": "airjam.read_game_settings",
|
|
83
|
+
"arguments": { "gameSessionId": "<session id>" }
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
The result contains `values` and `revision`, initially zero. Update using that
|
|
88
|
+
revision:
|
|
89
|
+
|
|
90
|
+
```json
|
|
91
|
+
{
|
|
92
|
+
"tool": "airjam.update_game_settings",
|
|
93
|
+
"arguments": {
|
|
94
|
+
"gameSessionId": "<session id>",
|
|
95
|
+
"patch": { "ballSpeed": 700 },
|
|
96
|
+
"expectedRevision": 0
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
A successful update returns the complete values and the next revision. The
|
|
102
|
+
whole patch is validated before any value changes. Both tools have the same
|
|
103
|
+
host authority requirements as semantic host actions. The runtime contract is
|
|
104
|
+
`settings-read` with `{ roomId, runtimeId }`, and `settings-update` with those
|
|
105
|
+
fields plus `patch` and `expectedRevision`.
|
|
106
|
+
|
|
107
|
+
The CLI uses the same operations:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
pnpm exec airjam session settings-read <session-id>
|
|
111
|
+
pnpm exec airjam session settings-update <session-id> --patch '{"ballSpeed":700}' --expected-revision 0
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Both commands print JSON. Use `airjam run start --settings '{"ballSpeed":600}'` with the other required
|
|
115
|
+
run options to start a fresh room at a chosen speed. Attaching a run to an
|
|
116
|
+
existing room does not replace its initial settings.
|
|
117
|
+
|
|
118
|
+
## Errors and replay
|
|
119
|
+
|
|
120
|
+
`AJ_SETTING_UNKNOWN` names an undeclared key. `AJ_SETTING_INVALID` means a
|
|
121
|
+
value violates its type, range, step or choices. `AJ_SETTING_STALE` means
|
|
122
|
+
another update changed the revision; read again before applying a new patch.
|
|
123
|
+
`AJ_SETTING_NOT_LIVE` means the setting requires a new room with that initial
|
|
124
|
+
value. Rejected updates leave all values and the revision unchanged.
|
|
125
|
+
|
|
126
|
+
Input logs use `version: 3`, with resolved initial `settings` and ordered
|
|
127
|
+
`settings` events containing each accepted `patch` and `revision`.
|
|
128
|
+
They share the stream with inputs and actions. `replayAirJamInputLog` creates
|
|
129
|
+
the host with the initial values and applies each update in order before its
|
|
130
|
+
recorded step. Use the same seed and game code to reproduce the final state.
|
|
131
|
+
|
|
132
|
+
## Keeping a value
|
|
133
|
+
|
|
134
|
+
A live change lasts for the current room. To make a chosen value the default for
|
|
135
|
+
future rooms and test runs, keep it in the game's declaration:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
pnpm exec airjam game settings keep --set ballSpeed=400 --json
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Use `--dir <game>` from another directory, and repeat `--set` for several values.
|
|
142
|
+
The command requires the game's own `typescript` dependency and rewrites only
|
|
143
|
+
literal `default:` values in the selected `defineAirJamSettings({...})`, even
|
|
144
|
+
when it is imported from another game module. Imported settings must resolve to
|
|
145
|
+
one `const` directly initialized by `defineAirJamSettings`; named and default
|
|
146
|
+
exports and explicit re-export aliases are resolved to that binding. Mutable or
|
|
147
|
+
ambiguous bindings are refused. The config must export the `defineAirJamGame`
|
|
148
|
+
binding as `airjam`, so an unused game declaration is never selected. Comments
|
|
149
|
+
and formatting stay in
|
|
150
|
+
place; the declaration remains the only source of defaults. Commit the change
|
|
151
|
+
as ordinary game source. Shared or computed defaults, ambiguous declarations,
|
|
152
|
+
unknown keys and values outside their declared type, range, step or options are
|
|
153
|
+
refused before writing. The JSON result names the file and each `{ key, from,
|
|
154
|
+
to }`; equal values write nothing. Studio's Keep operation uses this same
|
|
155
|
+
transform and captures the source change in history.
|