Passed
Push — master ( 6e56fb...e44069 )
by manny
01:57
created

gadgets.timer.Timer.timer_to_str()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
from datetime import datetime, timedelta, timezone
2
from models.race import Race
3
import logging
4
5
# ------------------------------------------------------------
6
7
class Timer:
8
    logger = logging.Logger("racetime-obs")
9
    enabled: bool = False
10
    source_name = ""
11
    timer_text = ""
12
    use_podium_colors = False
13
    pre_color = 0xFFFFFF
14
    racing_color = 0xFFFFFF
15
    first_color = 0xFF0000
16
    second_color = 0x00FF00
17
    third_color = 0x0000FF
18
    finished_color = 0xFFFFFF
19
20
    def get_timer_text(self, race: Race, full_name: str):
21
        entrant = race.get_entrant_by_name(full_name)
22
        color = self.racing_color
23
        time = "--:--:--.-"
24
        if race.status.value == "open" or race.status.value == "invitational":
25
            time = self.timer_to_str(race.start_delay)
26
            color = self.pre_color
27
        elif entrant is not None:
28
            if entrant.finish_time is not None:
29
                time = self.timer_to_str(entrant.finish_time)
30
                color = self.get_color(entrant.place)
31
            elif entrant.status.value == "dnf" or entrant.status.value == "dq" or race.status.value == "cancelled":
32
                color = 0xFF0000
33
            elif race.started_at is not None:
34
                timer = datetime.now(timezone.utc) - race.started_at
35
                time = self.timer_to_str(timer)
36
        elif race.status.value == "finished":
37
            # race is finished and our user is not an entrant
38
            time = self.timer_to_str(race.ended_at - race.started_at)
39
        elif race.started_at is not None:
40
            timer = datetime.now(timezone.utc) - race.started_at
41
            time = self.timer_to_str(timer)
42
        else:
43
            return
44
        if not self.use_podium_colors:
45
            color = None
46
        return color, time
47
48
49
    def get_color(self, place: int) -> int:
50
        if place == 1:
51
            return self.first_color
52
        elif place == 2:
53
            return self.second_color
54
        elif place == 3:
55
            return self.third_color
56
        else:
57
            return self.finished_color
58
59
60
    @staticmethod
61
    def timer_to_str(timer: timedelta) -> str:
62
        if timer.total_seconds() < 0.0:
63
            return "-0:00:{:04.1f}".format(timer.total_seconds() * -1.0)
64
        else:
65
            return str(timer)[:9]
66
67
    
68
69
70
    
71
72
73
74
75
76
# ------------------------------------------------------------