Merge pull request #9 from OfficialLambdax/welcome/leave-notification

Welcome on full sync- and Leave on disconnect messages
This commit is contained in:
Luuk van Oijen 2023-11-23 09:46:51 +01:00 committed by GitHub
commit efbd2a4bb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -507,6 +507,10 @@ impl Server {
} }
info!("Disconnecting client {}...", id); info!("Disconnecting client {}...", id);
self.broadcast(Packet::Notification(NotificationPacket::player_left( // broadcast left message
self.clients[i].info.as_ref().unwrap().username.clone()
)), Some(i as u8)).await;
if i == self.clients.len() - 1 { if i == self.clients.len() - 1 {
self.clients.remove(i); self.clients.remove(i);
} else { } else {
@ -790,9 +794,8 @@ impl Server {
} else { } else {
let packet_identifier = packet.data[0] as char; let packet_identifier = packet.data[0] as char;
match packet_identifier { match packet_identifier {
'H' => { 'H' => { // Player Full sync with server
// Full sync with server self.clients[client_idx] // tell the new player their playername
self.clients[client_idx]
.queue_packet(Packet::Raw(RawPacket::from_str(&format!( .queue_packet(Packet::Raw(RawPacket::from_str(&format!(
"Sn{}", "Sn{}",
self.clients[client_idx] self.clients[client_idx]
@ -804,6 +807,10 @@ impl Server {
)))) ))))
.await; .await;
self.broadcast(Packet::Notification(NotificationPacket::player_welcome( // welcome the player
self.clients[client_idx].info.as_ref().unwrap().username.clone()
)), Some(client_idx as u8)).await;
// TODO: Sync all existing cars on server (this code is broken) // TODO: Sync all existing cars on server (this code is broken)
for client in &self.clients { for client in &self.clients {
let pid = client.id as usize; let pid = client.id as usize;

View File

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