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-05-12 19:57:39 +00:00
|
|
|
use specs::{Builder, Entity as EcsEntity, Join};
|
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-05-11 12:43:19 +00:00
|
|
|
"/tp <alias> : Teleport to another player",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_tp
|
|
|
|
),
|
2019-05-11 12:43:19 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"pet",
|
|
|
|
"{}",
|
|
|
|
"/pet : Spawn a test pet NPC",
|
|
|
|
handle_pet
|
|
|
|
),
|
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) => {
|
2019-04-29 20:37:19 +00:00
|
|
|
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)) => {
|
2019-04-29 20:37:19 +00:00
|
|
|
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) => {
|
2019-04-22 08:20:25 +00:00
|
|
|
let ecs = server.state.ecs();
|
2019-04-16 15:38:01 +00:00
|
|
|
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);
|
2019-04-29 20:37:19 +00:00
|
|
|
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))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 12:43:19 +00:00
|
|
|
fn handle_pet(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
match server
|
|
|
|
.state
|
|
|
|
.read_component_cloned::<comp::phys::Pos>(entity)
|
|
|
|
{
|
|
|
|
Some(pos) => {
|
2019-05-12 21:34:20 +00:00
|
|
|
server
|
|
|
|
.create_npc(
|
|
|
|
"Bungo".to_owned(),
|
|
|
|
comp::Body::Humanoid(comp::HumanoidBody::random()),
|
|
|
|
)
|
2019-05-12 21:21:18 +00:00
|
|
|
.with(comp::Control::default())
|
2019-05-12 21:34:20 +00:00
|
|
|
.with(comp::Agent::Pet {
|
|
|
|
target: entity,
|
|
|
|
offset: Vec2::zero(),
|
|
|
|
})
|
2019-05-12 21:21:18 +00:00
|
|
|
.with(pos)
|
|
|
|
.build();
|
2019-05-12 21:34:20 +00:00
|
|
|
server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat("Spawned pet!".to_owned()));
|
|
|
|
}
|
|
|
|
None => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat("You have no position!".to_owned())),
|
2019-05-11 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:38:01 +00:00
|
|
|
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
|
|
|
}
|