Skip to content

Commit

Permalink
Merge pull request #494 from WardBrian/play-result-follow-on
Browse files Browse the repository at this point in the history
Allow more config of play results
  • Loading branch information
WardBrian authored Jul 17, 2023
2 parents 6ace212 + 9ab5327 commit 9c84a79
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 74 deletions.
9 changes: 8 additions & 1 deletion coordinates/w128h32.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,17 @@
"append_pitcher_name": false
},
"loop": 64,
"strikeout": {
"x": 60,
"y": 30,
"desc_length": "short",
"enabled": true
},
"play_result": {
"x": 60,
"y": 30,
"desc_length": "short"
"desc_length": "short",
"enabled": true
}
},
"pregame": {
Expand Down
9 changes: 8 additions & 1 deletion coordinates/w128h64.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,17 @@
"append_pitcher_name": false
},
"loop": 68,
"strikeout": {
"x": 16,
"y": 60,
"desc_length": "long",
"enabled": true
},
"play_result": {
"x": 16,
"y": 60,
"desc_length": "long"
"desc_length": "long",
"enabled": true
}
},
"batter_count": {
Expand Down
11 changes: 9 additions & 2 deletions coordinates/w192h64.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"y": 50,
"enabled": true,
"mph": true,
"desc_length": "Long"
"desc_length": "long"
},
"pitch_count": {
"font_name": "4x6",
Expand All @@ -145,10 +145,17 @@
"append_pitcher_name": false
},
"loop": 68,
"strikeout": {
"x": 16,
"y": 60,
"desc_length": "long",
"enabled": true
},
"play_result": {
"x": 16,
"y": 60,
"desc_length": "Long"
"desc_length": "long",
"enabled": true
}
},
"batter_count": {
Expand Down
9 changes: 8 additions & 1 deletion coordinates/w32h32.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,17 @@
"append_pitcher_name": false
},
"loop": 16,
"strikeout": {
"x": 33,
"y": 33,
"desc_length": "short",
"enabled": false
},
"play_result": {
"x": 33,
"y": 33,
"desc_length": "short"
"desc_length": "short",
"enabled": false
}
},
"batter_count": {
Expand Down
12 changes: 10 additions & 2 deletions coordinates/w64h32.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,21 @@
"append_pitcher_name": false
},
"loop": 36,
"strikeout": {
"x": 15,
"y": 29,
"font_name": "5x7",
"desc_length": "short",
"enabled": true
},
"play_result": {
"x": 15,
"y": 29,
"font_name": "5x7",
"desc_length": "short"
"desc_length": "short",
"enabled": true
}

},
"batter_count": {
"x": 34,
Expand Down
9 changes: 8 additions & 1 deletion coordinates/w64h64.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,17 @@
"append_pitcher_name": false
},
"loop": 64,
"strikeout": {
"x": 31,
"y": 36,
"desc_length": "short",
"enabled": true
},
"play_result": {
"x": 31,
"y": 36,
"desc_length": "short"
"desc_length": "short",
"enabled": true
}
},
"pregame": {
Expand Down
78 changes: 27 additions & 51 deletions data/plays.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,37 @@
SINGLE = "single"
DOUBLE = "double"
TRIPLE = "triple"
SINGLE = "single"
DOUBLE = "double"
TRIPLE = "triple"
HOME_RUN = "home_run"

WALK = "walk"
WALK = "walk"
INTENTIONAL_WALK = "intent_walk"
HIT_BY_PITCH = "hit_by_pitch"

STRIKEOUT = "strikeout"
STRIKEOUT = "strikeout"
STRIKEOUT_ALT = "strike_out"
STRIKEOUT_LOOKING = "strikeout_looking"

HITS = [
SINGLE,
DOUBLE,
TRIPLE
]
ERROR = "error"
FIELDERS_CHOICE = "fielders_choice"

WALKS = [
WALK,
INTENTIONAL_WALK
]
HITS = [SINGLE, DOUBLE, TRIPLE]

STRIKEOUTS = [
STRIKEOUT,
STRIKEOUT_LOOKING
]
WALKS = [WALK, INTENTIONAL_WALK, HIT_BY_PITCH]

OTHERS = [ERROR, FIELDERS_CHOICE]

STRIKEOUTS = [STRIKEOUT, STRIKEOUT_ALT, STRIKEOUT_LOOKING]

PLAY_RESULTS = {
SINGLE: {
"short": "1B",
"long": "Single"
},
DOUBLE: {
"short": "2B",
"long": "Double"
},
TRIPLE: {
"short": "3B",
"long": "Triple"
},
HOME_RUN: {
"short": "HR",
"long": "Home Run"
},
WALK: {
"short": "BB",
"long": "Walk"
},
INTENTIONAL_WALK: {
"short": "IBB",
"long": "Int. Walk"
},
STRIKEOUT: {
"short": "K",
"long": "K"
},
STRIKEOUT_LOOKING: {
"short": "ꓘ",
"long": "ꓘ"
}
}
SINGLE: {"short": "1B", "long": "Single"},
DOUBLE: {"short": "2B", "long": "Double"},
TRIPLE: {"short": "3B", "long": "Triple"},
WALK: {"short": "BB", "long": "Walk"},
INTENTIONAL_WALK: {"short": "IBB", "long": "Int. Walk"},
STRIKEOUT: {"short": "K", "long": "K"},
STRIKEOUT_ALT: {"short": "K", "long": "K"},
STRIKEOUT_LOOKING: {"short": "ꓘ", "long": "ꓘ"},
HIT_BY_PITCH: {"short": "HBP", "long": "Hit Bttr"},
ERROR: {"short": "E", "long": "Error"},
FIELDERS_CHOICE: {"short": "FC", "long": "Fielder's Chc"},
}
33 changes: 22 additions & 11 deletions renderers/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def render_live_game(canvas, layout: Layout, colors: Color, scoreboard: Scoreboa
scoreboard.play_result,
(animation_time // 6) % 2,
scoreboard.pitches

)
)

