Skip to content

Commit 02a2fed

Browse files
committed
fix: improve calc_current_stamina logic for accurate regeneration and enhance type hinting
1 parent fb66ca5 commit 02a2fed

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/swgoh_comlink/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SwgohComlinkBase:
6565
Direct instantiation is prevented; use ``SwgohComlink`` or ``SwgohComlinkAsync``.
6666
"""
6767

68-
__comlink_type__ = None
68+
__comlink_type__: str | None = None
6969
PROTOCOL = "http"
7070

7171
@staticmethod

src/swgoh_comlink/helpers/_conquest.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from __future__ import annotations
55

66
import time
7-
from typing import Any
87
from math import floor
8+
from typing import Any
99

1010
from ..exceptions import SwgohComlinkValueError
1111

@@ -32,15 +32,18 @@ def calc_current_stamina(unit: dict[str, Any], pass_plus: bool = False) -> int:
3232
raise SwgohComlinkValueError(f"'unit' must be a dict, not {type(unit)}")
3333

3434
acceleration_factor = 1.33 if pass_plus else 1.0
35-
remaining_stamina: int = unit.get("remainingStamina")
36-
last_refresh_time: int = int(unit.get("lastRefreshTime"))
35+
raw_stamina = unit.get("remainingStamina")
36+
raw_refresh = unit.get("lastRefreshTime")
3737

38-
if remaining_stamina is None or last_refresh_time is None:
38+
if raw_stamina is None or raw_refresh is None:
3939
raise SwgohComlinkValueError("Invalid unit data. Unable to determine current stamina and/or last refresh time.")
4040

41+
remaining_stamina: int = int(raw_stamina)
42+
last_refresh_time: int = int(raw_refresh)
43+
4144
time_diff_minutes = floor((floor(time.time()) - last_refresh_time) / 60)
4245

4346
# Stamina regenerates 1% every 30 minutes
4447
# Conquest Pass+ holders increase stamina regeneration by 33%
4548

46-
return min(floor(time_diff_minutes / 30 * acceleration_factor), 100)
49+
return min(floor(time_diff_minutes / 30 * acceleration_factor) + remaining_stamina, 100)

tests/unit/test_helpers_mocked.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from __future__ import annotations
44

5-
import pytest
65
from unittest.mock import patch
6+
7+
import pytest
78
from pytest_httpx import HTTPXMock
89

910
from swgoh_comlink import SwgohComlink, SwgohComlinkAsync

0 commit comments

Comments
 (0)