implememg global&delegate messages
authoralfadur
Wed, 10 Apr 2019 19:30:08 +0300
changeset 14805 8390d5e4e39c
parent 14804 b3adc030104b
child 14806 a1077e8d26f4
implememg global&delegate messages
rust/hedgewars-server/src/protocol/messages.rs
rust/hedgewars-server/src/server/handlers.rs
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/inroom.rs
--- a/rust/hedgewars-server/src/protocol/messages.rs	Wed Apr 10 18:12:30 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/messages.rs	Wed Apr 10 19:30:08 2019 +0300
@@ -153,13 +153,21 @@
     LegacyReady(bool, Vec<String>),
 }
 
-pub fn server_chat(msg: String) -> HWServerMessage {
+fn special_chat(nick: &str, msg: String) -> HWServerMessage {
     HWServerMessage::ChatMsg {
-        nick: "[server]".to_string(),
+        nick: nick.to_string(),
         msg,
     }
 }
 
+pub fn server_chat(msg: String) -> HWServerMessage {
+    special_chat("[server]", msg)
+}
+
+pub fn global_chat(msg: String) -> HWServerMessage {
+    special_chat("(global notice)", msg)
+}
+
 impl ServerVar {
     pub fn to_protocol(&self) -> Vec<String> {
         match self {
--- a/rust/hedgewars-server/src/server/handlers.rs	Wed Apr 10 18:12:30 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers.rs	Wed Apr 10 19:30:08 2019 +0300
@@ -23,6 +23,7 @@
 mod loggingin;
 
 use self::loggingin::LoginResult;
+use crate::protocol::messages::global_chat;
 use std::fmt::{Formatter, LowerHex};
 
 #[derive(PartialEq)]
@@ -215,6 +216,7 @@
                     HWProtocolMessage::Quit(None) => {
                         common::remove_client(server, response, "User quit".to_string());
                     }
+                    HWProtocolMessage::Global(msg) => response.add(global_chat(msg).send_all()),
                     _ => match server.clients[client_id].room_id {
                         None => lobby::handle(server, client_id, response, message),
                         Some(room_id) => {
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Wed Apr 10 18:12:30 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Wed Apr 10 19:30:08 2019 +0300
@@ -161,6 +161,35 @@
     response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all());
 }
 
+pub fn change_master(
+    server: &mut HWServer,
+    from_id: ClientId,
+    to_id: ClientId,
+    room_id: RoomId,
+    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);
+
+    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()],
+        )
+        .send_all()
+        .in_room(room_id),
+    );
+}
+
 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/inroom.rs	Wed Apr 10 18:12:30 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/inroom.rs	Wed Apr 10 19:30:08 2019 +0300
@@ -564,6 +564,27 @@
             response.add(chat_msg.send_all().in_room(room_id));
             response.add(result.send_all().in_room(room_id));
         }
+        Delegate(nick) => {
+            let delegate_id = server.find_client(&nick).map(|c| (c.id, c.room_id));
+            let client = &server.clients[client_id];
+            if !(client.is_admin() || client.is_master()) {
+                response.add(
+                    Warning("You're not the room master or a server admin!".to_string())
+                        .send_self(),
+                )
+            } else {
+                match delegate_id {
+                    None => response.add(Warning("Player is not online.".to_string()).send_self()),
+                    Some((id, _)) if id == client_id => response
+                        .add(Warning("You're already the room master.".to_string()).send_self()),
+                    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);
+                    }
+                }
+            }
+        }
         _ => warn!("Unimplemented!"),
     }
 }