espn_nba_fantasy 0.0.4 → 0.0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4306b1e3c1b828575b2baf7c5b237c54bfc3916463f84170a054c13dcd3d1dba
4
- data.tar.gz: 833f12b70aabf99b9365ac334924b612dd9b0e71603f69d7305091d68fedb140
3
+ metadata.gz: 625920e5a357e3f2eda3567e8241d14572c870da13407e3ab92a4c10d2478ca1
4
+ data.tar.gz: d2cd2b8c2bbca0c7ce7bf1af48c9f5e7d2c15191c4d4aac73eef2c280cfbcd67
5
5
  SHA512:
6
- metadata.gz: 88b1dcadb34024a8bf8d42c572fcea180fe3167370bda2dcb2a76a538c1014a1502e03feece0c95b504e04c054b9b3c91e26a2c19ffca9dfc80e9f838f1cdfd6
7
- data.tar.gz: feab0635a4fae468d362e316ac791d0410066c0889170babccf94632210e021527ef25130bb40bb073c1698135b4a4ca83741f86333af31bb31771d2c9220be7
6
+ metadata.gz: 6e7e2f7d59c0a87244665acc1b5a4dc2caffb0198f12eb02a86ee4d666a0dd84d7860fedc6e319c731921f1f1118785de56ad72f7dfef49eb2db5e27248c8425
7
+ data.tar.gz: e7eb379f3712c83520146fef0f7c5b15669954d6068c66d07de0dfbb6b551da9aff543edabb4fb8cf36665ec17afc2ffc0112cf0c0793fd610f0dc3b2e9c321f
@@ -1,94 +1,97 @@
1
1
  #these are some of the constants necessary to map ids from the ESPN Fantasy Library to,
2
2
  #so that things like position, stat name, etc are readable!
3
3
 
