welcome & leave

This commit is contained in:
Lambdax
2023-11-22 14:02:23 +01:00
parent cde157fdfe
commit de398c0208
2 changed files with 19 additions and 3 deletions

View File

@@ -512,11 +512,19 @@ impl Server {
}
info!("Disconnecting client {}...", id);
for client in &self.clients {
if client.id == i as u8 {continue}
client.queue_packet(Packet::Notification(NotificationPacket::left(
self.clients[i].info.as_ref().unwrap().username.clone()
))).await;
}
if i == self.clients.len() - 1 {
self.clients.remove(i);
} else {
self.clients.swap_remove(i);
}
info!("Client {} disconnected!", id);
}
}
@@ -795,9 +803,8 @@ impl Server {
} else {
let packet_identifier = packet.data[0] as char;
match packet_identifier {
'H' => {
// Full sync with server
self.clients[client_idx]
'H' => { // Player Full sync with server
self.clients[client_idx] // tell the new player their playername
.queue_packet(Packet::Raw(RawPacket::from_str(&format!(
"Sn{}",
self.clients[client_idx]
@@ -809,6 +816,13 @@ impl Server {
))))
.await;
for client in &self.clients { // welcome the player
if client.id == client_idx as u8 {continue}
client.queue_packet(Packet::Notification(NotificationPacket::welcome(
self.clients[client_idx].info.as_ref().unwrap().username.clone()
))).await;
}
// TODO: Sync all existing cars on server (this code is broken)
for client in &self.clients {
let pid = client.id as usize;

View File

@@ -54,6 +54,8 @@ impl NotificationPacket {
pub fn new<S: Into<String>>(msg: S) -> Self {
Self(format!("J{}", msg.into()))
}
pub fn welcome<S: Into<String>>(msg: S) -> Self { Self(format!("JWelcome {}!", msg.into()))}
pub fn left<S: Into<String>>(msg: S) -> Self { Self(format!("L{} left the server!", msg.into()))}
}
/// Protocol: