Add ping indicator to debug HUD

Former-commit-id: 7bd4a30e9b2dc97134e91f71c08cbb05ca109db5
This commit is contained in:
Algorhythm 2019-05-23 08:18:25 +00:00 committed by Joshua Barretto
parent 54c78479c3
commit 6456198b38
3 changed files with 45 additions and 8 deletions

View File

@ -1,4 +1,4 @@
#![feature(label_break_value)] #![feature(label_break_value, duration_float)]
pub mod error; pub mod error;
pub mod input; pub mod input;
@ -16,14 +16,13 @@ use common::{
msg::{ClientMsg, ClientState, ServerMsg}, msg::{ClientMsg, ClientState, ServerMsg},
net::PostBox, net::PostBox,
state::State, state::State,
terrain::TerrainChunk,
}; };
use specs::Builder;
use std::{ use std::{
collections::HashMap, collections::HashMap,
net::SocketAddr, net::SocketAddr,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use threadpool::ThreadPool; use threadpool::ThreadPool;
use vek::*; use vek::*;
@ -41,6 +40,9 @@ pub struct Client {
last_ping: f64, last_ping: f64,
pub postbox: PostBox<ClientMsg, ServerMsg>, pub postbox: PostBox<ClientMsg, ServerMsg>,
last_server_ping: Instant,
last_ping_delta: f64,
tick: u64, tick: u64,
state: State, state: State,
entity: EcsEntity, entity: EcsEntity,
@ -81,6 +83,9 @@ impl Client {
last_ping: state.get_time(), last_ping: state.get_time(),
postbox, postbox,
last_server_ping: Instant::now(),
last_ping_delta: 0.0,
tick: 0, tick: 0,
state, state,
entity, entity,
@ -138,6 +143,11 @@ impl Client {
self.postbox.send_message(ClientMsg::Chat(msg)) self.postbox.send_message(ClientMsg::Chat(msg))
} }
#[allow(dead_code)]
pub fn get_ping_ms(&self) -> f64 {
self.last_ping_delta * 1000.0
}
/// Execute a single client tick, handle input and update the game state by the given duration. /// Execute a single client tick, handle input and update the game state by the given duration.
#[allow(dead_code)] #[allow(dead_code)]
pub fn tick(&mut self, input: Input, dt: Duration) -> Result<Vec<Event>, Error> { pub fn tick(&mut self, input: Input, dt: Duration) -> Result<Vec<Event>, Error> {
@ -249,6 +259,12 @@ impl Client {
.retain(|_, created| now.duration_since(*created) < Duration::from_secs(10)); .retain(|_, created| now.duration_since(*created) < Duration::from_secs(10));
} }
// send a ping to the server once every second
if Instant::now().duration_since(self.last_server_ping) > Duration::from_secs(1) {
self.postbox.send_message(ClientMsg::Ping);
self.last_server_ping = Instant::now();
}
// Finish the tick, pass control back to the frontend (step 6). // Finish the tick, pass control back to the frontend (step 6).
self.tick += 1; self.tick += 1;
Ok(frontend_events) Ok(frontend_events)
@ -276,7 +292,11 @@ impl Client {
ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad), ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad),
ServerMsg::Shutdown => return Err(Error::ServerShutdown), ServerMsg::Shutdown => return Err(Error::ServerShutdown),
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()
.duration_since(self.last_server_ping)
.as_secs_f64()
}
ServerMsg::Chat(msg) => frontend_events.push(Event::Chat(msg)), ServerMsg::Chat(msg) => frontend_events.push(Event::Chat(msg)),
ServerMsg::SetPlayerEntity(uid) => { ServerMsg::SetPlayerEntity(uid) => {
self.entity = self.state.ecs().entity_from_uid(uid).unwrap() self.entity = self.state.ecs().entity_from_uid(uid).unwrap()
@ -332,6 +352,7 @@ impl Client {
} else if self.state.get_time() - self.last_ping > SERVER_TIMEOUT * 0.5 { } else if self.state.get_time() - self.last_ping > SERVER_TIMEOUT * 0.5 {
// Try pinging the server if the timeout is nearing. // Try pinging the server if the timeout is nearing.
self.postbox.send_message(ClientMsg::Ping); self.postbox.send_message(ClientMsg::Ping);
self.last_server_ping = Instant::now();
} }
Ok(frontend_events) Ok(frontend_events)

View File

@ -48,6 +48,7 @@ widget_ids! {
// Debug // Debug
debug_bg, debug_bg,
fps_counter, fps_counter,
ping,
// Game Version // Game Version
version, version,
@ -247,7 +248,7 @@ impl Hud {
} }
} }
fn update_layout(&mut self, tps: f64, global_state: &GlobalState) -> Vec<Event> { fn update_layout(&mut self, tps: f64, ping_ms: f64, global_state: &GlobalState) -> Vec<Event> {
let mut events = Vec::new(); let mut events = Vec::new();
let ref mut ui_widgets = self.ui.set_widgets(); let ref mut ui_widgets = self.ui.set_widgets();
let version = env!("CARGO_PKG_VERSION"); let version = env!("CARGO_PKG_VERSION");
@ -272,6 +273,12 @@ impl Hud {
.font_id(self.fonts.opensans) .font_id(self.fonts.opensans)
.font_size(14) .font_size(14)
.set(self.ids.fps_counter, ui_widgets); .set(self.ids.fps_counter, ui_widgets);
Text::new(&format!("Ping: {:.1}ms", ping_ms))
.color(TEXT_COLOR)
.down_from(self.ids.fps_counter, 5.0)
.font_id(self.fonts.opensans)
.font_size(14)
.set(self.ids.ping, ui_widgets);
} }
// Add Bag-Space Button. // Add Bag-Space Button.
@ -566,11 +573,16 @@ impl Hud {
handled handled
} }
pub fn maintain(&mut self, global_state: &mut GlobalState, tps: f64) -> Vec<Event> { pub fn maintain(
&mut self,
global_state: &mut GlobalState,
tps: f64,
ping_ms: f64,
) -> Vec<Event> {
if let Some(maybe_id) = self.to_focus.take() { if let Some(maybe_id) = self.to_focus.take() {
self.ui.focus_widget(maybe_id); self.ui.focus_widget(maybe_id);
} }
let events = self.update_layout(tps, &global_state); let events = self.update_layout(tps, ping_ms, &global_state);
self.ui.maintain(&mut global_state.window.renderer_mut()); self.ui.maintain(&mut global_state.window.renderer_mut());
events events
} }

View File

@ -177,7 +177,11 @@ impl PlayState for SessionState {
); );
// Maintain the UI. // Maintain the UI.
for event in self.hud.maintain(global_state, clock.get_tps()) { for event in self.hud.maintain(
global_state,
clock.get_tps(),
self.client.borrow().get_ping_ms(),
) {
match event { match event {
HudEvent::SendMessage(msg) => { HudEvent::SendMessage(msg) => {
// TODO: Handle result // TODO: Handle result