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 +4 -4
- data/lib/espn_nba_fantasy/constants.rb +86 -83
- data/lib/espn_nba_fantasy/league.rb +51 -49
- data/lib/espn_nba_fantasy/player.rb +28 -26
- data/lib/espn_nba_fantasy/team.rb +55 -53
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 625920e5a357e3f2eda3567e8241d14572c870da13407e3ab92a4c10d2478ca1
|
4
|
+
data.tar.gz: d2cd2b8c2bbca0c7ce7bf1af48c9f5e7d2c15191c4d4aac73eef2c280cfbcd67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
8
|
-
|
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
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
7
|
+
module ESPNNBAFantasy
|
8
|
+
class League
|
8
9
|
|
9
|
-
|
10
|
+
include PlayerFinder
|
10
11
|
|
11
|
-
|
12
|
+
attr_accessor :teams, :stat_data
|
12
13
|
|
13
|
-
|
14
|
+
#initializes the league
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
def initialize(league_id, year, s2, sw)
|
17
|
+
@league_id = league_id
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
26
|
+
def to_s
|
27
|
+
"League #{@league_id}"
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
+
#gives you an array of each team's name and id in the league
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
def team_list
|
33
|
+
@teams.map{|r| [r.name, r.team_id]}
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
+
#pull up a Player object based on their name and their team
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
42
|
-
|
42
|
+
find_players(team.players, str) || "#{str} cannot be found in #{team_name}'s roster"
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
+
private
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
def make_team_objects
|
48
|
+
@data['teams'].map{|team| Team.new(team, self)}
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
4
|
+
module ESPNNBAFantasy
|
5
|
+
class Player
|
5
6
|
|
6
|
-
|
7
|
+
include CalculateStats
|
7
8
|
|
8
|
-
|
9
|
+
attr_reader :name, :player_id, :position, :stats, :team
|
9
10
|
|
10
|
-
|
11
|
+
#basic initialization with attributes
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def to_s
|
22
|
+
"Player: #{name} | Position: #{position} | Stats: #{stats}"
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
+
private
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
6
|
+
module ESPNNBAFantasy
|
7
|
+
class Team
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
include CalculateStats
|
10
|
+
include PlayerFinder
|
10
11
|
|
11
|
-
|
12
|
+
attr_accessor :roster, :players, :name, :teamstats, :league, :team_id
|
12
13
|
|
13
|
-
|
14
|
+
#basic initialization with the various attributes
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
25
|
-
|
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
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def to_s
|
32
|
+
"Team Name: #{name} | Roster: #{roster_names}"
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
46
|
+
private
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
def roster_names
|
49
|
+
players.map(&:name)
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
def stat_differences(old, new)
|
53
|
+
new.map {|stat, val| [stat, val - old[stat]]}.to_h
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
64
|
+
def roster_maker(obj)
|
65
|
+
obj['roster']['entries'].map {|e| Player.new(e['playerPoolEntry']['player'], self)}
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|