forked from Setfive/fanduel-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineupGenerator.ts
205 lines (155 loc) · 7.5 KB
/
LineupGenerator.ts
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
203
204
205
import * as _ from "lodash";
import * as Q from "q";
import {Lineup, Player, SlateDetails, Fixture} from "./models";
class LineupSearch {
searchIndex : { [key:string] : number; };
currentLineup : Lineup;
constructor(searchIndex : { [key:string] : number; }, currentLineup : Lineup){
this.searchIndex = searchIndex;
this.currentLineup = currentLineup;
}
}
export class LineupGenerator {
slateDetails : SlateDetails;
players : Player[];
lineupDeferred : Q.Deferred<Lineup>;
groupedPlayers : { [key:string] : Player[]; };
bestFirstPlayers : { [key:string] : Player[]; };
salarySortedPlayers : { [key:string] : Player[]; };
isHalted : boolean = false;
queuedLineups : LineupSearch[] = [];
validLineups : Lineup[] = [];
constructor(slateDetails : SlateDetails, players : Player[]){
this.slateDetails = slateDetails;
this.players = players;
}
public createValidLineup(timeout : number = 10000) : Q.Promise<Lineup> {
this.queuedLineups = [];
this.validLineups = [];
this.lineupDeferred = Q.defer<Lineup>();
let lt = new Lineup(this.slateDetails);
const playerPool = _.filter(this.players, f => f.injured == false && f.fppg > 0);
this.groupedPlayers = _.mapValues(_.groupBy(playerPool, f => f.position), list => _.sortBy(list, "fppg"));
this.bestFirstPlayers = _.mapValues(this.groupedPlayers, list => list.reverse());
this.salarySortedPlayers = _.mapValues(this.groupedPlayers, list => _.sortBy(list, "salary"));
this.queuedLineups = _.range(0, 1000).map(f => {
const indexes = _.mapValues(this.groupedPlayers, list => this.randomInt(0, list.length));
return new LineupSearch(indexes, lt);
});
this.processQueuedLineups();
setTimeout(() => {
const bestLineup = this.validLineups.length ? this.validLineups[0] : null;
this.isHalted = true;
this.lineupDeferred.resolve(bestLineup);
}, timeout);
return this.lineupDeferred.promise;
}
public processQueuedLineups(): void {
if (this.isHalted == true) {
return;
}
const args = this.queuedLineups.shift();
this.searchValidLineupsForRoster.call(this, args);
if (this.queuedLineups.length) {
// this.queuedLineups = _.shuffle(this.queuedLineups);
setImmediate(this.processQueuedLineups.bind(this));
} else {
this.lineupDeferred.resolve();
this.isHalted = true;
}
}
public searchValidLineupsForRoster(searchParams : LineupSearch) : void {
if(!this.isValidRosterTree(searchParams)){
return;
}
const targetPosition = _.find(searchParams.currentLineup.roster, {player: null});
if(targetPosition == null
|| searchParams.searchIndex[targetPosition.position] >= this.groupedPlayers[targetPosition.position].length){
return;
}
const playerToAdd = this.groupedPlayers[targetPosition.position][ searchParams.searchIndex[targetPosition.position] ];
const updatedRoster = this.addPlayerToRoster(playerToAdd, searchParams.currentLineup);
if(updatedRoster.isValidRoster){
this.sortAndTrimLineups(updatedRoster);
return;
}
if(updatedRoster.isPossibleRoster) {
const nextSearch = _.extend({}, searchParams);
nextSearch.searchIndex[targetPosition.position] += 1;
this.queuedLineups.push(nextSearch);
}
searchParams.currentLineup = updatedRoster;
this.searchValidLineupsForRoster(searchParams);
}
public sortAndTrimLineups(lineup: Lineup): void {
this.validLineups.push(lineup);
this.validLineups = _.sortBy(this.validLineups, f => f.projectedFanduelPoints).reverse();
this.validLineups = this.validLineups.slice(0, 10);
}
public addPlayerToRoster(playerToAdd : Player, lineup : Lineup) : Lineup {
let didAdd = false;
const newLineup = lineup.clone();
for(var i = 0; i < newLineup.roster.length && didAdd == false; i++){
if(newLineup.roster[i].player && newLineup.roster[i].player.id == playerToAdd.id){
didAdd = false;
break;
}
if(newLineup.roster[i].position == playerToAdd.position && newLineup.roster[i].player == null){
newLineup.roster[i].player = playerToAdd;
didAdd = true;
}
}
newLineup.projectedFanduelPoints = _.reduce(newLineup.roster,
(total, p) => total + (p.player ? p.player.fppg : 0), 0);
newLineup.remainingSalary -= playerToAdd.salary;
if(didAdd == false || newLineup.remainingSalary < 0){
newLineup.isValidRoster = false;
return newLineup;
}
const occupiedRosters = newLineup.roster.filter(f => f.player != null);
const rosterPitcher = _.find(newLineup.roster, p => p.position == "P");
let hasPitcherAndBatterFromSameTeam = false;
let pitcherTeam : Fixture = null;
if(rosterPitcher){
const pitcherTeamates = occupiedRosters
.map(f => f.player.team)
.filter(f => f == rosterPitcher.player.team);
hasPitcherAndBatterFromSameTeam = pitcherTeamates.length > 1;
}
const uniqueTeams = occupiedRosters.map(f => f.player.team);
const hasAtLeastThreeTeams = _.uniq(uniqueTeams).length >= 3;
const groupedByTeam = _.mapValues(_.groupBy(occupiedRosters, e => e.player.team._members[0]), e => e.length);
const greaterThanFourPerTeam = _.values(groupedByTeam).filter(f => f > 4);
const hasLessThanFourPerTeam = greaterThanFourPerTeam.length == 0 ? true : false;
const emptyPositions = newLineup.roster.filter(f => f.player == null);
newLineup.isFull = emptyPositions.length == 0 ? true : false;
newLineup.isPossibleRoster = hasLessThanFourPerTeam ? true : false;
newLineup.isValidRoster = hasAtLeastThreeTeams && hasLessThanFourPerTeam && newLineup.isFull && !hasPitcherAndBatterFromSameTeam;
return newLineup;
}
public isValidRosterTree(searchParams : LineupSearch) : boolean {
const emptyPlayers = searchParams.currentLineup.roster.filter(f => f.player == null);
const lowestRemainingSalary = emptyPlayers
.map(f => this.salarySortedPlayers[f.position][0].salary)
.reduce((total, salary) => total + salary, 0);
const bestPossiblePlayerPoints = emptyPlayers
.map(f => {
const targetPlayer = _.find(this.bestFirstPlayers[f.position], ix => searchParams.currentLineup.remainingSalary);
return targetPlayer ? targetPlayer.fppg : 0;
})
.reduce((total, fppg) => total + fppg, 0);
if(lowestRemainingSalary > searchParams.currentLineup.remainingSalary){
return false;
}
if(this.validLineups.length){
if(bestPossiblePlayerPoints + searchParams.currentLineup.projectedFanduelPoints
< this.validLineups[0].projectedFanduelPoints){
return false;
}
}
return true;
}
public randomInt(min : number, max : number) : number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}