|
@@ -0,0 +1,102 @@
|
|
|
|
|
+"""Check the standalone skill against the user's actual colosseum config."""
|
|
|
|
|
+import ast
|
|
|
|
|
+from datetime import date
|
|
|
|
|
+import json
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+from types import SimpleNamespace as NS
|
|
|
|
|
+import unittest
|
|
|
|
|
+from unittest.mock import Mock, patch
|
|
|
|
|
+
|
|
|
|
|
+from client_local.utils.daily_spawn import daily_spawn_choice
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def class_node(path, name):
|
|
|
|
|
+ tree = ast.parse((ROOT / path).read_text(encoding='utf-8-sig'))
|
|
|
|
|
+ return next(node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == name)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class DailySpawnTests(unittest.TestCase):
|
|
|
|
|
+ def setup_skill(self):
|
|
|
|
|
+ tick = [1000]
|
|
|
|
|
+
|
|
|
|
|
+ class Skill:
|
|
|
|
|
+ def __init__(self, user, to, instances):
|
|
|
|
|
+ self.user = user
|
|
|
|
|
+ self.skill_instances = instances
|
|
|
|
|
+ self.skill_time_init = self.current_time = tick[0]
|
|
|
|
|
+ self.effect_instances = []
|
|
|
|
|
+ instances.append(self)
|
|
|
|
|
+
|
|
|
|
|
+ def update(self):
|
|
|
|
|
+ self.current_time = tick[0]
|
|
|
|
|
+
|
|
|
|
|
+ def kill(self):
|
|
|
|
|
+ if self in self.skill_instances:
|
|
|
|
|
+ self.skill_instances.remove(self)
|
|
|
|
|
+ for effect in self.effect_instances:
|
|
|
|
|
+ effect.state = 'dead'
|
|
|
|
|
+
|
|
|
|
|
+ config = {'basic': {'width': 60, 'height': 60}}
|
|
|
|
|
+ scene = NS(map_json=config, map_name='OnlineColosseum', me=NS(), skill_instances=[], npcs=[],
|
|
|
|
|
+ get_an_npc=Mock(side_effect=lambda name, point: NS(name=name, point=point)))
|
|
|
|
|
+ scope = {'Skill': Skill, 'level': NS(current_level=scene),
|
|
|
|
|
+ 'EffectInstance': Mock(side_effect=lambda *a: NS(set_xy=Mock())),
|
|
|
|
|
+ 'sound_player': NS(play=Mock()), 'get_skill_combo': lambda *a: None}
|
|
|
|
|
+ node = class_node('client_local/skill/parametric_skill.py', 'DailyRandomPointSpawn')
|
|
|
|
|
+ exec(compile(ast.Module(body=[node], type_ignores=[]), '<standalone-skill>', 'exec'), scope)
|
|
|
|
|
+ tree = ast.parse((ROOT / 'client_local/skill/skill_map.py').read_text(encoding='utf-8-sig'))
|
|
|
|
|
+ entries = [value for node in ast.walk(tree) if isinstance(node, ast.Dict)
|
|
|
|
|
+ for key, value in zip(node.keys, node.values)
|
|
|
|
|
+ if isinstance(key, ast.Constant) and key.value == 'DailyRandomPointSpawn']
|
|
|
|
|
+ self.assertEqual(len(entries), 1)
|
|
|
|
|
+ scope['skill_map'] = {'DailyRandomPointSpawn': eval(compile(ast.Expression(entries[0]), '<registration>', 'eval'), scope)}
|
|
|
|
|
+ method = next(node for node in class_node('client_local/sprite/npc.py', 'Npc').body
|
|
|
|
|
+ if isinstance(node, ast.FunctionDef) and node.name == 'using_skill_map')
|
|
|
|
|
+ exec(compile(ast.Module(body=[method], type_ignores=[]), '<npc-dispatch>', 'exec'), scope)
|
|
|
|
|
+ return tick, scene, scope
|
|
|
|
|
+
|
|
|
|
|
+ @unittest.skipUnless((ROOT / 'game/npc/OnlineColosseumController.json').is_file(),
|
|
|
|
|
+ 'Requires externally distributed colosseum controller resource')
|
|
|
|
|
+ def test_actual_twenty_controller_entries_dispatch_and_spawn(self):
|
|
|
|
|
+ controller = json.loads((ROOT / 'game/npc/OnlineColosseumController.json').read_text(encoding='utf-8-sig'))
|
|
|
|
|
+ entries = [row for row in controller['skills'] if row['name'] == 'DailyRandomPointSpawn']
|
|
|
|
|
+ self.assertEqual(len(entries), 20)
|
|
|
|
|
+ tick, scene, scope = self.setup_skill()
|
|
|
|
|
+ user = NS(skill_instances=[], effects_behind=[])
|
|
|
|
|
+ with patch('client_local.utils.daily_spawn.daily_spawn_date', return_value=date(2026, 11, 14)):
|
|
|
|
|
+ for entry in entries:
|
|
|
|
|
+ scope['using_skill_map'](user, entry['name'], entry['params'], 0)
|
|
|
|
|
+ self.assertEqual(len(user.skill_instances), 20)
|
|
|
|
|
+ instances = list(user.skill_instances)
|
|
|
|
|
+ for skill in instances:
|
|
|
|
|
+ skill.update()
|
|
|
|
|
+ self.assertFalse(scene.npcs)
|
|
|
|
|
+ tick[0] = 1300
|
|
|
|
|
+ for skill in instances:
|
|
|
|
|
+ skill.update()
|
|
|
|
|
+ skill.update()
|
|
|
|
|
+ self.assertEqual(len(scene.npcs), 20)
|
|
|
|
|
+ self.assertFalse(user.skill_instances)
|
|
|
|
|
+ for npc, entry in zip(scene.npcs, entries):
|
|
|
|
|
+ self.assertEqual(npc.point, tuple(entry['params']['point']))
|
|
|
|
|
+ self.assertEqual(npc.name, daily_spawn_choice(entry['params'], 'OnlineColosseum', date(2026, 11, 14)))
|
|
|
|
|
+
|
|
|
|
|
+ def test_same_point_is_stable_across_repeated_instances(self):
|
|
|
|
|
+ params = {'candidates': ['A', 'B', 'C'], 'point': [10, 8]}
|
|
|
|
|
+ expected = daily_spawn_choice(params, 'OnlineColosseum', date(2026, 11, 14))
|
|
|
|
|
+ for _ in range(30):
|
|
|
|
|
+ self.assertEqual(daily_spawn_choice(params, 'OnlineColosseum', date(2026, 11, 14)), expected)
|
|
|
|
|
+
|
|
|
|
|
+ def test_invalid_point_fails_before_registering(self):
|
|
|
|
|
+ tick, scene, scope = self.setup_skill()
|
|
|
|
|
+ instances = []
|
|
|
|
|
+ with self.assertRaises(ValueError):
|
|
|
|
|
+ scope['DailyRandomPointSpawn'](NS(), None, {'candidates': ['A'], 'point': [60, 0]}, instances)
|
|
|
|
|
+ self.assertFalse(instances)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
|
+ unittest.main()
|