StarRailCopilot/module/base/decorator.py
LmeSzinc 91e83271a4 Add: 增加了使用一键退役的支持
- 增加了根据设置选择切换函数的装饰器
- 增加了ADB截图和点击的设置, 默认使用ADB截图, uiautomator2点击
- 更新了assets.py文件, 上次commit忘记了
2020-04-14 12:26:57 +08:00

69 lines
1.8 KiB
Python

from functools import wraps
import numpy as np
from module.logger import logger
class Config:
"""
Decorator that calls different function with a same name according to config.
func_list likes:
func_list = {
'func1': [
{'options': {'ENABLE': True}, 'func': 1},
{'options': {'ENABLE': False}, 'func': 1}
]
}
"""
func_list = {}
@classmethod
def when(cls, **kwargs):
"""
Args:
**kwargs: Any option in AzurLaneConfig.
Examples:
@Config.when(USE_ONE_CLICK_RETIREMENT=True)
def retire_ships(self, amount=None, rarity=None):
pass
@Config.when(USE_ONE_CLICK_RETIREMENT=False)
def retire_ships(self, amount=None, rarity=None):
pass
"""
options = kwargs
def decorate(func):
name = func.__name__
data = {'options': options, 'func': func}
if name not in cls.func_list:
cls.func_list[name] = [data]
else:
cls.func_list[name].append(data)
@wraps(func)
def wrapper(self, *args, **kwargs):
"""
Args:
self: ModuleBase instance.
*args:
**kwargs:
"""
for record in cls.func_list[name]:
flag = [self.config.__getattribute__(key) == value for key, value in record['options'].items()]
if not np.all(flag):
continue
return record['func'](self, *args, **kwargs)
logger.warning(f'No option fits for {name}, using the last define func.')
return func(self, *args, **kwargs)
return wrapper
return decorate