fix room master change
authoralfadur
Thu, 11 Apr 2019 21:25:31 +0300
changeset 14811 e94fbf6cad2b
parent 14810 18240b308505
child 14812 b889d9e1115f
fix room master change
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/inroom.rs
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Thu Apr 11 21:20:41 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Thu Apr 11 21:25:31 2019 +0300
@@ -164,27 +164,30 @@
 
 pub fn change_master(
     server: &mut HWServer,
-    from_id: ClientId,
-    to_id: ClientId,
     room_id: RoomId,
+    new_master_id: ClientId,
     response: &mut Response,
 ) {
-    server.clients[from_id].set_is_master(false);
-    server.clients[to_id].set_is_master(true);
-    server.rooms[room_id].master_id = Some(to_id);
+    let room = &mut server.rooms[room_id];
+    if let Some(master_id) = room.master_id {
+        server.clients[master_id].set_is_master(false);
+        response.add(
+            ClientFlags(
+                remove_flags(&[Flags::RoomMaster]),
+                vec![server.clients[master_id].nick.clone()],
+            )
+            .send_all()
+            .in_room(room_id),
+        )
+    }
+
+    room.master_id = Some(new_master_id);
+    server.clients[new_master_id].set_is_master(true);
 
     response.add(
         ClientFlags(
             add_flags(&[Flags::RoomMaster]),
-            vec![server.clients[to_id].nick.clone()],
-        )
-        .send_all()
-        .in_room(room_id),
-    );
-    response.add(
-        ClientFlags(
-            remove_flags(&[Flags::RoomMaster]),
-            vec![server.clients[from_id].nick.clone()],
+            vec![server.clients[new_master_id].nick.clone()],
         )
         .send_all()
         .in_room(room_id),
--- a/rust/hedgewars-server/src/server/handlers/inroom.rs	Thu Apr 11 21:20:41 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/inroom.rs	Thu Apr 11 21:25:31 2019 +0300
@@ -605,7 +605,7 @@
                     Some((_, id)) if id != Some(room_id) => response
                         .add(Warning("The player is not in your room.".to_string()).send_self()),
                     Some((id, _)) => {
-                        super::common::change_master(server, client_id, id, room_id, response);
+                        super::common::change_master(server, room_id, id, response);
                     }
                 }
             }