gameServer2/src/server/server.rs
changeset 13416 cdf69667593b
parent 13119 1e39b8749072
child 13419 81e0ed105f5d
--- a/gameServer2/src/server/server.rs	Thu Jun 14 16:44:27 2018 -0400
+++ b/gameServer2/src/server/server.rs	Mon Jun 18 09:22:53 2018 -0400
@@ -1,20 +1,18 @@
 use slab;
-use mio::net::*;
-use mio::*;
-use std::io;
-
 use utils;
-use super::client::*;
-use super::room::*;
-use super::actions;
+use super::{
+    client::*, room::*, actions, handlers
+};
 use protocol::messages::*;
-use super::handlers;
 
 type Slab<T> = slab::Slab<T>;
 
+#[derive(Debug)]
 pub enum Destination {
+    ToAll,
     ToSelf(ClientId),
-    ToOthers(ClientId)
+    ToOthers(ClientId),
+    ToSelected(Vec<ClientId>)
 }
 
 pub struct PendingMessage(pub Destination, pub HWServerMessage);
@@ -67,9 +65,15 @@
     }
 
     pub fn handle_msg(&mut self, client_id: ClientId, msg: HWProtocolMessage) {
+        debug!("Handling message {:?} for client {}", msg, client_id);
         handlers::handle(self, client_id, msg);
     }
 
+    pub fn send_all(&mut self, msg: HWServerMessage) {
+        self.output.push(PendingMessage(
+            Destination::ToAll, msg));
+    }
+
     pub fn send_self(&mut self, client_id: ClientId, msg: HWServerMessage) {
         self.output.push(PendingMessage(
             Destination::ToSelf(client_id), msg));
@@ -80,9 +84,54 @@
             Destination::ToOthers(client_id), msg));
     }
 
+    pub fn send_to_selected(&mut self, client_ids: Vec<ClientId>, msg: HWServerMessage) {
+        self.output.push(PendingMessage(
+            Destination::ToSelected(client_ids), msg));
+    }
+
     pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) {
         for action in actions {
             actions::run_action(self, client_id, action);
         }
     }
-}
+
+    pub fn has_room(&self, name: &str) -> bool {
+        self.rooms.iter().any(|(_, r)| r.name == name)
+    }
+
+    pub fn find_room(&self, name: &str) -> Option<&HWRoom> {
+        self.rooms.iter().find(|(_, r)| r.name == name).map(|(_, r)| r)
+    }
+
+    pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HWRoom> {
+        self.rooms.iter_mut().find(|(_, r)| r.name == name).map(|(_, r)| r)
+    }
+
+    pub fn select_clients<F>(&self, f: F) -> Vec<ClientId>
+        where F: Fn(&(usize, &HWClient)) -> bool {
+        self.clients.iter().filter(f)
+            .map(|(_, c)| c.id).collect()
+    }
+
+    pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
+        self.select_clients(|(_, c)| c.room_id == Some(room_id))
+    }
+
+    pub fn protocol_clients(&self, protocol: u32) -> Vec<ClientId> {
+        self.select_clients(|(_, c)| c.protocol_number == protocol)
+    }
+
+    pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
+        let room_id = self.clients[self_id].room_id;
+        self.select_clients(|(id, c)| *id != self_id && c.room_id == room_id )
+    }
+
+    pub fn client_and_room(&mut self, client_id: ClientId) -> (&mut HWClient, Option<&mut HWRoom>) {
+        let c = &mut self.clients[client_id];
+        if let Some(room_id) = c.room_id {
+            (c, Some(&mut self.rooms[room_id]))
+        } else {
+            (c, None)
+        }
+    }
+}
\ No newline at end of file