-
Notifications
You must be signed in to change notification settings - Fork 5
/
AutoAFK2.py
1277 lines (1112 loc) · 59.6 KB
/
AutoAFK2.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import inspect
import math
import sys
from humanfriendly import format_timespan
from tools import * # Includes logging so we don't import here also
from consolemenu import *
from datetime import datetime, timezone
import ctypes
# Global variables
global last_synergy
last_synergy = time.time() - 300 # -300 so we don't wait 300 seconds before opening the first
global last_corrupt
last_corrupt = time.time()
# For stage pushing
global stage_defeats
stage_defeats = 0
global formation
formation = 1
global first_stage_won
first_stage_won = False
# Version output
version = '0.9.13'
# Current time in UTC for tracking which towers/events are open
currenttimeutc = datetime.now(timezone.utc)
# Game version to launch
global server
server = 'com.farlightgames.igame.gp'
# Enabling/Disabling formation loading
global load_formations
load_formations = True
# Configure launch arguments
parser = argparse.ArgumentParser()
# Modes
parser.add_argument("-a", "--abyss", action='store_true', help="Run the Trial of Abyss retry function")
parser.add_argument("-l", "--legend", action='store_true', help="Run the Legend Trials retry function")
parser.add_argument("-t", "--teamup", action='store_true', help="Run the Team-up function")
parser.add_argument("-d", "--dailies", action='store_true', help="Run the Dailies function")
parser.add_argument("-q", "--quest", action='store_true', help="Runs the Quest running function")
parser.add_argument("-dr", "--dream", action='store_true', help="Run the Dream Realm function")
parser.add_argument("-afks", action='store_true', help="Run AFK Stages")
parser.add_argument("-afkt", action='store_true', help="Run AFK Talent Stages")
parser.add_argument("-test", action='store_true', help="Used for testing functions")
parser.add_argument("-charms", action='store_true', help="Run the Dura's Trials function")
parser.add_argument("-fs", "--formation_skip", action='store_true', help="Don't load formations")
# Configurations
parser.add_argument("-s", "--server", choices=['global', 'vn'], default='global', help="Select alernative game servers")
parser.add_argument("-c", "--config", metavar="CONFIG", default="settings.ini", help="Define alternative settings file to load")
parser.add_argument('--forceprint', action='store_true', help='Force print output')
args = vars(parser.parse_args())
# Work out which config file we're reading/writing to/from
if args['config']:
settings = os.path.join(cwd, args['config'])
else:
settings = os.path.join(cwd, 'settings.ini')
config.read(settings)
# Change server if necessary
if args['server'] == 'vn':
globals()['server'] = 'com.farlightgames.igame.gp.vn'
# Disable formation loading if set
if args['formation_skip']:
globals()['load_formations'] = False
# Make a nice name for the output log file
if settings == 'settings.ini':
logname = 'autoafk2.log'
else:
logname = settings.split('.')[0] + '.log'
from threading import Event
import keyboard
hotkey = 'F10'
running = Event()
running.set() # at the start, it is running
def handle_key_event(event):
if event.event_type == 'down':
# toggle value of 'running'
if running.is_set():
running.clear()
logger.info('Pausing!')
else:
running.set()
# make it so that handle_key_event is called when k is pressed; this will
# be in a separate thread from the main execution
#keyboard.hook_key(hotkey, handle_key_event)
# File handler
file_log_handler = logging.FileHandler(filename=logname)
logger.addHandler(file_log_handler)
formatter = logging.Formatter('%(asctime)s %(message)s')
file_log_handler.setFormatter(formatter)
# STDERR handler so we don't lose that
logging.StreamHandler(stream=sys.stderr)
# Make timestamps etc look pretty
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%H:%M:%S',
level=logging.INFO)
# Define logger for tools.py usage also
logger = logging.getLogger('autoafk2')
# This logs execptions via logger which is great for finding out what went wrong with unnattended sessions
# Copied word for word from: https://stackoverflow.com/questions/6234405/logging-uncaught-exceptions-in-python
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
logger.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_exception
# Quick storage for commonly used regions
regions = {
#locate
'sunandstars': (770, 40, 100, 100),
'main_menu': (900, 1750, 150, 150),
'menu_activities': (20, 950, 1050, 800),
'back': (50, 1750, 150, 150),
'chat_window': (184, 362, 850, 1300),
'right_sidebar': (888, 744, 190, 1000),
'chat_selection': (20, 300, 170, 900),
'top_third': (0, 0, 1080, 640),
'middle_third': (0, 640, 1080, 640),
'bottom_third': (0, 1280, 1080, 640),
'bottom_buttons': (0, 1620, 1080, 300),
'confirm_deny': (500, 1100, 500, 300),
'battle_modes': (20, 580, 1050, 1100),
'action_buttons': (400, 1050, 300, 500), # gives out of bounds error and I'm too tired to work out why
'levelup': (150, 900, 950, 50),
'levelup_hero': (1000, 1700, 80, 60),
'x3_and_skip': (720, 1450, 350, 110)
}
# Boot up text
logger.info('Loaded settings file: ' + str(settings.split('\\')[-1]))
logger.info('Version: ' + version)
# Nice name for the window and debugging peoples screenshots
ctypes.windll.kernel32.SetConsoleTitleW("AutoAFK2 v" + version)
# Boot up activities before tasks are ran
connect_and_launch(port=config.get('ADVANCED', 'port'), server=globals()['server'])
resolutionCheck()
waitUntilGameActive()
# TODO single SA battle and daily GS collection
def dailies():
# start_autoprogress()
if config.getboolean('ACTIVITIES', 'claim_afk'):
claim_afk_rewards()
if config.getboolean('ACTIVITIES', 'friend_points'):
friend_points_collect()
if config.getboolean('ACTIVITIES', 'mail_collect'):
mail_connect()
if config.getint('ACTIVITIES', 'arena_battles') > 0:
arena(config.getint('ACTIVITIES', 'arena_battles'))
if config.getboolean('ACTIVITIES', 'emporium_purchases'):
emporium_purchases()
if config.getboolean('ACTIVITIES', 'single_recruit'):
single_recruit()
if config.getboolean('ACTIVITIES', 'dream_realm'):
dream_realm()
if config.getboolean('ACTIVITIES', 'collect_quests'):
collect_quests()
# if config.getboolean('ACTIVITIES', 'claim_events'):
# claim_events()
if config.getboolean('ACTIVITIES', 'push_towers'):
blind_push("daily_towers")
if config.getboolean('ACTIVITIES', 'push_dream_realm'):
blind_push("dream_realm")
if config.getboolean('ACTIVITIES', 'noble_path'):
noble_path()
if config.getboolean('ACTIVITIES', 'level_up'):
level_up()
if config.getboolean('ACTIVITIES', 'farm_affinity'):
farm_affinity()
logger.info('Dailies done!')
# Bit of an ugly function, we open the Team-Up chat and scan for the orange Join button and the Synergy Battle label for synergy battles
def team_up():
timer = 0
start = time.time()
while True: # Naughty perma-loop, nested inside another when we call this with startup flags so calling 'return' will start from the top
# First ensure we're at the main map
while not isVisible('labels/sunandstars', region=regions['sunandstars'], seconds=0):
click('buttons/back', suppress=True, region=regions['back'])
click_location('neutral')
# Then open team-up chat
while not isVisible('teamup/teamup', click=True, region=regions['chat_selection']): # Open the Team-Up section
click('teamup/chat', seconds=2, suppress=True, region=regions['right_sidebar']) # Open Chat window
click('teamup/chat_yellow', retry=5, seconds=2, suppress=True, confidence=0.7, region=regions['right_sidebar']) # Open Chat window
# Loop while searching for 'Join' button
while not isVisible('teamup/join', seconds=0, confidence=0.8, region=regions['chat_window']):
# If it's been more than 300s we might be stuck so we try these to get back to the chat window
if (time.time() - globals()['last_corrupt']) > 300 and (time.time() - globals()['last_synergy']) > 300:
click('teamup/chat', seconds=0, suppress=True, region=regions['right_sidebar']) # Ensure we actually have chat open
click('teamup/teamup', seconds=0, suppress=True, region=regions['chat_selection']) # Ensure we're in the right section
click('buttons/back', seconds=0, suppress=True, region=regions['back']) # Somehow we open afk rewards occasionally, this will exit that
isVisible('buttons/confirm', region=regions['confirm_deny'], click=True) # to catch 'Reconnect to chat?'
swipe(1000, 1500, 1000, 500, 500)
# Synergy battle hero lending is handled here for reasons
if isVisible('teamup/synergy', seconds=0, region=regions['chat_window']):
x, y = returnxy('teamup/synergy', region=regions['chat_window'])
# We wait 60s between each one else we can end up opening and closing the same one repeatadly
if x != 0: # 0 is the 'nothing found' return value from returnxy() so skip if it's returned
# If green button found and it's been more than 60s since the last Synergy
if return_pixel_colour(x, y + 220, 2, seconds=0) < 200 and (time.time() - globals()['last_synergy'] > 120):
logger.info('Synergy Battle found!')
clickXY(x, y + 220) # 220 is the button distance from the label
if isVisible('buttons/back', region=regions['back']):
clickXY(300, 900) # Second highest power hero (in case you want to save the primary or guildmates/friends)
clickXY(650, 1800)
click('buttons/back', suppress=True, region=regions['back'])
logger.info('Hero lent\n')
globals()['last_synergy'] = time.time()
return
else:
logger.info('Something went wrong with Synergy Battle, returning\n')
globals()['last_synergy'] = time.time()
return
else:
logger.info('Synergy button gone!\n')
return
# Log start time and click 'Join'
duration = time.time() - start
click_last('teamup/join', seconds=4, confidence=0.8, region=regions['chat_window'])
# If Ready button is not visible after clicking join then it's been disbanded/level locked etc so we restart
if not isVisible('teamup/ready', region=regions['bottom_buttons']):
# Try a quit just in case
click('teamup/quit', region=regions['bottom_buttons'], suppress=True)
click('buttons/confirm', region=regions['confirm_deny'], suppress=True) # to catch 'Reconnect to chat?
return
# Ready up
click('teamup/ready', seconds=4, region=regions['bottom_buttons'])
logger.info('Corrupt Creature found in ' + format_timespan(round(duration)) + '!') # Only message after we're in to avoid spam
# If Quit button is visible 15 cycles after readying up then the host is afk etc so we restart
while isVisible('teamup/quit', confidence=0.8, region=regions['bottom_buttons']):
timer += 1
if timer > 15:
logger.info('Lobby timeout error!\n')
click('teamup/quit', seconds=2, region=regions['bottom_buttons'])
clickXY(850, 1250, seconds=4)
return
# Deploy Heroes
while isVisible('teamup/ready_lobby', confidence=0.8, region=regions['bottom_buttons']):
logger.info('Deploying heroes')
wait(2) # Wait for the emulator to load new assets after moving to battle screen else first click below doesn't register
clickXY(120, 1300)
clickXY(270, 1300)
clickXY(450, 1300)
click('teamup/ready_lobby', suppress=True, confidence=0.8, region=regions['bottom_buttons'])
break # Break loop otherwise if we miss a button due to lag we loop here until battle starts
# Wait until battle finishes
while not isVisible('labels/tap_to_close', confidence=0.8, region=regions['bottom_buttons']):
timer += 1
if timer > 30:
logger.info('Battle timeout error!\n')
click_location('neutral') # Neutral taps to try and get back to main map if something went wrong
return
if isVisible('labels/tap_to_close', confidence=0.8, region=regions['bottom_buttons'], click=True):
logger.info('Battle complete!\n')
# Finish up and start the loop again
timer = 0
globals()['last_corrupt'] = time.time()
return
def start_autoprogress():
logger.info('Starting Auto-Progress')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
clickXY(100, 1800, seconds=4) # Open AFK Rewards
clickXY(750, 1750, seconds=4) # Clear Pop-Up
isVisible('buttons/claim_afkrewards', region=regions['middle_third'], click=True)
if isVisible('labels/afk_rewards_woi', region=[0, 680, 150, 200]):
clickXY(750, 1600, seconds=5) # AFK Stage Battle
click('buttons/autobattle', region=regions['bottom_buttons'])
click('buttons/confirm', region=regions['confirm_deny'], seconds=3)
click('buttons/auto-progress', seconds=3)
click('buttons/confirm', seconds=6)
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Auto-Progress started!\n')
else:
logger.info('Issue starting Auto-Progress!')
recover()
def claim_afk_rewards():
logger.info('Claiming AFK Rewards')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
clickXY(100, 1800, seconds=4) # Open AFK Rewards
clickXY(750, 1750, seconds=4) # Clear Pop-Up
if isVisible('labels/afk_rewards_woi', region=[0, 680, 150, 200]):
clickXY(550, 1400) # Click Chest
clickXY(550, 1080) # Click Collect
wait(2) # Wait and claim again to complete daily quest
clickXY(550, 1400) # Click Chest
clickXY(550, 1080) # Click Collect
# Fast rewards
if isVisible('labels/afk_rewards_woi', region=[0, 680, 150, 200]):
for _ in range(config.getint('ACTIVITIES', 'fast_rewards')):
if isVisible('buttons/fast_rewards', click=True):
logger.info('Fast reward #' + str(_ + 1) + ' claimed')
click('buttons/confirm', suppress=True)
clickXY(1000, 1800)
clickXY(100, 1800) # Close
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('AFK Rewards Claimed!\n')
else:
logger.info('Issue opening AFK Rewards!')
recover()
def friend_points_collect():
logger.info('Claiming Friend Gifts')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
wait(2) # For things to load
click('buttons/main_menu', region=regions['main_menu'])
click('buttons/friends', region=regions['menu_activities'], seconds=2)
if isVisible('labels/friends'):
clickXY(700, 1800, seconds=2)
clickXY(850, 300, seconds=2)
click_location('neutral')
click('buttons/back', region=regions['back'])
click('buttons/back', region=regions['back'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Friend Gifts Claimed!\n')
else:
logger.info('Issue claiming friends points!')
recover()
def mail_connect():
logger.info('Claiming Mail')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
click('buttons/main_menu', region=regions['main_menu'])
click('buttons/mail', region=regions['menu_activities'], seconds=2)
if isVisible('labels/mail'):
clickXY(750, 1800, seconds=2)
clickXY(750, 1800, seconds=2)
click('buttons/back', region=regions['back'])
click('buttons/back', region=regions['back'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Mail Claimed!\n')
else:
logger.info('Issue claiming Mail!')
recover()
def emporium_purchases():
logger.info('Purchasing daily summon card')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
click('buttons/main_menu', region=regions['main_menu'])
click('buttons/emporium', region=regions['menu_activities'], seconds=2)
clickXY(100, 700, seconds=2) # guild store
if isVisible('labels/emporium_guild', region=regions['top_third']):
if isVisible('emporium/guild_summoncard'):
click('emporium/guild_summoncard', region=regions['middle_third'])
click('buttons/purchase', region=regions['bottom_buttons'])
click('buttons/confirm', region=regions['confirm_deny'], seconds=2)
click_location('neutral')
else:
logger.info('Daily card already purchased!')
click('buttons/back2', region=regions['back'])
click('buttons/back', region=regions['back'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Daily summon card purchased!\n')
else:
logger.info('Issue purchasing summon card!')
recover()
def arena(battles=9):
timeout = 0
counter = 0
logger.info('Battling Arena')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
clickXY(450, 1825)
if isVisible('labels/battle_modes'):
click('buttons/arena', region=regions['battle_modes'], seconds=2)
if isVisible('labels/arena_weekly_report', region=regions['top_third']):
logger.info('Weekly Arena rewards found!')
clickXY(550, 1800)
click_location('neutral')
click_location('neutral')
while counter < battles:
logger.info('Fighting Arena Battle ' + str(counter+1) + ' of ' + str(battles))
click('buttons/challenge', region=regions['bottom_buttons'], seconds=3, retry=5, confidence=0.8)
if isVisible('buttons/confirm', region=regions['confirm_deny']):
# logger.info('Purchase challenge pop-up detected, confirming')
click('buttons/confirm', region=regions['confirm_deny'])
click('buttons/challenge', seconds=3, region=regions['bottom_buttons'])
clickXY(180, 1450, seconds=6) # Leftmost opponent
click('buttons/battle', region=regions['bottom_buttons'])
while not isVisible('labels/tap_to_close', region=regions['bottom_buttons'], confidence=0.8):
# Clear promotion screen if visible (not sure this does anything with while isVisible loop at the end covering the case)
if isVisible('labels/arena_promote', region=regions['bottom_third']):
clickXY(550, 1800)
if isVisible('buttons/skip_inverse', seconds=0, region=regions['x3_and_skip']):
click('buttons/skip_inverse', seconds=3, region=regions['x3_and_skip'])
logger.info('Skip available, skipping the fight')
timeout += 1
if timeout > 40: # Should be about 10 seconds longer than a full fight at 2x
logger.info('Arena timeout error\n')
timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')
save_screenshot('arena_timeout_' + timestamp)
recover()
return
logger.info('Battle complete')
while isVisible('labels/tap_to_close', region=regions['bottom_buttons'], confidence=0.8):
click('labels/tap_to_close', region=regions['bottom_buttons'], seconds=4, suppress=True)
counter += 1
timer = 0
# Collect Victory Rewards
clickXY(200, 550)
clickXY(200, 550)
clickXY(200, 550)
click_location('neutral')
else:
logger.info('Issue opening Arena!')
recover()
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Arena battles completed!\n')
def dream_realm():
timer = 0
logger.info('Battling Dream Realm')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
clickXY(450, 1825, seconds=3)
click('buttons/dream_realm', region=regions['battle_modes'], seconds=3)
# First collect rewards
if isVisible('buttons/battle', region=regions['bottom_buttons']):
logger.info('Collecting previous round rewards')
click('buttons/dr_rewards', region=regions['top_third'], seconds=4)
clickXY(550, 1800, seconds=2) # Clear loot
click('buttons/back2', region=regions['back'], seconds=3)
else:
logger.info('issue collecting rewards!')
recover()
return
# Then attempt a single battle
if isVisible('buttons/battle', region=regions['bottom_buttons']):
logger.info('Battling Dream Realm')
click('buttons/battle', region=regions['bottom_buttons'], seconds=5)
click('buttons/battle', region=regions['bottom_buttons'], seconds=5)
time.sleep(60) # wait for battle to end
while not isVisible('labels/tap_to_close', region=regions['bottom_buttons']): # Few clicks to clear loot too
timer += 1
if timer > 60:
logger.info('DR Timer Exceeded!')
break
pass
click('labels/tap_to_close', region=regions['bottom_buttons'], seconds=5, suppress=True)
if isVisible('buttons/deny', click=True, seconds=3):
logger.info('Not sharing formation..')
click('labels/tap_to_close', region=regions['bottom_buttons'], seconds=5, suppress=True)
logger.info('Battle complete!')
click('buttons/back', region=regions['back'], seconds=2)
click('buttons/back2', region=regions['back'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Dream Realm completed!\n')
else:
logger.info('Issue collecting rewards!')
recover()
return
def single_recruit():
logger.info('Attempting a single reruitment')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
# Navigate and open all hero recruitment
clickXY(300, 1850, seconds=6)
clickXY(420, 700, seconds=6)
click('buttons/all_hero_recruitment', seconds=6)
# Perform recruit, lots of long waits here as the animations are slow before we stabilise again
if isVisible('labels/all_hero_recruitment', region=regions['bottom_buttons']):
clickXY(250, 1550)
click('buttons/continue2', suppress=True) # long wait for animation
wait(15)
click('buttons/back')
click('buttons/back2', seconds=3)
click('buttons/back2')
logger.info('Single recruitment complete!\n')
else:
logger.info('Issue doing single recruitment!')
recover()
return
def collect_quests():
logger.info('Collecting Quests')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
click('buttons/main_menu', region=regions['main_menu'])
click('buttons/quests', region=regions['menu_activities'], seconds=3)
clickXY(300, 1800, seconds=2)# Daily quests
if isVisible('labels/daily_quests'):
logger.info(' Collecting Daily Quests')
isVisible('buttons/quick_claim', region=regions['bottom_third'], click=True)
wait(3)
if config.getboolean('ADVANCED', 'collect_daily_rewards') is True:
clickXY(900, 200, seconds=2) # collect dailies
click_location('neutral')
else:
logger.info('Skipping daily quest rewards collection')
# Guild quests
logger.info(' Collecting Guild Quests')
clickXY(500, 1800, seconds=2)
while isVisible('buttons/quests_claim'):
click('buttons/quests_claim')
# Season Quests
logger.info(' Collecting Season Growth Trials')
clickXY(950, 1825, seconds=2)
# Season Growth Trials
while isVisible('labels/reward', click=True, region=(232, 451, 700, 100)):
while isVisible('buttons/quests_claim'):
click('buttons/quests_claim')
# Season Growth Quests
logger.info(' Collecting Season Growth Quests')
clickXY(300, 1670, seconds=2)
while isVisible('buttons/quests_claim'):
click('buttons/quests_claim')
click('buttons/back2', region=regions['back'])
click('buttons/back', region=regions['back'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Quests collected!\n')
else:
logger.info('Issue collecting quests!')
recover()
return
def level_up():
logger.info('Levelling available heroes')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
# Open Heroes Hall
clickXY(650, 1850, seconds=3)
# Click to open hero
while isVisible('buttons/levelup_double', region=regions['levelup']) or isVisible('buttons/levelup_single', region=regions['levelup']):
logger.info('Hero found!')
click('buttons/levelup_double', region=regions['levelup'], suppress=True, retry=1)
click('buttons/levelup_single', region=regions['levelup'], suppress=True, retry=1)
# Keep clicking to level
while isVisible('buttons/levelup_double', region=regions['levelup_hero'], seconds=0):
swipe(800, 1800, 800, 1800, 5000) # Hacky way to hold it down
if isVisible('buttons/level_up', region=(500, 1725, 260, 100), seconds=0): # Region in the centre for the 10th level 'Level Up' button
click('buttons/level_up', region=regions['bottom_third'], seconds=4)
click('buttons/back', region=regions['bottom_third'])
while isVisible('buttons/levelup_single', region=regions['levelup_hero'], seconds=0):
swipe(800, 1800, 800, 1800, 5000) # Hacky way to hold it down
if isVisible('buttons/level_up', region=(500, 1725, 260, 100), seconds=0): # Region in the centre for the 10th level 'Level Up' button
click('buttons/level_up', region=regions['bottom_third'], seconds=4)
click('buttons/back', region=regions['bottom_third'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Heroes levelled!\n')
def farm_affinity(heroes=60): # 60 heros in game as of Tasi
logger.info('Clicking ' + str(heroes) + ' heroes for daily affinity bonus')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
counter = 1
clickXY(650, 1850, seconds=5) # Open heroes hall
clickXY(150, 1050, seconds=3) # Click top right hero
if isVisible('buttons/affinity', region=regions['top_third']):
while counter < heroes:
if counter % 10 == 0:
logger.info('Tapping ' + str(counter) + 'th hero')
clickXY(550, 1000, seconds=1)
clickXY(550, 1000, seconds=1)
clickXY(550, 1000, seconds=1)
clickXY(620, 1800, seconds=0.5)
clickXY(1000, 1100, seconds=1.5)
counter += 1
click('buttons/back', region=regions['back'], seconds=2)
click('buttons/back2', region=regions['back'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Affinity farmed!\n')
else:
logger.info('Something went wrong opening hero affinity!')
timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')
save_screenshot('affinity_opening_issue_' + timestamp)
recover()
def noble_path():
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
logger.info('Collecting noble path')
click('buttons/main_menu', region=regions['main_menu'], seconds=2)
click('buttons/noble_path', region=regions['menu_activities'], seconds=5)
click('buttons/start', region=regions['bottom_buttons'], suppress=True, seconds=3) # To clear new noble pop-up
def claim_and_collect(italics=True):
# This will claim quests/collet rewards
clickXY(750, 450) # Click Quests
if isVisible('buttons/claim_all_italics', click=True, region=regions['bottom_third']):
clickXY(1000, 1800) # Clear Loot
# Travelogue
clickXY(350, 450) # Click Trek
if isVisible('buttons/claim_all_italics', click=True, region=regions['bottom_third']):
clickXY(1000, 1800) # Clear Loot
# Fabled Road
logger.info(' Checking Fabled Road')
if isVisible('buttons/fabled_road_active', region=regions['bottom_third'], seconds=2, grayscale=True) or isVisible('buttons/fabled_road_inactive', region=regions['bottom_third'], click=True, seconds=2, grayscale=True):
claim_and_collect()
# Seasonal Noble Path
logger.info(' Checking Season Noble Path')
if isVisible('buttons/noble_season_active', region=regions['bottom_third'], seconds=2, grayscale=True) or isVisible('buttons/noble_season_inactive', region=regions['bottom_third'], click=True, seconds=2, grayscale=True):
claim_and_collect()
# Noble Path
logger.info(' Checking Noble Path')
if isVisible('buttons/noble_path_active', region=regions['bottom_third'], seconds=2, grayscale=True) or isVisible('buttons/noble_path_inactive', region=regions['bottom_third'], click=True, seconds=2, grayscale=True):
claim_and_collect()
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Noble path collected!\n')
else:
logger.info('Something went wrong collecting Season Noble path!')
recover()
def claim_events():
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
logger.info('Claiming event rewards')
click('buttons/main_menu', region=regions['main_menu'], seconds=3)
click('buttons/event', region=regions['menu_activities'], seconds=3)
# Timeless Gala
if isVisible('events/timeless_gala_active', seconds=2) or isVisible('events/timeless_gala_inactive', click=True, seconds=2):
clickXY(750, 750, seconds=2)
while isVisible('events/collect_gala', region=[650, 1100, 400, 550]):
click('events/collect_gala')
logger.info('Timeless Gala claimed')
# All Heroes
if isVisible('events/all_heroes', seconds=2) or isVisible('events/all_heroes_inactive', click=True, seconds=2):
if isVisible('events/all_heroes_claim', click=True, confidence=0.8, retry=10, yrelative=100):
logger.info('All Heroes claimed')
click_location('neutral')
# Swipe left for the next events
swipe(1000, 1800, 250, 1800, 500, seconds=2)
# Swallows Retreat
if isVisible('events/swallows_retreat_inactive', click=True, seconds=2, region=regions['bottom_buttons']):
clickXY(350, 1250) # Daily
while isVisible('events/collect_sr'):
click('events/collect_sr')
clickXY(800, 1250) # Special
while isVisible('events/collect_sr'):
click('events/collect_sr')
logger.info('Swallows Retreat claimed')
# Fishing Diary
if isVisible('events/fishing_diary_inactive', click=True, seconds=3, region=regions['bottom_buttons']):
if isVisible('buttons/collect', click=True, confidence=0.8):
logger.info('Fishing Diary claimed')
click('buttons/back', region=regions['back'])
click('buttons/back', region=regions['back'])
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Events claimed!\n')
def formation_handler(formation_number=1, already_open=False):
if globals()['load_formations'] is False:
logger.info('Formation loading disabled')
return
logger.info('Loading formation #' + str(math.trunc(globals()['formation'])))
counter = 1
unowned_counter = 0
if already_open is False: # Sometimes we're already in the formations menu
click('buttons/records', seconds=3)
while counter != formation_number:
clickXY(1000, 1025)
counter += 1
click('buttons/copy', seconds=2)
# Handle 'Hero not owned' popup
if isVisible('labels/not_owned'):
while isVisible('labels/not_owned'): # Try next formation and check again
logger.info('Hero not owned, trying next formation..')
clickXY(360, 1250)
clickXY(1000, 1025)
click('buttons/copy')
unowned_counter += 1
if unowned_counter > 7:
logger.info('All formations contained an unowned hero!')
break
click('buttons/confirm', suppress=True)
def blind_push(mode, tower=None, load_formation=True):
# Opens first found tower and pushes until defeat, then exits
if mode == "daily_towers":
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
logger.info('Pushing tower until first defeat')
clickXY(460, 1820, seconds=2)
click("labels/legend_trial", seconds=3, retry=3)
click_location('neutral') # To clear District popup
factions = ["Light", "Wilder", "Graveborn", "Mauler"]
for faction in factions:
if isVisible("towers/"+faction.lower(), confidence=0.94, click=True, seconds=4, yrelative=-20):
logger.info('Opening ' + faction + ' tower\n')
if isVisible("towers/lvl", click=True, region=(15, 850, 1050, 800), seconds=3, yrelative=-50, grayscale=True):
if isVisible("buttons/battle"):
formation_handler()
back_occurence = 0 # Somehow this handles defeats and I'm not sure why
while True:
if isVisible("labels/tap_to_close", click=True, seconds=2):
click("buttons/back")
break
elif isVisible("buttons/next", click=True, retry=3, region=regions['bottom_buttons']):
logger.info(faction + ' win detected, moving to next floor')
wait(5)
back_occurence = 0
formation_handler()
elif isVisible("buttons/back"):
if back_occurence == 0:
back_occurence = 1
else: # Exit if this counter reaches 1
click("buttons/back")
break
wait(5)
else:
logger.info('Tower floor not found!')
break
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Towers pushed!\n')
# Loads a tower and keeps retrying until victory, repeating for the next stage infinitely
if mode == "push_tower":
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
first_run = True
logger.info('Pushing ' + tower.capitalize() + ' tower!\n')
clickXY(460, 1820, seconds=4)
click("labels/legend_trial", seconds=2)
factions = ["graveborn", "light", "mauler", "wilder"]
for faction in factions:
if faction == tower:
if isVisible("towers/"+faction.lower(), confidence=0.95, click=True, seconds=4, yrelative=-20):
if isVisible("towers/lvl", click=True, region=(15, 850, 1050, 800), seconds=3, yrelative=-50, grayscale=True):
formation_handler()
while True:
# click("buttons/abyss_lvl", seconds=5, suppress=True, grayscale=True, confidence=0.8)
# if first_run is True:
# auto_load_formation()
# first_run = False
click("buttons/battle", suppress=True, region=regions['bottom_buttons'])
click("buttons/retry", suppress=True, region=regions['bottom_buttons'])
if isVisible("buttons/next", click=True, seconds=4, region=regions['bottom_buttons']):
logger.info(faction.capitalize() + ' win detected, moving to next floor\n')
formation_handler()
click("buttons/battle", seconds=3, suppress=True)
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Towers pushed!\n')
# Retries Abyss stages, probably very outdated at this point
if mode == 'abyss':
victory_counter = 0
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
logger.info('Auto-retrying Trial of Abyss')
click('buttons/main_menu', region=regions['main_menu'], seconds=3)
if isVisible('buttons/trial_of_abyss', click=True):
pass
else:
click('buttons/event', region=regions['menu_activities'], seconds=3)
while not isVisible('events/abyss', region=regions['bottom_third']):
swipe(700, 1800, 250, 1800, 2000)
click('events/abyss', region=regions['bottom_third'])
click('buttons/abyss_entry', region=regions['bottom_third'])
while isVisible('labels/trial_of_abyss', click=True): # First click can claim loot so we loop it to make sure we're opening ToA
while True:
if not running.is_set():
running.wait() # wait until running is set
logger.info('Resuming')
click("buttons/abyss_lvl", seconds=0.2, suppress=True)
if isVisible("buttons/battle", seconds=0.2, click=True, region=regions['bottom_buttons']):
if victory_counter > 0 and victory_counter % 100 == 0:
logger.info(str(victory_counter) + ' attempts made')
victory_counter += 1
click("labels/tap_to_close", suppress=True, seconds=0.2, region=regions['bottom_buttons'])
if isVisible("buttons/next", seconds=0.2, click=True, region=regions['bottom_buttons']):
logger.info('Stage passed in ' + str(victory_counter) + ' attemps!')
victory_counter = 0
wait(2)
else:
logger.info('Something went wrong opening Trial of Abyss!')
recover()
# Runs all available DR attempts
if mode == "dream_realm":
logger.info('Using all Dream Realm attempts')
safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='open')
dr_counter = 0
clickXY(450, 1825, seconds=3)
click('buttons/dream_realm', region=regions['battle_modes'], seconds=3)
# 20 Attempts
for _ in range(19):
# Handle opening the Battle
if isVisible('buttons/battle', region=regions['bottom_buttons'], click=True, seconds=5): # Enter battle screen
# Purchase Gold Attempts if it pops up
if isVisible('buttons/confirm', click=True, region=regions['confirm_deny']):
click('buttons/battle', region=regions['bottom_buttons'], retry=2, seconds=5) # 2 retries or it catches the button on the next screen and breaks battle detection
# Start the Battle
logger.info('Starting Battle')
if isVisible('buttons/battle', region=regions['bottom_buttons'], click=True, seconds=5, retry=5):
# If button is still visible after pressing we're out of attempts
if isVisible('buttons/battle', region=regions['bottom_buttons']): # Start battle
logger.info('Out of attempts! Exiting..\n')
return
# When we haven't seen the x3 button three times in a row we can assume the battle is over
while dr_counter < 3:
if isVisible('buttons/skip_inverse', seconds=0, region=regions['x3_and_skip']):
click('buttons/skip_inverse', seconds=2, region=regions['x3_and_skip'])
click('buttons/confirm', seconds=3, region=regions['confirm_deny'], suppress=True)
logger.info('Skip available, skipping the fight')
dr_counter = 0
else:
dr_counter += 1
click('labels/tap_to_close', region=regions['bottom_buttons'], seconds=5, retry=10, confidence=0.8)
if isVisible('buttons/deny', click=True, seconds=3):
logger.info('Skipping formation sharing..')
click('labels/tap_to_close', region=regions['bottom_buttons'], seconds=5, suppress=True)
logger.info('Dream Realm Battle #' + str(_+1) + ' complete!')
dr_counter = 0
else:
logger.info('Battle button not found! (battle ' + str(_) + ')')
debug_screen('dr_battle_not_found')
if safe_open_and_close(name=inspect.currentframe().f_code.co_name, state='close'):
logger.info('Dream Realm attempts exhausted.\n')
# For pushing afk stages
if mode == 'afkstages':
timeout = 0
timeout_warned = False
if isVisible('buttons/records', region=regions['bottom_buttons'], seconds=0, retry=20):
# Change formation if we we beat the 2nd round or have defeat >10 times in a row
if load_formation is True or globals()['stage_defeats'] >= config.getint('PUSHING', 'defeat_limit'):
# More than 10 defeats in a row and a multiple of 10 (i.e load new formation on 10th/20th/30th etc defeat)
if globals()['stage_defeats'] >= 1 and globals()['stage_defeats'] % config.getint('PUSHING', 'defeat_limit') == 0:
globals()['formation'] = (globals()['stage_defeats'] / config.getint('PUSHING', 'defeat_limit')) + 1 # number of defeats / defeat_limit, plus 1 as we start on formation #1
logger.info(str(globals()['stage_defeats']) + ' defeats, trying next formation')
formation_handler(globals()['formation'])
if globals()['first_stage_won'] is True: # Manually select second round if we've won the first
wait() # To stop getting stuck if this buttons not pressed
clickXY(550, 1100)
elif load_formation is True:
formation_handler(globals()['formation'])
# If multis_first_victory we know we've won the first round, if not formation_swap it's not a multi so we jump to the second fight check
if isVisible('labels/multis_first_victory', seconds=0):
globals()['first_stage_won'] = True
if not isVisible('buttons/formation_swap', seconds=0):
globals()['first_stage_won'] = True
# Start Battle
click('buttons/battle', retry=5, region=regions['bottom_buttons'], seconds=0)
click('buttons/confirm', seconds=0, suppress=True)
# In a multi first stage always gives 'Continue' screen so we check for that for victory/defeat markers
if globals()['first_stage_won'] is False:
result_value = isVisible_array(['labels/defeat', 'labels/victory'], confidence=0.9)
# Loop until we see either the Victory or Defeat screen
while result_value == 'not_found':
timeout += 1
if timeout > 90: # If nothing at 30 seconds start clicking in case battery saver mode is active
if timeout_warned is False:
logger.info('Possibly stuck, checking if it\'s the energysaver screen..')
debug_screen('battle_stuck')
timeout_warned = True
click_location('neutral')
if timeout > 180: # Still nothing at 60 seconds? Quit as somethings gone wrong and record the screen for debugging
logger.info('Battle timeout error!')
debug_screen('battle_timeout')
sys.exit(2)
result_value = isVisible_array(['labels/defeat', 'labels/victory'], confidence=0.9)
wait()
timeout = 0 # Reset timer after result found
# Take actions for victory or defeat
if result_value == 'labels/defeat':
globals()['stage_defeats'] += 1
logger.info('Defeat #' + str(globals()['stage_defeats']) + '! Retrying')
clickXY(550, 1800, seconds=3)
blind_push('afkstages', load_formation=False)
elif result_value == 'labels/victory':
globals()['stage_defeats'] = 0
logger.info('First round won!')
clickXY(730, 1800, seconds=3)
blind_push('afkstages', load_formation=False)
# Handle second stage or single stage logic
if globals()['first_stage_won'] is True:
# Wait for battle to end with either Continue button or Back button
while not isVisible('buttons/continue_stages', region=regions['bottom_buttons']):
timeout += 1
if timeout > 30: # If nothing at 30 seconds start clicking in case battery saver mode is active
click_location('neutral')
if timeout > 60: # Still nothing at 60 seconds? Quit as somethings gone wrong
logger.info('Battle timeout error!')
break
if isVisible('buttons/back', region=regions['bottom_buttons'], seconds=0):
break
wait()
# Continue on second battle is always defeat
if isVisible('buttons/continue_stages', region=regions['bottom_buttons']):
globals()['stage_defeats'] += 1
logger.info('Defeat #' + str(globals()['stage_defeats']) + '! Retrying')
clickXY(730, 1800, seconds=3)
blind_push('afkstages', load_formation=False)
# If we see a Back button we're at the Stage Passed screen (or seriously lost)
if isVisible('buttons/back', region=regions['bottom_buttons']):
globals()['stage_defeats'] = 0 # Reset defeats
globals()['formation'] = 1 # Reset formation
logger.info('Victory! Stage passed\n')
clickXY(750, 1800, seconds=4)
globals()['first_stage_won'] = False
blind_push('afkstages', load_formation=True)
else:
logger.info('Something went wrong opening AFK Stages!')
recover()
def open_afk_stages(afkstages=True):
clickXY(100, 1800, seconds=4) # Open AFK Rewards
if afkstages is True: # Standard Stage
logger.info('Opening AFK Stages')
logger.info('Changing formations after ' + str(config.getint('PUSHING', 'defeat_limit')) + ' defeats\n')
clickXY(715, 1600, seconds=3) # Battle
clickXY(715, 1600, seconds=2) # Battle (again since first can claim afk rewards when its >1h)
else: # Talent Stage
logger.info('Opening Talent Stages\n')
clickXY(370, 1600, seconds=3) # Battle
clickXY(370, 1600, seconds=2) # Battle (again since first can claim afk rewards when its >1h)
def handle_charms():
# Handles navigating the charms screen
def go_back(exit_mode=False):
click('buttons/confirm', suppress=True)
click('buttons/back', suppress=True)
click('buttons/back', suppress=True)
if exit_mode is True:
click('buttons/back', suppress=True)
click('buttons/back2', suppress=True)
top_max_floor = False