rust/hedgewars-server/src/server/core.rs
changeset 14694 25c564f77b7d
parent 14693 6a2e13e36b7f
child 14695 216d39de1a44
equal deleted inserted replaced
14693:6a2e13e36b7f 14694:25c564f77b7d
    55 }
    55 }
    56 
    56 
    57 pub struct HWServer {
    57 pub struct HWServer {
    58     pub clients: IndexSlab<HWClient>,
    58     pub clients: IndexSlab<HWClient>,
    59     pub rooms: Slab<HWRoom>,
    59     pub rooms: Slab<HWRoom>,
    60     pub lobby_id: RoomId,
       
    61     pub output: Vec<(Vec<ClientId>, HWServerMessage)>,
    60     pub output: Vec<(Vec<ClientId>, HWServerMessage)>,
    62     pub removed_clients: Vec<ClientId>,
    61     pub removed_clients: Vec<ClientId>,
    63     pub io: Box<dyn HWServerIO>,
    62     pub io: Box<dyn HWServerIO>,
    64     pub anteroom: HWAnteroom,
    63     pub anteroom: HWAnteroom,
    65 }
    64 }
    66 
    65 
    67 impl HWServer {
    66 impl HWServer {
    68     pub fn new(clients_limit: usize, rooms_limit: usize, io: Box<dyn HWServerIO>) -> Self {
    67     pub fn new(clients_limit: usize, rooms_limit: usize, io: Box<dyn HWServerIO>) -> Self {
    69         let rooms = Slab::with_capacity(rooms_limit);
    68         let rooms = Slab::with_capacity(rooms_limit);
    70         let clients = IndexSlab::with_capacity(clients_limit);
    69         let clients = IndexSlab::with_capacity(clients_limit);
    71         let mut server = Self {
    70         Self {
    72             clients,
    71             clients,
    73             rooms,
    72             rooms,
    74             lobby_id: 0,
       
    75             output: vec![],
    73             output: vec![],
    76             removed_clients: vec![],
    74             removed_clients: vec![],
    77             io,
    75             io,
    78             anteroom: HWAnteroom::new(clients_limit),
    76             anteroom: HWAnteroom::new(clients_limit),
    79         };
    77         }
    80         server.lobby_id = server.add_room().id;
       
    81         server
       
    82     }
    78     }
    83 
    79 
    84     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    80     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    85         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
    81         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
    86             let client = HWClient::new(client_id, protocol.get(), nick);
    82             let client = HWClient::new(client_id, protocol.get(), nick);
   117     #[inline]
   113     #[inline]
   118     pub fn move_to_room(&mut self, client_id: ClientId, room_id: RoomId) {
   114     pub fn move_to_room(&mut self, client_id: ClientId, room_id: RoomId) {
   119         move_to_room(&mut self.clients[client_id], &mut self.rooms[room_id])
   115         move_to_room(&mut self.clients[client_id], &mut self.rooms[room_id])
   120     }
   116     }
   121 
   117 
   122     pub fn lobby(&self) -> &HWRoom {
       
   123         &self.rooms[self.lobby_id]
       
   124     }
       
   125 
       
   126     pub fn has_room(&self, name: &str) -> bool {
   118     pub fn has_room(&self, name: &str) -> bool {
   127         self.rooms.iter().any(|(_, r)| r.name == name)
   119         self.rooms.iter().any(|(_, r)| r.name == name)
   128     }
   120     }
   129 
   121 
   130     pub fn find_room(&self, name: &str) -> Option<&HWRoom> {
   122     pub fn find_room(&self, name: &str) -> Option<&HWRoom> {
   154     pub fn select_clients<F>(&self, f: F) -> Vec<ClientId>
   146     pub fn select_clients<F>(&self, f: F) -> Vec<ClientId>
   155     where
   147     where
   156         F: Fn(&(usize, &HWClient)) -> bool,
   148         F: Fn(&(usize, &HWClient)) -> bool,
   157     {
   149     {
   158         self.clients.iter().filter(f).map(|(_, c)| c.id).collect()
   150         self.clients.iter().filter(f).map(|(_, c)| c.id).collect()
       
   151     }
       
   152 
       
   153     pub fn lobby_clients(&self) -> Vec<ClientId> {
       
   154         self.select_clients(|(_, c)| c.room_id == None)
   159     }
   155     }
   160 
   156 
   161     pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
   157     pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
   162         self.select_clients(|(_, c)| c.room_id == Some(room_id))
   158         self.select_clients(|(_, c)| c.room_id == Some(room_id))
   163     }
   159     }