Passed
Push — master ( e155f9...ca7331 )
by manny
47s queued 11s
created

Qualifier.update_qualifier_text_preview()   A

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
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_to_str(par_time)
29
30
            if entrant and entrant.finish_time is not None:
31
                entrant_score = min(2 - (entrant.finish_time / par_time), 1.05)
32
                self.entrant_score = "{:04.2f}".format(
33
                    entrant_score)
34
            self.logger.debug(self.entrant_score)
35
36
    def calculate_par_time(self, race: Race) -> timedelta:
37
        par_time = timedelta(microseconds=0)
38
        for i in range(1, self.qualifier_cutoff + 1):
39
            if race.get_entrant_by_place(i).finish_time is None:
40
                self.logger.error("error: qualifier finish time is None")
41
                return
42
            self.logger.debug(
43
                f"finish time for rank {i} is "
44
                f"{race.get_entrant_by_place(i).finish_time}"
45
            )
46
            par_time += race.get_entrant_by_place(i).finish_time
47
        par_time = par_time / self.qualifier_cutoff
48
        return par_time
49
50
    def is_enabled(self) -> bool:
51
        return (
52
            self.enabled and self.par_source != "" and self.score_source != ""
53
        )
54
55
    def update_qualifier_text_preview(self):
56
        self.par_text = "1:30:00.0"
57
        self.entrant_score = "0.69"
58