From 2609b4a70dbbecf6c6fb0b9ed894d8808a95bc83 Mon Sep 17 00:00:00 2001 From: LmeSzinc Date: Mon, 1 Jun 2020 02:01:04 +0800 Subject: [PATCH] Add: Timezone aware - Fix mat withdraw on slow device --- module/base/timer.py | 16 +++++++++++++++- module/campaign/run.py | 3 +-- module/config/config.py | 32 +++++++++++++++++++++++++++----- module/map/map_operation.py | 2 +- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/module/base/timer.py b/module/base/timer.py index a6dfe9dea..66ba8fa00 100644 --- a/module/base/timer.py +++ b/module/base/timer.py @@ -23,7 +23,7 @@ def future_time(string): string (str): Such as 14:59. Returns: - datetime.datetime: Time with given hour, minute, second in the future. + datetime.datetime: Time with given hour, minute in the future. """ hour, minute = [int(x) for x in string.split(':')] future = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0) @@ -31,6 +31,20 @@ def future_time(string): return future +def past_time(string): + """ + Args: + string (str): Such as 14:59. + + Returns: + datetime.datetime: Time with given hour, minute in the past. + """ + hour, minute = [int(x) for x in string.split(':')] + past = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0) + past = past - timedelta(days=1) if past > datetime.now() else past + return past + + def future_time_range(string): """ Args: diff --git a/module/campaign/run.py b/module/campaign/run.py index d997f6ab4..045218305 100644 --- a/module/campaign/run.py +++ b/module/campaign/run.py @@ -107,8 +107,7 @@ class CampaignRun(CampaignUI, Reward, LoginHandler): Returns: bool: If triggered a restart condition. """ - now = datetime.now() - if now.date() != self.start_time.date(): + if self.config.get_server_last_update(since=(0,)) > self.start_time: logger.hr('Triggered restart new day') return True if not self.campaign.config.IGNORE_LOW_EMOTION_WARN: diff --git a/module/config/config.py b/module/config/config.py index 369ca3b7b..8ef8897fb 100644 --- a/module/config/config.py +++ b/module/config/config.py @@ -2,15 +2,16 @@ import codecs import configparser import copy import os +from datetime import timezone import cv2 import numpy as np from PIL import Image +import module.config.server as server from module.base.timer import * from module.config.dictionary import * from module.logger import logger -import module.config.server as server class AzurLaneConfig: @@ -520,6 +521,30 @@ class AzurLaneConfig: self.C124_NON_S3_WITHDRAW_TOLERANCE = int(option['non_s3_enemy_withdraw_tolerance']) self.C124_AMMO_PICK_UP = int(option['ammo_pick_up_124']) + def get_server_timezone(self): + if self.SERVER == 'en': + return -7 + elif self.SERVER == 'cn': + return 8 + elif self.SERVER == 'jp': + return 9 + else: + return 8 + + def get_server_last_update(self, since): + """ + Args: + since (tuple(int)): Update hour in Azurlane, such as (0, 12, 18,). + + Returns: + datetime.datetime + """ + d = datetime.now(timezone.utc).astimezone() + diff = d.utcoffset() // timedelta(seconds=1) // 3600 - self.get_server_timezone() + since = np.sort((np.array(since) + diff) % 24) + update = sorted([past_time(f'{t}:00') for t in since])[-1] + return update + def record_executed_since(self, option, since): """ Args: @@ -530,10 +555,7 @@ class AzurLaneConfig: bool: If got a record after last game update. """ record = datetime.strptime(self.config.get(*option), self.TIME_FORMAT) - since = np.array(since) - - hour = since[since <= datetime.now().hour][-1] - update = datetime.now().replace(hour=hour, minute=0, second=0, microsecond=0) + update = self.get_server_last_update(since) logger.attr(f'{option[0]}_{option[1]}', f'Record time: {record}') logger.attr(f'{option[0]}_{option[1]}', f'Last update: {update}') diff --git a/module/map/map_operation.py b/module/map/map_operation.py index 008f801a6..8170bb0f2 100644 --- a/module/map/map_operation.py +++ b/module/map/map_operation.py @@ -128,7 +128,7 @@ class MapOperation(MysteryHandler, FleetPreparation, Retirement, FastForwardHand if self.handle_popup_confirm(): continue - if self.appear_then_click(WITHDRAW, interval=2): + if self.appear_then_click(WITHDRAW, interval=5): continue # End