script_task.py 9.1 KB

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