dracosh 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +10 -0
- package/README.md +100 -2
- package/assets/sounds/ask.wav +0 -0
- 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/assets/sounds/close.wav +0 -0
- package/assets/sounds/combo.wav +0 -0
- package/assets/sounds/correct-0.wav +0 -0
- package/assets/sounds/correct-1.wav +0 -0
- package/assets/sounds/correct-2.wav +0 -0
- package/assets/sounds/correct-3.wav +0 -0
- package/assets/sounds/correct-4.wav +0 -0
- package/assets/sounds/correct-5.wav +0 -0
- package/assets/sounds/evolve.wav +0 -0
- package/assets/sounds/goal.wav +0 -0
- package/assets/sounds/wrong.wav +0 -0
- package/data/tips.json +32 -0
- package/package.json +47 -4
- package/src/alerts.js +38 -0
- package/src/anki.js +36 -0
- package/src/backup.js +95 -0
- package/src/cli.js +197 -0
- package/src/commands.js +28 -0
- package/src/dates.js +15 -0
- package/src/demo.js +11 -0
- package/src/diff.js +46 -0
- package/src/hint.js +12 -0
- package/src/judge.js +78 -0
- package/src/library.js +127 -0
- package/src/lock.js +63 -0
- package/src/manage.js +75 -0
- package/src/parse.js +42 -0
- package/src/pause.js +23 -0
- package/src/progress.js +455 -0
- package/src/quiet.js +32 -0
- package/src/scheduler.js +94 -0
- package/src/session.js +227 -0
- package/src/settings.js +38 -0
- package/src/starter.js +38 -0
- package/src/store.js +100 -0
- package/src/tips.js +30 -0
- package/src/ui/AddCard.js +172 -0
- package/src/ui/AnswerInput.js +85 -0
- package/src/ui/App.js +509 -0
- package/src/ui/BadgeUnlock.js +50 -0
- package/src/ui/Badges.js +122 -0
- package/src/ui/Choice.js +33 -0
- package/src/ui/Companion.js +45 -0
- package/src/ui/Confetti.js +46 -0
- package/src/ui/Decks.js +49 -0
- package/src/ui/DieRoll.js +104 -0
- package/src/ui/Evolution.js +59 -0
- package/src/ui/Hatch.js +100 -0
- package/src/ui/Header.js +117 -0
- package/src/ui/Help.js +68 -0
- package/src/ui/Mascot.js +178 -0
- package/src/ui/Missed.js +53 -0
- package/src/ui/Preview.js +256 -0
- package/src/ui/Reset.js +72 -0
- package/src/ui/Settings.js +69 -0
- package/src/ui/Stats.js +111 -0
- package/src/ui/Summary.js +41 -0
- package/src/ui/TheOne.js +121 -0
- package/src/ui/icons.js +341 -0
- package/src/ui/kit.js +120 -0
- package/src/vacation.js +56 -0
- package/src/words.js +23 -0
package/src/parse.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const ENTITIES = { " ": " ", "&": "&", "<": "<", ">": ">", """: '"', "'": "'" };
|
|
2
|
+
|
|
3
|
+
const decode = (s) => s.replace(/&(?:nbsp|amp|lt|gt|quot|#39);/g, (m) => ENTITIES[m]);
|
|
4
|
+
|
|
5
|
+
// block-level closers and <br> become line breaks, every other tag disappears
|
|
6
|
+
function htmlToLines(html) {
|
|
7
|
+
return decode(
|
|
8
|
+
html
|
|
9
|
+
.replace(/<br\s*\/?>|<\/(?:div|p|span)>/gi, "\n")
|
|
10
|
+
.replace(/<[^>]*>/g, "")
|
|
11
|
+
)
|
|
12
|
+
.split("\n")
|
|
13
|
+
.map((l) => l.trim())
|
|
14
|
+
.filter(Boolean);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const unquote = (s) => s.replace(/^["“”]+|["“”]+$/g, "").trim();
|
|
18
|
+
|
|
19
|
+
// "define" cards (uppercase domain tag + English definition) have no translation to quiz on
|
|
20
|
+
const isDefineCard = (backHtml) => /text-transform:\s*uppercase/i.test(backHtml);
|
|
21
|
+
|
|
22
|
+
// → { noteId, word, translations[], example } or null when the card isn't a translation card
|
|
23
|
+
export function parseNote({ noteId, front, back }) {
|
|
24
|
+
if (isDefineCard(back)) return null;
|
|
25
|
+
const word = htmlToLines(front).join(" ");
|
|
26
|
+
const [first, ...rest] = htmlToLines(back);
|
|
27
|
+
if (!word || !first) return null;
|
|
28
|
+
const translations = first.split(/[,;]/).map((t) => t.trim()).filter(Boolean);
|
|
29
|
+
if (!translations.length) return null;
|
|
30
|
+
return { noteId, word, translations, example: rest.length ? unquote(rest.join(" ")) : "" };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function parseNotes(notes) {
|
|
34
|
+
const words = [];
|
|
35
|
+
let skipped = 0;
|
|
36
|
+
for (const note of notes) {
|
|
37
|
+
const parsed = parseNote(note);
|
|
38
|
+
if (parsed) words.push(parsed);
|
|
39
|
+
else skipped += 1;
|
|
40
|
+
}
|
|
41
|
+
return { words, skipped };
|
|
42
|
+
}
|
package/src/pause.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { parseSnooze } from "./quiet.js";
|
|
2
|
+
import { parseVacation } from "./vacation.js";
|
|
3
|
+
|
|
4
|
+
export const PAUSE_HELP = "Try /pause 30m, /pause 2h, /pause 3d or /pause 24.12 2.01";
|
|
5
|
+
|
|
6
|
+
// "/pause" arguments, one command for a short break and for days off:
|
|
7
|
+
// "" → { action: "list" } what's paused or planned
|
|
8
|
+
// "off" → { action: "off" } questions back, days off cancelled
|
|
9
|
+
// "30m", "2h", "90s" → { action: "snooze", ms } a break today; the streak isn't touched
|
|
10
|
+
// "3d", "24.12 2.01"… → { action: "days", from, to } days off that keep your streak
|
|
11
|
+
// A bare whole number is refused: "30" could mean minutes or days ("24.12" is a date). → null when not understood.
|
|
12
|
+
export function parsePause(args, today) {
|
|
13
|
+
const text = args.trim().toLowerCase();
|
|
14
|
+
if (!text) return { action: "list" };
|
|
15
|
+
if (["off", "cancel", "clear"].includes(text)) return { action: "off" };
|
|
16
|
+
if (/^\d+$/.test(text)) return null;
|
|
17
|
+
if (/^\d+(\.\d+)?\s*(s|m|h)$/.test(text)) {
|
|
18
|
+
const ms = parseSnooze(text);
|
|
19
|
+
return typeof ms === "number" ? { action: "snooze", ms } : null;
|
|
20
|
+
}
|
|
21
|
+
const days = parseVacation(text, today);
|
|
22
|
+
return days?.action === "add" ? { action: "days", from: days.from, to: days.to } : null;
|
|
23
|
+
}
|
package/src/progress.js
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { addDays, dayKey, daysBetween, weekStart } from "./dates.js";
|
|
2
|
+
import { BOX_INTERVALS_DAYS } from "./scheduler.js";
|
|
3
|
+
|
|
4
|
+
export const MAX_FREEZES = 2;
|
|
5
|
+
const MAX_BOX = BOX_INTERVALS_DAYS.length;
|
|
6
|
+
const AT_RISK_HOUR = 19;
|
|
7
|
+
const COMEBACK_AFTER_DAYS = 5;
|
|
8
|
+
const MISSED_KEEP_DAYS = 14;
|
|
9
|
+
const FALL_MIN = 30; // a lost streak this long counts as a fall for "Dragon's die"
|
|
10
|
+
|
|
11
|
+
// The die's faces: winning "Dragon's die" rolls one, and the dragon keeps that element for good.
|
|
12
|
+
export const ELEMENTS = ["earth", "wind", "water", "ice", "fire", "cosmos"];
|
|
13
|
+
export const rollElement = (random = Math.random) => ELEMENTS[Math.floor(random() * ELEMENTS.length)];
|
|
14
|
+
export const ELEMENT_ADJECTIVE = { earth: "Earth", wind: "Wind", water: "Water", ice: "Ice", fire: "Fire", cosmos: "Cosmic" };
|
|
15
|
+
|
|
16
|
+
// what the dragon is called: its form, with its element in front once it has one ("Ice Drake")
|
|
17
|
+
export const dragonName = (stage, element = null) => (element ? `${ELEMENT_ADJECTIVE[element]} ${STAGES[stage].name}` : STAGES[stage].name);
|
|
18
|
+
|
|
19
|
+
// The companion evolves permanently with your best streak, so a broken streak never takes a form away.
|
|
20
|
+
export const STAGES = [
|
|
21
|
+
{ name: "Hatchling", from: 0 },
|
|
22
|
+
{ name: "Imp", from: 7 },
|
|
23
|
+
{ name: "Whelp", from: 30 },
|
|
24
|
+
{ name: "Drake", from: 90 },
|
|
25
|
+
{ name: "Dragon", from: 180 },
|
|
26
|
+
{ name: "Legend", from: 365 }
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
export function stageFor(bestStreak) {
|
|
30
|
+
let index = 0;
|
|
31
|
+
while (index + 1 < STAGES.length && bestStreak >= STAGES[index + 1].from) index += 1;
|
|
32
|
+
const next = STAGES[index + 1] ?? null;
|
|
33
|
+
return { index, name: STAGES[index].name, nextName: next?.name ?? null, nextAt: next?.from ?? null };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function ensureProgress(state, now = Date.now()) {
|
|
37
|
+
state.progress ??= {
|
|
38
|
+
days: {},
|
|
39
|
+
streak: { count: 0, best: 0, lastGoalDay: null, freezes: MAX_FREEZES, freezeWeek: weekStart(dayKey(now)), freezesUsed: 0 },
|
|
40
|
+
badges: {},
|
|
41
|
+
bestCombo: 0,
|
|
42
|
+
lastActiveDay: null,
|
|
43
|
+
vacations: []
|
|
44
|
+
};
|
|
45
|
+
state.progress.vacations ??= [];
|
|
46
|
+
// badges already shown in /badges; ones unlocked before this was tracked count as seen, so they don't all flash at once
|
|
47
|
+
state.progress.seenBadges ??= Object.keys(state.progress.badges);
|
|
48
|
+
return state.progress;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ---- rest days: weekends (a setting) and vacations never break a streak ----------------------------------------
|
|
52
|
+
|
|
53
|
+
const NO_RULES = { skipWeekends: false };
|
|
54
|
+
|
|
55
|
+
export function isRestDay(progress, key, rules = NO_RULES) {
|
|
56
|
+
if (progress.days?.[key]?.nothingDue) return true; // opened, but no card anywhere was waiting
|
|
57
|
+
if (rules.skipWeekends) {
|
|
58
|
+
const weekday = new Date(`${key}T12:00:00`).getDay();
|
|
59
|
+
if (weekday === 0 || weekday === 6) return true;
|
|
60
|
+
}
|
|
61
|
+
return (progress.vacations ?? []).some((v) => key >= v.from && key <= v.to);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Freezes as they stand today, walking day by day from the last goal day: every new week gives one
|
|
65
|
+
// back (never more than MAX_FREEZES in stock), every missed working day takes one. A gap with more
|
|
66
|
+
// missed days than freezes breaks the streak and takes none. The stored streak.freezes / freezeWeek
|
|
67
|
+
// are as of the last time this was settled (a goal day); nothing is spent until then, but the
|
|
68
|
+
// screens show this walk, so they never promise freezes that are already used.
|
|
69
|
+
function walkFreezes(progress, today, rules) {
|
|
70
|
+
const { lastGoalDay, freezes: stored, freezeWeek } = progress.streak;
|
|
71
|
+
const walk = (spend) => {
|
|
72
|
+
let freezes = stored;
|
|
73
|
+
let week = freezeWeek ?? weekStart(today);
|
|
74
|
+
let used = 0;
|
|
75
|
+
for (let day = lastGoalDay ? addDays(lastGoalDay, 1) : today; day <= today; day = addDays(day, 1)) {
|
|
76
|
+
const dayWeek = weekStart(day);
|
|
77
|
+
if (dayWeek > week) {
|
|
78
|
+
freezes = Math.min(MAX_FREEZES, freezes + Math.round(daysBetween(week, dayWeek) / 7));
|
|
79
|
+
week = dayWeek;
|
|
80
|
+
}
|
|
81
|
+
if (!spend || day === today || isRestDay(progress, day, rules)) continue;
|
|
82
|
+
if (freezes === 0) return null; // the streak breaks here
|
|
83
|
+
freezes -= 1;
|
|
84
|
+
used += 1;
|
|
85
|
+
}
|
|
86
|
+
return { freezes, week, used };
|
|
87
|
+
};
|
|
88
|
+
const spent = lastGoalDay ? walk(true) : null;
|
|
89
|
+
return spent ? { ...spent, broken: false } : { ...walk(false), used: 0, broken: Boolean(lastGoalDay) };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// the streak as shown right now: alive while no working day was skipped, or while freezes can cover the gap
|
|
93
|
+
export function currentStreak(progress, today, rules = NO_RULES) {
|
|
94
|
+
const { count, lastGoalDay } = progress.streak;
|
|
95
|
+
if (!lastGoalDay || !count) return 0;
|
|
96
|
+
return walkFreezes(progress, today, rules).broken ? 0 : count;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const freezesNow = (progress, today, rules = NO_RULES) => walkFreezes(progress, today, rules).freezes;
|
|
100
|
+
|
|
101
|
+
function completeDay(progress, today, rules) {
|
|
102
|
+
const streak = progress.streak;
|
|
103
|
+
const { freezes, week, used, broken } = walkFreezes(progress, today, rules);
|
|
104
|
+
if (!streak.lastGoalDay) {
|
|
105
|
+
streak.count = 1;
|
|
106
|
+
} else if (broken) {
|
|
107
|
+
if (streak.count >= FALL_MIN) streak.fallen = streak.count; // the most recent long streak that was lost
|
|
108
|
+
streak.count = 1;
|
|
109
|
+
} else {
|
|
110
|
+
streak.count += 1;
|
|
111
|
+
streak.freezesUsed = (streak.freezesUsed ?? 0) + used;
|
|
112
|
+
}
|
|
113
|
+
Object.assign(streak, { freezes, freezeWeek: week, lastGoalDay: today });
|
|
114
|
+
streak.best = Math.max(streak.best, streak.count);
|
|
115
|
+
return streak.count;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ---- vacations ---------------------------------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
export function addVacation(state, from, to, now = Date.now()) {
|
|
121
|
+
const progress = ensureProgress(state, now);
|
|
122
|
+
const merged = [...progress.vacations, { from, to }].sort((a, b) => (a.from < b.from ? -1 : 1));
|
|
123
|
+
progress.vacations = merged.reduce((out, v) => {
|
|
124
|
+
const last = out[out.length - 1];
|
|
125
|
+
if (last && v.from <= addDays(last.to, 1)) last.to = v.to > last.to ? v.to : last.to; // overlapping or touching: one vacation
|
|
126
|
+
else out.push({ ...v });
|
|
127
|
+
return out;
|
|
128
|
+
}, []);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ends the vacation that is running (as of yesterday) and drops the ones still ahead
|
|
132
|
+
export function cancelVacations(state, now = Date.now()) {
|
|
133
|
+
const progress = ensureProgress(state, now);
|
|
134
|
+
const today = dayKey(now);
|
|
135
|
+
progress.vacations = progress.vacations
|
|
136
|
+
.map((v) => (v.to < today ? v : v.from < today ? { from: v.from, to: addDays(today, -1) } : null))
|
|
137
|
+
.filter(Boolean);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// the vacation running today (if any) and the ones still ahead
|
|
141
|
+
export function vacationInfo(progress, today) {
|
|
142
|
+
const upcoming = (progress.vacations ?? []).filter((v) => v.to >= today);
|
|
143
|
+
const active = upcoming.find((v) => v.from <= today) ?? null;
|
|
144
|
+
return { activeUntil: active?.to ?? null, upcoming };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const goalMetOn = (progress, key) => Boolean(progress.days[key]?.goalMet);
|
|
148
|
+
|
|
149
|
+
function goalDaysInMonth(progress, today) {
|
|
150
|
+
const prefix = today.slice(0, 7);
|
|
151
|
+
return Object.entries(progress.days).filter(([key, day]) => key.startsWith(prefix) && day.goalMet).length;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Every day of the Monday-to-Sunday week containing `today` hit its goal
|
|
155
|
+
function perfectWeek(progress, today) {
|
|
156
|
+
const monday = weekStart(today);
|
|
157
|
+
return Array.from({ length: 7 }, (_, i) => addDays(monday, i)).every((key) => goalMetOn(progress, key));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// `s` = stats from collectStats() plus `event`: what this very answer was (correct, hour, days since last activity)
|
|
161
|
+
export const BADGES = [
|
|
162
|
+
{ id: "hello", name: "Hello!", desc: "your first correct answer", test: (s) => s.totalCorrect >= 1 },
|
|
163
|
+
{ id: "first-steps", name: "First steps", desc: "10 correct answers", test: (s) => s.totalCorrect >= 10 },
|
|
164
|
+
{ id: "century", name: "Century", desc: "100 correct answers", test: (s) => s.totalCorrect >= 100 },
|
|
165
|
+
{ id: "thousand", name: "Thousand", desc: "1,000 correct answers", test: (s) => s.totalCorrect >= 1000 },
|
|
166
|
+
{ id: "five-thousand", name: "Five thousand", desc: "5,000 correct answers", test: (s) => s.totalCorrect >= 5000 },
|
|
167
|
+
{ id: "ten-thousand", name: "Ten thousand", desc: "10,000 correct answers", test: (s) => s.totalCorrect >= 10000 },
|
|
168
|
+
{ id: "dragons-hoard", name: "Dragon's hoard", desc: "20,000 correct answers", test: (s) => s.totalCorrect >= 20000 },
|
|
169
|
+
|
|
170
|
+
{ id: "on-a-roll", name: "On a roll", desc: "3-day streak", test: (s) => s.bestStreak >= 3 },
|
|
171
|
+
{ id: "week", name: "Full week", desc: "7-day streak", test: (s) => s.bestStreak >= 7 },
|
|
172
|
+
{ id: "fortnight", name: "Fortnight", desc: "14-day streak", test: (s) => s.bestStreak >= 14 },
|
|
173
|
+
{ id: "month", name: "Iron habit", desc: "30-day streak", test: (s) => s.bestStreak >= 30 },
|
|
174
|
+
{ id: "hundred-days", name: "Centurion", desc: "100-day streak", test: (s) => s.bestStreak >= 100 },
|
|
175
|
+
{ id: "dragonheart", name: "Dragonheart", desc: "180-day streak", test: (s) => s.bestStreak >= 180 },
|
|
176
|
+
|
|
177
|
+
{ id: "keeper", name: "Word keeper", desc: "10 cards mastered", test: (s) => s.mastered >= 10 },
|
|
178
|
+
{ id: "collector", name: "Collector", desc: "25 cards mastered", test: (s) => s.mastered >= 25 },
|
|
179
|
+
{ id: "dictionary", name: "Walking dictionary", desc: "50 cards mastered", test: (s) => s.mastered >= 50 },
|
|
180
|
+
{ id: "lexicon", name: "Living lexicon", desc: "150 cards mastered", test: (s) => s.mastered >= 150 },
|
|
181
|
+
{ id: "explorer", name: "Explorer", desc: "250 cards practised", test: (s) => s.practiced >= 250 },
|
|
182
|
+
|
|
183
|
+
{ id: "flawless", name: "Flawless", desc: "10+ answers in a day, no miss", test: (s) => s.today.asked >= 10 && s.today.correct === s.today.asked },
|
|
184
|
+
{ id: "overachiever", name: "Overachiever", desc: "double your daily goal in a day", test: (s) => s.today.correct >= s.goal * 2 },
|
|
185
|
+
{ id: "marathon", name: "Marathon", desc: "30 correct answers in a day", test: (s) => s.today.correct >= 30 },
|
|
186
|
+
|
|
187
|
+
{ id: "hot-streak", name: "Hot streak", desc: "10 correct in a row", test: (s) => s.bestCombo >= 10 },
|
|
188
|
+
{ id: "unstoppable", name: "Unstoppable", desc: "25 correct in a row", test: (s) => s.bestCombo >= 25 },
|
|
189
|
+
{ id: "combo-master", name: "Combo master", desc: "50 correct in a row", test: (s) => s.bestCombo >= 50 },
|
|
190
|
+
|
|
191
|
+
{ id: "early-bird", name: "Early bird", desc: "a correct answer before 8:00", test: (s) => s.event.correct && s.event.hour < 8 },
|
|
192
|
+
{ id: "night-owl", name: "Night owl", desc: "a correct answer after 23:00", test: (s) => s.event.correct && s.event.hour >= 23 },
|
|
193
|
+
|
|
194
|
+
{ id: "comeback", name: "Welcome back", desc: "back after 5+ days away", test: (s) => s.event.correct && s.event.daysAway >= COMEBACK_AFTER_DAYS },
|
|
195
|
+
{ id: "redemption", name: "Redemption", desc: "5 words missed today, got right later the same day", test: (s) => s.recoveredToday >= 5 },
|
|
196
|
+
{ id: "frozen", name: "Frozen in time", desc: "a freeze saved your streak", test: (s) => s.freezesUsed >= 1 },
|
|
197
|
+
{ id: "weekend", name: "Weekend warrior", desc: "goal on both Saturday and Sunday", test: (s) => s.weekendDone },
|
|
198
|
+
{ id: "perfect-week", name: "Perfect week", desc: "goal every day, Monday to Sunday", test: (s) => s.perfectWeek },
|
|
199
|
+
{ id: "regular", name: "Regular", desc: "goal on 20 days in one month", test: (s) => s.goalDaysThisMonth >= 20 },
|
|
200
|
+
{ id: "machine", name: "The machine", desc: "goal on 25 days in one month", test: (s) => s.goalDaysThisMonth >= 25 },
|
|
201
|
+
{ id: "hundred-club", name: "Hundred club", desc: "goal reached on 100 days in total", test: (s) => s.totalGoalDays >= 100 },
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
// keep this one last: it checks the others, including any unlocked earlier in the same answer
|
|
205
|
+
{ id: "one-ring", name: "The One...", desc: "every other badge", test: (s) => BADGES.every((b) => b.id === "one-ring" || b.hidden || s.badges[b.id]) },
|
|
206
|
+
|
|
207
|
+
// hidden: shown as "???" until won, and not needed for The One
|
|
208
|
+
{
|
|
209
|
+
id: "dragons-die",
|
|
210
|
+
name: "Dragon's die",
|
|
211
|
+
desc: "lost a 30+ day streak, then built a longer one",
|
|
212
|
+
hidden: true,
|
|
213
|
+
test: (s) => s.fallenStreak >= FALL_MIN && s.streakCount > s.fallenStreak
|
|
214
|
+
}
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
// progress is kept per card and direction ("id:en-pl"); badges count cards
|
|
218
|
+
const cardOf = (key) => key.slice(0, key.lastIndexOf(":"));
|
|
219
|
+
|
|
220
|
+
// a card is mastered once every direction it's been asked in has reached the last box
|
|
221
|
+
function masteredCards(items) {
|
|
222
|
+
const byCard = new Map();
|
|
223
|
+
for (const [key, entry] of items) byCard.set(cardOf(key), (byCard.get(cardOf(key)) ?? true) && entry.box === MAX_BOX);
|
|
224
|
+
return [...byCard.values()].filter(Boolean).length;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function collectStats(state, now, goal) {
|
|
228
|
+
const progress = state.progress;
|
|
229
|
+
const todayKey = dayKey(now);
|
|
230
|
+
const today = progress.days[todayKey] ?? { asked: 0, correct: 0 };
|
|
231
|
+
const items = Object.entries(state.items);
|
|
232
|
+
|
|
233
|
+
// words missed today that were answered correctly again later the same day (one per card)
|
|
234
|
+
const lastMissByKey = new Map();
|
|
235
|
+
for (const miss of progress.days[todayKey]?.missed ?? []) lastMissByKey.set(miss.key, miss);
|
|
236
|
+
let recoveredToday = 0;
|
|
237
|
+
for (const [key, miss] of lastMissByKey) {
|
|
238
|
+
const entry = state.items[key];
|
|
239
|
+
if (entry && entry.streak > 0 && entry.last > miss.at) recoveredToday += 1;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const isSunday = new Date(`${todayKey}T12:00:00`).getDay() === 0;
|
|
243
|
+
return {
|
|
244
|
+
goal,
|
|
245
|
+
badges: progress.badges, // live: badges unlocked earlier in the same award pass count too
|
|
246
|
+
fallenStreak: progress.streak.fallen ?? 0,
|
|
247
|
+
streakCount: progress.streak.count,
|
|
248
|
+
totalCorrect: Object.values(progress.days).reduce((sum, d) => sum + d.correct, 0),
|
|
249
|
+
bestStreak: progress.streak.best,
|
|
250
|
+
bestCombo: progress.bestCombo ?? 0,
|
|
251
|
+
freezesUsed: progress.streak.freezesUsed ?? 0,
|
|
252
|
+
mastered: masteredCards(items),
|
|
253
|
+
practiced: new Set(items.map(([key]) => cardOf(key))).size,
|
|
254
|
+
goalDaysThisMonth: goalDaysInMonth(progress, todayKey),
|
|
255
|
+
totalGoalDays: Object.values(progress.days).filter((d) => d.goalMet).length,
|
|
256
|
+
recoveredToday,
|
|
257
|
+
weekendDone: isSunday && goalMetOn(progress, todayKey) && goalMetOn(progress, addDays(todayKey, -1)),
|
|
258
|
+
perfectWeek: goalMetOn(progress, todayKey) && perfectWeek(progress, todayKey),
|
|
259
|
+
today
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Goal check, evolution and badges after something was credited today → cheers
|
|
264
|
+
// `caughtUp`: nothing is left to practise today, which counts as reaching the goal (a small deck can't
|
|
265
|
+
// give 20 answers a day, and doing everything there is to do shouldn't cost a streak).
|
|
266
|
+
function award(state, progress, day, today, { correct, goal, rules, now, daysAway = 0, caughtUp = false, random = Math.random }) {
|
|
267
|
+
const cheers = [];
|
|
268
|
+
if (((correct && day.correct >= goal) || caughtUp) && !day.goalMet) {
|
|
269
|
+
day.goalMet = true;
|
|
270
|
+
if (caughtUp) day.caughtUp = true;
|
|
271
|
+
const stageBefore = stageFor(progress.streak.best);
|
|
272
|
+
const streak = completeDay(progress, today, rules);
|
|
273
|
+
cheers.push({ kind: "goal", text: `${caughtUp ? "All done for today" : "Daily goal reached"} · ${streak}-day streak` });
|
|
274
|
+
const stageAfter = stageFor(progress.streak.best);
|
|
275
|
+
if (stageAfter.index > stageBefore.index)
|
|
276
|
+
cheers.push({ kind: "evolve", text: `Your companion evolved into a${/^[aeiou]/i.test(stageAfter.name) ? "n" : ""} ${stageAfter.name}!` });
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const stats = { ...collectStats(state, now, goal), event: { correct, hour: new Date(now).getHours(), daysAway } };
|
|
280
|
+
for (const badge of BADGES) {
|
|
281
|
+
if (!progress.badges[badge.id] && badge.test(stats)) {
|
|
282
|
+
progress.badges[badge.id] = today;
|
|
283
|
+
cheers.push({ kind: "badge", id: badge.id, name: badge.name, desc: badge.desc, text: `Badge unlocked: ${badge.name} · ${badge.desc}` });
|
|
284
|
+
// rolled and saved right away, before any animation, so quitting mid-roll can't reroll it
|
|
285
|
+
if (badge.id === "dragons-die" && !progress.element && !progress.elementDropped) {
|
|
286
|
+
progress.element = rollElement(random);
|
|
287
|
+
progress.elementRoll = { face: ELEMENTS.indexOf(progress.element) + 1, on: today };
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return cheers;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const dayOf = (progress, today) => (progress.days[today] ??= { asked: 0, correct: 0 });
|
|
295
|
+
|
|
296
|
+
// Records one answer in the progress log → { cheers[] }. `state.items` must already include this answer.
|
|
297
|
+
export function applyAnswer(state, { result, goal, rules = NO_RULES, combo = 0, now = Date.now(), random = Math.random }) {
|
|
298
|
+
const progress = ensureProgress(state, now);
|
|
299
|
+
const today = dayKey(now);
|
|
300
|
+
const day = dayOf(progress, today);
|
|
301
|
+
const correct = result !== "wrong";
|
|
302
|
+
// full days away since the last correct answer (a wrong first answer back doesn't end the absence)
|
|
303
|
+
const daysAway = progress.lastActiveDay ? daysBetween(progress.lastActiveDay, today) - 1 : 0;
|
|
304
|
+
|
|
305
|
+
day.asked += 1;
|
|
306
|
+
day.correct += correct ? 1 : 0;
|
|
307
|
+
progress.bestCombo = Math.max(progress.bestCombo ?? 0, combo);
|
|
308
|
+
if (correct) progress.lastActiveDay = today;
|
|
309
|
+
return { cheers: award(state, progress, day, today, { correct, goal, rules, now, daysAway, random }) };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// A wrong answer the user then declared right: it was already counted as asked, so only credit the correct one
|
|
313
|
+
export function applyOverrule(state, { goal, rules = NO_RULES, combo = 0, now = Date.now(), random = Math.random }) {
|
|
314
|
+
const progress = ensureProgress(state, now);
|
|
315
|
+
const today = dayKey(now);
|
|
316
|
+
const day = dayOf(progress, today);
|
|
317
|
+
|
|
318
|
+
const daysAway = progress.lastActiveDay ? daysBetween(progress.lastActiveDay, today) - 1 : 0;
|
|
319
|
+
day.correct += 1;
|
|
320
|
+
progress.bestCombo = Math.max(progress.bestCombo ?? 0, combo);
|
|
321
|
+
progress.lastActiveDay = today;
|
|
322
|
+
return { cheers: award(state, progress, day, today, { correct: true, goal, rules, now, daysAway, random }) };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Nothing left to practise today in the whole library → cheers. After at least one right answer today
|
|
326
|
+
// that counts as the goal (see award); with nothing answered at all the day only becomes a rest day, so just
|
|
327
|
+
// opening Dracosh never builds a streak. Nothing happens once the goal is met.
|
|
328
|
+
export function applyCaughtUp(state, { goal, rules = NO_RULES, now = Date.now(), random = Math.random }) {
|
|
329
|
+
const progress = ensureProgress(state, now);
|
|
330
|
+
const today = dayKey(now);
|
|
331
|
+
const day = dayOf(progress, today);
|
|
332
|
+
if (day.goalMet) return { cheers: [] };
|
|
333
|
+
if (!day.asked) {
|
|
334
|
+
day.nothingDue = true;
|
|
335
|
+
return { cheers: [] };
|
|
336
|
+
}
|
|
337
|
+
if (!day.correct) return { cheers: [] }; // a day of misses only isn't a day done
|
|
338
|
+
|
|
339
|
+
return { cheers: award(state, progress, day, today, { correct: false, goal, rules, now, caughtUp: true, random }) };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// everything the screens display, derived from state
|
|
343
|
+
export function snapshot(state, { goal, rules = NO_RULES, now = Date.now() }) {
|
|
344
|
+
const progress = ensureProgress(state, now);
|
|
345
|
+
const stats = collectStats(state, now, goal);
|
|
346
|
+
const today = dayKey(now);
|
|
347
|
+
const days = currentStreak(progress, today, rules);
|
|
348
|
+
const restToday = isRestDay(progress, today, rules);
|
|
349
|
+
const goalMet = Boolean(stats.today.goalMet) || stats.today.correct >= goal;
|
|
350
|
+
return {
|
|
351
|
+
today: { ...stats.today, goalMet },
|
|
352
|
+
goal,
|
|
353
|
+
streak: {
|
|
354
|
+
days,
|
|
355
|
+
best: progress.streak.best,
|
|
356
|
+
freezes: freezesNow(progress, today, rules),
|
|
357
|
+
atRisk: days > 0 && !goalMet && !restToday && new Date(now).getHours() >= AT_RISK_HOUR
|
|
358
|
+
},
|
|
359
|
+
restToday,
|
|
360
|
+
skipWeekends: rules.skipWeekends,
|
|
361
|
+
vacation: vacationInfo(progress, today),
|
|
362
|
+
companion: { ...stageFor(progress.streak.best), element: progress.element ?? null, roll: progress.elementRoll ?? null },
|
|
363
|
+
totalCorrect: stats.totalCorrect,
|
|
364
|
+
mastered: stats.mastered,
|
|
365
|
+
practiced: stats.practiced,
|
|
366
|
+
badges: BADGES.map((b) => ({ id: b.id, name: b.name, desc: b.desc, hidden: Boolean(b.hidden), unlockedOn: progress.badges[b.id] ?? null }))
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
|
371
|
+
|
|
372
|
+
// One calendar month, weeks Monday..Sunday: each day has the number of correct answers and whether the goal was met.
|
|
373
|
+
// `offset` moves back (negative) or forward from the current month.
|
|
374
|
+
export function calendarMonth(state, { offset = 0, rules = NO_RULES, now = Date.now() } = {}) {
|
|
375
|
+
const progress = ensureProgress(state, now);
|
|
376
|
+
const today = dayKey(now);
|
|
377
|
+
const [year, month] = today.split("-").map(Number);
|
|
378
|
+
const first = new Date(year, month - 1 + offset, 1, 12);
|
|
379
|
+
const firstKey = dayKey(first);
|
|
380
|
+
const daysInMonth = new Date(first.getFullYear(), first.getMonth() + 1, 0).getDate();
|
|
381
|
+
|
|
382
|
+
const cells = Array.from({ length: daysInMonth }, (_, i) => {
|
|
383
|
+
const key = addDays(firstKey, i);
|
|
384
|
+
const day = progress.days[key];
|
|
385
|
+
return { key, day: i + 1, correct: day?.correct ?? 0, goalMet: Boolean(day?.goalMet), rest: isRestDay(progress, key, rules), future: daysBetween(today, key) > 0, today: key === today };
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
const lead = (first.getDay() + 6) % 7; // blank cells before the 1st so columns line up Mon..Sun
|
|
389
|
+
const padded = [...Array(lead).fill(null), ...cells];
|
|
390
|
+
const weeks = [];
|
|
391
|
+
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)]);
|
|
392
|
+
|
|
393
|
+
return {
|
|
394
|
+
label: `${MONTH_NAMES[first.getMonth()]} ${first.getFullYear()}`,
|
|
395
|
+
weeks,
|
|
396
|
+
goalDays: cells.filter((c) => c.goalMet).length,
|
|
397
|
+
correct: cells.reduce((sum, c) => sum + c.correct, 0),
|
|
398
|
+
daysSoFar: cells.filter((c) => !c.future).length
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// ---- words you got wrong (for /missed) -------------------------------------------------------------------------
|
|
403
|
+
|
|
404
|
+
// miss = { key, label, prompt, expected[], answer, at }; kept per day, and only for the last MISSED_KEEP_DAYS days
|
|
405
|
+
export function recordMiss(state, miss, now = Date.now()) {
|
|
406
|
+
const progress = ensureProgress(state, now);
|
|
407
|
+
const today = dayKey(now);
|
|
408
|
+
(dayOf(progress, today).missed ??= []).push(miss);
|
|
409
|
+
for (const [key, day] of Object.entries(progress.days)) {
|
|
410
|
+
if (day.missed && daysBetween(key, today) > MISSED_KEEP_DAYS) delete day.missed;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// the miss no longer counts (the user declared that answer right)
|
|
415
|
+
export function undoMiss(state, key, at) {
|
|
416
|
+
const day = state.progress?.days[dayKey(at)];
|
|
417
|
+
if (!day?.missed) return;
|
|
418
|
+
const index = day.missed.findLastIndex((m) => m.key === key && m.at === at);
|
|
419
|
+
if (index >= 0) day.missed.splice(index, 1);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Misses of one day (offset 0 = today, -1 = yesterday...), one row per card with how often it was missed.
|
|
423
|
+
// `recovered` = answered correctly since.
|
|
424
|
+
export function missedOn(state, { offset = 0, now = Date.now() } = {}) {
|
|
425
|
+
const progress = ensureProgress(state, now);
|
|
426
|
+
const date = addDays(dayKey(now), offset);
|
|
427
|
+
const rows = new Map();
|
|
428
|
+
for (const miss of progress.days[date]?.missed ?? []) {
|
|
429
|
+
const row = rows.get(miss.key);
|
|
430
|
+
rows.set(miss.key, { ...miss, times: (row?.times ?? 0) + 1 });
|
|
431
|
+
}
|
|
432
|
+
const items = [...rows.values()]
|
|
433
|
+
.map((row) => {
|
|
434
|
+
const entry = state.items[row.key];
|
|
435
|
+
return { ...row, recovered: Boolean(entry && entry.streak > 0 && entry.last > row.at) };
|
|
436
|
+
})
|
|
437
|
+
.sort((a, b) => b.at - a.at);
|
|
438
|
+
return { date, items };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// ---- badges you haven't looked at yet (they light up when /badges opens) ----------------------------------
|
|
442
|
+
|
|
443
|
+
// → ids of badges unlocked but not yet seen in /badges, in the order they were won
|
|
444
|
+
export function unseenBadges(state, now = Date.now()) {
|
|
445
|
+
const progress = ensureProgress(state, now);
|
|
446
|
+
return BADGES.map((badge, index) => ({ id: badge.id, index, on: progress.badges[badge.id] }))
|
|
447
|
+
.filter((b) => b.on && !progress.seenBadges.includes(b.id))
|
|
448
|
+
.sort((a, b) => (a.on === b.on ? a.index - b.index : a.on < b.on ? -1 : 1))
|
|
449
|
+
.map((b) => b.id);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export function markBadgesSeen(state, now = Date.now()) {
|
|
453
|
+
const progress = ensureProgress(state, now);
|
|
454
|
+
progress.seenBadges = Object.keys(progress.badges);
|
|
455
|
+
}
|
package/src/quiet.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const MINUTE = 60_000;
|
|
2
|
+
const HOUR = 60 * MINUTE;
|
|
3
|
+
|
|
4
|
+
// quiet = { from, to } in whole hours (22 → 8 means 22:00 until 08:00, past midnight), or null for no quiet hours
|
|
5
|
+
export function inQuietHours(ms, quiet) {
|
|
6
|
+
if (!quiet || quiet.from === quiet.to) return false;
|
|
7
|
+
const hour = new Date(ms).getHours();
|
|
8
|
+
return quiet.from < quiet.to ? hour >= quiet.from && hour < quiet.to : hour >= quiet.from || hour < quiet.to;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// the next moment quiet hours end after `ms`
|
|
12
|
+
export function quietEnd(ms, quiet) {
|
|
13
|
+
const end = new Date(ms);
|
|
14
|
+
end.setHours(quiet.to, 0, 0, 0);
|
|
15
|
+
if (end.getTime() <= ms) end.setDate(end.getDate() + 1);
|
|
16
|
+
return end.getTime();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const formatQuiet = (quiet) => (quiet ? `${String(quiet.from).padStart(2, "0")}:00–${String(quiet.to).padStart(2, "0")}:00` : "off");
|
|
20
|
+
|
|
21
|
+
// /pause durations: "" → 1 h, "30", "30m", "2h", "90s" → milliseconds, "off" → "off", anything else → null
|
|
22
|
+
export function parseSnooze(arg) {
|
|
23
|
+
const text = arg.trim().toLowerCase();
|
|
24
|
+
if (!text) return HOUR;
|
|
25
|
+
if (text === "off" || text === "0") return "off";
|
|
26
|
+
const match = /^(\d+(?:\.\d+)?)\s*(s|m|h)?$/.exec(text);
|
|
27
|
+
if (!match) return null;
|
|
28
|
+
const amount = Number(match[1]) * { s: 1000, m: MINUTE, h: HOUR }[match[2] ?? "m"];
|
|
29
|
+
return amount > 0 ? amount : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const formatClock = (ms) => new Date(ms).toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit" });
|
package/src/scheduler.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { dayKey } from "./dates.js";
|
|
2
|
+
|
|
3
|
+
const MINUTE = 60_000;
|
|
4
|
+
const DAY = 24 * 60 * MINUTE;
|
|
5
|
+
|
|
6
|
+
// Leitner boxes: a correct answer moves the card one box up; a miss sends it back to box 1
|
|
7
|
+
export const BOX_INTERVALS_DAYS = [1, 3, 7, 14, 30];
|
|
8
|
+
export const RELEARN_DELAY = 10 * MINUTE;
|
|
9
|
+
export const NEW_PER_DAY = 20;
|
|
10
|
+
|
|
11
|
+
export const DIRECTIONS = ["en-pl", "pl-en"];
|
|
12
|
+
export const itemKey = (noteId, direction) => `${noteId}:${direction}`;
|
|
13
|
+
|
|
14
|
+
export function newToday(state, now) {
|
|
15
|
+
return state.newToday?.date === dayKey(now) ? state.newToday.count : 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const NEW_RATIO = 0.3; // when both due and unseen items exist, this share of questions introduces a new one
|
|
19
|
+
const MAX_OVERDUE_WEIGHT_DAYS = 3;
|
|
20
|
+
|
|
21
|
+
const pickRandom = (items, random) => items[Math.floor(random() * items.length)];
|
|
22
|
+
|
|
23
|
+
// Random among due items, but the longer overdue the likelier: weight 1 (just due) up to 4 (3+ days late).
|
|
24
|
+
// Sorting by due date instead would replay every review in the order it was first answered.
|
|
25
|
+
function pickDue(due, now, random) {
|
|
26
|
+
const weights = due.map((c) => 1 + Math.min(Math.max(now - c.entry.due, 0) / DAY, MAX_OVERDUE_WEIGHT_DAYS));
|
|
27
|
+
let roll = random() * weights.reduce((sum, w) => sum + w, 0);
|
|
28
|
+
for (let i = 0; i < due.length; i++) {
|
|
29
|
+
roll -= weights[i];
|
|
30
|
+
if (roll < 0) return due[i];
|
|
31
|
+
}
|
|
32
|
+
return due[due.length - 1];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Mixes due reviews with new words (up to the daily cap), at random; with nothing to do, practises a soon-due item.
|
|
36
|
+
// Never asks the same note twice in a row when there is any alternative. A `tour` deck is different:
|
|
37
|
+
// each card is asked once, in deck order, and never comes back as a review (null when it's done).
|
|
38
|
+
// What to ask next: a due review or a new card (up to the daily cap), mixed at random; null when there
|
|
39
|
+
// is nothing to do right now. Cards are never asked before they're due: answering early would move them
|
|
40
|
+
// up the boxes without the spacing that makes them stick. Never the same note twice in a row while
|
|
41
|
+
// anything else is waiting. A `tour` deck is different: each card is asked once, in deck order, and
|
|
42
|
+
// never comes back as a review.
|
|
43
|
+
export function pickNext({ words, state, now = Date.now(), lastNoteId = null, directions = DIRECTIONS, tour = false, random = Math.random }) {
|
|
44
|
+
const candidates = words.flatMap((word) => directions.map((direction) => ({ word, direction, entry: state.items[itemKey(word.noteId, direction)] })));
|
|
45
|
+
if (tour) return candidates.find((c) => !c.entry) ?? null;
|
|
46
|
+
|
|
47
|
+
const canIntroduce = newToday(state, now) < NEW_PER_DAY;
|
|
48
|
+
const waiting = candidates.filter((c) => (c.entry ? c.entry.due <= now : canIntroduce));
|
|
49
|
+
if (!waiting.length) return null;
|
|
50
|
+
const pool = waiting.some((c) => c.word.noteId !== lastNoteId) ? waiting.filter((c) => c.word.noteId !== lastNoteId) : waiting;
|
|
51
|
+
|
|
52
|
+
const due = pool.filter((c) => c.entry);
|
|
53
|
+
const fresh = pool.filter((c) => !c.entry);
|
|
54
|
+
if (due.length && fresh.length && random() < NEW_RATIO) return pickRandom(fresh, random);
|
|
55
|
+
return due.length ? pickDue(due, now, random) : pickRandom(fresh, random);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// When something will next be waiting, once pickNext has nothing: the earliest due card, or the
|
|
59
|
+
// start of tomorrow when only new cards are left (the daily cap is reached). null = nothing ever.
|
|
60
|
+
export function nextDueAt({ words, state, now = Date.now(), directions = DIRECTIONS }) {
|
|
61
|
+
let next = null;
|
|
62
|
+
for (const word of words) {
|
|
63
|
+
for (const direction of directions) {
|
|
64
|
+
const entry = state.items[itemKey(word.noteId, direction)];
|
|
65
|
+
const at = entry ? entry.due : startOfTomorrow(now);
|
|
66
|
+
if (next === null || at < next) next = at;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return next;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function startOfTomorrow(now) {
|
|
73
|
+
const date = new Date(now);
|
|
74
|
+
date.setHours(24, 0, 0, 0);
|
|
75
|
+
return date.getTime();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function grade(entry, correct, now = Date.now(), { hinted = false } = {}) {
|
|
79
|
+
const prev = entry ?? { box: 0, seen: 0, correct: 0, wrong: 0, streak: 0 };
|
|
80
|
+
if (correct) {
|
|
81
|
+
// right after a miss the card starts over at box 1 (back tomorrow) instead of skipping ahead
|
|
82
|
+
const relearning = prev.seen > 0 && prev.streak === 0;
|
|
83
|
+
const box = hinted || relearning ? Math.max(prev.box, 1) : Math.min(prev.box + 1, BOX_INTERVALS_DAYS.length);
|
|
84
|
+
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 };
|
|
85
|
+
}
|
|
86
|
+
return { ...prev, box: 1, due: now + RELEARN_DELAY, seen: prev.seen + 1, wrong: prev.wrong + 1, streak: 0, last: now };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function describeDue(entry, now = Date.now()) {
|
|
90
|
+
const ms = entry.due - now;
|
|
91
|
+
if (ms < 60 * MINUTE) return `${Math.max(1, Math.round(ms / MINUTE))} min`;
|
|
92
|
+
const hours = Math.round(ms / (60 * MINUTE));
|
|
93
|
+
return hours < 24 ? `${hours} h` : `${Math.round(ms / DAY)} d`;
|
|
94
|
+
}
|