PamGram/core/wiki/services.py

101 lines
3.6 KiB
Python
Raw Normal View History

from typing import List, NoReturn, Optional
2022-07-26 10:07:31 +00:00
from core.wiki.cache import WikiCache
from modules.wiki.character import Character
from modules.wiki.weapon import Weapon
from utils.log import logger
2022-07-26 10:07:31 +00:00
class WikiService:
def __init__(self, cache: WikiCache):
self._cache = cache
2022-08-06 09:21:58 +00:00
"""Redis 在这里的作用是作为持久化"""
self._character_list = []
self._character_name_list = []
self._weapon_name_list = []
self._weapon_list = []
2022-07-26 10:07:31 +00:00
self.first_run = True
async def refresh_weapon(self) -> NoReturn:
weapon_name_list = await Weapon.get_name_list()
logger.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:
logger.info(f"现在已经获取到 {num} 把武器信息")
2022-07-26 10:07:31 +00:00
logger.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()
logger.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:
logger.info(f"现在已经获取到 {num} 个角色信息")
2022-07-26 10:07:31 +00:00
logger.info("写入角色信息到Redis")
self._character_list = character_list
await self._cache.delete("characters")
await self._cache.set("characters", [i.json() for i in character_list])
2022-07-26 10:07:31 +00:00
async def refresh_wiki(self) -> NoReturn:
2022-07-26 10:07:31 +00:00
"""
用于把Redis的缓存全部加载进Python
:return:
"""
logger.info("正在重新获取Wiki")
logger.info("正在重新获取武器信息")
2022-07-26 10:07:31 +00:00
await self.refresh_weapon()
logger.info("正在重新获取角色信息")
2022-07-26 10:07:31 +00:00
await self.refresh_characters()
logger.info("刷新成功")
2022-07-26 10:07:31 +00:00
async def init(self) -> NoReturn:
2022-07-26 10:07:31 +00:00
"""
用于把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 = [weapon.name for weapon in self._weapon_list]
characters_dict = await self._cache.get("characters")
self._character_list = [Character.parse_obj(obj) for obj in characters_dict]
self._character_name_list = [character.name for character in self._character_list]
2022-07-26 10:07:31 +00:00
self.first_run = False
async def get_weapons(self, name: str) -> Optional[Weapon]:
2022-07-26 10:07:31 +00:00
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)
2022-07-26 10:07:31 +00:00
async def get_weapons_name_list(self) -> List[str]:
2022-07-26 10:07:31 +00:00
await self.init()
return self._weapon_name_list
2022-07-26 10:07:31 +00:00
async def get_weapons_list(self) -> List[Weapon]:
2022-07-26 10:07:31 +00:00
await self.init()
return self._weapon_list
2022-07-26 10:07:31 +00:00
async def get_characters_list(self) -> List[Character]:
2022-07-26 10:07:31 +00:00
await self.init()
return self._character_list
2022-07-26 10:07:31 +00:00
async def get_characters_name_list(self) -> List[str]:
2022-07-26 10:07:31 +00:00
await self.init()
return self._character_name_list