gameServer2/src/server/actions.rs
changeset 13119 1e39b8749072
parent 12853 a9d105dc5c95
child 13416 cdf69667593b
--- a/gameServer2/src/server/actions.rs	Thu Mar 08 16:49:49 2018 +0100
+++ b/gameServer2/src/server/actions.rs	Thu Mar 08 15:01:18 2018 -0500
@@ -3,14 +3,15 @@
 use std::io;
 
 use super::server::HWServer;
-use super::server::HWRoom;
+use super::room::HWRoom;
 use protocol::messages::HWProtocolMessage;
+use protocol::messages::HWServerMessage;
 use protocol::messages::HWServerMessage::*;
 use super::handlers;
 
 pub enum Action {
-    SendMe(String),
-    SendAllButMe(String),
+    SendMe(HWServerMessage),
+    SendAllButMe(HWServerMessage),
     RemoveClient,
     ByeClient(String),
     ReactProtocolMessage(HWProtocolMessage),
@@ -22,32 +23,30 @@
 
 use self::Action::*;
 
-pub fn run_action(server: &mut HWServer, token: usize, poll: &mio::Poll, action: Action) {
+pub fn run_action(server: &mut HWServer, token: usize, action: Action) {
     match action {
         SendMe(msg) =>
-            server.send(token, &msg),
+            server.send_self(token, msg),
         SendAllButMe(msg) => {
-            for (_i, c) in server.clients.iter_mut() {
-                if c.id != token {
-                    c.send_string(&msg)
-                }
-            }
+            server.send_others(token, msg)
         },
         ByeClient(msg) => {
-            server.react(token, poll, vec![
-                SendMe(Bye(&msg).to_raw_protocol()),
+            server.react(token, vec![
+                SendMe(Bye(msg)),
                 RemoveClient,
                 ]);
         },
         RemoveClient => {
-            server.clients[token].deregister(poll);
-            server.clients.remove(token);
+            server.removed_clients.push(token);
+            if server.clients.contains(token) {
+                server.clients.remove(token);
+            }
         },
         ReactProtocolMessage(msg) =>
-            handlers::handle(server, token, poll, msg),
+            handlers::handle(server, token, msg),
         CheckRegistered =>
             if server.clients[token].protocol_number > 0 && server.clients[token].nick != "" {
-                server.react(token, poll, vec![
+                server.react(token, vec![
                     JoinLobby,
                     ]);
             },
@@ -56,36 +55,34 @@
 
             let joined_msg;
             {
-                let mut lobby_nicks: Vec<&str> = Vec::new();
+                let mut lobby_nicks = Vec::new();
                 for (_, c) in server.clients.iter() {
                     if c.room_id.is_some() {
-                        lobby_nicks.push(&c.nick);
+                        lobby_nicks.push(c.nick.clone());
                     }
                 }
-                joined_msg = LobbyJoined(&lobby_nicks).to_raw_protocol();
+                joined_msg = LobbyJoined(lobby_nicks);
             }
-            let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol();
-            server.react(token, poll, vec![
+            let everyone_msg = LobbyJoined(vec![server.clients[token].nick.clone()]);
+            server.react(token, vec![
                 SendAllButMe(everyone_msg),
                 SendMe(joined_msg),
                 ]);
         },
         AddRoom(name, password) => {
-            let room_id = server.rooms.insert(HWRoom::new());
+            let room_id = server.add_room();;
             {
                 let r = &mut server.rooms[room_id];
                 let c = &mut server.clients[token];
                 r.name = name;
                 r.password = password;
-                r.id = room_id.clone();
                 r.ready_players_number = 1;
                 r.protocol_number = c.protocol_number;
                 c.room_id = Some(room_id);
             }
-
         },
         Warn(msg) => {
-            run_action(server, token, poll, SendMe(Warning(&msg).to_raw_protocol()));
+            run_action(server, token,SendMe(Warning(msg)));
         }
         //_ => unimplemented!(),
     }