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