From c23183b1ca5153d5a852208b75939fd3f1225de1 Mon Sep 17 00:00:00 2001 From: scott-c Date: Wed, 20 May 2020 19:59:44 +0800 Subject: [PATCH] Add character name and level to social window --- client/src/cmd.rs | 1 + client/src/lib.rs | 16 +++++++++------- common/src/msg/mod.rs | 3 ++- common/src/msg/server.rs | 11 +++++++++-- server/src/sys/message.rs | 28 +++++++++++++++++++--------- voxygen/src/hud/social.rs | 14 ++++---------- 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/client/src/cmd.rs b/client/src/cmd.rs index f24152fd92..2fc1e68db7 100644 --- a/client/src/cmd.rs +++ b/client/src/cmd.rs @@ -40,6 +40,7 @@ fn complete_player(part: &str, client: &Client) -> Vec { client .player_list .values() + .map(|player_info| &player_info.player_alias) .filter(|alias| alias.starts_with(part)) .cloned() .collect() diff --git a/client/src/lib.rs b/client/src/lib.rs index c8d21a84c8..bc646abd40 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -23,7 +23,7 @@ use common::{ event::{EventBus, SfxEvent, SfxEventItem}, msg::{ validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, Notification, - PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, ServerMsg, + PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, ServerMsg, MAX_BYTES_CHAT_MSG, }, net::PostBox, @@ -68,7 +68,7 @@ pub struct Client { thread_pool: ThreadPool, pub server_info: ServerInfo, pub world_map: (Arc, Vec2), - pub player_list: HashMap, + pub player_list: HashMap, pub character_list: CharacterList, postbox: PostBox, @@ -734,13 +734,15 @@ impl Client { ServerMsg::PlayerListUpdate(PlayerListUpdate::Init(list)) => { self.player_list = list }, - ServerMsg::PlayerListUpdate(PlayerListUpdate::Add(uid, name)) => { - if let Some(old_name) = self.player_list.insert(uid, name.clone()) { + ServerMsg::PlayerListUpdate(PlayerListUpdate::Add(uid, player_info)) => { + if let Some(old_player_info) = + self.player_list.insert(uid, player_info.clone()) + { warn!( "Received msg to insert {} with uid {} into the player list but \ there was already an entry for {} with the same uid that was \ overwritten!", - name, uid, old_name + player_info.player_alias, uid, old_player_info.player_alias ); } }, @@ -754,8 +756,8 @@ impl Client { } }, ServerMsg::PlayerListUpdate(PlayerListUpdate::Alias(uid, new_name)) => { - if let Some(name) = self.player_list.get_mut(&uid) { - *name = new_name; + if let Some(player_info) = self.player_list.get_mut(&uid) { + player_info.player_alias = new_name; } else { warn!( "Received msg to alias player with uid {} to {} but this uid is \ diff --git a/common/src/msg/mod.rs b/common/src/msg/mod.rs index c045383dd0..3e5c6d28ea 100644 --- a/common/src/msg/mod.rs +++ b/common/src/msg/mod.rs @@ -7,7 +7,8 @@ pub use self::{ client::ClientMsg, ecs_packet::EcsCompPacket, server::{ - Notification, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, ServerMsg, + Notification, PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, + ServerMsg, }, }; diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index ae0a3a4aa8..58e536024f 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -20,12 +20,19 @@ pub struct ServerInfo { #[derive(Debug, Clone, Serialize, Deserialize)] pub enum PlayerListUpdate { - Init(HashMap), - Add(u64, String), + Init(HashMap), + Add(u64, PlayerInfo), Remove(u64), Alias(u64, String), } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PlayerInfo { + pub player_alias: String, + pub character_name: String, + pub character_level: u32, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Notification { WaypointSaved, diff --git a/server/src/sys/message.rs b/server/src/sys/message.rs index 0c32efadb4..1eedd4fda3 100644 --- a/server/src/sys/message.rs +++ b/server/src/sys/message.rs @@ -10,8 +10,8 @@ use common::{ }, event::{EventBus, ServerEvent}, msg::{ - validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, PlayerListUpdate, - RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG, + validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, PlayerInfo, + PlayerListUpdate, RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG, }, state::{BlockChange, Time}, sync::Uid, @@ -83,9 +83,15 @@ impl<'a> System<'a> for Sys { let mut new_chat_msgs = Vec::new(); // Player list to send new players. - let player_list = (&uids, &players) + let player_list = (&uids, &players, &stats) .join() - .map(|(uid, player)| ((*uid).into(), player.alias.clone())) + .map(|(uid, player, stats)| { + ((*uid).into(), PlayerInfo { + player_alias: player.alias.clone(), + character_name: stats.name.clone(), + character_level: stats.level.level(), + }) + }) .collect::>(); // List of new players to update player lists of all clients. let mut new_players = Vec::new(); @@ -382,11 +388,15 @@ impl<'a> System<'a> for Sys { // Handle new players. // Tell all clients to add them to the player list. for entity in new_players { - if let (Some(uid), Some(player)) = (uids.get(entity), players.get(entity)) { - let msg = ServerMsg::PlayerListUpdate(PlayerListUpdate::Add( - (*uid).into(), - player.alias.clone(), - )); + if let (Some(uid), Some(player), Some(stats)) = + (uids.get(entity), players.get(entity), stats.get(entity)) + { + let msg = + ServerMsg::PlayerListUpdate(PlayerListUpdate::Add((*uid).into(), PlayerInfo { + player_alias: player.alias.clone(), + character_name: stats.name.clone(), + character_level: stats.level.level(), // TODO: stats.level.amount, + })); for client in (&mut clients).join().filter(|c| c.is_registered()) { client.notify(msg.clone()) } diff --git a/voxygen/src/hud/social.rs b/voxygen/src/hud/social.rs index 6ae106d9e8..cfb52293b6 100644 --- a/voxygen/src/hud/social.rs +++ b/voxygen/src/hud/social.rs @@ -192,18 +192,12 @@ impl<'a> Widget for Social<'a> { .font_id(self.fonts.cyri.conrod_id) .color(TEXT_COLOR) .set(ids.online_title, ui); - for (i, (_, player_alias)) in self.client.player_list.iter().enumerate() { + for (i, (_, player_info)) in self.client.player_list.iter().enumerate() { Text::new(&format!( "[{}] {} Lvl {}", - player_alias, - match self.client.character_list.characters.get(i) { - Some(s) => &s.character.alias, - None => "", - }, - match self.client.character_list.characters.get(i) { - Some(s) => s.level.to_string(), - None => "".to_string(), - }, + player_info.player_alias, + player_info.character_name, + player_info.character_level )) .down(3.0) .font_size(self.fonts.cyri.scale(15))