WIP: Draft: Resolve "Zuruecksetz Funktion fuer Teilnehmer" #201

Closed
jvs wants to merge 2 commits from 44-zuruecksetz-funktion-fuer-teilnehmer into master
3 changed files with 36 additions and 43 deletions

View File

@@ -11,9 +11,6 @@ import json
# Create your views here. # Create your views here.
def lobby(request, join_code): def lobby(request, join_code):
quiz_game = get_object_or_404(QuizGame, join_code=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) quiz = get_object_or_404(QivipQuiz, pk=quiz_game.quiz_id.id)
context = { context = {
@@ -21,7 +18,6 @@ def lobby(request, join_code):
'quiz_game': quiz_game, 'quiz_game': quiz_game,
} }
mode = request.GET.get('mode','default') mode = request.GET.get('mode','default')
request.session['mode'] = mode request.session['mode'] = mode
if "host_id" in request.COOKIES: if "host_id" in request.COOKIES:
@@ -32,9 +28,6 @@ def lobby(request, join_code):
elif mode=="learn": elif mode=="learn":
return redirect('play:create_participant', join_code=join_code) return redirect('play:create_participant', join_code=join_code)
if not "participant_id" in request.COOKIES: if not "participant_id" in request.COOKIES:
return redirect('play:create_participant', join_code=join_code) return redirect('play:create_participant', join_code=join_code)
@@ -54,48 +47,46 @@ def lobby(request, join_code):
def select_mode(request,join_code): def select_mode(request,join_code):
return render(request, 'play/select_mode.html', {'join_code': join_code}) return render(request, 'play/select_mode.html', {'join_code': join_code})
def create_participant(request, join_code): def create_participant(request, join_code):
mode = request.GET.get('mode','default') mode = request.GET.get('mode','default')
request.session['mode'] = mode request.session['mode'] = mode
quiz_game = get_object_or_404(QuizGame, join_code=join_code) quiz_game = get_object_or_404(QuizGame, join_code=join_code)
if mode!="learn": if mode!="learn":
if "participant_id" in request.COOKIES: if request.method == 'GET' and "participant_id" in request.COOKIES:
# Prüfe ob der Teilnehmer noch existiert # Prüfe ob der Teilnehmer noch existiert
participant_id = request.COOKIES['participant_id'] participant_id = request.COOKIES['participant_id']
try: try:
participant = QuizGameParticipant.objects.get(participant_id=participant_id) participant = QuizGameParticipant.objects.get(participant_id=participant_id)
if participant.quiz_game.join_code != join_code: if participant.quiz_game.join_code != join_code:
participant.quiz_game = quiz_game participant.quiz_game = quiz_game
participant.score = 0 # Reset score when joining new game participant.score = 0 # Reset score when joining new game
participant.save() participant.save()
return redirect('play:lobby', join_code=join_code) return render(request, 'play/initialize_participant.html', {'participant': participant, 'join_code': quiz_game.join_code})
except QuizGameParticipant.DoesNotExist: except QuizGameParticipant.DoesNotExist:
# Cookie löschen wenn Teilnehmer nicht mehr existiert # Cookie löschen wenn Teilnehmer nicht mehr existiert
response = HttpResponseRedirect(request.path) response = HttpResponseRedirect(request.path)
response.delete_cookie('participant_id') 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 return response
if request.method == 'POST': return render(request, 'play/initialize_participant.html', {'join_code': join_code})
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})
else: else:
messages.error(request, "Beitritt nicht möglich.") messages.error(request, "Beitritt nicht möglich.")
return render(request, 'play/join_game.html') return render(request, 'play/join_game.html')

View File

@@ -24,7 +24,8 @@
required required
maxlength="20" 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" 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 }}">
<p class="text-xs text-gray-500 mt-2">Maximal 20 Zeichen</p> <p class="text-xs text-gray-500 mt-2">Maximal 20 Zeichen</p>
</div> </div>

View File

@@ -22,6 +22,7 @@
<span class="w-2 h-2 bg-green-500 rounded-full mr-2"></span> <span class="w-2 h-2 bg-green-500 rounded-full mr-2"></span>
<span>Angemeldet als <b>{{ participant.display_name }}</b></span> <span>Angemeldet als <b>{{ participant.display_name }}</b></span>
</div> </div>
<a href="{% url 'play:create_participant' quiz_game.join_code %}">Auftreten ändern...</a>
</div> </div>
{% endif %} {% endif %}