rust/hedgewars-server/src/server/core.rs
changeset 14781 01f8ab45f806
parent 14780 65861ba8b4e8
child 14783 b3adc030104b
equal deleted inserted replaced
14780:65861ba8b4e8 14781:01f8ab45f806
    14 
    14 
    15 pub struct HWAnteClient {
    15 pub struct HWAnteClient {
    16     pub nick: Option<String>,
    16     pub nick: Option<String>,
    17     pub protocol_number: Option<NonZeroU16>,
    17     pub protocol_number: Option<NonZeroU16>,
    18     pub server_salt: String,
    18     pub server_salt: String,
       
    19     pub is_checker: bool,
    19 }
    20 }
    20 
    21 
    21 pub struct HWAnteroom {
    22 pub struct HWAnteroom {
    22     pub clients: IndexSlab<HWAnteClient>,
    23     pub clients: IndexSlab<HWAnteClient>,
    23 }
    24 }
    31     pub fn add_client(&mut self, client_id: ClientId, salt: String) {
    32     pub fn add_client(&mut self, client_id: ClientId, salt: String) {
    32         let client = HWAnteClient {
    33         let client = HWAnteClient {
    33             nick: None,
    34             nick: None,
    34             protocol_number: None,
    35             protocol_number: None,
    35             server_salt: salt,
    36             server_salt: salt,
       
    37             is_checker: false,
    36         };
    38         };
    37         self.clients.insert(client_id, client);
    39         self.clients.insert(client_id, client);
    38     }
    40     }
    39 
    41 
    40     pub fn remove_client(&mut self, client_id: ClientId) -> Option<HWAnteClient> {
    42     pub fn remove_client(&mut self, client_id: ClientId) -> Option<HWAnteClient> {
    60         }
    62         }
    61     }
    63     }
    62 
    64 
    63     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    65     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    64         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
    66         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
    65             let client = HWClient::new(client_id, protocol.get(), nick);
    67             let mut client = HWClient::new(client_id, protocol.get(), nick);
       
    68             client.set_is_checker(data.is_checker);
    66             self.clients.insert(client_id, client);
    69             self.clients.insert(client_id, client);
    67         }
    70         }
    68     }
    71     }
    69 
    72 
    70     pub fn remove_client(&mut self, client_id: ClientId) {
    73     pub fn remove_client(&mut self, client_id: ClientId) {
   117         self.clients
   120         self.clients
   118             .iter_mut()
   121             .iter_mut()
   119             .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
   122             .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
   120     }
   123     }
   121 
   124 
   122     pub fn select_clients<F>(&self, f: F) -> Vec<ClientId>
   125     pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId>
   123     where
   126     where
   124         F: Fn(&(usize, &HWClient)) -> bool,
   127         F: Fn(&(usize, &HWClient)) -> bool,
   125     {
   128     {
   126         self.clients.iter().filter(f).map(|(_, c)| c.id).collect()
   129         self.clients.iter().filter(f).map(|(_, c)| c.id).collect()
   127     }
   130     }
   128 
   131 
   129     pub fn lobby_clients(&self) -> Vec<ClientId> {
   132     pub fn collect_nicks<F>(&self, f: F) -> Vec<String>
   130         self.select_clients(|(_, c)| c.room_id == None)
   133     where
   131     }
   134         F: Fn(&(usize, &HWClient)) -> bool,
   132 
   135     {
   133     pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
   136         self.clients
   134         self.select_clients(|(_, c)| c.room_id == Some(room_id))
   137             .iter()
       
   138             .filter(f)
       
   139             .map(|(_, c)| c.nick.clone())
       
   140             .collect()
       
   141     }
       
   142 
       
   143     pub fn collect_lobby_clients(&self) -> Vec<ClientId> {
       
   144         self.collect_clients(|(_, c)| c.room_id == None)
       
   145     }
       
   146 
       
   147     pub fn collect_room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
       
   148         self.collect_clients(|(_, c)| c.room_id == Some(room_id))
   135     }
   149     }
   136 
   150 
   137     pub fn protocol_clients(&self, protocol: u16) -> Vec<ClientId> {
   151     pub fn protocol_clients(&self, protocol: u16) -> Vec<ClientId> {
   138         self.select_clients(|(_, c)| c.protocol_number == protocol)
   152         self.collect_clients(|(_, c)| c.protocol_number == protocol)
   139     }
   153     }
   140 
   154 
   141     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   155     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   142         let room_id = self.clients[self_id].room_id;
   156         let room_id = self.clients[self_id].room_id;
   143         self.select_clients(|(id, c)| *id != self_id && c.room_id == room_id)
   157         self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id)
   144     }
   158     }
   145 }
   159 }
   146 
   160 
   147 fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom {
   161 fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom {
   148     let entry = rooms.vacant_entry();
   162     let entry = rooms.vacant_entry();