|
@@ -4,7 +4,8 @@ import time
|
|
|
|
|
|
|
|
from functools import wraps
|
|
from functools import wraps
|
|
|
from dataclasses import dataclass
|
|
from dataclasses import dataclass
|
|
|
-from typing import TypeVar, ParamSpec
|
|
|
|
|
|
|
+from typing import TypeVar, ParamSpec, Callable
|
|
|
|
|
+from enum import Enum, auto
|
|
|
|
|
|
|
|
from module.logger import logger
|
|
from module.logger import logger
|
|
|
from module.base.timer import Timer
|
|
from module.base.timer import Timer
|
|
@@ -16,6 +17,11 @@ P = ParamSpec('P')
|
|
|
T = TypeVar('T')
|
|
T = TypeVar('T')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+class HookSignal(Enum):
|
|
|
|
|
+ CONTINUE = auto() # 无事发生
|
|
|
|
|
+ BUSY = auto() # 处理了,继续
|
|
|
|
|
+ DONE = auto() # 流程结束
|
|
|
|
|
+
|
|
|
@dataclass
|
|
@dataclass
|
|
|
class BattleWaitContext:
|
|
class BattleWaitContext:
|
|
|
completion = False
|
|
completion = False
|
|
@@ -26,34 +32,86 @@ class BattleWaitContext:
|
|
|
return self.__dict__[name]
|
|
return self.__dict__[name]
|
|
|
return None
|
|
return None
|
|
|
|
|
|
|
|
-@dataclass
|
|
|
|
|
class BattleWaitPlan:
|
|
class BattleWaitPlan:
|
|
|
"""
|
|
"""
|
|
|
就是一个超级大的状态机,这里拆成了很多hook
|
|
就是一个超级大的状态机,这里拆成了很多hook
|
|
|
"""
|
|
"""
|
|
|
- HOOK_NAMES = ('setup', 'completion', 'interrupt', 'success', 'failure', 'reserve', 'idle')
|
|
|
|
|
- SEQUENCE_DEFAULT = 'completion > interrupt > success > failure > reserve > idle'
|
|
|
|
|
- SETUP_DEFAULT = 'default'
|
|
|
|
|
-
|
|
|
|
|
|
|
+ HOOKS_DEFAULT = ('setup', 'completion', 'interrupt', 'success', 'failure', 'idle')
|
|
|
|
|
+ SEQUENCE_DEFAULT = 'completion > interrupt > success > failure >'
|
|
|
|
|
|
|
|
def __init__(self, *arg, **kwargs):
|
|
def __init__(self, *arg, **kwargs):
|
|
|
- self.sequence = kwargs.get('sequence', BattleWaitPlan.SEQUENCE_DEFAULT)
|
|
|
|
|
|
|
+ extra_sequence: str = ''
|
|
|
|
|
+ #
|
|
|
|
|
+ for event_strategy in arg:
|
|
|
|
|
+ if not isinstance(event_strategy, str):
|
|
|
|
|
+ raise TypeError(
|
|
|
|
|
+ 'Battle wait hook must be a string in '
|
|
|
|
|
+ '"event_strategy" format; '
|
|
|
|
|
+ f'got {event_strategy!r}'
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ if not event_strategy:
|
|
|
|
|
+ raise ValueError(
|
|
|
|
|
+ 'Battle wait hook cannot be empty; '
|
|
|
|
|
+ 'expected "event_strategy" format'
|
|
|
|
|
+ )
|
|
|
|
|
+ event, separator, strategy = event_strategy.partition('_')
|
|
|
|
|
+ if not separator or not event or not strategy:
|
|
|
|
|
+ raise ValueError(
|
|
|
|
|
+ 'Invalid battle wait hook '
|
|
|
|
|
+ f'{event_strategy!r}; expected "event_strategy" format'
|
|
|
|
|
+ )
|
|
|
|
|
+ setattr(self, event, strategy)
|
|
|
|
|
+ if event not in self.HOOKS_DEFAULT:
|
|
|
|
|
+ extra_sequence += f'{event} > '
|
|
|
|
|
+
|
|
|
|
|
+ #
|
|
|
|
|
+ for event, strategy in kwargs.items():
|
|
|
|
|
+ if not event or not isinstance(event, str) or not isinstance(strategy, str):
|
|
|
|
|
+ raise TypeError(f'{event} or {strategy} is not a string')
|
|
|
|
|
+ if getattr(self, event, None) is not None:
|
|
|
|
|
+ raise ValueError(
|
|
|
|
|
+ f'Battle wait event {event!r} was configured more than once'
|
|
|
|
|
+ )
|
|
|
|
|
+ setattr(self, event, strategy)
|
|
|
|
|
+ if event not in self.HOOKS_DEFAULT:
|
|
|
|
|
+ extra_sequence += f'{event} > '
|
|
|
|
|
+
|
|
|
|
|
+ # 补齐默认的
|
|
|
|
|
+ for event in self.HOOKS_DEFAULT:
|
|
|
|
|
+ if getattr(self, event, None) is None:
|
|
|
|
|
+ setattr(self, event, 'default')
|
|
|
|
|
+
|
|
|
|
|
+ #
|
|
|
|
|
+ self.sequence = kwargs.get('sequence', None)
|
|
|
|
|
+ if self.sequence is None:
|
|
|
|
|
+ self.sequence = f'{BattleWaitPlan.SEQUENCE_DEFAULT} {extra_sequence} idle'
|
|
|
if not isinstance(self.sequence, str):
|
|
if not isinstance(self.sequence, str):
|
|
|
- raise TypeError(f'{self.sequence} is not a string')
|
|
|
|
|
|
|
+ raise TypeError(
|
|
|
|
|
+ 'Battle wait sequence must be a string containing event names '
|
|
|
|
|
+ "separated by '>'; "
|
|
|
|
|
+ f'got {self.sequence!r}'
|
|
|
|
|
+ )
|
|
|
self.sequence: str = self.sequence.replace(' ', '').replace('\n', '').replace('\r', '').replace('setup', '')
|
|
self.sequence: str = self.sequence.replace(' ', '').replace('\n', '').replace('\r', '').replace('setup', '')
|
|
|
-
|
|
|
|
|
- self.setup = kwargs.get('setup', BattleWaitPlan.SETUP_DEFAULT)
|
|
|
|
|
- if not isinstance(self.setup, str):
|
|
|
|
|
- raise TypeError(f'{self.setup} is not a string')
|
|
|
|
|
- for step in self.sequence.split('>'):
|
|
|
|
|
- if step and step not in self.HOOK_NAMES:
|
|
|
|
|
|
|
+ for event_name in self.sequence.split('>'):
|
|
|
|
|
+ event_name = event_name.strip()
|
|
|
|
|
+ if event_name == "sequence":
|
|
|
continue
|
|
continue
|
|
|
- value = kwargs.get(step, 'default')
|
|
|
|
|
- setattr(self, step, value)
|
|
|
|
|
|
|
+ if getattr(self, event_name, None) is None:
|
|
|
|
|
+ raise ValueError(
|
|
|
|
|
+ f'Unknown battle wait event {event_name!r} in '
|
|
|
|
|
+ f'sequence {self.sequence!r}'
|
|
|
|
|
+ )
|
|
|
|
|
+ for event in self.HOOKS_DEFAULT:
|
|
|
|
|
+ if event != 'setup' and event not in self.sequence:
|
|
|
|
|
+ raise ValueError(
|
|
|
|
|
+ f'Battle wait sequence is missing required event {event!r}; '
|
|
|
|
|
+ f'got {self.sequence!r}'
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
def __str__(self):
|
|
def __str__(self):
|
|
|
lines = [f"{self.__class__.__name__}:", f" sequence: {self.sequence}", " hooks:"]
|
|
lines = [f"{self.__class__.__name__}:", f" sequence: {self.sequence}", " hooks:"]
|
|
|
- for event_name in self.HOOK_NAMES:
|
|
|
|
|
|
|
+ for event_name in self.HOOKS_DEFAULT:
|
|
|
strategy_name = getattr(self, event_name, None)
|
|
strategy_name = getattr(self, event_name, None)
|
|
|
if callable(strategy_name):
|
|
if callable(strategy_name):
|
|
|
if hasattr(strategy_name, '__name__'):
|
|
if hasattr(strategy_name, '__name__'):
|
|
@@ -71,6 +129,8 @@ class BattleWaitPlan:
|
|
|
|
|
|
|
|
@property
|
|
@property
|
|
|
def function_setup_name(self) -> str:
|
|
def function_setup_name(self) -> str:
|
|
|
|
|
+ if getattr(self, 'setup', None) is None:
|
|
|
|
|
+ self.setup = 'default'
|
|
|
return f'_bw_setup_{self.setup}'
|
|
return f'_bw_setup_{self.setup}'
|
|
|
|
|
|
|
|
def sequence_function_names(self) -> list[str]:
|
|
def sequence_function_names(self) -> list[str]:
|
|
@@ -79,8 +139,6 @@ class BattleWaitPlan:
|
|
|
event_name = event_name.strip()
|
|
event_name = event_name.strip()
|
|
|
if not event_name:
|
|
if not event_name:
|
|
|
continue
|
|
continue
|
|
|
- if event_name not in self.HOOK_NAMES:
|
|
|
|
|
- continue
|
|
|
|
|
|
|
|
|
|
if not hasattr(self, event_name):
|
|
if not hasattr(self, event_name):
|
|
|
raise ValueError(f'Unknown battle wait event: {event_name}')
|
|
raise ValueError(f'Unknown battle wait event: {event_name}')
|
|
@@ -95,6 +153,37 @@ class BattleWaitPlan:
|
|
|
function_names.append(f'_bw_{event_name}_{strategy_name}')
|
|
function_names.append(f'_bw_{event_name}_{strategy_name}')
|
|
|
return function_names
|
|
return function_names
|
|
|
|
|
|
|
|
|
|
+ def override(self, *arg, **kwargs):
|
|
|
|
|
+ if kwargs:
|
|
|
|
|
+ raise TypeError(
|
|
|
|
|
+ 'sequence is not supported' if 'sequence' in kwargs else f'Unexpected keyword arguments: {tuple(kwargs)}')
|
|
|
|
|
+ if not arg:
|
|
|
|
|
+ return self
|
|
|
|
|
+
|
|
|
|
|
+ events = [event for event in self.sequence.split('>') if event]
|
|
|
|
|
+ if 'idle' not in events:
|
|
|
|
|
+ raise ValueError(f'Sequence {self.sequence!r} must contain "idle"')
|
|
|
|
|
+ idle_index = events.index('idle')
|
|
|
|
|
+
|
|
|
|
|
+ for event_strategy in arg:
|
|
|
|
|
+ if not isinstance(event_strategy, str):
|
|
|
|
|
+ raise TypeError(f'event_strategy must be a string, got {event_strategy!r}')
|
|
|
|
|
+
|
|
|
|
|
+ event, separator, strategy = event_strategy.partition('_')
|
|
|
|
|
+ if not separator or not event or not strategy:
|
|
|
|
|
+ raise ValueError(f'Invalid event_strategy: {event_strategy!r}')
|
|
|
|
|
+ if event == 'sequence':
|
|
|
|
|
+ raise ValueError('sequence cannot be overridden')
|
|
|
|
|
+
|
|
|
|
|
+ is_new = not hasattr(self, event)
|
|
|
|
|
+ setattr(self, event, strategy)
|
|
|
|
|
+ if is_new:
|
|
|
|
|
+ events.insert(idle_index, event)
|
|
|
|
|
+ idle_index += 1
|
|
|
|
|
+
|
|
|
|
|
+ self.sequence = '>'.join(events)
|
|
|
|
|
+ return self
|
|
|
|
|
+
|
|
|
class battle_wait_strategy:
|
|
class battle_wait_strategy:
|
|
|
"""
|
|
"""
|
|
|
使用with 上下文临时修改 battle_wait_plan
|
|
使用with 上下文临时修改 battle_wait_plan
|
|
@@ -150,9 +239,21 @@ class battle_wait_strategy:
|
|
|
def _recreate_cm(self):
|
|
def _recreate_cm(self):
|
|
|
return self
|
|
return self
|
|
|
|
|
|
|
|
- def __call__(self, func):
|
|
|
|
|
|
|
+ def __call__(self, func: Callable[P, T]) -> Callable[P, T]:
|
|
|
@wraps(func)
|
|
@wraps(func)
|
|
|
- def inner(owner, *args, **kwargs):
|
|
|
|
|
|
|
+ def inner(owner, *args: P.args, **kwargs: P.kwargs) -> T:
|
|
|
|
|
+ # 兼容性处理
|
|
|
|
|
+ #-----------------------------------------------------------------------
|
|
|
|
|
+ if kwargs:
|
|
|
|
|
+ override_kwargs = {}
|
|
|
|
|
+ override_args = list()
|
|
|
|
|
+ for key, value in kwargs.items():
|
|
|
|
|
+ if key == 'random_click_swipt_enable' and value:
|
|
|
|
|
+ override_kwargs['randomclick'] = 'default'
|
|
|
|
|
+ override_args.append('randomclick_default')
|
|
|
|
|
+ battle_wait_strategy.battle_wait_plan =\
|
|
|
|
|
+ battle_wait_strategy.battle_wait_plan.override(*override_args)
|
|
|
|
|
+ # -----------------------------------------------------------------------
|
|
|
# kwargs.setdefault(
|
|
# kwargs.setdefault(
|
|
|
# 'battle_wait_plan',
|
|
# 'battle_wait_plan',
|
|
|
# self.battle_wait_plan,
|
|
# self.battle_wait_plan,
|
|
@@ -163,72 +264,77 @@ class battle_wait_strategy:
|
|
|
|
|
|
|
|
|
|
|
|
|
class BattleWait(BaseTask):
|
|
class BattleWait(BaseTask):
|
|
|
- def _bw_setup_default(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
|
|
+ # build in
|
|
|
|
|
+ # ------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
+ def _bw_setup_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
self.C_REWARD_1.name = 'C_REWARD'
|
|
self.C_REWARD_1.name = 'C_REWARD'
|
|
|
self.C_REWARD_2.name = 'C_REWARD'
|
|
self.C_REWARD_2.name = 'C_REWARD'
|
|
|
self.C_REWARD_3.name = 'C_REWARD'
|
|
self.C_REWARD_3.name = 'C_REWARD'
|
|
|
self.device.stuck_record_add('BATTLE_STATUS_S')
|
|
self.device.stuck_record_add('BATTLE_STATUS_S')
|
|
|
self.device.click_record_clear()
|
|
self.device.click_record_clear()
|
|
|
logger.info('Start battle process')
|
|
logger.info('Start battle process')
|
|
|
|
|
+ return HookSignal.DONE
|
|
|
|
|
|
|
|
- def _bw_completion_default(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
|
|
+ def _bw_completion_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
if bw_ctx.completion:
|
|
if bw_ctx.completion:
|
|
|
logger.info('Battle done')
|
|
logger.info('Battle done')
|
|
|
- return True
|
|
|
|
|
- return False
|
|
|
|
|
|
|
+ return HookSignal.DONE
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+
|
|
|
|
|
+ def _bw_interrupt_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
|
|
|
- def _bw_interrupt_default(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
- pass
|
|
|
|
|
- def _bw_success_default(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
|
|
+ def _bw_success_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
if self.appear_then_click(self.I_WIN, interval=0.8):
|
|
if self.appear_then_click(self.I_WIN, interval=0.8):
|
|
|
- return False
|
|
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
appear_ghost, appear_reward, appear_gold = (
|
|
appear_ghost, appear_reward, appear_gold = (
|
|
|
self.appear(self.I_GREED_GHOST),
|
|
self.appear(self.I_GREED_GHOST),
|
|
|
self.appear(self.I_REWARD),
|
|
self.appear(self.I_REWARD),
|
|
|
self.appear(self.I_REWARD_GOLD)
|
|
self.appear(self.I_REWARD_GOLD)
|
|
|
)
|
|
)
|
|
|
- if appear_ghost or appear_reward or appear_gold:
|
|
|
|
|
- logger.info('Win battle')
|
|
|
|
|
- timer = Timer(20).start()
|
|
|
|
|
- while 1:
|
|
|
|
|
- self.screenshot()
|
|
|
|
|
-
|
|
|
|
|
- _appear_ghost, _appear_reward, _appear_gold = (
|
|
|
|
|
- self.appear(self.I_GREED_GHOST, threshold=0.6),
|
|
|
|
|
- self.appear(self.I_REWARD),
|
|
|
|
|
- self.appear(self.I_REWARD_GOLD)
|
|
|
|
|
- )
|
|
|
|
|
- # logger.info(f'_appear_ghost: {_appear_ghost} _appear_reward: {_appear_reward} _appear_gold: {_appear_gold}')
|
|
|
|
|
- if any([_appear_ghost, _appear_reward, _appear_gold]):
|
|
|
|
|
- action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
|
|
|
|
|
- self.click(action_click, interval=1.5)
|
|
|
|
|
- else:
|
|
|
|
|
- logger.info('Battle done')
|
|
|
|
|
- bw_ctx.success = True
|
|
|
|
|
- bw_ctx.completion = True
|
|
|
|
|
- return True
|
|
|
|
|
- if timer.reached_and_reset():
|
|
|
|
|
- logger.warning('battle ')
|
|
|
|
|
- break
|
|
|
|
|
- return False
|
|
|
|
|
- return False
|
|
|
|
|
|
|
+ if not any([appear_ghost, appear_reward, appear_gold]):
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+ logger.info('Win battle')
|
|
|
|
|
+ timer = Timer(20).start()
|
|
|
|
|
+ while 1:
|
|
|
|
|
+ self.screenshot()
|
|
|
|
|
+
|
|
|
|
|
+ _appear_ghost, _appear_reward, _appear_gold = (
|
|
|
|
|
+ self.appear(self.I_GREED_GHOST, threshold=0.6),
|
|
|
|
|
+ self.appear(self.I_REWARD),
|
|
|
|
|
+ self.appear(self.I_REWARD_GOLD)
|
|
|
|
|
+ )
|
|
|
|
|
+ # logger.info(f'_appear_ghost: {_appear_ghost} _appear_reward: {_appear_reward} _appear_gold: {_appear_gold}')
|
|
|
|
|
+ if any([_appear_ghost, _appear_reward, _appear_gold]):
|
|
|
|
|
+ action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
|
|
|
|
|
+ self.click(action_click, interval=1.5)
|
|
|
|
|
+ else:
|
|
|
|
|
+ logger.info('Get all reward')
|
|
|
|
|
+ bw_ctx.success = True
|
|
|
|
|
+ bw_ctx.completion = True
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+ if timer.reached_and_reset():
|
|
|
|
|
+ logger.warning('battle ')
|
|
|
|
|
+ break
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
|
|
|
- def _bw_failure_default(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
|
|
+
|
|
|
|
|
+ def _bw_failure_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
if self.appear(self.I_FALSE, threshold=0.8):
|
|
if self.appear(self.I_FALSE, threshold=0.8):
|
|
|
logger.warning('False battle')
|
|
logger.warning('False battle')
|
|
|
self.ui_click_until_disappear(self.I_FALSE)
|
|
self.ui_click_until_disappear(self.I_FALSE)
|
|
|
bw_ctx.completion = True
|
|
bw_ctx.completion = True
|
|
|
return True
|
|
return True
|
|
|
return True
|
|
return True
|
|
|
- def _bw_idle_default(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
- pass
|
|
|
|
|
- def _bw_reserve_default(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
- pass
|
|
|
|
|
|
|
|
|
|
- # idle
|
|
|
|
|
- # ------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
- def _bw_idle_random_click(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
- if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
|
|
|
|
|
|
|
+ def _bw_idle_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+
|
|
|
|
|
+ def _bw_reserve_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+
|
|
|
|
|
+ def _bw_randomclick_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
|
|
+ if 0 <= random.randint(0, 500) <= 20: # 百分之4的概率
|
|
|
rand_type = random.randint(0, 2)
|
|
rand_type = random.randint(0, 2)
|
|
|
match rand_type:
|
|
match rand_type:
|
|
|
case 0:
|
|
case 0:
|
|
@@ -241,9 +347,104 @@ class BattleWait(BaseTask):
|
|
|
# self.device.stuck_record_add('BATTLE_STATUS_S')
|
|
# self.device.stuck_record_add('BATTLE_STATUS_S')
|
|
|
else:
|
|
else:
|
|
|
time.sleep(0.4) # 这样的好像不对
|
|
time.sleep(0.4) # 这样的好像不对
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
|
|
|
|
|
+ # custom
|
|
|
# ------------------------------------------------------------------------------------------------------------------
|
|
# ------------------------------------------------------------------------------------------------------------------
|
|
|
- def battle_wait_v3(self, *args, **kwargs) -> bool:
|
|
|
|
|
|
|
+ def _bw_success_soul(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
|
|
+ if self.appear_then_click(self.I_WIN, interval=0.8):
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+ appear_ghost, appear_reward, appear_gold, appear_skin = (
|
|
|
|
|
+ self.appear(self.I_GREED_GHOST),
|
|
|
|
|
+ self.appear(self.I_REWARD),
|
|
|
|
|
+ self.appear(self.I_REWARD_GOLD),
|
|
|
|
|
+ self.appear(self.I_REWARD_GOLD_SNAKE_SKIN)
|
|
|
|
|
+ )
|
|
|
|
|
+ if not any([appear_ghost, appear_reward, appear_gold, appear_skin]):
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+ logger.info('Win battle')
|
|
|
|
|
+ timer = Timer(20).start()
|
|
|
|
|
+ while 1:
|
|
|
|
|
+ self.screenshot()
|
|
|
|
|
+
|
|
|
|
|
+ _appear_ghost, _appear_reward, _appear_gold, _appear_skin = (
|
|
|
|
|
+ self.appear(self.I_GREED_GHOST, threshold=0.6),
|
|
|
|
|
+ self.appear(self.I_REWARD),
|
|
|
|
|
+ self.appear(self.I_REWARD_GOLD),
|
|
|
|
|
+ self.appear(self.I_REWARD_GOLD_SNAKE_SKIN)
|
|
|
|
|
+ )
|
|
|
|
|
+ # logger.info(f'_appear_ghost: {_appear_ghost} _appear_reward: {_appear_reward} _appear_gold: {_appear_gold} _appear_skin: {_appear_skin}')
|
|
|
|
|
+ if any([_appear_ghost, _appear_reward, _appear_gold, _appear_skin]):
|
|
|
|
|
+ action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
|
|
|
|
|
+ self.click(action_click, interval=1.5)
|
|
|
|
|
+ else:
|
|
|
|
|
+ logger.info('Get all reward')
|
|
|
|
|
+ bw_ctx.success = True
|
|
|
|
|
+ bw_ctx.completion = True
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+ if timer.reached_and_reset():
|
|
|
|
|
+ logger.warning('battle ')
|
|
|
|
|
+ break
|
|
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ # def _bw_idle_random_click(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
+ # if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
|
|
|
|
|
+ # rand_type = random.randint(0, 2)
|
|
|
|
|
+ # match rand_type:
|
|
|
|
|
+ # case 0:
|
|
|
|
|
+ # self.click(self.C_RANDOM_CLICK, interval=20)
|
|
|
|
|
+ # case 1:
|
|
|
|
|
+ # self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
|
|
|
|
|
+ # case 2:
|
|
|
|
|
+ # self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
|
|
|
|
|
+ # # 重新设置为长战斗
|
|
|
|
|
+ # # self.device.stuck_record_add('BATTLE_STATUS_S')
|
|
|
|
|
+ # else:
|
|
|
|
|
+ # time.sleep(0.4) # 这样的好像不对
|
|
|
|
|
+ #
|
|
|
|
|
+ # def _bw_random_click(self, bw_ctx: BattleWaitContext):
|
|
|
|
|
+ # if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
|
|
|
|
|
+ # rand_type = random.randint(0, 2)
|
|
|
|
|
+ # match rand_type:
|
|
|
|
|
+ # case 0:
|
|
|
|
|
+ # self.click(self.C_RANDOM_CLICK, interval=20)
|
|
|
|
|
+ # case 1:
|
|
|
|
|
+ # self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
|
|
|
|
|
+ # case 2:
|
|
|
|
|
+ # self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
|
|
|
|
|
+ # # 重新设置为长战斗
|
|
|
|
|
+ # # self.device.stuck_record_add('BATTLE_STATUS_S')
|
|
|
|
|
+ # else:
|
|
|
|
|
+ # time.sleep(0.4) # 这样的好像不对
|
|
|
|
|
+
|
|
|
|
|
+ # ------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
+ def battle_wait_with_strategy(self, *args, **kwargs) -> bool:
|
|
|
|
|
+ """
|
|
|
|
|
+ 三种自定义配置方法:
|
|
|
|
|
+ 1. 使用装饰器battle_wait_strategy
|
|
|
|
|
+ @battle_wait_strategy( 'reserve_default', 'idle_default', failure='default')
|
|
|
|
|
+ def battle_wait(self, *args, **kwargs):
|
|
|
|
|
+ return self.battle_wait_with_strategy(*args, **kwargs)
|
|
|
|
|
+ 2. 使用 with 上下文 (!在1基础上)
|
|
|
|
|
+ with battle_wait_strategy('reserve_default'):
|
|
|
|
|
+ test_battle_wait.battle_wait()
|
|
|
|
|
+ 3. 调用时动态传参 (!在2基础上)
|
|
|
|
|
+ obj.battle_wait(random_click_swipt_enable=1) # 详细参数看 battle_wait_strategy.__call__()
|
|
|
|
|
+
|
|
|
|
|
+ 理解 event + strategy 概念: 把战斗过程抽象为一系列触发事件以及对应的实现函数,也称hook。
|
|
|
|
|
+ 默认定义的 hook 有 BattleWaitPlan.HOOKS_DEFAULT = ('setup', 'completion', 'interrupt', 'success', 'failure', 'idle')
|
|
|
|
|
+ 这些 hook 跑在一个 while 里面,默认的调用顺序 BattleWaitPlan.SEQUENCE_DEFAULT = 'completion > interrupt > success > failure > idle'
|
|
|
|
|
+ setup 没有跑在 while里面 而是在 while之前
|
|
|
|
|
+
|
|
|
|
|
+ event + strategy 拼成了一个hook, 一个 event 在一次战斗过程中只能挂载一个 strategy】
|
|
|
|
|
+ 自定义hook就是字符串拼起来: battle_wait_strategy的入参可以有 ‘event_strategy’ 或者 'event=strategy'
|
|
|
|
|
+ 可以添加任意 event 以及其对应的 strategy。比如 ‘yyy_default’ 'abcd_edf'
|
|
|
|
|
+ 但是必须要实现对应的hook 上面的比如 _bw_yyy_default() 以及 _bw_abcd_edf()
|
|
|
|
|
+
|
|
|
|
|
+ hook 可以自定义顺序,比如 battle_wait_strategy(sequence='completion > interrupt > success > failure > idle')
|
|
|
|
|
+ 如果指定sequence, 新增的event会按照传参时候从左到右排序,左边高优先级,新增的会插入到 failure 和 idle 之间
|
|
|
|
|
+ """
|
|
|
battle_wait_plan = kwargs.get('battle_wait_plan')
|
|
battle_wait_plan = kwargs.get('battle_wait_plan')
|
|
|
if battle_wait_plan is None:
|
|
if battle_wait_plan is None:
|
|
|
battle_wait_plan = BattleWaitPlan()
|
|
battle_wait_plan = BattleWaitPlan()
|
|
@@ -259,9 +460,11 @@ class BattleWait(BaseTask):
|
|
|
self.screenshot()
|
|
self.screenshot()
|
|
|
for handler in handlers:
|
|
for handler in handlers:
|
|
|
result = handler(bw_ctx)
|
|
result = handler(bw_ctx)
|
|
|
- if handler.__name__ == '_bw_completion_default' and result:
|
|
|
|
|
|
|
+ if handler.__name__.startswith('_bw_completion') and result == HookSignal.DONE:
|
|
|
return True
|
|
return True
|
|
|
|
|
|
|
|
|
|
+ # bw_ctx.completion = True
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
@@ -276,13 +479,13 @@ if __name__ == '__main__':
|
|
|
def _bw_settlement(self):
|
|
def _bw_settlement(self):
|
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
- @battle_wait_strategy(idle='default', failure='default', sequence='idle > reserve> yyy > failure > completion')
|
|
|
|
|
|
|
+ @battle_wait_strategy( 'reserve_default', 'idle_default', failure='default')
|
|
|
def battle_wait(self, *args, **kwargs):
|
|
def battle_wait(self, *args, **kwargs):
|
|
|
- return self.battle_wait_v3(*args, **kwargs)
|
|
|
|
|
|
|
+ return self.battle_wait_with_strategy(*args, **kwargs)
|
|
|
|
|
|
|
|
test_battle_wait = TestBattleWait(c,d)
|
|
test_battle_wait = TestBattleWait(c,d)
|
|
|
test_battle_wait.battle_wait(random_click_swipt_enable=1)
|
|
test_battle_wait.battle_wait(random_click_swipt_enable=1)
|
|
|
- with battle_wait_strategy(sequence='idle > failure'):
|
|
|
|
|
|
|
+ with battle_wait_strategy(sequence='completion > interrupt > success > failure > idle'):
|
|
|
test_battle_wait.battle_wait()
|
|
test_battle_wait.battle_wait()
|
|
|
|
|
|
|
|
|
|
|