@intentius/behold 0.29.39 → 0.29.41
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/AGENTS.md +148 -10
- package/README.md +6 -2
- package/dist/cli.js +15965 -13076
- package/example-writes/README.md +1 -1
- package/example-writes/chant.config.ts +0 -6
- package/package.json +5 -2
- package/web/app.js +327 -21
- package/web/carve-export.js +142 -0
- package/web/carve-narration.js +268 -0
- package/web/carve-narration.test.js +18 -0
- package/web/change-set-overlay.js +147 -0
- package/web/change-set-overlay.test.js +83 -0
- package/web/dots-border.js +66 -0
- package/web/dots-border.test.js +27 -0
- package/web/dots-kiosk.js +279 -0
- package/web/dots-kiosk.test.js +60 -0
- package/web/dots-layout.d.ts +10 -0
- package/web/dots-layout.js +201 -0
- package/web/dots-live.js +316 -0
- package/web/dots-live.test.js +220 -0
- package/web/dots-lod.js +122 -0
- package/web/dots-lod.test.js +78 -0
- package/web/dots-recording.d.ts +7 -0
- package/web/dots-recording.js +247 -0
- package/web/dots-recording.test.js +71 -0
- package/web/dots-scene.js +294 -0
- package/web/dots-scene.test.js +118 -0
- package/web/dots-timeline.js +128 -0
- package/web/dots-timeline.test.js +83 -0
- package/web/dots-tour.js +153 -0
- package/web/dots.js +2124 -0
- package/web/dots.test.js +15 -0
- package/web/index.html +29 -0
- package/web/kiosk-carve.js +219 -0
- package/web/kiosk-carve.test.js +26 -0
- package/web/kiosk-event-help-ui.js +6 -0
- package/web/kiosk.html +2 -1
- package/web/kiosk.js +21 -6
- package/web/operator.js +1 -1
- package/web/run-playhead.js +102 -19
- package/web/run-playhead.test.js +81 -11
- package/web/tour-player.js +204 -0
- package/web/tour-player.test.js +115 -0
- package/web/tours/behold.tour.v1.schema.json +182 -0
- package/web/tours/terralith.json +330 -0
- package/web/trunk-lane.js +254 -0
- package/web/trunk-lane.test.js +117 -0
- package/web/workspace-gates.js +29 -4
- package/web/workspace-gates.test.js +23 -2
package/web/dots-live.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// The dots layer's live driver (#715): terragucci's estate rosters and its
|
|
2
|
+
// migrations' progress lines (/api/terragucci/carve, src/terragucci-carve.ts)
|
|
3
|
+
// read into the same "show" a recording makes (web/dots-recording.js), so the
|
|
4
|
+
// one layer draws both. Pure: the route's answer in, plain data out.
|
|
5
|
+
//
|
|
6
|
+
// What a dot is: one resource instance an estate's roster or a progress line
|
|
7
|
+
// names, keyed by its live id. A stock root's roster has no live ids (it is
|
|
8
|
+
// written from the state), so its dots are keyed by estate and address until a
|
|
9
|
+
// stamp names their live id; a move then finds them by that id.
|
|
10
|
+
//
|
|
11
|
+
// Each progress line is one event:
|
|
12
|
+
// stamped an adoption: the dot gains a ring, since it now wears its tags
|
|
13
|
+
// (and moves, when the line names another estate);
|
|
14
|
+
// moved a split: the dot flies to its new estate's box;
|
|
15
|
+
// refused the move is refused: the dot bounces back and stays lit red;
|
|
16
|
+
// skipped nothing to draw; it is counted.
|
|
17
|
+
//
|
|
18
|
+
// Lines play as they arrive, in real time. A line already in the first
|
|
19
|
+
// answer happened before the page looked, so it is drawn where it ended, with
|
|
20
|
+
// no flight; a line that arrives later plays from the moment it was read. An
|
|
21
|
+
// adoption on floci stamps 82 resources in under a second, so its lines come
|
|
22
|
+
// in one read and play as the one burst they were. Slowed playback is a
|
|
23
|
+
// person's choice (the kiosk's "slowed ×N", web/tour-player.js SLOWED): the
|
|
24
|
+
// lines of one read are then drawn `stepMs` apart, queued after the last one
|
|
25
|
+
// still to play. A recording's replay paces itself (web/dots-scene.js).
|
|
26
|
+
//
|
|
27
|
+
// An adoption turns a stock root into an estate (the answer's `adopted`
|
|
28
|
+
// pairs them). That is one box changing its name, not two boxes: the
|
|
29
|
+
// estate's resources and lines are drawn in the stock root's box, the stamps
|
|
30
|
+
// ring the dots where they are, and the box takes the estate's name when the
|
|
31
|
+
// first stamp plays.
|
|
32
|
+
|
|
33
|
+
/** How far apart the lines of one read play by default (0: real time, as read), and how far ahead of a line its flight may start. */
|
|
34
|
+
export const LIVE = { stepMs: 0, lead: 2_500 };
|
|
35
|
+
|
|
36
|
+
/** A progress line's key: the same line in two reads is the same event. */
|
|
37
|
+
export function lineKey(migration, line) {
|
|
38
|
+
return `${migration}|${line.at}|${line.estate_from}|${line.address}|${line.outcome}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* When each line is drawn from: `seen` maps a line's key to that time. Lines
|
|
43
|
+
* of the first read keep their own `at` (the past); the lines new in a later
|
|
44
|
+
* read play `stepMs` apart in the order they were written, from `now`, or
|
|
45
|
+
* after the last line of an earlier batch still waiting to play. Returns a new
|
|
46
|
+
* map; `seen` is not changed.
|
|
47
|
+
*/
|
|
48
|
+
export function stampArrivals(answer, seen, now, { first = false, stepMs = LIVE.stepMs } = {}) {
|
|
49
|
+
const out = new Map(seen);
|
|
50
|
+
const fresh = [];
|
|
51
|
+
for (const m of answer && answer.migrations ? answer.migrations : []) {
|
|
52
|
+
for (const l of m.lines) {
|
|
53
|
+
const k = lineKey(m.name, l);
|
|
54
|
+
if (out.has(k)) continue;
|
|
55
|
+
if (first) out.set(k, Date.parse(l.at) || now);
|
|
56
|
+
else fresh.push({ k, at: Date.parse(l.at) || 0 });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
fresh.sort((a, b) => a.at - b.at);
|
|
60
|
+
let queued = -Infinity;
|
|
61
|
+
for (const t of seen.values()) if (t > queued) queued = t;
|
|
62
|
+
let next = Math.max(now, queued + stepMs);
|
|
63
|
+
for (const f of fresh) {
|
|
64
|
+
out.set(f.k, next);
|
|
65
|
+
next += stepMs;
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The name an estate's box carries at show time `t`: a stock root an
|
|
72
|
+
* adoption replaced takes its estate's name once the first stamp plays.
|
|
73
|
+
*/
|
|
74
|
+
export function boxName(show, estate, t) {
|
|
75
|
+
const r = show && show.renames ? show.renames.get(estate) : undefined;
|
|
76
|
+
return r && t >= r.at ? r.to : estate;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const unknownType = (address) => {
|
|
80
|
+
const m = /(?:^|\.)([a-z][a-z0-9]*_[a-z0-9_]+)\.[^.]+$/.exec(address.replace(/\[[^\]]*\]/g, ""));
|
|
81
|
+
return m ? m[1] : "";
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The live carve as a show. `seen` is {@link stampArrivals}' map; `now` is the
|
|
86
|
+
* show's end so far. `t0`, when given, is the start an earlier read chose, so
|
|
87
|
+
* the show's clock stays put from one read to the next.
|
|
88
|
+
*/
|
|
89
|
+
export function liveShow(answer, seen, now, { t0: start = null } = {}) {
|
|
90
|
+
const dots = new Map(); // id -> { id, address, type, taggable, estate0, live_id, marked }
|
|
91
|
+
const byLive = new Map();
|
|
92
|
+
const byAddr = new Map();
|
|
93
|
+
const usedAddr = new Set();
|
|
94
|
+
const place = (estate, address) => `${estate}\u0000${address}`;
|
|
95
|
+
const addDot = (estate, r) => {
|
|
96
|
+
// A dot's address is its address where it was first seen, kept unique so the layout can key on it.
|
|
97
|
+
let address = r.address;
|
|
98
|
+
for (let n = 2; usedAddr.has(address); n++) address = `${r.address}#${n}`;
|
|
99
|
+
usedAddr.add(address);
|
|
100
|
+
const d = { id: address, address, type: r.type || unknownType(r.address), taggable: !r.follows, estate0: estate, live_id: r.live_id || null, marked: !!r.marked, follows: r.follows || null, home: estate };
|
|
101
|
+
dots.set(d.id, d);
|
|
102
|
+
byAddr.set(place(estate, r.address), d);
|
|
103
|
+
if (d.live_id) byLive.set(d.live_id, d);
|
|
104
|
+
return d;
|
|
105
|
+
};
|
|
106
|
+
const find = (estate, r) => (r.live_id && byLive.get(r.live_id)) || byAddr.get(place(estate, r.address));
|
|
107
|
+
|
|
108
|
+
// An adopted stock root and its estate are one box, keyed by the stock root's name.
|
|
109
|
+
const alias = new Map(((answer && answer.adopted) || []).map((a) => [a.to, a.from]));
|
|
110
|
+
const as = (e) => alias.get(e) ?? e;
|
|
111
|
+
const estates = ((answer && answer.estates) || []).map((e) => ({ ...e, estate: as(e.estate) }));
|
|
112
|
+
const migrations = ((answer && answer.migrations) || []).map((m) => ({ ...m, lines: m.lines.map((l) => ({ ...l, estate_from: as(l.estate_from), estate_to: as(l.estate_to), key: lineKey(m.name, l), into: l.estate_to, rawFrom: l.estate_from })) }));
|
|
113
|
+
// 1. A stock root's resources, which only an address names.
|
|
114
|
+
for (const e of estates) for (const r of e.resources) if (!r.live_id && !find(e.estate, r)) addDot(e.estate, r);
|
|
115
|
+
// 2. Each line in time order, finding its dot by live id, else where it came from.
|
|
116
|
+
const lines = [];
|
|
117
|
+
for (const m of migrations) for (const l of m.lines) lines.push({ m: m.name, l, t: seen.get(l.key) ?? (Date.parse(l.at) || now) });
|
|
118
|
+
lines.sort((a, b) => a.t - b.t);
|
|
119
|
+
const claimed = new Set();
|
|
120
|
+
for (const x of lines) {
|
|
121
|
+
const { l } = x;
|
|
122
|
+
let d = find(l.estate_from, l);
|
|
123
|
+
// A destination roster written after the move (a wave's, from planned values, so with no live ids) already drew
|
|
124
|
+
// the resource in the estate it moved to: the line moves that dot, from where it came, never a second one.
|
|
125
|
+
if (!d) {
|
|
126
|
+
const there = byAddr.get(place(l.estate_to, l.address));
|
|
127
|
+
if (there && !claimed.has(there) && there.estate0 === l.estate_to) {
|
|
128
|
+
d = there;
|
|
129
|
+
d.estate0 = l.estate_from;
|
|
130
|
+
byAddr.set(place(l.estate_from, l.address), d);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!d) d = addDot(l.estate_from, { address: l.address, type: "", live_id: l.live_id, follows: l.follows });
|
|
134
|
+
if (l.live_id && !d.live_id) {
|
|
135
|
+
d.live_id = l.live_id;
|
|
136
|
+
byLive.set(l.live_id, d);
|
|
137
|
+
}
|
|
138
|
+
// The destination's roster named this resource by address only (a wave's roster, written after the move), and
|
|
139
|
+
// an earlier line of the cycle (the adoption's stamp) already made its dot: one dot, under the plain address.
|
|
140
|
+
const old = byAddr.get(place(l.estate_to, l.address));
|
|
141
|
+
if (old && old !== d && !claimed.has(old) && old.estate0 === l.estate_to && !old.live_id) {
|
|
142
|
+
dots.delete(old.id);
|
|
143
|
+
if (d.id !== old.id) {
|
|
144
|
+
dots.delete(d.id);
|
|
145
|
+
usedAddr.delete(d.id);
|
|
146
|
+
d.id = d.address = old.id;
|
|
147
|
+
dots.set(d.id, d);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
byAddr.set(place(l.estate_to, l.address), d);
|
|
151
|
+
claimed.add(d);
|
|
152
|
+
x.dot = d;
|
|
153
|
+
}
|
|
154
|
+
// 3. Every other estate's resources: by live id, else by address, else a dot of their own.
|
|
155
|
+
for (const e of estates) {
|
|
156
|
+
for (const r of e.resources) {
|
|
157
|
+
const d = find(e.estate, r);
|
|
158
|
+
if (d) {
|
|
159
|
+
if (r.marked) d.marked = true;
|
|
160
|
+
if (!d.type && r.type) d.type = r.type;
|
|
161
|
+
if (r.follows) d.follows = d.follows || r.follows;
|
|
162
|
+
d.home = e.estate;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
addDot(e.estate, r).home = e.estate;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The source: where the first line came from, else the estate holding the most dots.
|
|
170
|
+
const owners0 = new Map([...dots.values()].map((d) => [d.id, d.estate0]));
|
|
171
|
+
let source = lines.length ? lines[0].l.estate_from : null;
|
|
172
|
+
if (!source) {
|
|
173
|
+
const n = new Map();
|
|
174
|
+
for (const e of owners0.values()) n.set(e, (n.get(e) ?? 0) + 1);
|
|
175
|
+
source = [...n].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1))[0]?.[0] ?? (estates[0] ? estates[0].estate : "estate");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Parents: a line or roster entry that follows another names it by address.
|
|
179
|
+
const parents = new Map();
|
|
180
|
+
for (const d of dots.values()) {
|
|
181
|
+
if (!d.follows) continue;
|
|
182
|
+
const p = byAddr.get(place(d.estate0, d.follows)) || [...dots.values()].find((q) => q.address === d.follows);
|
|
183
|
+
if (p && p !== d) parents.set(d.id, p.id);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Events.
|
|
187
|
+
const events = [];
|
|
188
|
+
const writeOf = new Map(); // `${migration}|${parent id}` -> its write, for followers
|
|
189
|
+
const followerLines = new Map(); // dot id -> the line of a follower that flew with its parent
|
|
190
|
+
let t0 = now;
|
|
191
|
+
for (const d of dots.values()) if (d.marked && !lines.some((x) => x.dot === d && x.l.outcome === "stamped")) events.push({ kind: "adopt", t: -Infinity, start: -Infinity, address: d.id, before: true });
|
|
192
|
+
for (const x of lines) {
|
|
193
|
+
const { l, t, dot } = x;
|
|
194
|
+
t0 = Math.min(t0, t);
|
|
195
|
+
if (l.outcome === "skipped") continue;
|
|
196
|
+
if (l.outcome === "stamped") {
|
|
197
|
+
events.push({ kind: "adopt", t: t + 300, start: t, address: dot.id, migration: x.m, at: Date.parse(l.at) || t, line: { outcome: "stamped", address: l.address, from: l.rawFrom, to: l.into, at: l.at } });
|
|
198
|
+
if (l.estate_to === l.estate_from) continue;
|
|
199
|
+
}
|
|
200
|
+
const ok = l.outcome !== "refused";
|
|
201
|
+
const parent = l.follows ? parents.get(dot.id) : undefined;
|
|
202
|
+
const pw = parent && writeOf.get(`${x.m}|${parent}`);
|
|
203
|
+
if (pw && ok && pw.ok) {
|
|
204
|
+
pw.followers.push(dot.id);
|
|
205
|
+
// A follower's own line, for its provenance: it moved with its parent's write.
|
|
206
|
+
followerLines.set(dot.id, { outcome: l.outcome, address: l.address, from: l.rawFrom, to: l.into, at: l.at, migration: x.m, start: t, follows: l.follows });
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const w = { kind: "write", t: t + 1, start: t, address: dot.id, followers: [], from: l.estate_from, to: l.estate_to, ok, migration: x.m, at: Date.parse(l.at) || t, line: { outcome: l.outcome, address: l.address, from: l.rawFrom, to: l.into, at: l.at, reason: l.reason || null }, ...(ok ? {} : { refusal: "refused", refusalSummary: l.reason || "the move was refused" }) };
|
|
210
|
+
writeOf.set(`${x.m}|${dot.id}`, w);
|
|
211
|
+
events.push(w);
|
|
212
|
+
}
|
|
213
|
+
t0 = start !== null && Number.isFinite(start) ? Math.min(start, t0 - LIVE.lead) : Math.min(t0, now) - LIVE.lead;
|
|
214
|
+
for (const ev of events) if (ev.before) ev.t = ev.start = t0;
|
|
215
|
+
events.sort((a, b) => a.start - b.start);
|
|
216
|
+
|
|
217
|
+
const ownersEnd = new Map(owners0);
|
|
218
|
+
for (const ev of events) {
|
|
219
|
+
if (ev.kind !== "write" || !ev.ok) continue;
|
|
220
|
+
ownersEnd.set(ev.address, ev.to);
|
|
221
|
+
for (const f of ev.followers) ownersEnd.set(f, ev.to);
|
|
222
|
+
}
|
|
223
|
+
for (const [child, parent] of parents) if (!events.some((e) => e.address === child)) ownersEnd.set(child, ownersEnd.get(parent) ?? ownersEnd.get(child));
|
|
224
|
+
|
|
225
|
+
// The box's new name plays with the first stamp into the estate; with none, it has the estate's name already.
|
|
226
|
+
const renames = new Map();
|
|
227
|
+
for (const [to, from] of alias) {
|
|
228
|
+
const stamps = lines.filter((x) => x.l.outcome === "stamped" && x.l.into === to).map((x) => x.t);
|
|
229
|
+
renames.set(from, { to, at: stamps.length ? Math.min(...stamps) : -Infinity });
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const roster = [...dots.values()].map((d) => ({ address: d.id, type: d.type, taggable: d.taggable, ...(d.live_id ? { live_id: d.live_id } : {}) }));
|
|
233
|
+
const names = [...new Set([source, ...estates.map((e) => e.estate), ...ownersEnd.values()])];
|
|
234
|
+
return {
|
|
235
|
+
kind: "live",
|
|
236
|
+
title: source,
|
|
237
|
+
recordedAt: null,
|
|
238
|
+
source,
|
|
239
|
+
estates: names,
|
|
240
|
+
roster,
|
|
241
|
+
owners0,
|
|
242
|
+
ownersEnd,
|
|
243
|
+
events,
|
|
244
|
+
threads: [],
|
|
245
|
+
phases: [],
|
|
246
|
+
parents,
|
|
247
|
+
t0,
|
|
248
|
+
t1: now,
|
|
249
|
+
spaced: false,
|
|
250
|
+
renames,
|
|
251
|
+
followerLines,
|
|
252
|
+
counts: Object.fromEntries(estates.map((e) => [e.estate, e.count])),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The write that landed a dot by show time `t`, as provenance reads it: the
|
|
258
|
+
* newest move, refusal or stamp of that dot that has played, with the
|
|
259
|
+
* migration that wrote it. Null when no line of this cycle names the dot.
|
|
260
|
+
*/
|
|
261
|
+
export function landedWrite(show, id, t) {
|
|
262
|
+
let best = null;
|
|
263
|
+
for (const ev of (show && show.events) || []) {
|
|
264
|
+
if (!ev.line || ev.before || ev.start > t) continue;
|
|
265
|
+
const mine = ev.address === id;
|
|
266
|
+
if (!mine) continue;
|
|
267
|
+
if (!best || ev.start >= best.start) best = { ...ev.line, migration: ev.migration, start: ev.start };
|
|
268
|
+
}
|
|
269
|
+
const f = show && show.followerLines ? show.followerLines.get(id) : null;
|
|
270
|
+
if (f && f.start <= t && (!best || f.start >= best.start)) best = f;
|
|
271
|
+
return best;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** What changed between two shows, so the layer rebuilds only when it must. */
|
|
275
|
+
export function showKey(show) {
|
|
276
|
+
const last = show.events.length ? show.events[show.events.length - 1] : null;
|
|
277
|
+
const ends = new Map();
|
|
278
|
+
for (const e of show.ownersEnd.values()) ends.set(e, (ends.get(e) ?? 0) + 1);
|
|
279
|
+
return [show.source, show.roster.length, show.events.length, last ? `${last.address}@${last.start}` : "", [...ends].sort().join(",")].join("|");
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The newest migration that is running, else the newest with lines; null when none. */
|
|
283
|
+
export function currentMigration(answer) {
|
|
284
|
+
const ms = (answer && answer.migrations) || [];
|
|
285
|
+
const newest = (m) => m.lines.reduce((a, l) => Math.max(a, Date.parse(l.at) || 0), 0);
|
|
286
|
+
const running = ms.filter((m) => m.running).sort((a, b) => newest(b) - newest(a));
|
|
287
|
+
if (running.length) return running[0];
|
|
288
|
+
return ms.filter((m) => m.lines.length).sort((a, b) => newest(b) - newest(a))[0] || null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Whether the layer has anything to draw: the gate is open and some roster or line names a resource. */
|
|
292
|
+
export function carveDrawable(answer) {
|
|
293
|
+
if (!answer || answer.live !== true) return false;
|
|
294
|
+
return (answer.estates || []).some((e) => e.resources.length > 0) || (answer.migrations || []).some((m) => m.lines.length > 0);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* A migration's counts as the page has played them by show time `t`: each
|
|
299
|
+
* line counts once its paced time has come. While lines are still to play,
|
|
300
|
+
* the counts are those lines' and the rest are left; once all have played,
|
|
301
|
+
* the record's own counts. `playing` says which.
|
|
302
|
+
*/
|
|
303
|
+
export function playedCounts(migration, seen, t) {
|
|
304
|
+
const c = { moved: 0, stamped: 0, refused: 0, skipped: 0 };
|
|
305
|
+
let n = 0;
|
|
306
|
+
for (const l of migration.lines) {
|
|
307
|
+
const at = seen.get(lineKey(migration.name, l)) ?? (Date.parse(l.at) || 0);
|
|
308
|
+
if (at > t) continue;
|
|
309
|
+
c[l.outcome] = (c[l.outcome] ?? 0) + 1;
|
|
310
|
+
n++;
|
|
311
|
+
}
|
|
312
|
+
if (n === migration.lines.length) return { counts: migration.counts, playing: false };
|
|
313
|
+
const r = migration.counts;
|
|
314
|
+
const total = r.moved + r.stamped + r.refused + r.skipped + (r.left ?? 0);
|
|
315
|
+
return { counts: { ...c, left: Math.max(0, Math.max(total, migration.lines.length) - n) }, playing: true };
|
|
316
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// #715: the dots layer's live driver. The route's answer (src/terragucci-carve.ts)
|
|
2
|
+
// becomes a show: one dot per resource, keyed by live id; a stamp leaves a
|
|
3
|
+
// ring, a move flies, a refusal bounces; lines play from when they were seen.
|
|
4
|
+
import { describe, it, expect } from "vitest";
|
|
5
|
+
import { liveShow, stampArrivals, lineKey, showKey, currentMigration, carveDrawable, boxName, LIVE } from "./dots-live.js";
|
|
6
|
+
import { buildScene, takeoffsBy } from "./dots-scene.js";
|
|
7
|
+
import { liveBeat, liveTipsBetween, liveEndTip } from "./dots-kiosk.js";
|
|
8
|
+
import { dotsCarving, dotsActive } from "./dots.js";
|
|
9
|
+
|
|
10
|
+
const T = Date.parse("2026-10-16T10:00:00Z");
|
|
11
|
+
const at = (s) => new Date(T + s * 1000).toISOString();
|
|
12
|
+
|
|
13
|
+
// A stock terralith (addresses only), adopted (stamped), then split: two moves and a refusal.
|
|
14
|
+
const stock = {
|
|
15
|
+
estate: "terralith",
|
|
16
|
+
at: at(-60),
|
|
17
|
+
count: 4,
|
|
18
|
+
resources: [
|
|
19
|
+
{ address: "module.team_a.aws_iam_role.app", type: "aws_iam_role", marked: false },
|
|
20
|
+
{ address: "module.team_a.aws_iam_role_policy.app", type: "aws_iam_role_policy", marked: false, follows: "module.team_a.aws_iam_role.app" },
|
|
21
|
+
{ address: "module.team_b.aws_iam_role.app", type: "aws_iam_role", marked: false },
|
|
22
|
+
{ address: "aws_vpc.main", type: "aws_vpc", marked: false },
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
const adopt = {
|
|
26
|
+
name: "adopt",
|
|
27
|
+
running: false,
|
|
28
|
+
counts: { moved: 0, stamped: 3, refused: 0, skipped: 1, left: 0 },
|
|
29
|
+
lines: [
|
|
30
|
+
{ at: at(0), estate_from: "terralith", estate_to: "terralith", address: "module.team_a.aws_iam_role.app", live_id: "role-a", outcome: "stamped" },
|
|
31
|
+
{ at: at(1), estate_from: "terralith", estate_to: "terralith", address: "module.team_b.aws_iam_role.app", live_id: "role-b", outcome: "stamped" },
|
|
32
|
+
{ at: at(2), estate_from: "terralith", estate_to: "terralith", address: "aws_vpc.main", live_id: "vpc-1", outcome: "stamped" },
|
|
33
|
+
{ at: at(3), estate_from: "terralith", estate_to: "terralith", address: "module.team_a.aws_iam_role_policy.app", outcome: "skipped" },
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
const split = (lines) => ({ name: "carve", running: true, counts: { moved: 1, stamped: 0, refused: lines.length > 2 ? 1 : 0, skipped: 0, left: 1 }, lines });
|
|
37
|
+
const moveA = { at: at(10), estate_from: "terralith", estate_to: "team-a", address: "module.team_a.aws_iam_role.app", live_id: "role-a", outcome: "moved" };
|
|
38
|
+
const followA = { at: at(10), estate_from: "terralith", estate_to: "team-a", address: "module.team_a.aws_iam_role_policy.app", outcome: "moved", follows: "module.team_a.aws_iam_role.app" };
|
|
39
|
+
const refuseB = { at: at(11), estate_from: "terralith", estate_to: "team-b", address: "module.team_b.aws_iam_role.app", live_id: "role-b", outcome: "refused", reason: "Unexpected changes in the marker rewrite" };
|
|
40
|
+
const answer = (migrations, estates = [stock]) => ({ live: true, source: "/tmp/reports", estates, migrations, demo: { title: "Carving the terralith", next: "carve: 1 of 2 moved" }, running: migrations.some((m) => m.running) });
|
|
41
|
+
|
|
42
|
+
describe("the live carve as a show (#715)", () => {
|
|
43
|
+
it("keys a dot by live id once a stamp names it, and plays stock, adopt, split in order", () => {
|
|
44
|
+
const a = answer([adopt, split([moveA, followA, refuseB])]);
|
|
45
|
+
const seen = stampArrivals(a, new Map(), T + 20_000, { first: true });
|
|
46
|
+
const show = liveShow(a, seen, T + 20_000);
|
|
47
|
+
expect(show.kind).toBe("live");
|
|
48
|
+
expect(show.source).toBe("terralith");
|
|
49
|
+
expect(show.roster).toHaveLength(4);
|
|
50
|
+
expect([...show.owners0.values()].every((e) => e === "terralith")).toBe(true);
|
|
51
|
+
expect(show.ownersEnd.get("module.team_a.aws_iam_role.app")).toBe("team-a");
|
|
52
|
+
// The policy follows its role.
|
|
53
|
+
expect(show.ownersEnd.get("module.team_a.aws_iam_role_policy.app")).toBe("team-a");
|
|
54
|
+
// The refused move leaves it where it was.
|
|
55
|
+
expect(show.ownersEnd.get("module.team_b.aws_iam_role.app")).toBe("terralith");
|
|
56
|
+
expect(show.events.filter((e) => e.kind === "adopt")).toHaveLength(3);
|
|
57
|
+
const writes = show.events.filter((e) => e.kind === "write");
|
|
58
|
+
expect(writes.map((w) => w.ok)).toEqual([true, false]);
|
|
59
|
+
expect(writes[0].followers).toEqual(["module.team_a.aws_iam_role_policy.app"]);
|
|
60
|
+
expect(writes[1].refusalSummary).toMatch(/marker rewrite/);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("joins a destination roster's resource to its dot by live id, not as a second dot", () => {
|
|
64
|
+
const teamA = { estate: "team-a", at: at(12), count: 1, resources: [{ address: "aws_iam_role.app", type: "aws_iam_role", live_id: "role-a", marked: true }] };
|
|
65
|
+
const a = answer([adopt, split([moveA])], [stock, teamA]);
|
|
66
|
+
const show = liveShow(a, stampArrivals(a, new Map(), T + 20_000, { first: true }), T + 20_000);
|
|
67
|
+
expect(show.roster).toHaveLength(4);
|
|
68
|
+
expect(show.estates).toContain("team-a");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("draws the first read's lines where they ended, and flies a later line from when it was seen", () => {
|
|
72
|
+
const first = answer([adopt]);
|
|
73
|
+
const seen0 = stampArrivals(first, new Map(), T + 5_000, { first: true });
|
|
74
|
+
expect(seen0.get(lineKey("adopt", adopt.lines[0]))).toBe(T);
|
|
75
|
+
const later = answer([adopt, split([moveA, refuseB])]);
|
|
76
|
+
const now = T + 30_000;
|
|
77
|
+
const seen1 = stampArrivals(later, seen0, now);
|
|
78
|
+
expect(seen1.get(lineKey("carve", moveA))).toBe(now);
|
|
79
|
+
// Real time: a line of the same read is drawn when it is read, with no pacing.
|
|
80
|
+
expect(seen1.get(lineKey("carve", refuseB))).toBe(now);
|
|
81
|
+
const show0 = liveShow(first, seen0, T + 5_000);
|
|
82
|
+
const show1 = liveShow(later, seen1, now, { t0: show0.t0 });
|
|
83
|
+
expect(show1.t0).toBe(show0.t0);
|
|
84
|
+
expect(showKey(show1)).not.toBe(showKey(show0));
|
|
85
|
+
const scene = buildScene(show1);
|
|
86
|
+
const flight = scene.flights.find((f) => f.address === "module.team_a.aws_iam_role.app");
|
|
87
|
+
// One to one: the flight starts at its arrival, in the show's own clock.
|
|
88
|
+
expect(flight.start).toBe(now - show1.t0);
|
|
89
|
+
expect(takeoffsBy(scene, now - show1.t0 - 1)).toBe(0);
|
|
90
|
+
// The role and its policy, which follows it, take off together.
|
|
91
|
+
expect(takeoffsBy(scene, now - show1.t0 + 1)).toBe(2);
|
|
92
|
+
expect(scene.flights.find((f) => f.address === "module.team_a.aws_iam_role_policy.app").parent).toBe("module.team_a.aws_iam_role.app");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("says what is happening from demo.json and the newest line, and tips the first landing and refusal", () => {
|
|
96
|
+
const a = answer([adopt, split([moveA, followA, refuseB])]);
|
|
97
|
+
const seen = stampArrivals(a, new Map(), T, { first: true });
|
|
98
|
+
const show = liveShow(a, seen, T + 20_000);
|
|
99
|
+
const scene = buildScene(show);
|
|
100
|
+
const m = currentMigration(a);
|
|
101
|
+
expect(m.name).toBe("carve");
|
|
102
|
+
const mid = liveBeat(scene, T + 11_500 - show.t0, { demo: a.demo, migration: m });
|
|
103
|
+
expect(mid.title).toBe("Carving the terralith");
|
|
104
|
+
expect(mid.next.text).toBe("carve: 1 of 2 moved");
|
|
105
|
+
expect(mid.line.tone).toBe("bad");
|
|
106
|
+
const tips = liveTipsBetween(scene, -1, T + 20_000 - show.t0);
|
|
107
|
+
expect(tips.map((t) => t.key).sort()).toEqual(["land:team-a", "refused"]);
|
|
108
|
+
const done = liveBeat(scene, T + 20_000 - show.t0, { demo: null, migration: { ...m, running: false } });
|
|
109
|
+
expect(done.phase).toBe("settled");
|
|
110
|
+
expect(done.focus).toEqual({ kind: "all" });
|
|
111
|
+
expect(liveEndTip(m, 0).key).toBe("end:carve");
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("the gate, in the page (#715)", () => {
|
|
116
|
+
it("draws nothing for a shut gate or an empty carve", () => {
|
|
117
|
+
expect(carveDrawable(null)).toBe(false);
|
|
118
|
+
expect(carveDrawable({ live: false, reason: "no choudoufu roots" })).toBe(false);
|
|
119
|
+
expect(carveDrawable({ live: true, estates: [], migrations: [] })).toBe(false);
|
|
120
|
+
expect(carveDrawable(answer([], [stock]))).toBe(true);
|
|
121
|
+
});
|
|
122
|
+
it("leaves the layer unmounted, with no director and no zoom limit of its own", () => {
|
|
123
|
+
expect(dotsActive()).toBe(false);
|
|
124
|
+
expect(dotsCarving()).toBe(false);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// The live local run's cycle 5: the adoption's 82 lines came in one read, all
|
|
129
|
+
// within a second. The stock root and the estate it became are one box.
|
|
130
|
+
describe("an adoption as one box changing its name", () => {
|
|
131
|
+
const N = 82;
|
|
132
|
+
const addr = (i) => `aws_iam_role.r${String(i).padStart(2, "0")}`;
|
|
133
|
+
const stockRoster = { estate: "terralith", at: at(-120), count: N, resources: Array.from({ length: N }, (_, i) => ({ address: addr(i), type: "aws_iam_role", marked: false })) };
|
|
134
|
+
// As the route gives them: a stamp's empty estate_from reads as the estate it is stamped into.
|
|
135
|
+
const stamps = Array.from({ length: N }, (_, i) => ({ at: at(1), estate_from: "tl5-terralith", estate_to: "tl5-terralith", address: addr(i), ...(i % 2 ? {} : { live_id: `role-${i}` }), outcome: i % 2 ? "skipped" : "stamped" }));
|
|
136
|
+
const adoptM = { name: "adopt", running: true, counts: { moved: 0, stamped: 41, refused: 0, skipped: 41, left: 0 }, lines: stamps };
|
|
137
|
+
const mid = { ...answer([adoptM], [stockRoster]), adopted: [{ from: "terralith", to: "tl5-terralith" }] };
|
|
138
|
+
|
|
139
|
+
it("draws a batch of lines in real time: the whole read at once, as the burst it was", () => {
|
|
140
|
+
const now = T + 10_000;
|
|
141
|
+
const seen = stampArrivals(mid, new Map(), now);
|
|
142
|
+
const times = stamps.map((l) => seen.get(lineKey("adopt", l)));
|
|
143
|
+
expect(LIVE.stepMs).toBe(0);
|
|
144
|
+
expect(new Set(times)).toEqual(new Set([now]));
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("slowed, paces a batch stepMs apart, after any still waiting to play", () => {
|
|
148
|
+
const now = T + 10_000;
|
|
149
|
+
const stepMs = 50;
|
|
150
|
+
const seen = stampArrivals(mid, new Map(), now, { stepMs });
|
|
151
|
+
const times = stamps.map((l) => seen.get(lineKey("adopt", l))).sort((a, b) => a - b);
|
|
152
|
+
expect(times[0]).toBe(now);
|
|
153
|
+
expect(times[N - 1]).toBe(now + (N - 1) * stepMs);
|
|
154
|
+
// A line of the next read waits behind the batch.
|
|
155
|
+
const more = { ...mid, migrations: [{ ...adoptM, lines: [...stamps, { ...stamps[0], at: at(2), address: "aws_s3_bucket.late" }] }] };
|
|
156
|
+
const next = stampArrivals(more, seen, now + 500, { stepMs });
|
|
157
|
+
expect(next.get(lineKey("adopt", { ...stamps[0], at: at(2), address: "aws_s3_bucket.late" }))).toBe(now + N * stepMs);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("rings the stock root's dots in place, in one box that takes the estate's name with the first stamp", () => {
|
|
161
|
+
const now = T + 10_000;
|
|
162
|
+
const seen = stampArrivals(mid, new Map(), now);
|
|
163
|
+
const show = liveShow(mid, seen, now + 1);
|
|
164
|
+
expect(show.roster).toHaveLength(N);
|
|
165
|
+
expect(show.estates).toEqual(["terralith"]);
|
|
166
|
+
expect(show.events.filter((e) => e.kind === "write")).toHaveLength(0);
|
|
167
|
+
expect(show.events.filter((e) => e.kind === "adopt")).toHaveLength(41);
|
|
168
|
+
expect(boxName(show, "terralith", now - 1)).toBe("terralith");
|
|
169
|
+
expect(boxName(show, "terralith", now)).toBe("tl5-terralith");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("after the adoption, draws the estate's roster in the same box, with the same dots", () => {
|
|
173
|
+
const estate = { estate: "tl5-terralith", at: at(12), count: N, resources: stockRoster.resources.map((r, i) => ({ ...r, ...(i % 2 ? {} : { live_id: `role-${i}` }), marked: i % 2 === 0 })) };
|
|
174
|
+
const done = { ...answer([{ ...adoptM, running: false }], [estate]), adopted: [{ from: "terralith", to: "tl5-terralith" }] };
|
|
175
|
+
// A page loaded after it: the lines are the past, the box has the estate's name.
|
|
176
|
+
const show = liveShow(done, stampArrivals(done, new Map(), T + 60_000, { first: true }), T + 60_000);
|
|
177
|
+
expect(show.roster).toHaveLength(N);
|
|
178
|
+
expect(show.estates).toEqual(["terralith"]);
|
|
179
|
+
expect(boxName(show, "terralith", T + 60_000)).toBe("tl5-terralith");
|
|
180
|
+
expect([...show.ownersEnd.values()].every((e) => e === "terralith")).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// The live run's cycle 8: after the split, wave 1 writes each team's roster from its plan, with no live ids, so the
|
|
185
|
+
// team's resources are named by address only. They are the dots the split's lines moved there, not a second set.
|
|
186
|
+
describe("a destination roster written after the move", () => {
|
|
187
|
+
it("joins the moved dots by address, so the team's box holds each resource once", () => {
|
|
188
|
+
const left = { estate: "tl8-terralith", at: at(30), count: 1, resources: [{ address: "aws_vpc.main", type: "aws_vpc", live_id: "vpc-1", marked: true }] };
|
|
189
|
+
const team = { estate: "tl8-team-a", at: at(40), count: 3, resources: ["aws_iam_role.r", "aws_iam_role_policy.p", "aws_cloudwatch_log_group.l"].map((address) => ({ address, type: address.split(".")[0], marked: false })) };
|
|
190
|
+
const mv = (address, o = {}) => ({ at: at(20), estate_from: "tl8-terralith", estate_to: "tl8-team-a", address, outcome: "moved", ...o });
|
|
191
|
+
const splitM = { name: "split", running: false, counts: { moved: 3, stamped: 0, refused: 0, skipped: 0, left: 0 }, lines: [mv("aws_iam_role.r", { live_id: "role-r" }), mv("aws_iam_role_policy.p", { follows: "aws_iam_role.r" }), mv("aws_cloudwatch_log_group.l", { live_id: "logs" })] };
|
|
192
|
+
const a = answer([splitM], [left, team]);
|
|
193
|
+
const show = liveShow(a, stampArrivals(a, new Map(), T + 60_000, { first: true }), T + 60_000);
|
|
194
|
+
expect(show.roster).toHaveLength(4);
|
|
195
|
+
const ends = {};
|
|
196
|
+
for (const e of show.ownersEnd.values()) ends[e] = (ends[e] ?? 0) + 1;
|
|
197
|
+
expect(ends).toEqual({ "tl8-team-a": 3, "tl8-terralith": 1 });
|
|
198
|
+
// They start in the terralith, where the lines say they came from.
|
|
199
|
+
expect(show.owners0.get("aws_iam_role.r")).toBe("tl8-terralith");
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// The live run's cycle 11: the adoption's stamps, then the split, then wave 1's team rosters with no live ids. The
|
|
204
|
+
// boxes read 26 for team rosters of 13: the stamps had made each resource's dot and the team roster made another.
|
|
205
|
+
describe("an adoption, a split, and the team rosters written after it", () => {
|
|
206
|
+
it("draws each resource once, so a team's box counts what its roster holds", () => {
|
|
207
|
+
const addrs = ["aws_iam_role.r", "aws_iam_role_policy.p", "aws_cloudwatch_log_group.l"];
|
|
208
|
+
const left = { estate: "tl11-terralith", at: at(30), count: 1, resources: [{ address: "aws_vpc.main", type: "aws_vpc", live_id: "vpc-1", marked: true }] };
|
|
209
|
+
const team = { estate: "tl11-team-a", at: at(40), count: 3, resources: addrs.map((address) => ({ address, type: address.split(".")[0], marked: false })) };
|
|
210
|
+
const ids = { "aws_iam_role.r": "role-r", "aws_iam_role_policy.p": "role-r:p", "aws_cloudwatch_log_group.l": "logs", "aws_vpc.main": "vpc-1" };
|
|
211
|
+
const adoptM = { name: "adopt", running: false, counts: { moved: 0, stamped: 3, refused: 0, skipped: 1, left: 0 }, lines: [...addrs, "aws_vpc.main"].map((address) => ({ at: at(5), estate_from: "tl11-terralith", estate_to: "tl11-terralith", address, live_id: ids[address], outcome: address.includes("policy") ? "skipped" : "stamped" })) };
|
|
212
|
+
const splitM = { name: "split", running: false, counts: { moved: 3, stamped: 0, refused: 0, skipped: 0, left: 0 }, lines: addrs.map((address) => ({ at: at(20), estate_from: "tl11-terralith", estate_to: "tl11-team-a", address, ...(address.includes("policy") ? { follows: "aws_iam_role.r" } : { live_id: ids[address] }), outcome: "moved" })) };
|
|
213
|
+
const a = { ...answer([adoptM, splitM], [left, team]), adopted: [{ from: "terralith", to: "tl11-terralith" }] };
|
|
214
|
+
const show = liveShow(a, stampArrivals(a, new Map(), T + 60_000, { first: true }), T + 60_000);
|
|
215
|
+
expect(show.roster.map((r) => r.address).sort()).toEqual([...addrs, "aws_vpc.main"].sort());
|
|
216
|
+
const ends = {};
|
|
217
|
+
for (const e of show.ownersEnd.values()) ends[e] = (ends[e] ?? 0) + 1;
|
|
218
|
+
expect(ends).toEqual({ "tl11-team-a": 3, terralith: 1 });
|
|
219
|
+
});
|
|
220
|
+
});
|
package/web/dots-lod.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Level of detail for the dots layer (#712, #715): each mark picks its form from
|
|
2
|
+
// how many pixels it covers at the current zoom, and the morph between two
|
|
3
|
+
// forms follows the zoom position, never a clock. Stopping halfway stays
|
|
4
|
+
// halfway; zooming back in runs it backwards. Pure.
|
|
5
|
+
|
|
6
|
+
/** Starting points, in CSS pixels on screen. */
|
|
7
|
+
export const LOD = {
|
|
8
|
+
/** A card at least this wide stays a card. */
|
|
9
|
+
cardPx: 90,
|
|
10
|
+
/** A card narrower than this is a dot. */
|
|
11
|
+
dotPx: 24,
|
|
12
|
+
/** An estate box at least this wide (its larger side) is still all dots. */
|
|
13
|
+
discFromPx: 72,
|
|
14
|
+
/** An estate box smaller than this is one disc. */
|
|
15
|
+
discPx: 48,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** 0 at or above `hi`, 1 at or below `lo`, linear in log(px) between. */
|
|
19
|
+
export function bandT(px, hi, lo) {
|
|
20
|
+
if (!(px > 0)) return 1;
|
|
21
|
+
if (px >= hi) return 0;
|
|
22
|
+
if (px <= lo) return 1;
|
|
23
|
+
return Math.log(hi / px) / Math.log(hi / lo);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Smoothstep, so a morph starts and ends at rest. */
|
|
27
|
+
export function smooth(t) {
|
|
28
|
+
const x = Math.max(0, Math.min(1, t));
|
|
29
|
+
return x * x * (3 - 2 * x);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Card to dot: 0 is a card, 1 a dot. Under reduced motion it switches at the
|
|
34
|
+
* geometric middle of the band, with no in-between.
|
|
35
|
+
*/
|
|
36
|
+
export function cardMorph(cardPx, { reduced = false, lod = LOD } = {}) {
|
|
37
|
+
if (reduced) return cardPx < Math.sqrt(lod.cardPx * lod.dotPx) ? 1 : 0;
|
|
38
|
+
return smooth(bandT(cardPx, lod.cardPx, lod.dotPx));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Dots to disc for one estate box: 0 is dots, 1 a disc. Reduced motion switches at `discPx`. */
|
|
42
|
+
export function discMorph(boxPx, { reduced = false, lod = LOD } = {}) {
|
|
43
|
+
if (reduced) return boxPx < lod.discPx ? 1 : 0;
|
|
44
|
+
return smooth(bandT(boxPx, lod.discFromPx, lod.discPx));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function lerp(a, b, t) {
|
|
48
|
+
return a + (b - a) * t;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A dot between its card's centre, its slot and its estate's disc. */
|
|
52
|
+
export function morphPoint(card, slot, disc, tCard, tDisc) {
|
|
53
|
+
const from = card ?? slot;
|
|
54
|
+
const x = lerp(from.x, slot.x, tCard);
|
|
55
|
+
const y = lerp(from.y, slot.y, tCard);
|
|
56
|
+
return { x: lerp(x, disc.x, tDisc), y: lerp(y, disc.y, tDisc) };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A disc's radius in the same units as `pitch`: its area is the area its
|
|
61
|
+
* resources cover as dots, so area stays proportional to the count.
|
|
62
|
+
*/
|
|
63
|
+
export function discRadius(count, pitch) {
|
|
64
|
+
return Math.sqrt(Math.max(0, count) / Math.PI) * pitch * 1.8;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** What the marks on screen are, from the card morph and every box's disc morph. */
|
|
68
|
+
export function levelOf(tCard, tDiscs) {
|
|
69
|
+
const ds = [...tDiscs];
|
|
70
|
+
const allDisc = ds.length > 0 && ds.every((t) => t >= 1);
|
|
71
|
+
const anyDisc = ds.some((t) => t > 0);
|
|
72
|
+
if (tCard <= 0) return "cards";
|
|
73
|
+
if (tCard < 1) return "cards-dots";
|
|
74
|
+
if (allDisc) return "discs";
|
|
75
|
+
if (anyDisc) return "dots-discs";
|
|
76
|
+
return "dots";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The line naming the marks at a level. */
|
|
80
|
+
export function caption(level) {
|
|
81
|
+
switch (level) {
|
|
82
|
+
case "cards":
|
|
83
|
+
return "each card is a resource";
|
|
84
|
+
case "cards-dots":
|
|
85
|
+
return "cards shrinking to dots: each dot is a resource";
|
|
86
|
+
case "dots":
|
|
87
|
+
return "each dot is a resource, in its estate's box and colour";
|
|
88
|
+
case "dots-discs":
|
|
89
|
+
return "each dot is a resource; each disc is an estate, sized by resources";
|
|
90
|
+
case "discs":
|
|
91
|
+
return "each disc is an estate, sized by resources";
|
|
92
|
+
default:
|
|
93
|
+
return "";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The rail's three marks as viewBox widths, for a pane `paneW` x `paneH` px:
|
|
99
|
+
* Cards puts a card at `cardAt` px; Resources fits the whole scene
|
|
100
|
+
* (`sceneW` x `sceneH` viewBox units) while the smallest estate box is still
|
|
101
|
+
* dots; Estates puts the typical box (default: the largest) under the disc threshold.
|
|
102
|
+
*/
|
|
103
|
+
export function railMarks({ paneW, paneH = paneW, cardW, sceneW, sceneH = 0, smallestBox, largestBox, typicalBox = largestBox, lod = LOD, cardAt = 140 }) {
|
|
104
|
+
const cards = (paneW * cardW) / cardAt;
|
|
105
|
+
let resources = Math.max(sceneW, (sceneH * paneW) / Math.max(1, paneH)) * 1.12;
|
|
106
|
+
// Far enough that the cards have all but become dots, however few there are.
|
|
107
|
+
resources = Math.max(resources, cards * 1.5, (paneW * cardW) / (lod.dotPx * 1.25));
|
|
108
|
+
// Close enough that the smallest box stays dots: that wins over the cards, so
|
|
109
|
+
// a small estate is still dots at this mark even when its cards are large.
|
|
110
|
+
if (smallestBox > 0) resources = Math.min(resources, (smallestBox * paneW) / (lod.discFromPx * 1.1));
|
|
111
|
+
resources = Math.max(resources, cards * 1.2);
|
|
112
|
+
// The typical box just under the disc size: most estates are discs, and a far larger one may still be dots.
|
|
113
|
+
const estates = Math.max((paneW * typicalBox) / (lod.discPx * 0.98), resources * 1.5);
|
|
114
|
+
return { cards, resources, estates };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Where a viewBox width sits on the rail: 0 at Cards, 0.5 at Resources, 1 at Estates (log scale, clamped). */
|
|
118
|
+
export function railPos(vbW, marks) {
|
|
119
|
+
const l = (v) => Math.log(Math.max(1e-9, v));
|
|
120
|
+
if (vbW <= marks.resources) return Math.max(0, Math.min(0.5, (0.5 * (l(vbW) - l(marks.cards))) / (l(marks.resources) - l(marks.cards))));
|
|
121
|
+
return Math.max(0.5, Math.min(1, 0.5 + (0.5 * (l(vbW) - l(marks.resources))) / (l(marks.estates) - l(marks.resources))));
|
|
122
|
+
}
|