105 room: &HwRoom, |
105 room: &HwRoom, |
106 room_clients: I, |
106 room_clients: I, |
107 response: &mut Response, |
107 response: &mut Response, |
108 ) { |
108 ) { |
109 #[inline] |
109 #[inline] |
110 fn collect_nicks<'a, I, F>(clients: I, f: F) -> Vec<String> |
110 fn partition_nicks<'a, I, F>(clients: I, f: F) -> (Vec<String>, Vec<String>) |
111 where |
111 where |
112 I: Iterator<Item = &'a HwClient>, |
112 I: Iterator<Item = &'a HwClient> + Clone, |
113 F: Fn(&&'a HwClient) -> bool, |
113 F: Fn(&&'a HwClient) -> bool, |
114 { |
114 { |
115 clients.filter(f).map(|c| &c.nick).cloned().collect() |
115 ( |
|
116 clients |
|
117 .clone() |
|
118 .filter(|c| f(c)) |
|
119 .map(|c| &c.nick) |
|
120 .cloned() |
|
121 .collect(), |
|
122 clients |
|
123 .filter(|c| !f(c)) |
|
124 .map(|c| &c.nick) |
|
125 .cloned() |
|
126 .collect(), |
|
127 ) |
116 } |
128 } |
117 |
129 |
118 let nick = client.nick.clone(); |
130 let nick = client.nick.clone(); |
119 response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room.id)); |
131 response.add( |
|
132 RoomJoined(vec![nick.clone()]) |
|
133 .send_all() |
|
134 .in_room(room.id) |
|
135 .but_self(), |
|
136 ); |
120 response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all()); |
137 response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all()); |
121 let nicks = collect_nicks(room_clients.clone(), |c| c.room_id == Some(room.id)); |
138 let nicks = room_clients.clone().map(|c| c.nick.clone()).collect(); |
122 response.add(RoomJoined(nicks).send_self()); |
139 response.add(RoomJoined(nicks).send_self()); |
123 |
140 |
124 get_room_teams(room, client.id, response); |
141 get_room_teams(room, client.id, response); |
125 get_room_config(room, client.id, response); |
142 get_room_config(room, client.id, response); |
126 |
143 |
127 let mut flag_selectors = [ |
144 let mut flag_selectors = [ |
128 ( |
145 ( |
129 Flags::RoomMaster, |
146 Flags::RoomMaster, |
130 collect_nicks(room_clients.clone(), |c| c.is_master()), |
147 partition_nicks(room_clients.clone(), |c| c.is_master()), |
131 ), |
148 ), |
132 ( |
149 ( |
133 Flags::Ready, |
150 Flags::Ready, |
134 collect_nicks(room_clients.clone(), |c| c.is_ready()), |
151 partition_nicks(room_clients.clone(), |c| c.is_ready()), |
135 ), |
152 ), |
136 ( |
153 ( |
137 Flags::InGame, |
154 Flags::InGame, |
138 collect_nicks(room_clients.clone(), |c| c.is_in_game()), |
155 partition_nicks(room_clients.clone(), |c| c.is_in_game()), |
139 ), |
156 ), |
140 ]; |
157 ]; |
141 |
158 |
142 for (flag, nicks) in &mut flag_selectors { |
159 for (flag, (set_nicks, cleared_nicks)) in &mut flag_selectors { |
143 response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self()); |
160 if !set_nicks.is_empty() { |
|
161 response.add(ClientFlags(add_flags(&[*flag]), replace(set_nicks, vec![])).send_self()); |
|
162 } |
|
163 |
|
164 if !cleared_nicks.is_empty() { |
|
165 response.add( |
|
166 ClientFlags(remove_flags(&[*flag]), replace(cleared_nicks, vec![])).send_self(), |
|
167 ); |
|
168 } |
144 } |
169 } |
145 |
170 |
146 if !room.greeting.is_empty() { |
171 if !room.greeting.is_empty() { |
147 response.add( |
172 response.add( |
148 ChatMsg { |
173 ChatMsg { |
156 |
181 |
157 pub fn get_room_join_error(error: JoinRoomError, response: &mut Response) { |
182 pub fn get_room_join_error(error: JoinRoomError, response: &mut Response) { |
158 use super::strings::*; |
183 use super::strings::*; |
159 match error { |
184 match error { |
160 JoinRoomError::DoesntExist => response.warn(NO_ROOM), |
185 JoinRoomError::DoesntExist => response.warn(NO_ROOM), |
161 JoinRoomError::WrongProtocol => response.warn(WRONG_PROTOCOL), |
186 JoinRoomError::WrongProtocol => response.warn(INCOMPATIBLE_ROOM_PROTOCOL), |
162 JoinRoomError::WrongPassword => { |
187 JoinRoomError::WrongPassword => { |
163 response.add(Notice("WrongPassword".to_string()).send_self()) |
188 response.add(Notice("WrongPassword".to_string()).send_self()) |
164 } |
189 } |
165 JoinRoomError::Full => response.warn(ROOM_FULL), |
190 JoinRoomError::Full => response.warn(ROOM_FULL), |
166 JoinRoomError::Restricted => response.warn(ROOM_JOIN_RESTRICTED), |
191 JoinRoomError::Restricted => response.warn(ROOM_JOIN_RESTRICTED), |
|
192 JoinRoomError::RegistrationRequired => response.warn(ROOM_REGISTRATION_REQUIRED), |
167 } |
193 } |
168 } |
194 } |
169 |
195 |
170 pub fn get_remove_teams_data( |
196 pub fn get_remove_teams_data( |
171 room_id: RoomId, |
197 room_id: RoomId, |