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,
ServerTimeout,
ServerShutdown,
TooManyPlayers,
Other(String),
}

View File

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

View File

@ -41,6 +41,7 @@ pub enum ServerMsg {
key: Vec2<i32>,
chunk: Box<TerrainChunk>,
},
TooManyPlayers,
Disconnect,
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) {
self.clients.insert(entity, client);
}

View File

@ -357,6 +357,10 @@ impl Server {
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).
client.notify(ServerMsg::InitialSync {
ecs_state: self.state.ecs().gen_state_package(),
@ -364,11 +368,12 @@ impl Server {
server_info: self.server_info.clone(),
});
self.clients.add(entity, client);
frontend_events.push(Event::ClientConnected { entity });
}
self.clients.add(entity, client);
}
Ok(frontend_events)
}

View File

@ -1,12 +1,11 @@
use serde_derive::{Deserialize, Serialize};
use std::{fs, io::prelude::*, net::SocketAddr, path::PathBuf};
/// `ControlSettings` contains keybindings.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct ServerSettings {
pub address: SocketAddr,
//pub max_players: u64,
pub max_players: usize,
pub world_seed: u32,
//pub pvp_enabled: bool,
pub server_name: String,
@ -21,6 +20,7 @@ impl Default for ServerSettings {
world_seed: 1337,
server_name: "Server name".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.
ConnectionFailed(ClientError),
ClientCrashed,
ServerIsFull,
}
// Used to asynchronously parse the server address, resolve host names,
@ -76,6 +77,10 @@ impl ClientInit {
ClientError::Network(_) => {
last_err = Some(Error::ConnectionFailed(err))
}
ClientError::TooManyPlayers => {
last_err = Some(Error::ServerIsFull);
break;
}
// TODO: Handle errors?
_ => panic!(
"Unexpected non-network error when creating client: {:?}",

View File

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