-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbadges.js
More file actions
1136 lines (1012 loc) · 36.8 KB
/
Copy pathbadges.js
File metadata and controls
1136 lines (1012 loc) · 36.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* badges.js — Badge System 2.0 (Rules-based achievement unlocking)
*
* Requirements implemented:
* - Rule schema: { id, title, condition: { type, params }, reward }
* - Achievement evaluator runs via existing call-sites:
* window.achievements.checkAndNotify() (triggered after quiz + mastery updates)
* - State:
* unlockedBadges: []
* badgeProgress: { [badgeId]: {...} }
* - UI:
* locked vs unlocked + progress toward badge
* renderStreakTimeline(containerId) — 30-day heatmap + milestone track
* - Persistence: localStorage key learnsphere_achievements_v1
*/
(function () {
const ACHIEVEMENTS_KEY = "learnsphere_achievements_v1";
const QUIZ_PROGRESS_KEY = "learnsphere_quiz_progress_v1";
// ---------------------------
// 1) Badge rule schema
// ---------------------------
const BADGE_RULES = [
{
id: "perfect_3_quizzes_in_week",
title: "3 perfect quizzes in a week",
icon: "🏆",
reward: { type: "badge" },
condition: {
type: "perfect_quizzes_in_week",
params: { targetPerfectQuizzes: 3, window: "week", perfectAccuracy: 1.0 }
},
description: "Achieve 100% accuracy in 3 quizzes within the same week."
},
{
id: "mastery_gt_80_any_skill",
title: "Mastery > 80% in any skill",
icon: "🎯",
reward: { type: "badge" },
condition: {
type: "mastery_threshold_any_skill",
params: { threshold: 0.8 }
},
description: "Reach at least 80% accuracy in any skill."
},
// ── Streaks ──
{
id: "streak_3_days",
title: "3-Day Streak",
icon: "⚡",
reward: { type: "badge" },
condition: {
type: "streak_threshold",
params: { targetStreakDays: 3 }
},
description: "Practice 3 days in a row.",
category: "streak",
milestoneOrder: 1
},
{
id: "streak_7_days",
title: "Week Warrior",
icon: "🔥",
reward: { type: "badge" },
condition: {
type: "streak_threshold",
params: { targetStreakDays: 7 }
},
description: "Practice every day for 7 consecutive days.",
category: "streak",
milestoneOrder: 2
},
{
id: "streak_14_days",
title: "Fortnight Focus",
icon: "🌟",
reward: { type: "badge" },
condition: {
type: "streak_threshold",
params: { targetStreakDays: 14 }
},
description: "Practice every day for 14 consecutive days.",
category: "streak",
milestoneOrder: 3
},
{
id: "streak_30_days",
title: "Monthly Master",
icon: "💎",
reward: { type: "badge" },
condition: {
type: "streak_threshold",
params: { targetStreakDays: 30 }
},
description: "30 days without missing a single day!",
category: "streak",
milestoneOrder: 4
},
// ── Quiz Milestones (total quiz completions) ──
{
id: "milestone_1_quiz",
title: "First Step",
icon: "🥚",
reward: { type: "badge" },
condition: {
type: "quizzes_completed_threshold",
params: { targetQuizzesCompleted: 1 }
},
description: "Complete your very first quiz.",
category: "milestone",
milestoneOrder: 1
},
{
id: "milestone_10_quizzes",
title: "Quiz Enthusiast",
icon: "🥇",
reward: { type: "badge" },
condition: {
type: "quizzes_completed_threshold",
params: { targetQuizzesCompleted: 10 }
},
description: "Complete 10 quizzes total.",
category: "milestone",
milestoneOrder: 2
},
{
id: "milestone_25_quizzes",
title: "Halfway Hero",
icon: "🏅",
reward: { type: "badge" },
condition: {
type: "quizzes_completed_threshold",
params: { targetQuizzesCompleted: 25 }
},
description: "Complete 25 quizzes total.",
category: "milestone",
milestoneOrder: 3
},
{
id: "milestone_50_quizzes",
title: "Dedicated Learner",
icon: "🎖️",
reward: { type: "badge" },
condition: {
type: "quizzes_completed_threshold",
params: { targetQuizzesCompleted: 50 }
},
description: "Complete 50 quizzes — serious commitment!",
category: "milestone",
milestoneOrder: 4
},
{
id: "milestone_100_quizzes",
title: "Century Scholar",
icon: "👑",
reward: { type: "badge" },
condition: {
type: "quizzes_completed_threshold",
params: { targetQuizzesCompleted: 100 }
},
description: "Complete 100 quizzes. You're a legend!",
category: "milestone",
milestoneOrder: 5
},
// ── Accuracy Improvement ──
{
id: "improve_10pct_recent",
title: "Accuracy Improvement",
icon: "📈",
reward: { type: "badge" },
condition: {
type: "accuracy_improvement",
params: {
recentDays: 3,
previousDays: 3,
improvementThreshold: 0.10,
minRecentAttempts: 2
}
},
description: "Improve your accuracy by 10% compared to the previous 3 days."
}
];
// ---------------------------
// 2) Persistence
// ---------------------------
function loadAchievements() {
try {
const raw = localStorage.getItem(ACHIEVEMENTS_KEY);
if (!raw) {
return { unlockedBadges: [], badgeProgress: {} };
}
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== "object") {
return { unlockedBadges: [], badgeProgress: {} };
}
if (!Array.isArray(parsed.unlockedBadges)) parsed.unlockedBadges = [];
if (!parsed.badgeProgress || typeof parsed.badgeProgress !== "object") parsed.badgeProgress = {};
return parsed;
} catch {
return { unlockedBadges: [], badgeProgress: {} };
}
}
function saveAchievements(data) {
try {
localStorage.setItem(ACHIEVEMENTS_KEY, JSON.stringify(data));
} catch (e) {
console.warn("LearnSphere: Could not save achievements.", e);
}
}
// ---------------------------
// 3) Date helpers
// ---------------------------
function safeDateKey(d) {
const dt = new Date(d);
if (Number.isNaN(dt.getTime())) return null;
return dt.toISOString();
}
function getWeekNumber(date) {
const d = new Date(date);
d.setHours(0, 0, 0, 0);
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
const yearStart = new Date(d.getFullYear(), 0, 1);
const weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
return `${d.getFullYear()}-W${weekNo}`;
}
function localISODate(d) {
const dd = new Date(d);
const yyyy = dd.getFullYear();
const mm = String(dd.getMonth() + 1).padStart(2, "0");
const day = String(dd.getDate()).padStart(2, "0");
return `${yyyy}-${mm}-${day}`;
}
// ---------------------------
// 4) Stats extraction
// ---------------------------
function getQuizAttemptsRaw() {
try {
const raw = localStorage.getItem(QUIZ_PROGRESS_KEY);
if (!raw) return [];
const parsed = JSON.parse(raw);
return Array.isArray(parsed.attempts) ? parsed.attempts : [];
} catch {
return [];
}
}
function getTotalQuizzesCompleted() {
return getQuizAttemptsRaw().length;
}
function getPerfectAttemptsInWindow({ windowType = "week", perfectAccuracy = 1.0 } = {}) {
const attempts = getQuizAttemptsRaw();
if (!attempts.length) {
return { perfectCount: 0, totalConsidered: 0, perfectAttemptDates: [] };
}
const now = new Date();
const currentWindowKey = windowType === "week" ? getWeekNumber(now) : null;
let perfectCount = 0;
let totalConsidered = 0;
const perfectAttemptDates = [];
for (const a of attempts) {
if (!a || !a.finishedAt) continue;
if (typeof a.accuracy !== "number" || Number.isNaN(a.accuracy)) continue;
if (windowType === "week") {
if (getWeekNumber(a.finishedAt) !== currentWindowKey) continue;
}
totalConsidered++;
const isPerfect = a.accuracy >= perfectAccuracy;
if (isPerfect) {
perfectCount++;
perfectAttemptDates.push(a.finishedAt);
}
}
return { perfectCount, totalConsidered, perfectAttemptDates };
}
function getAnySkillMasteryAccuracy() {
if (!window.quizProgress || typeof window.quizProgress.getMasteryStats !== "function") {
return { bestAccuracy: null, bestSkillId: null };
}
const mastery = window.quizProgress.getMasteryStats();
if (!mastery || typeof mastery !== "object") {
return { bestAccuracy: null, bestSkillId: null };
}
let bestAccuracy = null;
let bestSkillId = null;
for (const [skillId, m] of Object.entries(mastery)) {
const attempts = m?.attempts || 0;
const correct = m?.correct || 0;
if (!attempts || attempts <= 0) continue;
const acc = correct / attempts;
if (typeof acc === "number" && !Number.isNaN(acc)) {
if (bestAccuracy == null || acc > bestAccuracy) {
bestAccuracy = acc;
bestSkillId = skillId;
}
}
}
return { bestAccuracy, bestSkillId };
}
function getCurrentStreakDerived() {
try {
if (window.studyProgress && typeof window.studyProgress.loadStreakState === "function") {
const s = window.studyProgress.loadStreakState();
const current = Number(s?.currentStreak);
return {
currentStreakDays: Number.isFinite(current) ? current : 0,
lastActiveDate: s?.lastActiveDate || null,
longestStreak: Number(s?.longestStreak) || 0
};
}
} catch {}
try {
const st = window.quizProgress?.getStreak?.();
const current = Number(st?.currentStreak);
return {
currentStreakDays: Number.isFinite(current) ? current : 0,
lastActiveDate: st?.lastPracticeDate || null,
longestStreak: Number(st?.longestStreak) || 0
};
} catch {}
return { currentStreakDays: 0, lastActiveDate: null, longestStreak: 0 };
}
function getAccuracyImprovementDerived({ recentDays = 3, previousDays = 3, minRecentAttempts = 2 } = {}) {
const attempts = getQuizAttemptsRaw();
if (!attempts.length) {
return {
recentAvg: null,
previousAvg: null,
improvement: null,
recentAttemptsCount: 0,
previousAttemptsCount: 0,
};
}
const now = new Date();
function parseISOToToken(isoDateYYYYMMDD) {
if (!isoDateYYYYMMDD || typeof isoDateYYYYMMDD !== "string") return null;
const [y, m, d] = isoDateYYYYMMDD.split("-").map(Number);
if (!y || !m || !d) return null;
const dt = new Date(y, m - 1, d, 0, 0, 0, 0);
return Math.floor(dt.getTime() / 86400000);
}
const todayToken = parseISOToToken(localISODate(now));
if (todayToken == null) {
return {
recentAvg: null,
previousAvg: null,
improvement: null,
recentAttemptsCount: 0,
previousAttemptsCount: 0,
};
}
const recentStart = todayToken - (recentDays - 1);
const previousStart = todayToken - (recentDays + previousDays - 1);
const previousEnd = todayToken - recentDays;
let recentCorrect = 0;
let recentTotal = 0;
let recentAttemptsCount = 0;
let prevCorrect = 0;
let prevTotal = 0;
let previousAttemptsCount = 0;
for (const a of attempts) {
if (!a || !a.practiceDate) continue;
const token = parseISOToToken(a.practiceDate);
if (token == null) continue;
const acc = typeof a.accuracy === "number" && !Number.isNaN(a.accuracy) ? a.accuracy : null;
const totalQ = typeof a.totalQuestions === "number" && !Number.isNaN(a.totalQuestions) ? a.totalQuestions : null;
if (acc == null || totalQ == null || totalQ <= 0) continue;
const correctFromCount = acc * totalQ;
if (token >= recentStart && token <= todayToken) {
recentCorrect += correctFromCount;
recentTotal += totalQ;
recentAttemptsCount += 1;
} else if (token >= previousStart && token <= previousEnd) {
prevCorrect += correctFromCount;
prevTotal += totalQ;
previousAttemptsCount += 1;
}
}
const recentAvg = recentTotal > 0 ? recentCorrect / recentTotal : null;
const previousAvg = prevTotal > 0 ? prevCorrect / prevTotal : null;
let improvement = null;
if (recentAvg != null && previousAvg != null) {
improvement = (recentAvg - previousAvg);
}
return {
recentAvg,
previousAvg,
improvement,
recentAttemptsCount,
previousAttemptsCount,
metMinRecentAttempts: recentAttemptsCount >= (minRecentAttempts || 0)
};
}
// ---------------------------
// 5) Rule evaluator + progress
// ---------------------------
function evaluateBadge(rule, derived) {
const { type, params } = rule.condition || {};
if (type === "perfect_quizzes_in_week") {
const target = Number(params?.targetPerfectQuizzes) || 0;
const perfectAccuracy = typeof params?.perfectAccuracy === "number" ? params.perfectAccuracy : 1.0;
const { perfectCount } = derived.perfectAttemptsInWindow;
const pct = target > 0 ? Math.min(1, perfectCount / target) : 0;
return {
unlocked: perfectCount >= target && target > 0,
progress: {
kind: "count",
current: perfectCount,
target,
percent: pct
},
progressText: target > 0 ? `${Math.min(perfectCount, target)}/${target}` : "—"
};
}
if (type === "mastery_threshold_any_skill") {
const threshold = typeof params?.threshold === "number" ? params.threshold : 0.8;
const bestAccuracy = derived.anySkillBestAccuracy.bestAccuracy;
const bestSkillId = derived.anySkillBestAccuracy.bestSkillId;
const safeBest = typeof bestAccuracy === "number" && !Number.isNaN(bestAccuracy) ? bestAccuracy : 0;
const pct = Math.min(1, safeBest / threshold);
const unlocked = typeof bestAccuracy === "number" && bestAccuracy >= threshold && safeBest > 0;
return {
unlocked,
progress: {
kind: "ratio",
current: safeBest,
target: threshold,
percent: pct,
bestSkillId
},
progressText: typeof bestAccuracy === "number" && !Number.isNaN(bestAccuracy)
? `${Math.round(bestAccuracy * 100)}% / ${Math.round(threshold * 100)}%`
: `0% / ${Math.round(threshold * 100)}%`
};
}
if (type === "streak_threshold") {
const targetDays = Number(params?.targetStreakDays) || 0;
const currentDays = derived.currentStreak?.currentStreakDays ?? 0;
const pct = targetDays > 0 ? Math.min(1, currentDays / targetDays) : 0;
return {
unlocked: targetDays > 0 && currentDays >= targetDays,
progress: {
kind: "count",
current: currentDays,
target: targetDays,
percent: pct
},
progressText: targetDays > 0 ? `${Math.min(currentDays, targetDays)}/${targetDays} days` : "—"
};
}
if (type === "quizzes_completed_threshold") {
const target = Number(params?.targetQuizzesCompleted) || 0;
const current = derived.totalQuizzesCompleted ?? 0;
const pct = target > 0 ? Math.min(1, current / target) : 0;
return {
unlocked: target > 0 && current >= target,
progress: {
kind: "count",
current,
target,
percent: pct
},
progressText: target > 0 ? `${Math.min(current, target)}/${target} quizzes` : "—"
};
}
if (type === "accuracy_improvement") {
const recentDays = Number(params?.recentDays) || 3;
const previousDays = Number(params?.previousDays) || 3;
const thresholdDelta = typeof params?.improvementThreshold === "number" ? params.improvementThreshold : 0.1;
const minRecentAttempts = Number(params?.minRecentAttempts) || 0;
const imp = derived.accuracyImprovement;
const recentAvg = imp?.recentAvg;
const previousAvg = imp?.previousAvg;
const improvement = imp?.improvement;
const canScore =
recentAvg != null &&
previousAvg != null &&
imp?.metMinRecentAttempts;
let pct = 0;
if (canScore) {
pct = thresholdDelta > 0 ? Math.max(0, Math.min(1, improvement / thresholdDelta)) : 0;
}
const deltaMet = canScore && improvement >= thresholdDelta;
const currentDeltaPctText = canScore
? `${Math.round(improvement * 100)}% improvement`
: "Not enough recent data";
return {
unlocked: !!deltaMet,
progress: {
kind: "delta",
current: canScore ? improvement : 0,
target: thresholdDelta,
percent: pct,
recentDays,
previousDays
},
progressText: canScore
? `${Math.round(Math.max(0, improvement) * 100)}% / ${Math.round(thresholdDelta * 100)}%`
: currentDeltaPctText
};
}
return {
unlocked: false,
progress: { kind: "unknown", current: 0, target: 0, percent: 0 },
progressText: "—"
};
}
function buildDerived() {
return {
perfectAttemptsInWindow: getPerfectAttemptsInWindow({
windowType: "week",
perfectAccuracy: 1.0
}),
anySkillBestAccuracy: getAnySkillMasteryAccuracy(),
currentStreak: getCurrentStreakDerived(),
totalQuizzesCompleted: getTotalQuizzesCompleted(),
accuracyImprovement: getAccuracyImprovementDerived({
recentDays: 3,
previousDays: 3,
minRecentAttempts: 2
})
};
}
function getUnlockedSet(ach) {
const set = new Set(Array.isArray(ach.unlockedBadges) ? ach.unlockedBadges : []);
return set;
}
function unlockBadgeIfNeeded(ach, rule, evaluation) {
const unlockedSet = getUnlockedSet(ach);
if (unlockedSet.has(rule.id)) {
return { changed: false, unlockedNow: false };
}
if (evaluation.unlocked) {
ach.unlockedBadges.push(rule.id);
if (!ach.badgeProgress) ach.badgeProgress = {};
ach.badgeProgress[rule.id] = {
...evaluation.progress,
unlockedAt: new Date().toISOString(),
progressText: evaluation.progressText
};
return { changed: true, unlockedNow: true };
}
// Not unlocked: still update progress cache
if (!ach.badgeProgress) ach.badgeProgress = {};
ach.badgeProgress[rule.id] = {
...evaluation.progress,
unlockedAt: ach.badgeProgress?.[rule.id]?.unlockedAt || null,
progressText: evaluation.progressText
};
return { changed: true, unlockedNow: false };
}
// ---------------------------
// 6) Toast
// ---------------------------
function ensureToastStyles() {
if (document.getElementById("badge-toast-styles")) return;
const style = document.createElement("style");
style.id = "badge-toast-styles";
style.textContent = `
.badge-toast-container {
position: fixed;
bottom: 24px;
right: 24px;
z-index: 10000;
display: flex;
flex-direction: column;
gap: 12px;
pointer-events: none;
}
.badge-toast {
background: rgba(15, 23, 42, 0.85);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid rgba(102, 252, 241, 0.35);
border-radius: 12px;
padding: 14px 18px;
width: 320px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5), 0 0 15px rgba(102, 252, 241, 0.2);
display: flex;
gap: 14px;
align-items: center;
pointer-events: auto;
animation: toast-slide-in 0.4s cubic-bezier(0.16, 1, 0.3, 1) forwards;
transition: opacity 0.3s ease, transform 0.3s ease;
}
.badge-toast.fade-out { animation: toast-fade-out 0.3s ease forwards; }
.badge-toast-icon {
font-size: 28px;
background: rgba(102, 252, 241, 0.1);
border-radius: 50%;
width: 48px;
height: 48px;
display: flex;
align-items: center;
justify-content: center;
border: 1px solid rgba(102, 252, 241, 0.25);
flex-shrink: 0;
}
.badge-toast-title {
color: #66fcf1;
font-weight: 800;
font-size: 0.95rem;
margin: 0 0 4px 0;
text-shadow: 0 0 8px rgba(102, 252, 241, 0.4);
}
.badge-toast-desc {
color: #cbd5e1;
font-size: 0.8rem;
margin: 0;
line-height: 1.3;
}
@keyframes toast-slide-in {
from { opacity: 0; transform: translateY(20px) scale(0.95); }
to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes toast-fade-out {
from { opacity: 1; transform: translateY(0); }
to { opacity: 0; transform: translateY(-20px) scale(0.95); }
}
`;
document.head.appendChild(style);
}
function showUnlockToast(rule) {
ensureToastStyles();
let container = document.getElementById("badge-toast-container");
if (!container) {
container = document.createElement("div");
container.id = "badge-toast-container";
container.className = "badge-toast-container";
document.body.appendChild(container);
}
const toast = document.createElement("div");
toast.className = "badge-toast";
toast.setAttribute("role", "alert");
toast.innerHTML = `
<div class="badge-toast-icon">${rule.icon || "🏅"}</div>
<div class="badge-toast-details">
<h4 class="badge-toast-title">Achievement Unlocked!</h4>
<p class="badge-toast-desc"><strong>${rule.title}</strong>: ${rule.description || ""}</p>
</div>
`;
container.appendChild(toast);
setTimeout(() => {
toast.classList.add("fade-out");
toast.addEventListener("animationend", () => {
toast.remove();
if (container.children.length === 0) container.remove();
});
}, 4500);
}
// ---------------------------
// 7) Public evaluator
// ---------------------------
function checkAndNotify() {
const derived = buildDerived();
const ach = loadAchievements();
let changed = false;
const newlyUnlocked = [];
for (const rule of BADGE_RULES) {
const evaluation = evaluateBadge(rule, derived);
const { changed: c, unlockedNow } = unlockBadgeIfNeeded(ach, rule, evaluation);
if (c) changed = true;
if (unlockedNow) newlyUnlocked.push(rule);
}
if (changed) saveAchievements(ach);
newlyUnlocked.forEach(rule => showUnlockToast(rule));
return newlyUnlocked;
}
// ---------------------------
// 8) UI rendering — badge cards
// ---------------------------
function ensureStyles(containerEl) {
if (!containerEl) return;
if (containerEl.dataset.badgesStylesApplied === "true") return;
containerEl.dataset.badgesStylesApplied = "true";
const style = document.createElement("style");
style.textContent = `
.badges-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
margin-top: 10px;
}
@media (max-width: 560px) {
.badges-grid { grid-template-columns: 1fr; }
}
.badge-card {
background: rgba(255,255,255,0.04);
border: 1px solid rgba(255,255,255,0.12);
border-radius: 12px;
padding: 12px;
transition: transform 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}
.badge-card:hover { transform: translateY(-2px); }
.badge-top {
display:flex;
gap:10px;
align-items:flex-start;
}
.badge-icon { width:26px; text-align:center; font-size:20px; }
.badge-title { font-weight:800; }
.badge-desc { font-size:12px; opacity:0.85; margin-top:2px; }
.badge-bottom {
margin-top:10px;
font-size:12px;
opacity:0.9;
display:flex;
flex-direction:column;
gap:8px;
}
.badge-progressbar {
width:100%;
height:8px;
background: rgba(255,255,255,0.08);
border-radius: 999px;
overflow:hidden;
}
.badge-progressbar > i {
display:block;
height:100%;
width:0%;
background: #66fcf1;
border-radius: 999px;
}
`;
document.head.appendChild(style);
}
function badgeCardHTML(rule, unlocked, progressEntry) {
const dim = unlocked ? "" : "opacity:0.55; filter: grayscale(0.3);";
const border = unlocked ? "border-color: rgba(102,252,241,0.55);" : "border-color: rgba(255,255,255,0.12);";
const shadow = unlocked ? "0 10px 26px rgba(102,252,241,0.16)" : "none";
const progressText = progressEntry?.progressText || "";
const percent = typeof progressEntry?.percent === "number" ? Math.max(0, Math.min(1, progressEntry.percent)) : 0;
const barW = Math.round(percent * 100);
return `
<div class="badge-card" style="${dim} ${border} box-shadow:${shadow}">
<div class="badge-top">
<div class="badge-icon" aria-hidden="true">${rule.icon || "🏅"}</div>
<div style="min-width:0">
<div class="badge-title" style="font-weight:800">${rule.title}</div>
<div class="badge-desc">${rule.description || ""}</div>
</div>
</div>
<div class="badge-bottom">
${unlocked
? `<span style="color:#66fcf1; font-weight:700">Unlocked ✓</span>`
: `<span>Locked • ${progressText || "Progress"}</span>`}
<div class="badge-progressbar" aria-hidden="true">
<i style="width:${barW}%; background:${unlocked ? "#66fcf1" : "#66fcf1"}"></i>
</div>
</div>
</div>
`;
}
function getTopUnlocked(ach, limit = 3) {
const unlockedSet = new Set(ach.unlockedBadges || []);
const progress = ach.badgeProgress || {};
const unlockedRules = BADGE_RULES.filter(r => unlockedSet.has(r.id));
unlockedRules.sort((a, b) => {
const atA = progress?.[a.id]?.unlockedAt ? new Date(progress[a.id].unlockedAt).getTime() : 0;
const atB = progress?.[b.id]?.unlockedAt ? new Date(progress[b.id].unlockedAt).getTime() : 0;
return atB - atA;
});
return unlockedRules.slice(0, limit);
}
function renderBadges(containerId) {
const container = document.getElementById(containerId);
if (!container) return;
ensureStyles(container);
// Re-evaluate
checkAndNotify();
const ach = loadAchievements();
const unlockedSet = getUnlockedSet(ach);
const progress = ach.badgeProgress || {};
container.innerHTML = `
<div class="badges-grid" role="list" aria-label="Achievements and badges">
${BADGE_RULES.map(rule => {
const unlocked = unlockedSet.has(rule.id);
const entry = progress[rule.id] || {};
return `<div role="listitem">${badgeCardHTML(rule, unlocked, entry)}</div>`;
}).join("")}
</div>
<div style="margin-top:10px; font-size:12px; opacity:0.8">
Achievements are based on your quiz accuracy and mastery progress.
</div>
`;
}
function renderTopUnlocked(containerId) {
const container = document.getElementById(containerId);
if (!container) return;
const ach = loadAchievements();
const unlocked = getTopUnlocked(ach, 3);
if (!unlocked.length) {
container.innerHTML = `<div class="muted" style="font-size:13px; opacity:0.8">No achievements unlocked yet.</div>`;
return;
}
container.innerHTML = `
<div style="display:flex; gap:10px; flex-wrap:wrap;">
${unlocked.map(r => `
<div style="display:flex; align-items:center; gap:8px; padding:8px 10px; border-radius:999px;
background:rgba(102,252,241,0.10); border:1px solid rgba(102,252,241,0.35);">
<span aria-hidden="true">${r.icon || "🏅"}</span>
<span style="font-weight:800; color:#66fcf1; font-size:13px;">${r.title}</span>
</div>
`).join("")}
</div>
`;
}
// ---------------------------
// 9) Streak history (for calendar heatmap)
// ---------------------------
/**
* Returns an array of the last `days` local date strings (YYYY-MM-DD),
* each annotated with { date, active: bool, isToday: bool }.
* "active" = there was at least one quiz attempt on that day.
*/
function getStreakHistory(days = 30) {
const attempts = getQuizAttemptsRaw();
// Build a Set of active dates from attempt records
const activeDates = new Set();
for (const a of attempts) {
// practiceDate is the canonical YYYY-MM-DD field
if (a?.practiceDate && typeof a.practiceDate === "string") {
activeDates.add(a.practiceDate);
}
// Fallback: derive from finishedAt ISO timestamp
if (a?.finishedAt && !a.practiceDate) {
const d = localISODate(a.finishedAt);
if (d) activeDates.add(d);
}
}
const today = localISODate(new Date());
const result = [];
for (let i = days - 1; i >= 0; i--) {
const d = new Date();
d.setDate(d.getDate() - i);
const dateStr = localISODate(d);
result.push({
date: dateStr,
active: activeDates.has(dateStr),
isToday: dateStr === today
});
}
return result;
}
// ---------------------------
// 10) Streak Timeline Renderer
// ---------------------------
/**
* Renders the full Streaks & Milestones timeline card into the given container.
* Includes:
* - 30-day activity calendar heatmap
* - Horizontal milestone track for quiz completions
* - Streak badge nodes (3/7/14/30 days)
*/
function renderStreakTimeline(containerId) {
const container = document.getElementById(containerId);
if (!container) return;
// Run evaluations to refresh state
const derived = buildDerived();
const ach = loadAchievements();
const unlockedSet = getUnlockedSet(ach);
const progress = ach.badgeProgress || {};
const { currentStreakDays, longestStreak } = derived.currentStreak;
const totalQuizzes = derived.totalQuizzesCompleted;
// 30-day history
const history = getStreakHistory(30);
// Streak milestone nodes
const streakNodes = BADGE_RULES.filter(r => r.category === "streak")
.sort((a, b) => (a.milestoneOrder || 0) - (b.milestoneOrder || 0));
// Quiz milestone nodes
const quizNodes = BADGE_RULES.filter(r => r.category === "milestone")
.sort((a, b) => (a.milestoneOrder || 0) - (b.milestoneOrder || 0));
// Build calendar HTML (7-column grid, Mon–Sun)
const dayLetters = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
// Find the weekday of the first cell and prepend empties
const firstDate = history[0]?.date;
const firstDay = firstDate ? new Date(firstDate + "T00:00:00").getDay() : 0;
const prefixCells = Array.from({ length: firstDay }, () =>
`<div class="streak-day-box streak-day-empty" aria-hidden="true"></div>`
).join("");
const dayCells = history.map(({ date, active, isToday }) => {
let cls = "streak-day-box";
let label = "";
let title = date;
if (isToday) { cls += " streak-day-today"; label = "★"; }
else if (active) cls += " streak-day-active";
else cls += " streak-day-inactive";