rust/hedgewars-server/src/server/core.rs
changeset 14786 8ecdb5c6bb2a
parent 14783 b3adc030104b
child 14789 18240b308505
equal deleted inserted replaced
14785:a1077e8d26f4 14786:8ecdb5c6bb2a
     5     room::HWRoom,
     5     room::HWRoom,
     6 };
     6 };
     7 use crate::utils;
     7 use crate::utils;
     8 
     8 
     9 use crate::protocol::messages::HWProtocolMessage::Greeting;
     9 use crate::protocol::messages::HWProtocolMessage::Greeting;
       
    10 use bitflags::*;
    10 use log::*;
    11 use log::*;
    11 use slab;
    12 use slab;
    12 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    13 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    13 
    14 
    14 type Slab<T> = slab::Slab<T>;
    15 type Slab<T> = slab::Slab<T>;
    58             for_old_protocols: "\u{1f994} is watching".to_string(),
    59             for_old_protocols: "\u{1f994} is watching".to_string(),
    59         }
    60         }
    60     }
    61     }
    61 }
    62 }
    62 
    63 
       
    64 bitflags! {
       
    65     pub struct ServerFlags: u8 {
       
    66         const REGISTERED_ONLY = 0b0000_1000;
       
    67     }
       
    68 }
       
    69 
    63 pub struct HWServer {
    70 pub struct HWServer {
    64     pub clients: IndexSlab<HWClient>,
    71     pub clients: IndexSlab<HWClient>,
    65     pub rooms: Slab<HWRoom>,
    72     pub rooms: Slab<HWRoom>,
    66     pub anteroom: HWAnteroom,
    73     pub anteroom: HWAnteroom,
    67     pub latest_protocol: u16,
    74     pub latest_protocol: u16,
       
    75     pub flags: ServerFlags,
    68     pub greetings: ServerGreetings,
    76     pub greetings: ServerGreetings,
    69 }
    77 }
    70 
    78 
    71 impl HWServer {
    79 impl HWServer {
    72     pub fn new(clients_limit: usize, rooms_limit: usize) -> Self {
    80     pub fn new(clients_limit: usize, rooms_limit: usize) -> Self {
    76             clients,
    84             clients,
    77             rooms,
    85             rooms,
    78             anteroom: HWAnteroom::new(clients_limit),
    86             anteroom: HWAnteroom::new(clients_limit),
    79             greetings: ServerGreetings::new(),
    87             greetings: ServerGreetings::new(),
    80             latest_protocol: 58,
    88             latest_protocol: 58,
       
    89             flags: ServerFlags::empty(),
    81         }
    90         }
    82     }
    91     }
    83 
    92 
    84     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    93     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    85         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
    94         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
   181 
   190 
   182     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   191     pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
   183         let room_id = self.clients[self_id].room_id;
   192         let room_id = self.clients[self_id].room_id;
   184         self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id)
   193         self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id)
   185     }
   194     }
       
   195 
       
   196     pub fn is_registered_only(&self) -> bool {
       
   197         self.flags.contains(ServerFlags::REGISTERED_ONLY)
       
   198     }
       
   199 
       
   200     pub fn set_is_registered_only(&mut self, value: bool) {
       
   201         self.flags.set(ServerFlags::REGISTERED_ONLY, value)
       
   202     }
   186 }
   203 }
   187 
   204 
   188 fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom {
   205 fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom {
   189     let entry = rooms.vacant_entry();
   206     let entry = rooms.vacant_entry();
   190     let room = HWRoom::new(entry.key());
   207     let room = HWRoom::new(entry.key());