Added private, broadcast, and game_state messages

This commit is contained in:
tommy 2019-07-21 14:34:52 -04:00
parent ea0bbe7e47
commit 95b5f4acfb
9 changed files with 138 additions and 84 deletions

View File

@ -68,10 +68,7 @@ fn main() {
for event in events { for event in events {
match event { match event {
Event::Chat { Event::Chat { message, .. } => println!("{}", message),
chat_type: _,
message,
} => println!("{}", message),
Event::Disconnect => {} // TODO Event::Disconnect => {} // TODO
} }
} }

View File

@ -389,16 +389,9 @@ impl Client {
.duration_since(self.last_server_ping) .duration_since(self.last_server_ping)
.as_secs_f64() .as_secs_f64()
} }
ServerMsg::ChatMsg { chat_type, msg } => match chat_type { ServerMsg::ChatMsg { chat_type, message } => {
ChatType::Chat => frontend_events.push(Event::Chat { frontend_events.push(Event::Chat { chat_type, message })
chat_type: ChatType::Chat, }
message: msg,
}),
ChatType::Tell => frontend_events.push(Event::Chat {
chat_type: ChatType::Tell,
message: 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()
} // TODO: Don't unwrap here! } // TODO: Don't unwrap here!

View File

@ -53,6 +53,9 @@ pub mod net;
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChatType { pub enum ChatType {
Broadcast,
Chat, Chat,
GameUpdate,
Private,
Tell, Tell,
} }

View File

@ -21,7 +21,7 @@ pub enum ClientMsg {
Pong, Pong,
ChatMsg { ChatMsg {
chat_type: ChatType, chat_type: ChatType,
msg: String, message: String,
}, },
PlayerPhysics { PlayerPhysics {
pos: comp::Pos, pos: comp::Pos,
@ -35,16 +35,34 @@ pub enum ClientMsg {
} }
impl ClientMsg { impl ClientMsg {
pub fn chat(message: String) -> crate::msg::client::ClientMsg { pub fn chat(message: String) -> ClientMsg {
crate::msg::client::ClientMsg::ChatMsg { ClientMsg::ChatMsg {
chat_type: ChatType::Chat, chat_type: ChatType::Chat,
msg: message, message,
} }
} }
pub fn tell(message: String) -> crate::msg::client::ClientMsg { pub fn tell(message: String) -> ClientMsg {
crate::msg::client::ClientMsg::ChatMsg { ClientMsg::ChatMsg {
chat_type: ChatType::Tell, 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,
} }
} }
} }

View File

@ -35,7 +35,7 @@ pub enum ServerMsg {
Pong, Pong,
ChatMsg { ChatMsg {
chat_type: ChatType, chat_type: ChatType,
msg: String, message: String,
}, },
SetPlayerEntity(u64), SetPlayerEntity(u64),
EcsSync(sphynx::SyncPackage<EcsCompPacket, EcsResPacket>), EcsSync(sphynx::SyncPackage<EcsCompPacket, EcsResPacket>),
@ -63,16 +63,34 @@ pub enum ServerError {
} }
impl ServerMsg { impl ServerMsg {
pub fn chat(message: String) -> crate::msg::server::ServerMsg { pub fn chat(message: String) -> ServerMsg {
crate::msg::server::ServerMsg::ChatMsg { ServerMsg::ChatMsg {
chat_type: ChatType::Chat, chat_type: ChatType::Chat,
msg: message, message,
} }
} }
pub fn tell(message: String) -> crate::msg::server::ServerMsg { pub fn tell(message: String) -> ServerMsg {
crate::msg::server::ServerMsg::ChatMsg { ServerMsg::ChatMsg {
chat_type: ChatType::Tell, 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,
} }
} }
} }

View File

@ -166,13 +166,13 @@ fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &Ch
} }
None => server.clients.notify( None => server.clients.notify(
entity, entity,
ServerMsg::chat(String::from("You have no position!")), ServerMsg::private(String::from("You have no position!")),
), ),
} }
} }
_ => server _ => server
.clients .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 _ => server
.clients .clients
.notify(entity, ServerMsg::chat(String::from(action.help_string))), .notify(entity, ServerMsg::private(String::from(action.help_string))),
}, },
None => { None => {
server.clients.notify( server.clients.notify(
entity, 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() { Some(n) => match n.parse() {
Ok(n) => n, Ok(n) => n,
Err(_) => { Err(_) => {
server server.clients.notify(
.clients entity,
.notify(entity, ServerMsg::chat(format!("'{}' is not a time!", n))); ServerMsg::private(format!("'{}' is not a time!", n)),
);
return; return;
} }
}, },
None => { None => {
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::chat("You must specify a time!".to_string()), ServerMsg::private("You must specify a time!".to_string()),
); );
return; return;
} }
@ -249,13 +250,13 @@ fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &
None => { None => {
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::chat(String::from("You must specify health amount!")), ServerMsg::private(String::from("You must specify health amount!")),
); );
} }
}, },
None => server.clients.notify( None => server.clients.notify(
entity, 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 None => server
.clients .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( None => server.clients.notify(
entity, entity,
ServerMsg::chat(format!("Unable to teleport to player '{}'!", alias)), ServerMsg::private(format!(
"Unable to teleport to player '{}'!",
alias
)),
), ),
}, },
None => { None => {
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::chat(format!("Player '{}' not found!", alias)), ServerMsg::private(format!("Player '{}' not found!", alias)),
); );
server server
.clients .clients
.notify(entity, ServerMsg::chat(String::from(action.help_string))); .notify(entity, ServerMsg::private(String::from(action.help_string)));
} }
}, },
None => { None => {
server server
.clients .clients
.notify(entity, ServerMsg::chat(format!("You have no position!"))); .notify(entity, ServerMsg::private(format!("You have no position!")));
} }
} }
} }
None => server None => server
.clients .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( server.clients.notify(
entity, entity,
ServerMsg::chat(format!("Spawned {} entities", amount).to_owned()), ServerMsg::private(format!("Spawned {} entities", amount).to_owned()),
); );
} }
None => server None => server.clients.notify(
.clients entity,
.notify(entity, ServerMsg::chat("You have no position!".to_owned())), ServerMsg::private("You have no position!".to_owned()),
),
} }
} }
_ => server _ => server
.clients .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 server
.clients .clients
.notify(entity, ServerMsg::chat(header_message + &player_list)); .notify(entity, ServerMsg::private(header_message + &player_list));
} else { } else {
server server
.clients .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); .remove(entity);
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::chat(String::from("Toggled off build mode!")), ServerMsg::private(String::from("Toggled off build mode!")),
); );
} else { } else {
let _ = server let _ = server
@ -413,7 +418,7 @@ fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action:
.insert(entity, comp::CanBuild); .insert(entity, comp::CanBuild);
server.clients.notify( server.clients.notify(
entity, 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() { for cmd in CHAT_COMMANDS.iter() {
server server
.clients .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 { } else {
"No NPCs on server.".to_string() "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) { 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 => { None => {
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::chat(String::from("You do not exist!")), ServerMsg::private(String::from("You do not exist!")),
); );
} }
} }
} else { } else {
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::chat(format!( ServerMsg::private(format!(
"You really should say something to {}!", "You really should say something to {}!",
alias alias
)), )),
@ -649,19 +654,19 @@ fn handle_tell(server: &mut Server, entity: EcsEntity, args: String, action: &Ch
} else { } else {
server server
.clients .clients
.notify(entity, ServerMsg::chat(format!("Don't be crazy!"))); .notify(entity, ServerMsg::private(format!("Don't be crazy!")));
} }
} }
None => { None => {
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::chat(format!("Player '{}' not found!", alias)), ServerMsg::private(format!("Player '{}' not found!", alias)),
); );
} }
} }
} }
None => server None => server
.clients .clients
.notify(entity, ServerMsg::chat(String::from(action.help_string))), .notify(entity, ServerMsg::private(String::from(action.help_string))),
} }
} }

