From a9e1ccbf2e8f22b8588bdde73a7f24927084dc22 Mon Sep 17 00:00:00 2001 From: "Taylor C. Richberger" Date: Mon, 20 Apr 2020 11:19:50 -0600 Subject: [PATCH] restructure into dice module --- Cargo.lock | 1 + src/dice.rs | 44 ++++++++++++++++++++++++++++++++++++++++ src/{ => dice}/parser.rs | 30 +++------------------------ src/lib.rs | 3 ++- src/roll.rs | 39 +++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 src/dice.rs rename src/{ => dice}/parser.rs (92%) create mode 100644 src/roll.rs diff --git a/Cargo.lock b/Cargo.lock index 00bfec5..4366697 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,7 @@ name = "axfive-matrix-dicebot" version = "0.1.0" dependencies = [ "nom", + "rand", "reqwest", "serde", "serde_json", diff --git a/src/dice.rs b/src/dice.rs new file mode 100644 index 0000000..7fd0254 --- /dev/null +++ b/src/dice.rs @@ -0,0 +1,44 @@ +pub mod parser; + +use std::ops::{Deref, DerefMut}; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct Dice { + pub(crate) count: u32, + pub(crate) sides: u32, +} + +impl Dice { + fn new(count: u32, sides: u32) -> Dice { + Dice { count, sides } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Element { + Dice(Dice), + Bonus(u32), +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum SignedElement { + Positive(Element), + Negative(Element), +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct ElementExpression(Vec); + +impl Deref for ElementExpression { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ElementExpression { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/src/parser.rs b/src/dice/parser.rs similarity index 92% rename from src/parser.rs rename to src/dice/parser.rs index a31d1c3..7a1a672 100644 --- a/src/parser.rs +++ b/src/dice/parser.rs @@ -7,39 +7,14 @@ use nom::{ tag, IResult, }; -#[derive(Debug, PartialEq, Eq)] -struct Dice { - count: u32, - sides: u32, -} +use crate::dice::{Dice, Element, SignedElement, ElementExpression}; -impl Dice { - fn new(count: u32, sides: u32) -> Dice { - Dice { count, sides } - } -} - -#[derive(Debug, PartialEq, Eq)] -enum Element { - Dice(Dice), - Bonus(u32), -} - -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] enum Sign { Plus, Minus, } -#[derive(Debug, PartialEq, Eq)] -enum SignedElement { - Positive(Element), - Negative(Element), -} - -#[derive(Debug, PartialEq, Eq)] -struct ElementExpression(Vec); - fn is_whitespace(input: char) -> bool { input == ' ' || input == '\n' || input == '\t' || input == '\r' } @@ -205,3 +180,4 @@ mod tests { ); } } + diff --git a/src/lib.rs b/src/lib.rs index bc0754b..8f10660 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod bot; +pub mod dice; pub mod matrix; -pub mod parser; +pub mod roll; diff --git a/src/roll.rs b/src/roll.rs new file mode 100644 index 0000000..3738bd8 --- /dev/null +++ b/src/roll.rs @@ -0,0 +1,39 @@ +use rand::prelude::*; +use crate::dice; + +pub trait Roll { + type Output; + + fn roll(&self) -> Self::Output; +} + +impl Roll for dice::Dice { + type Output = u32; + + fn roll(&self) -> u32 { + let mut rng = rand::thread_rng(); + (0..self.count).map(|_| rng.gen_range(1, self.sides + 1)).sum() + } +} + +impl Roll for dice::Element { + type Output = u32; + + fn roll(&self) -> u32 { + match self { + dice::Element::Dice(d) => d.roll(), + dice::Element::Bonus(b) => *b, + } + } +} + +impl Roll for dice::SignedElement { + type Output = i32; + + fn roll(&self) -> i32 { + match self { + dice::SignedElement::Positive(e) => e.roll() as i32, + dice::SignedElement::Negative(e) => -(e.roll() as i32), + } + } +}