From 66f9bc6013f48fc13ff0a68c0f67541745141cd8 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Wed, 4 Nov 2020 20:34:57 +0000 Subject: [PATCH] Move original dice rolling code into its own 'basic' module. This gives it parity with the other systems: cofd and cthulhu. More refactoring and a rewrite later as we trend towards more system-specific implementations. --- src/basic.rs | 3 ++ src/basic/dice.rs | 85 ++++++++++++++++++++++++++++++++++ src/{dice => basic}/parser.rs | 18 +++++++- src/{ => basic}/roll.rs | 2 +- src/cofd/dice.rs | 7 --- src/commands/basic_rolling.rs | 4 +- src/commands/parser.rs | 2 +- src/dice.rs | 87 ----------------------------------- src/lib.rs | 2 +- src/parser.rs | 15 ------ 10 files changed, 109 insertions(+), 116 deletions(-) create mode 100644 src/basic.rs create mode 100644 src/basic/dice.rs rename src/{dice => basic}/parser.rs (92%) rename src/{ => basic}/roll.rs (99%) diff --git a/src/basic.rs b/src/basic.rs new file mode 100644 index 0000000..96b4061 --- /dev/null +++ b/src/basic.rs @@ -0,0 +1,3 @@ +pub mod dice; +pub mod parser; +pub mod roll; diff --git a/src/basic/dice.rs b/src/basic/dice.rs new file mode 100644 index 0000000..64f6879 --- /dev/null +++ b/src/basic/dice.rs @@ -0,0 +1,85 @@ +use std::fmt; +use std::ops::{Deref, DerefMut}; + +//Old stuff, for regular dice rolling. To be moved elsewhere. + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct Dice { + pub(crate) count: u32, + pub(crate) sides: u32, +} + +impl fmt::Display for Dice { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}d{}", self.count, self.sides) + } +} + +impl Dice { + pub fn new(count: u32, sides: u32) -> Dice { + Dice { count, sides } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Element { + Dice(Dice), + Bonus(u32), +} + +impl fmt::Display for Element { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Element::Dice(d) => write!(f, "{}", d), + Element::Bonus(b) => write!(f, "{}", b), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum SignedElement { + Positive(Element), + Negative(Element), +} + +impl fmt::Display for SignedElement { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + SignedElement::Positive(e) => write!(f, "{}", e), + SignedElement::Negative(e) => write!(f, "-{}", e), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct ElementExpression(pub 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 + } +} + +impl fmt::Display for ElementExpression { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut iter = self.0.iter(); + if let Some(first) = iter.next() { + write!(f, "{}", first)?; + for roll in iter { + match roll { + SignedElement::Positive(e) => write!(f, " + {}", e)?, + SignedElement::Negative(e) => write!(f, " - {}", e)?, + } + } + } + Ok(()) + } +} diff --git a/src/dice/parser.rs b/src/basic/parser.rs similarity index 92% rename from src/dice/parser.rs rename to src/basic/parser.rs index 3e59557..67ed016 100644 --- a/src/dice/parser.rs +++ b/src/basic/parser.rs @@ -1,10 +1,24 @@ +use nom::bytes::complete::take_while; use nom::{ alt, bytes::complete::tag, character::complete::digit1, complete, many0, named, sequence::tuple, tag, IResult, }; -use crate::dice::{Dice, Element, ElementExpression, SignedElement}; -use crate::parser::eat_whitespace; +use super::dice::*; + +//****************************** +//Legacy Code +//****************************** + +fn is_whitespace(input: char) -> bool { + input == ' ' || input == '\n' || input == '\t' || input == '\r' +} + +/// Eat whitespace, returning it +pub fn eat_whitespace(input: &str) -> IResult<&str, &str> { + let (input, whitespace) = take_while(is_whitespace)(input)?; + Ok((input, whitespace)) +} #[derive(Debug, PartialEq, Eq, Clone, Copy)] enum Sign { diff --git a/src/roll.rs b/src/basic/roll.rs similarity index 99% rename from src/roll.rs rename to src/basic/roll.rs index 3352270..36e22c9 100644 --- a/src/roll.rs +++ b/src/basic/roll.rs @@ -1,4 +1,4 @@ -use crate::dice; +use crate::basic::dice; use rand::prelude::*; use std::fmt; use std::ops::{Deref, DerefMut}; diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index 93e3586..f97ad73 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -1,7 +1,6 @@ use crate::context::Context; use crate::error::{BotError, DiceRollingError}; use crate::parser::{Amount, Element, Operator}; -use crate::roll::Rolled; use itertools::Itertools; use std::convert::TryFrom; use std::fmt; @@ -205,12 +204,6 @@ impl DicePoolRoll { /// Attach a Context to a dice pool. Needed for database access. pub struct DicePoolWithContext<'a>(pub &'a DicePool, pub &'a Context<'a>); -impl Rolled for DicePoolRoll { - fn rolled_value(&self) -> i32 { - self.successes() - } -} - impl fmt::Display for DicePoolRoll { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let successes = self.successes(); diff --git a/src/commands/basic_rolling.rs b/src/commands/basic_rolling.rs index c2e23b5..df6801d 100644 --- a/src/commands/basic_rolling.rs +++ b/src/commands/basic_rolling.rs @@ -1,7 +1,7 @@ use super::{Command, Execution}; +use crate::basic::dice::ElementExpression; +use crate::basic::roll::Roll; use crate::context::Context; -use crate::dice::ElementExpression; -use crate::roll::Roll; use async_trait::async_trait; pub struct RollCommand(pub ElementExpression); diff --git a/src/commands/parser.rs b/src/commands/parser.rs index fbb2d80..57e90a4 100644 --- a/src/commands/parser.rs +++ b/src/commands/parser.rs @@ -1,3 +1,4 @@ +use crate::basic::parser::parse_element_expression; use crate::cofd::parser::{create_chance_die, parse_dice_pool}; use crate::commands::{ basic_rolling::RollCommand, @@ -10,7 +11,6 @@ use crate::commands::{ Command, }; use crate::cthulhu::parser::{parse_advancement_roll, parse_regular_roll}; -use crate::dice::parser::parse_element_expression; use crate::error::BotError; use crate::help::parse_help_topic; use crate::variables::parse_set_variable; diff --git a/src/dice.rs b/src/dice.rs index 05e92ee..1472559 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -1,5 +1,3 @@ -pub mod parser; - use crate::context::Context; use crate::db::variables::UserAndRoom; use crate::error::BotError; @@ -7,8 +5,6 @@ use crate::error::DiceRollingError; use crate::parser::Amount; use crate::parser::Element as NewElement; use futures::stream::{self, StreamExt, TryStreamExt}; -use std::fmt; -use std::ops::{Deref, DerefMut}; //New hotness pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result { @@ -33,86 +29,3 @@ pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Res dice_amount } - -//Old stuff, for regular dice rolling. To be moved elsewhere. - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub struct Dice { - pub(crate) count: u32, - pub(crate) sides: u32, -} - -impl fmt::Display for Dice { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}d{}", self.count, self.sides) - } -} - -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), -} - -impl fmt::Display for Element { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Element::Dice(d) => write!(f, "{}", d), - Element::Bonus(b) => write!(f, "{}", b), - } - } -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum SignedElement { - Positive(Element), - Negative(Element), -} - -impl fmt::Display for SignedElement { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - SignedElement::Positive(e) => write!(f, "{}", e), - SignedElement::Negative(e) => write!(f, "-{}", e), - } - } -} - -#[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 - } -} - -impl fmt::Display for ElementExpression { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut iter = self.0.iter(); - if let Some(first) = iter.next() { - write!(f, "{}", first)?; - for roll in iter { - match roll { - SignedElement::Positive(e) => write!(f, " + {}", e)?, - SignedElement::Negative(e) => write!(f, " - {}", e)?, - } - } - } - Ok(()) - } -} diff --git a/src/lib.rs b/src/lib.rs index cb466db..3b78482 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod basic; pub mod bot; pub mod cofd; pub mod commands; @@ -9,6 +10,5 @@ pub mod dice; pub mod error; mod help; mod parser; -pub mod roll; pub mod state; pub mod variables; diff --git a/src/parser.rs b/src/parser.rs index 9d03693..ec70a3f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,6 +1,5 @@ use combine::parser::char::{digit, letter, spaces}; use combine::{many, many1, one_of, Parser}; -use nom::{bytes::complete::take_while, IResult}; use thiserror::Error; //****************************** @@ -122,20 +121,6 @@ pub fn parse_amounts(input: &str) -> Result, DiceParsingError> { } } -//****************************** -//Legacy Code -//****************************** - -fn is_whitespace(input: char) -> bool { - input == ' ' || input == '\n' || input == '\t' || input == '\r' -} - -/// Eat whitespace, returning it -pub fn eat_whitespace(input: &str) -> IResult<&str, &str> { - let (input, whitespace) = take_while(is_whitespace)(input)?; - Ok((input, whitespace)) -} - #[cfg(test)] mod tests {