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.
142 lines
4.7 KiB
142 lines
4.7 KiB
from django.db import models
|
|
from . import Round, GroupStage, FederalMatchCategory
|
|
from django.utils import timezone
|
|
import uuid
|
|
|
|
class Match(models.Model):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True)
|
|
round = models.ForeignKey(Round, null=True, blank=True, on_delete=models.CASCADE)
|
|
group_stage = models.ForeignKey(GroupStage, null=True, blank=True, on_delete=models.CASCADE)
|
|
start_date = models.DateTimeField(null=True, blank=True)
|
|
end_date = models.DateTimeField(null=True, blank=True)
|
|
index = models.IntegerField(default=0)
|
|
format = models.IntegerField(default=FederalMatchCategory.NINE_GAMES, choices=FederalMatchCategory.choices, null=True, blank=True)
|
|
court = models.CharField(max_length=50, null=True, blank=True)
|
|
serving_team_id = models.UUIDField(null=True, blank=True)
|
|
winning_team_id = models.UUIDField(null=True, blank=True)
|
|
losing_team_id = models.UUIDField(null=True, blank=True)
|
|
broadcasted = models.BooleanField(default=False)
|
|
|
|
def __str__(self):
|
|
|
|
items = [self.name(), self.formatted_start_date()]
|
|
desc = " - ".join(items)
|
|
player_names = " / ".join(self.player_names())
|
|
if self.round:
|
|
return f"{desc} > {player_names}"
|
|
elif self.group_stage:
|
|
return f"{desc} > {player_names}"
|
|
else:
|
|
return "--"
|
|
|
|
def name(self):
|
|
items = []
|
|
if self.round:
|
|
items.append(self.round.name())
|
|
items.append(f" #{self.index}")
|
|
elif self.group_stage:
|
|
items.append(self.group_stage.name())
|
|
items.append(f"Match #{self.index}")
|
|
return " ".join(items)
|
|
|
|
def stage_name(self):
|
|
if self.round:
|
|
return self.round.name()
|
|
elif self.group_stage:
|
|
return self.group_stage.name()
|
|
else:
|
|
return '--'
|
|
|
|
def player_names(self):
|
|
return map(lambda ts: ts.player_names(), self.team_scores.all())
|
|
|
|
def formatted_start_date(self):
|
|
if self.start_date:
|
|
return self.start_date.strftime("%H:%M")
|
|
else:
|
|
return ''
|
|
# return str(self.start_date) #.strftime("%H:%M")
|
|
|
|
def time_indication(self):
|
|
if self.end_date:
|
|
return self.formatted_duration()
|
|
elif self.start_date:
|
|
if self.started():
|
|
return self.formatted_duration()
|
|
else:
|
|
start = self.start_date.strftime("%H:%M")
|
|
return f"Prévu à {start}"
|
|
else:
|
|
return 'À venir...'
|
|
|
|
def current_duration(self):
|
|
if self.end_date:
|
|
return (self.end_date - self.start_date).total_seconds()
|
|
elif self.start_date:
|
|
return (timezone.now() - self.start_date).total_seconds()
|
|
else:
|
|
return 0
|
|
|
|
def started(self):
|
|
if self.start_date:
|
|
return timezone.now() > self.start_date
|
|
else:
|
|
return False
|
|
|
|
def formatted_duration(self):
|
|
|
|
_seconds = self.current_duration()
|
|
|
|
if _seconds > 0:
|
|
_hours = int(_seconds / 3600)
|
|
_minutes = int((_seconds % 3600) / 60)
|
|
return f"{_hours:02d}h{_minutes:02d}min"
|
|
else :
|
|
_seconds = _seconds * -1
|
|
_hours = int(_seconds / 3600)
|
|
_minutes = int((_seconds % 3600) / 60)
|
|
return f"{_hours:02d}h{_minutes:02d}min"
|
|
|
|
# def seconds(self):
|
|
# return (timezone.now() - self.start_date).total_seconds()
|
|
|
|
def live_match(self):
|
|
title = self.name()
|
|
date = self.formatted_start_date()
|
|
duration = self.time_indication()
|
|
court = ""
|
|
if self.court:
|
|
court = f"Terrain {self.court}"
|
|
|
|
livematch = LiveMatch(title, date, duration, court, self.started())
|
|
|
|
for team_score in self.team_scores.all():
|
|
image = team_score.team_registration.logo
|
|
names = team_score.team_names()
|
|
scores = team_score.scores_array()
|
|
weight = team_score.team_registration.weight()
|
|
is_winner = team_score.team_registration.id == self.winning_team_id
|
|
team = Team(image, names, scores, weight, is_winner)
|
|
livematch.add_team(team)
|
|
|
|
return livematch
|
|
|
|
class Team:
|
|
def __init__(self, image, names, scores, weight, is_winner):
|
|
self.image = image
|
|
self.names = names
|
|
self.scores = scores
|
|
self.weight = weight
|
|
self.is_winner = is_winner
|
|
|
|
class LiveMatch:
|
|
def __init__(self, title, date, duration, court, started):
|
|
self.title = title
|
|
self.date = date
|
|
self.teams = []
|
|
self.duration = duration
|
|
self.court = court
|
|
self.started = started
|
|
|
|
def add_team(self, team):
|
|
self.teams.append(team)
|
|
|