File size: 2,801 Bytes
cdfb731 0bcd8e4 cdfb731 0bcd8e4 19abad4 cdfb731 0bcd8e4 cdfb731 0bcd8e4 cdfb731 0bcd8e4 cdfb731 0bcd8e4 cdfb731 98960e8 0bcd8e4 98960e8 0bcd8e4 98960e8 cdfb731 0bcd8e4 98960e8 cdfb731 |
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 |
const socket = io();
let username;
function joinQuiz() {
username = document.getElementById('username').value;
socket.emit('join', { username: username });
document.getElementById('username').style.display = 'none';
document.querySelector('button').style.display = 'none';
document.getElementById('logged-user').textContent = username;
document.getElementById('quiz-content').style.display = 'block';
document.getElementById('waiting-message').style.display = 'block';
document.getElementById('join-title').style.display = 'none';
}
socket.on('update_participants', (data) => {
document.getElementById('participant-count').textContent = data.count;
});
socket.on('new_question', (data) => {
document.getElementById('waiting-message').style.display = 'none';
document.getElementById('question-text').innerText = data.question;
const options = data.options.map((opt) =>
`<button onclick="submitAnswer('${opt}')" class="btn btn-secondary">${opt}</button>`
).join('');
document.getElementById('options').innerHTML = options;
});
function submitAnswer(answer) {
socket.emit('submit_answer', { answer: answer });
}
function checkAnswers() {
socket.emit('check_answers');
}
function nextQuestion() {
socket.emit('next_question');
}
function endQuiz() {
socket.emit('end_quiz');
}
function restartQuiz() {
socket.emit('restart_quiz');
}
socket.on('display_results', (data) => {
const img = `<img src="data:image/png;base64,${data.chart}" alt="Results Chart" />`;
const resultText = `<p>Correct Answer: ${data.results.correct_answer}</p>`;
document.getElementById('results').innerHTML = img + resultText;
});
socket.on('enable_end_quiz', () => {
document.getElementById('end-quiz').disabled = false;
});
socket.on('clear_results', () => {
document.getElementById('results').innerHTML = '';
});
socket.on('display_final_results', (finalResults) => {
document.getElementById('quiz-content').style.display = 'none';
const resultsTable = document.getElementById('results-table');
resultsTable.innerHTML = '';
finalResults.forEach((participant) => {
const row = `<tr><td>${participant.username}</td><td>${participant.score}</td></tr>`;
resultsTable.innerHTML += row;
});
document.getElementById('final-results').style.display = 'block';
});
socket.on('quiz_reset', () => {
document.getElementById('results').innerHTML = '';
document.getElementById('question-text').innerText = '';
document.getElementById('options').innerHTML = '';
document.getElementById('final-results').style.display = 'none';
document.getElementById('quiz-content').style.display = 'block';
document.getElementById('waiting-message').style.display = 'block';
});
|