1
|
|
|
from datetime import datetime, timedelta, timezone |
2
|
|
|
import logging |
3
|
|
|
from helpers import timer_to_str |
4
|
|
|
|
5
|
|
|
from models.race import Entrant, Race |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class Coop: |
9
|
|
|
logger: logging.Logger = logging.Logger("racetime-obs") |
10
|
|
|
enabled = False |
11
|
|
|
partner = None |
12
|
|
|
opponent2 = None |
13
|
|
|
opponent1 = None |
14
|
|
|
our_time_source = None |
15
|
|
|
opponent_time_source = None |
16
|
|
|
our_time_text = " " |
17
|
|
|
opponent_time_text = " " |
18
|
|
|
our_time_color: int = 0x000000 |
19
|
|
|
opponent_time_color: int = 0x000000 |
20
|
|
|
winner_color = 0xFF00FF00 |
21
|
|
|
loser_color = 0xFF0000FF |
22
|
|
|
still_racing_color = 0xFFFFFFFF |
23
|
|
|
|
24
|
|
|
def update_coop_text(self, race: Race, full_name): |
25
|
|
|
if not self.is_enabled(): |
26
|
|
|
return |
27
|
|
|
|
28
|
|
|
entrant = race.get_entrant_by_name(full_name) |
29
|
|
|
partner = race.get_entrant_by_name(self.partner) |
30
|
|
|
opponent1 = race.get_entrant_by_name(self.opponent1) |
31
|
|
|
opponent2 = race.get_entrant_by_name(self.opponent2) |
32
|
|
|
|
33
|
|
|
our_total = self.get_coop_times(entrant, partner) |
34
|
|
|
opponent_total = self.get_coop_times(opponent1, opponent2) |
35
|
|
|
|
36
|
|
|
if our_total and opponent_total: |
37
|
|
|
self.update_text_if_both_finished(our_total, opponent_total) |
38
|
|
|
elif our_total and not opponent_total: |
39
|
|
|
self.update_text_if_we_finished( |
40
|
|
|
race, our_total, opponent1, opponent2) |
41
|
|
|
elif opponent_total and not our_total: |
42
|
|
|
self.update_text_if_they_finished( |
43
|
|
|
race, opponent_total, entrant, partner) |
44
|
|
|
else: |
45
|
|
|
self.our_time_text = " " |
46
|
|
|
self.our_time_color = self.still_racing_color |
47
|
|
|
self.opponent_time_text = " " |
48
|
|
|
self.opponent_time_color = self.still_racing_color |
49
|
|
|
|
50
|
|
|
def update_text_if_they_finished( |
51
|
|
|
self, race: Race, opponent_total, entrant: Entrant, partner: Entrant |
52
|
|
|
): |
53
|
|
|
current_timer = datetime.now(timezone.utc) - race.started_at |
54
|
|
|
entrant_timer = self.get_finish_time_or_default(entrant, current_timer) |
55
|
|
|
partner_timer = self.get_finish_time_or_default(partner, current_timer) |
56
|
|
|
our_current_total = entrant_timer + partner_timer |
57
|
|
|
if our_current_total > opponent_total: |
58
|
|
|
self.opponent_time_color = self.winner_color |
59
|
|
|
self.our_time_color = self.loser_color |
60
|
|
|
else: |
61
|
|
|
our_current_total = opponent_total - our_current_total |
62
|
|
|
self.opponent_time_color = self.still_racing_color |
63
|
|
|
self.our_time_color = self.still_racing_color |
64
|
|
|
self.our_time_text = timer_to_str(our_current_total/2) |
65
|
|
|
self.opponent_time_text = timer_to_str(opponent_total/2) |
66
|
|
|
|
67
|
|
|
def update_text_if_we_finished( |
68
|
|
|
self, race: Race, our_total, opponent1: Entrant, opponent2: Entrant |
69
|
|
|
): |
70
|
|
|
current_timer = datetime.now(timezone.utc) - race.started_at |
71
|
|
|
opp1_timer = self.get_finish_time_or_default(opponent1, current_timer) |
72
|
|
|
opp2_timer = self.get_finish_time_or_default(opponent2, current_timer) |
73
|
|
|
opponent_current_total = opp1_timer + opp2_timer |
74
|
|
|
if opponent_current_total > our_total: |
75
|
|
|
self.opponent_time_color = self.loser_color |
76
|
|
|
self.our_time_color = self.winner_color |
77
|
|
|
else: |
78
|
|
|
opponent_current_total = our_total - opponent_current_total |
79
|
|
|
self.opponent_time_color = self.still_racing_color |
80
|
|
|
self.our_time_color = self.still_racing_color |
81
|
|
|
self.our_time_text = timer_to_str(our_total/2) |
82
|
|
|
self.opponent_time_text = timer_to_str(opponent_current_total/2) |
83
|
|
|
|
84
|
|
|
@staticmethod |
85
|
|
|
def get_finish_time_or_default(player: Entrant, default: timedelta): |
86
|
|
|
if player and player.finish_time: |
87
|
|
|
return player.finish_time |
88
|
|
|
else: |
89
|
|
|
return default |
90
|
|
|
|
91
|
|
|
def update_text_if_both_finished(self, our_total, opponent_total): |
92
|
|
|
if our_total and opponent_total: |
93
|
|
|
self.our_time_text = timer_to_str(our_total/2) |
94
|
|
|
self.opponent_time_text = timer_to_str(opponent_total/2) |
95
|
|
|
if our_total > opponent_total: |
96
|
|
|
self.our_time_color = self.loser_color |
97
|
|
|
self.opponent_time_color = self.winner_color |
98
|
|
|
elif our_total < opponent_total: |
99
|
|
|
self.opponent_time_color = self.loser_color |
100
|
|
|
self.our_time_color = self.winner_color |
101
|
|
|
else: |
102
|
|
|
# an exact tie?!?! |
103
|
|
|
self.opponent_time_color = self.still_racing_color |
104
|
|
|
self.our_time_color = self.still_racing_color |
105
|
|
|
|
106
|
|
|
@staticmethod |
107
|
|
|
def get_coop_text(label_text_start: str, finished_partner: Entrant, |
108
|
|
|
finished1: Entrant, finished2: Entrant, |
109
|
|
|
current_timer: timedelta): |
110
|
|
|
finished_team_total = finished1.finish_time + finished2.finish_time |
111
|
|
|
time_to_beat = finished_team_total - finished_partner.finish_time |
112
|
|
|
if time_to_beat > current_timer: |
113
|
|
|
coop_text = timer_to_str(time_to_beat) |
114
|
|
|
coop_label_text = label_text_start + "to finish before" |
115
|
|
|
else: |
116
|
|
|
coop_label_text = str.format( |
117
|
|
|
f"{finished1.user.name} and {finished2.user.name} won") |
118
|
|
|
coop_text = timer_to_str(finished_team_total / 2) |
119
|
|
|
return coop_label_text, coop_text |
120
|
|
|
|
121
|
|
|
def get_coop_times(self, player1: Entrant, player2: Entrant): |
122
|
|
|
team_total = None |
123
|
|
|
if player1 and player1.finish_time and player2 and player2.finish_time: |
124
|
|
|
team_total = player1.finish_time + player2.finish_time |
125
|
|
|
return team_total |
126
|
|
|
|
127
|
|
|
def is_enabled(self) -> bool: |
128
|
|
|
return ( |
129
|
|
|
self.enabled and self.opponent_time_source != "" |
130
|
|
|
and self.our_time_source != "" # and |
131
|
|
|
# self.partner is not None and self.opponent1 is not None and |
132
|
|
|
# self.opponent2 is not None |
133
|
|
|
) |
134
|
|
|
|