Skip to content

Commit

Permalink
Add reserve teams to break.
Browse files Browse the repository at this point in the history
  • Loading branch information
teymour-aldridge authored and tienne-B committed Sep 15, 2024
1 parent 517d3c3 commit a36a590
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
23 changes: 21 additions & 2 deletions tabbycat/breakqual/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, category):
"""`category` is a BreakCategory instance."""
self.category = category
self.break_size = category.break_size
self.reserve_size = category.reserve_size

def generate(self):
self.set_team_queryset()
Expand Down Expand Up @@ -215,8 +216,17 @@ def populate_database(self):
group = list(group)
for tsi in group:
bt, _ = BreakingTeam.objects.update_or_create(
break_category=self.category, team=tsi.team,
defaults={'rank': rank, 'break_rank': break_rank, 'remark': None},
break_category=self.category,
team=tsi.team,
defaults={
"rank": rank,
"break_rank": break_rank,
"remark": (
BreakingTeam.REMARK_RESERVE
if self.break_size < rank <= self.reserve_size + self.break_size
else None
),
},
)
bts_to_keep.append(bt.id)
logger.info("Breaking in %s (rank %s): %s", bt.break_rank, rank, bt.team)
Expand Down Expand Up @@ -255,9 +265,18 @@ def compute_break(self):
self.breaking_teams = self.eligible_teams[:self.break_size]

# If the last spot is tied, add all tied teams
tied_teams_count = 0
if len(self.eligible_teams) >= self.break_size:
last_rank = self.eligible_teams[self.break_size-1].get_ranking("rank")
for tsi in self.eligible_teams[self.break_size:]:
if tsi.get_ranking("rank") != last_rank:
break
self.breaking_teams.append(tsi)
tied_teams_count += 1

# Then add the reserve teams (these are later marked as "reserve" in the
# populate database section)
reserve = self.eligible_teams[
self.break_size + tied_teams_count : self.break_size + tied_teams_count + self.reserve_size
]
self.breaking_teams.extend(reserve)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.4 on 2024-09-06 17:20

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('breakqual', '0006_alter_breakcategory_unique_together_and_more'),
]

operations = [
migrations.AddField(
model_name='breakcategory',
name='reserve_size',
field=models.IntegerField(default=0, help_text='Number of reserve teams in this category.', validators=[django.core.validators.MinValueValidator(0)], verbose_name='Reserve size'),
),
migrations.AlterField(
model_name='breakingteam',
name='remark',
field=models.CharField(blank=True, choices=[('C', 'Capped'), ('I', 'Ineligible'), ('D', 'Different break'), ('d', 'Disqualified'), ('t', 'Lost coin toss'), ('w', 'Withdrawn'), ('R', 'Reserve')], help_text="Used to explain why an otherwise-qualified team didn't break", max_length=1, null=True, verbose_name='remark'),
),
]
12 changes: 8 additions & 4 deletions tabbycat/breakqual/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class BreakCategory(models.Model):
verbose_name=_("sequence number"),
help_text=_("The order in which the categories are displayed"))
break_size = models.IntegerField(validators=[MinValueValidator(2)],
verbose_name=_("break size"),
help_text=_("Number of breaking teams in this category"))
verbose_name=_("break size"),
help_text=_("Number of breaking teams in this category"))
reserve_size = models.PositiveIntegerField(default=0, verbose_name=_("Reserve size"),
help_text=_("Number of reserve teams in this category."))
is_general = models.BooleanField(
verbose_name=_("is general"),
help_text=_("Are teams eligible for this break by default"))
Expand Down Expand Up @@ -91,17 +93,19 @@ class BreakingTeam(models.Model):
REMARK_DISQUALIFIED = 'd'
REMARK_LOST_COIN_TOSS = 't'
REMARK_WITHDRAWN = 'w'
REMARK_RESERVE = 'R'
REMARK_CHOICES = (
(REMARK_CAPPED, _("Capped")),
(REMARK_INELIGIBLE, _("Ineligible")),
(REMARK_DIFFERENT_BREAK, _("Different break")),
(REMARK_DISQUALIFIED, _("Disqualified")),
(REMARK_LOST_COIN_TOSS, _("Lost coin toss")),
(REMARK_WITHDRAWN, _("Withdrawn")),
(REMARK_RESERVE, _("Reserve")),
)
remark = models.CharField(max_length=1, choices=REMARK_CHOICES, blank=True, null=True,
verbose_name=_("remark"),
help_text=_("Used to explain why an otherwise-qualified team didn't break"))
verbose_name=_("remark"),
help_text=_("Used to explain why an otherwise-qualified team didn't break"))

class Meta:
constraints = [UniqueConstraint(fields=['break_category', 'team'])]
Expand Down

0 comments on commit a36a590

Please sign in to comment.