rust/hedgewars-server/src/server/core.rs
changeset 14804 b3adc030104b
parent 14802 01f8ab45f806
child 14807 8ecdb5c6bb2a
equal deleted inserted replaced
14803:50fcef24003f 14804:b3adc030104b
     4     indexslab::IndexSlab,
     4     indexslab::IndexSlab,
     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 log::*;
    10 use log::*;
    10 use slab;
    11 use slab;
    11 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    12 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    12 
    13 
    13 type Slab<T> = slab::Slab<T>;
    14 type Slab<T> = slab::Slab<T>;
    43         let mut client = self.clients.remove(client_id);
    44         let mut client = self.clients.remove(client_id);
    44         client
    45         client
    45     }
    46     }
    46 }
    47 }
    47 
    48 
       
    49 pub struct ServerGreetings {
       
    50     pub for_latest_protocol: String,
       
    51     pub for_old_protocols: String,
       
    52 }
       
    53 
       
    54 impl ServerGreetings {
       
    55     fn new() -> Self {
       
    56         Self {
       
    57             for_latest_protocol: "\u{1f994} is watching".to_string(),
       
    58             for_old_protocols: "\u{1f994} is watching".to_string(),
       
    59         }
       
    60     }
       
    61 }
       
    62 
    48 pub struct HWServer {
    63 pub struct HWServer {
    49     pub clients: IndexSlab<HWClient>,
    64     pub clients: IndexSlab<HWClient>,
    50     pub rooms: Slab<HWRoom>,
    65     pub rooms: Slab<HWRoom>,
    51     pub anteroom: HWAnteroom,
    66     pub anteroom: HWAnteroom,
       
    67     pub latest_protocol: u16,
       
    68     pub greetings: ServerGreetings,
    52 }
    69 }
    53 
    70 
    54 impl HWServer {
    71 impl HWServer {
    55     pub fn new(clients_limit: usize, rooms_limit: usize) -> Self {
    72     pub fn new(clients_limit: usize, rooms_limit: usize) -> Self {
    56         let rooms = Slab::with_capacity(rooms_limit);
    73         let rooms = Slab::with_capacity(rooms_limit);
    57         let clients = IndexSlab::with_capacity(clients_limit);
    74         let clients = IndexSlab::with_capacity(clients_limit);
    58         Self {
    75         Self {
    59             clients,
    76             clients,
    60             rooms,
    77             rooms,
    61             anteroom: HWAnteroom::new(clients_limit),
    78             anteroom: HWAnteroom::new(clients_limit),
       
    79             greetings: ServerGreetings::new(),
       
    80             latest_protocol: 58,
    62         }
    81         }
    63     }
    82     }
    64 
    83 
    65     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    84     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    66         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
    85         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
    70         }
    89         }
    71     }
    90     }
    72 
    91 
    73     pub fn remove_client(&mut self, client_id: ClientId) {
    92     pub fn remove_client(&mut self, client_id: ClientId) {
    74         self.clients.remove(client_id);
    93         self.clients.remove(client_id);
       
    94     }
       
    95 
       
    96     pub fn get_greetings(&self, client_id: ClientId) -> &str {
       
    97         if self.clients[client_id].protocol_number < self.latest_protocol {
       
    98             &self.greetings.for_old_protocols
       
    99         } else {
       
   100             &self.greetings.for_latest_protocol
       
   101         }
    75     }
   102     }
    76 
   103 
    77     #[inline]
   104     #[inline]
    78     pub fn create_room(
   105     pub fn create_room(
    79         &mut self,
   106         &mut self,