You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
padelclub_backend/tournaments/models/round.py

221 lines
9.1 KiB

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)
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):
short_names = double_butterfly_mode
if double_butterfly_mode and self.tournament.rounds.count() < 3:
short_names = False
matches = self.matches.filter(disabled=False).order_by('index')
if len(matches) == 0:
return None
if next_round:
next_round_matches = next_round.matches.filter(disabled=False).order_by('index')
else:
next_round_matches = []
if len(matches) < len(next_round_matches):
all_matches = self.matches.order_by('index')
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
# Only filter out the pair if both matches are disabled
if current_match.disabled and pair_match and pair_match.disabled:
# 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)
pass
else:
# Keep the current match
if current_match.disabled == False:
filtered_matches.append(current_match)
# If there's a pair match, keep it too
if pair_match and pair_match.disabled == False:
filtered_matches.append(pair_match)
# Move to the next pair
i += 2
# Replace the matches list with our filtered list
matches = filtered_matches
if matches:
if len(matches) > 1 and double_butterfly_mode:
if len(matches) % 2 == 1:
# 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))
# 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]
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:]
# 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)
# 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
else:
midpoint = len(matches) // 2 # More to second half
else:
# No missing indices or only one expected match, split normally
midpoint = len(matches) // 2
else:
# Even number of matches: split evenly
midpoint = len(matches) // 2
first_half_matches = matches[:midpoint]
if secondHalf:
first_half_matches = matches[midpoint:]
else:
first_half_matches = list(matches) # Convert QuerySet to a list
if self.index == 0 and loser_final:
loser_match = loser_final.matches.first()
if loser_match:
first_half_matches.append(loser_match)
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
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
)
return match_group
return None