make rooms field private
authoralfadur <mail@none>
Mon, 23 Dec 2019 18:55:25 +0300
changeset 15522 4a0b06b03199
parent 15521 1fcce8feace4
child 15523 f4f6060b536c
make rooms field private
rust/hedgewars-server/src/core/server.rs
rust/hedgewars-server/src/handlers.rs
rust/hedgewars-server/src/handlers/common.rs
rust/hedgewars-server/src/handlers/inroom.rs
--- a/rust/hedgewars-server/src/core/server.rs	Mon Dec 23 18:11:15 2019 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Mon Dec 23 18:55:25 2019 +0300
@@ -128,7 +128,7 @@
 
 pub struct HwServer {
     clients: IndexSlab<HwClient>,
-    pub rooms: Slab<HwRoom>,
+    rooms: Slab<HwRoom>,
     pub latest_protocol: u16,
     pub flags: ServerFlags,
     pub greetings: ServerGreetings,
@@ -178,6 +178,21 @@
     }
 
     #[inline]
+    pub fn get_room(&self, room_id: RoomId) -> Option<&HwRoom> {
+        self.rooms.get(room_id)
+    }
+
+    #[inline]
+    pub fn get_room_mut(&mut self, room_id: RoomId) -> Option<&mut HwRoom> {
+        self.rooms.get_mut(room_id)
+    }
+
+    #[inline]
+    pub fn iter_rooms(&self) -> impl Iterator<Item = &HwRoom> {
+        self.rooms.iter().map(|(_, r)| r)
+    }
+
+    #[inline]
     pub fn client_and_room(&self, client_id: ClientId, room_id: RoomId) -> (&HwClient, &HwRoom) {
         (&self.clients[client_id], &self.rooms[room_id])
     }
@@ -187,7 +202,7 @@
         &mut self,
         client_id: ClientId,
         room_id: RoomId,
-    ) -> (&HwClient, &mut HwRoom) {
+    ) -> (&HwClient, &HwRoom) {
         (&self.clients[client_id], &mut self.rooms[room_id])
     }
 
--- a/rust/hedgewars-server/src/handlers.rs	Mon Dec 23 18:11:15 2019 +0300
+++ b/rust/hedgewars-server/src/handlers.rs	Mon Dec 23 18:55:25 2019 +0300
@@ -462,7 +462,7 @@
             response.warn(ROOM_CONFIG_SAVE_FAILED);
         }
         IoResult::LoadRoom(room_id, Some(contents)) => {
-            if let Some(ref mut room) = state.server.rooms.get_mut(room_id) {
+            if let Some(ref mut room) = state.server.get_room_mut(room_id) {
                 match room.set_saves(&contents) {
                     Ok(_) => response.add(server_chat(ROOM_CONFIG_LOADED.to_string()).send_self()),
                     Err(e) => {
--- a/rust/hedgewars-server/src/handlers/common.rs	Mon Dec 23 18:11:15 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/common.rs	Mon Dec 23 18:55:25 2019 +0300
@@ -74,11 +74,9 @@
     let server_msg = ServerMessage(server.get_greetings(client).to_string());
 
     let rooms_msg = Rooms(
-        server
-            .rooms
-            .iter()
-            .filter(|(_, r)| r.protocol_number == client.protocol_number)
-            .flat_map(|(_, r)| r.info(r.master_id.map(|id| server.client(id))))
+        server.iter_rooms()
+            .filter(|r| r.protocol_number == client.protocol_number)
+            .flat_map(|r| r.info(r.master_id.map(|id| server.client(id))))
             .collect(),
     );
 
@@ -342,7 +340,7 @@
     to_client: ClientId,
     response: &mut Response,
 ) {
-    let room = &server.rooms[room_id];
+    let room = server.room(room_id);
     if let Some(id) = room.master_id {
         response.add(
             ClientFlags(
@@ -378,13 +376,13 @@
         }
         VoteType::Map(None) => (),
         VoteType::Map(Some(name)) => {
-            if let Some(location) = server.rooms[room_id].load_config(&name) {
+            if let Some(location) = server.room_mut(room_id).load_config(&name) {
                 response.add(
                     server_chat(location.to_string())
                         .send_all()
                         .in_room(room_id),
                 );
-                let room = &server.rooms[room_id];
+                let room = &server.room(room_id);
                 let room_master = if let Some(id) = room.master_id {
                     Some(server.client(id))
                 } else {
@@ -394,13 +392,13 @@
 
                 for client in server.iter_clients() {
                     if client.room_id == Some(room_id) {
-                        super::common::get_room_config(&server.rooms[room_id], client.id, response);
+                        super::common::get_room_config(server.room(room_id), client.id, response);
                     }
                 }
             }
         }
         VoteType::Pause => {
-            if let Some(ref mut info) = server.rooms[room_id].game_info {
+            if let Some(ref mut info) = server.room_mut(room_id).game_info {
                 info.is_paused = !info.is_paused;
                 response.add(
                     server_chat("Pause toggled.".to_string())
@@ -418,10 +416,10 @@
             let seed = thread_rng().gen_range(0, 1_000_000_000).to_string();
             let cfg = GameCfg::Seed(seed);
             response.add(cfg.to_server_msg().send_all().in_room(room_id));
-            server.rooms[room_id].set_config(cfg);
+            server.room_mut(room_id).set_config(cfg);
         }
         VoteType::HedgehogsPerTeam(number) => {
-            let r = &mut server.rooms[room_id];
+            let r = server.room_mut(room_id);
             let nicks = r.set_hedgehogs_number(number);
 
             response.extend(
--- a/rust/hedgewars-server/src/handlers/inroom.rs	Mon Dec 23 18:11:15 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/inroom.rs	Mon Dec 23 18:55:25 2019 +0300
@@ -390,7 +390,7 @@
                     }
                 }
                 VoteType::Map(None) => {
-                    let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect();
+                    let names: Vec<_> = server.room(room_id).saves.keys().cloned().collect();
                     if names.is_empty() {
                         Some("/callvote map: No maps saved in this room!".to_string())
                     } else {
@@ -422,7 +422,7 @@
                 None => {
                     let msg = voting_description(&kind);
                     let voting = Voting::new(kind, server.room_clients(client_id).collect());
-                    let room = &mut server.rooms[room_id];
+                    let room = server.room_mut(room_id);
                     room.voting = Some(voting);
                     response.add(server_chat(msg).send_all().in_room(room_id));
                     super::common::submit_vote(