Passed
Push — master ( d83fa0...73c582 )
by manny
02:10
created

gadgets.qualifier.Qualifier.calculate_par_time()   A

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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