Send the rnd reply to the room only.
authorMarcin Mielniczuk <marmistrz.dev@zoho.eu>
Fri, 06 Jul 2018 16:59:52 +0200
changeset 13450 d3c86ade3d4d
parent 13449 914f9b970f4d
child 13451 dd2e51f7303d
Send the rnd reply to the room only.
gameServer2/src/server/handlers/common.rs
gameServer2/src/server/handlers/inroom.rs
gameServer2/src/server/handlers/lobby.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/server/handlers/common.rs	Fri Jul 06 16:27:25 2018 +0200
+++ b/gameServer2/src/server/handlers/common.rs	Fri Jul 06 16:59:52 2018 +0200
@@ -1,10 +1,24 @@
 use protocol::messages::{
-    HWProtocolMessage::{self, Rnd}, HWServerMessage::ChatMsg,
+    HWProtocolMessage::{self, Rnd},
+    HWServerMessage::{self, ChatMsg},
 };
 use rand::{self, Rng};
-use server::{actions::Action, server::HWServer};
+use server::{
+    actions::Action,
+    room::HWRoom,
+    server::HWServer
+};
 
-pub fn rnd_reply(options: Vec<String>) -> Vec<Action> {
+pub fn rnd_action(options: Vec<String>, room: Option<&mut HWRoom>) -> Vec<Action> {
+    if let Some(room) = room {
+        let msg = rnd_reply(options);
+        vec![msg.send_all().in_room(room.id).action()]
+    } else {
+        Vec::new()
+    }
+}
+
+fn rnd_reply(options: Vec<String>) -> HWServerMessage {
     let options = if options.is_empty() {
         vec!["heads".to_owned(), "tails".to_owned()]
     } else {
@@ -15,8 +29,7 @@
         nick: "[random]".to_owned(),
         msg: reply.clone(),
     };
-    let msg = msg.send_all().action();
-    vec![msg]
+    msg
 }
 
 #[cfg(test)]
@@ -27,14 +40,10 @@
         Action::{self, Send}, PendingMessage,
     };
 
-    fn reply2string(mut r: Vec<Action>) -> String {
-        assert_eq!(r.len(), 1);
-        match r.remove(0) {
-            Send(PendingMessage {
-                message: ChatMsg { msg: p, .. },
-                ..
-            }) => String::from(p),
-            _ => panic!("reply should be a string"),
+    fn reply2string(r: HWServerMessage) -> String {
+        match r {
+            ChatMsg { msg: p, .. } => String::from(p),
+            _ => panic!("expected a ChatMsg"),
         }
     }
 
--- a/gameServer2/src/server/handlers/inroom.rs	Fri Jul 06 16:27:25 2018 +0200
+++ b/gameServer2/src/server/handlers/inroom.rs	Fri Jul 06 16:59:52 2018 +0200
@@ -13,7 +13,7 @@
 use utils::is_name_illegal;
 use std::mem::swap;
 use base64::{encode, decode};
-use super::common::rnd_reply;
+use super::common::rnd_action;
 
 #[derive(Clone)]
 struct ByMsg<'a> {
@@ -289,7 +289,10 @@
             }
             server.react(client_id, actions)
         },
-        Rnd(v) => server.react(client_id, rnd_reply(v)),
+        Rnd(v) => {
+            let actions = rnd_action(v, server.room(client_id));
+            server.react(client_id, actions)
+        },
         _ => warn!("Unimplemented!")
     }
 }
--- a/gameServer2/src/server/handlers/lobby.rs	Fri Jul 06 16:27:25 2018 +0200
+++ b/gameServer2/src/server/handlers/lobby.rs	Fri Jul 06 16:59:52 2018 +0200
@@ -10,7 +10,7 @@
     HWServerMessage::*
 };
 use utils::is_name_illegal;
-use super::common::rnd_reply;
+use super::common::rnd_action;
 
 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
     use protocol::messages::HWProtocolMessage::*;
@@ -58,7 +58,10 @@
             }
             server.react(client_id, actions);
         },
-        Rnd(v) => server.react(client_id, rnd_reply(v)),
+        Rnd(v) => {
+            let actions = rnd_action(v, server.room(client_id));
+            server.react(client_id, actions)
+        },
         List => warn!("Deprecated LIST message received"),
         _ => warn!("Incorrect command in lobby state"),
     }
--- a/gameServer2/src/server/server.rs	Fri Jul 06 16:27:25 2018 +0200
+++ b/gameServer2/src/server/server.rs	Fri Jul 06 16:59:52 2018 +0200
@@ -132,4 +132,8 @@
             (c, None)
         }
     }
-}
\ No newline at end of file
+
+    pub fn room(&mut self, client_id: ClientId) -> Option<&mut HWRoom> {
+        self.client_and_room(client_id).1
+    }
+}