-
Notifications
You must be signed in to change notification settings - Fork 104
/
main.py
292 lines (248 loc) · 11.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import time
from typing import Callable, NoReturn
from data.screens import ScreenType
import debug
from data import Data, status
from data.scoreboard import Scoreboard
from data.scoreboard.postgame import Postgame
from data.scoreboard.pregame import Pregame
from renderers import network, offday, standings
from renderers.games import game as gamerender
from renderers.games import irregular
from renderers.games import postgame as postgamerender
from renderers.games import pregame as pregamerender
from renderers.games import teams
# TODO(BMW) make configurable time?
STANDINGS_NEWS_SWITCH_TIME = 120
class MainRenderer:
def __init__(self, matrix, data):
self.matrix = matrix
self.data: Data = data
self.is_playoffs = self.data.schedule.date > self.data.headlines.important_dates.playoffs_start_date
self.canvas = matrix.CreateFrameCanvas()
self.scrolling_text_pos = self.canvas.width
self.game_changed_time = time.time()
self.animation_time = 0
self.standings_stat = "w"
self.standings_league = "NL"
def render(self):
screen = self.data.get_screen_type()
# display the news ticker
if screen == ScreenType.ALWAYS_NEWS:
self.__draw_news(permanent_cond)
# display the standings
elif screen == ScreenType.ALWAYS_STANDINGS:
self.__render_standings()
elif screen == ScreenType.LEAGUE_OFFDAY:
self.__render_offday(team_offday=False)
elif screen == ScreenType.PREFERRED_TEAM_OFFDAY:
self.__render_offday(team_offday=True)
# Playball!
else:
self.__render_gameday()
def __render_offday(self, team_offday=True) -> NoReturn:
if team_offday:
news = self.data.config.news_ticker_team_offday
standings = self.data.config.standings_team_offday
else:
news = True
standings = self.data.config.standings_mlb_offday
if news and standings:
while True:
self.__draw_news(timer_cond(STANDINGS_NEWS_SWITCH_TIME))
self.__draw_standings(timer_cond(STANDINGS_NEWS_SWITCH_TIME))
elif news:
self.__draw_news(permanent_cond)
else:
self.__render_standings()
def __render_standings(self) -> NoReturn:
self.__draw_standings(permanent_cond)
# Out of season off days don't always return standings so fall back on the news renderer
debug.error("No standings data. Falling back to news.")
self.__draw_news(permanent_cond)
# Renders a game screen based on it's status
# May also call draw_offday or draw_standings if there are no games
def __render_gameday(self) -> NoReturn:
refresh_rate = self.data.config.scrolling_speed
while True:
if not self.data.schedule.games_live():
if self.data.config.news_no_games and self.data.config.standings_no_games:
self.__draw_news(all_of(timer_cond(STANDINGS_NEWS_SWITCH_TIME), self.no_games_cond))
self.__draw_standings(all_of(timer_cond(STANDINGS_NEWS_SWITCH_TIME), self.no_games_cond))
continue
elif self.data.config.news_no_games:
self.__draw_news(self.no_games_cond)
elif self.data.config.standings_no_games:
self.__draw_standings(self.no_games_cond)
if self.game_changed_time < self.data.game_changed_time:
self.scrolling_text_pos = self.canvas.width
self.data.scrolling_finished = False
self.game_changed_time = time.time()
# Draw the current game
self.__draw_game()
# Check if we need to scroll until it's finished
if not self.data.config.rotation_scroll_until_finished:
self.data.scrolling_finished = True
time.sleep(refresh_rate)
# Draws the provided game on the canvas
def __draw_game(self):
game = self.data.current_game
bgcolor = self.data.config.scoreboard_colors.color("default.background")
self.canvas.Fill(bgcolor["r"], bgcolor["g"], bgcolor["b"])
scoreboard = Scoreboard(game)
layout = self.data.config.layout
colors = self.data.config.scoreboard_colors
teams.render_team_banner(
self.canvas,
layout,
self.data.config.team_colors,
scoreboard.home_team,
scoreboard.away_team,
self.data.config.full_team_names,
self.data.config.short_team_names_for_runs_hits,
show_score=not status.is_pregame(game.status()),
)
if status.is_pregame(game.status()): # Draw the pregame information
self.__max_scroll_x(layout.coords("pregame.scrolling_text"))
pregame = Pregame(game, self.data.config.time_format)
pos = pregamerender.render_pregame(
self.canvas,
layout,
colors,
pregame,
self.scrolling_text_pos,
self.data.config.pregame_weather,
self.is_playoffs,
)
self.__update_scrolling_text_pos(pos, self.canvas.width)
elif status.is_complete(game.status()): # Draw the game summary
self.__max_scroll_x(layout.coords("final.scrolling_text"))
final = Postgame(game)
pos = postgamerender.render_postgame(
self.canvas, layout, colors, final, scoreboard, self.scrolling_text_pos, self.is_playoffs
)
self.__update_scrolling_text_pos(pos, self.canvas.width)
elif status.is_irregular(game.status()): # Draw game status
short_text = self.data.config.layout.coords("status.text")["short_text"]
if scoreboard.get_text_for_reason():
self.__max_scroll_x(layout.coords("status.scrolling_text"))
pos = irregular.render_irregular_status(
self.canvas, layout, colors, scoreboard, short_text, self.scrolling_text_pos
)
self.__update_scrolling_text_pos(pos, self.canvas.width)
else:
irregular.render_irregular_status(self.canvas, layout, colors, scoreboard, short_text)
self.data.scrolling_finished = True
else: # draw a live game
if scoreboard.homerun() or scoreboard.strikeout():
self.animation_time += 1
else:
self.animation_time = 0
loop_point = self.data.config.layout.coords("atbat")["loop"]
self.scrolling_text_pos = min(self.scrolling_text_pos, loop_point)
pos = gamerender.render_live_game(
self.canvas, layout, colors, scoreboard, self.scrolling_text_pos, self.animation_time
)
self.__update_scrolling_text_pos(pos, loop_point)
# Show network issues
if self.data.network_issues:
network.render_network_error(self.canvas, layout, colors)
self.canvas = self.matrix.SwapOnVSync(self.canvas)
def __draw_news(self, cond: Callable[[], bool]):
"""
Draw the news screen for as long as cond returns True
"""
color = self.data.config.scoreboard_colors.color("default.background")
while cond():
self.canvas.Fill(color["r"], color["g"], color["b"])
self.__max_scroll_x(self.data.config.layout.coords("offday.scrolling_text"))
pos = offday.render_offday_screen(
self.canvas,
self.data.config.layout,
self.data.config.scoreboard_colors,
self.data.weather,
self.data.headlines,
self.data.config.time_format,
self.scrolling_text_pos,
)
# todo make scrolling_text_pos something persistent/news-specific
# if we want to show news as part of rotation?
# not strictly necessary but would be nice, avoids only seeing first headline over and over
self.__update_scrolling_text_pos(pos, self.canvas.width)
# Show network issues
if self.data.network_issues:
network.render_network_error(self.canvas, self.data.config.layout, self.data.config.scoreboard_colors)
self.canvas = self.matrix.SwapOnVSync(self.canvas)
time.sleep(self.data.config.scrolling_speed)
def __draw_standings(self, cond: Callable[[], bool]):
"""
Draw the standings screen for as long as cond returns True
"""
if not self.data.standings.populated():
return
if self.data.standings.is_postseason() and self.canvas.width <= 32:
return
update = 1
while cond():
if self.data.standings.is_postseason():
standings.render_bracket(
self.canvas,
self.data.config.layout,
self.data.config.scoreboard_colors,
self.data.standings.leagues[self.standings_league],
)
else:
standings.render_standings(
self.canvas,
self.data.config.layout,
self.data.config.scoreboard_colors,
self.data.standings.current_standings(),
self.standings_stat,
)
if self.data.network_issues:
network.render_network_error(self.canvas, self.data.config.layout, self.data.config.scoreboard_colors)
self.canvas = self.matrix.SwapOnVSync(self.canvas)
if self.data.standings.is_postseason() and update % 20 == 0:
if self.standings_league == "NL":
self.standings_league = "AL"
else:
self.standings_league = "NL"
elif self.canvas.width == 32 and update % 5 == 0:
if self.standings_stat == "w":
self.standings_stat = "l"
else:
self.standings_stat = "w"
self.data.standings.advance_to_next_standings()
elif self.canvas.width > 32 and update % 10 == 0:
self.data.standings.advance_to_next_standings()
time.sleep(1)
update = (update + 1) % 100
def __max_scroll_x(self, scroll_coords):
scroll_max_x = scroll_coords["x"] + scroll_coords["width"]
self.scrolling_text_pos = min(scroll_max_x, self.scrolling_text_pos)
def __update_scrolling_text_pos(self, new_pos, end):
"""Updates the position of scrolling text"""
pos_after_scroll = self.scrolling_text_pos - 1
if pos_after_scroll + new_pos < 0:
self.data.scrolling_finished = True
if pos_after_scroll + new_pos < -10:
self.scrolling_text_pos = end
return
self.scrolling_text_pos = pos_after_scroll
def no_games_cond(self) -> bool:
"""A condition that is true only while there are no games live"""
return not self.data.schedule.games_live()
def permanent_cond() -> bool:
"""A condition that is always true"""
return True
def timer_cond(seconds) -> Callable[[], bool]:
"""Create a condition that is true for the specified number of seconds"""
end = time.time() + seconds
def cond():
return time.time() < end
return cond
def all_of(*conds) -> Callable[[], bool]:
"""Create a condition that is true if all of the given conditions are true"""
def cond():
return all(c() for c in conds)
return cond