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

View File

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

View File

@ -5,7 +5,7 @@ pub mod server;
// Reexports // Reexports
pub use self::client::ClientMsg; pub use self::client::ClientMsg;
pub use self::ecs_packet::{EcsCompPacket, EcsResPacket}; 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)] #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ClientState { pub enum ClientState {

View File

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

View File

@ -15,7 +15,7 @@ use crate::{
}; };
use common::{ use common::{
comp, comp,
msg::{ClientMsg, ClientState, RequestStateError, ServerInfo, ServerMsg}, msg::{ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg},
net::PostOffice, net::PostOffice,
state::{State, TerrainChange, Uid}, state::{State, TerrainChange, Uid},
terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainMap}, terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainMap},
@ -357,9 +357,8 @@ 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() { if self.server_settings.max_players <= self.clients.len() {
client.notify(ServerMsg::TooManyPlayers); client.notify(ServerMsg::Error(ServerError::TooManyPlayers));
} else { } 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 {