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

@@ -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'),
),
]

View File

@@ -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)
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

View File

@@ -5,6 +5,6 @@ app_name = 'play'
urlpatterns = [
path('lobby/<str:join_code>', views.lobby, name='lobby'),
path('lobby/<str:join_code>/participate', views.create_participant, name='create_participant'),
path('join', views.join_game, name='join_game'),
path('participant', views.create_participant, name='create_participant'),
]

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):

View File

@@ -5,6 +5,8 @@
<div class="input-group border-blue-600 border-2 rounded-xl shadow-md">
<form method="post">
{% csrf_token %}
<input type="hidden" name="join_code" value="{{join_code}}">
<input type="text" name="display_name" placeholder="Anzeigename">
<button type="submit">Speichern</button>
</form>
</div>

View File

@@ -3,14 +3,19 @@
{% block content %}
<div class="container mx-auto px-4">
<h1>Bundeslaender Deutschland Quiz</h1>
<div class="mt-4 mb-4 text-center">
<h3>Sichtbar als <b>{{ participant.display_name }}</b></h3>
</div>
<div id="player-list">
<ul>
<li>Spieler 1</li>
<li>Spieler 2</li>
<li>Spieler 3</li>
<li>Noch keine Spieler gefunden.</li>
</ul>
{{debug}}
</div>
<button class="w-full p-3 rounded-full bg-blue-600 text-white font-black hover:scale-105 transition duration-200" type="submit">Starten</button>
</div>
<script>
// TODO: Websocket Verbindung aufbauen und Teilnehmerliste bei beitreten updaten
</script>
{% endblock %}