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);
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 {
self.clients.remove(i);
} else {
@ -790,9 +794,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]
@ -804,6 +807,10 @@ impl Server {
))))
.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)
for client in &self.clients {
let pid = client.id as usize;

View File

@ -54,6 +54,12 @@ impl NotificationPacket {
pub fn new<S: Into<String>>(msg: S) -> Self {
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: