-
Notifications
You must be signed in to change notification settings - Fork 0
/
player-data.js
202 lines (179 loc) · 5.86 KB
/
player-data.js
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
38
39
40
41
42
43
44
45
46
47
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Projections CSV File Format:
* id,last,first,pos,pos1,team,patt,pcmp,pyds,p300,ptds,pint,ratt,ryds,r100,rtds,rec,cyds,c100,ctds,fum,FGM,FGA,EPM,EPA,TD,FF,FR,INT,Sack,Saf,yds,pts,iInt,iPD,iTck,iAst,iSack,iFF,iFR,iTD,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,GP,age,exp,kryd,krtd,pryd,prtd
*
* ADP CSV File Format:
* lname,fname,pos,team,adp_consensus,adp_expert,adp_mfl,adp_mock,adp_cbs,adp_mag,adp_consensus_ppr,adp_expert_ppr,adp_mfl_ppr,adp_mock_ppr
*
* DEF PTS/Game Equation: f(x) = -0.372x + 7.76
*/
var parse = require('csv/node_modules/csv-parse');
var fs = require('fs');
var results = [];
var adpSorted = [];
var lock = 1;
function Result(pos, name, team, total) {
this.pos = pos;
this.name = name;
this.team = team;
this.total = parseFloat(total).toFixed(2);
this.adp = 1000;
};
Result.prototype.setAdp = function setAdp(adp) {
this.adp = adp;
};
Result.prototype.setTotal = function setTotal(total) {
this.total = total.toFixed(2);
};
var totalSort = function(a, b){
return b.total - a.total;
};
var adpSort = function(a, b){
return a.adp - b.adp;
};
function getParser() {
return parse({delimiter: '\t', columns: true, auto_parse: true}, function(err, data) {
for (var i in data) {
var item = data[i];
var pos = item['pos'];
var total;
if (pos === 'K') {
total =
(item['fg'] * 3)
+ ((item['fga'] - item['fg']) * -1)
+ (item['xpt'] * 1);
} else if (pos === 'QB') {
total =
(item['pass_yds'] / 25)
+ (item['pass_tds'] * 6)
+ (item['pass_ints'] * -2)
+ (item['rush_yds'] / 10)
+ (item['rush_tds'] * 6);
} else if (pos === 'RB' || pos === 'WR') {
total =
(item['rush_yds'] / 10)
+ (item['rush_tds'] * 6)
+ (item['rec_att'] * .5)
+ (item['rec_yds'] / 10)
+ (item['rec_tds'] * 6)
+ (item['fumbles'] * -2);
} else if (pos === 'TE') {
total =
(item['rec_att'] * .5)
+ (item['rec_yds'] / 10)
+ (item['rec_tds'] * 6)
+ (item['fumbles'] * -2);
}
results[pos] = results[pos] || [];
var result = new Result(pos, item['name'], item['team'], total);
results[pos].push(result);
if (pos == 'RB' || pos == 'WR') {
results['FLEXRW'] = results['FLEXRW'] || [];
results['FLEXRW'].push(result);
}
if (pos == 'WR' || pos == 'TE') {
results['FLEXWT'] = results['FLEXWT'] || [];
results['FLEXWT'].push(result);
}
};
for (var pos in results) {
results[pos].sort(totalSort);
};
var adpParser = parse({delimiter: '\t', columns: true, auto_parse: true}, function(err, data) {
if (lock == 1) {
lock++;
var setAdpForPlayer = function(player, adp) {
for (var pos in results) {
for (var i in results[pos]) {
if (results[pos][i].name === player) {
results[pos][i].setAdp(adp);
return results[pos][i];
}
}
}
};
for (var i in data) {
var item = data[i];
var player = item['name'];
var adp = item['adp'] || 1000;
adpSorted.push(setAdpForPlayer(player, adp));
};
adpSorted.sort(adpSort);
}
});
fs.createReadStream(__dirname + '/data/adp.csv').pipe(adpParser);
});
}
fs.createReadStream(__dirname + '/data/qb.csv').pipe(getParser());
fs.createReadStream(__dirname + '/data/rb.csv').pipe(getParser());
fs.createReadStream(__dirname + '/data/wr.csv').pipe(getParser());
fs.createReadStream(__dirname + '/data/te.csv').pipe(getParser());
fs.createReadStream(__dirname + '/data/k.csv').pipe(getParser());
var bestAvailable = function bestAvailable(picks) {
function ResultEntry(position, delta, string) {
this.position = position;
this.delta = delta.toFixed(2);
this.string = string;
}
var result = [];
var adp = adpSorted[picks]['adp'];
var nextBest = [];
for (var pos in results) {
for (var i in results[pos]) {
if (results[pos][i].adp > adp) {
nextBest[pos] = results[pos][i];
break;
}
}
var delta = 0;
var string = 'n/a';
if (nextBest[pos]) {
delta = (results[pos][0].total - nextBest[pos].total);
string = results[pos][0].name
+ ' ('
+ results[pos][0].adp
+ ') vs '
+ nextBest[pos].name
+ ' ('
+ nextBest[pos].adp
+ ')';
}
result.push(new ResultEntry(pos, delta, string));
}
result.sort(function(a, b) {
return b.delta - a.delta;
});
return result;
};
var allPlayers = function allPlayers() {
var all = [];
for (var pos in results) {
for (var i in results[pos]) {
if (all.indexOf(results[pos][i]) == -1) {
all.push(results[pos][i]);
}
}
}
all.sort(adpSort);
return all;
};
var draft = function draft(name) {
function removePlayer(players) {
for (var i in players) {
if (players[i] && players[i].name === name) {
players.splice(i, 1);
}
}
};
for (var pos in results) {
removePlayer(results[pos]);
}
removePlayer(adpSorted);
};
var allByPosition = function allByPosition(position) {
return results[position];
};
module.exports.bestAvailable = bestAvailable;
module.exports.allPlayers = allPlayers;
module.exports.draft = draft;
module.exports.allByPosition = allByPosition;