Passed
Push — master ( d83e06...1fde47 )
by manny
02:00 queued 10s
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 Entrant, 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
    cancel_dq_color = 0xFF0000
20
21
    def get_timer_text(self, race: Race, full_name: str):
22
        entrant = race.get_entrant_by_name(full_name)
23
        color = self.racing_color
24
        time = "--:--:--.-"
25
        
26
        # default value used if user is not in race and race is running currently
27
        color, time = self.get_color_and_text_by_started_at(race.started_at, race.status.value, race.ended_at)
28
        
29
        # if race is has not started or cancelled
30
        color, time = self.get_color_and_text_by_race_status(race.status.value, race.start_delay, color, time)
31
32
        # value if user is an entrant in this race
33
        color, time = self.get_color_and_text_by_entrant(entrant, race.started_at, color, time)
34
        if not self.use_podium_colors:
35
            color = None
36
        return color, time
37
38
    def get_color_and_text_by_race_status(self, status_value: str, start_delay: timedelta, fallback_color: int = None, fallback_text: str = None):
39
        if status_value == "open" or status_value == "invitational":
40
            time = self.timer_to_str(start_delay)
41
            color = self.pre_color
42
        elif status_value == "cancelled":
43
            color = self.cancel_dq_color
44
            time = "--:--:--.-"
45
        else:
46
            return fallback_color, fallback_text
47
        return color, time
48
49
    def get_color_and_text_by_started_at(self, started_at: datetime, status_value: str, ended_at: datetime = None, fallback_color: int = None, fallback_text: str = None):
50
        if status_value == "finished":
51
            # race is finished and assume user is not an entrant
52
            time = self.timer_to_str(ended_at - started_at)
53
            color = self.finished_color
54
        elif started_at is not None:
55
            timer = datetime.now(timezone.utc) - started_at
56
            time = self.timer_to_str(timer)
57
            color = self.racing_color
58
        else:
59
            return fallback_color, fallback_text
60
        return color, time
61
        
62
    def get_color_and_text_by_entrant(self, entrant: Entrant = None, started_at: datetime = None, fallback_color: int = None, fallback_text: str = None):
63
        time = fallback_text
64
        color = fallback_color
65
        if entrant is not None:
66
            if entrant.finish_time is not None:
67
                time = self.timer_to_str(entrant.finish_time)
68
                color = self.get_color_by_place(entrant.place)
69
            elif entrant.status.value == "dnf" or entrant.status.value == "dq":
70
                time = "--:--:--.-"
71
                color = self.cancel_dq_color
72
            elif started_at is not None:
73
                timer = datetime.now(timezone.utc) - started_at
74
                time = self.timer_to_str(timer)
75
        return color, time
76
77
    def get_color_by_place(self, place: int) -> int:
78
        if place == 1:
79
            return self.first_color
80
        elif place == 2:
81
            return self.second_color
82
        elif place == 3:
83
            return self.third_color
84
        else:
85
            return self.finished_color
86
87
88
    @staticmethod
89
    def timer_to_str(timer: timedelta) -> str:
90
        if timer.total_seconds() < 0.0:
91
            return "-0:00:{:04.1f}".format(timer.total_seconds() * -1.0)
92
        else:
93
            return str(timer)[:9]
94