View File

@ -507,7 +507,10 @@ impl Server {
if let Some(player) = if let Some(player) =
state.ecs().read_storage::<comp::Player>().get(entity) state.ecs().read_storage::<comp::Player>().get(entity)
{ {
new_chat_msgs.push((None, format!("{} joined", &player.alias))); new_chat_msgs.push((
None,
ServerMsg::broadcast(format!("{} joined", &player.alias)),
));
} }
} }
ClientState::Character => { ClientState::Character => {
@ -526,17 +529,15 @@ impl Server {
} }
ClientState::Pending => {} ClientState::Pending => {}
}, },
ClientMsg::ChatMsg { ClientMsg::ChatMsg { chat_type, message } => match client.client_state {
chat_type: _,
msg: message,
} => match client.client_state {
ClientState::Connected => { ClientState::Connected => {
client.error_state(RequestStateError::Impossible) client.error_state(RequestStateError::Impossible)
} }
ClientState::Registered ClientState::Registered
| ClientState::Spectator | ClientState::Spectator
| ClientState::Dead | 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 => {} ClientState::Pending => {}
}, },
ClientMsg::PlayerPhysics { pos, vel, ori } => match client.client_state { ClientMsg::PlayerPhysics { pos, vel, ori } => match client.client_state {
@ -607,7 +608,10 @@ impl Server {
if disconnect { if disconnect {
if let Some(player) = state.ecs().read_storage::<comp::Player>().get(entity) { if let Some(player) = state.ecs().read_storage::<comp::Player>().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); disconnected_clients.push(entity);
client.postbox.send_message(ServerMsg::Disconnect); client.postbox.send_message(ServerMsg::Disconnect);
@ -619,23 +623,31 @@ impl Server {
// Handle new chat messages. // Handle new chat messages.
for (entity, msg) in new_chat_msgs { for (entity, msg) in new_chat_msgs {
match msg {
ServerMsg::ChatMsg { chat_type, message } => {
if let Some(entity) = entity { if let Some(entity) = entity {
// Handle chat commands. // Handle chat commands.
if msg.starts_with("/") && msg.len() > 1 { if message.starts_with("/") && message.len() > 1 {
let argv = String::from(&msg[1..]); let argv = String::from(&message[1..]);
self.process_chat_cmd(entity, argv); self.process_chat_cmd(entity, argv);
} else { } else {
self.clients.notify_registered(ServerMsg::chat( let message =
match self.state.ecs().read_storage::<comp::Player>().get(entity) { match self.state.ecs().read_storage::<comp::Player>().get(entity) {
Some(player) => format!("[{}] {}", &player.alias, msg), Some(player) => format!("[{}] {}", &player.alias, message),
None => format!("[<anon>] {}", msg), None => format!("[<anon>] {}", message),
}, };
)); self.clients
.notify_registered(ServerMsg::ChatMsg { chat_type, message });
} }
} else { } else {
self.clients.notify_registered(ServerMsg::chat(msg.clone())); self.clients
.notify_registered(ServerMsg::ChatMsg { chat_type, message });
}
}
_ => {
panic!("Invalid message type.");
}
} }
frontend_events.push(Event::Chat { entity, msg });
} }
// Handle client disconnects. // Handle client disconnects.

View File

@ -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 client::Event as ClientEvent;
use common::ChatType; use common::ChatType;
use conrod_core::{ use conrod_core::{
@ -190,7 +192,10 @@ impl<'a> Widget for Chat<'a> {
ClientEvent::Chat { chat_type, message } => { ClientEvent::Chat { chat_type, message } => {
let color = match chat_type { let color = match chat_type {
ChatType::Tell => TELL_COLOR, 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) let text = Text::new(&message)
.font_size(15) .font_size(15)

View File

@ -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 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 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 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! { widget_ids! {
struct Ids { struct Ids {