Skip to content

Commit

Permalink
Added what I belive should work
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo authored Mar 6, 2024
1 parent 55cf15b commit 8a1a667
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
27 changes: 6 additions & 21 deletions gameserver/models/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
3 changes: 2 additions & 1 deletion gameserver/views/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 8a1a667

Please sign in to comment.