SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
authorunc0rr
Fri, 27 Jan 2017 23:03:31 +0300
changeset 12146 8d8fb85bc09c
parent 12145 a482c7a5f6e3
child 12147 03ccb89820f3
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
gameServer2/src/server/actions.rs
gameServer2/src/server/client.rs
gameServer2/src/server/handlers.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/server/actions.rs	Thu Jan 26 14:45:44 2017 +0300
+++ b/gameServer2/src/server/actions.rs	Fri Jan 27 23:03:31 2017 +0300
@@ -9,6 +9,7 @@
 
 pub enum Action {
     SendMe(String),
+    SendAllButMe(String),
     RemoveClient,
     ByeClient(String),
     ReactProtocolMessage(HWProtocolMessage),
@@ -22,6 +23,13 @@
     match action {
         SendMe(msg) =>
             server.send(token, &msg),
+        SendAllButMe(msg) => {
+            for c in server.clients.iter_mut() {
+                if c.id != token {
+                    c.send_string(&msg)
+                }
+            }
+        },
         ByeClient(msg) => {
             server.react(token, poll, vec![
                 SendMe(Bye(&msg).to_raw_protocol()),
@@ -35,15 +43,27 @@
         ReactProtocolMessage(msg) =>
             handlers::handle(server, token, poll, msg),
         CheckRegistered =>
-            if server.clients[token].protocolNumber > 0 && server.clients[token].nick != "" {
+            if server.clients[token].protocol_number > 0 && server.clients[token].nick != "" {
+                server.react(token, poll, vec![
+                    JoinLobby,
+                    ]);
+            },
+        JoinLobby => {
+            let joined_msg;
+            {
+                let mut lobby_nicks: Vec<&str> = Vec::new();
+                for c in server.clients.iter() {
+                    if c.room_id.is_some() {
+                        lobby_nicks.push(&c.nick);
+                    }
+                }
+                joined_msg = LobbyJoined(&lobby_nicks).to_raw_protocol();
+            }
+            let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol();
+            server.clients[token].room_id = Some(server.lobby_id);
             server.react(token, poll, vec![
-                JoinLobby,
-                ]);
-        },
-        JoinLobby => {
-            let msg_string = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol();
-            server.react(token, poll, vec![
-                SendMe(msg_string),
+                SendAllButMe(everyone_msg),
+                SendMe(joined_msg),
                 ]);
         },
         //_ => unimplemented!(),
--- a/gameServer2/src/server/client.rs	Thu Jan 26 14:45:44 2017 +0300
+++ b/gameServer2/src/server/client.rs	Fri Jan 27 23:03:31 2017 +0300
@@ -14,22 +14,24 @@
     sock: TcpStream,
     decoder: ProtocolDecoder,
     buf_out: netbuf::Buf,
-    room_id: Token,
 
+    pub id: Token,
+    pub room_id: Option<Token>,
     pub nick: String,
-    pub protocolNumber: u32,
+    pub protocol_number: u32,
 }
 
 impl HWClient {
-    pub fn new(sock: TcpStream, roomId: &Token) -> HWClient {
+    pub fn new(sock: TcpStream) -> HWClient {
         HWClient {
             sock: sock,
             decoder: ProtocolDecoder::new(),
             buf_out: netbuf::Buf::new(),
-            room_id: roomId.clone(),
+            room_id: None,
+            id: Token(0),
 
             nick: String::new(),
-            protocolNumber: 0,
+            protocol_number: 0,
         }
     }
 
--- a/gameServer2/src/server/handlers.rs	Thu Jan 26 14:45:44 2017 +0300
+++ b/gameServer2/src/server/handlers.rs	Fri Jan 27 23:03:31 2017 +0300
@@ -17,13 +17,13 @@
         HWProtocolMessage::Quit(None) =>
             server.react(token, poll, vec![ByeClient("User quit".to_string())]),
         HWProtocolMessage::Nick(nick) =>
-            if server.clients[token].nick.len() == 0 {
+            if server.clients[token].room_id == None {
                 server.react(token, poll, vec![SendMe(Nick(&nick).to_raw_protocol())]);
                 server.clients[token].nick = nick;
                 server.react(token, poll, vec![CheckRegistered]);
             },
         HWProtocolMessage::Proto(proto) => {
-                server.clients[token].protocolNumber = proto;
+                server.clients[token].protocol_number = proto;
                 server.react(token, poll, vec![CheckRegistered]);
         },
         HWProtocolMessage::List => warn!("Deprecated LIST message received"),
--- a/gameServer2/src/server/server.rs	Thu Jan 26 14:45:44 2017 +0300
+++ b/gameServer2/src/server/server.rs	Fri Jan 27 23:03:31 2017 +0300
@@ -37,10 +37,11 @@
         let (sock, addr) = self.listener.accept()?;
         info!("Connected: {}", addr);
 
-        let client = HWClient::new(sock, &self.lobby_id);
+        let client = HWClient::new(sock);
         let token = self.clients.insert(client)
             .ok().expect("could not add connection to slab");
 
+        self.clients[token].id = token;
         self.clients[token].register(poll, token);
 
         Ok(())