Teilnehmlogik grundlegend fertiggestellt

This commit is contained in:
juhnsa
2025-03-15 20:30:49 +01:00
parent 7fe29c5bd4
commit 3ef7d61da6
6 changed files with 84 additions and 16 deletions

View File

@@ -1,21 +1,40 @@
from django.shortcuts import render
from django.shortcuts import render, redirect, get_object_or_404
from play.models import QuizGame
from django.shortcuts import render, redirect, get_object_or_404, reverse
from play.models import QuizGame, QuizGameParticipant
from library.models import QivipQuiz
from django.http import HttpResponse
from django.http import HttpResponseRedirect
# Create your views here.
def lobby(request, join_code):
quiz_game = get_object_or_404(QuizGame, join_code=join_code)
return render(request, 'play/lobby.html', {'debug': "test"})
if not "participant_id" in request.COOKIES:
return redirect('play:create_participant', join_code=join_code)
def create_participant(request):
join_code = request.GET.get('join_code')
if "participant_id" in request.COOKIES:
participant_id = request.COOKIES['participant_id']
quiz_game = get_object_or_404(QuizGame, join_code=join_code)
participant = get_object_or_404(QuizGameParticipant, participant_id=participant_id)
return render(request, 'play/lobby.html', {'debug': "test", 'participant': participant})
def create_participant(request, join_code):
if "participant_id" in request.COOKIES: # Umleiten, falls Teilnehmer bereits erstellt
return redirect('play:lobby', join_code=join_code)
if request.method == 'POST':
display_name = request.POST.get('username')
display_name = request.POST.get('display_name')
join_code = request.POST.get('join_code')
try:
quiz = QuizGame.objects.get(join_code=join_code)
participant = QuizGameParticipant()
participant.display_name = display_name
participant.quiz_game = quiz
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
except QuizGame.DoesNotExist:
# TODO: Fehlermeldung fuer nicht-existierendes Quiz
pass
return render(request, 'play/initialize_participant.html', {'join_code': join_code})
def join_game(request):