Skip to content

Commit

Permalink
Ensure drops cannot end up with inconsistent current minutes and prog…
Browse files Browse the repository at this point in the history
…ress
  • Loading branch information
DevilXD committed Jul 1, 2024
1 parent da05082 commit 5ade35d
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ def __init__(
super().__init__(campaign, data, claimed_benefits)
self._manager: GUIManager = self._twitch.gui
self._gui_inv: InventoryOverview = self._manager.inv
self.current_minutes: int = 0
if "self" in data:
self.current_minutes = data["self"]["currentMinutesWatched"]
self.current_minutes: int = "self" in data and data["self"]["currentMinutesWatched"] or 0
self.required_minutes: int = data["requiredMinutesWatched"]
if self.is_claimed:
# claimed drops report 0 current minutes, so we need to make a correction
# claimed drops may report inconsistent current minutes, so we need to overwrite them
self.current_minutes = self.required_minutes

def __repr__(self) -> str:
Expand Down Expand Up @@ -224,9 +222,11 @@ def total_remaining_minutes(self) -> int:

@cached_property
def progress(self) -> float:
if self.required_minutes > 0:
return self.current_minutes / self.required_minutes
return 0.0 # Drops with 0 required minutes are filtered via _base_can_earn
if self.current_minutes <= 0 or self.required_minutes <= 0:
return 0.0
elif self.current_minutes >= self.required_minutes:
return 1.0
return self.current_minutes / self.required_minutes

def _base_can_earn(self) -> bool:
return self.required_minutes > 0 and super()._base_can_earn()
Expand All @@ -248,7 +248,12 @@ async def claim(self) -> bool:
return result

def update_minutes(self, minutes: int):
self.current_minutes = minutes
if minutes < 0:
return
elif minutes <= self.required_minutes:
self.current_minutes = minutes
else:
self.current_minutes = self.required_minutes
self._on_minutes_changed()
self.display()

Expand Down

0 comments on commit 5ade35d

Please sign in to comment.