Add MusicGameResults model and score calculation logic in GameEndView
This commit is contained in:
parent
b1ec960dfa
commit
303538bf48
7 changed files with 116 additions and 18 deletions
|
@ -9,7 +9,7 @@ from django.contrib.auth.models import User
|
|||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import IntegrityError
|
||||
from django.db.models import Count, Q
|
||||
from django.db.models import Count, F, Q
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.views import View
|
||||
from django.views.generic import TemplateView
|
||||
|
@ -468,5 +468,25 @@ class GameEndView(LoginRequiredMixin, SingleObjectMixin, View):
|
|||
if not game.group.is_leader(request.user):
|
||||
raise PermissionDenied()
|
||||
game.over = True
|
||||
|
||||
for player in game.players.all():
|
||||
score = (
|
||||
100
|
||||
* player.musicgameanswer_set.filter(game__game=game)
|
||||
.exclude(game__player=player)
|
||||
.filter(game__player=F("answer"))
|
||||
.count()
|
||||
)
|
||||
score -= (
|
||||
50
|
||||
* player.musicgameanswer_set.filter(game__game=game)
|
||||
.filter(game__player=player)
|
||||
.exclude(game__player=F("answer"))
|
||||
.count()
|
||||
)
|
||||
models.MusicGameResults.objects.create(
|
||||
game=game, player=player, score=score
|
||||
)
|
||||
|
||||
game.save()
|
||||
return redirect("game_detail", pk)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue