This repository has been archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoTFT.py
421 lines (420 loc) · 14.6 KB
/
AutoTFT.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from PyMacroV2 import *
import datetime
import re
from win32api import GetSystemMetrics
import pytesseract
import psutil
import win32process
import subprocess
import threading
import traceback
import sys
import time
#subprocess.Popen('start /min "" AutoTFT3.py', shell=True)
def echo_status():
global status
msg = "["+str(datetime.datetime.now())+"]"+status
print(msg)
open("AutoTFT.log", 'ab').write((msg+"\n").encode())
Delay(1)
def get_lolc_hwnd():
def callback(hwnd, ctx):
if win32gui.GetWindowText(hwnd) == "League of Legends":
rect = win32gui.GetWindowRect(hwnd)
x = rect[0]
y = rect[1]
w = rect[2] - x
h = rect[3] - y
if len(hwnds) == 0 and w in (160, 1280) and h in (28, 720):
hwnds.append(hwnd)
while True:
hwnds = []
win32gui.EnumWindows(callback, None)
try:
return hwnds[0]
except:
pass
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
x, y = (GetSystemMetrics(0), GetSystemMetrics(1))
lolc = "League of Legends"
lolgc = "League of Legends (TM) Client"
ziggs = "Woops! Something broke."
if WindowExists(lolc):
lolc_hwnd = get_lolc_hwnd()
win32gui.MoveWindow(lolc_hwnd, 0, 0, 1280, 720, True)
status = ""
# status = "queueing"
count = 1
limit = 60*22.5
if len(sys.argv) == 2:
if sys.argv[1] != "":
limit = int(sys.argv[1])
status = "ingame"
timer = 60*2
# timer = 60*15
unit_cost_rgb = ((39,57,71),(8,92,39),(15,96,117),(140,31,140),(110,96,64))
# limit = 1
while True:
try:
if status == "":
if WindowExists(lolgc):
status = "ingame"
elif WindowExists(lolc):
ShowWindowByHWND(lolc_hwnd)
Delay(1)
if FindImage("07_client_play_again.png", 0, 0, x, y, 0.95)[0] > 0:
status = "ff"
elif FindImage("01_client_find_match_button.bmp", 0, 0, x, y, 0.95)[0] > 0:
status = "pending"
else:
status = "queueing"
else:
count = 121
status = "queueing"
echo_status()
if count > timer:
if status == "queueing" or FindImage("03_game_reconnecting_message.bmp", 0, 0, x, y, 0.95):
subprocess.Popen('start /min "" [end_lolc].bat', shell=True)
if FindImage("03_game_reconnecting_message.bmp", 0, 0, x, y, 0.95):
subprocess.Popen('start /min "" [end_lolgc].bat', shell=True)
Delay(1)
if WindowExists("Garena - Game Center"):
SwitchToWindow("Garena - Game Center")
Delay(4)
ClickOnImage("19_stuck_1.png", 0, 0, x, y, 0.95)
Delay(2)
ClickOnImage("19_stuck_2.png", 0, 0, x, y, 0.95)
Delay(30*2)
lolc_hwnd = get_lolc_hwnd()
ShowWindowByHWND(lolc_hwnd)
Delay(2)
ClickOnImage("18_stuck_queueing_0.png", 0, 0, x, y, 0.95) # or start button
Delay(2)
ClickOnImage("18_stuck_queueing_1.png", 0, 0, x, y, 0.95)
Delay(2)
ClickOnImage("18_stuck_queueing_2.png", 0, 0, x, y, 0.95)
Delay(2)
ClickOnImage("18_stuck_queueing_3.png", 0, 0, x, y, 0.95)
Delay(2)
ClickOnImage("18_stuck_queueing_4.png", 0, 0, x, y, 0.95)
Delay(2)
msg = "["+str(datetime.datetime.now())+"]same status '"+status+"' stuck for "+str(count)+", resetting"
count = 0
status = ""
print(msg)
open("AutoTFT.log", 'ab').write((msg+"\n").encode())
continue
if WindowExists(ziggs):
SwitchToWindow(ziggs)
Delay(1)
ClickOnImage("12_bugsplat_dont_send.png", 0, 0, x, y, 0.95)
Delay(1)
if WindowExists(lolc):
ShowWindowByHWND(lolc_hwnd)
Delay(1)
if ClickOnImage("00_client_reconnect_button.bmp", 0, 0, x, y, 0.95):
status = "queueing"
Delay(1)
if ClickOnImage("08_client_skip_result.png", 0, 0, x, y, 0.95):
status = "pending"
count = 9999
Delay(1)
if ClickOnImage("10_client_confirm_mission.png", 0, 0, x, y, 0.95):
status = "ff"
Delay(5)
continue
if WindowExists(lolc) and re.search("^ff", status) != None:
if WindowExists(lolgc):
Delay(10)
if WindowExists(lolgc):
SwitchToWindow(lolgc)
Delay(1)
KeyPress(Key.enter)
Delay(1/3)
KeyPress(Key_('/'))
Delay(1/3)
KeyPress(Key_('f'))
Delay(1/3)
KeyPress(Key_('f'))
Delay(1/3)
KeyPress(Key.enter)
Delay(1)
ClickOnImage("13_ff.png", 0, 0, x, y, 0.95) or ClickOnImage("14_ff_ff.png", 0, 0, x, y, 0.95)
continue
ALT_TAB()
Delay(1)
lolc_hwnd = get_lolc_hwnd()
ShowWindowByHWND(lolc_hwnd)
# win32gui.MoveWindow(lolc_hwnd, int((x-1280)/2), int((y-720)/2), 1280, 720, True)
win32gui.MoveWindow(lolc_hwnd, 0, 0, 1280, 720, True)
Delay(1)
# SaveImage("[debug]client_play_again", 0, 0, x, y)
pos = FindImage("07_client_play_again.png", 0, 0, x, y, 0.95)
ok = True
waitsec = 5
while pos[0] == -1:
ALT_TAB()
Delay(1)
ShowWindowByHWND(lolc_hwnd)
# win32gui.MoveWindow(lolc_hwnd, int((x-1280)/2), int((y-720)/2), 1280, 720, True)
win32gui.MoveWindow(lolc_hwnd, 0, 0, 1280, 720, True)
Delay(waitsec)
waitsec += 1
pos = FindImage("07_client_play_again.png", 0, 0, x, y, 0.95)
# SaveImage("[debug]client_play_again", 0, 0, x, y)
count += 1
if count > timer:
ok = False
break
if not ok:
continue
opx, opy = FindImage("16_placement_bar.png", 0, 0, x, y, 0.95)
opx = opx - 6
px, py = FindImage("16_placement.png", opx, opy, opx+43, opy+409, 0.95)
# SaveImage("[debug]16_placement", opx, opy, opx+43, opy+409)
SaveImage("[debug]placement", opx+px+15, opy+py+6, opx+px+15+22, opy+py+6+22)
place = str(pytesseract.image_to_string(cv2.bitwise_not(cv2.imread("[debug]placement.png")),config='--psm 7 digit'))
# print(place)
# input()
status = "ended w/ "+place+"th place"
echo_status()
img = cv2.imread("07_client_play_again.png")
height, width, channels = img.shape
MouseLPress(pos[0]+width/2, pos[1]+height/2)
MoveMouse(0, 0)
status = "pending"
count = 0
echo_status()
subprocess.Popen('start /min "" [run].bat', shell=True)
sys.exit(0)
if not WindowExists(lolgc) and status == "ingame":
Delay(10)
if not WindowExists(lolgc) and status == "ingame":
status = ""
count = 0
continue
if WindowExists(lolgc) and status == "ingame":
SwitchToWindow(lolgc)
Delay(1)
r = False
units = [
["20_malphite.png", 0, 3],
["20_yasuo.png", 0, 3],
["20_janna.png", 0, 3],
["20_leona.png", 0, 9],
["20_karma.png", 0, 3],
["20_azir.png", 0, 9]
]
qiyana = ["20_qiyana_rock.png", 0, 3]
taliyah = ["20_taliyah.png", 0, 3]
qiyanas = [
"20_qiyana.png",
"20_qiyana_ocea.png",
"20_qiyana_fire.png"
]
item = (
"20_belt.png",
"20_bow.png",
"20_chest.png",
"20_cloak.png",
"20_glove.png",
"20_rod.png",
"20_spat.png",
"20_sword.png",
"20_tear.png"
)
is_wind = None
appended = False
r = True
starttime = int(time.time())
while True:
def timeleft():
m = (limit-(int(time.time())-starttime))//60
m = str(int(m)).zfill(2)
s = (limit-(int(time.time())-starttime))%60
s = str(int(s)).zfill(2)
return m+"m "+s+"s left "
if int(time.time()) - starttime > limit:
break
try:
if FindImage("14_leave.png", 0, 0 , x, y, 0.95)[0] != -1:
break
print("\r"+timeleft(), end='')
gx, gy = FindImage("20_gold.png", 0, 0, x, y, 0.95)
ogx, ogy = (11, 5)
SaveImage("[debug]gold", gx+ogx, gy-ogy, gx+ogx+40, gy-ogy+17)
#try:
# cg = int(str(pytesseract.image_to_string(cv2.bitwise_not(cv2.imread("[debug]gold.png")),config='--psm 7 digit')))
#except:
# continue
if int(time.time()) - starttime < 60*9:
continue
if True:
if FindImage("20_gold.png", 0, 0, x, y, 0.95)[0] != -1:
for i in range(0, len(item)):
ix, iy = FindImage(item[i], 0, 0, x, 820, 0.97)
if ix == -1 or iy >= 820 or iy <= 404:
continue
else:
print(f'''{item[i].split("_")[1].split(".")[0]}@({ix},{iy}),''',end="",flush=True)
MoveMouse(ix+32, iy+32)
Delay(0.5)
MouseLDown()
Delay(0.5)
MoveMouse(x/2, 900)
Delay(0.5)
MouseLUp()
else:
continue
gx, gy = FindImage("20_gold.png", 0, 0, x, y, 0.95)
#if gx != -1:
# ogx, ogy = (11, 5)
# SaveImage("[debug]gold", gx+ogx, gy-ogy, gx+ogx+40, gy-ogy+17)
# try:
# pg = int(str(pytesseract.image_to_string(cv2.bitwise_not(cv2.imread("[debug]gold.png")),config='--psm 7 digit')))
# print(f"pg{pg},", end="", flush=True)
# except:
# continue
if not appended:
if is_wind is None:
if FindImage(qiyana[0], 0, 0, x, y, 0.97)[0] != -1:
is_wind = True
if is_wind is None:
for yy in range(0, len(qiyanas)):
if FindImage(qiyanas[yy], 0, 0, x, y, 0.97)[0] != -1:
is_wind = False
break
if is_wind == True:
units.append(qiyana)
appended = True
elif is_wind == False:
units.append(taliyah)
appended = True
print(f"(is_rock,{is_wind}),",end="",flush=True)
for zz in range(0, len(units)):
if units[zz][1] < units[zz][2]:
if ClickOnImage(units[zz][0], 0, 0, x, y, 0.95):
print(f'''{units[zz][0].split("_")[1].split(".")[0]},''',end="",flush=True)
units[zz][1] += 1
r = False
gx, gy = FindImage("20_gold.png", 0, 0, x, y, 0.95)
if gx != -1:
ogx, ogy = (11, 5)
SaveImage("[debug]gold", gx+ogx, gy-ogy, gx+ogx+40, gy-ogy+17)
try:
cg = int(str(pytesseract.image_to_string(cv2.bitwise_not(cv2.imread("[debug]gold.png")),config='--psm 7 digit')))
print(f"cg{cg},", end="",flush=True)
except:
continue
try:
ohpx, ohpy = (0, 0)
hpx, hpy = FindImage("15_hp_corner.png", ohpx, ohpy, x, y, 0.95)
SaveImage("[debug]hp", ohpx+hpx+11, ohpy+hpy+3, ohpx+hpx+11+25, ohpy+hpy+3+20)
hp = int(str(pytesseract.image_to_string(cv2.bitwise_not(cv2.imread("[debug]hp.png")),config='--psm 7 digit')))
print(f"hp{hp},", end="",flush=True)
except:
continue
if hp < 35:
if not r:
ClickOnImage("20_f.png", 0, 0, x, y, 0.95)
Delay(0.5)
MoveMouse(x/2, y/2)
ClickOnImage("20_r.png", 0, 0, x, y, 0.95)
Delay(0.5)
MoveMouse(x/2, y/2)
else:
if cg >= 50:
if not r:
ClickOnImage("20_f.png", 0, 0, x, y, 0.95)
else:
ClickOnImage("20_r.png", 0, 0, x, y, 0.95)
Delay(0.5)
MoveMouse(x/2, y/2)
if r:
print("r",flush=True)
else:
print("f",flush=True)
r = True
except KeyboardInterrupt:
break
print("\r",end='')
# SwitchToWindow(lolgc)
# Delay(1)
#0 672
#11 675 25 20
ohpx, ohpy = (0, 0)
hpx, hpy = FindImage("15_hp_corner.png", ohpx, ohpy, x, y, 0.95)
SaveImage("[debug]hp", ohpx+hpx+11, ohpy+hpy+3, ohpx+hpx+11+25, ohpy+hpy+3+20)
hp = str(pytesseract.image_to_string(cv2.bitwise_not(cv2.imread("[debug]hp.png")),config='--psm 7 digit'))
# print(hpx, hpy, hp)
# input()
Delay(1)
ok = False
while not ok:
ShowWindowByHWND(lolc_hwnd)
SwitchToWindow(lolgc)
KeyPress(Key.enter)
Delay(1/3)
KeyPress(Key_('/'))
Delay(1/3)
KeyPress(Key_('f'))
Delay(1/3)
KeyPress(Key_('f'))
Delay(1/3)
KeyPress(Key.enter)
Delay(1)
if ClickOnImage("13_ff.png", 0, 0, x, y, 0.95) or ClickOnImage("14_ff_ff.png", 0, 0, x, y, 0.95):
ok = True
else:
ok = False
if not WindowExists(lolgc):
count = 9999
break
Delay(1)
if not ok:
continue
status = "ff w/ "+hp+" hp"
count = 0
echo_status()
Delay(8)
if WindowExists(lolgc) and status == "queueing":
Delay(10)
SwitchToWindow(lolgc)
Delay(1)
if FindImage("09_rito.png", 0, 0, x, y, 0.95)[0] == -1:
count += 1
continue
status = "ingame"
count = 0
echo_status()
if WindowExists(lolc) and status == "queueing":
ShowWindowByHWND(lolc_hwnd)
Delay(1)
SaveImage("[debug]client_accept_match_button", 0, 0, x, y)
if not ClickOnImage("02_client_accept_match_button.bmp", 0, 0, x, y, 0.95):
count += 1
if FindImage("18_stuck_queueing_0.png", 0, 0, x, y, 0.95)[0] != -1:
count = 9999
continue
MoveMouse(0, 0)
count = 0
status = "accepted"
echo_status()
status = "queueing"
echo_status()
if WindowExists(lolc) and status == "pending":
ShowWindowByHWND(lolc_hwnd)
Delay(1)
SaveImage("[debug]client_find_match_button", 0, 0, x, y)
if not ClickOnImage("01_client_find_match_button.bmp", 0, 0, x, y, 0.95):
count += 1
continue
MoveMouse(0, 0)
status = "queueing"
count = 0
echo_status()
Delay(1)
except (KeyboardInterrupt, SystemExit):
exit()
except:
traceback.print_exc()