watchpot 1.0.0-rc.8 → 1.0.0-rc.9

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/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  Each release is a tag `vX.Y.Z`. A breaking change names its codemod in `codemods/` (policy in `codemods/README.md`).
4
4
 
5
+ ## 1.0.0-rc.9
6
+
7
+ - `watchpot sim`'s knob panel scales to hundreds of knobs: groups fold shut (their headers count knobs and those set by hand, and the desk remembers which are open), a filter matches key, label or group, and `Changed only` shows what is set by hand. A slider reruns on release and a number field on Enter or blur, not on every tick or keystroke; the rerun debounce is 1000 ms.
8
+ - `watchpot sim`'s page has a `Waits` table, read from the median seed: the longest dry spells between purchases and what ended them, waits per buyable (bought, first, median and longest wait) and per window. A purchase is a change in a buyable's next price, so a wait is only as fine as the sample step the table names. Picking a dry spell marks it on both graphs.
9
+
5
10
  ## 1.0.0-rc.8
6
11
 
7
12
  Fix: `bunx watchpot sim` in an installed game ran under Node (bunx honours the bin's node shebang) and refused to start; the bin now hands `sim` to Bun.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "watchpot",
3
- "version": "1.0.0-rc.8",
3
+ "version": "1.0.0-rc.9",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "engines": {
@@ -33,7 +33,8 @@ import {
33
33
  valueAt,
34
34
  yAxis,
35
35
  } from "./series";
36
- import { GateTable, HourTable, type Pin } from "./Tables";
36
+ import { GateTable, HourTable, type Pin, WaitTable } from "./Tables";
37
+ import { waitStats } from "./waits";
37
38
 
38
39
  export interface AppProps {
39
40
  /** Opens the line to the desk server. Default: the WebSocket at `/ws`. A test passes a fake. */
@@ -63,7 +64,7 @@ const TRENDS = [
63
64
  /** Where the viewer's trend choice is kept. */
64
65
  const TREND_KEY = "watchpot-desk-trend";
65
66
  /** How long after the last change to a knob or a pin the pacing reruns. */
66
- const RERUN_MS = 500;
67
+ const RERUN_MS = 1000;
67
68
  const cid = (name: string) => `cur:${name}`;
68
69
  const nid = (key: string) => `next:${key}`;
69
70
 
@@ -97,6 +98,8 @@ export function App({ connect = connectSocket, load = fetchDesk, onSeriesRender
97
98
  // The choice holds for this session; nothing is kept.
98
99
  }
99
100
  };
101
+ // The dry spell picked in the waits table, marked on both graphs; a new run clears it.
102
+ const [spell, setSpell] = useState<{ from: number; to: number }>();
100
103
  const [online, setOnline] = useState(false);
101
104
  const link = useRef<DeskLink>(undefined);
102
105
  const lastId = useRef(0);
@@ -118,17 +121,19 @@ export function App({ connect = connectSocket, load = fetchDesk, onSeriesRender
118
121
  if (!via) return;
119
122
  const id = ++lastId.current;
120
123
  const { knobs, pins } = handRef.current;
124
+ setSpell(undefined);
121
125
  dispatch({ type: "start", id, seeds });
122
126
  via.send({ type: "run", id, knobs: { ...knobs }, pins: structuredClone(pins), seeds });
123
127
  }, []);
124
128
 
125
129
  /** Changes what is set by hand, and reruns once it has been left alone for a moment. */
126
130
  const edit = useCallback(
127
- (change: (h: Hand) => Hand) => {
131
+ (change: (h: Hand) => Hand, rerun = true) => {
128
132
  const next = change(handRef.current);
129
133
  handRef.current = next;
130
134
  setHandState(next);
131
135
  setNote(undefined);
136
+ if (!rerun) return;
132
137
  if (timer.current !== undefined) clearTimeout(timer.current);
133
138
  timer.current = setTimeout(() => play(seedsRef.current), RERUN_MS);
134
139
  },
@@ -205,8 +210,8 @@ export function App({ connect = connectSocket, load = fetchDesk, onSeriesRender
205
210
  });
206
211
  }, []);
207
212
 
208
- const setKnob = (key: string, value: number) =>
209
- edit((h) => ({ ...h, knobs: { ...h.knobs, [key]: value } }));
213
+ const setKnob = (key: string, value: number, commit = true) =>
214
+ edit((h) => ({ ...h, knobs: { ...h.knobs, [key]: value } }), commit);
210
215
  const resetKnob = (key: string) => {
211
216
  const code = desk?.knobs.find((k) => k.key === key)?.code;
212
217
  if (code !== undefined) setKnob(key, code);
@@ -438,6 +443,7 @@ export function App({ connect = connectSocket, load = fetchDesk, onSeriesRender
438
443
  ];
439
444
 
440
445
  const end = desk?.end.hours ?? 0;
446
+ const waits = useMemo(() => (desk ? waitStats(data, desk.windowHours, end) : undefined), [data, desk, end]);
441
447
  const playedH = Math.floor(run.playedMs / HOUR_MS);
442
448
  const phase =
443
449
  run.phase === "running"
@@ -583,6 +589,7 @@ export function App({ connect = connectSocket, load = fetchDesk, onSeriesRender
583
589
  log={log}
584
590
  lines={view?.lines1 ?? []}
585
591
  gates={marks}
592
+ span={spell}
586
593
  stops={view?.stops1 ?? []}
587
594
  trend={trend}
588
595
  readout={readout1}
@@ -599,6 +606,7 @@ export function App({ connect = connectSocket, load = fetchDesk, onSeriesRender
599
606
  log={log}
600
607
  lines={view.lines2}
601
608
  gaps={view.gaps2}
609
+ span={spell}
602
610
  stops={view.stops2}
603
611
  trend={trend}
604
612
  readout={readout2}
@@ -616,6 +624,7 @@ export function App({ connect = connectSocket, load = fetchDesk, onSeriesRender
616
624
  waiting={waiting}
617
625
  onPin={setPin}
618
626
  />
627
+ <WaitTable stats={waits} waiting={waiting} selected={spell} onSelect={setSpell} />
619
628
  </div>
620
629
  </main>
621
630
  {desk && (
@@ -80,6 +80,8 @@ export interface GraphProps {
80
80
  /** Hatched shapes in `[hours, y]`. */
81
81
  gaps?: readonly (readonly (readonly [number, number])[])[];
82
82
  gates?: readonly GateMark[];
83
+ /** A span of hours drawn as a translucent band behind the lines (a selected dry spell). */
84
+ span?: { from: number; to: number };
83
85
  /** The hours a crosshair can sit at, ascending. */
84
86
  stops: readonly number[];
85
87
  /** The trend window in hours: 0 (the default) draws the lines as they are. */
@@ -389,6 +391,7 @@ export function Graph(props: GraphProps) {
389
391
  lines,
390
392
  gaps,
391
393
  gates,
394
+ span: band,
392
395
  stops,
393
396
  trend = 0,
394
397
  readout,
@@ -587,6 +590,15 @@ export function Graph(props: GraphProps) {
587
590
  <line className="graph__axis" x1={left} x2={left + plotW} y1={bottom} y2={bottom} />
588
591
 
589
592
  <g clipPath={`url(#${uid}-clip)`}>
593
+ {band && (
594
+ <rect
595
+ className="graph__span"
596
+ x={xOf(band.from)}
597
+ y={top}
598
+ width={Math.max(1, xOf(band.to) - xOf(band.from))}
599
+ height={plotH}
600
+ />
601
+ )}
590
602
  <Series
591
603
  uid={uid}
592
604
  lines={lines}
@@ -1,6 +1,6 @@
1
1
  // The knob panel: the game's knobs in groups, a slider and a number field each, what is set by hand marked and one
2
2
  // click from being put back; Save writes the lot to the game's tuning file. Pins live in the gates table (Tables.tsx).
3
- import { type CSSProperties, type KeyboardEvent, useState } from "react";
3
+ import { type CSSProperties, type KeyboardEvent, useId, useState } from "react";
4
4
  import type { DeskInfo, DeskKnob } from "../protocol";
5
5
  import { clock, knobText, parseClock } from "./series";
6
6
 
@@ -34,7 +34,7 @@ function KnobRow({
34
34
  }: {
35
35
  knob: DeskKnob;
36
36
  value: number;
37
- onChange: (v: number) => void;
37
+ onChange: (v: number, commit: boolean) => void;
38
38
  onReset: () => void;
39
39
  }) {
40
40
  const [draft, setDraft] = useState<string>();
@@ -65,7 +65,7 @@ function KnobRow({
65
65
  const nudge = (dir: 1 | -1) => {
66
66
  const by = step ?? (ranged ? (max - min) / 100 : 1);
67
67
  const next = Number((value + dir * by).toFixed(Math.min(12, places(by))));
68
- onChange(Math.min(max ?? next, Math.max(min ?? next, next)));
68
+ onChange(Math.min(max ?? next, Math.max(min ?? next, next)), true);
69
69
  };
70
70
  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {
71
71
  const dir =
@@ -78,6 +78,15 @@ function KnobRow({
78
78
  e.preventDefault();
79
79
  nudge(dir);
80
80
  };
81
+ // Keys that move the thumb without being an arrow (Home, End, the page keys) commit on release.
82
+ const onKeyUp = (e: KeyboardEvent<HTMLInputElement>) => {
83
+ if (["Home", "End", "PageUp", "PageDown"].includes(e.key)) onChange(Number(e.currentTarget.value), true);
84
+ };
85
+ const commitDraft = () => {
86
+ const n = draft === undefined ? undefined : parsed(draft);
87
+ setDraft(undefined);
88
+ if (n !== undefined) onChange(n, true);
89
+ };
81
90
 
82
91
  return (
83
92
  <li className="knob" data-hand={hand ? "true" : undefined}>
@@ -100,8 +109,10 @@ function KnobRow({
100
109
  max={max}
101
110
  step={step ?? "any"}
102
111
  value={value}
103
- onChange={(e) => onChange(Number(e.target.value))}
112
+ onChange={(e) => onChange(Number(e.target.value), false)}
113
+ onPointerUp={(e) => onChange(Number(e.currentTarget.value), true)}
104
114
  onKeyDown={onKey}
115
+ onKeyUp={onKeyUp}
105
116
  />
106
117
  )}
107
118
  <span className="knob__field">
@@ -121,12 +132,12 @@ function KnobRow({
121
132
  max={clocked || max === undefined ? undefined : clean(max * scale)}
122
133
  step={clocked ? undefined : step === undefined ? "any" : clean(step * scale)}
123
134
  value={typed}
124
- onChange={(e) => {
125
- setDraft(e.target.value);
126
- const n = parsed(e.target.value);
127
- if (n !== undefined) onChange(n);
135
+ onChange={(e) => setDraft(e.target.value)}
136
+ onKeyDown={(e) => {
137
+ if (e.key === "Enter") commitDraft();
138
+ else if (e.key === "Escape") setDraft(undefined);
128
139
  }}
129
- onBlur={() => setDraft(undefined)}
140
+ onBlur={commitDraft}
130
141
  />
131
142
  {unit === "%" && (
132
143
  <span className="knob__unit" aria-hidden="true">
@@ -169,20 +180,63 @@ export interface PanelProps {
169
180
  saving: boolean;
170
181
  /** The strip's line after a save: what was written, or what went wrong. */
171
182
  note?: { kind: "saved" | "error"; text: string };
172
- onKnob: (key: string, value: number) => void;
183
+ /** `commit` is false while a slider is dragged and true once the value is settled: only then is a rerun worth it. */
184
+ onKnob: (key: string, value: number, commit: boolean) => void;
173
185
  onReset: (key: string) => void;
174
186
  onSave: () => void;
175
187
  onRevert: () => void;
176
188
  }
177
189
 
190
+ const OPEN_KEY = "watchpot-desk-open-groups";
191
+
192
+ const readOpen = (): Set<string> => {
193
+ try {
194
+ const v: unknown = JSON.parse(localStorage.getItem(OPEN_KEY) ?? "[]");
195
+ return new Set(Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : []);
196
+ } catch {
197
+ return new Set();
198
+ }
199
+ };
200
+
201
+ const writeOpen = (open: ReadonlySet<string>) => {
202
+ try {
203
+ localStorage.setItem(OPEN_KEY, JSON.stringify([...open]));
204
+ } catch {
205
+ // No storage: the panel still works, it just forgets.
206
+ }
207
+ };
208
+
178
209
  export function Panel(p: PanelProps) {
179
- const groups: { name: string; knobs: DeskKnob[] }[] = [];
210
+ const [open, setOpen] = useState(readOpen);
211
+ const [query, setQuery] = useState("");
212
+ const [changed, setChanged] = useState(false);
213
+ const listId = useId();
214
+ const toggle = (name: string) =>
215
+ setOpen((was) => {
216
+ const next = new Set(was);
217
+ if (next.has(name)) next.delete(name);
218
+ else next.add(name);
219
+ writeOpen(next);
220
+ return next;
221
+ });
222
+ const value = (k: DeskKnob) => p.values[k.key] ?? k.code;
223
+ const byHand = (k: DeskKnob) => value(k) !== k.code;
224
+ const groups: { name: string; plain: boolean; knobs: DeskKnob[] }[] = [];
180
225
  for (const k of p.knobs) {
181
226
  const name = k.group ?? "Knobs";
182
227
  const found = groups.find((g) => g.name === name);
183
228
  if (found) found.knobs.push(k);
184
- else groups.push({ name, knobs: [k] });
229
+ else groups.push({ name, plain: k.group === undefined, knobs: [k] });
185
230
  }
231
+ const needle = query.trim().toLowerCase();
232
+ const filtering = needle !== "" || changed;
233
+ const matches = (k: DeskKnob) =>
234
+ (!changed || byHand(k)) &&
235
+ (needle === "" || [k.key, k.label ?? "", k.group ?? ""].some((t) => t.toLowerCase().includes(needle)));
236
+ const shown = groups
237
+ .map((g) => ({ ...g, all: g.knobs, knobs: filtering ? g.knobs.filter(matches) : g.knobs }))
238
+ .filter((g) => g.knobs.length > 0);
239
+ const single = groups.length === 1 && groups[0]?.plain === true;
186
240
  return (
187
241
  <aside className="panel" aria-label="Knobs">
188
242
  <div className="panel__stick">
@@ -209,28 +263,72 @@ export function Panel(p: PanelProps) {
209
263
  </p>
210
264
  )}
211
265
  </div>
266
+ {groups.length > 0 && (
267
+ <div className="panel__filter">
268
+ <input
269
+ className="field panel__search"
270
+ type="search"
271
+ aria-label="Filter knobs"
272
+ placeholder="Filter knobs"
273
+ value={query}
274
+ onChange={(e) => setQuery(e.target.value)}
275
+ />
276
+ <button
277
+ type="button"
278
+ className="control"
279
+ aria-pressed={changed}
280
+ onClick={() => setChanged((c) => !c)}
281
+ >
282
+ Changed only
283
+ </button>
284
+ </div>
285
+ )}
212
286
  {groups.length === 0 ? (
213
287
  <p className="empty">
214
288
  This game names no knobs. Name the numbers worth turning with <code>defineKnobs</code> and each
215
289
  gets a slider here.
216
290
  </p>
291
+ ) : shown.length === 0 ? (
292
+ <p className="empty panel__none">No knobs match.</p>
217
293
  ) : (
218
- groups.map((g) => (
219
- <section className="knobs" key={g.name} aria-label={g.name}>
220
- <h3 className="knobs__head lettering">{g.name}</h3>
221
- <ul className="knobs__list">
222
- {g.knobs.map((k) => (
223
- <KnobRow
224
- key={k.key}
225
- knob={k}
226
- value={p.values[k.key] ?? k.code}
227
- onChange={(v) => p.onKnob(k.key, v)}
228
- onReset={() => p.onReset(k.key)}
229
- />
230
- ))}
231
- </ul>
232
- </section>
233
- ))
294
+ shown.map((g) => {
295
+ const collapsible = !(single && g.plain);
296
+ const isOpen = !collapsible || filtering || open.has(g.name);
297
+ const hands = g.all.filter(byHand).length;
298
+ const id = `${listId}-${g.name.replace(/\s+/g, "-")}`;
299
+ return (
300
+ <section className="knobs" key={g.name} aria-label={g.name}>
301
+ {collapsible ? (
302
+ <h3 className="knobs__head lettering">
303
+ <button
304
+ type="button"
305
+ className="knobs__toggle"
306
+ aria-expanded={isOpen}
307
+ aria-controls={id}
308
+ onClick={() => toggle(g.name)}
309
+ >
310
+ {`${g.name} · ${g.all.length} knobs${hands > 0 ? ` · ${hands} by hand` : ""}`}
311
+ </button>
312
+ </h3>
313
+ ) : (
314
+ <h3 className="knobs__head lettering">{g.name}</h3>
315
+ )}
316
+ {isOpen && (
317
+ <ul className="knobs__list" id={id}>
318
+ {g.knobs.map((k) => (
319
+ <KnobRow
320
+ key={k.key}
321
+ knob={k}
322
+ value={value(k)}
323
+ onChange={(v, commit) => p.onKnob(k.key, v, commit)}
324
+ onReset={() => p.onReset(k.key)}
325
+ />
326
+ ))}
327
+ </ul>
328
+ )}
329
+ </section>
330
+ );
331
+ })
234
332
  )}
235
333
  </div>
236
334
  </div>
@@ -16,6 +16,7 @@ import {
16
16
  parsePrice,
17
17
  percentChange,
18
18
  } from "./series";
19
+ import type { WaitStats } from "./waits";
19
20
 
20
21
  export type Pins = NonNullable<DeskInfo["pins"]>;
21
22
  export type Pin = Pins[string];
@@ -284,3 +285,149 @@ export function GateTable({
284
285
  </section>
285
286
  );
286
287
  }
288
+
289
+ const KEY_LIMIT = 20;
290
+
291
+ /** The waits between upgrades, for the median seed: the longest dry spells (each selectable, to mark it on the
292
+ * graphs), a row per upgrade, and a row per window. */
293
+ export const WaitTable = memo(function WaitTable({
294
+ stats,
295
+ waiting,
296
+ selected,
297
+ onSelect,
298
+ }: {
299
+ stats: WaitStats | undefined;
300
+ waiting: boolean;
301
+ selected?: { from: number; to: number };
302
+ onSelect: (spell: { from: number; to: number } | undefined) => void;
303
+ }) {
304
+ const [all, setAll] = useState(false);
305
+ const keys = stats ? (all ? stats.keys : stats.keys.slice(0, KEY_LIMIT)) : [];
306
+ const wait = (h: number | undefined) => (h === undefined ? "–" : hoursMinutes(h));
307
+ return (
308
+ <section className="plate sheet">
309
+ <div className="plate__head">
310
+ <div className="graph__titlerow">
311
+ <h2 className="lettering">Waits</h2>
312
+ {stats && (
313
+ <p className="graph__caption">
314
+ seed {stats.seed} · samples every {hoursMinutes(stats.step)}
315
+ </p>
316
+ )}
317
+ </div>
318
+ </div>
319
+ {!stats ? (
320
+ <p className="empty">
321
+ {waiting
322
+ ? "Waiting for the first window: the waits between upgrades land here."
323
+ : "No samples to read waits from."}
324
+ </p>
325
+ ) : (
326
+ <div className="sheet__scroll">
327
+ <table className="parts" aria-label="Longest dry spells">
328
+ <caption className="parts__caption lettering">Longest dry spells</caption>
329
+ <thead>
330
+ <tr>
331
+ <th scope="col">From</th>
332
+ <th scope="col" className="parts__n">
333
+ To
334
+ </th>
335
+ <th scope="col" className="parts__n">
336
+ Length
337
+ </th>
338
+ <th scope="col">Ended by</th>
339
+ </tr>
340
+ </thead>
341
+ <tbody>
342
+ {stats.spells.map((s) => {
343
+ const on = selected?.from === s.from && selected?.to === s.to;
344
+ return (
345
+ <tr key={`${s.from}-${s.to}`} className={on ? "parts__on" : undefined}>
346
+ <th scope="row" className="num">
347
+ <button
348
+ type="button"
349
+ className="parts__pick"
350
+ aria-pressed={on}
351
+ onClick={() => onSelect(on ? undefined : { from: s.from, to: s.to })}
352
+ >
353
+ {hoursMinutes(s.from)}
354
+ </button>
355
+ </th>
356
+ <td className="num parts__n">{hoursMinutes(s.to)}</td>
357
+ <td className="num parts__n">{hoursMinutes(s.hours)}</td>
358
+ <td>{s.endedBy.length === 0 ? "end of run" : s.endedBy.join(", ")}</td>
359
+ </tr>
360
+ );
361
+ })}
362
+ </tbody>
363
+ </table>
364
+ <table className="parts" aria-label="Per upgrade">
365
+ <caption className="parts__caption lettering">Per upgrade</caption>
366
+ <thead>
367
+ <tr>
368
+ <th scope="col">Upgrade</th>
369
+ <th scope="col" className="parts__n">
370
+ Bought
371
+ </th>
372
+ <th scope="col" className="parts__n">
373
+ First
374
+ </th>
375
+ <th scope="col" className="parts__n">
376
+ Median wait
377
+ </th>
378
+ <th scope="col" className="parts__n">
379
+ Longest wait
380
+ </th>
381
+ </tr>
382
+ </thead>
383
+ <tbody>
384
+ {keys.map((k) => (
385
+ <tr key={k.key}>
386
+ <th scope="row">{k.key}</th>
387
+ <td className="num parts__n">{k.bought}</td>
388
+ <td className="num parts__n">{hoursMinutes(k.firstHours)}</td>
389
+ <td className="num parts__n">{wait(k.medianWait)}</td>
390
+ <td className="num parts__n">{wait(k.longestWait)}</td>
391
+ </tr>
392
+ ))}
393
+ </tbody>
394
+ </table>
395
+ {!all && stats.keys.length > KEY_LIMIT && (
396
+ <button type="button" className="control" onClick={() => setAll(true)}>
397
+ Show all {stats.keys.length}
398
+ </button>
399
+ )}
400
+ <table className="parts" aria-label="Per window">
401
+ <caption className="parts__caption lettering">Per window</caption>
402
+ <thead>
403
+ <tr>
404
+ <th scope="col">Window</th>
405
+ <th scope="col" className="parts__n">
406
+ Buys
407
+ </th>
408
+ <th scope="col" className="parts__n">
409
+ Median gap
410
+ </th>
411
+ <th scope="col" className="parts__n">
412
+ Longest gap
413
+ </th>
414
+ </tr>
415
+ </thead>
416
+ <tbody>
417
+ {stats.windows.map((w) => (
418
+ <tr key={w.from}>
419
+ <th scope="row" className="num">
420
+ {hoursMinutes(w.from)}–{hoursMinutes(w.to)}
421
+ </th>
422
+ <td className="num parts__n">{w.buys}</td>
423
+ <td className="num parts__n">{w.buys > 0 ? hoursMinutes(w.median) : "–"}</td>
424
+ <td className="num parts__n">{w.buys > 0 ? hoursMinutes(w.longest) : "–"}</td>
425
+ </tr>
426
+ ))}
427
+ </tbody>
428
+ </table>
429
+ </div>
430
+ )}
431
+ </section>
432
+ );
433
+ });
@@ -706,6 +706,33 @@ legend {
706
706
  width: 1%;
707
707
  }
708
708
 
709
+ .parts__caption {
710
+ padding: 0.5rem 0.75rem 0.25rem;
711
+ color: var(--dp-ink-soft);
712
+ text-align: left;
713
+ }
714
+
715
+ .parts__pick {
716
+ padding: 0;
717
+ border: 0;
718
+ background: none;
719
+ color: inherit;
720
+ font: inherit;
721
+ text-decoration: underline;
722
+ cursor: pointer;
723
+ }
724
+
725
+ .parts tr.parts__on th,
726
+ .parts tr.parts__on td {
727
+ background: color-mix(in oklch, var(--dp-live-deep) 14%, transparent);
728
+ }
729
+
730
+ /* A selected dry spell, marked on the graph behind the lines. */
731
+ .graph__span {
732
+ fill: color-mix(in oklch, var(--dp-live-deep) 16%, transparent);
733
+ stroke: none;
734
+ }
735
+
709
736
  .parts__delta {
710
737
  color: var(--dp-ink-soft);
711
738
  }
@@ -783,6 +810,17 @@ legend {
783
810
  gap: 0.5rem;
784
811
  }
785
812
 
813
+ .panel__filter {
814
+ display: flex;
815
+ gap: 0.5rem;
816
+ padding: 0.5rem 0.75rem 0;
817
+ }
818
+
819
+ .panel__search {
820
+ flex: 1;
821
+ min-width: 0;
822
+ }
823
+
786
824
  .panel__strip {
787
825
  grid-column: 1 / -1;
788
826
  min-height: 1.5em;
@@ -812,6 +850,28 @@ legend {
812
850
  border-bottom: var(--dp-hair) solid var(--dp-ink);
813
851
  }
814
852
 
853
+ .knobs__toggle {
854
+ display: block;
855
+ width: 100%;
856
+ padding: 0;
857
+ border: 0;
858
+ background: none;
859
+ color: inherit;
860
+ font: inherit;
861
+ letter-spacing: inherit;
862
+ text-align: left;
863
+ text-transform: inherit;
864
+ cursor: pointer;
865
+ }
866
+
867
+ .knobs__toggle::before {
868
+ content: "+ ";
869
+ }
870
+
871
+ .knobs__toggle[aria-expanded="true"]::before {
872
+ content: "− ";
873
+ }
874
+
815
875
  .knobs__list {
816
876
  list-style: none;
817
877
  }
@@ -0,0 +1,148 @@
1
+ // The waits between upgrades, read from a seed's samples: when each buyable was bought, the dry spells between
2
+ // purchases, and how they spread over keys and windows. Pure: no React. A wait is only as fine as the sample step.
3
+ import type { DeskData, Sample } from "./series";
4
+
5
+ export interface Purchase {
6
+ hours: number;
7
+ key: string;
8
+ }
9
+
10
+ export interface DrySpell {
11
+ from: number;
12
+ to: number;
13
+ hours: number;
14
+ /** The keys bought at `to`; empty for the trailing gap that runs out the run. */
15
+ endedBy: string[];
16
+ }
17
+
18
+ export interface KeyWaits {
19
+ key: string;
20
+ bought: number;
21
+ firstHours: number;
22
+ medianWait: number | undefined;
23
+ longestWait: number | undefined;
24
+ }
25
+
26
+ export interface WindowWaits {
27
+ from: number;
28
+ to: number;
29
+ longest: number;
30
+ median: number;
31
+ buys: number;
32
+ }
33
+
34
+ const medianOf = (xs: readonly number[]): number => {
35
+ if (xs.length === 0) return 0;
36
+ const s = [...xs].sort((a, b) => a - b);
37
+ const mid = s.length >> 1;
38
+ return s.length % 2 ? (s[mid] as number) : ((s[mid - 1] as number) + (s[mid] as number)) / 2;
39
+ };
40
+
41
+ /** One event per key per consecutive pair of samples where its next cost changed, appeared or disappeared. */
42
+ export function purchases(samples: readonly Sample[]): Purchase[] {
43
+ const out: Purchase[] = [];
44
+ for (let i = 1; i < samples.length; i++) {
45
+ const a = (samples[i - 1] as Sample).next;
46
+ const b = (samples[i] as Sample).next;
47
+ const hours = (samples[i] as Sample).hours;
48
+ for (const key of new Set([...Object.keys(a), ...Object.keys(b)])) {
49
+ const x = a[key];
50
+ const y = b[key];
51
+ if (!x || !y || !x.cost.eq(y.cost)) out.push({ hours, key });
52
+ }
53
+ }
54
+ return out;
55
+ }
56
+
57
+ /** The median gap between consecutive samples' hours. */
58
+ export function sampleStepHours(samples: readonly Sample[]): number {
59
+ if (samples.length < 2) return 0;
60
+ return medianOf(samples.slice(1).map((s, i) => s.hours - (samples[i] as Sample).hours));
61
+ }
62
+
63
+ /** The gaps between consecutive distinct purchase times (and from the start, and out to the end), longest first. */
64
+ export function drySpells(samples: readonly Sample[], endHours: number): DrySpell[] {
65
+ const first = samples[0];
66
+ if (!first) return [];
67
+ const byTime = new Map<number, string[]>();
68
+ for (const p of purchases(samples)) byTime.set(p.hours, [...(byTime.get(p.hours) ?? []), p.key]);
69
+ const out: DrySpell[] = [];
70
+ let at = first.hours;
71
+ for (const to of [...byTime.keys()].sort((a, b) => a - b)) {
72
+ out.push({ from: at, to, hours: to - at, endedBy: byTime.get(to) as string[] });
73
+ at = to;
74
+ }
75
+ if (endHours > at) out.push({ from: at, to: endHours, hours: endHours - at, endedBy: [] });
76
+ return out.sort((a, b) => b.hours - a.hours || a.from - b.from);
77
+ }
78
+
79
+ /** Per key: how many were bought, when the first was, and the median and longest gap between its purchases. */
80
+ export function perKey(samples: readonly Sample[]): KeyWaits[] {
81
+ const times = new Map<string, number[]>();
82
+ for (const p of purchases(samples)) times.set(p.key, [...(times.get(p.key) ?? []), p.hours]);
83
+ const rows = [...times].map(([key, ts]): KeyWaits => {
84
+ const gaps = ts.slice(1).map((t, i) => t - (ts[i] as number));
85
+ return {
86
+ key,
87
+ bought: ts.length,
88
+ firstHours: ts[0] as number,
89
+ medianWait: gaps.length > 0 ? medianOf(gaps) : undefined,
90
+ longestWait: gaps.length > 0 ? Math.max(...gaps) : undefined,
91
+ };
92
+ });
93
+ return rows.sort((a, b) => {
94
+ if (a.longestWait === undefined || b.longestWait === undefined)
95
+ return a.longestWait === b.longestWait
96
+ ? a.key.localeCompare(b.key)
97
+ : a.longestWait === undefined
98
+ ? 1
99
+ : -1;
100
+ return b.longestWait - a.longestWait || a.key.localeCompare(b.key);
101
+ });
102
+ }
103
+
104
+ /** Per window `[i*w, (i+1)*w)` up to the end: the longest and median dry spell ending in it, and the purchases in it. */
105
+ export function perWindow(samples: readonly Sample[], windowHours: number, endHours: number): WindowWaits[] {
106
+ if (!(windowHours > 0) || !(endHours > 0)) return [];
107
+ const spells = drySpells(samples, endHours);
108
+ const buys = purchases(samples);
109
+ const out: WindowWaits[] = [];
110
+ for (let i = 0; i * windowHours < endHours; i++) {
111
+ const from = i * windowHours;
112
+ const to = (i + 1) * windowHours;
113
+ const inside = spells.filter((s) => s.to >= from && s.to < to).map((s) => s.hours);
114
+ out.push({
115
+ from,
116
+ to,
117
+ longest: inside.length > 0 ? Math.max(...inside) : 0,
118
+ median: medianOf(inside),
119
+ buys: buys.filter((p) => p.hours >= from && p.hours < to).length,
120
+ });
121
+ }
122
+ return out;
123
+ }
124
+
125
+ export interface WaitStats {
126
+ seed: number;
127
+ step: number;
128
+ spells: DrySpell[];
129
+ keys: KeyWaits[];
130
+ windows: WindowWaits[];
131
+ }
132
+
133
+ /** The waits of the median seed: the one whose longest dry spell is the median across seeds (the lower, if even). */
134
+ export function waitStats(data: DeskData, windowHours: number, endHours: number): WaitStats | undefined {
135
+ const all = [...data.seeds]
136
+ .map(([seed, d]) => ({ seed, samples: d.samples, spells: drySpells(d.samples, endHours) }))
137
+ .filter((s) => s.samples.length > 0)
138
+ .sort((a, b) => (a.spells[0]?.hours ?? 0) - (b.spells[0]?.hours ?? 0) || a.seed - b.seed);
139
+ const pick = all[(all.length - 1) >> 1];
140
+ if (!pick) return undefined;
141
+ return {
142
+ seed: pick.seed,
143
+ step: sampleStepHours(pick.samples),
144
+ spells: pick.spells.slice(0, 5),
145
+ keys: perKey(pick.samples),
146
+ windows: perWindow(pick.samples, windowHours, endHours),
147
+ };
148
+ }