-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_stats.py
330 lines (267 loc) · 11 KB
/
match_stats.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
import json
from datetime import datetime
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
from collections import defaultdict
""" with open('jungler_network_pros.json','r') as p:
data = json.load(p) """
with open("jungler_network_noobs.json", "r") as p:
data = json.load(p)
import requests
def get_champion_name_from_id(champion_id):
# URL dell'endpoint dell'API di Riot Games per ottenere i dettagli dei campioni
url = "https://ddragon.leagueoflegends.com/cdn/11.3.1/data/en_US/champion.json"
response = requests.get(url)
data = response.json()
# Ricerca del nome del campione usando l'ID
for champ, details in data["data"].items():
if details["key"] == str(champion_id):
return champ
return None
def timestamp_to_min(timestamp_in_secondi):
minuti = timestamp_in_secondi // 60
secondi = timestamp_in_secondi % 60
formato_mm_ss = "{:02d}:{:02d}".format(int(minuti), int(secondi))
return minuti
def timestamp_to_datetime(timestamp_ms):
timestamp_seconds = timestamp_ms / 1000
dt_object = datetime.fromtimestamp(timestamp_seconds)
formatted_time = dt_object.strftime("%Y-%m-%d %H:%M:%S")
return formatted_time
average_duration = 0
count_match = 0
timestamps_list = []
for player, player_data in data.items():
for match in player_data["OUT"]:
duration = match["info"]["gameDuration"]
timestamp = match["info"]["gameStartTimestamp"]
timestamps_list.append(timestamp / 1000)
average_duration += duration
count_match += 1
for match in player_data["IN"]:
duration = match["info"]["gameDuration"]
timestamp = match["info"]["gameStartTimestamp"]
timestamps_list.append(timestamp / 1000)
average_duration += duration
count_match += 1
average_duration = average_duration / count_match
# Crea una lista di oggetti datetime dai timestamp
datetimes_list = [datetime.fromtimestamp(timestamp) for timestamp in timestamps_list]
# Dizionari per contare il numero di picks e bans per ogni campione
champion_picks = defaultdict(int)
champion_bans = defaultdict(int)
matches = set()
# Scansione dei dati per contare picks e bans
for player, player_data in data.items():
for match_type in ["OUT", "IN"]:
for match in player_data.get(match_type, []):
# Estrazione dell'ID del match
matches.add(match["matchID"])
# Conteggio dei picks
champion_picks[match["champion"]] += 1
champion_picks[match["vsChampion"]] += 1
# Conteggio dei bans
for banned_champion in match["bans"]:
champion_bans[banned_champion] += 1
for banned_champion in match["vs_bans"]:
champion_bans[banned_champion] += 1
# Calcolo del totale delle partite
total_matches = len(matches)
# Calcolo dei pick rate e ban rate
pick_rates = {
champ: (count / total_matches) * 100 for champ, count in champion_picks.items()
}
ban_rates = {
champ: (count / total_matches) * 100 for champ, count in champion_bans.items()
}
# Calcolo della somma di pick rate e ban rate
combined_rates = {
champ: pick_rates.get(champ, 0) + ban_rates.get(champ, 0)
for champ in set(champion_picks) | set(champion_bans)
}
played_champions = set(champion_picks.keys())
# Filtraggio dei combined_rates per includere solo i campioni giocati
filtered_combined_rates = {
champ: rate
for champ, rate in combined_rates.items()
if get_champion_name_from_id(champ) in played_champions
}
# Ordinamento dei campioni in base alla somma di pick rate e ban rate tra i campioni giocati
sorted_filtered_combined_rates = sorted(
filtered_combined_rates.items(), key=lambda x: x[1], reverse=True
)
for elem_combined in sorted_filtered_combined_rates[:10]:
elem = elem_combined[0]
print(
get_champion_name_from_id(elem),
" -- ",
pick_rates[get_champion_name_from_id(elem)],
"--",
ban_rates[elem],
)
def aggiungi_campioni(dizionario, lista_campioni):
for campione in lista_campioni:
if campione in dizionario:
dizionario[campione] += 1
else:
dizionario[campione] = 1
return dizionario
def extract_most_banned_champions_from_json(file_path):
# Caricamento del file JSON
with open(file_path, "r") as f:
data = json.load(f)
# Estrazione degli attributi dei nodi
bans = {}
for player, player_data in data.items():
for match in player_data["OUT"]:
player_bans = match["bans"]
avversary_bans = match["vs_bans"]
match_bans = list(set(player_bans) | set(avversary_bans))
bans = aggiungi_campioni(bans, match_bans)
for match in player_data["IN"]:
player_bans = match["bans"]
avversary_bans = match["vs_bans"]
match_bans = player_bans + avversary_bans
bans = aggiungi_campioni(bans, match_bans)
# Ordinamento degli ID dei campioni in base al numero di volte in cui sono stati bannati
sorted_bans = sorted(bans.items(), key=lambda x: x[1], reverse=True)
return sorted_bans
# Uso della funzione
# file_path = "jungler_network_2.json"
file_path = "jungler_network_noobs.json"
most_banned_champions = extract_most_banned_champions_from_json(file_path)
def top_10_banned_champions(sorted_bans):
# Estrai i primi 10 campioni più bannati
top_10_bans = sorted_bans[:10]
result = []
for champion_id, bans in top_10_bans:
champion_name = get_champion_name_from_id(champion_id)
percentage = (bans / total_matches) * 100
result.append((champion_id, champion_name, bans, percentage, total_matches))
return result
top_10_champions = top_10_banned_champions(most_banned_champions)
for champion in top_10_champions:
print(
f"{champion[0]}: {champion[1]} -> {champion[2]} bans ({champion[3]:.2f}%) in {champion[4]} matches"
)
target = 107
target_bans = []
# Scansione dei dati per trovare quando e da chi è stato bannato Rengar
for player, player_data in data.items():
for match in player_data["OUT"]:
if target in match["bans"]:
target_bans.append(
{"player": player, "champion": match["champion"], "banned_by": "player"}
)
if target in match["vs_bans"]:
target_bans.append(
{
"player": player,
"champion": match["vsChampion"],
"banned_by": "opponent",
}
)
for match in player_data["IN"]:
if target in match["bans"]:
target_bans.append(
{"player": player, "champion": match["champion"], "banned_by": "player"}
)
if target in match["vs_bans"]:
target_bans.append(
{
"player": player,
"champion": match["vsChampion"],
"banned_by": "opponent",
}
)
target_bans_analysis_v4 = []
# Scansione dei dati per trovare quando e da chi è stato bannato Rengar
for player, player_data in data.items():
# Controllo l'esistenza del campo 'INFO' e successivamente dei campi 'tier' e 'rank'
player_info = player_data.get("INFO") or {}
player_rank = player_info.get("tier", "Unknown")
for match in player_data.get("OUT", []):
if target in match["bans"]:
target_bans_analysis_v4.append(
{
"player": player,
"champion": match["champion"],
"banned_by": "player",
"rank": player_rank,
}
)
if target in match["vs_bans"]:
target_bans_analysis_v4.append(
{
"player": player,
"champion": match["vsChampion"],
"banned_by": "opponent",
"rank": player_rank,
}
)
for match in player_data.get("IN", []):
if target in match["bans"]:
target_bans_analysis_v4.append(
{
"player": player,
"champion": match["champion"],
"banned_by": "player",
"rank": player_rank,
}
)
if target in match["vs_bans"]:
target_bans_analysis_v4.append(
{
"player": player,
"champion": match["vsChampion"],
"banned_by": "opponent",
"rank": player_rank,
}
)
target_bans_analysis_v4[:10]
# Creazione di un dizionario per contare l'occorrenza di ciascun campione nelle partite in cui Rengar è stato bannato
champion_correlation = defaultdict(int)
for entry in target_bans_analysis_v4:
champion_correlation[entry["champion"]] += 1
# Ordinamento dei campioni in base al numero di volte in cui sono apparsi
sorted_champions = sorted(
champion_correlation.items(), key=lambda x: x[1], reverse=True
)
print(sorted_champions[:10]) # Mostra i primi 10 campioni più frequenti
# Dizionari per contare il numero di picks e bans per ogni campione
champion_picks = defaultdict(int)
champion_bans = defaultdict(int)
matches = set()
# Scansione dei dati per contare picks e bans
for player, player_data in data.items():
for match_type in ["OUT", "IN"]:
for match in player_data.get(match_type, []):
# Estrazione dell'ID del match
match_id = list(match.keys())[0]
matches.add(match_id)
# Conteggio dei picks
champion_picks[match["champion"]] += 1
champion_picks[match["vsChampion"]] += 1
# Conteggio dei bans
for banned_champion in match["bans"]:
champion_bans[banned_champion] += 1
for banned_champion in match["vs_bans"]:
champion_bans[banned_champion] += 1
# Calcolo del totale delle partite
total_matches = len(matches)
# Calcolo dei pick rate e ban rate
pick_rates = {
champ: (count / total_matches) * 100 for champ, count in champion_picks.items()
}
ban_rates = {
champ: (count / total_matches) * 100 for champ, count in champion_bans.items()
}
# Calcolo della somma di pick rate e ban rate
combined_rates = {
champ: pick_rates.get(champ, 0) + ban_rates.get(champ, 0)
for champ in set(champion_picks) | set(champion_bans)
}
# Ordinamento dei campioni in base alla somma di pick rate e ban rate
sorted_combined_rates = sorted(pick_rates.items(), key=lambda x: x[1], reverse=True)
sorted_combined_rates = sorted(ban_rates.items(), key=lambda x: x[1], reverse=True)
sorted_combined_rates = sorted(combined_rates.items(), key=lambda x: x[1], reverse=True)