Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
762 lines (638 loc) · 24.7 KB
/
Copy pathapp.js
File metadata and controls
762 lines (638 loc) · 24.7 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
// -------------------------------------------------
// Firebase SDK Imports
// -------------------------------------------------
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-app.js";
import {
getAuth,
GoogleAuthProvider,
signInWithPopup,
onAuthStateChanged,
setPersistence,
browserLocalPersistence
} from "https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js";
import { getAnalytics, logEvent } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-analytics.js";
import {
getFirestore,
doc,
getDoc,
setDoc,
setLogLevel,
updateDoc,
arrayUnion,
runTransaction
} from "https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js";
// -------------------------------------------------
// DOM Element References
// -------------------------------------------------
const $setupScreen = document.getElementById('setup-screen');
const $countdownScreen = document.getElementById('countdown-screen');
const $gameScreen = document.getElementById('game-screen');
const $resultsScreen = document.getElementById('results-screen');
const $startButton = document.getElementById('start-button');
const $playAgainButton = document.getElementById('play-again-button');
const $changeSettingsButton = document.getElementById('change-settings-button');
const $categorySelect = document.getElementById('category');
const $difficultySelect = document.getElementById('difficulty');
const $timeSelect = document.getElementById('time');
const $loadingIndicator = document.getElementById('loading-indicator');
const $loadingStatus = document.getElementById('loading-status');
const $countdownTimer = document.getElementById('countdown-timer');
const $gameTimerValue = document.getElementById('game-timer-value');
const $wordDisplay = document.getElementById('word-display');
const $wordContainer = document.getElementById('word-container');
const $skipArea = document.getElementById('skip-area');
const $correctArea = document.getElementById('correct-area');
const $feedbackOverlay = document.getElementById('feedback-overlay');
// --- UPGRADED Results Screen Elements ---
const $finalScore = document.getElementById('final-score');
const $correctCount = document.getElementById('correct-count');
const $skippedCount = document.getElementById('skipped-count');
const $viewCorrectBtn = document.getElementById('view-correct-btn');
const $viewSkippedBtn = document.getElementById('view-skipped-btn');
// --- NEW Modal Elements ---
const $wordModal = document.getElementById('word-modal');
const $modalBackdrop = document.getElementById('modal-backdrop');
const $modalContent = document.getElementById('modal-content');
const $modalTitle = document.getElementById('modal-title');
const $modalList = document.getElementById('modal-list');
const $modalCloseBtn = document.getElementById('modal-close-btn');
// -------------------------------------------------
// Firebase & App Config
// -------------------------------------------------
// --- DEPLOYMENT CONFIG ---
const appId = "heads-up-v1";
const firebaseConfig = {
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID,
measurementId: import.meta.env.VITE_FIREBASE_MEASUREMENT_ID
};
// Initialize Firebase
let app, auth, db, googleProvider, analytics;
try {
app = initializeApp(firebaseConfig);
auth = getAuth(app);
db = getFirestore(app);
googleProvider = new GoogleAuthProvider();
analytics = getAnalytics(app);
setLogLevel('Debug');
console.log("Firebase Initialized Successfully.");
} catch (e) {
console.error("Firebase initialization failed:", e);
$loadingStatus.textContent = "Error: Config Failed.";
}
// -------------------------------------------------
// Sound Effect State
// -------------------------------------------------
let audioReady = false;
let synth, beepSynth, tickSynth;
// -------------------------------------------------
// Game State Variables
// -------------------------------------------------
let userId = null;
let currentWordDeck = [];
let correctWords = [];
let skippedWords = [];
let gameActive = false;
let timeRemaining = 60;
let gameTimerInterval = null;
let countdownTimerInterval = null;
let currentWordIndex = 0;
let wordDeck = [];
let skipLocked = false;
let correctLocked = false;
let totalScore = 0;
let currentSettings = {
category: 'Movies',
difficulty: 'Easy',
time: 60
};
const REFRESH_BATCH_COUNT = 20;
// -------------------------------------------------
// Core Game Logic
// -------------------------------------------------
function initAudio() {
if (audioReady || !window.Tone) return;
try {
// Define synths
synth = new Tone.Synth().toDestination();
beepSynth = new Tone.MembraneSynth().toDestination();
tickSynth = new Tone.Synth({
oscillator: { type: 'triangle' },
envelope: { attack: 0.005, decay: 0.1, sustain: 0.01, release: 0.1 }
}).toDestination();
audioReady = true;
console.log("Audio context started.");
} catch (e) {
console.error("Failed to initialize Tone.js:", e);
}
}
function playSound(sound) {
if (!audioReady) return;
const now = Tone.now();
try {
switch(sound) {
case 'countdownBeep':
beepSynth.triggerAttackRelease("C2", "8n", now);
break;
case 'countdownGo':
beepSynth.triggerAttackRelease("G2", "4n", now);
break;
case 'correct':
synth.triggerAttackRelease("E5", "16n", now);
break;
case 'skip':
tickSynth.triggerAttackRelease("C3", "16n", now);
break;
case 'timerTick':
tickSynth.triggerAttackRelease("C6", "16n", now);
break;
case 'endGame':
synth.triggerAttackRelease("C4", "8n", now);
synth.triggerAttackRelease("G4", "8n", now + 0.2);
synth.triggerAttackRelease("C5", "4n", now + 0.4);
break;
}
} catch (e) {
console.error("Tone.js playSound error:", e);
}
}
/**
* Main entry point. Authenticates the user and sets up listeners.
*/
async function main() {
if (!auth) {
console.error("Authentication service is not available.");
$loadingStatus.textContent = "Error: Auth Failed.";
$loadingIndicator.classList.remove('hidden');
return;
}
// Set persistence to local so they stay logged in
await setPersistence(auth, browserLocalPersistence);
// Listen for auth state changes
onAuthStateChanged(auth, (user) => {
if (user) {
userId = user.uid;
console.log("User is authenticated. UID:", userId);
// User is signed in, show the start button
$startButton.disabled = false;
$startButton.textContent = "START GAME";
} else {
userId = null;
console.log("User is not authenticated. Showing sign-in button.");
// User is not signed in, show the sign-in button
$startButton.disabled = false;
$startButton.textContent = "SIGN IN WITH GOOGLE";
}
});
}
/**
* --- UPDATED: Robust Sign-In Logic (POPUP ONLY) ---
* We use Popup for both desktop and mobile to ensure session persistence.
*/
async function signInWithGoogle() {
try {
$startButton.disabled = true;
$startButton.textContent = "SIGNING IN...";
// Always use Popup - it bypasses the storage partitioning issues on mobile
await signInWithPopup(auth, googleProvider);
// onAuthStateChanged will handle the rest
} catch (error) {
console.error("Google Sign-In failed:", error);
// Handle popup closed by user specifically
if (error.code === 'auth/popup-closed-by-user') {
console.log("User closed the popup.");
} else {
alert(`Login Error: ${error.message}`);
}
$startButton.disabled = false;
$startButton.textContent = "SIGN IN WITH GOOGLE";
}
}
// -------------------------------------------------
// UI Helpers
// -------------------------------------------------
function switchScreen(screenId) {
$setupScreen.classList.add('hidden');
$countdownScreen.classList.add('hidden');
$gameScreen.classList.add('hidden');
$resultsScreen.classList.add('hidden');
const targetScreen = document.getElementById(`${screenId}-screen`);
if (targetScreen) {
targetScreen.classList.remove('hidden');
} else {
console.error(`Screen ID "${screenId}" not found.`);
}
}
/**
* Fetches word deck, then starts the pre-game countdown
*/
async function handleStartGame() {
// Check if user is signed in
if (!userId) {
await signInWithGoogle();
return;
}
// Initialize Audio on user gesture
if (!audioReady && window.Tone) {
await Tone.start();
initAudio();
}
// Show loading indicator
$loadingIndicator.classList.remove('hidden');
$loadingStatus.textContent = "Building your deck... 📚";
// Store current settings
currentSettings.category = $categorySelect.value;
currentSettings.difficulty = $difficultySelect.value;
currentSettings.time = parseInt($timeSelect.value, 10);
timeRemaining = currentSettings.time;
try {
// Log game start event
if (analytics) {
logEvent(analytics, 'game_start', {
category: currentSettings.category,
difficulty: currentSettings.difficulty,
time: currentSettings.time
});
}
// Reset game state
gameActive = false;
timeRemaining = currentSettings.time;
currentWordIndex = 0;
correctWords = [];
skippedWords = [];
totalScore = 0;
// Show loading state
$loadingIndicator.classList.remove('hidden');
$loadingStatus.textContent = `Preparing your '${currentSettings.category}' game...`;
$startButton.disabled = true;
// Fetch and prepare the word deck
const deckId = `${currentSettings.category}_${currentSettings.difficulty}`.toLowerCase();
const wordDeck = await getWordDeck(deckId);
if (!wordDeck || wordDeck.length === 0) {
throw new Error("Deck is empty, and AI failed to populate it.");
}
// Reset game state
currentWordDeck = [...wordDeck].sort(() => 0.5 - Math.random()); // Shuffle the deck
correctWords = [];
skippedWords = [];
totalScore = 0;
console.log(`Game starting with ${currentWordDeck.length} words.`);
// Hide loading and start countdown
$loadingIndicator.classList.add('hidden');
startCountdown();
} catch (error) {
console.error("Error starting game:", error);
$loadingIndicator.classList.add('hidden');
alert(`Error: ${error.message}. Please try again.`);
}
}
function startCountdown() {
switchScreen('countdown');
let count = 3;
$countdownTimer.textContent = count;
playSound('countdownBeep');
countdownTimerInterval = setInterval(() => {
count--;
if (count > 0) {
$countdownTimer.textContent = count;
playSound('countdownBeep');
} else {
clearInterval(countdownTimerInterval);
playSound('countdownGo');
startGame();
}
}, 1000);
}
function startGame() {
switchScreen('game');
updateGameTimerDisplay();
nextWord();
gameTimerInterval = setInterval(() => {
timeRemaining--;
updateGameTimerDisplay();
if (timeRemaining <= 0) {
endGame();
}
}, 1000);
}
function endGame() {
clearInterval(gameTimerInterval);
switchScreen('results');
playSound('endGame');
const correctScore = correctWords.length * 2;
const skippedPenalty = skippedWords.length * 1;
totalScore = correctScore - skippedPenalty;
if (analytics) {
logEvent(analytics, 'game_end', {
score: totalScore,
correct: correctWords.length,
skipped: skippedWords.length
});
}
$finalScore.textContent = totalScore;
$correctCount.textContent = correctWords.length;
$skippedCount.textContent = skippedWords.length;
}
function nextWord() {
if (currentWordDeck.length > 0) {
const word = currentWordDeck.pop();
$wordDisplay.textContent = word;
adjustWordFontSize();
} else {
$wordDisplay.textContent = "DECK EMPTY!";
adjustWordFontSize();
endGame();
}
}
function adjustWordFontSize() {
const container = $wordContainer;
const wordEl = $wordDisplay;
wordEl.style.fontSize = '8vw';
let currentFontSize = 8;
const availableHeight = container.clientHeight - 80;
const availableWidth = container.clientWidth - 40;
while ((wordEl.scrollHeight > availableHeight || wordEl.scrollWidth > availableWidth) && currentFontSize > 1) {
currentFontSize -= 0.5;
wordEl.style.fontSize = `${currentFontSize}vw`;
}
}
function handleCorrect() {
if (timeRemaining <= 0 || correctLocked) return;
const word = $wordDisplay.textContent;
if (word && word !== "DECK EMPTY!") {
if (analytics) logEvent(analytics, 'word_correct', { word: word });
correctLocked = true;
$correctArea.style.opacity = '0.5';
$correctArea.style.pointerEvents = 'none';
setTimeout(() => {
correctLocked = false;
$correctArea.style.opacity = '';
$correctArea.style.pointerEvents = '';
}, 2000);
correctWords.push(word);
updateSeenWords(word);
flashFeedback('green');
playSound('correct');
nextWord();
}
}
function handleSkip() {
if (timeRemaining <= 0 || skipLocked) return;
const word = $wordDisplay.textContent;
if (word && word !== "DECK EMPTY!") {
if (analytics) logEvent(analytics, 'word_skipped', { word: word });
skipLocked = true;
$skipArea.style.opacity = '0.5';
$skipArea.style.pointerEvents = 'none';
setTimeout(() => {
skipLocked = false;
$skipArea.style.opacity = '';
$skipArea.style.pointerEvents = '';
}, 1000);
skippedWords.push(word);
flashFeedback('red');
playSound('skip');
nextWord();
}
}
function flashFeedback(color) {
const colorClass = color === 'green' ? 'bg-green-500/70' : 'bg-red-500/70';
$feedbackOverlay.classList.add(colorClass, 'opacity-100');
setTimeout(() => {
$feedbackOverlay.classList.remove(colorClass, 'opacity-100');
}, 150);
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
}
function updateGameTimerDisplay() {
$gameTimerValue.textContent = formatTime(timeRemaining);
if (timeRemaining > 0 && timeRemaining <= 5) {
playSound('timerTick');
}
}
function shuffleArray(array) {
let newArray = [...array];
for (let i = newArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[newArray[i], newArray[j]] = [newArray[j], newArray[i]];
}
return newArray;
}
// -------------------------------------------------
// Data & API Logic (ALL UPGRADED)
// -------------------------------------------------
/**
* --- FIXED: Calls our Firebase Cloud Function instead of Gemini directly ---
*/
async function callGeminiAPI(category, difficulty, count, existingWords = []) {
// Using Cloud Run endpoint for better reliability
const functionUrl = "https://getaiwords-2vgpkucjlq-uc.a.run.app";
const payload = {
category,
difficulty,
count,
existingWords
};
try {
const response = await fetch(functionUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
});
if (!response.ok) {
console.error("Cloud Function call failed:", response.status, await response.text());
throw new Error("Failed to fetch words from the server.");
}
return await response.json();
} catch (error) {
console.error("Error calling Firebase Function:", error);
throw new Error("Network or server error while fetching words.");
}
}
async function updateSeenWords(word) {
const deckId = `${currentSettings.category}_${currentSettings.difficulty}`.toLowerCase();
const userDeckRef = doc(db, `artifacts/${appId}/users/${userId}/userDecks`, deckId);
try {
await updateDoc(userDeckRef, {
seenWords: arrayUnion(word)
});
} catch (error) {
console.error("Failed to update seenWords:", error);
}
}
async function getWordDeck(deckId) {
console.log(`Getting deck: ${deckId} for user: ${userId}`);
const userDeckRef = doc(db, `artifacts/${appId}/users/${userId}/userDecks`, deckId);
const userDeckSnap = await getDoc(userDeckRef);
let userDeckData;
if (!userDeckSnap.exists()) {
console.log("No private deck. Seeding from master bank...");
$loadingStatus.textContent = "Creating your personal deck... 📇";
userDeckData = await seedUserDeck(deckId, userDeckRef);
if (!userDeckData) {
throw new Error("Failed to seed new user deck.");
}
} else {
userDeckData = userDeckSnap.data();
}
const { allWords = [], seenWords = [] } = userDeckData;
const seenSet = new Set(seenWords);
let availableWords = allWords.filter(word => !seenSet.has(word));
console.log(`Deck status: ${allWords.length} total, ${seenWords.length} seen, ${availableWords.length} available.`);
const dynamicLowWordThreshold = Math.floor(allWords.length / 10);
if (availableWords.length === 0) {
console.warn("User has seen all words. Refreshing deck...");
$loadingStatus.textContent = "Deck complete! Fetching new words... ♻️";
const updatedDeckData = await refreshWordCache(deckId, userDeckRef);
const { allWords: newAll, seenWords: newSeen } = updatedDeckData;
const newSeenSet = new Set(newSeen);
availableWords = newAll.filter(word => !newSeenSet.has(word));
if (availableWords.length === 0) {
throw new Error("Deck is empty and AI refresh failed.");
}
}
else if (availableWords.length > 0 && availableWords.length < dynamicLowWordThreshold) {
console.log(`Word deck is low (${availableWords.length} available). Triggering background refresh...`);
refreshWordCache(deckId, userDeckRef).catch(err => {
console.error("Background refresh failed:", err);
});
}
return availableWords;
}
async function seedUserDeck(deckId, userDeckRef) {
const masterDeckRef = doc(db, `artifacts/${appId}/public/data/decks`, deckId);
let masterDeckSnap = await getDoc(masterDeckRef);
let masterDeckData = masterDeckSnap.data();
if (!masterDeckSnap.exists() || !masterDeckData || !masterDeckData.allWords || masterDeckData.allWords.length === 0) {
console.log("Master bank is empty. Seeding from AI...");
$loadingStatus.textContent = `Creating first deck for ${currentSettings.category}...`;
masterDeckData = await refreshWordCache(deckId, userDeckRef, masterDeckRef);
}
const newPrivateDeck = {
allWords: masterDeckData.allWords,
seenWords: []
};
await setDoc(userDeckRef, newPrivateDeck);
console.log("Private deck seeded.");
return newPrivateDeck;
}
async function refreshWordCache(deckId, userDeckRef, masterDeckRef) {
if (!masterDeckRef) {
masterDeckRef = doc(db, `artifacts/${appId}/public/data/decks`, deckId);
}
console.log(`Refreshing cache for ${deckId}...`);
$loadingStatus.textContent = `Topping up your '${currentSettings.category}' deck...`;
$loadingIndicator.classList.remove('hidden');
try {
const [userDeckSnap, masterDeckSnap] = await Promise.all([
getDoc(userDeckRef),
getDoc(masterDeckRef)
]);
const userDeckData = userDeckSnap.data() || { allWords: [], seenWords: [] };
const masterDeckData = masterDeckSnap.data() || { allWords: [] };
let existingWords = new Set([...masterDeckData.allWords, ...userDeckData.allWords]);
let allNewWords = [];
for (let i = 0; i < 5; i++) {
try {
const result = await callGeminiAPI(
currentSettings.category,
currentSettings.difficulty,
100,
Array.from(existingWords)
);
if (result && result.words) {
for (const word of result.words) {
const trimmed = word.trim();
if (trimmed.length > 0 && !existingWords.has(trimmed)) {
allNewWords.push(trimmed);
existingWords.add(trimmed);
}
}
console.log(`Batch ${i + 1} added ${result.words.length} words`);
}
} catch (error) {
console.error(`Error in batch ${i + 1}:`, error);
}
}
if (allNewWords.length === 0) {
console.warn("AI returned no new words.");
if (userDeckData.allWords.length === 0) {
throw new Error("AI failed and no words are in the cache.");
}
return userDeckData;
}
console.log(`Fetched ${allNewWords.length} new unique words.`);
const newDeckData = await runTransaction(db, async (transaction) => {
transaction.set(masterDeckRef, {
allWords: arrayUnion(...allNewWords)
}, { merge: true });
const updatedUserDeck = {
allWords: [...userDeckData.allWords, ...allNewWords],
seenWords: []
};
transaction.set(userDeckRef, updatedUserDeck);
return updatedUserDeck;
});
console.log("Cache refresh complete. User's seen list was reset.");
return newDeckData;
} catch (error) {
console.error("CRITICAL: Failed to refresh word cache:", error);
throw new Error(`Could not load word deck: ${error.message}`);
} finally {
$loadingIndicator.classList.add('hidden');
}
}
// --- Modal Show/Hide Logic ---
function showWordModal(title, words) {
$modalTitle.textContent = `${title} (${words.length})`;
if (words.length === 0) {
$modalList.innerHTML = `<p class="text-gray-400 p-4">No words to show.</p>`;
} else {
$modalList.innerHTML = words
.map(word => `<div class="results-list-item">${word}</div>`)
.join('');
}
$wordModal.classList.add('is-open');
}
function hideWordModal() {
$wordModal.classList.remove('is-open');
}
// -------------------------------------------------
// Event Listeners
// -------------------------------------------------
$startButton.addEventListener('click', handleStartGame);
$playAgainButton.addEventListener('click', handleStartGame);
$changeSettingsButton.addEventListener('click', () => switchScreen('setup'));
$viewCorrectBtn.addEventListener('click', () => showWordModal('Correct', correctWords));
$viewSkippedBtn.addEventListener('click', () => showWordModal('Skipped', skippedWords));
$modalBackdrop.addEventListener('click', hideWordModal);
$modalCloseBtn.addEventListener('click', hideWordModal);
$skipArea.addEventListener('mousedown', (e) => {
e.preventDefault();
handleSkip();
});
$correctArea.addEventListener('mousedown', (e) => {
e.preventDefault();
handleCorrect();
});
window.addEventListener('keydown', (e) => {
if ($gameScreen.classList.contains('hidden')) {
return;
}
if (e.key === 'ArrowLeft') {
handleSkip();
} else if (e.key === 'ArrowRight') {
handleCorrect();
}
});
// -------------------------------------------------
// Initialize the App
// -------------------------------------------------
main();