script_task.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. # This Python file uses the following encoding: utf-8
  2. # @brief Ryou Dokan Toppa (阴阳竂道馆突破功能)
  3. # @author jackyhwei
  4. # @note draft version without full test
  5. # github https://github.com/roarhill/oas
  6. import sys
  7. from pathlib import Path
  8. # 获取当前文件的目录
  9. current_dir = Path(__file__).resolve().parent
  10. # 获取上级目录
  11. upper_dir = current_dir.parent.parent
  12. print(sys.path)
  13. sys.path.append(str(upper_dir))
  14. print(sys.path)
  15. from enum import Enum
  16. import time
  17. from datetime import datetime, timedelta
  18. import random
  19. import numpy as np
  20. from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
  21. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  22. from tasks.Dokan.assets import DokanAssets
  23. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  24. from tasks.Component.config_base import ConfigBase, Time
  25. from tasks.GameUi.game_ui import GameUi
  26. from tasks.GameUi.page import page_main, page_kekkai_toppa, page_shikigami_records, page_guild
  27. from tasks.RealmRaid.assets import RealmRaidAssets
  28. from module.logger import logger
  29. from module.exception import TaskEnd
  30. from module.atom.image_grid import ImageGrid
  31. from module.base.utils import point2str
  32. from module.base.timer import Timer
  33. from module.exception import GamePageUnknownError
  34. from tasks.Dokan.config import DokanConfig, Dokan
  35. def detect_safe_area2(image, target_gray_value=255, tolerance=0):
  36. """
  37. 在灰度图片中查找与目标灰度值在容差范围内的纯色区块,并返回这些区块的坐标。
  38. :param image_path: 图片路径
  39. :param target_gray_value: 目标灰度值,默认为255(最亮)
  40. :param tolerance: 灰度差异容忍度,默认为0(即完全匹配)
  41. :return: 包含找到的纯色区块坐标元组的列表
  42. """
  43. import cv2
  44. # 加载图片并转换为灰度图
  45. # img = Image.open(image_path).convert('L') # 'L' 表示转换为灰度图
  46. # img_arr = np.array(img) # 转换为numpy数组以便高效处理
  47. # 将图片转换为灰度图
  48. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  49. _, img_arr = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
  50. # 初始化结果列表
  51. solid_regions = []
  52. # 遍历图片的每个像素
  53. for y in range(img_arr.shape[0]): # 高度
  54. for x in range(img_arr.shape[1]): # 宽度
  55. pixel_gray_value = img_arr[y, x]
  56. # 检查当前像素灰度值是否与目标灰度值在容差范围内匹配
  57. if abs(pixel_gray_value - target_gray_value) <= tolerance:
  58. # 检查周围是否也是同样的灰度值,以确定这是一个区块而非单个像素
  59. is_solid_region = True
  60. for dy in range(-1, 2): # 检查3x3区域
  61. for dx in range(-1, 2):
  62. if dy == 0 and dx == 0:
  63. continue # 跳过中心点
  64. ny, nx = y + dy, x + dx
  65. if 0 <= ny < img_arr.shape[0] and 0 <= nx < img_arr.shape[1]:
  66. if abs(img_arr[ny, nx] - target_gray_value) > tolerance:
  67. is_solid_region = False
  68. break
  69. if not is_solid_region:
  70. break
  71. if is_solid_region:
  72. # 记录纯色区块的左上角坐标和右下角坐标
  73. start_x, start_y = x, y
  74. while start_y > 0 and abs(img_arr[start_y-1, x] - target_gray_value) <= tolerance:
  75. start_y -= 1
  76. while start_x > 0 and abs(img_arr[y, start_x-1] - target_gray_value) <= tolerance:
  77. start_x -= 1
  78. end_x, end_y = x, y
  79. while end_y < img_arr.shape[0]-1 and abs(img_arr[end_y+1, x] - target_gray_value) <= tolerance:
  80. end_y += 1
  81. while end_x < img_arr.shape[1]-1 and abs(img_arr[y, end_x+1] - target_gray_value) <= tolerance:
  82. end_x += 1
  83. solid_regions.append(((start_x, start_y), (end_x+1, end_y+1)))
  84. return solid_regions
  85. def detect_safe_area2(image, safe_color_lower, safe_color_upper, num_areas=3, debug=False):
  86. '''
  87. 找出当前截图里最大的纯色区域,找出来的这个区域将被用于随机点击的区域。
  88. :note: 注意拿到这个区域后要马上点击,不要sleep!
  89. :param image: 截图图像
  90. :return: 最大纯色区域的坐标
  91. '''
  92. import cv2
  93. import numpy as np
  94. # 将图片从BGR转换到HSV色彩空间
  95. hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  96. # 定义颜色范围
  97. # 注意:这里的颜色范围需要根据实际图片中的安全区域颜色进行调整
  98. mask = cv2.inRange(hsv, safe_color_lower, safe_color_upper)
  99. # 应用形态学操作来消除噪点和填补小的孔洞
  100. kernel = np.ones((5,5),np.uint8)
  101. mask = cv2.dilate(mask,kernel,iterations = 1)
  102. mask = cv2.erode(mask,kernel,iterations = 1)
  103. # 查找轮廓
  104. contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  105. # 计算每个轮廓的面积并排序
  106. # areas = [(cv2.contourArea(contour), contour) for contour in contours]
  107. areas = []
  108. for contour in contours:
  109. area = cv2.contourArea(contour)
  110. print(f"area={area}")
  111. areas.append((area, contour))
  112. # areas.sort(reverse=True)
  113. # 按面积降序排序
  114. areas_sorted = sorted(areas, key=lambda x: x[0], reverse=True)
  115. if debug:
  116. # 绘制面积排名前3的区域
  117. for area, contour in areas_sorted[:num_areas]:
  118. x1, y1, w1, h1 = cv2.boundingRect(contour)
  119. # 使用蓝色边框,线宽为4
  120. cv2.rectangle(image, (x1, y1), (x1+w1, y1+h1), (255, 0, 0), 4)
  121. # 使用红色边框,线宽为3
  122. cv2.drawContours(image, [contour], -1, (0, 0, 255), 3)
  123. # 假设最大的轮廓是安全区域
  124. if contours:
  125. safe_area = max(contours, key=cv2.contourArea)
  126. x, y, w, h = cv2.boundingRect(safe_area)
  127. if debug:
  128. # 绘制安全区域
  129. cv2.drawContours(image, [safe_area], -1, (0, 255, 0), 3)
  130. return (x, y, w, h)
  131. return (0, 0, 0, 0)
  132. class DokanScene(Enum):
  133. # 未知界面
  134. RYOU_DOKAN_SCENE_UNKNOWN = 0
  135. # 进入道馆,集结中
  136. RYOU_DOKAN_SCENE_GATHERING = 1
  137. # 进入战场,等待用户点击开始战斗
  138. RYOU_DOKAN_SCENE_IN_FIELD = 2
  139. # 通常是失败了,并退出来到集结界面,可重新开始点击右下角挑战进入战斗
  140. RYOU_DOKAN_SCENE_START_CHALLENGE = 3
  141. # 失败次数超过上限,CD中
  142. RYOU_DOKAN_SCENE_CD = 4
  143. # 战斗进行中
  144. RYOU_DOKAN_SCENE_FIGHTING = 5
  145. # 加油进行中
  146. RYOU_DOKAN_SCENE_CHEERING = 6
  147. # 道馆失败投票保留
  148. RYOU_DOKAN_SCENE_FAILED_VOTE_NO = 7
  149. # 阴阳竂
  150. RYOU_DOKAN_RYOU = 8
  151. # 战斗结算,可能是打完小朋友了,也可能是失败了。
  152. RYOU_DOKAN_SCENE_BATTLE_OVER = 9
  153. # 等待BOSS战
  154. RYOU_DOKAN_SCENE_BOSS_WAITING = 10
  155. # 道馆结束
  156. RYOU_DOKAN_SCENE_FINISHED = 99
  157. def __str__(self):
  158. return self.name.title()
  159. def print(self):
  160. print(DokanScene.RYOU_DOKAN_SCENE_GATHERING) # 输出: DokanScene.Ryou_Daoguan_Scene_Gathering
  161. print(DokanScene.RYOU_DOKAN_SCENE_CD.value) # 输出: 2
  162. print(str(DokanScene.RYOU_DOKAN_SCENE_FIGHTING)) # 输出: Ryou_Daoguan_Scene_Fighting
  163. class ScriptTask(GeneralBattle, GameUi, SwitchSoul, DokanAssets):
  164. medal_grid: ImageGrid = None
  165. attack_priority_selected: bool = False
  166. team_switched: bool = False
  167. green_mark_done : bool = False
  168. def run(self):
  169. """ 道馆主函数
  170. :return:
  171. """
  172. cfg: Dokan = self.config.dokan
  173. current_scene: DokanScene = DokanScene.RYOU_DOKAN_SCENE_UNKNOWN
  174. in_dokan: bool = False
  175. # 攻击优先顺序
  176. attack_priority: int = cfg.dokan_config.dokan_attack_priority
  177. # 自动换御魂
  178. if cfg.switch_soul_config.enable:
  179. self.ui_get_current_page()
  180. self.ui_goto(page_shikigami_records)
  181. self.run_switch_soul(cfg.switch_soul_config.switch_group_team)
  182. if cfg.switch_soul_config.enable_switch_by_name:
  183. self.ui_get_current_page()
  184. self.ui_goto(page_shikigami_records)
  185. self.run_switch_soul_by_name(cfg.switch_soul_config.group_name, cfg.switch_soul_config.team_name)
  186. success = True
  187. scene_err_count = 0
  188. # 在阴阳竂界面点击并进入道馆
  189. # 检测当前界面的场景(时间关系,暂时没有做庭院、町中等主界面的场景检测, 应考虑在GameUI.game_ui.ui_get_current_page()里实现)
  190. in_dokan, current_scene = self.get_current_scene(True)
  191. if not in_dokan:
  192. self.ui_get_current_page()
  193. self.ui_goto(page_guild)
  194. self.goto_dokan()
  195. # 开始道馆流程
  196. while 1:
  197. self.screenshot()
  198. # 检测当前界面的场景(时间关系,暂时没有做庭院、町中等主界面的场景检测, 应考虑在GameUI.game_ui.ui_get_current_page()里实现)
  199. in_dokan, current_scene = self.get_current_scene(True)
  200. logger.info(f"in_dokan={in_dokan}, current_scene={current_scene}")
  201. # 如果当前不在道馆,或者被人工操作退出道馆了,重新尝试进入道馆
  202. if not in_dokan:
  203. scene_err_count += 1
  204. logger.info(f"err count={scene_err_count}")
  205. if scene_err_count >= 3:
  206. self.goto_dokan()
  207. # 先点击一下,然后等5秒再重新截图
  208. self.anti_detect(True, True, False)
  209. time.sleep(5)
  210. continue
  211. else:
  212. scene_err_count = 0
  213. # 场景状态:道馆集结中
  214. if current_scene == DokanScene.RYOU_DOKAN_SCENE_GATHERING:
  215. logger.debug(f"Ryou DOKAN gathering, 集结中...")
  216. # 如果还未选择优先攻击,选一下
  217. if not self.attack_priority_selected:
  218. self.dokan_choose_attack_priority(attack_priority=attack_priority)
  219. self.attack_priority_selected = True
  220. # 场景状态:等待馆主战开始
  221. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_BOSS_WAITING:
  222. logger.debug(f"Ryou DOKAN boss battle waiting...")
  223. # 场景状态:检查右下角有没有挑战?通常是失败了,并退出来到集结界面,可重新开始点击右下角挑战进入战斗
  224. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_START_CHALLENGE:
  225. self.appear_then_click(self.I_RYOU_DOKAN_START_CHALLENGE)
  226. # 场景状态:进入战斗,待开始
  227. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_IN_FIELD:
  228. # 战斗
  229. success = self.dokan_battle(cfg)
  230. # 战斗结束后,随便点三下,确保跳过各种结算画面
  231. self.click(click=self.C_DOKAN_READY_FOR_BATLLE, interval=1.5)
  232. self.click(click=self.C_DOKAN_RANDOM_CLICK_AREA2, interval=2.2)
  233. self.click(click=self.C_DOKAN_RANDOM_CLICK_AREA3, interval=1.8)
  234. # 每次战斗结束都重置绿标
  235. self.green_mark_done = False
  236. # 场景状态:如果CD中,开始加油
  237. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_CD:
  238. logger.info(f"CD中,是否加油:{cfg.dokan_config.dokan_auto_cheering_while_cd}..")
  239. if cfg.dokan_config.dokan_auto_cheering_while_cd:
  240. self.start_cheering()
  241. # 场景状态:战斗中,左上角的加油图标
  242. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_FIGHTING:
  243. logger.info("战斗进行中")
  244. # 场景状态:加油中
  245. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_CHEERING:
  246. self.appear_then_click(self.I_RYOU_DOKAN_CHEERING)
  247. # 场景状态:道馆已经结束
  248. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_FINISHED:
  249. logger.info("道馆已经结束,退出")
  250. break
  251. elif current_scene == DokanScene.RYOU_DOKAN_SCENE_FAILED_VOTE_NO:
  252. logger.info("道馆挑战失败:投票保留")
  253. else:
  254. logger.info(f"unknown scene, skipped")
  255. # 防封,随机移动,随机点击(安全点击),随机时延
  256. if not self.anti_detect(True, True, True):
  257. time.sleep(1)
  258. # 保持好习惯,一个任务结束了就返回到庭院,方便下一任务的开始
  259. self.goto_main()
  260. # 设置下次运行时间
  261. if success:
  262. self.set_next_run(task='Dokan', finish=True, server=True, success=True)
  263. else:
  264. self.set_next_run(task='Dokan', finish=True, server=True, success=False)
  265. raise TaskEnd
  266. def dokan_battle(self, cfg: Dokan):
  267. """ 道馆战斗
  268. 道馆集结结束后会自动进入战斗,打完一个也会自动进入下一个,因此直接点击右下角的开始
  269. :return: 战斗成功(True) or 战斗失败(False) or 区域不可用(False)
  270. """
  271. config: GeneralBattleConfig = cfg.general_battle_config
  272. # 正式进攻会设定 2s - 10s 的随机延迟,避免攻击间隔及其相近被检测为脚本。
  273. if cfg.dokan_config.random_delay:
  274. self.anti_detect(False, False, True)
  275. # 上面可能睡了一觉,重新截图
  276. self.screenshot()
  277. # 更换队伍
  278. if not self.team_switched:
  279. logger.info(f"更换队伍: config.preset_enable={config.preset_enable}, config.preset_group={config.preset_group}, config.preset_team={config.preset_team}")
  280. self.switch_preset_team(config.preset_enable, config.preset_group, config.preset_team)
  281. self.team_switched = True
  282. # 切完队伍后有时候会卡顿,先睡一觉,防止快速跳到绿标流程,导致未能成功绿标
  283. time.sleep(3)
  284. # 等待准备按钮的出现
  285. logger.debug("等待准备按钮的出现")
  286. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  287. # 点击准备
  288. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  289. logger.info("点击准备")
  290. # 绿标式神, should we check there's a green marked role?
  291. if not self.green_mark_done and self.is_in_battle(False):
  292. logger.info(f"绿标式神:config.green_enable={config.green_enable}, config.green_mark={config.green_mark}")
  293. self.green_mark(config.green_enable, config.green_mark)
  294. self.green_mark_done = True
  295. # 等待战斗结果
  296. logger.info(f"等待战斗结果:config.random_click_swipt_enable={config.random_click_swipt_enable}")
  297. logger.info("come on baby, let's go.")
  298. while 1:
  299. # logger.info("take a nap")
  300. time.sleep(1)
  301. self.device.stuck_record_add('BATTLE_STATUS_S')
  302. self.device.click_record_clear()
  303. self.screenshot()
  304. # 如果出现赢 就点击
  305. if self.appear(GeneralBattle.I_WIN, threshold=0.8):
  306. logger.info("道馆的小朋友们打完了,boss is on the way")
  307. win = True
  308. break
  309. # 如果出现打败馆主的赢,就点击
  310. if self.appear(self.I_RYOU_DOKAN_WIN, threshold=0.8):
  311. logger.info("We've defeated the boss, and win the final game.")
  312. win = True
  313. break
  314. # 如果出现失败 就点击,返回False。 TODO 不知道挑战馆主失败是不是同一个画面?
  315. if self.appear(GeneralBattle.I_FALSE, threshold=0.8):
  316. logger.info("Battle failed")
  317. win = False
  318. break
  319. # 如果领奖励
  320. if self.appear(self.I_RYOU_DOKAN_BATTLE_OVER, threshold=0.6):
  321. logger.info("Battle over")
  322. win = True
  323. break
  324. # 如果领奖励出现金币
  325. if self.appear(GeneralBattle.I_REWARD_GOLD, threshold=0.8):
  326. win = True
  327. break
  328. # 如果开启战斗过程随机滑动
  329. if config.random_click_swipt_enable:
  330. logger.info("random swipt ...")
  331. self.random_click_swipt()
  332. # 打完一个小朋友,自动进入下一个小朋友
  333. if self.appear_then_click(self.I_RYOU_DOKAN_IN_FIELD):
  334. logger.info("New battle starts")
  335. continue
  336. logger.info(f"输赢:{win}")
  337. if win:
  338. return True
  339. else:
  340. return False
  341. def dokan_choose_attack_priority(self, attack_priority: int):
  342. ''' 选择优先攻击
  343. : return
  344. '''
  345. logger.info("选择优先攻击")
  346. ret = False
  347. max_try = 5
  348. if not self.appear_then_click(self.I_RYOU_DOKAN_PRIORITY, interval=2):
  349. logger.error(f"can not find dokan priority option button, choose attack priority process skipped")
  350. return ret
  351. logger.info(f"start select attack priority: {attack_priority}, remain try: {max_try}")
  352. while 1:
  353. time.sleep(2)
  354. self.screenshot()
  355. if max_try <= 0:
  356. logger.warn("give up priority selection!")
  357. break
  358. if attack_priority == 0:
  359. if self.appear_then_click(self.I_RYOU_DOKAN_PRIORITY_0, interval=1.8):
  360. logger.info(f"selected attack priority: {attack_priority}")
  361. else:
  362. continue
  363. elif attack_priority == 1:
  364. if self.appear_then_click(self.I_RYOU_DOKAN_PRIORITY_1, interval=1.8):
  365. logger.info(f"selected attack priority: {attack_priority}")
  366. else:
  367. continue
  368. elif attack_priority == 2:
  369. if self.appear_then_click(self.I_RYOU_DOKAN_PRIORITY_2, interval=1.8):
  370. logger.info(f"selected attack priority: {attack_priority}")
  371. else:
  372. continue
  373. elif attack_priority == 3:
  374. if self.appear_then_click(self.I_RYOU_DOKAN_PRIORITY_3, interval=1.8):
  375. logger.info(f"selected attack priority: {attack_priority}")
  376. else:
  377. continue
  378. elif attack_priority == 4:
  379. if self.appear_then_click(self.I_RYOU_DOKAN_PRIORITY_4, interval=1.8):
  380. logger.info(f"selected attack priority: {attack_priority}")
  381. else:
  382. continue
  383. else:
  384. self.appear_then_click(self.I_RYOU_DOKAN_PRIORITY_0, interval=1.8)
  385. logger.error(f"invalid attack priority setting: {attack_priority}")
  386. self.attack_priority_selected = True
  387. ret = True
  388. break
  389. return ret
  390. def anti_detect(self, random_move: bool = True, random_click: bool = True, random_delay: bool = True):
  391. '''额外的防封测试
  392. 准备找个号做做爬楼活动的每天300次试试
  393. '''
  394. res = False
  395. # 三个行为中,任何一个生效了都返回True, 否则返回False
  396. if random_move:
  397. self.random_click_swipt()
  398. res = True
  399. if random_click:
  400. # 0到2秒之间的随机浮点数
  401. sleep = random.uniform(0, 2)
  402. # 只保留2位小数
  403. sleep = round(sleep, 2)
  404. if not self.config.dokan.dokan_config.anti_detect_click_fixed_random_area:
  405. # 多搞几个安全点击区域
  406. num = random.randint(0, 5)
  407. if num == 0:
  408. self.click(click=self.C_DOKAN_RANDOM_CLICK_AREA, interval=sleep)
  409. elif num == 1:
  410. self.click(click=self.C_DOKAN_RANDOM_CLICK_AREA2, interval=sleep)
  411. elif num == 2:
  412. self.click(click=self.C_DOKAN_RANDOM_CLICK_AREA3, interval=sleep)
  413. elif num == 3:
  414. self.click(click=self.C_DOKAN_RANDOM_CLICK_AREA, interval=sleep)
  415. else:
  416. self.click(click=self.C_DOKAN_RANDOM_CLICK_AREA2, interval=sleep)
  417. else:
  418. # 假设安全区域是绿色的
  419. safe_color_lower = np.array([45, 25, 25]) # HSV颜色空间的绿色下界
  420. safe_color_upper = np.array([90, 255,255]) # HSV颜色空间的绿色上界
  421. pos = detect_safe_area2(self.device.image, safe_color_lower, safe_color_upper, 3, True)
  422. logger.info(f"random click area: {pos}, delay: {sleep}")
  423. self.click(pos)
  424. res = True
  425. if random_delay:
  426. # 0到2秒之间的随机浮点数
  427. sleep = random.uniform(0, 5)
  428. # 只保留2位小数
  429. sleep = round(sleep, 2)
  430. time.sleep(sleep)
  431. res = True
  432. return res
  433. def goto_main(self):
  434. ''' 保持好习惯,一个任务结束了就返回庭院,方便下一任务的开始或者是出错重启
  435. FIXME 退出道馆。注意:有的时候有退出确认框,有的时候没有。未找到规律。
  436. 先试试用确认框的,若是实在不行,就改成等道馆时间结束后,系统自动退出
  437. 但是如果出错了,需要重启任务时必须走GameUi.ui_goto(page_main),
  438. 那样有或者无确认框不确定性还是会导致ui_goto()出错
  439. '''
  440. # self.ui_current = page_dokan
  441. # self.ui_goto(page_main)
  442. max_try = 3
  443. while 1:
  444. self.screenshot()
  445. if self.appear_then_click(GeneralBattle.I_EXIT, interval=1.5):
  446. logger.info(f"Click {GeneralBattle.I_EXIT.name}")
  447. continue
  448. # 点了后EXIT后,可能无确认框
  449. if self.appear_then_click(self.I_RYOU_DOKAN_EXIT_ENSURE, interval=1.5):
  450. logger.info(f"Click {self.I_RYOU_DOKAN_EXIT_ENSURE}")
  451. break
  452. else:
  453. max_try -= 1
  454. time.sleep(1.2)
  455. if max_try <= 0:
  456. break
  457. # 退出道馆地图
  458. while 1:
  459. self.screenshot()
  460. # 确认后会跳到竂地图,再点击左上角的蓝色的返回,但是不知道这个返回有没有确认
  461. if self.appear_then_click(GameUi.I_BACK_BL, interval=2.5):
  462. logger.info(f"Click {GameUi.I_BACK_BL.name}")
  463. break
  464. def goto_dokan(self):
  465. ''' 进入道馆
  466. TODO 道馆相关场景
  467. :return
  468. '''
  469. self.ui_get_current_page()
  470. try_count = 0
  471. while try_count < 5:
  472. # 点击进入道馆
  473. logger.info("进入阴阳竂")
  474. self.ui_goto(page_guild)
  475. # 查找道馆
  476. activity = "道馆"
  477. pos = self.list_find(self.L_RYOU_ACTIVITY_LIST, activity)
  478. if pos:
  479. logger.info(f"Enter Dokan map: pos = {pos}")
  480. self.device.click(x=pos[0], y=pos[1])
  481. # 进了后的画面,我没有截图,盲猜应该是在画面正中间的
  482. time.sleep(2)
  483. image = self.screenshot()
  484. # O_DOKAN_MAP = RuleOcr(roi=(270,130,740,460), area=(270,130,740,460), mode="Full", method="Default", keyword="万", name="dokan_map")
  485. # image = cv2.imread("g:/yys/oas/tests/2.png")
  486. pos = self.O_DOKAN_MAP.ocr_full(image)
  487. if pos == (0, 0, 0, 0):
  488. logger.info("failed to find {self.O_DOKAN_MAP.keyword}")
  489. else:
  490. # 取中间
  491. x = pos[0] + pos[2]/2
  492. # 往上偏移20
  493. y = pos[1] - 20
  494. logger.info("ocr detect result pos={pos}, try click pos, x={x}, y={y}")
  495. self.device.click(x=x, y=y)
  496. break
  497. try_count += 1
  498. time.sleep(1)
  499. print(f"try_count: {try_count}")
  500. def start_cheering(self):
  501. logger.info("start cheering")
  502. def get_current_scene(self, reuse_screenshot: bool = True):
  503. ''' 检测当前场景
  504. '''
  505. if not reuse_screenshot:
  506. self.screenshot()
  507. logger.info(f"get_current_scene={reuse_screenshot}")
  508. # 场景检测:阴阳竂
  509. if self.appear(self.I_SCENE_RYOU, threshold=0.8):
  510. return False, DokanScene.RYOU_DOKAN_RYOU
  511. # 状态:判断是否集结中
  512. # if self.ocr_appear(self.O_DOKAN_GATHERING):
  513. if self.appear(self.I_RYOU_DOKAN_GATHERING, threshold=0.95):
  514. return True, DokanScene.RYOU_DOKAN_SCENE_GATHERING
  515. # 状态:是否在等待馆主战
  516. # if self.ocr_appear(self.O_DOKAN_BOSS_WAITING):
  517. if self.appear(self.I_DOKAN_BOSS_WAITING):
  518. return True, DokanScene.RYOU_DOKAN_SCENE_BOSS_WAITING
  519. # 状态:检查右下角有没有挑战?通常是失败了,并退出来到集结界面,可重新开始点击右下角挑战进入战斗
  520. elif self.appear(self.I_RYOU_DOKAN_START_CHALLENGE, 0.95):
  521. return True, DokanScene.RYOU_DOKAN_SCENE_START_CHALLENGE
  522. # 状态:进入战斗,待开始
  523. elif self.appear(self.I_RYOU_DOKAN_IN_FIELD, threshold=0.85):
  524. return True, DokanScene.RYOU_DOKAN_SCENE_IN_FIELD
  525. # 状态:战斗结算,可能是打完小朋友了,也可能是失败了。
  526. if self.appear(self.I_RYOU_DOKAN_BATTLE_OVER, threshold=0.85):
  527. return True, DokanScene.RYOU_DOKAN_SCENE_BATTLE_OVER
  528. # 状态:达到失败次数,CD中
  529. if self.appear(self.I_RYOU_DOKAN_CD, threshold=0.8):
  530. return True, DokanScene.RYOU_DOKAN_SCENE_CD
  531. # 状态:加油中,左下角有鼓
  532. if self.appear_then_click(self.I_RYOU_DOKAN_CHEERING, threshold=0.8) or self.appear(self.I_RYOU_DOKAN_CHEERING_GRAY, threshold=0.8):
  533. return True, DokanScene.RYOU_DOKAN_SCENE_CHEERING
  534. # # 状态:战斗中,左上角的加油图标
  535. # if self.appear(self.I_RYOU_DOKAN_FIGHTING, threshold=0.8):
  536. # return True, DokanScene.RYOU_DOKAN_SCENE_FIGHTING
  537. # if self.appear(self.I_RYOU_DOKAN_FAILED_VOTE_NO):
  538. # return True, DokanScene.RYOU_DOKAN_SCENE_FAILED_VOTE_NO
  539. # 状态:道馆已经结束
  540. # if self.ocr_appear(self.O_DOKAN_SUCCEEDED):
  541. if self.appear(self.I_RYOU_DOKAN_FINISHED, threshold=0.8):
  542. return True, DokanScene.RYOU_DOKAN_SCENE_FINISHED
  543. return False, DokanScene.RYOU_DOKAN_SCENE_UNKNOWN
  544. def test_ocr_locate_dokan_target():
  545. import cv2
  546. from module.atom.ocr import RuleOcr
  547. logger.info("========================================>>test 2")
  548. O_DOKAN_MAP = RuleOcr(roi=(270,130,740,460), area=(270,130,740,460), mode="Full", method="Default", keyword="万", name="dokan_map")
  549. image = cv2.imread("g:/yys/oas/tests/1.png")
  550. pos = O_DOKAN_MAP.ocr_full(image)
  551. print(f"image1: {pos}")
  552. image = cv2.imread("g:/yys/oas/tests/2.png")
  553. pos = O_DOKAN_MAP.ocr_full(image)
  554. print(f"image2: {pos}")
  555. image = cv2.imread("g:/yys/oas/tests/3.png")
  556. pos = O_DOKAN_MAP.ocr_full(image)
  557. print(f"image3: {pos}")
  558. image = cv2.imread("g:/yys/oas/tests/4.png")
  559. pos = O_DOKAN_MAP.ocr_full(image)
  560. print(f"image4: {pos}")
  561. if pos == (0, 0, 0, 0):
  562. logger.info("failed to find {self.O_DOKAN_MAP.keyword}")
  563. else:
  564. # 取中间
  565. x = pos[0] + pos[2]/2
  566. # 往上偏移20
  567. y = pos[1] - 20
  568. logger.info(f"ocr detect result pos={pos}, try click pos, x={x}, y={y}")
  569. def test_anti_detect_random_click():
  570. import cv2
  571. # image_list = ['g:/yys/oas/tests/1.png', 'g:/yys/oas/tests/2.png', 'g:/yys/oas/tests/3.png', 'g:/yys/oas/tests/4.png', 'g:/yys/oas/tests/5.png']
  572. image_list = ['g:/yys/oas/tests/a.png', 'g:/yys/oas/tests/b.png', 'g:/yys/oas/tests/c.png']
  573. for item in image_list:
  574. print(f"item: {item}")
  575. image = cv2.imread(item)
  576. # 注意:这里的颜色范围需要根据实际图片进行调整
  577. # http://kb.rg4.net/docs/omassistant/omassistant-1fnirpstkhdnh
  578. # 0<=h<20, 红色
  579. # 30<=h<45,黄色
  580. # 45<=h<90,绿色
  581. # 90<=h<125,青色
  582. # 125<=h<150,蓝色
  583. # 150<=h<175,紫色
  584. # 175<=h<200,粉红色
  585. # 200<=h<220,砖红色
  586. # 220<=h<255,品红色
  587. # 示例使用:假设安全区域是绿色的
  588. safe_color_lower = np.array([45, 25, 25]) # HSV颜色空间的绿色下界
  589. safe_color_upper = np.array([90, 255,255]) # HSV颜色空间的绿色上界
  590. # 读取图片
  591. image = cv2.imread('c.png')
  592. if image is None:
  593. print("Error: Image not found.")
  594. else:
  595. # 调用函数
  596. pos = detect_safe_area2(image, safe_color_lower, safe_color_upper, 3, True)
  597. print(f"pos={pos}")
  598. # 显示结果
  599. cv2.imshow('Safe Area', image)
  600. cv2.waitKey(0)
  601. cv2.destroyAllWindows()
  602. def test_goto_main():
  603. from module.config.config import Config
  604. from module.device.device import Device
  605. from tasks.GameUi.page import page_dokan
  606. config = Config('oas1')
  607. device = Device(config)
  608. t = ScriptTask(config, device)
  609. # t.run()
  610. t.ui_current = page_dokan
  611. t.ui_goto(page_main)
  612. if __name__ == "__main__":
  613. # from module.config.config import Config
  614. # from module.device.device import Device
  615. # config = Config('oas1')
  616. # device = Device(config)
  617. # t = ScriptTask(config, device)
  618. # t.run()
  619. # test_ocr_locate_dokan_target()
  620. # test_anti_detect_random_click()
  621. test_goto_main()