dracosh 0.2.0 → 0.4.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 +25 -17
- package/locales/de.json +1098 -0
- package/locales/en.json +1081 -0
- package/locales/fr.json +1148 -0
- package/locales/pl.json +1173 -0
- package/locales/uk.json +1174 -0
- package/package.json +2 -2
- package/src/alerts.js +71 -4
- package/src/backup.js +2 -1
- package/src/cardline.js +71 -0
- package/src/cli.js +41 -54
- package/src/commands.js +14 -12
- package/src/demo.js +11 -0
- package/src/diff.js +20 -10
- package/src/i18n.js +68 -0
- package/src/judge.js +164 -13
- package/src/language.js +355 -0
- package/src/library.js +91 -38
- package/src/lock.js +2 -1
- package/src/manage.js +193 -58
- package/src/pause.js +1 -2
- package/src/progress.js +119 -63
- package/src/quiet.js +3 -2
- package/src/scheduler.js +26 -9
- package/src/session.js +118 -36
- package/src/settings.js +14 -6
- package/src/starter.js +36 -27
- package/src/store.js +11 -8
- package/src/tips.js +12 -3
- package/src/ui/AddCard.js +449 -81
- package/src/ui/AnswerInput.js +22 -10
- package/src/ui/App.js +209 -57
- package/src/ui/BadgeUnlock.js +5 -4
- package/src/ui/Badges.js +9 -8
- package/src/ui/Choice.js +14 -9
- package/src/ui/Companion.js +13 -13
- package/src/ui/Confetti.js +2 -2
- package/src/ui/Decks.js +167 -9
- package/src/ui/DieRoll.js +9 -9
- package/src/ui/Evolution.js +7 -7
- package/src/ui/Hatch.js +7 -6
- package/src/ui/Header.js +58 -16
- package/src/ui/Help.js +43 -25
- package/src/ui/Languages.js +167 -0
- package/src/ui/Mascot.js +1 -2
- package/src/ui/Missed.js +13 -12
- package/src/ui/Preview.js +48 -29
- package/src/ui/Reset.js +13 -14
- package/src/ui/Settings.js +27 -19
- package/src/ui/Stats.js +24 -23
- package/src/ui/Summary.js +30 -15
- package/src/ui/TheOne.js +7 -6
- package/src/ui/icons.js +1 -2
- package/src/ui/kit.js +34 -6
- package/src/vacation.js +2 -1
- package/src/words.js +8 -2
- package/data/tips.json +0 -32
package/src/session.js
CHANGED
|
@@ -1,24 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { t as say } from "./i18n.js"; // `t` and `text` are taken here (the clock, an answer)
|
|
2
|
+
import { blankGap, plainFront } from "./cardline.js";
|
|
3
|
+
import { ALMOST, closestCandidate, judge } from "./judge.js";
|
|
4
|
+
import { addVacation, applyAnswer, applyCaughtUp, markWorkWaiting, applyOverrule, calendarMonth, cancelVacations, ensureProgress, markBadgesSeen, missedOn, recordMiss, snapshot, undoMiss, unseenBadges, vacationInfo } from "./progress.js";
|
|
5
|
+
import { BOX_INTERVALS_DAYS, describeDue, grade, itemKey, nextDueAt, pickNext, startOfTomorrow } from "./scheduler.js";
|
|
4
6
|
import { recordAnswer, saveState } from "./store.js";
|
|
5
7
|
import { createTipDeck } from "./tips.js";
|
|
6
8
|
import { dayKey } from "./dates.js";
|
|
7
|
-
import {
|
|
9
|
+
import { parsePause } from "./pause.js";
|
|
8
10
|
import { formatRange } from "./vacation.js";
|
|
9
11
|
|
|
10
|
-
const LABELS = { "en-pl": "EN → PL", "pl-en": "PL → EN" };
|
|
11
12
|
|
|
12
|
-
// the tag on the card's border: the language
|
|
13
|
-
function labelOf(deck
|
|
14
|
-
|
|
15
|
-
if (!deck.languages) return deck.name;
|
|
16
|
-
const { front, back } = deck.languages;
|
|
17
|
-
const [from, to] = direction === "en-pl" ? [front, back] : [back, front];
|
|
18
|
-
return `${from.toUpperCase()} → ${to.toUpperCase()}`;
|
|
13
|
+
// the tag on the card's border: the deck's name (language codes like "DE → UK" read as countries)
|
|
14
|
+
function labelOf(deck) {
|
|
15
|
+
return deck?.name ?? "";
|
|
19
16
|
}
|
|
20
17
|
const COMBO_CHEER_EVERY = 5;
|
|
21
18
|
|
|
19
|
+
// Pronunciation reads the side in the language you're learning in this deck (learningSide), whichever way
|
|
20
|
+
// the card asks: the answer (a cloze card: its whole sentence), or the question. Nothing in a deck that
|
|
21
|
+
// isn't for a language. `answer`: the right answer you gave (one of several), read instead of the first.
|
|
22
|
+
export function toSpeak(question, answer = null) {
|
|
23
|
+
const lang = question.learningLanguage;
|
|
24
|
+
if (question.tour || !lang) return null;
|
|
25
|
+
const cloze = question.direction.startsWith("c");
|
|
26
|
+
const given = answer && question.expected.includes(answer) ? answer : question.expected[0];
|
|
27
|
+
if (question.answerLanguage === lang) return { text: cloze ? plainFront(question.word.word) : given, lang };
|
|
28
|
+
// a cloze card's prompt is its sentence with a gap: nothing to read there
|
|
29
|
+
if (!cloze && question.promptLanguage === lang) return { text: question.prompt.split(" / ")[0], lang };
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
22
33
|
// Quiz logic without any I/O of its own: words, settings and persistence are injected, the UI only renders what this returns.
|
|
23
34
|
// `loadAllWords` → [{ words, deck }] for every deck of yours: "all done for today" means the whole library.
|
|
24
35
|
export function createSession({ loadWords, loadAllWords, state, getSettings, save = saveState, now = Date.now, tipDeck = createTipDeck({ state }) }) {
|
|
@@ -33,13 +44,18 @@ export function createSession({ loadWords, loadAllWords, state, getSettings, sav
|
|
|
33
44
|
async function settleDay() {
|
|
34
45
|
const decks = loadAllWords ? await loadAllWords() : [current].filter((d) => !d.deck?.tour);
|
|
35
46
|
const t = now();
|
|
47
|
+
let nextDue = null;
|
|
36
48
|
for (const { words, deck } of decks) {
|
|
37
49
|
const directions = directionsOf(deck);
|
|
38
|
-
if (pickNext({ words, state, now: t, directions }))
|
|
50
|
+
if (pickNext({ words, state, now: t, directions })) {
|
|
51
|
+
markWorkWaiting(ensureProgress(state, t), dayKey(t));
|
|
52
|
+
return { done: false, cheers: [] };
|
|
53
|
+
}
|
|
39
54
|
const due = nextDueAt({ words, state, now: t, directions });
|
|
40
55
|
if (due !== null && due < startOfTomorrow(t)) return { done: false, cheers: [] };
|
|
56
|
+
if (due !== null && (nextDue === null || due < nextDue)) nextDue = due;
|
|
41
57
|
}
|
|
42
|
-
const { cheers } = applyCaughtUp(state, { goal: getSettings().dailyGoal, rules: rules(), now: t });
|
|
58
|
+
const { cheers } = applyCaughtUp(state, { goal: getSettings().dailyGoal, rules: rules(), now: t, nextDue });
|
|
43
59
|
await save(state);
|
|
44
60
|
return { done: true, cheers };
|
|
45
61
|
}
|
|
@@ -56,49 +72,85 @@ export function createSession({ loadWords, loadAllWords, state, getSettings, sav
|
|
|
56
72
|
current = { words, deck };
|
|
57
73
|
inTour = Boolean(deck?.tour);
|
|
58
74
|
const item = pickNext({ words, state, now: now(), lastNoteId, directions: directionsOf(deck), tour: deck?.tour });
|
|
75
|
+
if (item && !deck?.tour) markWorkWaiting(ensureProgress(state, now()), dayKey(now())); // not a rest day after all
|
|
59
76
|
// nothing to ask is a normal state, not an error: the UI shows a way on (a finished tour, a new
|
|
60
77
|
// deck) or when the next card is due. Done with everything for today counts as the daily goal.
|
|
61
78
|
if (!item) {
|
|
62
|
-
|
|
79
|
+
// new cards held back by the daily limit, so "all done" doesn't hide them
|
|
80
|
+
const heldNew = deck?.tour ? 0 : words.filter((word) => !(word.directions ?? directionsOf(deck)).some((d) => state.items[itemKey(word.noteId, d)])).length;
|
|
81
|
+
const empty = { empty: true, tour: Boolean(deck?.tour), count: words.length, nextDue: null, cheers: [], dayDone: false, heldNew };
|
|
63
82
|
if (!deck?.tour && words.length) {
|
|
64
83
|
empty.nextDue = nextDueAt({ words, state, now: now(), directions: directionsOf(deck) });
|
|
65
84
|
const day = await settleDay();
|
|
66
85
|
empty.cheers = day.cheers;
|
|
67
86
|
empty.dayDone = day.done; // false: this deck is done, but another one still has cards today
|
|
68
87
|
}
|
|
69
|
-
const message = deck?.tour ? "
|
|
88
|
+
const message = deck?.tour ? say("quiz.empty.tourTitle") : words.length ? say("quiz.empty.laterTitle") : say("quiz.empty.noCardsTitle", { deck: deck?.name });
|
|
70
89
|
throw Object.assign(new Error(message), empty);
|
|
71
90
|
}
|
|
72
91
|
lastNoteId = item.word.noteId;
|
|
73
92
|
lastWrong = null;
|
|
74
93
|
|
|
75
94
|
const forward = item.direction === "en-pl";
|
|
95
|
+
const gap = item.direction.startsWith("c") ? Number(item.direction.slice(1)) - 1 : null;
|
|
96
|
+
const [prompt, expected] =
|
|
97
|
+
gap !== null
|
|
98
|
+
? [blankGap(item.word.word, gap), [item.word.cloze[gap]]]
|
|
99
|
+
: forward
|
|
100
|
+
? [item.word.word, item.word.translations]
|
|
101
|
+
: [item.word.translations.join(" / "), [item.word.word]];
|
|
76
102
|
return {
|
|
77
103
|
word: item.word,
|
|
78
104
|
direction: item.direction,
|
|
79
|
-
label: labelOf(deck
|
|
80
|
-
prompt
|
|
81
|
-
expected
|
|
105
|
+
label: labelOf(deck),
|
|
106
|
+
prompt,
|
|
107
|
+
expected,
|
|
108
|
+
// a cloze card's back is a hint under the sentence ("Cieszę się na to")
|
|
109
|
+
context: gap !== null ? item.word.translations.join(" / ") : "",
|
|
110
|
+
// what the answer is written in, when the deck's languages are known: its own spellings count
|
|
111
|
+
answerLanguage: (forward ? deck?.languages?.back : deck?.languages?.front) ?? null,
|
|
112
|
+
promptLanguage: (forward ? deck?.languages?.front : deck?.languages?.back) ?? null,
|
|
113
|
+
learningLanguage: deck?.learning?.lang ?? null,
|
|
82
114
|
wordCount: words.length,
|
|
83
115
|
tour: Boolean(deck?.tour) // the tour marks what to type with `backticks`
|
|
84
116
|
};
|
|
85
117
|
}
|
|
86
118
|
|
|
87
|
-
// Each deck decides which way it's asked (
|
|
88
|
-
// (
|
|
119
|
+
// Each deck decides which way it's asked (picked after its first card): front to back ("2 + 2" → "4"),
|
|
120
|
+
// back to front (a definition shown, its term typed), or both. The front-to-back direction is keyed
|
|
121
|
+
// "en-pl" internally, whatever the languages; a cloze card's gaps ("c1", "c2"…) come with the card.
|
|
89
122
|
function directionsOf(deck) {
|
|
90
|
-
return deck?.directions === "forward" ? ["en-pl"] : ["en-pl", "pl-en"];
|
|
123
|
+
return deck?.directions === "forward" ? ["en-pl"] : deck?.directions === "backward" ? ["pl-en"] : ["en-pl", "pl-en"];
|
|
91
124
|
}
|
|
92
125
|
|
|
126
|
+
// A typed answer, checked. (An empty one is a reveal: see reveal and selfGrade.)
|
|
93
127
|
async function answer(question, text, { hinted = false } = {}) {
|
|
94
128
|
const key = itemKey(question.word.noteId, question.direction);
|
|
95
129
|
// answers you once declared right (/correct) count for this card from now on
|
|
96
|
-
const result = judge(text, [...question.expected, ...(state.accepted?.[key] ?? [])]);
|
|
130
|
+
const result = judge(text, [...question.expected, ...(state.accepted?.[key] ?? [])], { lang: question.answerLanguage, otherLang: question.promptLanguage, learning: question.learningLanguage ?? null });
|
|
131
|
+
return record(question, { result, text, hinted });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// An empty Enter shows the answer and nothing is written yet: you say whether you knew it (selfGrade).
|
|
135
|
+
const reveal = (question) => ({ expected: question.expected, example: question.word.example });
|
|
136
|
+
|
|
137
|
+
// After a reveal. "Knew it" counts exactly like a typed right answer: boxes, goal, streak, combo and
|
|
138
|
+
// badges. Long answers can't be typed word for word, and they must be able to earn everything too.
|
|
139
|
+
async function selfGrade(question, knew, { hinted = false } = {}) {
|
|
140
|
+
return { ...(await record(question, { result: knew ? "exact" : "wrong", text: "", hinted })), self: true };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function record(question, { result, text, hinted }) {
|
|
144
|
+
const key = itemKey(question.word.noteId, question.direction);
|
|
97
145
|
const correct = result !== "wrong";
|
|
98
146
|
const t = now();
|
|
99
147
|
const before = state.items[key];
|
|
100
148
|
const comboBefore = totals.combo;
|
|
101
|
-
|
|
149
|
+
// almost right (an article left out, an umlaut left plain) counts, but the card doesn't move up, like with a hint
|
|
150
|
+
const almost = ALMOST.includes(result);
|
|
151
|
+
const entry = grade(before, correct, t, { hinted: hinted || almost });
|
|
152
|
+
const would = almost ? grade(before, true, t, { hinted }) : entry; // a hint alone would have held it too
|
|
153
|
+
const held = would.box > entry.box ? { days: BOX_INTERVALS_DAYS[entry.box - 1], would: BOX_INTERVALS_DAYS[would.box - 1] } : null;
|
|
102
154
|
recordAnswer(state, question.word.noteId, question.direction, entry, t, { countNew: !question.tour });
|
|
103
155
|
|
|
104
156
|
totals.asked += 1;
|
|
@@ -106,23 +158,24 @@ export function createSession({ loadWords, loadAllWords, state, getSettings, sav
|
|
|
106
158
|
totals.combo = correct ? totals.combo + 1 : 0;
|
|
107
159
|
lastWrong = correct ? null : { question, text, key, before, comboBefore, hinted, at: t };
|
|
108
160
|
// a blank answer on a tour card just shows it: the tour isn't something to have "missed"
|
|
109
|
-
|
|
161
|
+
const prompt = question.context ? `${question.prompt} (${question.context})` : question.prompt; // a cloze gap keeps its hint
|
|
162
|
+
if (!correct && !(question.tour && !text.trim())) recordMiss(state, { key, label: question.label, prompt, expected: question.expected, answer: text, at: t }, t);
|
|
110
163
|
|
|
111
164
|
const reward = applyAnswer(state, { result, goal: getSettings().dailyGoal, rules: rules(), combo: totals.combo, now: t });
|
|
112
165
|
const cheers = [...reward.cheers];
|
|
113
|
-
if (correct && totals.combo % COMBO_CHEER_EVERY === 0) cheers.push({ kind: "combo", text:
|
|
166
|
+
if (correct && totals.combo % COMBO_CHEER_EVERY === 0) cheers.push({ kind: "combo", text: say("cheers.combo", { count: totals.combo }) });
|
|
114
167
|
|
|
115
168
|
await save(state);
|
|
116
169
|
// the last card of the day settles it right away, so quitting now doesn't lose the goal
|
|
117
170
|
if (!question.tour) cheers.push(...(await settleDay()).cheers);
|
|
118
171
|
// given/closest feed the per-letter diff the UI shows after a miss or a forgiven typo
|
|
119
|
-
const closest = result === "exact" ? null : closestCandidate(text, [...question.expected, ...(state.accepted?.[key] ?? [])]);
|
|
120
|
-
return { result, hinted: hinted && correct, dueIn: describeDue(entry, t), combo: totals.combo, cheers, given: text, closest, nextDue: upNext() };
|
|
172
|
+
const closest = result === "exact" || !text ? null : closestCandidate(text, [...question.expected, ...(state.accepted?.[key] ?? [])], { lang: question.answerLanguage });
|
|
173
|
+
return { result, held, hinted: hinted && correct, dueIn: describeDue(entry, t), combo: totals.combo, cheers, given: text, closest, nextDue: upNext() };
|
|
121
174
|
}
|
|
122
175
|
|
|
123
176
|
// Re-grade the last wrong answer as correct and remember the answer for this card.
|
|
124
177
|
async function overrule() {
|
|
125
|
-
if (!lastWrong) throw new Error("
|
|
178
|
+
if (!lastWrong) throw new Error(say("errors.nothingToCorrect"));
|
|
126
179
|
const { question, text, key, before, comboBefore, hinted, at } = lastWrong;
|
|
127
180
|
lastWrong = null;
|
|
128
181
|
|
|
@@ -142,6 +195,7 @@ export function createSession({ loadWords, loadAllWords, state, getSettings, sav
|
|
|
142
195
|
}
|
|
143
196
|
|
|
144
197
|
const canOverrule = () => lastWrong !== null && lastWrong.text.trim() !== ""; // nothing typed, nothing to accept
|
|
198
|
+
const wrongAnswer = () => lastWrong?.text ?? "";
|
|
145
199
|
|
|
146
200
|
const stats = () => snapshot(state, { goal: getSettings().dailyGoal, rules: rules(), now: now() });
|
|
147
201
|
|
|
@@ -153,24 +207,23 @@ export function createSession({ loadWords, loadAllWords, state, getSettings, sav
|
|
|
153
207
|
async function pause(args) {
|
|
154
208
|
const today = dayKey(now());
|
|
155
209
|
const parsed = parsePause(args, today);
|
|
156
|
-
if (!parsed) throw new Error(
|
|
210
|
+
if (!parsed) throw new Error(say("pause.help"));
|
|
157
211
|
if (parsed.action === "snooze") return { snooze: parsed.ms };
|
|
158
212
|
if (parsed.action === "days") {
|
|
159
213
|
// days off start today at the earliest: missed days can't be turned into days off afterwards
|
|
160
214
|
const from = parsed.from < today ? today : parsed.from;
|
|
161
|
-
if (parsed.to < today) throw new Error("
|
|
215
|
+
if (parsed.to < today) throw new Error(say("pause.past"));
|
|
162
216
|
addVacation(state, from, parsed.to, now());
|
|
163
217
|
await save(state);
|
|
164
|
-
|
|
165
|
-
return { message: `${what}: your streak is safe.` };
|
|
218
|
+
return { message: from === parsed.to ? say("pause.dayOff", { day: formatRange(from, from) }) : say("pause.daysOff", { range: formatRange(from, parsed.to) }) };
|
|
166
219
|
}
|
|
167
220
|
if (parsed.action === "off") {
|
|
168
221
|
cancelVacations(state, now());
|
|
169
222
|
await save(state);
|
|
170
|
-
return { snooze: "off", message: "
|
|
223
|
+
return { snooze: "off", message: say("pause.off") };
|
|
171
224
|
}
|
|
172
225
|
const { upcoming } = vacationInfo(ensureProgress(state, now()), today);
|
|
173
|
-
return { list: true, message: upcoming.length ?
|
|
226
|
+
return { list: true, message: upcoming.length ? say("pause.list", { ranges: upcoming.map((v) => formatRange(v.from, v.to)).join(", ") }) : say("pause.none", { help: say("pause.help") }) };
|
|
174
227
|
}
|
|
175
228
|
|
|
176
229
|
// a tip for the wait between questions; remembering which ones were shown is best-effort
|
|
@@ -186,6 +239,27 @@ export function createSession({ loadWords, loadAllWords, state, getSettings, sav
|
|
|
186
239
|
const gone = (key) => ids.has(key.slice(0, key.lastIndexOf(":")));
|
|
187
240
|
for (const key of Object.keys(state.items)) if (gone(key)) delete state.items[key];
|
|
188
241
|
for (const key of Object.keys(state.accepted ?? {})) if (gone(key)) delete state.accepted[key];
|
|
242
|
+
if (state.dormant) state.dormant = state.dormant.filter((key) => !gone(key));
|
|
243
|
+
await save(state);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// A deck is asked another way now (/decks): the directions it no longer asks rest. Their progress stays
|
|
247
|
+
// for when they're back, but doesn't count against mastering the card.
|
|
248
|
+
async function setDirections(cardIds, directions) {
|
|
249
|
+
const asked = new Set(directions === "forward" ? ["en-pl"] : directions === "backward" ? ["pl-en"] : ["en-pl", "pl-en"]);
|
|
250
|
+
const dormant = new Set(state.dormant ?? []);
|
|
251
|
+
for (const id of cardIds) for (const direction of ["en-pl", "pl-en"]) (asked.has(direction) ? dormant.delete : dormant.add).call(dormant, itemKey(id, direction));
|
|
252
|
+
state.dormant = [...dormant];
|
|
253
|
+
await save(state);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// progress of cards that changed shape (a cloze card fixed into a plain one, a gap removed) is dropped
|
|
257
|
+
async function forgetKeys(keys) {
|
|
258
|
+
for (const key of keys) {
|
|
259
|
+
delete state.items[key];
|
|
260
|
+
delete state.accepted?.[key];
|
|
261
|
+
}
|
|
262
|
+
if (state.dormant) state.dormant = state.dormant.filter((key) => !keys.includes(key));
|
|
189
263
|
await save(state);
|
|
190
264
|
}
|
|
191
265
|
|
|
@@ -223,5 +297,13 @@ export function createSession({ loadWords, loadAllWords, state, getSettings, sav
|
|
|
223
297
|
return fresh;
|
|
224
298
|
}
|
|
225
299
|
|
|
226
|
-
|
|
300
|
+
// cards added (or a direction switched on): there's work today, whether or not it's asked yet, so a
|
|
301
|
+
// quiet stretch ends here
|
|
302
|
+
async function workAdded() {
|
|
303
|
+
markWorkWaiting(ensureProgress(state, now()), dayKey(now()));
|
|
304
|
+
await save(state);
|
|
305
|
+
await settleDay(); // nothing askable after all (a direction switched off): the quiet stretch goes on
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return { totals, next, answer, workAdded, reveal, selfGrade, setDirections, forgetKeys, overrule, canOverrule, wrongAnswer, stats, month, missed, nextTip, pause, isFirstRun, markHatched, openBadges, forget, reset, inTour: () => inTour };
|
|
227
309
|
}
|
package/src/settings.js
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
import { LANGUAGES, systemLanguage, t } from "./i18n.js";
|
|
2
|
+
|
|
3
|
+
export const DEFAULTS = { everyMs: 600_000, rhythm: "minutes", sound: true, speak: false, volume: 0.5, dailyGoal: 20, tips: "sometimes", quiet: null, skipWeekends: true, deck: null };
|
|
2
4
|
|
|
3
5
|
export const INTERVALS = [60_000, 120_000, 300_000, 600_000, 900_000, 1_200_000, 1_800_000, 2_700_000, 3_600_000];
|
|
4
6
|
export const GOALS = [5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 150, 200];
|
|
5
7
|
export const VOLUMES = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
|
|
6
8
|
export const TIP_MODES = ["always", "sometimes", "off"];
|
|
7
|
-
|
|
9
|
+
// how cards come: one every few minutes (everyMs), or a session where Enter asks the next one right
|
|
10
|
+
// away, endless or for a set number of cards, after which the minutes take over again
|
|
11
|
+
export const RHYTHMS = ["minutes", "session", "session-10", "session-20"];
|
|
12
|
+
export const sessionLength = (rhythm) => (rhythm.startsWith("session-") ? Number(rhythm.slice("session-".length)) : null);
|
|
8
13
|
// presets for /settings; any { from, to } in settings.json works too
|
|
9
14
|
export const QUIET_PRESETS = [null, { from: 21, to: 7 }, { from: 22, to: 8 }, { from: 23, to: 7 }, { from: 0, to: 8 }];
|
|
10
15
|
|
|
11
16
|
export function formatInterval(ms) {
|
|
12
|
-
if (ms < 60_000) return
|
|
13
|
-
if (ms < 3_600_000) return
|
|
14
|
-
return
|
|
17
|
+
if (ms < 60_000) return t("time.seconds", { n: Math.round(ms / 1000) });
|
|
18
|
+
if (ms < 3_600_000) return t("time.minutes", { n: Math.round(ms / 60_000) });
|
|
19
|
+
return t("time.hours", { n: Math.round(ms / 3_600_000) });
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
// older versions saved tips as a boolean
|
|
@@ -26,13 +31,16 @@ export function normalizeSettings(raw = {}) {
|
|
|
26
31
|
raw = raw && typeof raw === "object" ? raw : {};
|
|
27
32
|
return {
|
|
28
33
|
everyMs: isNumber(raw.everyMs) && raw.everyMs >= 1000 ? raw.everyMs : DEFAULTS.everyMs,
|
|
34
|
+
rhythm: RHYTHMS.includes(raw.rhythm) ? raw.rhythm : DEFAULTS.rhythm,
|
|
29
35
|
sound: typeof raw.sound === "boolean" ? raw.sound : DEFAULTS.sound,
|
|
36
|
+
speak: typeof raw.speak === "boolean" ? raw.speak : DEFAULTS.speak,
|
|
30
37
|
volume: isNumber(raw.volume) && raw.volume >= 0 && raw.volume <= 1 ? raw.volume : DEFAULTS.volume,
|
|
31
38
|
dailyGoal: isNumber(raw.dailyGoal) && raw.dailyGoal >= 1 ? Math.round(raw.dailyGoal) : DEFAULTS.dailyGoal,
|
|
32
39
|
tips: normalizeTips(raw.tips),
|
|
33
40
|
skipWeekends: typeof raw.skipWeekends === "boolean" ? raw.skipWeekends : DEFAULTS.skipWeekends,
|
|
34
41
|
quiet: raw.quiet && isHour(raw.quiet.from) && isHour(raw.quiet.to) ? { from: raw.quiet.from, to: raw.quiet.to } : null,
|
|
35
|
-
deck: typeof raw.deck === "string" && raw.deck.trim() ? raw.deck : null // picked in /decks; null = your first deck
|
|
42
|
+
deck: typeof raw.deck === "string" && raw.deck.trim() ? raw.deck : null, // picked in /decks; null = your first deck
|
|
43
|
+
language: typeof raw.language === "string" && Object.hasOwn(LANGUAGES, raw.language) ? raw.language : systemLanguage() // until picked in /settings, the system's
|
|
36
44
|
};
|
|
37
45
|
}
|
|
38
46
|
|
package/src/starter.js
CHANGED
|
@@ -1,38 +1,47 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { entry, language, t } from "./i18n.js";
|
|
2
|
+
import { addCard, cardsOf, createDeck, findDeck } from "./library.js";
|
|
2
3
|
|
|
3
|
-
// The deck a fresh install starts with: a short tour that teaches Dracosh by doing.
|
|
4
|
-
// exactly what to type, and the line shown after the answer
|
|
5
|
-
// deck: every card is asked once, in this order, and never
|
|
6
|
-
// what to type (or a command); the quiz shows them
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const STARTER_CARDS = [
|
|
12
|
-
{ front: "Welcome! Every few minutes a card asks you something. Type the answer and press Enter. Try it and type `hello`", back: ["hello"], example: "Press `Enter` now for the next card, or wait and it will come by itself." },
|
|
13
|
-
{ front: "Small typos still count. Type `dragon`, but miss a letter on purpose (like `dragn`)", back: ["dragon"], example: "A typo counts as right, and Dracosh shows you the correct spelling." },
|
|
14
|
-
{ front: "A card can accept more than one answer. What is 2 + 2? (`4` and `four` both work)", back: ["4", "four"], example: "Every right answer moves a card to a later box, so you see it less and less often." },
|
|
15
|
-
{ front: "Stuck? Type `/hint` and press Enter, and the answer shows up letter by letter. Try it, then type `apple`", back: ["apple"], example: "A hinted answer still counts, but the card stays in its box for now." },
|
|
16
|
-
{ front: "What is your dragon's secret name? Nobody knows, so get this one wrong on purpose and type `dog`", back: ["Asogaras"], example: "In your own decks a missed card comes back sooner. If you were marked wrong by mistake, type `/correct` now." },
|
|
17
|
-
{ front: "Type `/` at any time to see all commands. Try `/badges` after this card. For now, type `ok`", back: ["ok", "okay"], example: "`/stats` shows your calendar, `/companion` your dragon, `/settings` how often cards come." },
|
|
18
|
-
{ front: "Reach your daily goal every day to keep your streak. Your dragon grows with it. Type `every day`", back: ["every day", "everyday", "daily"], example: "Miss a day and a freeze saves your streak, if you have one left." },
|
|
19
|
-
{ front: "Need a break? `/pause 1h` holds the cards for an hour, `/pause 3d` gives you days off. For now, type `later`", back: ["later"], example: "Leave Dracosh alone for a few minutes and the dragon dozes off. Any key wakes it." },
|
|
20
|
-
{ front: "That's the tour! Type `done`", back: ["done"], example: "Now make it yours and type `/add` to start your own deck. The tour makes room for it." }
|
|
21
|
-
];
|
|
4
|
+
// The deck a fresh install starts with: a short tour that teaches Dracosh by doing. Its cards live in
|
|
5
|
+
// the locale (tour.cards): every card says exactly what to type, and the line shown after the answer
|
|
6
|
+
// explains the next thing. It's a `tour` deck: every card is asked once, in this order, and never
|
|
7
|
+
// comes back as a review. `Backticks` mark what to type (or a command); the quiz shows them
|
|
8
|
+
// highlighted, in the tour only. Answers never start with "/", because "/" opens the commands.
|
|
9
|
+
export const STARTER_DECK = "Getting started"; // the tour's English name, which the first versions were found by
|
|
10
|
+
// bump it when the cards change: an older copy of the tour in someone's library gets replaced
|
|
11
|
+
const STARTER_VERSION = 8;
|
|
22
12
|
|
|
23
13
|
// the tour as an earlier version created it (the first one had no version number yet)
|
|
24
14
|
const isStarter = (deck) => deck.starter !== undefined || (deck.name === STARTER_DECK && deck.type === "qa" && !deck.languages);
|
|
25
15
|
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
const tourCards = () => entry("tour.cards");
|
|
17
|
+
|
|
18
|
+
// The app's language changed: the tour's cards say the same in the new one. They keep their ids, so
|
|
19
|
+
// the cards already done stay done. The deck takes the new name unless one of yours has it.
|
|
20
|
+
function translateTour(library, deck, lang) {
|
|
21
|
+
const cards = tourCards();
|
|
22
|
+
cardsOf(library, deck.id).forEach((card, i) => Object.assign(card, { front: cards[i].front, back: [...cards[i].back], example: cards[i].example }));
|
|
23
|
+
const name = t("tour.deck");
|
|
24
|
+
if ((findDeck(library, name) ?? deck) === deck) deck.name = name;
|
|
25
|
+
deck.tourLanguage = lang;
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Adds the tour to an empty library, brings an older copy of it up to date (its progress is dropped
|
|
30
|
+
// with the old cards), or translates it into the app's language. Returns true when the library
|
|
31
|
+
// changed and needs saving.
|
|
28
32
|
export function seedLibrary(library, now = Date.now()) {
|
|
29
33
|
let deck = library.decks.find(isStarter);
|
|
30
34
|
if (library.decks.length && !deck) return false;
|
|
31
|
-
|
|
35
|
+
const lang = language();
|
|
36
|
+
const current = deck && (deck.starter ?? 1) >= STARTER_VERSION && cardsOf(library, deck.id).length === tourCards().length;
|
|
37
|
+
if (current) return (deck.tourLanguage ?? "en") === lang ? false : translateTour(library, deck, lang);
|
|
32
38
|
|
|
33
|
-
if (deck)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
if (deck) {
|
|
40
|
+
library.cards = library.cards.filter((card) => card.deckId !== deck.id);
|
|
41
|
+
const name = t("tour.deck");
|
|
42
|
+
if ((findDeck(library, name) ?? deck) === deck) deck.name = name;
|
|
43
|
+
} else deck = createDeck(library, { name: t("tour.deck"), type: "qa", languages: null, directions: "forward", tour: true, now });
|
|
44
|
+
Object.assign(deck, { tour: true, starter: STARTER_VERSION, tourLanguage: lang });
|
|
45
|
+
for (const card of tourCards()) addCard(library, deck.id, { ...card, now });
|
|
37
46
|
return true;
|
|
38
47
|
}
|
package/src/store.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { t } from "./i18n.js";
|
|
1
2
|
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
2
3
|
import { homedir } from "node:os";
|
|
3
4
|
import { dirname, join } from "node:path";
|
|
@@ -18,7 +19,8 @@ const emptyState = () => ({ version: STATE_VERSION, items: {}, newToday: { date:
|
|
|
18
19
|
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
19
20
|
|
|
20
21
|
// what to say when a data file can't be used as it is
|
|
21
|
-
|
|
22
|
+
// marked, not only worded: the message is in the app's language
|
|
23
|
+
export const damaged = (file, why) => Object.assign(new Error(t("errors.damaged", { file, why })), { damaged: true });
|
|
22
24
|
|
|
23
25
|
export async function readJson(file, fallback) {
|
|
24
26
|
try {
|
|
@@ -26,7 +28,7 @@ export async function readJson(file, fallback) {
|
|
|
26
28
|
} catch (err) {
|
|
27
29
|
if (err.code === "ENOENT") return fallback;
|
|
28
30
|
if (err instanceof SyntaxError) throw damaged(file, err.message);
|
|
29
|
-
throw new Error(
|
|
31
|
+
throw new Error(t("errors.cantRead", { file, error: err.message }));
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
|
|
@@ -59,8 +61,8 @@ export const saveLibrary = (library) => writeJson(LIBRARY_FILE, library);
|
|
|
59
61
|
export async function loadState() {
|
|
60
62
|
const state = await readJson(STATE_FILE, undefined);
|
|
61
63
|
if (state === undefined) return emptyState();
|
|
62
|
-
if (!isObject(state) || !isObject(state.items)) throw damaged(STATE_FILE, "
|
|
63
|
-
if (!Object.values(state.items).every((entry) => isObject(entry) && Number.isFinite(entry.box) && Number.isFinite(entry.due))) throw damaged(STATE_FILE, "
|
|
64
|
+
if (!isObject(state) || !isObject(state.items)) throw damaged(STATE_FILE, t("errors.why.noProgress"));
|
|
65
|
+
if (!Object.values(state.items).every((entry) => isObject(entry) && Number.isFinite(entry.box) && Number.isFinite(entry.due))) throw damaged(STATE_FILE, t("errors.why.progressEntry"));
|
|
64
66
|
const { progress } = state;
|
|
65
67
|
const validProgress =
|
|
66
68
|
isObject(progress) &&
|
|
@@ -70,8 +72,8 @@ export async function loadState() {
|
|
|
70
72
|
["count", "best", "freezes"].every((key) => Number.isFinite(progress.streak[key])) &&
|
|
71
73
|
isObject(progress.badges) &&
|
|
72
74
|
(progress.vacations === undefined || Array.isArray(progress.vacations));
|
|
73
|
-
if (progress !== undefined && !validProgress) throw damaged(STATE_FILE, "
|
|
74
|
-
if (state.version > STATE_VERSION) throw new Error(
|
|
75
|
+
if (progress !== undefined && !validProgress) throw damaged(STATE_FILE, t("errors.why.streak"));
|
|
76
|
+
if (state.version > STATE_VERSION) throw new Error(t("errors.newerState", { file: STATE_FILE }));
|
|
75
77
|
if (!isObject(state.newToday)) state.newToday = { date: "", count: 0 };
|
|
76
78
|
return state;
|
|
77
79
|
}
|
|
@@ -80,7 +82,8 @@ export const saveState = (state) => writeJson(STATE_FILE, state);
|
|
|
80
82
|
// `countNew: false` keeps a card out of the daily cap on new cards (the tour's don't use it up)
|
|
81
83
|
export function recordAnswer(state, noteId, direction, entry, now = Date.now(), { countNew = true } = {}) {
|
|
82
84
|
const key = itemKey(noteId, direction);
|
|
83
|
-
|
|
85
|
+
// a new card counts once towards the daily limit, whichever way it's asked first
|
|
86
|
+
const isNew = countNew && !state.items[key] && !Object.keys(state.items).some((k) => k.startsWith(`${noteId}:`));
|
|
84
87
|
const date = dayKey(now);
|
|
85
88
|
state.items[key] = entry;
|
|
86
89
|
if (isNew) state.newToday = { date, count: newToday(state, now) + 1 };
|
|
@@ -89,7 +92,7 @@ export function recordAnswer(state, noteId, direction, entry, now = Date.now(),
|
|
|
89
92
|
|
|
90
93
|
// hand-edited into something odd, even broken JSON: the defaults take over (nothing worth a restore)
|
|
91
94
|
export const loadSettingsRaw = async () => {
|
|
92
|
-
const raw = await readJson(SETTINGS_FILE, {}).catch((err) => (err.
|
|
95
|
+
const raw = await readJson(SETTINGS_FILE, {}).catch((err) => (err.damaged ? {} : Promise.reject(err)));
|
|
93
96
|
return isObject(raw) ? raw : {};
|
|
94
97
|
};
|
|
95
98
|
|
package/src/tips.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { englishMessages, t } from "./i18n.js";
|
|
2
2
|
|
|
3
|
-
// Short tips about Dracosh itself, shown now and then while you wait for the next card.
|
|
4
|
-
|
|
3
|
+
// Short tips about Dracosh itself, shown now and then while you wait for the next card. They live in
|
|
4
|
+
// the locale (tips.<id>); the ids come from English, the text from the language in use.
|
|
5
|
+
export const TIPS = Object.keys(englishMessages().tips).map((id) => ({
|
|
6
|
+
id,
|
|
7
|
+
get cat() {
|
|
8
|
+
return t(`tips.${id}.cat`);
|
|
9
|
+
},
|
|
10
|
+
get text() {
|
|
11
|
+
return t(`tips.${id}.text`);
|
|
12
|
+
}
|
|
13
|
+
}));
|
|
5
14
|
const SOMETIMES_CHANCE = 1 / 3;
|
|
6
15
|
|
|
7
16
|
// "always" shows a tip on every wait, "sometimes" on about one in three
|