Skip to content

Commit

Permalink
fix: Fix server reset time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Mar 10, 2024
1 parent 79a2492 commit b0bf8ae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
14 changes: 11 additions & 3 deletions hoyo_buddy/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ..constants import UID_SERVER_RESET_HOURS
from ..enums import GAME_CONVERTER, Game, NotesNotifyType
from ..hoyo.clients.gpy_client import GenshinClient
from ..utils import get_now

if TYPE_CHECKING:
import genshin
Expand Down Expand Up @@ -52,12 +53,19 @@ def client(self) -> GenshinClient:
return GenshinClient(self.cookies, game=game, uid=self.uid)

@property
def server_reset_time(self) -> datetime.time:
def server_reset_datetime(self) -> datetime.datetime:
"""Server reset time in UTC+8."""
for uid_start, reset_hour in UID_SERVER_RESET_HOURS.items():
if str(self.uid).startswith(uid_start):
return datetime.time(reset_hour, 0)
return datetime.time(4, 0)
reset_time = get_now().replace(hour=reset_hour, minute=0, second=0, microsecond=0)
break
else:
reset_time = get_now().replace(hour=4, minute=0, second=0, microsecond=0)

if reset_time < get_now():
reset_time += datetime.timedelta(days=1)

return reset_time


class AccountNotifSettings(Model):
Expand Down
6 changes: 2 additions & 4 deletions hoyo_buddy/hoyo/auto_tasks/notes_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ...icons import COMMISSION_ICON, PT_ICON, REALM_CURRENCY_ICON, RESIN_ICON, RTBP_ICON, TBP_ICON
from ...models import DrawInput
from ...ui.hoyo.notes.view import NotesView
from ...utils import get_hour_before_time, get_now
from ...utils import get_now

if TYPE_CHECKING:
from ...bot.bot import HoyoBuddy
Expand Down Expand Up @@ -381,9 +381,7 @@ def _determine_skip(cls, notify: NotesNotify) -> bool: # noqa: PLR0911
):
return True

if notify.notify_time is not None and get_now().time() < get_hour_before_time(
notify.account.server_reset_time, notify.notify_time
):
if notify.notify_time is not None and get_now() < notify.account.server_reset_datetime:
return True

return False
Expand Down
9 changes: 0 additions & 9 deletions hoyo_buddy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ def get_now() -> datetime.datetime:
return datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8)))


def get_hour_before_time(time: datetime.time, hour: int) -> datetime.time:
"""
Get the time before a specific hour
"""
return (
datetime.datetime.combine(datetime.date.today(), time) - datetime.timedelta(hours=hour)
).time()


def timer(func: "Callable[..., Any]") -> "Callable[..., Any]":
"""
A decorator that prints the runtime of the decorated function
Expand Down

0 comments on commit b0bf8ae

Please sign in to comment.