|
|
|
@ -960,7 +960,7 @@ def tournament_bracket(request, tournament_id): |
|
|
|
View to display tournament bracket structure. |
|
|
|
View to display tournament bracket structure. |
|
|
|
""" |
|
|
|
""" |
|
|
|
tournament = get_object_or_404(Tournament, pk=tournament_id) |
|
|
|
tournament = get_object_or_404(Tournament, pk=tournament_id) |
|
|
|
context = get_bracket(tournament, parent_round=None, double_butterfly_mode=False, display_loser_final=False) |
|
|
|
context = get_butterfly_bracket_view_context(tournament, parent_round=None, double_butterfly_mode=False, display_loser_final=True) |
|
|
|
return render(request, 'tournaments/tournament_bracket.html', context) |
|
|
|
return render(request, 'tournaments/tournament_bracket.html', context) |
|
|
|
|
|
|
|
|
|
|
|
def round_bracket(request, tournament_id, round_id): |
|
|
|
def round_bracket(request, tournament_id, round_id): |
|
|
|
@ -969,158 +969,15 @@ def round_bracket(request, tournament_id, round_id): |
|
|
|
""" |
|
|
|
""" |
|
|
|
tournament = get_object_or_404(Tournament, pk=tournament_id) |
|
|
|
tournament = get_object_or_404(Tournament, pk=tournament_id) |
|
|
|
round = get_object_or_404(Round, pk=round_id) |
|
|
|
round = get_object_or_404(Round, pk=round_id) |
|
|
|
context = get_bracket(tournament, round, double_butterfly_mode=False, display_loser_final=False) |
|
|
|
context = get_butterfly_bracket_view_context(tournament, round, double_butterfly_mode=False, display_loser_final=False) |
|
|
|
return render(request, 'tournaments/tournament_bracket.html', context) |
|
|
|
return render(request, 'tournaments/tournament_bracket.html', context) |
|
|
|
|
|
|
|
|
|
|
|
def get_bracket(tournament, parent_round=None, double_butterfly_mode=False, display_loser_final=False): |
|
|
|
def get_butterfly_bracket_view_context(tournament, parent_round=None, double_butterfly_mode=False, display_loser_final=False): |
|
|
|
loser_final = None |
|
|
|
|
|
|
|
main_rounds_reversed = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if parent_round: |
|
|
|
if parent_round: |
|
|
|
double_butterfly_mode = False |
|
|
|
double_butterfly_mode = False |
|
|
|
display_loser_final = False |
|
|
|
display_loser_final = True |
|
|
|
|
|
|
|
|
|
|
|
# Get main bracket rounds (excluding children/ranking matches) |
|
|
|
|
|
|
|
main_rounds = tournament.round_set.filter( |
|
|
|
|
|
|
|
parent=parent_round, |
|
|
|
|
|
|
|
group_stage_loser_bracket=False |
|
|
|
|
|
|
|
).order_by('-index') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if double_butterfly_mode: |
|
|
|
|
|
|
|
main_rounds_reversed = tournament.round_set.filter( |
|
|
|
|
|
|
|
parent=parent_round, |
|
|
|
|
|
|
|
group_stage_loser_bracket=False |
|
|
|
|
|
|
|
).order_by('index') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if display_loser_final: |
|
|
|
|
|
|
|
if len(main_rounds_reversed) >= 1: |
|
|
|
|
|
|
|
semi = main_rounds_reversed[1] |
|
|
|
|
|
|
|
loser_round = tournament.round_set.filter( |
|
|
|
|
|
|
|
parent=semi, |
|
|
|
|
|
|
|
group_stage_loser_bracket=False |
|
|
|
|
|
|
|
).order_by('index') |
|
|
|
|
|
|
|
if len(loser_round) >= 1: |
|
|
|
|
|
|
|
loser_final = loser_round[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create serializable match groups data |
|
|
|
|
|
|
|
serializable_match_groups = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add first half of each round (from last to semi-finals) |
|
|
|
|
|
|
|
for round in main_rounds: |
|
|
|
|
|
|
|
matches = round.match_set.filter(disabled=False).order_by('index') |
|
|
|
|
|
|
|
next_round = main_rounds.filter(index=round.index - 1).first() |
|
|
|
|
|
|
|
if next_round: |
|
|
|
|
|
|
|
next_round_matches = next_round.match_set.filter(disabled=False).order_by('index') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
next_round_matches = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(matches) < len(next_round_matches): |
|
|
|
|
|
|
|
all_matches = round.match_set.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 |
|
|
|
|
|
|
|
filtered_matches.append(current_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: |
|
|
|
|
|
|
|
midpoint = int(len(matches) / 2) |
|
|
|
|
|
|
|
first_half_matches = matches[:midpoint] |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
first_half_matches = list(matches) # Convert QuerySet to a list |
|
|
|
|
|
|
|
if loser_final: |
|
|
|
|
|
|
|
loser_matches = loser_final.match_set.all() |
|
|
|
|
|
|
|
if len(loser_matches) >= 1: |
|
|
|
|
|
|
|
first_half_matches.append(loser_matches[0]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if first_half_matches: |
|
|
|
serializable_match_groups = tournament.get_butterfly_bracket_match_group(parent_round, double_butterfly_mode, display_loser_final) |
|
|
|
name = round.name() |
|
|
|
|
|
|
|
if parent_round: |
|
|
|
|
|
|
|
name = first_half_matches[0].computed_name() |
|
|
|
|
|
|
|
match_group = tournament.create_match_group( |
|
|
|
|
|
|
|
name=name, |
|
|
|
|
|
|
|
matches=first_half_matches, |
|
|
|
|
|
|
|
round_id=round.id |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
serializable_match_groups.append(match_group) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if double_butterfly_mode: |
|
|
|
|
|
|
|
for round in main_rounds_reversed: |
|
|
|
|
|
|
|
matches = round.match_set.filter(disabled=False).order_by('index') |
|
|
|
|
|
|
|
next_round = main_rounds_reversed.filter(index=round.index - 1).first() |
|
|
|
|
|
|
|
if next_round: |
|
|
|
|
|
|
|
next_round_matches = next_round.match_set.filter(disabled=False).order_by('index') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
next_round_matches = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(matches) < len(next_round_matches): |
|
|
|
|
|
|
|
all_matches = round.match_set.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 |
|
|
|
|
|
|
|
filtered_matches.append(current_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: |
|
|
|
|
|
|
|
midpoint = int(len(matches) / 2) |
|
|
|
|
|
|
|
first_half_matches = matches[midpoint:] |
|
|
|
|
|
|
|
name = round.name() |
|
|
|
|
|
|
|
if parent_round: |
|
|
|
|
|
|
|
name = first_half_matches[0].computed_name() |
|
|
|
|
|
|
|
match_group = tournament.create_match_group( |
|
|
|
|
|
|
|
name=name, |
|
|
|
|
|
|
|
matches=first_half_matches, |
|
|
|
|
|
|
|
round_id=round.id |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
serializable_match_groups.append(match_group) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context = { |
|
|
|
context = { |
|
|
|
'tournament': tournament, |
|
|
|
'tournament': tournament, |
|
|
|
|