base.py 14 KB

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