# Check if we're deep enough into a game and it's a no hitter or perfect game
should_display_nohitter = layout.coords("nohitter")["innings_until_display"]
Expand All @@ -38,7 +37,7 @@ def render_live_game(canvas, layout: Layout, colors: Color, scoreboard: Scoreboa
_render_bases(canvas, layout, colors, scoreboard.bases, scoreboard.homerun(), (animation_time % 16) // 5)

_render_inning_display(canvas, layout, colors, scoreboard.inning)

else:
_render_inning_break(canvas, layout, colors, scoreboard.inning)
_render_due_up(canvas, layout, colors, scoreboard.atbat)
Expand All @@ -47,32 +46,44 @@ def render_live_game(canvas, layout: Layout, colors: Color, scoreboard: Scoreboa


# --------------- at-bat ---------------
def _render_at_bat(canvas, layout, colors, atbat: AtBat, text_pos, play_result, animation, pitches: Pitches):
def _render_at_bat(canvas, layout, colors, atbat: AtBat, text_pos, play_result, animation, pitches: Pitches):
plength = __render_pitcher_text(canvas, layout, colors, atbat.pitcher, pitches, text_pos)
__render_pitch_text(canvas, layout, colors, pitches)
__render_pitch_count(canvas, layout, colors, pitches)
results = list(PLAY_RESULTS.keys())
if play_result in results:
if play_result in results and __should_render_play_result(play_result, layout):
if animation:
__render_play_result(canvas, layout, colors, play_result)
return plength
else:
blength = __render_batter_text(canvas, layout, colors, atbat.batter, text_pos)
return max(plength, blength)


def __should_render_play_result(play_result, layout):
if "strikeout" in play_result:
coords = layout.coords("atbat.strikeout")
else:
coords = layout.coords("atbat.play_result")
return coords["enabled"]


def __render_play_result(canvas, layout, colors, play_result):
coords = layout.coords("atbat.play_result")
if "strikeout" in play_result:
color = colors.graphics_color("atbat.strikeout")
else:
coords = layout.coords("atbat.strikeout")
font = layout.font("atbat.strikeout")
else:
color = colors.graphics_color("atbat.play_result")
font = layout.font("atbat.play_result")
coords = layout.coords("atbat.play_result")
font = layout.font("atbat.play_result")
try:
text = PLAY_RESULTS[play_result][coords["desc_length"].lower()]
text = PLAY_RESULTS[play_result][coords["desc_length"].lower()]
except KeyError:
return # There's no text or coordinates to render
return
graphics.DrawText(canvas, font["font"], coords["x"], coords["y"], color, text)


def __render_batter_text(canvas, layout, colors, batter, text_pos):
coords = layout.coords("atbat.batter")
color = colors.graphics_color("atbat.batter")
Expand Down Expand Up @@ -248,7 +259,7 @@ def __fill_out_circle(canvas, out, color):

# --------------- inning information ---------------
def _render_inning_break(canvas, layout, colors, inning: Inning):

text_font = layout.font("inning.break.text")
num_font = layout.font("inning.break.number")
text_coords = layout.coords("inning.break.text")
Expand Down
12 changes: 8 additions & 4 deletions tests/test_data_up_to_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ def test_status_complete(self):
self.assertSetEqual(official_statuses, our_statuses)

def test_teams_complete(self):
teams = statsapi.get('teams', {'sportIds':1})['teams']
teams = statsapi.get("teams", {"sportIds": 1})["teams"]

abbr_to_full = {t['teamName']:t['name'] for t in teams}
abbr_to_full = {t["teamName"]: t["name"] for t in teams}
self.assertEqual(abbr_to_full, data.teams.TEAM_FULL)
full_to_abbr = {t['name']:t['abbreviation'] for t in teams}
full_to_abbr = {t["name"]: t["abbreviation"] for t in teams}
self.assertEqual(full_to_abbr, data.teams.TEAM_ABBR_LN)

def test_pitches_complete(self):
pitches = set(p["code"] for p in statsapi.meta("pitchTypes"))
pitches = set(p["code"] for p in statsapi.meta("pitchTypes"))
self.assertSetEqual(pitches, set(data.pitches.PITCH_SHORT.keys()))
self.assertSetEqual(pitches, set(data.pitches.PITCH_LONG.keys()))

def test_results_exist(self):
events = set(e["code"] for e in statsapi.meta("eventTypes"))
events.add("strikeout_looking") # our custom event
self.assertTrue(events.issuperset(data.plays.PLAY_RESULTS.keys()))


if __name__ == "__main__":
Expand Down

0 comments on commit 9c84a79

Please sign in to comment.