From 5b4c76a1c86944d05953b8d28042f5e10f464c5a Mon Sep 17 00:00:00 2001 From: ben8 Date: Mon, 14 Apr 2025 20:23:47 +0200 Subject: [PATCH 1/8] Zwischenstand Lernmodus --- django/play/views.py | 7 +++ django/templates/play/game/question_host.html | 47 +++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/django/play/views.py b/django/play/views.py index 6887cc0..b332d9e 100644 --- a/django/play/views.py +++ b/django/play/views.py @@ -84,6 +84,13 @@ def create_game(request, quiz_id): game.quiz_id = quiz game.host_id = game.generate_unique_id() game.save() + + participant = QuizGameParticipant() + participant.display_name = "DU" + participant.participant_id=game.host_id + participant.quiz_game = game + participant.score = 0 # Initialize score + participant.save() response = HttpResponseRedirect(reverse('play:lobby', kwargs={'join_code': game.join_code})) response.set_cookie('host_id', game.host_id, max_age=3600) diff --git a/django/templates/play/game/question_host.html b/django/templates/play/game/question_host.html index 7a4f17c..1f8c4b0 100644 --- a/django/templates/play/game/question_host.html +++ b/django/templates/play/game/question_host.html @@ -17,14 +17,19 @@ -
+
{% for option in question_data.options %} -
+
+ {% endfor %}
+ + +
{% endblock %} @@ -125,5 +130,39 @@ })); updateTimer(); }; + + + let hasAnswered = false; + const participantId = '{{ host_id }}'; + + + 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 + })); + } {% endblock %} \ No newline at end of file From 9fb9ab9198c8520366fcfd453c99a76390a02378 Mon Sep 17 00:00:00 2001 From: ben8 Date: Mon, 14 Apr 2025 23:39:43 +0200 Subject: [PATCH 2/8] Lernfunktion --- django/play/urls.py | 1 + django/play/views.py | 74 +++++++++++++++---- django/templates/play/game/question_host.html | 25 ++++++- django/templates/play/lobby.html | 2 + django/templates/play/select_mode.html | 6 +- 5 files changed, 90 insertions(+), 18 deletions(-) diff --git a/django/play/urls.py b/django/play/urls.py index df8326c..261b472 100644 --- a/django/play/urls.py +++ b/django/play/urls.py @@ -13,4 +13,5 @@ urlpatterns = [ path('game//scores', views.scores, name='scores'), path('game//finished', views.finished, name='finished'), path('select_mode/', views.select_mode, name='select_mode'), + path('selected_mode/', views.selected_mode, name='selected_mode'), ] \ No newline at end of file diff --git a/django/play/views.py b/django/play/views.py index cd8992d..e28b3ac 100644 --- a/django/play/views.py +++ b/django/play/views.py @@ -1,3 +1,4 @@ +from django.utils import timezone from django.shortcuts import render from django.shortcuts import render, redirect, get_object_or_404, reverse from play.models import QuizGame, QuizGameParticipant @@ -7,8 +8,12 @@ from django.contrib import messages import json + # Create your views here. def lobby(request, join_code): + + + quiz_game = get_object_or_404(QuizGame, join_code=join_code) quiz = get_object_or_404(QivipQuiz, pk=quiz_game.quiz_id.id) context = { @@ -16,11 +21,18 @@ def lobby(request, join_code): 'quiz_game': quiz_game, } + + if "host_id" in request.COOKIES: host_id = request.COOKIES['host_id'] if host_id == quiz_game.host_id: context['host_id'] = host_id return render(request, 'play/lobby.html', context=context) + elif mode=="learn": + return redirect('play:create_participant', join_code=join_code) + + + if not "participant_id" in request.COOKIES: return redirect('play:create_participant', join_code=join_code) @@ -45,8 +57,8 @@ def select_mode(request,join_code): def create_participant(request, join_code): quiz_game = get_object_or_404(QuizGame, join_code=join_code) - - if "participant_id" in request.COOKIES: + if mode!="learn": + if "participant_id" in request.COOKIES: # Prüfe ob der Teilnehmer noch existiert participant_id = request.COOKIES['participant_id'] try: @@ -62,7 +74,7 @@ def create_participant(request, join_code): response.delete_cookie('participant_id') return response - if request.method == 'POST': + if request.method == 'POST': display_name = request.POST.get('display_name') if not display_name: return render(request, 'play/initialize_participant.html', { @@ -80,22 +92,19 @@ def create_participant(request, join_code): response.set_cookie('participant_id', participant.participant_id, max_age=3600) return response - return render(request, 'play/initialize_participant.html', {'join_code': join_code}) - + return render(request, 'play/initialize_participant.html', {'join_code': join_code}) + else: + messages.error(request, "Beitritt nicht möglich.") + return render(request, 'play/join_game.html') def create_game(request, quiz_id): + try: quiz = QivipQuiz.objects.get(id=quiz_id) game = QuizGame() game.quiz_id = quiz game.host_id = game.generate_unique_id() game.save() - - participant = QuizGameParticipant() - participant.display_name = "DU" - participant.participant_id=game.host_id - participant.quiz_game = game - participant.score = 0 # Initialize score - participant.save() + response = HttpResponseRedirect(reverse('play:select_mode', kwargs={'join_code': game.join_code})) response.set_cookie('host_id', game.host_id, max_age=3600) @@ -267,4 +276,43 @@ def waiting_room(request, join_code): return render(request, 'play/game/wait_for_other_players.html', { 'quiz_game': quiz_game, 'participant': participant - }) \ No newline at end of file + }) + + +def selected_mode(request, join_code): + + global mode + mode = request.GET.get('mode','default') + request.session['mode'] = mode + quiz_game = get_object_or_404(QuizGame, join_code=join_code) + quiz = get_object_or_404(QivipQuiz, pk=quiz_game.quiz_id.id) + QuizGameParticipant.objects.filter(quiz_game=quiz_game).delete() + + context = { + 'quiz': quiz, + 'quiz_game': quiz_game, + } + if "host_id" in request.COOKIES: + host_id = request.COOKIES['host_id'] + if host_id == quiz_game.host_id: + context['host_id'] = host_id + + if mode=="learn": + participant = QuizGameParticipant() + if request.user.is_authenticated: + participant.display_name = request.user.username + else: + participant.display_name = "SIE" + + participant.participant_id=host_id + participant.quiz_game = quiz_game + participant.score = 0 # Initialize score + participant.save() + quiz_game.current_state = "question" + quiz_game.question_start_time = timezone.now() + quiz_game.save() + + return redirect('play:question',join_code) + + + return render(request, 'play/lobby.html', context=context) diff --git a/django/templates/play/game/question_host.html b/django/templates/play/game/question_host.html index 1f8c4b0..a9a5732 100644 --- a/django/templates/play/game/question_host.html +++ b/django/templates/play/game/question_host.html @@ -1,7 +1,15 @@ {% extends 'base.html' %} {% block content %} + +{% if request.session.mode == "learn" %} +
+
+ +
{% endif %}

