rust/hedgewars-server/src/server/core.rs
changeset 14702 f64e21f164a5
parent 14701 8a45c90f4580
child 14784 f43ab2bd76ae
equal deleted inserted replaced
14701:8a45c90f4580 14702:f64e21f164a5
    79         }
    79         }
    80     }
    80     }
    81 
    81 
    82     pub fn remove_client(&mut self, client_id: ClientId) {
    82     pub fn remove_client(&mut self, client_id: ClientId) {
    83         self.clients.remove(client_id);
    83         self.clients.remove(client_id);
    84     }
       
    85 
       
    86     pub fn add_room(&mut self) -> &mut HWRoom {
       
    87         allocate_room(&mut self.rooms)
       
    88     }
    84     }
    89 
    85 
    90     #[inline]
    86     #[inline]
    91     pub fn create_room(
    87     pub fn create_room(
    92         &mut self,
    88         &mut self,
   106     pub fn move_to_room(&mut self, client_id: ClientId, room_id: RoomId) {
   102     pub fn move_to_room(&mut self, client_id: ClientId, room_id: RoomId) {
   107         move_to_room(&mut self.clients[client_id], &mut self.rooms[room_id])
   103         move_to_room(&mut self.clients[client_id], &mut self.rooms[room_id])
   108     }
   104     }
   109 
   105 
   110     pub fn has_room(&self, name: &str) -> bool {
   106     pub fn has_room(&self, name: &str) -> bool {
   111         self.rooms.iter().any(|(_, r)| r.name == name)
   107         self.find_room(name).is_some()
   112     }
   108     }
   113 
   109 
   114     pub fn find_room(&self, name: &str) -> Option<&HWRoom> {
   110     pub fn find_room(&self, name: &str) -> Option<&HWRoom> {
   115         self.rooms
   111         self.rooms
   116             .iter()
   112             .iter()
   156 
   152 
   157     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   153     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   158         let room_id = self.clients[self_id].room_id;
   154         let room_id = self.clients[self_id].room_id;
   159         self.select_clients(|(id, c)| *id != self_id && c.room_id == room_id)
   155         self.select_clients(|(id, c)| *id != self_id && c.room_id == room_id)
   160     }
   156     }
   161 
       
   162     pub fn client_and_room(&mut self, client_id: ClientId) -> (&mut HWClient, Option<&mut HWRoom>) {
       
   163         let c = &mut self.clients[client_id];
       
   164         if let Some(room_id) = c.room_id {
       
   165             (c, Some(&mut self.rooms[room_id]))
       
   166         } else {
       
   167             (c, None)
       
   168         }
       
   169     }
       
   170 
       
   171     pub fn room(&mut self, client_id: ClientId) -> Option<&mut HWRoom> {
       
   172         self.client_and_room(client_id).1
       
   173     }
       
   174 }
   157 }
   175 
   158 
   176 fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom {
   159 fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom {
   177     let entry = rooms.vacant_entry();
   160     let entry = rooms.vacant_entry();
   178     let key = entry.key();
       
   179     let room = HWRoom::new(entry.key());
   161     let room = HWRoom::new(entry.key());
   180     entry.insert(room)
   162     entry.insert(room)
   181 }
   163 }
   182 
   164 
   183 fn create_room(
   165 fn create_room(