base.py 15 KB

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