add typed protocol flags
authoralfadur
Wed, 10 Apr 2019 16:14:33 +0300
changeset 14782 50fcef24003f
parent 14781 01f8ab45f806
child 14783 b3adc030104b
add typed protocol flags
rust/hedgewars-server/src/protocol/messages.rs
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/inroom.rs
rust/hedgewars-server/src/server/handlers/lobby.rs
--- a/rust/hedgewars-server/src/protocol/messages.rs	Wed Apr 10 01:13:29 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/messages.rs	Wed Apr 10 16:14:33 2019 +0300
@@ -67,6 +67,49 @@
 }
 
 #[derive(Debug)]
+pub enum ProtocolFlags {
+    InRoom,
+    RoomMaster,
+    Ready,
+    InGame,
+    Authenticated,
+    Admin,
+    Contributor,
+}
+
+impl ProtocolFlags {
+    #[inline]
+    fn flag_char(&self) -> char {
+        match self {
+            ProtocolFlags::InRoom => 'i',
+            ProtocolFlags::RoomMaster => 'h',
+            ProtocolFlags::Ready => 'r',
+            ProtocolFlags::InGame => 'g',
+            ProtocolFlags::Authenticated => 'u',
+            ProtocolFlags::Admin => 'a',
+            ProtocolFlags::Contributor => 'c',
+        }
+    }
+
+    #[inline]
+    fn format(prefix: char, flags: &[ProtocolFlags]) -> String {
+        once(prefix)
+            .chain(flags.iter().map(|f| f.flag_char()))
+            .collect()
+    }
+}
+
+#[inline]
+pub fn add_flags(flags: &[ProtocolFlags]) -> String {
+    ProtocolFlags::format('+', flags)
+}
+
+#[inline]
+pub fn remove_flags(flags: &[ProtocolFlags]) -> String {
+    ProtocolFlags::format('-', flags)
+}
+
+#[derive(Debug)]
 pub enum HWServerMessage {
     Ping,
     Pong,
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Wed Apr 10 01:13:29 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Wed Apr 10 16:14:33 2019 +0300
@@ -1,8 +1,10 @@
 use crate::{
     protocol::messages::server_chat,
     protocol::messages::{
+        add_flags, remove_flags,
         HWProtocolMessage::{self, Rnd},
         HWServerMessage::{self, *},
+        ProtocolFlags as Flags,
     },
     server::{
         client::HWClient,
@@ -42,7 +44,7 @@
 
     let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]);
     let flags_msg = ClientFlags(
-        "+i".to_string(),
+        add_flags(&[Flags::InRoom]),
         server.collect_nicks(|(_, c)| c.room_id.is_some()),
     );
     let server_msg = ServerMessage("\u{1f994} is watching".to_string());
@@ -136,7 +138,7 @@
         if client.is_master() && !room.is_fixed() {
             client.set_is_master(false);
             response.add(
-                ClientFlags("-h".to_string(), vec![client.nick.clone()])
+                ClientFlags(remove_flags(&[Flags::RoomMaster]), vec![client.nick.clone()])
                     .send_all()
                     .in_room(room.id),
             );
@@ -153,7 +155,7 @@
     };
     response.add(update_msg.send_all().with_protocol(room.protocol_number));
 
-    response.add(ClientFlags("-i".to_string(), vec![client.nick.clone()]).send_all());
+    response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all());
 }
 
 pub fn exit_room(server: &mut HWServer, client_id: ClientId, response: &mut Response, msg: &str) {
@@ -180,7 +182,7 @@
                 room.set_unregistered_players_restriction(true);
 
                 response.add(
-                    ClientFlags("+h".to_string(), vec![new_master_nick])
+                    ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick])
                         .send_all()
                         .in_room(room.id),
                 );
@@ -248,7 +250,11 @@
     let room = &server.rooms[room_id];
     if let Some(id) = room.master_id {
         response.add(
-            ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]).send(to_client),
+            ClientFlags(
+                add_flags(&[Flags::RoomMaster]),
+                vec![server.clients[id].nick.clone()],
+            )
+            .send(to_client),
         );
     }
     let nicks: Vec<_> = server
