|
|
@@ -1,16 +1,20 @@
|
|
|
import random
|
|
|
import time
|
|
|
+import copy
|
|
|
|
|
|
|
|
|
from functools import wraps
|
|
|
from dataclasses import dataclass
|
|
|
from typing import TypeVar, ParamSpec, Callable
|
|
|
from enum import Enum, auto
|
|
|
+from cached_property import cached_property
|
|
|
|
|
|
from module.logger import logger
|
|
|
from module.base.timer import Timer
|
|
|
+from module.atom.click import RuleClickExclude
|
|
|
|
|
|
from tasks.base_task import BaseTask
|
|
|
+from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
|
|
|
|
|
|
|
|
|
P = ParamSpec('P')
|
|
|
@@ -24,6 +28,7 @@ class HookSignal(Enum):
|
|
|
|
|
|
@dataclass
|
|
|
class BattleWaitContext:
|
|
|
+ options: dict[str: dict] = None
|
|
|
completion = False
|
|
|
success = False
|
|
|
|
|
|
@@ -190,9 +195,28 @@ class battle_wait_strategy:
|
|
|
首次装饰 func 初始化 battle_wait_plan
|
|
|
"""
|
|
|
battle_wait_plan: BattleWaitPlan = None
|
|
|
+ options: dict[str: dict] = {
|
|
|
+ 'success': {
|
|
|
+ 'reward_exclude_click_1': ['C_END_MESSAGE_RIGHT_TOP', 'C_END_BUFF_AREA_1', 'C_END_BUFF_AREA_2', 'C_END_SOUL_RECORD', 'C_END_SOUL_DETAILS',],
|
|
|
+ 'reward_exclude_click_2': ['C_END_MESSAGE_RIGHT_TOP', 'C_END_BUFF_AREA_1', 'C_END_BUFF_AREA_2', 'C_END_SOUL_RECORD', 'C_END_SOUL_DETAILS',
|
|
|
+ 'C_END_1_1', 'C_END_1_2', 'C_END_1_3', 'C_END_1_4', 'C_END_1_5', 'C_END_1_6',
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
def __init__(self, *arg, **kwargs):
|
|
|
+ self._temp_options = kwargs.pop('options', None)
|
|
|
+ if self._temp_options is not None:
|
|
|
+ if not isinstance(self._temp_options, dict):
|
|
|
+ raise TypeError(f'temp_options must be a dict, got {self._temp_options!r}')
|
|
|
+ for key, value in self._temp_options.items():
|
|
|
+ if not isinstance(key, str):
|
|
|
+ raise
|
|
|
+ if not isinstance(value, dict):
|
|
|
+ raise TypeError(f'temp_options must be a dict, got {self._temp_options!r}')
|
|
|
+ battle_wait_strategy.options.update(self._temp_options)
|
|
|
+
|
|
|
self._temp_battle_wait_plan = None
|
|
|
if battle_wait_strategy.battle_wait_plan is None:
|
|
|
# 首次装饰 func 初始化 battle_wait_plan
|
|
|
@@ -210,11 +234,14 @@ class battle_wait_strategy:
|
|
|
|
|
|
def __exit__(self, *exc):
|
|
|
battle_wait_strategy.battle_wait_plan = self._previous_plan
|
|
|
+ self._temp_options = None
|
|
|
return False
|
|
|
|
|
|
def __str__(self):
|
|
|
temp_plan = getattr(self, '_temp_battle_wait_plan', None)
|
|
|
default_plan = getattr(self, 'battle_wait_plan', None)
|
|
|
+ options = dict(battle_wait_strategy.options or {})
|
|
|
+ options.update(self._temp_options or {})
|
|
|
|
|
|
if temp_plan is not None:
|
|
|
plan = temp_plan
|
|
|
@@ -224,11 +251,12 @@ class battle_wait_strategy:
|
|
|
scope = 'decorator'
|
|
|
|
|
|
if plan is None:
|
|
|
- return f'{type(self).__name__}(plan=None)'
|
|
|
+ return f'{type(self).__name__}(options={options}, plan=None)'
|
|
|
|
|
|
return (
|
|
|
f'{type(self).__name__}('
|
|
|
f'scope={scope}, '
|
|
|
+ f'options={options}, '
|
|
|
f'plan=\n{plan}'
|
|
|
f')'
|
|
|
)
|
|
|
@@ -251,19 +279,69 @@ class battle_wait_strategy:
|
|
|
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)
|
|
|
+ current_plan = copy.deepcopy(battle_wait_strategy.battle_wait_plan)
|
|
|
+ current_plan = current_plan.override(*override_args)
|
|
|
+ else:
|
|
|
+ current_plan = battle_wait_strategy.battle_wait_plan
|
|
|
# -----------------------------------------------------------------------
|
|
|
# kwargs.setdefault(
|
|
|
# 'battle_wait_plan',
|
|
|
# self.battle_wait_plan,
|
|
|
# )
|
|
|
- return func(owner, battle_wait_plan=battle_wait_strategy.battle_wait_plan)
|
|
|
+ options = dict(battle_wait_strategy.options or {})
|
|
|
+ options.update(self._temp_options or {})
|
|
|
+ return func(owner, battle_wait_plan=current_plan, options=options) \
|
|
|
+ if options else func(owner, battle_wait_plan=current_plan)
|
|
|
|
|
|
return inner
|
|
|
|
|
|
+ def with_options(self, options: dict) -> "battle_wait_strategy":
|
|
|
+ if not isinstance(options, dict):
|
|
|
+ raise
|
|
|
+ for event, v in options.items():
|
|
|
+ if not isinstance(event, str):
|
|
|
+ raise TypeError()
|
|
|
+ if not isinstance(v, dict):
|
|
|
+ raise TypeError()
|
|
|
+ # 先装饰器, 后临时变量
|
|
|
+ if battle_wait_strategy.options is None:
|
|
|
+ battle_wait_strategy.options = options
|
|
|
+ self._temp_options = None
|
|
|
+ else:
|
|
|
+ self._temp_options = options
|
|
|
+ return self
|
|
|
+
|
|
|
+
|
|
|
+class BattleWait(BaseTask, GeneralBattleAssets):
|
|
|
+
|
|
|
+ # ------------------------------------------------------------------------------------------------------------------
|
|
|
+ @cached_property
|
|
|
+ def exclude_button_stage_1(self):
|
|
|
+ return ['C_END_MESSAGE_RIGHT_TOP', 'C_END_BUFF_AREA_1', 'C_END_BUFF_AREA_2',
|
|
|
+ 'C_END_SOUL_RECORD', 'C_END_SOUL_DETAILS',
|
|
|
+ ]
|
|
|
+
|
|
|
+ @cached_property
|
|
|
+ def exclude_button_stage_2(self):
|
|
|
+ return ['C_END_MESSAGE_RIGHT_TOP', 'C_END_BUFF_AREA_1', 'C_END_BUFF_AREA_2', 'C_END_SOUL_RECORD', 'C_END_SOUL_DETAILS',
|
|
|
+ 'C_END_1_1', 'C_END_1_2', 'C_END_1_3', 'C_END_1_4', 'C_END_1_5', 'C_END_1_6',
|
|
|
+ ]
|
|
|
+
|
|
|
+ def reward_exclude_click(self, areas: list[str] = None, name: str = 'success_exclude_click') -> RuleClickExclude:
|
|
|
+ inputs = []
|
|
|
+ for area in areas:
|
|
|
+ click = getattr(self, area, None)
|
|
|
+ if click is None:
|
|
|
+ raise ValueError(f'Unknown success exclusion click: {area!r}')
|
|
|
+ inputs.append(click)
|
|
|
+ return RuleClickExclude(inputs, name=name)
|
|
|
+
|
|
|
+ def reward_exclude_click_1(self, areas: list[str] = None) -> RuleClickExclude:
|
|
|
+ return self.reward_exclude_click(areas, name='reward_exclude_click_1')
|
|
|
+
|
|
|
+ def reward_exclude_click_2(self, areas: list[str] = None) -> RuleClickExclude:
|
|
|
+ return self.reward_exclude_click(areas, name='reward_exclude_click_2')
|
|
|
|
|
|
-class BattleWait(BaseTask):
|
|
|
# build in
|
|
|
# ------------------------------------------------------------------------------------------------------------------
|
|
|
def _bw_setup_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
@@ -273,6 +351,17 @@ class BattleWait(BaseTask):
|
|
|
self.device.stuck_record_add('BATTLE_STATUS_S')
|
|
|
self.device.click_record_clear()
|
|
|
logger.info('Start battle process')
|
|
|
+ options: dict[str, dict] = getattr(bw_ctx, 'options', None) or {}
|
|
|
+ success_options = options.get('success') or {}
|
|
|
+ if not isinstance(success_options, dict):
|
|
|
+ raise TypeError("battle wait option 'success' must be a dict")
|
|
|
+ excludes = success_options.get('excludes')
|
|
|
+ exclude_stage_1 = excludes if excludes is not None else self.exclude_button_stage_1
|
|
|
+ exclude_stage_2 = excludes if excludes is not None else self.exclude_button_stage_2
|
|
|
+ self._reward_exclude_click_1 = self.reward_exclude_click_1(exclude_stage_1)
|
|
|
+ self._reward_exclude_click_2 = self.reward_exclude_click_2(exclude_stage_2)
|
|
|
+ # print(self._reward_exclude_click_1)
|
|
|
+ # print(self._reward_exclude_click_2)
|
|
|
return HookSignal.DONE
|
|
|
|
|
|
def _bw_completion_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
@@ -282,32 +371,44 @@ class BattleWait(BaseTask):
|
|
|
return HookSignal.CONTINUE
|
|
|
|
|
|
def _bw_interrupt_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
+ # 比如 御魂溢出
|
|
|
return HookSignal.CONTINUE
|
|
|
|
|
|
def _bw_success_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
if self.appear_then_click(self.I_WIN, interval=0.8):
|
|
|
+ self.click(self._reward_exclude_click_1)
|
|
|
return HookSignal.CONTINUE
|
|
|
- appear_ghost, appear_reward, appear_gold = (
|
|
|
+ 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),
|
|
|
+ self.appear(self.I_REWARD_GOLD_SNAKE_SKIN)
|
|
|
)
|
|
|
- if not any([appear_ghost, appear_reward, appear_gold]):
|
|
|
+ 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 = (
|
|
|
+ # 不小心点到了具体的奖励,他会弹出这个物品的详细描述 里面必定包含有“获取途径”
|
|
|
+ if self.appear(self.I_END_FIX_1) or self.appear(self.I_END_FIX_2):
|
|
|
+ self.click(self.C_REWARD_2, interval=1.5)
|
|
|
+
|
|
|
+ _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),
|
|
|
+ self.appear(self.I_REWARD_GOLD_SNAKE_SKIN)
|
|
|
)
|
|
|
- # 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)
|
|
|
+ # 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]):
|
|
|
+ if random.random() < 0.02:
|
|
|
+ # 有一定的概率专门点击具体的奖励物品
|
|
|
+ x, y = self._reward_exclude_click_2.coord_in_excluded(None)
|
|
|
+ self.device.click(x=x, y=y, control_name='reward_item')
|
|
|
+ continue
|
|
|
+ self.click(self._reward_exclude_click_2, interval=1.5)
|
|
|
else:
|
|
|
logger.info('Get all reward')
|
|
|
bw_ctx.success = True
|
|
|
@@ -324,8 +425,8 @@ class BattleWait(BaseTask):
|
|
|
logger.warning('False battle')
|
|
|
self.ui_click_until_disappear(self.I_FALSE)
|
|
|
bw_ctx.completion = True
|
|
|
- return True
|
|
|
- return True
|
|
|
+ return HookSignal.CONTINUE
|
|
|
+ return HookSignal.CONTINUE
|
|
|
|
|
|
def _bw_idle_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
return HookSignal.CONTINUE
|
|
|
@@ -334,6 +435,8 @@ class BattleWait(BaseTask):
|
|
|
return HookSignal.CONTINUE
|
|
|
|
|
|
def _bw_randomclick_default(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
+ if not self.is_in_battle(is_screenshot=False):
|
|
|
+ return HookSignal.CONTINUE
|
|
|
if 0 <= random.randint(0, 500) <= 20: # 百分之4的概率
|
|
|
rand_type = random.randint(0, 2)
|
|
|
match rand_type:
|
|
|
@@ -352,6 +455,11 @@ class BattleWait(BaseTask):
|
|
|
# custom
|
|
|
# ------------------------------------------------------------------------------------------------------------------
|
|
|
def _bw_success_soul(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
+ options: dict[str, dict] = getattr(bw_ctx, 'options', None) or {}
|
|
|
+ success_options = options.get('success') or {}
|
|
|
+ if not isinstance(success_options, dict):
|
|
|
+ raise TypeError("battle wait option 'success' must be a dict")
|
|
|
+ action_click = self.reward_exclude_click(success_options.get('excludes'))
|
|
|
if self.appear_then_click(self.I_WIN, interval=0.8):
|
|
|
return HookSignal.CONTINUE
|
|
|
appear_ghost, appear_reward, appear_gold, appear_skin = (
|
|
|
@@ -375,7 +483,6 @@ class BattleWait(BaseTask):
|
|
|
)
|
|
|
# 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')
|
|
|
@@ -387,36 +494,56 @@ class BattleWait(BaseTask):
|
|
|
break
|
|
|
return HookSignal.CONTINUE
|
|
|
|
|
|
+ @cached_property
|
|
|
+ def exclude_click_activity(self, areas: list[str] = None) -> RuleClickExclude:
|
|
|
+ inputs = []
|
|
|
+ for area in ['C_END_MESSAGE_RIGHT_TOP', 'C_END_ACTIVITY_REWARD']:
|
|
|
+ click = getattr(self, area, None)
|
|
|
+ if click is None:
|
|
|
+ raise ValueError(f'Unknown success exclusion click: {area!r}')
|
|
|
+ inputs.append(click)
|
|
|
+ return RuleClickExclude(inputs, name='exclude_click_activity', strategy='rejection', distribution='uniform')
|
|
|
+
|
|
|
+ def _bw_success_activity(self, bw_ctx: BattleWaitContext) -> HookSignal:
|
|
|
+ """
|
|
|
+ 战斗结算是有 “获得奖励” 的适用
|
|
|
+ """
|
|
|
+ if not self.appear(self.I_UI_REWARD):
|
|
|
+ return HookSignal.CONTINUE
|
|
|
+ self.screenshot()
|
|
|
+ if not self.appear(self.I_UI_REWARD):
|
|
|
+ return HookSignal.CONTINUE
|
|
|
+
|
|
|
+ logger.info('Win battle')
|
|
|
+ timer = Timer(20).start()
|
|
|
+ while 1:
|
|
|
+ self.screenshot()
|
|
|
+
|
|
|
+ # 不小心点到了具体的奖励,他会弹出这个物品的详细描述 里面必定包含有“获取途径”
|
|
|
+ if self.appear(self.I_END_FIX_1) or self.appear(self.I_END_FIX_2):
|
|
|
+ self.click(self.C_REWARD_2, interval=1.5)
|
|
|
+
|
|
|
+ if self.appear(self.I_UI_REWARD):
|
|
|
+ if random.random() < 0.02:
|
|
|
+ # 有一定的概率专门点击具体的奖励物品
|
|
|
+ x, y = self.exclude_click_activity.coord_in_excluded(['C_END_ACTIVITY_REWARD'])
|
|
|
+ self.device.click(x=x, y=y, control_name='reward_item')
|
|
|
+ continue
|
|
|
+ self.click(self.exclude_click_activity, interval=1.5)
|
|
|
+ elif not self.appear(self.I_END_FIX_2):
|
|
|
+ self.screenshot()
|
|
|
+ if self.appear(self.I_UI_REWARD) or self.appear(self.I_END_FIX_2):
|
|
|
+ continue
|
|
|
+ logger.info('Get all reward')
|
|
|
+ bw_ctx.success = True
|
|
|
+ bw_ctx.completion = True
|
|
|
+ return HookSignal.DONE
|
|
|
+
|
|
|
+ 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:
|
|
|
@@ -443,15 +570,29 @@ class BattleWait(BaseTask):
|
|
|
但是必须要实现对应的hook 上面的比如 _bw_yyy_default() 以及 _bw_abcd_edf()
|
|
|
|
|
|
hook 可以自定义顺序,比如 battle_wait_strategy(sequence='completion > interrupt > success > failure > idle')
|
|
|
- 如果指定sequence, 新增的event会按照传参时候从左到右排序,左边高优先级,新增的会插入到 failure 和 idle 之间
|
|
|
+ 如果没有指定sequence, 新增的event会按照传参时候从左到右排序,左边高优先级,新增的会插入到 failure 和 idle 之间
|
|
|
+
|
|
|
+ 如果希望每一个hook带上参数:
|
|
|
+ 1. 可以在装饰器定义 @battle_wait_strategy(options = options)
|
|
|
+ 2. 上下文带上 with battle_wait_strategy(...).with_options(options)
|
|
|
+ options: dict[str: dict] = {
|
|
|
+ "setup": {...}
|
|
|
+ ...
|
|
|
+ }
|
|
|
+
|
|
|
"""
|
|
|
+ bw_ctx = BattleWaitContext()
|
|
|
+
|
|
|
battle_wait_plan = kwargs.get('battle_wait_plan')
|
|
|
if battle_wait_plan is None:
|
|
|
battle_wait_plan = BattleWaitPlan()
|
|
|
# print(battle_wait_plan)
|
|
|
# print(battle_wait_plan.sequence_function_names())
|
|
|
+ options = kwargs.get('options', None)
|
|
|
+ if options is not None:
|
|
|
+ bw_ctx.options = options
|
|
|
+ # logger.info('options: {}'.format(options))
|
|
|
|
|
|
- bw_ctx = BattleWaitContext()
|
|
|
setup_func = getattr(self, battle_wait_plan.function_setup_name, self._bw_setup_default)
|
|
|
setup_func(bw_ctx)
|
|
|
handlers = [getattr(self, func_name, None) for func_name in battle_wait_plan.sequence_function_names()]
|
|
|
@@ -462,6 +603,8 @@ class BattleWait(BaseTask):
|
|
|
result = handler(bw_ctx)
|
|
|
if handler.__name__.startswith('_bw_completion') and result == HookSignal.DONE:
|
|
|
return True
|
|
|
+ if result == HookSignal.CONTINUE:
|
|
|
+ continue
|
|
|
|
|
|
# bw_ctx.completion = True
|
|
|
|
|
|
@@ -485,7 +628,7 @@ if __name__ == '__main__':
|
|
|
|
|
|
test_battle_wait = TestBattleWait(c,d)
|
|
|
test_battle_wait.battle_wait(random_click_swipt_enable=1)
|
|
|
- with battle_wait_strategy(sequence='completion > interrupt > success > failure > idle'):
|
|
|
+ with battle_wait_strategy(sequence='completion > interrupt > success > failure > idle').with_options(options={"setup": {"11": "11"}}):
|
|
|
test_battle_wait.battle_wait()
|
|
|
|
|
|
|
|
|
@@ -495,8 +638,3 @@ if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|