general_buff.py 8.8 KB

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