Implement server-side logic for Rnd
authorMarcin Mielniczuk <marmistrz.dev@zoho.eu>
Fri, 06 Jul 2018 16:27:25 +0200
changeset 13444 914f9b970f4d
parent 13443 2501428303a2
child 13445 d3c86ade3d4d
Implement server-side logic for Rnd
gameServer2/src/protocol/messages.rs
gameServer2/src/server/handlers/common.rs
gameServer2/src/server/handlers/inroom.rs
gameServer2/src/server/handlers/lobby.rs
gameServer2/src/server/handlers/mod.rs
--- a/gameServer2/src/protocol/messages.rs	Wed Jul 04 18:49:51 2018 +0300
+++ b/gameServer2/src/protocol/messages.rs	Fri Jul 06 16:27:25 2018 +0200
@@ -76,7 +76,7 @@
     Proto(u32),
     LobbyLeft(String, String),
     LobbyJoined(Vec<String>),
-    ChatMsg(String, String),
+    ChatMsg {nick: String, msg: String},
     ClientFlags(String, Vec<String>),
     Rooms(Vec<String>),
     RoomAdd(Vec<String>),
@@ -284,7 +284,7 @@
             ForwardEngineMessage(em) =>
                 construct_message(&["EM"], &em),
             RoundFinished => msg!["ROUND_FINISHED"],
-            ChatMsg(nick, msg) => msg!["CHAT", nick, msg],
+            ChatMsg {nick, msg} => msg!["CHAT", nick, msg],
             ServerMessage(msg) => msg!["SERVER_MESSAGE", msg],
             Warning(msg) => msg!["WARNING", msg],
             Error(msg) => msg!["ERROR", msg],
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/handlers/common.rs	Fri Jul 06 16:27:25 2018 +0200
@@ -0,0 +1,57 @@
+use protocol::messages::{
+    HWProtocolMessage::{self, Rnd}, HWServerMessage::ChatMsg,
+};
+use rand::{self, Rng};
+use server::{actions::Action, server::HWServer};
+
+pub fn rnd_reply(options: Vec<String>) -> Vec<Action> {
+    let options = if options.is_empty() {
+        vec!["heads".to_owned(), "tails".to_owned()]
+    } else {
+        options
+    };
+    let reply = rand::thread_rng().choose(&options).unwrap();
+    let msg = ChatMsg {
+        nick: "[random]".to_owned(),
+        msg: reply.clone(),
+    };
+    let msg = msg.send_all().action();
+    vec![msg]
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use protocol::messages::HWServerMessage::ChatMsg;
+    use server::actions::{
+        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 run_handle_test(opts: Vec<String>) {
+        let opts2 = opts.clone();
+        for opt in opts {
+            while reply2string(rnd_reply(opts2.clone())) != opt {}
+        }
+    }
+
+    #[test]
+    fn test_handle_rnd_empty() {
+        run_handle_test(vec![])
+    }
+
+    #[test]
+    fn test_handle_rnd_nonempty() {
+        run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()])
+    }
+}
--- a/gameServer2/src/server/handlers/inroom.rs	Wed Jul 04 18:49:51 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Fri Jul 06 16:27:25 2018 +0200
@@ -13,6 +13,7 @@
 use utils::is_name_illegal;
 use std::mem::swap;
 use base64::{encode, decode};
+use super::common::rnd_reply;
 
 #[derive(Clone)]
 struct ByMsg<'a> {
@@ -79,7 +80,7 @@
         Chat(msg) => {
             let actions = {
                 let c = &mut server.clients[client_id];
-                let chat_msg = ChatMsg(c.nick.clone(), msg);
+                let chat_msg = ChatMsg {nick: c.nick.clone(), msg: msg};
                 if let Some(room_id) = c.room_id {
                     vec![chat_msg.send_all().in_room(room_id).but_self().action()]
                 } else {
@@ -287,7 +288,8 @@
                 }
             }
             server.react(client_id, actions)
-        }
+        },
+        Rnd(v) => server.react(client_id, rnd_reply(v)),
         _ => warn!("Unimplemented!")
     }
 }
--- a/gameServer2/src/server/handlers/lobby.rs	Wed Jul 04 18:49:51 2018 +0300
+++ b/gameServer2/src/server/handlers/lobby.rs	Fri Jul 06 16:27:25 2018 +0200
@@ -10,6 +10,7 @@
     HWServerMessage::*
 };
 use utils::is_name_illegal;
+use super::common::rnd_reply;
 
 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
     use protocol::messages::HWProtocolMessage::*;
@@ -30,7 +31,7 @@
             server.react(client_id, actions);
         },
         Chat(msg) => {
-            let chat_msg = ChatMsg(server.clients[client_id].nick.clone(), msg);
+            let chat_msg = ChatMsg {nick: server.clients[client_id].nick.clone(), msg: msg};
             server.react(client_id, vec![chat_msg.send_all().but_self().action()]);
         },
         JoinRoom(name, password) => {
@@ -57,6 +58,7 @@
             }
             server.react(client_id, actions);
         },
+        Rnd(v) => server.react(client_id, rnd_reply(v)),
         List => warn!("Deprecated LIST message received"),
         _ => warn!("Incorrect command in lobby state"),
     }
--- a/gameServer2/src/server/handlers/mod.rs	Wed Jul 04 18:49:51 2018 +0300
+++ b/gameServer2/src/server/handlers/mod.rs	Fri Jul 06 16:27:25 2018 +0200
@@ -8,6 +8,7 @@
 use protocol::messages::HWProtocolMessage;
 use protocol::messages::HWServerMessage::*;
 
+mod common;
 mod loggingin;
 mod lobby;
 mod inroom;