script_task.py 4.1 KB

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