mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
add configurable max player count
This commit is contained in:
parent
bbb024224d
commit
b6c8bdc223
@ -6,6 +6,7 @@ pub enum Error {
|
||||
ServerWentMad,
|
||||
ServerTimeout,
|
||||
ServerShutdown,
|
||||
TooManyPlayers,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
|
@ -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 => {
|
||||
|
@ -41,6 +41,7 @@ pub enum ServerMsg {
|
||||
key: Vec2<i32>,
|
||||
chunk: Box<TerrainChunk>,
|
||||
},
|
||||
TooManyPlayers,
|
||||
Disconnect,
|
||||
Shutdown,
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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: {:?}",
|
||||
|
@ -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",
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user