@@ -12,8 +12,8 @@ use crate::packets::p767::{c2s, s2c};
1212// Идентификаторы клиентов
1313#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
1414pub enum ClientId {
15- C ,
16- L ,
15+ Cheat ,
16+ Legit ,
1717}
1818
1919// События для регулятора
@@ -22,7 +22,6 @@ pub enum Event {
2222 ClientData ( ClientId , RawPacket ) ,
2323 ClientDisconnected ( ClientId ) ,
2424 ServerData ( RawPacket ) ,
25- // SwitchActive,
2625}
2726
2827// Регулятор
@@ -33,7 +32,8 @@ pub struct Controller {
3332 remote_tx : Sender < RawPacket > ,
3433 event_rx : Receiver < Event > ,
3534 threshold : Option < i32 > ,
36- both_active : bool ,
35+ cheat_active : bool ,
36+ legit_active : bool ,
3737 position : s2c:: Position ,
3838 last_action : i16 ,
3939 need_sync : bool ,
@@ -57,7 +57,8 @@ impl Controller {
5757 remote_tx,
5858 event_rx,
5959 threshold,
60- both_active : true ,
60+ cheat_active : true ,
61+ legit_active : true ,
6162 position : s2c:: Position {
6263 x : 0.0 ,
6364 y : 0.0 ,
@@ -77,42 +78,38 @@ impl Controller {
7778 match event {
7879 Event :: ClientData ( client_id, packet) => {
7980 if client_id == self . active_client {
80- if self . both_active {
81+ if self . both_active ( ) {
8182 if let Ok ( Some ( packet) ) = packet. try_uncompress ( self . threshold ) {
8283 match packet. packet_id . 0 {
8384 0x12 | 0x13 | 0x14 => {
8485 let _ = self . update_position ( & packet) ;
8586 // Уведомляем пассивного клиента
8687 let passive = match self . active_client {
87- ClientId :: C => ClientId :: L ,
88- ClientId :: L => ClientId :: C ,
88+ ClientId :: Cheat => ClientId :: Legit ,
89+ ClientId :: Legit => ClientId :: Cheat ,
8990 } ;
9091
91- let notice = match self . threshold {
92- Some ( t) => self
93- . position
94- . as_uncompressed ( )
95- . unwrap ( )
96- . compress ( t as usize )
97- . unwrap ( )
98- . to_raw_packet ( ) ,
99- None => self
100- . position
101- . as_uncompressed ( )
102- . unwrap ( )
103- . to_raw_packet ( )
104- . unwrap ( ) ,
105- } ;
92+ let notice = self
93+ . position
94+ . as_uncompressed ( )
95+ . unwrap ( )
96+ . compress_to_raw ( self . threshold )
97+ . unwrap ( ) ;
10698 match passive {
107- ClientId :: C => self . cheat_tx . send ( notice) . await . ok ( ) ,
108- ClientId :: L => self . legit_tx . send ( notice) . await . ok ( ) ,
99+ ClientId :: Cheat => {
100+ self . cheat_tx . send ( notice) . await . ok ( )
101+ }
102+ ClientId :: Legit => {
103+ self . legit_tx . send ( notice) . await . ok ( )
104+ }
109105 } ;
110106 }
111107
112108 _ => { }
113109 }
114110 }
115111 }
112+
116113 if self . bypass {
117114 if let Ok ( Some ( packet) ) = packet. try_uncompress ( self . threshold ) {
118115 if packet. packet_id . 0 == 0x07 {
@@ -135,19 +132,11 @@ impl Controller {
135132 println ! ( "Синхронизация: Отправка {}" , i) ;
136133 let mut new_transaction = t. clone ( ) ;
137134 new_transaction. action = i;
138- let new_transaction = match self . threshold {
139- Some ( t) => new_transaction
140- . as_uncompressed ( )
141- . unwrap ( )
142- . compress ( t as usize )
143- . unwrap ( )
144- . to_raw_packet ( ) ,
145- None => new_transaction
146- . as_uncompressed ( )
147- . unwrap ( )
148- . to_raw_packet ( )
149- . unwrap ( ) ,
150- } ;
135+ let new_transaction = new_transaction
136+ . as_uncompressed ( )
137+ . unwrap ( )
138+ . compress_to_raw ( self . threshold )
139+ . unwrap ( ) ;
151140 self . remote_tx
152141 . send ( new_transaction)
153142 . await
@@ -164,44 +153,44 @@ impl Controller {
164153 }
165154 }
166155 }
167- // Перенаправляем активного клиента на сервер
156+
168157 if let Err ( e) = self . remote_tx . send ( packet) . await {
169- eprintln ! ( "Server send error: {}" , e) ;
158+ println ! ( "Ошибка отправки пакета на сервер: {}" , e) ;
159+ return ;
170160 }
171161 }
172162 }
173163 Event :: ClientDisconnected ( client_id) => {
174- if !self . both_active {
164+ if !self . both_active ( ) {
175165 println ! ( "Оба клиента отключились" ) ;
176166 return ;
177167 }
178- self . both_active = false ;
168+ match client_id {
169+ ClientId :: Cheat => self . cheat_active = false ,
170+ ClientId :: Legit => self . legit_active = false ,
171+ } ;
172+
179173 if self . active_client == client_id {
180- // Автоматическое переключение при отключении активного
181174 self . active_client = match client_id {
182- ClientId :: C => ClientId :: L ,
183- ClientId :: L => ClientId :: C ,
175+ ClientId :: Cheat => ClientId :: Legit ,
176+ ClientId :: Legit => ClientId :: Cheat ,
184177 } ;
185178 println ! ( "Переключился на {:?}" , self . active_client) ;
186179 self . need_sync = true ;
187180 }
188181 }
189182 Event :: ServerData ( packet) => {
190- // Рассылаем всем клиентам
191- self . cheat_tx . send ( packet. clone ( ) ) . await . ok ( ) ;
192- self . legit_tx . send ( packet) . await . ok ( ) ;
193- } // Event::SwitchActive => {
194- // if self.can_switch {
195- // self.active_client = match self.active_client {
196- // ClientId::C => ClientId::L,
197- // ClientId::L => ClientId::C,
198- // };
199- // println!(
200- // "Manually switched active client to {:?}",
201- // self.active_client
202- // );
203- // }
204- // }
183+ if self . cheat_active {
184+ if self . cheat_tx . send ( packet. clone ( ) ) . await . is_err ( ) {
185+ self . cheat_active = false ;
186+ }
187+ }
188+ if self . legit_active {
189+ if self . legit_tx . send ( packet) . await . is_err ( ) {
190+ self . legit_active = false ;
191+ }
192+ }
193+ }
205194 }
206195 }
207196 }
@@ -233,6 +222,10 @@ impl Controller {
233222
234223 Ok ( ( ) )
235224 }
225+
226+ fn both_active ( & self ) -> bool {
227+ ( self . cheat_active == self . legit_active ) && self . cheat_active
228+ }
236229}
237230
238231// Запуск клиентского обработчика
@@ -249,7 +242,6 @@ pub async fn run_client(
249242 loop {
250243 match RawPacket :: read( & mut client_read) . await {
251244 Ok ( packet) => {
252- if client_id == ClientId :: C { }
253245 if event_tx
254246 . send( Event :: ClientData ( client_id, packet) )
255247 . await
0 commit comments