Skip to content

Commit

Permalink
fix: check for none values in completion
Browse files Browse the repository at this point in the history
  • Loading branch information
NiceAesth committed Aug 23, 2023
1 parent 2166a12 commit d1cf468
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
46 changes: 31 additions & 15 deletions aiosu/models/lazer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def calculate_score_completion(
statistics: LazerScoreStatistics,
beatmap: Beatmap,
) -> float:
) -> Optional[float]:
"""Calculates completion for a score.
:param statistics: The statistics of the score
Expand All @@ -39,8 +39,11 @@ def calculate_score_completion(
:type beatmap: aiosu.models.beatmap.Beatmap
:raises ValueError: If the gamemode is unknown
:return: Completion for the given score
:rtype: float
:rtype: Optional[float]
"""
if not beatmap.count_objects:
return None

return (
(
statistics.perfect
Expand Down Expand Up @@ -81,27 +84,33 @@ class LazerScoreStatistics(BaseModel):
perfect: int = 0
legacy_combo_increase: int = 0

@computed_field
@computed_field # type: ignore
@property
def count_300(self) -> int:
return self.great

@computed_field
@computed_field # type: ignore
@property
def count_100(self) -> int:
return self.ok

@computed_field
@computed_field # type: ignore
@property
def count_50(self) -> int:
return self.meh

@computed_field
@computed_field # type: ignore
@property
def count_miss(self) -> int:
return self.miss

@computed_field
@computed_field # type: ignore
@property
def count_geki(self) -> int:
return self.perfect

@computed_field
@computed_field # type: ignore
@property
def count_katu(self) -> int:
return self.good

Expand Down Expand Up @@ -139,15 +148,18 @@ class LazerScore(BaseModel):
pp: Optional[float] = None
weight: Optional[ScoreWeight] = None

@computed_field
@computed_field # type: ignore
@property
def mods_str(self) -> str:
return "".join(str(mod) for mod in self.mods)

@computed_field
@computed_field # type: ignore
@property
def created_at(self) -> datetime:
return self.ended_at

@computed_field
@computed_field # type: ignore
@property
def completion(self) -> Optional[float]:
"""Beatmap completion.
Expand All @@ -162,15 +174,18 @@ def completion(self) -> Optional[float]:

return calculate_score_completion(self.statistics, self.beatmap)

@computed_field
@computed_field # type: ignore
@property
def mode(self) -> Gamemode:
return Gamemode(self.ruleset_id)

@computed_field
@computed_field # type: ignore
@property
def score(self) -> int:
return self.total_score

@computed_field
@computed_field # type: ignore
@property
def score_url(self) -> Optional[str]:
r"""Link to the score.
Expand All @@ -185,7 +200,8 @@ def score_url(self) -> Optional[str]:
else f"https://osu.ppy.sh/scores/{self.id}"
)

@computed_field
@computed_field # type: ignore
@property
def replay_url(self) -> Optional[str]:
r"""Link to the replay.
Expand Down
17 changes: 12 additions & 5 deletions aiosu/models/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def calculate_score_completion(
mode: Gamemode,
statistics: ScoreStatistics,
beatmap: Beatmap,
) -> float:
) -> Optional[float]:
"""Calculates completion for a score.
:param mode: The gamemode of the score
Expand All @@ -56,8 +56,11 @@ def calculate_score_completion(
:type beatmap: aiosu.models.beatmap.Beatmap
:raises ValueError: If the gamemode is unknown
:return: Completion for the given score
:rtype: float
:rtype: Optional[float]
"""
if not beatmap.count_objects:
return None

if mode == Gamemode.STANDARD:
return (
(
Expand Down Expand Up @@ -90,6 +93,7 @@ def calculate_score_completion(
)
/ beatmap.count_objects
) * 100

raise ValueError("Unknown mode specified.")


Expand Down Expand Up @@ -157,7 +161,8 @@ class Score(BaseModel):
beatmap_id: Optional[int] = None
"""Only present on API v1"""

@computed_field
@computed_field # type: ignore
@property
def completion(self) -> Optional[float]:
"""Beatmap completion.
Expand All @@ -173,7 +178,8 @@ def completion(self) -> Optional[float]:

return calculate_score_completion(self.mode, self.statistics, self.beatmap)

@computed_field
@computed_field # type: ignore
@property
def score_url(self) -> Optional[str]:
r"""Link to the score.
Expand All @@ -188,7 +194,8 @@ def score_url(self) -> Optional[str]:
else f"https://osu.ppy.sh/scores/{self.id}"
)

@computed_field
@computed_field # type: ignore
@property
def replay_url(self) -> Optional[str]:
r"""Link to the replay.
Expand Down

0 comments on commit d1cf468

Please sign in to comment.