v3
Browse files- app.py +11 -3
- static/script.js +20 -6
- templates/client.html +13 -0
- templates/host.html +2 -1
app.py
CHANGED
@@ -85,6 +85,8 @@ def check_answers():
|
|
85 |
if current_question['answers'].get(participant["username"]) == correct_answer:
|
86 |
participants[sid]["score"] += 1
|
87 |
|
|
|
|
|
88 |
@socketio.on('next_question')
|
89 |
def next_question():
|
90 |
current_question['index'] += 1
|
@@ -95,12 +97,18 @@ def next_question():
|
|
95 |
emit('new_question', question, room='quiz')
|
96 |
else:
|
97 |
final_results = calculate_final_results()
|
98 |
-
emit('
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
@socketio.on('restart_quiz')
|
101 |
def restart_quiz():
|
102 |
reset_quiz()
|
103 |
emit('quiz_reset', room='quiz')
|
|
|
104 |
|
105 |
def generate_chart(answers, options):
|
106 |
counts = [list(answers.values()).count(option) for option in options]
|
@@ -118,8 +126,8 @@ def generate_chart(answers, options):
|
|
118 |
return chart_base64
|
119 |
|
120 |
def calculate_final_results():
|
121 |
-
|
122 |
-
return
|
123 |
|
124 |
def reset_quiz():
|
125 |
global questions, current_question
|
|
|
85 |
if current_question['answers'].get(participant["username"]) == correct_answer:
|
86 |
participants[sid]["score"] += 1
|
87 |
|
88 |
+
emit('enable_end_quiz', room='quiz') # Enable the "End Quiz" button
|
89 |
+
|
90 |
@socketio.on('next_question')
|
91 |
def next_question():
|
92 |
current_question['index'] += 1
|
|
|
97 |
emit('new_question', question, room='quiz')
|
98 |
else:
|
99 |
final_results = calculate_final_results()
|
100 |
+
emit('display_final_results', final_results, room='quiz')
|
101 |
+
|
102 |
+
@socketio.on('end_quiz')
|
103 |
+
def end_quiz():
|
104 |
+
final_results = calculate_final_results()
|
105 |
+
emit('display_final_results', final_results, room='quiz')
|
106 |
|
107 |
@socketio.on('restart_quiz')
|
108 |
def restart_quiz():
|
109 |
reset_quiz()
|
110 |
emit('quiz_reset', room='quiz')
|
111 |
+
start_quiz() # Automatically start the quiz again after reset
|
112 |
|
113 |
def generate_chart(answers, options):
|
114 |
counts = [list(answers.values()).count(option) for option in options]
|
|
|
126 |
return chart_base64
|
127 |
|
128 |
def calculate_final_results():
|
129 |
+
sorted_scores = sorted(participants.values(), key=lambda x: x['score'], reverse=True)
|
130 |
+
return [{"username": p["username"], "score": p["score"]} for p in sorted_scores]
|
131 |
|
132 |
def reset_quiz():
|
133 |
global questions, current_question
|
static/script.js
CHANGED
@@ -41,6 +41,10 @@ function nextQuestion() {
|
|
41 |
socket.emit('next_question');
|
42 |
}
|
43 |
|
|
|
|
|
|
|
|
|
44 |
function restartQuiz() {
|
45 |
socket.emit('restart_quiz');
|
46 |
}
|
@@ -51,20 +55,30 @@ socket.on('display_results', (data) => {
|
|
51 |
document.getElementById('results').innerHTML = img + resultText;
|
52 |
});
|
53 |
|
|
|
|
|
|
|
|
|
54 |
socket.on('clear_results', () => {
|
55 |
document.getElementById('results').innerHTML = '';
|
56 |
});
|
57 |
|
58 |
-
socket.on('
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
64 |
});
|
65 |
|
66 |
socket.on('quiz_reset', () => {
|
67 |
document.getElementById('results').innerHTML = '';
|
68 |
document.getElementById('question-text').innerText = '';
|
69 |
document.getElementById('options').innerHTML = '';
|
|
|
|
|
|
|
70 |
});
|
|
|
41 |
socket.emit('next_question');
|
42 |
}
|
43 |
|
44 |
+
function endQuiz() {
|
45 |
+
socket.emit('end_quiz');
|
46 |
+
}
|
47 |
+
|
48 |
function restartQuiz() {
|
49 |
socket.emit('restart_quiz');
|
50 |
}
|
|
|
55 |
document.getElementById('results').innerHTML = img + resultText;
|
56 |
});
|
57 |
|
58 |
+
socket.on('enable_end_quiz', () => {
|
59 |
+
document.getElementById('end-quiz').disabled = false;
|
60 |
+
});
|
61 |
+
|
62 |
socket.on('clear_results', () => {
|
63 |
document.getElementById('results').innerHTML = '';
|
64 |
});
|
65 |
|
66 |
+
socket.on('display_final_results', (finalResults) => {
|
67 |
+
document.getElementById('quiz-content').style.display = 'none';
|
68 |
+
const resultsTable = document.getElementById('results-table');
|
69 |
+
resultsTable.innerHTML = '';
|
70 |
+
finalResults.forEach((participant) => {
|
71 |
+
const row = `<tr><td>${participant.username}</td><td>${participant.score}</td></tr>`;
|
72 |
+
resultsTable.innerHTML += row;
|
73 |
+
});
|
74 |
+
document.getElementById('final-results').style.display = 'block';
|
75 |
});
|
76 |
|
77 |
socket.on('quiz_reset', () => {
|
78 |
document.getElementById('results').innerHTML = '';
|
79 |
document.getElementById('question-text').innerText = '';
|
80 |
document.getElementById('options').innerHTML = '';
|
81 |
+
document.getElementById('final-results').style.display = 'none';
|
82 |
+
document.getElementById('quiz-content').style.display = 'block';
|
83 |
+
document.getElementById('waiting-message').style.display = 'block';
|
84 |
});
|
templates/client.html
CHANGED
@@ -18,6 +18,19 @@
|
|
18 |
<div id="options"></div>
|
19 |
<div id="results" class="mt-4"></div>
|
20 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
</div>
|
22 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.0/socket.io.js"></script>
|
23 |
<script src="/static/script.js"></script>
|
|
|
18 |
<div id="options"></div>
|
19 |
<div id="results" class="mt-4"></div>
|
20 |
</div>
|
21 |
+
<div id="final-results" style="display: none;" class="mt-4">
|
22 |
+
<h3>And the Winners are:</h3>
|
23 |
+
<table class="table mt-2">
|
24 |
+
<thead>
|
25 |
+
<tr>
|
26 |
+
<th>Participant</th>
|
27 |
+
<th>Score</th>
|
28 |
+
</tr>
|
29 |
+
</thead>
|
30 |
+
<tbody id="results-table">
|
31 |
+
</tbody>
|
32 |
+
</table>
|
33 |
+
</div>
|
34 |
</div>
|
35 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.0/socket.io.js"></script>
|
36 |
<script src="/static/script.js"></script>
|
templates/host.html
CHANGED
@@ -13,7 +13,8 @@
|
|
13 |
<button onclick="startQuiz()" class="btn btn-success">Start Quiz</button>
|
14 |
<button onclick="checkAnswers()" class="btn btn-primary mt-2">Check Answers</button>
|
15 |
<button onclick="nextQuestion()" class="btn btn-secondary mt-2">Next Question</button>
|
16 |
-
<button onclick="
|
|
|
17 |
<h3 id="question-text" class="mt-4"></h3>
|
18 |
<div id="options" class="mt-2"></div>
|
19 |
<div id="results" class="mt-4"></div>
|
|
|
13 |
<button onclick="startQuiz()" class="btn btn-success">Start Quiz</button>
|
14 |
<button onclick="checkAnswers()" class="btn btn-primary mt-2">Check Answers</button>
|
15 |
<button onclick="nextQuestion()" class="btn btn-secondary mt-2">Next Question</button>
|
16 |
+
<button onclick="endQuiz()" id="end-quiz" class="btn btn-danger mt-2" disabled>End Quiz</button>
|
17 |
+
<button onclick="restartQuiz()" class="btn btn-warning mt-2">Start New Quiz</button>
|
18 |
<h3 id="question-text" class="mt-4"></h3>
|
19 |
<div id="options" class="mt-2"></div>
|
20 |
<div id="results" class="mt-4"></div>
|