-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.py
675 lines (511 loc) · 18.9 KB
/
index.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# -*- coding: utf-8 - Lizier -*-
from ast import Return
from pickle import TRUE
from cv2 import cv2
from captcha.solveCaptcha import solveCaptcha
from os import listdir
from src.logger import logger, loggerMapClicked
from random import randint
from random import random
import numpy as np
import mss
import pyautogui
import time
import sys
import yaml
import math
msg = """
_
>>---> Bot começou a rodar!
>>---> Acesse: https://github.com/sCryptoHelp/spacecrypto-bot
>>---> Pressione ctrl + c para parar o bot.
"""
print(msg)
time.sleep(3)
if __name__ == '__main__':
stream = open("config.yaml", 'r')
c = yaml.safe_load(stream)
ct = c['threshold']
ch = c['home']
pause = c['time_intervals']['interval_between_moviments']
pyautogui.PAUSE = pause
pyautogui.FAILSAFE = False
hero_clicks = 0
login_attempts = 0
last_log_is_progress = False
count_victory = 0
time_start_bot = time.time()
count_reloadSpacheship = 0
count_nexList = 1
bot_working = False
last = {
"lessPosition":[],
"CheckInitialPage":0,
"CheckInicialCube":0,
"CheckBotWork":0,
}
def addRandomness(n, randomn_factor_size=None):
if randomn_factor_size is None:
randomness_percentage = 0.1
randomn_factor_size = randomness_percentage * n
random_factor = 2 * random() * randomn_factor_size
if random_factor > 5:
random_factor = 5
without_average_random_factor = n - randomn_factor_size
randomized_n = int(without_average_random_factor + random_factor)
return int(randomized_n)
def moveToWithRandomness(x,y,t):
pyautogui.moveTo(addRandomness(x,10),addRandomness(y,10),t+random()/2)
def remove_suffix(input_string, suffix):
if suffix and input_string.endswith(suffix):
return input_string[:-len(suffix)]
return input_string
def load_images():
file_names = listdir('./targets/')
targets = {}
for file in file_names:
path = 'targets/' + file
targets[remove_suffix(file, '.png')] = cv2.imread(path)
return targets
images = load_images()
def clickBtn(img,name=None, timeout=3, threshold = ct['default']):
logger(None, progress_indicator=True)
if not name is None:
pass
start = time.time()
while(True):
matches = positions(img, threshold=threshold)
if(len(matches)==0):
hast_timed_out = time.time()-start > timeout
if(hast_timed_out):
if not name is None:
pass
return False
continue
x,y,w,h = matches[0]
pos_click_x = x+w/2
pos_click_y = y+h/2
if (restrictArea(x=pos_click_x,y=pos_click_y) == False):
moveToWithRandomness(pos_click_x,pos_click_y,ct['mouse_speed'])
pyautogui.click()
return True
def printSreen():
with mss.mss() as sct:
monitor = sct.monitors[0]
sct_img = np.array(sct.grab(monitor))
return sct_img[:,:,:3]
def positions(target, threshold=ct['default'],img = None):
if img is None:
img = printSreen()
result = cv2.matchTemplate(img,target,cv2.TM_CCOEFF_NORMED)
w = target.shape[1]
h = target.shape[0]
yloc, xloc = np.where(result >= threshold)
rectangles = []
for (x, y) in zip(xloc, yloc):
rectangles.append([int(x), int(y), int(w), int(h)])
rectangles.append([int(x), int(y), int(w), int(h)])
rectangles, weights = cv2.groupRectangles(rectangles, 1, 0.2)
return rectangles
def scroll(clickAndDragAmount):
flagScroll = positions(images['spg-flag-scrool'], threshold = ct['commom'])
if (len(flagScroll) == 0):
return
x,y,w,h = flagScroll[len(flagScroll)-1]
moveToWithRandomness(x,y,ct['mouse_speed'])
pyautogui.dragRel(0,clickAndDragAmount,duration=1, button='left')
def refreshPage():
if(ct['type_refresh']) == 'ctrl+f5':
pyautogui.hotkey('ctrl','f5')
else:
if(ct['type_refresh']) == 'shift+f5':
pyautogui.hotkey('shift','f5')
else:
if(ct['type_refresh']) == 'ctrl+shift+r':
pyautogui.hotkey('ctrl','shift','r')
else:
if(ct['type_refresh']) == 'Todos':
pyautogui.hotkey('ctrl','f5')
time.sleep(1.5)
pyautogui.hotkey('shift','f5')
time.sleep(1.5)
pyautogui.hotkey('ctrl','shift','r')
time.sleep(ct['timeW_after_refreshPage'])
processLogin()
def goToSpaceShips():
if clickBtn(images['spg-spaceships-ico']):
global login_attempts
login_attempts = 0
def loginSPG():
global login_attempts
if login_attempts > 3:
logger('🔃 Too many login attempts, refreshing')
login_attempts = 0
processLogin()
return
if clickBtn(images['spg-connect-wallet'], name='connectWalletBtn', timeout = 10):
logger('🎉 Connect wallet button detected, logging in!')
login_attempts = login_attempts + 1
if clickBtn(images['select-wallet-2'], name='sign button', timeout=8):
# sometimes the sign popup appears imediately
login_attempts = login_attempts + 1
return
# click ok button
if not clickBtn(images['select-wallet-1-no-hover'], name='selectMetamaskBtn'):
if clickBtn(images['select-wallet-1-hover'], name='selectMetamaskHoverBtn', threshold = ct['select_wallet_buttons'] ):
pass
else:
pass
if clickBtn(images['select-wallet-2'], name='signBtn', timeout = 20):
login_attempts = login_attempts + 1
checkClose()
def playSPG():
if clickBtn(images['spg-play'], name='okPlay', timeout=5):
logger('played SPG')
def removeSpaceships():
time.sleep(2)
while True:
if(CheckTimeRestartGame()):
break
if(checkClose()):
break
if(CheckBotWork() == False):
break
if(checkHome()):
buttons = positions(images['spg-x'], threshold=ct['remove_to_work_btn'])
if len(buttons) > 0:
# Havia criado com objetivo de clicar nos X de baixo para cima
# e para conseguir fazer isso eu havia criado um while para posicionar os index ao contrario.
# \o/ porem descobri o "reversed" que faz isso certinho.
# index = len(buttons)
# while index > 0:
# index -= 1
# buttonsNewOrder.append(buttons[index])
# for (x, y, w, h) in buttonsNewOrder:
# moveToWithRandomness(x+(w/2),y+(h/2),1)
# pyautogui.click()
for (x, y, w, h) in reversed(buttons):
if (restrictArea(x=x,y=y) == False):
moveToWithRandomness(x+(w/2),y+(h/2),ct['mouse_speed'])
pyautogui.click()
if(CheckTimeRestartGame()):
break
if(checkClose()):
break
if(CheckBotWork() == False):
break
else:
logger('Click restricted Area Eixo')
if len(buttons) == 0:
break
else:
break
def renewSpaces():
buttonsRenew = positions(images['spg-taxa-r'], threshold=ct['go_to_work_btn'])
for (x, y, w, h) in reversed(buttonsRenew):
if restrictArea(x=x,y=y) == False:
moveToWithRandomness(x+(w/2),y+(h/2),ct['mouse_speed'])
pyautogui.click()
time.sleep(1)
clickBtn(images['spg-yes'])
time.sleep(1)
def clickButtonsFight():
global count_reloadSpacheship
if(ct['send_space_only_full'] == True):
buttons = positions(images['spg-go-fight-100'], threshold=ct['go_to_work_btn'])
ajustX = 14
ajustY = 14
else:
buttons = positions(images['spg-go-fight'], threshold=ct['go_to_work_btn'])
ajustX = 0
ajustY = 0
if(ct['auto_renew'] == True):
renewSpaces()
qtd_send_spaceships = ct['qtd_send_spaceships']
for (x, y, w, h) in reversed(buttons): #Adjust for click button a little more intelligent
tx = x+ajustX
ty = y+ajustY
if restrictArea(x=tx,y=ty) == False:
moveToWithRandomness(tx+(w/2),ty+(h/2),ct['mouse_speed'])
pyautogui.click()
global hero_clicks
hero_clicks = hero_clicks + 1
if hero_clicks >= qtd_send_spaceships:
logger('Finish Click Hero')
return -1
if(ct['send_incomplete_team']):
if hero_clicks > 0:
if hero_clicks >= ct['send_space_min']:
if count_reloadSpacheship >= ct['qtd_check_reloadSpacheship']:
logger('Send incomplet team')
hero_clicks = qtd_send_spaceships
return -1
else:
logger('Click restricted Area')
return len(buttons)
def refreshSpaceships(qtd):
if(checkHome()):
logger('Refresh Spaceship to Fight')
global count_reloadSpacheship
global count_nexList
buttonsClicked = 1
qtd_spaceships = ct['qtd_spaceships']
qtd_send_spaceships = ct['qtd_send_spaceships']
cda = c['click_and_drag_amount']
global hero_clicks
hero_clicks = 0
empty_scrolls_attempts = qtd_send_spaceships
if(checkClose()):
return False
if(CheckTimeRestartGame()):
return False
if qtd > 0:
hero_clicks = qtd
logger('Quantidade ja selecionada {}'.format(hero_clicks))
if hero_clicks == qtd_send_spaceships:
empty_scrolls_attempts = 0
goToFight()
while(empty_scrolls_attempts >0):
if(CheckTimeRestartGame()):
break
if(checkClose()):
break
if(checkHome()):
buttonsClicked = clickButtonsFight()
if(CheckBotWork() == False):
break
if buttonsClicked == 0:
empty_scrolls_attempts = empty_scrolls_attempts - 1
scroll(-cda)
else:
if buttonsClicked == -1:
empty_scrolls_attempts = 0
else:
if buttonsClicked > 0:
empty_scrolls_attempts = empty_scrolls_attempts + 1
time.sleep(2)
logger('💪 {} Spaceships sent to Fight'.format(hero_clicks))
else:
break
if hero_clicks == qtd_send_spaceships:
empty_scrolls_attempts = 0
count_reloadSpacheship = 0
count_nexList = 1
goToFight()
checkVictory()
#Check IF Type is EndFightAndSurrender for to return to first boss
if(ct['type_limit_wave'] == 'EndFightAndSurrender'):
surrenderFight()
else:
count_reloadSpacheship +=1
qtd_spaceships = ct['qtd_spaceships']
qtd_spaceships = math.ceil(qtd_spaceships/30)
if(checkHome()):
if count_nexList < qtd_spaceships:
reloadSpacheship()
time.sleep(1)
clickBtn(images['spg-next'])
time.sleep(1)
count_nexList +=1
else:
count_nexList = 1
reloadSpacheship()
refreshSpaceships(hero_clicks)
else:
empty_scrolls_attempts = 0
count_reloadSpacheship = 0
count_nexList = 1
def goToFight():
logger("Go fight")
CheckBotWork(SetWorking=True)
clickBtn(images['spg-go-to-boss'])
time.sleep(1)
clickBtn(images['spg-confirm'])
def surrenderFight():
if len(positions(images['spg-surrender'], threshold=ct['end_boss']) ) > 0:
clickBtn(images['spg-surrender'])
time.sleep(0.8)
clickBtn(images['spg-confirm-surrender'])
global count_victory
count_victory = 0
CheckBotWork(SetWorking=True)
def endFight():
logger("End fight")
CheckBotWork(SetWorking=True)
time.sleep(3)
returnBase()
time.sleep(15)
if len(positions(images['spg-processing'], threshold=ct['commom_position'])) > 0:
time.sleep(ct['check_processing_time'])
if (checkHome()):
removeSpaceships()
time.sleep(1)
refreshSpaceships(0)
else:
refreshPage()
def returnBase():
goToSpaceShips()
def lifeBoss():
lessPosition = positions(images['spg-life-boss-1'], threshold=ct['end_boss'])
if len(lessPosition) == 0:
lessPosition = positions(images['spg-life-boss-2'], threshold=ct['end_boss'])
if len(lessPosition) == 0:
lessPosition = positions(images['spg-life-boss-3'], threshold=ct['end_boss'])
return lessPosition
def processLogin():
logger('Starting Login')
sys.stdout.flush()
loginSPG()
time.sleep(3)
playSPG()
def checkClose():
if clickBtn(images['spg-close'], name='closeBtn', timeout=1):
processLogin()
return True
def reloadSpacheship():
if len(positions(images['spg-base'], threshold=ct['commom_position'])) > 0 and len(positions(images['spg-go-to-boss'], threshold=ct['base_position'])) > 0:
clickBtn(images['spg-base'], name='closeBtn', timeout=1)
time.sleep(3)
clickBtn(images['spg-spaceships-ico'], name='closeBtn', timeout=1)
time.sleep(3)
def checkVictory():
global count_victory
if clickBtn(images['spg-confirm-victory'], name='okVicBtn', timeout=1):
count_victory += 1
return True
return False
def checkLimitWave():
global count_victory
limitWave = ct['limit_wave']
qtdLimitWave = ct['qtd_limit_wave']
typeLimitWave = ct['type_limit_wave']
if(limitWave == True):
if(count_victory >= qtdLimitWave):
count_victory = 0
time.sleep(1)
if(typeLimitWave == 'EndFight' or typeLimitWave == 'EndFightAndSurrender'):
endFight()
else:
surrenderFight()
time.sleep(2)
return True
else:
checkVictory()
return False
return False
def CheckTimeRestartGame():
if( ct['time_restart_game'] > 0):
global time_start_bot
now = time.time()
if now - time_start_bot > addRandomness(ct['time_restart_game']*60):
refreshPage()
return True
return False
def connectWallet():
if clickBtn(images['spg-connect-wallet'], name='conectBtn', timeout=5):
return True
def checkHome():
if len(positions(images['spg-go-to-boss'], threshold=ct['base_position'])) > 0:
return True
def clickConfirm():
if clickBtn(images['spg-confirm'], name='okBtn', timeout=3):
return True
def checkProcessing():
if len(positions(images['spg-processing'], threshold=ct['commom_position'])) > 0:
time.sleep(ct['check_processing_time'])
if len(positions(images['spg-processing'], threshold=ct['commom_position'])) > 0:
return True
def CheckBotWork(SetWorking = False):
global bot_working
global last
now = time.time()
if SetWorking == True:
bot_working = True
if bot_working == False:
logger('Bot is not performing any action.')
if now - last["CheckBotWork"] > addRandomness(ct['Check_Bot_Work']*60):
logger('Bot is not performing any action. The Game will be restarted.')
refreshPage()
return False
else:
last["CheckBotWork"] = now
return True
def restrictArea(x,y):
if y >= 612:
logger('Eixo y {}'.format(y))
return True
else:
return False
def main():
time.sleep(5)
t = c['time_intervals']
global last
last = {
"lessPosition":[],
"CheckInitialPage":0,
"CheckInicialCube":0,
"CheckBotWork":0,
}
last["CheckBotWork"] = time.time()
while True:
global bot_working
bot_working = False
now = time.time()
if (connectWallet()):
bot_working = True
processLogin()
else:
if (checkHome()):
bot_working = True
removeSpaceships()
refreshSpaceships(0)
if (clickConfirm()):
bot_working = True
time.sleep(2)
endFight()
if (checkVictory()):
bot_working = True
if (checkProcessing()):
bot_working = True
refreshPage()
if (checkClose()):
bot_working = True
if len(positions(images['spg-surrender'], threshold=ct['end_boss']) ) > 0:
cont = ct['check_boss']
while(cont >0):
cont = cont-1
nowPosition = lifeBoss()
if(checkLimitWave() == False):
if len(last["lessPosition"]) == 0:
if len(nowPosition) > 0:
last["lessPosition"] = nowPosition
logger("Starting position")
else:
if np.array_equal(nowPosition,last["lessPosition"]) == False:
last["lessPosition"] = nowPosition
logger("Updating position")
break
else:
if clickBtn(images['spg-confirm'], name='okBtn', timeout=3):
time.sleep(2)
endFight()
break
else:
if cont == 0:
logger("End time wait")
endFight()
break
else:
logger("Waiting")
last["lessPosition"] = nowPosition
bot_working = True
if(checkLimitWave() == False):
time.sleep(5)
else:
bot_working = True
if len(nowPosition) == 0:
last["checkBossTime"] = now
if(CheckTimeRestartGame()):
bot_working = True
CheckBotWork()
main()