base.py 15 KB

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