exp_test.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. class Step:
  23. def __init__(self,
  24. step_name: str,
  25. pos: tuple,
  26. time: float,
  27. check=None,
  28. save_img: bool = False,
  29. save_interval: float = 0.1,
  30. save_time: float = None,
  31. save_max: int = None,
  32. save_roi: tuple = (0, 0),
  33. info: dict = None,
  34. ) -> None:
  35. self.step_name = step_name
  36. self.pos = pos
  37. self.time = time
  38. self.check = check
  39. self.save_img = save_img
  40. self.save_interval = save_interval
  41. self.save_time = save_time if save_time else time
  42. self.save_max = save_max
  43. self.save_roi = save_roi
  44. self.info = info
  45. x_start, y_start = self.save_roi
  46. if x_start > 640 or y_start > 80:
  47. raise ValueError(f'Invalid roi: {self.save_roi}')
  48. def __str__(self):
  49. return f'{self.step_name}'
  50. def __repr__(self):
  51. return f'{self.step_name}'
  52. # -----------------------------------------------------------------------
  53. # -----------------------------------------------------------------------
  54. class ExpTest(RightActivity, FastDevice, RestartAssets, ExplorationAssets):
  55. @cached_property
  56. def save_path(self) -> Path:
  57. save_path = Path(f'./tasks/Hyakkiyakou/temp/{self.name}')
  58. save_path.mkdir(parents=True, exist_ok=True)
  59. return save_path
  60. def init(self, name: str):
  61. self.name = name
  62. self.config.model.script.device.screenshot_method = ScreenshotMethod.WINDOW_BACKGROUND
  63. self.config.model.script.device.control_method = ControlMethod.WINDOW_MESSAGE
  64. def _exit(self):
  65. self.ui_get_current_page()
  66. self.ui_goto(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.ui_get_current_page()
  71. self.ui_goto(page_exploration)
  72. def run_step(self, step: Step):
  73. logger.hr(f'Running step: {step}', 1)
  74. cache_image: dict = {}
  75. timer_step = Timer(step.time)
  76. timer_save_img = Timer(step.save_time)
  77. timer_save_interval = Timer(step.save_interval)
  78. save_image_count = 0
  79. start_flag = False
  80. while 1:
  81. with usage_time('screenshot'):
  82. self.fast_screenshot()
  83. if not start_flag:
  84. with usage_time('click'):
  85. self.device.click(x=step.pos[0], y=step.pos[1], control_name=step.step_name)
  86. timer_step.start()
  87. timer_save_img.start()
  88. timer_save_interval.start()
  89. start_flag = True
  90. if not start_flag:
  91. continue
  92. if timer_step.reached():
  93. break
  94. if not step.save_img:
  95. continue
  96. if timer_save_img.reached():
  97. continue
  98. if step.save_max and save_image_count >= step.save_max:
  99. continue
  100. if timer_save_interval.reached():
  101. timer_save_interval.reset()
  102. save_image_count += 1
  103. save_name = f't@{self.name}@{int(datetime.now().timestamp() * 1000)}_{save_image_count:03d}.png'
  104. x_start, y_start = step.save_roi
  105. img = self.device.image[y_start:y_start + 640, x_start:x_start + 640]
  106. cache_image[save_name] = img
  107. logger.info(f'Save image: {save_name}')
  108. # cv2.imwrite(str(self.save_path / save_name), img)
  109. if cache_image:
  110. logger.info(f'Save image: {cache_image.keys()}')
  111. for key, value in cache_image.items():
  112. cv2.imwrite(str(self.save_path / key), value)
  113. def run_town(self):
  114. step_a = (641, 474)
  115. step_b = (1165, 481)
  116. step_c = (641, 556)
  117. step_d = (1231, 567)
  118. step_save_roi = (640, 80)
  119. step_1 = Step('step_1', pos=step_b, time=2)
  120. step_2 = Step('step_2', pos=step_a, time=2, save_img=True, save_roi=step_save_roi)
  121. step_3 = Step('step_3', pos=step_b, time=2)
  122. step_4 = Step('step_4', pos=step_c, time=2, save_img=True, save_roi=step_save_roi)
  123. step_5 = Step('step_5', pos=step_d, time=2)
  124. step_6 = Step('step_6', pos=step_a, time=2, save_img=True, save_roi=step_save_roi)
  125. step_7 = Step('step_7', pos=step_d, time=2)
  126. step_8 = Step('step_8', pos=step_c, time=2, save_img=True, save_roi=step_save_roi)
  127. self.run_step(step_1)
  128. self.run_step(step_2)
  129. self.run_step(step_3)
  130. self.run_step(step_4)
  131. self.run_step(step_5)
  132. self.run_step(step_6)
  133. self.run_step(step_7)
  134. self.run_step(step_8)
  135. def exp_enter(self, layer: int = 25):
  136. w = 188
  137. h = 80
  138. x_25, y_25 = 1052, 208
  139. x_26, y_26 = 1052, 316
  140. x_27, y_27 = 1052, 443
  141. x_28, y_28 = 1052, 564
  142. exp_click_25 = RuleClick(roi_front=(x_25, y_25, w, h), roi_back=(x_25, y_25, w, h), name="exp_click_25")
  143. exp_click_26 = RuleClick(roi_front=(x_26, y_26, w, h), roi_back=(x_26, y_26, w, h), name="exp_click_26")
  144. exp_click_27 = RuleClick(roi_front=(x_27, y_27, w, h), roi_back=(x_27, y_27, w, h), name="exp_click_27")
  145. exp_click_28 = RuleClick(roi_front=(x_28, y_28, w, h), roi_back=(x_28, y_28, w, h), name="exp_click_28")
  146. match layer:
  147. case 25:
  148. exp_click = exp_click_25
  149. case 26:
  150. exp_click = exp_click_26
  151. case 27:
  152. exp_click = exp_click_27
  153. case 28:
  154. exp_click = exp_click_28
  155. case _:
  156. exp_click = exp_click_28
  157. self.ui_click(exp_click, self.I_E_EXPLORATION_CLICK)
  158. while 1:
  159. self.screenshot()
  160. if self.appear(self.I_E_AUTO_ROTATE_ON) or self.appear(self.I_E_AUTO_ROTATE_OFF):
  161. break
  162. if self.appear_then_click(self.I_E_EXPLORATION_CLICK, interval=1):
  163. continue
  164. from time import sleep
  165. sleep(1)
  166. logger.info(f'Enter exploration {layer}')
  167. def exp_exit(self):
  168. while 1:
  169. self.screenshot()
  170. if self.appear(self.I_UI_BACK_RED):
  171. break
  172. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  173. continue
  174. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  175. continue
  176. if self.appear_then_click(self.I_E_EXIT_CONFIRM, interval=1):
  177. continue
  178. if self.appear_then_click(self.I_UI_BACK_BLUE, interval=1.5):
  179. continue
  180. self.ui_click_until_disappear(self.I_UI_BACK_RED, interval=0.5)
  181. def run_exp_25(self):
  182. pos_right = 1270
  183. pos_left = 260
  184. pos_top = 390
  185. pos_bottom = 500
  186. pos_level = (pos_top + pos_bottom) / 2
  187. step_1 = Step('step_1', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  188. step_2 = Step('step_2', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  189. step_3 = Step('step_3', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  190. step_4 = Step('step_4', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  191. step_5 = Step('step_5', pos=(pos_right, pos_bottom), time=2.1)
  192. save_time = 1.6
  193. back_1 = Step('back_1', pos=(pos_left, pos_bottom), time=1, save_img=True,
  194. save_roi=(random.randint(100, 540), 80))
  195. back_2 = Step('back_2', pos=(pos_left, pos_top), time=2, save_img=True,
  196. save_roi=(random.randint(100, 540), 80))
  197. back_3 = Step('back_3', pos=(pos_left, pos_top), time=2, save_img=True,
  198. save_roi=(random.randint(100, 540), 80))
  199. back_4 = Step('back_4', pos=(pos_left, pos_bottom), time=2, save_time=1.6, save_img=True,
  200. save_roi=(random.randint(100, 540), 80))
  201. back_5 = Step('back_5', pos=(pos_left, pos_level), time=save_time, save_img=True,
  202. save_roi=(random.randint(100, 540), 80))
  203. self.run_step(step_1)
  204. self.run_step(step_2)
  205. self.run_step(step_3)
  206. self.run_step(step_4)
  207. self.run_step(step_5)
  208. self.run_step(back_1)
  209. self.run_step(back_2)
  210. self.run_step(back_3)
  211. self.run_step(back_4)
  212. self.run_step(back_5)
  213. def run_exp_26(self):
  214. pos_right = 1270
  215. pos_left = 260
  216. pos_top = 454
  217. pos_bottom = 620
  218. pos_level = (pos_top + pos_bottom) / 2
  219. step_1 = Step('step_1', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  220. step_2 = Step('step_2', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  221. step_3 = Step('step_3', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  222. step_4 = Step('step_4', pos=(pos_right, pos_bottom), time=2.1)
  223. self.run_step(step_1)
  224. self.run_step(step_2)
  225. self.run_step(step_3)
  226. self.run_step(step_4)
  227. back_1 = Step('back_1', pos=(pos_left, pos_bottom), time=1, save_img=True,
  228. save_roi=(random.randint(100, 540), 80))
  229. back_2 = Step('back_2', pos=(pos_left, pos_top), time=2, save_img=True,
  230. save_roi=(random.randint(100, 540), 80))
  231. back_3 = Step('back_3', pos=(pos_left, pos_top), time=2, save_img=True,
  232. save_roi=(random.randint(100, 540), 80))
  233. back_4 = Step('back_4', pos=(pos_left, pos_bottom), time=2, save_time=1.6, save_img=True,
  234. save_roi=(random.randint(100, 540), 80))
  235. back_5 = Step('back_5', pos=(pos_left, pos_level), time=1.7, save_img=True,
  236. save_roi=(random.randint(100, 540), 80))
  237. self.run_step(back_1)
  238. self.run_step(back_2)
  239. self.run_step(back_3)
  240. self.run_step(back_4)
  241. self.run_step(back_5)
  242. def run_exp_27(self):
  243. pos_right = 1270
  244. pos_left = 260
  245. pos_top = 550
  246. pos_bottom = 620
  247. pos_level = (pos_top + pos_bottom) / 2
  248. step_1 = Step('step_1', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.1)
  249. step_2 = Step('step_2', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.3)
  250. step_3 = Step('step_3', pos=(pos_right, random.randint(pos_top, pos_bottom)), time=2.5)
  251. step_4 = Step('step_4', pos=(pos_right, pos_bottom), time=2.9)
  252. self.run_step(step_1)
  253. self.run_step(step_2)
  254. self.run_step(step_3)
  255. self.run_step(step_4)
  256. back_1 = Step('back_1', pos=(pos_left, pos_bottom), time=1, save_img=True,
  257. save_roi=(random.randint(100, 540), 80))
  258. back_2 = Step('back_2', pos=(pos_left, pos_top), time=2, save_img=True,
  259. save_roi=(random.randint(100, 540), 80))
  260. back_3 = Step('back_3', pos=(pos_left, pos_top), time=2, save_img=True,
  261. save_roi=(random.randint(100, 540), 80))
  262. back_4 = Step('back_4', pos=(pos_left, pos_bottom), time=2, save_time=1.6, save_img=True,
  263. save_roi=(random.randint(100, 540), 80))
  264. back_5 = Step('back_5', pos=(pos_left, pos_level), time=1.4, save_img=True,
  265. save_roi=(random.randint(100, 540), 80))
  266. self.run_step(back_1)
  267. self.run_step(back_2)
  268. self.run_step(back_3)
  269. self.run_step(back_4)
  270. self.run_step(back_5)
  271. def run_main(self):
  272. from time import sleep
  273. self.ui_get_current_page()
  274. self.ui_goto(page_main)
  275. sleep(1)
  276. pos_right = 1270
  277. pos_left = 540
  278. pos_top = 540
  279. pos_bottom = 660
  280. step_1 = Step('step_1', pos=(pos_right, pos_top), time=2.1)
  281. back_1 = Step('back_1', pos=(pos_left, pos_top), time=1.7, save_img=True, save_roi=(640, 80))
  282. step_2 = step_1
  283. step_2.name = 'step_2'
  284. back_2 = Step('back_2', pos=(pos_left, pos_bottom), time=1.7, save_img=True, save_roi=(640, 80))
  285. self.run_step(step_1)
  286. self.run_step(back_1)
  287. self.run_step(step_2)
  288. self.run_step(back_2)
  289. def back_main(self):
  290. self.ui_get_current_page()
  291. self.ui_goto(page_main)
  292. def open_expect_level(self, level: int):
  293. self.config.model.script.device.control_method = ControlMethod.UIAUTOMATOR2
  294. exploration_level = self.exp_level_map.get(level, None)
  295. swipeCount = 0
  296. while 1:
  297. self.screenshot()
  298. results = self.O_E_EXPLORATION_LEVEL_NUMBER.detect_and_ocr(self.device.image)
  299. text1 = [result.ocr_text for result in results]
  300. result = set(text1).intersection({exploration_level})
  301. if self.appear(self.I_E_EXPLORATION_CLICK) or result and len(result) > 0:
  302. break
  303. self.device.click_record_clear()
  304. self.swipe(self.S_SWIPE_LEVEL_UP)
  305. swipeCount += 1
  306. if swipeCount >= 25:
  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)