general_buff.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. @staticmethod
  44. def get_area(image: np.array, buff: RuleOcr) -> tuple:
  45. """
  46. 获取要点击的开关buff的区域
  47. :param cls:
  48. :param image:
  49. :param buff:
  50. :return: 如果没有就返回None
  51. """
  52. area = buff.ocr(image)
  53. if area == tuple([0, 0, 0, 0]):
  54. logger.info('No gold 50 buff')
  55. return None
  56. # 开始的x坐标就是文字的右边
  57. start_x = area[0] + area[2] + 10 # 10是文字和开关之间的间隔
  58. start_y = area[1] - 10
  59. width = 80 # 开关的宽度 80够了
  60. height = area[3] + 20
  61. return int(start_x), int(start_y), int(width), int(height)
  62. def set_switch_area(self, area):
  63. """
  64. 设置开关的区域
  65. :param area:
  66. :return:
  67. """
  68. self.I_OPEN_YELLOW.roi_back = list(area) # 动态设置roi
  69. self.I_CLOSE_RED.roi_back = list(area)
  70. def gold_50(self, is_open: bool = True):
  71. """
  72. 金币50buff
  73. :param is_open: 是否打开
  74. :return:
  75. """
  76. logger.info('Gold 50 buff')
  77. self.screenshot()
  78. area = self.get_area(self.device.image, self.O_GOLD_50)
  79. if not area:
  80. logger.warning('No gold 50 buff')
  81. return None
  82. self.I_OPEN_YELLOW.roi_back = list(area) # 动态设置roi
  83. self.I_CLOSE_RED.roi_back = list(area)
  84. if is_open:
  85. while 1:
  86. self.screenshot()
  87. if self.appear(self.I_OPEN_YELLOW):
  88. break
  89. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  90. continue
  91. else:
  92. while 1:
  93. self.screenshot()
  94. if self.appear(self.I_CLOSE_RED):
  95. break
  96. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  97. continue
  98. def gold_100(self, is_open: bool = True):
  99. """
  100. 金币100buff
  101. :param is_open: 是否打开
  102. :return:
  103. """
  104. logger.info('Gold 100 buff')
  105. self.screenshot()
  106. area = self.get_area(self.device.image, self.O_GOLD_100)
  107. if not area:
  108. logger.warning('No gold 100 buff')
  109. return None
  110. self.I_OPEN_YELLOW.roi_back = list(area)
  111. self.I_CLOSE_RED.roi_back = list(area)
  112. if is_open:
  113. while 1:
  114. self.screenshot()
  115. if self.appear(self.I_OPEN_YELLOW):
  116. break
  117. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  118. continue
  119. else:
  120. while 1:
  121. self.screenshot()
  122. if self.appear(self.I_CLOSE_RED):
  123. break
  124. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  125. continue
  126. def exp_50(self, is_open: bool = True):
  127. """
  128. 经验50buff
  129. :param is_open: 是否打开
  130. :return:
  131. """
  132. logger.info('Exp 50 buff')
  133. while 1:
  134. self.screenshot()
  135. area = self.get_area(self.device.image, self.O_EXP_50)
  136. if not area:
  137. logger.warning('No exp 50 buff')
  138. continue
  139. self.set_switch_area(area)
  140. if not self.appear(self.I_OPEN_YELLOW) and not self.appear(self.I_CLOSE_RED):
  141. logger.info('No exp 50 buff')
  142. self.device.swipe(p2=(530, 240), p1=(580, 320))
  143. time.sleep(1)
  144. else:
  145. break
  146. if is_open:
  147. while 1:
  148. self.screenshot()
  149. if self.appear(self.I_OPEN_YELLOW):
  150. break
  151. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  152. continue
  153. else:
  154. while 1:
  155. self.screenshot()
  156. if self.appear(self.I_CLOSE_RED):
  157. break
  158. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  159. continue
  160. def exp_100(self, is_open: bool = True):
  161. """
  162. 经验100buff
  163. :param is_open: 是否打开
  164. :return:
  165. """
  166. logger.info('Exp 100 buff')
  167. while 1:
  168. self.screenshot()
  169. area = self.get_area(self.device.image, self.O_EXP_100)
  170. if not area:
  171. logger.warning('No exp 100 buff')
  172. continue
  173. self.set_switch_area(area)
  174. if not self.appear(self.I_OPEN_YELLOW) and not self.appear(self.I_CLOSE_RED):
  175. logger.info('No exp 100 buff')
  176. self.device.swipe(p2=(530, 240), p1=(580, 320))
  177. time.sleep(1)
  178. else:
  179. break
  180. if is_open:
  181. while 1:
  182. self.screenshot()
  183. if self.appear(self.I_OPEN_YELLOW):
  184. break
  185. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  186. continue
  187. else:
  188. while 1:
  189. self.screenshot()
  190. if self.appear(self.I_CLOSE_RED):
  191. break
  192. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  193. continue
  194. @staticmethod
  195. def get_area_image(image: np.array, target: RuleImage) -> list:
  196. """
  197. 获取觉醒加成或者是御魂加成所要点击的区域
  198. 因为实在的图片比ocr快
  199. :param image:
  200. :param target:
  201. :return:
  202. """
  203. if not target.match(image):
  204. logger.warning(f'No {target.name} buff')
  205. return None
  206. # logger.info(f'front area: {target.roi_front}')
  207. # logger.info(f'front center: {target.front_center()}')
  208. start_x = int(target.front_center()[0] + 364)
  209. start_y = int(target.roi_front[1])
  210. width = 80
  211. height = int(target.roi_front[3])
  212. return [start_x, start_y, width, height]
  213. def awake(self, is_open: bool = True):
  214. """
  215. 觉醒buff
  216. :param is_open: 是否打开
  217. :return:
  218. """
  219. logger.info('Awake buff')
  220. self.screenshot()
  221. area = self.get_area_image(self.device.image, self.I_AWAKE)
  222. if not area:
  223. logger.warning('No awake buff')
  224. return None
  225. self.set_switch_area(area)
  226. if is_open:
  227. while 1:
  228. self.screenshot()
  229. if self.appear(self.I_OPEN_YELLOW):
  230. break
  231. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  232. continue
  233. else:
  234. while 1:
  235. self.screenshot()
  236. if self.appear(self.I_CLOSE_RED):
  237. break
  238. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  239. continue
  240. def soul(self, is_open: bool = True):
  241. """
  242. 御魂buff
  243. :param is_open: 是否打开
  244. :return:
  245. """
  246. logger.info('Soul buff')
  247. self.screenshot()
  248. area = self.get_area_image(self.device.image, self.I_SOUL)
  249. if not area:
  250. logger.warning('No soul buff')
  251. return None
  252. self.set_switch_area(area)
  253. if is_open:
  254. while 1:
  255. self.screenshot()
  256. if self.appear(self.I_OPEN_YELLOW):
  257. break
  258. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  259. continue
  260. else:
  261. while 1:
  262. self.screenshot()
  263. if self.appear(self.I_CLOSE_RED):
  264. break
  265. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  266. continue
  267. if __name__ == '__main__':
  268. from module.config.config import Config
  269. from module.device.device import Device
  270. c = Config('oas1')
  271. d = Device(c)
  272. t = GeneralBuff(c, d)
  273. t.open_buff()
  274. # t.screenshot()
  275. #
  276. t.awake(is_open=True)
  277. t.soul(is_open=True)
  278. # t.gold_50(is_open=True)
  279. t.gold_100(is_open=True)
  280. t.exp_50(is_open=True)
  281. t.exp_100(is_open=True)