forked from zxcvqwerasdf/TFT-OCR-BOT
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauto_queue.py
216 lines (190 loc) · 6.37 KB
/
auto_queue.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
"""
Handles getting into a game
"""
from time import sleep
import json
from requests.auth import HTTPBasicAuth
import requests
import urllib3
from retrying import retry
import settings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def retry_if_connection_error_or_timeout(exception):
"""Determine if a retry should be attempted based on the given exception."""
return isinstance(
exception,
(requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError),
)
@retry(
retry_on_exception=retry_if_connection_error_or_timeout,
wait_fixed=2000,
stop_max_delay=20000,
)
def create_lobby(client_info: tuple) -> bool:
"""Creates a lobby"""
payload: dict[str, int] = {"queueId": 1090} # Ranked TFT is 1100
payload: dict[str, int] = json.dumps(payload)
try:
status = requests.post(
f"{client_info[1]}/lol-lobby/v2/lobby/",
payload,
auth=HTTPBasicAuth("riot", client_info[0]),
timeout=20,
verify=False,
)
if status.status_code == 200:
print(" Creating lobby")
return True
return False
except (requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError):
return False
@retry(
retry_on_exception=retry_if_connection_error_or_timeout,
wait_fixed=2000,
stop_max_delay=20000,
)
def start_queue(client_info: tuple) -> bool:
"""Starts queue"""
try:
status = requests.post(
f"{client_info[1]}/lol-lobby/v2/lobby/matchmaking/search",
auth=HTTPBasicAuth("riot", client_info[0]),
timeout=20,
verify=False,
)
if status.status_code == 204:
print(" Starting queue")
return True
return False
except (requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError):
return False
@retry(
retry_on_exception=retry_if_connection_error_or_timeout,
wait_fixed=2000,
stop_max_delay=20000,
)
def check_queue(client_info: tuple) -> bool:
"""Checks queue to see if we are searching"""
try:
status = requests.get(
f"{client_info[1]}/lol-lobby/v2/lobby/matchmaking/search-state",
auth=HTTPBasicAuth("riot", client_info[0]),
timeout=20,
verify=False,
)
return status.json().get("searchState") == "Searching"
except (requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError):
return False
@retry(
retry_on_exception=retry_if_connection_error_or_timeout,
wait_fixed=2000,
stop_max_delay=20000,
)
def check_game_status(client_info: tuple) -> bool:
"""Checks to see if we are in a game"""
try:
status = requests.get(
f"{client_info[1]}/lol-gameflow/v1/session",
auth=HTTPBasicAuth("riot", client_info[0]),
timeout=20,
verify=False,
)
return status.json().get("phase", "None")
except (requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError):
return False
@retry(
retry_on_exception=retry_if_connection_error_or_timeout,
wait_fixed=2000,
stop_max_delay=20000,
)
def accept_queue(client_info: tuple) -> bool:
"""Accepts the queue"""
try:
requests.post(
f"{client_info[1]}/lol-matchmaking/v1/ready-check/accept",
auth=HTTPBasicAuth("riot", client_info[0]),
timeout=20,
verify=False,
)
return True
except (requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError):
return False
@retry(
retry_on_exception=retry_if_connection_error_or_timeout,
wait_fixed=2000,
stop_max_delay=20000,
)
def change_arena_skin(client_info: tuple) -> bool:
"""Changes arena skin to default, other arena skins have different coordinates"""
try:
status = requests.delete(
f"{client_info[1]}/lol-cosmetics/v1/selection/tft-map-skin",
auth=HTTPBasicAuth("riot", client_info[0]),
timeout=20,
verify=False,
)
if status.status_code == 204:
print(" Changed arena skin to default")
return True
return False
except (requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError):
return False
def get_client() -> tuple:
"""Gets data about the client such as port and auth token"""
print("\n\n[Auto Queue]")
file_path = settings.LEAGUE_CLIENT_PATH + "\\lockfile"
got_lock_file = False
while not got_lock_file:
try:
with open(file_path, "r", encoding="utf-8") as data:
data: list[str] = data.read().split(":")
app_port: str = data[2]
remoting_auth_token: str = data[3]
server_url: str = f"https://127.0.0.1:{app_port}"
got_lock_file = True
except IOError:
print("Client not open! Trying again in 10 seconds.")
sleep(10)
print(" Client found")
return remoting_auth_token, server_url
@retry(
retry_on_exception=retry_if_connection_error_or_timeout,
wait_fixed=2000,
stop_max_delay=20000,
)
def reconnect(client_info: tuple) -> None:
"""Reconnect to game when "Failed to Connect" windows are found"""
try:
requests.post(
f"{client_info[1]}/lol-gameflow/v1/reconnect",
auth=HTTPBasicAuth("riot", client_info[0]),
timeout=20,
verify=False,
)
return True
except (requests.exceptions.ConnectionError, urllib3.exceptions.ReadTimeoutError):
return False
def handle_queue() -> None:
"""Handles getting into a game"""
client_info: tuple = get_client()
while check_game_status(client_info) == "InProgress":
sleep(2)
if check_game_status(client_info) == "Reconnect":
print(" Reconnecting")
reconnect(client_info)
return
while not create_lobby(client_info):
sleep(3)
change_arena_skin(client_info)
sleep(3)
while state := check_game_status(client_info):
if state == "None":
create_lobby(client_info)
if state == "Lobby":
start_queue(client_info)
if state == "ReadyCheck":
accept_queue(client_info)
print(" Accepting")
if state == "InProgress":
return
sleep(3)