Skip to content

Commit

Permalink
Add Tournament.shuffle_participants
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Jan 30, 2024
1 parent f2a2a95 commit d8473d1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions tournaments/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def get(self, request, *args, **kwargs):
return redirect('update-tournament', pk = self.object.id)

# Change tournament state to "active".
self.object.shuffle_participants()
self.object.update_state()

if self.object.state in ('active', 'finished'):
Expand Down
34 changes: 29 additions & 5 deletions tournaments/tournaments/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import math
import re
import random

from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.db.models import CheckConstraint, Q, Max
from django.db.models import CheckConstraint, Q, Min, Max
from django.db.models.signals import pre_delete
from django.dispatch import receiver

Expand Down Expand Up @@ -72,6 +73,29 @@ def current_stage(self):
return stage
return None ## indicates that the tournament is finished

@transaction.atomic
def shuffle_participants(self):
count = self.participations.count()
if count == 0: return ## early out, so that min/max operations below are well defined
new_slot_ids = list(range(count))
random.shuffle(new_slot_ids)

# SQLite does not support deferred unique constraints, therefore we need to work around.
min_slot_id = self.participations.aggregate(Min('slot_id'))['slot_id__min']
max_slot_id = self.participations.aggregate(Max('slot_id'))['slot_id__max']

# If any value of `new_slot_ids` is already taken, add an offset to establish uniqueness.
if min_slot_id < count:
for new_slot_id, participation in zip(new_slot_ids, self.participations.all()):
participation.slot_id = max_slot_id + 1 + new_slot_id
participation.save()

# Otherwise, the values of `new_slot_ids` can be assigned directly.
else:
for new_slot_id, participation in zip(new_slot_ids, self.participations.all()):
participation.slot_id = new_slot_id
participation.save()

def update_state(self):
if self.current_stage is None:

Expand Down Expand Up @@ -565,10 +589,10 @@ class Fixture(models.Model):

class Meta:
constraints = [
CheckConstraint(
check = (Q(score1__isnull = True) & Q(score2__isnull = True)) | (Q(score1__isnull = False) & Q(score2__isnull = False)),
name = 'score1 and score2 must be both null or neither')
]
CheckConstraint(
check = (Q(score1__isnull = True) & Q(score2__isnull = True)) | (Q(score1__isnull = False) & Q(score2__isnull = False)),
name = 'score1 and score2 must be both null or neither')
]

def clean(self):
super(Fixture, self).clean()
Expand Down
17 changes: 17 additions & 0 deletions tournaments/tournaments/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,23 @@ def test_delete(self):
tournament = self.test_load_tournament1()
tournament.delete()

def test_shuffle_participants(self, repeat = 0):
tournament = self.test_load_tournament1()
_add_participants(self.participants, tournament)
permutations = list()
original_participants = tuple([p.id for p in tournament.participants])
for itr in range(repeat + 1):
tournament.shuffle_participants()
actual_participants = tuple([p.id for p in tournament.participants])
self.assertNotIn(actual_participants, permutations)
self.assertEqual(len(actual_participants), len(original_participants))
for pid in original_participants:
self.assertIn(pid, actual_participants)
permutations.append(actual_participants)

def test_shuffle_participants_twice(self):
self.test_shuffle_participants(repeat = 1)

def test_current_stage(self):
tournament = self.test_load_tournament1()
self.assertEqual(tournament.current_stage.id, tournament.stages.all()[0].id)
Expand Down

0 comments on commit d8473d1

Please sign in to comment.