script_task.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.goto_page(page_shikigami_records)
  28. self.run_switch_soul(self.config.true_orochi.switch_soul.switch_group_team)
  29. # 御魂切换方式二
  30. if self.config.true_orochi.switch_soul.enable_switch_by_name:
  31. self.goto_page(page_shikigami_records)
  32. self.run_switch_soul_by_name(self.config.true_orochi.switch_soul.group_name,
  33. self.config.true_orochi.switch_soul.team_name)
  34. self.goto_page(page_soul_zones)
  35. self.orochi_enter()
  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_soul_zones)
  79. self.orochi_enter()
  80. sleep(0.5)
  81. if conf.current_success < 2 and self.check_true_orochi(True):
  82. battle = True
  83. logger.info('Find another true orochi entry, continue')
  84. self.run_true_orochi_battle()
  85. self.check_times(battle)
  86. raise TaskEnd('TrueOrochi')
  87. def run_true_orochi_battle(self):
  88. """执行一次真蛇战斗"""
  89. conf = self.config.true_orochi.true_orochi_config
  90. logger.hr('True Orochi Battle')
  91. while 1:
  92. self.screenshot()
  93. if self.appear(self.I_ST_CREATE_ROOM):
  94. break
  95. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  96. continue
  97. if self.appear_then_click(self.I_ST_FIRE, interval=4):
  98. # 修正已经挑战的次数, 注意这个是战斗开始之前的次数
  99. current, current_success, total = self.O_TIMES.ocr(self.device.image)
  100. if current_success < 0 or current_success > 2:
  101. continue
  102. logger.info(f'current: {current}, current_success: {current_success}, total: {total}')
  103. conf.current_success = current_success
  104. self.config.save()
  105. continue
  106. if self.appear_then_click(self.I_FIND_TS, interval=1):
  107. continue
  108. self.ensure_private()
  109. while 1:
  110. self.screenshot()
  111. if self.appear(self.I_ST_FIRE_PREPARE):
  112. break
  113. if self.appear_then_click(self.I_FIRE, interval=3, threshold=0.7):
  114. continue
  115. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  116. continue
  117. if self.appear_then_click(self.I_ST_CREATE_ROOM, interval=1):
  118. continue
  119. # 战斗准备
  120. logger.info('Battle prepare')
  121. self.ui_click(self.I_ST_FIRE_PREPARE, self.I_BUFF)
  122. while 1:
  123. self.screenshot()
  124. if not self.appear(self.I_BUFF):
  125. break
  126. if self.appear_then_click(self.I_ST_AUTO_FALSE, interval=1.8):
  127. continue
  128. # 下面代码 "点击准备" 会造成,点击了准备打第一层 循环 if not self.appear(self.I_BUFF): 时候跳出while循环,导致真蛇卡在第二层
  129. # if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.8):
  130. # continue
  131. self.device.stuck_record_add('BATTLE_STATUS_S')
  132. self.device.click_record_clear()
  133. logger.info("Start battle process")
  134. check_timer = Timer(280)
  135. check_timer.start()
  136. check_count = 0
  137. while 1:
  138. self.screenshot()
  139. if self.appear(self.I_GREED_GHOST):
  140. sleep(0.7)
  141. self.screenshot()
  142. if not self.appear(self.I_GREED_GHOST):
  143. continue
  144. # 左上角的贪吃鬼
  145. while 1:
  146. self.screenshot()
  147. if not self.appear(self.I_GREED_GHOST):
  148. break
  149. if self.appear_then_click(self.I_GREED_GHOST, interval=1):
  150. continue
  151. if self.appear_then_click(self.I_ST_FRAME, interval=1):
  152. continue
  153. break
  154. if self.appear_then_click(self.I_ST_FRAME, interval=1):
  155. continue
  156. if check_timer.reached():
  157. if check_count > 3:
  158. raise GameStuckError
  159. check_count += 1
  160. logger.warning('Battle timeout')
  161. check_timer.reset()
  162. self.device.stuck_record_clear()
  163. self.device.stuck_record_add('BATTLE_STATUS_S')
  164. sleep(0.5)
  165. logger.info("Battle process end")
  166. def check_true_orochi(self, screenshot=False) -> bool:
  167. """
  168. 检查是否出现真蛇(要求当前的界面必须是在御魂挑战的界面)
  169. :return:
  170. """
  171. if screenshot:
  172. self.screenshot()
  173. return self.appear(self.I_FIND_TS)
  174. def check_times(self, battle: bool):
  175. """
  176. 后续的次数和时间设置
  177. :param battle: 本次是否挑战了真蛇
  178. :param current_success: 这周的成功次数
  179. :return:
  180. """
  181. now = datetime.now()
  182. now_year, now_week_number, now_weekday = now.isocalendar()
  183. if battle:
  184. # 只有真正打过才加一次数
  185. logger.info('Add current_success by 1')
  186. self.config.true_orochi.true_orochi_config.current_success += 1
  187. self.config.true_orochi.true_orochi_config.current_success = min(2, self.config.true_orochi.true_orochi_config.current_success)
  188. next_run = now + self.config.true_orochi.scheduler.success_interval
  189. else:
  190. logger.info('Battle skipped or not found True Orochi')
  191. next_run = now + self.config.true_orochi.scheduler.failure_interval
  192. next_run_year, next_run_week_number, next_run_weekday = next_run.isocalendar()
  193. # 如果下次运行的时间是下一周,那么就重置成功次数
  194. if now_week_number != next_run_week_number:
  195. logger.info('Reset current_success')
  196. self.config.true_orochi.true_orochi_config.current_success = 0
  197. self.config.save()
  198. self.set_next_run(task='TrueOrochi', target=next_run)
  199. # self.set_next_run('TrueOrochi', finish=True, success=True)
  200. def run_true_orochi(self) -> bool:
  201. pass
  202. if __name__ == '__main__':
  203. from module.config.config import Config
  204. from module.device.device import Device
  205. c = Config('oas1')
  206. d = Device(c)
  207. t = ScriptTask(c, d)
  208. t.screenshot()
  209. t.run()