Browse Source

Merge pull request #25 from runhey/dev

Dev
huangrunheng 3 years ago
parent
commit
f7e1cf97d3

+ 18 - 1
config/template.json

@@ -18,7 +18,8 @@
       "screenshot_interval": 0.3,
       "combat_screenshot_interval": 1.0,
       "task_hoarding_duration": 0,
-      "when_task_queue_empty": "goto_main"
+      "when_task_queue_empty": "goto_main",
+      "schedule_rule": "Filter"
     }
   },
   "restart": {
@@ -307,5 +308,21 @@
       "green_mark": "green_left1",
       "random_click_swipt_enable": false
     }
+  },
+  "true_orochi": {
+    "scheduler": {
+      "enable": false,
+      "next_run": "2023-01-01 00:00:00",
+      "priority": 10,
+      "success_interval": "07 00:00:00",
+      "failure_interval": "01 00:00:00",
+      "server_update": "09:00:00"
+    },
+    "true_orochi_config": {
+      "find_true_orochi": true,
+      "green_enable": false,
+      "green_mark_type": "left_1",
+      "current_success": 0
+    }
   }
 }

+ 19 - 14
module/config/config.py

@@ -15,6 +15,7 @@ from module.config.config_watcher import ConfigWatcher
 from module.config.config_menu import ConfigMenu
 from module.config.config_model import ConfigModel
 from module.config.config_state import ConfigState
+from module.config.scheduler import TaskScheduler
 from module.config.utils import *
 
 from module.exception import RequestHumanTakeover, ScriptError
@@ -45,6 +46,12 @@ class Function:
         if isinstance(next_run, str):
             next_run = datetime.strptime(next_run, "%Y-%m-%d %H:%M:%S")
         self.next_run: datetime = next_run
+        priority = data['scheduler']['priority']
+        if isinstance(priority, str):
+            priority = int(priority)
+        self.priority: int = priority
+        if not isinstance(self.priority, int):
+            logger.error(f"Invalid priority: {self.priority}")
 
         # self.enable = deep_get(data, keys="Scheduler.Enable", default=False)
         # self.command = deep_get(data, keys="Scheduler.Command", default="Unknown")
@@ -52,7 +59,7 @@ class Function:
 
     def __str__(self):
         enable = "Enable" if self.enable else "Disable"
-        return f"{self.command} ({enable}, {str(self.next_run)})"
+        return f"{self.command} ({enable}, {self.priority}, {str(self.next_run)})"
 
     __repr__ = __str__
 
@@ -168,12 +175,13 @@ class Config(ConfigState, ConfigManual, ConfigWatcher, ConfigMenu):
             else:
                 waiting_task.append(func)
 
-        f = Filter(regex=r"(.*)", attr=["command"])
-        f.load(self.SCHEDULER_PRIORITY)
+        # f = Filter(regex=r"(.*)", attr=["command"])
+        # f.load(self.SCHEDULER_PRIORITY)
         if pending_task:
-            pending_task = f.apply(pending_task)
+            pending_task = TaskScheduler.schedule(rule=self.model.script.optimization.schedule_rule,
+                                                  pending=pending_task)
         if waiting_task:
-            waiting_task = f.apply(waiting_task)
+            # waiting_task = f.apply(waiting_task)
             waiting_task = sorted(waiting_task, key=operator.attrgetter("next_run"))
         if error:
             pending_task = error + pending_task
@@ -306,17 +314,14 @@ class Config(ConfigState, ConfigManual, ConfigWatcher, ConfigMenu):
         self.save()
 
 if __name__ == '__main__':
-    # config = Config(config_name='oas1')
+    config = Config(config_name='oas1')
+    config.update_scheduler()
+    print(config.pending_task)
+    print(config.waiting_task)
+    config.update_scheduler()
+    print(config.pending_task)
     # print(config.get_next())
 
-    test = {}
-    test['ww1'] = {}
-    test['ww1']['ww2'] = 3
-
-    print(deep_get(test, 'ww1.ww2'))
-    print(test)
-    deep_set(test, 'ww1.ww2', 4)
-    print(deep_get(test, 'ww1.ww2'))
 
 
 

BIN
module/config/i18n/zh_CN.qm


+ 18 - 1
module/config/i18n/zh_CN.ts

