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

+ 12 - 1
gui.py

@@ -2,10 +2,21 @@
 # @author runhey
 # github https://github.com/runhey
 
+from module.gui.context.add import Add
+from module.gui.context.process_manager import ProcessManager
 from module.gui.fluent_app import FluentApp
 
 if __name__ == "__main__":
     # 检查是不是以管理员身份运行,脚本启动的其他进程会继承权限
+
     # 启动一个UI交互的线程,因为信号槽不可跨进程
+    # 后面选择注入上下文快
+    app = FluentApp()
+    add = Add()
+    process_manager = ProcessManager()
+    process_manager.create_all()
+
+    app.set_context_property(add, 'add')
+    app.set_context_property(process_manager, 'process_manager')
     # 启动一个GUI
-    FluentApp().run()
+    app.run()

+ 1 - 1
module/device/control.py

@@ -11,7 +11,7 @@ from module.device.method.window import Window
 from module.logger import logger
 
 
-class Control(Minitouch, Scrcpy, Window, Adb):
+class Control(Minitouch, Scrcpy, Window):
     def handle_control_check(self, button):
         # Will be overridden in Device
         pass

+ 5 - 5
module/device/device.py

@@ -9,7 +9,7 @@ from module.device.control import Control
 from module.device.screenshot import Screenshot
 from module.exception import (GameNotRunningError, GameStuckError,
                               GameTooManyClickError, RequestHumanTakeover)
-from module.handler.assets import GET_MISSION
+# from module.handler.assets import GET_MISSION
 from module.logger import logger
 
 if sys.platform == 'win32':
@@ -65,10 +65,10 @@ class Device(Screenshot, Control, AppControl, EmulatorManager):
         if threshold < diff < 86400 - threshold:
             return False
 
-        if GET_MISSION.match(self.image, offset=True):
-            logger.info('Night commission appear.')
-            self.click(GET_MISSION)
-            return True
+        # if GET_MISSION.match(self.image, offset=True):
+        #     logger.info('Night commission appear.')
+        #     self.click(GET_MISSION)
+        #     return True
 
         return False
 

+ 85 - 0
module/gui/context/add.py

@@ -0,0 +1,85 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+#
+import re
+from pathlib import Path
+
+from module.logger import logger
+
+# 震惊到我姥姥家 除了第一个函数all_script_files是我自己写的
+# 后面的都是github copilot写的
+class Add:
+
+    @classmethod
+    def all_script_files(cls) -> list:
+        """
+        获取所有的脚本文件 除了tmplate
+        :return: ['oas1', 'oas2']
+        """
+        # 获取某个路径的所有json文件名
+        config_path = Path.cwd() / 'config'
+        json_files = config_path.glob('*.json')
+        result = []
+        for json in json_files:
+            if json.stem == 'template':
+                continue
+            result.append(json.stem)
+        return result
+
+    @classmethod
+    def all_json_file(cls) -> list:
+        """
+        获取所有的json文件
+        :return: ['oas1', 'oas2']
+        """
+        # 获取某个路径的所有json文件名
+        config_path = Path.cwd() / 'config'
+        json_files = config_path.glob('*.json')
+        result = []
+        for json in json_files:
+            result.append(json.stem)
+        return result
+
+    @classmethod
+    def copy(cls, file: str, template: str = 'template') -> None:
+        """
+        复制一个配置文件
+        :param file:  不带json后缀
+        :param template:
+        :return:
+        """
+        config_path = Path.cwd() / 'config'
+        template_path = config_path / f'{template}.json'
+        file_path = config_path / f'{file}.json'
+        if file_path.exists():
+            logger.error(f'{file_path} is exists')
+            return
+        with open(template_path, 'r') as f:
+            template_content = f.read()
+        with open(file_path, 'w') as f:
+            f.write(template_content)
+        logger.info(f'copy {template_path} to {file_path}')
+
+    @classmethod
+    def generate_script_name(cls) -> str:
+        """
+        生成一个新的配置的名字
+        :return:
+        """
+        all_script_files = cls.all_script_files()
+        if not all_script_files:
+            return 'oas1'
+        all_script_files.sort()
+        last_script = all_script_files[-1]
+        last_script_number = re.findall(r'\d+', last_script)[0]
+        new_script_number = int(last_script_number) + 1
+        return f'oas{new_script_number}'
+
+if __name__ == "__main__":
+    a = Add()
+    print(a.all_script_files())
+    print(a.all_json_file())
+    print(a.generate_script_name())
+
+

