script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  6. from tasks.Exploration.assets import ExplorationAssets
  7. from tasks.Exploration.config import ChooseRarity, AutoRotate, AttackNumber
  8. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  9. from tasks.GameUi.game_ui import GameUi
  10. from tasks.GameUi.page import page_exploration, page_shikigami_records, page_main
  11. from module.logger import logger
  12. from module.exception import RequestHumanTakeover, TaskEnd
  13. from module.atom.image_grid import ImageGrid
  14. class ScriptTask(GeneralBattle, GameUi, SwitchSoul, ExplorationAssets):
  15. medal_grid: ImageGrid = None
  16. def run(self):
  17. """
  18. 执行
  19. :return:
  20. """
  21. # 探索的 config
  22. explorationConfig = self.config.exploration
  23. # 切换御魂
  24. if explorationConfig.switch_soul_config.enable:
  25. self.ui_get_current_page()
  26. self.ui_goto(page_shikigami_records)
  27. self.run_switch_soul(explorationConfig.switch_soul_config.switch_group_team)
  28. if explorationConfig.switch_soul_config.enable_switch_by_name:
  29. self.ui_get_current_page()
  30. self.ui_goto(page_shikigami_records)
  31. self.run_switch_soul_by_name(explorationConfig.switch_soul_config.group_name,
  32. explorationConfig.switch_soul_config.team_name)
  33. # 开启加成
  34. con = self.config.exploration.exploration_config
  35. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  36. self.ui_get_current_page()
  37. self.ui_goto(page_main)
  38. self.open_buff()
  39. if con.buff_gold_50_click:
  40. self.gold_50()
  41. if con.buff_gold_100_click:
  42. self.gold_100()
  43. if con.buff_exp_50_click:
  44. self.exp_50()
  45. if con.buff_exp_100_click:
  46. self.exp_100()
  47. self.close_buff()
  48. self.ui_get_current_page()
  49. # 探索页面
  50. self.ui_goto(page_exploration)
  51. # ************************* 跳转至对应指定章节并进入 *******************
  52. # 默认全部解锁, 当前处于第二十八章
  53. # 查找指定的章节:
  54. if not self.open_expect_level():
  55. logger.critical(f'Not find {explorationConfig.exploration_config.exploration_level} or'
  56. f' Enter {explorationConfig.exploration_config.exploration_level} failed!')
  57. raise RequestHumanTakeover
  58. # 只探索7次
  59. if explorationConfig.exploration_config.attack_number == AttackNumber.SEVEN:
  60. count = 0
  61. while count < 7:
  62. if self.wait_until_appear(self.I_E_EXPLORATION_CLICK, wait_time=1):
  63. self.click(self.I_E_EXPLORATION_CLICK)
  64. count += 1
  65. # 进入战斗环节
  66. self.battle_process()
  67. if self.appear(self.I_EXPLORATION_TITLE):
  68. self.open_expect_level()
  69. if self.wait_until_appear(self.I_RED_CLOSE, wait_time=2):
  70. self.appear_then_click(self.I_RED_CLOSE)
  71. self.ui_goto(page_main)
  72. # 关闭 buff
  73. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  74. self.open_buff()
  75. self.gold_50(is_open=False)
  76. self.gold_100(is_open=False)
  77. self.exp_50(is_open=False)
  78. self.exp_100(is_open=False)
  79. self.close_buff()
  80. self.set_next_run(task='Exploration', success=True, finish=False)
  81. raise TaskEnd
  82. # 查找指定的章节:
  83. def open_expect_level(self):
  84. swipeCount = 0
  85. while 1:
  86. # 探索的 config
  87. explorationConfig = self.config.exploration
  88. # 判断有无目标章节
  89. self.screenshot()
  90. # 获取当前章节名
  91. results = self.O_E_EXPLORATION_LEVEL_NUMBER.detect_and_ocr(self.device.image)
  92. text1 = [result.ocr_text for result in results]
  93. # 判断当前章节有无目标章节
  94. result = set(text1).intersection({explorationConfig.exploration_config.exploration_level})
  95. # 有则跳出检测
  96. if self.appear(self.I_E_EXPLORATION_CLICK) or result and len(result) > 0:
  97. break
  98. self.device.click_record_clear()
  99. self.swipe(self.S_SWIPE_LEVEL_UP)
  100. swipeCount += 1
  101. if swipeCount >= 25:
  102. return False
  103. # 选中对应章节
  104. while 1:
  105. self.screenshot()
  106. self.O_E_EXPLORATION_LEVEL_NUMBER.keyword = explorationConfig.exploration_config.exploration_level
  107. if self.ocr_appear_click(self.O_E_EXPLORATION_LEVEL_NUMBER):
  108. self.wait_until_appear(self.I_E_EXPLORATION_CLICK, wait_time=3)
  109. if self.appear(self.I_E_EXPLORATION_CLICK):
  110. break
  111. return True
  112. # 候补:
  113. def enter_settings_and_do_operations(self):
  114. # 打开设置
  115. self.click(self.C_CLICK_SETTINGS)
  116. while 1:
  117. self.screenshot()
  118. if self.appear(self.I_E_OPEN_SETTINGS):
  119. break
  120. else:
  121. self.click(self.C_CLICK_SETTINGS)
  122. if self.wait_until_appear(self.I_E_OPEN_SETTINGS):
  123. break
  124. # 候补出战数量识别
  125. self.screenshot()
  126. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  127. if cu >= 40:
  128. self.appear_then_click(self.I_E_SURE_BUTTON)
  129. return
  130. else:
  131. self.add_shiki()
  132. # 添加式神
  133. def add_shiki(self):
  134. self.screenshot()
  135. # 点击展开式神稀有度选择界面
  136. self.click(self.C_CLICK_ALL_SHIKI)
  137. while 1:
  138. self.screenshot()
  139. # 成功进入稀有度选择界面
  140. if self.appear(self.I_E_ENTER_CHOOSE_RARITY):
  141. if self.config.exploration.exploration_config.choose_rarity == ChooseRarity.N:
  142. # N 卡
  143. self.click(self.C_CLICK_N_SHIKI)
  144. else:
  145. # 素材卡
  146. self.appear_then_click(self.I_E_ENTER_CHOOSE_RARITY)
  147. self.screenshot()
  148. if self.appear(self.I_E_N_RARITY):
  149. # 进入N卡添加界面 退出
  150. break
  151. if self.appear(self.I_E_S_RARITY):
  152. # 进入素材添加界面 退出
  153. break
  154. # 选中候补出战框
  155. self.click(self.C_CLICK_STANDBY_TEAM)
  156. # 移动至未候补的狗粮
  157. while 1:
  158. # 慢一点
  159. time.sleep(0.5)
  160. self.screenshot()
  161. if self.appear(self.I_E_RATATE_EXSIT):
  162. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT)
  163. else:
  164. break
  165. while 1:
  166. # 候补出战数量识别
  167. self.screenshot()
  168. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  169. if cu >= 40:
  170. break
  171. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT_ONE)
  172. # 慢一点
  173. time.sleep(0.5)
  174. self.screenshot()
  175. self.click(self.C_CLICK_ROTATE_1)
  176. self.device.click_record_clear()
  177. self.appear_then_click(self.I_E_SURE_BUTTON)
  178. # 探索战斗流程
  179. def do_battle(self):
  180. while 1:
  181. self.screenshot()
  182. # 战后奖励
  183. if self.appear(self.I_BATTLE_REWARD) and not self.appear(self.I_GET_REWARD):
  184. self.click(self.I_BATTLE_REWARD)
  185. # boss 战
  186. if self.appear_then_click(self.I_BOSS_BATTLE_BUTTON):
  187. if self.wait_until_appear(self.I_BATTLE_START, wait_time=5):
  188. self.run_general_battle(self.config.exploration.general_battle_config)
  189. else:
  190. continue
  191. # 小怪 战
  192. if self.appear_then_click(self.I_NORMAL_BATTLE_BUTTON):
  193. if self.wait_until_appear(self.I_BATTLE_START, wait_time=5):
  194. self.run_general_battle(self.config.exploration.general_battle_config)
  195. else:
  196. continue
  197. # 滑动
  198. elif self.appear(self.I_E_AUTO_ROTATE_ON) or self.appear(self.I_GET_REWARD) or self.appear(self.I_REWARD):
  199. self.swipe(self.S_SWIPE_BACKGROUND_RIGHT)
  200. # 结束流程
  201. if self.appear(self.I_E_EXPLORATION_CLICK) or self.appear(self.I_EXPLORATION_TITLE):
  202. self.appear_then_click(self.I_RED_CLOSE)
  203. # 宝箱
  204. if self.appear(self.I_BATTLE_REWARD_BOX) and not self.appear(self.I_REWARD):
  205. self.click(self.I_BATTLE_REWARD_BOX)
  206. continue
  207. break
  208. # 战斗流程
  209. def battle_process(self):
  210. # 进入指定章节
  211. self.screenshot()
  212. if self.appear_then_click(self.I_E_EXPLORATION_CLICK):
  213. self.appear_then_click(self.I_E_SETTINGS_BUTTON)
  214. # ************************* 进入设置并操作 *******************
  215. # 候补以及自动轮换打开:
  216. # 自动轮换功能打开
  217. while 1:
  218. self.screenshot()
  219. # 自动轮换开着 则跳过
  220. if self.appear(self.I_E_AUTO_ROTATE_ON):
  221. break
  222. # 自动轮换关着 则打开
  223. if self.appear_then_click(self.I_E_AUTO_ROTATE_OFF):
  224. if self.appear(self.I_E_AUTO_ROTATE_ON):
  225. break
  226. # 自动添加候补式神
  227. if self.config.exploration.exploration_config.auto_rotate == AutoRotate.yes:
  228. self.enter_settings_and_do_operations()
  229. # 修复卡结算问题
  230. # 卡结算是因为没有设置锁定队伍,修改后无论是否锁定都不会因为没有锁定队伍而卡在结算界面
  231. if not self.config.exploration.general_battle_config.lock_team_enable:
  232. self.config.exploration.general_battle_config.lock_team_enable = True
  233. # 进入战斗环节
  234. self.do_battle()
  235. if __name__ == "__main__":
  236. from module.config.config import Config
  237. from module.device.device import Device
  238. config = Config('oas2')
  239. device = Device(config)
  240. t = ScriptTask(config, device)
  241. t.config.exploration.exploration_config.exploration_level = '第二十八章'
  242. t.run()
  243. # t.battle_process()
  244. # t.enter_settings_and_do_operations()