fix leaving rooms
authoralfadur
Wed, 27 Mar 2024 02:19:44 +0300
changeset 16001 8ba2b5007c29
parent 16000 d9f1b239b6d7
child 16002 e915ed28726e
fix leaving rooms
rust/hedgewars-server/src/core/server.rs
rust/hedgewars-server/src/handlers/common.rs
rust/hedgewars-server/src/handlers/inlobby.rs
--- a/rust/hedgewars-server/src/core/server.rs	Mon Mar 25 16:05:11 2024 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Wed Mar 27 02:19:44 2024 +0300
@@ -345,7 +345,7 @@
         client_id: ClientId,
         room_id: RoomId,
         room_password: Option<&str>,
-    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
+    ) -> Result<(&HwClient, Option<&HwClient>, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
         use JoinRoomError::*;
         let room = &mut self.rooms[room_id];
         let client = &mut self.clients[client_id];
@@ -359,7 +359,7 @@
             Err(WrongPassword)
         } else if room.is_join_restricted() {
             Err(Restricted)
-        } else if room.is_registration_required() {
+        } else if room.is_registration_required() && !client.is_registered() {
             Err(RegistrationRequired)
         } else if room.players_number == u8::MAX {
             Err(Full)
@@ -368,6 +368,7 @@
             let room_id = room.id;
             Ok((
                 &self.clients[client_id],
+                room.master_id.map(|id| &self.clients[id]),
                 &self.rooms[room_id],
                 self.iter_clients()
                     .filter(move |c| c.room_id == Some(room_id)),
@@ -381,7 +382,7 @@
         client_id: ClientId,
         room_name: &str,
         room_password: Option<&str>,
-    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
+    ) -> Result<(&HwClient, Option<&HwClient>, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
         use JoinRoomError::*;
         let room = self.rooms.iter().find(|(_, r)| r.name == room_name);
         if let Some((_, room)) = room {
@@ -633,7 +634,9 @@
         let was_in_game = client.is_in_game();
         let mut removed_teams = vec![];
 
-        if is_empty && !is_fixed {
+        self.is_room_removed = is_empty && !is_fixed;
+
+        if !self.is_room_removed {
             if client.is_ready() && room.ready_players_number > 0 {
                 room.ready_players_number -= 1;
             }
@@ -663,33 +666,29 @@
         client.set_is_ready(false);
         client.set_is_in_game(false);
 
-        if !is_fixed {
-            if room.players_number == 0 {
-                self.is_room_removed = true
-            } else if room.master_id == None {
-                let protocol_number = room.protocol_number;
-                let new_master_id = self.server.room_client_ids(self.room_id).next();
+        if !self.is_room_removed && room.master_id == None {
+            let protocol_number = room.protocol_number;
+            let new_master_id = self.server.room_client_ids(self.room_id).next();
+
+            if let Some(new_master_id) = new_master_id {
+                let room = self.room_mut();
+                room.master_id = Some(new_master_id);
+                let new_master = &mut self.server.clients[new_master_id];
+                new_master.set_is_master(true);
 
-                if let Some(new_master_id) = new_master_id {
-                    let room = self.room_mut();
-                    room.master_id = Some(new_master_id);
-                    let new_master = &mut self.server.clients[new_master_id];
-                    new_master.set_is_master(true);
+                if protocol_number < 42 {
+                    let nick = new_master.nick.clone();
+                    self.room_mut().name = nick;
+                }
 
-                    if protocol_number < 42 {
-                        let nick = new_master.nick.clone();
-                        self.room_mut().name = nick;
-                    }
-
-                    let room = self.room_mut();
-                    room.set_join_restriction(false);
-                    room.set_team_add_restriction(false);
-                    room.set_unregistered_players_restriction(true);
-                }
+                let room = self.room_mut();
+                room.set_join_restriction(false);
+                room.set_team_add_restriction(false);
+                room.set_unregistered_players_restriction(false);
             }
         }
 
-        if is_empty && !is_fixed {
+        if self.is_room_removed {
             LeaveRoomResult::RoomRemoved
         } else {
             LeaveRoomResult::RoomRemains {
--- a/rust/hedgewars-server/src/handlers/common.rs	Mon Mar 25 16:05:11 2024 +0300
+++ b/rust/hedgewars-server/src/handlers/common.rs	Wed Mar 27 02:19:44 2024 +0300
@@ -106,6 +106,7 @@
 
 pub fn get_room_join_data<'a, I: Iterator<Item = &'a HwClient> + Clone>(
     client: &HwClient,
+    master: Option<&HwClient>,
     room: &HwRoom,
     room_clients: I,
     response: &mut Response,
@@ -234,6 +235,8 @@
 
         get_room_config_impl(room.config(), Destination::ToSelf, response);
     }
+
+    get_room_update(None, room, master, response);
 }
 
 pub fn get_room_join_error(error: JoinRoomError, response: &mut Response) {
@@ -331,8 +334,10 @@
 
             get_remove_teams_data(room.id, was_in_game, removed_teams, response);
 
+            let master = new_master.or(Some(client.id)).map(|id| server.client(id));
+
             response.add(
-                RoomUpdated(room.name.clone(), room.info(Some(&client)))
+                RoomUpdated(room.name.clone(), room.info(master))
                     .send_all()
                     .with_protocol(room.protocol_number),
             );
@@ -359,12 +364,12 @@
 }
 
 pub fn get_room_update(
-    room_name: Option<String>,
+    old_name: Option<String>,
     room: &HwRoom,
     master: Option<&HwClient>,
     response: &mut Response,
 ) {
-    let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master));
+    let update_msg = RoomUpdated(old_name.unwrap_or(room.name.clone()), room.info(master));
     response.add(update_msg.send_all().with_protocol(room.protocol_number));
 }
 
--- a/rust/hedgewars-server/src/handlers/inlobby.rs	Mon Mar 25 16:05:11 2024 +0300
+++ b/rust/hedgewars-server/src/handlers/inlobby.rs	Wed Mar 27 02:19:44 2024 +0300
@@ -66,8 +66,8 @@
         JoinRoom(name, password) => {
             match server.join_room_by_name(client_id, &name, password.as_deref()) {
                 Err(error) => super::common::get_room_join_error(error, response),
-                Ok((client, room, room_clients)) => {
-                    super::common::get_room_join_data(client, room, room_clients, response)
+                Ok((client, master, room, room_clients)) => {
+                    super::common::get_room_join_data(client, master, room, room_clients, response)
                 }
             }
         }
@@ -76,8 +76,8 @@
                 if let Some(room_id) = client.room_id {
                     match server.join_room(client_id, room_id, None) {
                         Err(error) => super::common::get_room_join_error(error, response),
-                        Ok((client, room, room_clients)) => {
-                            super::common::get_room_join_data(client, room, room_clients, response)
+                        Ok((client, master, room, room_clients)) => {
+                            super::common::get_room_join_data(client, master, room, room_clients, response)
                         }
                     }
                 } else {