Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ty-porter committed Mar 18, 2024
1 parent 8ef96bf commit 25fdbae
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 43 deletions.
16 changes: 8 additions & 8 deletions rewrite/data/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)

Expand Down
12 changes: 7 additions & 5 deletions rewrite/data/pitches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
}

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions rewrite/presenters/live_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
9 changes: 6 additions & 3 deletions rewrite/screens/components/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from driver import graphics


class Base:
def __init__(self, number, screen):
self.number = number
Expand All @@ -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
Expand All @@ -50,4 +53,4 @@ def colors(self):

@property
def layout(self):
return self.screen.layout
return self.screen.layout
4 changes: 2 additions & 2 deletions rewrite/screens/components/out.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from utils.graphics import DrawRect


class Out:
def __init__(self, number, screen):
self.number = number
Expand All @@ -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.
Expand Down Expand Up @@ -45,4 +45,4 @@ def colors(self):

@property
def layout(self):
return self.screen.layout
return self.screen.layout
13 changes: 5 additions & 8 deletions rewrite/screens/games/live_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -85,7 +82,7 @@ def __render_pitcher_text(self, presenter):
color,
bgcolor,
text,
center=False
center=False,
)
scroller.render_text()

Expand Down Expand Up @@ -135,7 +132,7 @@ def __render_batter_text(self):
color,
bgcolor,
self.game.batter(),
center=False
center=False,
)
scroller.render_text()

Expand Down
1 change: 1 addition & 0 deletions rewrite/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def value_at_keypath(current, keypath):

return current


def format_id(ID):
if "ID" in str(ID):
return ID
Expand Down
3 changes: 2 additions & 1 deletion rewrite/utils/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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)

30 changes: 16 additions & 14 deletions rewrite/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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,
)


Expand Down

0 comments on commit 25fdbae

Please sign in to comment.