exp_test.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import cv2
  5. import random
  6. from datetime import datetime
  7. from pathlib import Path
  8. from cached_property import cached_property
  9. from module.logger import logger
  10. from module.base.timer import Timer
  11. from module.atom.click import RuleClick
  12. from tasks.Exploration.assets import ExplorationAssets
  13. from tasks.GameUi.game_ui import GameUi
  14. from tasks.GameUi.page import page_main, page_town, page_exploration
  15. from tasks.base_task import BaseTask
  16. from tasks.Script.config_device import ScreenshotMethod, ControlMethod
  17. from tasks.Component.RightActivity.right_activity import RightActivity
  18. from tasks.Restart.assets import RestartAssets
  19. from tasks.Exploration.config import ExplorationLevel
  20. from utils import usage_time
  21. from fast_device import FastDevice
  22. from module.exception import GameStuckError
  23. class Step:
  24. def __init__(self,
  25. step_name: str,
  26. pos: tuple,
  27. time: float,
  28. check=None,
  29. save_img: bool = False,
  30. save_interval: float = 0.1,
  31. save_time: float = None,
  32. save_max: int = None,
  33. save_roi: tuple = (0, 0),
  34. info: dict = None,
  35. ) -> None:
  36. self.step_name = step_name
  37. self.pos = pos
  38. self.time = time
  39. self.check = check
  40. self.save_img = save_img
  41. self.save_interval = save_interval
  42. self.save_time = save_time if save_time else time
  43. self.save_max = save_max
  44. self.save_roi = save_roi
  45. self.info = info
  46. x_start, y_start = self.save_roi
  47. if x_start > 640 or y_start > 80:
  48. raise ValueError(f'Invalid roi: {self.save_roi}')
  49. def __str__(self):
  50. return f'{self.step_name}'
  51. def __repr__(self):
  52. return f'{self.step_name}'
  53. # -----------------------------------------------------------------------
  54. # -----------------------------------------------------------------------
  55. class ExpTest(RightActivity, FastDevice, RestartAssets, ExplorationAssets):
  56. @cached_property
  57. def save_path(self) -> Path:
  58. save_path = Path(f'./tasks/Hyakkiyakou/temp/{self.name}')
  59. save_path.mkdir(parents=True, exist_ok=True)
  60. return save_path
  61. def init(self, name: str):
  62. self.name = name
  63. self.config.model.script.device.screenshot_method = ScreenshotMethod.WINDOW_BACKGROUND
  64. self.config.model.script.device.control_method = ControlMethod.WINDOW_MESSAGE
  65. def _exit(self):
  66. self.ui_get_current_page()
  67. self.ui_goto(page_town)
  68. self.right_close()
  69. self.ui_click(self.I_LOGIN_SCROOLL_OPEN, self.I_LOGIN_SCROOLL_CLOSE, interval=1)
  70. def _go(self):
  71. self.ui_get_current_page()
  72. self.ui_goto(page_exploration)
  73. def run_step(self, step: Step):
  74. logger.hr(f'Running step: {step}', 1)
  75. cache_image: dict = {}
  76. timer_step = Timer(step.time)
  77. timer_save_img = Timer(step.save_time)
  78. timer_save_interval = Timer(step.save_interval)
  79. save_image_count = 0
  80. start_flag = False
  81. while 1:
  82. with usage_time('screenshot'):
  83. self.fast_screenshot()
  84. if not start_flag:
  85. with usage_time('click'):
  86. self.device.click(x=step.pos[0], y=step.pos[1], control_name=step.step_name)
  87. timer_step.start()
  88. timer_save_img.start()
  89. timer_save_interval.start()
  90. start_flag = True
  91. if not start_flag:
  92. continue
  93. if timer_step.reached():
  94. break
  95. if not step.save_img:
  96. continue
  97. if timer_save_img.reached():
  98. continue
  99. if step.save_max and save_image_count >= step.save_max:
  100. continue
  101. if timer_save_interval.reached():
  102. timer_save_interval.reset()
  103. save_image_count += 1
  104. save_name = f't@{self.name}@{int(datetime.now().timestamp() * 1000)}_{save_image_count:03d}.png'
  105. x_start, y_start = step.save_roi
  106. img = self.device.image[y_start:y_start + 640, x_start:x_start + 640]
  107. cache_image[save_name] = img
  108. logger.info(f'Save image: {save_name}')
  109. # cv2.imwrite(str(self.save_path / save_name), img)
  110. if cache_image:
  111. logger.info(f'Save image: {cache_image.keys()}')
  112. for key, value in cache_image.items():
  113. cv2.imwrite(str(self.save_path / key), value)
  114. def run_town(self):
  115. step_a = (641, 474)
  116. step_b = (1165, 481)
  117. step_c = (641, 556)
  118. step_d = (1231, 567)
  119. step_save_roi = (640, 80)
  120. step_1 = Step('step_1', pos=step_b, time=2)
  121. step_2 = Step('step_2', pos=step_a, time=2, save_img=True, save_roi=step_save_roi)
  122. step_3 = Step('step_3', pos=step_b, time=2)
  123. step_4 = Step('step_4', pos=step_c, time=2, save_img=True, save_roi=step_save_roi)
  124. step_5 = Step('step_5', pos=step_d, time=2)
  125. step_6 = Step('step_6', pos=step_a, time=2, save_img=True, save_roi=step_save_roi)
  126. step_7 = Step('step_7', pos=step_d, time=2)
  127. step_8 = Step('step_8', pos=step_c, time=2, save_img=True, save_roi=step_save_roi)
  128. self.run_step(step_1)
  129. self.run_step(step_2)
  130. self.run_step(step_3)
  131. self.run_step(step_4)
  132. self.run_step(step_5)
  133. self.run_step(step_6)
  134. self.run_step(step_7)
  135. self.run_step(step_8)
  136. def exp_enter(self, layer: int = 25):
  137. w = 188
  138. h = 80
  139. x_25, y_25 = 1052, 208
  140. x_26, y_26 = 1052, 316
  141. x_27, y_27 = 1052, 443
  142. x_28, y_28 = 1052, 564
  143. exp_click_25 = RuleClick(roi_front=(x_25, y_25, w, h), roi_back=(x_25, y_25, w, h), name="exp_click_25")
  144. exp_click_26 = RuleClick(roi_front=(x_26, y_26, w, h), roi_back=(x_26, y_26, w, h), name="exp_click_26")
  145. exp_click_27 = RuleClick(roi_front=(x_27, y_27, w, h), roi_back=(x_27, y_27, w, h), name="exp_click_27")
  146. exp_click_28 = RuleClick(roi_front=(x_28, y_28, w, h), roi_back=(x_28, y_28, w, h), name="exp_click_28")
  147. match layer:
  148. case 25:
  149. exp_click = exp_click_25
  150. case 26:
  151. exp_click = exp_click_26
  152. case 27:
  153. exp_click = exp_click_27
  154. case 28:
  155. exp_click = exp_click_28
  156. case _:
  157. exp_click = exp_click_28
  158. self.ui_click(exp_click, self.I_E_EXPLORATION_CLICK)
  159. while 1:
  160. self.screenshot()
  161. if self.appear(self.I_E_AUTO_ROTATE_ON) or self.appear(self.I_E_AUTO_ROTATE_OFF):
  162. break
  163. if self.appear_then_click(self.I_E_EXPLORATION_CLICK, interval=1):
  164. continue
  165. from time import sleep
  166. sleep(1)
  167. logger.info(f'Enter exploration {layer}')
  168. def exp_exit(self):
  169. while 1:
  170. self.screenshot()
  171. if self.appear(self.I_UI_BACK_RED):
  172. break
  173. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  174. continue
  175. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  176. continue
  177. if self.appear_then_click(self.I_E_EXIT_CONFIRM, interval=1):
  178. continue
  179. if self.appear_then_click(self.I_UI_BACK_BLUE, interval=1.5):
  180. continue
  181. self.ui_click_until_disappear(self.I_UI_BACK_RED, interval=0.5)
  182. def run_exp_25(self):
  183. pos_right = 1270
  184. pos_left = 260
  185. pos_top = 390
  186. pos_bottom = 500
  187. pos_level = (pos_top + pos_bottom) / 2
  188. step_1 = Step('step_1', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  189. step_2 = Step('step_2', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  190. step_3 = Step('step_3', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  191. step_4 = Step('step_4', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  192. step_5 = Step('step_5', pos=(pos_right, pos_bottom), time=2.1)
  193. save_time = 1.6
  194. back_1 = Step('back_1', pos=(pos_left, pos_bottom), time=1, save_img=True,
  195. save_roi=(random.randint(100, 540), 80))
  196. back_2 = Step('back_2', pos=(pos_left, pos_top), time=2, save_img=True,
  197. save_roi=(random.randint(100, 540), 80))
  198. back_3 = Step('back_3', pos=(pos_left, pos_top), time=2, save_img=True,
  199. save_roi=(random.randint(100, 540), 80))
  200. back_4 = Step('back_4', pos=(pos_left, pos_bottom), time=2, save_time=1.6, save_img=True,
  201. save_roi=(random.randint(100, 540), 80))
  202. back_5 = Step('back_5', pos=(pos_left, pos_level), time=save_time, save_img=True,
  203. save_roi=(random.randint(100, 540), 80))
  204. self.run_step(step_1)
  205. self.run_step(step_2)
  206. self.run_step(step_3)
  207. self.run_step(step_4)
  208. self.run_step(step_5)
  209. self.run_step(back_1)
  210. self.run_step(back_2)
  211. self.run_step(back_3)
  212. self.run_step(back_4)
  213. self.run_step(back_5)
  214. def run_exp_26(self):
  215. pos_right = 1270
  216. pos_left = 260
  217. pos_top = 454
  218. pos_bottom = 620
  219. pos_level = (pos_top + pos_bottom) / 2
  220. step_1 = Step('step_1', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  221. step_2 = Step('step_2', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  222. step_3 = Step('step_3', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  223. step_4 = Step('step_4', pos=(pos_right, pos_bottom), time=2.1)
  224. self.run_step(step_1)
  225. self.run_step(step_2)
  226. self.run_step(step_3)
  227. self.run_step(step_4)
  228. back_1 = Step('back_1', pos=(pos_left, pos_bottom), time=1, save_img=True,
  229. save_roi=(random.randint(100, 540), 80))
  230. back_2 = Step('back_2', pos=(pos_left, pos_top), time=2, save_img=True,
  231. save_roi=(random.randint(100, 540), 80))
  232. back_3 = Step('back_3', pos=(pos_left, pos_top), time=2, save_img=True,
  233. save_roi=(random.randint(100, 540), 80))
  234. back_4 = Step('back_4', pos=(pos_left, pos_bottom), time=2, save_time=1.6, save_img=True,
  235. save_roi=(random.randint(100, 540), 80))
  236. back_5 = Step('back_5', pos=(pos_left, pos_level), time=1.7, save_img=True,
  237. save_roi=(random.randint(100, 540), 80))
  238. self.run_step(back_1)
  239. self.run_step(back_2)
  240. self.run_step(back_3)
  241. self.run_step(back_4)
  242. self.run_step(back_5)
  243. def run_exp_27(self):
  244. pos_right = 1270
  245. pos_left = 260
  246. pos_top = 550
  247. pos_bottom = 620
  248. pos_level = (pos_top + pos_bottom) / 2
  249. step_1 = Step('step_1', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  250. step_2 = Step('step_2', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.3)
  251. step_3 = Step('step_3', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.5)
  252. step_4 = Step('step_4', pos=(pos_right, pos_bottom), time=2.9)
  253. self.run_step(step_1)
  254. self.run_step(step_2)
  255. self.run_step(step_3)
  256. self.run_step(step_4)
  257. back_1 = Step('back_1', pos=(pos_left, pos_bottom), time=1, save_img=True,
  258. save_roi=(random.randint(100, 540), 80))
  259. back_2 = Step('back_2', pos=(pos_left, pos_top), time=2, save_img=True,
  260. save_roi=(random.randint(100, 540), 80))
  261. back_3 = Step('back_3', pos=(pos_left, pos_top), time=2, save_img=True,
  262. save_roi=(random.randint(100, 540), 80))
  263. back_4 = Step('back_4', pos=(pos_left, pos_bottom), time=2, save_time=1.6, save_img=True,
  264. save_roi=(random.randint(100, 540), 80))
  265. back_5 = Step('back_5', pos=(pos_left, pos_level), time=1.4, save_img=True,
  266. save_roi=(random.randint(100, 540), 80))
  267. self.run_step(back_1)
  268. self.run_step(back_2)
  269. self.run_step(back_3)
  270. self.run_step(back_4)
  271. self.run_step(back_5)
  272. def run_main(self):
  273. from time import sleep
  274. self.ui_get_current_page()
  275. self.ui_goto(page_main)
  276. sleep(1)
  277. pos_right = 1270
  278. pos_left = 540
  279. pos_top = 540
  280. pos_bottom = 660
  281. step_1 = Step('step_1', pos=(pos_right, pos_top), time=2.1)
  282. back_1 = Step('back_1', pos=(pos_left, pos_top), time=1.7, save_img=True, save_roi=(640, 80))
  283. step_2 = step_1
  284. step_2.name = 'step_2'
  285. back_2 = Step('back_2', pos=(pos_left, pos_bottom), time=1.7, save_img=True, save_roi=(640, 80))
  286. self.run_step(step_1)
  287. self.run_step(back_1)
  288. self.run_step(step_2)
  289. self.run_step(back_2)
  290. def back_main(self):
  291. self.ui_get_current_page()
  292. self.ui_goto(page_main)
  293. def open_expect_level(self, level: int):
  294. self.config.model.script.device.control_method = ControlMethod.UIAUTOMATOR2
  295. exploration_level = self.exp_level_map.get(level, None)
  296. swipeCount = 0
  297. while 1:
  298. self.screenshot()
  299. results = self.O_E_EXPLORATION_LEVEL_NUMBER.detect_and_ocr(self.device.image)
  300. text1 = [result.ocr_text for result in results]
  301. result = set(text1).intersection({exploration_level})
  302. if self.appear(self.I_E_EXPLORATION_CLICK) or result and len(result) > 0:
  303. break
  304. self.device.click_record_clear()
  305. self.swipe(self.S_SWIPE_LEVEL_UP)
  306. swipeCount += 1
  307. if swipeCount >= 25:
  308. raise GameStuckError(
  309. f"Swiped too many times ({swipeCount}), seems stuck in exploration level selection"
  310. )
  311. return False
  312. self.config.model.script.device.control_method = ControlMethod.WINDOW_MESSAGE
  313. self.O_E_EXPLORATION_LEVEL_NUMBER.keyword = exploration_level
  314. while 1:
  315. self.screenshot()
  316. if self.appear(self.I_E_EXPLORATION_CLICK):
  317. break
  318. if self.ocr_appear_click(self.O_E_EXPLORATION_LEVEL_NUMBER):
  319. self.wait_until_appear(self.I_E_EXPLORATION_CLICK, wait_time=3)
  320. while 1:
  321. self.screenshot()
  322. if self.appear(self.I_E_AUTO_ROTATE_ON) or self.appear(self.I_E_AUTO_ROTATE_OFF):
  323. break
  324. if self.appear_then_click(self.I_E_EXPLORATION_CLICK, interval=1):
  325. continue
  326. from time import sleep
  327. sleep(1)
  328. logger.info(f'Enter exploration {level}')
  329. @cached_property
  330. def exp_level_map(self):
  331. return {
  332. 1: ExplorationLevel.EXPLORATION_1,
  333. 2: ExplorationLevel.EXPLORATION_2,
  334. 3: ExplorationLevel.EXPLORATION_3,
  335. 4: ExplorationLevel.EXPLORATION_4,
  336. 5: ExplorationLevel.EXPLORATION_5,
  337. 6: ExplorationLevel.EXPLORATION_6,
  338. 7: ExplorationLevel.EXPLORATION_7,
  339. 8: ExplorationLevel.EXPLORATION_8,
  340. 9: ExplorationLevel.EXPLORATION_9,
  341. 10: ExplorationLevel.EXPLORATION_10,
  342. 11: ExplorationLevel.EXPLORATION_11,
  343. 12: ExplorationLevel.EXPLORATION_12,
  344. 13: ExplorationLevel.EXPLORATION_13,
  345. 14: ExplorationLevel.EXPLORATION_14,
  346. 15: ExplorationLevel.EXPLORATION_15,
  347. 16: ExplorationLevel.EXPLORATION_16,
  348. 17: ExplorationLevel.EXPLORATION_17,
  349. 18: ExplorationLevel.EXPLORATION_18,
  350. 19: ExplorationLevel.EXPLORATION_19,
  351. 20: ExplorationLevel.EXPLORATION_20,
  352. 21: ExplorationLevel.EXPLORATION_21,
  353. 22: ExplorationLevel.EXPLORATION_22,
  354. 23: ExplorationLevel.EXPLORATION_23,
  355. 24: ExplorationLevel.EXPLORATION_24,
  356. 25: ExplorationLevel.EXPLORATION_25,
  357. 26: ExplorationLevel.EXPLORATION_26,
  358. 27: ExplorationLevel.EXPLORATION_27,
  359. 28: ExplorationLevel.EXPLORATION_28,
  360. }
  361. def run_exp_base(self, pos_top: int = 550, go_times: int = 7, back_times: int = 7):
  362. pos_right = 1270
  363. pos_left = 260
  364. pos_top = pos_top
  365. pos_bottom = 620
  366. pos_level = (pos_top + pos_bottom) / 2
  367. save_roi_start = 200
  368. save_roi_end = 640 - save_roi_start
  369. step_1 = Step('step_1', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  370. step_2 = Step('step_2', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  371. step_3 = Step('step_3', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  372. step_4 = Step('step_4', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  373. step_5 = Step('step_5', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  374. step_6 = Step('step_6', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  375. step_7 = Step('step_7', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  376. step_8 = Step('step_7', pos=(pos_right, random.randint(pos_bottom - 10, pos_bottom)), time=2.3)
  377. back_1 = Step('back_1', pos=(pos_left, pos_level), time=1.7, save_img=True,
  378. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  379. back_2 = Step('back_2', pos=(pos_left, pos_top), time=1.7, save_img=True,
  380. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  381. back_3 = Step('back_3', pos=(pos_left, pos_level), time=1.7, save_img=True,
  382. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  383. back_4 = Step('back_4', pos=(pos_left, pos_bottom), time=1.7, save_img=True,
  384. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  385. back_5 = Step('back_5', pos=(pos_left, pos_bottom), time=1.4, save_img=True,
  386. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  387. back_6 = Step('back_6', pos=(pos_left, pos_top), time=2.1, save_img=True,
  388. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  389. back_7 = Step('back_7', pos=(pos_left, pos_bottom), time=2.1, save_img=True,
  390. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  391. back_8 = Step('back_8', pos=(pos_left, pos_level), time=1.8, save_img=True,
  392. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  393. back_9 = Step('back_9', pos=(pos_left, pos_top), time=1.8, save_img=True,
  394. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  395. back_10 = Step('back_10', pos=(pos_left, pos_bottom), time=2.1, save_img=True,
  396. save_roi=(random.randint(save_roi_start, save_roi_end), 80))
  397. if go_times >= 3:
  398. self.run_step(step_1)
  399. self.run_step(step_2)
  400. self.run_step(step_3)
  401. if go_times >= 4:
  402. self.run_step(step_4)
  403. if go_times >= 5:
  404. self.run_step(step_5)
  405. if go_times >= 6:
  406. self.run_step(step_6)
  407. if go_times >= 7:
  408. self.run_step(step_7)
  409. if go_times >= 8:
  410. self.run_step(step_8)
  411. if back_times >= 4:
  412. self.run_step(back_1)
  413. self.run_step(back_2)
  414. self.run_step(back_3)
  415. self.run_step(back_4)
  416. if back_times >= 5:
  417. self.run_step(back_5)
  418. if back_times >= 6:
  419. self.run_step(back_6)
  420. if back_times >= 7:
  421. self.run_step(back_7)
  422. if back_times >= 8:
  423. self.run_step(back_8)
  424. if back_times >= 9:
  425. self.run_step(back_9)
  426. if back_times >= 10:
  427. self.run_step(back_10)
  428. logger.info('exp done')
  429. logger.info('-----------------------------------------------------------------------------')
  430. def run_exp_24(self):
  431. self.run_exp_base(pos_top=470, go_times=8, back_times=10)
  432. def run_exp_20(self):
  433. self.run_exp_base(pos_top=470, go_times=7, back_times=8)
  434. def run_exp_18(self):
  435. self.run_exp_base(pos_top=470, go_times=4, back_times=5)
  436. def run_exp_15(self):
  437. self.run_exp_base(pos_top=470, go_times=7, back_times=8)
  438. def run_exp_12(self):
  439. self.run_exp_base(pos_top=470, go_times=3, back_times=5)