from django.db import models from . import Tournament, FederalMatchCategory import uuid class Round(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE) index = models.IntegerField(default=0) loser = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, related_name='children') format = models.IntegerField(default=FederalMatchCategory.NINE_GAMES, choices=FederalMatchCategory.choices, null=True, blank=True) def __str__(self): if self.loser: return f"{self.tournament.name} - Loser bracket of : {self.name()}" else: return f"{self.tournament.name} - {self.name()}" def name(self): if self.loser: return "Matchs de classement" else: if self.index == 0: return "Finale" elif self.index == 1: return "Demi-Finales" elif self.index == 2: return "Quarts de finale" else: squared = 2 ** self.index return f"{squared}ème" def ranking_matches(self, hide_empty_matches): matches = [] for child in self.children.all(): child_matches = child.match_set.all() if hide_empty_matches: child_matches = [m for m in matches if m.should_appear()] matches.extend(child_matches) matches.extend(child.ranking_matches(hide_empty_matches)) return matches def all_matches(self): matches = self.match_set.all() matches.extend(self.ranking_matches(True)) return matches