@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
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { applyPatches, createDraft, enablePatches, finishDraft } from "immer";
|
|
2
|
+
//#region src/plugins/model/store/drafts.ts
|
|
3
|
+
/** Root order of a commit result, so two commits never report the same roots in another order. */
|
|
4
|
+
const rootOrder = Object.freeze([
|
|
5
|
+
"player",
|
|
6
|
+
"session",
|
|
7
|
+
"rng"
|
|
8
|
+
]);
|
|
9
|
+
/**
|
|
10
|
+
* Turns on Immer's patch recording. Idempotent, and called from the state factory rather than at
|
|
11
|
+
* module scope: a module-scope call would run on import, even when no app is created.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
function enableDraftPatches() {
|
|
15
|
+
enablePatches();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Freezes a tree and every branch of it. Immer freezes what it produces; this covers the trees
|
|
19
|
+
* that never went through a draft — the initial trees and the ones handed to `restore()`.
|
|
20
|
+
*
|
|
21
|
+
* @param tree - The tree to freeze, in place.
|
|
22
|
+
* @returns The same reference, now frozen.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const tree = deepFreeze({ player: { bag: ["key"] } });
|
|
26
|
+
* Object.isFrozen(tree.player.bag); // true
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function deepFreeze(tree) {
|
|
30
|
+
if (!tree || typeof tree !== "object") return tree;
|
|
31
|
+
for (const branch of Object.values(tree)) deepFreeze(branch);
|
|
32
|
+
return Object.freeze(tree);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Maps one Immer patch to our own structural patch, so no Immer type reaches a public type.
|
|
36
|
+
* A removal carries no value, and `exactOptionalPropertyTypes` forbids an explicit `undefined`.
|
|
37
|
+
*
|
|
38
|
+
* @param patch - The Immer patch.
|
|
39
|
+
* @param path - Path of the patch inside its own tree, without the container segment.
|
|
40
|
+
* @returns The structural patch.
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* toPatch({ op: "remove", path: ["doc", "player", "key"] }, ["player", "key"]);
|
|
44
|
+
* // { op: "remove", path: ["player", "key"] }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
function toPatch(patch, path) {
|
|
48
|
+
if (patch.op === "remove") return {
|
|
49
|
+
op: patch.op,
|
|
50
|
+
path
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
op: patch.op,
|
|
54
|
+
path,
|
|
55
|
+
value: patch.value
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Derives the touched roots from the patches of one commit, in a fixed order.
|
|
60
|
+
*
|
|
61
|
+
* @param docPatches - Patches of the save document.
|
|
62
|
+
* @param sessionPatches - Patches of the session tree.
|
|
63
|
+
* @returns The roots a listener has to reconcile.
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* rootsOf([{ op: "add", path: ["rng", "streams", "dice"], value: 7 }], []); // ["rng"]
|
|
67
|
+
* rootsOf([], [{ op: "replace", path: ["rolls"], value: 1 }]); // ["session"]
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function rootsOf(docPatches, sessionPatches) {
|
|
71
|
+
const touched = /* @__PURE__ */ new Set();
|
|
72
|
+
for (const patch of docPatches) {
|
|
73
|
+
const [head] = patch.path;
|
|
74
|
+
if (head === "player" || head === "rng") touched.add(head);
|
|
75
|
+
}
|
|
76
|
+
if (sessionPatches.length > 0) touched.add("session");
|
|
77
|
+
return rootOrder.filter((root) => touched.has(root));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Opens mutable drafts of the frozen save document and session tree.
|
|
81
|
+
* Both live in one draft container, so one `finishDraft` closes the transaction and the patches
|
|
82
|
+
* arrive in one list, tagged by their container key.
|
|
83
|
+
*
|
|
84
|
+
* @param doc - Frozen save document.
|
|
85
|
+
* @param session - Frozen session tree.
|
|
86
|
+
* @returns The open drafts of one transaction.
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const pair = openDrafts({ player: { coins: 1 }, rng: { seed: 7, streams: {} } }, { rolls: 0 });
|
|
90
|
+
* pair.session; // a mutable draft of { rolls: 0 }: a write never reaches the frozen input
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
function openDrafts(doc, session) {
|
|
94
|
+
return createDraft({
|
|
95
|
+
doc,
|
|
96
|
+
session
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Finishes the drafts: the new frozen trees, the patches per tree and the touched roots.
|
|
101
|
+
* The drafts are revoked, so a draft that leaked into a view throws on the next touch instead of
|
|
102
|
+
* corrupting the committed tree in silence.
|
|
103
|
+
*
|
|
104
|
+
* @param pair - Open drafts of one transaction.
|
|
105
|
+
* @returns The frozen trees, the patches split by tree, and the touched roots.
|
|
106
|
+
*/
|
|
107
|
+
function finishDrafts(pair) {
|
|
108
|
+
const collected = [];
|
|
109
|
+
const finished = finishDraft(pair, (patches) => {
|
|
110
|
+
collected.push(...patches);
|
|
111
|
+
});
|
|
112
|
+
const docPatches = [];
|
|
113
|
+
const sessionPatches = [];
|
|
114
|
+
for (const patch of collected) {
|
|
115
|
+
const [container, ...path] = patch.path;
|
|
116
|
+
(container === "doc" ? docPatches : sessionPatches).push(toPatch(patch, path));
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
doc: finished.doc,
|
|
120
|
+
session: finished.session,
|
|
121
|
+
patches: {
|
|
122
|
+
doc: docPatches,
|
|
123
|
+
session: sessionPatches
|
|
124
|
+
},
|
|
125
|
+
roots: rootsOf(docPatches, sessionPatches)
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Applies the patches of one commit to a document, the way a save seam stores what it was handed.
|
|
130
|
+
* A whole-document patch carries the path `[]` and replaces the document. The input is left as it
|
|
131
|
+
* is: the result is a new frozen document.
|
|
132
|
+
*
|
|
133
|
+
* @param document - The document the patches apply to.
|
|
134
|
+
* @param patches - The patches of one commit, in order.
|
|
135
|
+
* @returns The document after the patches.
|
|
136
|
+
* @throws {Error} When a patch names a path the document does not have.
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* applyTo({ player: { coins: 0 } }, [{ op: "replace", path: ["player", "coins"], value: 4 }]);
|
|
140
|
+
* // { player: { coins: 4 } }
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
function applyTo(document, patches) {
|
|
144
|
+
enablePatches();
|
|
145
|
+
return applyPatches(document, patches);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Drops the drafts without a result: the base trees stay as they are and the drafts are revoked.
|
|
149
|
+
*
|
|
150
|
+
* @param pair - Open drafts of one transaction.
|
|
151
|
+
*/
|
|
152
|
+
function dropDrafts(pair) {
|
|
153
|
+
finishDraft(pair);
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/plugins/model/store/providers/memory.ts
|
|
157
|
+
/** Seed of a save built by `saveOf` when the caller does not pin one. */
|
|
158
|
+
const defaultSeed = 1;
|
|
159
|
+
/** The base a patch applies to when the provider was never handed a document. */
|
|
160
|
+
const noDocument = {};
|
|
161
|
+
/**
|
|
162
|
+
* Creates an in-memory provider that keeps what it was committed and records every call.
|
|
163
|
+
* It is the default when a game configures no `playerProvider`, and the double every test uses:
|
|
164
|
+
* `provider.calls` is the whole persistence protocol of one run, in order. The document lives as
|
|
165
|
+
* long as the provider does, so a second app over the same instance reads what the first one
|
|
166
|
+
* wrote, and nothing survives the process.
|
|
167
|
+
*
|
|
168
|
+
* `load()` reads the fixture, or everything committed since; without either, the player is new.
|
|
169
|
+
* `commitDurable()` resolves at once: there is no disk behind it.
|
|
170
|
+
*
|
|
171
|
+
* @param fixture - The save `load()` starts from. Omitted: a new player.
|
|
172
|
+
* @param fixture.state - The saved document.
|
|
173
|
+
* @param fixture.version - Schema version of the saved document.
|
|
174
|
+
* @returns A provider that keeps its document and records its calls.
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* // A new player is saved at once. One roll reaches the provider at the next rest node.
|
|
178
|
+
* const provider = memory();
|
|
179
|
+
* const game = await createHeadless(createGame(provider));
|
|
180
|
+
*
|
|
181
|
+
* await game.walk([{ at: "home", intent: "roll" }]);
|
|
182
|
+
* provider.calls.map(call => call.method); // ["load", "commit", "commit"]
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
function memory(fixture) {
|
|
186
|
+
const calls = [];
|
|
187
|
+
const held = {
|
|
188
|
+
document: fixture?.state,
|
|
189
|
+
version: fixture?.version ?? 0
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Applies the patches of one commit to the held document.
|
|
193
|
+
*
|
|
194
|
+
* @param patches - Doc patches since the last commit.
|
|
195
|
+
* @param version - Schema version this build writes.
|
|
196
|
+
* @throws {Error} When a patch names a path the held document does not have.
|
|
197
|
+
*/
|
|
198
|
+
const hold = (patches, version) => {
|
|
199
|
+
held.document = applyTo(held.document ?? noDocument, patches);
|
|
200
|
+
held.version = version;
|
|
201
|
+
};
|
|
202
|
+
return {
|
|
203
|
+
calls,
|
|
204
|
+
load: async () => {
|
|
205
|
+
calls.push({ method: "load" });
|
|
206
|
+
const document = held.document;
|
|
207
|
+
if (document === void 0) return null;
|
|
208
|
+
return {
|
|
209
|
+
state: document,
|
|
210
|
+
version: held.version
|
|
211
|
+
};
|
|
212
|
+
},
|
|
213
|
+
commit: (patches, version) => {
|
|
214
|
+
calls.push({
|
|
215
|
+
method: "commit",
|
|
216
|
+
patches,
|
|
217
|
+
version
|
|
218
|
+
});
|
|
219
|
+
hold(patches, version);
|
|
220
|
+
},
|
|
221
|
+
commitDurable: async (patches, txId, version) => {
|
|
222
|
+
calls.push({
|
|
223
|
+
method: "commitDurable",
|
|
224
|
+
patches,
|
|
225
|
+
txId,
|
|
226
|
+
version
|
|
227
|
+
});
|
|
228
|
+
hold(patches, version);
|
|
229
|
+
},
|
|
230
|
+
flush: async () => {
|
|
231
|
+
calls.push({ method: "flush" });
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Builds a save document from a player tree, with no rng streams drawn yet.
|
|
237
|
+
* Short fixtures in tests read as `saveOf({ coins: 5 })` instead of spelling out the rng branch.
|
|
238
|
+
*
|
|
239
|
+
* @param player - Player tree of the save.
|
|
240
|
+
* @param seed - Rng seed of the save.
|
|
241
|
+
* @returns The save document.
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* // A returning player with 5 coins. The test starts from this save, not from `initialPlayer`.
|
|
245
|
+
* saveOf({ coins: 5 }, 42); // { player: { coins: 5 }, rng: { seed: 42, streams: {} } }
|
|
246
|
+
* const provider = memory({ state: saveOf({ coins: 5 }, 42), version: 1 });
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
function saveOf(player, seed = defaultSeed) {
|
|
250
|
+
return {
|
|
251
|
+
player,
|
|
252
|
+
rng: {
|
|
253
|
+
seed,
|
|
254
|
+
streams: {}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
//#endregion
|
|
259
|
+
export { enableDraftPatches as a, dropDrafts as i, saveOf as n, finishDrafts as o, deepFreeze as r, openDrafts as s, memory as t };
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
//#region src/plugins/flow/runner/journal.ts
|
|
2
|
+
/** FNV-1a 32-bit offset basis. */
|
|
3
|
+
const OFFSET_BASIS = 2166136261;
|
|
4
|
+
/** FNV-1a 32-bit prime. */
|
|
5
|
+
const PRIME = 16777619;
|
|
6
|
+
/** Separates the parts of a hashed edge, so "a|b" and "a" + "b" never collide. */
|
|
7
|
+
const SEPARATOR = "\0";
|
|
8
|
+
/**
|
|
9
|
+
* Hashes text with FNV-1a, 32 bit. Pure and deterministic: the same text always gives the same
|
|
10
|
+
* eight hexadecimal characters, in every runtime.
|
|
11
|
+
*
|
|
12
|
+
* @param text - The text to hash.
|
|
13
|
+
* @returns Eight lowercase hexadecimal characters.
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* hashText("board/merge"); // "53ceca70"
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function hashText(text) {
|
|
20
|
+
let hash = OFFSET_BASIS;
|
|
21
|
+
for (const character of text) hash = Math.imul(hash ^ (character.codePointAt(0) ?? 0), PRIME);
|
|
22
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Hashes one edge, so a replay against edited code fails loudly in dev.
|
|
26
|
+
*
|
|
27
|
+
* @param path - Path of the node that produced the outcome.
|
|
28
|
+
* @param outcome - Outcome name.
|
|
29
|
+
* @param next - Rendered edge target.
|
|
30
|
+
* @returns The digest of the three parts together.
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* entryHash("board/merge", "done", "board/awaitIntent"); // "e881176b"
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
function entryHash(path, outcome, next) {
|
|
37
|
+
return hashText([
|
|
38
|
+
path,
|
|
39
|
+
outcome,
|
|
40
|
+
next
|
|
41
|
+
].join(SEPARATOR));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Appends an entry with the next index and its hash, and drops the oldest entries above the limit.
|
|
45
|
+
* The index keeps counting while entries are dropped, so two entries never share a number.
|
|
46
|
+
*
|
|
47
|
+
* @param state - Runner state that holds the journal.
|
|
48
|
+
* @param entry - The taken edge without `index` and `hash`.
|
|
49
|
+
* @param limit - Journal entries kept between checkpoints.
|
|
50
|
+
* @returns The stored entry.
|
|
51
|
+
*/
|
|
52
|
+
function pushEntry(state, entry, limit) {
|
|
53
|
+
const stored = {
|
|
54
|
+
...entry,
|
|
55
|
+
index: state.journalIndex,
|
|
56
|
+
hash: entryHash(entry.path, entry.outcome, entry.next)
|
|
57
|
+
};
|
|
58
|
+
state.journalIndex += 1;
|
|
59
|
+
state.journal.push(stored);
|
|
60
|
+
if (state.journal.length > limit) state.journal.splice(0, state.journal.length - limit);
|
|
61
|
+
return stored;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Clears the journal at a checkpoint. The running index is kept, and so is the array itself: a
|
|
65
|
+
* reader that holds `history()` sees the compaction.
|
|
66
|
+
*
|
|
67
|
+
* @param state - Runner state that holds the journal.
|
|
68
|
+
*/
|
|
69
|
+
function compact(state) {
|
|
70
|
+
state.journal.length = 0;
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/plugins/flow/runner/registry.ts
|
|
74
|
+
/**
|
|
75
|
+
* Creates the empty flow map. It lives in its own function because the plugin's lint rule L5
|
|
76
|
+
* refuses a collection built inside an exported declaration.
|
|
77
|
+
*
|
|
78
|
+
* @returns An empty map of flow id to flow.
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* emptyFlows().size; // 0
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
function emptyFlows() {
|
|
85
|
+
return /* @__PURE__ */ new Map();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Adds a flow and every flow it reaches by reference to the map. The first flow under an id wins,
|
|
89
|
+
* so a collision is visible to `validateGraph` instead of silently replacing a flow.
|
|
90
|
+
*
|
|
91
|
+
* @param flow - The flow to add.
|
|
92
|
+
* @param flows - The map being filled.
|
|
93
|
+
*/
|
|
94
|
+
function addFlow(flow, flows) {
|
|
95
|
+
if (flows.has(flow.id)) return;
|
|
96
|
+
flows.set(flow.id, flow);
|
|
97
|
+
for (const entry of Object.values(flow.nodes)) if (entry.kind === "flow") addFlow(entry, flows);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Collects every flow reachable from the main flow by reference, plus the registered extras.
|
|
101
|
+
* The main flow is the first entry: `describeGraph` reads it as the entry point of the graph.
|
|
102
|
+
*
|
|
103
|
+
* @param main - The top-level flow.
|
|
104
|
+
* @param extra - Flows added with `register`, not reachable by reference.
|
|
105
|
+
* @returns Flow id to flow, main first.
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* [...collectFlows(mainFlow, []).keys()]; // ["main", "board"]: the main flow first
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
function collectFlows(main, extra) {
|
|
112
|
+
const flows = emptyFlows();
|
|
113
|
+
addFlow(main, flows);
|
|
114
|
+
for (const flow of extra) addFlow(flow, flows);
|
|
115
|
+
return flows;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Renders a position as a path: the node of every frame, outermost first.
|
|
119
|
+
*
|
|
120
|
+
* @param stack - One frame per nesting level.
|
|
121
|
+
* @returns The path, for example `"board/awaitIntent"`; empty without a position.
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* const board = { flow: "main", node: "board", input: null };
|
|
125
|
+
* framePath([board, { flow: "board", node: "merge", input: null }]); // "board/merge"
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
function framePath(stack) {
|
|
129
|
+
return stack.map((frame) => frame.node).join("/");
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Picks the flow a path continues in after one entry: the sub-flow itself, or, behind a slot, the
|
|
133
|
+
* first contribution in order that has the next segment as a node. A path keeps node names only,
|
|
134
|
+
* so two contributions of one slot with the same node name cannot be told apart; `validateGraph`
|
|
135
|
+
* warns about that.
|
|
136
|
+
*
|
|
137
|
+
* @param entry - The entry the path just passed.
|
|
138
|
+
* @param next - The next segment of the path.
|
|
139
|
+
* @param contributionsOf - Reads the contributions of a slot, in order.
|
|
140
|
+
* @returns The flow to go on in, or `undefined` when the path ends here.
|
|
141
|
+
*/
|
|
142
|
+
function flowBehind(entry, next, contributionsOf) {
|
|
143
|
+
if (entry.kind === "flow") return entry;
|
|
144
|
+
if (entry.kind !== "slot") return void 0;
|
|
145
|
+
return contributionsOf(entry.name).find((item) => Object.hasOwn(item.flow.nodes, next))?.flow;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Reads no contributions: the default of `findNode` for a caller that has no features API.
|
|
149
|
+
*
|
|
150
|
+
* @returns An empty list.
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* noContributions(); // []
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
function noContributions() {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Finds the node, sub-flow or slot a path names, starting at the main flow. A path descends one
|
|
161
|
+
* sub-flow per segment, and through a slot into the contribution that has the next segment; a
|
|
162
|
+
* segment after a plain node finds nothing.
|
|
163
|
+
*
|
|
164
|
+
* @param main - The top-level flow.
|
|
165
|
+
* @param path - A path as `framePath` renders it.
|
|
166
|
+
* @param contributionsOf - Reads the contributions of a slot, in order. Without it a path ends at
|
|
167
|
+
* a slot.
|
|
168
|
+
* @returns Where the path leads, or `undefined` when no such node exists.
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* findNode(mainFlow, "board/awaitIntent")?.trail;
|
|
172
|
+
* // [{ flow: "main", node: "board" }, { flow: "board", node: "awaitIntent" }]
|
|
173
|
+
* findNode(mainFlow, "afterOrder/show", () => [{ flow: rewardFlow }])?.trail;
|
|
174
|
+
* // [{ flow: "main", node: "afterOrder" }, { flow: "rewardPopup", node: "show" }]
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
function findNode(main, path, contributionsOf = noContributions) {
|
|
178
|
+
const names = path.split("/").filter(Boolean);
|
|
179
|
+
const trail = [];
|
|
180
|
+
let flow = main;
|
|
181
|
+
for (const [step, name] of names.entries()) {
|
|
182
|
+
const entry = flow.nodes[name];
|
|
183
|
+
if (!entry) return void 0;
|
|
184
|
+
trail.push({
|
|
185
|
+
flow: flow.id,
|
|
186
|
+
node: name
|
|
187
|
+
});
|
|
188
|
+
if (step === names.length - 1) return {
|
|
189
|
+
flow,
|
|
190
|
+
name,
|
|
191
|
+
entry,
|
|
192
|
+
trail
|
|
193
|
+
};
|
|
194
|
+
const behind = flowBehind(entry, names[step + 1] ?? "", contributionsOf);
|
|
195
|
+
if (behind === void 0) return void 0;
|
|
196
|
+
flow = behind;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Renders an edge target as a string: a node name, `"exit:win"` or `"map:node"`.
|
|
201
|
+
*
|
|
202
|
+
* @param target - The target as the edge table holds it.
|
|
203
|
+
* @returns The rendered target.
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* renderTarget("home"); // "home"
|
|
207
|
+
* renderTarget({ kind: "exit", outcome: "win" }); // "exit:win"
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
function renderTarget(target) {
|
|
211
|
+
if (typeof target === "string") return target;
|
|
212
|
+
return target.kind === "exit" ? `exit:${target.outcome}` : `map:${target.target}`;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Renders the edge table of one flow.
|
|
216
|
+
*
|
|
217
|
+
* @param flow - The flow to render.
|
|
218
|
+
* @returns Node name to outcome name to rendered target.
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* renderEdges(boardFlow).awaitIntent?.leave; // "exit:left"
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
function renderEdges(flow) {
|
|
225
|
+
const edges = {};
|
|
226
|
+
for (const [name, row] of Object.entries(flow.edges)) {
|
|
227
|
+
const rendered = {};
|
|
228
|
+
for (const [outcome, target] of Object.entries(row)) rendered[outcome] = renderTarget(target);
|
|
229
|
+
edges[name] = rendered;
|
|
230
|
+
}
|
|
231
|
+
return edges;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Maps every node and flow a feature brought to the name of that feature.
|
|
235
|
+
*
|
|
236
|
+
* @param features - Features API.
|
|
237
|
+
* @returns Node or flow object to feature name.
|
|
238
|
+
*/
|
|
239
|
+
function collectOwners(features) {
|
|
240
|
+
const owners = /* @__PURE__ */ new Map();
|
|
241
|
+
for (const { name, description } of features.all()) {
|
|
242
|
+
for (const node of description.nodes ?? []) owners.set(node, name);
|
|
243
|
+
for (const flow of description.flows ?? []) owners.set(flow, name);
|
|
244
|
+
}
|
|
245
|
+
return owners;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Describes one entry of a flow: its flags, its scene, its outcome names, and the slot, sub-flow
|
|
249
|
+
* or owning feature when it has one.
|
|
250
|
+
*
|
|
251
|
+
* @param flow - The flow that holds the entry.
|
|
252
|
+
* @param name - Name of the entry inside that flow.
|
|
253
|
+
* @param entry - The node, sub-flow or slot.
|
|
254
|
+
* @param owner - Name of the feature that brought it, when a feature did.
|
|
255
|
+
* @returns The entry as JSON.
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* describeNode(mainFlow, "home", home, undefined).rest; // true
|
|
259
|
+
* describeNode(mainFlow, "board", boardFlow, "board").subFlow; // "board"
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
function describeNode(flow, name, entry, owner) {
|
|
263
|
+
const node = entry.kind === "node" ? entry : void 0;
|
|
264
|
+
return {
|
|
265
|
+
flow: flow.id,
|
|
266
|
+
node: name,
|
|
267
|
+
rest: node?.rest ?? false,
|
|
268
|
+
over: node?.over ?? false,
|
|
269
|
+
checkpoint: node?.checkpoint ?? false,
|
|
270
|
+
barrier: node?.barrier ?? false,
|
|
271
|
+
outcomes: Object.keys(entry.outcomes),
|
|
272
|
+
...node?.scene === void 0 ? {} : { scene: node.scene },
|
|
273
|
+
...entry.kind === "slot" ? { slot: entry.name } : {},
|
|
274
|
+
...entry.kind === "flow" ? { subFlow: entry.id } : {},
|
|
275
|
+
...owner === void 0 ? {} : { owner }
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Describes every entry of one flow.
|
|
280
|
+
*
|
|
281
|
+
* @param flow - The flow to describe.
|
|
282
|
+
* @param owners - Node or flow object to feature name.
|
|
283
|
+
* @returns Node name to description.
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* describeNodes(boardFlow, new Map()).merge?.outcomes; // ["done", "rejected"]
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
function describeNodes(flow, owners) {
|
|
290
|
+
const nodes = {};
|
|
291
|
+
for (const [name, entry] of Object.entries(flow.nodes)) nodes[name] = describeNode(flow, name, entry, owners.get(entry) ?? owners.get(flow));
|
|
292
|
+
return nodes;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Collects the slot names of every flow of the graph. `describe()` and `validate` both read the
|
|
296
|
+
* contributions of exactly these slots.
|
|
297
|
+
*
|
|
298
|
+
* @param flows - Every collected flow by id.
|
|
299
|
+
* @returns The slot names, in the order the graph declares them, each once.
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* slotNames(collectFlows(mainFlow, [])); // ["afterOrder"]
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
function slotNames(flows) {
|
|
306
|
+
const names = [];
|
|
307
|
+
for (const flow of flows.values()) for (const entry of Object.values(flow.nodes)) if (entry.kind === "slot" && !names.includes(entry.name)) names.push(entry.name);
|
|
308
|
+
return names;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Renders the whole graph as JSON without running the game: nodes, flags, outcomes, edges,
|
|
312
|
+
* slots and who contributed. Edge targets become `"node"`, `"exit:win"`, `"map:node"`.
|
|
313
|
+
*
|
|
314
|
+
* @param flows - Every collected flow by id, the main flow first, as `collectFlows` returns them.
|
|
315
|
+
* @param features - Features API: owners and slot contributions.
|
|
316
|
+
* @returns The graph as plain JSON.
|
|
317
|
+
*/
|
|
318
|
+
function describeGraph(flows, features) {
|
|
319
|
+
const owners = collectOwners(features);
|
|
320
|
+
const described = {};
|
|
321
|
+
const slots = {};
|
|
322
|
+
for (const flow of flows.values()) described[flow.id] = {
|
|
323
|
+
nodes: describeNodes(flow, owners),
|
|
324
|
+
start: flow.start,
|
|
325
|
+
edges: renderEdges(flow)
|
|
326
|
+
};
|
|
327
|
+
for (const name of slotNames(flows)) slots[name] = features.contributions(name).map(({ feature, flow, order }) => ({
|
|
328
|
+
feature,
|
|
329
|
+
flow: flow.id,
|
|
330
|
+
order
|
|
331
|
+
}));
|
|
332
|
+
return {
|
|
333
|
+
main: [...flows.keys()][0] ?? "",
|
|
334
|
+
flows: described,
|
|
335
|
+
slots
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Compares two object keys without a locale, so the rendering is the same everywhere.
|
|
340
|
+
*
|
|
341
|
+
* @param first - First key.
|
|
342
|
+
* @param second - Second key.
|
|
343
|
+
* @returns A negative number, zero or a positive number.
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* compareKeys("edges", "nodes"); // -1
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
function compareKeys(first, second) {
|
|
350
|
+
if (first === second) return 0;
|
|
351
|
+
return first < second ? -1 : 1;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Renders a JSON value with its object keys sorted, so two equal graphs written in a different
|
|
355
|
+
* order hash the same.
|
|
356
|
+
*
|
|
357
|
+
* @param value - Any JSON value.
|
|
358
|
+
* @returns The value as text.
|
|
359
|
+
* @example
|
|
360
|
+
* ```ts
|
|
361
|
+
* stableJson({ b: 1, a: 2 }); // '{"a":2,"b":1}'
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
function stableJson(value) {
|
|
365
|
+
if (Array.isArray(value)) return `[${value.map((item) => stableJson(item)).join(",")}]`;
|
|
366
|
+
if (typeof value !== "object" || !value) return JSON.stringify(value) ?? "";
|
|
367
|
+
return `{${Object.entries(value).filter(([, item]) => item !== void 0).toSorted(([first], [second]) => compareKeys(first, second)).map(([key, item]) => `${JSON.stringify(key)}:${stableJson(item)}`).join(",")}}`;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Hashes a graph description. A bookmark of a plain rest node is accepted only while this hash
|
|
371
|
+
* is unchanged.
|
|
372
|
+
*
|
|
373
|
+
* @param graph - Result of `describeGraph`.
|
|
374
|
+
* @returns Eight lowercase hexadecimal characters.
|
|
375
|
+
* @example
|
|
376
|
+
* ```ts
|
|
377
|
+
* graphHash({ main: "main", flows: {}, slots: {} }); // "cfb16c03"
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
function graphHash(graph) {
|
|
381
|
+
return hashText(stableJson(graph));
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
export { graphHash as a, pushEntry as c, framePath as i, describeGraph as n, slotNames as o, findNode as r, compact as s, collectFlows as t };
|