Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add Exception #10

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions python_genshin_artifact/calculator.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from json import JSONDecodeError

from genshin_artifact_core import (
get_damage_analysis as _get_damage_analysis,
get_transformative_damage as _get_transformative_damage,
)

from python_genshin_artifact.error import JsonParseException
from python_genshin_artifact.models.calculator import CalculatorConfig
from python_genshin_artifact.models.damage.analysis import DamageAnalysis, TransformativeDamage


def get_damage_analysis(value: CalculatorConfig) -> DamageAnalysis:
ret = _get_damage_analysis(value.json())
try:
ret = _get_damage_analysis(value.json())
except JSONDecodeError as exc:
raise JsonParseException from exc
return DamageAnalysis.parse_raw(ret)


def get_transformative_damage(value: CalculatorConfig) -> TransformativeDamage:
ret = _get_transformative_damage(value.json())
try:
ret = _get_transformative_damage(value.json())
except JSONDecodeError as exc:
raise JsonParseException from exc
return TransformativeDamage.parse_raw(ret)
20 changes: 13 additions & 7 deletions python_genshin_artifact/enka/enka_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from python_genshin_artifact.enka.characters import characters_map
from python_genshin_artifact.enka.fight import fight_map
from python_genshin_artifact.enka.weapon import weapon_name_map
from python_genshin_artifact.error import EnkaParseException
from python_genshin_artifact.models.artifact import ArtifactInfo
from python_genshin_artifact.models.characterInfo import CharacterInfo
from python_genshin_artifact.models.weapon import WeaponInfo
Expand All @@ -25,15 +26,15 @@ def is_ascend(level: int, promote_level: int) -> bool:
def enka_parser(data: dict, avatar_id: int) -> Tuple[CharacterInfo, WeaponInfo, List[ArtifactInfo]]:
character_info = assets.get_character(avatar_id)
if character_info is None:
raise RuntimeError
raise EnkaParseException(f"avatarId={avatar_id} is not found in assets")
_avatar_info_list = data["avatarInfoList"]
_avatar_info: dict = {}
for _value in _avatar_info_list:
if _value["avatarId"] == avatar_id:
_avatar_info = _value
break
else:
raise ValueError(f"avatarId={avatar_id} is not found")
raise EnkaParseException(f"avatarId={avatar_id} is not found")
_prop_map = _avatar_info.get("propMap", {})
level = int(_prop_map["4001"].get("ival", 0)) if "4001" in _prop_map else 0
talent_id_list = _avatar_info.get("talentIdList", []) # 命之座 ID 列 如果未解锁任何命之座则此数据不存在
Expand Down Expand Up @@ -74,10 +75,13 @@ def de_equip_list(equip_list: list[dict]) -> Tuple[WeaponInfo, List[ArtifactInfo
if _reliquary:
sub_stats: List[Tuple[str, float]] = []
_flat = _equip["flat"]
artifact_id = int(re.findall(r"\d+", _flat["icon"])[0])
try:
artifact_id = int(re.findall(r"\d+", _flat["icon"])[0])
except IndexError:
raise EnkaParseException(f"artifact_id is not found in {_flat['icon']}")
set_name = artifacts_name_map.get(artifact_id)
if set_name is None:
raise RuntimeError
raise EnkaParseException(f"artifact_id={artifact_id} is not found in assets")
_level = _reliquary["level"] - 1
_reliquary_main_stat = _flat["reliquaryMainstat"]
_main_prop_id = _reliquary_main_stat["mainPropId"]
Expand Down Expand Up @@ -121,10 +125,12 @@ def de_equip_list(equip_list: list[dict]) -> Tuple[WeaponInfo, List[ArtifactInfo
weapon_id = _equip["itemId"]
weapon_name = weapon_name_map.get(weapon_id)
if weapon_name is None:
raise RuntimeError
raise EnkaParseException(f"weapon_id={weapon_id} is not found in assets")
_level = _weapon["level"]
refinement_level = next(iter(_weapon["affixMap"].values())) + 1
_promote_level = _weapon["promoteLevel"]
ascend = is_ascend(_level, _promote_level)
_promote_level = _weapon.get("promoteLevel")
ascend = False
if _promote_level is not None:
ascend = is_ascend(_level, _promote_level)
weapon = WeaponInfo(level=_level, refine=refinement_level, ascend=ascend, name=weapon_name)
return weapon, artifacts
10 changes: 10 additions & 0 deletions python_genshin_artifact/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class GenshinArtifactException(Exception):
"""GenshinArtifactException"""


class JsonParseException(GenshinArtifactException):
"""JSON 解析错误"""


class EnkaParseException(GenshinArtifactException):
"""EnkaParse 解析错误"""