Frage {{ question_index|add:1 }} von {{ total_questions }}

{{ question_data.question }}

@@ -22,7 +30,8 @@ {% endfor %} @@ -133,8 +142,20 @@ let hasAnswered = false; - const participantId = '{{ host_id }}'; + 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; diff --git a/django/templates/play/lobby.html b/django/templates/play/lobby.html index 1731e6b..890cb39 100644 --- a/django/templates/play/lobby.html +++ b/django/templates/play/lobby.html @@ -7,11 +7,13 @@

{{ quiz.name }}

+ {% if request.session.mode != "learn" %}

{{ quiz_game.join_code }}

Spiel-Code

+ {% endif %} {% if participant %} diff --git a/django/templates/play/select_mode.html b/django/templates/play/select_mode.html index 0737422..e7b3c1d 100644 --- a/django/templates/play/select_mode.html +++ b/django/templates/play/select_mode.html @@ -9,7 +9,7 @@
- + @@ -22,7 +22,7 @@ - + @@ -35,7 +35,7 @@ - + From e22ea409107dac4808c49a1882adf734ddee197d Mon Sep 17 00:00:00 2001 From: ben8 Date: Fri, 18 Apr 2025 12:48:35 +0200 Subject: [PATCH 3/8] Lernmodus verbessert und Testmodus entfernt --- django/play/views.py | 21 ++++++++++++--- django/templates/play/game/finished.html | 2 +- django/templates/play/game/question_host.html | 25 ++++++++++++----- .../templates/play/game/score_overview.html | 27 +++++++++++++------ django/templates/play/select_mode.html | 14 +--------- 5 files changed, 57 insertions(+), 32 deletions(-) diff --git a/django/play/views.py b/django/play/views.py index e28b3ac..296ed6f 100644 --- a/django/play/views.py +++ b/django/play/views.py @@ -22,7 +22,8 @@ def lobby(request, join_code): } - + mode = request.GET.get('mode','default') + request.session['mode'] = mode if "host_id" in request.COOKIES: host_id = request.COOKIES['host_id'] if host_id == quiz_game.host_id: @@ -56,6 +57,8 @@ def select_mode(request,join_code): def create_participant(request, join_code): + mode = request.GET.get('mode','default') + request.session['mode'] = mode quiz_game = get_object_or_404(QuizGame, join_code=join_code) if mode!="learn": if "participant_id" in request.COOKIES: @@ -149,7 +152,7 @@ def finished(request, join_code): def scores(request, join_code): quiz_game = get_object_or_404(QuizGame, join_code=join_code) - + # Verify game state if quiz_game.current_state != 'scores': return redirect('play:lobby', join_code=join_code) @@ -167,9 +170,18 @@ def scores(request, join_code): current_question = questions[quiz_game.current_question_index] question_data = json.loads(current_question.data) + try: + my_participant = request.session.get('my_participant-participant_id') + participant = QuizGameParticipant.objects.get(participant_id=my_participant) + my_participant=participant + except: + participant = QuizGameParticipant.objects.none() + return render(request, 'play/game/score_overview.html', { 'quiz_game': quiz_game, 'participants': participants, + 'participant': participant, + 'my_participant':my_participant, 'is_host': is_host, 'host_id': quiz_game.host_id if is_host else None, 'is_last_question': quiz_game.current_question_index + 1 >= len(questions), @@ -244,8 +256,11 @@ def question(request, join_code): participant_id = request.COOKIES['participant_id'] try: participant = QuizGameParticipant.objects.get(participant_id=participant_id) + + except QuizGameParticipant.DoesNotExist: return redirect('play:create_participant', join_code=join_code) + request.session['my_participant-participant_id'] = participant.participant_id return render(request, 'play/game/question_participant.html', { 'quiz_game': quiz_game, @@ -281,7 +296,7 @@ def waiting_room(request, join_code): def selected_mode(request, join_code): - global mode + mode = request.GET.get('mode','default') request.session['mode'] = mode quiz_game = get_object_or_404(QuizGame, join_code=join_code) diff --git a/django/templates/play/game/finished.html b/django/templates/play/game/finished.html index b0300e1..9290126 100644 --- a/django/templates/play/game/finished.html +++ b/django/templates/play/game/finished.html @@ -7,7 +7,7 @@