@@ -501,5 +501,22 @@ mode_3策略是刷到五个契灵后开始结契,选择高级、中级盘来
 	<message><source>green_mark_type_help</source><translation> </translation></message>
 	<message><source>Current Success</source><translation>本周已经挑战成功的次数</translation></message>
 	<message><source>current_success_help</source><translation>请不要手动修改这一项,是为了给机器计数的,机器会自动修改</translation></message>
-</context>		
+</context>	
+<context>
+    <name>Args</name>
+	<message><source>ScheduleRule</source><translation>选择任务调度规则</translation></message>	
+	<message><source>schedule_rule_help</source><translation>这里所指的调度的对象是指Pending中的,Waiting中的任务不属于。
+基于过滤器(Filter)的调度:默认的选项,任务的执行顺序会根据开发时所确定的顺序来调度,一般是最优解
+基于先来后到(FIFO)的调度:是会按照下次执行时间进行排序,靠前的先执行
+基于优先级(Priority)的调度:高优先级先于低优先级执行,同优先级按照先来后到顺序</translation></message>	
+</context>	
+<context>
+    <name>Args</name>
+	<message><source>SwitchSoulConfig</source><translation>执行任务前切换御魂</translation></message>	
+	<message><source>Switch Group Team</source><translation>御魂装配分组设置</translation></message>
+	<message><source>switch_group_team_help</source><translation>初始值是不合适的,你需要根据自己的情况设置
+"1,2"表示第一个预设组,第二个队伍
+请使用英文输入法下的逗号
+预设组支持[1-7], 预设队伍支持[1-4]</translation></message>	
+</context>	
 </TS>

+ 96 - 0
module/config/scheduler.py

@@ -0,0 +1,96 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import datetime
+import operator
+
+from cached_property import cached_property
+
+from module.base.filter import Filter
+
+from module.config.config_manual import ConfigManual
+from module.logger import logger
+
+from tasks.Script.config_optimization import ScheduleRule
+
+
+class TaskScheduler:
+    filter = Filter(regex=r"(.*)", attr=["command"]).load(ConfigManual.SCHEDULER_PRIORITY)
+
+
+    @staticmethod
+    def schedule(rule: ScheduleRule, pending: list["Function"]) -> list["Function"]:
+        """
+        执行 任务的调度
+        :param rule:
+        :param pending:
+        :return:
+        """
+        if isinstance(rule, ScheduleRule) is False:
+            logger.error(f"Invalid rule: {rule}")
+            return pending
+        if isinstance(pending, list) is False:
+            logger.error(f"Invalid pending: {pending}")
+            return pending
+
+        # 第一种
+        if rule == ScheduleRule.FILTER:
+            pending_task = filter.apply(pending)
+            return pending_task
+
+        # 第二种
+        if rule == ScheduleRule.FIFO:
+            pending_task = TaskScheduler.fifo(pending)
+            return pending_task
+
+        # 第三种
+        if rule == ScheduleRule.PRIORITY:
+            pending_task = TaskScheduler.priority(pending)
+            return pending_task
+
+    @staticmethod
+    def fifo(pending: list["Function"]) -> list["Function"]:
+        """
+        先来后到,(按照任务的先后顺序进行调度)
+        :param pending:
+        :return:
+        """
+        tasks_pending = sorted(pending, key=operator.attrgetter("next_run"))
+        for task in tasks_pending:
+            # 永远保证 Restart 任务在第一个
+            if task.command == 'Restart':
+                tasks_pending.remove(task)
+                tasks_pending.insert(0, task)
+                break
+        return tasks_pending
+
+    @staticmethod
+    def priority(pending: list["Function"]) -> list["Function"]:
+        """
+        基于优先级,同一个优先级的任务按照先来后到的顺序进行调度,优先级高的任务先调度
+        :param pending:
+        :return:
+        """
+        # 1. 按照优先级进行分组
+        sorted(pending, key=operator.attrgetter("priority"))
+        groups = {}
+        for task in pending:
+            if groups.get(task.priority) is None:
+                groups[task.priority] = []
+            groups[task.priority].append(task)
+        # 2. 对每一组进行先来后到的排序
+        for priority, tasks in groups.items():
+            groups[priority] = TaskScheduler.fifo(tasks)
+
+        # 3.按照顺序合并所有的任务
+        tasks_pending = []
+        for priority in sorted(groups.keys()):
+            tasks_pending.extend(groups[priority])
+
+        return tasks_pending
+
+
+# 测试代码
+if __name__ == '__main__':
+    pass
+

