// 🌍 Game state let allWords = [], words = [], current = 0, results = []; // πŸ‘©β€πŸŽ“ Show student interface function showStudent() { console.log("πŸ”Š showStudent() executed"); const studentPanel = document.getElementById('studentPanel'); const adminPanel = document.getElementById('adminPanel'); studentPanel?.classList.remove('hidden'); adminPanel?.classList.add('hidden'); setTimeout(() => { const btn = document.getElementById('startGameBtn'); if (btn) { console.log("🎯 Button found in delayed reveal"); btn.classList.remove('hidden'); btn.style.display = 'inline-block'; } else { console.warn("⚠️ Button still missing after timeout"); } }, 100); } // πŸš€ Begin game session async function startGame() { const user = JSON.parse(localStorage.getItem('loggedInUser')); if (!user) return; const res = await fetch(`/getWordList?user=${encodeURIComponent(user.username)}`); allWords = await res.json(); if (!allWords.length) { alert("No words found. Please add words in Admin Panel."); return; } words = [...allWords]; current = 0; results = []; document.getElementById('summary')?.classList.add('hidden'); document.getElementById('badgeDisplay')?.classList.add('hidden'); showWord(); } // πŸ”€ Show a word, then hide after timeout function showWord() { const wordBox = document.getElementById('wordBox'); const wordEl = document.getElementById('word'); if (!words[current]) { endGame(); return; } wordEl.textContent = words[current]; wordBox.classList.remove('hidden'); setTimeout(() => { wordBox.classList.add('hidden'); document.getElementById('inputSection').classList.remove('hidden'); document.getElementById('userInput').value = ''; document.getElementById('userInput').focus(); }, 2000); } // πŸ“ Check user’s answer and move to next function submitAnswer() { const input = document.getElementById('userInput').value.trim(); results.push({ word: words[current], attempt: input }); current++; document.getElementById('inputSection').classList.add('hidden'); if (current < words.length) { showWord(); } else { showSummary(); } } // πŸ“Š Show results summary function showSummary() { const summary = document.getElementById('summary'); let correct = 0; let missed = []; const report = results.map(({ word, attempt }) => { const isCorrect = word.toLowerCase() === attempt.toLowerCase(); if (isCorrect) correct++; else missed.push(word); return `
  • ${word} β€” ${attempt || '(no answer)'}
  • `; }).join(''); summary.innerHTML = `

    Results

    You got ${correct} out of ${words.length} correct (${Math.round((correct / words.length) * 100)}%)

    ${missed.length ? `` : ''} `; summary.classList.remove('hidden'); // πŸ† Badge evaluation const earnedBadges = []; if (correct === words.length) earnedBadges.push("Perfect Round"); if (missed.length === 0) earnedBadges.push("No Misses"); const user = JSON.parse(localStorage.getItem('loggedInUser')); if (user) { // πŸ“‘ Save structured results fetch('/saveResults', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: user.username, result: { score: correct, completed: true, answers: results.map(({ word, attempt }) => ({ word, correct: word.toLowerCase() === attempt.toLowerCase() })) } }) }); // πŸŽ– Send earned badges if (earnedBadges.length) { fetch('/awardBadges', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: user.username, badges: earnedBadges }) }); // 🎨 Display badges const badgeSection = document.getElementById("badgeDisplay"); const badgeList = document.getElementById("badgeList"); badgeList.innerHTML = ""; earnedBadges.forEach(b => { const li = document.createElement("li"); li.textContent = `πŸ… ${b}`; badgeList.appendChild(li); }); badgeSection.classList.remove("hidden"); } else { document.getElementById("badgeDisplay")?.classList.add("hidden"); } } } // πŸ” Retry only the missed words function retryMissed(missedWords) { words = missedWords; current = 0; results = []; document.getElementById('summary')?.classList.add('hidden'); document.getElementById('badgeDisplay')?.classList.add('hidden'); showWord(); }