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 {
match event {
Event::Chat {
chat_type: _,
message,
} => println!("{}", message),
Event::Chat { message, .. } => println!("{}", message),
Event::Disconnect => {} // TODO
}
}

View File

@ -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!

View File

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

View File

@ -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,
}
}
}

View File

@ -35,7 +35,7 @@ pub enum ServerMsg {
Pong,
ChatMsg {
chat_type: ChatType,
msg: String,
message: String,
},
SetPlayerEntity(u64),
EcsSync(sphynx::SyncPackage<EcsCompPacket, EcsResPacket>),
@ -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,
}
}
}

View File

@ -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))),
}
}

View File

@ -507,7 +507,10 @@ impl Server {
if let Some(player) =
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 => {
@ -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::<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);
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::<comp::Player>().get(entity) {
Some(player) => format!("[{}] {}", &player.alias, msg),
None => format!("[<anon>] {}", 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::<comp::Player>().get(entity) {
Some(player) => format!("[{}] {}", &player.alias, message),
None => format!("[<anon>] {}", 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.

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 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)

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 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 {