diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 36de3bd704..d117d47d7f 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -119,10 +119,16 @@ lazy_static! { handle_build, ), ChatCommand::new( - "killnpcs", - "{}", - "/killnpcs : Kill the NPCs", - handle_killnpcs, + "msg", + "{}", + "/msg : Send a message to another player", + handle_msg, + ), + ChatCommand::new( + "killnpcs", + "{}", + "/killnpcs : Kill the NPCs", + handle_killnpcs, ), ]; } @@ -430,3 +436,58 @@ fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _actio }; server.clients.notify(entity, ServerMsg::Chat(text)); } + +fn handle_msg(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { + let opt_alias = scan_fmt!(&args, action.arg_fmt, String); + match opt_alias { + Some(alias) => { + let ecs = server.state.ecs(); + let opt_player = (&ecs.entities(), &ecs.read_storage::()) + .join() + .find(|(_, player)| player.alias == alias) + .map(|(entity, _)| entity); + let msg = &args[alias.len()..args.len()]; + match opt_player { + Some(player) => { + if msg.len() > 1 { + let opt_name = ecs + .read_storage::() + .get(entity) + .map(|s| s.alias.clone()); + match opt_name { + Some(name) => { + server.clients.notify( + player, + ServerMsg::Chat(format!("{} tells you:{}", name, msg)), + ); + } + None => { + server.clients.notify( + entity, + ServerMsg::Chat(String::from("You do not exist!")), + ); + } + } + } else { + server.clients.notify( + entity, + ServerMsg::Chat(format!( + "You really should say something to {}!", + alias + )), + ); + } + } + None => { + server.clients.notify( + entity, + ServerMsg::Chat(format!("Player '{}' not found!", alias)), + ); + } + } + } + None => server + .clients + .notify(entity, ServerMsg::Chat(String::from(action.help_string))), + } +}