script_task.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import random
  6. import re
  7. from cached_property import cached_property
  8. from enum import Enum
  9. from datetime import timedelta
  10. from module.exception import TaskEnd, RequestHumanTakeover
  11. from module.logger import logger
  12. from module.base.timer import Timer
  13. from module.atom.ocr import RuleOcr
  14. from tasks.GameUi.game_ui import GameUi
  15. from tasks.GameUi.page import page_main, page_guild
  16. from tasks.CollectiveMissions.assets import CollectiveMissionsAssets
  17. class MC(str, Enum):
  18. BL = '契灵'
  19. AW1 = '觉醒一'
  20. AW2 = '觉醒二'
  21. AW3 = '觉醒三'
  22. GR1 = '御灵一'
  23. GR2 = '御灵二'
  24. GR3 = '御灵三'
  25. SO1 = '御魂一'
  26. SO2 = '御魂二'
  27. FRIEND = '好友'
  28. UNKNOWN = '未知'
  29. class ScriptTask(GameUi, CollectiveMissionsAssets):
  30. missions: list = [] # 用于记录三个的任务的种类
  31. @cached_property
  32. def rule(self) -> list:
  33. rule = self.config.collective_missions.missions_config.missions_rule
  34. rule = rule.replace(' ', '').replace('\n', '')
  35. # 正则表达式 分离 ">"
  36. rule = re.split(r'>', rule)
  37. mc_values_list = [member.value for member in MC]
  38. rule = [item for item in rule if item in mc_values_list]
  39. return rule
  40. def run(self):
  41. self.ui_get_current_page()
  42. self.ui_goto(page_guild)
  43. rule = self.config.collective_missions.missions_config.missions_rule
  44. self.ui_click(self.I_CM_SHRINE, self.I_CM_CM)
  45. self.ui_click(self.I_CM_CM, self.I_CM_RECORDS)
  46. logger.info('Start to detect missions')
  47. # 判断今天是否已经完成了, 还是多少次数的任务
  48. self.screenshot()
  49. current, remain, total = self.O_CM_NUMBER.ocr(self.device.image)
  50. if current == total == 30:
  51. logger.warning('Today\'s missions have been completed')
  52. self.set_next_run(task='CollectiveMissions', success=False, finish=True)
  53. raise TaskEnd('CollectiveMissions')
  54. # 判断最优的任务是哪一个
  55. mission, index = self.detect_best()
  56. logger.info(f'Best mission is {mission}')
  57. logger.info(f'Best mission index is {index}')
  58. if mission == MC.BL:
  59. # 契灵单独处理
  60. self._bondling_fairyland(index)
  61. elif mission == MC.AW1 or mission == MC.AW2 or mission == MC.AW3 \
  62. or mission == MC.GR1 or mission == MC.GR2 or mission == MC.GR3:
  63. # 其他就捐材料
  64. self._donate(index)
  65. elif mission == MC.SO1 or mission == MC.SO2:
  66. # 御魂就捐御魂
  67. self._soul(index)
  68. # 退出
  69. while 1:
  70. self.screenshot()
  71. if self.appear(self.I_CM_SHRINE) or self.appear(self.I_CHECK_MAIN):
  72. break
  73. if self.appear_then_click(self.I_UI_BACK_RED, interval=1):
  74. continue
  75. if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=1):
  76. continue
  77. self.set_next_run(task='CollectiveMissions', success=True, finish=True)
  78. raise TaskEnd('CollectiveMissions')
  79. def detect_one(self, ocr_1: RuleOcr, ocr_2: RuleOcr) -> MC:
  80. """
  81. 检测某一个位置是什么的任务
  82. :param ocr_1:
  83. :param ocr_2:
  84. :return:
  85. """
  86. self.screenshot()
  87. result_1 = ocr_1.ocr(self.device.image)
  88. result_2 = ocr_2.ocr(self.device.image)
  89. result_1 = result_1.replace('·', '')
  90. if result_1 == '结伴同行':
  91. return MC.FRIEND
  92. elif result_1 == '契灵探查':
  93. return MC.BL
  94. if result_1 != '远远不够':
  95. logger.warning(f'Ocr task name: {result_1}')
  96. return MC.UNKNOWN
  97. if result_2 == '觉醒一':
  98. return MC.AW1
  99. elif result_2 == '觉醒二':
  100. return MC.AW2
  101. elif result_2 == '觉醒三':
  102. return MC.AW3
  103. elif result_2 == '御灵一':
  104. return MC.GR1
  105. elif result_2 == '御灵二':
  106. return MC.GR2
  107. elif result_2 == '御灵三':
  108. return MC.GR3
  109. elif result_2 == '御魂一':
  110. return MC.SO1
  111. elif result_2 == '御魂二':
  112. return MC.SO2
  113. return MC.UNKNOWN
  114. def detect_best(self) -> tuple:
  115. """
  116. 自动寻找最好的任务并返回,期间记录三个任务的类型
  117. :return: 任务类型, 0/1/2
  118. """
  119. best_index = 0
  120. best_class = self.detect_one(self.O_CM_1, self.O_CM_2)
  121. self.missions.append(best_class)
  122. # 判断第二个,是否比第一个好
  123. last_index = self.rule.index(best_class)
  124. now_class = self.detect_one(self.O_CM_3, self.O_CM_4)
  125. self.missions.append(now_class)
  126. now_index = self.rule.index(now_class)
  127. if now_index < last_index:
  128. best_index = 1
  129. best_class = now_class
  130. # 判断第三个,是否比前两个好
  131. last_index = self.rule.index(best_class)
  132. now_class = self.detect_one(self.O_CM_5, self.O_CM_6)
  133. self.missions.append(now_class)
  134. if now_class == MC.FRIEND:
  135. logger.info('The third mission is friend')
  136. return best_class, best_index
  137. now_index = self.rule.index(now_class)
  138. if now_index < last_index:
  139. best_index = 2
  140. best_class = now_class
  141. return best_class, best_index
  142. def _bondling_fairyland(self, index: int):
  143. """
  144. 如果御灵已经做了那么就领取奖励
  145. 否则将契灵之境的任务设置为当前,同时两个小时后继续执行当前的任务收菜
  146. :return:
  147. """
  148. def bondling_finish():
  149. self.screenshot()
  150. if self.appear(self.I_CM_REWARDS):
  151. return True
  152. return False
  153. if not bondling_finish():
  154. self.config.bondling_fairyland.scheduler.next_run = self.start_time
  155. if not self.config.bondling_fairyland.scheduler.enable:
  156. logger.error('The scheduler of bondling_fairyland is not enable')
  157. logger.error('Please enable it in config file')
  158. raise RequestHumanTakeover
  159. self.set_next_run(task='CollectiveMissions', success=True, finish=True, target=self.start_time+ timedelta(hours=2))
  160. return True
  161. # 领取奖励
  162. logger.info('Start to collect bondling rewards')
  163. check_timer = Timer(3)
  164. check_timer.start()
  165. while 1:
  166. self.screenshot()
  167. if self.ui_reward_appear_click(True):
  168. check_timer.reset()
  169. continue
  170. if self.appear_then_click(self.I_CM_REWARDS, interval=1):
  171. check_timer.reset()
  172. continue
  173. if check_timer.reached():
  174. break
  175. logger.info('Finish to collect bondling rewards')
  176. def _donate(self, index: int):
  177. """
  178. 捐赠材料
  179. :param index: 0, 1, 2 三个任务的位置
  180. :return:
  181. """
  182. match_click = {
  183. 0: self.C_CM_1,
  184. 1: self.C_CM_2,
  185. 2: self.C_CM_3,
  186. }
  187. while 1:
  188. self.screenshot()
  189. if self.appear(self.I_CM_PRESENT):
  190. break
  191. if self.click(match_click[index], interval=1.5):
  192. continue
  193. # 开始捐材料
  194. logger.info('Start to donate')
  195. # 判断哪一个的材料最多
  196. self.screenshot()
  197. max_index = 0
  198. max_number = 0
  199. for i, ocr in enumerate([self.O_CM_1_MATTER, self.O_CM_2_MATTER,
  200. self.O_CM_3_MATTER, self.O_CM_4_MATTER]):
  201. curr, remain, total = ocr.ocr(self.device.image)
  202. if total > max_number:
  203. max_number = total
  204. max_index = i
  205. if max_number <= 30:
  206. logger.info('The number of all matter is less than 30')
  207. logger.info('Please check your game resolution')
  208. raise RequestHumanTakeover
  209. match_swipe = {
  210. 0: self.S_CM_MATTER_1,
  211. 1: self.S_CM_MATTER_2,
  212. 2: self.S_CM_MATTER_3,
  213. 3: self.S_CM_MATTER_4,
  214. }
  215. match_image = {
  216. 0: self.I_CM_ADD_1,
  217. 1: self.I_CM_ADD_2,
  218. 2: self.I_CM_ADD_3,
  219. 3: self.I_CM_ADD_4,
  220. }
  221. # 滑动到最多的材料
  222. random_click = [self.I_CM_ADD_1, self.I_CM_ADD_2, self.I_CM_ADD_3, self.I_CM_ADD_4]
  223. window_control = self.config.script.device.control_method == 'window_message'
  224. swipe_count = 0
  225. click_count = 0
  226. while 1:
  227. self.screenshot()
  228. if self.appear(self.I_CM_MATTER):
  229. break
  230. if not window_control and self.swipe(match_swipe[max_index], interval=2.5):
  231. swipe_count += 1
  232. time.sleep(1.5)
  233. continue
  234. # 为什么使用window_message无法滑动
  235. if window_control and click_count > 30:
  236. logger.info('Swipe to the most matter failed')
  237. logger.info('Please check your game resolution')
  238. break
  239. if window_control and self.click(random.choice(random_click), interval=0.7):
  240. click_count += 1
  241. continue
  242. if not window_control and swipe_count >= 5:
  243. logger.info('Swipe to the most matter failed')
  244. logger.info('Please check your game resolution')
  245. raise RequestHumanTakeover
  246. logger.info('Swipe to the most matter')
  247. # 还有一点很重要的,捐赠会有双倍的,需要领两次
  248. reward_number = 0
  249. while 1:
  250. self.screenshot()
  251. if reward_number >= 2:
  252. break
  253. if self.ui_reward_appear_click(False):
  254. reward_number += 1
  255. continue
  256. if self.appear_then_click(self.I_CM_PRESENT, interval=1):
  257. continue
  258. self.ui_reward_appear_click(True)
  259. logger.info('Donate finished')
  260. return True
  261. def _soul(self, index: int):
  262. """
  263. 搞收御魂的任务
  264. :param index:
  265. :return:
  266. """
  267. match_click = {
  268. 0: self.C_CM_1,
  269. 1: self.C_CM_2,
  270. 2: self.C_CM_3,
  271. }
  272. self.ui_click(match_click[index], self.I_SL_SUBMIT)
  273. while 1:
  274. self.screenshot()
  275. number_text = self.O_SL_NUMBER.ocr(self.device.image)
  276. submit_number = int(re.findall(r'\d+', number_text)[-1])
  277. if submit_number > 0:
  278. break
  279. if self.ocr_appear(self.O_SL_LEVEL):
  280. # 如果没有识别到这个,那就说明没有御魂可以提交了,要退出
  281. logger.warning('No soul can be submit')
  282. self.ui_click(self.I_UI_BACK_RED, self.I_CM_RECORDS)
  283. return False
  284. if self.click(self.L_SL_LONG, interval=2.5):
  285. time.sleep(1)
  286. continue
  287. # 领取奖励
  288. logger.info('Start to collect soul rewards')
  289. check_timer = Timer(3)
  290. check_timer.start()
  291. while 1:
  292. self.screenshot()
  293. if self.ui_reward_appear_click(True):
  294. check_timer.reset()
  295. continue
  296. if self.appear_then_click(self.I_SL_SUBMIT, interval=1):
  297. check_timer.reset()
  298. continue
  299. if check_timer.reached():
  300. break
  301. logger.info('Finish to collect soul rewards')
  302. self.wait_until_appear(self.I_CM_RECORDS)
  303. if __name__ == '__main__':
  304. from module.config.config import Config
  305. from module.device.device import Device
  306. c = Config('oas1')
  307. d = Device(c)
  308. t = ScriptTask(c, d)
  309. t.screenshot()
  310. t.run()