generalise errors based on zests suggestion

This commit is contained in:
Songtronix 2019-07-04 18:14:45 +02:00
parent e2b83883f3
commit b41508f025
5 changed files with 20 additions and 9 deletions

View File

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

View File

@ -9,7 +9,7 @@ pub use specs::Entity as EcsEntity;
use common::{
comp,
msg::{ClientMsg, ClientState, ServerInfo, ServerMsg},
msg::{ClientMsg, ClientState, ServerError, ServerInfo, ServerMsg},
net::PostBox,
state::State,
terrain::{block::Block, chonk::ChonkMetrics, TerrainChunk, TerrainChunkSize},
@ -73,7 +73,9 @@ impl Client {
.ok_or(Error::ServerWentMad)?;
(state, entity, server_info)
}
Some(ServerMsg::TooManyPlayers) => return Err(Error::TooManyPlayers),
Some(ServerMsg::Error(ServerError::TooManyPlayers)) => {
return Err(Error::TooManyPlayers)
}
_ => return Err(Error::ServerWentMad),
};
@ -362,9 +364,12 @@ impl Client {
if new_msgs.len() > 0 {
for msg in new_msgs {
match msg {
ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad),
ServerMsg::TooManyPlayers => return Err(Error::ServerWentMad),
ServerMsg::Error(e) => match e {
ServerError::TooManyPlayers => return Err(Error::ServerWentMad),
ServerError::InvalidAlias => return Err(Error::InvalidAlias),
},
ServerMsg::Shutdown => return Err(Error::ServerShutdown),
ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad),
ServerMsg::Ping => self.postbox.send_message(ClientMsg::Pong),
ServerMsg::Pong => {
self.last_ping_delta = Instant::now()

View File

@ -5,7 +5,7 @@ pub mod server;
// Reexports
pub use self::client::ClientMsg;
pub use self::ecs_packet::{EcsCompPacket, EcsResPacket};
pub use self::server::{RequestStateError, ServerInfo, ServerMsg};
pub use self::server::{RequestStateError, ServerError, ServerInfo, ServerMsg};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ClientState {

View File

@ -41,7 +41,13 @@ pub enum ServerMsg {
key: Vec2<i32>,
chunk: Box<TerrainChunk>,
},
TooManyPlayers,
Error(ServerError),
Disconnect,
Shutdown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ServerError {
TooManyPlayers,
InvalidAlias,
}

View File

@ -15,7 +15,7 @@ use crate::{
};
use common::{
comp,
msg::{ClientMsg, ClientState, RequestStateError, ServerInfo, ServerMsg},
msg::{ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg},
net::PostOffice,
state::{State, TerrainChange, Uid},
terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainMap},
@ -357,9 +357,8 @@ 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);
client.notify(ServerMsg::Error(ServerError::TooManyPlayers));
} else {
// Return the state of the current world (all of the components that Sphynx tracks).
client.notify(ServerMsg::InitialSync {