|
|
|
|
@ -503,10 +503,13 @@ class Tournament(BaseModel): |
|
|
|
|
|
|
|
|
|
match_groups = [] |
|
|
|
|
if group_stage_id: |
|
|
|
|
group_stage = self.group_stages.filter(id=group_stage_id).first() |
|
|
|
|
# Use prefetched data instead of additional query |
|
|
|
|
group_stage = next((gs for gs in self.group_stages.all() if str(gs.id) == group_stage_id), None) |
|
|
|
|
if group_stage: |
|
|
|
|
match_groups.append(self.group_stage_match_group(group_stage, broadcasted, hide_empty_matches=False)) |
|
|
|
|
elif round_id: |
|
|
|
|
round = self.rounds.filter(id=round_id).first() |
|
|
|
|
# Use prefetched data instead of additional query |
|
|
|
|
round = next((r for r in self.rounds.all() if str(r.id) == round_id), None) |
|
|
|
|
if round and display_brackets is True: |
|
|
|
|
match_groups = self.round_match_groups(round, broadcasted, hide_empty_matches=False) |
|
|
|
|
else: |
|
|
|
|
@ -518,13 +521,17 @@ class Tournament(BaseModel): |
|
|
|
|
groups = [] |
|
|
|
|
|
|
|
|
|
if self.display_matches(): |
|
|
|
|
rounds = self.rounds.filter(parent=None, group_stage_loser_bracket=False).all().order_by('index').prefetch_related('matches', 'matches__team_scores', 'matches__team_scores__team_registration', 'matches__team_scores__team_registration__player_registrations') |
|
|
|
|
for round in rounds: |
|
|
|
|
# Use already prefetched rounds to avoid additional queries |
|
|
|
|
bracket_rounds = [r for r in self.rounds.all() if r.parent is None and not r.group_stage_loser_bracket] |
|
|
|
|
bracket_rounds.sort(key=lambda r: r.index) |
|
|
|
|
for round in bracket_rounds: |
|
|
|
|
groups.extend(self.round_match_groups(round, broadcasted, hide_empty_matches=True)) |
|
|
|
|
|
|
|
|
|
if self.display_group_stages(): |
|
|
|
|
rounds = self.rounds.filter(parent=None, group_stage_loser_bracket=True).all().order_by('index').prefetch_related('matches', 'matches__team_scores', 'matches__team_scores__team_registration', 'matches__team_scores__team_registration__player_registrations') |
|
|
|
|
for round in rounds: |
|
|
|
|
# Use already prefetched rounds to avoid additional queries |
|
|
|
|
loser_bracket_rounds = [r for r in self.rounds.all() if r.parent is None and r.group_stage_loser_bracket] |
|
|
|
|
loser_bracket_rounds.sort(key=lambda r: r.index) |
|
|
|
|
for round in loser_bracket_rounds: |
|
|
|
|
groups.extend(self.round_match_groups(round, broadcasted, hide_empty_matches=True)) |
|
|
|
|
|
|
|
|
|
group_stages = sorted(self.sorted_group_stages(), key=lambda s: (-s.step, s.index)) |
|
|
|
|
@ -609,19 +616,25 @@ class Tournament(BaseModel): |
|
|
|
|
return [gs.live_group_stages() for gs in group_stages] |
|
|
|
|
|
|
|
|
|
def sorted_group_stages(self): |
|
|
|
|
# Get all group stages and sort by step (descending) and index (ascending) |
|
|
|
|
group_stages = self.group_stages.all().order_by('-step', 'index').prefetch_related('matches', 'matches__team_scores', 'matches__team_scores__team_registration', 'matches__team_scores__team_registration__player_registrations') |
|
|
|
|
# Use already prefetched group stages to avoid additional queries |
|
|
|
|
group_stages = list(self.group_stages.all()) |
|
|
|
|
group_stages.sort(key=lambda gs: (-gs.step, gs.index)) |
|
|
|
|
|
|
|
|
|
# List to collect live group stages from finished steps |
|
|
|
|
filtered = [] |
|
|
|
|
steps_completed = set() |
|
|
|
|
|
|
|
|
|
for group_stage in group_stages: |
|
|
|
|
if group_stage.step > 0: |
|
|
|
|
# Check the previous step's group stages |
|
|
|
|
previous_step_group_stages = self.group_stages.filter(step=group_stage.step - 1) |
|
|
|
|
|
|
|
|
|
# Check the previous step's group stages using already loaded data |
|
|
|
|
prev_step = group_stage.step - 1 |
|
|
|
|
if prev_step not in steps_completed: |
|
|
|
|
previous_step_group_stages = [gs for gs in group_stages if gs.step == prev_step] |
|
|
|
|
# Check if all previous step group stages are completed |
|
|
|
|
if all(gs.is_completed() for gs in previous_step_group_stages): |
|
|
|
|
if previous_step_group_stages and all(gs.is_completed() for gs in previous_step_group_stages): |
|
|
|
|
steps_completed.add(prev_step) |
|
|
|
|
|
|
|
|
|
if prev_step in steps_completed: |
|
|
|
|
filtered.append(group_stage) |
|
|
|
|
else: |
|
|
|
|
# Always include step 0 |
|
|
|
|
|