-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.js
More file actions
395 lines (334 loc) · 13.6 KB
/
Copy pathquiz.js
File metadata and controls
395 lines (334 loc) · 13.6 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
let quizMode = false;
let quizData = {
questions: [],
currentIndex: 0,
score: 0,
total: 5,
processing: false
};
function setupQuizButton() {
const quizBtn = document.getElementById('quizBtn');
if (quizBtn) {
quizBtn.addEventListener('click', () => {
toggleQuizMode();
});
}
}
function toggleQuizMode() {
// Deprecated by Tab System
// Keeping logic for reuse if needed
}
function renderQuizMenu() {
const container = document.getElementById('quizContainer');
container.innerHTML = '<p>Loading...</p>';
chrome.storage.local.get(['wordDex'], (data) => {
const wordDex = data.wordDex || {};
// Filter out GOD rarity words for quiz
const filteredWords = Object.keys(wordDex).filter(word => wordDex[word].rarity !== 'god');
const totalWords = filteredWords.length;
if (totalWords < 3) {
container.innerHTML = `<p style="padding:20px;text-align:center;">${t('quizMinWords')}</p>`;
return;
}
container.innerHTML = `
<div class="quiz-menu" style="text-align:center; padding:10px;">
<h3 style="margin-bottom:20px;">${t('quizMenuTitle')}</h3>
<div style="margin-bottom: 24px; text-align:left; background:#f9f9f9; padding:16px; border-radius:8px; border:1px solid #eee;">
<label class="quiz-setting-label" style="font-weight:bold; font-size:12px; display:block; margin-bottom:8px;">${t('quizNumQuestions')}</label>
<div style="display:flex; align-items:center; gap:12px;">
<input type="range" id="quizSize" min="3" max="${Math.min(20, totalWords)}" value="5" style="flex:1">
<span id="quizSizeVal" class="quiz-setting-label" style="font-weight:bold; width:24px;">5</span>
</div>
</div>
<button id="btnStartCustomQuiz" class="quiz-start-btn" style="font-size:16px; margin-bottom:12px;">${t('quizStartBtn')}</button>
<button id="btnStartTraining" class="quiz-start-btn" style="font-size:16px; background: #333; color: white; border: 1px solid #666;">${t('dailyTraining')}</button>
</div>
`;
const range = document.getElementById('quizSize');
// Check if training is already done today
chrome.storage.local.get(['lastTrainingDate'], (res) => {
const lastDate = res.lastTrainingDate;
const today = new Date().toDateString();
const trainingBtn = document.getElementById('btnStartTraining');
if (lastDate === today) {
trainingBtn.disabled = true;
trainingBtn.textContent = `${t('dailyTraining')} (Done)`;
trainingBtn.style.opacity = '0.5';
trainingBtn.style.cursor = 'not-allowed';
trainingBtn.style.background = '#ccc';
trainingBtn.style.color = '#666';
}
});
// Validate range values
const maxVal = Math.min(20, totalWords);
range.max = maxVal;
range.value = Math.min(5, maxVal); // Ensure initial value is within bounds
// Update display immediately
const valDisplay = document.getElementById('quizSizeVal');
valDisplay.textContent = range.value;
range.oninput = () => valDisplay.textContent = range.value;
document.getElementById('btnStartCustomQuiz').onclick = () => {
startQuiz({ size: parseInt(range.value), mode: 'random' });
};
document.getElementById('btnStartTraining').onclick = () => {
startQuiz({ size: 10, mode: 'training' });
};
});
}
function startQuiz(options = { size: 5, mode: 'random' }) {
const container = document.getElementById('quizContainer');
// If we are already in the middle of a quiz (and not just starting from menu), don't reset unless force
// Store mode for later use in results
quizData.mode = options.mode;
chrome.storage.local.get(['wordDex'], (data) => {
const wordDex = data.wordDex || {};
// Exclude GOD rarity words
const words = Object.entries(wordDex).filter(([word, info]) => info.rarity !== 'god');
let selectedWords = [];
if (options.mode === 'training') {
// SRS Filter: Due for review
const now = Date.now();
const dueWords = words.filter(([word, info]) => {
if (!info.srs) return true; // Treat new/migrated words as due
return now >= info.srs.nextReview;
});
if (dueWords.length === 0) {
// Smart Fallback: Review words that are coming up soon, or lowest SRS level
// For now, simple fallback to random but show toast?
// Actually, let's grab words with lowest SRS levels
selectedWords = words.sort((a, b) => {
const srsA = a[1].srs ? a[1].srs.level : 0;
const srsB = b[1].srs ? b[1].srs.level : 0;
return srsA - srsB;
}).slice(0, 10);
// alert("No words strictly due! Reviewing your weakest words."); // UX improvement needed later
} else {
selectedWords = dueWords.sort((a,b) => {
// Sort by how overdue they are (descending)
return (b[1].srs ? b[1].srs.nextReview : 0) - (a[1].srs ? a[1].srs.nextReview : 0);
}).slice(0, Math.min(10, dueWords.length));
}
} else {
// Select random words
const shuffled = words.sort(() => 0.5 - Math.random());
const quizSize = options.size || 5;
selectedWords = shuffled.slice(0, Math.min(quizSize, words.length));
}
quizData.questions = selectedWords.map(([word, info]) => {
// Default Question: Definition -> Word
let type = 'definition';
let questionText = info.origin || 'No definition available';
let answer = word.toLowerCase();
let label = t('quizDefinition');
let hint = t('quizFillBlank');
// Specialized Questions
if (info.tags) {
if (info.tags.includes('chem') && info.origin.includes('Symbol:')) {
// Chemical Element: Symbol -> Name
const symbolMatch = info.origin.match(/Symbol: (\w+)/);
if (symbolMatch) {
type = 'symbol';
questionText = `${t('quizSymbolQuestion')} ${symbolMatch[1]}?`;
label = t('quizChemistry');
hint = t('quizElementName');
}
} else if (info.tags.includes('astro') && info.origin.includes('Type: Planet')) {
// Planet: Fact -> Name
type = 'astro';
label = t('quizAstronomy');
}
}
return {
word: word,
type: type,
question: questionText,
answer: answer,
label: label,
hint: hint
};
});
quizData.currentIndex = 0;
quizData.score = 0;
quizData.total = quizData.questions.length;
quizData.processing = false;
showQuestion();
});
}
function showQuestion() {
quizData.processing = false;
const quizContainer = document.getElementById('quizContainer');
if (quizData.currentIndex >= quizData.questions.length) {
showResults();
return;
}
const q = quizData.questions[quizData.currentIndex];
quizContainer.innerHTML = `
<div class="quiz-score">${t('quizQuestion')} ${quizData.currentIndex + 1} ${t('quizOf')} ${quizData.total}</div>
<div class="quiz-question">
<strong>${q.label}</strong><br>
${escapeHtml(q.question)}
</div>
<div class="quiz-question">
<strong>${q.hint}</strong><br>
${t('quizTheWordIs')} <input type="text" class="quiz-input" id="quizAnswer" placeholder="${t('quizAnswerPlaceholder')}">
</div>
<button class="quiz-btn" id="submitAnswer">${t('quizSubmit')}</button>
<button class="quiz-btn" id="skipQuestion">${t('quizSkip')}</button>
`;
const input = document.getElementById('quizAnswer');
input.focus();
// Submit on Enter
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') checkAnswer(q.answer);
});
document.getElementById('submitAnswer').onclick = () => checkAnswer(q.answer);
document.getElementById('skipQuestion').onclick = () => {
if(quizData.processing) return;
checkAnswer(null); // incorrect
};
}
// Simple Mutex Implementation to prevent race conditions during Async SRS updates
const QuizMutex = {
_locked: false,
_queue: [],
// Acquire lock
acquire: function() {
return new Promise(resolve => {
if (!this._locked) {
this._locked = true;
resolve();
} else {
this._queue.push(resolve);
}
});
},
// Release lock
release: function() {
if (this._queue.length > 0) {
const next = this._queue.shift();
next(); // Grant lock to next in line
} else {
this._locked = false;
}
},
// Run an async task with lock protection
dispatch: async function(task) {
await this.acquire();
try {
await task();
} finally {
this.release();
}
}
};
function checkAnswer(correctWord) {
if (quizData.processing) return;
quizData.processing = true;
const input = document.getElementById('quizAnswer');
const submitBtn = document.getElementById('submitAnswer');
const skipBtn = document.getElementById('skipQuestion');
// Disable controls to prevent multiple submissions
if(input) input.disabled = true;
if(submitBtn) submitBtn.disabled = true;
if(skipBtn) skipBtn.disabled = true;
const answer = input ? input.value.trim().toLowerCase() : '';
const isCorrect = correctWord && answer === correctWord;
if (isCorrect) {
quizData.score++;
}
// SRS Update
const currentQ = quizData.questions[quizData.currentIndex];
if (currentQ.word) {
// Async update SRS with Mutex Protection
QuizMutex.dispatch(async () => {
return new Promise((resolve) => {
chrome.storage.local.get(['wordDex'], (data) => {
const dex = data.wordDex || {};
const entry = dex[currentQ.word];
if (entry) {
// Init SRS if missing (double safety)
if (!entry.srs) entry.srs = { level: 0, streak: 0, nextReview: 0 };
if (typeof SRS !== 'undefined') {
const newSrs = SRS.calculateSRS(entry.srs, isCorrect);
entry.srs = newSrs;
// Evolution Trigger Logic (3 Stages)
if (newSrs.level >= 1 && (!entry.evolution || entry.evolution.stage < 1)) {
// Eligible for Evo 1 (Bronze)
if (!entry.evolution) entry.evolution = { stage: 0 };
entry.evolution.canEvolve = true;
}
if (newSrs.level >= 3 && (!entry.evolution || entry.evolution.stage < 2)) {
// Eligible for Evo 2 (Silver)
entry.evolution.canEvolve = true;
}
if (newSrs.level >= 5 && (!entry.evolution || entry.evolution.stage < 3)) {
// Eligible for Evo 3 (Gold)
entry.evolution.canEvolve = true;
}
chrome.storage.local.set({ wordDex: dex }, () => {
resolve(); // Release mutex after write complete
});
} else {
resolve();
}
} else {
resolve();
}
});
});
});
}
const quizContainer = document.getElementById('quizContainer');
const resultDiv = document.createElement('div');
resultDiv.className = `quiz-result ${isCorrect ? 'correct' : 'incorrect'}`;
resultDiv.innerHTML = isCorrect
? t('quizCorrect', { word: correctWord })
: t('quizIncorrect', { word: correctWord || '...' });
if(quizContainer.firstChild) {
quizContainer.insertBefore(resultDiv, quizContainer.firstChild);
} else {
quizContainer.appendChild(resultDiv);
}
setTimeout(() => {
quizData.currentIndex++;
showQuestion();
}, 2000);
}
function showResults() {
const quizContainer = document.getElementById('quizContainer');
const percentage = Math.round((quizData.score / quizData.total) * 100);
// Mark Daily Training as Done ONLY if completed
if (quizData.mode === 'training') {
chrome.storage.local.set({ lastTrainingDate: new Date().toDateString() });
}
let message = '';
if (percentage === 100) {
message = t('quizPerfect');
} else if (percentage >= 80) {
message = t('quizGreat');
} else if (percentage >= 60) {
message = t('quizGood');
} else {
message = t('quizPractice');
}
quizContainer.innerHTML = `
<div class="quiz-score">${t('quizComplete')}</div>
<div class="quiz-result ${percentage >= 60 ? 'correct' : 'incorrect'}">
<strong>${t('quizScore')} ${quizData.score} / ${quizData.total} (${percentage}%)</strong><br><br>
${message}
</div>
`;
const tryAgainBtn = document.createElement('button');
tryAgainBtn.className = 'quiz-btn';
tryAgainBtn.textContent = t('quizTryAgain');
tryAgainBtn.onclick = () => renderQuizMenu();
const backBtn = document.createElement('button');
backBtn.className = 'quiz-btn secondary';
backBtn.textContent = t('quizBack');
backBtn.style.marginTop = '8px';
backBtn.onclick = () => {
switchTab('dex'); // Assuming switchTab is global
};
quizContainer.appendChild(tryAgainBtn);
quizContainer.appendChild(backBtn);
}