#154-counter_played_quizzes

This commit is contained in:
ben8
2025-11-14 17:24:51 +01:00
parent 92214d3bda
commit 304cf0a01b
12 changed files with 215 additions and 7 deletions

View File

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