Skip to content

Commit

Permalink
Add ping to client props
Browse files Browse the repository at this point in the history
  • Loading branch information
Jupeyy committed Jan 18, 2025
1 parent 39514b1 commit 7dae5b7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions game/editor/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ impl EditorClient {
color: self.color,
cursor_world: cursor_world_pos,
server_id: self.server_id,
stats: None,
},
)));
}
Expand Down
3 changes: 0 additions & 3 deletions game/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2452,9 +2452,6 @@ impl Editor {
color: None,
},
);
self.tabs
.values_mut()
.for_each(|tab| tab.client.update_info(vec2::new(-10000.0, -10000.0)));

Ok(hash)
}
Expand Down
5 changes: 4 additions & 1 deletion game/editor/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use async_trait::async_trait;
use base::hash::Hash;
use math::math::vector::vec2;
use network::network::{
connection::NetworkConnectionId, event::NetworkEvent,
connection::{ConnectionStats, NetworkConnectionId},
event::NetworkEvent,
event_generator::NetworkEventToGameEventGenerator,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -46,6 +47,8 @@ pub struct ClientProps {

/// unique id on the server
pub server_id: u64,

pub stats: Option<ConnectionStats>,
}

/// editor events are a collection of either actions or commands
Expand Down
11 changes: 10 additions & 1 deletion game/editor/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use graphics::{
},
};
use map::map::Map;
use math::math::vector::vec2;
use network::network::{
connection::NetworkConnectionId,
event::NetworkEvent,
Expand Down Expand Up @@ -128,12 +129,14 @@ impl EditorServer {
mapper_name: mapper_name.clone(),
color: *color,

cursor_world: Default::default(),
cursor_world: vec2::new(-10000.0, -10000.0),
server_id: {
let id = self.client_ids;
self.client_ids += 1;
id
},

stats: client.props.stats,
};

if !*is_local_client {
Expand Down Expand Up @@ -356,6 +359,7 @@ impl EditorServer {
EditorEventClientToServer::Info(mut info) => {
// make sure the id stays unique
info.server_id = client.props.server_id;
info.stats = client.props.stats;
client.props = info;

self.broadcast_client_infos();
Expand Down Expand Up @@ -417,6 +421,11 @@ impl EditorServer {

self.broadcast_client_infos();
}
NetworkEvent::NetworkStats(stats) => {
if let Some(client) = self.clients.get_mut(&id) {
client.props.stats = Some(*stats);
}
}
_ => {
// ignore
}
Expand Down
19 changes: 15 additions & 4 deletions game/editor/src/ui/top_tabs/main_frame.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::Button;
use egui::{Button, Grid};
use ui_base::types::UiRenderPipe;

use crate::ui::user_data::UserData;
Expand Down Expand Up @@ -27,9 +27,20 @@ pub fn render(ui: &mut egui::Ui, pipe: &mut UiRenderPipe<UserData>) {
if tab.client.clients.len() > 1 {
btn = btn.on_hover_ui(|ui| {
ui.vertical(|ui| {
for client in tab.client.clients.iter() {
ui.label(&client.mapper_name);
}
Grid::new("overview-mappers-network-tooltip")
.num_columns(2)
.show(ui, |ui| {
for client in tab.client.clients.iter() {
ui.label(&client.mapper_name);
if let Some(stats) = &client.stats {
ui.label(format!(
"Ping: {}ms",
stats.ping.as_millis()
));
}
ui.end_row();
}
});
});
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/network/src/network/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl NetworkConnectionId {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ConnectionStats {
pub ping: Duration,
pub packets_lost: u64,
Expand Down

0 comments on commit 7dae5b7

Please sign in to comment.