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, Option<&HwClient>, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
348 ) -> Result< |
|
349 ( |
|
350 &HwClient, |
|
351 Option<&HwClient>, |
|
352 &HwRoom, |
|
353 impl Iterator<Item = &HwClient> + Clone, |
|
354 ), |
|
355 JoinRoomError, |
|
356 > { |
349 use JoinRoomError::*; |
357 use JoinRoomError::*; |
350 let room = &mut self.rooms[room_id]; |
358 let room = &mut self.rooms[room_id]; |
351 let client = &mut self.clients[client_id]; |
359 let client = &mut self.clients[client_id]; |
352 |
360 |
353 if client.protocol_number != room.protocol_number { |
361 if client.protocol_number != room.protocol_number { |
380 pub fn join_room_by_name( |
388 pub fn join_room_by_name( |
381 &mut self, |
389 &mut self, |
382 client_id: ClientId, |
390 client_id: ClientId, |
383 room_name: &str, |
391 room_name: &str, |
384 room_password: Option<&str>, |
392 room_password: Option<&str>, |
385 ) -> Result<(&HwClient, Option<&HwClient>, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
393 ) -> Result< |
|
394 ( |
|
395 &HwClient, |
|
396 Option<&HwClient>, |
|
397 &HwRoom, |
|
398 impl Iterator<Item = &HwClient> + Clone, |
|
399 ), |
|
400 JoinRoomError, |
|
401 > { |
386 use JoinRoomError::*; |
402 use JoinRoomError::*; |
387 let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
403 let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
388 if let Some((_, room)) = room { |
404 if let Some((_, room)) = room { |
389 let room_id = room.id; |
405 let room_id = room.id; |
390 self.join_room(client_id, room_id, room_password) |
406 self.join_room(client_id, room_id, room_password) |
546 Ok(()) |
562 Ok(()) |
547 } |
563 } |
548 } |
564 } |
549 } |
565 } |
550 |
566 |
|
567 pub enum HwRoomOrServer<'a> { |
|
568 Room(HwRoomControl<'a>), |
|
569 Server(&'a mut HwServer), |
|
570 } |
|
571 |
|
572 impl<'a> HwRoomOrServer<'a> { |
|
573 #[inline] |
|
574 pub fn into_room(self) -> Option<HwRoomControl<'a>> { |
|
575 match self { |
|
576 HwRoomOrServer::Room(control) => Some(control), |
|
577 HwRoomOrServer::Server(_) => None, |
|
578 } |
|
579 } |
|
580 } |
|
581 |
551 pub struct HwRoomControl<'a> { |
582 pub struct HwRoomControl<'a> { |
552 server: &'a mut HwServer, |
583 server: &'a mut HwServer, |
553 client_id: ClientId, |
584 client_id: ClientId, |
554 room_id: RoomId, |
585 room_id: RoomId, |
555 is_room_removed: bool, |
586 is_room_removed: bool, |
556 } |
587 } |
557 |
588 |
558 impl<'a> HwRoomControl<'a> { |
589 impl<'a> HwRoomControl<'a> { |
559 #[inline] |
590 #[inline] |
560 pub fn new(server: &'a mut HwServer, client_id: ClientId) -> Option<Self> { |
591 pub fn new(server: &'a mut HwServer, client_id: ClientId) -> HwRoomOrServer { |
561 if let Some(room_id) = server.clients[client_id].room_id { |
592 if let Some(room_id) = server.clients[client_id].room_id { |
562 Some(Self { |
593 HwRoomOrServer::Room(Self { |
563 server, |
594 server, |
564 client_id, |
595 client_id, |
565 room_id, |
596 room_id, |
566 is_room_removed: false, |
597 is_room_removed: false, |
567 }) |
598 }) |
568 } else { |
599 } else { |
569 None |
600 HwRoomOrServer::Server(server) |
570 } |
|
571 } |
|
572 |
|
573 #[inline] |
|
574 pub fn cleanup_room(self) { |
|
575 if self.is_room_removed { |
|
576 self.server.rooms.remove(self.room_id); |
|
577 } |
601 } |
578 } |
602 } |
579 |
603 |
580 #[inline] |
604 #[inline] |
581 pub fn server(&self) -> &HwServer { |
605 pub fn server(&self) -> &HwServer { |
611 fn get_mut(&mut self) -> (&mut HwClient, &mut HwRoom) { |
635 fn get_mut(&mut self) -> (&mut HwClient, &mut HwRoom) { |
612 ( |
636 ( |
613 &mut self.server.clients[self.client_id], |
637 &mut self.server.clients[self.client_id], |
614 &mut self.server.rooms[self.room_id], |
638 &mut self.server.rooms[self.room_id], |
615 ) |
639 ) |
616 } |
|
617 |
|
618 pub fn change_client<'b: 'a>(self, client_id: ClientId) -> Option<HwRoomControl<'a>> { |
|
619 let room_id = self.room_id; |
|
620 HwRoomControl::new(self.server, client_id).filter(|c| c.room_id == room_id) |
|
621 } |
640 } |
622 |
641 |
623 fn remove_from_room(&mut self, client_id: ClientId) -> LeaveRoomResult { |
642 fn remove_from_room(&mut self, client_id: ClientId) -> LeaveRoomResult { |
624 let (client, room) = self |
643 let (client, room) = self |
625 .server |
644 .server |