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.
103 lines
3.7 KiB
103 lines
3.7 KiB
from django.db import models
|
|
from . import Match, TeamRegistration, PlayerRegistration, FederalMatchCategory
|
|
import uuid
|
|
|
|
class TeamScore(models.Model):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True)
|
|
match = models.ForeignKey(Match, on_delete=models.CASCADE, related_name="team_scores")
|
|
team_registration = models.ForeignKey(TeamRegistration, on_delete=models.CASCADE, null=True, blank=True)
|
|
score = models.CharField(max_length=50, null=True, blank=True)
|
|
walk_out = models.IntegerField(null=True, blank=True) # TODO type of WO: forfeit, injury...
|
|
lucky_loser = models.IntegerField(null=True, blank=True)
|
|
|
|
def __str__(self):
|
|
return f"{self.match.stage_name()} #{self.match.index}: {self.player_names()}"
|
|
|
|
def player_names(self):
|
|
if self.team_registration:
|
|
if self.team_registration.name:
|
|
return self.team_registration.name
|
|
else:
|
|
names = self.team_registration.team_names()
|
|
return " - ".join(names)
|
|
else:
|
|
return "--"
|
|
|
|
def shortened_team_names(self):
|
|
names = []
|
|
if self.team_registration:
|
|
names = self.team_registration.shortened_team_names()
|
|
return names
|
|
|
|
def team_names(self):
|
|
names = []
|
|
if self.team_registration:
|
|
if self.team_registration.name:
|
|
names.append(self.team_registration.name)
|
|
else:
|
|
names = self.team_registration.team_names()
|
|
return names
|
|
|
|
def scores(self):
|
|
if self.score:
|
|
return [
|
|
int(x.split('-')[0]) # Extract the integer part before the hyphen
|
|
for x in self.score.split(',') # Split by commas for multiple scores
|
|
]
|
|
else:
|
|
return []
|
|
|
|
def parsed_scores(self):
|
|
if self.score:
|
|
return [
|
|
{
|
|
'main': int(x.split('-')[0]), # Main score
|
|
'tiebreak': x.split('-')[1] if '-' in x else None # Tiebreak
|
|
}
|
|
for x in self.score.split(',')
|
|
]
|
|
return []
|
|
|
|
def scores_array(self):
|
|
if self.score:
|
|
return [x for x in self.score.split(',')]
|
|
else:
|
|
return []
|
|
|
|
def estimated_number_of_games(self):
|
|
scores = self.scores()
|
|
format = FederalMatchCategory.NINE_GAMES_DECISIVE_POINT
|
|
games = 0.0
|
|
if self.match.format:
|
|
format = self.match.format
|
|
if format == FederalMatchCategory.TWO_SETS_OF_SUPER_TIE:
|
|
games = sum(scores) / 4
|
|
return games
|
|
if FederalMatchCategory.last_set_is_tie_break(format) and len(scores) == FederalMatchCategory.max_number_of_sets(format):
|
|
points = scores.pop()
|
|
games += points / 4 # we take 4 points in average for a game
|
|
games += sum(scores)
|
|
return games
|
|
|
|
def number_of_games(self):
|
|
scores = self.scores()
|
|
return sum(scores)
|
|
|
|
def live_team(self, match):
|
|
if self.team_registration:
|
|
id = self.team_registration.id
|
|
image = self.team_registration.logo
|
|
weight = self.team_registration.weight
|
|
is_winner = self.team_registration.id == match.winning_team_id
|
|
else:
|
|
id = None
|
|
image = None
|
|
weight= None
|
|
is_winner = False
|
|
names = self.shortened_team_names()
|
|
scores = self.parsed_scores()
|
|
walk_out = self.walk_out
|
|
from .match import Team # Import Team only when needed
|
|
is_lucky_loser = self.lucky_loser is not None
|
|
team = Team(id, image, names, scores, weight, is_winner, walk_out, is_lucky_loser)
|
|
return team
|
|
|