@@ -258,7 +264,7 @@
         .map(|(_, c)| c.nick.clone())
         .collect();
     if !nicks.is_empty() {
-        response.add(ClientFlags("+r".to_string(), nicks).send(to_client));
+        response.add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send(to_client));
     }
 }
 
@@ -409,7 +415,7 @@
         }
         response.add(RunGame.send_all().in_room(room.id));
         response.add(
-            ClientFlags("+g".to_string(), room_nicks)
+            ClientFlags(add_flags(&[Flags::InGame]), room_nicks)
                 .send_all()
                 .in_room(room.id),
         );
@@ -469,7 +475,7 @@
         let msg = if room.protocol_number < 38 {
             LegacyReady(false, nicks)
         } else {
-            ClientFlags("-r".to_string(), nicks)
+            ClientFlags(remove_flags(&[Flags::Ready]), nicks)
         };
         response.add(msg.send_all().in_room(room_id));
     }
--- a/rust/hedgewars-server/src/server/handlers/inroom.rs	Wed Apr 10 01:13:29 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/inroom.rs	Wed Apr 10 16:14:33 2019 +0300
@@ -3,7 +3,10 @@
 use super::common::rnd_reply;
 use crate::utils::to_engine_msg;
 use crate::{
-    protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage::*},
+    protocol::messages::{
+        add_flags, remove_flags, server_chat, HWProtocolMessage, HWServerMessage::*,
+        ProtocolFlags as Flags,
+    },
     server::{
         core::HWServer,
         coretypes,
@@ -170,16 +173,16 @@
         ToggleReady => {
             let flags = if client.is_ready() {
                 room.ready_players_number -= 1;
-                "-r"
+                remove_flags(&[Flags::Ready])
             } else {
                 room.ready_players_number += 1;
-                "+r"
+                add_flags(&[Flags::Ready])
             };
 
             let msg = if client.protocol_number < 38 {
                 LegacyReady(client.is_ready(), vec![client.nick.clone()])
             } else {
-                ClientFlags(flags.to_string(), vec![client.nick.clone()])
+                ClientFlags(flags, vec![client.nick.clone()])
             };
             response.add(msg.send_all().in_room(room.id));
             client.set_is_ready(!client.is_ready());
@@ -505,7 +508,7 @@
             if client.is_in_game() {
                 client.set_is_in_game(false);
                 response.add(
-                    ClientFlags("-g".to_string(), vec![client.nick.clone()])
+                    ClientFlags(remove_flags(&[Flags::InGame]), vec![client.nick.clone()])
                         .send_all()
                         .in_room(room.id),
                 );
--- a/rust/hedgewars-server/src/server/handlers/lobby.rs	Wed Apr 10 01:13:29 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/lobby.rs	Wed Apr 10 16:14:33 2019 +0300
@@ -2,7 +2,9 @@
 
 use super::common::rnd_reply;
 use crate::{
-    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
+    protocol::messages::{
+        add_flags, remove_flags, HWProtocolMessage, HWServerMessage::*, ProtocolFlags as Flags,
+    },
     server::{core::HWServer, coretypes::ClientId},
     utils::is_name_illegal,
 };
@@ -25,7 +27,7 @@
                 );
             } else {
                 let flags_msg = ClientFlags(
-                    "+hr".to_string(),
+                    add_flags(&[Flags::RoomMaster, Flags::Ready]),
                     vec![server.clients[client_id].nick.clone()],
                 );
 
@@ -40,7 +42,9 @@
                 );
                 response.add(flags_msg.send_self());
 
-                response.add(ClientFlags("+i".to_string(), vec![client.nick.clone()]).send_self());
+                response.add(
+                    ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(),
+                );
             };
         }
         Chat(msg) => {
@@ -85,7 +89,7 @@
                     server.move_to_room(client_id, room_id);
 
                     response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
-                    response.add(ClientFlags("+i".to_string(), vec![nick]).send_all());
+                    response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all());
                     response.add(RoomJoined(nicks).send_self());
 
                     let room = &server.rooms[room_id];