From 95b5f4acfb2a8af1b2f381fee0de3b4963bb642a Mon Sep 17 00:00:00 2001 From: tommy Date: Sun, 21 Jul 2019 14:34:52 -0400 Subject: [PATCH] Added private, broadcast, and game_state messages --- chat-cli/src/main.rs | 5 +-- client/src/lib.rs | 13 ++------ common/src/lib.rs | 3 ++ common/src/msg/client.rs | 32 +++++++++++++++---- common/src/msg/server.rs | 32 +++++++++++++++---- server/src/cmd.rs | 69 +++++++++++++++++++++------------------- server/src/lib.rs | 56 +++++++++++++++++++------------- voxygen/src/hud/chat.rs | 9 ++++-- voxygen/src/hud/mod.rs | 3 ++ 9 files changed, 138 insertions(+), 84 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index dc0569b82a..e6920e8dee 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -68,10 +68,7 @@ fn main() { for event in events { match event { - Event::Chat { - chat_type: _, - message, - } => println!("{}", message), + Event::Chat { message, .. } => println!("{}", message), Event::Disconnect => {} // TODO } } diff --git a/client/src/lib.rs b/client/src/lib.rs index 1be02e2527..57fab1038e 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -389,16 +389,9 @@ impl Client { .duration_since(self.last_server_ping) .as_secs_f64() } - ServerMsg::ChatMsg { chat_type, msg } => match chat_type { - ChatType::Chat => frontend_events.push(Event::Chat { - chat_type: ChatType::Chat, - message: msg, - }), - ChatType::Tell => frontend_events.push(Event::Chat { - chat_type: ChatType::Tell, - message: msg, - }), - }, + ServerMsg::ChatMsg { chat_type, message } => { + frontend_events.push(Event::Chat { chat_type, message }) + } ServerMsg::SetPlayerEntity(uid) => { self.entity = self.state.ecs().entity_from_uid(uid).unwrap() } // TODO: Don't unwrap here! diff --git a/common/src/lib.rs b/common/src/lib.rs index 67330defb8..adb734a5b7 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -53,6 +53,9 @@ pub mod net; #[derive(Debug, Clone, Serialize, Deserialize)] pub enum ChatType { + Broadcast, Chat, + GameUpdate, + Private, Tell, } diff --git a/common/src/msg/client.rs b/common/src/msg/client.rs index c5ab98c029..08cddaa2f3 100644 --- a/common/src/msg/client.rs +++ b/common/src/msg/client.rs @@ -21,7 +21,7 @@ pub enum ClientMsg { Pong, ChatMsg { chat_type: ChatType, - msg: String, + message: String, }, PlayerPhysics { pos: comp::Pos, @@ -35,16 +35,34 @@ pub enum ClientMsg { } impl ClientMsg { - pub fn chat(message: String) -> crate::msg::client::ClientMsg { - crate::msg::client::ClientMsg::ChatMsg { + pub fn chat(message: String) -> ClientMsg { + ClientMsg::ChatMsg { chat_type: ChatType::Chat, - msg: message, + message, } } - pub fn tell(message: String) -> crate::msg::client::ClientMsg { - crate::msg::client::ClientMsg::ChatMsg { + pub fn tell(message: String) -> ClientMsg { + ClientMsg::ChatMsg { chat_type: ChatType::Tell, - msg: message, + message, + } + } + pub fn game(message: String) -> ClientMsg { + ClientMsg::ChatMsg { + chat_type: ChatType::GameUpdate, + message, + } + } + pub fn broadcast(message: String) -> ClientMsg { + ClientMsg::ChatMsg { + chat_type: ChatType::Broadcast, + message, + } + } + pub fn private(message: String) -> ClientMsg { + ClientMsg::ChatMsg { + chat_type: ChatType::Private, + message, } } } diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index 2b3ab19a0e..5cfe7f178d 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -35,7 +35,7 @@ pub enum ServerMsg { Pong, ChatMsg { chat_type: ChatType, - msg: String, + message: String, }, SetPlayerEntity(u64), EcsSync(sphynx::SyncPackage), @@ -63,16 +63,34 @@ pub enum ServerError { } impl ServerMsg { - pub fn chat(message: String) -> crate::msg::server::ServerMsg { - crate::msg::server::ServerMsg::ChatMsg { + pub fn chat(message: String) -> ServerMsg { + ServerMsg::ChatMsg { chat_type: ChatType::Chat, - msg: message, + message, } } - pub fn tell(message: String) -> crate::msg::server::ServerMsg { - crate::msg::server::ServerMsg::ChatMsg { + pub fn tell(message: String) -> ServerMsg { + ServerMsg::ChatMsg { chat_type: ChatType::Tell, - msg: message, + message, + } + } + pub fn game(message: String) -> ServerMsg { + ServerMsg::ChatMsg { + chat_type: ChatType::GameUpdate, + message, + } + } + pub fn broadcast(message: String) -> ServerMsg { + ServerMsg::ChatMsg { + chat_type: ChatType::Broadcast, + message, + } + } + pub fn private(message: String) -> ServerMsg { + ServerMsg::ChatMsg { + chat_type: ChatType::Private, + message, } } } diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 706891158e..569dade614 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -166,13 +166,13 @@ fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &Ch } None => server.clients.notify( entity, - ServerMsg::chat(String::from("You have no position!")), + ServerMsg::private(String::from("You have no position!")), ), } } _ => server .clients - .notify(entity, ServerMsg::chat(String::from(action.help_string))), + .notify(entity, ServerMsg::private(String::from(action.help_string))), } } @@ -188,12 +188,12 @@ fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &Ch } _ => server .clients - .notify(entity, ServerMsg::chat(String::from(action.help_string))), + .notify(entity, ServerMsg::private(String::from(action.help_string))), }, None => { server.clients.notify( entity, - ServerMsg::chat(String::from("You don't have any position!")), + ServerMsg::private(String::from("You don't have any position!")), ); } } @@ -219,16 +219,17 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch Some(n) => match n.parse() { Ok(n) => n, Err(_) => { - server - .clients - .notify(entity, ServerMsg::chat(format!("'{}' is not a time!", n))); + server.clients.notify( + entity, + ServerMsg::private(format!("'{}' is not a time!", n)), + ); return; } }, None => { server.clients.notify( entity, - ServerMsg::chat("You must specify a time!".to_string()), + ServerMsg::private("You must specify a time!".to_string()), ); return; } @@ -249,13 +250,13 @@ fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: & None => { server.clients.notify( entity, - ServerMsg::chat(String::from("You must specify health amount!")), + ServerMsg::private(String::from("You must specify health amount!")), ); } }, None => server.clients.notify( entity, - ServerMsg::chat(String::from("You have no position.")), + ServerMsg::private(String::from("You have no position.")), ), } } @@ -273,7 +274,7 @@ fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &C } None => server .clients - .notify(entity, ServerMsg::chat(String::from(action.help_string))), + .notify(entity, ServerMsg::private(String::from(action.help_string))), } } @@ -295,29 +296,32 @@ fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &Chat } None => server.clients.notify( entity, - ServerMsg::chat(format!("Unable to teleport to player '{}'!", alias)), + ServerMsg::private(format!( + "Unable to teleport to player '{}'!", + alias + )), ), }, None => { server.clients.notify( entity, - ServerMsg::chat(format!("Player '{}' not found!", alias)), + ServerMsg::private(format!("Player '{}' not found!", alias)), ); server .clients - .notify(entity, ServerMsg::chat(String::from(action.help_string))); + .notify(entity, ServerMsg::private(String::from(action.help_string))); } }, None => { server .clients - .notify(entity, ServerMsg::chat(format!("You have no position!"))); + .notify(entity, ServerMsg::private(format!("You have no position!"))); } } } None => server .clients - .notify(entity, ServerMsg::chat(String::from(action.help_string))), + .notify(entity, ServerMsg::private(String::from(action.help_string))), } } @@ -351,17 +355,18 @@ fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &C } server.clients.notify( entity, - ServerMsg::chat(format!("Spawned {} entities", amount).to_owned()), + ServerMsg::private(format!("Spawned {} entities", amount).to_owned()), ); } - None => server - .clients - .notify(entity, ServerMsg::chat("You have no position!".to_owned())), + None => server.clients.notify( + entity, + ServerMsg::private("You have no position!".to_owned()), + ), } } _ => server .clients - .notify(entity, ServerMsg::chat(String::from(action.help_string))), + .notify(entity, ServerMsg::private(String::from(action.help_string))), } } @@ -381,11 +386,11 @@ fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action server .clients - .notify(entity, ServerMsg::chat(header_message + &player_list)); + .notify(entity, ServerMsg::private(header_message + &player_list)); } else { server .clients - .notify(entity, ServerMsg::chat(header_message)); + .notify(entity, ServerMsg::private(header_message)); } } @@ -403,7 +408,7 @@ fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: .remove(entity); server.clients.notify( entity, - ServerMsg::chat(String::from("Toggled off build mode!")), + ServerMsg::private(String::from("Toggled off build mode!")), ); } else { let _ = server @@ -413,7 +418,7 @@ fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: .insert(entity, comp::CanBuild); server.clients.notify( entity, - ServerMsg::chat(String::from("Toggled on build mode!")), + ServerMsg::private(String::from("Toggled on build mode!")), ); } } @@ -422,7 +427,7 @@ fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: & for cmd in CHAT_COMMANDS.iter() { server .clients - .notify(entity, ServerMsg::chat(String::from(cmd.help_string))); + .notify(entity, ServerMsg::private(String::from(cmd.help_string))); } } @@ -460,7 +465,7 @@ fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _actio } else { "No NPCs on server.".to_string() }; - server.clients.notify(entity, ServerMsg::chat(text)); + server.clients.notify(entity, ServerMsg::private(text)); } fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action: &ChatCommand) { @@ -633,14 +638,14 @@ fn handle_tell(server: &mut Server, entity: EcsEntity, args: String, action: &Ch None => { server.clients.notify( entity, - ServerMsg::chat(String::from("You do not exist!")), + ServerMsg::private(String::from("You do not exist!")), ); } } } else { server.clients.notify( entity, - ServerMsg::chat(format!( + ServerMsg::private(format!( "You really should say something to {}!", alias )), @@ -649,19 +654,19 @@ fn handle_tell(server: &mut Server, entity: EcsEntity, args: String, action: &Ch } else { server .clients - .notify(entity, ServerMsg::chat(format!("Don't be crazy!"))); + .notify(entity, ServerMsg::private(format!("Don't be crazy!"))); } } None => { server.clients.notify( entity, - ServerMsg::chat(format!("Player '{}' not found!", alias)), + ServerMsg::private(format!("Player '{}' not found!", alias)), ); } } } None => server .clients - .notify(entity, ServerMsg::chat(String::from(action.help_string))), + .notify(entity, ServerMsg::private(String::from(action.help_string))), } } diff --git a/server/src/lib.rs b/server/src/lib.rs index 65473961d1..79c6736f82 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -507,7 +507,10 @@ impl Server { if let Some(player) = state.ecs().read_storage::().get(entity) { - new_chat_msgs.push((None, format!("{} joined", &player.alias))); + new_chat_msgs.push(( + None, + ServerMsg::broadcast(format!("{} joined", &player.alias)), + )); } } ClientState::Character => { @@ -526,17 +529,15 @@ impl Server { } ClientState::Pending => {} }, - ClientMsg::ChatMsg { - chat_type: _, - msg: message, - } => match client.client_state { + ClientMsg::ChatMsg { chat_type, message } => match client.client_state { ClientState::Connected => { client.error_state(RequestStateError::Impossible) } ClientState::Registered | ClientState::Spectator | ClientState::Dead - | ClientState::Character => new_chat_msgs.push((Some(entity), message)), + | ClientState::Character => new_chat_msgs + .push((Some(entity), ServerMsg::ChatMsg { chat_type, message })), ClientState::Pending => {} }, ClientMsg::PlayerPhysics { pos, vel, ori } => match client.client_state { @@ -607,7 +608,10 @@ impl Server { if disconnect { if let Some(player) = state.ecs().read_storage::().get(entity) { - new_chat_msgs.push((None, format!("{} disconnected", &player.alias))); + new_chat_msgs.push(( + None, + ServerMsg::broadcast(format!("{} disconnected", &player.alias)), + )); } disconnected_clients.push(entity); client.postbox.send_message(ServerMsg::Disconnect); @@ -619,23 +623,31 @@ impl Server { // Handle new chat messages. for (entity, msg) in new_chat_msgs { - if let Some(entity) = entity { - // Handle chat commands. - if msg.starts_with("/") && msg.len() > 1 { - let argv = String::from(&msg[1..]); - self.process_chat_cmd(entity, argv); - } else { - self.clients.notify_registered(ServerMsg::chat( - match self.state.ecs().read_storage::().get(entity) { - Some(player) => format!("[{}] {}", &player.alias, msg), - None => format!("[] {}", msg), - }, - )); + match msg { + ServerMsg::ChatMsg { chat_type, message } => { + if let Some(entity) = entity { + // Handle chat commands. + if message.starts_with("/") && message.len() > 1 { + let argv = String::from(&message[1..]); + self.process_chat_cmd(entity, argv); + } else { + let message = + match self.state.ecs().read_storage::().get(entity) { + Some(player) => format!("[{}] {}", &player.alias, message), + None => format!("[] {}", message), + }; + self.clients + .notify_registered(ServerMsg::ChatMsg { chat_type, message }); + } + } else { + self.clients + .notify_registered(ServerMsg::ChatMsg { chat_type, message }); + } + } + _ => { + panic!("Invalid message type."); } - } else { - self.clients.notify_registered(ServerMsg::chat(msg.clone())); } - frontend_events.push(Event::Chat { entity, msg }); } // Handle client disconnects. diff --git a/voxygen/src/hud/chat.rs b/voxygen/src/hud/chat.rs index f11a58a2a4..76568a636c 100644 --- a/voxygen/src/hud/chat.rs +++ b/voxygen/src/hud/chat.rs @@ -1,4 +1,6 @@ -use super::{img_ids::Imgs, Fonts, TELL_COLOR, TEXT_COLOR}; +use super::{ + img_ids::Imgs, Fonts, BROADCAST_COLOR, GAME_UPDATE_COLOR, PRIVATE_COLOR, TELL_COLOR, TEXT_COLOR, +}; use client::Event as ClientEvent; use common::ChatType; use conrod_core::{ @@ -190,7 +192,10 @@ impl<'a> Widget for Chat<'a> { ClientEvent::Chat { chat_type, message } => { let color = match chat_type { ChatType::Tell => TELL_COLOR, - _ => TEXT_COLOR, + ChatType::Chat => TEXT_COLOR, + ChatType::Private => PRIVATE_COLOR, + ChatType::Broadcast => BROADCAST_COLOR, + ChatType::GameUpdate => GAME_UPDATE_COLOR, }; let text = Text::new(&message) .font_size(15) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 3cbb4fa615..eb03cf8aae 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -51,6 +51,9 @@ const TEXT_COLOR_2: Color = Color::Rgba(0.0, 0.0, 0.0, 1.0); const HP_COLOR: Color = Color::Rgba(0.33, 0.63, 0.0, 1.0); const MANA_COLOR: Color = Color::Rgba(0.42, 0.41, 0.66, 1.0); const TELL_COLOR: Color = Color::Rgba(1.0, 1.0, 0.0, 1.0); +const PRIVATE_COLOR: Color = Color::Rgba(1.0, 1.0, 0.0, 1.0); +const BROADCAST_COLOR: Color = Color::Rgba(0.0, 1.0, 0.0, 1.0); +const GAME_UPDATE_COLOR: Color = Color::Rgba(1.0, 1.0, 0.0, 1.0); widget_ids! { struct Ids {