Change client_state_pending to ClientState::Pending

Former-commit-id: 4b6a304a138ccfbffc3b6e28d36feaa2693d01a7
This commit is contained in:
timokoesters 2019-05-24 21:10:18 +02:00
parent 1dbca8b994
commit 3697c47e33
5 changed files with 24 additions and 26 deletions

View File

@ -30,8 +30,7 @@ pub enum Event {
} }
pub struct Client { pub struct Client {
client_state: Option<ClientState>, client_state: ClientState,
pending_state_request: bool,
thread_pool: ThreadPool, thread_pool: ThreadPool,
last_ping: f64, last_ping: f64,
@ -52,7 +51,7 @@ impl Client {
/// Create a new `Client`. /// Create a new `Client`.
#[allow(dead_code)] #[allow(dead_code)]
pub fn new<A: Into<SocketAddr>>(addr: A, view_distance: Option<u32>) -> Result<Self, Error> { pub fn new<A: Into<SocketAddr>>(addr: A, view_distance: Option<u32>) -> Result<Self, Error> {
let mut client_state = Some(ClientState::Connected); let mut client_state = ClientState::Connected;
let mut postbox = PostBox::to(addr)?; let mut postbox = PostBox::to(addr)?;
// Wait for initial sync // Wait for initial sync
@ -76,7 +75,6 @@ impl Client {
Ok(Self { Ok(Self {
client_state, client_state,
pending_state_request: false,
thread_pool: threadpool::Builder::new() thread_pool: threadpool::Builder::new()
.thread_name("veloren-worker".into()) .thread_name("veloren-worker".into())
.build(), .build(),
@ -98,13 +96,13 @@ impl Client {
pub fn register(&mut self, player: comp::Player) { pub fn register(&mut self, player: comp::Player) {
self.postbox.send_message(ClientMsg::Register { player }); self.postbox.send_message(ClientMsg::Register { player });
self.pending_state_request = true; self.client_state = ClientState::Pending;
} }
pub fn request_character(&mut self, name: String, body: comp::Body) { pub fn request_character(&mut self, name: String, body: comp::Body) {
self.postbox self.postbox
.send_message(ClientMsg::Character { name, body }); .send_message(ClientMsg::Character { name, body });
self.pending_state_request = true; self.client_state = ClientState::Pending;
} }
pub fn set_view_distance(&mut self, view_distance: u32) { pub fn set_view_distance(&mut self, view_distance: u32) {
@ -148,15 +146,10 @@ impl Client {
/// Get the client state /// Get the client state
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_client_state(&self) -> Option<ClientState> { pub fn get_client_state(&self) -> ClientState {
self.client_state self.client_state
} }
/// Get the pending state request bool
pub fn is_request_pending(&self) -> bool {
self.pending_state_request
}
/// Get the current tick number. /// Get the current tick number.
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_tick(&self) -> u64 { pub fn get_tick(&self) -> u64 {
@ -360,18 +353,15 @@ impl Client {
self.pending_chunks.remove(&key); self.pending_chunks.remove(&key);
} }
ServerMsg::StateAnswer(Ok(state)) => { ServerMsg::StateAnswer(Ok(state)) => {
self.client_state = Some(state); self.client_state = state;
self.pending_state_request = false;
} }
ServerMsg::StateAnswer(Err((error, state))) => { ServerMsg::StateAnswer(Err((error, state))) => {
self.client_state = Some(state); self.client_state = state;
self.pending_state_request = false;
} }
ServerMsg::ForceState(state) => { ServerMsg::ForceState(state) => {
self.client_state = Some(state); self.client_state = state;
} }
ServerMsg::Disconnect => { ServerMsg::Disconnect => {
self.client_state = None;
frontend_events.push(Event::Disconnect); frontend_events.push(Event::Disconnect);
} }
} }

View File

@ -9,6 +9,7 @@ pub use self::server::{RequestStateError, ServerMsg};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ClientState { pub enum ClientState {
Pending,
Connected, Connected,
Registered, Registered,
Spectator, Spectator,

View File

@ -384,6 +384,7 @@ impl Server {
ClientState::Spectator ClientState::Spectator
| ClientState::Character | ClientState::Character
| ClientState::Dead => client.allow_state(ClientState::Registered), | ClientState::Dead => client.allow_state(ClientState::Registered),
ClientState::Pending => {}
}, },
ClientState::Spectator => match requested_state { ClientState::Spectator => match requested_state {
// Become Registered first. // Become Registered first.
@ -396,12 +397,14 @@ impl Server {
ClientState::Registered ClientState::Registered
| ClientState::Character | ClientState::Character
| ClientState::Dead => client.allow_state(ClientState::Spectator), | ClientState::Dead => client.allow_state(ClientState::Spectator),
ClientState::Pending => {}
}, },
// Use ClientMsg::Character instead. // Use ClientMsg::Character instead.
ClientState::Character => { ClientState::Character => {
client.error_state(RequestStateError::WrongMessage) client.error_state(RequestStateError::WrongMessage)
} }
ClientState::Dead => client.error_state(RequestStateError::Impossible), ClientState::Dead => client.error_state(RequestStateError::Impossible),
ClientState::Pending => {}
}, },
ClientMsg::Register { player } => match client.client_state { ClientMsg::Register { player } => match client.client_state {
ClientState::Connected => { ClientState::Connected => {
@ -433,6 +436,7 @@ impl Server {
ClientState::Character => { ClientState::Character => {
client.error_state(RequestStateError::Already) client.error_state(RequestStateError::Already)
} }
ClientState::Pending => {}
}, },
ClientMsg::Chat(msg) => match client.client_state { ClientMsg::Chat(msg) => match client.client_state {
ClientState::Connected => { ClientState::Connected => {
@ -442,6 +446,7 @@ impl Server {
| ClientState::Spectator | ClientState::Spectator
| ClientState::Dead | ClientState::Dead
| ClientState::Character => new_chat_msgs.push((entity, msg)), | ClientState::Character => new_chat_msgs.push((entity, msg)),
ClientState::Pending => {}
}, },
ClientMsg::PlayerInputs(mut inputs) => match client.client_state { ClientMsg::PlayerInputs(mut inputs) => match client.client_state {
ClientState::Character | ClientState::Dead => { ClientState::Character | ClientState::Dead => {
@ -490,6 +495,7 @@ impl Server {
None => requested_chunks.push(key), None => requested_chunks.push(key),
} }
} }
ClientState::Pending => {}
}, },
// Always possible. // Always possible.
ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong), ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong),

View File

@ -47,9 +47,8 @@ impl PlayState for CharSelectionState {
let mut clock = Clock::new(); let mut clock = Clock::new();
self.client.borrow_mut().reset_terrain(); self.client.borrow_mut().reset_terrain();
while self.client.borrow().is_request_pending() let mut current_client_state = self.client.borrow().get_client_state();
|| self.client.borrow().get_client_state() == Some(ClientState::Registered) while let ClientState::Pending | ClientState::Registered = current_client_state {
{
// Handle window events. // Handle window events.
for event in global_state.window.fetch_events() { for event in global_state.window.fetch_events() {
match event { match event {
@ -124,6 +123,8 @@ impl PlayState for CharSelectionState {
// Wait for the next tick. // Wait for the next tick.
clock.tick(Duration::from_millis(1000 / FPS)); clock.tick(Duration::from_millis(1000 / FPS));
current_client_state = self.client.borrow().get_client_state();
} }
PlayStateResult::Pop PlayStateResult::Pop

View File

@ -129,11 +129,9 @@ impl PlayState for SessionState {
*/ */
// Game loop // Game loop
while self.client.borrow().is_request_pending() let mut current_client_state = self.client.borrow().get_client_state();
|| self.client.borrow().get_client_state() == Some(ClientState::Character) while let ClientState::Pending | ClientState::Character | ClientState::Dead = current_client_state {
|| self.client.borrow().get_client_state() == Some(ClientState::Dead) let alive = self.client.borrow().get_client_state() == ClientState::Character;
{
let alive = self.client.borrow().get_client_state() == Some(ClientState::Character);
// Handle window events. // Handle window events.
for event in global_state.window.fetch_events() { for event in global_state.window.fetch_events() {
@ -248,6 +246,8 @@ impl PlayState for SessionState {
// Clean things up after the tick. // Clean things up after the tick.
self.cleanup(); self.cleanup();
current_client_state = self.client.borrow().get_client_state();
} }
PlayStateResult::Pop PlayStateResult::Pop