4
- POSITION_MAP = ['PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'SG/SF', 'G/F', 'PF/C', 'F/C', 'UT', 'BE', 'IR', '', 'Rookie']
4
+ module ESPNNBAFantasy
5
+
6
+ POSITION_MAP = ['PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'SG/SF', 'G/F', 'PF/C', 'F/C', 'UT', 'BE', 'IR', '', 'Rookie']
5
7
 
6
- PRO_TEAM_MAP = ['FA', 'ATL','BOS','NOP','CHI','CLE','DAL','DEN','DET','GSW','HOU','IND',
7
- 'LAC','LAL','MIA','MIL','MIN','BKN','NYK','ORL','PHL','PHO','POR','SAC','SAS','OKC',
8
- 'UTA','WAS','TOR','MEM','CHA']
8
+ PRO_TEAM_MAP = ['FA', 'ATL','BOS','NOP','CHI','CLE','DAL','DEN','DET','GSW','HOU','IND',
9
+ 'LAC','LAL','MIA','MIL','MIN','BKN','NYK','ORL','PHL','PHO','POR','SAC','SAS','OKC',
10
+ 'UTA','WAS','TOR','MEM','CHA']
9
11
 
10
- STATS_MAP = {
11
- '0' => 'PTS',
12
- '1' => 'BLK',
13
- '2' => 'STL',
14
- '3' => 'AST',
15
- '4' => 'OREB',
16
- '5' => 'DREB',
17
- '6' => 'REB',
18
- '7'=> '7',
19
- '8'=> '8',
20
- '9'=> 'PF',
21
- '10'=> '10',
22
- '11'=> 'TO',
23
- '12'=> '12',
24
- '13'=> 'FGM',
25
- '14'=> 'FGA',
26
- '15'=> 'FTM',
27
- '16'=> 'FTA',
28
- '17'=> '3PTM',
29
- '18'=> '3PTA',
30
- '19'=> 'FG%',
31
- '20'=> 'FT%',
32
- '21'=> '3PT%',
33
- '22'=> '22',
34
- '23'=> '23',
35
- '24'=> '24',
36
- '25'=> '25',
37
- '26'=> '26',
38
- '27'=> '27',
39
- '28'=> 'MPG',
40
- '29'=> '29',
41
- '30'=> '30',
42
- '31'=> '31',
43
- '32'=> '32',
44
- '33'=> '33',
45
- '34'=> '34',
46
- '35'=> '35',
47
- '36'=> '36',
48
- '37'=> '37',
49
- '38'=> '38',
50
- '39'=> '39',
51
- '40'=> 'MIN',
52
- '41'=> 'GS',
53
- '42'=> 'GP',
54
- '43'=> '43',
55
- '44'=> '44',
56
- '45'=> '45',
57
- }
12
+ STATS_MAP = {
13
+ '0' => 'PTS',
14
+ '1' => 'BLK',
15
+ '2' => 'STL',
16
+ '3' => 'AST',
17
+ '4' => 'OREB',
18
+ '5' => 'DREB',
19
+ '6' => 'REB',
20
+ '7'=> '7',
21
+ '8'=> '8',
22
+ '9'=> 'PF',
23
+ '10'=> '10',
24
+ '11'=> 'TO',
25
+ '12'=> '12',
26
+ '13'=> 'FGM',
27
+ '14'=> 'FGA',
28
+ '15'=> 'FTM',
29
+ '16'=> 'FTA',
30
+ '17'=> '3PTM',
31
+ '18'=> '3PTA',
32
+ '19'=> 'FG%',
33
+ '20'=> 'FT%',
34
+ '21'=> '3PT%',
35
+ '22'=> '22',
36
+ '23'=> '23',
37
+ '24'=> '24',
38
+ '25'=> '25',
39
+ '26'=> '26',
40
+ '27'=> '27',
41
+ '28'=> 'MPG',
42
+ '29'=> '29',
43
+ '30'=> '30',
44
+ '31'=> '31',
45
+ '32'=> '32',
46
+ '33'=> '33',
47
+ '34'=> '34',
48
+ '35'=> '35',
49
+ '36'=> '36',
50
+ '37'=> '37',
51
+ '38'=> '38',
52
+ '39'=> '39',
53
+ '40'=> 'MIN',
54
+ '41'=> 'GS',
55
+ '42'=> 'GP',
56
+ '43'=> '43',
57
+ '44'=> '44',
58
+ '45'=> '45',
59
+ }
58
60
 
59
- STAT_ID_MAP = {
60
- '00': 'total',
61
- '10': 'projected',
62
- '01': 'last_7',
63
- '02': 'last_15',
64
- '03': 'last_30'
65
- }
61
+ STAT_ID_MAP = {
62
+ '00': 'total',
63
+ '10': 'projected',
64
+ '01': 'last_7',
65
+ '02': 'last_15',
66
+ '03': 'last_30'
67
+ }
66
68
 
67
- ACTIVITY_MAP = {
68
- 178 => 'FA ADDED',
69
- 180 => 'WAIVER ADDED',
70
- 179 => 'DROPPED',
71
- 181 => 'DROPPED',
72
- 239 => 'DROPPED',
73
- 244 => 'TRADED',
74
- 'FA': 178,
75
- 'WAIVER': 180,
76
- 'TRADED': 244
77
- }
69
+ ACTIVITY_MAP = {
70
+ 178 => 'FA ADDED',
71
+ 180 => 'WAIVER ADDED',
72
+ 179 => 'DROPPED',
73
+ 181 => 'DROPPED',
74
+ 239 => 'DROPPED',
75
+ 244 => 'TRADED',
76
+ 'FA': 178,
77
+ 'WAIVER': 180,
78
+ 'TRADED': 244
79
+ }
78
80
 
79
- NINE_CAT_STATS = [
80
- '3PTM',
81
- 'AST',
82
- 'BLK',
83
- 'FG%',
84
- 'FT%',
85
- 'PTS',
86
- 'REB',
87
- 'STL',
88
- 'TO'
89
- ]
81
+ NINE_CAT_STATS = [
82
+ '3PTM',
83
+ 'AST',
84
+ 'BLK',
85
+ 'FG%',
86
+ 'FT%',
87
+ 'PTS',
88
+ 'REB',
89
+ 'STL',
90
+ 'TO'
91
+ ]
90
92
 
91
- STATS = ['PTS','BLK','STL','AST','OREB','DREB','TO','FGM','FTM','3PTM', 'FGA', '3PTA', 'FTA']
93
+ STATS = ['PTS','BLK','STL','AST','OREB','DREB','TO','FGM','FTM','3PTM', 'FGA', '3PTA', 'FTA']
92
94
 
93
- EMPTY_STATS = {'PTS'=>0,'BLK'=>0,'STL'=>0,'AST'=>0,'OREB'=>0,'DREB'=>0,'TO'=>0,'FGM'=>0,'FTM'=>0,'3PTM'=>0, 'FGA'=>0, '3PTA'=>0, 'FTA'=>0}
95
+ EMPTY_STATS = {'PTS'=>0,'BLK'=>0,'STL'=>0,'AST'=>0,'OREB'=>0,'DREB'=>0,'TO'=>0,'FGM'=>0,'FTM'=>0,'3PTM'=>0, 'FGA'=>0, '3PTA'=>0, 'FTA'=>0}
94
96
 
97
+ end
@@ -4,73 +4,75 @@ require_relative 'constants'
4
4
  require_relative 'team'
5
5
  require_relative 'player_finder'
6
6
 
7
- class League
7
+ module ESPNNBAFantasy
8
+ class League
8
9
 
9
- include PlayerFinder
10
+ include PlayerFinder
10
11
 
11
- attr_accessor :teams, :stat_data
12
+ attr_accessor :teams, :stat_data
12
13
 
13
- #initializes the league
14
+ #initializes the league
14
15
 
15
- def initialize(league_id, year, s2, sw)
16
- @league_id = league_id
16
+ def initialize(league_id, year, s2, sw)
17
+ @league_id = league_id
17
18
 
18
- @uri = "https://lm-api-reads.fantasy.espn.com/apis/v3/games/fba/seasons/#{year}/segments/0/leagues/#{league_id}?view=mRoster&view=mSettings&view=mTeam&view=modular&view=mNav"
19
- @cookies = {'espn_s2': "#{s2}", 'SWID': "#{sw}"}
20
- @data = JSON.parse(RestClient.get(@uri, {cookies: @cookies}))
21
- @teams = make_team_objects
22
- @stat_data = make_stat_data
23
- end
19
+ @uri = "https://lm-api-reads.fantasy.espn.com/apis/v3/games/fba/seasons/#{year}/segments/0/leagues/#{league_id}?view=mRoster&view=mSettings&view=mTeam&view=modular&view=mNav"
20
+ @cookies = {'espn_s2': "#{s2}", 'SWID': "#{sw}"}
21
+ @data = JSON.parse(RestClient.get(@uri, {cookies: @cookies}))
22
+ @teams = make_team_objects
23
+ @stat_data = make_stat_data
24
+ end
24
25
 
25
- def to_s
26
- "League #{@league_id}"
27
- end
26
+ def to_s
27
+ "League #{@league_id}"
28
+ end
28
29
 
29
- #gives you an array of each team's name and id in the league
30
+ #gives you an array of each team's name and id in the league
30
31
 
31
- def team_list
32
- @teams.map{|r| [r.name, r.team_id]}
33
- end
32
+ def team_list
33
+ @teams.map{|r| [r.name, r.team_id]}
34
+ end
34
35
 
35
- #pull up a Player object based on their name and their team
36
+ #pull up a Player object based on their name and their team
36
37
 
37
- def findplayer(team_name, str)
38
- team = teams.select{|t| t.name == team_name}.first
39
- return "No team named #{team_name}" unless team
38
+ def findplayer(team_name, str)
39
+ team = teams.select{|t| t.name == team_name}.first
40
+ return "No team named #{team_name}" unless team
40
41
 
41
- find_players(team.players, str) || "#{str} cannot be found in #{team_name}'s roster"
42
- end
42
+ find_players(team.players, str) || "#{str} cannot be found in #{team_name}'s roster"
43
+ end
43
44
 
44
- private
45
+ private
45
46
 
46
- def make_team_objects
47
- @data['teams'].map{|team| Team.new(team, self)}
48
- end
47
+ def make_team_objects
48
+ @data['teams'].map{|team| Team.new(team, self)}
49
+ end
49
50
 
50
- def make_stat_data
51
- stats = {}
52
- teams.each do |team|
53
- team.teamstats.each do |stat, value|
54
- stats[stat] ? stats[stat] << value : stats[stat] = [value]
51
+ def make_stat_data
52
+ stats = {}
53
+ teams.each do |team|
54
+ team.teamstats.each do |stat, value|
55
+ stats[stat] ? stats[stat] << value : stats[stat] = [value]
56
+ end
55
57
  end
58
+ calculate_league_stats(stats)
59
+ end
60
+
61
+ def calculate_league_stats(s)
62
+ calc = {}
63
+ s.each do |stat, values|
64
+ avg = (values.sum/values.length).to_f
65
+ calc["#{stat} Average"] = avg
66
+ calc["#{stat} Standard Deviation"] = std_dev(avg, values)
67
+ end
68
+ calc
56
69
  end
57
- calculate_league_stats(stats)
58
- end
59
70
 
60
- def calculate_league_stats(s)
61
- calc = {}
62
- s.each do |stat, values|
63
- avg = (values.sum/values.length).to_f
64
- calc["#{stat} Average"] = avg
65
- calc["#{stat} Standard Deviation"] = std_dev(avg, values)
71
+ def std_dev(mean, values)
72
+ sum = values.inject(0){|accum, i| accum +(i-mean)**2 }
73
+ variance = sum/(values.length - 1).to_f
74
+ Math.sqrt(variance)
66
75
  end
67
- calc
68
- end
69
76
 
70
- def std_dev(mean, values)
71
- sum = values.inject(0){|accum, i| accum +(i-mean)**2 }
72
- variance = sum/(values.length - 1).to_f
73
- Math.sqrt(variance)
74
77
  end
75
-
76
78
  end
@@ -1,39 +1,41 @@
1
1
  require_relative 'constants'
2
2
  require_relative 'calculate_stats'
3
3
 
4
- class Player
4
+ module ESPNNBAFantasy
5
+ class Player
5
6
 
6
- include CalculateStats
7
+ include CalculateStats
7
8
 
8
- attr_reader :name, :player_id, :position, :stats, :team
9
+ attr_reader :name, :player_id, :position, :stats, :team
9
10
 
10
- #basic initialization with attributes
11
+ #basic initialization with attributes
11
12
 
12
- def initialize(player, team)
13
- @name = player['fullName']
14
- @player_id = player['id']
15
- @position = POSITION_MAP[player['defaultPositionId']-1]
16
- @stats = stat_card_maker(player)
17
- @team = team
18
- end
13
+ def initialize(player, team)
14
+ @name = player['fullName']
15
+ @player_id = player['id']
16
+ @position = POSITION_MAP[player['defaultPositionId']-1]
17
+ @stats = stat_card_maker(player)
18
+ @team = team
19
+ end
19
20
 
20
- def to_s
21
- "Player: #{name} | Position: #{position} | Stats: #{stats}"
22
- end
21
+ def to_s
22
+ "Player: #{name} | Position: #{position} | Stats: #{stats}"
23
+ end
23
24
 
24
- private
25
+ private
25
26
 
26
- def stat_card_maker(player)
27
- avgstats = player['stats'].first['averageStats']
28
- statcard = avgstats ? avgstats.transform_keys!{ |k| STATS_MAP[k] }.slice(*STATS) : EMPTY_STATS
29
- calculate_stats(statcard)
30
- end
27
+ def stat_card_maker(player)
28
+ avgstats = player['stats'].first['averageStats']
29
+ statcard = avgstats ? avgstats.transform_keys!{ |k| STATS_MAP[k] }.slice(*STATS) : EMPTY_STATS
30
+ calculate_stats(statcard)
31
+ end
31
32
 
32
- def calculate_stats(s)
33
- s['FGA'] = s['FGA'] + 0.0001
34
- s['TO'] = s['TO'] + 0.00001
35
- s['FTA'] = s['FTA'] + 0.00001
36
- add_calculated_stats(s)
37
- end
33
+ def calculate_stats(s)
34
+ s['FGA'] = s['FGA'] + 0.0001
35
+ s['TO'] = s['TO'] + 0.00001
36
+ s['FTA'] = s['FTA'] + 0.00001
37
+ add_calculated_stats(s)
38
+ end
38
39
 
40
+ end
39
41
  end
@@ -3,73 +3,75 @@ require_relative 'player'
3
3
  require_relative 'calculate_stats'
4
4
  require_relative 'player_finder'
5
5
 
6
- class Team
6
+ module ESPNNBAFantasy
7
+ class Team
7
8
 
8
- include CalculateStats
9
- include PlayerFinder
9
+ include CalculateStats
10
+ include PlayerFinder
10
11
 
11
- attr_accessor :roster, :players, :name, :teamstats, :league, :team_id
12
+ attr_accessor :roster, :players, :name, :teamstats, :league, :team_id
12
13
 
13
- #basic initialization with the various attributes
14
+ #basic initialization with the various attributes
14
15
 
15
- def initialize(obj, league)
16
- @name = obj['name']
17
- @team_id = obj['id']
18
- @players = roster_maker(obj)
19
- @roster = roster_names
20
- @teamstats = make_team_stats(@players)
21
- @league = league
22
- end
16
+ def initialize(obj, league)
17
+ @name = obj['name']
18
+ @team_id = obj['id']
19
+ @players = roster_maker(obj)
20
+ @roster = roster_names
21
+ @teamstats = make_team_stats(@players)
22
+ @league = league
23
+ end
23
24
 
24
- #method that lets you input who you're trading (Array of names), who you're receiving (Array of names),
25
- #and what team you're trading with (String).
25
+ #method that lets you input who you're trading (Array of names), who you're receiving (Array of names),
26
+ #and what team you're trading with (String).
26
27
 
27
- #returns data including the players trading away and receiving, your stats before and after the trade, and your
28
- #change in stats for each category
28
+ #returns data including the players trading away and receiving, your stats before and after the trade, and your
29
+ #change in stats for each category
29
30
 
30
- def to_s
31
- "Team Name: #{name} | Roster: #{roster_names}"
32
- end
31
+ def to_s
32
+ "Team Name: #{name} | Roster: #{roster_names}"
33
+ end
33
34
 
34
- def trade_players(to_trade = [], to_receive = [], other_team_name)
35
- other_team = league.teams.select{|team| team.name == other_team_name}.first
36
- return "No team named #{other_team_name}" unless other_team
37
- own_players = to_trade.map{|player| find_players(players, player)}
38
- other_players = to_receive.map{|player| find_players(other_team.players, player)}
39
- return "This trade included players not one of the specified teams" if (own_players.include?(nil)|| other_players.include?(nil))
40
- {'Trading:' => own_players.map{|p| p.name},
41
- 'Receiving:' => other_players.map{|p| p.name}}.merge(new_team_stats(own_players, other_players))
42
-
43
- end
35
+ def trade_players(to_trade = [], to_receive = [], other_team_name)
36
+ other_team = league.teams.select{|team| team.name == other_team_name}.first
37
+ return "No team named #{other_team_name}" unless other_team
38
+ own_players = to_trade.map{|player| find_players(players, player)}
39
+ other_players = to_receive.map{|player| find_players(other_team.players, player)}
40
+ return "This trade included players not one of the specified teams" if (own_players.include?(nil)|| other_players.include?(nil))
41
+ {'Trading:' => own_players.map{|p| p.name},
42
+ 'Receiving:' => other_players.map{|p| p.name}}.merge(new_team_stats(own_players, other_players))
43
+
44
+ end
44
45
 
45
- private
46
+ private
46
47
 
47
- def roster_names
48
- players.map(&:name)
49
- end
48
+ def roster_names
49
+ players.map(&:name)
50
+ end
50
51
 
51
- def stat_differences(old, new)
52
- new.map {|stat, val| [stat, val - old[stat]]}.to_h
53
- end
52
+ def stat_differences(old, new)
53
+ new.map {|stat, val| [stat, val - old[stat]]}.to_h
54
+ end
54
55
 
55
- def new_team_stats(own, others)
56
- new_ros = players.dup
57
- others.each{|other| new_ros << other}
58
- own.each {|p| new_ros.delete_at(new_ros.index(p))}
59
- newstats = make_team_stats(new_ros)
60
- {'Before:' => teamstats, 'After:' => newstats, 'Changes:' => stat_differences(teamstats, newstats) }
61
- end
56
+ def new_team_stats(own, others)
57
+ new_ros = players.dup
58
+ others.each{|other| new_ros << other}
59
+ own.each {|p| new_ros.delete_at(new_ros.index(p))}
60
+ newstats = make_team_stats(new_ros)
61
+ {'Before:' => teamstats, 'After:' => newstats, 'Changes:' => stat_differences(teamstats, newstats) }
62
+ end
62
63
 
63
- def roster_maker(obj)
64
- obj['roster']['entries'].map {|e| Player.new(e['playerPoolEntry']['player'], self)}
65
- end
64
+ def roster_maker(obj)
65
+ obj['roster']['entries'].map {|e| Player.new(e['playerPoolEntry']['player'], self)}
66
+ end
66
67
 
67
- def make_team_stats(ros)
68
- stats = {}
69
- ros.each do |player|
70
- player.stats.each {|stat, value| stats[stat] ? stats[stat] += value : stats[stat] = value}
68
+ def make_team_stats(ros)
69
+ stats = {}
70
+ ros.each do |player|
71
+ player.stats.each {|stat, value| stats[stat] ? stats[stat] += value : stats[stat] = value}
72
+ end
73
+ add_calculated_stats(stats)
71
74
  end
72
- add_calculated_stats(stats)
73
- end
74
75
 
76
+ end
75
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: espn_nba_fantasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Usborn Ocampo