general_buff.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 100 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. self.screenshot()
  134. area = self.get_area(self.device.image, self.O_EXP_50)
  135. if not area:
  136. logger.warning('No gold 100 buff')
  137. return None
  138. self.I_OPEN_YELLOW.roi_back = list(area)
  139. self.I_CLOSE_RED.roi_back = list(area)
  140. if is_open:
  141. while 1:
  142. self.screenshot()
  143. if self.appear(self.I_OPEN_YELLOW):
  144. break
  145. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  146. continue
  147. else:
  148. while 1:
  149. self.screenshot()
  150. if self.appear(self.I_CLOSE_RED):
  151. break
  152. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  153. continue
  154. def exp_100(self, is_open: bool = True):
  155. """
  156. 经验100buff
  157. :param is_open: 是否打开
  158. :return:
  159. """
  160. logger.info('Exp 100 buff')
  161. while 1:
  162. self.screenshot()
  163. area = self.get_area(self.device.image, self.O_EXP_100)
  164. if not area:
  165. logger.warning('No gold 100 buff')
  166. continue
  167. self.set_switch_area(area)
  168. if not self.appear(self.I_OPEN_YELLOW) and not self.appear(self.I_CLOSE_RED):
  169. logger.info('No exp 100 buff')
  170. self.device.swipe(p2=(530, 240), p1=(580, 320))
  171. time.sleep(1)
  172. else:
  173. break
  174. if is_open:
  175. while 1:
  176. self.screenshot()
  177. if self.appear(self.I_OPEN_YELLOW):
  178. break
  179. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  180. continue
  181. else:
  182. while 1:
  183. self.screenshot()
  184. if self.appear(self.I_CLOSE_RED):
  185. break
  186. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  187. continue
  188. @staticmethod
  189. def get_area_image(image: np.array, target: RuleImage) -> list:
  190. """
  191. 获取觉醒加成或者是御魂加成所要点击的区域
  192. 因为实在的图片比ocr快
  193. :param image:
  194. :param target:
  195. :return:
  196. """
  197. if not target.match(image):
  198. logger.warning(f'No {target.name} buff')
  199. return None
  200. # logger.info(f'front area: {target.roi_front}')
  201. # logger.info(f'front center: {target.front_center()}')
  202. start_x = int(target.front_center()[0] + 364)
  203. start_y = int(target.roi_front[1])
  204. width = 80
  205. height = int(target.roi_front[3])
  206. return [start_x, start_y, width, height]
  207. def awake(self, is_open: bool = True):
  208. """
  209. 觉醒buff
  210. :param is_open: 是否打开
  211. :return:
  212. """
  213. logger.info('Awake buff')
  214. self.screenshot()
  215. area = self.get_area_image(self.device.image, self.I_AWAKE)
  216. if not area:
  217. logger.warning('No awake buff')
  218. return None
  219. self.set_switch_area(area)
  220. if is_open:
  221. while 1:
  222. self.screenshot()
  223. if self.appear(self.I_OPEN_YELLOW):
  224. break
  225. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  226. continue
  227. else:
  228. while 1:
  229. self.screenshot()
  230. if self.appear(self.I_CLOSE_RED):
  231. break
  232. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  233. continue
  234. def soul(self, is_open: bool = True):
  235. """
  236. 御魂buff
  237. :param is_open: 是否打开
  238. :return:
  239. """
  240. logger.info('Soul buff')
  241. self.screenshot()
  242. area = self.get_area_image(self.device.image, self.I_SOUL)
  243. if not area:
  244. logger.warning('No soul buff')
  245. return None
  246. self.set_switch_area(area)
  247. if is_open:
  248. while 1:
  249. self.screenshot()
  250. if self.appear(self.I_OPEN_YELLOW):
  251. break
  252. if self.appear_then_click(self.I_CLOSE_RED, interval=1):
  253. continue
  254. else:
  255. while 1:
  256. self.screenshot()
  257. if self.appear(self.I_CLOSE_RED):
  258. break
  259. if self.appear_then_click(self.I_OPEN_YELLOW, interval=1):
  260. continue
  261. if __name__ == '__main__':
  262. from module.config.config import Config
  263. from module.device.device import Device
  264. c = Config('oas1')
  265. d = Device(c)
  266. t = GeneralBuff(c, d)
  267. t.open_buff()
  268. # t.screenshot()
  269. #
  270. t.awake(is_open=True)
  271. t.soul(is_open=True)
  272. # t.gold_50(is_open=True)
  273. t.gold_100(is_open=True)
  274. t.exp_50(is_open=True)
  275. t.exp_100(is_open=True)