From bcdfebf7915928339f2b07bf2d161114d1f41588 Mon Sep 17 00:00:00 2001 From: ben8 Date: Fri, 18 Apr 2025 14:00:29 +0000 Subject: [PATCH 1/2] Merge branch '103-verschiedene-lernmodi' into 'master' Resolve "verschiedene Lernmodi" Closes #103 See merge request enrichment-2024/qivip!47 (cherry picked from commit d4252c1297b162c9adda3af50c85eb72ca182aab) 5b4c76a1 Zwischenstand Lernmodus 4209dcbc Merge branch 'master' into 103-verschiedene-lernmodi 9fb9ab91 Lernfunktion e22ea409 Lernmodus verbessert und Testmodus entfernt 2052a702 Merge branch 'master' into '103-verschiedene-lernmodi' Co-authored-by: ben8 --- django/play/urls.py | 1 + django/play/views.py | 84 +++++++++++++++-- django/templates/play/game/finished.html | 2 +- django/templates/play/game/question_host.html | 93 ++++++++++++++++--- .../templates/play/game/score_overview.html | 27 ++++-- django/templates/play/lobby.html | 2 + django/templates/play/select_mode.html | 18 +--- 7 files changed, 185 insertions(+), 42 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 173bbeb..ab6488e 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,19 @@ def lobby(request, join_code): 'quiz_game': quiz_game, } + + 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: 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) @@ -44,9 +57,11 @@ 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 "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 +77,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,9 +95,12 @@ 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() @@ -90,6 +108,7 @@ def create_game(request, quiz_id): game.host_id = game.generate_unique_id() game.save() + response = HttpResponseRedirect(reverse('play:select_mode', kwargs={'join_code': game.join_code})) response.set_cookie('host_id', game.host_id, max_age=3600) @@ -133,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) @@ -151,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), @@ -229,8 +257,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, @@ -262,4 +293,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): + + + 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/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 84f1d7f..82b2a9b 100644 --- a/django/templates/play/game/question_host.html +++ b/django/templates/play/game/question_host.html @@ -1,46 +1,67 @@ {% extends 'base.html' %} - {% block content %} + +{% if request.session.mode == "learn" %} +
+
+ +
{% 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 %}