Class: Team

Inherits:
Object
  • Object
show all
Defined in:
lib/team.rb

Overview

This class

Constant Summary collapse

START_MONTH =

April

4
END_MONTH =

October

10
@@abrevs =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abrev) ⇒ Team

Setup team names, abbreviations, and league



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/team.rb', line 16

def initialize(abrev)
  if (abrev && abrev != '')
    @abrev = abrev
    if Team.teams[@abrev]
      @city = Team.teams[@abrev][0]
      @name = Team.teams[@abrev][1]
      if Team.teams[@abrev].length > 2
        @league = Team.teams[@abrev][2]
      end
    else
      @city = @abrev
      @name = @abrev
      @league = ''
    end
  else
      @city = ''
      @name = ''
      @league = ''
  end
end

Instance Attribute Details

#abrevObject

Returns the value of attribute abrev.



13
14
15
# File 'lib/team.rb', line 13

def abrev
  @abrev
end

#cityObject

Returns the value of attribute city.



13
14
15
# File 'lib/team.rb', line 13

def city
  @city
end

#gamesObject

Returns the value of attribute games.



13
14
15
# File 'lib/team.rb', line 13

def games
  @games
end

#leagueObject

Returns the value of attribute league.



13
14
15
# File 'lib/team.rb', line 13

def league
  @league
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/team.rb', line 13

def name
  @name
end

Class Method Details

.get_teams_for_gid(gid) ⇒ Object

Returns a 2 element array specifying the two teams associated with the game id (gid) passed in. teams = visitor teams = home



81
82
83
84
85
86
87
88
# File 'lib/team.rb', line 81

def self.get_teams_for_gid(gid)
  teams = []
  info = GamedayUtil.parse_gameday_id('gid_'+gid)
  home_team_abbrev = info["home_team_abbrev"]
  visit_team_abbrev = info["visiting_team_abbrev"]
  teams << Team.new(visit_team_abbrev)
  teams << Team.new(home_team_abbrev)
end

.teamsObject



73
74
75
# File 'lib/team.rb', line 73

def self.teams
  @@abrevs
end

Instance Method Details

#all_away_games(year) ⇒ Object

Returns an array of all away games for this team for the specified season



128
129
130
131
# File 'lib/team.rb', line 128

def all_away_games(year)
  games = all_games(year)
  results = games.select {|g| g.visit_team_abbrev == @abrev }
end

#all_games(year) ⇒ Object

Returns an array of all games for this team for the specified season



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
# File 'lib/team.rb', line 92

def all_games(year)
  if !@games
    puts 'Finding all games for team...'
    results = []
    (START_MONTH..END_MONTH).each do |month|
      puts "Month: " + month.to_s
      month_s = GamedayUtil.convert_digit_to_string(month)
      (1..31).each do |date|
        if !GamedayUtil.is_date_valid(month, date)
          next
        end
        date_s = GamedayUtil.convert_digit_to_string(date)
        games = games_for_date(year, month_s, date_s)
        if games
          # make sure game was not postponed
          good_games = games.select { |g| g.get_boxscore.status_ind != 'P' }
          good_games.each do |game|
            results << game
          end
        end
      end
    end
    @games = results
  end
  @games
end

#all_home_games(year) ⇒ Object

Returns an array of all home games for this team for the specified season



121
122
123
124
# File 'lib/team.rb', line 121

def all_home_games(year)
  games = all_games(year)
  results = games.select {|g| g.home_team_abbrev == @abrev }
end

#games_for_date(year, month, day) ⇒ Object

Returns an array of the team’s game objects for the date passed in.



135
136
137
138
139
140
141
142
143
144
# File 'lib/team.rb', line 135

def games_for_date(year, month, day)
  games_page = GamedayFetcher.fetch_games_page(year, month, day)
  gids = find_gid_for_date(year, month, day, games_page)
  if gids
    results = gids.collect {|gid| Game.new(gid) }
  else 
    results = nil
  end
  results
end

#get_cleanup_hitters_by_year(year) ⇒ Object

Returns an array containing the cleanup hitters for each game of the specified season. The cleanup hitter is the 4th hitter in the batting order



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/team.rb', line 175

def get_cleanup_hitters_by_year(year)
  results = []
  games = all_games(year)
  games.each do |game|
    boxscore = game.get_boxscore
    hitters = boxscore.get_cleanup_hitters
    if game.home_team_abbrev == @abrev
      results << hitters[1]
    else
      results << hitters[0]
    end
  end
  results
end

#get_cleanup_hitters_unique(year) ⇒ Object

Returns an array of all hitters who have hit in the cleanup spot (4) at least one game during the specified season



