diff -r 6dea1ca64992 -r 18240b308505 rust/hedgewars-server/src/server/core.rs --- a/rust/hedgewars-server/src/server/core.rs Thu Apr 11 19:30:22 2019 +0300 +++ b/rust/hedgewars-server/src/server/core.rs Thu Apr 11 21:20:41 2019 +0300 @@ -158,11 +158,29 @@ .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) } + pub fn all_clients(&self) -> impl Iterator + '_ { + self.clients.iter().map(|(id, _)| id) + } + + pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator + 'a + where + F: Fn(&(usize, &HWClient)) -> bool + 'a, + { + self.clients.iter().filter(f).map(|(_, c)| c.id) + } + + pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator + 'a + where + F: Fn(&(usize, &HWRoom)) -> bool + 'a, + { + self.rooms.iter().filter(f).map(|(_, c)| c.id) + } + pub fn collect_clients(&self, f: F) -> Vec where F: Fn(&(usize, &HWClient)) -> bool, { - self.clients.iter().filter(f).map(|(_, c)| c.id).collect() + self.filter_clients(f).collect() } pub fn collect_nicks(&self, f: F) -> Vec @@ -176,16 +194,20 @@ .collect() } - pub fn collect_lobby_clients(&self) -> Vec { - self.collect_clients(|(_, c)| c.room_id == None) + pub fn lobby_clients(&self) -> impl Iterator + '_ { + self.filter_clients(|(_, c)| c.room_id == None) } - pub fn collect_room_clients(&self, room_id: RoomId) -> Vec { - self.collect_clients(|(_, c)| c.room_id == Some(room_id)) + pub fn room_clients(&self, room_id: RoomId) -> impl Iterator + '_ { + self.filter_clients(move |(_, c)| c.room_id == Some(room_id)) } - pub fn protocol_clients(&self, protocol: u16) -> Vec { - self.collect_clients(|(_, c)| c.protocol_number == protocol) + pub fn protocol_clients(&self, protocol: u16) -> impl Iterator + '_ { + self.filter_clients(move |(_, c)| c.protocol_number == protocol) + } + + pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator + '_ { + self.filter_rooms(move |(_, r)| r.protocol_number == protocol) } pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec {