-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressManager.js
More file actions
225 lines (192 loc) · 6.71 KB
/
progressManager.js
File metadata and controls
225 lines (192 loc) · 6.71 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
// progressManager.js - Manages XP, Levels, and Achievements
export const LEVEL_DATA = [
{ level: 1, minXP: 0, name: "The Egg", icon: "🥚" },
{ level: 2, minXP: 100, name: "The Hatchling", icon: "🐣" },
{ level: 3, minXP: 300, name: "The Fledgling", icon: "🐥" },
{ level: 4, minXP: 600, name: "The Scout", icon: "🐦" },
{ level: 5, minXP: 1000, name: "The Majestic Umusambi", icon: "🦩" }
];
export const ACHIEVEMENTS = {
EARLY_BIRD: { id: "early_bird", name: "Early Bird", desc: "Earned your first XP!", icon: "🌅" },
BRAINIAC: { id: "brainiac", name: "Brainiac", desc: "Answered 10 questions correctly in a row!", icon: "🧠" },
EXPLORER: { id: "explorer", name: "Explorer", desc: "Viewed 20 unique flashcards!", icon: "🗺️" },
POLYGLOT: { id: "polyglot", name: "Polyglot", desc: "Tried all three languages!", icon: "🌍" },
DAILY_HERO: { id: "daily_hero", name: "Daily Hero", desc: "Found the Word of the Day!", icon: "✨" }
};
class ProgressManager {
constructor() {
this.storageKey = 'ejo_progress';
this.data = this.loadData();
this.updateStreak();
}
loadData() {
const defaults = {
xp: 0,
level: 1,
coins: 0,
achievements: [],
streak: 0,
languagesTried: [],
viewedFlashcardIds: [],
masteredFlashcardIds: [],
unlockedThemes: ['default'],
activeTheme: 'default',
lastDailyDiscovery: null,
lastVisitDate: null
};
try {
const stored = localStorage.getItem(this.storageKey);
return stored ? { ...defaults, ...JSON.parse(stored) } : defaults;
} catch (e) {
return defaults;
}
}
saveData() {
localStorage.setItem(this.storageKey, JSON.stringify(this.data));
// Dispatch custom event for UI updates
window.dispatchEvent(new CustomEvent('ejoProgressUpdated', { detail: this.data }));
}
updateStreak() {
const today = new Date();
const todayStr = today.toDateString();
const lastVisitStr = this.data.lastVisitDate;
if (lastVisitStr === todayStr) {
return; // Already visited today
}
let changed = false;
if (!lastVisitStr) {
this.data.streak = 1;
changed = true;
} else {
const lastVisit = new Date(lastVisitStr);
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
if (lastVisit.toDateString() === yesterday.toDateString()) {
this.data.streak += 1;
changed = true;
} else {
// More than a day gap
this.data.streak = 1;
changed = true;
}
}
this.data.lastVisitDate = todayStr;
this.saveData();
}
addXP(amount) {
if (amount <= 0) return;
const oldXP = this.data.xp;
this.data.xp += amount;
if (oldXP === 0 && amount > 0) {
this.unlockAchievement(ACHIEVEMENTS.EARLY_BIRD);
}
this.checkLevelUp();
this.saveData();
}
checkLevelUp() {
const currentLevel = this.data.level;
let newLevel = currentLevel;
for (const ld of LEVEL_DATA) {
if (this.data.xp >= ld.minXP) {
newLevel = ld.level;
}
}
if (newLevel > currentLevel) {
this.data.level = newLevel;
window.dispatchEvent(new CustomEvent('ejoLevelUp', { detail: { level: newLevel, levelData: LEVEL_DATA[newLevel - 1] } }));
}
}
addCoins(amount) {
if (amount <= 0) return;
this.data.coins += amount;
this.saveData();
}
unlockAchievement(achievement) {
if (!this.data.achievements.includes(achievement.id)) {
this.data.achievements.push(achievement.id);
window.dispatchEvent(new CustomEvent('ejoAchievementUnlocked', { detail: achievement }));
this.saveData();
}
}
toggleFlashcardMastery(id) {
const index = this.data.masteredFlashcardIds.indexOf(id);
if (index === -1) {
this.data.masteredFlashcardIds.push(id);
this.addXP(10); // Reward for mastering
} else {
this.data.masteredFlashcardIds.splice(index, 1);
}
this.saveData();
return this.data.masteredFlashcardIds.includes(id);
}
unlockTheme(themeId, cost) {
if (this.data.coins >= cost && !this.data.unlockedThemes.includes(themeId)) {
this.data.coins -= cost;
this.data.unlockedThemes.push(themeId);
this.saveData();
return true;
}
return false;
}
setActiveTheme(themeId) {
if (this.data.unlockedThemes.includes(themeId)) {
this.data.activeTheme = themeId;
this.saveData();
return true;
}
return false;
}
recordLanguageTry(lang) {
if (!this.data.languagesTried.includes(lang)) {
this.data.languagesTried.push(lang);
if (this.data.languagesTried.length >= 3) {
this.unlockAchievement(ACHIEVEMENTS.POLYGLOT);
}
this.saveData();
}
}
recordFlashcardView(id) {
if (!this.data.viewedFlashcardIds.includes(id)) {
this.data.viewedFlashcardIds.push(id);
this.addXP(5);
if (this.data.viewedFlashcardIds.length >= 20) {
this.unlockAchievement(ACHIEVEMENTS.EXPLORER);
}
this.saveData();
}
}
recordQuizResult(correct, total, streak) {
if (streak >= 10) {
this.unlockAchievement(ACHIEVEMENTS.BRAINIAC);
}
// XP for completion
this.addXP(20);
// Bonus XP for accuracy
const accuracyBonus = Math.floor((correct / total) * 30);
this.addXP(accuracyBonus);
this.saveData();
}
recordDailyDiscovery() {
const today = new Date().toDateString();
if (this.data.lastDailyDiscovery !== today) {
this.data.lastDailyDiscovery = today;
this.addXP(50);
this.unlockAchievement(ACHIEVEMENTS.DAILY_HERO);
this.saveData();
return true;
}
return false;
}
getProgressData() {
return { ...this.data };
}
getCurrentLevelData() {
return LEVEL_DATA[this.data.level - 1];
}
getNextLevelXP() {
const nextLevel = LEVEL_DATA[this.data.level];
return nextLevel ? nextLevel.minXP : null;
}
}
const progressManager = new ProgressManager();
export default progressManager;