192
193
194
195
196
197
# File 'lib/team.rb', line 192

def get_cleanup_hitters_unique(year)
  hitters = get_cleanup_hitters_by_year(year)
  h = {}
  hitters.each {|hitter| h[hitter.batter_name]=hitter}
  h.values
end

#get_close_pitcher_appearances_by_year(year) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/team.rb', line 224

def get_close_pitcher_appearances_by_year(year)
  pitchers = []
  games = all_games(year)
  games.each do |game|
    closers = game.get_closing_pitchers
    if game.home_team_abbrev == @abrev
      pitchers << closers[1]
    else
      pitchers << closers[0]
    end
  end
  pitchers
end

#get_closers_unique(year) ⇒ Object

Returns an array of all pitchers who have closed at least one game during the specified season



240
241
242
243
244
245
# File 'lib/team.rb', line 240

def get_closers_unique(year)
  pitchers = get_close_pitcher_appearances_by_year(year)
  h = {}
  pitchers.each {|pitcher| h[pitcher.pitcher_name]=pitcher}
  h.values
end

#get_games_for_month(year, month) ⇒ Object

Returns an array of all the games for this team for the year and month specified



275
276
277
# File 'lib/team.rb', line 275

def get_games_for_month(year, month)
  
end

#get_leadoff_hitters_by_year(year) ⇒ Object

Returns an array of BattingAppearance containing the leadoff hitters for each game of the specified season.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/team.rb', line 148

def get_leadoff_hitters_by_year(year)
  results = []
  games = all_games(year)
  games.each do |game|
    boxscore = game.get_boxscore
    leadoffs = boxscore.get_leadoff_hitters
    if game.home_team_abbrev == @abrev
      results << leadoffs[1]
    else
      results << leadoffs[0]
    end
  end
  results
end

#get_leadoff_hitters_unique(year) ⇒ Object

Returns an array of BattingAppearance of all hitters who have led off at least one game during the specified season



165
166
167
168
169
170
# File 'lib/team.rb', line 165

def get_leadoff_hitters_unique(year)
  hitters = get_leadoff_hitters_by_year(year)
  h = {}
  hitters.each {|hitter| h[hitter.batter_name]=hitter}
  h.values
end

#get_opening_day_game(year) ⇒ Object

Returns a game object representing the opening day game for this team for the season passed in.



282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/team.rb', line 282

def get_opening_day_game(year)
  schedule = Schedule.new(year)
  oday = schedule.get_opening_day
  oday_array = GamedayUtil.parse_date_string(oday)
  games = games_for_date(oday_array[0], oday_array[1], oday_array[2])
  if games[0] == nil
    games = games_for_date(oday_array[0], 
                           oday_array[1], 
                           GamedayUtil.convert_digit_to_string(oday_array[2].to_i + 1))
  end
  return games[0]
end

#get_start_pitcher_appearances_by_year(year) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/team.rb', line 200

def get_start_pitcher_appearances_by_year(year)
  pitchers = []
  games = all_games(year)
  games.each do |game|
    starters = game.get_starting_pitchers
    if game.home_team_abbrev == @abrev
      pitchers << starters[1]
    else
      pitchers << starters[0]
    end
  end
  pitchers
end

#get_starters_unique(year) ⇒ Object

Returns an array of all pitchers who have started at least one game during the specified season



216
217
218
219
220
221
# File 'lib/team.rb', line 216

def get_starters_unique(year)
  pitchers = get_start_pitcher_appearances_by_year(year)
  h = {}
  pitchers.each {|pitcher| h[pitcher.pitcher_name]=pitcher}
  h.values
end

#opening_day_roster(year) ⇒ Object

Returns a Roster object representing the opening day roster for this team for the specified year.



298
299
300
301
302
# File 'lib/team.rb', line 298

def opening_day_roster(year)
  game = get_opening_day_game(year)
  rosters = game.get_rosters
  rosters[0].team_name == city + ' ' + name ? rosters[0] : rosters[1]
end

#players_for_season(year) ⇒ Object

Returns an array of all players who have played at least one game for this team during the specified season.



269
270
271
# File 'lib/team.rb', line 269

def players_for_season(year)
  
end

#quality_starts_count(year) ⇒ Object

Returns a count of the number of quality starts for this team for the specified year.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/team.rb', line 249

def quality_starts_count(year)
  count = 0
  games = all_games(year)
  games.each do |game|
    starters = game.get_starting_pitchers
    if game.home_team_abbrev == @abrev
      if starters[1].quality_start?
        count = count + 1
      end
    else
      if starters[0].quality_start?
        count = count + 1
      end
    end
  end
  count
end