script.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import zerorpc
  5. import zmq
  6. import msgpack
  7. import random
  8. import re
  9. import cv2
  10. import time
  11. import os
  12. import inflection
  13. import asyncio
  14. import json
  15. from typing import Callable
  16. from datetime import datetime, timedelta
  17. from pathlib import Path
  18. from cached_property import cached_property
  19. from pydantic import BaseModel, ValidationError
  20. from threading import Thread
  21. from multiprocessing.queues import Queue
  22. from module.config.utils import convert_to_underscore
  23. from module.config.config import Config
  24. from module.config.config_model import ConfigModel
  25. from module.device.device import Device
  26. from module.base.utils import load_module
  27. from module.base.decorator import del_cached_property
  28. from module.logger import logger
  29. from module.exception import *
  30. from module.server.i18n import I18n
  31. from module.device.platform2.platform_windows import minimize_by_name,show_window_by_name
  32. class Script:
  33. def __init__(self, config_name: str ='oas') -> None:
  34. logger.hr('Start', level=0)
  35. self.server = None
  36. self.state_queue: Queue = None
  37. self.gui_update_task: Callable = None # 回调函数, gui进程注册当每次config更新任务的时候更新gui的信息
  38. self.config_name = config_name
  39. # Skip first restart
  40. self.is_first_task = True
  41. # Failure count of tasks
  42. # Key: str, task name, value: int, failure count
  43. self.failure_record = {}
  44. # 运行loop的线程
  45. self.loop_thread: Thread = None
  46. @cached_property
  47. def config(self) -> "Config":
  48. try:
  49. from module.config.config import Config
  50. config = Config(config_name=self.config_name)
  51. return config
  52. except RequestHumanTakeover:
  53. logger.critical('Request human takeover')
  54. exit(1)
  55. except Exception as e:
  56. logger.exception(e)
  57. exit(1)
  58. @cached_property
  59. def device(self) -> "Device":
  60. try:
  61. from module.device.device import Device
  62. device = Device(config=self.config)
  63. return device
  64. except RequestHumanTakeover:
  65. logger.critical('Request human takeover')
  66. exit(1)
  67. except Exception as e:
  68. logger.exception(e)
  69. exit(1)
  70. @cached_property
  71. def checker(self):
  72. """
  73. 占位函数,在alas中是检查服务器是否正常的
  74. :return:
  75. """
  76. return None
  77. def save_error_log(self):
  78. """
  79. Save last 60 screenshots in ./log/error/<timestamp>
  80. Save logs to ./log/error/<timestamp>/log.txt
  81. """
  82. from module.base.utils import save_image
  83. from module.handler.sensitive_info import (handle_sensitive_image,
  84. handle_sensitive_logs)
  85. if self.config.script.error.save_error:
  86. if not os.path.exists('./log/error'):
  87. os.mkdir('./log/error')
  88. folder = f'./log/error/{int(time.time() * 1000)}'
  89. logger.warning(f'Saving error: {folder}')
  90. os.mkdir(folder)
  91. for data in self.device.screenshot_deque:
  92. image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f')
  93. image = handle_sensitive_image(data['image'])
  94. save_image(image, f'{folder}/{image_time}.png')
  95. with open(logger.log_file, 'r', encoding='utf-8') as f:
  96. lines = f.readlines()
  97. start = 0
  98. for index, line in enumerate(lines):
  99. line = line.strip(' \r\t\n')
  100. if re.match('^═{15,}$', line):
  101. start = index
  102. lines = lines[start - 2:]
  103. lines = handle_sensitive_logs(lines)
  104. with open(f'{folder}/log.txt', 'w', encoding='utf-8') as f:
  105. f.writelines(lines)
  106. def init_server(self, port: int) -> int:
  107. """
  108. 初始化zerorpc服务,返回端口号
  109. :return:
  110. """
  111. self.server = zerorpc.Server(self)
  112. try:
  113. self.server.bind(f'tcp://127.0.0.1:{port}')
  114. return port
  115. except zmq.error.ZMQError:
  116. logger.error(f"Ocr server cannot bind on port {port}")
  117. return None
  118. def run_server(self) -> None:
  119. """
  120. 启动zerorpc服务
  121. :return:
  122. """
  123. self.server.run()
  124. def gui_args(self, task: str) -> str:
  125. """
  126. 获取给gui显示的参数
  127. :return:
  128. """
  129. return self.config.gui_args(task=task)
  130. def gui_menu(self) -> str:
  131. """
  132. 获取给gui显示的菜单
  133. :return:
  134. """
  135. return self.config.gui_menu
  136. def gui_task(self, task: str) -> str:
  137. """
  138. 获取给gui显示的任务 的参数的具体值
  139. :return:
  140. """
  141. return self.config.model.gui_task(task=task)
  142. def gui_set_task(self, task: str, group: str, argument: str, value) -> bool:
  143. """
  144. 设置给gui显示的任务 的参数的具体值
  145. :return:
  146. """
  147. # 验证参数
  148. task = convert_to_underscore(task)
  149. group = convert_to_underscore(group)
  150. argument = convert_to_underscore(argument)
  151. # pandtic验证
  152. if isinstance(value, str):
  153. if len(value) == 8:
  154. try:
  155. value = datetime.strptime(value, '%H:%M:%S').time()
  156. except ValueError:
  157. pass
  158. path = f'{task}.{group}.{argument}'
  159. task_object = getattr(self.config.model, task, None)
  160. group_object = getattr(task_object, group, None)
  161. argument_object = getattr(group_object, argument, None)
  162. if argument_object is None:
  163. logger.error(f'Set arg {task}.{group}.{argument}.{value} failed')
  164. return False
  165. try:
  166. setattr(group_object, argument, value)
  167. argument_object = getattr(group_object, argument, None)
  168. logger.info(f'Set arg {task}.{group}.{argument}.{argument_object}')
  169. self.config.save() # 我是没有想到什么方法可以使得属性改变自动保存的
  170. return True
  171. except ValidationError as e:
  172. logger.error(e)
  173. return False
  174. @zerorpc.stream
  175. def gui_mirror_image(self):
  176. """
  177. 获取给gui显示的镜像
  178. :return: cv2的对象将 numpy 数组转换为字节串。接下来MsgPack 进行序列化发送方将图像数据转换为字节串
  179. """
  180. # return msgpack.packb(cv2.imencode('.jpg', self.device.screenshot())[1].tobytes())
  181. img = cv2.cvtColor(self.device.screenshot(), cv2.COLOR_RGB2BGR)
  182. self.device.stuck_record_clear()
  183. ret, buffer = cv2.imencode('.jpg', img)
  184. yield buffer.tobytes()
  185. def _gui_update_tasks(self) -> None:
  186. """
  187. 获取更新任务后 pending waiting 的任务 和 当前的任务的数据。打包给gui显示
  188. :return:
  189. """
  190. data = {}
  191. pending = []
  192. waiting = []
  193. task = {}
  194. if self.config.task is not None and self.config.task.next_run < datetime.now():
  195. task["name"] = self.config.task.command
  196. task["next_run"] = str(self.config.task.next_run)
  197. data["task"] = task
  198. for p in self.config.pending_task[1:]:
  199. item = {"name": p.command, "next_run": str(p.next_run)}
  200. pending.append(item)
  201. for w in self.config.waiting_task:
  202. item = {"name": w.command, "next_run": str(w.next_run)}
  203. waiting.append(item)
  204. data["pending"] = pending
  205. data["waiting"] = waiting
  206. if self.gui_update_task is not None:
  207. self.gui_update_task(data)
  208. def _gui_set_status(self, status: str) -> None:
  209. """
  210. 设置给gui显示的状态
  211. :param status: 可以在gui中显示的状态 有 "Init", "Empty"(不显示), "Run"(运行中), "Error", "Free"(空闲)
  212. :return:
  213. """
  214. data = {"status": status}
  215. if self.gui_update_task is not None:
  216. self.gui_update_task(data)
  217. def gui_task_list(self) -> str:
  218. """
  219. 获取给gui显示的任务列表
  220. :return:
  221. """
  222. result = {}
  223. for key, value in self.config.model.dict().items():
  224. if isinstance(value, str):
  225. continue
  226. if key == "restart":
  227. continue
  228. if "scheduler" not in value:
  229. continue
  230. scheduler = value["scheduler"]
  231. item = {"enable": scheduler["enable"],
  232. "next_run": str(scheduler["next_run"])}
  233. key = self.config.model.type(key)
  234. result[key] = item
  235. return json.dumps(result)
  236. def wait_until(self, future):
  237. """
  238. Wait until a specific time.
  239. Args:
  240. future (datetime):
  241. Returns:
  242. bool: True if wait finished, False if config changed.
  243. """
  244. future = future + timedelta(seconds=1)
  245. self.config.start_watching()
  246. while 1:
  247. if datetime.now() > future:
  248. return True
  249. # if self.stop_event is not None:
  250. # if self.stop_event.is_set():
  251. # logger.info("Update event detected")
  252. # logger.info(f"[{self.config_name}] exited. Reason: Update")
  253. # exit(0)
  254. time.sleep(5)
  255. if self.config.should_reload():
  256. return False
  257. def get_next_task(self) -> str:
  258. """
  259. 获取下一个任务的名字, 大驼峰。
  260. :return:
  261. """
  262. while 1:
  263. task = self.config.get_next()
  264. self.config.task = task
  265. if self.state_queue:
  266. self.state_queue.put({"schedule": self.config.get_schedule_data()})
  267. # from module.base.resource import release_resources
  268. # if self.config.task.command != 'Alas':
  269. # release_resources(next_task=task.command)
  270. if task.next_run > datetime.now():
  271. logger.info(f'Wait until {task.next_run} for task `{task.command}`')
  272. # self.is_first_task = False
  273. method = self.config.script.optimization.when_task_queue_empty
  274. if method == 'close_game':
  275. logger.info('Close game during wait')
  276. self.device.app_stop()
  277. self.device.release_during_wait()
  278. if not self.wait_until(task.next_run):
  279. del_cached_property(self, 'config')
  280. continue
  281. self.run('Restart')
  282. elif method == 'goto_main':
  283. logger.info('Goto main page during wait')
  284. self.run('GotoMain')
  285. self.device.release_during_wait()
  286. if not self.wait_until(task.next_run):
  287. del_cached_property(self, 'config')
  288. continue
  289. else:
  290. logger.warning(f'Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there')
  291. self.device.release_during_wait()
  292. if not self.wait_until(task.next_run):
  293. del_cached_property(self, 'config')
  294. continue
  295. break
  296. return task.command
  297. def run(self, command: str) -> bool:
  298. """
  299. :param command: 大写驼峰命名的任务名字
  300. :return:
  301. """
  302. if command == 'start' or command == 'goto_main':
  303. logger.error(f'Invalid command `{command}`')
  304. try:
  305. self.device.screenshot()
  306. module_name = 'script_task'
  307. module_path = str(Path.cwd() / 'tasks' / command / (module_name+'.py'))
  308. logger.info(f'module_path: {module_path}, module_name: {module_name}')
  309. task_module = load_module(module_name, module_path)
  310. task_module.ScriptTask(config=self.config, device=self.device).run()
  311. except TaskEnd:
  312. return True
  313. except GameNotRunningError as e:
  314. logger.warning(e)
  315. self.config.task_call('Restart')
  316. return True
  317. except (GameStuckError, GameTooManyClickError) as e:
  318. logger.error(e)
  319. self.save_error_log()
  320. logger.warning(f'Game stuck, {self.device.package} will be restarted in 10 seconds')
  321. logger.warning('If you are playing by hand, please stop Alas')
  322. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GameStuckError or GameTooManyClickError")
  323. self.config.task_call('Restart')
  324. self.device.sleep(10)
  325. return False
  326. except GameBugError as e:
  327. logger.warning(e)
  328. self.save_error_log()
  329. logger.warning('An error has occurred in Azur Lane game client, Alas is unable to handle')
  330. logger.warning(f'Restarting {self.device.package} to fix it')
  331. self.config.task_call('Restart')
  332. self.device.sleep(10)
  333. return False
  334. except GamePageUnknownError:
  335. logger.info('Game server may be under maintenance or network may be broken, check server status now')
  336. # 这个还不重要 留着坑填
  337. logger.critical('Game page unknown')
  338. self.save_error_log()
  339. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GamePageUnknownError")
  340. return False
  341. except ScriptError as e:
  342. logger.critical(e)
  343. logger.critical('This is likely to be a mistake of developers, but sometimes just random issues')
  344. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> ScriptError")
  345. exit(1)
  346. except RequestHumanTakeover as e:
  347. logger.critical(e)
  348. logger.critical('Request human takeover')
  349. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> RequestHumanTakeover")
  350. exit(1)
  351. except Exception as e:
  352. logger.exception(e)
  353. self.save_error_log()
  354. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> Exception occured")
  355. exit(1)
  356. def loop(self):
  357. """
  358. Main loop of scheduler.
  359. :return:
  360. """
  361. logger.set_file_logger(self.config_name)
  362. logger.info(f'Start scheduler loop: {self.config_name}')
  363. # Update GUI 防呆, 读取设置并立刻显示后台模拟器到前台
  364. if not self.config.script.device.run_background_only:
  365. target_window_name = self.config.script.device.handle # 在这里输入你的具体窗口名称
  366. if self.config.script.device.emulator_window_minimize:
  367. minimize_by_name(target_window_name)
  368. logger.info(f'重新显示: {target_window_name}')
  369. else:
  370. show_window_by_name(target_window_name)
  371. while 1:
  372. # Check update event from GUI
  373. # if self.stop_event is not None:
  374. # if self.stop_event.is_set():
  375. # logger.info("Update event detected")
  376. # logger.info(f"Alas [{self.config_name}] exited.")
  377. # break
  378. # Check game server maintenance
  379. # self.checker.wait_until_available()
  380. # if self.checker.is_recovered():
  381. # # There is an accidental bug hard to reproduce
  382. # # Sometimes, config won't be updated due to blocking
  383. # # even though it has been changed
  384. # # So update it once recovered
  385. # del_cached_property(self, 'config')
  386. # logger.info('Server or network is recovered. Restart game client')
  387. # self.config.task_call('Restart')
  388. # Get task
  389. task = self.get_next_task()
  390. # 更新 gui的任务
  391. # Init device and change server
  392. _ = self.device
  393. # Skip first restart
  394. if self.is_first_task and task == 'Restart':
  395. logger.info('Skip task `Restart` at scheduler start')
  396. self.config.task_delay(task='Restart', success=True, server=True)
  397. del_cached_property(self, 'config')
  398. continue
  399. # Run
  400. logger.info(f'Scheduler: Start task `{task}`')
  401. self.device.stuck_record_clear()
  402. self.device.click_record_clear()
  403. logger.hr(task, level=0)
  404. success = self.run(inflection.camelize(task))
  405. logger.info(f'Scheduler: End task `{task}`')
  406. self.is_first_task = False
  407. # Check failures
  408. # failed = deep_get(self.failure_record, keys=task, default=0)
  409. failed = self.failure_record[task] if task in self.failure_record else 0
  410. failed = 0 if success else failed + 1
  411. # deep_set(self.failure_record, keys=task, value=failed)
  412. self.failure_record[task] = failed
  413. if failed >= 3:
  414. logger.critical(f"Task `{task}` failed 3 or more times.")
  415. logger.critical("Possible reason #1: You haven't used it correctly. "
  416. "Please read the help text of the options.")
  417. logger.critical("Possible reason #2: There is a problem with this task. "
  418. "Please contact developers or try to fix it yourself.")
  419. logger.critical('Request human takeover')
  420. exit(1)
  421. if success:
  422. del_cached_property(self, 'config')
  423. continue
  424. elif self.config.script.error.handle_error:
  425. # self.config.task_delay(success=False)
  426. del_cached_property(self, 'config')
  427. # self.checker.check_now()
  428. continue
  429. else:
  430. break
  431. def start_loop(self) -> None:
  432. """
  433. 创建一个线程,运行loop
  434. :return:
  435. """
  436. if self.loop_thread is None:
  437. self.loop_thread = Thread(target=self.loop, name='Script_loop')
  438. self.loop_thread.start()
  439. if __name__ == "__main__":
  440. script = Script("oas1")
  441. print(script.gui_task_list())
  442. print(script.config.gui_menu)