Class: Big3::League

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ League

Returns a new instance of League.



9
10
11
12
13
# File 'lib/big_3/league.rb', line 9

def initialize(args={})
  @player_name = args[:player_name] ||= nil
  @team_name = args[:team_name] ||= nil
  retrieve_game_data
end

Instance Attribute Details

#game_idsObject

Returns the value of attribute game_ids.



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

def game_ids
  @game_ids
end

#game_statsObject

Returns the value of attribute game_stats.



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

def game_stats
  @game_stats
end

#gamesObject

Returns the value of attribute games.



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

def games
  @games
end

#p_sObject

Returns the value of attribute p_s.



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

def p_s
  @p_s
end

#player_nameObject

Returns the value of attribute player_name.



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

def player_name
  @player_name
end

#player_statsObject

Returns the value of attribute player_stats.



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

def player_stats
  @player_stats
end

#t_sObject

Returns the value of attribute t_s.



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

def t_s
  @t_s
end

#team_nameObject

Returns the value of attribute team_name.



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

def team_name
  @team_name
end

Instance Method Details

#add_game(scores, away_team, home_team, stats, season_team, location, game_count) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/big_3/league.rb', line 94

def add_game(scores,
             away_team,
             home_team,
             stats,
             season_team,
             location,
             game_count)
  add_scoring(scores, away_team, home_team)
  add_stats(scores, away_team, home_team, stats, location, game_count)
  add_stats_to_season(stats, season_team)
end

#add_players(players, team_name, league) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/big_3/league.rb', line 178

def add_players(players,
                team_name,
                league)
  players.each do |player|
    team_name = team_name.sub("3s","3's")
    player_name = player['@name']
    player_name = player_name.sub("Mahmoud-","Mahmoud ")
    if teams.include?(player_name) || player_name.include?("TEAM") || league[team_name].include?(player_name)
      next
    else
      league[team_name] << player_name
    end
  end
end

#add_scoring(scores, away_team, home_team) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/big_3/league.rb', line 68

def add_scoring(scores,
                away_team,
                home_team)
  scores["first half"] = { "home" => home_team['linescore']["lineprd"][0]["@score"],
                           "away" => away_team['linescore']["lineprd"][0]["@score"] }
  scores["second half"] = { "home" => home_team['linescore']["lineprd"][1]["@score"],
                            "away" => away_team['linescore']["lineprd"][1]["@score"] }
  scores["final"] = { "home" => home_team['linescore']["@score"],
                      "away" => away_team['linescore']["@score"] }
end

#add_stats(scores, away_team, home_team, stats, location, game_count) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/big_3/league.rb', line 111

def add_stats(scores,
              away_team,
              home_team,
              stats,
              location,
              game_count)
  from_both_teams = { "home" => home_team["totals"]["stats"],
                      "away" => away_team["totals"]["stats"] }
  stats["games"] << { "matchup" => "#{home_team["@name"]} vs #{away_team["@name"]}",
                      "home team" => home_team["@name"],
                      "away team" => away_team["@name"],
                      "scores" => scores,
                      "stats" => from_both_teams,
                      "location" => location,
                      "game_number" => game_count.to_s }
end

#add_stats_to_season(stats, team) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/big_3/league.rb', line 79

def add_stats_to_season(stats,
                        team)
  team["totals"]["stats"].each do |k, v|
    v = v.to_f
    stats["season"][k] += v
  end
end

#filter_team_stats(scores, away_team, home_team, stats, season_team, location, game_count) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/big_3/league.rb', line 128

def filter_team_stats(scores,
                      away_team,
                      home_team,
                      stats,
                      season_team,
                      location,
                      game_count)
  stats["name"] = team_name.split.map(&:capitalize).join(' ')
  if names_match?(away_team, team_name)
    add_game(scores, away_team, home_team, stats, season_team, location, game_count)
  elsif names_match?(home_team, team_name)
    add_game(scores, away_team, home_team, stats, season_team, location, game_count)
  end
end

#find_rostersObject



169
170
171
172
173
174
175
176
# File 'lib/big_3/league.rb', line 169

def find_rosters
  @rosters = Hash.new { |h, k| h[k] = [] }
  game_stats.each do |team|
    add_players(team.away_team_players, team.away_team_name, rosters)
    add_players(team.home_team_players, team.home_team_name, rosters)
  end
  @rosters
end

#find_season_averages(stats) ⇒ Object



87
88
89
90
91
92
# File 'lib/big_3/league.rb', line 87

def find_season_averages(stats)
  stats["season"].each do |k, v|
    average = v / stats["games"].length
    stats["season"][k] = average.round(2).to_s
  end
end

#names_match?(item, passed_item) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/big_3/league.rb', line 106

def names_match?(item,
                 passed_item)
  item["@name"].gsub(/[^A-Za-z]/, "").downcase == passed_item.gsub(/[^A-Za-z]/, "").downcase
end

#parse_game_stats(players, stats, location) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/big_3/league.rb', line 50

def parse_game_stats(players,
                     stats,
                     location)
  players.each do |player|
    if names_match?(player, player_name)
      stats["games"] << { "stats" => player["stats"],
                          "location" => location,
                          "game_number" => "#{stats["games"].length + 1}" }
      player["stats"].each do |k, v|
        v = v.to_f
        stats["season"][k] += v
      end
    else
      next
    end
  end
end

#retrieve_game_dataObject



155
156
157
158
# File 'lib/big_3/league.rb', line 155

def retrieve_game_data
  @game_stats = GameManager.new({ids: game_ids}).create_games
  @game_stats
end

#rostersObject



165
166
167
# File 'lib/big_3/league.rb', line 165

def rosters
  @rosters ||= find_rosters
end

#team_statsObject



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

def team_stats
  @t_s = Hash.new { |h, k| h[k] = [] }
  @t_s["season"] = Hash.new { |h, k| h[k] = 0 }
  game_stats.each.with_index(1) do |teams, game_count|
    scores = {}
    if team_name.nil?
      add_scoring(scores, teams.away_team, teams.home_team)
      add_stats(scores, teams.away_team, teams.home_team, t_s, teams.location, game_count)
    else
      filter_team_stats(scores, teams.away_team, teams.home_team, t_s, teams.away_team, teams.location, game_count)
    end
  end
  unless team_name.nil?
    find_season_averages(t_s)
  else
    @t_s.delete("season")
  end
  @t_s
end

#teamsObject



160
161
162
163
# File 'lib/big_3/league.rb', line 160

def teams
  @teams = ["Tri State", "Killer 3s", "Ball Hogs", "3 Headed Monsters", "Trilogy", "Power", "Ghost Ballers", "3's Company"]
  @teams
end