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 @@