| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- # This Python file uses the following encoding: utf-8
- # @author runhey
- # github https://github.com/runhey
- import zerorpc
- import zmq
- import msgpack
- import random
- import re
- import cv2
- import time
- import os
- import inflection
- import asyncio
- from typing import Callable
- from datetime import datetime, timedelta
- from pathlib import Path
- from cached_property import cached_property
- from pydantic import BaseModel, ValidationError
- from PySide6.QtGui import QImage
- from threading import Thread
- from module.config.utils import convert_to_underscore
- from module.config.config import Config
- from module.device.device import Device
- from module.base.utils import load_module
- from module.base.decorator import del_cached_property
- from module.logger import logger
- from module.exception import *
- class Script:
- def __init__(self, config_name: str ='oas') -> None:
- logger.hr('Start', level=0)
- self.server = None
- self.gui_update_task: Callable = None # 回调函数, gui进程注册当每次config更新任务的时候更新gui的信息
- self.config_name = config_name
- # Skip first restart
- self.is_first_task = True
- # Failure count of tasks
- # Key: str, task name, value: int, failure count
- self.failure_record = {}
- # 运行loop的线程
- self.loop_thread: Thread = None
- @cached_property
- def config(self) -> "Config":
- try:
- from module.config.config import Config
- config = Config(config_name=self.config_name)
- return config
- except RequestHumanTakeover:
- logger.critical('Request human takeover')
- exit(1)
- except Exception as e:
- logger.exception(e)
- exit(1)
- @cached_property
- def device(self) -> "Device":
- try:
- from module.device.device import Device
- device = Device(config=self.config)
- return device
- except RequestHumanTakeover:
- logger.critical('Request human takeover')
- exit(1)
- except Exception as e:
- logger.exception(e)
- exit(1)
- @cached_property
- def checker(self):
- """
- 占位函数,在alas中是检查服务器是否正常的
- :return:
- """
- return None
- def save_error_log(self):
- """
- Save last 60 screenshots in ./log/error/<timestamp>
- Save logs to ./log/error/<timestamp>/log.txt
- """
- from module.base.utils import save_image
- from module.handler.sensitive_info import (handle_sensitive_image,
- handle_sensitive_logs)
- if self.config.script.error.save_error:
- if not os.path.exists('./log/error'):
- os.mkdir('./log/error')
- 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'])
- save_image(image, f'{folder}/{image_time}.png')
- with open(logger.log_file, 'r', encoding='utf-8') as f:
- lines = f.readlines()
- start = 0
- for index, line in enumerate(lines):
- line = line.strip(' \r\t\n')
- if re.match('^═{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 init_server(self, port: int) -> int:
- """
- 初始化zerorpc服务,返回端口号
- :return:
- """
- self.server = zerorpc.Server(self)
- try:
- self.server.bind(f'tcp://127.0.0.1:{port}')
- return port
- except zmq.error.ZMQError:
- logger.error(f"Ocr server cannot bind on port {port}")
- return None
- def run_server(self) -> None:
- """
- 启动zerorpc服务
- :return:
- """
- self.server.run()
- def gui_args(self, task: str) -> str:
- """
- 获取给gui显示的参数
- :return:
- """
- return self.config.gui_args(task=task)
- def gui_menu(self) -> str:
- """
- 获取给gui显示的菜单
- :return:
- """
- return self.config.gui_menu
- def gui_task(self, task: str) -> str:
- """
- 获取给gui显示的任务 的参数的具体值
- :return:
- """
- return self.config.model.gui_task(task=task)
- def gui_set_task(self, task: str, group: str, argument: str, value) -> bool:
- """
- 设置给gui显示的任务 的参数的具体值
- :return:
- """
- task = convert_to_underscore(task)
- group = convert_to_underscore(group)
- argument = convert_to_underscore(argument)
- path = f'{task}.{group}.{argument}'
- task_object = getattr(self.config.model, task, None)
- group_object = getattr(task_object, group, None)
- argument_object = getattr(group_object, argument, None)
- if argument_object is None:
- logger.error(f'gui_set_task {task}.{group}.{argument}.{value} failed')
- return False
- try:
- setattr(group_object, argument, value)
- argument_object = getattr(group_object, argument, None)
- logger.info(f'gui_set_task {task}.{group}.{argument}.{argument_object}')
- self.config.save() # 我是没有想到什么方法可以使得属性改变自动保存的
- return True
- except ValidationError as e:
- logger.error(e)
- return False
- @zerorpc.stream
- def gui_mirror_image(self):
- """
- 获取给gui显示的镜像
- :return: cv2的对象将 numpy 数组转换为字节串。接下来MsgPack 进行序列化发送方将图像数据转换为字节串
- """
- # return msgpack.packb(cv2.imencode('.jpg', self.device.screenshot())[1].tobytes())
- img = cv2.cvtColor(self.device.screenshot(), cv2.COLOR_RGB2BGR)
- ret, buffer = cv2.imencode('.jpg', img)
- yield buffer.tobytes()
- def _gui_update_tasks(self) -> None:
- """
- 获取更新任务后 pending waiting 的任务 和 当前的任务的数据。打包给gui显示
- :return:
- """
- data = {}
- pending = []
- waiting = []
- task = {}
- if self.config.task is not None and self.config.task.next_run < datetime.now():
- task["name"] = self.config.task.command
- task["next_run"] = str(self.config.task.next_run)
- data["task"] = task
- for p in self.config.pending_task[1:]:
- item = {"name": p.command, "next_run": str(p.next_run)}
- pending.append(item)
- for w in self.config.waiting_task:
- item = {"name": w.command, "next_run": str(w.next_run)}
- waiting.append(item)
- data["pending"] = pending
- data["waiting"] = waiting
- if self.gui_update_task is not None:
- self.gui_update_task(data)
- def _gui_set_status(self, status: str) -> None:
- """
- 设置给gui显示的状态
- :param status: 可以在gui中显示的状态 有 "Init", "Empty"(不显示), "Run"(运行中), "Error", "Free"(空闲)
- :return:
- """
- data = {"status": status}
- if self.gui_update_task is not None:
- self.gui_update_task(data)
- def wait_until(self, future):
- """
- Wait until a specific time.
- Args:
- future (datetime):
- Returns:
- bool: True if wait finished, False if config changed.
- """
- future = future + timedelta(seconds=1)
- self.config.start_watching()
- while 1:
- if datetime.now() > future:
- return True
- # if self.stop_event is not None:
- # if self.stop_event.is_set():
- # logger.info("Update event detected")
- # logger.info(f"[{self.config_name}] exited. Reason: Update")
- # exit(0)
- time.sleep(5)
- if self.config.should_reload():
- return False
- def get_next_task(self) -> str:
- """
- 获取下一个任务的名字, 大驼峰。
- :return:
- """
- while 1:
- task = self.config.get_next()
- self.config.task = task
- self._gui_update_tasks()
- # from module.base.resource import release_resources
- # if self.config.task.command != 'Alas':
- # release_resources(next_task=task.command)
- if task.next_run > datetime.now():
- logger.info(f'Wait until {task.next_run} for task `{task.command}`')
- # self.is_first_task = False
- method = self.config.script.optimization.when_task_queue_empty
- if method == 'close_game':
- logger.info('Close game during wait')
- self.device.app_stop()
- self.device.release_during_wait()
- if not self.wait_until(task.next_run):
- del_cached_property(self, 'config')
- continue
- self.run('start')
- elif method == 'goto_main':
- logger.info('Goto main page during wait')
- self.run('goto_main')
- self.device.release_during_wait()
- if not self.wait_until(task.next_run):
- del_cached_property(self, 'config')
- continue
- else:
- logger.warning(f'Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there')
- self.device.release_during_wait()
- if not self.wait_until(task.next_run):
- del_cached_property(self, 'config')
- continue
- break
- return task.command
- def run(self, command: str) -> bool:
- """
- :param command:
- :return:
- """
- if command == 'start' and command == 'goto_main':
- logger.error(f'Invalid command `{command}`')
- try:
- self.device.screenshot()
- module_name = 'script_task'
- module_path = str(Path.cwd() / 'tasks' / command / (module_name+'.py'))
- logger.info(f'module_path: {module_path}, module_name: {module_name}')
- task_module = load_module(module_name, module_path)
- task_module.ScriptTask(config=self.config, device=self.device).run()
- except TaskEnd:
- return True
- except GameNotRunningError as e:
- logger.warning(e)
- self.config.task_call('Restart')
- return True
- except (GameStuckError, GameTooManyClickError) as e:
- logger.error(e)
- self.save_error_log()
- logger.warning(f'Game stuck, {self.device.package} 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 GameBugError as e:
- logger.warning(e)
- self.save_error_log()
- logger.warning('An error has occurred in Azur Lane game client, Alas is unable to handle')
- logger.warning(f'Restarting {self.device.package} to fix it')
- self.config.task_call('Restart')
- self.device.sleep(10)
- return False
- except GamePageUnknownError:
- logger.info('Game server may be under maintenance or network may be broken, check server status now')
- self.checker.check_now()
- if self.checker.is_available():
- logger.critical('Game page unknown')
- self.save_error_log()
- exit(1)
- else:
- self.checker.wait_until_available()
- 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 loop(self):
- """
- Main loop of scheduler.
- :return:
- """
- logger.set_file_logger(self.config_name)
- logger.info(f'Start scheduler loop: {self.config_name}')
- while 1:
- # Check update event from GUI
- # if self.stop_event is not None:
- # if self.stop_event.is_set():
- # logger.info("Update event detected")
- # logger.info(f"Alas [{self.config_name}] exited.")
- # break
- # Check game server maintenance
- # self.checker.wait_until_available()
- # if self.checker.is_recovered():
- # # There is an accidental bug hard to reproduce
- # # Sometimes, config won't be updated due to blocking
- # # even though it has been changed
- # # So update it once recovered
- # del_cached_property(self, 'config')
- # logger.info('Server or network is recovered. Restart game client')
- # self.config.task_call('Restart')
- # Get task
- task = self.get_next_task()
- # 更新 gui的任务
- # Init device and change server
- _ = self.device
- # Skip first restart
- if self.is_first_task and task == 'Restart':
- logger.info('Skip task `Restart` at scheduler start')
- self.config.task_delay(server_update=True)
- del_cached_property(self, 'config')
- continue
- # Run
- logger.info(f'Scheduler: Start task `{task}`')
- self.device.stuck_record_clear()
- self.device.click_record_clear()
- logger.hr(task, level=0)
- success = self.run(inflection.camelize(task))
- logger.info(f'Scheduler: End task `{task}`')
- self.is_first_task = False
- # Check failures
- # failed = deep_get(self.failure_record, keys=task, default=0)
- failed = self.failure_record[task] if task in self.failure_record else 0
- failed = 0 if success else failed + 1
- # deep_set(self.failure_record, keys=task, value=failed)
- self.failure_record[task] = failed
- if failed >= 3:
- logger.critical(f"Task `{task}` failed 3 or more times.")
- logger.critical("Possible reason #1: You haven't used it correctly. "
- "Please read the help text of the options.")
- logger.critical("Possible reason #2: There is a problem with this task. "
- "Please contact developers or try to fix it yourself.")
- logger.critical('Request human takeover')
- exit(1)
- if success:
- del_cached_property(self, 'config')
- continue
- elif self.config.error.handle.error:
- # self.config.task_delay(success=False)
- del_cached_property(self, 'config')
- self.checker.check_now()
- continue
- else:
- break
- def start_loop(self) -> None:
- """
- 创建一个线程,运行loop
- :return:
- """
- if self.loop_thread is None:
- self.loop_thread = Thread(target=self.loop, name='Script_loop')
- self.loop_thread.start()
- if __name__ == "__main__":
- script = Script("oas1")
- # print(script.get_next_task())
- script.run("AreaBoss")
- # script.run('RealmRaid')
|