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.
 
 
 
 
padelclub_backend/tournaments/models/match.py

118 lines
4.1 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(null=True, blank=True)
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}"
def name(self):
items = []
if self.round:
items.append(self.round.name())
elif self.group_stage:
items.append(self.group_stage.name())
items.append(f"{self.index}")
return " ".join(items)
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 "no date"
# return str(self.start_date) #.strftime("%H:%M")
def current_duration(self):
if self.end_date:
return (self.end_date - self.start_date).total_seconds()
else:
return (timezone.now() - self.start_date).total_seconds()
def durationPrefix(self):
if self.current_duration() > 0:
return "Temps de jeu"
else:
return "Démarrage prévu dans"
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.formatted_duration()
court = ""
if self.court:
court = f"Terrain {self.court}"
livematch = LiveMatch(title, date, duration, court)
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):
self.title = title
self.date = date
self.teams = []
self.duration = duration
self.court = court
def add_team(self, team):
self.teams.append(team)
# def toJSON(self):
# return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)