refactor: add equipment registry
This commit is contained in:
@@ -4,6 +4,7 @@ from functools import cache
|
||||
from sqlalchemy.orm import Session
|
||||
from database.models import EmblemData, OathData, OathSuitData, OathSuitSkillData, PrimerData, EquData, SuitData, EnchantData, JadeData
|
||||
from database.connect import get_db_engine as get_engine
|
||||
from core.basic.equipment_registry import EquipmentRegistry
|
||||
|
||||
|
||||
def parse_to_number_list(info: str, default: Sequence[float] = (0,)) -> list[float]:
|
||||
@@ -422,14 +423,8 @@ class OathSuit:
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
|
||||
global equ0
|
||||
equ0 = Equipments('0')
|
||||
_equipment_registry = EquipmentRegistry(Equipments)
|
||||
|
||||
|
||||
def get_equipment(version: str = '0') -> Equipments:
|
||||
# print('加载装备信息')
|
||||
try:
|
||||
temp = eval(f'equ{version}'.format(version))
|
||||
except Exception:
|
||||
temp = equ0
|
||||
return temp
|
||||
return _equipment_registry.get(version)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
from functools import cache
|
||||
from pathlib import Path
|
||||
|
||||
from database.connect import BACKEND_DIR
|
||||
|
||||
|
||||
class EquipmentRegistry:
|
||||
def __init__(self, equipment_cls):
|
||||
self.equipment_cls = equipment_cls
|
||||
|
||||
@cache
|
||||
def get(self, version: str = '0'):
|
||||
normalized = version or '0'
|
||||
if not self._version_exists(normalized):
|
||||
if normalized == '0':
|
||||
return self.equipment_cls(normalized)
|
||||
return self.get('0')
|
||||
return self.equipment_cls(normalized)
|
||||
|
||||
def _version_exists(self, version: str) -> bool:
|
||||
db_path = Path(BACKEND_DIR) / 'dataFiles' / f'dcalc_{version}.db'
|
||||
return db_path.exists()
|
||||
@@ -0,0 +1,30 @@
|
||||
from core.basic.equipment import get_equipment
|
||||
from core.basic.equipment_registry import EquipmentRegistry
|
||||
|
||||
|
||||
def test_get_equipment_returns_cached_default_version():
|
||||
first = get_equipment('0')
|
||||
second = get_equipment('0')
|
||||
|
||||
assert first is second
|
||||
assert first.version == '0'
|
||||
|
||||
|
||||
def test_get_equipment_falls_back_to_default_version():
|
||||
equipment = get_equipment('missing-version')
|
||||
|
||||
assert equipment.version == '0'
|
||||
|
||||
|
||||
def test_equipment_registry_caches_by_version():
|
||||
calls = []
|
||||
|
||||
class Equipment:
|
||||
def __init__(self, version):
|
||||
calls.append(version)
|
||||
self.version = version
|
||||
|
||||
registry = EquipmentRegistry(Equipment)
|
||||
|
||||
assert registry.get('0') is registry.get('0')
|
||||
assert calls == ['0']
|
||||
Reference in New Issue
Block a user