Преглед изворни кода

Merge remote-tracking branch 'origin/dev' into dev

runhey пре 3 година
родитељ
комит
a5efcd48cf

+ 29 - 1
tasks/FallenSun/assets.py

@@ -8,5 +8,33 @@ from module.atom.list import RuleList
 # This file was automatically generated by ./dev_tools/assets_extract.py.
 # This file was automatically generated by ./dev_tools/assets_extract.py.
 # Don't modify it manually.
 # Don't modify it manually.
 class FallenSunAssets: 
 class FallenSunAssets: 
-	pass
+
+
+	# Image Rule Assets
+	# 选择日轮 
+	I_FALLEN_SUN = RuleImage(roi_front=(784,113,290,396), roi_back=(784,113,290,396), threshold=0.8, method="Template matching", file="./tasks/FallenSun/f/f_fallen_sun.png")
+	# 组队 
+	I_FORM_TEAM = RuleImage(roi_front=(939,594,96,90), roi_back=(939,594,96,90), threshold=0.8, method="Template matching", file="./tasks/FallenSun/f/f_form_team.png")
+	# description 
+	I_FALLEN_SUN_FIRE = RuleImage(roi_front=(1108,592,96,93), roi_back=(1108,592,96,93), threshold=0.8, method="Template matching", file="./tasks/FallenSun/f/f_fallen_sun_fire.png")
+	# description 
+	I_FALLEN_SUN_UNLOCK = RuleImage(roi_front=(557,560,31,38), roi_back=(557,560,31,38), threshold=0.8, method="Template matching", file="./tasks/FallenSun/f/f_fallen_sun_unlock.png")
+	# description 
+	I_FALLEN_SUN_LOCK = RuleImage(roi_front=(557,561,31,37), roi_back=(557,561,31,37), threshold=0.8, method="Template matching", file="./tasks/FallenSun/f/f_fallen_sun_lock.png")
+	# description 
+	I_FALLEN_SUN_MATCHING = RuleImage(roi_front=(62,568,44,114), roi_back=(62,568,44,114), threshold=0.8, method="Template matching", file="./tasks/FallenSun/f/f_fallen_sun_matching.png")
+	# 小小宠物,发现宝藏 
+	I_PET_PRESENT = RuleImage(roi_front=(873,184,62,147), roi_back=(873,184,62,147), threshold=0.8, method="Template matching", file="./tasks/FallenSun/f/f_pet_present.png")
+
+
+	# List Rule Assets
+	# 这个是御魂界面选择不同层数的 
+	L_LAYER_LIST = RuleList(folder="./tasks/FallenSun/res", direction="vertical", mode="ocr", roi_back=(138,130,359,500), size=(40, 93), 
+					 array=["壹", "贰", "叁"])
+
+
+	# Ocr Rule Assets
+	# Ocr-description 
+	O_O_TEST_OCR = RuleOcr(roi=(126,136,360,491), area=(126,136,360,491), mode="Full", method="Default", keyword="", name="o_test_ocr")
+
 
 

+ 35 - 4
tasks/FallenSun/config.py

@@ -1,11 +1,42 @@
 # This Python file uses the following encoding: utf-8
 # This Python file uses the following encoding: utf-8
-# @author runhey
-# github https://github.com/runhey
+# @author TripleEarth
+# github https://github.com/TripleEarth
 from pydantic import BaseModel, Field
 from pydantic import BaseModel, Field
+from enum import Enum
+from datetime import datetime, time
 
 
+from tasks.Component.SwitchSoul.switch_soul_config import SwitchSoulConfig
 from tasks.Component.config_scheduler import Scheduler
 from tasks.Component.config_scheduler import Scheduler
