dracosh 0.1.0 → 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.
- package/README.md +42 -20
- package/assets/sounds/badge-1.wav +0 -0
- package/assets/sounds/badge-2.wav +0 -0
- package/assets/sounds/badge-3.wav +0 -0
- package/assets/sounds/badge-4.wav +0 -0
- package/data/tips.json +31 -1361
- package/package.json +1 -1
- package/src/alerts.js +4 -3
- package/src/backup.js +95 -0
- package/src/cli.js +97 -16
- package/src/commands.js +7 -7
- package/src/judge.js +33 -12
- package/src/library.js +127 -0
- package/src/lock.js +63 -0
- package/src/manage.js +75 -0
- package/src/pause.js +23 -0
- package/src/progress.js +145 -53
- package/src/quiet.js +1 -1
- package/src/scheduler.js +38 -26
- package/src/session.js +128 -30
- package/src/settings.js +4 -9
- package/src/starter.js +38 -0
- package/src/store.js +55 -16
- package/src/tips.js +7 -24
- package/src/ui/AddCard.js +172 -0
- package/src/ui/AnswerInput.js +8 -5
- package/src/ui/App.js +211 -71
- package/src/ui/BadgeUnlock.js +50 -0
- package/src/ui/Badges.js +75 -17
- package/src/ui/Choice.js +33 -0
- package/src/ui/Companion.js +20 -35
- package/src/ui/Decks.js +49 -0
- package/src/ui/DieRoll.js +104 -0
- package/src/ui/Evolution.js +6 -5
- package/src/ui/Hatch.js +5 -4
- package/src/ui/Header.js +7 -6
- package/src/ui/Help.js +68 -0
- package/src/ui/Mascot.js +88 -38
- package/src/ui/Missed.js +6 -5
- package/src/ui/Preview.js +92 -17
- package/src/ui/Reset.js +72 -0
- package/src/ui/Settings.js +15 -11
- package/src/ui/Stats.js +10 -7
- package/src/ui/Summary.js +12 -5
- package/src/ui/TheOne.js +121 -0
- package/src/ui/icons.js +156 -24
- package/src/ui/kit.js +58 -1
- package/src/vacation.js +7 -2
- package/src/words.js +21 -14
- package/assets/sounds/badge.wav +0 -0
- package/src/ui/Tip.js +0 -25
package/src/ui/App.js
CHANGED
|
@@ -4,28 +4,37 @@ import { commandsFor } from "../commands.js";
|
|
|
4
4
|
import { diffChars } from "../diff.js";
|
|
5
5
|
import { AnswerInput } from "./AnswerInput.js";
|
|
6
6
|
import { Header } from "./Header.js";
|
|
7
|
-
import { html,
|
|
7
|
+
import { Bar, cellWidth, cutToWidth, html, KeyHints, Marked, theme, Typewriter, usePanelWidth } from "./kit.js";
|
|
8
8
|
import { hintTarget, makeHint } from "../hint.js";
|
|
9
|
-
import { formatClock, inQuietHours,
|
|
9
|
+
import { formatClock, inQuietHours, quietEnd } from "../quiet.js";
|
|
10
|
+
import { startOfTomorrow } from "../scheduler.js";
|
|
11
|
+
import { formatDay } from "../vacation.js";
|
|
12
|
+
import { dayKey } from "../dates.js";
|
|
10
13
|
import { shouldShowTip } from "../tips.js";
|
|
11
14
|
import { Badges } from "./Badges.js";
|
|
12
15
|
import { Companion } from "./Companion.js";
|
|
13
16
|
import { Confetti } from "./Confetti.js";
|
|
17
|
+
import { BadgeUnlock, badgeSound } from "./BadgeUnlock.js";
|
|
18
|
+
import { DieRoll } from "./DieRoll.js";
|
|
14
19
|
import { Evolution } from "./Evolution.js";
|
|
15
20
|
import { Hatch } from "./Hatch.js";
|
|
21
|
+
import { AddCard } from "./AddCard.js";
|
|
22
|
+
import { Decks } from "./Decks.js";
|
|
23
|
+
import { Reset } from "./Reset.js";
|
|
24
|
+
import { Help } from "./Help.js";
|
|
16
25
|
import { Missed } from "./Missed.js";
|
|
17
26
|
import { Settings } from "./Settings.js";
|
|
18
27
|
import { Stats } from "./Stats.js";
|
|
19
28
|
import { Summary } from "./Summary.js";
|
|
20
|
-
import {
|
|
29
|
+
import { TheOne } from "./TheOne.js";
|
|
21
30
|
|
|
22
|
-
const SPINNER = ["
|
|
31
|
+
const SPINNER = ["⠚", "⠓", "⠋", "⠙"]; // the old braille dots on a 2×2 square: the gap goes round
|
|
23
32
|
const TICK_MS = 250; // the steady redraw: slow enough to stay idle for hours, fast enough for the spinner
|
|
24
33
|
const SLEEP_AFTER_MS = 3 * 60_000; // no key pressed for this long while waiting → the dragon dozes off
|
|
25
|
-
const PAUSED_SLEEP_AFTER_MS = 15_000; // during
|
|
34
|
+
const PAUSED_SLEEP_AFTER_MS = 15_000; // during a pause or quiet hours it nods off again much sooner
|
|
26
35
|
const CHEER_STYLE = {
|
|
27
|
-
goal: { icon: "
|
|
28
|
-
evolve: { icon: "
|
|
36
|
+
goal: { icon: "▪", color: theme.good },
|
|
37
|
+
evolve: { icon: "▴", color: theme.accent },
|
|
29
38
|
badge: { icon: "✦", color: theme.warn },
|
|
30
39
|
combo: { icon: "»", color: theme.warn }
|
|
31
40
|
};
|
|
@@ -35,53 +44,59 @@ function clock(ms) {
|
|
|
35
44
|
return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}`;
|
|
36
45
|
}
|
|
37
46
|
|
|
38
|
-
// your answer with every letter judged: green lined up with the expected word,
|
|
47
|
+
// your answer with every letter judged: green lined up with the expected word, underlined didn't
|
|
39
48
|
function AnswerDiff({ given, closest }) {
|
|
40
49
|
const parts = diffChars(given, closest);
|
|
41
50
|
return html`
|
|
42
51
|
<${Text}>
|
|
43
|
-
<${Text} dimColor>you wrote
|
|
44
|
-
${parts.map((p, i) => html`<${Text} key=${i} color=${p.ok ? theme.good : theme.
|
|
52
|
+
<${Text} dimColor>you wrote <//>
|
|
53
|
+
${parts.map((p, i) => html`<${Text} key=${i} color=${p.ok ? theme.good : theme.text} underline=${!p.ok}>${p.ch}<//>`)}
|
|
45
54
|
<//>
|
|
46
55
|
`;
|
|
47
56
|
}
|
|
48
57
|
|
|
49
58
|
function Verdict({ outcome, question }) {
|
|
50
|
-
if (outcome.result === "skipped") return html`<${Text} dimColor>– skipped<//>`;
|
|
51
59
|
const wrong = outcome.result === "wrong";
|
|
52
60
|
const showDiff = outcome.closest && outcome.given && !outcome.overruled && (wrong || outcome.result === "typo");
|
|
53
61
|
return html`
|
|
54
62
|
<${Box} flexDirection="column">
|
|
55
63
|
<${Text} color=${wrong ? theme.bad : theme.good} bold>${wrong ? "✗" : "✓"} ${question.expected.join(", ")}${outcome.result === "typo" ? " (close enough)" : ""}<//>
|
|
56
64
|
${showDiff && html`<${AnswerDiff} given=${outcome.given} closest=${outcome.closest} />`}
|
|
57
|
-
${question.word.example && html`<${Text} dimColor
|
|
65
|
+
${question.word.example && html`<${Box} marginTop=${1}>${question.tour ? html`<${Marked} dimColor text=${question.word.example} />` : html`<${Text} dimColor italic>“${question.word.example}”<//>`}<//>`}
|
|
58
66
|
${(outcome.hinted || outcome.overruled) &&
|
|
59
67
|
html`
|
|
60
68
|
<${Box} marginTop=${1} flexDirection="column">
|
|
61
69
|
${outcome.hinted && html`<${Text} dimColor>hint used · the card stays in its box<//>`}
|
|
62
|
-
${outcome.overruled && html`<${Text} color=${theme.good}>counted as correct · "${outcome.accepted}" is accepted from now on<//>`}
|
|
70
|
+
${outcome.overruled && html`<${Text} color=${theme.good}>counted as correct${question.tour ? "" : ` · "${outcome.accepted}" is accepted from now on`}<//>`}
|
|
63
71
|
<//>
|
|
64
72
|
`}
|
|
65
73
|
<//>
|
|
66
74
|
`;
|
|
67
75
|
}
|
|
68
76
|
|
|
69
|
-
|
|
77
|
+
const BADGE_FIRST_MS = 400; // after the answer's own sound
|
|
78
|
+
const BADGE_STAGGER_MS = 700; // several badges at once light up one after another
|
|
79
|
+
|
|
80
|
+
function Cheers({ cheers, play }) {
|
|
81
|
+
const lines = cheers.filter((cheer) => cheer.kind !== "badge");
|
|
82
|
+
const badges = cheers.filter((cheer) => cheer.kind === "badge");
|
|
70
83
|
return html`
|
|
71
84
|
<${Box} flexDirection="column" paddingX=${1} marginTop=${1}>
|
|
72
|
-
${
|
|
85
|
+
${lines.map((cheer, i) => {
|
|
73
86
|
const style = CHEER_STYLE[cheer.kind] ?? CHEER_STYLE.combo;
|
|
74
87
|
return html`<${Text} key=${i} color=${style.color} bold>${style.icon} ${cheer.text}<//>`;
|
|
75
88
|
})}
|
|
89
|
+
${badges.map((badge, i) => html`<${BadgeUnlock} key=${badge.id} ...${badge} delay=${BADGE_FIRST_MS + i * BADGE_STAGGER_MS} sound=${badgeSound(i)} play=${play} />`)}
|
|
76
90
|
<//>
|
|
77
91
|
`;
|
|
78
92
|
}
|
|
79
93
|
|
|
80
94
|
// The card's top edge with the direction ("EN → PL") set into its right corner, like a title on the border
|
|
81
95
|
function CardTop({ label, width, color }) {
|
|
82
|
-
const tag = ` ${label} `;
|
|
83
96
|
const tail = 2;
|
|
84
|
-
const
|
|
97
|
+
const room = Math.max(4, width - 2 - tail - 4); // a label too long for a narrow window is cut short
|
|
98
|
+
const tag = ` ${cutToWidth(label, room)} `;
|
|
99
|
+
const dashes = Math.max(0, width - 2 - cellWidth(tag) - tail);
|
|
85
100
|
return html`
|
|
86
101
|
<${Text}>
|
|
87
102
|
<${Text} color=${color}>╭${"─".repeat(dashes)}<//>
|
|
@@ -91,6 +106,35 @@ function CardTop({ label, width, color }) {
|
|
|
91
106
|
`;
|
|
92
107
|
}
|
|
93
108
|
|
|
109
|
+
// "at 14:30", "tomorrow", "on 14 Oct"
|
|
110
|
+
function whenDue(at, now) {
|
|
111
|
+
const tomorrow = startOfTomorrow(now);
|
|
112
|
+
if (at < tomorrow) return `at ${formatClock(at)}`;
|
|
113
|
+
return at < startOfTomorrow(tomorrow) ? "tomorrow" : `on ${formatDay(dayKey(at))}`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// what the quiz shows when the deck has nothing to ask: no countdown, just the way on
|
|
117
|
+
function EmptyDeck({ deck, tour, count, nextDue, dayDone, now, width }) {
|
|
118
|
+
const [title, next] = tour
|
|
119
|
+
? ["You've finished the tour.", "Now make it yours: type /add to start your own deck."]
|
|
120
|
+
: !count
|
|
121
|
+
? [`"${deck}" has no cards yet.`, "Type /add to add the first one."]
|
|
122
|
+
: nextDue >= startOfTomorrow(now) && !dayDone
|
|
123
|
+
? [`Nothing more in "${deck}" today.`, `Its cards come back ${whenDue(nextDue, now)}. Other decks still have cards: /decks`]
|
|
124
|
+
: nextDue >= startOfTomorrow(now)
|
|
125
|
+
? ["All done for today.", `Your cards come back ${whenDue(nextDue, now)}. Want more today? /add some new ones.`]
|
|
126
|
+
: ["Nothing to practise right now.", `The next card is due ${whenDue(nextDue, now)}. It will come by itself.`];
|
|
127
|
+
return html`
|
|
128
|
+
<${Box} flexDirection="column" width=${width}>
|
|
129
|
+
<${CardTop} label=${deck} width=${width} color=${theme.accent} />
|
|
130
|
+
<${Box} flexDirection="column" borderStyle="round" borderTop=${false} borderColor=${theme.accent} paddingX=${2} paddingY=${1}>
|
|
131
|
+
<${Text} bold>${title}<//>
|
|
132
|
+
<${Text}>${next}<//>
|
|
133
|
+
<//>
|
|
134
|
+
<//>
|
|
135
|
+
`;
|
|
136
|
+
}
|
|
137
|
+
|
|
94
138
|
function TipBox({ tip, width }) {
|
|
95
139
|
return html`
|
|
96
140
|
<${Box} flexDirection="column" paddingX=${1} marginTop=${2} width=${width}>
|
|
@@ -100,15 +144,21 @@ function TipBox({ tip, width }) {
|
|
|
100
144
|
`;
|
|
101
145
|
}
|
|
102
146
|
|
|
103
|
-
|
|
147
|
+
// `library` holds the /add actions (src/manage.js) and `onSwitchDeck` points the session at another
|
|
148
|
+
// deck; the preview gallery passes neither.
|
|
149
|
+
export function App({ session, deck: initialDeck, initialSettings, alerts, persistSettings, onExit, library, onSwitchDeck }) {
|
|
104
150
|
const { exit: exitApp } = useApp();
|
|
105
151
|
const exit = onExit ?? exitApp; // the preview gallery runs the quiz as a scene and takes quitting back to its menu
|
|
106
152
|
const { stdout } = useStdout();
|
|
107
153
|
const [settings, setSettings] = useState(initialSettings);
|
|
108
|
-
|
|
154
|
+
const [deck, setDeck] = useState(initialDeck);
|
|
155
|
+
const [addNew, setAddNew] = useState(false); // /add opened from "+ New deck" in /decks
|
|
156
|
+
const [restUntil, setRestUntil] = useState(null); // nothing is waiting until then (after the last card due now)
|
|
157
|
+
const [empty, setEmpty] = useState(null); // { tour, count, nextDue } when there is nothing to ask right now
|
|
158
|
+
// quiz | settings | reset | add | decks | stats | badges | companion | help | missed | evolve | the-one | die | summary | hatch
|
|
109
159
|
const [screen, setScreen] = useState(() => (session.isFirstRun() ? "hatch" : "quiz"));
|
|
110
160
|
const [tip, setTip] = useState(null);
|
|
111
|
-
const [phase, setPhase] = useState("loading"); // loading | asking | waiting
|
|
161
|
+
const [phase, setPhase] = useState("loading"); // loading | asking | waiting | empty
|
|
112
162
|
const [question, setQuestion] = useState(null);
|
|
113
163
|
const [outcome, setOutcome] = useState(null);
|
|
114
164
|
const [problem, setProblem] = useState("");
|
|
@@ -121,6 +171,8 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
121
171
|
const [petEvent, setPetEvent] = useState(null); // { kind: "jump" | "shake", at } → a little hop or head-shake
|
|
122
172
|
const [confetti, setConfetti] = useState(false);
|
|
123
173
|
const [evolution, setEvolution] = useState(null); // { from, to, name } while the ceremony plays
|
|
174
|
+
const [freshBadges, setFreshBadges] = useState([]); // won since /badges was last opened; they light up there
|
|
175
|
+
const [ceremonies, setCeremonies] = useState([]); // full-screen moments still to show: "evolve", "the-one"
|
|
124
176
|
const [lastActive, setLastActive] = useState(Date.now()); // last key press or new question: the dragon sleeps after a long gap
|
|
125
177
|
const busy = useRef(false);
|
|
126
178
|
const asleepRef = useRef(false);
|
|
@@ -136,10 +188,13 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
136
188
|
return Math.max(snoozeRef.current > t ? snoozeRef.current : 0, inQuietHours(t, quiet) ? quietEnd(t, quiet) : 0);
|
|
137
189
|
};
|
|
138
190
|
|
|
139
|
-
|
|
140
|
-
|
|
191
|
+
// `nextDue`: nothing is waiting until then, so the next question waits for it (never sooner than usual)
|
|
192
|
+
const waitForNext = useCallback((nextDue = null) => {
|
|
193
|
+
const usual = Date.now() + settingsRef.current.everyMs;
|
|
194
|
+
setNextAt(nextDue ? Math.max(nextDue, usual) : usual);
|
|
195
|
+
setRestUntil(nextDue);
|
|
141
196
|
setPhase("waiting");
|
|
142
|
-
setTip(shouldShowTip(settingsRef.current.tips) ? session.nextTip() : null);
|
|
197
|
+
setTip(!session.inTour() && shouldShowTip(settingsRef.current.tips) ? session.nextTip() : null);
|
|
143
198
|
}, [session]);
|
|
144
199
|
|
|
145
200
|
const ask = useCallback(async () => {
|
|
@@ -153,10 +208,18 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
153
208
|
const next = await session.next();
|
|
154
209
|
setQuestion(next);
|
|
155
210
|
setOutcome(null);
|
|
211
|
+
setEmpty(null);
|
|
156
212
|
setPhase("asking");
|
|
157
213
|
} catch (err) {
|
|
158
|
-
|
|
159
|
-
|
|
214
|
+
if (!err.empty) {
|
|
215
|
+
setProblem(err.message);
|
|
216
|
+
return waitForNext();
|
|
217
|
+
}
|
|
218
|
+
setQuestion(null);
|
|
219
|
+
setEmpty({ tour: err.tour, count: err.count, nextDue: err.nextDue, dayDone: err.dayDone });
|
|
220
|
+
setOutcome(err.cheers.length ? { result: "done", cheers: err.cheers } : null);
|
|
221
|
+
if (err.cheers.length) celebrate({ result: "exact", cheers: err.cheers, combo: session.totals.combo });
|
|
222
|
+
setPhase("empty");
|
|
160
223
|
}
|
|
161
224
|
}, [session, waitForNext]);
|
|
162
225
|
|
|
@@ -170,8 +233,15 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
170
233
|
return () => clearInterval(id);
|
|
171
234
|
}, []);
|
|
172
235
|
|
|
236
|
+
// with nothing to practise, the quiz picks up again by itself once the next card is due
|
|
173
237
|
useEffect(() => {
|
|
174
|
-
if (phase !== "
|
|
238
|
+
if (phase !== "empty" || !empty?.nextDue || screen !== "quiz" || now < empty.nextDue || pauseEnd(now)) return;
|
|
239
|
+
alerts.ask();
|
|
240
|
+
ask();
|
|
241
|
+
}, [now, phase, empty, screen, ask, alerts]);
|
|
242
|
+
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (phase !== "waiting" || screen === "summary" || screen === "evolve" || screen === "the-one" || screen === "die") return;
|
|
175
245
|
if (now >= nextAt && !pauseEnd(now)) {
|
|
176
246
|
alerts.ask();
|
|
177
247
|
ask();
|
|
@@ -191,10 +261,10 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
191
261
|
});
|
|
192
262
|
|
|
193
263
|
useInput((input, key) => {
|
|
194
|
-
if (screen !== "quiz" || phase !== "waiting" || commandMode) return;
|
|
264
|
+
if (screen !== "quiz" || (phase !== "waiting" && phase !== "empty") || commandMode) return;
|
|
195
265
|
if (input === "/") return setCommandMode(true);
|
|
196
266
|
if (key.escape || input === "q") return requestExit();
|
|
197
|
-
if (key.return) ask();
|
|
267
|
+
if (key.return && phase === "waiting") ask();
|
|
198
268
|
});
|
|
199
269
|
|
|
200
270
|
const clearProblem = useCallback(() => {
|
|
@@ -208,49 +278,69 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
208
278
|
bestCombo.current = Math.max(bestCombo.current, result.combo);
|
|
209
279
|
setPetEvent({ kind: result.result === "wrong" ? "shake" : "jump", at: Date.now() });
|
|
210
280
|
if (result.cheers.some((c) => c.kind === "goal")) setConfetti(true);
|
|
281
|
+
const queue = [];
|
|
211
282
|
if (result.cheers.some((c) => c.kind === "evolve")) {
|
|
212
283
|
const companion = session.stats().companion;
|
|
213
|
-
setEvolution({ from: Math.max(0, companion.index - 1), to: companion.index, name: companion.name });
|
|
214
|
-
|
|
284
|
+
setEvolution({ from: Math.max(0, companion.index - 1), to: companion.index, name: companion.name, element: companion.element });
|
|
285
|
+
queue.push("evolve");
|
|
286
|
+
}
|
|
287
|
+
if (result.cheers.some((c) => c.id === "dragons-die")) queue.push("die");
|
|
288
|
+
if (result.cheers.some((c) => c.id === "one-ring")) queue.push("the-one");
|
|
289
|
+
if (queue.length) {
|
|
290
|
+
setCeremonies(queue.slice(1));
|
|
291
|
+
setScreen(queue[0]);
|
|
215
292
|
}
|
|
216
293
|
}
|
|
217
294
|
|
|
295
|
+
// after a full-screen ceremony: the next one if both happened at once, otherwise back to the quiz
|
|
296
|
+
function nextCeremony() {
|
|
297
|
+
const [next, ...rest] = ceremonies;
|
|
298
|
+
setCeremonies(rest);
|
|
299
|
+
setScreen(next ?? "quiz");
|
|
300
|
+
}
|
|
301
|
+
|
|
218
302
|
function runCommand(name, args = "") {
|
|
219
303
|
setCommandMode(false);
|
|
220
304
|
if (name === "/quit") return requestExit();
|
|
221
305
|
if (name === "/settings") return setScreen("settings");
|
|
222
306
|
if (name === "/stats") return setScreen("stats");
|
|
223
|
-
if (name === "/badges")
|
|
307
|
+
if (name === "/badges") {
|
|
308
|
+
return session
|
|
309
|
+
.openBadges()
|
|
310
|
+
.then((fresh) => {
|
|
311
|
+
setFreshBadges(fresh);
|
|
312
|
+
setScreen("badges");
|
|
313
|
+
})
|
|
314
|
+
.catch((err) => setProblem(err.message));
|
|
315
|
+
}
|
|
224
316
|
if (name === "/companion") return setScreen("companion");
|
|
225
|
-
if (name === "/tip") return setScreen("tip");
|
|
226
317
|
if (name === "/missed") return setScreen("missed");
|
|
318
|
+
if (name === "/help") return setScreen("help");
|
|
319
|
+
if (name === "/add") return library ? (setAddNew(false), setScreen("add")) : setNotice("The preview can't add cards; run dracosh for that.");
|
|
320
|
+
if (name === "/decks") return library ? setScreen("decks") : setNotice("The preview has one demo deck; run dracosh for yours.");
|
|
227
321
|
if (name === "/hint" && phase === "asking") return setHintLevel((level) => level + 1);
|
|
228
|
-
if (name === "/
|
|
322
|
+
if (name === "/pause") {
|
|
229
323
|
return session
|
|
230
|
-
.
|
|
231
|
-
.then(
|
|
324
|
+
.pause(args)
|
|
325
|
+
.then(({ snooze, list, message }) => {
|
|
326
|
+
if (snooze === "off") setSnoozeUntil(0);
|
|
327
|
+
if (typeof snooze === "number") {
|
|
328
|
+
setSnoozeUntil(Date.now() + snooze);
|
|
329
|
+
return phase === "asking" ? setNotice(`Paused until ${formatClock(Date.now() + snooze)}`) : undefined; // while waiting, the status line says it
|
|
330
|
+
}
|
|
331
|
+
const paused = list && snoozeRef.current > Date.now() ? `Paused until ${formatClock(snoozeRef.current)}. ` : "";
|
|
332
|
+
setNotice(paused + message);
|
|
333
|
+
})
|
|
232
334
|
.catch((err) => setProblem(err.message));
|
|
233
335
|
}
|
|
234
|
-
if (name === "/snooze") {
|
|
235
|
-
const length = parseSnooze(args);
|
|
236
|
-
if (length === null) return setProblem("Try /snooze 30m, /snooze 2h or /snooze off");
|
|
237
|
-
if (length === "off") {
|
|
238
|
-
setSnoozeUntil(0);
|
|
239
|
-
return setNotice("Snooze off: questions are back");
|
|
240
|
-
}
|
|
241
|
-
setSnoozeUntil(Date.now() + length);
|
|
242
|
-
return phase === "asking" ? setNotice(`Questions paused until ${formatClock(Date.now() + length)}`) : undefined; // while waiting, the status line says it
|
|
243
|
-
}
|
|
244
|
-
if (name === "/skip") {
|
|
245
|
-
setOutcome({ result: "skipped", cheers: [] });
|
|
246
|
-
return waitForNext();
|
|
247
|
-
}
|
|
248
336
|
if (name === "/correct" && session.canOverrule()) {
|
|
249
337
|
return session
|
|
250
338
|
.overrule()
|
|
251
339
|
.then((result) => {
|
|
252
340
|
setOutcome(result);
|
|
253
341
|
celebrate(result);
|
|
342
|
+
if (phase === "empty") return ask(); // the card's next date changed: look again
|
|
343
|
+
waitForNext(result.nextDue);
|
|
254
344
|
})
|
|
255
345
|
.catch((err) => setProblem(`Couldn't save progress: ${err.message}`));
|
|
256
346
|
}
|
|
@@ -268,35 +358,77 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
268
358
|
if (patch.volume !== undefined || patch.sound === true) alerts.play("correct-0"); // audible preview
|
|
269
359
|
}
|
|
270
360
|
|
|
361
|
+
// an empty answer means "I don't know": the answer is shown and the card counts as missed
|
|
271
362
|
async function submit(raw) {
|
|
272
363
|
const text = raw.trim();
|
|
273
|
-
if (
|
|
364
|
+
if (busy.current) return;
|
|
274
365
|
busy.current = true;
|
|
366
|
+
let nextDue = null;
|
|
275
367
|
try {
|
|
276
368
|
const result = await session.answer(question, text, { hinted: hintLevel > 0 });
|
|
369
|
+
nextDue = result.nextDue;
|
|
277
370
|
setOutcome(result);
|
|
278
371
|
celebrate(result);
|
|
279
372
|
} catch (err) {
|
|
280
373
|
setProblem(`Couldn't save progress: ${err.message}`);
|
|
281
374
|
} finally {
|
|
282
375
|
busy.current = false;
|
|
283
|
-
waitForNext();
|
|
376
|
+
waitForNext(nextDue);
|
|
284
377
|
}
|
|
285
378
|
}
|
|
286
379
|
|
|
287
|
-
|
|
288
|
-
|
|
380
|
+
// /decks and a first deck from /add: quiz another deck from now on, and remember it
|
|
381
|
+
function switchDeck(name) {
|
|
382
|
+
setScreen("quiz");
|
|
383
|
+
if (name === deck) return;
|
|
384
|
+
onSwitchDeck?.(name);
|
|
385
|
+
setDeck(name);
|
|
386
|
+
changeSettings({ deck: name });
|
|
387
|
+
ask();
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Reset (from /settings), after a backup. Everything brings back the tour and the hatching egg.
|
|
391
|
+
async function reset(scope) {
|
|
392
|
+
await library.backupBeforeReset();
|
|
393
|
+
await session.reset(scope);
|
|
394
|
+
setFreshBadges([]);
|
|
395
|
+
if (scope === "progress") {
|
|
396
|
+
setScreen("quiz");
|
|
397
|
+
return setNotice("Your progress starts over. Your cards are all still here.");
|
|
398
|
+
}
|
|
399
|
+
const tour = await library.resetLibrary();
|
|
400
|
+
onSwitchDeck?.(tour);
|
|
401
|
+
setDeck(tour);
|
|
402
|
+
changeSettings({ deck: null });
|
|
403
|
+
setScreen("hatch");
|
|
404
|
+
ask();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// after /add: a first deck of your own replaces the tour; a deck started from /decks (or in place
|
|
408
|
+
// of an empty one) becomes the one you practise; new cards end an empty state
|
|
409
|
+
function addDone({ deck: started, removed, added, dropped }) {
|
|
410
|
+
setScreen("quiz");
|
|
411
|
+
if (removed.length) session.forget(removed).catch(() => {});
|
|
412
|
+
const switching = started && (removed.length || phase === "empty" || addNew);
|
|
413
|
+
if (switching) switchDeck(started);
|
|
414
|
+
else if (phase === "empty" && added) ask();
|
|
415
|
+
const notes = [dropped && "The card you were writing wasn't saved.", started && !switching && `"${started}" is ready; you're still practising "${deck}". Switch with /decks.`];
|
|
416
|
+
setNotice(notes.filter(Boolean).join(" ")); // after ask(), which clears the notice
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const width = usePanelWidth();
|
|
420
|
+
const border = !outcome ? theme.accent : outcome.result === "wrong" ? theme.bad : theme.good;
|
|
289
421
|
const pausedUntil = pauseEnd(now);
|
|
290
422
|
const tick = Math.floor(now / TICK_MS);
|
|
291
423
|
const frame = SPINNER[tick % SPINNER.length];
|
|
292
424
|
const stats = session.stats();
|
|
293
425
|
// mood: how the last answer went, otherwise whether today's goal is done (Header adds the blinking)
|
|
294
426
|
const resting = stats.today.goalMet ? "smile" : "idle";
|
|
295
|
-
const face = outcome?.result === "wrong" ? "sad" : outcome
|
|
427
|
+
const face = outcome?.result === "wrong" ? "sad" : outcome ? "happy" : resting;
|
|
296
428
|
// strictly boolean: a bare 0 leaking into the markup crashes Ink ("Text string must be rendered inside <Text>")
|
|
297
429
|
const paused = pausedUntil > nextAt || (now >= nextAt && pausedUntil > 0);
|
|
298
430
|
// asleep: no key pressed for a while, whether a question is open or not; a key always wakes it,
|
|
299
|
-
// even during
|
|
431
|
+
// even during a pause or quiet hours (where it nods off again sooner)
|
|
300
432
|
const idleLimit = phase === "waiting" && pausedUntil > 0 ? PAUSED_SLEEP_AFTER_MS : SLEEP_AFTER_MS;
|
|
301
433
|
const asleep = phase !== "loading" && now - lastActive > idleLimit;
|
|
302
434
|
asleepRef.current = asleep;
|
|
@@ -310,28 +442,34 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
310
442
|
if (screen === "evolve" && evolution)
|
|
311
443
|
return html`<${Box} flexDirection="column" marginY=${1}><${Evolution} ...${evolution} onClose=${() => {
|
|
312
444
|
setEvolution(null);
|
|
313
|
-
|
|
445
|
+
nextCeremony();
|
|
314
446
|
}} /><//>`;
|
|
447
|
+
if (screen === "die")
|
|
448
|
+
return html`<${Box} flexDirection="column" marginY=${1}><${DieRoll} element=${stats.companion.element} stage=${stats.companion.index} play=${alerts.play} onClose=${nextCeremony} /><//>`;
|
|
449
|
+
if (screen === "the-one") return html`<${Box} flexDirection="column" marginY=${1}><${TheOne} play=${alerts.play} onClose=${nextCeremony} /><//>`;
|
|
315
450
|
if (screen === "stats") return html`<${Box} flexDirection="column" marginY=${1}><${Stats} stats=${stats} getMonth=${session.month} onClose=${() => setScreen("quiz")} /><//>`;
|
|
316
|
-
if (screen === "companion") return html`<${Box} flexDirection="column" marginY=${1}><${Companion} current=${stats.companion.index} best=${stats.streak.best} onClose=${() => setScreen("quiz")} /><//>`;
|
|
317
|
-
if (screen === "
|
|
451
|
+
if (screen === "companion") return html`<${Box} flexDirection="column" marginY=${1}><${Companion} current=${stats.companion.index} best=${stats.streak.best} element=${stats.companion.element} roll=${stats.companion.roll} onClose=${() => setScreen("quiz")} /><//>`;
|
|
452
|
+
if (screen === "help") return html`<${Box} flexDirection="column" marginY=${1}><${Help} onClose=${() => setScreen("quiz")} /><//>`;
|
|
318
453
|
if (screen === "missed") return html`<${Box} flexDirection="column" marginY=${1}><${Missed} getMissed=${session.missed} onClose=${() => setScreen("quiz")} /><//>`;
|
|
319
|
-
if (screen === "badges") return html`<${Box} flexDirection="column" marginY=${1}><${Badges} badges=${stats.badges} onClose=${() => setScreen("quiz")} /><//>`;
|
|
454
|
+
if (screen === "badges") return html`<${Box} flexDirection="column" marginY=${1}><${Badges} badges=${stats.badges} fresh=${freshBadges} onClose=${() => setScreen("quiz")} /><//>`;
|
|
320
455
|
|
|
321
456
|
return html`
|
|
322
457
|
<${Box} flexDirection="column" marginY=${1}>
|
|
323
|
-
<${Header} deck=${deck} everyMs=${settings.everyMs} wordCount=${question?.wordCount} stats=${stats} combo=${session.totals.combo} face=${face} event=${petEvent} tick=${tick} asleep=${asleep} />
|
|
458
|
+
<${Header} deck=${deck} everyMs=${settings.everyMs} wordCount=${question?.wordCount ?? empty?.count} stats=${stats} combo=${session.totals.combo} face=${face} event=${petEvent} tick=${tick} asleep=${asleep} />
|
|
324
459
|
<${Box} marginTop=${1} flexDirection="column">
|
|
325
|
-
${screen === "settings"
|
|
326
|
-
|
|
327
|
-
|
|
460
|
+
${screen === "settings" && html`<${Settings} settings=${settings} onChange=${changeSettings} onClose=${() => setScreen("quiz")} onAction=${library ? () => setScreen("reset") : null} />`}
|
|
461
|
+
${screen === "reset" && html`<${Reset} stats=${stats} actions=${library} onConfirm=${reset} onClose=${() => setScreen("settings")} />`}
|
|
462
|
+
${screen === "add" && html`<${AddCard} current=${deck} actions=${library} onDone=${addDone} startNew=${addNew} />`}
|
|
463
|
+
${screen === "decks" && html`<${Decks} current=${deck} actions=${library} onPick=${switchDeck} onNew=${() => (setAddNew(true), setScreen("add"))} onClose=${() => setScreen("quiz")} />`}
|
|
464
|
+
${!["settings", "reset", "add", "decks"].includes(screen) &&
|
|
465
|
+
html`
|
|
328
466
|
<${Fragment}>
|
|
329
467
|
${question &&
|
|
330
468
|
html`
|
|
331
469
|
<${Box} flexDirection="column" width=${width}>
|
|
332
470
|
<${CardTop} label=${question.label} width=${width} color=${border} />
|
|
333
471
|
<${Box} flexDirection="column" borderStyle="round" borderTop=${false} borderColor=${border} paddingX=${2} paddingTop=${1}>
|
|
334
|
-
<${
|
|
472
|
+
<${Marked} bold text=${question.prompt} plain=${!question.tour} />
|
|
335
473
|
${phase === "asking" && hintLevel > 0 && html`<${Text} dimColor>hint ${makeHint(hintTarget(question.expected), hintLevel)}<//>`}
|
|
336
474
|
<${Box} marginTop=${1} flexDirection="column">
|
|
337
475
|
${phase === "asking" && html`<${AnswerInput} key=${question.word.noteId + question.direction} onSubmit=${submit} onCommand=${runCommand} onExit=${requestExit} onEdit=${clearProblem} commands=${commandsFor({ phase: "asking" })} />`}
|
|
@@ -340,26 +478,28 @@ export function App({ session, deck, initialSettings, alerts, persistSettings, o
|
|
|
340
478
|
<//>
|
|
341
479
|
<//>
|
|
342
480
|
`}
|
|
481
|
+
${phase === "empty" && html`<${EmptyDeck} deck=${deck} ...${empty} now=${now} width=${width} />`}
|
|
343
482
|
${confetti && html`<${Confetti} width=${width} onDone=${() => setConfetti(false)} />`}
|
|
344
|
-
${outcome?.cheers?.length > 0 && html`<${Cheers} cheers=${outcome.cheers} />`}
|
|
483
|
+
${outcome?.cheers?.length > 0 && html`<${Cheers} cheers=${outcome.cheers} play=${alerts.play} />`}
|
|
345
484
|
${phase === "waiting" && tip && html`<${TipBox} tip=${tip} width=${width} />`}
|
|
346
485
|
<${Box} paddingX=${1} marginTop=${1} flexDirection="column">
|
|
347
|
-
${question?.cached && html`<${Text} color=${theme.warn}>Anki is offline, using the saved word list<//>`}
|
|
348
486
|
${problem && html`<${Text} color=${theme.bad}>${problem}<//>`}
|
|
349
|
-
${phase === "loading" && html`<${Text}
|
|
487
|
+
${phase === "loading" && html`<${Text}><${Text} color=${theme.accent}>${frame}<//><${Text} dimColor> loading…<//><//>`}
|
|
350
488
|
${notice && html`<${Text} color=${theme.accent}>${notice}<//>`}
|
|
351
489
|
${phase === "waiting" && paused && html`<${Text} dimColor>paused until ${formatClock(pausedUntil)}${inQuietHours(now, settings.quiet) ? " (quiet hours)" : ""}<//>`}
|
|
490
|
+
${phase === "waiting" && !paused && restUntil && nextAt - now > settings.everyMs && html`<${Text} dimColor>nothing waiting · next card ${whenDue(restUntil, now)}<//>`}
|
|
352
491
|
${phase === "waiting" &&
|
|
353
492
|
!paused &&
|
|
493
|
+
!(restUntil && nextAt - now > settings.everyMs) &&
|
|
354
494
|
html`
|
|
355
495
|
<${Text}>
|
|
356
|
-
<${Text}
|
|
496
|
+
<${Text} color=${theme.accent}>${frame} <//>
|
|
357
497
|
<${Bar} value=${Math.max(0, nextAt - now)} max=${settings.everyMs} width=${14} color="#6b5e8a" />
|
|
358
|
-
<${Text} dimColor> next
|
|
498
|
+
<${Text} dimColor> next card in ${clock(nextAt - now)}<//>
|
|
359
499
|
<//>
|
|
360
500
|
`}
|
|
361
|
-
${phase === "waiting" && commandMode && html`<${AnswerInput} commandOnly initial="/" commands=${commandsFor({ phase: "waiting", canOverrule: session.canOverrule() })} onCommand=${runCommand} onCancel=${() => setCommandMode(false)} onEdit=${clearProblem} />`}
|
|
362
|
-
${phase !== "loading" && !commandMode && html`<${
|
|
501
|
+
${(phase === "waiting" || phase === "empty") && commandMode && html`<${AnswerInput} commandOnly initial="/" commands=${commandsFor({ phase: "waiting", canOverrule: session.canOverrule() })} onCommand=${runCommand} onCancel=${() => setCommandMode(false)} onEdit=${clearProblem} />`}
|
|
502
|
+
${phase !== "loading" && !commandMode && html`<${KeyHints} text=${phase === "asking" ? "enter submit (empty: reveal) · / commands · esc quit" : phase === "empty" ? "/add · / commands · q quit" : `${session.canOverrule() ? "/correct if you were right · " : ""}enter ask now · / commands · q quit`} />`}
|
|
363
503
|
<//>
|
|
364
504
|
<//>
|
|
365
505
|
`}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { Box, Text } from "ink";
|
|
3
|
+
import { badgeColor } from "./icons.js";
|
|
4
|
+
import { html, theme } from "./kit.js";
|
|
5
|
+
|
|
6
|
+
const FRAME_MS = 90;
|
|
7
|
+
const FLASH_FRAMES = [5, 7]; // white-out frames before the name lights up in its rank color
|
|
8
|
+
const COLOR_FROM = 8;
|
|
9
|
+
const SPARKS_UNTIL = 22;
|
|
10
|
+
const SPARKS = ["✦", "·", "✧", "·"];
|
|
11
|
+
const GREY = "#4a5058";
|
|
12
|
+
const WHITE = "#ffffff";
|
|
13
|
+
const STEPS = 4; // badge-1 … badge-4 (scripts/make-sounds.js)
|
|
14
|
+
|
|
15
|
+
// Badges won with the same answer ring one after another, each a step higher. The climb starts
|
|
16
|
+
// over with every answer: a badge won later, on its own, plays the first step again.
|
|
17
|
+
export const badgeSound = (i) => `badge-${Math.min(i + 1, STEPS)}`;
|
|
18
|
+
|
|
19
|
+
// A badge just won, as one line under the card: the name comes out of grey, flashes white and
|
|
20
|
+
// lights up in its rank color while a few sparks fly. The badge art lives in /badges.
|
|
21
|
+
// `delay` staggers several badges won at once; `sound` rings as the line appears.
|
|
22
|
+
export function BadgeUnlock({ id, name, desc, delay = 0, sound = badgeSound(0), play }) {
|
|
23
|
+
const [frame, setFrame] = useState(-Math.round(delay / FRAME_MS));
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (frame === 0) play?.(sound);
|
|
26
|
+
}, [frame === 0]);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const timer = setInterval(() => {
|
|
29
|
+
setFrame((f) => {
|
|
30
|
+
if (f >= SPARKS_UNTIL) clearInterval(timer);
|
|
31
|
+
return f + 1;
|
|
32
|
+
});
|
|
33
|
+
}, FRAME_MS);
|
|
34
|
+
return () => clearInterval(timer);
|
|
35
|
+
}, []);
|
|
36
|
+
|
|
37
|
+
if (frame < 0) return null;
|
|
38
|
+
const color = FLASH_FRAMES.includes(frame) ? WHITE : frame >= COLOR_FROM ? badgeColor(id) : GREY;
|
|
39
|
+
const sparks = frame < SPARKS_UNTIL ? ` ${Array.from({ length: 3 }, (_, i) => SPARKS[(frame + i) % SPARKS.length]).join(" ")}` : "";
|
|
40
|
+
|
|
41
|
+
return html`
|
|
42
|
+
<${Box}>
|
|
43
|
+
<${Text} color=${color}>✦ <//>
|
|
44
|
+
<${Text} color=${theme.warn}>Badge unlocked: <//>
|
|
45
|
+
<${Text} bold color=${color}>${name}<//>
|
|
46
|
+
<${Text} dimColor> · ${desc}<//>
|
|
47
|
+
<${Text} color=${theme.warn}>${sparks}<//>
|
|
48
|
+
<//>
|
|
49
|
+
`;
|
|
50
|
+
}
|