implement follow message
authoralfadur
Thu, 11 Apr 2019 01:42:14 +0300
changeset 14792 0e64acbc3f8b
parent 14791 8ecdb5c6bb2a
child 14793 6dea1ca64992
implement follow message
rust/hedgewars-server/src/protocol/messages.rs
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/lobby.rs
--- a/rust/hedgewars-server/src/protocol/messages.rs	Thu Apr 11 01:13:29 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/messages.rs	Thu Apr 11 01:42:14 2019 +0300
@@ -130,6 +130,7 @@
     RoomLeft(String, String),
     RoomRemove(String),
     RoomUpdated(String, Vec<String>),
+    Joining(String),
     TeamAdd(Vec<String>),
     TeamRemove(String),
     TeamAccepted(String),
@@ -379,6 +380,7 @@
             RoomLeft(nick, msg) => msg!["LEFT", nick, msg],
             RoomRemove(name) => msg!["ROOM", "DEL", name],
             RoomUpdated(name, info) => construct_message(&["ROOM", "UPD", name], &info),
+            Joining(name) => msg!["JOINING", name],
             TeamAdd(info) => construct_message(&["ADD_TEAM"], &info),
             TeamRemove(name) => msg!["REMOVE_TEAM", name],
             TeamAccepted(name) => msg!["TEAM_ACCEPTED", name],
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Thu Apr 11 01:13:29 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Thu Apr 11 01:42:14 2019 +0300
@@ -191,6 +191,33 @@
     );
 }
 
+pub fn enter_room(
+    server: &mut HWServer,
+    client_id: ClientId,
+    room_id: RoomId,
+    response: &mut Response,
+) {
+    let nick = server.clients[client_id].nick.clone();
+    server.move_to_room(client_id, room_id);
+
+    response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
+    response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all());
+    let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id));
+    response.add(RoomJoined(nicks).send_self());
+
+    let room = &server.rooms[room_id];
+
+    if !room.greeting.is_empty() {
+        response.add(
+            ChatMsg {
+                nick: "[greeting]".to_string(),
+                msg: room.greeting.clone(),
+            }
+            .send_self(),
+        );
+    }
+}
+
 pub fn exit_room(server: &mut HWServer, client_id: ClientId, response: &mut Response, msg: &str) {
     let client = &mut server.clients[client_id];
 
--- a/rust/hedgewars-server/src/server/handlers/lobby.rs	Thu Apr 11 01:13:29 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/lobby.rs	Thu Apr 11 01:42:14 2019 +0300
@@ -6,6 +6,7 @@
         add_flags, remove_flags, HWProtocolMessage, HWServerMessage::*, ProtocolFlags as Flags,
     },
     server::{
+        client::HWClient,
         core::HWServer,
         coretypes::{ClientId, ServerVar},
     },
@@ -83,30 +84,23 @@
                 } else if room.players_number == u8::max_value() {
                     response.add(Warning("This room is already full".to_string()).send_self());
                 } else if let Some(room_id) = room_id {
-                    let nick = client.nick.clone();
-                    server.move_to_room(client_id, room_id);
-
-                    response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
-                    response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all());
-                    let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id));
-                    response.add(RoomJoined(nicks).send_self());
-
-                    let room = &server.rooms[room_id];
-
-                    if !room.greeting.is_empty() {
-                        response.add(
-                            ChatMsg {
-                                nick: "[greeting]".to_string(),
-                                msg: room.greeting.clone(),
-                            }
-                            .send_self(),
-                        );
-                    }
+                    super::common::enter_room(server, client_id, room_id, response);
                 }
             } else {
                 response.add(Warning("No such room.".to_string()).send_self());
             }
         }
+        Follow(nick) => {
+            if let Some(HWClient {
+                room_id: Some(room_id),
+                ..
+            }) = server.find_client(&nick)
+            {
+                let room = &server.rooms[*room_id];
+                response.add(Joining(room.name.clone()).send_self());
+                super::common::enter_room(server, client_id, *room_id, response);
+            }
+        }
         SetServerVar(var) => {
             if !server.clients[client_id].is_admin() {
                 response.add(Warning("Access denied.".to_string()).send_self());