-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
135 lines (114 loc) · 4.86 KB
/
project.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
import requests
from dotenv import load_dotenv
import os
# import json
load_dotenv()
riot_api_key = os.environ.get("riot_api_key")
def main():
while True:
try:
region_server, game_name, tag_line, server = get_username_from_user()
if region_server == "end": # Check if the user wants to quit
print("Exiting the program...")
break # Exit the loop and end the program
puuid = get_puuid(region_server, game_name, tag_line)
matches = get_matches(server, puuid)
# count win and lose
win_count = 0
lose_count = 0
for match_id in matches:
info = get_match_info(server, match_id)
if info is not None:
stats = game_stats(info, puuid)
win, lose = win_rate_checker(stats)
win_count += win # Update win count
lose_count += lose # Update lose count
if win_count + lose_count == 50: # Check if we check 50 matches
break # exit the loop
#calculate the winrate
total_matches = win_count + lose_count
win_rate = (win_count / total_matches) * 100 if total_matches > 0 else 0
print()
print(f"WINRATE: {win_rate:.2f}%")
print()
except Exception as e:
print("An error occurred:", str(e))
def get_username_from_user():
print('type "end" in username if you want to quit' )
game_name = input("Enter your username: ").replace(" ", "_")
if game_name.lower() == "end": # Check if the user wants to quit
return "end", None, None, None
tag_line = input("Enter your tagline: ")
print("Servers are NA, BR, LAN, LAS, KR, JP, EUNE, EUW, TR, RU, OCE, PH2, SG2, TH2, TW2, VN2")
server = input("Enter Server: ").upper()
region_server = None
if server in ("NA", "BR", "LAN", "LAS"):
region_server = "americas"
elif server in ("KR", "JP", "PH2", "OCE", "SG2", "TH2", "TW2", "VN2"):
region_server = "asia"
elif server in ("EUNE", "EUW", "TR", "RU"):
region_server = "europe"
else:
print("Invalid server. Please try again.")
return get_username_from_user() # Restart the function if the server is invalid
if not game_name or not tag_line:
print("Username and tagline cannot be empty. Please try again.")
return get_username_from_user() # Restart the function if username or tagline is empty
return region_server, game_name, tag_line, server
def get_puuid(server, game_name, tag_line):
url = f"https://{server}.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{game_name}/{tag_line}?api_key={riot_api_key}"
response = requests.get(url)
if response.status_code == 404:
raise ValueError("Player not found. Please enter a valid player")
response.raise_for_status() # Raise exception for HTTP errors
return response.json()["puuid"]
def get_matches(server, puuid):
if server in ("NA", "BR", "LAN", "LAS"):
new_server = "americas"
elif server in ("KR", "JP"):
new_server = "asia"
elif server in ("EUNE", "EUW", "TR", "RU"):
new_server = "europe"
elif server in ("PH2", "OCE", "SG2", "TH2", "TW2", "VN2"):
new_server = "sea"
url = f"https://{new_server}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids?queue=450&start=0&count=100&api_key={riot_api_key}"
response = requests.get(url)
response.raise_for_status() # Raise exception for HTTP errors
return response.json()
def get_match_info(server, match_id):
if server in ("NA", "BR", "LAN", "LAS"):
new_server = "americas"
elif server in ("KR", "JP"):
new_server = "asia"
elif server in ("EUNE", "EUW", "TR", "RU"):
new_server = "europe"
elif server in ("PH2", "OCE", "SG2", "TH2", "TW2", "VN2"):
new_server = "sea"
url = f"https://{new_server}.api.riotgames.com/lol/match/v5/matches/{match_id}?api_key={riot_api_key}"
response = requests.get(url)
response.raise_for_status() # Raise exception for HTTP errors
match_data = response.json()
return match_data # json.dumps(match_data, indent = 4)
def game_stats(info, puuid):
match_info = info["info"]["participants"]
# Search for the player with the puuid
player_index = None
for i, participant in enumerate(match_info):
if participant["puuid"] == puuid:
player_index = i
break
if player_index is None:
return None # Player not found in the match
# Get the win status
win = match_info[player_index]["win"]
return win
def win_rate_checker(stats):
win = 0
lose = 0
if stats == True:
win += 1
else:
lose += 1
return win, lose
if __name__ == "__main__":
main()