forked from projectmoon/tenebrous-dicebot
cargo fix and fmt
This commit is contained in:
parent
198f0bd43f
commit
e7458c12ad
|
@ -1,8 +1,4 @@
|
|||
use axfive_matrix_dicebot::dice::parser::parse_element_expression;
|
||||
use axfive_matrix_dicebot::roll::{Roll, Rolled};
|
||||
use axfive_matrix_dicebot::commands::parse_command;
|
||||
use std::error::Error;
|
||||
use std::string::ToString;
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
let command = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
|
||||
|
|
12
src/bot.rs
12
src/bot.rs
|
@ -1,4 +1,4 @@
|
|||
use crate::matrix::{SyncCommand, Event, RoomEvent, MessageContent};
|
||||
use crate::matrix::{Event, MessageContent, RoomEvent, SyncCommand};
|
||||
use reqwest::{Client, Url};
|
||||
use serde::{self, Deserialize, Serialize};
|
||||
use std::fs;
|
||||
|
@ -118,19 +118,21 @@ impl DiceBot {
|
|||
let sync: SyncCommand = serde_json::from_str(&body).unwrap();
|
||||
// First join invited rooms
|
||||
for room in sync.rooms.invite.keys() {
|
||||
let mut join_url = self.url(&format!("/_matrix/client/r0/rooms/{}/join", room), &[]);
|
||||
self.client.post(join_url)
|
||||
let join_url = self.url(&format!("/_matrix/client/r0/rooms/{}/join", room), &[]);
|
||||
self.client
|
||||
.post(join_url)
|
||||
.header("user-agent", USER_AGENT)
|
||||
.send()
|
||||
.await?;
|
||||
}
|
||||
|
||||
for (room_id, room) in sync.rooms.join.iter() {
|
||||
for (_room_id, room) in sync.rooms.join.iter() {
|
||||
for event in &room.timeline.events {
|
||||
if let Event::Room(RoomEvent {
|
||||
content: MessageContent::Text(message),
|
||||
..
|
||||
}) = event {
|
||||
}) = event
|
||||
{
|
||||
// TODO: create command parser (maybe command.rs) to parse !roll/!r commands
|
||||
// and reply
|
||||
println!("Body: {}", message.body());
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::dice::ElementExpression;
|
||||
use crate::roll::{Roll, Rolled};
|
||||
use nom::error::ErrorKind;
|
||||
use nom::IResult;
|
||||
use crate::roll::Roll;
|
||||
|
||||
pub mod parser;
|
||||
|
||||
pub struct Execution {
|
||||
|
@ -29,11 +28,11 @@ impl Command for RollCommand {
|
|||
fn execute(&self) -> Execution {
|
||||
let roll = self.0.roll();
|
||||
let plain = format!("Dice: {}\nResult: {}", self.0, roll);
|
||||
let html = format!("<strong>Dice:</strong> {}<br><strong>Result</strong>: {}", self.0, roll);
|
||||
Execution {
|
||||
plain,
|
||||
html,
|
||||
}
|
||||
let html = format!(
|
||||
"<strong>Dice:</strong> {}<br><strong>Result</strong>: {}",
|
||||
self.0, roll
|
||||
);
|
||||
Execution { plain, html }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
use nom::{
|
||||
tuple,
|
||||
switch,
|
||||
take_while,
|
||||
bytes::complete::{tag, take_while},
|
||||
character::complete::digit1,
|
||||
character::is_alphabetic,
|
||||
complete, many0, named,
|
||||
sequence::tuple,
|
||||
tag, IResult,
|
||||
switch, tag, take_while, tuple, IResult,
|
||||
};
|
||||
|
||||
use crate::parser::eat_whitespace;
|
||||
use crate::commands::{Command, RollCommand};
|
||||
use crate::dice::parser::parse_element_expression;
|
||||
use crate::parser::eat_whitespace;
|
||||
|
||||
// Parse a roll expression.
|
||||
fn parse_roll(input: &str) -> IResult<&str, RollCommand> {
|
||||
|
@ -29,14 +26,14 @@ pub fn parse_command(original_input: &str) -> IResult<&str, Option<Box<dyn Comma
|
|||
let (input, command) = match command(input) {
|
||||
// Strip the exclamation mark
|
||||
Ok((input, (_, result))) => (input, result),
|
||||
Err(e) => return Ok((original_input, None)),
|
||||
Err(_e) => return Ok((original_input, None)),
|
||||
};
|
||||
let (input, command) = match command {
|
||||
"r" | "roll" => {
|
||||
let (input, command) = parse_roll(input)?;
|
||||
let command: Box<dyn Command> = Box::new(command);
|
||||
(input, command)
|
||||
},
|
||||
}
|
||||
// No recognized command, ignore this.
|
||||
_ => return Ok((original_input, None)),
|
||||
};
|
||||
|
|
|
@ -83,4 +83,3 @@ impl fmt::Display for ElementExpression {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
use nom::{
|
||||
alt,
|
||||
bytes::complete::{tag, take_while},
|
||||
character::complete::digit1,
|
||||
complete, many0, named,
|
||||
sequence::tuple,
|
||||
tag, IResult,
|
||||
alt, bytes::complete::tag, character::complete::digit1, complete, many0, named,
|
||||
sequence::tuple, tag, IResult,
|
||||
};
|
||||
|
||||
use crate::dice::{Dice, Element, ElementExpression, SignedElement};
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
pub mod bot;
|
||||
pub mod commands;
|
||||
pub mod dice;
|
||||
pub mod matrix;
|
||||
pub mod roll;
|
||||
pub mod commands;
|
||||
mod parser;
|
||||
|
||||
pub mod roll;
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
use nom::{
|
||||
bytes::complete::take_while,
|
||||
IResult,
|
||||
};
|
||||
use nom::{bytes::complete::take_while, IResult};
|
||||
|
||||
fn is_whitespace(input: char) -> bool {
|
||||
input == ' ' || input == '\n' || input == '\t' || input == '\r'
|
||||
|
|
81
src/roll.rs
81
src/roll.rs
|
@ -1,8 +1,7 @@
|
|||
use rand::prelude::*;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use crate::dice;
|
||||
use rand::prelude::*;
|
||||
use std::fmt;
|
||||
use std::string::ToString;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
pub trait Roll {
|
||||
type Output;
|
||||
|
@ -54,7 +53,9 @@ impl Roll for dice::Dice {
|
|||
|
||||
fn roll(&self) -> DiceRoll {
|
||||
let mut rng = rand::thread_rng();
|
||||
let rolls = (0..self.count).map(|_| rng.gen_range(1, self.sides + 1)).collect();
|
||||
let rolls = (0..self.count)
|
||||
.map(|_| rng.gen_range(1, self.sides + 1))
|
||||
.collect();
|
||||
DiceRoll(rolls)
|
||||
}
|
||||
}
|
||||
|
@ -193,54 +194,86 @@ mod tests {
|
|||
fn dice_roll_display_test() {
|
||||
assert_eq!(DiceRoll(vec![1, 3, 4]).to_string(), "8 (1 + 3 + 4)");
|
||||
assert_eq!(DiceRoll(vec![]).to_string(), "0");
|
||||
assert_eq!(DiceRoll(vec![4, 7, 2, 10]).to_string(), "23 (4 + 7 + 2 + 10)");
|
||||
assert_eq!(
|
||||
DiceRoll(vec![4, 7, 2, 10]).to_string(),
|
||||
"23 (4 + 7 + 2 + 10)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn element_roll_display_test() {
|
||||
assert_eq!(ElementRoll::Dice(DiceRoll(vec![1, 3, 4])).to_string(), "8 (1 + 3 + 4)");
|
||||
assert_eq!(
|
||||
ElementRoll::Dice(DiceRoll(vec![1, 3, 4])).to_string(),
|
||||
"8 (1 + 3 + 4)"
|
||||
);
|
||||
assert_eq!(ElementRoll::Bonus(7).to_string(), "7");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_element_roll_display_test() {
|
||||
assert_eq!(SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), "8 (1 + 3 + 4)");
|
||||
assert_eq!(SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), "-8 (1 + 3 + 4)");
|
||||
assert_eq!(SignedElementRoll::Positive(ElementRoll::Bonus(7)).to_string(), "7");
|
||||
assert_eq!(SignedElementRoll::Negative(ElementRoll::Bonus(7)).to_string(), "-7");
|
||||
assert_eq!(
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(),
|
||||
"8 (1 + 3 + 4)"
|
||||
);
|
||||
assert_eq!(
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(),
|
||||
"-8 (1 + 3 + 4)"
|
||||
);
|
||||
assert_eq!(
|
||||
SignedElementRoll::Positive(ElementRoll::Bonus(7)).to_string(),
|
||||
"7"
|
||||
);
|
||||
assert_eq!(
|
||||
SignedElementRoll::Negative(ElementRoll::Bonus(7)).to_string(),
|
||||
"-7"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn element_expression_roll_display_test() {
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
||||
]).to_string(), "8 (1 + 3 + 4)");
|
||||
ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice(
|
||||
DiceRoll(vec![1, 3, 4])
|
||||
)),])
|
||||
.to_string(),
|
||||
"8 (1 + 3 + 4)"
|
||||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
||||
]).to_string(), "-8 (1 + 3 + 4)");
|
||||
ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice(
|
||||
DiceRoll(vec![1, 3, 4])
|
||||
)),])
|
||||
.to_string(),
|
||||
"-8 (1 + 3 + 4)"
|
||||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
|
||||
]).to_string(), "7");
|
||||
ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Bonus(7)),])
|
||||
.to_string(),
|
||||
"7"
|
||||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Negative(ElementRoll::Bonus(7)),
|
||||
]).to_string(), "-7");
|
||||
ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Bonus(7)),])
|
||||
.to_string(),
|
||||
"-7"
|
||||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
|
||||
SignedElementRoll::Positive(ElementRoll::Bonus(4)),
|
||||
SignedElementRoll::Negative(ElementRoll::Bonus(7)),
|
||||
]).to_string(), "2 (8 (1 + 3 + 4) - 3 (1 + 2) + 4 - 7)");
|
||||
])
|
||||
.to_string(),
|
||||
"2 (8 (1 + 3 + 4) - 3 (1 + 2) + 4 - 7)"
|
||||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
|
||||
SignedElementRoll::Negative(ElementRoll::Bonus(4)),
|
||||
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
|
||||
]).to_string(), "-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)");
|
||||
])
|
||||
.to_string(),
|
||||
"-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue