base.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import numpy as np
  6. import random
  7. from enum import Enum
  8. from cached_property import cached_property
  9. from datetime import timedelta, datetime
  10. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  11. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  12. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  13. from tasks.Component.ReplaceShikigami.replace_shikigami import ReplaceShikigami
  14. from tasks.Exploration.assets import ExplorationAssets
  15. from tasks.Exploration.config import ChooseRarity, AutoRotate, AttackNumber, UpType
  16. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  17. from tasks.GameUi.game_ui import GameUi
  18. from tasks.GameUi.page import page_exploration, page_shikigami_records, page_main
  19. from tasks.RealmRaid.script_task import ScriptTask as RealmRaidScriptTask
  20. from tasks.Utils.config_enum import ShikigamiClass
  21. from module.logger import logger
  22. from module.exception import RequestHumanTakeover, TaskEnd
  23. from module.atom.image_grid import ImageGrid
  24. from module.atom.animate import RuleAnimate
  25. from module.base.utils import load_image
  26. class Scene(Enum):
  27. UNKNOWN = 0 #
  28. WORLD = 1 # 探索大世界
  29. ENTRANCE = 2 # 入口弹窗
  30. MAIN = 3 # 探索里面
  31. BATTLE_PREPARE = 4 # 战斗准备
  32. BATTLE_FIGHTING = 5 # 战斗中
  33. TEAM = 6 # 组队
  34. class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, ReplaceShikigami, SwitchSoul, ExplorationAssets):
  35. minions_cnt = 0
  36. @cached_property
  37. def _config(self):
  38. self.config.exploration.general_battle_config.lock_team_enable = True
  39. limit_time = self.config.exploration.exploration_config.limit_time
  40. self.limit_time: timedelta = timedelta(
  41. hours=limit_time.hour,
  42. minutes=limit_time.minute,
  43. seconds=limit_time.second
  44. )
  45. return self.config.model.exploration
  46. @cached_property
  47. def _match_end(self):
  48. return RuleAnimate(self.I_SWIPE_END)
  49. def get_current_scene(self, reuse_screenshot: bool = True) -> Scene:
  50. if not reuse_screenshot:
  51. self.screenshot()
  52. if self.appear(self.I_CHECK_EXPLORATION) and not self.appear(self.I_E_SETTINGS_BUTTON):
  53. return Scene.WORLD
  54. elif self.appear(self.I_UI_BACK_RED) and self.appear(self.I_E_EXPLORATION_CLICK):
  55. return Scene.ENTRANCE
  56. elif self.appear(self.I_E_SETTINGS_BUTTON) or self.appear(self.I_E_AUTO_ROTATE_ON) or self.appear(self.I_E_AUTO_ROTATE_OFF):
  57. return Scene.MAIN
  58. elif self.is_in_prepare():
  59. return Scene.BATTLE_PREPARE
  60. elif self.is_in_battle():
  61. return Scene.BATTLE_FIGHTING
  62. elif self.is_in_room() or self.appear(self.I_CREATE_ENSURE):
  63. return Scene.TEAM
  64. logger.info("Unknown scene")
  65. return Scene.UNKNOWN
  66. def pre_process(self):
  67. explorationConfig = self._config
  68. if explorationConfig.switch_soul_config.enable:
  69. self.ui_get_current_page()
  70. self.ui_goto(page_shikigami_records)
  71. self.run_switch_soul(explorationConfig.switch_soul_config.switch_group_team)
  72. if explorationConfig.switch_soul_config.enable_switch_by_name:
  73. self.ui_get_current_page()
  74. self.ui_goto(page_shikigami_records)
  75. self.run_switch_soul_by_name(explorationConfig.switch_soul_config.group_name,
  76. explorationConfig.switch_soul_config.team_name)
  77. # 开启加成
  78. con = self.config.exploration.exploration_config
  79. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  80. self.ui_get_current_page()
  81. self.ui_goto(page_main)
  82. self.open_buff()
  83. if con.buff_gold_50_click:
  84. self.gold_50()
  85. if con.buff_gold_100_click:
  86. self.gold_100()
  87. if con.buff_exp_50_click:
  88. self.exp_50()
  89. if con.buff_exp_100_click:
  90. self.exp_100()
  91. self.close_buff()
  92. self.ui_get_current_page()
  93. # 探索页面
  94. self.ui_goto(page_exploration)
  95. def post_process(self):
  96. self.wait_until_stable(self.I_UI_BACK_RED)
  97. if self.appear(self.I_UI_BACK_RED):
  98. self.ui_click_until_disappear(self.I_UI_BACK_RED)
  99. self.ui_get_current_page()
  100. self.ui_goto(page_main)
  101. con = self._config.exploration_config
  102. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  103. self.open_buff()
  104. self.gold_50(is_open=False)
  105. self.gold_100(is_open=False)
  106. self.exp_50(is_open=False)
  107. self.exp_100(is_open=False)
  108. self.close_buff()
  109. self.set_next_run(task='Exploration', success=True, finish=False)
  110. raise TaskEnd
  111. # 打开指定的章节:
  112. def open_expect_level(self):
  113. swipeCount = 0
  114. while 1:
  115. # 探索的 config
  116. explorationConfig = self.config.exploration
  117. # 判断有无目标章节
  118. self.screenshot()
  119. # 获取当前章节名
  120. results = self.O_E_EXPLORATION_LEVEL_NUMBER.detect_and_ocr(self.device.image)
  121. text1 = [result.ocr_text for result in results]
  122. # 判断当前章节有无目标章节
  123. result = set(text1).intersection({explorationConfig.exploration_config.exploration_level})
  124. # 有则跳出检测
  125. if self.appear(self.I_E_EXPLORATION_CLICK) or result and len(result) > 0:
  126. break
  127. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  128. continue
  129. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  130. continue
  131. self.device.click_record_clear()
  132. self.swipe(self.S_SWIPE_LEVEL_UP)
  133. swipeCount += 1
  134. if swipeCount >= 25:
  135. return False
  136. # 选中对应章节
  137. while 1:
  138. self.screenshot()
  139. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  140. continue
  141. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  142. continue
  143. self.O_E_EXPLORATION_LEVEL_NUMBER.keyword = explorationConfig.exploration_config.exploration_level
  144. if self.ocr_appear_click(self.O_E_EXPLORATION_LEVEL_NUMBER):
  145. self.wait_until_appear(self.I_E_EXPLORATION_CLICK, wait_time=3)
  146. if self.appear(self.I_E_EXPLORATION_CLICK):
  147. break
  148. if self.is_in_room():
  149. break
  150. return True
  151. # 候补:
  152. def enter_settings_and_do_operations(self):
  153. # 打开设置
  154. while 1:
  155. self.screenshot()
  156. if self.appear(self.I_E_OPEN_SETTINGS):
  157. logger.info("Open settings")
  158. break
  159. if self.is_in_battle():
  160. logger.warning('Opening settings failed due to now in battle')
  161. return
  162. if self.click(self.C_CLICK_SETTINGS, interval=2):
  163. continue
  164. # 候补出战数量识别
  165. self.screenshot()
  166. if not self.appear(self.I_E_OPEN_SETTINGS):
  167. logger.warning('Opening settings failed due to now in battle')
  168. return
  169. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  170. if cu >= 10:
  171. logger.info("Alternate number is enough")
  172. self.ui_click_until_disappear(self.I_E_SURE_BUTTON)
  173. return
  174. else:
  175. self.add_shiki()
  176. # 添加式神
  177. def add_shiki(self, screenshot=True):
  178. if screenshot:
  179. self.screenshot()
  180. if not self.appear(self.I_E_OPEN_SETTINGS):
  181. logger.warning('Opening settings failed due to now in battle')
  182. return
  183. choose_rarity = self._config.exploration_config.choose_rarity
  184. rarity = ShikigamiClass.N if choose_rarity == ChooseRarity.N else ShikigamiClass.MATERIAL
  185. self.switch_shikigami_class(rarity)
  186. self.click(self.C_CLICK_STANDBY_TEAM)
  187. # 移动至未候补的狗粮
  188. while 1:
  189. # 慢一点
  190. time.sleep(0.5)
  191. self.screenshot()
  192. if not self.appear(self.I_E_OPEN_SETTINGS):
  193. logger.warning('Opening settings failed due to now in battle')
  194. return
  195. if self.appear(self.I_E_RATATE_EXSIT):
  196. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT)
  197. else:
  198. break
  199. while 1:
  200. # 候补出战数量识别
  201. self.screenshot()
  202. if not self.appear(self.I_E_OPEN_SETTINGS):
  203. logger.warning('Opening settings failed due to now in battle')
  204. return
  205. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  206. if cu >= 40:
  207. break
  208. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT_ONE)
  209. # 慢一点
  210. time.sleep(0.5)
  211. self.screenshot()
  212. self.click(self.L_ROTATE_1)
  213. self.device.click_record_clear()
  214. self.appear_then_click(self.I_E_SURE_BUTTON)
  215. # 找up按钮
  216. def search_up_fight(self, up_type: UpType = None):
  217. if up_type is None:
  218. up_type = self._config.exploration_config.up_type
  219. if up_type != UpType.ALL:
  220. match up_type:
  221. case UpType.EXP:
  222. find_flag = self.I_UP_EXP
  223. case UpType.COIN:
  224. find_flag = self.I_UP_COIN
  225. case UpType.DARUMAA:
  226. find_flag = self.I_UP_DARUMA
  227. case _:
  228. find_flag = self.I_UP_EXP
  229. appear = self.appear(find_flag)
  230. if not appear:
  231. return None
  232. logger.info(f'Found up type: {up_type} at {find_flag.roi_front}')
  233. x, y, _, _ = find_flag.roi_front
  234. x_center, y_center = find_flag.front_center()
  235. roi_back_y = max(0, y - 300)
  236. roi_back_h = y - 20 - roi_back_y
  237. roi_back_x = max(0, x - 160)
  238. roi_back_w = min(1280, x + 200) - roi_back_x
  239. # self.I_NORMAL_BATTLE_BUTTON.roi_back = [roi_back_x, roi_back_y, roi_back_w, roi_back_h]
  240. logger.info(f'It will search normal battle button at {roi_back_x, roi_back_y, roi_back_w, roi_back_h}')
  241. matches = self.I_NORMAL_BATTLE_BUTTON.match_all(
  242. image=self.device.image,
  243. threshold=0.9,
  244. roi=[roi_back_x, roi_back_y, roi_back_w, roi_back_h]
  245. )
  246. if not matches:
  247. return None
  248. distances = []
  249. for match in matches:
  250. x_match, y_match = match[1], match[2]
  251. distance = np.linalg.norm(
  252. np.array([x_center, y_center]) - np.array([x_match, y_match])
  253. )
  254. distances.append((distance, match))
  255. distances.sort(key=lambda x: x[0], reverse=False)
  256. match = distances[0][1]
  257. roi_front = list(match[1:]) # x,y,w,h
  258. self.I_NORMAL_BATTLE_BUTTON.roi_front = roi_front
  259. logger.info(f"Found normal battle button at {roi_front}")
  260. return self.I_NORMAL_BATTLE_BUTTON
  261. if self.appear(self.I_NORMAL_BATTLE_BUTTON):
  262. return self.I_NORMAL_BATTLE_BUTTON
  263. return None
  264. def activate_realm_raid(self, con_scrolls, con) -> None:
  265. # 判断是否开启突破票检测
  266. if not con_scrolls.scrolls_enable:
  267. return
  268. if self.appear(self.I_E_EXPLORATION_CLICK) and self.appear(self.I_EXP_CREATE_TEAM):
  269. cu, res, total = self.O_REALM_RAID_NUMBER1.ocr(self.device.image)
  270. else:
  271. cu, res, total = self.O_REALM_RAID_NUMBER.ocr(self.device.image)
  272. # 判断突破票数量
  273. if cu < con_scrolls.scrolls_threshold:
  274. return
  275. # 关闭加成
  276. if self.appear(self.I_RED_CLOSE):
  277. self.ui_click_until_disappear(self.I_RED_CLOSE)
  278. if self.appear(self.I_UI_CANCEL):
  279. self.ui_click_until_disappear(self.I_UI_CANCEL)
  280. if self.appear(self.I_UI_CANCEL_SAMLL):
  281. self.ui_click_until_disappear(self.I_UI_CANCEL_SAMLL)
  282. self.ui_goto(page_main)
  283. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  284. self.open_buff()
  285. self.gold_50(is_open=False)
  286. self.gold_100(is_open=False)
  287. self.exp_50(is_open=False)
  288. self.exp_100(is_open=False)
  289. self.close_buff()
  290. # 设置下次执行行时间
  291. logger.info("RealmRaid and Exploration set_next_run !")
  292. next_run = datetime.now() + con_scrolls.scrolls_cd
  293. self.set_next_run(task='Exploration', success=False, finish=False, target=next_run)
  294. self.set_next_run(task='RealmRaid', success=False, finish=False, target=datetime.now())
  295. raise TaskEnd
  296. #
  297. def check_exit(self) -> bool:
  298. # True 表示要退出这个任务
  299. if self.minions_cnt >= self._config.exploration_config.minions_cnt:
  300. logger.info('Minions count is enough, exit')
  301. return True
  302. if datetime.now() - self.start_time >= self.limit_time:
  303. logger.info('Exploration time limit out')
  304. return True
  305. self.activate_realm_raid(self._config.scrolls, self._config.exploration_config)
  306. return False
  307. def quit_explore(self):
  308. logger.info('Quit explore')
  309. while 1:
  310. self.screenshot()
  311. if self.appear(self.I_UI_BACK_RED) and self.appear(self.I_E_EXPLORATION_CLICK):
  312. break
  313. if self.appear_then_click(self.I_E_EXIT_CONFIRM, interval=0.8):
  314. continue
  315. if self.appear_then_click(self.I_UI_BACK_BLUE, interval=1.5):
  316. continue
  317. def fire(self, button) -> bool:
  318. self.ui_click_until_disappear(button, interval=3)
  319. self.screenshot()
  320. if (self.appear(self.I_E_SETTINGS_BUTTON) or
  321. self.appear(self.I_E_AUTO_ROTATE_ON) or
  322. self.appear(self.I_E_AUTO_ROTATE_OFF)):
  323. # 如果还在探索说明,这个是显示滑动导致挑战按钮不在范围内
  324. logger.warning('Fire button disappear, but still in exploration')
  325. return False
  326. self.run_general_battle(self._config.general_battle_config)
  327. self.minions_cnt += 1
  328. return True
  329. if __name__ == "__main__":
  330. from module.config.config import Config
  331. from module.device.device import Device
  332. config = Config('oas1')
  333. device = Device(config)
  334. t = BaseExploration(config, device)
  335. t.screenshot()
  336. # IMAGE_FILE = r"C:\Users\萌萌哒\Desktop\QQ20240818-163854.png"
  337. # image = load_image(IMAGE_FILE)
  338. # t.device.image = image
  339. while 1:
  340. # print(t.search_up_fight(UpType.EXP))
  341. t.screenshot()
  342. print(t.I_UP_DARUMA.test_match(t.device.image))
  343. time.sleep(0.2)
  344. from PIL import Image
  345. # Image.fromarray(t.device.image.astype(np.uint8)).show()