|
|
|
|
@ -52,10 +52,10 @@ class Match(models.Model): |
|
|
|
|
items = [] |
|
|
|
|
if self.round: |
|
|
|
|
items.append(self.round.name()) |
|
|
|
|
items.append(f" #{self.index}") |
|
|
|
|
items.append(f" #{self.index_in_round() + 1}") |
|
|
|
|
elif self.group_stage: |
|
|
|
|
items.append(self.group_stage.name()) |
|
|
|
|
items.append(f"Match #{self.index}") |
|
|
|
|
items.append(f"Match #{self.index + 1}") |
|
|
|
|
return " ".join(items) |
|
|
|
|
|
|
|
|
|
def summon_stage_name(self): |
|
|
|
|
@ -76,9 +76,116 @@ class Match(models.Model): |
|
|
|
|
else: |
|
|
|
|
return '--' |
|
|
|
|
|
|
|
|
|
def get_previous_round(self): |
|
|
|
|
# Calculate the next index |
|
|
|
|
if self.round is None: |
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
previous_index = self.round.index + 1 |
|
|
|
|
|
|
|
|
|
# Check if the next index is within the bounds of the rounds array |
|
|
|
|
if previous_index < len(self.round.tournament.round_set.all()): |
|
|
|
|
return self.round.tournament.round_set.filter(index=previous_index, parent = None).first() |
|
|
|
|
|
|
|
|
|
# Return None or an appropriate value if the index is out of bounds |
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
def precedent_match(self, top): |
|
|
|
|
previous_round = self.get_previous_round() |
|
|
|
|
#print(previous_round) |
|
|
|
|
match = None |
|
|
|
|
if previous_round: |
|
|
|
|
matches = previous_round.match_set.all() # Retrieve the QuerySet |
|
|
|
|
match_index = self.index * 2 + 1 |
|
|
|
|
if top == False: |
|
|
|
|
match_index += 1 |
|
|
|
|
match = matches.filter(index=match_index).first() |
|
|
|
|
return match |
|
|
|
|
|
|
|
|
|
def computed_name(self): |
|
|
|
|
if self.round and self.round.parent is None: |
|
|
|
|
return self.backup_name() |
|
|
|
|
title = self.name if self.name else self.backup_name() |
|
|
|
|
return title |
|
|
|
|
|
|
|
|
|
def index_in_round(self): |
|
|
|
|
if self.round is not None: |
|
|
|
|
matches = sorted( |
|
|
|
|
self.round.match_set.filter(disabled=False), |
|
|
|
|
key=lambda match: match.index |
|
|
|
|
) |
|
|
|
|
try: |
|
|
|
|
index_of_self = matches.index(self) |
|
|
|
|
return index_of_self |
|
|
|
|
except ValueError: |
|
|
|
|
# `self` is not in `matches`, return self.index as a fallback |
|
|
|
|
return self.index |
|
|
|
|
|
|
|
|
|
return self.index |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def player_names(self): |
|
|
|
|
return map(lambda ts: ts.player_names(), self.team_scores.all()) |
|
|
|
|
|
|
|
|
|
def default_live_team(self, names): |
|
|
|
|
image = None |
|
|
|
|
weight= None |
|
|
|
|
is_winner = False |
|
|
|
|
scores = [] |
|
|
|
|
walk_out = None |
|
|
|
|
team = Team(image, names, scores, weight, is_winner, walk_out) |
|
|
|
|
return team |
|
|
|
|
|
|
|
|
|
def live_teams(self): |
|
|
|
|
#print('player names from match') |
|
|
|
|
##return map(lambda ts: ts.player_names(), self.team_scores.all()) |
|
|
|
|
# List to hold the names of the teams |
|
|
|
|
teams = [] |
|
|
|
|
|
|
|
|
|
# Check if team scores exist |
|
|
|
|
team_scores = list(self.team_scores.all()) |
|
|
|
|
previous_top_match = self.precedent_match(True) |
|
|
|
|
previous_bottom_match = self.precedent_match(False) |
|
|
|
|
if len(team_scores) == 0: |
|
|
|
|
if (self.round and len(self.round.tournament.round_set.all()) == self.round.index -1): |
|
|
|
|
return teams |
|
|
|
|
if (self.group_stage): |
|
|
|
|
return teams |
|
|
|
|
|
|
|
|
|
if self.round and self.round.parent: |
|
|
|
|
return teams |
|
|
|
|
|
|
|
|
|
# No team scores at all |
|
|
|
|
if previous_top_match: |
|
|
|
|
names = [f"Gagnants du {previous_top_match.computed_name()}", ''] |
|
|
|
|
team = self.default_live_team(names) |
|
|
|
|
teams.append(team) |
|
|
|
|
if previous_bottom_match: |
|
|
|
|
names = [f"Gagnants du {previous_bottom_match.computed_name()}", ''] |
|
|
|
|
team = self.default_live_team(names) |
|
|
|
|
teams.append(team) |
|
|
|
|
elif len(team_scores) == 1: |
|
|
|
|
# Only one team score, handle missing one |
|
|
|
|
existing_team = team_scores[0].live_team(self) |
|
|
|
|
|
|
|
|
|
if previous_top_match and previous_top_match.disabled == False and previous_top_match.end_date is None: |
|
|
|
|
names = [f"Gagnants du {previous_top_match.computed_name()}", ''] |
|
|
|
|
team = self.default_live_team(names) |
|
|
|
|
teams.append(team) |
|
|
|
|
teams.append(existing_team) |
|
|
|
|
elif previous_bottom_match: |
|
|
|
|
names = [f"Gagnants du {previous_bottom_match.computed_name()}", ''] |
|
|
|
|
team = self.default_live_team(names) |
|
|
|
|
teams.append(existing_team) |
|
|
|
|
teams.append(team) |
|
|
|
|
else: |
|
|
|
|
teams.append(existing_team) |
|
|
|
|
else: |
|
|
|
|
# Both team scores present |
|
|
|
|
teams.extend([team_score.live_team(self) for team_score in team_scores]) |
|
|
|
|
|
|
|
|
|
return teams |
|
|
|
|
|
|
|
|
|
def local_start_date(self): |
|
|
|
|
timezone = self.tournament().timezone() |
|
|
|
|
return self.start_date.astimezone(timezone) |
|
|
|
|
@ -194,7 +301,7 @@ class Match(models.Model): |
|
|
|
|
# return f"{_hours:02d}h{_minutes:02d}min" |
|
|
|
|
|
|
|
|
|
def live_match(self): |
|
|
|
|
title = self.name if self.name else self.backup_name() |
|
|
|
|
title = self.computed_name() |
|
|
|
|
date = self.formatted_start_date() |
|
|
|
|
time_indication = self.time_indication() |
|
|
|
|
court = self.court_name(self.court_index) |
|
|
|
|
@ -205,19 +312,7 @@ class Match(models.Model): |
|
|
|
|
ended = self.end_date is not None |
|
|
|
|
livematch = LiveMatch(title, date, time_indication, court, self.started(), ended, group_stage_name) |
|
|
|
|
|
|
|
|
|
for team_score in self.sorted_team_scores(): |
|
|
|
|
if team_score.team_registration: |
|
|
|
|
image = team_score.team_registration.logo |
|
|
|
|
weight = team_score.team_registration.weight |
|
|
|
|
is_winner = team_score.team_registration.id == self.winning_team_id |
|
|
|
|
else: |
|
|
|
|
image = None |
|
|
|
|
weight= None |
|
|
|
|
is_winner = False |
|
|
|
|
names = team_score.shortened_team_names() |
|
|
|
|
scores = team_score.scores_array() |
|
|
|
|
walk_out = team_score.walk_out |
|
|
|
|
team = Team(image, names, scores, weight, is_winner, walk_out) |
|
|
|
|
for team in self.live_teams(): |
|
|
|
|
livematch.add_team(team) |
|
|
|
|
|
|
|
|
|
return livematch |
|
|
|
|
|