script_task.py 6.1 KB

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