script_task.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. con_scrolls = self.config.exploration.scrolls
  61. # 判断是否绘卷模式,设置探索次数
  62. if con_scrolls.scrolls_enable:
  63. exploration_count = 50
  64. else:
  65. exploration_count = explorationConfig.exploration_config.current_exploration_count
  66. logger.info("exploration_count:" + str(exploration_count))
  67. if exploration_count > 0:
  68. count = 0
  69. while count < exploration_count:
  70. if self.wait_until_appear(self.I_E_EXPLORATION_CLICK, wait_time=1):
  71. # 如果打开绘卷模式
  72. if con_scrolls.scrolls_enable:
  73. self.screenshot()
  74. cu, res, total = self.O_REALM_RAID_NUMBER1.ocr(self.device.image)
  75. # 如果突破卷超出设定数量,去打个突
  76. if cu >= con_scrolls.scrolls_threshold:
  77. # 设定下次探索时间
  78. next_run = datetime.now() + con_scrolls.scrolls_cd
  79. self.set_next_run(task='Exploration', success=False, finish=False, target=next_run)
  80. self.set_next_run(task='RealmRaid', success=False, finish=False, target=datetime.now())
  81. self.appear_then_click(self.I_RED_CLOSE)
  82. self.ui_goto(page_main)
  83. # 关闭 buff
  84. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  85. self.open_buff()
  86. self.gold_50(is_open=False)
  87. self.gold_100(is_open=False)
  88. self.exp_50(is_open=False)
  89. self.exp_100(is_open=False)
  90. self.close_buff()
  91. raise TaskEnd
  92. self.click(self.I_E_EXPLORATION_CLICK)
  93. count += 1
  94. logger.info("exploration count :" + str(count))
  95. # 进入战斗环节
  96. self.battle_process()
  97. # 判断宝箱
  98. if self.appear(self.I_TREASURE_BOX_CLICK):
  99. self.click(self.I_TREASURE_BOX_CLICK)
  100. self.open_expect_level()
  101. # 判断妖气
  102. elif self.appear(self.I_EXPLORATION_TITLE):
  103. self.open_expect_level()
  104. if self.wait_until_appear(self.I_RED_CLOSE, wait_time=2):
  105. self.appear_then_click(self.I_RED_CLOSE)
  106. self.ui_goto(page_main)
  107. # 关闭 buff
  108. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  109. self.open_buff()
  110. self.gold_50(is_open=False)
  111. self.gold_100(is_open=False)
  112. self.exp_50(is_open=False)
  113. self.exp_100(is_open=False)
  114. self.close_buff()
  115. self.set_next_run(task='Exploration', success=True, finish=False)
  116. raise TaskEnd
  117. # 查找指定的章节:
  118. def open_expect_level(self):
  119. swipeCount = 0
  120. while 1:
  121. # 探索的 config
  122. explorationConfig = self.config.exploration
  123. # 判断有无目标章节
  124. self.screenshot()
  125. # 获取当前章节名
  126. results = self.O_E_EXPLORATION_LEVEL_NUMBER.detect_and_ocr(self.device.image)
  127. text1 = [result.ocr_text for result in results]
  128. # 判断当前章节有无目标章节
  129. result = set(text1).intersection({explorationConfig.exploration_config.exploration_level})
  130. # 有则跳出检测
  131. if self.appear(self.I_E_EXPLORATION_CLICK) or result and len(result) > 0:
  132. break
  133. self.device.click_record_clear()
  134. self.swipe(self.S_SWIPE_LEVEL_UP)
  135. swipeCount += 1
  136. if swipeCount >= 25:
  137. return False
  138. # 选中对应章节
  139. while 1:
  140. self.screenshot()
  141. self.O_E_EXPLORATION_LEVEL_NUMBER.keyword = explorationConfig.exploration_config.exploration_level
  142. if self.ocr_appear_click(self.O_E_EXPLORATION_LEVEL_NUMBER):
  143. self.wait_until_appear(self.I_E_EXPLORATION_CLICK, wait_time=3)
  144. if self.appear(self.I_E_EXPLORATION_CLICK):
  145. break
  146. return True
  147. # 候补:
  148. def enter_settings_and_do_operations(self):
  149. # 打开设置
  150. self.click(self.C_CLICK_SETTINGS)
  151. while 1:
  152. self.screenshot()
  153. if self.appear(self.I_E_OPEN_SETTINGS):
  154. break
  155. else:
  156. self.click(self.C_CLICK_SETTINGS)
  157. if self.wait_until_appear(self.I_E_OPEN_SETTINGS):
  158. break
  159. # 候补出战数量识别
  160. self.screenshot()
  161. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  162. if cu >= 40:
  163. self.appear_then_click(self.I_E_SURE_BUTTON)
  164. return
  165. else:
  166. self.add_shiki()
  167. # 添加式神
  168. def add_shiki(self):
  169. self.screenshot()
  170. # 点击展开式神稀有度选择界面
  171. self.click(self.C_CLICK_ALL_SHIKI)
  172. while 1:
  173. self.screenshot()
  174. # 成功进入稀有度选择界面
  175. if self.appear(self.I_E_ENTER_CHOOSE_RARITY):
  176. if self.config.exploration.exploration_config.choose_rarity == ChooseRarity.N:
  177. # N 卡
  178. self.click(self.C_CLICK_N_SHIKI)
  179. else:
  180. # 素材卡
  181. self.appear_then_click(self.I_E_ENTER_CHOOSE_RARITY)
  182. self.screenshot()
  183. if self.appear(self.I_E_N_RARITY):
  184. # 进入N卡添加界面 退出
  185. break
  186. if self.appear(self.I_E_S_RARITY):
  187. # 进入素材添加界面 退出
  188. break
  189. # 选中候补出战框
  190. self.click(self.C_CLICK_STANDBY_TEAM)
  191. # 移动至未候补的狗粮
  192. while 1:
  193. # 慢一点
  194. time.sleep(0.5)
  195. self.screenshot()
  196. if self.appear(self.I_E_RATATE_EXSIT):
  197. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT)
  198. else:
  199. break
  200. while 1:
  201. # 候补出战数量识别
  202. self.screenshot()
  203. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  204. if cu >= 40:
  205. break
  206. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT_ONE)
  207. # 慢一点
  208. time.sleep(0.5)
  209. self.screenshot()
  210. self.click(self.C_CLICK_ROTATE_1)
  211. self.device.click_record_clear()
  212. self.appear_then_click(self.I_E_SURE_BUTTON)
  213. # 探索战斗流程
  214. def do_battle(self):
  215. while 1:
  216. self.screenshot()
  217. # 战后奖励
  218. if self.appear(self.I_BATTLE_REWARD) and not self.appear(self.I_GET_REWARD):
  219. self.click(self.I_BATTLE_REWARD)
  220. # boss 战
  221. if self.appear_then_click(self.I_BOSS_BATTLE_BUTTON):
  222. if self.wait_until_appear(self.I_BATTLE_START, wait_time=5):
  223. self.run_general_battle(self.config.exploration.general_battle_config)
  224. else:
  225. continue
  226. # 小怪 战
  227. if self.appear_then_click(self.I_NORMAL_BATTLE_BUTTON):
  228. if self.wait_until_appear(self.I_BATTLE_START, wait_time=5):
  229. self.run_general_battle(self.config.exploration.general_battle_config)
  230. else:
  231. continue
  232. # 滑动
  233. elif self.appear(self.I_E_AUTO_ROTATE_ON) or self.appear(self.I_GET_REWARD):
  234. self.swipe(self.S_SWIPE_BACKGROUND_RIGHT)
  235. # 结束流程
  236. if self.appear(self.I_E_EXPLORATION_CLICK) or self.appear(self.I_EXPLORATION_TITLE):
  237. break
  238. # 战斗流程
  239. def battle_process(self):
  240. # 进入指定章节
  241. self.screenshot()
  242. if self.appear_then_click(self.I_E_EXPLORATION_CLICK):
  243. self.appear_then_click(self.I_E_SETTINGS_BUTTON)
  244. # ************************* 进入设置并操作 *******************
  245. # 候补以及自动轮换打开:
  246. # 自动轮换功能打开
  247. while 1:
  248. self.screenshot()
  249. # 自动轮换开着 则跳过
  250. if self.appear(self.I_E_AUTO_ROTATE_ON):
  251. break
  252. # 自动轮换关着 则打开
  253. if self.appear_then_click(self.I_E_AUTO_ROTATE_OFF):
  254. if self.appear(self.I_E_AUTO_ROTATE_ON):
  255. break
  256. # 自动添加候补式神
  257. if self.config.exploration.exploration_config.auto_rotate == AutoRotate.yes:
  258. self.enter_settings_and_do_operations()
  259. # 修复卡结算问题
  260. # 卡结算是因为没有设置锁定队伍,修改后无论是否锁定都不会因为没有锁定队伍而卡在结算界面
  261. if not self.config.exploration.general_battle_config.lock_team_enable:
  262. self.config.exploration.general_battle_config.lock_team_enable = True
  263. # 进入战斗环节
  264. self.do_battle()
  265. if __name__ == "__main__":
  266. from module.config.config import Config
  267. from module.device.device import Device
  268. config = Config('oas1')
  269. device = Device(config)
  270. t = ScriptTask(config, device)
  271. t.config.exploration.exploration_config.exploration_level = '第二十八章'
  272. t.run()
  273. # t.battle_process()
  274. # t.enter_settings_and_do_operations()