mirror of
https://gitee.com/sui-feng-cb/AzurLaneAutoScript1
synced 2026-03-29 02:25:31 +08:00
Opt: Watch user config changes while waiting for new tasks (resolve #1176)
This commit is contained in:
100
alas.py
100
alas.py
@@ -2,7 +2,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import inflection
|
import inflection
|
||||||
from cached_property import cached_property
|
from cached_property import cached_property
|
||||||
@@ -327,57 +327,71 @@ class AzurLaneAutoScript:
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
future (datetime):
|
future (datetime):
|
||||||
"""
|
|
||||||
seconds = future.timestamp() - datetime.now().timestamp() + 1
|
|
||||||
if seconds <= 0:
|
|
||||||
logger.warning(f'Wait until {str(future)}, but sleep length < 0, skip waiting')
|
|
||||||
return
|
|
||||||
|
|
||||||
if self.stop_event is not None:
|
Returns:
|
||||||
self.stop_event.wait(seconds)
|
bool: True if wait finished, False if config changed.
|
||||||
if self.stop_event.is_set():
|
"""
|
||||||
logger.info("Update event detected")
|
future = future + timedelta(seconds=1)
|
||||||
logger.info(f"[{self.config_name}] exited. Reason: Update")
|
self.config.start_watching()
|
||||||
exit(0)
|
while 1:
|
||||||
else:
|
if datetime.now() > future:
|
||||||
time.sleep(seconds)
|
return True
|
||||||
|
if self.stop_event is not None:
|
||||||
|
if self.stop_event.is_set():
|
||||||
|
logger.info("Update event detected")
|
||||||
|
logger.info(f"[{self.config_name}] exited. Reason: Update")
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
if self.config.should_reload():
|
||||||
|
return False
|
||||||
|
|
||||||
def get_next_task(self):
|
def get_next_task(self):
|
||||||
"""
|
"""
|
||||||
Returns:
|
Returns:
|
||||||
str: Name of the next task.
|
str: Name of the next task.
|
||||||
"""
|
"""
|
||||||
|
while 1:
|
||||||
|
task = self.config.get_next()
|
||||||
|
self.config.task = task
|
||||||
|
self.config.bind(task)
|
||||||
|
|
||||||
task = self.config.get_next()
|
from module.base.resource import release_resources
|
||||||
self.config.task = task
|
if self.config.task.command != 'Alas':
|
||||||
self.config.bind(task)
|
release_resources(next_task=task.command)
|
||||||
|
|
||||||
from module.base.resource import release_resources
|
if task.next_run > datetime.now():
|
||||||
if self.config.task.command != 'Alas':
|
logger.info(f'Wait until {task.next_run} for task `{task.command}`')
|
||||||
release_resources(next_task=task.command)
|
method = self.config.Optimization_WhenTaskQueueEmpty
|
||||||
|
if method == 'close_game':
|
||||||
if task.next_run > datetime.now():
|
logger.info('Close game during wait')
|
||||||
logger.info(f'Wait until {task.next_run} for task `{task.command}`')
|
self.device.app_stop()
|
||||||
method = self.config.Optimization_WhenTaskQueueEmpty
|
release_resources()
|
||||||
if method == 'close_game':
|
if not self.wait_until(task.next_run):
|
||||||
logger.info('Close game during wait')
|
del self.__dict__['config']
|
||||||
self.device.app_stop()
|
continue
|
||||||
release_resources()
|
self.run('start')
|
||||||
self.wait_until(task.next_run)
|
elif method == 'goto_main':
|
||||||
self.run('start')
|
logger.info('Goto main page during wait')
|
||||||
elif method == 'goto_main':
|
self.run('goto_main')
|
||||||
logger.info('Goto main page during wait')
|
release_resources()
|
||||||
self.run('goto_main')
|
if not self.wait_until(task.next_run):
|
||||||
release_resources()
|
del self.__dict__['config']
|
||||||
self.wait_until(task.next_run)
|
continue
|
||||||
elif method == 'stay_there':
|
elif method == 'stay_there':
|
||||||
logger.info('Stay there during wait')
|
logger.info('Stay there during wait')
|
||||||
release_resources()
|
release_resources()
|
||||||
self.wait_until(task.next_run)
|
if not self.wait_until(task.next_run):
|
||||||
else:
|
del self.__dict__['config']
|
||||||
logger.warning(f'Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there')
|
continue
|
||||||
release_resources()
|
else:
|
||||||
self.wait_until(task.next_run)
|
logger.warning(f'Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there')
|
||||||
|
release_resources()
|
||||||
|
if not self.wait_until(task.next_run):
|
||||||
|
del self.__dict__['config']
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
|
||||||
AzurLaneConfig.is_hoarding_task = False
|
AzurLaneConfig.is_hoarding_task = False
|
||||||
return task.command
|
return task.command
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from module.base.filter import Filter
|
|||||||
from module.config.config_generated import GeneratedConfig
|
from module.config.config_generated import GeneratedConfig
|
||||||
from module.config.config_manual import ManualConfig, OutputConfig
|
from module.config.config_manual import ManualConfig, OutputConfig
|
||||||
from module.config.config_updater import ConfigUpdater
|
from module.config.config_updater import ConfigUpdater
|
||||||
|
from module.config.watcher import ConfigWatcher
|
||||||
from module.config.utils import *
|
from module.config.utils import *
|
||||||
from module.exception import RequestHumanTakeover, ScriptError
|
from module.exception import RequestHumanTakeover, ScriptError
|
||||||
from module.logger import logger
|
from module.logger import logger
|
||||||
@@ -55,7 +56,7 @@ def name_to_function(name):
|
|||||||
return function
|
return function
|
||||||
|
|
||||||
|
|
||||||
class AzurLaneConfig(ConfigUpdater, ManualConfig, GeneratedConfig):
|
class AzurLaneConfig(ConfigUpdater, ManualConfig, GeneratedConfig, ConfigWatcher):
|
||||||
stop_event: threading.Event = None
|
stop_event: threading.Event = None
|
||||||
bound = {}
|
bound = {}
|
||||||
|
|
||||||
|
|||||||
33
module/config/watcher.py
Normal file
33
module/config/watcher.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from module.config.utils import filepath_config, DEFAULT_TIME
|
||||||
|
from module.logger import logger
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigWatcher:
|
||||||
|
config_name = 'alas'
|
||||||
|
start_mtime = DEFAULT_TIME
|
||||||
|
|
||||||
|
def start_watching(self) -> None:
|
||||||
|
self.start_mtime = self.get_mtime()
|
||||||
|
|
||||||
|
def get_mtime(self) -> datetime:
|
||||||
|
"""
|
||||||
|
Last modify time of the file
|
||||||
|
"""
|
||||||
|
timestamp = os.stat(filepath_config(self.config_name)).st_mtime
|
||||||
|
mtime = datetime.fromtimestamp(timestamp).replace(microsecond=0)
|
||||||
|
return mtime
|
||||||
|
|
||||||
|
def should_reload(self) -> bool:
|
||||||
|
"""
|
||||||
|
Returns:
|
||||||
|
bool: Whether the file has been modified and configs should reload
|
||||||
|
"""
|
||||||
|
mtime = self.get_mtime()
|
||||||
|
if mtime > self.start_mtime:
|
||||||
|
logger.info(f'Config "{self.config_name}" changed at {mtime}')
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
Reference in New Issue
Block a user