general_buff.py 8.9 KB

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