improve lobby joining
authoralfadur
Fri, 12 Apr 2019 02:48:16 +0300
changeset 14812 b889d9e1115f
parent 14811 e94fbf6cad2b
child 14813 38e66519e585
improve lobby joining
rust/hedgewars-server/src/protocol/messages.rs
rust/hedgewars-server/src/server/client.rs
rust/hedgewars-server/src/server/handlers.rs
rust/hedgewars-server/src/server/handlers/common.rs
--- a/rust/hedgewars-server/src/protocol/messages.rs	Thu Apr 11 21:25:31 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/messages.rs	Fri Apr 12 02:48:16 2019 +0300
@@ -66,13 +66,13 @@
     Empty,
 }
 
-#[derive(Debug)]
+#[derive(Debug, Clone, Copy)]
 pub enum ProtocolFlags {
     InRoom,
     RoomMaster,
     Ready,
     InGame,
-    Authenticated,
+    Registered,
     Admin,
     Contributor,
 }
@@ -85,7 +85,7 @@
             ProtocolFlags::RoomMaster => 'h',
             ProtocolFlags::Ready => 'r',
             ProtocolFlags::InGame => 'g',
-            ProtocolFlags::Authenticated => 'u',
+            ProtocolFlags::Registered => 'u',
             ProtocolFlags::Admin => 'a',
             ProtocolFlags::Contributor => 'c',
         }
--- a/rust/hedgewars-server/src/server/client.rs	Thu Apr 11 21:25:31 2019 +0300
+++ b/rust/hedgewars-server/src/server/client.rs	Fri Apr 12 02:48:16 2019 +0300
@@ -2,7 +2,7 @@
 use bitflags::*;
 
 bitflags! {
-    pub struct ClientFlags: u8 {
+    pub struct ClientFlags: u16 {
         const IS_ADMIN = 0b0000_0001;
         const IS_MASTER = 0b0000_0010;
         const IS_READY = 0b0000_0100;
@@ -11,6 +11,7 @@
         const IS_CHECKER = 0b0010_0000;
         const IS_CONTRIBUTOR = 0b0100_0000;
         const HAS_SUPER_POWER = 0b1000_0000;
+        const IS_REGISTERED = 0b0001_0000_0000;
 
         const NONE = 0b0000_0000;
         const DEFAULT = Self::NONE.bits;
@@ -74,6 +75,9 @@
     pub fn has_super_power(&self) -> bool {
         self.contains(ClientFlags::HAS_SUPER_POWER)
     }
+    pub fn is_registered(&self) -> bool {
+        self.contains(ClientFlags::IS_REGISTERED)
+    }
 
     pub fn set_is_admin(&mut self, value: bool) {
         self.set(ClientFlags::IS_ADMIN, value)
@@ -99,4 +103,7 @@
     pub fn set_has_super_power(&mut self, value: bool) {
         self.set(ClientFlags::HAS_SUPER_POWER, value)
     }
+    pub fn set_is_registered(&mut self, value: bool) {
+        self.set(ClientFlags::IS_REGISTERED, value)
+    }
 }
--- a/rust/hedgewars-server/src/server/handlers.rs	Thu Apr 11 21:25:31 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers.rs	Fri Apr 12 02:48:16 2019 +0300
@@ -337,6 +337,7 @@
                 if let Some(client) = server.anteroom.remove_client(client_id) {
                     server.add_client(client_id, client);
                     let client = &mut server.clients[client_id];
+                    client.set_is_registered(info.is_registered);
                     client.set_is_admin(info.is_admin);
                     client.set_is_contributor(info.is_admin)
                 }
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Thu Apr 11 21:25:31 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Fri Apr 12 02:48:16 2019 +0300
@@ -39,15 +39,37 @@
 pub fn join_lobby(server: &mut HWServer, response: &mut Response) {
     let client_id = response.client_id();
 
-    let lobby_nicks: Vec<_> = server.collect_nicks(|(_, c)| c.room_id.is_none());
+    let client = &server.clients[client_id];
+    let nick = vec![client.nick.clone()];
+    let mut flags = vec![];
+    if client.is_registered() {
+        flags.push(Flags::Registered)
+    }
+    if client.is_admin() {
+        flags.push(Flags::Admin)
+    }
+    if client.is_contributor() {
+        flags.push(Flags::Contributor)
+    }
 
-    let joined_msg = LobbyJoined(lobby_nicks);
+    let all_nicks: Vec<_> = server.collect_nicks(|_| true);
 
-    let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]);
-    let flags_msg = ClientFlags(
-        add_flags(&[Flags::InRoom]),
-        server.collect_nicks(|(_, c)| c.room_id.is_some()),
-    );
+    let mut flag_selectors = [
+        (
+            Flags::Registered,
+            server.collect_nicks(|(_, c)| c.is_registered()),
+        ),
+        (Flags::Admin, server.collect_nicks(|(_, c)| c.is_admin())),
+        (
+            Flags::Contributor,
+            server.collect_nicks(|(_, c)| c.is_contributor()),
+        ),
+        (
+            Flags::InRoom,
+            server.collect_nicks(|(_, c)| c.room_id.is_some()),
+        ),
+    ];
+
     let server_msg = ServerMessage(server.get_greetings(client_id).to_string());
 
     let rooms_msg = Rooms(
@@ -58,9 +80,18 @@
             .collect(),
     );
 
-    response.add(everyone_msg.send_all().but_self());
-    response.add(joined_msg.send_self());
-    response.add(flags_msg.send_self());
+    response.add(LobbyJoined(nick).send_all().but_self());
+    response.add(
+        ClientFlags(add_flags(&flags), all_nicks.clone())
+            .send_all()
+            .but_self(),
+    );
+
+    response.add(LobbyJoined(all_nicks).send_self());
+    for (flag, nicks) in &mut flag_selectors {
+        response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self());
+    }
+
     response.add(server_msg.send_self());
     response.add(rooms_msg.send_self());
 }