-from tasks.Component.config_base import ConfigBase
+from tasks.Component.config_base import ConfigBase, TimeDelta
+from tasks.Component.GeneralInvite.config_invite import InviteConfig
+from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
+
+
+class UserStatus(str, Enum):
+    LEADER = 'leader'
+    MEMBER = 'member'
+    ALONE = 'alone'
+    WILD = 'wild'  # 还不打算实现
+
+class Layer(str, Enum):
+    ONE = '壹层'
+    TWO = '贰层'
+    THREE = '叁层'
+
+class FallenSunConfig(ConfigBase):
+    # 身份
+    user_status: UserStatus = Field(default=UserStatus.LEADER, description='user_status_help')
+    # 层数
+    layer: Layer = Field(default=Layer.THREE, description='layer_help')
+    # 限制时间
+    limit_time: time = Field(default=time(minute=30), description='limit_time_help')
+    # 限制次数
+    limit_count: int = Field(default=50, description='limit_count_help')
+
 
 
 class FallenSun(ConfigBase):
 class FallenSun(ConfigBase):
     scheduler: Scheduler = Field(default_factory=Scheduler)
     scheduler: Scheduler = Field(default_factory=Scheduler)
-
+    fallen_sun_config: FallenSunConfig = Field(default_factory=FallenSunConfig)
+    invite_config: InviteConfig = Field(default_factory=InviteConfig)
+    general_battle_config: GeneralBattleConfig = Field(default_factory=GeneralBattleConfig)
+    switch_soul: SwitchSoulConfig = Field(default_factory=SwitchSoulConfig)

BIN
tasks/FallenSun/f/f_fallen_sun.png


BIN
tasks/FallenSun/f/f_fallen_sun_fire.png


BIN
tasks/FallenSun/f/f_fallen_sun_lock.png


BIN
tasks/FallenSun/f/f_fallen_sun_matching.png


BIN
tasks/FallenSun/f/f_fallen_sun_unlock.png


BIN
tasks/FallenSun/f/f_form_team.png


BIN
tasks/FallenSun/f/f_pet_present.png


+ 65 - 0
tasks/FallenSun/f/image.json

@@ -0,0 +1,65 @@
+[
+  {
+    "itemName": "fallen_sun",
+    "imageName": "f_fallen_sun.png",
+    "roiFront": "784,113,290,396",
+    "roiBack": "784,113,290,396",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "选择日轮"
+  },
+  {
+    "itemName": "form_team",
+    "imageName": "f_form_team.png",
+    "roiFront": "939,594,96,90",
+    "roiBack": "939,594,96,90",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "组队"
+  },
+  {
+    "itemName": "fallen_sun_fire",
+    "imageName": "f_fallen_sun_fire.png",
+    "roiFront": "1108,592,96,93",
+    "roiBack": "1108,592,96,93",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "fallen_sun_unlock",
+    "imageName": "f_fallen_sun_unlock.png",
+    "roiFront": "557,560,31,38",
+    "roiBack": "557,560,31,38",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "fallen_sun_lock",
+    "imageName": "f_fallen_sun_lock.png",
+    "roiFront": "557,561,31,37",
+    "roiBack": "557,561,31,37",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "fallen_sun_matching",
+    "imageName": "f_fallen_sun_matching.png",
+    "roiFront": "62,568,44,114",
+    "roiBack": "62,568,44,114",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "pet_present",
+    "imageName": "f_pet_present.png",
+    "roiFront": "873,184,62,147",
+    "roiBack": "873,184,62,147",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "小小宠物,发现宝藏"
+  }
+]

+ 21 - 0
tasks/FallenSun/res/list.json

@@ -0,0 +1,21 @@
+{
+  "name": "layer_list",
+  "direction": "vertical",
+  "type": "ocr",
+  "roiBack": "138,130,359,500",
+  "description": "这个是御魂界面选择不同层数的",
+  "list": [
+    {
+      "itemName": "壹",
+      "roiFront": "281,135,39,102"
+    },
+    {
+      "itemName": "贰",
+      "roiFront": "283,293,39,85"
+    },
+    {
+      "itemName": "叁",
+      "roiFront": "281,439,43,93"
+    }
+  ]
+}

