From f42d8ba3940fcfada6ef5b6d36b725a4421bc65e Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 25 Jun 2024 14:06:04 +0200 Subject: [PATCH] Fix estimation if the last set --- tournaments/models/enums.py | 12 ++++++++++++ tournaments/models/match.py | 4 ++-- tournaments/models/team_score.py | 14 +++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tournaments/models/enums.py b/tournaments/models/enums.py index 78a335a..76a340b 100644 --- a/tournaments/models/enums.py +++ b/tournaments/models/enums.py @@ -44,3 +44,15 @@ class FederalMatchCategory(models.IntegerChoices): TWO_SETS_DECISIVE_POINT_SUPER_TIE = 7, 'Two Sets with decisive point and super tie-break' TWO_SETS_FOUR_GAME_DECISIVE_POINT = 8, 'Two sets of four games with decisive point' NINE_GAMES_DECISIVE_POINT = 9, 'Nine games with decisive point' + + def last_set_is_tie_break(value): + if value == FederalMatchCategory.TWO_SETS_FOUR_GAME or value == FederalMatchCategory.TWO_SETS_FOUR_GAME_DECISIVE_POINT or value == FederalMatchCategory.TWO_SETS_SUPER_TIE or value == FederalMatchCategory.SUPER_TIE or value == FederalMatchCategory.MEGA_TIE or value == FederalMatchCategory.TWO_SETS_DECISIVE_POINT_SUPER_TIE: + return True + else: + return False + + def max_number_of_sets(value): + if value == FederalMatchCategory.SUPER_TIE or value == FederalMatchCategory.MEGA_TIE or value == FederalMatchCategory.NINE_GAMES or value == FederalMatchCategory.NINE_GAMES_DECISIVE_POINT: + return 1 + else: + return 3 diff --git a/tournaments/models/match.py b/tournaments/models/match.py index 5b396b9..04a1116 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -116,9 +116,9 @@ class Match(models.Model): return 3 * 60 * self.total_number_of_games() def total_number_of_games(self): - games = 0 + games = 0.0 for team_score in self.team_scores.all(): - games += team_score.number_of_games() + games += team_score.estimated_number_of_games() return games def current_duration(self): diff --git a/tournaments/models/team_score.py b/tournaments/models/team_score.py index dbed600..7bea595 100644 --- a/tournaments/models/team_score.py +++ b/tournaments/models/team_score.py @@ -1,5 +1,5 @@ from django.db import models -from . import Match, TeamRegistration, PlayerRegistration +from . import Match, TeamRegistration, PlayerRegistration, FederalMatchCategory import uuid class TeamScore(models.Model): @@ -44,6 +44,18 @@ class TeamScore(models.Model): 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 FederalMatchCategory.last_set_is_tie_break(format) and len(scores) == FederalMatchCategory.max_number_of_sets(format): + points = scores.pop() + games += points / 7 # we take 7 points in average for a game + games += sum(scores) + return games + def number_of_games(self): scores = self.scores() return sum(scores)