add configurable max player count

This commit is contained in:
Songtronix 2019-07-01 13:19:26 +02:00
parent bbb024224d
commit b6c8bdc223
8 changed files with 29 additions and 10 deletions

View File

@ -6,6 +6,7 @@ pub enum Error {
ServerWentMad, ServerWentMad,
ServerTimeout, ServerTimeout,
ServerShutdown, ServerShutdown,
TooManyPlayers,
Other(String), Other(String),
} }

View File

@ -73,6 +73,7 @@ impl Client {
.ok_or(Error::ServerWentMad)?; .ok_or(Error::ServerWentMad)?;
(state, entity, server_info) (state, entity, server_info)
} }
Some(ServerMsg::TooManyPlayers) => return Err(Error::TooManyPlayers),
_ => return Err(Error::ServerWentMad), _ => return Err(Error::ServerWentMad),
}; };
@ -362,6 +363,7 @@ impl Client {
for msg in new_msgs { for msg in new_msgs {
match msg { match msg {
ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad), ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad),
ServerMsg::TooManyPlayers => return Err(Error::ServerWentMad),
ServerMsg::Shutdown => return Err(Error::ServerShutdown), ServerMsg::Shutdown => return Err(Error::ServerShutdown),
ServerMsg::Ping => self.postbox.send_message(ClientMsg::Pong), ServerMsg::Ping => self.postbox.send_message(ClientMsg::Pong),
ServerMsg::Pong => { ServerMsg::Pong => {

View File

@ -41,6 +41,7 @@ pub enum ServerMsg {
key: Vec2<i32>, key: Vec2<i32>,
chunk: Box<TerrainChunk>, chunk: Box<TerrainChunk>,
}, },
TooManyPlayers,
Disconnect, Disconnect,
Shutdown, Shutdown,
} }

View File

@ -41,6 +41,10 @@ impl Clients {
} }
} }
pub fn len(&mut self) -> usize {
self.clients.len()
}
pub fn add(&mut self, entity: EcsEntity, client: Client) { pub fn add(&mut self, entity: EcsEntity, client: Client) {
self.clients.insert(entity, client); self.clients.insert(entity, client);
} }

View File

@ -357,6 +357,10 @@ impl Server {
last_ping: self.state.get_time(), last_ping: self.state.get_time(),
}; };
// TODO: Figure out if this if/else if correct
if self.server_settings.max_players <= self.clients.len() {
client.notify(ServerMsg::TooManyPlayers);
} else {
// Return the state of the current world (all of the components that Sphynx tracks). // Return the state of the current world (all of the components that Sphynx tracks).
client.notify(ServerMsg::InitialSync { client.notify(ServerMsg::InitialSync {
ecs_state: self.state.ecs().gen_state_package(), ecs_state: self.state.ecs().gen_state_package(),
@ -364,11 +368,12 @@ impl Server {
server_info: self.server_info.clone(), server_info: self.server_info.clone(),
}); });
self.clients.add(entity, client);
frontend_events.push(Event::ClientConnected { entity }); frontend_events.push(Event::ClientConnected { entity });
} }
self.clients.add(entity, client);
}
Ok(frontend_events) Ok(frontend_events)
} }

View File

@ -1,12 +1,11 @@
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::{fs, io::prelude::*, net::SocketAddr, path::PathBuf}; use std::{fs, io::prelude::*, net::SocketAddr, path::PathBuf};
/// `ControlSettings` contains keybindings.
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)] #[serde(default)]
pub struct ServerSettings { pub struct ServerSettings {
pub address: SocketAddr, pub address: SocketAddr,
//pub max_players: u64, pub max_players: usize,
pub world_seed: u32, pub world_seed: u32,
//pub pvp_enabled: bool, //pub pvp_enabled: bool,
pub server_name: String, pub server_name: String,
@ -21,6 +20,7 @@ impl Default for ServerSettings {
world_seed: 1337, world_seed: 1337,
server_name: "Server name".to_owned(), server_name: "Server name".to_owned(),
server_description: "This is the best Veloren server.".to_owned(), server_description: "This is the best Veloren server.".to_owned(),
max_players: 16,
} }
} }
} }

View File

@ -20,6 +20,7 @@ pub enum Error {
// Parsing/host name resolution successful but could not connect. // Parsing/host name resolution successful but could not connect.
ConnectionFailed(ClientError), ConnectionFailed(ClientError),
ClientCrashed, ClientCrashed,
ServerIsFull,
} }
// Used to asynchronously parse the server address, resolve host names, // Used to asynchronously parse the server address, resolve host names,
@ -76,6 +77,10 @@ impl ClientInit {
ClientError::Network(_) => { ClientError::Network(_) => {
last_err = Some(Error::ConnectionFailed(err)) last_err = Some(Error::ConnectionFailed(err))
} }
ClientError::TooManyPlayers => {
last_err = Some(Error::ServerIsFull);
break;
}
// TODO: Handle errors? // TODO: Handle errors?
_ => panic!( _ => panic!(
"Unexpected non-network error when creating client: {:?}", "Unexpected non-network error when creating client: {:?}",

View File

@ -64,6 +64,7 @@ impl PlayState for MainMenuState {
self.main_menu_ui.login_error( self.main_menu_ui.login_error(
match err { match err {
InitError::BadAddress(_) | InitError::NoAddress => "Server not found", InitError::BadAddress(_) | InitError::NoAddress => "Server not found",
InitError::ServerIsFull => "Server is Full!",
InitError::ConnectionFailed(_) => "Connection failed", InitError::ConnectionFailed(_) => "Connection failed",
InitError::ClientCrashed => "Client crashed", InitError::ClientCrashed => "Client crashed",
} }