script_task.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from time import sleep
  5. from datetime import datetime, time
  6. from module.logger import logger
  7. from module.exception import TaskEnd, GameStuckError
  8. from module.base.timer import Timer
  9. from tasks.GameUi.game_ui import GameUi
  10. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  11. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  12. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  13. from tasks.Orochi.script_task import ScriptTask as OrochiScriptTask
  14. from tasks.Orochi.config import Layer
  15. from tasks.GameUi.page import page_main, page_soul_zones, page_shikigami_records
  16. from tasks.TrueOrochi.assets import TrueOrochiAssets
  17. class ScriptTask(OrochiScriptTask, TrueOrochiAssets):
  18. def run(self):
  19. conf = self.config.true_orochi.true_orochi_config
  20. if conf.current_success >= 2:
  21. # 超过两次就说明这周打完了没有必要再打了
  22. logger.warning('This week is full')
  23. self.check_times(True)
  24. raise TaskEnd('TrueOrochi')
  25. # 御魂切换方式一
  26. if self.config.true_orochi.switch_soul.enable:
  27. self.ui_get_current_page()
  28. self.ui_goto(page_shikigami_records)
  29. self.run_switch_soul(self.config.true_orochi.switch_soul.switch_group_team)
  30. # 御魂切换方式二
  31. if self.config.true_orochi.switch_soul.enable_switch_by_name:
  32. self.ui_get_current_page()
  33. self.ui_goto(page_shikigami_records)
  34. self.run_switch_soul_by_name(self.config.true_orochi.switch_soul.group_name,
  35. self.config.true_orochi.switch_soul.team_name)
  36. self.ui_get_current_page()
  37. self.ui_goto(page_soul_zones)
  38. self.orochi_enter()
  39. sleep(0.5)
  40. battle = self.check_true_orochi(True)
  41. if not battle:
  42. logger.warning('Not find true orochi')
  43. logger.warning('Try to battle orochi for ten times')
  44. # 判断是否需要挑战十层触发真蛇
  45. if not conf.find_true_orochi:
  46. logger.info('Not find_true_orochi_help')
  47. self.check_times(False)
  48. raise TaskEnd('TrueOrochi')
  49. self.check_layer(Layer.TEN)
  50. self.check_lock(False)
  51. count_orochi_ten = 0
  52. while 1:
  53. self.screenshot()
  54. # 检查猫咪奖励
  55. if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
  56. continue
  57. if not self.appear(self.I_OROCHI_FIRE):
  58. continue
  59. if self.check_true_orochi(False):
  60. logger.info('Find true orochi')
  61. battle = True
  62. break
  63. if count_orochi_ten >= 10:
  64. logger.warning('Not find true orochi')
  65. battle = False
  66. break
  67. # 否则点击挑战
  68. if self.appear(self.I_OROCHI_FIRE):
  69. self.ui_click_until_disappear(self.I_OROCHI_FIRE)
  70. self.run_general_battle()
  71. count_orochi_ten += 1
  72. continue
  73. if not battle:
  74. # 如果还没有真蛇,那么就退出
  75. self.check_times(battle)
  76. raise TaskEnd('TrueOrochi')
  77. # 如果有真蛇,那么就开始战斗
  78. logger.hr('True Orochi Battle')
  79. conf.current_success += 1
  80. while 1:
  81. self.screenshot()
  82. if self.appear(self.I_ST_CREATE_ROOM):
  83. break
  84. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  85. continue
  86. if self.appear_then_click(self.I_ST_FIRE, interval=4):
  87. # 修正已经挑战的次数, 注意这个是战斗开始之前的次数
  88. current, current_success, total = self.O_TIMES.ocr(self.device.image)
  89. if current_success < 0 or current_success > 2:
  90. continue
  91. logger.info(f'current: {current}, current_success: {current_success}, total: {total}')
  92. conf.current_success = current_success
  93. self.config.save()
  94. continue
  95. if self.appear_then_click(self.I_FIND_TS, interval=1):
  96. continue
  97. self.ensure_private()
  98. while 1:
  99. self.screenshot()
  100. if self.appear(self.I_ST_FIRE_PREPARE):
  101. break
  102. if self.appear_then_click(self.I_FIRE, interval=3, threshold=0.7):
  103. continue
  104. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  105. continue
  106. if self.appear_then_click(self.I_ST_CREATE_ROOM, interval=1):
  107. continue
  108. # 战斗准备
  109. logger.info('Battle prepare')
  110. self.ui_click(self.I_ST_FIRE_PREPARE, self.I_BUFF)
  111. while 1:
  112. self.screenshot()
  113. if not self.appear(self.I_BUFF):
  114. break
  115. if self.appear_then_click(self.I_ST_AUTO_FALSE, interval=1.8):
  116. continue
  117. # 下面代码 "点击准备" 会造成,点击了准备打第一层 循环 if not self.appear(self.I_BUFF): 时候跳出while循环,导致真蛇卡在第二层
  118. # if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.8):
  119. # continue
  120. self.device.stuck_record_add('BATTLE_STATUS_S')
  121. self.device.click_record_clear()
  122. logger.info("Start battle process")
  123. check_timer = Timer(280)
  124. check_timer.start()
  125. check_count = 0
  126. while 1:
  127. self.screenshot()
  128. if self.appear(self.I_GREED_GHOST):
  129. sleep(0.7)
  130. self.screenshot()
  131. if not self.appear(self.I_GREED_GHOST):
  132. continue
  133. # 左上角的贪吃鬼
  134. while 1:
  135. self.screenshot()
  136. if not self.appear(self.I_GREED_GHOST):
  137. break
  138. if self.appear_then_click(self.I_GREED_GHOST, interval=1):
  139. continue
  140. if self.appear_then_click(self.I_ST_FRAME, interval=1):
  141. continue
  142. break
  143. if self.appear_then_click(self.I_ST_FRAME, interval=1):
  144. continue
  145. if check_timer.reached():
  146. if check_count > 3:
  147. raise GameStuckError
  148. check_count += 1
  149. logger.warning('Battle timeout')
  150. check_timer.reset()
  151. self.device.stuck_record_clear()
  152. self.device.stuck_record_add('BATTLE_STATUS_S')
  153. sleep(0.5)
  154. logger.info("Battle process end")
  155. self.check_times(battle)
  156. raise TaskEnd('TrueOrochi')
  157. def check_true_orochi(self, screenshot=False) -> bool:
  158. """
  159. 检查是否出现真蛇(要求当前的界面必须是在御魂挑战的界面)
  160. :return:
  161. """
  162. if screenshot:
  163. self.screenshot()
  164. return self.appear(self.I_FIND_TS)
  165. def check_times(self, battle: bool):
  166. """
  167. 后续的次数和时间设置
  168. :param battle: 本次是否挑战了真蛇
  169. :param current_success: 这周的成功次数
  170. :return:
  171. """
  172. now = datetime.now()
  173. now_year, now_week_number, now_weekday = now.isocalendar()
  174. if battle:
  175. # 只有真正打过才加一次数
  176. logger.info('Add current_success by 1')
  177. self.config.true_orochi.true_orochi_config.current_success += 1
  178. self.config.true_orochi.true_orochi_config.current_success = min(2, self.config.true_orochi.true_orochi_config.current_success)
  179. next_run = now + self.config.true_orochi.scheduler.success_interval
  180. else:
  181. logger.info('Battle skipped or not found True Orochi')
  182. next_run = now + self.config.true_orochi.scheduler.failure_interval
  183. next_run_year, next_run_week_number, next_run_weekday = next_run.isocalendar()
  184. # 如果下次运行的时间是下一周,那么就重置成功次数
  185. if now_week_number != next_run_week_number:
  186. logger.info('Reset current_success')
  187. self.config.true_orochi.true_orochi_config.current_success = 0
  188. self.config.save()
  189. self.set_next_run(task='TrueOrochi', target=next_run)
  190. # self.set_next_run('TrueOrochi', finish=True, success=True)
  191. def run_true_orochi(self) -> bool:
  192. pass
  193. if __name__ == '__main__':
  194. from module.config.config import Config
  195. from module.device.device import Device
  196. c = Config('oas1')
  197. d = Device(c)
  198. t = ScriptTask(c, d)
  199. t.screenshot()
  200. t.run()