2023-04-26 08:48:05 +00:00
|
|
|
from typing import List, Dict, Optional
|
2022-08-28 14:37:31 +00:00
|
|
|
|
2023-04-26 08:48:05 +00:00
|
|
|
from modules.wiki.base import WikiModel
|
2023-07-20 05:35:01 +00:00
|
|
|
from modules.wiki.models.avatar import YattaAvatar
|
2022-08-28 14:37:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Character(WikiModel):
|
2023-04-26 08:48:05 +00:00
|
|
|
avatar_url = WikiModel.BASE_URL + "avatars.json"
|
|
|
|
avatar_path = WikiModel.BASE_PATH / "avatars.json"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2023-07-20 05:35:01 +00:00
|
|
|
self.all_avatars: List[YattaAvatar] = []
|
|
|
|
self.all_avatars_map: Dict[int, YattaAvatar] = {}
|
|
|
|
self.all_avatars_name: Dict[str, YattaAvatar] = {}
|
2023-04-26 08:48:05 +00:00
|
|
|
|
|
|
|
def clear_class_data(self) -> None:
|
|
|
|
self.all_avatars.clear()
|
|
|
|
self.all_avatars_map.clear()
|
|
|
|
self.all_avatars_name.clear()
|
|
|
|
|
|
|
|
async def refresh(self):
|
|
|
|
datas = await self.remote_get(self.avatar_url)
|
|
|
|
await self.dump(datas.json(), self.avatar_path)
|
|
|
|
await self.read()
|
|
|
|
|
|
|
|
async def read(self):
|
|
|
|
if not self.avatar_path.exists():
|
|
|
|
await self.refresh()
|
|
|
|
return
|
|
|
|
datas = await WikiModel.read(self.avatar_path)
|
|
|
|
self.clear_class_data()
|
|
|
|
for data in datas:
|
2023-07-20 05:35:01 +00:00
|
|
|
m = YattaAvatar(**data)
|
2023-04-26 08:48:05 +00:00
|
|
|
self.all_avatars.append(m)
|
|
|
|
self.all_avatars_map[m.id] = m
|
|
|
|
self.all_avatars_name[m.name] = m
|
|
|
|
|
2023-07-20 05:35:01 +00:00
|
|
|
def get_by_id(self, cid: int) -> Optional[YattaAvatar]:
|
2023-05-11 15:09:39 +00:00
|
|
|
return self.all_avatars_map.get(cid)
|
2023-04-26 08:48:05 +00:00
|
|
|
|
2023-07-20 05:35:01 +00:00
|
|
|
def get_by_name(self, name: str) -> Optional[YattaAvatar]:
|
2023-05-11 15:09:39 +00:00
|
|
|
return self.all_avatars_name.get(name)
|
2023-04-26 08:48:05 +00:00
|
|
|
|
|
|
|
def get_name_list(self) -> List[str]:
|
|
|
|
return list(self.all_avatars_name.keys())
|