Просмотр исходного кода

perf: reuse config scheduler and remove Ocochi

runhey 3 лет назад
Родитель
Сommit
290f7f5fc1

+ 50 - 0
tasks/Component/GeneralBattle/config_general_battle.py

@@ -0,0 +1,50 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+from enum import Enum
+from datetime import datetime, time
+from pydantic import BaseModel, ValidationError, validator, Field
+
+class GreenMarkType(str, Enum):
+    GREEN_LEFT1 = 'green_left1'
+    GREEN_LEFT2 = 'green_left2'
+    GREEN_LEFT3 = 'green_left3'
+    GREEN_LEFT4 = 'green_left4'
+    GREEN_LEFT5 = 'green_left5'
+    GREEN_MAIN = 'green_main'
+
+class GeneralBattleConfig(BaseModel):
+
+    # 是否锁定阵容, 有些的战斗是外边的锁定阵容甚至有些的战斗没有锁定阵容的
+    lock_team_enable: bool = Field(default=False, description='[锁定阵容]:默认为False \n如果锁定阵容将无法启用预设队伍、加成功能')
+
+    # 是否启动 预设队伍
+    preset_enable: bool = Field(default=False, description='[启用预设]:默认为False')
+    # 选哪一个预设组
+    preset_group: int = Field(default=1, description='[设置预设组]:默认为1, 可选[1-7]', ge=1, le=7)
+    # 选哪一个队伍
+    preset_team: int = Field(default=1, description='[设置预设队伍]:默认为1, 可选[1-5]', ge=1, le=5)
+    # 是否启动开启buff
+    buff_enable: bool = Field(default=False, description='[启用加成]:默认为False')
+    # 是否点击觉醒Buff
+    buff_awake_click: bool = Field(default=False, description='[觉醒加成]:默认为False')
+    # 是否点击御魂buff
+    buff_soul_click: bool = Field(default=False, description='[御魂加成]:默认为False')
+    # 是否点击金币50buff
+    buff_gold_50_click: bool = Field(default=False, description='[金币50加成]:默认为False')
+    # 是否点击金币100buff
+    buff_gold_100_click: bool = Field(default=False, description='[金币100加成]:默认为False')
+    # 是否点击经验50buff
+    buff_exp_50_click: bool = Field(default=False, description='[经验50加成]:默认为False')
+    # 是否点击经验100buff
+    buff_exp_100_click: bool = Field(default=False, description='[经验100加成]:默认为False')
+
+    # 是否开启绿标
+    green_enable: bool = Field(default=False, description='[启用绿标]:默认为False')
+    # 选哪一个绿标
+    green_mark: GreenMarkType = Field(default=GreenMarkType.GREEN_LEFT1, description='[设置绿标]:默认为左一 \n可选[左一, 左二, 左三, 左四, 左五, 主阴阳师]')
+
+    # 是否启动战斗时随机点击或者随机滑动
+    random_click_swipt_enable: bool = Field(default=False, description='[战斗时随机点击或滑动]:默认为False \n防封优化,请注意这个与绿标功能冲突,可能会乱点绿标')
+
+

+ 237 - 0
tasks/Component/GeneralBattle/general_battle.py

