class Object
Constants
- STAT_CACHE
- STAT_LINE
Public Instance Methods
add_player(player, players)
click to toggle source
# File lib/fantasy_foobar/players.rb, line 1 def add_player(player, players) opts = players.find_all { |i| i['last_name'].casecmp(player).zero? } unless opts.any? puts puts "Player with the last name '#{player}' does not exist" return end selection = {} puts opts.each_with_index do |opt, i| team = TEAMS[opt['team']] primary_color = team['primary_color'] secondary_color = team['secondary_color'] stat_categories = stat_categories(opt['position']) gsid = get_player_gsid(opt['profile_link']) selection[(i + 1).to_s] = opt.merge(stat_categories).merge(gsid) puts "#{i + 1}. #{Paint[opt['full_name'], :bright]} from the #{Paint[team['name'], secondary_color, primary_color, :bright]} " end puts puts '----------------------------------------------------- 🏈' selected_player = get_selected_player(selection) if selected_player.nil? puts puts 'YOUR TEAM HAS NOT BEEN UPDATED' return end CURRENT_TEAM << selected_player puts puts "You added #{Paint[CURRENT_TEAM.last['full_name'], :bright]} " end
add_players()
click to toggle source
# File lib/fantasy_foobar/players.rb, line 39 def add_players players = JSON.parse(NflData::API::Player.get_all) player_pool = players['quarterbacks'] + players['wide_receivers'] + players['runningbacks'] + players['tight_ends'] + players['kickers'] print_current_team input = 'y' while input.casecmp('y').zero? printf 'Add a player to your team: ' player = gets.chomp add_player(player, player_pool) inner_input = '' while !inner_input.casecmp('y').zero? && !inner_input.casecmp('n').zero? print_current_team printf 'Add another player? (y/n) ' inner_input = gets.chomp end input = inner_input end end
add_players?()
click to toggle source
# File lib/fantasy_foobar/app_flow.rb, line 1 def add_players? input = '' while !input.casecmp('y').zero? && !input.casecmp('n').zero? printf 'Would you like to add players to your team? (y/n) ' input = gets.chomp end input == 'y' end
bye_week?(player)
click to toggle source
# File lib/fantasy_foobar/players.rb, line 61 def bye_week?(player) if JSON.parse(CURRENT_GAMES)[player['team']].nil? puts "#{player['full_name']} does not have a game this week" return true end false end
calculate_fumbles(data)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 35 def calculate_fumbles(data) return 0 if data.empty? data['lost'] * -2 end
calculate_kicking(data)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 30 def calculate_kicking(data) return 0 if data.empty? data['xpmade'] + (data['fgm'] * 3) - (data['fga'] - data['fgm']) end
calculate_passing(data)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 15 def calculate_passing(data) return 0 if data.empty? (data['yds'] * 0.04) + (data['tds'] * 4) + (data['twoptm'] * 2) - (data['ints'] * 2) end
calculate_points(stat_category, data)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 4 def calculate_points(stat_category, data) case stat_category when 'passing' then calculate_passing(data) when 'rushing' then calculate_rushing(data) when 'receiving' then calculate_receiving(data) when 'kicking' then calculate_kicking(data) when 'fumbles' then calculate_fumbles(data) else 0 end end
calculate_receiving(data)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 25 def calculate_receiving(data) return 0 if data.empty? (data['yds'] * 0.1) + (data['tds'] * 6) + (data['twoptm'] * 2) + (data['rec'] * 0.5) end
calculate_rushing(data)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 20 def calculate_rushing(data) return 0 if data.empty? (data['yds'] * 0.1) + (data['tds'] * 6) + (data['twoptm'] * 2) end
find_or_create_statline(player)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 40 def find_or_create_statline(player) STAT_LINE[player['gsid']] ||= {} player['stat_categories'].each do |stat| STAT_LINE[player['gsid']][stat] ||= {} end end
get_player_gsid(url)
click to toggle source
# File lib/fantasy_foobar/players.rb, line 69 def get_player_gsid(url) req = Net::HTTP.get_response(URI.parse(url)).body doc = Nokogiri::HTML(req) gsid = doc.xpath("//comment()[contains(.,'GSIS ID')]").first.text.split('GSIS ID: ')[1].split("\n\t")[0] { 'gsid' => gsid } end
get_selected_player(selection)
click to toggle source
# File lib/fantasy_foobar/players.rb, line 78 def get_selected_player(selection) puts printf "Input number to confirm player above you'd like to add (enter any other value to cancel): " player = gets.chomp if player.to_i.zero? return nil end selection[player] end
print_current_team()
click to toggle source
# File lib/fantasy_foobar/players.rb, line 90 def print_current_team puts CURRENT_TEAM.each_with_index do |player, i| team = TEAMS[player['team']] primary_color = team['primary_color'] secondary_color = team['secondary_color'] puts "#{i + 1}. #{Paint[player['full_name'], :bright]} from the #{Paint[team['name'], secondary_color, primary_color, :bright]} " end puts end
print_player_feed(player)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 48 def print_player_feed(player) return if bye_week?(player) player_gsid = player['gsid'] team = TEAMS[player['team']] primary_color = team['primary_color'] secondary_color = team['secondary_color'] STAT_CACHE[player_gsid] = {} eid = JSON.parse(CURRENT_GAMES)[player['team']]['eid'] loop.with_index do |_, i| begin data = Net::HTTP.get_response(URI.parse("http://www.nfl.com/liveupdate/game-center/#{eid}/#{eid}_gtd.json")).body JSON.parse(data)[eid]['drives'].each do |drive_key, drive_value| STAT_CACHE[player_gsid][drive_key] ||= {} next if drive_key == 'crntdrv' drive_value['plays'].each do |play_key, play_value| next if STAT_CACHE[player_gsid][drive_key][play_key] STAT_CACHE[player_gsid][drive_key][play_key] = play_value['desc'] next unless play_value['players'][player_gsid] puts Paint[play_value['desc'], secondary_color, primary_color, :bright] player['stat_categories'].each do |stat_category| updated_statline(player, data, stat_category) end updated_fantasy_points(player) end end rescue i.zero? && (puts "#{player['full_name']}'s game has not yet started") end sleep 30 end end
print_stats()
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 84 def print_stats threads = [] CURRENT_TEAM.each do |player| threads << Thread.new(player) do |my_player| find_or_create_statline(my_player) print_player_feed(my_player) end end threads.each(&:join) end
remove_player()
click to toggle source
# File lib/fantasy_foobar/players.rb, line 101 def remove_player printf "Input the number of player above you'd like to remove (enter any other value to cancel): " player = gets.chomp if player.to_i.zero? puts puts 'YOUR TEAM HAS NOT BEEN UPDATED' return end unless CURRENT_TEAM.delete_at(player.to_i - 1) puts puts 'YOUR TEAM HAS NOT BEEN UPDATED' end end
remove_players()
click to toggle source
# File lib/fantasy_foobar/players.rb, line 117 def remove_players input = 'y' while input.casecmp('y').zero? print_current_team remove_player inner_input = '' while !inner_input.casecmp('y').zero? && !inner_input.casecmp('n').zero? inner_input = 'n' && break unless CURRENT_TEAM.any? print_current_team printf 'Remove another player? (y/n) ' inner_input = gets.chomp end input = inner_input end end
remove_players?()
click to toggle source
# File lib/fantasy_foobar/app_flow.rb, line 11 def remove_players? input = '' while !input.casecmp('y').zero? && !input.casecmp('n').zero? printf 'Would you like to remove players from your team? (y/n) ' input = gets.chomp end input == 'y' end
stat_categories(pos)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 96 def stat_categories(pos) categories = case pos when 'QB' then { 'stat_categories' => %w[passing rushing fumbles] } when 'K' then { 'stat_categories' => %w[kicking] } when 'TE' then { 'stat_categories' => %w[receiving fumbles] } else { 'stat_categories' => %w[receiving rushing fumbles] } end categories end
update_team?()
click to toggle source
# File lib/fantasy_foobar/app_flow.rb, line 21 def update_team? print_current_team input = '' while !input.casecmp('y').zero? && !input.casecmp('n').zero? printf 'Would you like to update your current team? (y/n) ' input = gets.chomp end input == 'y' end
updated_fantasy_points(player)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 106 def updated_fantasy_points(player) points = 0 STAT_LINE[player['gsid']].each do |k,v| points += calculate_points(k, v) end STAT_LINE[player['gsid']]['fantasy_points'] = points puts "#{player['full_name']}: #{'%g' % ('%.1f' % points)} fantasy points" end
updated_statline(player, data, stat_category)
click to toggle source
# File lib/fantasy_foobar/stats.rb, line 115 def updated_statline(player, data, stat_category) player_name = "#{player['first_name'][0]}.#{player['last_name']}" player_gsid = player['gsid'] eid = JSON.parse(CURRENT_GAMES)[player['team']]['eid'] location = JSON.parse(CURRENT_GAMES)[player['team']]['location'] team_stat_category = JSON.parse(data)[eid][location]['stats'][stat_category] return if team_stat_category.nil? player_data = team_stat_category[player_gsid] return if player_data.nil? STAT_LINE[player_gsid] ||= {} STAT_LINE[player_gsid][stat_category] = player_data result = case stat_category when 'passing' then "#{player_name}: #{player_data['cmp']}/#{player_data['att']} for #{player_data['yds']} yards with #{player_data['tds']} touchdowns and #{player_data['ints']} interceptions" when 'rushing' then "#{player_name}: #{player_data['att']} attempts for #{player_data['yds']} yards and #{player_data['tds']} touchdowns" when 'receiving' then "#{player_name}: #{player_data['rec']} receptions for #{player_data['yds']} yards and #{player_data['tds']} touchdowns" when 'kicking' then "#{player_name}: #{player_data['fgm']}/#{player_data['fga']} in FGs and #{player_data['xpmade']}/#{player_data['xpa']} in extra points" when 'fumbles' then "#{player_name}: #{player_data['lost']} fumble lost" else '' end puts result end