exp_test.py 20 KB

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