check the room passwords
authoralfadur <mail@none>
Sat, 28 Dec 2019 19:41:05 +0300
changeset 15533 0606f89698e7
parent 15532 f1205f33bf5b
child 15534 bb93e9642b5b
check the room passwords
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	Fri Dec 27 22:36:19 2019 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Sat Dec 28 19:41:05 2019 +0300
@@ -5,9 +5,8 @@
     room::HwRoom,
     types::{ClientId, GameCfg, RoomId, ServerVar, TeamInfo, Vote, VoteType, Voting},
 };
-use crate::{protocol::messages::HwProtocolMessage::Greeting, utils};
+use crate::utils;
 
-use bitflags::_core::hint::unreachable_unchecked;
 use bitflags::*;
 use log::*;
 use slab::Slab;
@@ -23,6 +22,7 @@
 pub enum JoinRoomError {
     DoesntExist,
     WrongProtocol,
+    WrongPassword,
     Full,
     Restricted,
 }
@@ -290,11 +290,6 @@
     }
 
     #[inline]
-    pub fn get_client_nick(&self, client_id: ClientId) -> &str {
-        &self.clients[client_id].nick
-    }
-
-    #[inline]
     pub fn create_room(
         &mut self,
         creator_id: ClientId,
@@ -320,6 +315,7 @@
         &mut self,
         client_id: ClientId,
         room_id: RoomId,
+        room_password: Option<&str>,
     ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
         use JoinRoomError::*;
         let room = &mut self.rooms[room_id];
@@ -327,6 +323,8 @@
 
         if client.protocol_number != room.protocol_number {
             Err(WrongProtocol)
+        } else if room.password.is_some() && room_password != room.password.as_deref() {
+            Err(WrongPassword)
         } else if room.is_join_restricted() {
             Err(Restricted)
         } else if room.players_number == u8::max_value() {
@@ -347,12 +345,13 @@
         &mut self,
         client_id: ClientId,
         room_name: &str,
+        room_password: Option<&str>,
     ) -> Result<(&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 {
             let room_id = room.id;
-            self.join_room(client_id, room_id)
+            self.join_room(client_id, room_id, room_password)
         } else {
             Err(DoesntExist)
         }
@@ -1082,7 +1081,11 @@
     client.room_id = Some(room.id);
     client.set_is_master(true);
     client.set_is_ready(true);
+    client.set_is_in_game(false);
     client.set_is_joined_mid_game(false);
+    client.clan = None;
+    client.teams_in_game = 0;
+    client.team_indices = vec![];
 
     (client, room)
 }
--- a/rust/hedgewars-server/src/handlers/common.rs	Fri Dec 27 22:36:19 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/common.rs	Sat Dec 28 19:41:05 2019 +0300
@@ -159,6 +159,9 @@
     match error {
         JoinRoomError::DoesntExist => response.warn(NO_ROOM),
         JoinRoomError::WrongProtocol => response.warn(WRONG_PROTOCOL),
+        JoinRoomError::WrongPassword => {
+            response.add(Notice("WrongPassword".to_string()).send_self())
+        }
         JoinRoomError::Full => response.warn(ROOM_FULL),
         JoinRoomError::Restricted => response.warn(ROOM_JOIN_RESTRICTED),
     }
--- a/rust/hedgewars-server/src/handlers/inlobby.rs	Fri Dec 27 22:36:19 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/inlobby.rs	Sat Dec 28 19:41:05 2019 +0300
@@ -35,20 +35,17 @@
                 response.add(RoomJoined(vec![client.nick.clone()]).send_self());
                 response.add(
                     ClientFlags(
-                        add_flags(&[Flags::RoomMaster, Flags::Ready]),
+                        add_flags(&[Flags::RoomMaster, Flags::Ready, Flags::InRoom]),
                         vec![client.nick.clone()],
                     )
-                    .send_self(),
-                );
-                response.add(
-                    ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(),
+                    .send_all(),
                 );
             }
         },
         Chat(msg) => {
             response.add(
                 ChatMsg {
-                    nick: server.get_client_nick(client_id).to_string(),
+                    nick: server.client(client_id).nick.clone(),
                     msg,
                 }
                 .send_all()
@@ -56,16 +53,18 @@
                 .but_self(),
             );
         }
-        JoinRoom(name, _password) => match server.join_room_by_name(client_id, &name) {
-            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)
+        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)
+                }
             }
-        },
+        }
         Follow(nick) => {
             if let Some(client) = server.find_client(&nick) {
                 if let Some(room_id) = client.room_id {
-                    match server.join_room(client_id, 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)