bit of cleanup of unused old code

This commit is contained in:
Luuk van Oijen committed 2023-11-09 14:54:45 +01:00
1 parent ad85317f60
commit ff5a5204c9
2 files changed
+44 -55

No files matched your search

+35 -35
View File
@@ -57,13 +57,6 @@ pub struct Client {
pub state: ClientState, pub state: ClientState,
pub info: Option<UserData>, pub info: Option<UserData>,
pub cars: Vec<(u8, Car)>, pub cars: Vec<(u8, Car)>,
pub ready: bool,
pub grid_spot: usize,
pub finished: bool,
pub incidents: usize,
pub last_physics_update: Instant,
} }
impl Drop for Client { impl Drop for Client {
@@ -108,13 +101,6 @@ impl Client {
state: ClientState::Connecting, state: ClientState::Connecting,
info: None, info: None,
cars: Vec::new(), cars: Vec::new(),
ready: false,
grid_spot: 0,
finished: false,
incidents: 0,
last_physics_update: Instant::now(),
} }
} }
@@ -155,19 +141,19 @@ impl Client {
let id = self.read_raw(1).await?[0] as usize; let id = self.read_raw(1).await?[0] as usize;
debug!("HandleDownload connection for client id: {}", id); debug!("HandleDownload connection for client id: {}", id);
// TODO: How does this work??? let mut mod_name = {
let mut lock = CLIENT_MOD_PROGRESS.lock().await;
if lock.get(&id).is_none() { lock.insert(id, 0); }
let next_id = lock.get_mut(&id).unwrap();
let mut lock = CLIENT_MOD_PROGRESS.lock().await; let bmod = &config.mods[*next_id]; // TODO: This is a bit uhh yeah
if lock.get(&id).is_none() { lock.insert(id, 0); } debug!("Mod name: {}", bmod.0);
let next_id = lock.get_mut(&id).unwrap();
let bmod = &config.mods[*next_id]; // TODO: This is a bit uhh yeah *next_id += 1;
*next_id += 1; bmod.0.clone()
};
debug!("Mod name: {}", bmod.0);
let mut mod_name = bmod.0.clone();
if mod_name.starts_with("/") == false { if mod_name.starts_with("/") == false {
mod_name = format!("/{mod_name}"); mod_name = format!("/{mod_name}");
} }
@@ -175,9 +161,7 @@ impl Client {
let mod_path = format!("Resources/Client{mod_name}"); let mod_path = format!("Resources/Client{mod_name}");
let file_data = std::fs::read(mod_path)?; let file_data = std::fs::read(mod_path)?;
let packet = RawPacket::from_data(file_data); let packet = RawPacket::from_data(file_data[(file_data.len()/2)..].to_vec());
// let packet1 = RawPacket::from_data(file_data[..(file_data.len()/2)].to_vec());
// let packet2 = RawPacket::from_data(file_data[(file_data.len()/2)..].to_vec());
{ {
let mut lock = self.write_half.lock().await; let mut lock = self.write_half.lock().await;
@@ -186,12 +170,6 @@ impl Client {
if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet)).await { if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet)).await {
error!("{:?}", e); error!("{:?}", e);
} }
// if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet1)).await {
// error!("{:?}", e);
// }
// if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet2)).await {
// error!("{:?}", e);
// }
trace!("Packets sent!"); trace!("Packets sent!");
drop(lock); drop(lock);
} }
@@ -200,6 +178,7 @@ impl Client {
return Ok(false); return Ok(false);
} }
_ => { _ => {
error!("Unknown code: {}", code);
return Err(ClientError::AuthenticateError.into()); return Err(ClientError::AuthenticateError.into());
} }
} }
@@ -286,11 +265,32 @@ impl Client {
} }
'f' => { 'f' => {
// Handle file download // Handle file download
let mut file_name = packet.data_as_string().clone(); let mut mod_name = packet.data_as_string().clone();
file_name.remove(0); // Remove f mod_name.remove(0); // Remove f
debug!("Client requested file {}", file_name); debug!("Client requested file {}", mod_name);
self.write_packet(Packet::Raw(RawPacket::from_str("AG"))).await?; self.write_packet(Packet::Raw(RawPacket::from_str("AG"))).await?;
// Send the first half of the file
if mod_name.starts_with("/") == false {
mod_name = format!("/{mod_name}");
}
let mod_path = format!("Resources/Client{mod_name}");
let file_data = std::fs::read(mod_path)?;
let packet = RawPacket::from_data(file_data[..(file_data.len()/2)].to_vec());
{
let mut lock = self.write_half.lock().await;
lock.writable().await?;
trace!("Sending packets!");
if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet)).await {
error!("{:?}", e);
}
trace!("Packets sent!");
drop(lock);
}
} }
_ => error!("Unknown packet! {:?}", packet), _ => error!("Unknown packet! {:?}", packet),
} }
+9 -20
View File
@@ -86,7 +86,7 @@ impl Server {
let mut client = Client::new(socket); let mut client = Client::new(socket);
match client.authenticate(&cfg_ref).await { match client.authenticate(&cfg_ref).await {
Ok(b) if b => { Ok(isClient) if isClient => {
let mut lock = ci_ref let mut lock = ci_ref
.lock() .lock()
.map_err(|e| error!("{:?}", e)) .map_err(|e| error!("{:?}", e))
@@ -94,13 +94,14 @@ impl Server {
lock.push(client); lock.push(client);
drop(lock); drop(lock);
}, },
Ok(b) => { Ok(isClient) => {
debug!("Downloader?"); debug!("Downloader?");
}, },
Err(e) => { Err(e) => {
error!("Authentication error occured, kicking player..."); error!("Authentication error occured, kicking player...");
error!("{:?}", e); error!("{:?}", e);
client.kick("Failed to authenticate player!").await; client.kick("Failed to authenticate player!").await;
// client.disconnect();
} }
} }
}); });
@@ -247,7 +248,7 @@ impl Server {
Ok(()) Ok(())
} }
// NOTE: Skips all clients that are currently connecting! // NOTE: Skips all clients that are currently connecting or syncing resources!
async fn broadcast(&self, packet: Packet, owner: Option<u8>) { async fn broadcast(&self, packet: Packet, owner: Option<u8>) {
for client in &self.clients { for client in &self.clients {
if let Some(id) = owner { if let Some(id) = owner {
@@ -255,7 +256,7 @@ impl Server {
continue; continue;
} }
} }
if client.state == ClientState::Connecting { if client.state == ClientState::Connecting || client.state == ClientState::SyncingResources {
continue; continue;
} }
client.queue_packet(packet.clone()).await; client.queue_packet(packet.clone()).await;
@@ -505,22 +506,10 @@ impl Server {
'O' => self.parse_vehicle_packet(client_idx, packet).await?, 'O' => self.parse_vehicle_packet(client_idx, packet).await?,
'C' => { 'C' => {
// TODO: Chat filtering? // TODO: Chat filtering?
let packet_data = packet.data_as_string(); // let packet_data = packet.data_as_string();
let message = packet_data.split(":").collect::<Vec<&str>>().get(2).map(|s| s.to_string()).unwrap_or(String::new()); // let message = packet_data.split(":").collect::<Vec<&str>>().get(2).map(|s| s.to_string()).unwrap_or(String::new());
let message = message.trim(); // let message = message.trim();
if message.starts_with("!") { self.broadcast(Packet::Raw(packet), None).await;
if message == "!ready" {
self.clients[client_idx].ready = true;
self.clients[client_idx].queue_packet(Packet::Raw(RawPacket::from_str("C:Server:You are now ready!"))).await;
} else if message == "!pos" {
let car = &self.clients[client_idx].cars.get(0).ok_or(ServerError::CarDoesntExist)?.1;
trace!("car transform (pos/rot/vel/rvel): {:?}", (car.pos, car.rot, car.vel, car.rvel));
} else {
self.clients[client_idx].queue_packet(Packet::Raw(RawPacket::from_str("C:Server:Unknown command!"))).await;
}
} else {
self.broadcast(Packet::Raw(packet), None).await;
}
} }
_ => { _ => {
let string_data = String::from_utf8_lossy(&packet.data[..]); let string_data = String::from_utf8_lossy(&packet.data[..]);