script_task.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  6. from tasks.RyouToppa.assets import RyouToppaAssets
  7. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  8. from tasks.GameUi.game_ui import GameUi
  9. from tasks.GameUi.page import page_realm_raid, page_main, page_kekkai_toppa, page_shikigami_records
  10. from tasks.RealmRaid.assets import RealmRaidAssets
  11. from module.logger import logger
  12. from module.exception import TaskEnd
  13. from module.atom.image_grid import ImageGrid
  14. from module.base.timer import Timer
  15. from module.exception import GamePageUnknownError
  16. from tasks.RyouToppa.config import RaidMode, AttackNumber, HaveManageAccess, RaidConfig, RyouToppa
  17. class ScriptTask(GeneralBattle, GameUi, SwitchSoul, RyouToppaAssets):
  18. medal_grid: ImageGrid = None
  19. def run(self):
  20. """
  21. 执行
  22. :return:
  23. """
  24. ryou_config = self.config.ryou_toppa
  25. self.medal_grid = ImageGrid([RealmRaidAssets.I_MEDAL_5, RealmRaidAssets.I_MEDAL_4, RealmRaidAssets.I_MEDAL_3,
  26. RealmRaidAssets.I_MEDAL_2, RealmRaidAssets.I_MEDAL_1, RealmRaidAssets.I_MEDAL_0])
  27. if ryou_config.switch_soul_config.enable:
  28. self.ui_get_current_page()
  29. self.ui_goto(page_shikigami_records)
  30. self.run_switch_soul(ryou_config.switch_soul_config.switch_group_team)
  31. if ryou_config.switch_soul_config.enable_switch_by_name:
  32. self.ui_get_current_page()
  33. self.ui_goto(page_shikigami_records)
  34. self.run_switch_soul_by_name(ryou_config.switch_soul_config.group_name, ryou_config.switch_soul_config.team_name)
  35. self.ui_get_current_page()
  36. self.ui_goto(page_kekkai_toppa)
  37. ryou_toppa_start_flag = True
  38. ryou_toppa_success_penetration = False
  39. # 点击突破
  40. while 1:
  41. self.screenshot()
  42. if self.appear_then_click(RealmRaidAssets.I_REALM_RAID, interval=1):
  43. logger.info(f'Click {RealmRaidAssets.I_REALM_RAID.name}')
  44. continue
  45. if self.appear(self.I_REAL_RAID_REFRESH, threshold=0.8):
  46. if self.appear_then_click(self.I_RYOU_TOPPA, interval=1):
  47. logger.info(f'Click {self.I_RYOU_TOPPA.name}')
  48. continue
  49. # 攻破阴阳寮,说明寮突已开,则退出
  50. elif self.appear(self.I_SUCCESS_PENETRATION, threshold=0.8):
  51. ryou_toppa_start_flag = True
  52. ryou_toppa_success_penetration = True
  53. break
  54. # 出现选择寮突说明寮突未开
  55. elif self.appear(self.I_SELECT_RYOU_BUTTON, threshold=0.8):
  56. ryou_toppa_start_flag = False
  57. break
  58. # 出现寮奖励, 说明寮突已开
  59. elif self.appear(self.I_RYOU_REWARD, threshold=0.8) or self.appear(self.I_RYOU_REWARD_90, threshold=0.8):
  60. ryou_toppa_start_flag = True
  61. break
  62. # 寮突未开 并且有权限, 开开寮突
  63. if not ryou_toppa_start_flag and ryou_config.raid_config.ryou_access == 'yes':
  64. # 开寮突
  65. self.start_ryou_toppa()
  66. # 100% 攻破, 第二天再执行
  67. if ryou_toppa_success_penetration:
  68. self.set_next_run(task='RyouToppa', finish=True, success=True)
  69. raise TaskEnd
  70. # 开始突破
  71. while 1:
  72. if self.execute_round():
  73. continue
  74. else:
  75. break
  76. # 回 page_main 失败
  77. # self.ui_current = page_ryou_toppa
  78. # self.ui_goto(page_main)
  79. self.set_next_run(task='RyouToppa', finish=True, server=False, success=True)
  80. raise TaskEnd
  81. def start_ryou_toppa(self):
  82. """
  83. 开启寮突破
  84. :return:
  85. """
  86. # 点击寮突
  87. while 1:
  88. self.screenshot()
  89. if self.appear_then_click(self.I_SELECT_RYOU_BUTTON, interval=1):
  90. break
  91. logger.info(f'Click {self.I_SELECT_RYOU_BUTTON.name}')
  92. # 选择第一个寮
  93. while 1:
  94. self.screenshot()
  95. if self.appear_then_click(self.I_GUILD_ORDERS_REWARDS, action=self.C_SELECT_FIRST_RYOU, interval=1):
  96. break
  97. logger.info(f'Click {self.C_SELECT_FIRST_RYOU.name}')
  98. # 点击开始突入
  99. while 1:
  100. self.screenshot()
  101. if self.appear_then_click(self.I_START_TOPPA_BUTTON, interval=1):
  102. continue
  103. # 出现寮奖励, 说明寮突已开
  104. if self.appear(self.I_RYOU_REWARD, threshold=0.8):
  105. break
  106. logger.info(f'Click {self.I_START_TOPPA_BUTTON.name}')
  107. def is_ticket(self) -> bool:
  108. """
  109. 如果没有票了,那么就返回False
  110. :return:
  111. """
  112. self.wait_until_appear(RealmRaidAssets.I_BACK_RED)
  113. self.screenshot()
  114. cu, res, total = self.O_NUMBER.ocr(self.device.image)
  115. logger.info(f"cu = {cu}, res = {res}, total = {total}")
  116. if cu == 0 and cu + res == total:
  117. logger.warning(f'Execute round failed, no chance to attack')
  118. return False
  119. return True
  120. def medal_fire(self):
  121. """
  122. 点击勋章
  123. :return:
  124. """
  125. # 点击勋章的挑战 和挑战
  126. time.sleep(0.2)
  127. is_click = False
  128. while 1:
  129. self.screenshot()
  130. if self.appear(RealmRaidAssets.I_FIRE, threshold=0.8):
  131. break
  132. if self.appear_then_click(RealmRaidAssets.I_SOUL_RAID, interval=1.5):
  133. while 1:
  134. self.screenshot()
  135. if self.appear_then_click(RealmRaidAssets.I_SOUL_RAID, interval=1.5):
  136. continue
  137. if not self.appear(RealmRaidAssets.I_SOUL_RAID, threshold=0.6):
  138. break
  139. continue
  140. target = self.medal_grid.find_anyone(self.device.image)
  141. if target:
  142. self.appear_then_click(target, interval=2) # 点击勋章,但是设置为两秒的间隔,适应不同的模拟器速度
  143. is_click = not is_click
  144. if is_click:
  145. continue
  146. logger.info(f'Click Medal')
  147. # 点击挑战
  148. self.wait_until_appear(RealmRaidAssets.I_FIRE)
  149. while 1:
  150. self.screenshot()
  151. if self.appear_then_click(RealmRaidAssets.I_FIRE, interval=2):
  152. continue
  153. if not self.appear(RealmRaidAssets.I_FIRE, threshold=0.8):
  154. break
  155. logger.info(f'Click {RealmRaidAssets.I_FIRE.name}')
  156. def execute_round(self) -> bool:
  157. """
  158. 执行一轮 除非票不够,一直到到九次
  159. :return:
  160. """
  161. ryou_config = self.config.ryou_toppa
  162. # 如果没有票了,就退出
  163. if not self.is_ticket():
  164. return False
  165. if not self.is_target_available():
  166. return False
  167. # 打20次
  168. for i in range(30):
  169. if not self.is_ticket():
  170. return False
  171. if not self.is_target_available():
  172. return False
  173. self.medal_fire()
  174. self.run_general_battle(ryou_config.general_battle_config)
  175. self.wait_until_appear(RealmRaidAssets.I_BACK_RED)
  176. return True
  177. # 判断是否还有可进攻的目标
  178. def is_target_available(self) -> bool:
  179. """
  180. 如果没有可进攻目标了,那么就返回False
  181. :return:
  182. """
  183. self.wait_until_appear(RealmRaidAssets.I_BACK_RED)
  184. self.screenshot()
  185. # 有徽章被检测到
  186. if self.medal_grid.find_anyone(self.device.image):
  187. return True
  188. # 没有徽章被检测到,且目标寮已被攻破
  189. elif self.appear(self.I_SUCCESS_PENETRATION, threshold=0.8):
  190. logger.info(f'Execute round failed, no target')
  191. return False
  192. else:
  193. raise GamePageUnknownError
  194. if __name__ == "__main__":
  195. from module.config.config import Config
  196. from module.device.device import Device
  197. config = Config('oas1')
  198. device = Device(config)
  199. t = ScriptTask(config, device)
  200. t.run()