general_buff.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import cv2
  6. import numpy as np
  7. from tasks.Component.GeneralBuff.assets import GeneralBuffAssets
  8. from module.atom.ocr import RuleOcr
  9. from module.atom.image import RuleImage
  10. from tasks.base_task import BaseTask
  11. from module.logger import logger
  12. class GeneralBuff(BaseTask, GeneralBuffAssets):
  13. def open_buff(self):
  14. """
  15. 打开buff的总界面
  16. :return:
  17. """
  18. logger.info('Open buff')
  19. while 1:
  20. self.screenshot()
  21. if self.appear(self.I_CLOUD):
  22. break
  23. if self.appear_then_click(self.I_BUFF_1, interval=2):
  24. continue
  25. check_image = self.I_AWAKE
  26. while 1:
  27. self.screenshot()
  28. if self.appear(check_image):
  29. break
  30. self.swipe(self.S_BUFF_UP, interval=2)
  31. def close_buff(self):
  32. """
  33. 关闭buff的总界面, 但是要确保buff界面已经打开了
  34. :return:
  35. """
  36. logger.info('Close buff')
  37. while 1:
  38. self.screenshot()
  39. if not self.appear(self.I_CLOUD):
  40. break
  41. if self.appear_then_click(self.I_BUFF_1, interval=2):
  42. continue
  43. def get_area(self, buff: RuleOcr) -> tuple:
  44. """
  45. 获取要点击的开关buff的区域
  46. :param cls:
  47. :param image:
  48. :param buff:
  49. :return: 如果没有就返回None
  50. """
  51. # 防止邀请框挡住BUFF框架
  52. self.reject_invite()
  53. self.screenshot()
  54. area = buff.ocr(self.device.image)
  55. if area == tuple([0, 0, 0, 0]):
  56. logger.info('No gold 50 buff')
  57. return None
  58. # 开始的x坐标就是文字的右边
  59. start_x = area[0] + area[2] + 10 # 10是文字和开关之间的间隔
  60. start_y = area[1] - 10
  61. width = 80 # 开关的宽度 80够了
  62. height = area[3] + 20
  63. return int(start_x), int(start_y), int(width), int(height)
  64. def set_switch_area(self, area):
  65. """
  66. 设置开关的区域
  67. :param area:
  68. :return:
  69. """
  70. self.I_OPEN_YELLOW.roi_back = list(area) # 动态设置roi
  71. self.I_CLOSE_RED.roi_back = list(area)
  72. def gold_50(self, is_open: bool = True):
  73. """
  74. 金币50buff
  75. :param is_open: 是否打开
  76. :return:
  77. """
  78. logger.info('Gold 50 buff')
  79. self.screenshot()
  80. area = self.get_area(self.O_GOLD_50)
  81. if not area:
  82. logger.warning('No gold 50 buff')
  83. return None
  84. self.I_OPEN_YELLOW.roi_back = list(area) # 动态设置roi
  85. self.I_CLOSE_RED.roi_back = list(area)
  86. if is_open:
  87. while 1:
  88. self.screenshot()
  89. if self.appear(self.I_OPEN_YELLOW):
  90. break
  91. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  92. continue
  93. else:
  94. while 1:
  95. self.screenshot()
  96. if self.appear(self.I_CLOSE_RED):
  97. break
  98. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  99. continue
  100. def gold_100(self, is_open: bool = True):
  101. """
  102. 金币100buff
  103. :param is_open: 是否打开
  104. :return:
  105. """
  106. logger.info('Gold 100 buff')
  107. self.screenshot()
  108. area = self.get_area(self.O_GOLD_100)
  109. if not area:
  110. logger.warning('No gold 100 buff')
  111. return None
  112. self.I_OPEN_YELLOW.roi_back = list(area)
  113. self.I_CLOSE_RED.roi_back = list(area)
  114. if is_open:
  115. while 1:
  116. self.screenshot()
  117. if self.appear(self.I_OPEN_YELLOW):
  118. break
  119. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  120. continue
  121. else:
  122. while 1:
  123. self.screenshot()
  124. if self.appear(self.I_CLOSE_RED):
  125. break
  126. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  127. continue
  128. def exp_50(self, is_open: bool = True):
  129. """
  130. 经验50buff
  131. :param is_open: 是否打开
  132. :return:
  133. """
  134. logger.info('Exp 50 buff')
  135. while 1:
  136. self.screenshot()
  137. area = self.get_area(self.O_EXP_50)
  138. if not area:
  139. logger.warning('No exp 50 buff')
  140. continue
  141. self.set_switch_area(area)
  142. if not self.appear(self.I_OPEN_YELLOW) and not self.appear(self.I_CLOSE_RED):
  143. logger.info('No exp 50 buff')
  144. self.device.swipe(p2=(530, 240), p1=(580, 320))
  145. time.sleep(1)
  146. else:
  147. break
  148. if is_open:
  149. while 1:
  150. self.screenshot()
  151. if self.appear(self.I_OPEN_YELLOW):
  152. break
  153. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  154. continue
  155. else:
  156. while 1:
  157. self.screenshot()
  158. if self.appear(self.I_CLOSE_RED):
  159. break
  160. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  161. continue
  162. def exp_100(self, is_open: bool = True):
  163. """
  164. 经验100buff
  165. :param is_open: 是否打开
  166. :return:
  167. """
  168. logger.info('Exp 100 buff')
  169. while 1:
  170. self.screenshot()
  171. area = self.get_area(self.O_EXP_100)
  172. if not area:
  173. logger.warning('No exp 100 buff')
  174. continue
  175. self.set_switch_area(area)
  176. if not self.appear(self.I_OPEN_YELLOW) and not self.appear(self.I_CLOSE_RED):
  177. logger.info('No exp 100 buff')
  178. self.device.swipe(p2=(530, 240), p1=(580, 320))
  179. time.sleep(1)
  180. else:
  181. break
  182. if is_open:
  183. while 1:
  184. self.screenshot()
  185. if self.appear(self.I_OPEN_YELLOW):
  186. break
  187. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  188. continue
  189. else:
  190. while 1:
  191. self.screenshot()
  192. if self.appear(self.I_CLOSE_RED):
  193. break
  194. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  195. continue
  196. def get_area_image(self, target: RuleImage) -> list:
  197. """
  198. 获取觉醒加成或者是御魂加成所要点击的区域
  199. 因为实在的图片比ocr快
  200. :param image:
  201. :param target:
  202. :return:
  203. """
  204. self.reject_invite()
  205. self.screenshot()
  206. if not target.match(self.device.image):
  207. logger.warning(f'No {target.name} buff')
  208. return None
  209. # logger.info(f'front area: {target.roi_front}')
  210. # logger.info(f'front center: {target.front_center()}')
  211. start_x = int(target.front_center()[0] + 364)
  212. start_y = int(target.roi_front[1])
  213. width = 80
  214. height = int(target.roi_front[3])
  215. return [start_x, start_y, width, height]
  216. def awake(self, is_open: bool = True):
  217. """
  218. 觉醒buff
  219. :param is_open: 是否打开
  220. :return:
  221. """
  222. logger.info('Awake buff')
  223. self.screenshot()
  224. area = self.get_area_image(self.I_AWAKE)
  225. if not area:
  226. logger.warning('No awake buff')
  227. return None
  228. self.set_switch_area(area)
  229. if is_open:
  230. while 1:
  231. self.screenshot()
  232. if self.appear(self.I_OPEN_YELLOW):
  233. break
  234. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  235. continue
  236. else:
  237. while 1:
  238. self.screenshot()
  239. if self.appear(self.I_CLOSE_RED):
  240. break
  241. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  242. continue
  243. def soul(self, is_open: bool = True):
  244. """
  245. 御魂buff
  246. :param is_open: 是否打开
  247. :return:
  248. """
  249. logger.info('Soul buff')
  250. self.screenshot()
  251. area = self.get_area_image(self.I_SOUL)
  252. if not area:
  253. logger.warning('No soul buff')
  254. return None
  255. self.set_switch_area(area)
  256. if is_open:
  257. while 1:
  258. self.screenshot()
  259. if self.appear(self.I_OPEN_YELLOW):
  260. break
  261. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  262. continue
  263. else:
  264. while 1:
  265. self.screenshot()
  266. if self.appear(self.I_CLOSE_RED):
  267. break
  268. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  269. continue
  270. def reject_invite(self):
  271. from tasks.Component.GeneralInvite.assets import GeneralInviteAssets as gia
  272. while 1:
  273. self.screenshot()
  274. if not (self.appear(gia.I_I_REJECT_1) or self.appear(gia.I_I_REJECT_2) or self.appear(gia.I_I_REJECT_3)):
  275. break
  276. if self.appear(gia.I_I_REJECT_3):
  277. self.click(gia.I_I_REJECT_3, 6)
  278. continue
  279. if self.appear(gia.I_I_REJECT_2):
  280. self.click(gia.I_I_REJECT_2, 6)
  281. continue
  282. if self.appear(gia.I_I_REJECT_1):
  283. self.click(gia.I_I_REJECT_1, 6)
  284. continue
  285. if __name__ == '__main__':
  286. from module.config.config import Config
  287. from module.device.device import Device
  288. c = Config('oas1')
  289. d = Device(c)
  290. t = GeneralBuff(c, d)
  291. t.open_buff()
  292. # t.screenshot()
  293. #
  294. t.awake(is_open=True)
  295. t.soul(is_open=True)
  296. # t.gold_50(is_open=True)
  297. t.gold_100(is_open=True)
  298. t.exp_50(is_open=True)
  299. t.exp_100(is_open=True)