-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lib/baseballbot/templates/game_threads/components/matchups.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
class Baseballbot | ||
module Templates | ||
module GameThreads | ||
module Components | ||
class Matchups | ||
include MarkdownHelpers | ||
|
||
BASE_URL = 'https://bdfed.stitch.mlbinfra.com/bdfed/matchup/%<game_pk>s?statList=avg&statList=atBats' \ | ||
'&statList=homeRuns&statList=rbi&statList=ops&statList=strikeOuts' | ||
|
||
HEADERS = %w[AVG OPS AB HR RBI K].freeze | ||
|
||
def initialize(game_thread) | ||
@game_thread = game_thread | ||
end | ||
|
||
def to_s = "#{home_table}\n\n#{away_table}" | ||
|
||
protected | ||
|
||
def home_table | ||
table( | ||
headers: [ | ||
"#{data.dig('probables', 'homeAbbreviation')} vs. #{data.dig('probables', 'awayProbableLastName')}", | ||
*HEADERS | ||
], | ||
rows: data['home'].map { [_1['boxscoreName'], *player_stats(_1)] } | ||
) | ||
end | ||
|
||
def away_table | ||
table( | ||
headers: [ | ||
"#{data.dig('probables', 'awayAbbreviation')} vs. #{data.dig('probables', 'homeProbableLastName')}", | ||
*HEADERS | ||
], | ||
rows: data['away'].map { [_1['boxscoreName'], *player_stats(_1)] } | ||
) | ||
end | ||
|
||
def player_row(player) | ||
return ['-'] * 6 unless player['stats'] | ||
|
||
player['stats'].values_at('avg', 'ops', 'atBats', 'homeRuns', 'rbi', 'strikeOuts') | ||
end | ||
|
||
def data | ||
@data ||= JSON.parse(URI.parse(format(BASE_URL, game_pk: @game_thread.game_pk)).open.read) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
58 changes: 58 additions & 0 deletions
58
lib/baseballbot/templates/game_threads/components/media.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
class Baseballbot | ||
module Templates | ||
module GameThreads | ||
module Components | ||
class ProbablesAndMedia | ||
include MarkdownHelpers | ||
|
||
HOME_FEED_TYPES = %w[HOME NATIONAL].freeze | ||
AWAY_FEED_TYPES = %w[AWAY NATIONAL].freeze | ||
|
||
def initialize(game_thread) | ||
@game_thread = game_thread | ||
end | ||
|
||
def to_s | ||
table headers: %w[Team TV Radio], rows: [ | ||
[team_link(@game_thread.away_team), tv_feeds(:away), radio_feeds(:away)], | ||
[team_link(@game_thread.home_team), tv_feeds(:home), radio_feeds(:home)] | ||
] | ||
end | ||
|
||
protected | ||
|
||
def team_link(team) = "[#{team.name}](/r/#{@game_thread.subreddit.code_to_subreddit_name(team.code)})" | ||
|
||
def tv_feeds(flag) | ||
television_feeds | ||
.select { (flag == :home ? HOME_FEED_TYPES : AWAY_FEED_TYPES).include?(_1['mediaFeedType']) } | ||
.map { _1['callLetters'] } | ||
.join(', ') | ||
end | ||
|
||
def radio_feeds(flag) | ||
audio_feeds | ||
.select { (flag == :home ? HOME_FEED_TYPES : AWAY_FEED_TYPES).include?(_1['type']) } | ||
.sort_by { _1['language'] == 'en' ? 0 : 1 } | ||
.map { _1['language'] == 'en' ? _1['callLetters'] : "#{_1['callLetters']} (#{_1['language'].upcase})" } | ||
.join(', ') | ||
end | ||
|
||
def television_feeds | ||
@television_feeds ||= @game_thread.content.dig('media', 'epg') | ||
&.detect { _1['title'] == 'MLBTV' } | ||
&.fetch('items') || [] | ||
end | ||
|
||
def audio_feeds | ||
@audio_feeds ||= @game_thread.content.dig('media', 'epg') | ||
&.detect { _1['title'] == 'Audio' } | ||
&.fetch('items') || [] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
65 changes: 65 additions & 0 deletions
65
lib/baseballbot/templates/game_threads/components/probable_starters.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
class Baseballbot | ||
module Templates | ||
module GameThreads | ||
module Components | ||
class ProbableStarters | ||
include MarkdownHelpers | ||
|
||
TBA = ['TBA', '', '', '', '', '', '', '', ''].freeze | ||
|
||
HEADERS = [ | ||
'Team', | ||
'Pitcher', | ||
['Record', :center], | ||
['ERA', :center], | ||
['IP', :center], | ||
['H', :center], | ||
['ER', :center], | ||
['BB', :center], | ||
['SO', :center], | ||
['WHIP', :center] | ||
].freeze | ||
|
||
def initialize(game_thread) | ||
@game_thread = game_thread | ||
end | ||
|
||
def to_s | ||
table headers: HEADERS, rows: [ | ||
[team_link(@game_thread.away_team), *probable_starter_line('away')], | ||
[team_link(@game_thread.home_team), *probable_starter_line('home')] | ||
] | ||
end | ||
|
||
protected | ||
|
||
def team_link(team) = "[#{team.name}](/r/#{@game_thread.subreddit.code_to_subreddit_name(team.code)})" | ||
|
||
def probable_starter_line(flag) | ||
pitcher_id = @game_thread.game_data.dig('probablePitchers', flag, 'id') | ||
|
||
return TBA unless pitcher_id | ||
|
||
pitcher_line(@game_thread.boxscore.dig('teams', flag, 'players', "ID#{pitcher_id}")) | ||
end | ||
|
||
def pitcher_line(pitcher) | ||
return TBA unless pitcher | ||
|
||
season_stats = pitcher.dig('seasonStats', 'pitching') | ||
|
||
[ | ||
"[#{pitcher.dig('person', 'fullName')}](#{player_url(pitcher.dig('person', 'id'))})", | ||
"#{season_stats['wins']}-#{season_stats['losses']}", | ||
*season_stats.values_at( | ||
'era', 'inningsPitched', 'hits', 'earnedRuns', 'baseOnBalls', 'strikeOuts', 'whip' | ||
) | ||
] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |