dracosh 0.1.0 → 0.3.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 +49 -22
- 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/locales/de.json +1046 -0
- package/locales/en.json +1029 -0
- package/locales/fr.json +1094 -0
- package/locales/pl.json +1117 -0
- package/locales/uk.json +1118 -0
- package/package.json +2 -2
- package/src/alerts.js +7 -4
- package/src/backup.js +96 -0
- package/src/cardline.js +71 -0
- package/src/cli.js +106 -38
- package/src/commands.js +15 -13
- package/src/demo.js +11 -0
- package/src/diff.js +13 -7
- package/src/i18n.js +68 -0
- package/src/judge.js +51 -12
- package/src/library.js +154 -0
- package/src/lock.js +64 -0
- package/src/manage.js +165 -0
- package/src/pause.js +22 -0
- package/src/progress.js +202 -93
- package/src/quiet.js +4 -3
- package/src/scheduler.js +39 -30
- package/src/session.js +181 -33
- package/src/settings.js +15 -13
- package/src/starter.js +47 -0
- package/src/store.js +58 -17
- package/src/tips.js +17 -25
- package/src/ui/AddCard.js +496 -0
- package/src/ui/AnswerInput.js +28 -13
- package/src/ui/App.js +360 -89
- package/src/ui/BadgeUnlock.js +51 -0
- package/src/ui/Badges.js +79 -20
- package/src/ui/Choice.js +38 -0
- package/src/ui/Companion.js +24 -39
- package/src/ui/Confetti.js +2 -2
- package/src/ui/Decks.js +99 -0
- package/src/ui/DieRoll.js +104 -0
- package/src/ui/Evolution.js +11 -10
- package/src/ui/Hatch.js +9 -7
- package/src/ui/Header.js +62 -19
- package/src/ui/Help.js +74 -0
- package/src/ui/Mascot.js +89 -40
- package/src/ui/Missed.js +15 -13
- package/src/ui/Preview.js +123 -29
- package/src/ui/Reset.js +71 -0
- package/src/ui/Settings.js +34 -23
- package/src/ui/Stats.js +28 -24
- package/src/ui/Summary.js +18 -10
- package/src/ui/TheOne.js +122 -0
- package/src/ui/icons.js +156 -25
- package/src/ui/kit.js +87 -2
- package/src/vacation.js +9 -3
- package/src/words.js +22 -14
- package/assets/sounds/badge.wav +0 -0
- package/data/tips.json +0 -1362
- package/src/ui/Tip.js +0 -25
package/src/progress.js
CHANGED
|
@@ -1,20 +1,38 @@
|
|
|
1
|
+
import { dateLocale, t } from "./i18n.js";
|
|
1
2
|
import { addDays, dayKey, daysBetween, weekStart } from "./dates.js";
|
|
2
3
|
import { BOX_INTERVALS_DAYS } from "./scheduler.js";
|
|
3
4
|
|
|
4
5
|
export const MAX_FREEZES = 2;
|
|
5
6
|
const MAX_BOX = BOX_INTERVALS_DAYS.length;
|
|
6
7
|
const AT_RISK_HOUR = 19;
|
|
7
|
-
const COMEBACK_AFTER_DAYS =
|
|
8
|
+
const COMEBACK_AFTER_DAYS = 5;
|
|
8
9
|
const MISSED_KEEP_DAYS = 14;
|
|
10
|
+
const FALL_MIN = 30; // a lost streak this long counts as a fall for "Dragon's die"
|
|
11
|
+
|
|
12
|
+
// The die's faces: winning "Dragon's die" rolls one, and the dragon keeps that element for good.
|
|
13
|
+
export const ELEMENTS = ["earth", "wind", "water", "ice", "fire", "cosmos"];
|
|
14
|
+
export const rollElement = (random = Math.random) => ELEMENTS[Math.floor(random() * ELEMENTS.length)];
|
|
15
|
+
export const elementName = (element) => t(`dragon.elements.${element}.name`);
|
|
16
|
+
export const elementAdjective = (element) => t(`dragon.elements.${element}.adjective`);
|
|
17
|
+
|
|
18
|
+
// what the dragon is called: its form, with its element in front once it has one ("Ice Drake")
|
|
19
|
+
export const dragonName = (stage, element = null) => (element ? t("dragon.named", { element: elementAdjective(element), stage: STAGES[stage].name }) : STAGES[stage].name);
|
|
9
20
|
|
|
10
21
|
// The companion evolves permanently with your best streak, so a broken streak never takes a form away.
|
|
22
|
+
const stage = (id, from) => ({
|
|
23
|
+
id,
|
|
24
|
+
from,
|
|
25
|
+
get name() {
|
|
26
|
+
return t(`dragon.stages.${id}`);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
11
29
|
export const STAGES = [
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
30
|
+
stage("hatchling", 0),
|
|
31
|
+
stage("imp", 7),
|
|
32
|
+
stage("whelp", 30),
|
|
33
|
+
stage("drake", 90),
|
|
34
|
+
stage("dragon", 180),
|
|
35
|
+
stage("legend", 365)
|
|
18
36
|
];
|
|
19
37
|
|
|
20
38
|
export function stageFor(bestStreak) {
|
|
@@ -27,20 +45,15 @@ export function stageFor(bestStreak) {
|
|
|
27
45
|
export function ensureProgress(state, now = Date.now()) {
|
|
28
46
|
state.progress ??= {
|
|
29
47
|
days: {},
|
|
30
|
-
streak: { count: 0, best: 0, lastGoalDay: null, freezes:
|
|
48
|
+
streak: { count: 0, best: 0, lastGoalDay: null, freezes: MAX_FREEZES, freezeWeek: weekStart(dayKey(now)), freezesUsed: 0 },
|
|
31
49
|
badges: {},
|
|
32
50
|
bestCombo: 0,
|
|
33
51
|
lastActiveDay: null,
|
|
34
52
|
vacations: []
|
|
35
53
|
};
|
|
36
54
|
state.progress.vacations ??= [];
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
const { streak } = state.progress;
|
|
40
|
-
if (streak.freezeWeek !== week) {
|
|
41
|
-
streak.freezeWeek = week;
|
|
42
|
-
streak.freezes = Math.min(MAX_FREEZES, streak.freezes + 1);
|
|
43
|
-
}
|
|
55
|
+
// badges already shown in /badges; ones unlocked before this was tracked count as seen, so they don't all flash at once
|
|
56
|
+
state.progress.seenBadges ??= Object.keys(state.progress.badges);
|
|
44
57
|
return state.progress;
|
|
45
58
|
}
|
|
46
59
|
|
|
@@ -49,6 +62,7 @@ export function ensureProgress(state, now = Date.now()) {
|
|
|
49
62
|
const NO_RULES = { skipWeekends: false };
|
|
50
63
|
|
|
51
64
|
export function isRestDay(progress, key, rules = NO_RULES) {
|
|
65
|
+
if (progress.days?.[key]?.nothingDue) return true; // opened, but no card anywhere was waiting
|
|
52
66
|
if (rules.skipWeekends) {
|
|
53
67
|
const weekday = new Date(`${key}T12:00:00`).getDay();
|
|
54
68
|
if (weekday === 0 || weekday === 6) return true;
|
|
@@ -56,37 +70,56 @@ export function isRestDay(progress, key, rules = NO_RULES) {
|
|
|
56
70
|
return (progress.vacations ?? []).some((v) => key >= v.from && key <= v.to);
|
|
57
71
|
}
|
|
58
72
|
|
|
59
|
-
//
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
// Freezes as they stand today, walking day by day from the last goal day: every new week gives one
|
|
74
|
+
// back (never more than MAX_FREEZES in stock), every missed working day takes one. A gap with more
|
|
75
|
+
// missed days than freezes breaks the streak and takes none. The stored streak.freezes / freezeWeek
|
|
76
|
+
// are as of the last time this was settled (a goal day); nothing is spent until then, but the
|
|
77
|
+
// screens show this walk, so they never promise freezes that are already used.
|
|
78
|
+
function walkFreezes(progress, today, rules) {
|
|
79
|
+
const { lastGoalDay, freezes: stored, freezeWeek } = progress.streak;
|
|
80
|
+
const walk = (spend) => {
|
|
81
|
+
let freezes = stored;
|
|
82
|
+
let week = freezeWeek ?? weekStart(today);
|
|
83
|
+
let used = 0;
|
|
84
|
+
for (let day = lastGoalDay ? addDays(lastGoalDay, 1) : today; day <= today; day = addDays(day, 1)) {
|
|
85
|
+
const dayWeek = weekStart(day);
|
|
86
|
+
if (dayWeek > week) {
|
|
87
|
+
freezes = Math.min(MAX_FREEZES, freezes + Math.round(daysBetween(week, dayWeek) / 7));
|
|
88
|
+
week = dayWeek;
|
|
89
|
+
}
|
|
90
|
+
if (!spend || day === today || isRestDay(progress, day, rules)) continue;
|
|
91
|
+
if (freezes === 0) return null; // the streak breaks here
|
|
92
|
+
freezes -= 1;
|
|
93
|
+
used += 1;
|
|
94
|
+
}
|
|
95
|
+
return { freezes, week, used };
|
|
96
|
+
};
|
|
97
|
+
const spent = lastGoalDay ? walk(true) : null;
|
|
98
|
+
return spent ? { ...spent, broken: false } : { ...walk(false), used: 0, broken: Boolean(lastGoalDay) };
|
|
64
99
|
}
|
|
65
100
|
|
|
66
101
|
// the streak as shown right now: alive while no working day was skipped, or while freezes can cover the gap
|
|
67
102
|
export function currentStreak(progress, today, rules = NO_RULES) {
|
|
68
|
-
const { count, lastGoalDay
|
|
103
|
+
const { count, lastGoalDay } = progress.streak;
|
|
69
104
|
if (!lastGoalDay || !count) return 0;
|
|
70
|
-
return
|
|
105
|
+
return walkFreezes(progress, today, rules).broken ? 0 : count;
|
|
71
106
|
}
|
|
72
107
|
|
|
108
|
+
export const freezesNow = (progress, today, rules = NO_RULES) => walkFreezes(progress, today, rules).freezes;
|
|
109
|
+
|
|
73
110
|
function completeDay(progress, today, rules) {
|
|
74
111
|
const streak = progress.streak;
|
|
112
|
+
const { freezes, week, used, broken } = walkFreezes(progress, today, rules);
|
|
75
113
|
if (!streak.lastGoalDay) {
|
|
76
114
|
streak.count = 1;
|
|
115
|
+
} else if (broken) {
|
|
116
|
+
if (streak.count >= FALL_MIN) streak.fallen = streak.count; // the most recent long streak that was lost
|
|
117
|
+
streak.count = 1;
|
|
77
118
|
} else {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
streak.count += 1;
|
|
81
|
-
} else if (missed <= streak.freezes) {
|
|
82
|
-
streak.freezes -= missed;
|
|
83
|
-
streak.freezesUsed = (streak.freezesUsed ?? 0) + missed;
|
|
84
|
-
streak.count += 1;
|
|
85
|
-
} else {
|
|
86
|
-
streak.count = 1;
|
|
87
|
-
}
|
|
119
|
+
streak.count += 1;
|
|
120
|
+
streak.freezesUsed = (streak.freezesUsed ?? 0) + used;
|
|
88
121
|
}
|
|
89
|
-
streak
|
|
122
|
+
Object.assign(streak, { freezes, freezeWeek: week, lastGoalDay: today });
|
|
90
123
|
streak.best = Math.max(streak.best, streak.count);
|
|
91
124
|
return streak.count;
|
|
92
125
|
}
|
|
@@ -135,54 +168,85 @@ function perfectWeek(progress, today) {
|
|
|
135
168
|
|
|
136
169
|
// `s` = stats from collectStats() plus `event`: what this very answer was (correct, hour, days since last activity)
|
|
137
170
|
export const BADGES = [
|
|
138
|
-
{ id: "hello",
|
|
139
|
-
{ id: "first-steps",
|
|
140
|
-
{ id: "century",
|
|
141
|
-
{ id: "
|
|
142
|
-
{ id: "thousand",
|
|
143
|
-
{ id: "
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
{ id: "
|
|
147
|
-
{ id: "
|
|
148
|
-
{ id: "
|
|
149
|
-
{ id: "
|
|
150
|
-
|
|
151
|
-
{ id: "
|
|
152
|
-
|
|
153
|
-
{ id: "
|
|
154
|
-
{ id: "
|
|
155
|
-
|
|
156
|
-
{ id: "
|
|
157
|
-
{ id: "
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
{ id: "
|
|
161
|
-
{ id: "
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
{ id: "
|
|
165
|
-
{ id: "
|
|
166
|
-
|
|
167
|
-
{ id: "
|
|
168
|
-
{ id: "
|
|
169
|
-
|
|
170
|
-
{ id: "
|
|
171
|
-
{ id: "
|
|
172
|
-
{ id: "
|
|
173
|
-
{ id: "
|
|
174
|
-
{ id: "
|
|
175
|
-
|
|
176
|
-
{ id: "
|
|
171
|
+
{ id: "hello", test: (s) => s.totalCorrect >= 1 },
|
|
172
|
+
{ id: "first-steps", test: (s) => s.totalCorrect >= 10 },
|
|
173
|
+
{ id: "century", test: (s) => s.totalCorrect >= 100 },
|
|
174
|
+
{ id: "thousand", test: (s) => s.totalCorrect >= 1000 },
|
|
175
|
+
{ id: "five-thousand", test: (s) => s.totalCorrect >= 5000 },
|
|
176
|
+
{ id: "ten-thousand", test: (s) => s.totalCorrect >= 10000 },
|
|
177
|
+
{ id: "dragons-hoard", test: (s) => s.totalCorrect >= 20000 },
|
|
178
|
+
|
|
179
|
+
{ id: "on-a-roll", test: (s) => s.bestStreak >= 3 },
|
|
180
|
+
{ id: "week", test: (s) => s.bestStreak >= 7 },
|
|
181
|
+
{ id: "fortnight", test: (s) => s.bestStreak >= 14 },
|
|
182
|
+
{ id: "month", test: (s) => s.bestStreak >= 30 },
|
|
183
|
+
{ id: "hundred-days", test: (s) => s.bestStreak >= 100 },
|
|
184
|
+
{ id: "dragonheart", test: (s) => s.bestStreak >= 180 },
|
|
185
|
+
|
|
186
|
+
{ id: "keeper", test: (s) => s.mastered >= 10 },
|
|
187
|
+
{ id: "collector", test: (s) => s.mastered >= 25 },
|
|
188
|
+
{ id: "dictionary", test: (s) => s.mastered >= 50 },
|
|
189
|
+
{ id: "lexicon", test: (s) => s.mastered >= 150 },
|
|
190
|
+
{ id: "explorer", test: (s) => s.practiced >= 250 },
|
|
191
|
+
|
|
192
|
+
{ id: "flawless", test: (s) => s.today.asked >= 10 && s.today.correct === s.today.asked },
|
|
193
|
+
{ id: "overachiever", test: (s) => s.today.correct >= s.goal * 2 },
|
|
194
|
+
{ id: "marathon", test: (s) => s.today.correct >= 30 },
|
|
195
|
+
|
|
196
|
+
{ id: "hot-streak", test: (s) => s.bestCombo >= 10 },
|
|
197
|
+
{ id: "unstoppable", test: (s) => s.bestCombo >= 25 },
|
|
198
|
+
{ id: "combo-master", test: (s) => s.bestCombo >= 50 },
|
|
199
|
+
|
|
200
|
+
{ id: "early-bird", test: (s) => s.event.correct && s.event.hour < 8 },
|
|
201
|
+
{ id: "night-owl", test: (s) => s.event.correct && s.event.hour >= 23 },
|
|
202
|
+
|
|
203
|
+
{ id: "comeback", test: (s) => s.event.correct && s.event.daysAway >= COMEBACK_AFTER_DAYS },
|
|
204
|
+
{ id: "redemption", test: (s) => s.recoveredToday >= 5 },
|
|
205
|
+
{ id: "frozen", test: (s) => s.freezesUsed >= 1 },
|
|
206
|
+
{ id: "weekend", test: (s) => s.weekendDone },
|
|
207
|
+
{ id: "perfect-week", test: (s) => s.perfectWeek },
|
|
208
|
+
{ id: "regular", test: (s) => s.goalDaysThisMonth >= 20 },
|
|
209
|
+
{ id: "machine", test: (s) => s.goalDaysThisMonth >= 25 },
|
|
210
|
+
{ id: "hundred-club", test: (s) => s.totalGoalDays >= 100 },
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
// keep this one last: it checks the others, including any unlocked earlier in the same answer
|
|
214
|
+
{ id: "one-ring", test: (s) => BADGES.every((b) => b.id === "one-ring" || b.hidden || s.badges[b.id]) },
|
|
215
|
+
|
|
216
|
+
// hidden: shown as "???" until won, and not needed for The One
|
|
217
|
+
{
|
|
218
|
+
id: "dragons-die",
|
|
219
|
+
hidden: true,
|
|
220
|
+
test: (s) => s.fallenStreak >= FALL_MIN && s.streakCount > s.fallenStreak
|
|
221
|
+
}
|
|
177
222
|
];
|
|
178
223
|
|
|
224
|
+
// a badge's name and description come from the locale (badges.<id>), read each time they're shown
|
|
225
|
+
for (const badge of BADGES) {
|
|
226
|
+
Object.defineProperties(badge, {
|
|
227
|
+
name: { get: () => t(`badges.${badge.id}.name`), enumerable: true },
|
|
228
|
+
desc: { get: () => t(`badges.${badge.id}.desc`), enumerable: true }
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// progress is kept per card and direction ("id:en-pl"); badges count cards
|
|
233
|
+
const cardOf = (key) => key.slice(0, key.lastIndexOf(":"));
|
|
234
|
+
|
|
235
|
+
// a card is mastered once every direction it's asked in has reached the last box; a direction its deck
|
|
236
|
+
// no longer asks (`dormant`, kept for when it's turned back on) doesn't hold it back
|
|
237
|
+
function masteredCards(items, dormant = []) {
|
|
238
|
+
const resting = new Set(dormant);
|
|
239
|
+
items = items.filter(([key]) => !resting.has(key));
|
|
240
|
+
const byCard = new Map();
|
|
241
|
+
for (const [key, entry] of items) byCard.set(cardOf(key), (byCard.get(cardOf(key)) ?? true) && entry.box === MAX_BOX);
|
|
242
|
+
return [...byCard.values()].filter(Boolean).length;
|
|
243
|
+
}
|
|
244
|
+
|
|
179
245
|
function collectStats(state, now, goal) {
|
|
180
246
|
const progress = state.progress;
|
|
181
247
|
const todayKey = dayKey(now);
|
|
182
248
|
const today = progress.days[todayKey] ?? { asked: 0, correct: 0 };
|
|
183
249
|
const items = Object.entries(state.items);
|
|
184
|
-
const correctByDirection = { "en-pl": 0, "pl-en": 0 };
|
|
185
|
-
for (const [key, entry] of items) correctByDirection[key.split(":")[1]] += entry.correct;
|
|
186
250
|
|
|
187
251
|
// words missed today that were answered correctly again later the same day (one per card)
|
|
188
252
|
const lastMissByKey = new Map();
|
|
@@ -196,13 +260,15 @@ function collectStats(state, now, goal) {
|
|
|
196
260
|
const isSunday = new Date(`${todayKey}T12:00:00`).getDay() === 0;
|
|
197
261
|
return {
|
|
198
262
|
goal,
|
|
263
|
+
badges: progress.badges, // live: badges unlocked earlier in the same award pass count too
|
|
264
|
+
fallenStreak: progress.streak.fallen ?? 0,
|
|
265
|
+
streakCount: progress.streak.count,
|
|
199
266
|
totalCorrect: Object.values(progress.days).reduce((sum, d) => sum + d.correct, 0),
|
|
200
267
|
bestStreak: progress.streak.best,
|
|
201
268
|
bestCombo: progress.bestCombo ?? 0,
|
|
202
269
|
freezesUsed: progress.streak.freezesUsed ?? 0,
|
|
203
|
-
mastered: items
|
|
204
|
-
practiced: items.
|
|
205
|
-
correctByDirection,
|
|
270
|
+
mastered: masteredCards(items, state.dormant),
|
|
271
|
+
practiced: new Set(items.map(([key]) => cardOf(key))).size,
|
|
206
272
|
goalDaysThisMonth: goalDaysInMonth(progress, todayKey),
|
|
207
273
|
totalGoalDays: Object.values(progress.days).filter((d) => d.goalMet).length,
|
|
208
274
|
recoveredToday,
|
|
@@ -213,23 +279,31 @@ function collectStats(state, now, goal) {
|
|
|
213
279
|
}
|
|
214
280
|
|
|
215
281
|
// Goal check, evolution and badges after something was credited today → cheers
|
|
216
|
-
|
|
282
|
+
// `caughtUp`: nothing is left to practise today, which counts as reaching the goal (a small deck can't
|
|
283
|
+
// give 20 answers a day, and doing everything there is to do shouldn't cost a streak).
|
|
284
|
+
function award(state, progress, day, today, { correct, goal, rules, now, daysAway = 0, caughtUp = false, random = Math.random }) {
|
|
217
285
|
const cheers = [];
|
|
218
|
-
if (correct &&
|
|
286
|
+
if (((correct && day.correct >= goal) || caughtUp) && !day.goalMet) {
|
|
219
287
|
day.goalMet = true;
|
|
288
|
+
if (caughtUp) day.caughtUp = true;
|
|
220
289
|
const stageBefore = stageFor(progress.streak.best);
|
|
221
290
|
const streak = completeDay(progress, today, rules);
|
|
222
|
-
cheers.push({ kind: "goal", text:
|
|
291
|
+
cheers.push({ kind: "goal", text: t(caughtUp ? "cheers.allDone" : "cheers.goal", { count: streak }) });
|
|
223
292
|
const stageAfter = stageFor(progress.streak.best);
|
|
224
293
|
if (stageAfter.index > stageBefore.index)
|
|
225
|
-
cheers.push({ kind: "evolve", text:
|
|
294
|
+
cheers.push({ kind: "evolve", text: t("cheers.evolved", { name: stageAfter.name }) });
|
|
226
295
|
}
|
|
227
296
|
|
|
228
297
|
const stats = { ...collectStats(state, now, goal), event: { correct, hour: new Date(now).getHours(), daysAway } };
|
|
229
298
|
for (const badge of BADGES) {
|
|
230
299
|
if (!progress.badges[badge.id] && badge.test(stats)) {
|
|
231
300
|
progress.badges[badge.id] = today;
|
|
232
|
-
cheers.push({ kind: "badge",
|
|
301
|
+
cheers.push({ kind: "badge", id: badge.id, name: badge.name, desc: badge.desc, text: t("cheers.badge", { name: badge.name, desc: badge.desc }) });
|
|
302
|
+
// rolled and saved right away, before any animation, so quitting mid-roll can't reroll it
|
|
303
|
+
if (badge.id === "dragons-die" && !progress.element && !progress.elementDropped) {
|
|
304
|
+
progress.element = rollElement(random);
|
|
305
|
+
progress.elementRoll = { face: ELEMENTS.indexOf(progress.element) + 1, on: today };
|
|
306
|
+
}
|
|
233
307
|
}
|
|
234
308
|
}
|
|
235
309
|
return cheers;
|
|
@@ -238,29 +312,49 @@ function award(state, progress, day, today, { correct, goal, rules, now, daysAwa
|
|
|
238
312
|
const dayOf = (progress, today) => (progress.days[today] ??= { asked: 0, correct: 0 });
|
|
239
313
|
|
|
240
314
|
// Records one answer in the progress log → { cheers[] }. `state.items` must already include this answer.
|
|
241
|
-
export function applyAnswer(state, { result, goal, rules = NO_RULES, combo = 0, now = Date.now() }) {
|
|
315
|
+
export function applyAnswer(state, { result, goal, rules = NO_RULES, combo = 0, now = Date.now(), random = Math.random }) {
|
|
242
316
|
const progress = ensureProgress(state, now);
|
|
243
317
|
const today = dayKey(now);
|
|
244
318
|
const day = dayOf(progress, today);
|
|
245
319
|
const correct = result !== "wrong";
|
|
246
|
-
|
|
320
|
+
// full days away since the last correct answer (a wrong first answer back doesn't end the absence)
|
|
321
|
+
const daysAway = progress.lastActiveDay ? daysBetween(progress.lastActiveDay, today) - 1 : 0;
|
|
247
322
|
|
|
248
323
|
day.asked += 1;
|
|
249
324
|
day.correct += correct ? 1 : 0;
|
|
250
325
|
progress.bestCombo = Math.max(progress.bestCombo ?? 0, combo);
|
|
251
|
-
progress.lastActiveDay = today;
|
|
252
|
-
return { cheers: award(state, progress, day, today, { correct, goal, rules, now, daysAway }) };
|
|
326
|
+
if (correct) progress.lastActiveDay = today;
|
|
327
|
+
return { cheers: award(state, progress, day, today, { correct, goal, rules, now, daysAway, random }) };
|
|
253
328
|
}
|
|
254
329
|
|
|
255
330
|
// A wrong answer the user then declared right: it was already counted as asked, so only credit the correct one
|
|
256
|
-
export function applyOverrule(state, { goal, rules = NO_RULES, combo = 0, now = Date.now() }) {
|
|
331
|
+
export function applyOverrule(state, { goal, rules = NO_RULES, combo = 0, now = Date.now(), random = Math.random }) {
|
|
257
332
|
const progress = ensureProgress(state, now);
|
|
258
333
|
const today = dayKey(now);
|
|
259
334
|
const day = dayOf(progress, today);
|
|
260
335
|
|
|
336
|
+
const daysAway = progress.lastActiveDay ? daysBetween(progress.lastActiveDay, today) - 1 : 0;
|
|
261
337
|
day.correct += 1;
|
|
262
338
|
progress.bestCombo = Math.max(progress.bestCombo ?? 0, combo);
|
|
263
|
-
|
|
339
|
+
progress.lastActiveDay = today;
|
|
340
|
+
return { cheers: award(state, progress, day, today, { correct: true, goal, rules, now, daysAway, random }) };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Nothing left to practise today in the whole library → cheers. After at least one right answer today
|
|
344
|
+
// that counts as the goal (see award); with nothing answered at all the day only becomes a rest day, so just
|
|
345
|
+
// opening Dracosh never builds a streak. Nothing happens once the goal is met.
|
|
346
|
+
export function applyCaughtUp(state, { goal, rules = NO_RULES, now = Date.now(), random = Math.random }) {
|
|
347
|
+
const progress = ensureProgress(state, now);
|
|
348
|
+
const today = dayKey(now);
|
|
349
|
+
const day = dayOf(progress, today);
|
|
350
|
+
if (day.goalMet) return { cheers: [] };
|
|
351
|
+
if (!day.asked) {
|
|
352
|
+
day.nothingDue = true;
|
|
353
|
+
return { cheers: [] };
|
|
354
|
+
}
|
|
355
|
+
if (!day.correct) return { cheers: [] }; // a day of misses only isn't a day done
|
|
356
|
+
|
|
357
|
+
return { cheers: award(state, progress, day, today, { correct: false, goal, rules, now, caughtUp: true, random }) };
|
|
264
358
|
}
|
|
265
359
|
|
|
266
360
|
// everything the screens display, derived from state
|
|
@@ -277,21 +371,20 @@ export function snapshot(state, { goal, rules = NO_RULES, now = Date.now() }) {
|
|
|
277
371
|
streak: {
|
|
278
372
|
days,
|
|
279
373
|
best: progress.streak.best,
|
|
280
|
-
freezes: progress
|
|
374
|
+
freezes: freezesNow(progress, today, rules),
|
|
281
375
|
atRisk: days > 0 && !goalMet && !restToday && new Date(now).getHours() >= AT_RISK_HOUR
|
|
282
376
|
},
|
|
283
377
|
restToday,
|
|
284
378
|
skipWeekends: rules.skipWeekends,
|
|
285
379
|
vacation: vacationInfo(progress, today),
|
|
286
|
-
companion: stageFor(progress.streak.best),
|
|
380
|
+
companion: { ...stageFor(progress.streak.best), element: progress.element ?? null, roll: progress.elementRoll ?? null },
|
|
287
381
|
totalCorrect: stats.totalCorrect,
|
|
288
382
|
mastered: stats.mastered,
|
|
289
383
|
practiced: stats.practiced,
|
|
290
|
-
badges: BADGES.map((b) => ({ id: b.id, name: b.name, desc: b.desc, unlockedOn: progress.badges[b.id] ?? null }))
|
|
384
|
+
badges: BADGES.map((b) => ({ id: b.id, name: b.name, desc: b.desc, hidden: Boolean(b.hidden), unlockedOn: progress.badges[b.id] ?? null }))
|
|
291
385
|
};
|
|
292
386
|
}
|
|
293
387
|
|
|
294
|
-
const MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
|
295
388
|
|
|
296
389
|
// One calendar month, weeks Monday..Sunday: each day has the number of correct answers and whether the goal was met.
|
|
297
390
|
// `offset` moves back (negative) or forward from the current month.
|
|
@@ -315,7 +408,7 @@ export function calendarMonth(state, { offset = 0, rules = NO_RULES, now = Date.
|
|
|
315
408
|
for (let i = 0; i < padded.length; i += 7) weeks.push([...padded.slice(i, i + 7), ...Array(Math.max(0, i + 7 - padded.length)).fill(null)]);
|
|
316
409
|
|
|
317
410
|
return {
|
|
318
|
-
label:
|
|
411
|
+
label: new Intl.DateTimeFormat(dateLocale(), { month: "long", year: "numeric" }).format(first),
|
|
319
412
|
weeks,
|
|
320
413
|
goalDays: cells.filter((c) => c.goalMet).length,
|
|
321
414
|
correct: cells.reduce((sum, c) => sum + c.correct, 0),
|
|
@@ -361,3 +454,19 @@ export function missedOn(state, { offset = 0, now = Date.now() } = {}) {
|
|
|
361
454
|
.sort((a, b) => b.at - a.at);
|
|
362
455
|
return { date, items };
|
|
363
456
|
}
|
|
457
|
+
|
|
458
|
+
// ---- badges you haven't looked at yet (they light up when /badges opens) ----------------------------------
|
|
459
|
+
|
|
460
|
+
// → ids of badges unlocked but not yet seen in /badges, in the order they were won
|
|
461
|
+
export function unseenBadges(state, now = Date.now()) {
|
|
462
|
+
const progress = ensureProgress(state, now);
|
|
463
|
+
return BADGES.map((badge, index) => ({ id: badge.id, index, on: progress.badges[badge.id] }))
|
|
464
|
+
.filter((b) => b.on && !progress.seenBadges.includes(b.id))
|
|
465
|
+
.sort((a, b) => (a.on === b.on ? a.index - b.index : a.on < b.on ? -1 : 1))
|
|
466
|
+
.map((b) => b.id);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export function markBadgesSeen(state, now = Date.now()) {
|
|
470
|
+
const progress = ensureProgress(state, now);
|
|
471
|
+
progress.seenBadges = Object.keys(progress.badges);
|
|
472
|
+
}
|
package/src/quiet.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { dateLocale, t } from "./i18n.js";
|
|
1
2
|
const MINUTE = 60_000;
|
|
2
3
|
const HOUR = 60 * MINUTE;
|
|
3
4
|
|
|
@@ -16,9 +17,9 @@ export function quietEnd(ms, quiet) {
|
|
|
16
17
|
return end.getTime();
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
export const formatQuiet = (quiet) => (quiet ? `${String(quiet.from).padStart(2, "0")}:00–${String(quiet.to).padStart(2, "0")}:00` : "off");
|
|
20
|
+
export const formatQuiet = (quiet) => (quiet ? `${String(quiet.from).padStart(2, "0")}:00–${String(quiet.to).padStart(2, "0")}:00` : t("settings.off"));
|
|
20
21
|
|
|
21
|
-
//
|
|
22
|
+
// /pause durations: "" → 1 h, "30", "30m", "2h", "90s" → milliseconds, "off" → "off", anything else → null
|
|
22
23
|
export function parseSnooze(arg) {
|
|
23
24
|
const text = arg.trim().toLowerCase();
|
|
24
25
|
if (!text) return HOUR;
|
|
@@ -29,4 +30,4 @@ export function parseSnooze(arg) {
|
|
|
29
30
|
return amount > 0 ? amount : null;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
export const formatClock = (ms) => new Date(ms).toLocaleTimeString(
|
|
33
|
+
export const formatClock = (ms) => new Date(ms).toLocaleTimeString(dateLocale(), { hour: "2-digit", minute: "2-digit" });
|
package/src/scheduler.js
CHANGED
|
@@ -3,7 +3,7 @@ import { dayKey } from "./dates.js";
|
|
|
3
3
|
const MINUTE = 60_000;
|
|
4
4
|
const DAY = 24 * 60 * MINUTE;
|
|
5
5
|
|
|
6
|
-
// Leitner boxes: a correct answer moves the
|
|
6
|
+
// Leitner boxes: a correct answer moves the card one box up; a miss sends it back to box 1
|
|
7
7
|
export const BOX_INTERVALS_DAYS = [1, 3, 7, 14, 30];
|
|
8
8
|
export const RELEARN_DELAY = 10 * MINUTE;
|
|
9
9
|
export const NEW_PER_DAY = 20;
|
|
@@ -16,9 +16,7 @@ export function newToday(state, now) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const NEW_RATIO = 0.3; // when both due and unseen items exist, this share of questions introduces a new one
|
|
19
|
-
const EXTRA_PRACTICE_POOL = 10; // with nothing due, practice one of the next few upcoming items
|
|
20
19
|
const MAX_OVERDUE_WEIGHT_DAYS = 3;
|
|
21
|
-
const SOLO_DUE_MIX = 0.35; // how often a lone due card yields to other material instead of repeating
|
|
22
20
|
|
|
23
21
|
const pickRandom = (items, random) => items[Math.floor(random() * items.length)];
|
|
24
22
|
|
|
@@ -34,41 +32,52 @@ function pickDue(due, now, random) {
|
|
|
34
32
|
return due[due.length - 1];
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
35
|
+
// What to ask next: a due review or a new card (up to the daily cap), mixed at random; null when there
|
|
36
|
+
// is nothing to do right now. Cards are never asked before they're due: answering early would move them
|
|
37
|
+
// up the boxes without the spacing that makes them stick. Never the same note twice in a row while
|
|
38
|
+
// anything else is waiting. A `tour` deck is different: each card is asked once, in deck order, and
|
|
39
|
+
// never comes back as a review. A word's own `directions` (a cloze card's gaps) replace the deck's.
|
|
40
|
+
export function pickNext({ words, state, now = Date.now(), lastNoteId = null, directions = DIRECTIONS, tour = false, random = Math.random }) {
|
|
41
|
+
const candidates = words.flatMap((word) => (word.directions ?? directions).map((direction) => ({ word, direction, entry: state.items[itemKey(word.noteId, direction)] })));
|
|
42
|
+
if (tour) return candidates.find((c) => !c.entry) ?? null;
|
|
43
|
+
|
|
44
|
+
const canIntroduce = newToday(state, now) < NEW_PER_DAY;
|
|
45
|
+
const waiting = candidates.filter((c) => (c.entry ? c.entry.due <= now : canIntroduce));
|
|
46
|
+
if (!waiting.length) return null;
|
|
47
|
+
const pool = waiting.some((c) => c.word.noteId !== lastNoteId) ? waiting.filter((c) => c.word.noteId !== lastNoteId) : waiting;
|
|
48
|
+
|
|
49
|
+
const due = pool.filter((c) => c.entry);
|
|
50
|
+
const fresh = pool.filter((c) => !c.entry);
|
|
51
|
+
if (due.length && fresh.length && random() < NEW_RATIO) return pickRandom(fresh, random);
|
|
52
|
+
return due.length ? pickDue(due, now, random) : pickRandom(fresh, random);
|
|
53
|
+
}
|
|
50
54
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
// When something will next be waiting, once pickNext has nothing: the earliest due card, or the
|
|
56
|
+
// start of tomorrow when only new cards are left (the daily cap is reached). null = nothing ever.
|
|
57
|
+
export function nextDueAt({ words, state, now = Date.now(), directions = DIRECTIONS }) {
|
|
58
|
+
let next = null;
|
|
59
|
+
for (const word of words) {
|
|
60
|
+
for (const direction of word.directions ?? directions) {
|
|
61
|
+
const entry = state.items[itemKey(word.noteId, direction)];
|
|
62
|
+
const at = entry ? entry.due : startOfTomorrow(now);
|
|
63
|
+
if (next === null || at < next) next = at;
|
|
64
|
+
}
|
|
58
65
|
}
|
|
66
|
+
return next;
|
|
67
|
+
}
|
|
59
68
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return upcoming.length ? pickRandom(upcoming, random) : pickRandom(unseen, random) ?? null;
|
|
69
|
+
export function startOfTomorrow(now) {
|
|
70
|
+
const date = new Date(now);
|
|
71
|
+
date.setHours(24, 0, 0, 0);
|
|
72
|
+
return date.getTime();
|
|
65
73
|
}
|
|
66
74
|
|
|
67
|
-
// → the updated entry (pure; caller stores it). A correct answer given with a hint counts, but doesn't move up a box.
|
|
68
75
|
export function grade(entry, correct, now = Date.now(), { hinted = false } = {}) {
|
|
69
76
|
const prev = entry ?? { box: 0, seen: 0, correct: 0, wrong: 0, streak: 0 };
|
|
70
77
|
if (correct) {
|
|
71
|
-
|
|
78
|
+
// right after a miss the card starts over at box 1 (back tomorrow) instead of skipping ahead
|
|
79
|
+
const relearning = prev.seen > 0 && prev.streak === 0;
|
|
80
|
+
const box = hinted || relearning ? Math.max(prev.box, 1) : Math.min(prev.box + 1, BOX_INTERVALS_DAYS.length);
|
|
72
81
|
return { ...prev, box, due: now + BOX_INTERVALS_DAYS[box - 1] * DAY, seen: prev.seen + 1, correct: prev.correct + 1, streak: prev.streak + 1, last: now };
|
|
73
82
|
}
|
|
74
83
|
return { ...prev, box: 1, due: now + RELEARN_DELAY, seen: prev.seen + 1, wrong: prev.wrong + 1, streak: 0, last: now };
|