Qualifier.update_qualifier_text_preview()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import logging
2
from datetime import timedelta
3
4
from models.race import Race
5
from helpers import timer_to_str
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
    decimals: bool = True
17
18
    def update_qualifier_text(self, race: Race, full_name: str):
19
        if not self.is_enabled():
20
            return
21
        entrant = race.get_entrant_by_name(full_name)
22
        self.logger.debug(entrant)
23
24
        self.par_text = " "
25
        self.entrant_score = " "
26
        if race.entrants_count_finished >= self.qualifier_cutoff:
27
            par_time = self.calculate_par_time(race)
28
            self.logger.debug(par_time)
29
            self.par_text = timer_to_str(par_time, self.decimals)
30
31
            if entrant and entrant.finish_time is not None:
32
                entrant_score = min(2 - (entrant.finish_time / par_time), 1.05)
33
                self.entrant_score = "{:04.2f}".format(
34
                    entrant_score)
35
            self.logger.debug(self.entrant_score)
36
37
    def calculate_par_time(self, race: Race) -> timedelta:
38
        par_time = timedelta(microseconds=0)
39
        for i in range(1, self.qualifier_cutoff + 1):
40
            if race.get_entrant_by_place(i).finish_time is None:
41
                self.logger.error("error: qualifier finish time is None")
42
                return
43
            self.logger.debug(
44
                f"finish time for rank {i} is "
45
                f"{race.get_entrant_by_place(i).finish_time}"
46
            )
47
            par_time += race.get_entrant_by_place(i).finish_time
48
        par_time = par_time / self.qualifier_cutoff
49
        return par_time
50
51
    def is_enabled(self) -> bool:
52
        return (
53
            self.enabled and self.par_source != "" and self.score_source != ""
54
        )
55
56
    def update_qualifier_text_preview(self):
57
        self.par_text = "1:30:00.0"
58
        self.entrant_score = "0.69"
59