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

gadgets.timer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 21

7 Methods

Rating   Name   Duplication   Size   Complexity  
A Timer.get_timer_text() 0 20 2
A Timer.get_color_by_place() 0 9 4
B Timer.get_color_and_text_by_entrant() 0 17 6
A Timer.get_color_and_text_by_started_at() 0 16 3
A Timer.get_color_and_text_by_race_status() 0 13 4
A Timer.is_enabled() 0 2 1
A Timer.get_timer_text_preview() 0 2 1
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
24
    def get_timer_text(self, race: Race, full_name: str):
25
        entrant = race.get_entrant_by_name(full_name)
26
        color = self.racing_color
27
        time = "--:--:--.-"
28
29
        # default value used if user is not in race and race is running
30
        #  currently
31
        color, time = self.get_color_and_text_by_started_at(
32
            race.started_at, race.status.value, race.ended_at)
33
34
        # if race is has not started or cancelled
35
        color, time = self.get_color_and_text_by_race_status(
36
            race.status.value, race.start_delay, color, time)
37
38
        # value if user is an entrant in this race
39
        color, time = self.get_color_and_text_by_entrant(
40
            entrant, race.started_at, color, time)
41
        if not self.use_podium_colors:
42
            color = None
43
        return color, time
44
45
    def get_color_and_text_by_race_status(
46
        self, status_value: str, start_delay: timedelta,
47
        fallback_color: int = None, fallback_text: str = None
48
    ):
49
        if status_value == "open" or status_value == "invitational":
50
            time = timer_to_str(start_delay * -1.0)
51
            color = self.pre_color
52
        elif status_value == "cancelled":
53
            color = self.cancel_dq_color
54
            time = "--:--:--.-"
55
        else:
56
            return fallback_color, fallback_text
57
        return color, time
58
59
    def get_color_and_text_by_started_at(
60
        self, started_at: datetime, status_value: str,
61
        ended_at: datetime = None, fallback_color: int = None,
62
        fallback_text: str = None
63
    ):
64
        if status_value == "finished":
65
            # race is finished and assume user is not an entrant
66
            time = timer_to_str(ended_at - started_at)
67
            color = self.finished_color
68
        elif started_at is not None:
69
            timer = datetime.now(timezone.utc) - started_at
70
            time = timer_to_str(timer)
71
            color = self.racing_color
72
        else:
73
            return fallback_color, fallback_text
74
        return color, time
75
76
    def get_color_and_text_by_entrant(
77
        self, entrant: Entrant = None, started_at: datetime = None,
78
        fallback_color: int = None, fallback_text: str = None
79
    ):
80
        time = fallback_text
81
        color = fallback_color
82
        if entrant is not None:
83
            if entrant.finish_time is not None:
84
                time = timer_to_str(entrant.finish_time)
85
                color = self.get_color_by_place(entrant.place)
86
            elif entrant.status.value == "dnf" or entrant.status.value == "dq":
87
                time = "--:--:--.-"
88
                color = self.cancel_dq_color
89
            elif started_at is not None:
90
                timer = datetime.now(timezone.utc) - started_at
91
                time = timer_to_str(timer)
92
        return color, time
93
94
    def get_color_by_place(self, place: int) -> int:
95
        if place == 1:
96
            return self.first_color
97
        elif place == 2:
98
            return self.second_color
99
        elif place == 3:
100
            return self.third_color
101
        else:
102
            return self.finished_color
103
104
    def is_enabled(self) -> bool:
105
        return self.enabled and self.source_name != ""
106
107
    def get_timer_text_preview(self):
108
        return self.racing_color, "1:23:45.6"
109