gameServer2/src/server/handlers/lobby.rs
changeset 12152 03ccb89820f3
child 12857 bd35cb2302b3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/handlers/lobby.rs	Sat Feb 04 19:52:38 2017 +0300
@@ -0,0 +1,39 @@
+use mio;
+
+use server::server::HWServer;
+use server::actions::Action;
+use server::actions::Action::*;
+use protocol::messages::HWProtocolMessage;
+use protocol::messages::HWServerMessage::*;
+
+pub fn handle(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, message: HWProtocolMessage) {
+    match message {
+        HWProtocolMessage::Chat(msg) => {
+            let chat_msg = ChatMsg(&server.clients[token].nick, &msg).to_raw_protocol();
+            server.react(token, poll, vec![SendAllButMe(chat_msg)]);
+        },
+        HWProtocolMessage::CreateRoom(name, password) => {
+            let room_exists = server.rooms.iter().find(|&r| r.name == name).is_some();
+            if room_exists {
+                server.react(token, poll, vec![Warn("Room exists".to_string())]);
+            } else {
+                let flags_msg = ClientFlags("+hr", &[&server.clients[token].nick]).to_raw_protocol();
+                {
+                    let c = &mut server.clients[token];
+                    c.is_master = true;
+                    c.is_ready = true;
+                    c.is_joined_mid_game = false;
+                }
+                server.react(token, poll, vec![
+                    AddRoom(name, password)
+                    , SendMe(flags_msg)
+                    ]);
+            }
+        },
+        HWProtocolMessage::Join(name, password) => {
+
+        },
+        HWProtocolMessage::List => warn!("Deprecated LIST message received"),
+        _ => warn!("Incorrect command in lobby state"),
+    }
+}