diff --git a/client/src/lib.rs b/client/src/lib.rs index d3d3fd7784..6d2dbe996a 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -53,21 +53,16 @@ impl Client { #[allow(dead_code)] pub fn new>( addr: A, - player: comp::Player, view_distance: u64, ) -> Result { - let mut client_state = ClientState::Connected; let mut postbox = PostBox::to(addr)?; - // Send connection request - postbox.send_message(ClientMsg::Connect { player }); - // Wait for initial sync let (state, player_entity) = match postbox.next_message() { - Some(ServerMsg::InitialSync { ecs_state, player_entity_uid }) => { + Some(ServerMsg::InitialSync { ecs_state, entity_uid }) => { let mut state = State::from_state_package(ecs_state); - let player_entity = state.ecs().entity_from_uid(player_entity_uid).ok_or(Error::ServerWentMad)?; + let player_entity = state.ecs().entity_from_uid(entity_uid).ok_or(Error::ServerWentMad)?; (state, player_entity) }, _ => return Err(Error::ServerWentMad), @@ -91,6 +86,10 @@ impl Client { }) } + pub fn register(&mut self, player: comp::Player) { + self.postbox.send_message(ClientMsg::Register { player }); + } + /// Get a reference to the client's worker thread pool. This pool should be used for any /// computationally expensive operations that run outside of the main thread (i.e: threads that /// block on I/O operations are exempt). diff --git a/common/src/msg/client.rs b/common/src/msg/client.rs index 3f1afe1370..2f1ea8f25c 100644 --- a/common/src/msg/client.rs +++ b/common/src/msg/client.rs @@ -4,7 +4,7 @@ use crate::comp; #[derive(Debug, Clone, Serialize, Deserialize)] pub enum ClientMsg { - Connect { player: comp::Player }, + Register { player: comp::Player }, Character(comp::Character), RequestState(ClientState), Ping, diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index 86fc96c665..60f4498a90 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -14,12 +14,12 @@ pub enum RequestStateError { #[derive(Clone, Serialize, Deserialize)] pub enum ServerMsg { - StateAnswer(Result), - ForceState(ClientState), InitialSync { ecs_state: sphynx::StatePackage, - player_entity_uid: u64, + entity_uid: u64, }, + StateAnswer(Result), + ForceState(ClientState), Ping, Pong, Chat(String), diff --git a/server/src/lib.rs b/server/src/lib.rs index d5db7688e1..1dd15e6f0b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -213,14 +213,26 @@ impl Server { for mut postbox in self.postoffice.new_postboxes() { let entity = self.state.ecs_mut().create_entity_synced().build(); + let mut client = Client { + client_state: ClientState::Connected, + postbox, + last_ping: self.state.get_time(), + }; + + // Return the state of the current world + // (All components Sphynx tracks) + client.notify(ServerMsg::InitialSync { + ecs_state: self.state.ecs().gen_state_package(), + entity_uid: self.state + .ecs() + .uid_from_entity(entity) + .unwrap() + .into(), + }); self.clients.add( entity, - Client { - client_state: ClientState::Connected, - postbox, - last_ping: self.state.get_time(), - }, + client, ); frontend_events.push(Event::ClientConnected { entity }); @@ -250,7 +262,7 @@ impl Server { for msg in new_msgs { match msg { ClientMsg::RequestState(requested_state) => match requested_state { - ClientState::Connected => disconnect = true, + ClientState::Connected => disconnect = true, // Default state ClientState::Registered => match client.client_state { ClientState::Connected => {}, // Use ClientMsg::Connect instead ClientState::Registered => client.error_state(RequestStateError::Already), @@ -265,8 +277,8 @@ impl Server { }, ClientState::Character => disconnect = true, // Use ClientMsg::Character instead }, - ClientMsg::Connect { player } => match client.client_state { - ClientState::Connected => Self::initialize_client(state, entity, client, player), + ClientMsg::Register { player } => match client.client_state { + ClientState::Connected => Self::initialize_player(state, entity, client, player), _ => disconnect = true, }, ClientMsg::Character(character) => match client.client_state { @@ -370,7 +382,7 @@ impl Server { } /// Initialize a new client states with important information - fn initialize_client( + fn initialize_player( state: &mut State, entity: specs::Entity, client: &mut Client, @@ -379,17 +391,6 @@ impl Server { // Save player metadata (for example the username) state.write_component(entity, player); - // Return the state of the current world - // (All components Sphynx tracks) - client.notify(ServerMsg::InitialSync { - ecs_state: state.ecs().gen_state_package(), - player_entity_uid: state - .ecs() - .uid_from_entity(entity) - .unwrap() - .into(), - }); - // Sync logical information other players have authority over, not the server for (other_entity, &uid, &animation_history) in ( &state.ecs().entities(), diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index 5094d6977d..10bbc87c70 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -44,8 +44,9 @@ impl ClientInit { let mut last_err = None; for socket_addr in first_addrs.into_iter().chain(second_addrs) { - match Client::new(socket_addr, player.clone(), view_distance) { - Ok(client) => { + match Client::new(socket_addr, view_distance) { + Ok(mut client) => { + client.register(player); let _ = tx.send(Ok(client)); return; }