Top Level Namespace
Defined Under Namespace
Modules: FantasyFoobar, NflData
Constant Summary
collapse
- STAT_LINE =
{}
- STAT_CACHE =
{}
Instance Method Summary
collapse
Instance Method Details
#add_player(player, players) ⇒ Object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# 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 ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# 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? ⇒ Boolean
1
2
3
4
5
6
7
8
9
|
# 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) ⇒ Boolean
61
62
63
64
65
66
67
|
# 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) ⇒ Object
35
36
37
38
|
# File 'lib/fantasy_foobar/stats.rb', line 35
def calculate_fumbles(data)
return 0 if data.empty?
data['lost'] * -2
end
|
#calculate_kicking(data) ⇒ Object
30
31
32
33
|
# 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) ⇒ Object
15
16
17
18
|
# 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) ⇒ Object
#calculate_receiving(data) ⇒ Object
25
26
27
28
|
# 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) ⇒ Object
20
21
22
23
|
# 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) ⇒ Object
40
41
42
43
44
45
46
|
# 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) ⇒ Object
69
70
71
72
73
74
75
76
|
# 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) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
|
# 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 ⇒ Object
90
91
92
93
94
95
96
97
98
99
|
# 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) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# 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 ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# 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 ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# 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 ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# 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? ⇒ Boolean
11
12
13
14
15
16
17
18
19
|
# 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) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# 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? ⇒ Boolean
21
22
23
24
25
26
27
28
29
30
31
|
# 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) ⇒ Object
106
107
108
109
110
111
112
113
|
# 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) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# 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
|