#154-counter_played_quizzes
This commit is contained in:
@@ -4,7 +4,7 @@ from channels.generic.websocket import AsyncWebsocketConsumer
|
||||
from channels.db import database_sync_to_async
|
||||
from django.utils import timezone
|
||||
from django.urls import reverse
|
||||
from play.models import QuizGame, QuizGameParticipant, QuizAnswer
|
||||
from play.models import QuizGame, QuizGameParticipant, QuizAnswer,QivipQuiz
|
||||
from django.conf import settings
|
||||
from geopy.distance import geodesic
|
||||
|
||||
@@ -149,6 +149,7 @@ class GameConsumer(AsyncWebsocketConsumer):
|
||||
await self.show_scoreboard()
|
||||
|
||||
elif message_type == 'finish_game':
|
||||
|
||||
host_id = text_data_json['host_id']
|
||||
if await self.verify_host(host_id):
|
||||
await self.finish_game()
|
||||
@@ -625,11 +626,35 @@ class GameConsumer(AsyncWebsocketConsumer):
|
||||
'redirect_url': reverse('play:scores', kwargs={'join_code': self.join_code})
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@database_sync_to_async
|
||||
def counter_played_quizzes(self): # Zähler, wie oft ein Quiz gespielt worden ist.
|
||||
|
||||
quiz = QuizGame.objects.get(join_code=self.join_code).quiz_id
|
||||
quiz_game = QuizGame.objects.get(join_code=self.join_code)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if quiz_game.single_player_or_multiple=="multiple":
|
||||
quiz.quiz_played_Moderiermodus+=1
|
||||
|
||||
active_participants = QuizGameParticipant.objects.filter(
|
||||
quiz_game=quiz_game
|
||||
).count()
|
||||
quiz.quiz_played_Moderiermodus_players+=active_participants
|
||||
|
||||
else:
|
||||
quiz.quiz_played_Lernmodus+=1
|
||||
quiz.save()
|
||||
|
||||
@database_sync_to_async
|
||||
def _finish_game(self):
|
||||
try:
|
||||
|
||||
quiz_game = QuizGame.objects.get(join_code=self.join_code)
|
||||
|
||||
quiz_game.current_state = 'finished'
|
||||
quiz_game.save()
|
||||
return True
|
||||
@@ -638,6 +663,7 @@ class GameConsumer(AsyncWebsocketConsumer):
|
||||
|
||||
async def finish_game(self):
|
||||
success = await self._finish_game()
|
||||
await self.counter_played_quizzes()
|
||||
if success:
|
||||
# Notify clients to redirect to finished page
|
||||
await self.channel_layer.group_send(
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 5.1.7 on 2025-11-14 13:53
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('play', '0016_quizgameparticipant_coordinate_answer'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='quizgame',
|
||||
name='quiz_played_Lernmodus',
|
||||
field=models.IntegerField(blank=True, default=0, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='quizgame',
|
||||
name='quiz_played_Moderiermodus',
|
||||
field=models.IntegerField(blank=True, default=0, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='quizgame',
|
||||
name='quiz_played_Moderiermodus_players',
|
||||
field=models.IntegerField(blank=True, default=0, null=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.1.7 on 2025-11-14 14:11
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('play', '0017_quizgame_quiz_played_lernmodus_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='quizgame',
|
||||
name='single_player_or_multiple',
|
||||
field=models.CharField(blank=True, max_length=20),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,29 @@
|
||||
# Generated by Django 5.1.7 on 2025-11-14 14:50
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('play', '0018_quizgame_single_player_or_multiple'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='quizgame',
|
||||
name='quiz_played_Lernmodus',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='quizgame',
|
||||
name='quiz_played_Moderiermodus',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='quizgame',
|
||||
name='quiz_played_Moderiermodus_players',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='quizgame',
|
||||
name='single_player_or_multiple',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.1.7 on 2025-11-14 15:04
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('play', '0019_remove_quizgame_quiz_played_lernmodus_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='quizgame',
|
||||
name='single_player_or_multiple',
|
||||
field=models.CharField(blank=True, max_length=20),
|
||||
),
|
||||
]
|
||||
@@ -15,7 +15,7 @@ class QuizGame(models.Model):
|
||||
('scoreboard', 'Scoreboard'),
|
||||
|
||||
]
|
||||
|
||||
single_player_or_multiple=models.CharField(max_length=20, blank=True)
|
||||
host_id = models.CharField(max_length=200, unique=True)
|
||||
join_code = models.CharField(max_length=6, unique=True, blank=True)
|
||||
quiz_id = models.ForeignKey(QivipQuiz, on_delete=models.CASCADE)
|
||||
|
||||
@@ -419,14 +419,24 @@ def waiting_room(request, join_code):
|
||||
|
||||
|
||||
def selected_mode(request, join_code):
|
||||
|
||||
|
||||
|
||||
mode = request.GET.get('mode','default')
|
||||
request.session['mode'] = mode
|
||||
|
||||
quiz_game = QuizGame.objects.get(join_code=join_code)
|
||||
if mode=="learn":
|
||||
|
||||
quiz_game.single_player_or_multiple="single"
|
||||
else:
|
||||
|
||||
quiz_game.single_player_or_multiple="multiple"
|
||||
|
||||
quiz_game.save()
|
||||
|
||||
|
||||
quiz_game = get_object_or_404(QuizGame, join_code=join_code)
|
||||
quiz = get_object_or_404(QivipQuiz, pk=quiz_game.quiz_id.id)
|
||||
QuizGameParticipant.objects.filter(quiz_game=quiz_game).delete()
|
||||
QuizGameParticipant.objects.filter(quiz_game=quiz_game).delete()
|
||||
|
||||
# Hier werden die Fragen gemischt!
|
||||
import random
|
||||
|
||||
Reference in New Issue
Block a user