2020-10-16 21:20:17 +00:00
|
|
|
use crate::cofd::dice::{DicePool, DicePoolWithContext};
|
2020-10-15 16:52:08 +00:00
|
|
|
use crate::context::Context;
|
2020-10-16 12:40:25 +00:00
|
|
|
use crate::db::DataError;
|
2020-04-21 03:15:13 +00:00
|
|
|
use crate::dice::ElementExpression;
|
2020-10-16 12:40:25 +00:00
|
|
|
use crate::error::BotError;
|
2020-08-28 22:02:41 +00:00
|
|
|
use crate::help::HelpTopic;
|
2020-04-21 06:09:43 +00:00
|
|
|
use crate::roll::Roll;
|
2020-10-16 12:40:25 +00:00
|
|
|
use thiserror::Error;
|
2020-04-21 06:09:43 +00:00
|
|
|
|
2020-04-21 03:15:13 +00:00
|
|
|
pub mod parser;
|
|
|
|
|
2020-10-16 12:40:25 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum CommandError {
|
|
|
|
#[error("invalid command: {0}")]
|
|
|
|
InvalidCommand(String),
|
|
|
|
|
|
|
|
#[error("ignored command")]
|
|
|
|
IgnoredCommand,
|
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub struct Execution {
|
|
|
|
plain: String,
|
|
|
|
html: String,
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
impl Execution {
|
|
|
|
pub fn plain(&self) -> &str {
|
|
|
|
&self.plain
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub fn html(&self) -> &str {
|
|
|
|
&self.html
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Command {
|
2020-10-15 16:52:08 +00:00
|
|
|
fn execute(&self, ctx: &Context) -> Execution;
|
2020-08-21 21:49:22 +00:00
|
|
|
fn name(&self) -> &'static str;
|
2020-04-21 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:49:22 +00:00
|
|
|
pub struct RollCommand(ElementExpression);
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
impl Command for RollCommand {
|
2020-08-21 21:49:22 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"roll regular dice"
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:52:08 +00:00
|
|
|
fn execute(&self, _ctx: &Context) -> Execution {
|
2020-04-21 06:07:03 +00:00
|
|
|
let roll = self.0.roll();
|
|
|
|
let plain = format!("Dice: {}\nResult: {}", self.0, roll);
|
2020-04-21 06:09:43 +00:00
|
|
|
let html = format!(
|
2020-04-22 04:19:15 +00:00
|
|
|
"<p><strong>Dice:</strong> {}</p><p><strong>Result</strong>: {}</p>",
|
2020-04-21 06:09:43 +00:00
|
|
|
self.0, roll
|
|
|
|
);
|
|
|
|
Execution { plain, html }
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:49:22 +00:00
|
|
|
pub struct PoolRollCommand(DicePool);
|
|
|
|
|
|
|
|
impl Command for PoolRollCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"roll dice pool"
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:20:17 +00:00
|
|
|
fn execute(&self, ctx: &Context) -> Execution {
|
|
|
|
let pool_with_ctx = DicePoolWithContext(&self.0, ctx);
|
|
|
|
let roll_result = pool_with_ctx.roll();
|
2020-10-04 21:32:50 +00:00
|
|
|
|
|
|
|
let (plain, html) = match roll_result {
|
|
|
|
Ok(rolled_pool) => {
|
|
|
|
let plain = format!("Pool: {}\nResult: {}", rolled_pool, rolled_pool.roll);
|
|
|
|
let html = format!(
|
|
|
|
"<p><strong>Pool:</strong> {}</p><p><strong>Result</strong>: {}</p>",
|
|
|
|
rolled_pool, rolled_pool.roll
|
|
|
|
);
|
|
|
|
(plain, html)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
let plain = format!("Error: {}", e);
|
|
|
|
let html = format!("<p><strong>Error:</strong> {}</p>", e);
|
|
|
|
(plain, html)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-21 21:49:22 +00:00
|
|
|
Execution { plain, html }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 22:02:41 +00:00
|
|
|
pub struct HelpCommand(Option<HelpTopic>);
|
|
|
|
|
|
|
|
impl Command for HelpCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"help information"
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:52:08 +00:00
|
|
|
fn execute(&self, _ctx: &Context) -> Execution {
|
2020-08-28 22:02:41 +00:00
|
|
|
let help = match &self.0 {
|
|
|
|
Some(topic) => topic.message(),
|
|
|
|
_ => "There is no help for this topic",
|
|
|
|
};
|
|
|
|
|
|
|
|
let plain = format!("Help: {}", help);
|
|
|
|
let html = format!("<p><strong>Help:</strong> {}", help.replace("\n", "<br/>"));
|
|
|
|
Execution { plain, html }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:52:08 +00:00
|
|
|
pub struct GetVariableCommand(String);
|
|
|
|
|
|
|
|
impl Command for GetVariableCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"retrieve variable value"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(&self, ctx: &Context) -> Execution {
|
|
|
|
let name = &self.0;
|
2020-10-17 15:18:51 +00:00
|
|
|
let value = match ctx.db.get_user_variable(&ctx.room_id, &ctx.username, name) {
|
2020-10-16 12:40:25 +00:00
|
|
|
Ok(num) => format!("{} = {}", name, num),
|
|
|
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
|
2020-10-15 16:52:08 +00:00
|
|
|
Err(e) => format!("error getting {}: {}", name, e),
|
|
|
|
};
|
|
|
|
|
|
|
|
let plain = format!("Variable: {}", value);
|
|
|
|
let html = format!("<p><strong>Variable:</strong> {}", value);
|
|
|
|
Execution { plain, html }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SetVariableCommand(String, i32);
|
|
|
|
|
|
|
|
impl Command for SetVariableCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"set variable value"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(&self, ctx: &Context) -> Execution {
|
|
|
|
let name = &self.0;
|
|
|
|
let value = self.1;
|
2020-10-16 12:40:25 +00:00
|
|
|
let result = ctx
|
2020-10-15 16:52:08 +00:00
|
|
|
.db
|
2020-10-17 15:18:51 +00:00
|
|
|
.set_user_variable(&ctx.room_id, &ctx.username, name, value);
|
2020-10-16 12:40:25 +00:00
|
|
|
|
|
|
|
let content = match result {
|
2020-10-15 16:52:08 +00:00
|
|
|
Ok(_) => format!("{} = {}", name, value),
|
|
|
|
Err(e) => format!("error setting {}: {}", name, e),
|
|
|
|
};
|
|
|
|
|
2020-10-16 12:40:25 +00:00
|
|
|
let plain = format!("Set Variable: {}", content);
|
|
|
|
let html = format!("<p><strong>Set Variable:</strong> {}", content);
|
2020-10-15 16:52:08 +00:00
|
|
|
Execution { plain, html }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DeleteVariableCommand(String);
|
|
|
|
|
|
|
|
impl Command for DeleteVariableCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"delete variable"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(&self, ctx: &Context) -> Execution {
|
|
|
|
let name = &self.0;
|
2020-10-17 15:18:51 +00:00
|
|
|
let value = match ctx
|
|
|
|
.db
|
|
|
|
.delete_user_variable(&ctx.room_id, &ctx.username, name)
|
|
|
|
{
|
2020-10-15 16:52:08 +00:00
|
|
|
Ok(()) => format!("{} now unset", name),
|
2020-10-16 12:40:25 +00:00
|
|
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
|
2020-10-15 16:52:08 +00:00
|
|
|
Err(e) => format!("error deleting {}: {}", name, e),
|
|
|
|
};
|
|
|
|
|
|
|
|
let plain = format!("Remove Variable: {}", value);
|
|
|
|
let html = format!("<p><strong>Remove Variable:</strong> {}", value);
|
|
|
|
Execution { plain, html }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl dyn Command {
|
|
|
|
/// Parse a command string into a dynamic command execution trait
|
|
|
|
/// object. Returns an error if a command was recognized but not
|
|
|
|
/// parsed correctly. Returns Ok(None) if no command was recognized.
|
|
|
|
pub fn parse(s: &str) -> Result<Box<dyn Command>, BotError> {
|
|
|
|
match parser::parse_command(s) {
|
|
|
|
Ok(Some(command)) => Ok(command),
|
|
|
|
Ok(None) => Err(BotError::CommandError(CommandError::IgnoredCommand)),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CommandResult {
|
|
|
|
pub plain: String,
|
|
|
|
pub html: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to execute a command, and return the content that should
|
|
|
|
/// go back to Matrix, if the command was executed (successfully or
|
|
|
|
/// not). If a command is determined to be ignored, this function will
|
|
|
|
/// return None, signifying that we should not send a response.
|
2020-10-17 15:18:51 +00:00
|
|
|
pub fn execute_command(ctx: &Context) -> Option<CommandResult> {
|
|
|
|
let res = Command::parse(&ctx.message_body).map(|cmd| {
|
2020-10-15 16:52:08 +00:00
|
|
|
let execution = cmd.execute(ctx);
|
|
|
|
(execution.plain().into(), execution.html().into())
|
|
|
|
});
|
|
|
|
|
|
|
|
let (plain, html) = match res {
|
|
|
|
Ok(plain_and_html) => plain_and_html,
|
|
|
|
Err(BotError::CommandError(CommandError::IgnoredCommand)) => return None,
|
|
|
|
Err(e) => {
|
|
|
|
let message = format!("Error parsing command: {}", e);
|
|
|
|
let html_message = format!("<p><strong>{}</strong></p>", message);
|
|
|
|
(message, html_message)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let plain = format!("{}\n{}", ctx.username, plain);
|
|
|
|
let html = format!("<p>{}</p>\n{}", ctx.username, html);
|
|
|
|
|
|
|
|
Some(CommandResult {
|
|
|
|
plain: plain,
|
|
|
|
html: html,
|
|
|
|
})
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
2020-08-21 21:49:22 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chance_die_is_not_malformed() {
|
2020-10-15 16:52:08 +00:00
|
|
|
assert!(Command::parse("!chance").is_ok());
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roll_malformed_expression_test() {
|
2020-10-15 16:52:08 +00:00
|
|
|
assert!(Command::parse("!roll 1d20asdlfkj").is_err());
|
|
|
|
assert!(Command::parse("!roll 1d20asdlfkj ").is_err());
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-30 20:48:52 +00:00
|
|
|
fn roll_dice_pool_malformed_expression_test() {
|
2020-10-15 16:52:08 +00:00
|
|
|
assert!(Command::parse("!pool 8abc").is_err());
|
|
|
|
assert!(Command::parse("!pool 8abc ").is_err());
|
2020-08-30 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pool_whitespace_test() {
|
2020-10-15 16:52:08 +00:00
|
|
|
Command::parse("!pool ns3:8 ").expect("was error");
|
|
|
|
Command::parse(" !pool ns3:8").expect("was error");
|
|
|
|
Command::parse(" !pool ns3:8 ").expect("was error");
|
2020-08-30 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 22:02:41 +00:00
|
|
|
#[test]
|
|
|
|
fn help_whitespace_test() {
|
2020-10-15 16:52:08 +00:00
|
|
|
Command::parse("!help stuff ").expect("was error");
|
|
|
|
Command::parse(" !help stuff").expect("was error");
|
|
|
|
Command::parse(" !help stuff ").expect("was error");
|
2020-08-28 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 20:48:52 +00:00
|
|
|
#[test]
|
|
|
|
fn roll_whitespace_test() {
|
2020-10-15 16:52:08 +00:00
|
|
|
Command::parse("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
|
|
Command::parse("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
|
|
Command::parse(" !roll 1d4 + 5d6 -3 ").expect("was error");
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
}
|