This repository was archived by the owner on Feb 19, 2022. It is now read-only.
forked from Egoistically/ALAuto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALAuto.py
274 lines (248 loc) · 9.49 KB
/
ALAuto.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
import sys
import traceback
import argparse
from modules.combat import CombatModule
from modules.commission import CommissionModule
from modules.enhancement import EnhancementModule
from modules.mission import MissionModule
from modules.retirement import RetirementModule
from modules.headquarters import HeadquartersModule
from modules.event import EventModule
from datetime import datetime, timedelta
from util.adb import Adb
from util.updater import UpdateUtil
from util.config import Config
from util.logger import Logger
from util.stats import Stats
from util.utils import Utils, Region
class ALAuto(object):
modules = {
'updates': None,
'combat': None,
'commissions': None,
'enhancement': None,
'missions': None,
'retirement': None,
'headquarters': None,
'event': None
}
def __init__(self, config):
"""Initializes the primary azurlane-auto instance with the passed in
Config instance; creates the Stats instance and resets scheduled sleep
timers.
Args:
config (Config): azurlane-auto Config instance
"""
self.config = config
self.oil_limit = 0
self.stats = Stats(config)
if self.config.updates['enabled']:
self.modules['updates'] = UpdateUtil(self.config)
if self.config.combat['enabled']:
self.modules['combat'] = CombatModule(self.config, self.stats)
self.oil_limit = self.config.combat['oil_limit']
if self.config.commissions['enabled']:
self.modules['commissions'] = CommissionModule(
self.config, self.stats)
if self.config.enhancement['enabled']:
self.modules['enhancement'] = EnhancementModule(
self.config, self.stats)
if self.config.missions['enabled']:
self.modules['missions'] = MissionModule(self.config, self.stats)
if self.config.retirement['enabled']:
self.modules['retirement'] = RetirementModule(
self.config, self.stats)
if self.config.dorm['enabled'] or self.config.academy['enabled']:
self.modules['headquarters'] = HeadquartersModule(
self.config, self.stats)
if self.config.events['enabled']:
self.modules['event'] = EventModule(self.config, self.stats)
self.print_stats_check = True
self.next_combat = datetime.now()
def run_update_check(self):
if self.modules['updates']:
if self.modules['updates'].checkUpdate():
Logger.log_warning(
"A new release is available, please check the github.")
def should_sortie(self):
"""Method to check wether bot should combat or not.
"""
return self.modules['combat'] \
and script.next_combat != 0 \
and script.next_combat < datetime.now() \
and Utils.check_oil(self.oil_limit)
def run_sortie_cycle(self):
"""Method to run all cycles related to combat.
"""
self.run_event_cycle()
self.run_combat_cycle()
self.run_enhancement_cycle()
self.run_retirement_cycle()
def run_combat_cycle(self):
"""Method to run the combat cycle.
"""
if self.modules['combat']:
result = self.modules['combat'].combat_logic_wrapper()
if result == 1:
# if boss is defeated
Logger.log_msg(
"Boss successfully defeated, going back to menu.")
self.print_stats_check = True
if result == 2:
# if morale is too low
Logger.log_warning(
"Ships morale is too low, entering standby mode for an hour.")
self.next_combat = datetime.now() + timedelta(hours=1)
self.print_stats_check = False
if result == 3:
# if dock is full
Logger.log_warning("Dock is full, need to retire.")
if self.modules['retirement']:
self.modules['retirement'].retirement_logic_wrapper(True)
else:
Logger.log_error("Retirement isn't enabled, exiting.")
sys.exit()
if result == 4:
Logger.log_warning("Failed to defeat enemy.")
self.print_stats_check = False
else:
self.next_combat = 0
def run_commission_cycle(self):
"""Method to run the expedition cycle.
"""
if self.modules['commissions']:
self.modules['commissions'].commission_logic_wrapper()
def run_enhancement_cycle(self):
"""Method to run the enhancement cycle.
"""
if self.modules['enhancement']:
self.modules['enhancement'].enhancement_logic_wrapper()
def run_mission_cycle(self):
"""Method to run the mission cycle
"""
if self.modules['missions']:
self.modules['missions'].mission_logic_wrapper()
def run_retirement_cycle(self):
"""Method to run the retirement cycle
"""
if self.modules['retirement']:
self.modules['retirement'].retirement_logic_wrapper()
def run_hq_cycle(self):
"""Method to run the headquarters cycle.
"""
if self.modules['headquarters']:
self.modules['headquarters'].hq_logic_wrapper()
def run_event_cycle(self):
"""Method to run the event cycle
"""
if self.modules['event']:
self.modules['event'].event_logic_wrapper()
def print_cycle_stats(self):
"""Method to print the cycle stats"
"""
if self.print_stats_check:
self.stats.print_stats(Utils.check_oil(self.oil_limit))
self.print_stats_check = False
# check run-time args
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config',
metavar='CONFIG_FILE',
help='Use the specified configuration file instead ' +
'of the default config.ini')
parser.add_argument('-d', '--debug',
help='Enables debugging logs.', action='store_true')
parser.add_argument('-l', '--legacy',
help='Enables sed usage.', action='store_true')
args = parser.parse_args()
# check args, and if none provided, load default config
if args:
if args.config:
config = Config(args.config)
else:
config = Config('config.ini')
if args.debug:
Logger.log_info("Enabled debugging.")
Logger.enable_debugging(Logger)
if args.legacy:
Logger.log_info("Enabled sed usage.")
Adb.enable_legacy(Adb)
script = ALAuto(config)
script.run_update_check()
Adb.service = config.network['service']
Adb.device = '-d' if (Adb.service == 'PHONE') else '-e'
adb = Adb()
if adb.init():
Logger.log_msg('Successfully connected to the service.')
res = ['1920x1080', '1080x1920']
if Adb.exec_out('wm size').decode('utf-8').strip()[15:] not in res:
Logger.log_error("Resolution is not 1920x1080, please change it.")
sys.exit()
Utils.assets = config.assets['server']
else:
Logger.log_error('Unable to connect to the service.')
sys.exit()
try:
while True:
Utils.wait_update_screen(1)
# temporal solution to event alerts
if Utils.find("menu/azurlane"):
Logger.log_msg("Found Azurlane Screen")
for x in range(2):
Utils.touch_randomly(Region(700, 400, 100, 100))
Utils.script_sleep(1)
continue
if Utils.find("menu/announcement"):
Logger.log_msg("Found Announcement Window")
Utils.touch_randomly(Region(1790, 100, 60, 60))
Utils.script_sleep(1)
continue
if Utils.find("menu/item_found"):
Logger.log_msg("Found item message.")
Utils.find_and_touch("menu/tap_to_continue")
Utils.script_sleep(1)
continue
if Utils.find("menu/alert_info"):
Logger.log_msg("Found alert.")
Utils.find_and_touch("menu/alert_close")
Utils.script_sleep(1)
continue
if not Utils.find("menu/button_battle"):
Utils.touch_randomly(Region(54, 57, 67, 67))
Utils.script_sleep(1)
continue
if Utils.find("commission/alert_completed"):
script.run_commission_cycle()
script.print_cycle_stats()
continue
if Utils.find("mission/alert_completed"):
script.run_mission_cycle()
continue
if Utils.find("headquarters/hq_alert"):
script.run_hq_cycle()
continue
if script.should_sortie():
script.run_sortie_cycle()
script.print_cycle_stats()
continue
else:
Logger.log_msg("Nothing to do, will check again in a few minutes.")
Utils.script_sleep(60)
continue
except KeyboardInterrupt:
# handling ^C from user
Logger.log_msg("Received keyboard interrupt from user. Closing...")
# writing traceback to file
f = open("traceback.log", "w")
traceback.print_exc(None, f, True)
f.close()
script.stats.print_stats(0)
sys.exit(0)
except:
# registering whatever exception occurs during execution
Logger.log_error(
"An error occurred. For more info check the traceback.log file.")
# writing traceback to file
f = open("traceback.log", "w")
traceback.print_exc(None, f, True)
f.close()
sys.exit(1)