script_task.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import re
  5. from datetime import timedelta, datetime, time
  6. from cached_property import cached_property
  7. from tasks.GameUi.game_ui import GameUi
  8. from tasks.GameUi.page import page_main, page_team
  9. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  10. from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
  11. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  12. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  13. from tasks.Nian.assets import NianAssets
  14. from tasks.Component.GeneralBuff.config_buff import BuffClass
  15. from module.exception import TaskEnd
  16. from module.logger import logger
  17. from module.base.timer import Timer
  18. class ScriptTask(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, NianAssets):
  19. def run(self) -> None:
  20. def cd_exit(cd: timedelta=False):
  21. logger.warning(f'Nian in CD {cd}')
  22. if cd is False:
  23. self.set_next_run(task='Nian', success=False, finish=True)
  24. raise TaskEnd('Nian')
  25. next_run = datetime.now() + cd
  26. self.set_next_run(task='Nian', success=False, finish=False, target=next_run)
  27. raise TaskEnd('Nian')
  28. self.goto_page(page_team)
  29. con = self.config.nian.nian_config
  30. # 进入
  31. self.goto_page(page_team)
  32. self.check_zones('年兽')
  33. cd = self.check_cd()
  34. if cd:
  35. cd_exit(cd)
  36. # 但是这个时候还不一定可以完全判断有没有进入cd
  37. cd_count = 0
  38. while 1:
  39. self.screenshot()
  40. if self.appear(self.I_N_WAITING):
  41. break
  42. if cd_count >= 4:
  43. # 4 x 1.5 = 6秒没有进入说明是在冷却中
  44. cd_exit()
  45. if self.appear_then_click(self.I_GR_AUTO_MATCH, interval=1.5):
  46. cd_count += 1
  47. continue
  48. # 匹配个8分钟,要是八分钟还没人拿没啥了
  49. logger.info('Waiting for match')
  50. click_timer = Timer(240)
  51. check_timer = Timer(480)
  52. click_timer.start()
  53. check_timer.start()
  54. self.device.stuck_record_add('LOGIN_CHECK')
  55. while 1:
  56. self.screenshot()
  57. # 如果被秒开进入战斗, 被秒开不支持开启buff
  58. if self.check_take_over_battle(False, config=self.battle_config):
  59. logger.info('Nian take over battle')
  60. break
  61. # 如果进入房间
  62. elif self.is_in_room():
  63. self.device.stuck_record_clear()
  64. if self.wait_battle(wait_time=time(minute=1)):
  65. buff = []
  66. if con.buff_gold_50_click:
  67. buff.append(BuffClass.GOLD_50)
  68. if con.buff_gold_100_click:
  69. buff.append(BuffClass.GOLD_100)
  70. if buff is []:
  71. buff = None
  72. self.run_general_battle(config=self.battle_config, buff=buff)
  73. # 打完后返回庭院,记得关闭buff
  74. self.open_buff()
  75. if con.buff_gold_50_click:
  76. self.gold_50(False)
  77. if con.buff_gold_100_click:
  78. self.gold_100(False)
  79. self.close_buff()
  80. break
  81. else:
  82. break
  83. # 如果时间到了
  84. if click_timer and click_timer.reached():
  85. logger.warning('It has waited for 240s , but the battle has not started.')
  86. logger.warning('It will be waited for 240s and try again.')
  87. self.screenshot()
  88. self.click(self.C_CLIC_SAFE)
  89. click_timer = None
  90. self.device.stuck_record_clear()
  91. self.device.stuck_record_add('LOGIN_CHECK')
  92. continue
  93. if check_timer.reached():
  94. logger.warning('Nian match timeout')
  95. while 1:
  96. self.screenshot()
  97. if not self.appear(self.I_N_WAITING):
  98. break
  99. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  100. continue
  101. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  102. continue
  103. if self.appear_then_click(self.I_N_WAITING, interval=1):
  104. continue
  105. logger.info('Nian match timeout, exit')
  106. break
  107. # 如果还在匹配中
  108. if self.appear(self.I_N_WAITING):
  109. continue
  110. # 退出结束
  111. self.set_next_run(task='Nian', success=True, finish=False)
  112. raise TaskEnd('Nian')
  113. def check_cd(self) -> False or timedelta:
  114. """
  115. 检查是否在冷却中,
  116. :return: 如果是冷却中, 返回剩余时间, 否则返回False
  117. """
  118. self.screenshot()
  119. if not self.appear(self.I_N_CHECK):
  120. return False
  121. if not self.ocr_appear(self.O_N_BATTLE_AGAIN):
  122. return False
  123. # 可以判断是在冷却了
  124. result = self.O_N_CD.ocr(self.device.image)
  125. if not isinstance(result, str):
  126. logger.error(f'OCR error {result}')
  127. return False
  128. try:
  129. result = re.search(r'(\d+)时(\d+)分后可', result)
  130. time_delta = timedelta(hours=int(result.group(1)), minutes=int(result.group(2)))
  131. except Exception as e:
  132. logger.error(f'{e} {result}')
  133. time_delta = timedelta(hours=4)
  134. return time_delta
  135. @cached_property
  136. def battle_config(self) -> GeneralBattleConfig:
  137. conf = GeneralBattleConfig()
  138. return conf
  139. if __name__ == '__main__':
  140. from module.config.config import Config
  141. from module.device.device import Device
  142. c = Config('oas1')
  143. d = Device(c)
  144. t = ScriptTask(c, d)
  145. t.screenshot()
  146. t.run()