From aabf9d7b21965c318a622569f35adf52db8c54ab Mon Sep 17 00:00:00 2001 From: CapsizeGlimmer <> Date: Wed, 10 Jun 2020 00:21:56 -0400 Subject: [PATCH] fix chat-cli name formatting. --- chat-cli/src/main.rs | 3 +- client/src/lib.rs | 44 ++++++++++++++++ common/src/comp/agent.rs | 46 +---------------- server-cli/src/main.rs | 2 + voxygen/src/hud/chat.rs | 109 +++++++-------------------------------- 5 files changed, 67 insertions(+), 137 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index bf2908ae9d..7e80e3099f 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -84,8 +84,7 @@ fn main() { for event in events { match event { - // TODO client is now responsible for formatting the `[{player_name}] {}` - Event::Chat(m) => println!("{}", m.message), + Event::Chat(m) => println!("{}", client.format_message(&m)), Event::Disconnect => {}, // TODO Event::DisconnectionNotification(time) => { let message = match time { diff --git a/client/src/lib.rs b/client/src/lib.rs index 5f0145634e..d85bc50576 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1007,6 +1007,50 @@ impl Client { self.entity = entity_builder.with(uid).build(); } + + /// Format a message for the client (voxygen chat box or chat-cli) + pub fn format_message(&self, comp::ChatMsg { chat_type, message }: &comp::ChatMsg) -> String { + let alias_of_uid = |uid| { + self.player_list + .get(uid) + .map_or("".to_string(), |player_info| { + if player_info.is_admin { + format!("ADMIN - {}", player_info.player_alias) + } else { + player_info.player_alias.to_string() + } + }) + }; + let message_format = |uid, message, group| { + if let Some(group) = group { + format!("{{{}}} [{}]: {}", group, alias_of_uid(uid), message) + } else { + format!("[{}]: {}", alias_of_uid(uid), message) + } + }; + match chat_type { + comp::ChatType::Private => message.to_string(), + comp::ChatType::Broadcast => message.to_string(), + comp::ChatType::Kill => message.to_string(), + comp::ChatType::Tell(from, to) => { + let from_alias = alias_of_uid(from); + let to_alias = alias_of_uid(to); + if Some(from) == self.state.ecs().read_storage::().get(self.entity) { + format!("To [{}]: {}", to_alias, message) + } else { + format!("From [{}]: {}", from_alias, message) + } + }, + comp::ChatType::Say(uid) => message_format(uid, message, None), + comp::ChatType::Group(uid, s) => message_format(uid, message, Some(s)), + comp::ChatType::Faction(uid, s) => message_format(uid, message, Some(s)), + comp::ChatType::Region(uid) => message_format(uid, message, None), + comp::ChatType::World(uid) => message_format(uid, message, None), + // NPCs can't talk. Should be filtered by hud/mod.rs for voxygen and should be filtered + // by server for chat-cli + comp::ChatType::Npc(_uid, _r) => "".to_string(), + } + } } impl Drop for Client { diff --git a/common/src/comp/agent.rs b/common/src/comp/agent.rs index 07c7b4b5e8..49c183c34c 100644 --- a/common/src/comp/agent.rs +++ b/common/src/comp/agent.rs @@ -1,4 +1,4 @@ -use crate::{path::Chaser, state::Time}; +use crate::path::Chaser; use specs::{Component, Entity as EcsEntity}; use specs_idvs::IDVStorage; use vek::*; @@ -107,47 +107,3 @@ impl Activity { impl Default for Activity { fn default() -> Self { Activity::Idle(Vec2::zero()) } } - -/// Default duration in seconds of speech bubbles -pub const SPEECH_BUBBLE_DURATION: f64 = 5.0; - -/// The contents of a speech bubble -#[derive(Clone, Debug, Serialize, Deserialize)] -pub enum SpeechBubbleMessage { - /// This message was said by a player and needs no translation - Plain(String), - /// This message was said by an NPC. The fields are a i18n key and a random - /// u16 index - Localized(String, u16), -} - -/// Adds a speech bubble to the entity -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct SpeechBubble { - pub message: SpeechBubbleMessage, - pub timeout: Option