dracosh 0.0.0-stage → 0.2.0

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.
Files changed (70) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +10 -0
  3. package/README.md +100 -2
  4. package/assets/sounds/ask.wav +0 -0
  5. package/assets/sounds/badge-1.wav +0 -0
  6. package/assets/sounds/badge-2.wav +0 -0
  7. package/assets/sounds/badge-3.wav +0 -0
  8. package/assets/sounds/badge-4.wav +0 -0
  9. package/assets/sounds/close.wav +0 -0
  10. package/assets/sounds/combo.wav +0 -0
  11. package/assets/sounds/correct-0.wav +0 -0
  12. package/assets/sounds/correct-1.wav +0 -0
  13. package/assets/sounds/correct-2.wav +0 -0
  14. package/assets/sounds/correct-3.wav +0 -0
  15. package/assets/sounds/correct-4.wav +0 -0
  16. package/assets/sounds/correct-5.wav +0 -0
  17. package/assets/sounds/evolve.wav +0 -0
  18. package/assets/sounds/goal.wav +0 -0
  19. package/assets/sounds/wrong.wav +0 -0
  20. package/data/tips.json +32 -0
  21. package/package.json +47 -4
  22. package/src/alerts.js +38 -0
  23. package/src/anki.js +36 -0
  24. package/src/backup.js +95 -0
  25. package/src/cli.js +197 -0
  26. package/src/commands.js +28 -0
  27. package/src/dates.js +15 -0
  28. package/src/demo.js +11 -0
  29. package/src/diff.js +46 -0
  30. package/src/hint.js +12 -0
  31. package/src/judge.js +78 -0
  32. package/src/library.js +127 -0
  33. package/src/lock.js +63 -0
  34. package/src/manage.js +75 -0
  35. package/src/parse.js +42 -0
  36. package/src/pause.js +23 -0
  37. package/src/progress.js +455 -0
  38. package/src/quiet.js +32 -0
  39. package/src/scheduler.js +94 -0
  40. package/src/session.js +227 -0
  41. package/src/settings.js +38 -0
  42. package/src/starter.js +38 -0
  43. package/src/store.js +100 -0
  44. package/src/tips.js +30 -0
  45. package/src/ui/AddCard.js +172 -0
  46. package/src/ui/AnswerInput.js +85 -0
  47. package/src/ui/App.js +509 -0
  48. package/src/ui/BadgeUnlock.js +50 -0
  49. package/src/ui/Badges.js +122 -0
  50. package/src/ui/Choice.js +33 -0
  51. package/src/ui/Companion.js +45 -0
  52. package/src/ui/Confetti.js +46 -0
  53. package/src/ui/Decks.js +49 -0
  54. package/src/ui/DieRoll.js +104 -0
  55. package/src/ui/Evolution.js +59 -0
  56. package/src/ui/Hatch.js +100 -0
  57. package/src/ui/Header.js +117 -0
  58. package/src/ui/Help.js +68 -0
  59. package/src/ui/Mascot.js +178 -0
  60. package/src/ui/Missed.js +53 -0
  61. package/src/ui/Preview.js +256 -0
  62. package/src/ui/Reset.js +72 -0
  63. package/src/ui/Settings.js +69 -0
  64. package/src/ui/Stats.js +111 -0
  65. package/src/ui/Summary.js +41 -0
  66. package/src/ui/TheOne.js +121 -0
  67. package/src/ui/icons.js +341 -0
  68. package/src/ui/kit.js +120 -0
  69. package/src/vacation.js +56 -0
  70. package/src/words.js +23 -0
