send cleared flags on room join
authoralfadur <mail@none>
Mon, 30 Dec 2019 17:25:44 +0300
changeset 15556 bb93e9642b5b
parent 15555 0606f89698e7
child 15557 3f6a7a867040
send cleared flags on room join
rust/hedgewars-server/src/core/room.rs
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
rust/hedgewars-server/src/handlers/strings.rs
--- a/rust/hedgewars-server/src/core/room.rs	Sat Dec 28 19:41:05 2019 +0300
+++ b/rust/hedgewars-server/src/core/room.rs	Mon Dec 30 17:25:44 2019 +0300
@@ -62,7 +62,7 @@
         const FIXED = 0b0000_0001;
         const RESTRICTED_JOIN = 0b0000_0010;
         const RESTRICTED_TEAM_ADD = 0b0000_0100;
-        const RESTRICTED_UNREGISTERED_PLAYERS = 0b0000_1000;
+        const REGISTRATION_REQUIRED = 0b0000_1000;
     }
 }
 
@@ -252,9 +252,8 @@
     pub fn is_team_add_restricted(&self) -> bool {
         self.flags.contains(RoomFlags::RESTRICTED_TEAM_ADD)
     }
-    pub fn are_unregistered_players_restricted(&self) -> bool {
-        self.flags
-            .contains(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS)
+    pub fn is_registration_required(&self) -> bool {
+        self.flags.contains(RoomFlags::REGISTRATION_REQUIRED)
     }
 
     pub fn set_is_fixed(&mut self, value: bool) {
@@ -267,8 +266,7 @@
         self.flags.set(RoomFlags::RESTRICTED_TEAM_ADD, value)
     }
     pub fn set_unregistered_players_restriction(&mut self, value: bool) {
-        self.flags
-            .set(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, value)
+        self.flags.set(RoomFlags::REGISTRATION_REQUIRED, value)
     }
 
     fn flags_string(&self) -> String {
@@ -282,7 +280,7 @@
         if self.is_join_restricted() {
             result += "j"
         }
-        if self.are_unregistered_players_restricted() {
+        if self.is_registration_required() {
             result += "r"
         }
         result
--- a/rust/hedgewars-server/src/core/server.rs	Sat Dec 28 19:41:05 2019 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Mon Dec 30 17:25:44 2019 +0300
@@ -25,6 +25,7 @@
     WrongPassword,
     Full,
     Restricted,
+    RegistrationRequired,
 }
 
 #[derive(Debug)]
@@ -206,7 +207,7 @@
     }
 
     #[inline]
-    pub fn iter_clients(&self) -> impl Iterator<Item = &HwClient> {
+    pub fn iter_clients(&self) -> impl Iterator<Item = &HwClient> + Clone {
         self.clients.iter().map(|(_, c)| c)
     }
 
@@ -323,10 +324,15 @@
 
         if client.protocol_number != room.protocol_number {
             Err(WrongProtocol)
-        } else if room.password.is_some() && room_password != room.password.as_deref() {
+        } else if room.password.is_some()
+            && room_password != room.password.as_deref()
+            && !client.has_super_power()
+        {
             Err(WrongPassword)
         } else if room.is_join_restricted() {
             Err(Restricted)
+        } else if room.is_registration_required() {
+            Err(RegistrationRequired)
         } else if room.players_number == u8::max_value() {
             Err(Full)
         } else {
@@ -335,7 +341,8 @@
             Ok((
                 &self.clients[client_id],
                 &self.rooms[room_id],
-                self.clients.iter().map(|(_, c)| c),
+                self.iter_clients()
+                    .filter(move |c| c.room_id == Some(room_id)),
             ))
         }
     }
--- a/rust/hedgewars-server/src/handlers.rs	Sat Dec 28 19:41:05 2019 +0300
+++ b/rust/hedgewars-server/src/handlers.rs	Mon Dec 30 17:25:44 2019 +0300
@@ -270,8 +270,11 @@
                     LoginResult::Unchanged => (),
                     LoginResult::Complete => {
                         if let Some(client) = state.anteroom.remove_client(client_id) {
+                            let is_checker = client.is_checker;
                             state.server.add_client(client_id, client);
-                            common::get_lobby_join_data(&state.server, response);
+                            if !is_checker {
+                                common::get_lobby_join_data(&state.server, response);
+                            }
                         }
                     }
                     LoginResult::Exit => {
--- a/rust/hedgewars-server/src/handlers/common.rs	Sat Dec 28 19:41:05 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/common.rs	Mon Dec 30 17:25:44 2019 +0300
@@ -107,18 +107,35 @@
     response: &mut Response,
 ) {
     #[inline]
-    fn collect_nicks<'a, I, F>(clients: I, f: F) -> Vec<String>
+    fn partition_nicks<'a, I, F>(clients: I, f: F) -> (Vec<String>, Vec<String>)
     where
-        I: Iterator<Item = &'a HwClient>,
+        I: Iterator<Item = &'a HwClient> + Clone,
         F: Fn(&&'a HwClient) -> bool,
     {
-        clients.filter(f).map(|c| &c.nick).cloned().collect()
+        (
+            clients
+                .clone()
+                .filter(|c| f(c))
+                .map(|c| &c.nick)
+                .cloned()
+                .collect(),
+            clients
+                .filter(|c| !f(c))
+                .map(|c| &c.nick)
+                .cloned()
+                .collect(),
+        )
     }
 
     let nick = client.nick.clone();
-    response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room.id));
+    response.add(
+        RoomJoined(vec![nick.clone()])
+            .send_all()
+            .in_room(room.id)
+            .but_self(),
+    );
     response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all());
