-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
134 lines (98 loc) · 3.54 KB
/
util.py
File metadata and controls
134 lines (98 loc) · 3.54 KB
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
from __future__ import print_function
import requests
N = 113 # number of heroes
PICK_BAN_ORDER = [(False, 0), # where the picker is on team 0
(False, 1),
(False, 0),
(False, 1),
(True, 0),
(True, 1),
(True, 1),
(True, 0),
(False, 1),
(False, 0),
(False, 1),
(False, 0),
(True, 1),
(True, 0),
(True, 1),
(True, 0),
(False, 1),
(False, 0),
(True, 1),
(True, 0)]
def readCMGamesFromJSON(filename):
return [game for game in json.load(open(filename, "r")) if len(game["picks_bans"]) == 20]
class APIHero:
def __init__(self, json):
self.json = json
def getID(self):
return self.json["id"]
def getName(self):
return self.json["localized_name"]
def getIconURL(self):
return "/static/draft/images/hero_icons/" + self.json["name"].replace("npc_dota_hero_", "") + ".png"
@classmethod
def byID(cls, id):
return cls.heroes[id]
@classmethod
def byName(cls, name):
return cls.heroByName[cls.getPlainName(name)]
@staticmethod
def getPlainName(name):
# TODO use regex here
return name.lower().replace(" ", "").replace("_", "").replace("-", "")
print("downloading hero data..")
APIHero.heroes = requests.get("https://api.opendota.com/api/heroes").json()
# maps from apiID -> localID
_idMap = {APIHero.heroes[i]["id"]: i for i in range(len(APIHero.heroes))}
_idMapInv = {i: APIHero.heroes[i]["id"] for i in range(len(APIHero.heroes))}
def getShiftedID(apiID):
return _idMap[apiID]
def getUnshiftedID(localID):
return _idMapInv[localID]
for hero in APIHero.heroes:
hero["id"] = getShiftedID(hero["id"])
APIHero.heroes = [APIHero(hero) for hero in APIHero.heroes]
APIHero.heroByName = {APIHero.getPlainName(hero.getName()): hero for hero in APIHero.heroes}
def getVectorForSet(heroSet):
return [1 if hero in heroSet else 0 for hero in APIHero.heroes]
class Team:
MAX_PICKS = 5
def __init__(self):
# sets holding hero objects
self.picks = []
self.bans = []
# vectors with v_i = 1 iff hero i is picked/banned
self.pickVector = [0] * N
self.banVector = [0] * N
def pick(self, hero):
if self.isFull(): return False
self.picks.append(hero)
self.pickVector[hero.getID()] = 1
return True
def ban(self, hero):
self.bans.append(hero)
self.banVector[hero.getID()] = 1
def isPicked(self, hero):
return self.pickVector[hero.getID()] == 1
def isBanned(self, hero):
return self.banVector[hero.getID()] == 1
def __contains__(self, hero):
return self.isPicked(hero) or self.isBanned(hero)
def isFull(self):
return len(self.picks) == Team.MAX_PICKS
# returns a list of boolean flags for each hero
def getNotAllowed(self):
return [True if h in self else False for h in APIHero.heroes]
def getContextVector(self):
return self.pickVector + self.banVector
@staticmethod
def getContextVectorFor(team0, team1, pickBit):
team0.getContextVector() + team1.getContextVector() + [pickBit]
@staticmethod
def fromJSON(json):
t = Team()
for pick in json["picks"]: t.pick(APIHero.byID(pick))
for ban in json["bans"]: t.ban(APIHero.byID(ban))
return t