test_costume.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from copy import deepcopy
  2. from types import SimpleNamespace
  3. import numpy as np
  4. import pytest
  5. import cv2
  6. from module.atom.image import RuleImage
  7. from tasks.Component.Costume import costume_base
  8. from tasks.Component.Costume.assets import CostumeAssets
  9. from tasks.Component.Costume.config import MainType, BattleSceneType
  10. from tasks.Component.Costume.config import ShikigamiType
  11. from tasks.Component.CostumeShikigami.assets import CostumeShikigamiAssets
  12. from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
  13. def screenshot_for(asset):
  14. image = asset.image
  15. x, y = asset.roi_front[:2]
  16. height, width = image.shape[:2]
  17. screenshot = np.zeros((720, 1280, 3), dtype=np.uint8)
  18. screenshot[y:y + height, x:x + width] = image
  19. return screenshot
  20. def test_shikigami_13_switch_updates_all_cached_rules():
  21. host = costume_base.CostumeBase()
  22. before = costume_base.shikigami_costume_model[ShikigamiType.COSTUME_SHIKIGAMI_12]
  23. after = costume_base.shikigami_costume_model[ShikigamiType.COSTUME_SHIKIGAMI_13]
  24. references = {}
  25. for key, name in before.items():
  26. rule = deepcopy(getattr(CostumeShikigamiAssets, name))
  27. setattr(host, key, rule)
  28. references[key] = rule
  29. assert rule.match(screenshot_for(rule))
  30. host.check_costume_shikigami(ShikigamiType.COSTUME_SHIKIGAMI_13)
  31. assert len(after) == 19
  32. for key, name in after.items():
  33. rule = getattr(host, key)
  34. target = getattr(CostumeShikigamiAssets, name)
  35. assert rule is references[key]
  36. assert rule.file == target.file
  37. assert rule.match(screenshot_for(target))
  38. @pytest.mark.parametrize('animated_type', [MainType.COSTUME_MAIN_13, MainType.COSTUME_MAIN_17])
  39. def test_switch_animated_to_static_and_back_preserves_shared_reference(monkeypatch, animated_type):
  40. # Match real costume templates without mutating the process-wide asset objects.
  41. assets = SimpleNamespace(**{
  42. name: deepcopy(value)
  43. for name, value in vars(CostumeAssets).items()
  44. if isinstance(value, RuleImage)
  45. })
  46. monkeypatch.setattr(costume_base, 'CostumeAssets', lambda: assets)
  47. host = costume_base.CostumeBase()
  48. host.I_CHECK_MAIN = deepcopy(assets.I_CHECK_MAIN_2)
  49. page_reference = host.I_CHECK_MAIN
  50. # Prime the static image cache before changing to a GIF costume.
  51. assert page_reference.match(screenshot_for(assets.I_CHECK_MAIN_2))
  52. host.check_costume_main(animated_type)
  53. frame_names = costume_base.main_costume_model[animated_type]['I_CHECK_MAIN']
  54. animated_screen = screenshot_for(getattr(assets, frame_names[-1]))
  55. assert page_reference.match(animated_screen)
  56. host.check_costume_main(MainType.COSTUME_MAIN_1)
  57. assert host.I_CHECK_MAIN is page_reference
  58. assert page_reference.match(screenshot_for(assets.I_CHECK_MAIN_1))
  59. assert not page_reference.match(animated_screen)
  60. assert not hasattr(page_reference, 'targets')
  61. assert page_reference.name == assets.I_CHECK_MAIN_1.name
  62. # A second static costume must not reuse the first static template's cache.
  63. host.check_costume_main(MainType.COSTUME_MAIN_2)
  64. assert page_reference.match(screenshot_for(assets.I_CHECK_MAIN_2))
  65. host.check_costume_main(animated_type)
  66. assert host.I_CHECK_MAIN is page_reference
  67. assert page_reference.match(animated_screen)
  68. assert page_reference.name == getattr(assets, frame_names[0]).name
  69. def make_rule(tmp_path, name, size, method, masked=False):
  70. pixels = np.random.default_rng(size).integers(0, 256, (size, size, 3), dtype=np.uint8)
  71. path = tmp_path / f'{name}.png'
  72. path.write_bytes(cv2.imencode('.png', pixels)[1].tobytes())
  73. if masked:
  74. mask = np.zeros((size, size), dtype=np.uint8)
  75. mask[:, :size // 2] = 255
  76. path.with_name(f'{name}_mask.png').write_bytes(cv2.imencode('.png', mask)[1].tobytes())
  77. return RuleImage((10, 10, size, size), (0, 0, 80, 80), method, 0.99, str(path))
  78. def masked_screenshot(asset):
  79. screen = np.zeros((80, 80, 3), dtype=np.uint8)
  80. image = asset.image.copy()
  81. image[:, image.shape[1] // 2:] = 0
  82. screen[10:10 + image.shape[0], 10:10 + image.shape[1]] = image
  83. return screen
  84. def test_replace_img_invalidates_plain_to_masked_dispatch(tmp_path):
  85. host = costume_base.CostumeBase()
  86. host.I_CHECK_MAIN = make_rule(tmp_path, 'plain', 20, 'Template matching')
  87. masked = make_rule(tmp_path, 'masked', 20, 'Masked template matching', masked=True)
  88. screen = masked_screenshot(masked)
  89. assert not host.I_CHECK_MAIN.match(screen)
  90. host.replace_img('I_CHECK_MAIN', masked)
  91. assert host.I_CHECK_MAIN.match(screen)
  92. def test_replace_img_loads_the_new_mask_after_size_changes(tmp_path):
  93. host = costume_base.CostumeBase()
  94. host.I_CHECK_MAIN = make_rule(tmp_path, 'old', 20, 'Masked template matching', masked=True)
  95. assert host.I_CHECK_MAIN.match(masked_screenshot(host.I_CHECK_MAIN))
  96. replacement = make_rule(tmp_path, 'new', 24, 'Masked template matching', masked=True)
  97. host.replace_img('I_CHECK_MAIN', replacement)
  98. assert host.I_CHECK_MAIN.match(masked_screenshot(replacement))
  99. assert host.I_CHECK_MAIN.mask.shape == (24, 24)
  100. def test_replace_img_invalidates_multi_scale_dispatch(tmp_path):
  101. host = costume_base.CostumeBase()
  102. host.I_CHECK_MAIN = make_rule(tmp_path, 'scaled', 20, 'Multi-scale template matching')
  103. screen = np.zeros((80, 80, 3), dtype=np.uint8)
  104. screen[10:26, 10:26] = cv2.resize(host.I_CHECK_MAIN.image, (16, 16))
  105. assert host.I_CHECK_MAIN.match(screen)
  106. replacement = make_rule(tmp_path, 'plain', 20, 'Template matching')
  107. host.replace_img('I_CHECK_MAIN', replacement)
  108. assert not host.I_CHECK_MAIN.match(screen)
  109. def test_battle_scene_override_is_isolated_to_the_allowed_task():
  110. class Task(costume_base.CostumeBase):
  111. I_REWARD = deepcopy(GeneralBattleAssets.I_REWARD)
  112. def __init__(self, name):
  113. self.name = name
  114. def get_task_name(self):
  115. return self.name
  116. original = Task.I_REWARD
  117. original_file = original.file
  118. orochi = Task('Orochi')
  119. orochi.check_costume_battle_scene(BattleSceneType.COSTUME_BATTLE_SCENE_1)
  120. assert orochi.I_REWARD is not original
  121. assert orochi.I_REWARD.file != original_file
  122. assert orochi.I_REWARD.match(screenshot_for(orochi.I_REWARD))
  123. next_task = Task('RealmRaid')
  124. next_task.check_costume_battle_scene(BattleSceneType.COSTUME_BATTLE_SCENE_1)
  125. assert next_task.I_REWARD is original
  126. assert next_task.I_REWARD.file == original_file
  127. assert next_task.I_REWARD.match(screenshot_for(original))
  128. orochi.check_costume_battle_scene(BattleSceneType.COSTUME_BATTLE_SCENE_DEFAULT)
  129. assert orochi.I_REWARD is original