-    let nicks = collect_nicks(room_clients.clone(), |c| c.room_id == Some(room.id));
+    let nicks = room_clients.clone().map(|c| c.nick.clone()).collect();
     response.add(RoomJoined(nicks).send_self());
 
     get_room_teams(room, client.id, response);
@@ -127,20 +144,28 @@
     let mut flag_selectors = [
         (
             Flags::RoomMaster,
-            collect_nicks(room_clients.clone(), |c| c.is_master()),
+            partition_nicks(room_clients.clone(), |c| c.is_master()),
         ),
         (
             Flags::Ready,
-            collect_nicks(room_clients.clone(), |c| c.is_ready()),
+            partition_nicks(room_clients.clone(), |c| c.is_ready()),
         ),
         (
             Flags::InGame,
-            collect_nicks(room_clients.clone(), |c| c.is_in_game()),
+            partition_nicks(room_clients.clone(), |c| c.is_in_game()),
         ),
     ];
 
-    for (flag, nicks) in &mut flag_selectors {
-        response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self());
+    for (flag, (set_nicks, cleared_nicks)) in &mut flag_selectors {
+        if !set_nicks.is_empty() {
+            response.add(ClientFlags(add_flags(&[*flag]), replace(set_nicks, vec![])).send_self());
+        }
+
+        if !cleared_nicks.is_empty() {
+            response.add(
+                ClientFlags(remove_flags(&[*flag]), replace(cleared_nicks, vec![])).send_self(),
+            );
+        }
     }
 
     if !room.greeting.is_empty() {
@@ -158,12 +183,13 @@
     use super::strings::*;
     match error {
         JoinRoomError::DoesntExist => response.warn(NO_ROOM),
-        JoinRoomError::WrongProtocol => response.warn(WRONG_PROTOCOL),
+        JoinRoomError::WrongProtocol => response.warn(INCOMPATIBLE_ROOM_PROTOCOL),
         JoinRoomError::WrongPassword => {
             response.add(Notice("WrongPassword".to_string()).send_self())
         }
         JoinRoomError::Full => response.warn(ROOM_FULL),
         JoinRoomError::Restricted => response.warn(ROOM_JOIN_RESTRICTED),
+        JoinRoomError::RegistrationRequired => response.warn(ROOM_REGISTRATION_REQUIRED),
     }
 }
 
--- a/rust/hedgewars-server/src/handlers/inroom.rs	Sat Dec 28 19:41:05 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/inroom.rs	Mon Dec 30 17:25:44 2019 +0300
@@ -100,7 +100,7 @@
     match msg {
         ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN,
         ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD,
-        ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS,
+        ToggleRegisteredOnly => RoomFlags::REGISTRATION_REQUIRED,
         _ => RoomFlags::empty(),
     }
 }
