189 lines
6.5 KiB
HTML
189 lines
6.5 KiB
HTML
{% extends 'base.html' %}
|
|
|
|
{% block content %}
|
|
|
|
{% if request.session.mode == "learn" %}
|
|
|
|
<div class="container mx-auto px-4">
|
|
<div class="flex justify-end mb-4">
|
|
<button onclick="leaveGame()" class="bg-red-500 hover:bg-red-600 text-white font-bold py-2 px-4 rounded">
|
|
Spiel verlassen
|
|
</button>
|
|
</div>{% endif %}
|
|
<div class="bg-white rounded-lg shadow-md p-6 mb-6">
|
|
<p class="text-gray-700 font-bold text-xl mb-4">Frage {{ question_index|add:1 }} von {{ total_questions }}</p>
|
|
<h1 class="text-3xl font-bold mb-6">{{ question_data.question }}</h1>
|
|
|
|
<div class="grid grid-cols-2 gap-4 mb-6">
|
|
<div class="p-4 bg-blue-50 rounded-lg">
|
|
<p class="text-blue-600 text-xl mb-2">Verbleibende Zeit</p>
|
|
<p id="remaining-time" class="text-6xl font-bold text-blue-700">30</p>
|
|
</div>
|
|
<div class="p-4 bg-blue-50 rounded-lg">
|
|
<p class="text-blue-600 text-xl mb-2">Antworten</p>
|
|
<p id="answer-count" class="text-6xl font-bold text-blue-700">0/0</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="grid grid-cols-2 gap-4" id="options-container">
|
|
{% for option in question_data.options %}
|
|
<button
|
|
class="p-4 bg-gray-100 hover:bg-blue-100 rounded-lg transition-colors duration-200 answer-option"
|
|
data-index="{{ forloop.counter0 }}"
|
|
{% if request.session.mode == "learn" %}
|
|
onclick="submitAnswer({{ forloop.counter0 }});" {% endif %}>
|
|
<p class="text-lg">{{ option.value }}</p>
|
|
</button>
|
|
{% endfor %}
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
|
|
{% block extra_js %}
|
|
{% include 'play/game/common_scripts.html' %}
|
|
<script>
|
|
const joinCode = '{{ quiz_game.join_code }}';
|
|
const hostId = '{{ host_id }}';
|
|
const questionStartTime = {{ start_time }};
|
|
const questionDuration = 30000; // 30 seconds in milliseconds
|
|
const gameSocket = initializeGameSocket(joinCode);
|
|
let answeredParticipants = 0;
|
|
let totalParticipants = 0;
|
|
const optionCounts = {};
|
|
|
|
gameSocket.onmessage = function(e) {
|
|
const data = JSON.parse(e.data);
|
|
if (data.type === 'participant_answer') {
|
|
answeredParticipants++;
|
|
updateAnswerCount();
|
|
updateOptionCount(data.answer);
|
|
} else if (data.type === 'participant_list_update') {
|
|
totalParticipants = data.participants.length;
|
|
updateAnswerCount();
|
|
} else if (data.type === 'game_state_update' &&
|
|
data.action === 'show_scores' &&
|
|
data.redirect_url) {
|
|
window.location.href = data.redirect_url;
|
|
}
|
|
};
|
|
|
|
gameSocket.onclose = function(e) {
|
|
wsUtil.handleClose(e);
|
|
};
|
|
|
|
function updateAnswerCount() {
|
|
const answerCountElement = document.getElementById('answer-count');
|
|
if (answerCountElement) {
|
|
answerCountElement.textContent = `${answeredParticipants}/${totalParticipants}`;
|
|
}
|
|
|
|
// If everyone has answered, advance to scores
|
|
if (answeredParticipants === totalParticipants && totalParticipants > 0) {
|
|
advanceToScores();
|
|
}
|
|
}
|
|
|
|
function updateOptionCount(optionIndex) {
|
|
optionCounts[optionIndex] = (optionCounts[optionIndex] || 0) + 1;
|
|
const optionElement = document.getElementById(`option-count-${optionIndex}`);
|
|
if (optionElement) {
|
|
optionElement.textContent = `${optionCounts[optionIndex]} Antworten`;
|
|
}
|
|
}
|
|
|
|
function advanceToScores() {
|
|
// Show correct answers and answer counts
|
|
var correctOptions = document.querySelectorAll('[data-correct="True"]');
|
|
for (var i = 0; i < correctOptions.length; i++) {
|
|
correctOptions[i].classList.add('border-2', 'border-green-500');
|
|
}
|
|
|
|
// Show answer counts
|
|
var countElements = document.querySelectorAll('[id^="option-count-"]');
|
|
for (var j = 0; j < countElements.length; j++) {
|
|
countElements[j].classList.remove('hidden');
|
|
}
|
|
|
|
// Wait a moment to show the correct answer and counts
|
|
setTimeout(function() {
|
|
gameSocket.send(JSON.stringify({
|
|
'type': 'advance_to_scores',
|
|
'host_id': hostId
|
|
}));
|
|
}, 2000);
|
|
}
|
|
|
|
// Timer
|
|
function updateTimer() {
|
|
const now = Date.now();
|
|
const elapsed = now - questionStartTime;
|
|
const remaining = Math.max(0, Math.ceil((questionDuration - elapsed) / 1000));
|
|
|
|
document.getElementById('remaining-time').textContent = remaining;
|
|
|
|
if (remaining === 0) {
|
|
advanceToScores();
|
|
} else {
|
|
requestAnimationFrame(updateTimer);
|
|
}
|
|
}
|
|
|
|
// Request initial participants list
|
|
gameSocket.onopen = function(e) {
|
|
gameSocket.send(JSON.stringify({
|
|
'type': 'update_participants'
|
|
}));
|
|
updateTimer();
|
|
};
|
|
|
|
|
|
let hasAnswered = false;
|
|
const participantId = '{{ host_id }}'; // HIER IST DER HOST DER SPIELER
|
|
|
|
function leaveGame() {
|
|
if (confirm('Möchtest du das Spiel wirklich verlassen?')) {
|
|
gameSocket.send(JSON.stringify({
|
|
type: 'leave_game',
|
|
participant_id: participantId
|
|
}));
|
|
window.location.href = '/';
|
|
gameSocket.close();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function submitAnswer(optionIndex) {
|
|
if (hasAnswered) return;
|
|
|
|
hasAnswered = true;
|
|
const now = Date.now();
|
|
const timeRemaining = Math.max(0, questionDuration - (now - questionStartTime));
|
|
|
|
// Disable all options and highlight selected
|
|
const options = document.getElementsByClassName('answer-option');
|
|
for (let option of options) {
|
|
option.disabled = true;
|
|
option.classList.remove('hover:bg-blue-100');
|
|
if (parseInt(option.dataset.index) === optionIndex) {
|
|
option.classList.remove('bg-gray-100');
|
|
option.classList.add('bg-blue-600', 'text-white');
|
|
} else {
|
|
option.classList.add('opacity-50');
|
|
}
|
|
}
|
|
|
|
// Send answer to server
|
|
gameSocket.send(JSON.stringify({
|
|
type: 'submit_answer',
|
|
participant_id: participantId,
|
|
answer: optionIndex,
|
|
time_remaining: timeRemaining
|
|
}));
|
|
}
|
|
</script>
|
|
{% endblock %} |