-
Notifications
You must be signed in to change notification settings - Fork 0
/
getData.py
289 lines (263 loc) · 11.8 KB
/
getData.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
import json
import urllib2
"""
Static Data Handling
"""
itemData = {}
champData = {}
"""
Data Storage Initialization
"""
champDict = {}
overallData = {}
overallItemPickData = {}
def displayChampDict():
for champ in champDict:
for index in range(8):
for item in champDict[champ][index]:
for stat in champDict[champ][index][item]:
if champDict[champ][index][item][stat] > 0:
print str(champ) +" " + str(index) + " " + str(item) + " " + stat + " " + str(champDict[champ][index][item][stat])
"""
Method for single call handling
"""
import time
def analyze(matchId):
"""
Given a matchId, add all important data to the running overall data global variables.
"""
while True:
try:
handler = urllib2.urlopen("https://na.api.pvp.net/api/lol/na/v2.2/match/"+str(matchId)+"?includeTimeline=true&api_key="+api_key)
break
except:
print "requests overloaded, waiting"
time.sleep(1)
gameData = json.loads(handler.read())
"""
Handle Post-Match Data:
participants
participantId
championId
teamId
stats
item[0-5]
teams (array)
teamId
winner
"""
winners = []
people = {}
winteam = -1
global overallData
global champDict
for tem in gameData['teams']:
if tem['winner']:
winteam = tem['teamId']
for person in gameData['participants']:
if str(person['teamId']) == str(winteam):
winners.append(person['participantId'])
people[person['participantId']] = person['championId']
itemsInGame = {}
for person in gameData['participants']:
add = 'W' if person['participantId'] in winners else 'L'
overallData[str(person['championId'])][add] += 1
for i in range(7):
if 'item'+str(i) in person['stats']:
try:
champDict[str(person['championId'])][7][str(person['stats']['item'+str(i)])][add] += 1
itemsInGame[person['stats']['item'+str(i)]] = 1
except KeyError:
continue
for key in itemsInGame:
overallItemPickData[str(key)] += 1
"""
Handle Timeline Data:
timeline
frameInterval
frames (array)
timestamp
participantFrames
[events] (array)
timestamp (ms)
eventType (ITEM_PURCHASED)
itemId
participantId
"""
def isBoots(intid):
"""
Originally for ignoring boots as an ending item, but expanded to include several other items as well (doran's)
"""
boots = ['3117', '3009', '3158', '3020', '3006', '3047', '3111', '2045', '3092', '3401', '3069']
return str(intid) in boots
data = {}
for num in people:
data[num] = {'itemCount': 0, 'lastTime': 0, 'lastGold': 0, 'counting': 0, 'lastId': 0}
for frame in gameData['timeline']['frames']:
timestamp = frame['timestamp']
peopleInfo = frame['participantFrames']
if 'events' in frame:
for event in frame['events']:
if event['eventType'] == 'ITEM_PURCHASED':
add = 'W' if event['participantId'] in winners else 'L'
# GPM data between buys
if data[event['participantId']]['counting'] and not event['itemId'] == data[event['participantId']]['lastId']:
champDict[str(people[event['participantId']])][data[event['participantId']]['itemCount']-1][str(data[event['participantId']]['lastId'])]['TE'] += timestamp - data[event['participantId']]['lastTime']
champDict[str(people[event['participantId']])][data[event['participantId']]['itemCount']-1][str(data[event['participantId']]['lastId'])]['GP'] += peopleInfo[str(event['participantId'])]['totalGold'] - data[event['participantId']]['lastGold']
data[event['participantId']]['counting'] = 0
# Item Buy Updates
if (isBoots(event['itemId']) or int(itemData['data'][str(event['itemId'])]['gold']['total']) > 2000) and data[event['participantId']]['itemCount'] < 6 and not event['itemId'] == data[event['participantId']]['lastId']:
champDict[str(people[event['participantId']])][data[event['participantId']]['itemCount']][str(event['itemId'])][add] += 1
champDict[str(people[event['participantId']])][data[event['participantId']]['itemCount']][str(event['itemId'])]['T'] += event['timestamp']
data[event['participantId']]['itemCount'] += 1
data[event['participantId']]['counting'] = 1
data[event['participantId']]['lastId'] = event['itemId']
data[event['participantId']]['lastTime'] = timestamp
data[event['participantId']]['lastGold'] = peopleInfo[str(event['participantId'])]['totalGold']
# Last Frame Check for items whose data have not yet been recorded
for i in range(1, 11):
timestamp = frame['timestamp']
peopleInfo = frame['participantFrames']
add = 'W' if i in winners else 'L'
# GPM data between buys
if data[i]['counting']:
champDict[str(people[i])][data[i]['itemCount']-1][str(data[i]['lastId'])]['TE'] += timestamp - data[i]['lastTime']
champDict[str(people[i])][data[i]['itemCount']-1][str(data[i]['lastId'])]['GP'] += peopleInfo[str(i)]['totalGold'] - data[i]['lastGold']
data[i]['counting'] = 0
def extractStatistics():
"""
Given dictionary of data collected from all matches, compile actual useful statistics into various categories.
"""
ret = {}
# General W/L Data for items POST game (so index 7)
## Overall W/L for certain champions
ret['overallChampionData'] = overallData
## Overall W/L/T/GP/D/TE for certain items
ret['overallItemData'] = {}
for item in itemData['data']:
ret['overallItemData'][item] = {}
for i in range(8):
ret['overallItemData'][item][str(i)] = {'W':0, 'L':0, 'T':0, 'GP':0, 'D':0, 'TE':0}
for champ in champDict:
for item in itemData['data']:
for i in range(8):
ret['overallItemData'][item][str(i)]['W'] += champDict[champ][i][item].get('W',0)
ret['overallItemData'][item][str(i)]['T'] += champDict[champ][i][item].get('T',0)
ret['overallItemData'][item][str(i)]['L'] += champDict[champ][i][item].get('L',0)
ret['overallItemData'][item][str(i)]['GP'] += champDict[champ][i][item].get('GP',0)
ret['overallItemData'][item][str(i)]['D'] += champDict[champ][i][item].get('D',0)
ret['overallItemData'][item][str(i)]['TE'] += champDict[champ][i][item].get('TE',0)
## W/L+GPM for certain champion given that they purchased a item at a certain timeline
ret['championItemOrderWR'] = {}
for champ in champDict:
ret['championItemOrderWR'][champ] = {}
for item in itemData['data']:
ret['championItemOrderWR'][champ][item] = {}
for i in range(8):
ret['championItemOrderWR'][champ][item][str(i)] = champDict[champ][i][item]
## Average Time of Item Purchase Overall
ret['itemTime'] = {}
itemCount = {}
for champ in champDict:
for item in itemData['data']:
for i in range(6):
ret['itemTime'][item] = ret['itemTime'].get(item,0) + champDict[champ][i][item].get('T',0)
itemCount[item] = itemCount.get(item, 0) + champDict[champ][i][item].get('W',0)
itemCount[item] = itemCount[item] + champDict[champ][i][item].get('L',0)
for item in itemData['data']:
if itemCount[item] > 0:
ret['itemTime'][item] /= itemCount[item]
## Average Time of Item Purchase given champion
ret['itemTimeChampion'] = {}
itemCountChamp = {}
for champ in champDict:
ret['itemTimeChampion'][champ] = {}
itemCountChamp[champ] = 0
for item in itemData['data']:
for i in range(6):
ret['itemTimeChampion'][champ][item] = ret['itemTimeChampion'][champ].get(item, 0) + champDict[champ][i][item].get('T',0)
itemCountChamp[item] = itemCount.get(item,0)+ champDict[champ][i][item].get('W',0)
itemCountChamp[item] += champDict[champ][i][item].get('L',0)
## Pick Rate of champions
totalGames = 0
for key in overallData:
totalGames += overallData[key]['W']+overallData[key]['L']
totalGames /= 10
ret['championPickRate'] = {}
for key in overallData:
ret['championPickRate'][key] = (overallData[key]['W']+overallData[key]['L']) / float(totalGames)
## Pick Rate of items
ret['itemPickRate'] = {}
for key in overallItemPickData:
ret['itemPickRate'][key] = overallItemPickData[key] / float(totalGames)
## Average number of item bought per game
ret['avgItemPerGame'] = {}
for item in itemData['data']:
ret['avgItemPerGame'][item] = 0
for champ in champDict:
for item in itemData['data']:
ret['avgItemPerGame'][item] += champDict[champ][7][item]['W'] + champDict[champ][7][item]['L']
for item in itemData['data']:
if ret['avgItemPerGame'][item] > 0:
ret['avgItemPerGame'][item] /= float(overallItemPickData[item])
## Pick Rate of items given champion
ret['itemPickRateChampion'] = {}
for champ in champDict:
ret['itemPickRateChampion'][champ] = {}
for item in itemData['data']:
if champDict[champ][7][item]['W']+champDict[champ][7][item]['L'] > 0:
ret['itemPickRateChampion'][champ][item] = (champDict[champ][7][item]['W']+champDict[champ][7][item]['L'])/float(overallData[champ]['W']+overallData[champ]['L'])
return ret
def initialize(dir):
"""
Initialize global static item data.
dir specifies whether to pull from 5.14 or 5.11 (item ids changed)
"""
global itemData
global champData
if dir:
handler = urllib2.urlopen("https://global.api.pvp.net/api/lol/static-data/na/v1.2/item?version=5.14.1&itemListData=gold&api_key="+api_key)
itemData = json.loads(handler.read())
else:
handler = urllib2.urlopen("https://global.api.pvp.net/api/lol/static-data/na/v1.2/item?version=5.11.1&itemListData=gold&api_key="+api_key)
itemData = json.loads(handler.read())
handler = urllib2.urlopen("https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion?dataById=true&api_key="+api_key)
champData = json.loads(handler.read())
global champDict
global overallData
global overallItemPickData
champDict = {}
overallData = {}
overallItemPickData = {}
for key in itemData['data']:
overallItemPickData[key] = 0
for key in champData['data']:
tempList = []
for i in range(8):
tempDict = {}
for key1 in itemData['data']:
tempDict[key1.rstrip().strip()] = {'W':0, 'L':0, 'T':0, 'GP':0, 'D':0, 'TE':0}
tempList.append(tempDict)
overallData[key.rstrip().strip()] = {'W':0, 'L':0}
champDict[key.rstrip().strip()] = tempList
def doAll():
f1 = open('NA-5.11.json')
games511 = json.loads(f1.read())
initialize(False)
for i in games511:
print "511 ",i
analyze(i)
json511 = extractStatistics()
f3 = open('NA-5.11Analysis.json', 'w')
f3.write(json.dumps(json511))
f2 = open('NA-5.14.json')
games514 = json.loads(f2.read())
initialize(True)
for i in games514:
print "514 ",i
analyze(i)
json514 = extractStatistics()
f4 = open('NA-5.14Analysis.json', 'w')
f4.write(json.dumps(json514))
api_key = str(raw_input('Insert your api key:'))
doAll()