From 8a1a667388b666d31f4e205e085ce7872ad4a27f Mon Sep 17 00:00:00 2001 From: Jason Cameron Date: Wed, 6 Mar 2024 10:00:25 -0500 Subject: [PATCH] Added what I belive should work --- gameserver/models/contest.py | 27 ++++++--------------------- gameserver/views/problem.py | 3 ++- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/gameserver/models/contest.py b/gameserver/models/contest.py index 43c73c4..efb8759 100644 --- a/gameserver/models/contest.py +++ b/gameserver/models/contest.py @@ -421,25 +421,16 @@ def save(self, *args, **kwargs): from django.contrib.auth.models import User class ContestScore(models.Model): - user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True) - team = models.ForeignKey(Team, on_delete=models.CASCADE, null=True, blank=True) - contest = models.ForeignKey(Contest, on_delete=models.CASCADE) + participation=models.ForeignKey(ContestParticipation, on_delete=CASCADE, db_index=True) points = models.PositiveIntegerField(help_text="The amount of points.", default=0) flag_count = models.PositiveIntegerField(help_text="The amount of flags the user/team has.", default=0) @classmethod - def update_or_create(cls, change_in_score: int, contest: Contest, user: Optional[User] = None, team: Optional[Team] = None, update_flags: bool = True): - - if not user and not team: - raise ValueError("You must specify either a user or a team") - - queryset = cls.objects.filter(contest=contest) - if user: - queryset = queryset.filter(user=user) - elif team: - queryset = queryset.filter(team=team) + def update_or_create(cls, change_in_score: int, participant: ContestParticipation, update_flags: bool = True): + queryset = cls.objects.filter(participation=participant) + if not queryset.exists(): # no user/team found matching that - cls.objects.create(contest=contest, user=user, team=team, flag_count=int(update_flags), points=change_in_score) + cls.objects.create(participation=participant, flag_count=int(update_flags), points=change_in_score) return cls.update_or_create(contest=contest, change_in_score=change_in_score, user=user, team=team, update_flags=update_flags) with transaction.atomic(): @@ -448,10 +439,4 @@ def update_or_create(cls, change_in_score: int, contest: Contest, user: Optional if update_flags: queryset.update(points=F('points') + change_in_score) else: - queryset.update(points=F('points') + change_in_score, flag_count=F('flag_count') + 1) - - class Meta: - indexes = [ - models.Index(fields=['user', 'contest']), # Composite index for user and contest - models.Index(fields=['team', 'contest']), # Composite index for team and contest - ] + queryset.update(points=F('points') + change_in_score, flag_count=F('flag_count') + 1) \ No newline at end of file diff --git a/gameserver/views/problem.py b/gameserver/views/problem.py index cbfb7fb..4693ca7 100644 --- a/gameserver/views/problem.py +++ b/gameserver/views/problem.py @@ -145,7 +145,8 @@ def _create_submission_object(self, form, is_correct=None): submission=submission, participation=self.request.participation, ) - #models.ContestScore. + if is_correct: + models.ContestScore.update_or_create(participation=self.request.participation, change_in_points = self.object.points, update_flags=True) return submission def get_form_kwargs(self, *args, **kwargs):