+ 3 - 0
module/notify/notify.py

@@ -0,0 +1,3 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey

+ 10 - 0
tasks/Component/SwitchSoul/switch_soul_config.py

@@ -1,3 +1,13 @@
 # This Python file uses the following encoding: utf-8
 # @author runhey
 # github https://github.com/runhey
+from pydantic import BaseModel, Field
+from enum import Enum
+from datetime import datetime, time
+
+
+
+class SwitchSoulConfig(BaseModel):
+    enable: bool = Field(default=False)
+    switch_group_team: str = Field(default='-1,-1', description='switch_group_team_help')
+

+ 2 - 2
tasks/Orochi/assets.py

@@ -16,11 +16,11 @@ class OrochiAssets:
 	# 组队 
 	I_FORM_TEAM = RuleImage(roi_front=(937,591,100,100), roi_back=(937,591,100,100), threshold=0.8, method="Template matching", file="./tasks/Orochi/o/o_form_team.png")
 	# description 
-	I_OROCHI_LOCK = RuleImage(roi_front=(558,562,31,33), roi_back=(558,562,31,33), threshold=0.8, method="Template matching", file="./tasks/Orochi/o/o_lock.png")
+	I_OROCHI_LOCK = RuleImage(roi_front=(558,562,31,33), roi_back=(558,562,31,33), threshold=0.8, method="Template matching", file="./tasks/Orochi/o/o_orochi_lock.png")
 	# description 
 	I_OROCHI_UNLOCK = RuleImage(roi_front=(560,564,26,29), roi_back=(560,564,26,29), threshold=0.8, method="Template matching", file="./tasks/Orochi/o/o_orochi_unlock.png")
 	# 点击挑战 
-	I_OROCHI_FIRE = RuleImage(roi_front=(1095,577,131,124), roi_back=(1095,577,131,124), threshold=0.6, method="Template matching", file="./tasks/Orochi/o/o_fire.png")
+	I_OROCHI_FIRE = RuleImage(roi_front=(1095,577,131,124), roi_back=(1095,577,131,124), threshold=0.6, method="Template matching", file="./tasks/Orochi/o/o_orochi_fire.png")
 	# 式神录 
 	I_SHI_RECORDS = RuleImage(roi_front=(821,638,48,45), roi_back=(821,638,48,45), threshold=0.8, method="Template matching", file="./tasks/Orochi/o/o_shi_records.png")
 	# description 

+ 2 - 2
tasks/Orochi/o/image.json

