forked from sCryptoHelp/spacecrypto-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
549 lines (404 loc) · 15.5 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
# -*- coding: utf-8 -*-
from ast import Return
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 pygetwindow
import numpy as np
import mss
import pyautogui
import time
import sys
import yaml
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
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 show(rectangles, img = None):
if img is None:
with mss.mss() as sct:
monitor = sct.monitors[0]
img = np.array(sct.grab(monitor))
for (x, y, w, h) in rectangles:
cv2.rectangle(img, (x, y), (x + w, y + h), (255,255,255,255), 2)
cv2.imshow('img',img)
cv2.waitKey(0)
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
moveToWithRandomness(pos_click_x,pos_click_y,1)
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,1)
pyautogui.dragRel(0,clickAndDragAmount,duration=1, button='left')
def refreshPage():
# refresh Page
clickBtn(images['refresh-page'])
def login():
global login_attempts
logger('😿 Checking if game has disconnected')
if login_attempts > 3:
logger('🔃 Too many login attempts, refreshing')
login_attempts = 0
processLogin()
return
if clickBtn(images['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
if len(positions(images['spg-go-to-boss'], threshold=ct['base_position'])) > 0:
login_attempts = 0
refreshSpaceships(0)
return
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 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
def playSPG():
if clickBtn(images['spg-play'], name='okPlay', timeout=5):
logger('played SPG')
def removeSpaceships():
time.sleep(2)
while True:
buttons = positions(images['spg-x'], threshold=ct['remove_to_work_btn'])
buttonsNewOrder = []
if len(buttons) > 0:
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()
if len(buttons) == 0:
break
def clickButtonsFight():
buttons = positions(images['spg-go-fight'], threshold=ct['go_to_work_btn'])
qtd_send_spaceships = ct['qtd_send_spaceships']
for (x, y, w, h) in buttons:
moveToWithRandomness(x+(w/2),y+(h/2),1)
pyautogui.click()
global hero_clicks
hero_clicks = hero_clicks + 1
if hero_clicks >= qtd_send_spaceships:
logger('Finish Click Hero')
return -1
return len(buttons)
def refreshSpaceships(qtd):
logger('Refresh Spaceship to Fight')
buttonsClicked = 1
empty_qtd_spaceships = ct['qtd_spaceships']
qtd_send_spaceships = ct['qtd_send_spaceships']
cda = c['click_and_drag_amount']
global hero_clicks
hero_clicks = 0
# c['scroll_attemps']
empty_scrolls_attempts = qtd_send_spaceships
checkClose()
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):
buttonsClicked = clickButtonsFight()
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))
if hero_clicks == qtd_send_spaceships:
empty_scrolls_attempts = 0
goToFight()
checkVictory()
surrenderFight()
else:
reloadSpacheship()
refreshSpaceships(hero_clicks)
def goToFight():
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(2)
clickBtn(images['spg-confirm-surrender'])
global count_victory
count_victory = 0
def endFight():
logger("End fight")
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 len(positions(images['spg-go-to-boss'], threshold=ct['base_position'])) > 0:
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()
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'):
endFight()
else:
surrenderFight()
time.sleep(2)
return True
else:
checkVictory()
return False
return False
def main():
time.sleep(5)
t = c['time_intervals']
windows = []
for w in pygetwindow.getWindowsWithTitle('spacecrypto'):
windows.append({
"window": w,
"lessPosition":[],
"CheckInitialPage":0,
"CheckInicialCube":0,
"CheckBackPage":0,
})
while True:
now = time.time()
for last in windows:
if clickBtn(images['spg-connect-wallet'], name='conectBtn', timeout=5):
processLogin()
else:
if len(positions(images['spg-go-to-boss'], threshold=ct['base_position'])) > 0:
removeSpaceships()
refreshSpaceships(0)
if clickBtn(images['spg-confirm'], name='okBtn', timeout=3):
time.sleep(2)
endFight()
checkVictory()
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:
refreshPage()
time.sleep(5)
processLogin()
if len(positions(images['spg-initial-pg'], threshold=ct['commom_position'])) > 0:
if now - last["CheckInitialPage"] > addRandomness(ct['check_initial_page']):
refreshPage()
time.sleep(5)
processLogin()
else:
last["CheckInitialPage"] = now
pass
else:
last["CheckInitialPage"] = now
if len(positions(images['spg-cube'], threshold=ct['commom_position'])) > 0:
if now - last["CheckInicialCube"] > addRandomness(ct['check_initial_cube']*60):
refreshPage()
time.sleep(5)
processLogin()
else:
last["CheckInicialCube"] = now
pass
else:
last["CheckInicialCube"] = now
if len(positions(images['spg-back'], threshold=ct['commom_position'])) > 0:
if now - last["CheckBackPage"] > addRandomness(ct['check_erro']*60):
refreshPage()
time.sleep(5)
processLogin()
else:
last["CheckBackPage"] = now
pass
else:
last["CheckBackPage"] = now
checkClose()
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
if(checkLimitWave() == False):
time.sleep(5)
if len(nowPosition) == 0:
last["checkBossTime"] = now
main()