script_task.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # This Python file uses the following encoding: utf-8
  2. # @author AzurTian
  3. from time import sleep
  4. from datetime import datetime
  5. from module.base.timer import Timer
  6. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  7. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  8. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  9. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  10. from tasks.GameUi.game_ui import GameUi
  11. from module.logger import logger
  12. from module.exception import TaskEnd
  13. from tasks.GameUi.page import (
  14. page_main, page_battle, page_battle_prepare, page_reward, page_shikigami_records,
  15. )
  16. from tasks.OtherWorldTwilight.assets import OtherWorldTwilightAssets
  17. from tasks.OtherWorldTwilight.page import page_owt
  18. from tasks.OtherWorldTwilight.config import OtherWorldTwilight
  19. from tasks.Orochi.config import UserStatus
  20. class ScriptTask(GeneralBattle, GeneralInvite, GeneralRoom, GameUi, SwitchSoul, OtherWorldTwilightAssets):
  21. conf: OtherWorldTwilight = None
  22. def run(self) -> bool:
  23. self.conf = self.config.other_world_twilight
  24. if self.conf.switch_soul.enable:
  25. self.goto_page(page_shikigami_records)
  26. self.run_switch_soul(self.conf.switch_soul.switch_group_team)
  27. if self.conf.switch_soul.enable_switch_by_name:
  28. self.goto_page(page_shikigami_records)
  29. self.run_switch_soul_by_name(self.conf.switch_soul.group_name, self.conf.switch_soul.team_name)
  30. success = True
  31. match self.conf.other_world_twilight_config.user_status:
  32. case UserStatus.LEADER: success = self.run_leader()
  33. case UserStatus.MEMBER: success = self.run_member()
  34. case UserStatus.ALONE: self.run_alone()
  35. case _: logger.error('Unknown user status')
  36. self.goto_page(page_main)
  37. if success:
  38. self.set_next_run('OtherWorldTwilight', finish=True, success=True)
  39. else:
  40. self.set_next_run('OtherWorldTwilight', finish=True, success=False)
  41. raise TaskEnd('OtherWorldTwilight')
  42. def run_leader(self):
  43. logger.info('Start run leader')
  44. self.goto_page(page_owt)
  45. self.check_lock(self.conf.general_battle_config.lock_team_enable, self.I_OWT_LOCK, self.I_OWT_UNLOCK)
  46. # 创建队伍
  47. logger.info('Create team')
  48. self.ui_click(self.I_OWT_TEAM, self.I_CHECK_TEAM, interval=1)
  49. # 创建房间
  50. self.create_room()
  51. self.ensure_private()
  52. self.create_ensure()
  53. # 邀请队友
  54. success = True
  55. is_first = True
  56. # 这个时候我已经进入房间了哦
  57. while 1:
  58. self.screenshot()
  59. # 无论胜利与否, 都会出现是否邀请一次队友
  60. # 区别在于,失败的话不会出现那个勾选默认邀请的框
  61. if self.check_and_invite(self.conf.invite_config.default_invite):
  62. continue
  63. if self.current_count >= self.conf.other_world_twilight_config.limit_count:
  64. if self.is_in_room():
  65. logger.info('Count limit out')
  66. break
  67. if datetime.now() - self.start_time >= self.conf.other_world_twilight_config.limit_time_v:
  68. if self.is_in_room():
  69. logger.info('Time limit out')
  70. break
  71. # 如果没有进入房间那就不需要后面的邀请
  72. if not self.is_in_room(False):
  73. if self.is_room_dead():
  74. logger.warning('Task failed')
  75. success = False
  76. break
  77. continue
  78. # 点击挑战
  79. if not is_first:
  80. if self.run_invite(config=self.conf.invite_config):
  81. self.run_general_battle(config=self.conf.general_battle_config)
  82. else:
  83. # 邀请失败,退出任务
  84. logger.warning('Invite failed and exit this task')
  85. success = False
  86. break
  87. # 第一次会邀请队友
  88. if is_first:
  89. if not self.run_invite(config=self.conf.invite_config, is_first=True):
  90. logger.warning('Invite failed and exit this task')
  91. success = False
  92. break
  93. else:
  94. is_first = False
  95. self.run_general_battle(config=self.conf.general_battle_config)
  96. # 当结束或者是失败退出循环的时候只有两个UI的可能,在房间或者是在组队界面
  97. # 两者都是幂等的,不在对应界面时直接返回
  98. self.exit_room()
  99. self.exit_team()
  100. return success
  101. def run_member(self):
  102. logger.info('Start run member')
  103. self.goto_page(page_main)
  104. # 进入战斗流程
  105. self.device.stuck_record_add('BATTLE_STATUS_S')
  106. while 1:
  107. self.screenshot()
  108. if self.current_count >= self.conf.other_world_twilight_config.limit_count:
  109. logger.info('Count limit out')
  110. break
  111. if datetime.now() - self.start_time >= self.conf.other_world_twilight_config.limit_time_v:
  112. logger.info('Time limit out')
  113. break
  114. if self.check_then_accept():
  115. continue
  116. if self.is_in_room(False):
  117. self.device.stuck_record_clear()
  118. if self.wait_battle(wait_time=self.conf.invite_config.wait_time):
  119. self.run_general_battle(config=self.conf.general_battle_config)
  120. else:
  121. break
  122. # 队长秒开的时候,检测是否加入到战斗中
  123. if self.is_in_battle(False):
  124. self.run_general_battle(config=self.conf.general_battle_config)
  125. # 有一种情况是本来要退出的,但是队长邀请了进入的战斗的加载界面
  126. while 1:
  127. if self.appear(self.I_CHECK_MAIN) or self.appear(self.I_OWT_FIRE):
  128. break
  129. # 可能在房间就退出,可能还在战斗中就退出战斗,两者都是幂等的
  130. self.exit_room()
  131. self.exit_battle()
  132. return True
  133. def run_alone(self):
  134. logger.info('Start run alone')
  135. self.goto_page(page_owt)
  136. self.check_lock(self.conf.general_battle_config.lock_team_enable, self.I_OWT_LOCK, self.I_OWT_UNLOCK)
  137. unknown_page_timer = Timer(10)
  138. while 1:
  139. if self.current_count >= self.conf.other_world_twilight_config.limit_count:
  140. logger.info('Count limit out')
  141. break
  142. if datetime.now() - self.start_time >= self.conf.other_world_twilight_config.limit_time_v:
  143. logger.info('Time limit out')
  144. break
  145. self.screenshot()
  146. current_page = self.get_current_page(False)
  147. # session 返回的 Page 是 clone,与模块级 page_owt 不是同一对象,
  148. # 必须用 == (Page.__eq__ 比 key),is 恒为 False
  149. if current_page is None:
  150. sleep(0.5)
  151. elif current_page == page_owt:
  152. unknown_page_timer.clear()
  153. self.appear_then_click(self.I_OWT_FIRE, interval=1.2)
  154. elif current_page in (page_battle_prepare, page_battle, page_reward):
  155. unknown_page_timer.clear()
  156. self.run_general_battle(self.conf.general_battle_config)
  157. else:
  158. # 未知页面, 可能是从别处进来的, 超时后拉回彼世逢魔页面
  159. if not unknown_page_timer.started():
  160. unknown_page_timer.start()
  161. elif unknown_page_timer.reached():
  162. logger.warning(f'Unknown page: {current_page}, goto page_owt')
  163. self.goto_page(page_owt)
  164. unknown_page_timer.clear()
  165. def is_room_dead(self) -> bool:
  166. # 如果在探索界面或者是出现在组队界面,那就是可能房间死了
  167. sleep(0.5)
  168. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  169. sleep(0.5)
  170. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  171. return True
  172. return False
  173. if __name__ == '__main__':
  174. from module.config.config import Config
  175. from module.device.device import Device
  176. c = Config('oas1')
  177. d = Device(c)
  178. t = ScriptTask(c, d)
  179. t.run()