rust/hedgewars-server/src/server/core.rs
changeset 14789 18240b308505
parent 14786 8ecdb5c6bb2a
equal deleted inserted replaced
14788:6dea1ca64992 14789:18240b308505
   156         self.clients
   156         self.clients
   157             .iter_mut()
   157             .iter_mut()
   158             .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
   158             .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
   159     }
   159     }
   160 
   160 
       
   161     pub fn all_clients(&self) -> impl Iterator<Item = ClientId> + '_ {
       
   162         self.clients.iter().map(|(id, _)| id)
       
   163     }
       
   164 
       
   165     pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a
       
   166     where
       
   167         F: Fn(&(usize, &HWClient)) -> bool + 'a,
       
   168     {
       
   169         self.clients.iter().filter(f).map(|(_, c)| c.id)
       
   170     }
       
   171 
       
   172     pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a
       
   173     where
       
   174         F: Fn(&(usize, &HWRoom)) -> bool + 'a,
       
   175     {
       
   176         self.rooms.iter().filter(f).map(|(_, c)| c.id)
       
   177     }
       
   178 
   161     pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId>
   179     pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId>
   162     where
   180     where
   163         F: Fn(&(usize, &HWClient)) -> bool,
   181         F: Fn(&(usize, &HWClient)) -> bool,
   164     {
   182     {
   165         self.clients.iter().filter(f).map(|(_, c)| c.id).collect()
   183         self.filter_clients(f).collect()
   166     }
   184     }
   167 
   185 
   168     pub fn collect_nicks<F>(&self, f: F) -> Vec<String>
   186     pub fn collect_nicks<F>(&self, f: F) -> Vec<String>
   169     where
   187     where
   170         F: Fn(&(usize, &HWClient)) -> bool,
   188         F: Fn(&(usize, &HWClient)) -> bool,
   174             .filter(f)
   192             .filter(f)
   175             .map(|(_, c)| c.nick.clone())
   193             .map(|(_, c)| c.nick.clone())
   176             .collect()
   194             .collect()
   177     }
   195     }
   178 
   196 
   179     pub fn collect_lobby_clients(&self) -> Vec<ClientId> {
   197     pub fn lobby_clients(&self) -> impl Iterator<Item = ClientId> + '_ {
   180         self.collect_clients(|(_, c)| c.room_id == None)
   198         self.filter_clients(|(_, c)| c.room_id == None)
   181     }
   199     }
   182 
   200 
   183     pub fn collect_room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
   201     pub fn room_clients(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ {
   184         self.collect_clients(|(_, c)| c.room_id == Some(room_id))
   202         self.filter_clients(move |(_, c)| c.room_id == Some(room_id))
   185     }
   203     }
   186 
   204 
   187     pub fn protocol_clients(&self, protocol: u16) -> Vec<ClientId> {
   205     pub fn protocol_clients(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ {
   188         self.collect_clients(|(_, c)| c.protocol_number == protocol)
   206         self.filter_clients(move |(_, c)| c.protocol_number == protocol)
       
   207     }
       
   208 
       
   209     pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ {
       
   210         self.filter_rooms(move |(_, r)| r.protocol_number == protocol)
   189     }
   211     }
   190 
   212 
   191     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   213     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   192         let room_id = self.clients[self_id].room_id;
   214         let room_id = self.clients[self_id].room_id;
   193         self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id)
   215         self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id)