+ 11 - 0
tasks/FallenSun/res/ocr.json

@@ -0,0 +1,11 @@
+[
+  {
+    "itemName": "o_test_ocr",
+    "roiFront": "126,136,360,491",
+    "roiBack": "126,136,360,491",
+    "mode": "Full",
+    "method": "Default",
+    "keyword": "",
+    "description": "Ocr-description"
+  }
+]

+ 345 - 0
tasks/FallenSun/script_task.py

@@ -0,0 +1,345 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+from time import sleep
+from datetime import time, datetime, timedelta
+
+from tasks.Component.GeneralBattle.general_battle import GeneralBattle
+from tasks.Component.GeneralInvite.general_invite import GeneralInvite
+from tasks.Component.GeneralBuff.general_buff import GeneralBuff
+from tasks.Component.GeneralRoom.general_room import GeneralRoom
+from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
+from tasks.GameUi.game_ui import GameUi
+from tasks.GameUi.page import page_main, page_soul_zones, page_shikigami_records
+from tasks.FallenSun.assets import FallenSunAssets
+from tasks.FallenSun.config import FallenSun, UserStatus
+from module.logger import logger
+from module.exception import TaskEnd
+
+class ScriptTask(GeneralBattle, GeneralInvite, GeneralBuff, GeneralRoom, GameUi, SwitchSoul, FallenSunAssets):
+
+    def run(self) -> bool:
+        # 御魂切换方式一
+        if self.config.fallen_sun.switch_soul.enable:
+            self.ui_get_current_page()
+            self.ui_goto(page_shikigami_records)
+            self.run_switch_soul(self.config.fallen_sun.switch_soul.switch_group_team)
+
+        # 御魂切换方式二
+        if self.config.fallen_sun.switch_soul.enable_switch_by_name:
+            self.ui_get_current_page()
+            self.ui_goto(page_shikigami_records)
+            self.run_switch_soul_by_name(self.config.fallen_sun.switch_soul.group_name,
+                                         self.config.fallen_sun.switch_soul.team_name)
+
+        limit_count = self.config.fallen_sun.fallen_sun_config.limit_count
+        limit_time = self.config.fallen_sun.fallen_sun_config.limit_time
+        self.current_count = 0
+        self.limit_count: int = limit_count
+        self.limit_time: timedelta = timedelta(hours=limit_time.hour, minutes=limit_time.minute, seconds=limit_time.second)
+
+        self.ui_get_current_page()
+        self.ui_goto(page_main)
+        config: FallenSun = self.config.fallen_sun
+
+        success = True
+        match config.fallen_sun_config.user_status:
+            case UserStatus.LEADER: success = self.run_leader()
+            case UserStatus.MEMBER: success = self.run_member()
+            case UserStatus.ALONE: self.run_alone()
+            case UserStatus.WILD: self.run_wild()
+            case _: logger.error('Unknown user status')
+
+        # 下一次运行时间
+        if success:
+            self.set_next_run('FallenSun', finish=True, success=True)
+        else:
+            self.set_next_run('FallenSun', finish=False, success=False)
+
+        raise TaskEnd
+
+
+
+    def fallen_sun_enter(self) -> bool:
+        logger.info('Enter fallen_sun')
+        while True:
+            self.screenshot()
+            if self.appear(self.I_FORM_TEAM):
+                return True
+            if self.appear_then_click(self.I_FALLEN_SUN, interval=1):
+                continue
+
+    def check_layer(self, layer: str) -> bool:
+        """
+        检查挑战的层数, 并选中挑战的层
+        :return:
+        """
+        pos = self.list_find(self.L_LAYER_LIST, layer)
+        if pos:
+            self.device.click(x=pos[0], y=pos[1])
+            return True
+
+    def check_lock(self, lock: bool = True) -> bool:
+        """
+        检查是否锁定阵容, 要求在八岐大蛇界面
+        :param lock:
+        :return:
+        """
+        logger.info('Check lock: %s', lock)
+        if lock:
+            while 1:
+                self.screenshot()
+                if self.appear(self.I_FALLEN_SUN_LOCK):
+                    return True
+                if self.appear_then_click(self.I_FALLEN_SUN_UNLOCK, interval=1):
+                    continue
+        else:
+            while 1:
+                self.screenshot()
+                if self.appear(self.I_FALLEN_SUN_UNLOCK):
+                    return True
+                if self.appear_then_click(self.I_FALLEN_SUN_LOCK, interval=1):
+                    continue
+
+
+    def run_leader(self):
+        logger.info('Start run leader')
+        self.ui_get_current_page()
+        self.ui_goto(page_soul_zones)
+        self.fallen_sun_enter()
+        layer = self.config.fallen_sun.fallen_sun_config.layer
+        self.check_layer(layer)
+        self.check_lock(self.config.fallen_sun.general_battle_config.lock_team_enable)
+        # 创建队伍
+        logger.info('Create team')
+        while 1:
+            self.screenshot()
+            if self.appear(self.I_FALLEN_SUN_MATCHING):
+                break
+            if self.appear_then_click(self.I_FORM_TEAM, interval=1):
+                continue
+        # 创建房间
+        self.create_room()
+        self.ensure_private()
+        self.create_ensure()
+
+        # 邀请队友
+        success = True
+        is_first = True
+        # 这个时候我已经进入房间了哦
+        while 1:
+            self.screenshot()
+            # 无论胜利与否, 都会出现是否邀请一次队友
+            # 区别在于,失败的话不会出现那个勾选默认邀请的框
+            if self.check_and_invite(self.config.fallen_sun.invite_config.default_invite):
+                continue
+
+            # 检查猫咪奖励
+            if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
+                continue
+
+            if self.current_count >= self.limit_count:
+                if self.is_in_room():
+                    logger.info('FallenSun count limit out')
+                    break
+
+            if datetime.now() - self.start_time >= self.limit_time:
+                if self.is_in_room():
+                    logger.info('FallenSun time limit out')
+                    break
+
+
+
+            # 如果没有进入房间那就不需要后面的邀请
+            if not self.is_in_room():
+                # 如果在探索界面或者是出现在组队界面, 那就是可能房间死了
+                # 要结束任务
+                sleep(0.5)
+                if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
+                    sleep(0.5)
+                    if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
+                        logger.warning('FallenSun task failed')
+                        success = False
+                        break
+                continue
+
+            # 点击挑战
+            if not is_first:
+                if self.run_invite(config=self.config.fallen_sun.invite_config):
+                    self.run_general_battle(config=self.config.fallen_sun.general_battle_config)
+                else:
+                    # 邀请失败,退出任务
+                    logger.warning('Invite failed and exit this fallen_sun task')
+                    success = False
+                    break
+
+            # 第一次会邀请队友
+            if is_first:
+                if not self.run_invite(config=self.config.fallen_sun.invite_config, is_first=True):
+                    logger.warning('Invite failed and exit this fallen_sun task')
+                    success = False
+                    break
+                else:
+                    is_first = False
+                    self.run_general_battle(config=self.config.fallen_sun.general_battle_config)
+
+        # 当结束或者是失败退出循环的时候只有两个UI的可能,在房间或者是在组队界面
+        # 如果在房间就退出
+        if self.exit_room():
+            pass
+        # 如果在组队界面就退出
+        if self.exit_team():
+            pass
+
+        self.ui_get_current_page()
+        self.ui_goto(page_main)
+
+        if not success:
+            return False
+        return True
+
+    def run_member(self):
+        logger.info('Start run member')
+        self.ui_get_current_page()
+        # self.ui_goto(page_soul_zones)
+        # self.fallen_sun_enter()
+        # self.check_lock(self.config.fallen_sun.general_battle_config.lock_team_enable)
+
+        # 进入战斗流程
+        self.device.stuck_record_add('BATTLE_STATUS_S')
+        while 1:
+            self.screenshot()
+
+            # 检查猫咪奖励
+            if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
+                continue
+
+            if self.current_count >= self.limit_count:
+                logger.info('FallenSun count limit out')
+                break
+            if datetime.now() - self.start_time >= self.limit_time:
+                logger.info('FallenSun time limit out')
+                break
+
+            if self.check_then_accept():
+                continue
+
+            if self.is_in_room():
+                self.device.stuck_record_clear()
+                if self.wait_battle(wait_time=self.config.fallen_sun.invite_config.wait_time):
+                    self.run_general_battle(config=self.config.fallen_sun.general_battle_config)
+                else:
+                    break
+            # 队长秒开的时候,检测是否进入到战斗中
+            elif self.check_take_over_battle(False, config=self.config.fallen_sun.general_battle_config):
+                continue
+
+        while 1:
+            # 有一种情况是本来要退出的,但是队长邀请了进入的战斗的加载界面
+            if self.appear(self.I_GI_HOME) or self.appear(self.I_GI_EXPLORE):
+                break
+            # 如果可能在房间就退出
+            if self.exit_room():
+                pass
+            # 如果还在战斗中,就退出战斗
+            if self.exit_battle():
+                pass
+
+
+        self.ui_get_current_page()
+        self.ui_goto(page_main)
+        return True
+
+    def run_alone(self):
+        logger.info('Start run alone')
+        self.ui_get_current_page()
+        self.ui_goto(page_soul_zones)
+        self.fallen_sun_enter()
+        layer = self.config.fallen_sun.fallen_sun_config.layer
+        self.check_layer(layer)
+        self.check_lock(self.config.fallen_sun.general_battle_config.lock_team_enable)
+
+        def is_in_fallen_sun(screenshot=False) -> bool:
+            if screenshot:
+                self.screenshot()
+            return self.appear(self.I_FALLEN_SUN_FIRE)
+
+        while 1:
+            self.screenshot()
+
+            # 检查猫咪奖励
+            if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
+                continue
+
+            if not is_in_fallen_sun():
+                continue
+
+            if self.current_count >= self.limit_count:
+                logger.info('FallenSun count limit out')
+                break
+            if datetime.now() - self.start_time >= self.limit_time:
+                logger.info('FallenSun time limit out')
+                break
+
+            # 点击挑战
+            while 1:
+                self.screenshot()
+                if self.appear_then_click(self.I_FALLEN_SUN_FIRE, interval=1):
+                    pass
+
+                if not self.appear(self.I_FALLEN_SUN_FIRE):
+                    self.run_general_battle(config=self.config.fallen_sun.general_battle_config)
+                    break
+
+        # 回去
+        while 1:
+            self.screenshot()
+            if not self.appear(self.I_FORM_TEAM):
+                break
+            if self.appear_then_click(self.I_BACK_BL, interval=1):
+                continue
+
+        self.ui_current = page_soul_zones
+        self.ui_goto(page_main)
+
+
+
+    def run_wild(self):
+        logger.error('Wild mode is not implemented')
+        pass
+
+
+
+
+
+
+if __name__ == '__main__':
+    from module.config.config import Config
+    from module.device.device import Device
+    from memory_profiler import profile
+    c = Config('oas1')
+    d = Device(c)
+    t = ScriptTask(c, d)
+
+    # t.run()
+
+    # t.check_layer('悲')
+
+    from module.base.timer import timer
+
+    @timer
+    @profile
+    def test_memory():
+        t.screenshot()
+        print(t.ocr_appear(t.O_O_TEST_OCR))
+        print(t.L_LAYER_LIST.image_appear(t.device.image, '叁'))
+    for i in range(4):
+        test_memory()
+        print('=====================')
+
+
+
+
+
+
+