EnkaNetwork.py/enkanetwork/model/equipments.py

118 lines
3.7 KiB
Python
Raw Normal View History

2022-07-05 08:46:47 +00:00
import logging
2022-07-05 06:41:29 +00:00
from pydantic import BaseModel, Field
from typing import Any, List
from ..enum import EquipmentsType, DigitType, EquipType
2022-07-16 19:36:26 +00:00
from ..assets import Assets
2022-07-05 06:41:29 +00:00
from ..utils import create_ui_path
2022-07-05 08:46:47 +00:00
LOGGER = logging.getLogger(__name__)
2022-07-05 06:41:29 +00:00
class EquipmentsStats(BaseModel):
prop_id: str = ""
type: DigitType = DigitType.NUMBER
name: str = ""
value: int = 0
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-05 08:46:47 +00:00
if isinstance(data["statValue"], float):
__pydantic_self__.type = DigitType.PERCENT
__pydantic_self__.value = data["statValue"]
LOGGER.debug(f"=== Fight prop ===")
2022-07-05 06:41:29 +00:00
if "mainPropId" in data:
__pydantic_self__.prop_id = str(data["mainPropId"])
2022-07-16 19:36:26 +00:00
fight_prop = Assets.get_hash_map(str(data["mainPropId"]))
2022-07-05 06:41:29 +00:00
else:
__pydantic_self__.prop_id = str(data["appendPropId"])
2022-07-16 19:36:26 +00:00
fight_prop = Assets.get_hash_map(str(data["appendPropId"]))
2022-07-05 06:41:29 +00:00
2022-07-05 08:46:47 +00:00
if not fight_prop:
return
2022-07-05 06:41:29 +00:00
2022-07-16 19:36:26 +00:00
__pydantic_self__.name = fight_prop
2022-07-05 06:41:29 +00:00
class EquipmentsDetail(BaseModel):
"""
API Response
"""
nameTextMapHash: str = ""
2022-07-05 08:46:47 +00:00
setnameTextMapHash: str = ""
2022-07-05 06:41:29 +00:00
"""
Custom data
"""
name: str = "" # Get from name hash map
2022-07-16 19:36:26 +00:00
artifactType: EquipType = EquipType.Unknown # Type of artifact
2022-07-05 06:41:29 +00:00
icon: str = ""
rarity: int = Field(0, alias="rankLevel")
mainstats: EquipmentsStats = Field(None, alias="reliquaryMainstat")
substats: List[EquipmentsStats] = []
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
2022-07-05 08:46:47 +00:00
if data["itemType"] == "ITEM_RELIQUARY": # AKA. Artifact
LOGGER.debug(f"=== Artifact ===")
2022-07-16 19:36:26 +00:00
__pydantic_self__.icon = Assets.create_icon_path(data["icon"])
__pydantic_self__.artifactType = EquipType(data["equipType"]).name
2022-07-05 08:46:47 +00:00
# Sub Stats
2022-07-05 06:41:29 +00:00
for stats in data["reliquarySubstats"]:
__pydantic_self__.substats.append(EquipmentsStats.parse_obj(stats))
if data["itemType"] == "ITEM_WEAPON": # AKA. Weapon
2022-07-05 08:46:47 +00:00
LOGGER.debug(f"=== Weapon ===")
__pydantic_self__.icon = create_ui_path(data["icon"])
# Main and Sub Stats
2022-07-05 06:41:29 +00:00
__pydantic_self__.mainstats = EquipmentsStats.parse_obj(data["weaponStats"][0])
for stats in data["weaponStats"][1:]:
__pydantic_self__.substats.append(EquipmentsStats.parse_obj(stats))
2022-07-05 08:46:47 +00:00
2022-07-16 19:36:26 +00:00
_name = Assets.get_hash_map(str(data["nameTextMapHash"]))
2022-07-05 08:46:47 +00:00
2022-07-16 19:36:26 +00:00
if _name is None:
return
2022-07-05 08:46:47 +00:00
2022-07-16 19:36:26 +00:00
__pydantic_self__.name = _name
2022-07-05 08:46:47 +00:00
2022-07-05 06:41:29 +00:00
class Config:
use_enum_values = True
class Equipments(BaseModel):
"""
API Response data
"""
id: int = Field(0, alias="itemId")
detail: EquipmentsDetail = Field({}, alias="flat")
"""
Custom data
"""
level: int = 0 # Get form key "reliquary" and "weapon"
type: EquipmentsType = EquipmentsType.UNKNOWN # Type of equipments (Ex. Artifact, Weapon)
refinement: int = 0 # Refinement of equipments (Weapon only)
2022-07-05 06:41:29 +00:00
class Config:
use_enum_values = True
def __init__(__pydantic_self__, **data: Any) -> None:
super().__init__(**data)
if data["flat"]["itemType"] == "ITEM_RELIQUARY": # AKA. Artifact
__pydantic_self__.type = EquipmentsType.ARTIFACT
__pydantic_self__.level = data["reliquary"]["level"] - 1
if data["flat"]["itemType"] == "ITEM_WEAPON": # AKA. Weapon
__pydantic_self__.type = EquipmentsType.WEAPON
__pydantic_self__.level = data["weapon"]["level"] - 1
if "affixMap" in data["weapon"]:
__pydantic_self__.refinement = data["weapon"]["affixMap"][list(data["weapon"]["affixMap"].keys())[0]] + 1