@@ -19,7 +19,7 @@
   },
   {
     "itemName": "orochi_lock",
-    "imageName": "o_lock.png",
+    "imageName": "o_orochi_lock.png",
     "roiFront": "558,562,31,33",
     "roiBack": "558,562,31,33",
     "method": "Template matching",
@@ -37,7 +37,7 @@
   },
   {
     "itemName": "orochi_fire",
-    "imageName": "o_fire.png",
+    "imageName": "o_orochi_fire.png",
     "roiFront": "1095,577,131,124",
     "roiBack": "1095,577,131,124",
     "method": "Template matching",

BIN
tasks/Orochi/o/o_matching.png


+ 0 - 0
tasks/Orochi/o/o_fire.png → tasks/Orochi/o/o_orochi_fire.png


+ 0 - 0
tasks/Orochi/o/o_lock.png → tasks/Orochi/o/o_orochi_lock.png


+ 0 - 0
tasks/Orochi/o/o_unlock.png → tasks/Orochi/o/o_orochi_unlock.png


+ 10 - 3
tasks/Orochi/script_task.py

@@ -256,7 +256,14 @@ class ScriptTask(GeneralBattle, GeneralInvite, GeneralBuff, GeneralRoom, GameUi,
         self.check_layer(layer)
         self.check_lock(self.config.orochi.general_battle_config.lock_team_enable)
 
+        def is_in_orochi(screenshot=False) -> bool:
+            if screenshot:
+                self.screenshot()
+            return self.appear(self.I_OROCHI_FIRE)
+
         while 1:
+            self.screenshot()
+
             if self.current_count >= self.limit_count:
                 logger.info('Orochi count limit out')
                 break
@@ -264,12 +271,12 @@ class ScriptTask(GeneralBattle, GeneralInvite, GeneralBuff, GeneralRoom, GameUi,
                 logger.info('Orochi time limit out')
                 break
 
+            if not is_in_orochi():
+                continue
+
             # 点击挑战
             while 1:
                 self.screenshot()
-                if not self.appear(self.I_OROCHI_FIRE):
-                    continue
-
                 if self.appear_then_click(self.I_OROCHI_FIRE, interval=1):
                     pass
 

+ 2 - 0
tasks/RealmRaid/config.py

@@ -7,6 +7,7 @@ from enum import Enum
 from tasks.Component.config_scheduler import Scheduler
 from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
 from tasks.Component.config_base import ConfigBase
+from tasks.Component.SwitchSoul.switch_soul_config import SwitchSoulConfig
 
 class RaidMode(str, Enum):
     NORMAL = 'retreat_four_attack_nine'
@@ -26,6 +27,7 @@ class RealmRaid(ConfigBase):
     scheduler: Scheduler = Field(default_factory=Scheduler)
     raid_config: RaidConfig = Field(default_factory=RaidConfig)
     general_battle_config: GeneralBattleConfig = Field(default_factory=GeneralBattleConfig)
+    switch_soul_config: SwitchSoulConfig = Field(default_factory=SwitchSoulConfig)
 
 
 

+ 1 - 1
tasks/Restart/assets.py

@@ -24,7 +24,7 @@ class RestartAssets:
 	# 全部收取 
 	I_HARVEST_MAIL_ALL = RuleImage(roi_front=(80,622,75,64), roi_back=(80,622,75,64), threshold=0.8, method="Template matching", file="./tasks/Restart/harvest/harvest_harvest_mail_all.png")
 	# 有些邮件需要点击一次 
-	I_HARVEST_MAIL_OPEN = RuleImage(roi_front=(157,122,66,62), roi_back=(139,86,100,487), threshold=0.8, method="Template matching", file="./tasks/Restart/harvest/harvest_harvest_mail_open.png")
+	I_HARVEST_MAIL_OPEN = RuleImage(roi_front=(163,367,45,48), roi_back=(139,86,100,487), threshold=0.9, method="Template matching", file="./tasks/Restart/harvest/harvest_harvest_mail_open.png")
 	# 确认收取邮件 
 	I_HARVEST_MAIL_CONFIRM = RuleImage(roi_front=(687,543,168,64), roi_back=(687,543,168,64), threshold=0.8, method="Template matching", file="./tasks/Restart/harvest/harvest_harvest_mail_confirm.png")
 	# description 

BIN
tasks/Restart/harvest/harvest_harvest_mail_open.png


+ 2 - 2
tasks/Restart/harvest/image.json

@@ -56,10 +56,10 @@
   {
     "itemName": "harvest_mail_open",
     "imageName": "harvest_harvest_mail_open.png",
-    "roiFront": "157,122,66,62",
+    "roiFront": "163,367,45,48",
     "roiBack": "139,86,100,487",
     "method": "Template matching",
-    "threshold": 0.8,
+    "threshold": 0.9,
     "description": "有些邮件需要点击一次"
   },
   {

+ 6 - 0
tasks/Script/config_optimization.py

@@ -11,6 +11,11 @@ class WhenTaskQueueEmpty(str, Enum):
     GOTO_HOME = 'goto_home'
     CLOSE_GAME = 'close_game'
 
+class ScheduleRule(str, Enum):
+    FILTER = 'Filter'  # 默认的基于过滤器,(按照开发者设定的调度规则进行调度)
+    FIFO = 'FIFO'  # 先来后到,(按照任务的先后顺序进行调度)
+    PRIORITY = 'Priority'  # 基于优先级,同一个优先级的任务按照先来后到的顺序进行调度,优先级高的任务先调度
+
 class Optimization(BaseModel):
     screenshot_interval: float = Field(default=0.3,
                                        description='screenshot_interval_help')
@@ -20,4 +25,5 @@ class Optimization(BaseModel):
                                           description='task_hoarding_duration_help')
     when_task_queue_empty: WhenTaskQueueEmpty = Field(default=WhenTaskQueueEmpty.GOTO_MAIN,
                                                       description='when_task_queue_empty_help')
+    schedule_rule: ScheduleRule = Field(default=ScheduleRule.FILTER, description='schedule_rule_help')