Room creation halfplemented
authorunc0rr
Sat, 04 Feb 2017 19:52:38 +0300
changeset 12147 03ccb89820f3
parent 12146 8d8fb85bc09c
child 12148 8591375271b8
Room creation halfplemented
gameServer2/src/protocol/messages.rs
gameServer2/src/server/actions.rs
gameServer2/src/server/client.rs
gameServer2/src/server/handlers/inroom.rs
gameServer2/src/server/handlers/lobby.rs
gameServer2/src/server/handlers/loggingin.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/protocol/messages.rs	Fri Jan 27 23:03:31 2017 +0300
+++ b/gameServer2/src/protocol/messages.rs	Sat Feb 04 19:52:38 2017 +0300
@@ -76,7 +76,10 @@
     Nick(&'a str),
     LobbyLeft(&'a str),
     LobbyJoined(&'a [&'a str]),
+    ChatMsg(&'a str, &'a str),
+    ClientFlags(&'a str, &'a [&'a str]),
 
+    Warning(&'a str),
     Connected(u32),
     Unreachable,
 }
@@ -109,15 +112,26 @@
             &HWServerMessage::Bye(msg)
                 => construct_message(&["BYE", &msg]),
             &HWServerMessage::Nick(nick)
-            => construct_message(&["NICK", &nick]),
-            &HWServerMessage::LobbyLeft(msg)
-                => construct_message(&["LOBBY_LEFT", &msg]),
-            &HWServerMessage::LobbyJoined(msg)
+                => construct_message(&["NICK", &nick]),
+            &HWServerMessage::LobbyLeft(nick)
+                => construct_message(&["LOBBY_LEFT", &nick]),
+            &HWServerMessage::LobbyJoined(nicks)
                 => {
                 let mut v = vec!["LOBBY:JOINED"];
-                v.extend_from_slice(msg);
+                v.extend_from_slice(nicks);
                 construct_message(&v)
             },
+            &HWServerMessage::ClientFlags(flags, nicks)
+            => {
+                let mut v = vec!["CLIENT_FLAGS"];
+                v.push(flags);
+                v.extend_from_slice(nicks);
+                construct_message(&v)
+            },
+            &HWServerMessage::ChatMsg(nick, msg)
+                => construct_message(&["CHAT", &nick, &msg]),
+            &HWServerMessage::Warning(msg)
+                => construct_message(&["WARNING", &msg]),
             _ => construct_message(&["ERROR", "UNIMPLEMENTED"]),
         }
     }
--- a/gameServer2/src/server/actions.rs	Fri Jan 27 23:03:31 2017 +0300
+++ b/gameServer2/src/server/actions.rs	Sat Feb 04 19:52:38 2017 +0300
@@ -3,6 +3,7 @@
 use std::io;
 
 use super::server::HWServer;
+use super::server::HWRoom;
 use protocol::messages::HWProtocolMessage;
 use protocol::messages::HWServerMessage::*;
 use super::handlers;
@@ -15,6 +16,8 @@
     ReactProtocolMessage(HWProtocolMessage),
     CheckRegistered,
     JoinLobby,
+    AddRoom(String, Option<String>),
+    Warn(String),
 }
 
 use self::Action::*;
@@ -49,6 +52,8 @@
                     ]);
             },
         JoinLobby => {
+            server.clients[token].room_id = Some(server.lobby_id);
+
             let joined_msg;
             {
                 let mut lobby_nicks: Vec<&str> = Vec::new();
@@ -60,12 +65,23 @@
                 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![
                 SendAllButMe(everyone_msg),
                 SendMe(joined_msg),
                 ]);
         },
+        AddRoom(name, password) => {
+            let room_id = server.rooms.insert(HWRoom::new()).ok().expect("Cannot add room");
+            let r = &mut server.rooms[room_id];
+            r.name = name;
+            r.password = password;
+            r.id = room_id.clone();
+            r.ready_players_number = 1;
+            server.clients[token].room_id = Some(room_id);
+        },
+        Warn(msg) => {
+            run_action(server, token, poll, SendMe(Warning(&msg).to_raw_protocol()));
+        }
         //_ => unimplemented!(),
     }
 }
--- a/gameServer2/src/server/client.rs	Fri Jan 27 23:03:31 2017 +0300
+++ b/gameServer2/src/server/client.rs	Sat Feb 04 19:52:38 2017 +0300
@@ -19,6 +19,9 @@
     pub room_id: Option<Token>,
     pub nick: String,
     pub protocol_number: u32,
+    pub is_master: bool,
+    pub is_ready: bool,
+    pub is_joined_mid_game: bool,
 }
 
 impl HWClient {
@@ -32,6 +35,9 @@
 
             nick: String::new(),
             protocol_number: 0,
+            is_master: false,
+            is_ready: false,
+            is_joined_mid_game: false,
         }
     }
 
@@ -44,7 +50,8 @@
     }
 
     pub fn deregister(&mut self, poll: &Poll) {
-        poll.deregister(&self.sock);
+        poll.deregister(&self.sock)
+            .ok().expect("could not deregister socket");
     }
 
     pub fn send_raw_msg(&mut self, msg: &[u8]) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/handlers/inroom.rs	Sat Feb 04 19:52:38 2017 +0300
@@ -0,0 +1,13 @@
+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 {
+        _ => warn!("Unimplemented!"),
+    }
+}
--- /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"),
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/handlers/loggingin.rs	Sat Feb 04 19:52:38 2017 +0300
@@ -0,0 +1,23 @@
+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::Nick(nick) =>
+            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].protocol_number = proto;
+            server.react(token, poll, vec![CheckRegistered]);
+        },
+        _ => warn!("Incorrect command in logging-in state"),
+    }
+}
--- a/gameServer2/src/server/server.rs	Fri Jan 27 23:03:31 2017 +0300
+++ b/gameServer2/src/server/server.rs	Sat Feb 04 19:52:38 2017 +0300
@@ -91,13 +91,19 @@
 
 
 pub struct HWRoom {
+    pub id: Token,
     pub name: String,
+    pub password: Option<String>,
+    pub ready_players_number: u8,
 }
 
 impl HWRoom {
     pub fn new() -> HWRoom {
         HWRoom {
+            id: Token(0),
             name: String::new(),
+            password: None,
+            ready_players_number: 0,
         }
     }
 }