343 pub fn join_room( |
343 pub fn join_room( |
344 &mut self, |
344 &mut self, |
345 client_id: ClientId, |
345 client_id: ClientId, |
346 room_id: RoomId, |
346 room_id: RoomId, |
347 room_password: Option<&str>, |
347 room_password: Option<&str>, |
348 ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
348 ) -> Result<(&HwClient, Option<&HwClient>, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
349 use JoinRoomError::*; |
349 use JoinRoomError::*; |
350 let room = &mut self.rooms[room_id]; |
350 let room = &mut self.rooms[room_id]; |
351 let client = &mut self.clients[client_id]; |
351 let client = &mut self.clients[client_id]; |
352 |
352 |
353 if client.protocol_number != room.protocol_number { |
353 if client.protocol_number != room.protocol_number { |
357 && !client.has_super_power() |
357 && !client.has_super_power() |
358 { |
358 { |
359 Err(WrongPassword) |
359 Err(WrongPassword) |
360 } else if room.is_join_restricted() { |
360 } else if room.is_join_restricted() { |
361 Err(Restricted) |
361 Err(Restricted) |
362 } else if room.is_registration_required() { |
362 } else if room.is_registration_required() && !client.is_registered() { |
363 Err(RegistrationRequired) |
363 Err(RegistrationRequired) |
364 } else if room.players_number == u8::MAX { |
364 } else if room.players_number == u8::MAX { |
365 Err(Full) |
365 Err(Full) |
366 } else { |
366 } else { |
367 move_to_room(client, room); |
367 move_to_room(client, room); |
368 let room_id = room.id; |
368 let room_id = room.id; |
369 Ok(( |
369 Ok(( |
370 &self.clients[client_id], |
370 &self.clients[client_id], |
|
371 room.master_id.map(|id| &self.clients[id]), |
371 &self.rooms[room_id], |
372 &self.rooms[room_id], |
372 self.iter_clients() |
373 self.iter_clients() |
373 .filter(move |c| c.room_id == Some(room_id)), |
374 .filter(move |c| c.room_id == Some(room_id)), |
374 )) |
375 )) |
375 } |
376 } |
379 pub fn join_room_by_name( |
380 pub fn join_room_by_name( |
380 &mut self, |
381 &mut self, |
381 client_id: ClientId, |
382 client_id: ClientId, |
382 room_name: &str, |
383 room_name: &str, |
383 room_password: Option<&str>, |
384 room_password: Option<&str>, |
384 ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
385 ) -> Result<(&HwClient, Option<&HwClient>, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
385 use JoinRoomError::*; |
386 use JoinRoomError::*; |
386 let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
387 let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
387 if let Some((_, room)) = room { |
388 if let Some((_, room)) = room { |
388 let room_id = room.id; |
389 let room_id = room.id; |
389 self.join_room(client_id, room_id, room_password) |
390 self.join_room(client_id, room_id, room_password) |
631 let is_fixed = room.is_fixed(); |
632 let is_fixed = room.is_fixed(); |
632 let was_master = room.master_id == Some(client.id); |
633 let was_master = room.master_id == Some(client.id); |
633 let was_in_game = client.is_in_game(); |
634 let was_in_game = client.is_in_game(); |
634 let mut removed_teams = vec![]; |
635 let mut removed_teams = vec![]; |
635 |
636 |
636 if is_empty && !is_fixed { |
637 self.is_room_removed = is_empty && !is_fixed; |
|
638 |
|
639 if !self.is_room_removed { |
637 if client.is_ready() && room.ready_players_number > 0 { |
640 if client.is_ready() && room.ready_players_number > 0 { |
638 room.ready_players_number -= 1; |
641 room.ready_players_number -= 1; |
639 } |
642 } |
640 |
643 |
641 if let Some(ref mut info) = room.game_info { |
644 if let Some(ref mut info) = room.game_info { |
661 } |
664 } |
662 |
665 |
663 client.set_is_ready(false); |
666 client.set_is_ready(false); |
664 client.set_is_in_game(false); |
667 client.set_is_in_game(false); |
665 |
668 |
666 if !is_fixed { |
669 if !self.is_room_removed && room.master_id == None { |
667 if room.players_number == 0 { |
670 let protocol_number = room.protocol_number; |
668 self.is_room_removed = true |
671 let new_master_id = self.server.room_client_ids(self.room_id).next(); |
669 } else if room.master_id == None { |
672 |
670 let protocol_number = room.protocol_number; |
673 if let Some(new_master_id) = new_master_id { |
671 let new_master_id = self.server.room_client_ids(self.room_id).next(); |
674 let room = self.room_mut(); |
672 |
675 room.master_id = Some(new_master_id); |
673 if let Some(new_master_id) = new_master_id { |
676 let new_master = &mut self.server.clients[new_master_id]; |
674 let room = self.room_mut(); |
677 new_master.set_is_master(true); |
675 room.master_id = Some(new_master_id); |
678 |
676 let new_master = &mut self.server.clients[new_master_id]; |
679 if protocol_number < 42 { |
677 new_master.set_is_master(true); |
680 let nick = new_master.nick.clone(); |
678 |
681 self.room_mut().name = nick; |
679 if protocol_number < 42 { |
|
680 let nick = new_master.nick.clone(); |
|
681 self.room_mut().name = nick; |
|
682 } |
|
683 |
|
684 let room = self.room_mut(); |
|
685 room.set_join_restriction(false); |
|
686 room.set_team_add_restriction(false); |
|
687 room.set_unregistered_players_restriction(true); |
|
688 } |
682 } |
689 } |
683 |
690 } |
684 let room = self.room_mut(); |
691 |
685 room.set_join_restriction(false); |
692 if is_empty && !is_fixed { |
686 room.set_team_add_restriction(false); |
|
687 room.set_unregistered_players_restriction(false); |
|
688 } |
|
689 } |
|
690 |
|
691 if self.is_room_removed { |
693 LeaveRoomResult::RoomRemoved |
692 LeaveRoomResult::RoomRemoved |
694 } else { |
693 } else { |
695 LeaveRoomResult::RoomRemains { |
694 LeaveRoomResult::RoomRemains { |
696 is_empty, |
695 is_empty, |
697 was_master, |
696 was_master, |