fix room exiting
authoralfadur
Fri, 12 Apr 2019 23:40:32 +0300
changeset 14803 d11481978633
parent 14802 ce2268ae261f
child 14804 e41131607167
fix room exiting
rust/hedgewars-server/src/server/handlers/common.rs
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Fri Apr 12 23:29:58 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Fri Apr 12 23:40:32 2019 +0300
@@ -146,8 +146,8 @@
     response: &mut Response,
     msg: &str,
 ) {
-    if room.players_number > 1 || room.is_fixed() {
-        room.players_number -= 1;
+    room.players_number -= 1;
+    if room.players_number > 0 || room.is_fixed() {
         if client.is_ready() && room.ready_players_number > 0 {
             room.ready_players_number -= 1;
         }
@@ -260,27 +260,31 @@
 
         remove_client_from_room(client, room, response, msg);
 
-        if !room.is_fixed() && room.master_id == None {
-            let new_master_id = server.room_clients(room_id).next();
-            if let Some(new_master_id) = new_master_id {
-                let new_master_nick = server.clients[new_master_id].nick.clone();
-                let room = &mut server.rooms[room_id];
-                room.master_id = Some(new_master_id);
-                server.clients[new_master_id].set_is_master(true);
+        if !room.is_fixed() {
+            if room.players_number == 0 {
+                server.rooms.remove(room_id);
+            } else if room.master_id == None {
+                let new_master_id = server.room_clients(room_id).next();
+                if let Some(new_master_id) = new_master_id {
+                    let new_master_nick = server.clients[new_master_id].nick.clone();
+                    let room = &mut server.rooms[room_id];
+                    room.master_id = Some(new_master_id);
+                    server.clients[new_master_id].set_is_master(true);
 
-                if room.protocol_number < 42 {
-                    room.name = new_master_nick.clone();
+                    if room.protocol_number < 42 {
+                        room.name = new_master_nick.clone();
+                    }
+
+                    room.set_join_restriction(false);
+                    room.set_team_add_restriction(false);
+                    room.set_unregistered_players_restriction(true);
+
+                    response.add(
+                        ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick])
+                            .send_all()
+                            .in_room(room.id),
+                    );
                 }
-
-                room.set_join_restriction(false);
-                room.set_team_add_restriction(false);
-                room.set_unregistered_players_restriction(true);
-
-                response.add(
-                    ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick])
-                        .send_all()
-                        .in_room(room.id),
-                );
             }
         }
     }