-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
673 lines (599 loc) · 25.2 KB
/
script.js
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
let currentQuestionIndex = 0;
let score = 0;
let questions = [];
let userAnswers = [];
let leaderboard = JSON.parse(localStorage.getItem('leaderboard')) || [];
let isDarkMode = localStorage.getItem('isDarkMode') === 'true';
let isMuted = localStorage.getItem('isMuted') === 'true';
let playerLevel = parseInt(localStorage.getItem('playerLevel')) || 1;
let playerXP = parseInt(localStorage.getItem('playerXP')) || 0;
let audio = new Audio('path/to/your/background-music.mp3');
let correctSound = new Audio('media/correct-sound.mp3');
let incorrectSound = new Audio('media/incorrect-sound.mp3');
audio.loop = true;
let selectedAvatar = null;
const categories = {
entertainment: ['Movies', 'Anime', 'HarryPotter', 'Comics'],
academics: ['ComputerScience', 'Science', 'GeneralKnowledge', 'Aptitude']
};
document.addEventListener('DOMContentLoaded', () => {
// Theme persistence
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
document.body.classList.add('light-mode');
const icon = document.querySelector('#darkModeToggle i');
if (icon) icon.className = 'fas fa-sun';
}
// Modify toggleDarkMode function
window.toggleDarkMode = () => {
const isDarkMode = !document.body.classList.contains('light-mode');
document.body.classList.toggle('light-mode');
const icon = document.querySelector('#darkModeToggle i');
if (icon) icon.className = isDarkMode ? 'fas fa-sun' : 'fas fa-moon';
localStorage.setItem('theme', isDarkMode ? 'light' : 'dark');
}
// Scroll to top on navigation
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
// Modified navigateTo function
window.navigateTo = (page) => {
scrollToTop();
switch(page) {
case 'home':
showHome();
break;
case 'leaderboard':
window.location.href = 'leaderboard.html';
break;
}
}
// Fix General Knowledge category
const originalFetchQuestions = window.fetchQuestions;
window.fetchQuestions = async (category) => {
if (category === 'GeneralKnowledge') {
category = 'G.K';
}
return originalFetchQuestions(category);
}
// Start playing background music when the page loads
audio.play();
// Handle browser navigation
window.onpopstate = function(event) {
if (event.state) {
switch(event.state.page) {
case 'home':
showHome();
break;
case 'categories':
showCategories();
break;
case 'leaderboard':
showLeaderboard();
break;
}
}
};
// Add event listeners to the avatars
document.querySelectorAll('.avatar').forEach(avatar => {
avatar.addEventListener('click', () => {
// Remove the selected class from all avatars
document.querySelectorAll('.avatar').forEach(a => a.classList.remove('selected'));
// Add the selected class to the clicked avatar
avatar.classList.add('selected');
// Update the selectedAvatar variable
selectedAvatar = avatar;
});
});
});
function showAlert(message) {
const alertElement = document.getElementById('customAlert');
alertElement.textContent = message;
alertElement.style.display = 'block';
setTimeout(() => {
alertElement.style.display = 'none';
}, 3000);
}
function toggleDarkMode() {
isDarkMode = !isDarkMode;
document.body.classList.toggle('light-mode');
const icon = document.querySelector('#darkModeToggle i');
icon.className = isDarkMode ? 'fas fa-moon' : 'fas fa-sun';
localStorage.setItem('isDarkMode', isDarkMode);
if (isDarkMode) {
document.body.style.background = '#0f172a';
document.body.style.color = '#f8fafc';
} else {
document.body.style.background = '#f0f4f8';
document.body.style.color = '#2d3748';
}
}
function toggleMute() {
isMuted = !isMuted; // Toggle the isMuted state
audio.muted = isMuted; // Update the audio muted state
correctSound.muted = isMuted; // Update the correct sound muted state
incorrectSound.muted = isMuted; // Update the incorrect sound muted state
const icon = document.querySelector('#muteToggle i');
icon.className = isMuted ? 'fas fa-volume-mute' : 'fas fa-volume-up'; // Update the mute toggle icon
localStorage.setItem('isMuted', isMuted); // Store the isMuted state in local storage
}
// Set the initial audio state
document.addEventListener('DOMContentLoaded', () => {
audio.muted = isMuted; // Set the initial audio state
const icon = document.querySelector('#muteToggle i');
icon.className = isMuted ? 'fas fa-volume-mute' : 'fas fa-volume-up'; // Update the mute toggle icon
if (isMuted) {
toggleMute(); // Toggle the mute state if it's initially muted
}
});
function showHome() {
document.getElementById('welcomeContainer').classList.remove('hidden');
document.getElementById('categoryContainer').classList.add('hidden');
document.getElementById('quiz').classList.add('hidden');
document.getElementById('scoreContainer').classList.add('hidden');
document.getElementById('reviewContainer').classList.add('hidden');
document.getElementById('leaderboardContainer').classList.add('hidden');
showSection('welcomeContainer');
}
function showCategories() {
const playerName = document.getElementById('playerName').value.trim();
if (!playerName) {
showAlert('Please enter your name before starting the quiz!');
return;
}
if (!selectedAvatar) {
showAlert('Please select an avatar before starting the quiz!');
return;
}
showSection('categoryContainer');
document.getElementById('welcomeContainer').classList.add('hidden');
document.getElementById('categoryContainer').classList.remove('hidden');
window.scrollTo({ top: 0, behavior: 'smooth' }); // Scroll to top of the page
// ...
const entertainmentGrid = document.getElementById('entertainmentGrid');
const academicsGrid = document.getElementById('academicsGrid');
entertainmentGrid.innerHTML = '';
academicsGrid.innerHTML = '';
categories.entertainment.forEach(category => {
const card = createCategoryCard(category);
entertainmentGrid.appendChild(card);
});
categories.academics.forEach(category => {
const card = createCategoryCard(category);
academicsGrid.appendChild(card);
});
}
function createCategoryCard(category) {
const card = document.createElement('div');
card.className = 'category-card';
card.onclick = () => startQuiz(category);
card.innerHTML = `
<img src="media/${category.toLowerCase()}.gif" alt="${category}" loading="lazy">
<h3>${category}</h3>
`;
return card;
}
function navigateTo(page) {
history.pushState({ page: page }, '', `#${page}`);
switch(page) {
case 'home':
showHome();
break;
case 'categories':
showCategories();
break;
case 'leaderboard':
showLeaderboard();
break;
}
}
async function startQuiz(category) {
showLoadingAnimation();
const fetchSuccessful = await fetchQuestions(category);
hideLoadingAnimation();
if (fetchSuccessful) {
document.getElementById('categoryContainer').classList.add('hidden');
document.getElementById('quiz').classList.remove('hidden');
window.scrollTo({ top: 0, behavior: 'smooth' }); // Scroll to top of the page
showQuestion();
} else {
showAlert('Failed to fetch questions. Please try again.');
showCategories();
}
}
async function fetchQuestions(category) {
let apiUrl;
switch (category) {
case 'Movies':
apiUrl = 'https://opentdb.com/api.php?amount=10&category=11&type=multiple';
break;
case 'Anime':
apiUrl = 'https://opentdb.com/api.php?amount=10&category=31&type=multiple';
break;
case 'HarryPotter':
apiUrl = 'https://hp-api.onrender.com/api/spells';
break;
case 'Comics':
apiUrl = 'https://opentdb.com/api.php?amount=10&category=29&type=multiple';
break;
case 'ComputerScience':
apiUrl = 'https://opentdb.com/api.php?amount=10&category=18&type=multiple';
break;
case 'Science':
apiUrl = 'https://opentdb.com/api.php?amount=10&category=17&type=multiple';
break;
case 'G.K':
apiUrl = 'https://opentdb.com/api.php?amount=10&category=9&type=multiple';
break;
case 'Aptitude':
apiUrl = 'https://opentdb.com/api.php?amount=10&category=19&type=multiple';
break;
default:
apiUrl = 'https://opentdb.com/api.php?amount=10&category=9&type=multiple';
break;
}
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`Failed to load questions: ${response.status} ${response.statusText}`);
}
const data = await response.json();
// ...
// ...
if (category === 'HarryPotter') {
questions = data.slice(0, 10).map(spell => ({
question: `What is the effect of the spell "${spell.name}"?`,
answers: shuffle([
spell.description,
'Disarms an opponent',
'Creates a shield charm',
'Reduces an object to pieces'
]),
correct: spell.description
}));
} else {
questions = data.results.map(question => ({
question: decodeHtml(question.question),
answers: shuffle([
...question.incorrect_answers.map(decodeHtml),
decodeHtml(question.correct_answer)
]),
correct: decodeHtml(question.correct_answer)
}));
}
// Ensure valid questions
questions = questions.filter(q =>
q.question && q.correct && q.answers &&
q.answers.length === 4 &&
new Set(q.answers).size === 4
);
if (questions.length < 5) {
throw new Error('Not enough valid questions');
}
currentQuestionIndex = 0;
score = 0;
userAnswers = [];
return true;
} catch (error) {
console.error('Error fetching questions:', error);
showAlert('Failed to fetch questions. Please try again.');
return false;
}
}
function decodeHtml(html) {
const txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
function shuffle(array) {
const 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;
}
function showQuestion() {
const questionContainer = document.getElementById("questionContainer");
const progressBar = document.createElement('div');
progressBar.className = 'progress-bar';
progressBar.innerHTML = `<div class="progress" style="width: ${(currentQuestionIndex / questions.length) * 100}%"></div>`;
const question = questions[currentQuestionIndex];
// Escape the question and answers to prevent syntax errors
const escapedQuestion = escapeHtml(question.question);
const escapedAnswers = question.answers.map(answer => escapeHtml(answer));
questionContainer.innerHTML = `
${progressBar.outerHTML}
<div class="question-content">
<h3>${escapedQuestion}</h3>
<ul>
${escapedAnswers.map((answer, index) => `
<li>
<button class="option-button" data-answer="${answer}">
${answer}
</button>
</li>`).join('')}
</ul>
</div>
`;
const buttons = questionContainer.querySelectorAll('.option-button');
buttons.forEach(button => {
button.addEventListener('click', () => {
checkAnswer(button.dataset.answer);
});
});
document.getElementById("currentQuestionNumber").textContent = currentQuestionIndex + 1;
document.getElementById("totalQuestions").textContent = questions.length;
}
// Function to escape HTML special characters
function escapeHtml(html) {
const text = document.createElement("textarea");
text.innerHTML = html;
return text.value;
}
function checkAnswer(selectedAnswer) {
const question = questions[currentQuestionIndex];
userAnswers.push({
question: question.question,
selected: selectedAnswer,
correct: question.correct
});
const buttons = document.querySelectorAll(".option-button");
const isCorrect = selectedAnswer === question.correct;
try {
buttons.forEach(button => {
const buttonAnswer = button.textContent.trim();
if (buttonAnswer === question.correct) {
button.classList.add("correct");
} else if (buttonAnswer === selectedAnswer) {
button.classList.add("incorrect");
}
button.disabled = true; // Disable buttons after answering
});
const feedbackMessage = document.getElementById("feedbackMessage");
feedbackMessage.textContent = isCorrect ? "Correct! Well done!" : `Incorrect. The correct answer is: ${question.correct}`;
feedbackMessage.className = `feedback-message ${isCorrect ? 'correct' : 'incorrect'} show`;
// Play sound effect
if (isCorrect) {
if (!isMuted) correctSound.play();
score++;
} else {
if (!isMuted) incorrectSound.play();
}
setTimeout(() => {
feedbackMessage.classList.remove("show");
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion();
} else {
showScore();
}
}, 2000);
} catch (error) {
console.error("Error checking answer:", error);
// Skip to the next question if there's an error
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion();
} else {
showScore();
}
}
}
function showScore() {
document.getElementById('quiz').classList.add('hidden');
document.getElementById('scoreContainer').classList.remove('hidden');
showSection('scoreContainer');
const finalScore = document.getElementById("finalScore");
const percentage = (score / questions.length) * 100;
finalScore.innerHTML = `
<h3>Your Final Score: ${score} out of ${questions.length}</h3>
<p>Percentage: ${percentage.toFixed(1)}%</p>
`;
updatePlayerProgress();
updateLeaderboard();
displayLevelInfo();
}
function updatePlayerProgress() {
const xpGained = score * 10; // 10 XP per correct answer
playerXP += xpGained;
while (playerXP >= playerLevel * 100) { // Level up when XP reaches level * 100
playerXP -= playerLevel * 100;
playerLevel++;
}
localStorage.setItem('playerLevel', playerLevel);
localStorage.setItem('playerXP', playerXP);
}
function displayLevelInfo() {
const levelInfo = document.getElementById('levelInfo');
levelInfo.innerHTML = `
<h3>Level: ${playerLevel}</h3>
<p>XP: ${playerXP} / ${playerLevel * 100}</p>
<div class="xp-bar">
<div class="xp-progress" style="width: ${(playerXP / (playerLevel * 100)) * 100}%"></div>
</div>
`;
}
function updateLeaderboard() {
const playerName = document.getElementById('playerName').value.trim();
const newEntry = {
name: playerName,
score: score,
total: questions.length,
percentage: ((score / questions.length) * 100).toFixed(1),
level: playerLevel,
avatar: selectedAvatar.src
};
leaderboard.push(newEntry);
leaderboard.sort((a, b) => (b.score/b.total) - (a.score/a.total));
leaderboard = leaderboard.slice(0, 10); // Keep only top 10
localStorage.setItem('leaderboard', JSON.stringify(leaderboard));
}
function showLeaderboard() {
document.getElementById('scoreContainer').classList.add('hidden');
document.getElementById('leaderboardContainer').classList.remove('hidden');
window.scrollTo({ top: 0, behavior: 'smooth' }); // Scroll to top of the page
displayLeaderboard();
showSection('leaderboardContainer');
}
function showSection(sectionId) {
// Hide all quiz sections
document.getElementById('welcomeContainer').classList.add('hidden');
document.getElementById('categoryContainer').classList.add('hidden');
document.getElementById('quiz').classList.add('hidden');
document.getElementById('scoreContainer').classList.add('hidden');
document.getElementById('reviewContainer').classList.add('hidden');
document.getElementById('leaderboardContainer').classList.add('hidden');
// Show the specified section
document.getElementById(sectionId).classList.remove('hidden');
}
function displayLeaderboard() {
const leaderboard = JSON.parse(localStorage.getItem('leaderboard')) || [];
const leaderboardList = document.getElementById('leaderboardList');
if (leaderboard.length > 0) {
leaderboardList.innerHTML = leaderboard.map((entry, index) => `
<div class="leaderboard-entry" style="animation: fadeIn 0.3s ease ${index * 0.1}s forwards">
<span>${index + 1}. ${entry.name} <img src="${entry.avatar}" alt="Avatar" class="leaderboard-avatar"></span>
<span>${entry.score}/${entry.total} (${entry.percentage}%) - Level ${entry.level}</span>
</div>
`).join('');
} else {
leaderboardList.innerHTML = '<p>No leaderboard entries found.</p>';
}
}
function clearLeaderboard() {
const confirmDialog = document.createElement('div');
confirmDialog.className = 'confirm-dialog';
confirmDialog.innerHTML = `
<div class="confirm-dialog-content">
<p>Are you sure you want to clear the leaderboard?</p>
<div class="button-group">
<button onclick="confirmClearLeaderboard(true)">Yes</button>
<button onclick="confirmClearLeaderboard(false)">No</button>
</div>
</div>
`;
document.body.appendChild(confirmDialog);
}
function confirmClearLeaderboard(confirmed) {
if (confirmed) {
leaderboard = [];
localStorage.removeItem('leaderboard');
displayLeaderboard();
showAlert('Leaderboard cleared!');
document.querySelector('.confirm-dialog').remove();
document.getElementById('leaderboardList').innerHTML = '';
} else {
document.querySelector('.confirm-dialog').remove();
}
}
function reviewAnswers() {
document.getElementById('scoreContainer').classList.add('hidden');
document.getElementById('leaderboardContainer').classList.add('hidden');
document.getElementById('reviewContainer').classList.remove('hidden');
showSection('reviewContainer');
window.scrollTo({ top: 0, behavior: 'smooth' }); // Scroll to top of the page
// ...
const reviewList = document.getElementById('reviewList');
reviewList.innerHTML = userAnswers.map((answer, index) => `
<div class="review-item ${answer.selected === answer.correct ? 'correct' : 'incorrect'}">
<h4>Question ${index + 1}</h4>
<p><strong>Q:</strong> ${answer.question}</p>
<div class="review-answer ${answer.selected === answer.correct ? 'correct' : 'incorrect'} selected">
<strong>Your answer:</strong> ${answer.selected}
</div>
${answer.selected !== answer.correct ? `
<div class="review-answer correct">
<strong>Correct answer:</strong> ${answer.correct}
</div>
` : ''}
</div>
`).join('');
}
function playAgain() {
showCategories();
currentQuestionIndex = 0;
score = 0;
questions = [];
userAnswers = [];
document.getElementById('reviewList').innerHTML = '';
showCategories();
}
function showLoadingAnimation() {
const loadingOverlay = document.createElement('div');
loadingOverlay.id = 'loadingOverlay';
loadingOverlay.innerHTML = '<div class="spinner"></div>';
document.body.appendChild(loadingOverlay);
}
function hideLoadingAnimation() {
const loadingOverlay = document.getElementById('loadingOverlay');
if (loadingOverlay) {
loadingOverlay.remove();
}
}
document.addEventListener('DOMContentLoaded', () => {
showHome();
document.getElementById('darkModeToggle').addEventListener('click', toggleDarkMode);
document.getElementById('muteToggle').addEventListener('click', toggleMute);
// Initialize particles.js
particlesJS.load('particles-js', 'particles.json', function() {
console.log('particles.js loaded - callback');
});
// Start playing background music when the page loads
audio.play();
});
// Handle browser navigation
window.onpopstate = function(event) {
if (event.state) {
switch(event.state.page) {
case 'home':
showHome();
break;
case 'categories':
showCategories();
break;
case 'leaderboard':
showLeaderboard();
break;
}
}
};
function navigateTo(page) {
switch(page) {
case 'home':
showHome();
break;
case 'leaderboard':
showLeaderboard();
break;
// Add more cases as needed
}
}
document.querySelectorAll('.avatar').forEach(avatar => {
avatar.addEventListener('click', () => {
if (selectedAvatar === avatar) {
// Deselect if clicking the same avatar
selectedAvatar = null;
document.querySelectorAll('.avatar').forEach(a => {
a.src = a.dataset.neutral;
a.classList.remove('chosen', 'not-chosen');
});
} else {
selectedAvatar = avatar;
document.querySelectorAll('.avatar').forEach(a => {
if (a === selectedAvatar) {
a.src = a.dataset.chosen;
a.classList.add('chosen');
a.classList.remove('not-chosen');
} else {
a.src = a.dataset.notChosen;
a.classList.add('not-chosen');
a.classList.remove('chosen');
}
});
}
});
});