PamGram/core/wiki/services.py
Karako 91a133b694
♻️ 重写 wiki 模块和相关插件
1. 使用 `pydantic` 重写了 wiki 模块所使用的 model
2. 添加了 weapon_level.json 用于后续计算武器升级所需的经验
3. 修改了 wiki 插件,以适应新的 model
2022-08-28 22:37:31 +08:00

102 lines
3.5 KiB
Python

from typing import List, NoReturn, Optional
from core.wiki.cache import WikiCache
from logger import Log
from models.wiki.character import Character
from models.wiki.weapon import Weapon
class WikiService:
def __init__(self, cache: WikiCache):
self._cache = cache
"""Redis 在这里的作用是作为持久化"""
self._character_list = []
self._character_name_list = []
self._weapon_name_list = []
self._weapon_list = []
self.first_run = True
async def refresh_weapon(self) -> NoReturn:
weapon_name_list = await Weapon.get_name_list()
Log.info(f"一共找到 {len(weapon_name_list)} 把武器信息")
weapon_list = []
num = 0
async for weapon in Weapon.full_data_generator():
weapon_list.append(weapon)
num += 1
if num % 10 == 0:
Log.info(f"现在已经获取到 {num} 把武器信息")
Log.info("写入武器信息到Redis")
self._weapon_list = weapon_list
await self._cache.delete("weapon")
await self._cache.set("weapon", [i.json() for i in weapon_list])
async def refresh_characters(self) -> NoReturn:
character_name_list = await Character.get_name_list()
Log.info(f"一共找到 {len(character_name_list)} 个角色信息")
character_list = []
num = 0
async for character in Character.full_data_generator():
character_list.append(character)
num += 1
if num % 10 == 0:
Log.info(f"现在已经获取到 {num} 个角色信息")
Log.info("写入角色信息到Redis")
self._character_list = character_list
await self._cache.delete("characters")
await self._cache.set("characters", [i.json() for i in character_list])
async def refresh_wiki(self) -> NoReturn:
"""
用于把Redis的缓存全部加载进Python
:return:
"""
Log.info("正在重新获取Wiki")
Log.info("正在重新获取武器信息")
await self.refresh_weapon()
Log.info("正在重新获取角色信息")
await self.refresh_characters()
Log.info("刷新成功")
async def init(self) -> NoReturn:
"""
用于把Redis的缓存全部加载进Python
:return:
"""
if self.first_run:
weapon_dict = await self._cache.get("weapon")
self._weapon_list = [Weapon.parse_obj(obj) for obj in weapon_dict]
self._weapon_name_list = await Weapon.get_name_list()
characters_dict = await self._cache.get("characters")
self._character_list = [Character.parse_obj(obj) for obj in characters_dict]
self._character_name_list = await Character.get_name_list()
self.first_run = False
async def get_weapons(self, name: str) -> Optional[Weapon]:
await self.init()
if len(self._weapon_list) == 0:
return None
return next((weapon for weapon in self._weapon_list if weapon.name == name), None)
async def get_weapons_name_list(self) -> List[str]:
await self.init()
return self._weapon_name_list
async def get_weapons_list(self) -> List[Weapon]:
await self.init()
return self._weapon_list
async def get_characters_list(self) -> List[Character]:
await self.init()
return self._character_list
async def get_characters_name_list(self) -> List[str]:
await self.init()
return self._character_name_list