-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (166 loc) · 6.36 KB
/
Copy pathscript.js
File metadata and controls
187 lines (166 loc) · 6.36 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
let username = '';
let score = 0;
let answered = false;
let askedQuestions = [];
const totalQuestions = 8;
function startGame() {
username = document.getElementById('username').value;
const password = document.getElementById('password').value;
if (!username || !password) {
alert('Please enter a username and password');
return;
}
fetch('http://127.0.0.1:8000/api/start', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById('login-section').classList.add('hidden');
document.getElementById('game-section').classList.remove('hidden');
fetchQuestion(); // Fetch the first question immediately
} else {
alert('Failed to start game');
}
})
.catch(error => {
console.error('Error:', error);
});
}
function fetchQuestion() {
if (askedQuestions.length >= totalQuestions) {
document.getElementById('question').innerText = 'No more questions';
document.getElementById('choices').innerHTML = '';
displayFinalScore();
return;
}
answered = false;
document.getElementById('feedback').innerText = '';
fetch('http://127.0.0.1:8000/api/quest', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `username=${encodeURIComponent(username)}`
})
.then(response => response.json())
.then(data => {
if (data.question) {
document.getElementById('question').innerText = data.question;
const choicesContainer = document.getElementById('choices');
choicesContainer.innerHTML = '';
data.choices.forEach((choice, index) => {
const button = document.createElement('button');
button.innerText = choice;
button.onclick = () => submitAnswer(choice, button, data.question, data.correctAnswer);
choicesContainer.appendChild(button);
});
} else {
document.getElementById('question').innerText = 'No more questions';
document.getElementById('choices').innerHTML = '';
displayFinalScore();
}
})
.catch(error => {
console.error('Error:', error);
});
}
function submitAnswer(answer, button, question, correctAnswer) {
if (answered) return;
answered = true;
fetch('http://127.0.0.1:8000/api/answer', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `username=${encodeURIComponent(username)}&answer=${encodeURIComponent(answer)}`
})
.then(response => response.json())
.then(data => {
const feedback = document.getElementById('feedback');
if (data.correct) {
feedback.innerText = 'Correct!';
score += 5;
} else {
feedback.innerText = 'Incorrect!';
}
updateScore();
storeQuestion(question, correctAnswer);
highlightAnswer(button, data.correct);
setTimeout(() => {
resetChoices();
fetchQuestion();
}, 3000); // 3-second delay before fetching the next question
})
.catch(error => {
console.error('Error:', error);
});
}
function highlightAnswer(button, correct) {
const buttons = document.querySelectorAll('#choices button');
buttons.forEach(btn => {
btn.disabled = true; // Disable all buttons after an answer is selected
if (btn === button) {
btn.style.backgroundColor = correct ? 'green' : 'red';
btn.style.color = 'white';
} else {
btn.style.backgroundColor = 'white';
btn.style.color = 'black';
}
});
}
function resetChoices() {
const buttons = document.querySelectorAll('#choices button');
buttons.forEach(btn => {
btn.style.backgroundColor = '#28a745';
btn.style.color = 'white';
btn.disabled = false;
});
document.getElementById('feedback').innerText = '';
}
function updateScore() {
document.getElementById('score').innerText = `Score: ${score}/40`;
}
function storeQuestion(question, correctAnswer) {
askedQuestions.push({ question, correctAnswer });
}
function displayFinalScore() {
const finalScore = document.getElementById('final-score');
finalScore.innerText = `Your final score is: ${score}/40`;
const questionList = document.createElement('ol');
askedQuestions.forEach((q, index) => {
const listItem = document.createElement('li');
listItem.innerHTML = `<strong>Question ${index + 1}:</strong> ${q.question}<br><strong>Correct Answer:</strong> ${q.correctAnswer}`;
questionList.appendChild(listItem);
});
const questionListContainer = document.getElementById('question-list-container');
questionListContainer.innerHTML = ''; // Clear any previous content
questionListContainer.appendChild(questionList);
questionListContainer.classList.remove('hidden');
document.getElementById('leaderboard-button').classList.remove('hidden');
}
function fetchLeaderboard() {
fetch('http://127.0.0.1:8000/api/leaderboard')
.then(response => response.json())
.then(data => {
const leaderboardTableBody = document.getElementById('leaderboard-table').getElementsByTagName('tbody')[0];
leaderboardTableBody.innerHTML = ''; // Clear any previous content
data.forEach((entry, index) => {
const row = leaderboardTableBody.insertRow();
const cell1 = row.insertCell(0);
const cell2 = row.insertCell(1);
const cell3 = row.insertCell(2);
cell1.innerText = index + 1; // Rank
cell2.innerText = entry.username;
cell3.innerText = entry.score;
});
document.getElementById('leaderboard-container').classList.remove('hidden');
})
.catch(error => {
console.error('Error:', error);
});
}