From ef5885767e49b2d11e4af665491fec5a3e27939c Mon Sep 17 00:00:00 2001 From: LmeSzinc Date: Mon, 27 Apr 2020 23:59:51 +0800 Subject: [PATCH] =?UTF-8?q?Optimize:=20=E6=88=98=E6=96=97=E4=B8=AD?= =?UTF-8?q?=E6=94=BE=E6=85=A2=E6=88=AA=E5=9B=BE=E9=80=9F=E5=BA=A6,=20?= =?UTF-8?q?=E9=99=8D=E4=BD=8ECPU=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/template.ini | 1 + module/base/timer.py | 8 ++++++++ module/combat/combat.py | 3 ++- module/combat/submarine.py | 5 +++-- module/config/argparser.py | 1 + module/config/config.py | 2 ++ module/config/dictionary.py | 1 + module/device/screenshot.py | 14 +++++++++++++- 8 files changed, 31 insertions(+), 4 deletions(-) diff --git a/config/template.ini b/config/template.ini index 0c5550398..dc0f5dc00 100644 --- a/config/template.ini +++ b/config/template.ini @@ -88,6 +88,7 @@ enable_error_log_and_screenshot_save = yes enable_perspective_error_image_save = no use_adb_screenshot = yes use_adb_control = no +combat_screenshot_interval = 1. [Daily] enable_daily_mission = yes diff --git a/module/base/timer.py b/module/base/timer.py index 2ba3b242b..0b47265aa 100644 --- a/module/base/timer.py +++ b/module/base/timer.py @@ -60,5 +60,13 @@ class Timer: def reset(self): self._current = time.time() + def wait(self): + """ + Wait until timer reached. + """ + diff = self._current + self.limit - time.time() + if diff > 0: + time.sleep(diff) + def show(self): logger.info('%s s' % str(self.current())) diff --git a/module/combat/combat.py b/module/combat/combat.py index 1235ae240..54d5a14a0 100644 --- a/module/combat/combat.py +++ b/module/combat/combat.py @@ -179,6 +179,7 @@ class Combat(HPBalancer, UrgentCommissionHandler, EnemySearchingHandler, Retirem self.combat_manual_reset() confirm_timer = Timer(10) confirm_timer.start() + self.device.screenshot_interval_set(self.config.COMBAT_SCREENSHOT_INTERVAL) while 1: self.device.screenshot() @@ -192,7 +193,6 @@ class Combat(HPBalancer, UrgentCommissionHandler, EnemySearchingHandler, Retirem continue if self.handle_combat_manual(): continue - if call_submarine_at_boss: pass else: @@ -201,6 +201,7 @@ class Combat(HPBalancer, UrgentCommissionHandler, EnemySearchingHandler, Retirem # End if self.handle_battle_status(save_get_items=save_get_items): + self.device.screenshot_interval_set(0) break def handle_battle_status(self, save_get_items=False): diff --git a/module/combat/submarine.py b/module/combat/submarine.py index 7f1551966..992c44daa 100644 --- a/module/combat/submarine.py +++ b/module/combat/submarine.py @@ -20,10 +20,11 @@ class SubmarineCall(ModuleBase): Returns: bool: If call. """ - if not self.config.SUBMARINE or self.config.SUBMARINE_MODE in ['do_not_use', 'hunt_only']: - return False if self.submarine_call_flag: return False + if not self.config.SUBMARINE or self.config.SUBMARINE_MODE in ['do_not_use', 'hunt_only']: + self.submarine_call_flag = True + return False if self.submarine_call_timer.reached(): logger.info('Submarine call timer reached') self.submarine_call_flag = True diff --git a/module/config/argparser.py b/module/config/argparser.py index c9527e823..6efcc3167 100644 --- a/module/config/argparser.py +++ b/module/config/argparser.py @@ -224,6 +224,7 @@ def main(ini_name=''): adb = emulator_parser.add_argument_group('ADB设置', '') adb.add_argument('--使用ADB截图', default=default('--使用ADB截图'), choices=['是', '否'], help='建议开启, 能减少CPU占用') adb.add_argument('--使用ADB点击', default=default('--使用ADB点击'), choices=['是', '否'], help='建议关闭, 能加快点击速度') + adb.add_argument('--战斗中截图间隔', default=default('--战斗中截图间隔'), help='战斗中放慢截图速度, 降低CPU使用') # ==========每日任务========== daily_parser = subs.add_parser('每日任务困难演习') diff --git a/module/config/config.py b/module/config/config.py index e71ef10c1..9f24cd2cc 100644 --- a/module/config/config.py +++ b/module/config/config.py @@ -87,6 +87,7 @@ class AzurLaneConfig: SUBMARINE_MODE = '' SUBMARINE_CALL_AT_BOSS = False COMBAT_AUTO_MODE = 'combat_auto' + COMBAT_SCREENSHOT_INTERVAL = 2 """ module.combat.hp_balance @@ -364,6 +365,7 @@ class AzurLaneConfig: self.ENABLE_PERSPECTIVE_ERROR_IMAGE_SAVE = to_bool(option['enable_perspective_error_image_save']) self.USE_ADB_SCREENSHOT = to_bool(option['use_adb_screenshot']) self.USE_ADB_CONTROL = to_bool(option['use_adb_control']) + self.COMBAT_SCREENSHOT_INTERVAL = float(option['combat_screenshot_interval']) option = config['Setting'] # Stop condition diff --git a/module/config/dictionary.py b/module/config/dictionary.py index d9cd781b2..d22199d66 100644 --- a/module/config/dictionary.py +++ b/module/config/dictionary.py @@ -115,6 +115,7 @@ dic_chi_to_eng = { '保存透视识别出错的图像': 'enable_perspective_error_image_save', '使用ADB截图': 'use_adb_screenshot', '使用ADB点击': 'use_adb_control', + '战斗中截图间隔': 'combat_screenshot_interval', '打每日': 'enable_daily_mission', '打困难': 'enable_hard_campaign', '打演习': 'enable_exercise', diff --git a/module/device/screenshot.py b/module/device/screenshot.py index 4b37e5817..16f1598dc 100644 --- a/module/device/screenshot.py +++ b/module/device/screenshot.py @@ -8,11 +8,13 @@ from retrying import retry from module.device.connection import Connection from module.logger import logger +from module.base.timer import Timer class Screenshot(Connection): _screenshot_method = 0 _screenshot_method_fixed = False + _screenshot_interval_timer = Timer(0.1) _adb = False _last_save_time = {} image: Image.Image @@ -54,6 +56,8 @@ class Screenshot(Connection): Returns: PIL.Image.Image: """ + self._screenshot_interval_timer.wait() + self._screenshot_interval_timer.reset() adb = self.config.USE_ADB_SCREENSHOT self._adb = adb @@ -65,7 +69,8 @@ class Screenshot(Connection): self.image.load() if self.config.ENABLE_ERROR_LOG_AND_SCREENSHOT_SAVE: logger.screenshot_deque.append({'time': datetime.now(), 'image': self.image}) - return self.image + + return self.image def save_screenshot(self, genre='items'): """Save a screenshot. Use millisecond timestamp as file name. @@ -92,3 +97,10 @@ class Screenshot(Connection): else: self._last_save_time[genre] = now return False + + def screenshot_interval_set(self, interval): + if interval < 0.1: + interval = 0.1 + if interval != self._screenshot_interval_timer.limit: + logger.info(f'Screenshot interval set to {interval}s') + self._screenshot_interval_timer.limit = interval