test_attack_area.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. """Replay raid selection with real click intervals and the device click guard."""
  2. from collections import Counter, deque
  3. from datetime import datetime
  4. from types import SimpleNamespace
  5. import pytest
  6. from module.base import timer as timer_module
  7. from module.device.device import Device
  8. from module.exception import TaskEnd
  9. from tasks.GameUi.page import page_kekkai_toppa, page_main
  10. from tasks.RealmRaid.assets import RealmRaidAssets
  11. from tasks.RyouToppa import script_task as raid_module
  12. from tasks.RyouToppa.config import RyouToppa
  13. class ReplayClock:
  14. def __init__(self):
  15. self.now = 100.0
  16. def time(self):
  17. return self.now
  18. def sleep(self, seconds):
  19. self.now += seconds
  20. class RaidReplay(raid_module.ScriptTask):
  21. def __init__(self, monkeypatch, *, enter_on=None, entry_delay=0,
  22. open_on=1, close_on=1, battle_result=True):
  23. self.clock = ReplayClock()
  24. monkeypatch.setattr(timer_module, 'time', self.clock)
  25. monkeypatch.setattr(raid_module, 'time', self.clock)
  26. self.config = SimpleNamespace(ryou_toppa=RyouToppa())
  27. self.config.ryou_toppa.raid_config.limit_count = 1
  28. self.interval_timer = {}
  29. self.current_count = 0
  30. self.start_time = datetime.now()
  31. self.frames = 0
  32. self.selected = None
  33. self.popup_visible = False
  34. self.in_battle = False
  35. self.enter_at = None
  36. self.enter_on = enter_on or {}
  37. self.entry_delay = entry_delay
  38. self.open_on = open_on
  39. self.close_on = close_on
  40. self.battle_result = battle_result
  41. self.selections = Counter()
  42. self.challenges = Counter()
  43. self.safe_clicks = []
  44. self.last_challenge_at = None
  45. self.battles = []
  46. self.destinations = []
  47. self.schedules = []
  48. self.device = SimpleNamespace(
  49. click=self.device_click,
  50. click_record=deque(maxlen=15),
  51. click_record_clear=lambda: self.device.click_record.clear(),
  52. stuck_record_add=lambda name: None,
  53. )
  54. def screenshot(self):
  55. self.frames += 1
  56. assert self.frames < 500, 'Raid selection did not terminate'
  57. self.clock.sleep(0.25)
  58. if self.enter_at is not None and self.clock.time() >= self.enter_at:
  59. self.in_battle = True
  60. self.popup_visible = False
  61. self.enter_at = None
  62. def appear(self, asset, **kwargs):
  63. if asset is self.I_TOPPA_RECORD or asset is self.I_RYOU_REWARD:
  64. return not self.in_battle
  65. if asset is RealmRaidAssets.I_FIRE:
  66. return self.popup_visible
  67. return False
  68. def device_click(self, x, y, control_name):
  69. Device.click_record_add(self.device, control_name)
  70. Device.click_record_check(self.device)
  71. if control_name == RealmRaidAssets.I_FIRE.name:
  72. assert self.popup_visible
  73. self.challenges[self.selected] += 1
  74. self.last_challenge_at = self.clock.time()
  75. if self.challenges[self.selected] == self.enter_on.get(self.selected):
  76. self.enter_at = self.clock.time() + self.entry_delay
  77. elif control_name == self.C_SAFE_AREA.name:
  78. self.safe_clicks.append(self.clock.time())
  79. if len(self.safe_clicks) >= self.close_on:
  80. self.popup_visible = False
  81. else:
  82. for index, area in enumerate(raid_module.area_map):
  83. if control_name == area['rule_click'].name:
  84. assert not self.popup_visible, 'Clicked a realm through the previous popup'
  85. self.selected = index
  86. self.selections[index] += 1
  87. self.popup_visible = self.selections[index] >= self.open_on
  88. break
  89. else:
  90. raise AssertionError(f'Unexpected click: {control_name}')
  91. def run_general_battle(self, config):
  92. assert self.in_battle
  93. self.battles.append(self.selected)
  94. self.current_count += 1
  95. self.in_battle = False
  96. return self.battle_result
  97. def goto_page(self, page):
  98. self.destinations.append(page)
  99. def ui_click(self, *args, **kwargs):
  100. return True
  101. def has_ticket(self):
  102. return True
  103. def set_next_run(self, **kwargs):
  104. self.schedules.append(kwargs)
  105. @pytest.mark.parametrize('close_on', [1, 2])
  106. def test_five_failed_challenges_close_popup_before_skipping(monkeypatch, close_on):
  107. task = RaidReplay(monkeypatch, close_on=close_on)
  108. assert task.attack_area(0) is False
  109. assert task.challenges == {0: 5}
  110. assert task.selections == {0: 1}
  111. assert len(task.safe_clicks) == close_on
  112. assert task.safe_clicks[0] - task.last_challenge_at >= 2
  113. assert task.popup_visible is False
  114. assert task.battles == []
  115. assert not task.device.click_record
  116. @pytest.mark.parametrize('entry_delay', [0, 1.5])
  117. @pytest.mark.parametrize('battle_result', [False, True])
  118. def test_last_challenge_can_still_enter_battle(monkeypatch, entry_delay, battle_result):
  119. task = RaidReplay(monkeypatch, enter_on={0: 5}, entry_delay=entry_delay,
  120. battle_result=battle_result)
  121. assert task.attack_area(0) is battle_result
  122. assert task.challenges == {0: 5}
  123. assert task.battles == [0]
  124. assert task.safe_clicks == []
  125. def test_repeated_area_clicks_without_a_challenge_button_are_bounded(monkeypatch):
  126. task = RaidReplay(monkeypatch, open_on=100)
  127. assert task.attack_area(0) is False
  128. assert task.selections == {0: 5}
  129. assert not task.challenges
  130. assert task.battles == []
  131. def test_last_selection_can_still_open_a_challenge(monkeypatch):
  132. task = RaidReplay(monkeypatch, open_on=5, enter_on={0: 1})
  133. assert task.attack_area(0) is True
  134. assert task.selections == {0: 5}
  135. assert task.challenges == {0: 1}
  136. assert task.battles == [0]
  137. def test_skipped_realms_do_not_exhaust_the_next_realms_retries_or_click_guard(monkeypatch):
  138. task = RaidReplay(monkeypatch, enter_on={2: 1})
  139. assert task.attack_area(0) is False
  140. assert task.attack_area(1) is False
  141. assert task.attack_area(2) is True
  142. assert task.challenges == {0: 5, 1: 5, 2: 1}
  143. assert task.selections == {0: 1, 1: 1, 2: 1}
  144. assert task.battles == [2]
  145. def test_run_moves_to_the_next_realm_after_challenge_retries(monkeypatch):
  146. task = RaidReplay(monkeypatch, enter_on={1: 1})
  147. with pytest.raises(TaskEnd):
  148. task.run()
  149. assert task.challenges == {0: 5, 1: 1}
  150. assert task.battles == [1]
  151. assert task.current_count == 1
  152. assert task.destinations == [page_kekkai_toppa, page_main]
  153. assert task.schedules == [dict(task='RyouToppa', finish=True, server=True, success=True)]