+ 85 - 0
module/gui/context/process_manager.py

@@ -0,0 +1,85 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+from module.gui.process.script_process import ScriptProcess
+from module.gui.context.add import Add
+from module.logger import logger
+
+
+class ProcessManager:
+    """
+    进程管理
+    """
+
+    def __init__(self) -> None:
+        """
+        init
+        """
+        super().__init__()
+        self.processes = {}
+
+    def create_all(self) -> None:
+        """
+        创建所有的配置实例
+        :return:
+        """
+        configs = Add().all_script_files()
+        for config in configs:
+            self.add(config)
+
+    def add(self, config: str) -> None:
+        """
+        add
+        :param config:
+        :return:
+        """
+        if config not in self.processes:
+            self.processes[config] = ScriptProcess(config)
+            self.processes[config].start()
+            logger.info(f'add script {config}')
+        else:
+            logger.info(f'script {config} is already running')
+
+    def remove(self, config: str) -> None:
+        """
+        remove
+        :param config:
+        :return:
+        """
+        if config in self.processes:
+            self.processes[config].stop()
+            del self.processes[config]
+            logger.info(f'remove script {config}')
+        else:
+            logger.info(f'script {config} is not running')
+
+    def restart(self, config: str) -> None:
+        """
+        restart
+        :param config:
+        :return:
+        """
+        if config in self.processes:
+            self.processes[config].restart()
+            logger.info(f'restart script {config}')
+        else:
+            logger.info(f'script {config} is not running')
+
+    def stop_all(self) -> None:
+        """
+        stop_all
+        :return:
+        """
+        for config in self.processes:
+            self.processes[config].stop()
+        logger.info(f'stop all script')
+
+    def restart_all(self) -> None:
+        """
+        restart_all
+        :return:
+        """
+        for config in self.processes:
+            self.processes[config].restart()
+        logger.info(f'restart all script')

+ 16 - 3
module/gui/fluent_app.py

@@ -33,10 +33,23 @@ class FluentApp():
         QGuiApplication.setApplicationName("oas")
         QGuiApplication.setOrganizationName("oas")
 
-    def run(self):
         FluentApp.engine = QQmlApplicationEngine()
-        FluentApp.engine.addImportPath(os.fspath(Path(__file__).resolve().parent ))
+        FluentApp.engine.addImportPath(os.fspath(Path(__file__).resolve().parent))
+
+    @classmethod
+    def run(cls):
         FluentApp.engine.load(os.fspath(Path(get_work_path() / 'module' / 'gui' / 'qml' / 'app.qml')))
         if not FluentApp.engine.rootObjects():
             sys.exit(-1)
-        sys.exit(FluentApp.app.exec())
+        sys.exit(FluentApp.app.exec())
+
+    @classmethod
+    def set_context_property(cls, context, name: str) -> None:
+        """
+        设置上下文
+        :param name:
+        :param context:
+        :return:
+        """
+        FluentApp.engine.rootContext().setContextProperty(name, context)
+

+ 59 - 0
module/gui/process/script_process.py

@@ -0,0 +1,59 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+from multiprocessing import Process
+
+from module.logger import logger
+
+
+class ScriptProcess(Process):
+
+    def __init__(self, config: str) -> None:
+        """
+
+        :param config: 如oas1
+        """
+        super().__init__()
+        self.config = config
+        self.name = config
+        self.daemon = True  # 设置为守护进程,主进程结束,子进程也结束
+
+    @property
+    def alive(self) -> bool:
+        """
+        alive
+        :return:
+        """
+        return self.is_alive()
+
+    def run(self) -> None:
+        """
+        run
+        :return:
+        """
+        try:
+            from script import Script
+            script = Script(config_name=self.config)
+        except:
+            logger.exception(f'run script {self.config} error')
+            raise
+
+    def stop(self) -> None:
+        """
+        stop the  process
+        是强制的,不会等待子进程结束
+        :return:
+        """
+        self.terminate()
+        self.join()
+        logger.info(f'stop script {self.config}')
+
+    def restart(self) -> None:
+        """
+        restart the process
+        :return:
+        """
+        self.stop()
+        self.start()
+        logger.info(f'restart script {self.config}')

+ 1 - 0
script.py

@@ -9,6 +9,7 @@ from module.device.device import Device
 class Script:
     def __init__(self, config_name: str ='oas') -> None:
         self.config_name = config_name
+        print('this is script')
 
     @cached_property
     def device(self) -> Device: