PUGE 1 месяц назад
Родитель
Сommit
35f2e250a0
1 измененных файлов с 136 добавлено и 23 удалено
  1. 136 23
      控制vm虚拟机.py

+ 136 - 23
控制vm虚拟机.py

@@ -302,7 +302,7 @@ class VmControlGUI:
         self.pause_duration = tk.StringVar(value="60")
 
         # ===== 脚本B变量 =====
-        self.channel_type = tk.StringVar(value="test_channel")
+        self.channel_type = tk.StringVar(value="控制虚拟机_1")
         self.ws_client = None
         self.is_b_running = False
         self.b_stop_flag = False
@@ -315,6 +315,7 @@ class VmControlGUI:
         self.b_stop_delay_max = tk.StringVar(value="30")
         self.b_wait_after_stop_min = tk.StringVar(value="200")
         self.b_wait_after_stop_max = tk.StringVar(value="500")
+        self.b_pause_duration = tk.StringVar(value="20")
 
         # ===== 激活坐标 =====
         self.ACTIVATE_X = 300
@@ -445,6 +446,10 @@ class VmControlGUI:
         ttk.Label(param_frame_b, text="最大:").grid(row=2, column=2, sticky=tk.W, pady=3, padx=(10, 0))
         ttk.Entry(param_frame_b, textvariable=self.b_wait_after_stop_max, width=8).grid(row=2, column=2, sticky=tk.W, pady=3, padx=(30, 0))
 
+        ttk.Label(param_frame_b, text="暂停延续(秒):").grid(row=3, column=0, sticky=tk.W, pady=3)
+        ttk.Label(param_frame_b, text="最小:").grid(row=3, column=1, sticky=tk.W, pady=3, padx=(10, 0))
+        ttk.Entry(param_frame_b, textvariable=self.b_pause_duration, width=8).grid(row=3, column=1, sticky=tk.W, pady=3, padx=(30, 0))
+
         # 脚本B状态
         self.b_status_label = ttk.Label(self.frame_b, text="脚本B状态: 等待启动信号", foreground="gray")
         self.b_status_label.grid(row=1, column=0, columnspan=4, sticky=tk.W, pady=5)
@@ -539,10 +544,18 @@ class VmControlGUI:
         self.ws_client.send_message("stop", "停止执行")
         self.log("📤 已群发停止指令")
     
+    def send_pause_command(self):
+        """群发暂停指令(让B执行停止序列)"""
+        if not self.ws_client or not self.ws_client.is_connected:
+            self.log("⚠️ WS未连接,无法发送指令")
+            return
+        
+        self.ws_client.send_message("pause", "暂停执行")
+        self.log("📤 已群发暂停指令")
+
     def on_ws_message(self, data):
         """处理WebSocket消息"""
         try:
-            # ===== 打印收到的原始消息 =====
             self.log(f"📨 收到原始消息: {json.dumps(data)}")
             
             msg_type = data.get("type")
@@ -553,12 +566,14 @@ class VmControlGUI:
             
             if msg_type == "start":
                 self.log(f"📨 收到开始指令")
-                # 启动脚本B
                 self.start_script_b()
             elif msg_type == "stop":
                 self.log(f"📨 收到停止指令")
-                # 停止脚本B
                 self.stop_script_b()
+            elif msg_type == "pause":
+                self.log(f"📨 收到暂停指令")
+                # 执行停止序列(2次),但不结束脚本B
+                self.execute_stop_sequence()
             elif msg_type == "close":
                 self.log(f"📨 收到关闭指令,可能是ID冲突")
             else:
@@ -567,6 +582,75 @@ class VmControlGUI:
         except Exception as e:
             self.log(f"处理WS消息失败: {e}")
 
+    def execute_stop_sequence(self):
+        """执行停止序列(2次),不结束脚本B"""
+        try:
+            wait_min = int(self.b_wait_after_stop_min.get()) / 1000.0
+            wait_max = int(self.b_wait_after_stop_max.get()) / 1000.0
+            
+            # 暂停时继续执行的秒数(默认20秒)
+            pause_duration = int(self.b_pause_duration.get())
+            
+            # ===== 1. 继续执行20秒(按- 和 2) =====
+            self.log(f"⏳ 暂停指令,继续执行 {pause_duration} 秒(按- 和 2)")
+            
+            # 按键间隔(30-50秒)
+            interval_min = int(self.b_key_interval_min.get())
+            interval_max = int(self.b_key_interval_max.get())
+            
+            # 记录上次按2的时间
+            last_key2_time = time.time()
+            pause_start_time = time.time()
+            
+            while time.time() - pause_start_time < pause_duration:
+                # 按 - (减号键) - 每300-800ms一次
+                self.log("发送: - (减号) [暂停延续中]")
+                self.press_key(0xBD, False)
+                
+                # 减号间隔 300-800ms
+                key_delay = random.uniform(0.3, 0.8)
+                time.sleep(key_delay)
+                
+                # 检查是否该按 2(30-50秒一次)
+                current_time = time.time()
+                if current_time - last_key2_time >= random.uniform(interval_min, interval_max):
+                    self.log("发送: 2 (数字2) [暂停延续中]")
+                    self.press_key(0x32, False)
+                    last_key2_time = current_time
+            
+            self.log(f"⏳ 延续结束,执行停止序列(2次)")
+            
+            # ===== 2. 执行停止序列(2次) =====
+            for i in range(2):
+                self.log(f"停止序列 {i+1}/2")
+                
+                # 按 =
+                wait_time = random.uniform(wait_min, wait_max)
+                self.log(f"  按 = (延迟 {wait_time*1000:.0f}ms)")
+                self.press_key(0xBB, False)
+                time.sleep(wait_time)
+                
+                # 按 小键盘2
+                wait_time = random.uniform(wait_min, wait_max)
+                self.log(f"  按 小键盘2 (延迟 {wait_time*1000:.0f}ms)")
+                self.press_key(0x62, True)
+                time.sleep(wait_time)
+                
+                # 按 ESC
+                self.log(f"  按 ESC")
+                self.press_key(0x1B, False)
+                
+                # 每次之间等待200-800ms(最后一次不等待)
+                if i < 1:
+                    between_delay = random.uniform(0.2, 0.8)
+                    self.log(f"  等待 {between_delay*1000:.0f}ms 后执行下一次")
+                    time.sleep(between_delay)
+            
+            self.log("✅ 暂停停止序列执行完成")
+            
+        except Exception as e:
+            self.log(f"执行暂停停止序列失败: {e}")
+
     # ========== 脚本B相关 ==========
     def start_script_b(self):
         """启动脚本B"""
@@ -599,7 +683,7 @@ class VmControlGUI:
             interval_min = int(self.b_key_interval_min.get())
             interval_max = int(self.b_key_interval_max.get())
             
-            # 停止延迟(10-30秒)
+            # 停止延迟(10-30秒)- 现在表示继续执行的时间
             stop_delay_min = int(self.b_stop_delay_min.get())
             stop_delay_max = int(self.b_stop_delay_max.get())
             
@@ -633,14 +717,32 @@ class VmControlGUI:
                     self.press_key(0x32, False)
                     last_key2_time = current_time
             
+            # ===== 收到停止指令后,继续执行10-30秒 =====
+            stop_duration = random.uniform(stop_delay_min, stop_delay_max)
+            self.log(f"⏳ 收到停止指令,继续执行 {stop_duration:.1f} 秒后停止")
+            
+            stop_start_time = time.time()
+            while time.time() - stop_start_time < stop_duration:
+                # 继续按 - (减号键)
+                self.log("发送: - (减号) [停止延迟中]")
+                self.press_key(0xBD, False)
+                
+                # 减号间隔 300-800ms
+                key_delay = random.uniform(0.3, 0.8)
+                time.sleep(key_delay)
+                
+                # 检查是否该按 2(30-50秒一次)
+                current_time = time.time()
+                if current_time - last_key2_time >= random.uniform(interval_min, interval_max):
+                    self.log("发送: 2 (数字2) [停止延迟中]")
+                    self.press_key(0x32, False)
+                    last_key2_time = current_time
+            
+            self.log(f"⏳ 延迟结束,开始执行停止序列")
+            
             # ===== 执行停止序列(共3次) =====
             self.log("脚本B停止序列开始")
             
-            # 随机延迟
-            stop_delay = random.uniform(stop_delay_min, stop_delay_max)
-            self.log(f"停止前延迟: {stop_delay:.1f}秒")
-            time.sleep(stop_delay)
-            
             # 执行3次停止序列
             for i in range(3):
                 self.log(f"停止序列 {i+1}/3")
@@ -819,13 +921,7 @@ class VmControlGUI:
             
             if self.ghost_available:
                 if key_code in key_name_map:
-                    ret = pressandreleasekeybyname(key_name_map[key_code])
-                    if ret != 1:
-                        ret = pressandreleasekeybycode(key_code)
-                    return ret == 1
-                else:
-                    ret = pressandreleasekeybycode(key_code)
-                    return ret == 1
+                    pressandreleasekeybyname(key_name_map[key_code])
             else:
                 if extended:
                     win32api.keybd_event(key_code, 0, win32con.KEYEVENTF_EXTENDEDKEY, 0)
@@ -930,7 +1026,7 @@ class VmControlGUI:
         self.log(f"按键: 小键盘8 -> 小键盘2 -> 左键 -> 空格")
         self.log("=" * 50)
 
-        # ===== 只在开始前执行一次鼠标点击激活窗口 =====
+        # 只在开始前执行一次鼠标点击激活窗口
         if not self.stop_flag:
             try:
                 if win32gui.IsWindow(self.vm_hwnd):
@@ -943,14 +1039,13 @@ class VmControlGUI:
             except Exception as e:
                 self.log(f"初始点击失败: {e}")
 
-        # ===== 进入键盘循环 =====
         while not self.stop_flag:
             try:
                 if not win32gui.IsWindow(self.vm_hwnd):
                     self.log("窗口已关闭,停止循环")
                     break
 
-                # 执行一轮按键(不再点击鼠标)
+                # 执行一轮按键
                 for key_code, extended, key_name in keys:
                     if self.stop_flag:
                         break
@@ -967,10 +1062,20 @@ class VmControlGUI:
                 if round_count % 10 == 0:
                     self.log(f"📊 已执行 {round_count} 轮")
 
-                # 暂停检查
+                # ===== 暂停检查 =====
                 current_time = time.time()
                 if current_time - last_pause_time >= random.uniform(pause_min, pause_max):
-                    self.log(f"⏸ 暂停 {pause_dur} 秒")
+                    self.log(f"⏸ 到达暂停时间,通知B执行停止序列")
+                    
+                    # 通知B执行停止序列(2次)
+                    if self.ws_client and self.ws_client.is_connected:
+                        self.ws_client.send_message("pause", "暂停执行,执行停止序列")
+                        self.log("📤 已群发暂停指令到同信道脚本B")
+                    else:
+                        self.log("⚠️ WebSocket未连接,无法通知脚本B")
+                    
+                    # A自己暂停
+                    self.log(f"⏸ A暂停 {pause_dur} 秒")
                     self.status_label.config(text=f"状态: 暂停中 ({pause_dur}s)", foreground="orange")
                     
                     for _ in range(int(pause_dur / 0.5)):
@@ -978,8 +1083,16 @@ class VmControlGUI:
                             break
                         time.sleep(0.5)
                     
-                    self.log("▶ 暂停结束")
+                    self.log("▶ A暂停结束,继续循环")
                     self.status_label.config(text="状态: 运行中", foreground="green")
+                    
+                    # 通知B继续(发送start)
+                    if self.ws_client and self.ws_client.is_connected:
+                        self.ws_client.send_message("start", "继续执行")
+                        self.log("📤 已群发继续指令到同信道脚本B")
+                    else:
+                        self.log("⚠️ WebSocket未连接,无法通知脚本B")
+                    
                     last_pause_time = time.time()
                     continue