script_task.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from datetime import datetime
  5. from tasks.Restart.config_scheduler import Scheduler
  6. from tasks.Restart.login import LoginHandler
  7. from tasks.Restart.assets import RestartAssets
  8. from tasks.base_task import BaseTask, Time
  9. from datetime import datetime, time
  10. from module.logger import logger
  11. from module.exception import TaskEnd, RequestHumanTakeover
  12. class ScriptTask(LoginHandler):
  13. def run(self) -> None:
  14. """
  15. 主要就是登录的模块
  16. :return:
  17. """
  18. if not self.delay_pending_tasks():
  19. self.app_restart()
  20. raise TaskEnd('ScriptTask end')
  21. def app_stop(self):
  22. logger.hr('App stop')
  23. self.device.app_stop()
  24. def app_start(self):
  25. logger.hr('App start')
  26. self.device.app_start()
  27. self.app_handle_login()
  28. # self.ensure_no_unfinished_campaign()
  29. def app_restart(self):
  30. logger.hr('App restart')
  31. self.device.app_stop()
  32. self.device.app_start()
  33. self.app_handle_login()
  34. # self.config.task_delay(server_update=True)
  35. self.set_next_run(task='Restart', success=True, finish=True, server=True)
  36. # 如果启用了定时领体力(每天 12-14、20-22 时内各有 20 体力)
  37. if self.config.restart.harvest_config.enable_ap:
  38. now = datetime.now()
  39. # 如果时间在00:00-12:00之间则设定时间为当日 12 时
  40. if now.time() < time(12, 0):
  41. self.custom_next_run(task='Restart', custom_time=Time(12, 0), time_delta=0)
  42. # 如果时间在12:00-20:00之间则设定时间为当日 20 时
  43. elif now.time() >= time(12, 0) and now.time() < time(20, 0):
  44. self.custom_next_run(task='Restart', custom_time=Time(20, 0), time_delta=0)
  45. # 如果时间在20:00-23:59之间则设定时间为次日 12 时
  46. else:
  47. self.custom_next_run(task='Restart', custom_time=Time(12, 0), time_delta=1)
  48. def delay_pending_tasks(self) -> bool:
  49. """
  50. 周三更新游戏的时候延迟
  51. @return:
  52. """
  53. datetime_now = datetime.now()
  54. if not (datetime_now.weekday() == 2 and 6 <= datetime_now.hour <= 8):
  55. return False
  56. logger.info("The game server is updating, delay the pending tasks to 9:00")
  57. logger.warning('Delay pending tasks')
  58. # running 中的必然是 Restart
  59. for task in self.config.pending_task:
  60. print(task.command)
  61. self.set_next_run(task=task.command, target=datetime_now.replace(hour=9, minute=0, second=0, microsecond=0))
  62. self.set_next_run(task='Restart', success=True, finish=True, server=True)
  63. return True
  64. if __name__ == '__main__':
  65. from module.config.config import Config
  66. from module.device.device import Device
  67. config = Config('oas1')
  68. device = Device(config)
  69. task = ScriptTask(config, device)
  70. for i in range(3):
  71. task.app_restart()
  72. # task.config.update_scheduler()
  73. # task.delay_pending_tasks()