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 bc864c2..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. @@ -21,7 +22,19 @@ class QuizGame(models.Model): 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", max_length=10) - 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 72f8362..32285c1 100644 --- a/core/play/urls.py +++ b/core/play/urls.py @@ -5,5 +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'), ] \ No newline at end of file diff --git a/core/play/views.py b/core/play/views.py index 69e57fc..bc69f69 100644 --- a/core/play/views.py +++ b/core/play/views.py @@ -1,12 +1,41 @@ 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 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) - return render(request, 'play/lobby.html', {'debug': "test"}) + 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('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': diff --git a/core/templates/play/initialize_participant.html b/core/templates/play/initialize_participant.html new file mode 100644 index 0000000..2bc4acc --- /dev/null +++ b/core/templates/play/initialize_participant.html @@ -0,0 +1,13 @@ +{% extends 'base.html' %} + +{% block content %} +

User for a Game:

+
+
+ {% csrf_token %} + + + +
+
+{% endblock %} \ No newline at end of file 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