2023-04-26 08:48:05 +00:00
|
|
|
from typing import List, Dict, Optional
|
|
|
|
|
|
|
|
from modules.wiki.base import WikiModel
|
2023-08-29 06:03:21 +00:00
|
|
|
from modules.wiki.models.light_cone import YattaLightCone
|
2023-04-26 08:48:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LightCone(WikiModel):
|
|
|
|
light_cone_url = WikiModel.BASE_URL + "light_cones.json"
|
|
|
|
light_cone_path = WikiModel.BASE_PATH / "light_cones.json"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2023-08-29 06:03:21 +00:00
|
|
|
self.all_light_cones: List[YattaLightCone] = []
|
|
|
|
self.all_light_cones_map: Dict[int, YattaLightCone] = {}
|
|
|
|
self.all_light_cones_name: Dict[str, YattaLightCone] = {}
|
2023-04-26 08:48:05 +00:00
|
|
|
|
|
|
|
def clear_class_data(self) -> None:
|
|
|
|
self.all_light_cones.clear()
|
|
|
|
self.all_light_cones_map.clear()
|
|
|
|
self.all_light_cones_name.clear()
|
|
|
|
|
|
|
|
async def refresh(self):
|
|
|
|
datas = await self.remote_get(self.light_cone_url)
|
|
|
|
await self.dump(datas.json(), self.light_cone_path)
|
|
|
|
await self.read()
|
|
|
|
|
|
|
|
async def read(self):
|
|
|
|
if not self.light_cone_path.exists():
|
|
|
|
await self.refresh()
|
|
|
|
return
|
|
|
|
datas = await WikiModel.read(self.light_cone_path)
|
|
|
|
self.clear_class_data()
|
|
|
|
for data in datas:
|
2023-08-29 06:03:21 +00:00
|
|
|
m = YattaLightCone(**data)
|
2023-04-26 08:48:05 +00:00
|
|
|
self.all_light_cones.append(m)
|
|
|
|
self.all_light_cones_map[m.id] = m
|
|
|
|
self.all_light_cones_name[m.name] = m
|
|
|
|
|
2023-08-29 06:03:21 +00:00
|
|
|
def get_by_id(self, cid: int) -> Optional[YattaLightCone]:
|
2023-05-11 15:09:39 +00:00
|
|
|
return self.all_light_cones_map.get(cid)
|
2023-04-26 08:48:05 +00:00
|
|
|
|
2023-08-29 06:03:21 +00:00
|
|
|
def get_by_name(self, name: str) -> Optional[YattaLightCone]:
|
2023-05-11 15:09:39 +00:00
|
|
|
return self.all_light_cones_name.get(name)
|
2023-04-26 08:48:05 +00:00
|
|
|
|
|
|
|
def get_name_list(self) -> List[str]:
|
|
|
|
return list(self.all_light_cones_name.keys())
|