script_task.py 12 KB

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