from django.db import models from . import TournamentSubModel, Tournament, FederalMatchCategory import uuid class Round(TournamentSubModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) tournament = models.ForeignKey(Tournament, on_delete=models.SET_NULL, related_name='rounds', null=True) index = models.IntegerField(default=0) parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL, related_name='children') format = models.IntegerField(default=FederalMatchCategory.NINE_GAMES, choices=FederalMatchCategory.choices, null=True, blank=True) start_date = models.DateTimeField(null=True, blank=True) planned_start_date = models.DateTimeField(null=True, blank=True) group_stage_loser_bracket = models.BooleanField(default=False) loser_bracket_mode = models.IntegerField(default=0) # Debug flag - set to False to disable all debug prints DEBUG_PREPARE_MATCH_GROUP = False @staticmethod def debug_print(*args, **kwargs): """Print debug messages only if DEBUG_PREPARE_MATCH_GROUP is True""" if Round.DEBUG_PREPARE_MATCH_GROUP: print(*args, **kwargs) def delete_dependencies(self): for round in self.children.all(): round.delete_dependencies() round.delete() for match in self.matches.all(): match.delete_dependencies() match.delete() def __str__(self): if self.parent: return f"LB: {self.name()}" else: return self.name() def get_tournament(self): # mandatory method for TournamentSubModel return self.tournament def name(self): if self.parent and self.parent.parent is None: return f"Classement {self.parent.name()}" elif self.parent and self.parent.parent is not None: return f"{self.parent.name()}" elif self.group_stage_loser_bracket is True: return "Classement de poule" else: if self.index == 0: return "Finale" elif self.index == 1: return "Demie" elif self.index == 2: return "Quart" else: squared = 2 ** self.index return f"{squared}ème" def plural_name(self): name = self.name() if self.parent is None and self.index > 0: return f'{name}s' return name def ranking_matches(self, hide_empty_matches): children_with_matches = self.children.prefetch_related('matches', 'matches__team_scores', 'matches__team_scores__team_registration', 'matches__team_scores__team_registration__player_registrations') matches = [] for child in children_with_matches: child_matches = child.matches.all() if hide_empty_matches: child_matches = [m for m in child_matches if m.should_appear()] else: child_matches = [m for m in child_matches if m.disabled is False] matches.extend(child_matches) matches.extend(child.ranking_matches(hide_empty_matches)) return matches def all_matches(self, hide_empty_matches): matches = [] matches.extend(self.get_matches_recursive(hide_empty_matches)) return matches def get_matches_recursive(self, hide_empty_matches): matches = list(self.matches.all()) # Retrieve matches associated with the current round if hide_empty_matches: matches = [m for m in matches if m.should_appear()] else: matches = [m for m in matches if m.disabled is False] matches.sort(key=lambda m: m.index) # Recursively fetch matches from child rounds for child_round in self.children.all(): matches.extend(child_round.get_matches_recursive(hide_empty_matches)) return matches def get_depth(self): depth = 0 current_round = self while current_round.parent: depth += 1 current_round = current_round.parent return depth def root_round(self): if self.parent is None: return self else: return self.parent.root_round() def all_matches_are_over(self): for match in self.matches.all(): if match.end_date is None and match.disabled is False: return False return True def prepare_match_group(self, next_round, parent_round, loser_final, double_butterfly_mode, secondHalf): Round.debug_print(f"\n[{self.name()}] === START prepare_match_group ===") Round.debug_print(f"[{self.name()}] index={self.index}, nextRound={next_round.name() if next_round else None}, parentRound={parent_round.name() if parent_round else None}") Round.debug_print(f"[{self.name()}] loserFinal={loser_final.name() if loser_final else None}, doubleButterfly={double_butterfly_mode}, secondHalf={secondHalf}") short_names = double_butterfly_mode if double_butterfly_mode and self.tournament.rounds.filter(parent=None).count() < 3: short_names = False Round.debug_print(f"[{self.name()}] Short names disabled (rounds < 3)") matches = self.matches.filter(disabled=False).order_by('index') Round.debug_print(f"[{self.name()}] Initial enabled matches: {len(matches)} - indices: {[m.index for m in matches]}") if len(matches) == 0: Round.debug_print(f"[{self.name()}] No matches, returning None") return None if next_round: next_round_matches = next_round.matches.filter(disabled=False).order_by('index') Round.debug_print(f"[{self.name()}] Next round matches: {len(next_round_matches)} - indices: {[m.index for m in next_round_matches]}") else: next_round_matches = [] Round.debug_print(f"[{self.name()}] No next round") if len(matches) < len(next_round_matches): Round.debug_print(f"[{self.name()}] FILTERING: matches({len(matches)}) < nextRoundMatches({len(next_round_matches)})") all_matches = self.matches.order_by('index') Round.debug_print(f"[{self.name()}] All matches (including disabled): {len(all_matches)} - indices: {[(m.index, m.disabled) for m in all_matches]}") filtered_matches = [] # Process matches in pairs i = 0 while i < len(all_matches): # Get the current match and its pair (if available) current_match = all_matches[i] pair_match = all_matches[i+1] if i+1 < len(all_matches) else None Round.debug_print(f"[{self.name()}] Pair {i//2}: current={current_match.index}(disabled={current_match.disabled}), pair={pair_match.index if pair_match else None}(disabled={pair_match.disabled if pair_match else None})") # Only filter out the pair if both matches are disabled if current_match.disabled and pair_match and pair_match.disabled: Round.debug_print(f"[{self.name()}] Both disabled, checking next_round for index {current_match.index // 2}") # Skip one of the matches in the pair if next_round_matches.filter(index=current_match.index // 2).exists(): filtered_matches.append(current_match) # filtered_matches.append(pair_match) # Keeping two was bugging the bracket Round.debug_print(f"[{self.name()}] Next round match exists, keeping one") else: Round.debug_print(f"[{self.name()}] No next round match, skipping both") else: # Keep the current match if current_match.disabled == False: filtered_matches.append(current_match) Round.debug_print(f"[{self.name()}] Keeping current match {current_match.index}") # If there's a pair match, keep it too if pair_match and pair_match.disabled == False: filtered_matches.append(pair_match) Round.debug_print(f"[{self.name()}] Keeping pair match {pair_match.index}") # Move to the next pair i += 2 # Replace the matches list with our filtered list matches = filtered_matches Round.debug_print(f"[{self.name()}] After filtering: {len(matches)} matches - indices: {[m.index for m in matches]}") if matches: if len(matches) > 1 and double_butterfly_mode: Round.debug_print(f"[{self.name()}] SPLITTING: doubleButterfly with {len(matches)} matches") if len(matches) % 2 == 1: Round.debug_print(f"[{self.name()}] ODD number of matches - using smart split logic") # Calculate expected index range for this round if self.index == 0: # Final: only index 0 expected_indices = [0] else: # For round n: 2^n matches, starting at index (2^n - 1) expected_count = 2 ** self.index start_index = (2 ** self.index) - 1 expected_indices = list(range(start_index, start_index + expected_count)) Round.debug_print(f"[{self.name()}] Expected indices: {expected_indices}") # Get actual match indices actual_indices = [match.index for match in matches] missing_indices = [idx for idx in expected_indices if idx not in actual_indices] Round.debug_print(f"[{self.name()}] Actual indices: {actual_indices}") Round.debug_print(f"[{self.name()}] Missing indices: {missing_indices}") if missing_indices and len(expected_indices) > 1: # Split the expected range in half midpoint_index = len(expected_indices) // 2 first_half_expected = expected_indices[:midpoint_index] second_half_expected = expected_indices[midpoint_index:] Round.debug_print(f"[{self.name()}] Expected halves: first={first_half_expected}, second={second_half_expected}") # Count actual matches in each theoretical half first_half_actual = sum(1 for idx in actual_indices if idx in first_half_expected) second_half_actual = sum(1 for idx in actual_indices if idx in second_half_expected) Round.debug_print(f"[{self.name()}] Actual counts: first={first_half_actual}, second={second_half_actual}") # Give more display space to the half with more actual matches if first_half_actual > second_half_actual: midpoint = (len(matches) + 1) // 2 # More to first half Round.debug_print(f"[{self.name()}] First half has more: midpoint={midpoint}") else: midpoint = len(matches) // 2 # More to second half Round.debug_print(f"[{self.name()}] Second half has more: midpoint={midpoint}") else: # No missing indices or only one expected match, split normally midpoint = len(matches) // 2 Round.debug_print(f"[{self.name()}] No missing indices: midpoint={midpoint}") else: # Even number of matches: split evenly midpoint = len(matches) // 2 Round.debug_print(f"[{self.name()}] EVEN number of matches: midpoint={midpoint}") first_half_matches = matches[:midpoint] if secondHalf: first_half_matches = matches[midpoint:] Round.debug_print(f"[{self.name()}] Using SECOND half: {len(first_half_matches)} matches - indices: {[m.index for m in first_half_matches]}") else: Round.debug_print(f"[{self.name()}] Using FIRST half: {len(first_half_matches)} matches - indices: {[m.index for m in first_half_matches]}") else: Round.debug_print(f"[{self.name()}] NO SPLITTING: singleButterfly or single match") first_half_matches = list(matches) # Convert QuerySet to a list Round.debug_print(f"[{self.name()}] Using all {len(first_half_matches)} matches - indices: {[m.index for m in first_half_matches]}") if self.index == 0 and loser_final: loser_match = loser_final.matches.first() if loser_match: first_half_matches.append(loser_match) Round.debug_print(f"[{self.name()}] Added loser final match: {loser_match.index}") if first_half_matches: name = self.plural_name() if parent_round and first_half_matches[0].name is not None: name = first_half_matches[0].name Round.debug_print(f"[{self.name()}] Using custom name from first match: '{name}'") else: Round.debug_print(f"[{self.name()}] Using round name: '{name}'") Round.debug_print(f"[{self.name()}] Creating match_group: name='{name}', roundId={self.id}, roundIndex={self.index}, shortNames={short_names}") Round.debug_print(f"[{self.name()}] Final matches in group: {[m.index for m in first_half_matches]}") match_group = self.tournament.create_match_group( name=name, matches=first_half_matches, round_id=self.id, round_index=self.index, short_names=short_names ) Round.debug_print(f"[{self.name()}] === END prepare_match_group - SUCCESS ===\n") return match_group Round.debug_print(f"[{self.name()}] === END prepare_match_group - NO MATCHES ===\n") return None