solo.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from time import sleep
  5. from cached_property import cached_property
  6. from module.logger import logger
  7. from module.base.timer import Timer
  8. from tasks.Component.GeneralInvite.config_invite import InviteConfig, InviteNumber, FindMode
  9. from tasks.Exploration.base import BaseExploration, UpType, Scene
  10. from tasks.Exploration.config import ChooseRarity, AutoRotate, UserStatus, ExplorationLevel
  11. class SoloExploration(BaseExploration):
  12. INVITE_FLAG_OFF = (157, 109, 83)
  13. INVITE_FLAG_ON = (227, 193, 153)
  14. @cached_property
  15. def _invite_config(self) -> InviteConfig:
  16. return InviteConfig(
  17. invite_number=InviteNumber.ONE,
  18. friend_1=self._config.invite_config.friend_1,
  19. friend_2='',
  20. find_mode=self._config.invite_config.find_mode,
  21. wait_time=self._config.invite_config.wait_time,
  22. default_invite=False
  23. )
  24. def run_solo(self):
  25. logger.hr('solo')
  26. explore_init = False
  27. search_fail_cnt = 0
  28. while 1:
  29. self.screenshot()
  30. scene = self.get_current_scene()
  31. logger.info(f'[run_solo] Current scene: {scene.name}')
  32. #
  33. if scene == Scene.WORLD:
  34. # 打开右边箭头
  35. self.ui_click(click=self.I_EXP_ARROW_LEFT, stop=self.I_EXP_ARROW_RIGHT, interval=2, timeout=3)
  36. if self.appear(self.I_TREASURE_BOX_CLICK):
  37. # 宝箱
  38. logger.info('Treasure box appear, get it.')
  39. self.ui_click_until_disappear(self.I_TREASURE_BOX_CLICK)
  40. # 处理识别页面出错的情况
  41. if not self.appear(self.I_CHECK_EXPLORATION):
  42. continue
  43. if self.check_exit():
  44. break
  45. self.open_expect_level()
  46. explore_init = False
  47. continue
  48. #
  49. elif scene == Scene.ENTRANCE:
  50. if self.check_exit():
  51. break
  52. self.ui_click(self.I_E_EXPLORATION_CLICK, stop=self.I_E_SETTINGS_BUTTON)
  53. explore_init = False
  54. continue
  55. #
  56. elif scene == Scene.MAIN:
  57. if not explore_init:
  58. self.ui_click(self.I_E_AUTO_ROTATE_OFF, stop=self.I_E_AUTO_ROTATE_ON)
  59. if self._config.exploration_config.auto_rotate == AutoRotate.yes:
  60. self.enter_settings_and_do_operations()
  61. explore_init = True
  62. continue
  63. # 小纸人
  64. if self.appear(self.I_BATTLE_REWARD):
  65. if self.ui_get_reward(self.I_BATTLE_REWARD):
  66. continue
  67. # boss
  68. if self.appear(self.I_BOSS_BATTLE_BUTTON):
  69. if self.fire(self.I_BOSS_BATTLE_BUTTON):
  70. logger.info(f'Boss battle, minions cnt {self.minions_cnt}')
  71. continue
  72. # 小怪
  73. fight_button = self.search_up_fight()
  74. if fight_button is not None:
  75. if self.fire(fight_button):
  76. logger.info(f'Fight, minions cnt {self.minions_cnt}')
  77. continue
  78. # 向后拉,寻找怪
  79. if search_fail_cnt >= 4:
  80. search_fail_cnt = 0
  81. if (self._config.exploration_config.exploration_level == ExplorationLevel.EXPLORATION_28\
  82. and self.appear(self.I_SWIPE_END))\
  83. or self._match_end.stable(self.device.image, refresh_after_stable=True):
  84. self.quit_explore()
  85. continue
  86. if self.swipe(self.S_SWIPE_BACKGROUND_RIGHT, interval=3):
  87. continue
  88. else:
  89. search_fail_cnt += 1
  90. #
  91. elif scene == Scene.BATTLE_PREPARE or scene == Scene.BATTLE_FIGHTING:
  92. self.check_take_over_battle(is_screenshot=False, config=self._config.general_battle_config)
  93. elif scene == Scene.UNKNOWN:
  94. continue
  95. def run_leader(self):
  96. logger.hr('leader')
  97. explore_init = False
  98. search_fail_cnt = 0
  99. friend_leave_timer = Timer(10)
  100. while 1:
  101. self.screenshot()
  102. scene = self.get_current_scene()
  103. logger.info(f'[run_leader] Current scene: {scene.name}')
  104. # 探索大世界
  105. if scene == Scene.WORLD:
  106. self.wait_until_stable(self.I_CHECK_EXPLORATION, timeout=5)
  107. # 打开右边箭头
  108. self.ui_click(click=self.I_EXP_ARROW_LEFT, stop=self.I_EXP_ARROW_RIGHT,timeout=3, interval=2)
  109. # 处理识别页面出错的情况
  110. if not self.appear(self.I_CHECK_EXPLORATION):
  111. continue
  112. if self.appear(self.I_TREASURE_BOX_CLICK):
  113. # 宝箱
  114. logger.info('Treasure box appear, get it.')
  115. self.wait_until_stable(self.I_UI_CANCEL, timer=Timer(0.6, 1))
  116. while 1:
  117. self.screenshot()
  118. if self.appear(self.I_REWARD):
  119. self.ui_click_until_disappear(self.I_REWARD)
  120. logger.info('Get reward.')
  121. break
  122. if self.ui_reward_appear_click():
  123. continue
  124. if self.appear_then_click(self.I_UI_CANCEL, interval=0.8):
  125. continue
  126. if self.appear_then_click(self.I_TREASURE_BOX_CLICK, interval=1):
  127. continue
  128. if self.check_exit():
  129. self.wait_until_stable(self.I_UI_CANCEL, timer=Timer(0.6, 2))
  130. if self.appear(self.I_UI_CANCEL):
  131. self.ui_click_until_disappear(self.I_UI_CANCEL)
  132. break
  133. if self.appear(self.I_UI_CONFIRM):
  134. self.ui_click_until_disappear(self.I_UI_CONFIRM)
  135. # 可以加一下,清空第一次 explore_init
  136. continue
  137. self.open_expect_level()
  138. explore_init = False
  139. continue
  140. # 邀请好友, 非常有可能是后面邀请好友,然后直接跳到组队了
  141. elif scene == Scene.ENTRANCE:
  142. while 1:
  143. self.screenshot()
  144. if self.is_in_room():
  145. break
  146. if self.appear_then_click(self.I_ENSURE_PRIVATE_FALSE, interval=0.5):
  147. continue
  148. if self.appear_then_click(self.I_ENSURE_PRIVATE_FALSE_2, interval=0.5):
  149. continue
  150. if self.appear_then_click(self.I_EXP_CREATE_TEAM, interval=1):
  151. continue
  152. if self.appear_then_click(self.I_EXP_CREATE_ENSURE, interval=2):
  153. continue
  154. #
  155. elif scene == Scene.TEAM:
  156. self.wait_until_stable(self.I_ADD_2, timer=Timer(0.8, 1))
  157. if self.appear(self.I_FIRE, threshold=0.8) and not self.appear(self.I_ADD_2):
  158. self.ui_click_until_disappear(self.I_FIRE, interval=1)
  159. continue
  160. if self.appear(self.I_ADD_2) and self.run_invite(config=self._invite_config, is_first=True):
  161. continue
  162. else:
  163. logger.warning('Invite failed, quit')
  164. while 1:
  165. self.screenshot()
  166. if self.appear(self.I_CHECK_EXPLORATION):
  167. break
  168. if self.appear_then_click(self.I_UI_CONFIRM, interval=0.5):
  169. continue
  170. if self.appear_then_click(self.I_UI_BACK_RED, interval=0.7):
  171. continue
  172. if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=1):
  173. continue
  174. break
  175. ##
  176. elif scene == Scene.MAIN:
  177. if not explore_init:
  178. self.ui_click(self.I_E_AUTO_ROTATE_OFF, stop=self.I_E_AUTO_ROTATE_ON)
  179. if self._config.exploration_config.auto_rotate == AutoRotate.yes:
  180. self.enter_settings_and_do_operations()
  181. friend_leave_timer = Timer(10)
  182. explore_init = True
  183. continue
  184. # 小纸人
  185. if self.appear(self.I_BATTLE_REWARD):
  186. if self.ui_get_reward(self.I_BATTLE_REWARD):
  187. continue
  188. # 中途有人跑路
  189. if not self.appear(self.I_TEAM_EMOJI):
  190. if not friend_leave_timer.started():
  191. logger.warning('Mate leave, start timer')
  192. friend_leave_timer = Timer(10)
  193. friend_leave_timer.start()
  194. elif friend_leave_timer.started() and friend_leave_timer.reached():
  195. logger.warning('Mate leave timer reached')
  196. logger.warning('Exit team')
  197. self.quit_explore()
  198. continue
  199. else:
  200. logger.warning('Team emoji appear again, clear friend_leave_timer')
  201. friend_leave_timer = Timer(10)
  202. # boss
  203. if self.appear(self.I_BOSS_BATTLE_BUTTON):
  204. if self.fire(self.I_BOSS_BATTLE_BUTTON):
  205. logger.info(f'Boss battle, minions cnt {self.minions_cnt}')
  206. continue
  207. # 小怪
  208. fight_button = self.search_up_fight()
  209. if fight_button is not None:
  210. if self.fire(fight_button):
  211. logger.info(f'Fight, minions cnt {self.minions_cnt}')
  212. continue
  213. # 向后拉,寻找怪
  214. if search_fail_cnt >= 4:
  215. search_fail_cnt = 0
  216. if (self._config.exploration_config.exploration_level == ExplorationLevel.EXPLORATION_28\
  217. and self.appear(self.I_SWIPE_END))\
  218. or self._match_end.stable(self.device.image, refresh_after_stable=True):
  219. self.quit_explore()
  220. continue
  221. if self.swipe(self.S_SWIPE_BACKGROUND_RIGHT, interval=4.5):
  222. continue
  223. else:
  224. search_fail_cnt += 1
  225. #
  226. elif scene == Scene.BATTLE_PREPARE or scene == Scene.BATTLE_FIGHTING:
  227. self.check_take_over_battle(is_screenshot=False, config=self._config.general_battle_config)
  228. elif scene == Scene.UNKNOWN:
  229. continue
  230. def run_member(self):
  231. logger.hr('member')
  232. explore_init = False
  233. wait_timer = Timer(50)
  234. friend_leave_timer = Timer(10)
  235. while 1:
  236. self.screenshot()
  237. scene = self.get_current_scene()
  238. logger.info(f'[run_member] Current scene: {scene.name}')
  239. #
  240. if scene == Scene.WORLD:
  241. # 打开右边箭头
  242. self.ui_click(click=self.I_EXP_ARROW_LEFT, stop=self.I_EXP_ARROW_RIGHT, interval=2)
  243. # 处理识别页面出错的情况
  244. if not self.appear(self.I_CHECK_EXPLORATION):
  245. continue
  246. if self.appear(self.I_TREASURE_BOX_CLICK):
  247. # 宝箱
  248. logger.info('Treasure box appear, get it.')
  249. self.ui_click_until_disappear(self.I_TREASURE_BOX_CLICK)
  250. if self.check_exit():
  251. break
  252. if self.check_then_accept():
  253. pass
  254. if wait_timer.started() and wait_timer.reached():
  255. logger.warning('Wait timer reached')
  256. break
  257. explore_init = False
  258. continue
  259. #
  260. elif scene == Scene.ENTRANCE:
  261. self.ui_click(self.I_UI_BACK_YELLOW,stop=self.I_CHECK_EXPLORATION)
  262. #
  263. elif scene == Scene.TEAM:
  264. continue
  265. #
  266. elif scene == Scene.MAIN:
  267. if not explore_init:
  268. self.ui_click(self.I_E_AUTO_ROTATE_OFF, stop=self.I_E_AUTO_ROTATE_ON)
  269. if self._config.exploration_config.auto_rotate == AutoRotate.yes:
  270. self.enter_settings_and_do_operations()
  271. explore_init = True
  272. continue
  273. # 小纸人
  274. if self.appear(self.I_BATTLE_REWARD):
  275. if self.ui_get_reward(self.I_BATTLE_REWARD):
  276. continue
  277. #
  278. if not self.appear(self.I_TEAM_EMOJI):
  279. logger.warning('Team emoji not appear')
  280. if not friend_leave_timer.started():
  281. logger.warning('Mate leave, start timer')
  282. friend_leave_timer = Timer(10)
  283. friend_leave_timer.start()
  284. elif friend_leave_timer.started() and friend_leave_timer.reached():
  285. logger.warning('Mate leave timer reached')
  286. logger.warning('Exit team')
  287. self.quit_explore()
  288. wait_timer = Timer(50)
  289. wait_timer.start()
  290. continue
  291. else:
  292. logger.warning('Team emoji appear again, clear friend_leave_timer')
  293. friend_leave_timer = Timer(10)
  294. #
  295. elif scene == Scene.BATTLE_PREPARE or scene == Scene.BATTLE_FIGHTING:
  296. logger.info('[run_member] Handling scene: BATTLE')
  297. self.check_take_over_battle(is_screenshot=False, config=self._config.general_battle_config)
  298. elif scene == Scene.UNKNOWN:
  299. continue
  300. def invite_friend(self, name: str = None, find_mode: FindMode = FindMode.AUTO_FIND) -> bool:
  301. logger.info('Click add to invite friend')
  302. # 点击+号
  303. while 1:
  304. self.screenshot()
  305. if self.appear(self.I_LOAD_FRIEND):
  306. break
  307. if self.appear(self.I_INVITE_ENSURE):
  308. break
  309. if self.appear_then_click(self.I_ADD_2, interval=1):
  310. continue
  311. if self.appear_then_click(self.I_ADD_5_4, interval=1):
  312. continue
  313. friend_class = []
  314. class_ocr = [self.O_F_LIST_1, self.O_F_LIST_2, self.O_F_LIST_3, self.O_F_LIST_4]
  315. class_index = 0
  316. list_1 = self.O_F_LIST_1.ocr(self.device.image)
  317. list_2 = self.O_F_LIST_2.ocr(self.device.image)
  318. list_3 = self.O_F_LIST_3.ocr(self.device.image)
  319. list_4 = self.O_F_LIST_4.ocr(self.device.image)
  320. list_1 = list_1.replace(' ', '').replace('、', '')
  321. list_2 = list_2.replace(' ', '').replace('、', '')
  322. list_3 = list_3.replace(' ', '').replace('、', '')
  323. if list_1 is not None and list_1 != '' and list_1 in self.friend_class:
  324. friend_class.append(list_1)
  325. if list_2 is not None and list_2 != '' and list_2 in self.friend_class:
  326. friend_class.append(list_2)
  327. if list_3 is not None and list_3 != '' and list_3 in self.friend_class:
  328. friend_class.append(list_3)
  329. if list_4 is not None and list_4 != '' and list_4 in self.friend_class:
  330. friend_class.append(list_4)
  331. for i in range(len(friend_class)):
  332. if friend_class[i] == '蔡友':
  333. friend_class[i] = '寮友'
  334. elif friend_class[i] == '路区':
  335. friend_class[i] = '跨区'
  336. elif friend_class[i] == '察友':
  337. friend_class[i] = '寮友'
  338. elif friend_class[i] == '区':
  339. friend_class[i] = '跨区'
  340. logger.info(f'Friend class: {friend_class}')
  341. is_select: bool = False # 是否选中了好友
  342. if find_mode == FindMode.RECENT_FRIEND:
  343. logger.info('Find recent friend')
  344. # 获取’最近‘在friend_class中的index
  345. if '最近' not in friend_class:
  346. logger.warning('No recent friend')
  347. return False
  348. recent_index = friend_class.index('最近')
  349. while recent_index == 1:
  350. self.screenshot()
  351. if self.appear(self.I_FLAG_2_ON):
  352. break
  353. if self.appear_then_click(self.I_FLAG_2_OFF, interval=1):
  354. continue
  355. logger.info(f'Now find friend in ”最近“')
  356. sleep(1)
  357. if not is_select:
  358. if self.detect_select(name):
  359. is_select = True
  360. sleep(1)
  361. if not is_select:
  362. if self.detect_select(name):
  363. is_select = True
  364. for index in range(len(friend_class)):
  365. # 如果不是自动寻找,就跳过
  366. if find_mode != FindMode.AUTO_FIND:
  367. continue
  368. # 如果已经选中了好友,就不需要再选中了
  369. if is_select:
  370. continue
  371. # 首先切换到不同的好友列表
  372. while index == 0:
  373. self.screenshot()
  374. if self.I_FLAG_1_ON.match_mean_color(self.device.image, self.INVITE_FLAG_ON, 10):
  375. break
  376. if self.click(self.I_FLAG_1_OFF, interval=1):
  377. continue
  378. while index == 1:
  379. self.screenshot()
  380. if self.I_FLAG_2_ON.match_mean_color(self.device.image, self.INVITE_FLAG_ON, 10):
  381. break
  382. if self.click(self.I_FLAG_2_OFF, interval=1):
  383. continue
  384. while index == 2:
  385. self.screenshot()
  386. if self.I_FLAG_3_ON.match_mean_color(self.device.image, self.INVITE_FLAG_ON, 10):
  387. break
  388. if self.click(self.I_FLAG_3_OFF, interval=1):
  389. continue
  390. while index == 3:
  391. self.screenshot()
  392. if self.I_FLAG_4_ON.match_mean_color(self.device.image, self.INVITE_FLAG_ON, 10):
  393. break
  394. if self.click(self.I_FLAG_4_OFF, interval=1):
  395. continue
  396. # 选中好友, 在这里游戏获取在线的好友并不是很快,根据不同的设备会有不同的时间,而且没有什么元素提供我们来判断
  397. # 所以这里就直接等待一段时间
  398. logger.info(f'Now find friend in {friend_class[index]}')
  399. sleep(1)
  400. if not is_select:
  401. if self.detect_select(name):
  402. is_select = True
  403. sleep(1)
  404. if not is_select:
  405. if self.detect_select(name):
  406. is_select = True
  407. # 点击确定
  408. logger.info('Click invite ensure')
  409. if not self.appear(self.I_INVITE_ENSURE):
  410. logger.warning('No appear invite ensure while invite friend')
  411. while 1:
  412. self.screenshot()
  413. if not self.appear(self.I_INVITE_ENSURE):
  414. break
  415. if self.appear_then_click(self.I_INVITE_ENSURE):
  416. continue
  417. # 哪怕没有找到好友也有点击 确认 以退出好友列表
  418. if not is_select:
  419. logger.warning('No find friend')
  420. # 这个时候任务运行失败
  421. logger.info('Task failed')
  422. return False
  423. return True
  424. class ScriptTask(SoloExploration):
  425. def run(self):
  426. logger.hr('exploration')
  427. random_click_cnt = 0
  428. while 1:
  429. self.screenshot()
  430. scene = self.get_current_scene()
  431. if random_click_cnt >= 2:
  432. break
  433. if scene == Scene.UNKNOWN:
  434. logger.warning('Unknown scene, random click')
  435. if self.click(self.C_SAFE_RANDOM, interval=1.5):
  436. random_click_cnt += 1
  437. continue
  438. else:
  439. break
  440. if scene == Scene.UNKNOWN:
  441. self.pre_process()
  442. match self._config.exploration_config.user_status:
  443. case UserStatus.ALONE:
  444. self.run_solo()
  445. case UserStatus.LEADER:
  446. self.run_leader()
  447. case UserStatus.MEMBER:
  448. self.run_member()
  449. case _:
  450. self.run_solo()
  451. self.post_process()
  452. if __name__ == "__main__":
  453. from module.config.config import Config
  454. from module.device.device import Device
  455. config = Config('oas1')
  456. device = Device(config)
  457. t = ScriptTask(config, device)
  458. t.run()