script_task.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if not self.create_room():
  51. logger.warning('Create room failed')
  52. return False
  53. self.ensure_private()
  54. if not self.create_ensure():
  55. logger.warning('Create ensure failed')
  56. return False
  57. # 邀请队友
  58. success = True
  59. is_first = True
  60. # 这个时候我已经进入房间了哦
  61. while 1:
  62. self.screenshot()
  63. # 无论胜利与否, 都会出现是否邀请一次队友
  64. # 区别在于,失败的话不会出现那个勾选默认邀请的框
  65. if self.check_and_invite(self.conf.invite_config.default_invite):
  66. continue
  67. if self.current_count >= self.conf.other_world_twilight_config.limit_count:
  68. if self.is_in_room():
  69. logger.info('Count limit out')
  70. break
  71. if datetime.now() - self.start_time >= self.conf.other_world_twilight_config.limit_time_v:
  72. if self.is_in_room():
  73. logger.info('Time limit out')
  74. break
  75. # 如果没有进入房间那就不需要后面的邀请
  76. if not self.is_in_room(False):
  77. if self.is_room_dead():
  78. logger.warning('Task failed')
  79. success = False
  80. break
  81. continue
  82. # 点击挑战
  83. if not is_first:
  84. if self.run_invite(config=self.conf.invite_config):
  85. self.run_general_battle(config=self.conf.general_battle_config)
  86. else:
  87. # 邀请失败,退出任务
  88. logger.warning('Invite failed and exit this task')
  89. success = False
  90. break
  91. # 第一次会邀请队友
  92. if is_first:
  93. if not self.run_invite(config=self.conf.invite_config, is_first=True):
  94. logger.warning('Invite failed and exit this task')
  95. success = False
  96. break
  97. else:
  98. is_first = False
  99. self.run_general_battle(config=self.conf.general_battle_config)
  100. # 当结束或者是失败退出循环的时候只有两个UI的可能,在房间或者是在组队界面
  101. # 两者都是幂等的,不在对应界面时直接返回
  102. self.exit_room()
  103. self.exit_team()
  104. return success
  105. def run_member(self):
  106. logger.info('Start run member')
  107. self.goto_page(page_main)
  108. # 进入战斗流程
  109. self.device.stuck_record_add('BATTLE_STATUS_S')
  110. while 1:
  111. self.screenshot()
  112. if self.current_count >= self.conf.other_world_twilight_config.limit_count:
  113. logger.info('Count limit out')
  114. break
  115. if datetime.now() - self.start_time >= self.conf.other_world_twilight_config.limit_time_v:
  116. logger.info('Time limit out')
  117. break
  118. if self.check_then_accept():
  119. continue
  120. if self.is_in_room(False):
  121. self.device.stuck_record_clear()
  122. if self.wait_battle(wait_time=self.conf.invite_config.wait_time):
  123. self.run_general_battle(config=self.conf.general_battle_config)
  124. else:
  125. break
  126. # 队长秒开的时候,检测是否加入到战斗中
  127. if self.is_in_battle(False):
  128. self.run_general_battle(config=self.conf.general_battle_config)
  129. # 有一种情况是本来要退出的,但是队长邀请了进入的战斗的加载界面
  130. while 1:
  131. if self.appear(self.I_CHECK_MAIN) or self.appear(self.I_OWT_FIRE):
  132. break
  133. # 可能在房间就退出,可能还在战斗中就退出战斗,两者都是幂等的
  134. self.exit_room()
  135. self.exit_battle()
  136. return True
  137. def run_alone(self):
  138. logger.info('Start run alone')
  139. self.goto_page(page_owt)
  140. self.check_lock(self.conf.general_battle_config.lock_team_enable, self.I_OWT_LOCK, self.I_OWT_UNLOCK)
  141. unknown_page_timer = Timer(10)
  142. while 1:
  143. if self.current_count >= self.conf.other_world_twilight_config.limit_count:
  144. logger.info('Count limit out')
  145. break
  146. if datetime.now() - self.start_time >= self.conf.other_world_twilight_config.limit_time_v:
  147. logger.info('Time limit out')
  148. break
  149. self.screenshot()
  150. current_page = self.get_current_page(False)
  151. # session 返回的 Page 是 clone,与模块级 page_owt 不是同一对象,
  152. # 必须用 == (Page.__eq__ 比 key),is 恒为 False
  153. if current_page is None:
  154. sleep(0.5)
  155. elif current_page == page_owt:
  156. unknown_page_timer.clear()
  157. self.appear_then_click(self.I_OWT_FIRE, interval=1.2)
  158. elif current_page in (page_battle_prepare, page_battle, page_reward):
  159. unknown_page_timer.clear()
  160. self.run_general_battle(self.conf.general_battle_config)
  161. else:
  162. # 未知页面, 可能是从别处进来的, 超时后拉回彼世逢魔页面
  163. if not unknown_page_timer.started():
  164. unknown_page_timer.start()
  165. elif unknown_page_timer.reached():
  166. logger.warning(f'Unknown page: {current_page}, goto page_owt')
  167. self.goto_page(page_owt)
  168. unknown_page_timer.clear()
  169. def is_room_dead(self) -> bool:
  170. # 如果在探索界面或者是出现在组队界面,那就是可能房间死了
  171. sleep(0.5)
  172. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  173. sleep(0.5)
  174. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  175. return True
  176. return False
  177. if __name__ == '__main__':
  178. from module.config.config import Config
  179. from module.device.device import Device
  180. c = Config('oas1')
  181. d = Device(c)
  182. t = ScriptTask(c, d)
  183. t.run()