2020-08-27 23:56:19 +00:00
|
|
|
use crate::cofd::dice::DicePool;
|
2020-04-21 03:15:13 +00:00
|
|
|
use crate::dice::ElementExpression;
|
2020-04-21 06:09:43 +00:00
|
|
|
use crate::roll::Roll;
|
|
|
|
|
2020-04-21 03:15:13 +00:00
|
|
|
pub mod parser;
|
|
|
|
|
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 {
|
|
|
|
fn execute(&self) -> 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-04-21 06:07:03 +00:00
|
|
|
fn execute(&self) -> Execution {
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(&self) -> Execution {
|
|
|
|
let roll = self.0.roll();
|
|
|
|
let plain = format!("Pool: {}\nResult: {}", self.0, roll);
|
|
|
|
let html = format!(
|
|
|
|
"<p><strong>Pool:</strong> {}</p><p><strong>Result</strong>: {}</p>",
|
|
|
|
self.0, roll
|
|
|
|
);
|
|
|
|
Execution { plain, html }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 04:19:15 +00:00
|
|
|
/// Parse a command string into a dynamic command execution trait object.
|
|
|
|
/// Returns an error if a command was recognized but not parsed correctly. Returns None if no
|
|
|
|
/// command was recognized.
|
2020-04-21 06:22:31 +00:00
|
|
|
pub fn parse_command(s: &str) -> Result<Option<Box<dyn Command>>, String> {
|
2020-04-21 06:07:03 +00:00
|
|
|
match parser::parse_command(s) {
|
2020-08-21 21:49:22 +00:00
|
|
|
Ok((input, result)) => match (input, &result) {
|
2020-08-28 00:13:01 +00:00
|
|
|
//This clause prevents bot from spamming messages to itself
|
|
|
|
//after executing a previous command.
|
2020-08-21 21:49:22 +00:00
|
|
|
("", Some(_)) | (_, None) => Ok(result),
|
|
|
|
_ => Err(format!("{}: malformed dice expression", s)),
|
|
|
|
},
|
2020-04-21 06:22:31 +00:00
|
|
|
Err(err) => Err(err.to_string()),
|
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() {
|
|
|
|
assert!(parse_command("!chance").is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roll_malformed_expression_test() {
|
|
|
|
assert!(parse_command("!roll 1d20asdlfkj").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roll_dice_pool_expression_test() {
|
|
|
|
assert!(parse_command("!pool 8abc").is_err());
|
|
|
|
}
|
|
|
|
}
|