@@ -0,0 +1,256 @@
1
+ import { Fragment, useEffect, useMemo, useState } from "react";
2
+ import { Box, Text, useApp, useInput } from "ink";
3
+ import { DEMO_WORDS } from "../demo.js";
4
+ import { addDays, dayKey } from "../dates.js";
5
+ import { BADGES, ELEMENTS, ensureProgress, rollElement, STAGES } from "../progress.js";
6
+ import { createSession } from "../session.js";
7
+ import { DEFAULTS } from "../settings.js";
8
+ import { DieRoll } from "./DieRoll.js";
9
+ import { App } from "./App.js";
10
+ import { BadgeUnlock, badgeSound } from "./BadgeUnlock.js";
11
+ import { Badges } from "./Badges.js";
12
+ import { Confetti } from "./Confetti.js";
13
+ import { Evolution } from "./Evolution.js";
14
+ import { Hatch } from "./Hatch.js";
15
+ import { Header, Snore, useBlink, useMove } from "./Header.js";
16
+ import { html, KeyHints, theme, usePanelWidth } from "./kit.js";
17
+ import { Mascot } from "./Mascot.js";
18
+ import { Stats } from "./Stats.js";
19
+ import { Summary } from "./Summary.js";
20
+ import { TheOne } from "./TheOne.js";
21
+
22
+ // Everything here runs on made-up data in memory: nothing is read from or written to ~/.dracosh.
23
+
24
+ const FACES = ["idle", "smile", "happy", "sad", "blink", "sleep"];
25
+
26
+ // a throwaway session; `progress` fills in a believable last few weeks
27
+ function fakeSession({ settings = {}, progress = false } = {}) {
28
+ const state = { items: {}, newToday: { date: "", count: 0 }, hatched: true };
29
+ const all = { ...DEFAULTS, dailyGoal: 5, everyMs: 6000, tips: "always", ...settings };
30
+ if (progress) {
31
+ const p = ensureProgress(state);
32
+ const today = dayKey(Date.now());
33
+ const counts = [9, 20, 0, 12, 25, 20, 3, 0, 20, 22, 8, 20, 0, 20, 14, 20, 20, 5];
34
+ counts.forEach((correct, i) => {
35
+ if (correct) p.days[addDays(today, -(i + 1))] = { asked: correct + 2, correct, goalMet: correct >= 20 };
36
+ });
37
+ p.days[today] = { asked: 7, correct: 6 };
38
+ p.streak = { ...p.streak, count: 12, best: 34, lastGoalDay: addDays(today, -2) };
39
+ BADGES.slice(0, 13).forEach((b, i) => (p.badges[b.id] = addDays(today, -i * 3)));
40
+ }
41
+ return createSession({ loadWords: async () => ({ words: DEMO_WORDS }), state, getSettings: () => all, save: async () => {} });
42
+ }
43
+
44
+ // a 250 ms tick for the lab's own little animations (the snoring z's)
45
+ function useClock() {
46
+ const [tick, setTick] = useState(0);
47
+ useEffect(() => {
48
+ const id = setInterval(() => setTick((t) => t + 1), 250);
49
+ return () => clearInterval(id);
50
+ }, []);
51
+ return tick;
52
+ }
53
+
54
+ // The mascot on its own, to poke at: every form, face, move and the combo glow.
55
+ function MascotLab({ onBack }) {
56
+ const panel = usePanelWidth();
57
+ const [stage, setStage] = useState(0);
58
+ const [face, setFace] = useState(0);
59
+ const [hot, setHot] = useState(false);
60
+ const [element, setElement] = useState(-1); // -1: the default green dragon
61
+ const [event, setEvent] = useState(null);
62
+ const [tick, setTick] = useState(0);
63
+ const move = useMove(event);
64
+ const blinking = useBlink();
65
+ const clock = useClock();
66
+ useInput((input, key) => {
67
+ if (key.escape || input === "q") return onBack();
68
+ if (key.leftArrow) return setStage((s) => Math.max(0, s - 1));
69
+ if (key.rightArrow) return setStage((s) => Math.min(STAGES.length - 1, s + 1));
70
+ if (key.upArrow || key.downArrow) return setFace((f) => (f + (key.upArrow ? FACES.length - 1 : 1)) % FACES.length);
71
+ if (input === "j") return setEvent({ kind: "jump", at: Date.now() });
72
+ if (input === "s") return setEvent({ kind: "shake", at: Date.now() });
73
+ if (input === "h") return setHot((h) => !h);
74
+ if (input === "e") return setElement((e) => (e + 2) % (ELEMENTS.length + 1) - 1);
75
+ if (input === " ") return setTick((t) => t + 1); // hand-cranked flicker frame
76
+ });
77
+ const shownFace = blinking && FACES[face] === "idle" ? "blink" : FACES[face];
78
+ return html`
79
+ <${Fragment}>
80
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} paddingY=${1} width=${panel} alignItems="center">
81
+ <${Text} bold color=${theme.accent}>Mascot lab<//>
82
+ <${Box} marginY=${1} flexDirection="column" alignItems="center">
83
+ <${Mascot} stage=${stage} face=${shownFace} hot=${hot} flicker=${hot && tick % 2 === 0} scale=${2} dx=${move.dx ?? 0} dy=${move.dy ?? 0} element=${ELEMENTS[element] ?? null} />
84
+ <//>
85
+ ${FACES[face] === "sleep" && html`<${Box}><${Mascot} stage=${stage} face="sleep" /><${Snore} tick=${clock} /><//>`}
86
+ <${Text}>${STAGES[stage].name}<${Text} dimColor> · face ${FACES[face]} · ${ELEMENTS[element] ?? "no element"}${hot ? " · combo glow" : ""}<//><//>
87
+ <//>
88
+ <${Box} paddingX=${1}><${KeyHints} text="←/→ form · ↑/↓ face · e element · j jump · s shake · h glow · esc back" /><//>
89
+ <//>
90
+ `;
91
+ }
92
+
93
+ // three badges won with one answer, lighting up one after another, as under the quiz card
94
+ const UNLOCK_SAMPLE = ["thousand", "hot-streak", "dragons-hoard"];
95
+
96
+ function BadgeUnlockLab({ onBack, play }) {
97
+ const panel = usePanelWidth();
98
+ const [run, setRun] = useState(1);
99
+ useInput((input, key) => {
100
+ if (key.escape || input === "q") return onBack();
101
+ if (key.return || input === " ") setRun((r) => r + 1);
102
+ });
103
+ const badges = UNLOCK_SAMPLE.map((id) => BADGES.find((badge) => badge.id === id));
104
+ return html`
105
+ <${Fragment}>
106
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.good} paddingX=${2} width=${panel}>
107
+ <${Text} color=${theme.good} bold>✓ dragon<//>
108
+ ${badges.map((badge, i) => html`<${BadgeUnlock} key=${`${run}-${badge.id}`} id=${badge.id} name=${badge.name} desc=${badge.desc} delay=${i * 700} sound=${badgeSound(i)} play=${play} />`)}
109
+ <//>
110
+ <${Box} paddingX=${1}><${KeyHints} text="enter replay · esc back" /><//>
111
+ <//>
112
+ `;
113
+ }
114
+
115
+ // The quiz screen's header with any form and element, to check how the dragon looks and fits there
116
+ function QuizLookLab({ onBack }) {
117
+ const panel = usePanelWidth();
118
+ const [stage, setStage] = useState(STAGES.length - 1);
119
+ const [element, setElement] = useState(-1);
120
+ const [combo, setCombo] = useState(0);
121
+ useInput((input, key) => {
122
+ if (key.escape || input === "q") return onBack();
123
+ if (key.leftArrow) return setStage((s) => Math.max(0, s - 1));
124
+ if (key.rightArrow) return setStage((s) => Math.min(STAGES.length - 1, s + 1));
125
+ if (input === "e") return setElement((e) => (e + 2) % (ELEMENTS.length + 1) - 1);
126
+ if (input === "c") return setCombo((c) => (c ? 0 : 7));
127
+ });
128
+ const stats = {
129
+ streak: { days: 42, best: 120, freezes: 1, atRisk: false },
130
+ today: { correct: 13, asked: 15, goalMet: false },
131
+ goal: 20,
132
+ vacation: { activeUntil: null },
133
+ companion: { index: stage, name: STAGES[stage].name, element: ELEMENTS[element] ?? null }
134
+ };
135
+ return html`
136
+ <${Fragment}>
137
+ <${Header} deck="Polish" everyMs=${600000} wordCount=${340} stats=${stats} combo=${combo} face="idle" event=${null} tick=${0} />
138
+ <${Box} marginTop=${1} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} paddingY=${1} width=${panel}>
139
+ <${Text} bold>la mariposa<//>
140
+ <${Box} marginTop=${1}><${Text} color=${theme.accent}>❯ <//><${Text} inverse> <//><//>
141
+ <//>
142
+ <${Box} paddingX=${1}><${Text} dimColor>${STAGES[stage].name} · ${ELEMENTS[element] ?? "no element"} · ←/→ form · e element · c combo · esc back<//><//>
143
+ <//>
144
+ `;
145
+ }
146
+
147
+ function ConfettiLab({ onBack }) {
148
+ const panel = usePanelWidth();
149
+ const [run, setRun] = useState(1);
150
+ useInput((input, key) => {
151
+ if (key.escape || input === "q") return onBack();
152
+ if (key.return || input === " ") setRun((r) => r + 1);
153
+ });
154
+ return html`
155
+ <${Fragment}>
156
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.good} paddingX=${2} width=${panel}>
157
+ <${Text} color=${theme.good} bold>▪ Daily goal reached · 12-day streak<//>
158
+ <//>
159
+ <${Box} height=${5}><${Confetti} key=${run} width=${panel} onDone=${() => {}} /><//>
160
+ <${Box} paddingX=${1}><${KeyHints} text="enter replay · esc back" /><//>
161
+ <//>
162
+ `;
163
+ }
164
+
165
+ const SCENES = [
166
+ { id: "hatch", label: "Hatching", hint: "the first-launch intro" },
167
+ ...STAGES.slice(1).map((stage, i) => ({ id: `evolve-${i + 1}`, label: `Evolution → ${stage.name}`, hint: `reached at a ${stage.from}-day best streak` })),
168
+ { id: "mascot", label: "Mascot lab", hint: "forms, faces, elements, moves, glow" },
169
+ { id: "quiz", label: "Quiz", hint: "a live round on demo words, goal at 5" },
170
+ { id: "quiz-look", label: "Quiz screen", hint: "the header with any form and element" },
171
+ { id: "unlock", label: "Badge unlocked", hint: "three badges won at once" },
172
+ { id: "the-one", label: "The One...", hint: "the ring rises from the lava" },
173
+ { id: "die", label: "Dragon's die", hint: "the secret badge: a roll for an element" },
174
+ { id: "confetti", label: "Confetti", hint: "the daily-goal burst" },
175
+ { id: "stats", label: "Stats", hint: "calendar tiles, weeks of fake history" },
176
+ { id: "badges", label: "Badges", hint: "all unlocked, three of them new" },
177
+ { id: "summary", label: "Session summary", hint: "what you see when quitting" }
178
+ ];
179
+
180
+ function Menu({ selected, onMove, onPick, onQuit }) {
181
+ const panel = usePanelWidth();
182
+ useInput((input, key) => {
183
+ if (key.escape || input === "q") return onQuit();
184
+ if (key.upArrow) return onMove(-1);
185
+ if (key.downArrow) return onMove(1);
186
+ if (key.return) onPick();
187
+ });
188
+ return html`
189
+ <${Fragment}>
190
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
191
+ <${Box}><${Text} bold color=${theme.accent}>Preview<//><${Text} dimColor> made-up data · nothing is saved<//><//>
192
+ <${Box} flexDirection="column" marginTop=${1}>
193
+ ${SCENES.map(
194
+ (scene, i) => html`
195
+ <${Box} key=${scene.id}>
196
+ <${Box} width=${25} flexShrink=${0}>
197
+ <${Text} color=${i === selected ? theme.accent : undefined} bold=${i === selected}>${i === selected ? "❯" : " "} ${scene.label}<//>
198
+ <//>
199
+ <${Text} dimColor wrap="truncate-end">${scene.hint}<//>
200
+ <//>
201
+ `
202
+ )}
203
+ <//>
204
+ <//>
205
+ <${Box} paddingX=${1}><${KeyHints} text="↑/↓ choose · enter play · esc quit" /><//>
206
+ <//>
207
+ `;
208
+ }
209
+
210
+ // `dracosh --preview`: a gallery of every scene and animation, to look at without playing for weeks.
211
+ export function Preview({ alerts }) {
212
+ const { exit } = useApp();
213
+ const [selected, setSelected] = useState(0);
214
+ const [scene, setScene] = useState(null);
215
+ const [run, setRun] = useState(0); // a fresh key per play, so every scene starts from its first frame
216
+ const back = () => setScene(null);
217
+ const history = useMemo(() => fakeSession({ progress: true }), []);
218
+ const quizSession = useMemo(() => fakeSession(), [run]); // every quiz run starts from zero
219
+ const dieElement = useMemo(() => rollElement(), [run]); // a fresh roll every time the scene plays
220
+
221
+ const wrap = (content) => html`<${Box} flexDirection="column" marginY=${1}>${content}<//>`;
222
+
223
+ function pick() {
224
+ const id = SCENES[selected].id;
225
+ if (id.startsWith("evolve-")) alerts.play("evolve"); // in the app the answer that triggers it plays this
226
+ setRun((r) => r + 1);
227
+ setScene(id);
228
+ }
229
+
230
+ if (!scene) {
231
+ return wrap(html`<${Menu} selected=${selected} onMove=${(d) => setSelected((s) => (s + d + SCENES.length) % SCENES.length)} onPick=${pick} onQuit=${exit} />`);
232
+ }
233
+ if (scene === "hatch") return wrap(html`<${Hatch} key=${run} play=${alerts.play} onDone=${back} />`);
234
+ if (scene.startsWith("evolve-")) {
235
+ const to = Number(scene.slice("evolve-".length));
236
+ return wrap(html`<${Evolution} key=${run} from=${to - 1} to=${to} name=${STAGES[to].name} onClose=${back} />`);
237
+ }
238
+ if (scene === "mascot") return wrap(html`<${MascotLab} onBack=${back} />`);
239
+ if (scene === "quiz-look") return wrap(html`<${QuizLookLab} onBack=${back} />`);
240
+ if (scene === "confetti") return wrap(html`<${ConfettiLab} onBack=${back} />`);
241
+ if (scene === "unlock") return wrap(html`<${BadgeUnlockLab} onBack=${back} play=${alerts.play} />`);
242
+ if (scene === "die") return wrap(html`<${DieRoll} key=${run} element=${dieElement} stage=${2} play=${alerts.play} onClose=${back} />`);
243
+ if (scene === "the-one") return wrap(html`<${TheOne} key=${run} play=${alerts.play} onClose=${back} />`);
244
+ if (scene === "stats") return wrap(html`<${Stats} stats=${history.stats()} getMonth=${history.month} onClose=${back} />`);
245
+ if (scene === "badges") {
246
+ // every badge unlocked, so each icon and rank color can be seen
247
+ const all = history.stats().badges.map((badge) => ({ ...badge, unlockedOn: badge.unlockedOn ?? dayKey(Date.now()) }));
248
+ return wrap(html`<${Badges} key=${run} badges=${all} fresh=${["thousand", "hot-streak", "dragonheart"]} onClose=${back} />`);
249
+ }
250
+ if (scene === "summary")
251
+ return wrap(html`<${Summary} stats=${history.stats()} totals=${{ asked: 23, correct: 20 }} bestCombo=${9} onDone=${back} />`);
252
+ if (scene === "quiz") {
253
+ return html`<${App} key=${run} session=${quizSession} deck="Demo" initialSettings=${{ ...DEFAULTS, dailyGoal: 5, everyMs: 6000, tips: "always" }} alerts=${alerts} persistSettings=${async () => {}} onExit=${back} />`;
254
+ }
255
+ return null;
256
+ }
@@ -0,0 +1,72 @@
1
+ import { Fragment, useEffect, useState } from "react";
2
+ import { Box, Text } from "ink";
3
+ import { Choice, CHOICE_KEYS } from "./Choice.js";
4
+ import { html, KeyHints, theme, usePanelWidth } from "./kit.js";
5
+
6
+ const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
7
+
8
+ // Reset from /settings: pick what to clear, see exactly what goes, then confirm (No is the default).
9
+ // A backup is made first, so `dracosh restore` can undo it.
10
+ export function Reset({ stats, actions, onConfirm, onClose }) {
11
+ const panel = usePanelWidth();
12
+ const [decks, setDecks] = useState(null);
13
+ const [scope, setScope] = useState(0);
14
+ const [answer, setAnswer] = useState(0);
15
+ const [step, setStep] = useState("choose"); // choose | confirm | working
16
+ const [error, setError] = useState("");
17
+
18
+ useEffect(() => {
19
+ actions.allDecks().then(setDecks, (err) => setError(err.message));
20
+ }, []);
21
+
22
+ const own = (decks ?? []).filter((d) => !d.tour);
23
+ const cards = own.reduce((sum, d) => sum + d.cards, 0);
24
+ const badges = stats.badges.filter((b) => b.unlockedOn).length;
25
+ const progress = [plural(badges, "badge"), stats.streak.best > 0 && `a ${stats.streak.best}-day best streak`, `your ${stats.companion.name}`].filter(Boolean).join(" · ");
26
+ const SCOPES = [
27
+ { scope: "progress", label: "Progress", note: "badges, streak and the dragon start over; your cards stay", question: "Reset your progress?", goes: progress, stays: "Your decks, cards and how well you know them stay." },
28
+ { scope: "everything", label: "Everything", note: "a fresh start with the tour; settings stay", question: "Reset everything?", goes: `${plural(own.length, "deck")} · ${plural(cards, "card")} · ${progress}`, stays: "Dracosh starts over like a fresh install, with the tour. Settings stay." }
29
+ ];
30
+ const chosen = SCOPES[scope];
31
+
32
+ async function confirm(index) {
33
+ if (index === 0) return setStep("choose");
34
+ setStep("working");
35
+ try {
36
+ await onConfirm(chosen.scope);
37
+ } catch (err) {
38
+ setError(err.message);
39
+ setStep("confirm");
40
+ }
41
+ }
42
+
43
+ return html`
44
+ <${Fragment}>
45
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${step === "choose" ? theme.accent : theme.bad} paddingX=${2} width=${panel}>
46
+ <${Text} bold color=${step === "choose" ? theme.accent : theme.bad}>${step === "choose" ? "Reset: what should start over?" : chosen.question}<//>
47
+ ${step === "choose" &&
48
+ html`<${Box} marginTop=${1}><${Choice} options=${SCOPES} selected=${scope} onMove=${setScope} onPick=${(i) => decks && (setScope(i), setAnswer(0), setStep("confirm"))} onCancel=${onClose} /><//>`}
49
+ ${step !== "choose" &&
50
+ html`
51
+ <${Box} flexDirection="column">
52
+ <${Box} flexDirection="column" marginTop=${1}>
53
+ <${Text}>This clears <${Text} bold>${chosen.goes}<//>.<//>
54
+ <${Text} dimColor>${chosen.stays} A backup is made first, so dracosh restore can undo it.<//>
55
+ <//>
56
+ <${Box} marginTop=${1}>
57
+ <${Choice}
58
+ options=${[{ label: "No, keep it all", key: "n" }, { label: `Yes, reset ${chosen.label.toLowerCase()}`, key: "y", danger: true }]}
59
+ selected=${answer}
60
+ onMove=${setAnswer}
61
+ onPick=${(i) => step === "confirm" && confirm(i)}
62
+ onCancel=${() => setStep("choose")}
63
+ />
64
+ <//>
65
+ <//>
66
+ `}
67
+ ${error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`}
68
+ <//>
69
+ <${Box} paddingX=${1}><${KeyHints} text=${step === "working" ? "resetting…" : `${CHOICE_KEYS} · esc back`} /><//>
70
+ <//>
71
+ `;
72
+ }
@@ -0,0 +1,69 @@
1
+ import { Fragment, useState } from "react";
2
+ import { Box, Text, useInput } from "ink";
3
+ import { GOALS, INTERVALS, QUIET_PRESETS, TIP_LABELS, TIP_MODES, VOLUMES, formatInterval } from "../settings.js";
4
+ import { formatQuiet } from "../quiet.js";
5
+ import { Bar, html, KeyHints, theme, usePanelWidth } from "./kit.js";
6
+
7
+ function VolumeValue({ value }) {
8
+ return html`<${Text}><${Bar} value=${value} max=${1} width=${10} /> ${Math.round(value * 100)}%<//>`;
9
+ }
10
+
11
+ const ALL_ROWS = [
12
+ { key: "everyMs", label: "Interval", options: INTERVALS, format: formatInterval, hint: "time between questions" },
13
+ { key: "sound", label: "Sound", options: [true, false], format: (v) => (v ? "on" : "off"), hint: "answer and new-card sounds", toggle: true },
14
+ { key: "volume", label: "Volume", options: VOLUMES, format: (v) => html`<${VolumeValue} value=${v} />`, hint: "plays a preview when changed" },
15
+ { key: "dailyGoal", label: "Daily goal", options: GOALS, format: (n) => `${n} correct`, hint: "keeps your streak alive" },
16
+ { key: "tips", label: "Tips", options: TIP_MODES, format: (v) => TIP_LABELS[v], hint: "a short tip about Dracosh while you wait" },
17
+ { key: "skipWeekends", label: "Weekends", options: [true, false], format: (v) => (v ? "rest days" : "count like other days"), hint: "rest days never break your streak (doing them still counts)", toggle: true },
18
+ { key: "quiet", label: "Quiet hours", options: QUIET_PRESETS, format: formatQuiet, hint: "no questions, sounds or banners during these hours" },
19
+ { key: "reset", label: "Reset", action: true, format: () => "press enter", hint: "start over: your progress only, or everything (a backup is made first)" }
20
+ ];
21
+
22
+ const nearestIndex = (options, value) => {
23
+ const exact = options.findIndex((option) => JSON.stringify(option) === JSON.stringify(value));
24
+ if (exact >= 0) return exact;
25
+ return options.reduce((best, option, i) => (Math.abs(option - value) < Math.abs(options[best] - value) ? i : best), 0);
26
+ };
27
+
28
+ // Changes apply and persist immediately; there is no save step. Action rows (Reset) open their own
29
+ // screen through `onAction`; without it (the preview) they are left out.
30
+ export function Settings({ settings, onChange, onClose, onAction }) {
31
+ const panel = usePanelWidth();
32
+ const [row, setRow] = useState(0);
33
+ const ROWS = onAction ? ALL_ROWS : ALL_ROWS.filter((item) => !item.action);
34
+
35
+ useInput((input, key) => {
36
+ if (key.escape || input === "q") return onClose();
37
+ if (key.upArrow) return setRow((r) => Math.max(0, r - 1));
38
+ if (key.downArrow) return setRow((r) => Math.min(ROWS.length - 1, r + 1));
39
+
40
+ const current = ROWS[row];
41
+ if (current.action) return key.return ? onAction(current.key) : undefined;
42
+ const direction = key.leftArrow ? -1 : key.rightArrow ? 1 : 0;
43
+ if (current.toggle && (direction || key.return || input === " ")) return onChange({ [current.key]: !settings[current.key] });
44
+ if (!direction) return;
45
+ const index = nearestIndex(current.options, settings[current.key]);
46
+ const next = current.options[Math.min(current.options.length - 1, Math.max(0, index + direction))];
47
+ if (JSON.stringify(next) !== JSON.stringify(settings[current.key])) onChange({ [current.key]: next });
48
+ });
49
+
50
+ return html`
51
+ <${Fragment}>
52
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
53
+ <${Text} bold color=${theme.accent}>Settings<//>
54
+ <${Box} flexDirection="column" marginTop=${1}>
55
+ ${ROWS.map(
56
+ (item, i) => html`
57
+ <${Box} key=${item.key}>
58
+ <${Text} color=${i === row ? theme.accent : undefined} bold=${i === row}>${i === row ? "❯" : " "} ${item.label.padEnd(13)}<//>
59
+ <${Text} color=${i === row ? theme.accent : undefined}>${i === row && !item.action ? "❮ " : " "}${item.format(settings[item.key])}${i === row && !item.action ? " ❯" : ""}<//>
60
+ <//>
61
+ `
62
+ )}
63
+ <//>
64
+ <${Box} marginTop=${1}><${Text} dimColor>${ROWS[row].hint}<//><//>
65
+ <//>
66
+ <${Box} paddingX=${1}><${KeyHints} text="↑/↓ select · ←/→ change · esc back" /><//>
67
+ <//>
68
+ `;
69
+ }
@@ -0,0 +1,111 @@
1
+ import { Fragment, useState } from "react";
2
+ import { Box, Text, useInput } from "ink";
3
+ import { dragonName } from "../progress.js";
4
+ import { formatRange } from "../vacation.js";
5
+ import { html, KeyHints, theme, usePanelWidth } from "./kit.js";
6
+
7
+ const DAY_LETTERS = ["M", "T", "W", "T", "F", "S", "S"];
8
+ // One shape for every day; only the color tells them apart, GitHub-contribution style.
9
+ const TILE = {
10
+ goal: theme.good,
11
+ halfway: "#3f9d5e",
12
+ some: "#2f6b45",
13
+ none: "#3a3f47",
14
+ rest: "#2a2e35",
15
+ future: "#1c1f25",
16
+ today: theme.accent // today with nothing done yet
17
+ };
18
+
19
+ function tileColor(cell, goal) {
20
+ if (cell.goalMet) return TILE.goal;
21
+ if (cell.correct > 0) return cell.correct >= goal / 2 ? TILE.halfway : TILE.some;
22
+ if (cell.today) return TILE.today;
23
+ if (cell.future) return TILE.future;
24
+ return cell.rest ? TILE.rest : TILE.none;
25
+ }
26
+
27
+ // A plain full-month grid: weeks as rows under one-letter day headers.
28
+ // Today: its weekday letter lights up, and its tile is violet until the first correct answer.
29
+ function Calendar({ month, goal }) {
30
+ const todayColumn = month.weeks.flat().findIndex((cell) => cell?.today) % 7;
31
+ return html`
32
+ <${Box} flexDirection="column">
33
+ <${Box}>
34
+ ${DAY_LETTERS.map((letter, i) =>
35
+ i === todayColumn ? html`<${Text} key=${i} color=${theme.accent} bold>${letter} <//>` : html`<${Text} key=${i} dimColor>${letter} <//>`
36
+ )}
37
+ <//>
38
+ ${month.weeks.map(
39
+ (week, w) => html`
40
+ <${Box} key=${w}>
41
+ ${week.map((cell, d) => (cell ? html`<${Text} key=${d}><${Text} color=${tileColor(cell, goal)}>■<//> <//>` : html`<${Text} key=${d}> <//>`))}
42
+ <//>
43
+ `
44
+ )}
45
+ <//>
46
+ `;
47
+ }
48
+
49
+ function Legend({ goal }) {
50
+ const items = [
51
+ [TILE.goal, `goal (${goal})`],
52
+ [TILE.halfway, "halfway"],
53
+ [TILE.some, "some"],
54
+ [TILE.none, "none"],
55
+ [TILE.rest, "rest"],
56
+ [TILE.today, "today"]
57
+ ];
58
+ return html`
59
+ <${Text}>
60
+ ${items.map(([color, label]) => html`<${Text} key=${label}><${Text} color=${color}>■<//><${Text} dimColor> ${label} <//><//>`)}
61
+ <//>
62
+ `;
63
+ }
64
+
65
+ export function Stats({ stats, getMonth, onClose }) {
66
+ const panel = usePanelWidth();
67
+ const [offset, setOffset] = useState(0);
68
+ useInput((input, key) => {
69
+ if (key.escape || key.return || input === "q") return onClose();
70
+ if (key.leftArrow) return setOffset((o) => o - 1);
71
+ if (key.rightArrow) return setOffset((o) => Math.min(0, o + 1));
72
+ });
73
+
74
+ const { streak, today, goal, companion } = stats;
75
+ const accuracy = today.asked ? Math.round((today.correct / today.asked) * 100) : null;
76
+ const unlocked = stats.badges.filter((b) => b.unlockedOn).length;
77
+ const month = getMonth(offset);
78
+ const next = companion.nextAt ? ` · next form at a ${companion.nextAt}-day streak` : " · fully evolved";
79
+ const title = dragonName(companion.index, companion.element);
80
+
81
+ return html`
82
+ <${Fragment}>
83
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
84
+ <${Text} bold color=${theme.accent}>Stats<//>
85
+
86
+ <${Box} marginTop=${1} flexDirection="column">
87
+ <${Text}>
88
+ ${streak.days > 0 ? html`<${Text} color=${theme.warn} bold>${streak.days}-day streak<//>` : html`<${Text} dimColor>no streak yet<//>`}
89
+ <${Text} dimColor> · best ${streak.best} · ${streak.freezes} freeze${streak.freezes === 1 ? "" : "s"} in stock<//>
90
+ <//>
91
+ <${Text} dimColor>today ${today.correct}/${goal} correct${accuracy === null ? "" : ` · ${accuracy}% accuracy`}<//>
92
+ <${Text} dimColor>cards ${stats.mastered} mastered · ${stats.practiced} practised · ${stats.totalCorrect} correct in total<//>
93
+ <${Text} dimColor>companion ${title} (${companion.index + 1}/6)${next}<//>
94
+ <${Text} dimColor>rest days ${stats.skipWeekends ? "weekends" : "no weekends"}${stats.vacation.upcoming.length ? ` · days off ${stats.vacation.upcoming.map((v) => formatRange(v.from, v.to)).join(", ")}` : ""}<//>
95
+ <//>
96
+
97
+ <${Box} marginTop=${1} flexDirection="column">
98
+ <${Text} bold>${month.label}<//>
99
+ <${Box} marginTop=${1}><${Calendar} month=${month} goal=${goal} /><//>
100
+ <${Box} marginTop=${1} flexDirection="column">
101
+ <${Text} dimColor>Goal reached on ${month.goalDays} of ${month.daysSoFar} days · ${month.correct} correct answers<//>
102
+ <${Legend} goal=${goal} />
103
+ <//>
104
+ <//>
105
+
106
+ <${Box} marginTop=${1}><${Text} dimColor>${unlocked}/${stats.badges.length} badges · /badges lists them all<//><//>
107
+ <//>
108
+ <${Box} paddingX=${1}><${KeyHints} text="←/→ month · esc back" /><//>
109
+ <//>
110
+ `;
111
+ }
@@ -0,0 +1,41 @@
1
+ import { Fragment } from "react";
2
+ import { Box, Text, useInput } from "ink";
3
+ import { Bar, html, KeyHints, theme, usePanelWidth } from "./kit.js";
4
+ import { dragonName } from "../progress.js";
5
+ import { Flame } from "./icons.js";
6
+ import { Mascot, MASCOT_WIDTH } from "./Mascot.js";
7
+
8
+ // The end-of-session screen: how this run went, shown on quit. Any key leaves for real.
9
+ export function Summary({ stats, totals, bestCombo, onDone }) {
10
+ const panel = usePanelWidth();
11
+ useInput(() => onDone());
12
+ const { streak, today, goal, companion } = stats;
13
+ const accuracy = totals.asked ? Math.round((totals.correct / totals.asked) * 100) : 0;
14
+ const face = today.goalMet ? "happy" : totals.correct > 0 ? "smile" : "idle";
15
+
16
+ return html`
17
+ <${Fragment}>
18
+ <${Box} borderStyle="round" borderColor=${theme.accent} paddingX=${2} paddingY=${1} width=${panel}>
19
+ <${Box} flexDirection="column" width=${MASCOT_WIDTH + 3}>
20
+ <${Mascot} face=${face} stage=${companion.index} element=${companion.element} />
21
+ <//>
22
+ <${Box} flexDirection="column" justifyContent="center">
23
+ <${Box}><${Text} bold color=${theme.accent}>Session over<//><//>
24
+ <${Box}><${Text}>${totals.correct}/${totals.asked} correct · ${accuracy}%${bestCombo >= 2 ? ` · best combo ×${bestCombo}` : ""}<//><//>
25
+ <${Box}>
26
+ <${Text} dimColor>today <//>
27
+ <${Bar} value=${today.correct} max=${goal} width=${12} color=${today.goalMet ? theme.good : theme.accent} />
28
+ <${Text} dimColor> ${Math.min(today.correct, goal)}/${goal}<//>
29
+ ${today.goalMet && html`<${Text} color=${theme.good}> goal reached<//>`}
30
+ <//>
31
+ <${Box}>
32
+ ${streak.days > 0 && html`<${Box}><${Flame} /><${Text}> <//><//>`}
33
+ <${Text} dimColor>${streak.days > 0 ? `${streak.days}-day streak` : "no streak yet"}${streak.atRisk ? " · still at risk today" : ""}<//>
34
+ <//>
35
+ <${Box}><${Text} dimColor>Your ${dragonName(companion.index, companion.element)} will be waiting.<//><//>
36
+ <//>
37
+ <//>
38
+ <${Box} paddingX=${1}><${KeyHints} text="see you later · any key to leave" /><//>
39
+ <//>
40
+ `;
41
+ }