AzurLaneAutoScript/alas.py

190 lines
6.5 KiB
Python
Raw Normal View History

import os
import re
import time
from datetime import datetime
2021-09-14 17:32:23 +08:00
import inflection
from cached_property import cached_property
from module.campaign.gems_farming import GemsFarming
from module.campaign.run import CampaignRun
2021-09-14 17:32:23 +08:00
from module.commission.commission import RewardCommission
from module.config.config import AzurLaneConfig, TaskEnd
from module.config.db import Database
from module.daily.daily import Daily
from module.device.device import Device
from module.dorm.dorm import RewardDorm
from module.exception import *
from module.exercise.exercise import Exercise
from module.guild.guild_reward import RewardGuild
from module.handler.login import LoginHandler
from module.handler.sensitive_info import handle_sensitive_image, handle_sensitive_logs
from module.hard.hard import CampaignHard
from module.logger import logger, log_file
from module.meowfficer.meowfficer import RewardMeowfficer
2021-09-14 17:32:23 +08:00
from module.research.research import RewardResearch
from module.reward.reward import Reward
2021-09-23 22:04:03 +08:00
from module.shop.shop_reward import RewardShop
from module.sos.sos import CampaignSos
2021-09-14 17:32:23 +08:00
from module.tactical.tactical_class import RewardTacticalClass
2021-09-25 00:38:53 +08:00
from module.os_ash.ash import AshBeaconAssist
2020-06-17 00:36:05 +08:00
class AzurLaneAutoScript:
2021-09-14 17:32:23 +08:00
def __init__(self, config_name='alas'):
self.config_name = config_name
Database().update_config(config_name)
@cached_property
def config(self):
config = AzurLaneConfig(config_name=self.config_name)
return config
@cached_property
def device(self):
device = Device(config=self.config)
return device
2020-03-29 01:22:46 +08:00
def run(self, command):
try:
self.__getattribute__(command)()
return True
except TaskEnd:
return True
except GameNotRunningError as e:
logger.warning(e)
self.config.task_call('Restart')
return True
except (GameStuckError, GameTooManyClickError) as e:
logger.warning(e)
self.save_error_log()
logger.warning(f'Game stuck, {self.config.Emulator_PackageName} will be restarted in 10 seconds')
logger.warning('If you are playing by hand, please stop Alas')
self.config.task_call('Restart')
self.device.sleep(10)
return False
except LogisticsRefreshBugHandler as e:
logger.warning(e)
self.save_error_log()
self.config.task_call('Restart')
self.device.sleep(10)
return False
except ScriptError as e:
logger.critical(e)
logger.critical('This is likely to be a mistake of developers, but sometimes just random issues')
exit(1)
except RequestHumanTakeover:
logger.critical('Request human takeover')
exit(1)
except Exception as e:
logger.exception(e)
self.save_error_log()
exit(1)
def save_error_log(self):
"""
Save last 60 screenshots in ./log/error/<timestamp>
Save logs to ./log/error/<timestamp>/log.txt
"""
if self.config.Error_SaveError:
folder = f'./log/error/{int(time.time() * 1000)}'
logger.warning(f'Saving error: {folder}')
os.mkdir(folder)
for data in self.device.screenshot_deque:
image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f')
image = handle_sensitive_image(data['image'])
image.save(f'{folder}/{image_time}.png')
with open(log_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
start = 0
for index, line in enumerate(lines):
if re.search('\+-{15,}\+', line):
start = index
lines = lines[start - 2:]
lines = handle_sensitive_logs(lines)
with open(f'{folder}/log.txt', 'w', encoding='utf-8') as f:
f.writelines(lines)
def restart(self):
LoginHandler(self.config, device=self.device).app_restart()
2021-09-14 17:32:23 +08:00
def research(self):
RewardResearch(config=self.config, device=self.device).run()
2021-09-14 17:32:23 +08:00
def commission(self):
RewardCommission(config=self.config, device=self.device).run()
2021-09-14 17:32:23 +08:00
def tactical(self):
RewardTacticalClass(config=self.config, device=self.device).run()
def dorm(self):
RewardDorm(config=self.config, device=self.device).run()
2021-09-23 22:04:03 +08:00
def meowfficer(self):
RewardMeowfficer(config=self.config, device=self.device).run()
def guild(self):
RewardGuild(config=self.config, device=self.device).run()
def reward(self):
Reward(config=self.config, device=self.device).run()
2021-09-23 22:04:03 +08:00
def shop(self):
RewardShop(config=self.config, device=self.device).run()
def daily(self):
Daily(config=self.config, device=self.device).run()
def hard(self):
CampaignHard(config=self.config, device=self.device).run()
def exercise(self):
Exercise(config=self.config, device=self.device).run()
def sos(self):
CampaignSos(config=self.config, device=self.device).run()
2021-09-25 00:38:53 +08:00
def opsi_ash(self):
AshBeaconAssist(config=self.config, device=self.device).run()
def main(self):
CampaignRun(config=self.config, device=self.device).run(
name=self.config.Campaign_Name,
folder=self.config.Campaign_Event,
mode=self.config.Campaign_Mode)
def gems_farming(self):
GemsFarming(config=self.config, device=self.device).run(
name=self.config.Campaign_Name,
folder=self.config.Campaign_Event,
mode=self.config.Campaign_Mode)
2021-09-14 17:32:23 +08:00
def loop(self):
2021-09-25 00:51:51 +08:00
is_first = True
2021-09-14 17:32:23 +08:00
while 1:
2021-09-25 00:51:51 +08:00
if is_first and self.config.task == 'Restart':
logger.info('Skip task `Restart` at scheduler start')
self.config.task_delay(server_update=True)
del self.__dict__['config']
2021-09-14 17:32:23 +08:00
logger.info(f'Scheduler: Start task `{self.config.task}`')
logger.hr(self.config.task, level=0)
2021-09-14 17:32:23 +08:00
success = self.run(inflection.underscore(self.config.task))
2021-09-14 17:32:23 +08:00
logger.info(f'Scheduler: End task `{self.config.task}`')
del self.__dict__['config']
2021-09-25 00:51:51 +08:00
is_first = False
if success:
continue
elif self.config.Error_HandleError:
# self.config.task_delay(success=False)
continue
else:
2021-09-14 17:32:23 +08:00
break
2021-04-17 22:20:21 +08:00
2021-09-14 17:32:23 +08:00
if __name__ == '__main__':
alas = AzurLaneAutoScript()
alas.loop()