Assorted chat fixes
authoralfadur
Mon, 16 Jul 2018 22:32:04 +0300
changeset 13492 ba5211dddb21
parent 13486 1ee192f13456
child 13493 282e5e54386f
Assorted chat fixes
gameServer2/src/server/handlers/common.rs
gameServer2/src/server/handlers/inroom.rs
gameServer2/src/server/handlers/lobby.rs
gameServer2/src/server/handlers/mod.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/server/handlers/common.rs	Fri Jul 13 19:52:19 2018 +0300
+++ b/gameServer2/src/server/handlers/common.rs	Mon Jul 16 22:32:04 2018 +0300
@@ -1,30 +1,21 @@
 use protocol::messages::{
     HWProtocolMessage::{self, Rnd}, HWServerMessage::{self, ChatMsg},
 };
-use rand::{self, Rng};
-use server::{actions::Action, room::HWRoom, server::HWServer};
+use rand::{self, Rng, thread_rng};
+use server::{actions::Action, server::HWServer};
 
-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()]
+pub fn rnd_reply(options: &Vec<String>) -> HWServerMessage {
+    let mut rng = thread_rng();
+    let reply = if options.is_empty() {
+        (*rng.choose(&["heads", "tails"]).unwrap()).to_owned()
     } else {
-        Vec::new()
-    }
-}
+        rng.choose(&options).unwrap().clone()
+    };
 
-fn rnd_reply(options: Vec<String>) -> HWServerMessage {
-    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 {
+    ChatMsg {
         nick: "[random]".to_owned(),
         msg: reply.clone(),
-    };
-    msg
+    }
 }
 
 #[cfg(test)]
@@ -45,7 +36,7 @@
     fn run_handle_test(opts: Vec<String>) {
         let opts2 = opts.clone();
         for opt in opts {
-            while reply2string(rnd_reply(opts2.clone())) != opt {}
+            while reply2string(rnd_reply(&opts2)) != opt {}
         }
     }
 
@@ -72,7 +63,7 @@
 
         while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps {
             tries += 1;
-            if reply2string(rnd_reply(opts.clone())) == 1.to_string() {
+            if reply2string(rnd_reply(&opts)) == 1.to_string() {
                 ones += 1;
             }
         }
--- a/gameServer2/src/server/handlers/inroom.rs	Fri Jul 13 19:52:19 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Mon Jul 16 22:32:04 2018 +0300
@@ -6,7 +6,7 @@
     server_chat
 };
 use server::{
-    coretypes::{ClientId, Voting, VoteType},
+    coretypes::{ClientId, RoomId, Voting, VoteType},
     server::HWServer,
     room::HWRoom,
     actions::{Action, Action::*}
@@ -14,7 +14,7 @@
 use utils::is_name_illegal;
 use std::mem::swap;
 use base64::{encode, decode};
-use super::common::rnd_action;
+use super::common::rnd_reply;
 
 #[derive(Clone)]
 struct ByMsg<'a> {
@@ -81,7 +81,7 @@
     })
 }
 
-pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
+pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
     use protocol::messages::HWProtocolMessage::*;
     match message {
         Part(None) => server.react(client_id, vec![
@@ -92,11 +92,7 @@
             let actions = {
                 let c = &mut server.clients[client_id];
                 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 {
-                    Vec::new()
-                }
+                vec![chat_msg.send_all().in_room(room_id).but_self().action()]
             };
             server.react(client_id, actions);
         },
@@ -162,7 +158,6 @@
         AddTeam(info) => {
             let mut actions = Vec::new();
             if let (c, Some(r)) = server.client_and_room(client_id) {
-                let room_id = r.id;
                 if r.teams.len() >= r.team_limit as usize {
                     actions.push(Warn("Too many teams!".to_string()))
                 } else if r.addable_hedgehogs() == 0 {
@@ -207,7 +202,6 @@
         },
         SetHedgehogsNumber(team_name, number) => {
             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
-                let room_id = r.id;
                 let addable_hedgehogs = r.addable_hedgehogs();
                 if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
                     if !c.is_master() {
@@ -232,7 +226,6 @@
         SetTeamColor(team_name, color) => {
             let mut owner_id = None;
             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
-                let room_id = r.id;
                 if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
                     if !c.is_master() {
                         vec![ProtocolError("You're not the room master!".to_string())]
@@ -280,8 +273,7 @@
                     .send_self().action()])
         }
         CallVote(Some(kind)) => {
-            let (room_id, is_in_game) = server.room(client_id)
-                .map(|r| (r.id, r.game_info.is_some())).unwrap();
+            let is_in_game = server.rooms[room_id].game_info.is_some();
             let error = match &kind {
                 VoteType::Kick(nick) => {
                     if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() {
@@ -398,8 +390,16 @@
             server.react(client_id, actions)
         },
         Rnd(v) => {
-            let actions = rnd_action(v, server.room(client_id));
-            server.react(client_id, actions)
+            let result = rnd_reply(&v);
+            let mut echo = vec!["/rnd".to_string()];
+            echo.extend(v.into_iter());
+            let chat_msg = ChatMsg {
+                nick: server.clients[client_id].nick.clone(),
+                msg: echo.join(" ")
+            };
+            server.react(client_id, vec![
+                chat_msg.send_all().in_room(room_id).action(),
+                result.send_all().in_room(room_id).action()])
         },
         _ => warn!("Unimplemented!")
     }
--- a/gameServer2/src/server/handlers/lobby.rs	Fri Jul 13 19:52:19 2018 +0300
+++ b/gameServer2/src/server/handlers/lobby.rs	Mon Jul 16 22:32:04 2018 +0300
@@ -10,7 +10,7 @@
     HWServerMessage::*
 };
 use utils::is_name_illegal;
-use super::common::rnd_action;
+use super::common::rnd_reply;
 
 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
     use protocol::messages::HWProtocolMessage::*;
@@ -31,8 +31,9 @@
             server.react(client_id, actions);
         },
         Chat(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()]);
+            let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg}
+                .send_all().in_room(server.lobby_id).but_self().action()];
+            server.react(client_id, actions);
         },
         JoinRoom(name, password) => {
             let actions;
@@ -59,8 +60,7 @@
             server.react(client_id, actions);
         },
         Rnd(v) => {
-            let actions = rnd_action(v, server.room(client_id));
-            server.react(client_id, actions)
+            server.react(client_id, vec![rnd_reply(&v).send_self().action()]);
         },
         List => warn!("Deprecated LIST message received"),
         _ => warn!("Incorrect command in lobby state"),
--- a/gameServer2/src/server/handlers/mod.rs	Fri Jul 13 19:52:19 2018 +0300
+++ b/gameServer2/src/server/handlers/mod.rs	Mon Jul 16 22:32:04 2018 +0300
@@ -1,36 +1,38 @@
 use mio;
-use std::io::Write;
-use std::io;
+use std::{io, io::Write};
 
-use super::server::HWServer;
-use super::actions::Action;
-use super::actions::Action::*;
-use protocol::messages::HWProtocolMessage;
-use protocol::messages::HWServerMessage::*;
-
+use super::{
+    server::HWServer,
+    actions::{Action, Action::*},
+    coretypes::ClientId
+};
+use protocol::messages::{
+    HWProtocolMessage,
+    HWServerMessage::*
+};
 mod common;
 mod loggingin;
 mod lobby;
 mod inroom;
 
-pub fn handle(server: &mut HWServer, token: usize, message: HWProtocolMessage) {
+pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
     match message {
         HWProtocolMessage::Ping =>
-            server.react(token, vec![Pong.send_self().action()]),
+            server.react(client_id, vec![Pong.send_self().action()]),
         HWProtocolMessage::Quit(Some(msg)) =>
-            server.react(token, vec![ByeClient("User quit: ".to_string() + &msg)]),
+            server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)]),
         HWProtocolMessage::Quit(None) =>
-            server.react(token, vec![ByeClient("User quit".to_string())]),
+            server.react(client_id, vec![ByeClient("User quit".to_string())]),
         HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
         HWProtocolMessage::Empty => warn!("Empty message"),
         _ => {
-            match server.clients[token].room_id {
+            match server.clients[client_id].room_id {
                 None =>
-                    loggingin::handle(server, token, message),
+                    loggingin::handle(server, client_id, message),
                 Some(id) if id == server.lobby_id =>
-                    lobby::handle(server, token, message),
-                _ =>
-                    inroom::handle(server, token, message)
+                    lobby::handle(server, client_id, message),
+                Some(id) =>
+                    inroom::handle(server, client_id, id, message)
             }
         },
     }
--- a/gameServer2/src/server/server.rs	Fri Jul 13 19:52:19 2018 +0300
+++ b/gameServer2/src/server/server.rs	Mon Jul 16 22:32:04 2018 +0300
@@ -94,6 +94,8 @@
         }
     }
 
+    pub fn lobby(&self) -> &HWRoom { &self.rooms[self.lobby_id] }
+
     pub fn has_room(&self, name: &str) -> bool {
         self.rooms.iter().any(|(_, r)| r.name == name)
     }