test_treasure_reward.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """Replay the reported in-scene chest, which is distinct from the reward title."""
  2. from copy import deepcopy
  3. from pathlib import Path
  4. from types import SimpleNamespace
  5. import cv2
  6. import numpy as np
  7. import pytest
  8. from tasks.Exploration.assets import ExplorationAssets
  9. from tasks.GlobalGame.assets import GlobalGameAssets
  10. from tasks.GlobalGame.global_game import GlobalGame
  11. from tasks.Exploration.base import Scene
  12. from tasks.Exploration.solo import SoloExploration
  13. @pytest.fixture
  14. def chest_crop():
  15. # User screenshot from 2026-10-10, resized from 1404x790 to 1280x720,
  16. # then cropped to (500, 340, 260, 200). Keep this independent of the template.
  17. path = Path(__file__).with_name('fixtures') / 'scene_chest.png'
  18. return cv2.cvtColor(cv2.imdecode(np.fromfile(path, dtype=np.uint8),
  19. cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
  20. def scene_with_chest(crop, x=500, y=340):
  21. image = np.zeros((720, 1280, 3), dtype=np.uint8)
  22. height, width = crop.shape[:2]
  23. image[y:y + height, x:x + width] = crop
  24. return image
  25. def reward_popup(variant='local'):
  26. image = np.zeros((720, 1280, 3), dtype=np.uint8)
  27. asset = (GlobalGameAssets.I_UI_REWARD if variant == 'local'
  28. else GlobalGameAssets.I_UI_REWARD_ALT)
  29. title = asset.image
  30. x, y = asset.roi_front[:2]
  31. height, width = title.shape[:2]
  32. image[y:y + height, x:x + width] = title
  33. return image
  34. @pytest.mark.parametrize('position', [(100, 180), (500, 340), (900, 400)])
  35. def test_scene_chest_is_found_at_different_map_positions(chest_crop, position):
  36. rule = deepcopy(ExplorationAssets.I_BATTLE_REWARD)
  37. x, y = position
  38. assert rule.match(scene_with_chest(chest_crop, x, y))
  39. # The clasp is at (81, 69) within the crop; a click must land inside it.
  40. click_x, click_y = rule.coord()
  41. assert x + 81 <= click_x < x + 112
  42. assert y + 69 <= click_y < y + 90
  43. @pytest.mark.parametrize('variant', ['local', 'upstream'])
  44. def test_reward_popup_is_not_an_unopened_chest(variant):
  45. image = reward_popup(variant)
  46. assert deepcopy(GlobalGame.I_UI_REWARD).match(image)
  47. assert not deepcopy(ExplorationAssets.I_BATTLE_REWARD).match(image)
  48. class RewardCollected(Exception):
  49. """Stop the exploration loop once the replay has collected its reward."""
  50. class ExplorationRewardReplay(SoloExploration):
  51. def __init__(self, chest_crop, variant='local'):
  52. self.explore_init = True
  53. self.interval_timer = {}
  54. self.screen = 'chest'
  55. self.frames = 0
  56. self.clicks = []
  57. self.images = {
  58. 'chest': scene_with_chest(chest_crop),
  59. 'reward': reward_popup(variant),
  60. 'done': np.zeros((720, 1280, 3), dtype=np.uint8),
  61. }
  62. self.device = SimpleNamespace(image=self.images[self.screen], click=self.record_click)
  63. self.I_BATTLE_REWARD = deepcopy(self.I_BATTLE_REWARD)
  64. self.I_UI_REWARD = deepcopy(self.I_UI_REWARD)
  65. def screenshot(self):
  66. self.frames += 1
  67. assert self.frames < 15, 'Reward collection did not finish'
  68. self.device.image = self.images[self.screen]
  69. def get_current_scene(self, reuse_screenshot=True):
  70. if self.screen == 'done':
  71. raise RewardCollected
  72. return Scene.MAIN
  73. def record_click(self, x, y, control_name):
  74. self.clicks.append(control_name)
  75. if self.screen == 'chest':
  76. assert control_name == self.I_BATTLE_REWARD.name
  77. assert 581 <= x < 612 and 409 <= y < 430
  78. self.screen = 'reward'
  79. else:
  80. assert self.screen == 'reward'
  81. assert control_name == self.I_UI_REWARD.name
  82. self.screen = 'done'
  83. def search_up_fight(self):
  84. pytest.fail('Started searching for enemies while a chest was visible')
  85. def _check_mate_leave(self, friend_leave_timer):
  86. pytest.fail('Checked for a missing teammate before collecting the chest')
  87. @pytest.mark.parametrize('mode', ['run_solo', 'run_leader', 'run_member'])
  88. @pytest.mark.parametrize('variant', ['local', 'upstream'])
  89. def test_all_exploration_modes_open_the_chest_and_collect_rewards(chest_crop, monkeypatch, mode, variant):
  90. monkeypatch.setattr('tasks.base_task.sleep', lambda _: None)
  91. task = ExplorationRewardReplay(chest_crop, variant)
  92. with pytest.raises(RewardCollected):
  93. getattr(task, mode)()
  94. assert task.clicks == [task.I_BATTLE_REWARD.name, task.I_UI_REWARD.name]