gadgets.timer.Timer.get_timer_text()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.75
c 0
b 0
f 0
cc 2
nop 3
1
import logging
2
from datetime import datetime, timedelta, timezone
3
4
from models.race import Entrant, Race
5
from helpers import timer_to_str
6
7
# ------------------------------------------------------------
8
9
10
class Timer:
11
    logger: logging.Logger = None
12
    enabled: bool = False
13
    source_name = ""
14
    timer_text = ""
15
    use_podium_colors = False
16
    pre_color = 0xFFFFFF
17
    racing_color = 0xFFFFFF
18
    first_color = 0xFF0000
19
    second_color = 0x00FF00
20
    third_color = 0x0000FF
21
    finished_color = 0xFFFFFF
22
    cancel_dq_color = 0xFF0000
23
    decimals: bool = True
24
25
    def get_timer_text(self, race: Race, full_name: str):
26
        entrant = race.get_entrant_by_name(full_name)
27
        color = self.racing_color
28
        time = "--:--:--.-"
29
30
        # default value used if user is not in race and race is running
31
        #  currently
32
        color, time = self.get_color_and_text_by_started_at(
33
            race.started_at, race.status.value, race.ended_at)
34
35
        # if race is has not started or cancelled
36
        color, time = self.get_color_and_text_by_race_status(
37
            race.status.value, race.start_delay, color, time)
38
39
        # value if user is an entrant in this race
40
        color, time = self.get_color_and_text_by_entrant(
41
            entrant, race.started_at, color, time)
42
        if not self.use_podium_colors:
43
            color = None
44
        return color, time
45
46
    def get_color_and_text_by_race_status(
47
        self, status_value: str, start_delay: timedelta,
48
        fallback_color: int = None, fallback_text: str = None
49
    ):
50
        if status_value == "open" or status_value == "invitational":
51
            time = timer_to_str(start_delay * -1.0, self.decimals)
52
            color = self.pre_color
53
        elif status_value == "cancelled":
54
            color = self.cancel_dq_color
55
            time = "--:--:--.-"
56
        else:
57
            return fallback_color, fallback_text
58
        return color, time
59
60
    def get_color_and_text_by_started_at(
61
        self, started_at: datetime, status_value: str,
62
        ended_at: datetime = None, fallback_color: int = None,
63
        fallback_text: str = None
64
    ):
65
        if status_value == "finished":
66
            # race is finished and assume user is not an entrant
67
            time = timer_to_str(ended_at - started_at, self.decimals)
68
            color = self.finished_color
69
        elif started_at is not None:
70
            timer = datetime.now(timezone.utc) - started_at
71
            time = timer_to_str(timer, self.decimals)
72
            color = self.racing_color
73
        else:
74
            return fallback_color, fallback_text
75
        return color, time
76
77
    def get_color_and_text_by_entrant(
78
        self, entrant: Entrant = None, started_at: datetime = None,
79
        fallback_color: int = None, fallback_text: str = None
80
    ):
81
        time = fallback_text
82
        color = fallback_color
83
        if entrant is not None:
84
            if entrant.finish_time is not None:
85
                time = timer_to_str(entrant.finish_time, self.decimals)
86
                color = self.get_color_by_place(entrant.place)
87
            elif entrant.status.value == "dnf" or entrant.status.value == "dq":
88
                time = "--:--:--.-"
89
                color = self.cancel_dq_color
90
            elif started_at is not None:
91
                timer = datetime.now(timezone.utc) - started_at
92
                time = timer_to_str(timer, self.decimals)
93
        return color, time
94
95
    def get_color_by_place(self, place: int) -> int:
96
        if place == 1:
97
            return self.first_color
98
        elif place == 2:
99
            return self.second_color
100
        elif place == 3:
101
            return self.third_color
102
        else:
103
            return self.finished_color
104
105
    def is_enabled(self) -> bool:
106
        return self.enabled and self.source_name != ""
107
108
    def get_timer_text_preview(self):
109
        return (
110
            self.racing_color,
111
            timer_to_str(
112
                timedelta(hours=1, minutes=23, seconds=45.6),
113
                self.decimals)
114
        )
115