From 2e50bf2081ba01cb67e2761b1db0a31ffcad003e Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Fri, 21 Jul 2023 17:11:30 +0800 Subject: [PATCH] :sparkles: Support starrail activity - FantasticStory --- simnet/models/starrail/character.py | 2 +- simnet/models/starrail/chronicle/activity.py | 60 ++++++++++++++++++-- simnet/models/starrail/chronicle/base.py | 4 +- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/simnet/models/starrail/character.py b/simnet/models/starrail/character.py index 8dcebe3..78c37dd 100644 --- a/simnet/models/starrail/character.py +++ b/simnet/models/starrail/character.py @@ -31,7 +31,7 @@ class RogueCharacter(StarRailBaseCharacter): level: int -class StarFightCharacter(StarRailBaseCharacter): +class ActivityCharacter(StarRailBaseCharacter): """Combat character model.""" level: int diff --git a/simnet/models/starrail/chronicle/activity.py b/simnet/models/starrail/chronicle/activity.py index 476d479..e0947eb 100644 --- a/simnet/models/starrail/chronicle/activity.py +++ b/simnet/models/starrail/chronicle/activity.py @@ -2,7 +2,7 @@ from typing import List, Optional from simnet.models.base import APIModel -from simnet.models.starrail.character import StarFightCharacter +from simnet.models.starrail.character import ActivityCharacter from .base import PartialTime @@ -25,7 +25,7 @@ class StarRailStarFightRecord(APIModel): round: int stage_id: int time: Optional[PartialTime] - lineup: List[StarFightCharacter] + lineup: List[ActivityCharacter] @property def time_str(self) -> str: @@ -42,16 +42,66 @@ class StarRailStarFight(StarRailActivityBase): records: List[StarRailStarFightRecord] +class StarRailFantasticStoryBuff(APIModel): + """Fantastic Story Buff""" + + id: int + name: str + desc: str + icon: str + + +class StarRailFantasticStoryRecord(APIModel): + """Fantastic Story Record""" + + name: str + score: int + score_rank: int + stage_id: int + finish_time: Optional[PartialTime] + avatars: List[ActivityCharacter] + buffs: List[StarRailFantasticStoryBuff] + + @property + def time_str(self) -> str: + """Get the time as a string.""" + if self.finish_time is None: + return "N/A" + + return self.finish_time.datetime.strftime("%Y.%m.%d %H:%M") + + +class StarRailFantasticStory(StarRailActivityBase): + """Fantastic Story""" + + records: List[StarRailFantasticStoryRecord] + + class StarRailActivity(APIModel): """Starrail chronicle activity.""" activities: List + def find_activity(self, key: str) -> Optional[dict]: + """Find an activity by key.""" + for activity in self.activities: + if list(activity.keys())[0] == key: + return activity + + return None + @property def star_fight(self) -> StarRailStarFight: """Get the star fight activity.""" - for activity in self.activities: - if list(activity.keys())[0] == "star_fight": - return StarRailStarFight(**activity["star_fight"]) + if data := self.find_activity("star_fight"): + return StarRailStarFight(**data["star_fight"]) raise ValueError("No star fight activity found.") + + @property + def fantastic_story(self) -> StarRailFantasticStory: + """Get the fantastic story activity.""" + if data := self.find_activity("fantastic_story"): + return StarRailFantasticStory(**data["fantastic_story"]) + + raise ValueError("No fantastic story activity found.") diff --git a/simnet/models/starrail/chronicle/base.py b/simnet/models/starrail/chronicle/base.py index 2d05b01..1080fb4 100644 --- a/simnet/models/starrail/chronicle/base.py +++ b/simnet/models/starrail/chronicle/base.py @@ -1,5 +1,6 @@ """Starrail Chronicle Base Model.""" import datetime +from typing import Optional from simnet.models.base import APIModel @@ -12,7 +13,8 @@ class PartialTime(APIModel): day: int hour: int minute: int + second: Optional[int] = None @property def datetime(self) -> datetime.datetime: - return datetime.datetime(self.year, self.month, self.day, self.hour, self.minute) + return datetime.datetime(self.year, self.month, self.day, self.hour, self.minute, self.second or 0)