Quiz beendet!

-

Vielen Dank fürs Mitspielen!

+

Vielen Dank fürs Spielen!

{% if not is_host %} diff --git a/django/templates/play/game/question_host.html b/django/templates/play/game/question_host.html index a9a5732..82b2a9b 100644 --- a/django/templates/play/game/question_host.html +++ b/django/templates/play/game/question_host.html @@ -1,5 +1,4 @@ {% extends 'base.html' %} - {% block content %} {% if request.session.mode == "learn" %} @@ -13,12 +12,18 @@

Frage {{ question_index|add:1 }} von {{ total_questions }}

{{ question_data.question }}

- -
+ {% if request.session.mode == "learn" %} +
+ {% else %} +
+ {% endif %} + {% if request.session.mode != "learn" %}
-

Verbleibende Zeit

+

Verbleibende Zeit

30

+ {% endif %} +

Antworten

0/0

@@ -43,18 +48,20 @@
{% endblock %} + + {% block extra_js %} {% include 'play/game/common_scripts.html' %} {% endblock %} \ No newline at end of file diff --git a/django/templates/play/game/score_overview.html b/django/templates/play/game/score_overview.html index e8960d0..ae201ac 100644 --- a/django/templates/play/game/score_overview.html +++ b/django/templates/play/game/score_overview.html @@ -10,43 +10,33 @@

{{ question_data.question }}

{% for option in question_data.options %} -
+

{{ option.value }}

0 Antworten

{% endfor %}
- {% if request.session.mode != "learn" %} - +

Punktestand

{% for participant in participants %} - {% if participant == my_participant or is_host %} -
-
-

{{ participant.display_name }}

-

{{ participant.score }} Punkte

-
+
+

{{ participant.display_name }}

+

{{ participant.score }} Punkte

+
{% if participant.last_answer_correct %} {% elif participant.last_answer_correct == False %} {% endif %}
- {% else %} - - - {% endif %} - - - {% endfor %}
- {% endif %} + {% if is_host %} {% if is_last_question %}
+
{% endif %}

Frage {{ question_index|add:1 }} von {{ total_questions }}

{{ question_data.question }}

- -
+ {% if request.session.mode == "learn" %} +
+ {% else %} +
+ {% endif %} + {% if request.session.mode != "learn" %}
-

Verbleibende Zeit

+

Verbleibende Zeit

30

+ {% endif %} +

Antworten

0/0

-
+
{% for option in question_data.options %} -
+
+ {% endfor %}
+ + +
{% endblock %} + + {% block extra_js %} {% include 'play/game/common_scripts.html' %} {% endblock %} \ No newline at end of file diff --git a/django/templates/play/game/score_overview.html b/django/templates/play/game/score_overview.html index ae201ac..e8960d0 100644 --- a/django/templates/play/game/score_overview.html +++ b/django/templates/play/game/score_overview.html @@ -10,33 +10,43 @@

{{ question_data.question }}

{% for option in question_data.options %} -
+

{{ option.value }}

0 Antworten

{% endfor %}
- + {% if request.session.mode != "learn" %} +

Punktestand

{% for participant in participants %} + {% if participant == my_participant or is_host %} +
-
-

{{ participant.display_name }}

-

{{ participant.score }} Punkte

-
+
+

{{ participant.display_name }}

+

{{ participant.score }} Punkte

+
{% if participant.last_answer_correct %} {% elif participant.last_answer_correct == False %} {% endif %}
+ {% else %} + + + {% endif %} + + + {% endfor %}
- + {% endif %} {% if is_host %} {% if is_last_question %}