-
Notifications
You must be signed in to change notification settings - Fork 113
/
orm.py
534 lines (435 loc) · 13.1 KB
/
orm.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
from collections import Counter
from typing import List, Dict
import locale
from terminaltables import AsciiTable
from functools import total_ordering
import re
try:
locale.setlocale(locale.LC_ALL, "en_US")
except Exception:
pass
def cs(n):
return locale._format("%d", n, grouping=True)
@total_ordering
class Player(object):
def __init__(
self,
pos,
name,
cost,
proj=0,
projected_ownership_pct=0,
lineup_count=0,
average_score=0,
matchup=None,
team=None,
marked=None,
possible_positions=None,
lock=False,
position_lock=False,
ban=False,
multi_position=False,
kv_store={},
):
self.pos = pos
self.name = name
self.cost = float(cost)
self.team = team.upper() if team else team
self.matchup = matchup
self.proj = proj
self.average_score = average_score
self.projected_ownership_pct = projected_ownership_pct
self.lineup_count = lineup_count
self.marked = marked
self.lock = lock
self.position_lock = position_lock
self.ban = ban
self.position_ban = False
self.multi_position = multi_position
self.possible_positions = possible_positions
self.kv_store = kv_store
def get_player_id(self, player_map):
return player_map[self.name + " " + self.possible_positions]
def to_table_row(self):
return [
self.formatted_position,
self.name,
self.team,
self.matchup,
cs(self.cost),
self.proj,
self.__format_v_avg(),
"LOCK" if self.lock else "",
]
def to_exposure_table_row(self, n, s_min, s_max):
return [
self.formatted_position,
self.name,
self.team,
self.matchup,
cs(self.cost),
self.proj,
n,
s_min,
s_max,
]
def is_opposing_team_in_match_up(self, team):
return (team.upper() != self.team.upper()) and (
team.upper() in self.matchup.upper()
)
def __repr__(self):
v_avg = self.__format_v_avg()
player_dict = dict(
pos=self.formatted_position,
name=self.name,
team=self.team,
match=self.matchup,
cost=cs(self.cost),
proj=self.proj,
v_avg=v_avg,
lock="LOCK" if self.lock else "",
)
return (
"[{pos: <2}] {name: <20} {team} {match} "
"(${cost}, {proj} ({v_avg})), {lock}".format(**player_dict)
)
def __eq__(self, player):
return (
self.pos == player.pos
and self.name == player.name
and self.cost == player.cost
and self.team == player.team
)
def __hash__(self):
return hash((self.pos, self.name, self.cost, self.team))
def __lt__(self, player):
if self.cost == player.cost:
return self.name < player.name
return self.cost < player.cost
@property
def value(self):
return round(self.proj / (self.cost / 1000), 2)
@property
def solver_id(self):
"""
Used by optimizer to accommodate same player in different positions
"""
return f"{self.name} {self.pos} {self.team}"
@property
def roster_id(self):
"""
Used for roster equality. From the perspective of someone
entering lineups,a lineup with the same players scores
the same points irrespective of positions
"""
return f"{self.name} {self.team}"
@property
def formatted_position(self):
if self.multi_position:
return f"{self.possible_positions} ({self.pos})"
return self.pos
@property
def v_avg(self):
return self.proj - self.average_score
@property
def is_home(self):
match_up_teams = self.matchup.split(" ")[0]
return self.team == match_up_teams.split("@")[-1]
@property
def nba_general_position(self):
if self.pos == "SG" or self.pos == "PG" or self.pos == "G":
return "G"
elif self.pos == "SF" or self.pos == "PF" or self.pos == "F":
return "F"
return "C"
@property
def mlb_general_position(self):
if self.pos in {"SP", "RP"}:
return "P"
return self.pos
@property
def short_name(self):
s = self.name.split()
# DST
if len(s) == 1:
return self.name
# like "AJ McCarron"
if re.match(r"^[A-Z]{2}$", s[0]):
return s
return f"{s[0][0]}. {s[1]}"
def __set_from_data_cache(self, player_data):
if player_data is None:
return
for k, v in list(player_data.items()):
setattr(self, k, v)
def __format_v_avg(self):
if self.v_avg > 0:
return "\x1b[0;32;40m{:0.2f}\x1b[0m".format(self.v_avg)
return "\x1b[0;31;40m{:0.2f}\x1b[0m".format(self.v_avg)
class Roster:
def __init__(self):
self.players = []
self.cached_id = None
def __repr__(self):
table_data = []
headers = [
"Position",
"Player",
"Team",
"Matchup",
"Salary",
"Projection",
"vs. Avg.",
"Locked",
]
table_data.append(headers)
for p in self.sorted_players():
table_data.append(p.to_table_row())
table = AsciiTable(table_data)
table.justify_columns[4] = "right"
table.justify_columns[5] = "right"
table.justify_columns[6] = "right"
aggregate_info = "\n\nProjected Score: {:0.2f} \t Cost: ${}".format(
self.projected(), cs(self.spent())
)
return table.table + aggregate_info
@property
def identifier(self):
if self.cached_id:
return self.cached_id
self.cached_id = " ".join(
sorted([x.roster_id for x in self.sorted_players()])
)
return self.cached_id
def __eq__(self, other):
if not other:
return False
return self.identifier == other.identifier
def __hash__(self):
return hash(self.identifier)
def __contains__(self, player):
if isinstance(player, str):
for p in self.players:
if p.name == player or p.short_name == player:
return True
elif isinstance(player, Player):
return player in self.players
else:
raise NotImplementedError()
return False
def add_player(self, player):
self.players.append(player)
def spent(self):
return sum([x.cost for x in self.players])
def projected(self):
return sum([x.proj for x in self.players])
def position_order(self, player):
# raises exception in case someone tries to instantiate base class
position_order = getattr(self, "POSITION_ORDER")
# default sort order is low->high, so use the negative of cost
return position_order[player.pos], -player.cost
def sorted_players(self):
return sorted(self.players, key=lambda p: self.position_order(p))
def different_player_count(self, other_roster):
dpc = 0
other_roster_players: List[Player] = other_roster.players
for player in self.players:
if player not in other_roster_players:
dpc += 1
return dpc
def shared_player_count(self, other_roster):
spc = 0
other_roster_players: List[Player] = other_roster.players
for player in self.players:
if player in other_roster_players:
spc += 1
return spc
class RosterGroup:
"""
Group of rosters that might be entered into GPP or other contest.
The purpose of this class is to allow for easy retrieval of metrics
that would be helpful for thinking about multi-entry.
"""
def __init__(self, rosters: List[Roster]):
self.rosters = rosters
def get_player_frequency(self):
players = []
for r in self.rosters:
for p in r.players:
players.append(p)
counter = Counter(players)
return sorted(
counter.items(),
reverse=True,
key=lambda items: items[1],
)
def get_salary_frequency(self) -> List[Dict[int, int]]:
salaries = []
for r in self.rosters:
salaries.append(r.spent())
counter = Counter(salaries)
return sorted(counter.items(), key=lambda items: items[0])
def get_similarity_score(self):
"""
A similarity score of 1 means you're playing all of the same lineups.
0 would all be all different players in all different lineups.
0.5 would be say 3 lineups of 6 players with three of the same in each.
"""
scores, pairs = [], []
for idx, r in enumerate(self.rosters):
for idx_comp, r_comp in enumerate(self.rosters):
if idx == idx_comp or (sorted([idx_comp, idx]) in pairs):
# Do not compare to self or re-make comparison
continue
if r == r_comp:
scores.append(1)
else:
scores.append(self.__get_roster_similarity(r, r_comp))
pairs.append(sorted([idx_comp, idx]))
return sum(scores) / len(scores)
def __get_roster_similarity(self, roster_a, roster_b):
return roster_a.shared_player_count(roster_b) / len(roster_a.players)
"""
POSITION_ORDER is based on the order
required by DraftKings' CSV download
"""
class ShowdownRoster(Roster):
POSITION_ORDER = {
"CPT": 0,
"FLEX": 1,
}
@property
def identifier(self):
"""
In Showdown, only two positions exist
and a change in position means a change
in points, so unique is on position.
"""
if self.cached_id:
return self.cached_id
self.cached_id = " ".join(
sorted([x.solver_id for x in self.sorted_players()])
)
return self.cached_id
class MVPRoster(ShowdownRoster):
POSITION_ORDER = {
# TODO - adjust NFL FD to MVP format
# and remove CPT, FLEX
"CPT": 0,
"MVP": 0,
"FLEX": 1,
"STAR": 1,
"PRO": 2,
"UTIL": 3,
}
class NFLRoster(Roster):
POSITION_ORDER = {
"QB": 0,
"RB": 1,
"WR": 2,
"TE": 3,
"DST": 4,
"D": 5,
}
class TenRoster(Roster):
POSITION_ORDER = {
"P": 1,
}
class MLBRoster(Roster):
POSITION_ORDER = {
"P": 0,
"RP": 0,
"SP": 0,
"C": 1,
"1B": 2,
"2B": 3,
"3B": 4,
"SS": 5,
"OF": 6,
}
class NBARoster(Roster):
POSITION_ORDER = {"PG": 0, "SG": 1, "SF": 2, "PF": 3, "C": 4}
class WNBARoster(Roster):
POSITION_ORDER = {
"G": 0,
"F": 1,
"SG": 2,
"SF": 3,
"PF": 4,
}
class NASCARRoster(Roster):
POSITION_ORDER = {
"D": 0,
}
class PGARoster(Roster):
POSITION_ORDER = {
"G": 0,
}
class PGACaptainRoster(Roster):
POSITION_ORDER = {
"CPT": 0,
"G": 1,
}
class SoccerRoster(Roster):
POSITION_ORDER = {
"F": 0,
"M": 1,
"D": 2,
"GK": 3,
}
class ELRoster(Roster):
POSITION_ORDER = {
"G": 0,
"F": 1,
}
class NHLRoster(Roster):
POSITION_ORDER = {
"C": 0,
"W": 1,
"D": 2,
"G": 3,
}
class F1ShowdownRoster(Roster):
POSITION_ORDER = {
"CPT": 0,
"D": 1,
"CNSTR": 2,
}
class RosterSelect:
@staticmethod
def roster_gen(league):
roster_dict = {
"NBA": NBARoster(),
"NBA_SHOWDOWN": ShowdownRoster(),
"WNBA": WNBARoster(),
"NFL": NFLRoster(),
"NFL_SHOWDOWN": ShowdownRoster(),
"MLB_MVP": MVPRoster(),
"NBA_MVP": MVPRoster(),
"NFL_MVP": MVPRoster(),
"MLB": MLBRoster(),
"PGA": PGARoster(),
"PGA_CAPTAIN": PGACaptainRoster(),
"NASCAR": NASCARRoster(),
"SOCCER": SoccerRoster(),
"EL": ELRoster(),
"NHL": NHLRoster(),
"NHL_SHOWDOWN": ShowdownRoster(),
"MLB_SHOWDOWN": ShowdownRoster(),
# XFL uses the same positions as NFL
"XFL": NFLRoster(),
"TEN": TenRoster(),
"CSGO_SHOWDOWN": ShowdownRoster(),
"F1_SHOWDOWN": F1ShowdownRoster(),
}
return roster_dict[league]
class Game:
def __init__(self, team, opp):
self.team = team
self.opponent = opp
def __repr__(self):
return f"{self.team} @ {self.opponent}"
def team_in_game(self, team):
return team == self.team or team == self.opponent
def get_teams(self):
return self.team, self.opponent