From 061ecdea4a914b8e2b44a1910f5a1317a1e95cd8 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 24 Jun 2024 17:57:45 +0200 Subject: [PATCH] Adds system to fix duration if they are not plausible --- tournaments/models/match.py | 52 +++++++++++++++++++++++++------- tournaments/models/team_score.py | 4 +++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/tournaments/models/match.py b/tournaments/models/match.py index fca8c42..5b396b9 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -5,6 +5,8 @@ from django.utils import timezone, formats from datetime import timedelta import uuid +from ..utils.extensions import format_seconds + 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) @@ -88,7 +90,7 @@ class Match(models.Model): def time_indication(self): if self.end_date: if self.start_date: - return self.formatted_duration() + return self.magic_duration() else: return '' elif self.start_date: @@ -100,12 +102,36 @@ class Match(models.Model): else: return 'À venir...' + def magic_duration(self): + seconds = (self.end_date - self.start_date).total_seconds() + average_duration = self.average_seconds_duration() + # print(f"{average_duration} / {seconds}") + + if (average_duration / 2) > seconds or seconds > (average_duration * 2): + seconds = average_duration + + return format_seconds(seconds) + + def average_seconds_duration(self): + return 3 * 60 * self.total_number_of_games() + + def total_number_of_games(self): + games = 0 + for team_score in self.team_scores.all(): + games += team_score.number_of_games() + return games + def current_duration(self): if self.start_date: if self.end_date: return (self.end_date - self.start_date).total_seconds() else: - return (timezone.now() - self.start_date).total_seconds() + current = (timezone.now() - self.start_date).total_seconds() + if current < self.average_seconds_duration() * 2: + return current + else: + return None + # return (timezone.now() - self.start_date).total_seconds() else: return 0 @@ -127,17 +153,21 @@ class Match(models.Model): def formatted_duration(self): - _seconds = self.current_duration() + seconds = self.current_duration() + if seconds is None: + return '' - if _seconds > 0: - _hours = int(_seconds / 3600) - _minutes = int((_seconds % 3600) / 60) - return f"{_hours:02d}h{_minutes:02d}min" + if seconds > 0: + return format_seconds(seconds) + # _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" + seconds = seconds * -1 + return format_seconds(seconds) + # _hours = int(_seconds / 3600) + # _minutes = int((_seconds % 3600) / 60) + # return f"{_hours:02d}h{_minutes:02d}min" def live_match(self): title = self.name if self.name else self.backup_name() diff --git a/tournaments/models/team_score.py b/tournaments/models/team_score.py index 73db17e..dc867eb 100644 --- a/tournaments/models/team_score.py +++ b/tournaments/models/team_score.py @@ -40,3 +40,7 @@ class TeamScore(models.Model): return [x for x in self.score.split(',')] else: return [] + + def number_of_games(self): + scores = self.scores() + return sum(scores)