Class: Player

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

Overview

This class represents a single MLB player from a single MLB game

Direct Known Subclasses

Batter, Pitcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appearancesObject

object pointers



14
15
16
# File 'lib/player.rb', line 14

def appearances
  @appearances
end

#avgObject

Returns the value of attribute avg.



8
9
10
# File 'lib/player.rb', line 8

def avg
  @avg
end

#bat_orderObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def bat_order
  @bat_order
end

#batsObject

attributes from batters/13353333.xml or pitchers/1222112.xml



11
12
13
# File 'lib/player.rb', line 11

def bats
  @bats
end

#boxnameObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def boxname
  @boxname
end

#dobObject

attributes from batters/13353333.xml or pitchers/1222112.xml



11
12
13
# File 'lib/player.rb', line 11

def dob
  @dob
end

#eraObject

Returns the value of attribute era.



8
9
10
# File 'lib/player.rb', line 8

def era
  @era
end

#firstObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def first
  @first
end

#game_positionObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def game_position
  @game_position
end

#gamesObject

object pointers



14
15
16
# File 'lib/player.rb', line 14

def games
  @games
end

#gidObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def gid
  @gid
end

#heightObject

attributes from batters/13353333.xml or pitchers/1222112.xml



11
12
13
# File 'lib/player.rb', line 11

def height
  @height
end

#hrObject

Returns the value of attribute hr.



8
9
10
# File 'lib/player.rb', line 8

def hr
  @hr
end

#lastObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def last
  @last
end

#lossesObject

Returns the value of attribute losses.



8
9
10
# File 'lib/player.rb', line 8

def losses
  @losses
end

#numObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def num
  @num
end

#pidObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def pid
  @pid
end

#positionObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def position
  @position
end

#rbiObject

Returns the value of attribute rbi.



8
9
10
# File 'lib/player.rb', line 8

def rbi
  @rbi
end

#rlObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def rl
  @rl
end

#savesObject

Returns the value of attribute saves.



8
9
10
# File 'lib/player.rb', line 8

def saves
  @saves
end

#statusObject

attributes from players.xml



7
8
9
# File 'lib/player.rb', line 7

def status
  @status
end

#std_hrObject

Returns the value of attribute std_hr.



8
9
10
# File 'lib/player.rb', line 8

def std_hr
  @std_hr
end

#team_abbrevObject

attributes from batters/13353333.xml or pitchers/1222112.xml



11
12
13
# File 'lib/player.rb', line 11

def team_abbrev
  @team_abbrev
end

#team_codeObject

Returns the value of attribute team_code.



8
9
10
# File 'lib/player.rb', line 8

def team_code
  @team_code
end

#team_objObject

object pointers



14
15
16
# File 'lib/player.rb', line 14

def team_obj
  @team_obj
end

#throwsObject

attributes from batters/13353333.xml or pitchers/1222112.xml



11
12
13
# File 'lib/player.rb', line 11

def throws
  @throws
end

#typeObject

attributes from batters/13353333.xml or pitchers/1222112.xml



11
12
13
# File 'lib/player.rb', line 11

def type
  @type
end

#weightObject

attributes from batters/13353333.xml or pitchers/1222112.xml



11
12
13
# File 'lib/player.rb', line 11

def weight
  @weight
end

#winsObject

Returns the value of attribute wins.



8
9
10
# File 'lib/player.rb', line 8

def wins
  @wins
end

Instance Method Details

#at_bats_countObject

Returns the number of at bats over the entire season for this player



67
68
69
70
71
# File 'lib/player.rb', line 67

def at_bats_count
  gameday_info = GamedayUtil.parse_gameday_id(@gid)
  appearances = get_all_appearances(gameday_info["year"])
  count = appearances.inject(0) {|sum, a| sum + a.ab.to_i }    
end

#get_all_appearances(year) ⇒ Object

Returns an array of all the appearances (Batting or Pitching) made by this player for the season specified.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/player.rb', line 42

def get_all_appearances(year)
  if !@appearances
    @appearances = []
    all_appearances = []
    games = get_games_for_season(year)    
    games.each do |game|
      @team_abbrev == game.home_team_abbrev ? status = 'home' : status = 'away'
      if @position == 'P'
        all_appearances.push *(game.get_pitchers(status))
      else
        all_appearances.push *(game.get_batters(status))
      end
    end
    # now go through all appearances to find those for this player
    all_appearances.each do |appearance|
      if appearance.pid == @pid
       @appearances << appearance
      end
    end
  end
  @appearances
end

#get_games_for_season(year) ⇒ Object

Returns an array of all the games for the team this player is on for the season specified currently will not handle a player who has played for multiple teams over a season



85
86
87
88
89
90
# File 'lib/player.rb', line 85

def get_games_for_season(year)
  if !@games
    @games = get_team.all_games(year)  
  end
  @games
end

#get_teamObject

Returns the Team object representing the team for which this player plays



75
76
77
78
79
80
# File 'lib/player.rb', line 75

def get_team
  if !@team_obj
    @team_obj = Team.new(@team_abbrev)
  end
  @team_obj
end

#init(element, gid) ⇒ Object

Initialize a player object by reading data from the players.xml file



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/player.rb', line 94

def init(element, gid)
  @gid = gid
  @pid = element.attributes['id']
  @first = element.attributes['first']
  @last = element.attributes['last']
  @num= element.attributes['num']
  @boxname = element.attributes['boxname']
  @rl, = element.attributes['rl']
  @position = element.attributes['position']
  @status = element.attributes['status']
  @bat_order = element.attributes['bat_order']
  @game_position = element.attributes['game_position']
  @avg = element.attributes['avg']
  @hr = element.attributes['hr']
  @rbi = element.attributes['rbi']
  @wins = element.attributes['wins']
  @losses = element.attributes['losses']
  @era = element.attributes['era']   
  set_extra_info
end

#init_pitcher_from_scoreboard(element) ⇒ Object

Initializes pitcher info from data read from the masterscoreboard.xml file



31
32
33
34
35
36
37
# File 'lib/player.rb', line 31

def init_pitcher_from_scoreboard(element)
  @first = element.attributes['first']
  @last = element.attributes['last']
  @wins = element.attributes['wins']
  @losses = element.attributes['losses']
  @era = element.attributes['era']
end

#load_from_id(gid, pid) ⇒ Object

Initializes a Player object by reading the player data from the players.xml file for the player specified by game id and player id.



18
19
20
21
22
23
24
25
26
27
# File 'lib/player.rb', line 18

def load_from_id(gid, pid)
  @gid = gid
  @pid = pid
  # fetch players.xml file
  @xml_data = GamedayFetcher.fetch_players(gid)
  @xml_doc = REXML::Document.new(@xml_data)
  # find specific player in the file
  pelement = @xml_doc.root.elements["team/player[@id=#{pid}]"]
  init(pelement, gid)
end