script_task.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import importlib
  2. from datetime import datetime, timedelta
  3. from module.config.utils import read_file
  4. from module.exception import TaskEnd, RequestHumanTakeover
  5. from tasks.Component.SwitchAccount.switch_account import SwitchAccount
  6. from tasks.FindJade import WantedQuestsEx
  7. from tasks.FindJade.assets import FindJadeAssets
  8. from tasks.FindJade.config import AccountInfo, FindJadeJSON
  9. from tasks.GameUi.game_ui import GameUi
  10. from tasks.WantedQuests.config import CooperationSelectMaskDescription
  11. from module.logger import logger
  12. class ScriptTask(GameUi, FindJadeAssets):
  13. fade_conf: FindJadeJSON = None
  14. def run(self):
  15. self.fade_conf = self.parse()
  16. self.fade_conf.updateHandle = self.save_jade_json
  17. for accountInfo in self.fade_conf.find_jade_accounts_info:
  18. logger.info("start %s-%s ", accountInfo.character, accountInfo.svr)
  19. if not self.is_need_login(accountInfo):
  20. logger.warning("%s Skipped last Login Time:%s", accountInfo.character, accountInfo.last_complete_time)
  21. continue
  22. suc = SwitchAccount(self.config, self.device, accountInfo).switchAccount()
  23. if not suc:
  24. logger.warning("switch to %s-%s Failed", accountInfo.character, accountInfo.svr)
  25. continue
  26. #
  27. wq = self.CreatObjectFromModule("WantedQuests", config=self.config, device=self.device)
  28. wq.fade_conf = self.fade_conf
  29. try:
  30. wq.run()
  31. except TaskEnd as e:
  32. logger.warning("%s-%s TaskEnd", accountInfo.character, accountInfo.svr)
  33. # 更新配置文件中的时间
  34. self.fade_conf.update_account_login_history(accountInfo)
  35. self.fade_conf.save2file(self.config.find_jade.find_jade_config.find_jade_json_path)
  36. continue
  37. except RequestHumanTakeover as e:
  38. raise
  39. except Exception as e:
  40. self.next_run("FindJade", success=False)
  41. self.next_run("FindJade",success=True)
  42. raise TaskEnd("FindJade")
  43. pass
  44. def parse(self) -> FindJadeJSON:
  45. conf_path = self.config.find_jade.find_jade_config.find_jade_json_path
  46. jsonData = read_file(conf_path)
  47. fjconf = FindJadeJSON(**jsonData)
  48. return fjconf
  49. def is_need_login(self, item: AccountInfo):
  50. """
  51. 根据上次登陆时间 判断是否需要登录查找
  52. @param item:
  53. @type item:
  54. """
  55. lastTime = item.last_complete_time
  56. now = datetime.now()
  57. if now - lastTime > timedelta(hours=13):
  58. return True
  59. if (lastTime.hour >= 18 or lastTime.hour < 5) and (18 > now.hour >= 5):
  60. return True
  61. if (5 <= lastTime.hour < 18) and now.hour >= 18:
  62. return True
  63. return False
  64. def CreatObjectFromModule(self, task_name: str, **kwargs):
  65. module_name = 'script_task'
  66. from pathlib import Path
  67. module_path = str(Path.cwd() / 'tasks' / task_name / (module_name + '.py'))
  68. spec = importlib.util.spec_from_file_location(module_name, module_path)
  69. module = importlib.util.module_from_spec(spec)
  70. spec.loader.exec_module(module)
  71. WQEX = type("WQEX", (module.ScriptTask,), {
  72. "need_invite_vip": WantedQuestsEx.need_invite_vip,
  73. "get_invite_vip_name": WantedQuestsEx.get_invite_vip_name,
  74. "next_run": WantedQuestsEx.next_run,
  75. "invite_success_callback": WantedQuestsEx.invite_success_callback,
  76. "get_config": WantedQuestsEx.get_config
  77. })
  78. wq = WQEX(**kwargs)
  79. return wq
  80. def save_jade_json(self):
  81. conf_path = self.config.find_jade.find_jade_config.find_jade_json_path
  82. self.fade_conf.save2file(conf_path)
  83. def next_run(self, task: str, finish: bool = False,
  84. success: bool = None, server: bool = True, target: datetime = None) -> None:
  85. now = datetime.now()
  86. if success:
  87. if (5 <= now.hour < 18):
  88. self.set_next_run(task, target=now.replace(hour=18, minute=5))
  89. elif (now.hour < 5):
  90. self.set_next_run(task, target=now.replace(hour=5, minute=5))
  91. else:
  92. self.set_next_run(task, target=now.replace(hour=18, minute=5) + timedelta(days=1))
  93. else:
  94. self.set_next_run(task, target=now+timedelta(minutes=10))
  95. if __name__ == '__main__':
  96. from module.config.config import Config
  97. from module.device.device import Device
  98. from mypatch import SimplePatch
  99. SimplePatch.patch()
  100. c = Config('oas1')
  101. d = Device(c)
  102. t = ScriptTask(c, d)
  103. t.run()