@moku-labs/game 0.0.1 → 0.0.2
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 +424 -37
- package/dist/assets.d.mts +161 -0
- package/dist/assets.mjs +4729 -0
- package/dist/component-DGg5DqKK.mjs +44 -0
- package/dist/control.d.mts +162 -0
- package/dist/control.mjs +758 -0
- package/dist/define-sFoO3y6X.d.mts +263 -0
- package/dist/headless-CvamCUcR.mjs +186 -0
- package/dist/headless-KaSWcd0s.d.mts +181 -0
- package/dist/index.d.mts +1204 -50
- package/dist/index.mjs +20978 -2010
- package/dist/inspect.d.mts +109 -0
- package/dist/inspect.mjs +519 -0
- package/dist/jsx-dev-runtime.d.mts +2 -0
- package/dist/jsx-dev-runtime.mjs +2 -0
- package/dist/jsx-runtime.d.mts +2 -0
- package/dist/jsx-runtime.mjs +2 -0
- package/dist/memory-CvgdnsQO.mjs +259 -0
- package/dist/registry-DlpRCibU.mjs +384 -0
- package/dist/runtime-DRlwxkIv.mjs +182 -0
- package/dist/runtime-DiOTkDZz.d.mts +77 -0
- package/dist/session-DmAxY6Ll.mjs +145 -0
- package/dist/testing.d.mts +23 -111
- package/dist/testing.mjs +12 -289
- package/dist/types-BxkNNYul.d.mts +2840 -0
- package/dist/types-CTPS9GBu.d.mts +742 -0
- package/dist/types-DD-QrG_z.d.mts +588 -0
- package/dist/types-DWILGrPn.d.mts +622 -0
- package/dist/types-DYLnSgMI.d.mts +570 -0
- package/dist/types-JNc_UQBo.d.mts +2896 -0
- package/dist/types-yg_ywtT-.d.mts +1859 -0
- package/dist/visual-BDUHSRvf.mjs +734 -0
- package/package.json +26 -4
- package/dist/registry-DWV5C0Mf.mjs +0 -666
- package/dist/types-BfsmUzLC.d.mts +0 -1908
package/dist/testing.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as saveOf, t as memory } from "./memory-CvgdnsQO.mjs";
|
|
2
|
+
import { i as stepFrames, r as runRepro, t as createHeadless } from "./headless-CvamCUcR.mjs";
|
|
2
3
|
//#region src/plugins/clock/fake.ts
|
|
3
4
|
/**
|
|
4
5
|
* Removes and returns the timer that is due first, up to and including `until`.
|
|
@@ -7,10 +8,6 @@ import { a as graphHash, l as memory, u as saveOf } from "./registry-DWV5C0Mf.mj
|
|
|
7
8
|
* @param state - Innards of the fake clock.
|
|
8
9
|
* @param until - Latest moment that counts as due.
|
|
9
10
|
* @returns The timer to fire, or `undefined` when nothing is due.
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* const due = takeDue(state, 1500);
|
|
13
|
-
* ```
|
|
14
11
|
*/
|
|
15
12
|
function takeDue(state, until) {
|
|
16
13
|
let earliest;
|
|
@@ -29,10 +26,6 @@ function takeDue(state, until) {
|
|
|
29
26
|
*
|
|
30
27
|
* @param state - Innards of the fake clock.
|
|
31
28
|
* @param until - Moment to stop at.
|
|
32
|
-
* @example
|
|
33
|
-
* ```ts
|
|
34
|
-
* runDue(state, state.now + 5000);
|
|
35
|
-
* ```
|
|
36
29
|
*/
|
|
37
30
|
function runDue(state, until) {
|
|
38
31
|
let due = takeDue(state, until);
|
|
@@ -44,14 +37,22 @@ function runDue(state, until) {
|
|
|
44
37
|
state.now = Math.max(state.now, until);
|
|
45
38
|
}
|
|
46
39
|
/**
|
|
47
|
-
* Creates a fake source for tests. Timers fire synchronously inside `advance`, in due order.
|
|
40
|
+
* Creates a fake source for tests. Timers fire synchronously inside `advance`, in due order. A
|
|
41
|
+
* negative delay counts as zero and an unknown handle is ignored.
|
|
48
42
|
*
|
|
49
43
|
* @param start - Starting moment in epoch milliseconds. Defaults to 0.
|
|
50
44
|
* @returns A clock source with the test-only controls `advance` and `set`.
|
|
51
45
|
* @example
|
|
52
46
|
* ```ts
|
|
47
|
+
* // An energy point refills 60 s after it was spent. The test moves the clock, not the minute.
|
|
53
48
|
* const clock = fakeClock(1000);
|
|
54
|
-
* clock
|
|
49
|
+
* const app = createApp({ pluginConfigs: { clock: { source: clock } } });
|
|
50
|
+
* const seen: number[] = [];
|
|
51
|
+
*
|
|
52
|
+
* app.clock.onElapsed(({ now }) => seen.push(now));
|
|
53
|
+
* app.clock.scheduleAt(61_000);
|
|
54
|
+
* clock.advance(60_000); // the timer fires inside advance, nothing waits
|
|
55
|
+
* seen; // [61000]
|
|
55
56
|
* ```
|
|
56
57
|
*/
|
|
57
58
|
function fakeClock(start = 0) {
|
|
@@ -61,27 +62,7 @@ function fakeClock(start = 0) {
|
|
|
61
62
|
timers: []
|
|
62
63
|
};
|
|
63
64
|
return {
|
|
64
|
-
/**
|
|
65
|
-
* Reads the fake moment.
|
|
66
|
-
*
|
|
67
|
-
* @returns The current moment in epoch milliseconds.
|
|
68
|
-
* @example
|
|
69
|
-
* ```ts
|
|
70
|
-
* const moment = clock.now();
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
65
|
now: () => state.now,
|
|
74
|
-
/**
|
|
75
|
-
* Schedules one callback. A negative delay counts as zero.
|
|
76
|
-
*
|
|
77
|
-
* @param callback - Function to run when the delay has passed.
|
|
78
|
-
* @param delayMs - Delay in milliseconds.
|
|
79
|
-
* @returns The handle to give back to `clearTimer`.
|
|
80
|
-
* @example
|
|
81
|
-
* ```ts
|
|
82
|
-
* const handle = clock.setTimer(fire, 1000);
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
66
|
setTimer: (callback, delayMs) => {
|
|
86
67
|
const id = state.nextId;
|
|
87
68
|
state.nextId += 1;
|
|
@@ -92,275 +73,17 @@ function fakeClock(start = 0) {
|
|
|
92
73
|
});
|
|
93
74
|
return id;
|
|
94
75
|
},
|
|
95
|
-
/**
|
|
96
|
-
* Cancels a pending timer. An unknown handle is ignored.
|
|
97
|
-
*
|
|
98
|
-
* @param handle - Handle returned by `setTimer`.
|
|
99
|
-
* @example
|
|
100
|
-
* ```ts
|
|
101
|
-
* clock.clearTimer(handle);
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
76
|
clearTimer: (handle) => {
|
|
105
77
|
if (typeof handle !== "number") return;
|
|
106
78
|
state.timers = state.timers.filter((timer) => timer.id !== handle);
|
|
107
79
|
},
|
|
108
|
-
/**
|
|
109
|
-
* Moves the clock forward and fires every timer that falls due, in due order.
|
|
110
|
-
* A negative amount moves nothing.
|
|
111
|
-
*
|
|
112
|
-
* @param ms - Milliseconds to move forward.
|
|
113
|
-
* @example
|
|
114
|
-
* ```ts
|
|
115
|
-
* clock.advance(60_000);
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
80
|
advance: (ms) => {
|
|
119
81
|
runDue(state, state.now + Math.max(0, ms));
|
|
120
82
|
},
|
|
121
|
-
/**
|
|
122
|
-
* Jumps the clock to a moment without firing any timer. Moving back is allowed: that is a
|
|
123
|
-
* device clock set by hand.
|
|
124
|
-
*
|
|
125
|
-
* @param moment - Moment in epoch milliseconds.
|
|
126
|
-
* @example
|
|
127
|
-
* ```ts
|
|
128
|
-
* clock.set(400);
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
83
|
set: (moment) => {
|
|
132
84
|
state.now = moment;
|
|
133
85
|
}
|
|
134
86
|
};
|
|
135
87
|
}
|
|
136
88
|
//#endregion
|
|
137
|
-
//#region src/plugins/flow/headless.ts
|
|
138
|
-
const noPayload = null;
|
|
139
|
-
/** Seed of a repro that pins no rng state, the seed `saveOf` writes. */
|
|
140
|
-
const defaultSeed = 1;
|
|
141
|
-
/**
|
|
142
|
-
* Turns whatever `run()` rejected with into an error that can be thrown again.
|
|
143
|
-
*
|
|
144
|
-
* @param value - The rejection value.
|
|
145
|
-
* @returns The value itself when it is an error, a wrapped one otherwise.
|
|
146
|
-
* @example
|
|
147
|
-
* ```ts
|
|
148
|
-
* throw asError(fatal.error);
|
|
149
|
-
* ```
|
|
150
|
-
*/
|
|
151
|
-
function asError(value) {
|
|
152
|
-
if (value instanceof Error) return value;
|
|
153
|
-
return /* @__PURE__ */ new Error(`[game] The headless run failed.\n ${String(value)}.`);
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Starts the one loop unless the app's own `onStart` already did, and keeps a fatal error until
|
|
157
|
-
* someone asks for it. `run()` rejects only on a fatal error, and nobody awaits it here.
|
|
158
|
-
*
|
|
159
|
-
* @param app - The started app.
|
|
160
|
-
* @returns The holder of the fatal error.
|
|
161
|
-
* @example
|
|
162
|
-
* ```ts
|
|
163
|
-
* const fatal = startLoop(app);
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
function startLoop(app) {
|
|
167
|
-
const fatal = { error: void 0 };
|
|
168
|
-
if (app.flow.state().running) return fatal;
|
|
169
|
-
app.flow.run().catch((error) => {
|
|
170
|
-
fatal.error = error;
|
|
171
|
-
});
|
|
172
|
-
return fatal;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Waits until the loop rests for the first time, or ends. An empty route walks nowhere: it only
|
|
176
|
-
* waits for the gate the loop opens at the node it enters, so a start that is a chain of transit
|
|
177
|
-
* nodes is played out before anything reads the game.
|
|
178
|
-
*
|
|
179
|
-
* @param app - The started app whose loop is running.
|
|
180
|
-
* @param fatal - Holder of the fatal error of `run()`.
|
|
181
|
-
* @returns Resolves once the loop rests at its first rest node.
|
|
182
|
-
* @throws {Error} When the loop failed fatally instead of reaching a rest node.
|
|
183
|
-
* @example
|
|
184
|
-
* ```ts
|
|
185
|
-
* await settleAtFirstRest(app, fatal);
|
|
186
|
-
* ```
|
|
187
|
-
*/
|
|
188
|
-
async function settleAtFirstRest(app, fatal) {
|
|
189
|
-
await app.flow.walk([]).catch((error) => {
|
|
190
|
-
throw asError(fatal.error ?? error);
|
|
191
|
-
});
|
|
192
|
-
if (fatal.error !== void 0) throw asError(fatal.error);
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Starts an app headless: sets flow mode `"fast"`, awaits `app.start()`, starts `flow.run()`
|
|
196
|
-
* unless the app's own `onStart` already did, and waits until the loop rests for the first time.
|
|
197
|
-
* `game.state().path` therefore names the first rest node of the graph as soon as the call
|
|
198
|
-
* resolves. A fatal error of the loop rejects this call, and a later one is re-thrown by `walk`
|
|
199
|
-
* and by `stop`, so a headless test never loses it.
|
|
200
|
-
*
|
|
201
|
-
* @param app - An app that is not started yet.
|
|
202
|
-
* @returns The game: walk it, answer it, read it, stop it.
|
|
203
|
-
* @throws {Error} When the loop failed fatally before it reached its first rest node.
|
|
204
|
-
* @example
|
|
205
|
-
* ```ts
|
|
206
|
-
* const game = await createHeadless(app);
|
|
207
|
-
* expect(game.state().path).toBe("home");
|
|
208
|
-
* await game.walk([{ at: "board/awaitIntent", intent: "merge", payload: { from: "c2", to: "c3" } }]);
|
|
209
|
-
* await game.stop();
|
|
210
|
-
* ```
|
|
211
|
-
*/
|
|
212
|
-
async function createHeadless(app) {
|
|
213
|
-
app.flow.setMode("fast");
|
|
214
|
-
await app.start();
|
|
215
|
-
const fatal = startLoop(app);
|
|
216
|
-
await settleAtFirstRest(app, fatal);
|
|
217
|
-
return {
|
|
218
|
-
/**
|
|
219
|
-
* Walks a route through the running loop.
|
|
220
|
-
*
|
|
221
|
-
* @param route - The player's answers and substituted sub-flow results, in order.
|
|
222
|
-
* @returns The state the walk ended in.
|
|
223
|
-
* @throws {Error} When a step is never reached, or the loop failed fatally.
|
|
224
|
-
* @example
|
|
225
|
-
* ```ts
|
|
226
|
-
* await game.walk([{ at: "home", intent: "play" }]);
|
|
227
|
-
* ```
|
|
228
|
-
*/
|
|
229
|
-
walk: async (route) => {
|
|
230
|
-
const state = await app.flow.walk(route).catch((error) => {
|
|
231
|
-
throw asError(fatal.error ?? error);
|
|
232
|
-
});
|
|
233
|
-
if (fatal.error !== void 0) throw asError(fatal.error);
|
|
234
|
-
return state;
|
|
235
|
-
},
|
|
236
|
-
/**
|
|
237
|
-
* Gives one answer to the gate, the way a tap does.
|
|
238
|
-
*
|
|
239
|
-
* @param answer - Intent and optional payload.
|
|
240
|
-
* @returns Whether the gate took it.
|
|
241
|
-
* @example
|
|
242
|
-
* ```ts
|
|
243
|
-
* game.answer({ intent: "play" });
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
answer: (answer) => app.flow.gate.answer(answer),
|
|
247
|
-
/**
|
|
248
|
-
* Reads where the graph stands.
|
|
249
|
-
*
|
|
250
|
-
* @returns Whether it runs, the path, the stack, what it waits for and the mode.
|
|
251
|
-
* @example
|
|
252
|
-
* ```ts
|
|
253
|
-
* expect(game.state().path).toBe("home");
|
|
254
|
-
* ```
|
|
255
|
-
*/
|
|
256
|
-
state: () => app.flow.state(),
|
|
257
|
-
/**
|
|
258
|
-
* Reads the edges taken since the last checkpoint.
|
|
259
|
-
*
|
|
260
|
-
* @returns A copy of the journal.
|
|
261
|
-
* @example
|
|
262
|
-
* ```ts
|
|
263
|
-
* const [last] = game.history().slice(-1);
|
|
264
|
-
* ```
|
|
265
|
-
*/
|
|
266
|
-
history: () => app.flow.history(),
|
|
267
|
-
/**
|
|
268
|
-
* Stops the app, then re-throws a fatal error of the loop.
|
|
269
|
-
*
|
|
270
|
-
* @throws {Error} When the loop failed fatally.
|
|
271
|
-
* @example
|
|
272
|
-
* ```ts
|
|
273
|
-
* await game.stop();
|
|
274
|
-
* ```
|
|
275
|
-
*/
|
|
276
|
-
stop: async () => {
|
|
277
|
-
await app.stop();
|
|
278
|
-
if (fatal.error !== void 0) throw asError(fatal.error);
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Reads the rest node a repro starts at: its checkpoint, or the start of the main flow.
|
|
284
|
-
*
|
|
285
|
-
* @param app - The started app.
|
|
286
|
-
* @param repro - Starting state, optional checkpoint and the route.
|
|
287
|
-
* @returns The path of the node the repro enters.
|
|
288
|
-
* @throws {Error} When the graph has no main flow to start from.
|
|
289
|
-
* @example
|
|
290
|
-
* ```ts
|
|
291
|
-
* const path = reproPath(app, repro);
|
|
292
|
-
* ```
|
|
293
|
-
*/
|
|
294
|
-
function reproPath(app, repro) {
|
|
295
|
-
if (repro.checkpoint !== void 0) return repro.checkpoint;
|
|
296
|
-
const graph = app.flow.describe();
|
|
297
|
-
const start = graph.flows[graph.main]?.start;
|
|
298
|
-
if (start === void 0) throw new Error(`[game] runRepro() found no flow "${graph.main}" to start from.\n Name the checkpoint the repro was taken at.`);
|
|
299
|
-
return start;
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Builds the bookmark a repro is entered with: its state, its checkpoint and the hash of the
|
|
303
|
-
* graph that runs now, so a checkpoint is accepted and any other rest node is checked.
|
|
304
|
-
*
|
|
305
|
-
* @param app - The started app.
|
|
306
|
-
* @param repro - Starting state, optional checkpoint and the route.
|
|
307
|
-
* @returns The bookmark to restore.
|
|
308
|
-
* @example
|
|
309
|
-
* ```ts
|
|
310
|
-
* const bookmark = reproBookmark(app, repro);
|
|
311
|
-
* ```
|
|
312
|
-
*/
|
|
313
|
-
function reproBookmark(app, repro) {
|
|
314
|
-
return {
|
|
315
|
-
path: reproPath(app, repro),
|
|
316
|
-
input: noPayload,
|
|
317
|
-
player: repro.player,
|
|
318
|
-
session: repro.session ?? {},
|
|
319
|
-
rng: repro.rng ?? {
|
|
320
|
-
seed: defaultSeed,
|
|
321
|
-
streams: {}
|
|
322
|
-
},
|
|
323
|
-
graph: graphHash(app.flow.describe())
|
|
324
|
-
};
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Plays a repro: starts the app headless, restores the bookmark built from its state and
|
|
328
|
-
* checkpoint, then walks its route. The app keeps running, so the caller can read more than the
|
|
329
|
-
* result and stops it itself.
|
|
330
|
-
*
|
|
331
|
-
* @param app - An app that is not started yet.
|
|
332
|
-
* @param repro - Starting state, optional checkpoint and the route.
|
|
333
|
-
* @returns The path the run ended at, and the committed state.
|
|
334
|
-
* @example
|
|
335
|
-
* ```ts
|
|
336
|
-
* const result = await runRepro(app, { player: saved, checkpoint: "home", route });
|
|
337
|
-
* ```
|
|
338
|
-
*/
|
|
339
|
-
async function runRepro(app, repro) {
|
|
340
|
-
const game = await createHeadless(app);
|
|
341
|
-
await app.flow.restore(reproBookmark(app, repro));
|
|
342
|
-
const state = await game.walk(repro.route);
|
|
343
|
-
const ended = app.flow.bookmark();
|
|
344
|
-
return {
|
|
345
|
-
path: state.stack.map((frame) => frame.node),
|
|
346
|
-
player: ended.player,
|
|
347
|
-
session: ended.session
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Steps the frame loop by hand: `count` calls of `app.time.step(deltaMs)`. A headless game has no
|
|
352
|
-
* frame source, so this is the only thing that moves the frame phases.
|
|
353
|
-
*
|
|
354
|
-
* @param app - A started app.
|
|
355
|
-
* @param count - Number of frames.
|
|
356
|
-
* @param deltaMs - Milliseconds per frame.
|
|
357
|
-
* @example
|
|
358
|
-
* ```ts
|
|
359
|
-
* stepFrames(app, 60, 16);
|
|
360
|
-
* ```
|
|
361
|
-
*/
|
|
362
|
-
function stepFrames(app, count, deltaMs) {
|
|
363
|
-
for (let frame = 0; frame < count; frame += 1) app.time.step(deltaMs);
|
|
364
|
-
}
|
|
365
|
-
//#endregion
|
|
366
89
|
export { createHeadless, fakeClock, memory, runRepro, saveOf, stepFrames };
|