From 6beccc772a3168370b742e6fe0137e46d228db23 Mon Sep 17 00:00:00 2001 From: Henrik Mildner Date: Sat, 15 Mar 2025 15:50:41 +0100 Subject: [PATCH 1/7] Add initialize_participant.html --- core/templates/play/ initialize_participant.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 core/templates/play/ initialize_participant.html diff --git a/core/templates/play/ initialize_participant.html b/core/templates/play/ initialize_participant.html new file mode 100644 index 0000000..b5f88b7 --- /dev/null +++ b/core/templates/play/ initialize_participant.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} + +{% block content %} +

User for a Game:

+
+
+ {% csrf_token %} + {{ form.as_p }} + +
+
+{% endblock %} \ No newline at end of file From 34a9ab6a43ff378a78df3bfc234226e26590907e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhn-Vitus=20Sa=C3=9F?= Date: Sat, 15 Mar 2025 16:10:34 +0100 Subject: [PATCH 2/7] View erstellen --- core/play/urls.py | 1 + core/play/views.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/core/play/urls.py b/core/play/urls.py index 72f8362..444ac54 100644 --- a/core/play/urls.py +++ b/core/play/urls.py @@ -6,4 +6,5 @@ app_name = 'play' urlpatterns = [ path('lobby/', views.lobby, name='lobby'), path('join', views.join_game, name='join_game'), + path('participant', views.create_participant, name='create_participant'), ] \ No newline at end of file diff --git a/core/play/views.py b/core/play/views.py index 69e57fc..055a1ad 100644 --- a/core/play/views.py +++ b/core/play/views.py @@ -2,12 +2,22 @@ from django.shortcuts import render from django.shortcuts import render, redirect, get_object_or_404 from play.models import QuizGame from library.models import QivipQuiz +from django.http import HttpResponse # 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"}) +def create_participant(request): + join_code = request.GET.get('join_code') + if "participant_id" in request.COOKIES: + return redirect('play:lobby', join_code=join_code) + if request.method == 'POST': + display_name = request.POST.get('username') + join_code = request.POST.get('join_code') + return render('play/initialize_participant.html', {'join_code': join_code}) + def join_game(request): if request.method == 'POST': join_code = request.POST.get('game_code') From 97fd3265615064df1051706688fd31ef238ae1a6 Mon Sep 17 00:00:00 2001 From: Henrik Mildner Date: Sat, 15 Mar 2025 16:11:35 +0100 Subject: [PATCH 3/7] Create initialize_participant.html --- core/templates/play/ initialize_participant.html | 1 - 1 file changed, 1 deletion(-) diff --git a/core/templates/play/ initialize_participant.html b/core/templates/play/ initialize_participant.html index b5f88b7..b27e5da 100644 --- a/core/templates/play/ initialize_participant.html +++ b/core/templates/play/ initialize_participant.html @@ -5,7 +5,6 @@
{% csrf_token %} - {{ form.as_p }}
From 57682d4607afe7bb122f27c5bde07a4b77d9a4a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhn-Vitus=20Sa=C3=9F?= Date: Sat, 15 Mar 2025 16:30:09 +0100 Subject: [PATCH 4/7] Fix routing issue --- core/play/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/play/views.py b/core/play/views.py index 055a1ad..b0c7e53 100644 --- a/core/play/views.py +++ b/core/play/views.py @@ -16,7 +16,7 @@ def create_participant(request): if request.method == 'POST': display_name = request.POST.get('username') join_code = request.POST.get('join_code') - return render('play/initialize_participant.html', {'join_code': join_code}) + return render(request, 'play/initialize_participant.html', {'join_code': join_code}) def join_game(request): if request.method == 'POST': From 5775e53bb85310557e2c4dbe6c7f482777ee4c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhn-Vitus=20Sa=C3=9F?= Date: Sat, 15 Mar 2025 16:30:31 +0100 Subject: [PATCH 5/7] Fix template name --- ...{ initialize_participant.html => initialize_participant.html} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core/templates/play/{ initialize_participant.html => initialize_participant.html} (100%) diff --git a/core/templates/play/ initialize_participant.html b/core/templates/play/initialize_participant.html similarity index 100% rename from core/templates/play/ initialize_participant.html rename to core/templates/play/initialize_participant.html From 7fe29c5bd4447af91608287ae3a84e5a07020ba5 Mon Sep 17 00:00:00 2001 From: juhnsa Date: Sat, 15 Mar 2025 19:41:13 +0100 Subject: [PATCH 6/7] Warnmeldung beheben --- core/play/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/play/models.py b/core/play/models.py index b4e16f0..0053f09 100644 --- a/core/play/models.py +++ b/core/play/models.py @@ -23,5 +23,5 @@ class QuizGame(models.Model): class QuizGameParticipant(models.Model): display_name = models.CharField(verbose_name="Anzeigename", max_length=15) quiz_game = models.ForeignKey(QuizGame, on_delete=models.CASCADE, related_name="participant") - score = models.IntegerField(verbose_name="Punkte", max_length=10) + score = models.IntegerField(verbose_name="Punkte") avatar = models.CharField(max_length=200) \ No newline at end of file From 3ef7d61da6674c589e071706160754d51ef9fd43 Mon Sep 17 00:00:00 2001 From: juhnsa Date: Sat, 15 Mar 2025 20:30:49 +0100 Subject: [PATCH 7/7] Teilnehmlogik grundlegend fertiggestellt --- ...gameparticipant_participant_id_and_more.py | 29 +++++++++++++++ core/play/models.py | 19 ++++++++-- core/play/urls.py | 2 +- core/play/views.py | 37 ++++++++++++++----- .../play/initialize_participant.html | 2 + core/templates/play/lobby.html | 11 ++++-- 6 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 core/play/migrations/0003_quizgameparticipant_participant_id_and_more.py diff --git a/core/play/migrations/0003_quizgameparticipant_participant_id_and_more.py b/core/play/migrations/0003_quizgameparticipant_participant_id_and_more.py new file mode 100644 index 0000000..ce0937c --- /dev/null +++ b/core/play/migrations/0003_quizgameparticipant_participant_id_and_more.py @@ -0,0 +1,29 @@ +# Generated by Django 5.1.7 on 2025-03-15 19:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('play', '0002_alter_quizgame_join_code'), + ] + + operations = [ + migrations.AddField( + model_name='quizgameparticipant', + name='participant_id', + field=models.CharField(default=1, max_length=200, unique=True), + preserve_default=False, + ), + migrations.AlterField( + model_name='quizgameparticipant', + name='avatar', + field=models.CharField(blank=True, default='', max_length=200), + ), + migrations.AlterField( + model_name='quizgameparticipant', + name='score', + field=models.IntegerField(default=0, verbose_name='Punkte'), + ), + ] diff --git a/core/play/models.py b/core/play/models.py index 0053f09..547af30 100644 --- a/core/play/models.py +++ b/core/play/models.py @@ -1,6 +1,7 @@ from django.db import models from django.contrib.auth.models import User from library.models import QivipQuiz +from django.http import response import random, string # Create your models here. @@ -16,12 +17,24 @@ class QuizGame(models.Model): def generate_unique_code(self): for i in range(10): - new_code = ''.join(random.choices(string.ascii_letters + string.digits, k=6)) + new_code = ''.join(random.choices(string.digits, k=6)) if not QuizGame.objects.filter(join_code=new_code).exists(): return new_code class QuizGameParticipant(models.Model): + participant_id = models.CharField(max_length=200, unique=True) display_name = models.CharField(verbose_name="Anzeigename", max_length=15) quiz_game = models.ForeignKey(QuizGame, on_delete=models.CASCADE, related_name="participant") - score = models.IntegerField(verbose_name="Punkte") - avatar = models.CharField(max_length=200) \ No newline at end of file + score = models.IntegerField(verbose_name="Punkte", default=0) + avatar = models.CharField(max_length=200, blank=True, default="") + + def save(self, *args, **kwargs): + if not self.participant_id: + self.participant_id = self.generate_unique_id() + super().save(*args, **kwargs) + + def generate_unique_id(self): + for i in range(10): + new_id = ''.join(random.choices(string.digits + string.ascii_lowercase, k=60)) + if not QuizGameParticipant.objects.filter(participant_id=new_id).exists(): + return new_id \ No newline at end of file diff --git a/core/play/urls.py b/core/play/urls.py index 444ac54..32285c1 100644 --- a/core/play/urls.py +++ b/core/play/urls.py @@ -5,6 +5,6 @@ app_name = 'play' urlpatterns = [ path('lobby/', views.lobby, name='lobby'), + path('lobby//participate', views.create_participant, name='create_participant'), path('join', views.join_game, name='join_game'), - path('participant', views.create_participant, name='create_participant'), ] \ No newline at end of file diff --git a/core/play/views.py b/core/play/views.py index b0c7e53..bc69f69 100644 --- a/core/play/views.py +++ b/core/play/views.py @@ -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): diff --git a/core/templates/play/initialize_participant.html b/core/templates/play/initialize_participant.html index b27e5da..2bc4acc 100644 --- a/core/templates/play/initialize_participant.html +++ b/core/templates/play/initialize_participant.html @@ -5,6 +5,8 @@
{% csrf_token %} + +
diff --git a/core/templates/play/lobby.html b/core/templates/play/lobby.html index 7165d55..1bdd7f5 100644 --- a/core/templates/play/lobby.html +++ b/core/templates/play/lobby.html @@ -3,14 +3,19 @@ {% block content %}

Bundeslaender Deutschland Quiz

+
+

Sichtbar als {{ participant.display_name }}

+
    -
  • Spieler 1
  • -
  • Spieler 2
  • -
  • Spieler 3
  • +
  • Noch keine Spieler gefunden.
{{debug}}
+ + {% endblock %} \ No newline at end of file