test_search.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from copy import deepcopy
  2. from types import SimpleNamespace
  3. import numpy as np
  4. import pytest
  5. from module.base import timer
  6. from module.base.timer import Timer
  7. from tasks.Exploration.base import BaseExploration, Scene
  8. from tasks.Exploration.config import UpType
  9. from tasks.Exploration.solo import SoloExploration
  10. def frame(value=0, scene=Scene.MAIN, popup=None):
  11. return value, scene, popup
  12. class SearchReplay(SoloExploration):
  13. def __init__(self, monkeypatch, frames, up_type=UpType.ALL):
  14. self._config = SimpleNamespace(exploration_config=SimpleNamespace(up_type=up_type))
  15. self.now = 100.0
  16. self.frames = iter(frames)
  17. self.scene = Scene.MAIN
  18. self.popup = None
  19. self.swipes = []
  20. self.removed = []
  21. self.exits = 0
  22. self.swipe_cnt = 0
  23. self.device = SimpleNamespace(
  24. image=np.zeros((720, 1280, 3), dtype=np.uint8),
  25. click_record_remove=self.removed.append,
  26. )
  27. monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: self.now))
  28. monkeypatch.setattr('tasks.Exploration.solo.sleep', lambda _: None)
  29. self.search_fail_timer = Timer(self.SEARCH_SWIPE_INTERVAL)
  30. def screenshot(self):
  31. value, self.scene, self.popup = next(self.frames)
  32. self.device.image = np.full((720, 1280, 3), value, dtype=np.uint8)
  33. def swipe(self, asset):
  34. self.swipes.append(asset.name)
  35. def get_current_scene(self):
  36. return self.scene
  37. def appear(self, asset):
  38. return asset is self.popup
  39. def quit_explore(self):
  40. self.exits += 1
  41. def search(self):
  42. self._search_next_area()
  43. self.now += 2
  44. self._search_next_area()
  45. def test_search_waits_for_no_target_interval(monkeypatch):
  46. task = SearchReplay(monkeypatch, [frame(80)])
  47. task._search_next_area()
  48. task.now += 1.4
  49. task._search_next_area()
  50. assert task.swipes == []
  51. task.now += 0.2
  52. task._search_next_area()
  53. assert len(task.swipes) == task.swipe_cnt == 1
  54. @pytest.mark.parametrize('up_type', [UpType.EXP, UpType.COIN, UpType.DARUMAA])
  55. def test_specific_up_restarts_after_two_search_swipes(monkeypatch, up_type):
  56. task = SearchReplay(monkeypatch, [], up_type)
  57. task.swipe_cnt = 2
  58. task.search()
  59. assert task.exits == 1
  60. assert task.swipe_cnt == 0
  61. assert not task.search_fail_timer.started()
  62. assert task.swipes == []
  63. def test_all_targets_continues_past_two_swipes(monkeypatch):
  64. task = SearchReplay(monkeypatch, [frame(80)])
  65. task.swipe_cnt = 2
  66. task.search()
  67. assert task.exits == 0
  68. assert task.swipe_cnt == 3
  69. assert task.removed == [task.S_SWIPE_BACKGROUND_RIGHT.name]
  70. def test_boundary_requires_a_second_swipe(monkeypatch):
  71. task = SearchReplay(monkeypatch, [frame(), frame(), frame()])
  72. task.search()
  73. assert len(task.swipes) == 2
  74. assert task.exits == 1
  75. @pytest.mark.parametrize('frames,swipes', [([frame(), frame(80)], 1),
  76. ([frame(), frame(), frame(80)], 2)])
  77. def test_delayed_or_initially_ignored_swipe_does_not_exit(monkeypatch, frames, swipes):
  78. task = SearchReplay(monkeypatch, frames)
  79. task.search()
  80. assert len(task.swipes) == swipes
  81. assert task.swipe_cnt == 1
  82. assert task.exits == 0
  83. @pytest.mark.parametrize('retry', [False, True])
  84. @pytest.mark.parametrize('interruption', ['battle', 'confirm', 'small-confirm'])
  85. def test_swipe_interruption_returns_to_scene_handling(monkeypatch, retry, interruption):
  86. task = SearchReplay(monkeypatch, [])
  87. scene = Scene.BATTLE_PREPARE if interruption == 'battle' else Scene.MAIN
  88. popup = {'battle': None, 'confirm': task.I_UI_CONFIRM,
  89. 'small-confirm': task.I_UI_CONFIRM_SAMLL}[interruption]
  90. frames = [frame(), frame(), frame(0, scene, popup)] if retry else [frame(80, scene, popup)]
  91. task.frames = iter(frames)
  92. task.search()
  93. assert task.exits == 0, 'A battle or popup must not be treated as the map boundary'
  94. assert task.swipe_cnt == 0
  95. assert len(task.swipes) == (2 if retry else 1)
  96. def test_swipe_comparison_ignores_floating_effects_but_detects_background_motion():
  97. task = object.__new__(BaseExploration)
  98. before = np.zeros((720, 1280, 3), dtype=np.uint8)
  99. effects = before.copy()
  100. effects[86:311, 1216:1280] = 255
  101. assert task.is_swipe_unchanged(before, effects)
  102. background = before.copy()
  103. background[140:230, 250:1050] = 80
  104. assert not task.is_swipe_unchanged(before, background)
  105. def test_specific_up_without_aligned_sword_does_not_attack_another_monster(monkeypatch):
  106. task = object.__new__(BaseExploration)
  107. task.device = SimpleNamespace(image=None)
  108. task.I_NORMAL_BATTLE_BUTTON = deepcopy(task.I_NORMAL_BATTLE_BUTTON)
  109. monkeypatch.setattr(task.I_NORMAL_BATTLE_BUTTON, 'match_all', lambda **kwargs: [])
  110. monkeypatch.setattr(task, 'appear', lambda asset: asset in (task.I_UP_EXP, task.I_NORMAL_BATTLE_BUTTON))
  111. assert task.search_up_fight(UpType.EXP) is None
  112. def test_all_targets_restores_full_map_search(monkeypatch):
  113. task = object.__new__(BaseExploration)
  114. task.I_NORMAL_BATTLE_BUTTON = deepcopy(task.I_NORMAL_BATTLE_BUTTON)
  115. task.I_NORMAL_BATTLE_BUTTON.roi_back = (100, 100, 80, 80)
  116. def appear(asset):
  117. assert asset.roi_back == (0, 0, 1279, 719)
  118. return True
  119. monkeypatch.setattr(task, 'appear', appear)
  120. assert task.search_up_fight(UpType.ALL) is task.I_NORMAL_BATTLE_BUTTON