52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
from django.shortcuts import render
|
|
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 HttpResponseRedirect
|
|
|
|
# Create your views here.
|
|
def lobby(request, join_code):
|
|
if not "participant_id" in request.COOKIES:
|
|
return redirect('play:create_participant', join_code=join_code)
|
|
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)
|
|
|
|
if not participant.quiz_game.join_code == join_code:
|
|
participant.quiz_game = quiz_game
|
|
participant.save()
|
|
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('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):
|
|
if request.method == 'POST':
|
|
join_code = request.POST.get('game_code')
|
|
try:
|
|
quiz = QuizGame.objects.get(join_code=join_code)
|
|
return redirect('play:lobby', join_code=join_code)
|
|
except QuizGame.DoesNotExist:
|
|
# TODO: Mit message eine Fehlermeldung weitergeben (und im entsprechenden Template Designen)
|
|
pass
|
|
return render(request, 'play/join_game.html') |