diff --git a/django/play/views.py b/django/play/views.py index c2dbc3a..4bbd671 100644 --- a/django/play/views.py +++ b/django/play/views.py @@ -11,9 +11,6 @@ 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 = { @@ -21,7 +18,6 @@ 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: @@ -32,9 +28,6 @@ def lobby(request, join_code): 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) @@ -54,48 +47,46 @@ def lobby(request, join_code): def select_mode(request,join_code): return render(request, 'play/select_mode.html', {'join_code': 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: - # Prüfe ob der Teilnehmer noch existiert - participant_id = request.COOKIES['participant_id'] - try: - participant = QuizGameParticipant.objects.get(participant_id=participant_id) - if participant.quiz_game.join_code != join_code: - participant.quiz_game = quiz_game - participant.score = 0 # Reset score when joining new game - participant.save() - return redirect('play:lobby', join_code=join_code) - except QuizGameParticipant.DoesNotExist: - # Cookie löschen wenn Teilnehmer nicht mehr existiert - response = HttpResponseRedirect(request.path) - response.delete_cookie('participant_id') + if request.method == 'GET' and "participant_id" in request.COOKIES: + # Prüfe ob der Teilnehmer noch existiert + participant_id = request.COOKIES['participant_id'] + try: + participant = QuizGameParticipant.objects.get(participant_id=participant_id) + if participant.quiz_game.join_code != join_code: + participant.quiz_game = quiz_game + participant.score = 0 # Reset score when joining new game + participant.save() + return render(request, 'play/initialize_participant.html', {'participant': participant, 'join_code': quiz_game.join_code}) + except QuizGameParticipant.DoesNotExist: + # Cookie löschen wenn Teilnehmer nicht mehr existiert + response = HttpResponseRedirect(request.path) + response.delete_cookie('participant_id') + return response + + if request.method == 'POST': + display_name = request.POST.get('display_name') + if not display_name: + return render(request, 'play/initialize_participant.html', { + 'join_code': join_code, + 'error': 'Bitte geben Sie einen Namen ein' + }) + + participant = QuizGameParticipant() + participant.display_name = display_name + participant.quiz_game = quiz_game + participant.score = 0 # Initialize score + participant.save() + + response = HttpResponseRedirect(reverse('play:lobby', kwargs={'join_code': join_code})) + response.set_cookie('participant_id', participant.participant_id, max_age=3600) return response - if request.method == 'POST': - display_name = request.POST.get('display_name') - if not display_name: - return render(request, 'play/initialize_participant.html', { - 'join_code': join_code, - 'error': 'Bitte geben Sie einen Namen ein' - }) - - participant = QuizGameParticipant() - participant.display_name = display_name - participant.quiz_game = quiz_game - participant.score = 0 # Initialize score - participant.save() - - response = HttpResponseRedirect(reverse('play:lobby', kwargs={'join_code': 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') diff --git a/django/templates/play/initialize_participant.html b/django/templates/play/initialize_participant.html index 9e92f51..082aaf6 100644 --- a/django/templates/play/initialize_participant.html +++ b/django/templates/play/initialize_participant.html @@ -24,7 +24,8 @@ required maxlength="20" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" - placeholder="Dein Spielername"> + placeholder="Dein Spielername" + value="{{ participant.display_name }}">
Maximal 20 Zeichen
diff --git a/django/templates/play/lobby.html b/django/templates/play/lobby.html index 890cb39..9265435 100644 --- a/django/templates/play/lobby.html +++ b/django/templates/play/lobby.html @@ -22,6 +22,7 @@ Angemeldet als {{ participant.display_name }} + Auftreten ändern... {% endif %}