diff --git a/rewrite/data/game.py b/rewrite/data/game.py index 7f228b46..ef86d68b 100644 --- a/rewrite/data/game.py +++ b/rewrite/data/game.py @@ -164,15 +164,15 @@ def pitcher_stat(self, player, stat, team=None): stats = keypath(team, ID).get("pitching", None) or keypath(team, ID).get("pitching", {}) return stats[stat] - + def man_on(self, base_number): - base = { 1: "first", 2: "second", 3: "third" }.get(base_number, None) + base = {1: "first", 2: "second", 3: "third"}.get(base_number, None) if not base: return None - + return value_at_keypath(self.data, f"liveData.linescore.offense.{base}").get("id", None) - + def batter(self): return self.__fetch_player("offense", "batter") @@ -184,20 +184,20 @@ def on_deck(self): def pitcher(self): return self.__fetch_player("defense", "pitcher") - + def boxscore_name(self, ID): ID = format_id(ID) return value_at_keypath(self.data, f"gameData.players.{ID}.boxscoreName") - + def __fetch_player(self, team, position): ID = value_at_keypath(self.data, f"liveData.linescore.{team}.{position}").get("id", None) return self.boxscore_name(ID) - + def pitches(self): return Pitches(self) - + def outs(self): return value_at_keypath(self.data, "liveData.linescore").get("outs", 0) diff --git a/rewrite/data/pitches.py b/rewrite/data/pitches.py index dd4bad07..cc876510 100644 --- a/rewrite/data/pitches.py +++ b/rewrite/data/pitches.py @@ -2,8 +2,8 @@ from utils import format_id, value_at_keypath -class Pitches: +class Pitches: # A list of mlb pitch types appearing in statcast # from statsapi.meta("pitchTypes") # Dont change the index, but feel free to change @@ -59,7 +59,7 @@ class Pitches: "SI": "SI", "SL": "SL", "SV": "SV", - "ST": "SW", # MLB default is "ST" + "ST": "SW", # MLB default is "ST" "UN": "UN", } @@ -98,7 +98,7 @@ def last_pitch(self): ) except: return None - + def current_pitcher_pitch_count(self): pitcher_id = value_at_keypath(self.game.data, "liveData.linescore.defense.pitcher").get("id", None) @@ -108,13 +108,15 @@ def current_pitcher_pitch_count(self): ID = format_id(pitcher_id) for team in [TeamType.HOME, TeamType.AWAY]: - pitches = value_at_keypath(self.game.data, f"liveData.boxscore.teams.{team}.players.{ID}.stats.pitching").get("numberOfPitches", 0) + pitches = value_at_keypath( + self.game.data, f"liveData.boxscore.teams.{team}.players.{ID}.stats.pitching" + ).get("numberOfPitches", 0) if pitches > 0: return pitches return 0 - + def __fetch_count_part(self, part): return value_at_keypath(self.game.data, "liveData.linescore").get(part, 0) diff --git a/rewrite/presenters/live_game.py b/rewrite/presenters/live_game.py index 2512336f..da797972 100644 --- a/rewrite/presenters/live_game.py +++ b/rewrite/presenters/live_game.py @@ -5,7 +5,7 @@ def __init__(self, game, config): def count_text(self): return "{}-{}".format(self.game.pitches().balls, self.game.pitches().strikes) - + def pitcher_text(self): pitcher = self.game.pitcher() pitch_count = self.config.layout.coords("atbat.pitch_count") @@ -30,5 +30,5 @@ def pitch_text(self): pitch_text = str(pitches.last_pitch_speed) + mph + pitches.last_pitch_type else: pitch_text = "" - + return pitch_text diff --git a/rewrite/screens/components/base.py b/rewrite/screens/components/base.py index 978496ad..83a702a2 100644 --- a/rewrite/screens/components/base.py +++ b/rewrite/screens/components/base.py @@ -1,5 +1,6 @@ from driver import graphics + class Base: def __init__(self, number, screen): self.number = number @@ -24,12 +25,14 @@ def __render_outline(self, coords, color): graphics.DrawLine(self.canvas, x + half, y + size, x, y + half, color) graphics.DrawLine(self.canvas, x + half, y + size, x + size, y + half, color) - def __render_runner(self, coords, color): + def __render_runner(self, coords, color): x, y = coords.x, coords.y size = coords.size half = abs(size // 2) for offset in range(1, half + 1): - graphics.DrawLine(self.canvas, x + half - offset, y + size - offset, x + half + offset, y + size - offset, color) + graphics.DrawLine( + self.canvas, x + half - offset, y + size - offset, x + half + offset, y + size - offset, color + ) graphics.DrawLine(self.canvas, x + half - offset, y + offset, x + half + offset, y + offset, color) @property @@ -50,4 +53,4 @@ def colors(self): @property def layout(self): - return self.screen.layout \ No newline at end of file + return self.screen.layout diff --git a/rewrite/screens/components/out.py b/rewrite/screens/components/out.py index 5e1169fc..c3ba09b2 100644 --- a/rewrite/screens/components/out.py +++ b/rewrite/screens/components/out.py @@ -1,5 +1,6 @@ from utils.graphics import DrawRect + class Out: def __init__(self, number, screen): self.number = number @@ -13,7 +14,6 @@ def render(self): self.__render_out(coords, color) else: self.__render_outline(coords, color) - # TODO: The size coordinates for these boxes are off-by-one because they fail to account for endpoints. # i.e. `size` config of 2 renders a 3x3 box instead of 2x2 because endpoints for graphics.DrawLine are inclusive. @@ -45,4 +45,4 @@ def colors(self): @property def layout(self): - return self.screen.layout \ No newline at end of file + return self.screen.layout diff --git a/rewrite/screens/games/live_game.py b/rewrite/screens/games/live_game.py index f0d861c9..238574be 100644 --- a/rewrite/screens/games/live_game.py +++ b/rewrite/screens/games/live_game.py @@ -7,17 +7,14 @@ from utils.text import ScrollingText + class LiveGameScreen(GameScreen): MAX_DURATION_SECONDS = 5 def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.bases = [ - Base(1, self), - Base(2, self), - Base(3, self) - ] + self.bases = [Base(1, self), Base(2, self), Base(3, self)] self.outs = [ Out(1, self), @@ -52,7 +49,7 @@ def __render_count(self, presenter): font, font_size = self.layout.font_for("batter_count") coords = self.layout.coords("batter_count") color = self.colors.graphics_color("batter_count") - + graphics.DrawText(self.canvas, font, coords.x, coords.y, color, text) def __render_at_bat(self, presenter): @@ -85,7 +82,7 @@ def __render_pitcher_text(self, presenter): color, bgcolor, text, - center=False + center=False, ) scroller.render_text() @@ -135,7 +132,7 @@ def __render_batter_text(self): color, bgcolor, self.game.batter(), - center=False + center=False, ) scroller.render_text() diff --git a/rewrite/utils/__init__.py b/rewrite/utils/__init__.py index 4946efce..37c7358d 100644 --- a/rewrite/utils/__init__.py +++ b/rewrite/utils/__init__.py @@ -211,6 +211,7 @@ def value_at_keypath(current, keypath): return current + def format_id(ID): if "ID" in str(ID): return ID diff --git a/rewrite/utils/graphics.py b/rewrite/utils/graphics.py index 255f6009..0c47ed85 100644 --- a/rewrite/utils/graphics.py +++ b/rewrite/utils/graphics.py @@ -11,6 +11,7 @@ def DrawRect(canvas, x, y, width, height, color, filled=True): else: _DrawUnfilledRect(canvas, x, y, width, height, color) + def _DrawFilledRect(canvas, x, y, width, height, color): """ Draws a rectangle on screen with (X, Y) given as screen coordinates where (0, 0) is top left. @@ -24,6 +25,7 @@ def _DrawFilledRect(canvas, x, y, width, height, color): for offset in range(0, width + 1): graphics.DrawLine(canvas, x + offset, y, x + offset, y + height, color) + def _DrawUnfilledRect(canvas, x, y, width, height, color): # Top horizontal graphics.DrawLine(canvas, x, y, x + width, y, color) @@ -33,4 +35,3 @@ def _DrawUnfilledRect(canvas, x, y, width, height, color): graphics.DrawLine(canvas, x, y, x, y + height, color) # Right vertical graphics.DrawLine(canvas, x + width, y, x + width, y + height, color) - \ No newline at end of file diff --git a/rewrite/utils/text.py b/rewrite/utils/text.py index 9e5c80d1..402a42cb 100644 --- a/rewrite/utils/text.py +++ b/rewrite/utils/text.py @@ -12,7 +12,7 @@ class TextPosition: class ScrollingText: - def __init__(self, canvas, x, y, width, font, font_size, text_color, bg_color, text, start_x = None, center=True): + def __init__(self, canvas, x, y, width, font, font_size, text_color, bg_color, text, start_x=None, center=True): # Matrix self.canvas = canvas @@ -98,7 +98,9 @@ def __render_static_text(self, center): self._centered_text.render_text() else: - graphics.DrawText(self.canvas, self.font, self.end_position.x, self.end_position.y, self.text_color, self.text) + graphics.DrawText( + self.canvas, self.font, self.end_position.x, self.end_position.y, self.text_color, self.text + ) def __truncate_text(self, text, font_w, font_h): text = self.text @@ -123,28 +125,28 @@ def __perform_scroll(self, text_width): self.position.x = next_x def __apply_mask(self): - ''' - Applies a mask to the matrix such that text is only visible if it is within the window configured by the scroller. - ''' + """ + Applies a mask to the matrix such that text is only visible if it is within the window configured by the scroller. + """ # Left side DrawRect( - self.canvas, + self.canvas, 0, - self.end_position.y - self.font_size[1], - self.end_position.x - 1, + self.end_position.y - self.font_size[1], + self.end_position.x - 1, self.font_size[1] + 1, - self.bg_color + self.bg_color, ) # Right side DrawRect( - self.canvas, - self.start_position.x + 1, - self.start_position.y - self.font_size[1], - self.canvas.width - self.start_position.x + 1, + self.canvas, + self.start_position.x + 1, + self.start_position.y - self.font_size[1], + self.canvas.width - self.start_position.x + 1, self.font_size[1] + 1, - self.bg_color + self.bg_color, )