2019-04-16 16:22:44 +00:00
|
|
|
//! # Implementing new commands
|
2019-04-16 16:08:36 +00:00
|
|
|
//! To implement a new command, add an instance of `ChatCommand` to `CHAT_COMMANDS`
|
2019-04-16 16:22:44 +00:00
|
|
|
//! and provide a handler function.
|
2019-04-16 16:08:36 +00:00
|
|
|
|
2019-04-16 15:38:01 +00:00
|
|
|
use crate::Server;
|
|
|
|
use common::{comp, msg::ServerMsg};
|
2019-04-16 16:34:41 +00:00
|
|
|
use specs::{join::Join, Entity as EcsEntity};
|
2019-04-16 15:38:01 +00:00
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
|
2019-04-16 16:34:41 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use scan_fmt::scan_fmt;
|
2019-04-16 16:08:36 +00:00
|
|
|
/// Struct representing a command that a user can run from server chat
|
2019-04-16 15:38:01 +00:00
|
|
|
pub struct ChatCommand {
|
2019-04-16 16:22:44 +00:00
|
|
|
/// The keyword used to invoke the command, omitting the leading '/'
|
2019-04-16 15:38:01 +00:00
|
|
|
pub keyword: &'static str,
|
2019-04-16 16:22:44 +00:00
|
|
|
/// A format string for parsing arguments
|
2019-04-16 15:38:01 +00:00
|
|
|
arg_fmt: &'static str,
|
2019-04-16 16:08:36 +00:00
|
|
|
/// message to explain how the command is used
|
2019-04-16 15:38:01 +00:00
|
|
|
help_string: &'static str,
|
2019-04-16 16:22:44 +00:00
|
|
|
/// Handler function called when the command is executed
|
|
|
|
/// # Arguments
|
|
|
|
/// * `&mut Server` - the `Server` instance executing the command
|
|
|
|
/// * `EcsEntity` - an `Entity` corresponding to the player that invoked the command
|
|
|
|
/// * `String` - a `String` containing the part of the command after the keyword
|
|
|
|
/// * `&ChatCommand` - the command to execute with the above arguments
|
|
|
|
/// Handler functions must parse arguments from the the given `String` (`scan_fmt!` is included for this purpose)
|
2019-04-16 15:38:01 +00:00
|
|
|
handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChatCommand {
|
2019-04-16 16:08:36 +00:00
|
|
|
/// Creates a new chat command
|
2019-04-16 15:38:01 +00:00
|
|
|
pub fn new(
|
|
|
|
keyword: &'static str,
|
|
|
|
arg_fmt: &'static str,
|
|
|
|
help_string: &'static str,
|
|
|
|
handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
keyword,
|
|
|
|
arg_fmt,
|
|
|
|
help_string,
|
|
|
|
handler,
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 16:08:36 +00:00
|
|
|
/// Calls the contained handler function, passing `&self` as the last argument
|
2019-04-16 15:38:01 +00:00
|
|
|
pub fn execute(&self, server: &mut Server, entity: EcsEntity, args: String) {
|
|
|
|
(self.handler)(server, entity, args, self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
2019-04-16 16:08:36 +00:00
|
|
|
/// Static list of chat commands available to the server
|
2019-04-16 15:38:01 +00:00
|
|
|
pub static ref CHAT_COMMANDS: Vec<ChatCommand> = vec![
|
|
|
|
ChatCommand::new(
|
|
|
|
"jump",
|
|
|
|
"{d} {d} {d}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/jump <dx> <dy> <dz> : Offset your current position",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_jump
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"goto",
|
|
|
|
"{d} {d} {d}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/goto <x> <y> <z> : Teleport to a position",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_goto
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"alias",
|
|
|
|
"{}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/alias <name> : Change your alias",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_alias
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"tp",
|
|
|
|
"{}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/tp <alias>: Teleport to another player",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_tp
|
|
|
|
),
|
2019-04-17 13:00:24 +00:00
|
|
|
ChatCommand::new("help", "", "/help: Display this message", handle_help)
|
2019-04-16 15:38:01 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let (opt_x, opt_y, opt_z) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32);
|
|
|
|
match (opt_x, opt_y, opt_z) {
|
|
|
|
(Some(x), Some(y), Some(z)) => {
|
2019-04-16 16:34:41 +00:00
|
|
|
match server
|
2019-04-16 15:38:01 +00:00
|
|
|
.state
|
|
|
|
.read_component_cloned::<comp::phys::Pos>(entity)
|
|
|
|
{
|
2019-04-17 13:00:24 +00:00
|
|
|
Some(current_pos) => {
|
|
|
|
server.state.write_component(entity, comp::phys::Pos(current_pos.0 + Vec3::new(x, y, z)));
|
|
|
|
server.state.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
},
|
2019-04-16 16:34:41 +00:00
|
|
|
None => server.clients.notify(
|
2019-04-16 15:38:01 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(String::from("Command 'jump' invalid in current state")),
|
2019-04-16 16:34:41 +00:00
|
|
|
),
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let (opt_x, opt_y, opt_z) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32);
|
|
|
|
match (opt_x, opt_y, opt_z) {
|
2019-04-17 13:00:24 +00:00
|
|
|
(Some(x), Some(y), Some(z)) => {
|
|
|
|
server.state.write_component(entity, comp::phys::Pos(Vec3::new(x, y, z)));
|
|
|
|
server.state.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
},
|
2019-04-16 15:38:01 +00:00
|
|
|
_ => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let opt_alias = scan_fmt!(&args, action.arg_fmt, String);
|
|
|
|
match opt_alias {
|
|
|
|
Some(alias) => server
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::player::Player { alias }),
|
|
|
|
None => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_tp(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().internal();
|
|
|
|
let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::player::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
|
|
|
.map(|(entity, _)| entity);
|
|
|
|
match opt_player {
|
|
|
|
Some(player) => match server
|
|
|
|
.state
|
|
|
|
.read_component_cloned::<comp::phys::Pos>(player)
|
|
|
|
{
|
2019-04-17 13:00:24 +00:00
|
|
|
Some(pos) => {
|
|
|
|
server.state.write_component(entity, pos);
|
|
|
|
server.state.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
},
|
2019-04-16 15:38:01 +00:00
|
|
|
None => server.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!("Unable to teleport to player '{}'", alias)),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
server.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
|
|
|
server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
|
|
|
|
for cmd in CHAT_COMMANDS.iter() {
|
|
|
|
server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(cmd.help_string)));
|
|
|
|
}
|
2019-04-17 13:00:24 +00:00
|
|
|
}
|