Passed
Push — master ( b1826e...729ce0 )
by manny
01:49
created

gadgets.qualifier.Qualifier.is_enabled()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import logging
2
from datetime import timedelta
3
4
from models.race import Race
5
from gadgets.timer import Timer
6
7
8
class Qualifier:
9
    logger: logging.Logger = logging.Logger("racetime-obs")
10
    enabled = False
11
    qualifier_cutoff = 3
12
    par_source = ""
13
    score_source = ""
14
    par_text = " "
15
    entrant_score = " "
16
17
    def update_qualifier_text(self, race: Race, full_name: str):
18
        if not self.is_enabled():
19
            return
20
        entrant = race.get_entrant_by_name(full_name)
21
        self.logger.debug(entrant)
22
23
        self.par_text = " "
24
        self.entrant_score = " "
25
        if race.entrants_count_finished >= self.qualifier_cutoff:
26
            par_time = self.calculate_par_time(race)
27
            self.logger.debug(par_time)
28
            self.par_text = Timer.timer_to_str(par_time)
29
30
            if entrant and entrant.finish_time is not None:
31
                self.entrant_score = "{:04.2f}".format(
32
                    2 - (entrant.finish_time / par_time))
33
            self.logger.debug(self.entrant_score)
34
35
    def calculate_par_time(self, race: Race) -> timedelta:
36
        par_time = timedelta(microseconds=0)
37
        for i in range(1, self.qualifier_cutoff + 1):
38
            if race.get_entrant_by_place(i).finish_time is None:
39
                self.logger.error("error: qualifier finish time is None")
40
                return
41
            self.logger.debug(
42
                f"finish time for rank {i} is "
43
                f"{race.get_entrant_by_place(i).finish_time}"
44
            )
45
            par_time += race.get_entrant_by_place(i).finish_time
46
        par_time = par_time / self.qualifier_cutoff
47
        return par_time
48
49
    def is_enabled(self) -> bool:
50
        return (
51
            self.enabled and self.par_source != "" and self.score_source != ""
52
        )
53