script_task.py 12 KB

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