solo.py 20 KB

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