--- a/rust/hedgewars-server/src/handlers/strings.rs	Sat Dec 28 19:41:05 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/strings.rs	Mon Dec 30 17:25:44 2019 +0300
@@ -1,38 +1,40 @@
-pub const ACCESS_DENIED: &str = "Access denied.";
-pub const AUTHENTICATION_FAILED: &str = "Authentication failed";
-pub const BAD_NUMBER: &str = "Bad number.";
-pub const ILLEGAL_CLIENT_NAME: &str = "Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}";
-pub const ILLEGAL_ROOM_NAME: &str = "Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}";
-pub const NICKNAME_PROVIDED: &str = "Nickname already provided.";
-pub const NO_CHECKER_RIGHTS: &str = "No checker rights";
-pub const NO_ROOM: &str = "No such room.";
-pub const NO_TEAM: &str = "No such team.";
-pub const NO_TEAM_TO_REMOVE: &str = "Error: The team you tried to remove does not exist.";
-pub const NO_USER: &str = "No such user.";
-pub const NOT_MASTER: &str = "You're not the room master!";
-pub const PROTOCOL_PROVIDED: &str = "Protocol already known.";
-pub const PROTOCOL_TOO_OLD: &str = "Protocol version is too old";
-pub const REPLAY_LOAD_FAILED: &str = "Could't load the replay";
-pub const REPLAY_NOT_SUPPORTED: &str = "This server does not support replays!";
-pub const REGISTRATION_REQUIRED: &str = "This server only allows registered users to join.";
-pub const REGISTERED_ONLY_ENABLED: &str =
-    "This server no longer allows unregistered players to join.";
-pub const REGISTERED_ONLY_DISABLED: &str = "This server now allows unregistered players to join.";
-pub const ROOM_CONFIG_SAVE_FAILED: &str = "Unable to save the room configs.";
-pub const ROOM_CONFIG_LOAD_FAILED: &str = "Unable to load the room configs.";
-pub const ROOM_CONFIG_DESERIALIZE_FAILED: &str = "Unable to deserialize the room configs.";
-pub const ROOM_CONFIG_LOADED: &str = "Room configs loaded successfully.";
-pub const ROOM_CONFIG_SAVED: &str = "Room configs saved successfully.";
-pub const ROOM_EXISTS: &str = "A room with the same name already exists.";
-pub const ROOM_FULL: &str = "This room is already full.";
-pub const ROOM_JOIN_RESTRICTED: &str = "Access denied. This room currently doesn't allow joining.";
-pub const ROUND_IN_PROGRESS: &str = "Joining not possible: Round is in progress.";
-pub const SUPER_POWER: &str = "Super power activated.";
-pub const TEAM_EXISTS: &str = "There's already a team with same name in the list.";
-pub const TEAM_NOT_OWNED: &str = "You can't remove a team you don't own.";
-pub const TEAM_ADD_RESTRICTED: &str = "This room currently does not allow adding new teams.";
-pub const TOO_MANY_HEDGEHOGS: &str = "Too many hedgehogs!";
-pub const TOO_MANY_TEAMS: &str = "Too many teams!";
-pub const USER_OFFLINE: &str = "Player is not online.";
-pub const VARIABLE_UPDATED: &str = "Server variable has been updated.";
-pub const WRONG_PROTOCOL: &str = "Room version incompatible to your Hedgewars version!";
+pub const ACCESS_DENIED: &str = "Access denied.";
+pub const AUTHENTICATION_FAILED: &str = "Authentication failed";
+pub const BAD_NUMBER: &str = "Bad number.";
+pub const ILLEGAL_CLIENT_NAME: &str = "Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}";
+pub const ILLEGAL_ROOM_NAME: &str = "Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}";
+pub const NICKNAME_PROVIDED: &str = "Nickname already provided.";
+pub const NO_CHECKER_RIGHTS: &str = "No checker rights";
+pub const NO_ROOM: &str = "No such room.";
+pub const NO_TEAM: &str = "No such team.";
+pub const NO_TEAM_TO_REMOVE: &str = "Error: The team you tried to remove does not exist.";
+pub const NO_USER: &str = "No such user.";
+pub const NOT_MASTER: &str = "You're not the room master!";
+pub const PROTOCOL_PROVIDED: &str = "Protocol already known.";
+pub const PROTOCOL_TOO_OLD: &str = "Protocol version is too old";
+pub const REPLAY_LOAD_FAILED: &str = "Could't load the replay";
+pub const REPLAY_NOT_SUPPORTED: &str = "This server does not support replays!";
+pub const REGISTRATION_REQUIRED: &str = "This server only allows registered users to join.";
+pub const REGISTERED_ONLY_ENABLED: &str =
+    "This server no longer allows unregistered players to join.";
+pub const REGISTERED_ONLY_DISABLED: &str = "This server now allows unregistered players to join.";
+pub const ROOM_CONFIG_SAVE_FAILED: &str = "Unable to save the room configs.";
+pub const ROOM_CONFIG_LOAD_FAILED: &str = "Unable to load the room configs.";
+pub const ROOM_CONFIG_DESERIALIZE_FAILED: &str = "Unable to deserialize the room configs.";
+pub const ROOM_CONFIG_LOADED: &str = "Room configs loaded successfully.";
+pub const ROOM_CONFIG_SAVED: &str = "Room configs saved successfully.";
+pub const ROOM_EXISTS: &str = "A room with the same name already exists.";
+pub const ROOM_FULL: &str = "This room is already full.";
+pub const ROOM_JOIN_RESTRICTED: &str = "Access denied. This room currently doesn't allow joining.";
+pub const ROUND_IN_PROGRESS: &str = "Joining not possible: Round is in progress.";
+pub const ROOM_REGISTRATION_REQUIRED: &str =
+    "Access denied. This room is for registered users only.";
+pub const SUPER_POWER: &str = "Super power activated.";
+pub const TEAM_EXISTS: &str = "There's already a team with same name in the list.";
+pub const TEAM_NOT_OWNED: &str = "You can't remove a team you don't own.";
+pub const TEAM_ADD_RESTRICTED: &str = "This room currently does not allow adding new teams.";
+pub const TOO_MANY_HEDGEHOGS: &str = "Too many hedgehogs!";
+pub const TOO_MANY_TEAMS: &str = "Too many teams!";
+pub const USER_OFFLINE: &str = "Player is not online.";
+pub const VARIABLE_UPDATED: &str = "Server variable has been updated.";
+pub const INCOMPATIBLE_ROOM_PROTOCOL: &str = "Room version incompatible to your Hedgewars version!";