script_task.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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
  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. while 1:
  126. self.screenshot()
  127. if self.appear(self.I_GREED_GHOST):
  128. sleep(0.7)
  129. self.screenshot()
  130. if not self.appear(self.I_GREED_GHOST):
  131. continue
  132. # 左上角的贪吃鬼
  133. while 1:
  134. self.screenshot()
  135. if not self.appear(self.I_GREED_GHOST):
  136. break
  137. if self.appear_then_click(self.I_GREED_GHOST, interval=1):
  138. continue
  139. if self.appear_then_click(self.I_ST_FRAME, interval=1):
  140. continue
  141. break
  142. if self.appear_then_click(self.I_ST_FRAME, interval=1):
  143. continue
  144. if check_timer.reached():
  145. logger.warning('Battle timeout')
  146. check_timer.reset()
  147. self.device.stuck_record_clear()
  148. self.device.stuck_record_add('BATTLE_STATUS_S')
  149. sleep(0.5)
  150. logger.info("Battle process end")
  151. self.check_times(battle)
  152. raise TaskEnd('TrueOrochi')
  153. def check_true_orochi(self, screenshot=False) -> bool:
  154. """
  155. 检查是否出现真蛇(要求当前的界面必须是在御魂挑战的界面)
  156. :return:
  157. """
  158. if screenshot:
  159. self.screenshot()
  160. return self.appear(self.I_FIND_TS)
  161. def check_times(self, battle: bool):
  162. """
  163. 后续的次数和时间设置
  164. :param battle:
  165. :param current_success: 这周的成功次数
  166. :return:
  167. """
  168. now = datetime.now()
  169. now_year, now_week_number, now_weekday = now.isocalendar()
  170. if battle:
  171. next_run = now + self.config.true_orochi.scheduler.success_interval
  172. else:
  173. next_run = now + self.config.true_orochi.scheduler.failure_interval
  174. next_run_year, next_run_week_number, next_run_weekday = next_run.isocalendar()
  175. # 如果下次运行的时间是下一周,那么就重置成功次数
  176. if now_week_number != next_run_week_number:
  177. logger.info('Reset current_success')
  178. self.config.true_orochi.true_orochi_config.current_success = 0
  179. else:
  180. # 如果不是下一周,那么就加一
  181. logger.info('Add current_success by 1')
  182. self.config.true_orochi.true_orochi_config.current_success += 1
  183. self.config.true_orochi.true_orochi_config.current_success = min(2, self.config.true_orochi.true_orochi_config.current_success)
  184. self.config.save()
  185. self.set_next_run(task='TrueOrochi', target=next_run)
  186. # self.set_next_run('TrueOrochi', finish=True, success=True)
  187. def run_true_orochi(self) -> bool:
  188. pass
  189. if __name__ == '__main__':
  190. from module.config.config import Config
  191. from module.device.device import Device
  192. c = Config('oas1')
  193. d = Device(c)
  194. t = ScriptTask(c, d)
  195. t.screenshot()
  196. t.run()