rust/hedgewars-server/src/core/server.rs
changeset 16002 e915ed28726e
parent 16001 8ba2b5007c29
equal deleted inserted replaced
16001:8ba2b5007c29 16002:e915ed28726e
   268             None
   268             None
   269         }
   269         }
   270     }
   270     }
   271 
   271 
   272     #[inline]
   272     #[inline]
   273     pub fn get_room_control(&mut self, client_id: ClientId) -> Option<HwRoomControl> {
   273     pub fn get_room_control(&mut self, client_id: ClientId) -> HwRoomOrServer {
   274         HwRoomControl::new(self, client_id)
   274         HwRoomControl::new(self, client_id)
   275     }
   275     }
   276 
   276 
   277     #[inline]
   277     #[inline]
   278     pub fn is_admin(&self, client_id: ClientId) -> bool {
   278     pub fn is_admin(&self, client_id: ClientId) -> bool {
   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
  1130             }
  1149             }
  1131         }
  1150         }
  1132     }
  1151     }
  1133 }
  1152 }
  1134 
  1153 
       
  1154 impl<'a> Drop for HwRoomControl<'a> {
       
  1155     #[inline]
       
  1156     fn drop(&mut self) {
       
  1157         if self.is_room_removed {
       
  1158             self.server.rooms.remove(self.room_id);
       
  1159         }
       
  1160     }
       
  1161 }
       
  1162 
  1135 fn allocate_room(rooms: &mut Slab<HwRoom>) -> &mut HwRoom {
  1163 fn allocate_room(rooms: &mut Slab<HwRoom>) -> &mut HwRoom {
  1136     let entry = rooms.vacant_entry();
  1164     let entry = rooms.vacant_entry();
  1137     let room = HwRoom::new(entry.key());
  1165     let room = HwRoom::new(entry.key());
  1138     entry.insert(room)
  1166     entry.insert(room)
  1139 }
  1167 }