Optimize: 战斗中放慢截图速度, 降低CPU使用

This commit is contained in:
LmeSzinc 2020-04-27 23:59:51 +08:00
parent 9dc69c7fac
commit ef5885767e
8 changed files with 31 additions and 4 deletions

View File

@ -88,6 +88,7 @@ enable_error_log_and_screenshot_save = yes
enable_perspective_error_image_save = no enable_perspective_error_image_save = no
use_adb_screenshot = yes use_adb_screenshot = yes
use_adb_control = no use_adb_control = no
combat_screenshot_interval = 1.
[Daily] [Daily]
enable_daily_mission = yes enable_daily_mission = yes

View File

@ -60,5 +60,13 @@ class Timer:
def reset(self): def reset(self):
self._current = time.time() 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): def show(self):
logger.info('%s s' % str(self.current())) logger.info('%s s' % str(self.current()))

View File

@ -179,6 +179,7 @@ class Combat(HPBalancer, UrgentCommissionHandler, EnemySearchingHandler, Retirem
self.combat_manual_reset() self.combat_manual_reset()
confirm_timer = Timer(10) confirm_timer = Timer(10)
confirm_timer.start() confirm_timer.start()
self.device.screenshot_interval_set(self.config.COMBAT_SCREENSHOT_INTERVAL)
while 1: while 1:
self.device.screenshot() self.device.screenshot()
@ -192,7 +193,6 @@ class Combat(HPBalancer, UrgentCommissionHandler, EnemySearchingHandler, Retirem
continue continue
if self.handle_combat_manual(): if self.handle_combat_manual():
continue continue
if call_submarine_at_boss: if call_submarine_at_boss:
pass pass
else: else:
@ -201,6 +201,7 @@ class Combat(HPBalancer, UrgentCommissionHandler, EnemySearchingHandler, Retirem
# End # End
if self.handle_battle_status(save_get_items=save_get_items): if self.handle_battle_status(save_get_items=save_get_items):
self.device.screenshot_interval_set(0)
break break
def handle_battle_status(self, save_get_items=False): def handle_battle_status(self, save_get_items=False):

View File

@ -20,10 +20,11 @@ class SubmarineCall(ModuleBase):
Returns: Returns:
bool: If call. 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: if self.submarine_call_flag:
return False 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(): if self.submarine_call_timer.reached():
logger.info('Submarine call timer reached') logger.info('Submarine call timer reached')
self.submarine_call_flag = True self.submarine_call_flag = True

View File

@ -224,6 +224,7 @@ def main(ini_name=''):
adb = emulator_parser.add_argument_group('ADB设置', '') 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='建议开启, 能减少CPU占用')
adb.add_argument('--使用ADB点击', default=default('--使用ADB点击'), choices=['', ''], help='建议关闭, 能加快点击速度') adb.add_argument('--使用ADB点击', default=default('--使用ADB点击'), choices=['', ''], help='建议关闭, 能加快点击速度')
adb.add_argument('--战斗中截图间隔', default=default('--战斗中截图间隔'), help='战斗中放慢截图速度, 降低CPU使用')
# ==========每日任务========== # ==========每日任务==========
daily_parser = subs.add_parser('每日任务困难演习') daily_parser = subs.add_parser('每日任务困难演习')

View File

@ -87,6 +87,7 @@ class AzurLaneConfig:
SUBMARINE_MODE = '' SUBMARINE_MODE = ''
SUBMARINE_CALL_AT_BOSS = False SUBMARINE_CALL_AT_BOSS = False
COMBAT_AUTO_MODE = 'combat_auto' COMBAT_AUTO_MODE = 'combat_auto'
COMBAT_SCREENSHOT_INTERVAL = 2
""" """
module.combat.hp_balance 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.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_SCREENSHOT = to_bool(option['use_adb_screenshot'])
self.USE_ADB_CONTROL = to_bool(option['use_adb_control']) self.USE_ADB_CONTROL = to_bool(option['use_adb_control'])
self.COMBAT_SCREENSHOT_INTERVAL = float(option['combat_screenshot_interval'])
option = config['Setting'] option = config['Setting']
# Stop condition # Stop condition

View File

@ -115,6 +115,7 @@ dic_chi_to_eng = {
'保存透视识别出错的图像': 'enable_perspective_error_image_save', '保存透视识别出错的图像': 'enable_perspective_error_image_save',
'使用ADB截图': 'use_adb_screenshot', '使用ADB截图': 'use_adb_screenshot',
'使用ADB点击': 'use_adb_control', '使用ADB点击': 'use_adb_control',
'战斗中截图间隔': 'combat_screenshot_interval',
'打每日': 'enable_daily_mission', '打每日': 'enable_daily_mission',
'打困难': 'enable_hard_campaign', '打困难': 'enable_hard_campaign',
'打演习': 'enable_exercise', '打演习': 'enable_exercise',

View File

@ -8,11 +8,13 @@ from retrying import retry
from module.device.connection import Connection from module.device.connection import Connection
from module.logger import logger from module.logger import logger
from module.base.timer import Timer
class Screenshot(Connection): class Screenshot(Connection):
_screenshot_method = 0 _screenshot_method = 0
_screenshot_method_fixed = False _screenshot_method_fixed = False
_screenshot_interval_timer = Timer(0.1)
_adb = False _adb = False
_last_save_time = {} _last_save_time = {}
image: Image.Image image: Image.Image
@ -54,6 +56,8 @@ class Screenshot(Connection):
Returns: Returns:
PIL.Image.Image: PIL.Image.Image:
""" """
self._screenshot_interval_timer.wait()
self._screenshot_interval_timer.reset()
adb = self.config.USE_ADB_SCREENSHOT adb = self.config.USE_ADB_SCREENSHOT
self._adb = adb self._adb = adb
@ -65,7 +69,8 @@ class Screenshot(Connection):
self.image.load() self.image.load()
if self.config.ENABLE_ERROR_LOG_AND_SCREENSHOT_SAVE: if self.config.ENABLE_ERROR_LOG_AND_SCREENSHOT_SAVE:
logger.screenshot_deque.append({'time': datetime.now(), 'image': self.image}) logger.screenshot_deque.append({'time': datetime.now(), 'image': self.image})
return self.image
return self.image
def save_screenshot(self, genre='items'): def save_screenshot(self, genre='items'):
"""Save a screenshot. Use millisecond timestamp as file name. """Save a screenshot. Use millisecond timestamp as file name.
@ -92,3 +97,10 @@ class Screenshot(Connection):
else: else:
self._last_save_time[genre] = now self._last_save_time[genre] = now
return False 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