@@ -0,0 +1,237 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import time
+import random
+
+from tasks.base_task import BaseTask
+from tasks.Component.GeneralBattle.config_general_battle import GreenMarkType, GeneralBattleConfig
+from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
+from module.logger import logger
+
+
+
+class GeneralBattle(BaseTask, GeneralBattleAssets):
+    """
+    使用这个通用的战斗必须要求这个任务的config有config_general_battle
+    """
+
+    def run_general_battle(self, config: dict=None) -> bool:
+        """
+        运行脚本
+        :return:
+        """
+        if config is None:
+            config = GeneralBattleConfig().dict()
+
+        # if isinstance(config, GeneralBattle):
+        #     logger.error(f"config is not GeneralBattle type, config: {config}")
+        #     config = GeneralBattle().dict()
+
+        # 如果没有锁定队伍。那么可以根据配置设定队伍
+        if not config.lock_team_enable:
+            logger.info("Lock team is not enable")
+            # 如果启动更换队伍
+            if config.preset_enable:
+                logger.info("Preset is enable")
+                # 点击预设按钮
+                logger.info("Click preset button")
+                while 1:
+                    self.screenshot()
+                    if self.appear_then_click(self.I_PRESET, threshold=0.8):
+                        continue
+                    if self.appear(self.I_PRESET_ENSURE):
+                        break
+                # 选择预设组
+                logger.info("Select preset group")
+                x, y = None, None
+                match config.preset_group:
+                    case 1: x, y = self.C_PRESET_GROUP_1.coord()
+                    case 2: x, y = self.C_PRESET_GROUP_2.coord()
+                    case 3: x, y = self.C_PRESET_GROUP_3.coord()
+                    case 4: x, y = self.C_PRESET_GROUP_4.coord()
+                    case 5: x, y = self.C_PRESET_GROUP_5.coord()
+                    case 6: x, y = self.C_PRESET_GROUP_6.coord()
+                    case 7: x, y = self.C_PRESET_GROUP_7.coord()
+                    case _: x, y = self.C_PRESET_GROUP_1.coord()
+                self.device.click(x, y)
+
+                # 选择预设的队伍
+                logger.info("select preset team")
+                time.sleep(0.5)
+                match config.preset_team:
+                    case 1: x, y = self.C_PRESET_TEAM_1.coord()
+                    case 2: x, y = self.C_PRESET_TEAM_2.coord()
+                    case 3: x, y = self.C_PRESET_TEAM_3.coord()
+                    case 4: x, y = self.C_PRESET_TEAM_4.coord()
+                    case _: x, y = self.C_PRESET_TEAM_1.coord()
+                self.device.click(x, y)
+
+                # 点击预设确认
+                logger.info("click preset ensure")
+                while 1:
+                    self.screenshot()
+                    if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
+                        continue
+                    if not self.appear(self.I_PRESET_ENSURE):
+                        break
+
+            # 点击buff按钮
+            if config.buff_enable:
+                logger.info("buff is enable")
+                logger.info("click buff button")
+                while 1:
+                    self.screenshot()
+                    if self.appear_then_click(self.I_BUFF, interval=1.5):
+                        continue
+                    if self.appear(self.I_BUFF_AWAKEN) or self.appear(self.I_BUFF_SOUL):
+                        break
+
+
+            # 点击准备按钮
+            self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
+            while 1:
+                self.screenshot()
+                if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
+                    continue
+                if not self.appear(self.I_BUFF):
+                    break
+            logger.info("Click prepare ensure button")
+
+            # 照顾一下某些模拟器慢的
+            time.sleep(0.1)
+
+        # 绿标
+        if config.green_enable:
+            logger.info("Green is enable")
+            x, y = None, None
+            match config.green_mark:
+                case GreenMarkType.GREEN_LEFT1: x, y = self.C_GREEN_LEFT_1.coord()
+                case GreenMarkType.GREEN_LEFT2: x, y = self.C_GREEN_LEFT_2.coord()
+                case GreenMarkType.GREEN_LEFT3: x, y = self.C_GREEN_LEFT_3.coord()
+                case GreenMarkType.GREEN_LEFT4: x, y = self.C_GREEN_LEFT_4.coord()
+                case GreenMarkType.GREEN_LEFT5: x, y = self.C_GREEN_LEFT_5.coord()
+                case GreenMarkType.GREEN_MAIN: x, y = self.C_GREEN_MAIN.coord()
+
+            # 判断有无坐标的偏移
+            self.appear_then_click(self.I_LOCAL)
+            time.sleep(0.3)
+            # 点击绿标
+            self.device.click(x, y)
+
+        # 战斗过程 随机点击和滑动 防封
+        logger.info("battle process")
+        win: bool = False
+        while 1:
+            self.screenshot()
+            # 如果出现赢 就点击
+            if self.appear(self.I_WIN, threshold=0.6):
+                logger.info("Battle result is win")
+                win = True
+                break
+
+            # 如果出现失败 就点击,返回False
+            if self.appear(self.I_FALSE, threshold=0.6):
+                logger.info("Battle result is false")
+                win = False
+                break
+
+            # 如果领奖励
+            if self.appear(self.I_REWARD, threshold=0.6):
+                win = True
+                break
+            # 如果开启战斗过程随机滑动
+            if config.random_click_swipt_enable:
+                time.sleep(0.4)  # 这样的好像不对
+                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)
+
+        # 再次确认战斗结果
+        logger.info("reconfirm the results of the battle")
+        while 1:
+            self.screenshot()
+            if win:
+                # 点击赢了
+                if self.appear_then_click(self.I_WIN):
+                    continue
+                if not self.appear(self.I_WIN):
+                    break
+            else:
+                # 如果失败且 点击失败后
+                if self.appear_then_click(self.I_FALSE, threshold=0.6):
+                    continue
+                if not self.appear(self.I_FALSE, threshold=0.6):
+                    return False
+        # 最后保证能点击 获得奖励
+        self.wait_until_appear(self.I_REWARD)
+        logger.info("get reward")
+        while 1:
+            self.screenshot()
+            # 如果出现领奖励
+            if self.appear_then_click(self.I_REWARD, interval=1.5):
+                continue
+            if not self.appear(self.I_REWARD):
+                break
+
+        if win:
+            return True
+        else:
+            return False
+
+
+    def run_general_battle_back(self, config: dict=None) -> bool:
+        """
+        进入挑战然后直接返回
+        :param config:
+        :return:
+        """
+        # 如果没有锁定队伍那么在点击准备后才退出的
+        if not config.lock_team_enable:
+            # 点击准备按钮
+            self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
+            while 1:
+                self.screenshot()
+                if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
+                    continue
+                if not self.appear(self.I_PRESET):
+                    break
+            logger.info(f"Click {self.I_PREPARE_HIGHLIGHT.name}")
+
+        # 点击返回
+        while 1:
+            self.screenshot()
+            if self.appear_then_click(self.I_EXIT, interval=1.5):
+                continue
+            if self.appear(self.I_EXIT_ENSURE):
+                break
+        logger.info(f"Click {self.I_EXIT.name}")
+
+        # 点击返回确认
+        while 1:
+            self.screenshot()
+            if self.appear_then_click(self.I_EXIT_ENSURE, interval=1.5):
+                continue
+            if self.appear(self.I_FALSE):
+                break
+        logger.info(f"Click {self.I_EXIT_ENSURE.name}")
+
+        # 点击失败确认
+        self.wait_until_appear(self.I_FALSE)
+        while 1:
+            self.screenshot()
+            if self.appear_then_click(self.I_FALSE, interval=1.5):
+                continue
+            if not self.appear(self.I_FALSE):
+                break
+        logger.info(f"Click {self.I_FALSE.name}")
+
+        return True
+
+