script.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 datetime import date
  16. import threading
  17. from typing import Callable
  18. from datetime import datetime, timedelta
  19. from pathlib import Path
  20. from cached_property import cached_property
  21. from pydantic import BaseModel, ValidationError
  22. from threading import Thread
  23. from multiprocessing.queues import Queue
  24. from module.config.utils import convert_to_underscore
  25. from module.config.config import Config
  26. from module.config.config_model import ConfigModel
  27. from module.device.device import Device
  28. from module.device.env import IS_WINDOWS
  29. from module.base.utils import load_module
  30. from module.base.decorator import del_cached_property
  31. from module.logger import logger
  32. from module.exception import *
  33. from module.server.i18n import I18n
  34. from module.ocr.rpc import ensure_ocr_server_started
  35. _log_switch_lock = threading.Lock()#线程锁
  36. class Script:
  37. def __init__(self, config_name: str ='oas') -> None:
  38. logger.hr('Start', level=0)
  39. self.server = None
  40. self.state_queue: Queue = None
  41. self._emulator_down = False
  42. self.gui_update_task: Callable = None # 回调函数, gui进程注册当每次config更新任务的时候更新gui的信息
  43. self.config_name = config_name
  44. # Skip first restart
  45. self.is_first_task = True
  46. # Failure count of tasks
  47. # Key: str, task name, value: int, failure count
  48. self.failure_record = {}
  49. # 运行loop的线程
  50. self.loop_thread: Thread = None
  51. @cached_property
  52. def config(self) -> "Config":
  53. try:
  54. from module.config.config import Config
  55. config = Config(config_name=self.config_name)
  56. return config
  57. except RequestHumanTakeover:
  58. logger.critical('Request human takeover')
  59. exit(1)
  60. except Exception as e:
  61. logger.exception(e)
  62. exit(1)
  63. @cached_property
  64. def device(self) -> "Device":
  65. try:
  66. from module.device.device import Device
  67. device = Device(config=self.config)
  68. return device
  69. except RequestHumanTakeover:
  70. logger.critical('Request human takeover')
  71. exit(1)
  72. except Exception as e:
  73. logger.exception(e)
  74. exit(1)
  75. @cached_property
  76. def checker(self):
  77. """
  78. 占位函数,在alas中是检查服务器是否正常的
  79. :return:
  80. """
  81. return None
  82. def save_error_log(self):
  83. """
  84. Save last 60 screenshots in ./log/error/<timestamp>
  85. Save logs to ./log/error/<timestamp>/log.txt
  86. """
  87. from module.base.utils import save_image
  88. from module.handler.sensitive_info import (handle_sensitive_image,
  89. handle_sensitive_logs)
  90. if self.config.script.error.save_error:
  91. if not os.path.exists('./log/error'):
  92. os.mkdir('./log/error')
  93. folder_name = str(int(time.time() * 1000))
  94. folder = f'./log/error/{folder_name}'
  95. logger.warning(f'Saving error: {folder}')
  96. logger.info('保存详细错误的日志和截图到路径:')
  97. logger.info(f'{str( Path.cwd() / "log" / "error" / folder_name)}')
  98. os.mkdir(folder)
  99. for data in self.device.screenshot_deque:
  100. image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f')
  101. image = handle_sensitive_image(data['image'])
  102. save_image(image, f'{folder}/{image_time}.png')
  103. with open(logger.log_file, 'r', encoding='utf-8') as f:
  104. lines = f.readlines()
  105. start = 0
  106. for index, line in enumerate(lines):
  107. line = line.strip(' \r\t\n')
  108. if re.match('^═{15,}$', line):
  109. start = index
  110. lines = lines[start - 2:]
  111. lines = handle_sensitive_logs(lines)
  112. with open(f'{folder}/log.txt', 'w', encoding='utf-8') as f:
  113. f.writelines(lines)
  114. def init_server(self, port: int) -> int:
  115. """
  116. 初始化zerorpc服务,返回端口号
  117. :return:
  118. """
  119. self.server = zerorpc.Server(self)
  120. try:
  121. self.server.bind(f'tcp://127.0.0.1:{port}')
  122. return port
  123. except zmq.error.ZMQError:
  124. logger.error(f"Ocr server cannot bind on port {port}")
  125. return None
  126. def run_server(self) -> None:
  127. """
  128. 启动zerorpc服务
  129. :return:
  130. """
  131. self.server.run()
  132. def gui_args(self, task: str) -> str:
  133. """
  134. 获取给gui显示的参数
  135. :return:
  136. """
  137. return self.config.gui_args(task=task)
  138. def gui_menu(self) -> str:
  139. """
  140. 获取给gui显示的菜单
  141. :return:
  142. """
  143. return self.config.gui_menu
  144. def gui_task(self, task: str) -> str:
  145. """
  146. 获取给gui显示的任务 的参数的具体值
  147. :return:
  148. """
  149. return self.config.model.gui_task(task=task)
  150. def gui_set_task(self, task: str, group: str, argument: str, value) -> bool:
  151. """
  152. 设置给gui显示的任务 的参数的具体值
  153. :return:
  154. """
  155. # 验证参数
  156. task = convert_to_underscore(task)
  157. group = convert_to_underscore(group)
  158. argument = convert_to_underscore(argument)
  159. # pandtic验证
  160. if isinstance(value, str):
  161. if len(value) == 8:
  162. try:
  163. value = datetime.strptime(value, '%H:%M:%S').time()
  164. except ValueError:
  165. pass
  166. path = f'{task}.{group}.{argument}'
  167. task_object = getattr(self.config.model, task, None)
  168. group_object = getattr(task_object, group, None)
  169. argument_object = getattr(group_object, argument, None)
  170. if argument_object is None:
  171. logger.error(f'Set arg {task}.{group}.{argument}.{value} failed')
  172. return False
  173. try:
  174. setattr(group_object, argument, value)
  175. argument_object = getattr(group_object, argument, None)
  176. logger.info(f'Set arg {task}.{group}.{argument}.{argument_object}')
  177. self.config.save() # 我是没有想到什么方法可以使得属性改变自动保存的
  178. return True
  179. except ValidationError as e:
  180. logger.error(e)
  181. return False
  182. @zerorpc.stream
  183. def gui_mirror_image(self):
  184. """
  185. 获取给gui显示的镜像
  186. :return: cv2的对象将 numpy 数组转换为字节串。接下来MsgPack 进行序列化发送方将图像数据转换为字节串
  187. """
  188. # return msgpack.packb(cv2.imencode('.jpg', self.device.screenshot())[1].tobytes())
  189. img = cv2.cvtColor(self.device.screenshot(), cv2.COLOR_RGB2BGR)
  190. self.device.stuck_record_clear()
  191. ret, buffer = cv2.imencode('.jpg', img)
  192. yield buffer.tobytes()
  193. def _gui_update_tasks(self) -> None:
  194. """
  195. 获取更新任务后 pending waiting 的任务 和 当前的任务的数据。打包给gui显示
  196. :return:
  197. """
  198. data = {}
  199. pending = []
  200. waiting = []
  201. task = {}
  202. if self.config.task is not None and self.config.task.next_run < datetime.now():
  203. task["name"] = self.config.task.command
  204. task["next_run"] = str(self.config.task.next_run)
  205. data["task"] = task
  206. for p in self.config.pending_task[1:]:
  207. item = {"name": p.command, "next_run": str(p.next_run)}
  208. pending.append(item)
  209. for w in self.config.waiting_task:
  210. item = {"name": w.command, "next_run": str(w.next_run)}
  211. waiting.append(item)
  212. data["pending"] = pending
  213. data["waiting"] = waiting
  214. if self.gui_update_task is not None:
  215. self.gui_update_task(data)
  216. def _gui_set_status(self, status: str) -> None:
  217. """
  218. 设置给gui显示的状态
  219. :param status: 可以在gui中显示的状态 有 "Init", "Empty"(不显示), "Run"(运行中), "Error", "Free"(空闲)
  220. :return:
  221. """
  222. data = {"status": status}
  223. if self.gui_update_task is not None:
  224. self.gui_update_task(data)
  225. def gui_task_list(self) -> str:
  226. """
  227. 获取给gui显示的任务列表
  228. :return:
  229. """
  230. result = {}
  231. for key, value in self.config.model.dict().items():
  232. if isinstance(value, str):
  233. continue
  234. if key == "restart":
  235. continue
  236. if "scheduler" not in value:
  237. continue
  238. scheduler = value["scheduler"]
  239. item = {"enable": scheduler["enable"],
  240. "next_run": str(scheduler["next_run"])}
  241. key = self.config.model.type(key)
  242. result[key] = item
  243. return json.dumps(result)
  244. def wait_until(self, future):
  245. """
  246. Wait until a specific time.
  247. Args:
  248. future (datetime):
  249. Returns:
  250. bool: True if wait finished, False if config changed.
  251. """
  252. future = future + timedelta(seconds=1)
  253. self.config.start_watching()
  254. while 1:
  255. if datetime.now() > future:
  256. return True
  257. # if self.stop_event is not None:
  258. # if self.stop_event.is_set():
  259. # logger.info("Update event detected")
  260. # logger.info(f"[{self.config_name}] exited. Reason: Update")
  261. # exit(0)
  262. time.sleep(5)
  263. if self.config.should_reload():
  264. return False
  265. def get_next_task(self) -> str:
  266. """
  267. 获取下一个任务的名字, 大驼峰。
  268. :return:
  269. """
  270. while True:
  271. task = self.config.get_next()
  272. self.config.task = task
  273. if self.state_queue:
  274. self.state_queue.put({"schedule": self.config.get_schedule_data()})
  275. now = datetime.now()
  276. # 任务时间到了返回任务名称
  277. if task.next_run <= now:
  278. return task.command
  279. # 根据策略执行等待逻辑
  280. if not self._handle_wait_during_idle(task.next_run):
  281. # 若等待被打断, 则刷新配置
  282. del_cached_property(self, "config")
  283. def _handle_wait_during_idle(self, next_run: datetime) -> bool:
  284. """
  285. 处理任务空闲期间的行为策略
  286. :param next_run: 下一个任务的时间
  287. :return: True 表示等待成功完成, False 表示等待被中断
  288. """
  289. method = self.config.script.optimization.when_task_queue_empty
  290. close_game_limit_time = self.config.script.optimization.close_game_limit_time
  291. close_emulator_limit_time = self.config.script.optimization.close_emulator_limit_time
  292. strategy_map = {
  293. "close_game": self._wait_close_game,
  294. "goto_main": self._wait_goto_main,
  295. "close_emulator_or_goto_main": self._wait_close_emulator_or,
  296. "close_emulator_or_close_game": self._wait_close_emulator_or,
  297. }
  298. func = strategy_map.get(method)
  299. if not func:
  300. logger.warning(f"Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there")
  301. func = self._wait_stay_there
  302. return func(next_run)
  303. if method in ["close_emulator_or_goto_main", "close_emulator_or_close_game"]:
  304. return func(next_run, close_game_limit_time, close_emulator_limit_time, method)
  305. if method == "close_game":
  306. return func(next_run, close_game_limit_time)
  307. return func(next_run)
  308. @staticmethod
  309. def _time_to_timedelta(value) -> timedelta:
  310. if value is None:
  311. return timedelta(0)
  312. return timedelta(hours=value.hour, minutes=value.minute, seconds=value.second)
  313. def _wait_until_with_emulator_preheat(self, next_run: datetime) -> bool:
  314. """Wait until next_run; if emulator is down, preheat startup before next task."""
  315. if not self._emulator_down:
  316. return self.wait_until(next_run)
  317. startup_lead = self._time_to_timedelta(self.config.script.optimization.emulator_startup_lead_time)
  318. wake_time = next_run - startup_lead if startup_lead > timedelta(0) else next_run
  319. if wake_time < datetime.now():
  320. wake_time = datetime.now()
  321. if wake_time > datetime.now():
  322. logger.info(f"Wait before wake emulator: {wake_time.strftime('%Y-%m-%d %H:%M:%S')}")
  323. if not self.wait_until(wake_time):
  324. return False
  325. if self._emulator_down:
  326. logger.info("Wake emulator before next task")
  327. self.device = Device(self.config)
  328. self._emulator_down = False
  329. if wake_time < next_run:
  330. return self.wait_until(next_run)
  331. return True
  332. def _wait_close_game(self, next_run: datetime, close_game_limit_time=None) -> bool:
  333. if self._emulator_down:
  334. logger.info("Emulator is down, skip close_game/goto_main action and wait with preheat")
  335. return self._wait_until_with_emulator_preheat(next_run)
  336. close_game_limit = self._time_to_timedelta(close_game_limit_time)
  337. if close_game_limit > timedelta(0) and next_run > datetime.now() + close_game_limit:
  338. logger.info("Close game during wait")
  339. self.device.app_stop()
  340. self.device.release_during_wait()
  341. if not self.wait_until(next_run):
  342. return False
  343. self.run("Restart")
  344. return True
  345. logger.info("Goto main page during wait (close_game limit time not reached)")
  346. self.device.release_during_wait()
  347. if not self.wait_until(next_run):
  348. return False
  349. return True
  350. def _wait_goto_main(self, next_run: datetime) -> bool:
  351. if self._emulator_down:
  352. logger.info("Emulator is down, skip goto_main and wait with preheat")
  353. return self._wait_until_with_emulator_preheat(next_run)
  354. logger.info("Goto main page during wait")
  355. self.run("GotoMain")
  356. self.device.release_during_wait()
  357. return self.wait_until(next_run)
  358. def _wait_close_emulator_or(self, next_run: datetime, close_game_limit_time=None,
  359. close_emulator_limit_time=None, method=None) -> bool:
  360. close_emulator_limit = self._time_to_timedelta(close_emulator_limit_time)
  361. if close_emulator_limit > timedelta(0) and next_run > datetime.now() + close_emulator_limit:
  362. logger.info("Close emulator during wait")
  363. if not self._emulator_down:
  364. self.device.emulator_stop()
  365. else:
  366. logger.info("Emulator already closed")
  367. self._emulator_down = True
  368. if not self._wait_until_with_emulator_preheat(next_run):
  369. return False
  370. self.run("Restart")
  371. return True
  372. if method == "close_emulator_or_goto_main":
  373. return self._wait_goto_main(next_run)
  374. return self._wait_close_game(next_run, close_game_limit_time)
  375. def _wait_stay_there(self, next_run: datetime) -> bool:
  376. if self._emulator_down:
  377. logger.info("Stay_there during wait (emulator is down, with preheat)")
  378. return self._wait_until_with_emulator_preheat(next_run)
  379. logger.info("Stay_there (no action) during wait")
  380. self.device.release_during_wait()
  381. return self.wait_until(next_run)
  382. def exception_handler(self, e: Exception, command: str) -> None:
  383. # 处理御魂溢出
  384. from tasks.Utils.post_diagnotor import PostDiagnotor, AnalyzeType
  385. image = getattr(self.device, 'image', None)
  386. # image为None则不做处理
  387. if image is None:
  388. return
  389. analyse_type = PostDiagnotor().handle(e=e, command=command, image=image)
  390. if analyse_type == AnalyzeType.SoulOverflow:
  391. self.config.task_call('SoulsTidy')
  392. time.sleep(1)
  393. def run(self, command: str) -> bool:
  394. """
  395. :param command: 大写驼峰命名的任务名字
  396. :return:
  397. """
  398. if command == 'start' or command == 'goto_main':
  399. logger.error(f'Invalid command `{command}`')
  400. try:
  401. self.device.screenshot()
  402. module_name = 'script_task'
  403. module_path = str(Path.cwd() / 'tasks' / command / (module_name+'.py'))
  404. logger.info(f'module_path: {module_path}, module_name: {module_name}')
  405. task_module = load_module(module_name, module_path)
  406. task_module.ScriptTask(config=self.config, device=self.device).run()
  407. except TaskEnd:
  408. return True
  409. except GameNotRunningError as e:
  410. logger.warning(e)
  411. self.exception_handler(e=e, command=command)
  412. self.config.task_call('Restart')
  413. return True
  414. except (GameStuckError, GameTooManyClickError) as e:
  415. logger.error(e)
  416. self.save_error_log()
  417. self.exception_handler(e=e, command=command)
  418. logger.warning(f'Game stuck, {self.device.package} will be restarted in 10 seconds')
  419. logger.warning('If you are playing by hand, please stop Alas')
  420. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GameStuckError or GameTooManyClickError")
  421. self.config.task_call('Restart')
  422. self.device.sleep(10)
  423. return False
  424. except GameBugError as e:
  425. logger.warning(e)
  426. self.save_error_log()
  427. self.exception_handler(e=e, command=command)
  428. logger.warning('An error has occurred in Azur Lane game client, Alas is unable to handle')
  429. logger.warning(f'Restarting {self.device.package} to fix it')
  430. self.config.task_call('Restart')
  431. self.device.sleep(10)
  432. return False
  433. except GamePageUnknownError as e:
  434. logger.info('Game server may be under maintenance or network may be broken, check server status now')
  435. # 这个还不重要 留着坑填
  436. logger.critical('Game page unknown')
  437. self.save_error_log()
  438. self.exception_handler(e=e, command=command)
  439. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GamePageUnknownError")
  440. self.config.task_call('Restart')
  441. self.device.sleep(10)
  442. return False
  443. except ScriptError as e:
  444. logger.critical(e)
  445. self.exception_handler(e=e, command=command)
  446. logger.critical('This is likely to be a mistake of developers, but sometimes just random issues')
  447. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> ScriptError")
  448. exit(1)
  449. except RequestHumanTakeover as e:
  450. logger.critical(e)
  451. self.exception_handler(e=e, command=command)
  452. logger.critical('Request human takeover')
  453. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> RequestHumanTakeover")
  454. exit(1)
  455. except Exception as e:
  456. logger.exception(e)
  457. self.exception_handler(e=e, command=command)
  458. self.save_error_log()
  459. self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> Exception occured")
  460. exit(1)
  461. def loop(self):
  462. """
  463. Main loop of scheduler.
  464. :return:
  465. """
  466. with _log_switch_lock:
  467. logger.set_file_logger(self.config_name, do_cleanup=True)
  468. start_day = date.today()
  469. logger.info(f'Start scheduler loop: {self.config_name}')
  470. self.config.model.running_task = ''
  471. # Update GUI 防呆, 读取设置并立刻显示后台模拟器到前台
  472. if not self.config.script.device.run_background_only and IS_WINDOWS:
  473. from module.device.platform2.platform_windows import minimize_by_name, show_window_by_name
  474. target_window_name = self.config.script.device.handle # 在这里输入你的具体窗口名称
  475. if self.config.script.device.emulator_window_minimize:
  476. minimize_by_name(target_window_name)
  477. logger.info(f'重新显示: {target_window_name}')
  478. else:
  479. show_window_by_name(target_window_name)
  480. while 1:
  481. if date.today() > start_day:
  482. with _log_switch_lock:
  483. logger.set_file_logger(self.config_name, do_cleanup=True)
  484. start_day = date.today()
  485. # Check update event from GUI
  486. # if self.stop_event is not None:
  487. # if self.stop_event.is_set():
  488. # logger.info("Update event detected")
  489. # logger.info(f"Alas [{self.config_name}] exited.")
  490. # break
  491. # Check game server maintenance
  492. # self.checker.wait_until_available()
  493. # if self.checker.is_recovered():
  494. # # There is an accidental bug hard to reproduce
  495. # # Sometimes, config won't be updated due to blocking
  496. # # even though it has been changed
  497. # # So update it once recovered
  498. # del_cached_property(self, 'config')
  499. # logger.info('Server or network is recovered. Restart game client')
  500. # self.config.task_call('Restart')
  501. # Get task
  502. task = self.get_next_task()
  503. # Skip first restart
  504. if self.is_first_task and task == 'Restart':
  505. logger.info('Skip task `Restart` at scheduler start')
  506. self.config.task_delay(task='Restart', success=True, server=True)
  507. del_cached_property(self, 'config')
  508. continue
  509. if self._emulator_down:
  510. self.device = Device(self.config)
  511. self._emulator_down = False
  512. else:
  513. _ = self.device
  514. # Run
  515. logger.info(f'Scheduler: Start task `{task}`')
  516. self.device.stuck_record_clear()
  517. self.device.click_record_clear()
  518. logger.hr(task, level=0)
  519. self.config.model.running_task = task
  520. success = self.run(inflection.camelize(task))
  521. self.config.model.running_task = ''
  522. logger.info(f'Scheduler: End task `{task}`')
  523. self.is_first_task = False
  524. # Check failures
  525. # failed = deep_get(self.failure_record, keys=task, default=0)
  526. failed = self.failure_record[task] if task in self.failure_record else 0
  527. failed = 0 if success else failed + 1
  528. # deep_set(self.failure_record, keys=task, value=failed)
  529. self.failure_record[task] = failed
  530. if failed >= 3:
  531. logger.critical(f"Task `{task}` failed 3 or more times.")
  532. logger.critical("Possible reason #1: You haven't used it correctly. "
  533. "Please read the help text of the options.")
  534. logger.critical("Possible reason #2: There is a problem with this task. "
  535. "Please contact developers or try to fix it yourself.")
  536. logger.critical('Request human takeover')
  537. # 添加失败三次的推送通知
  538. self.config.notifier.push(
  539. title=f'{I18n.trans_zh_cn(task)}{task}',
  540. content=f"<{self.config_name}> 任务连续失败三次,请上线查看"
  541. )
  542. # 关闭模拟器
  543. if self.config.script.error.error_repeated:
  544. self.device.emulator_stop()
  545. exit(1)
  546. if success:
  547. del_cached_property(self, 'config')
  548. continue
  549. elif self.config.script.error.handle_error:
  550. # self.config.task_delay(success=False)
  551. del_cached_property(self, 'config')
  552. # self.checker.check_now()
  553. continue
  554. else:
  555. break
  556. def start_loop(self) -> None:
  557. """
  558. 创建一个线程,运行loop
  559. :return:
  560. """
  561. if self.loop_thread is None:
  562. self.loop_thread = Thread(target=self.loop, name='Script_loop')
  563. self.loop_thread.start()
  564. if __name__ == "__main__":
  565. ensure_ocr_server_started()
  566. script = Script("oas1")
  567. script.loop()