From 3d5cda39c8d15ed86e92469d0184b7c3917a62ed Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 21 May 2021 14:26:58 +0000 Subject: [PATCH] Consolidate dice module into logic module. --- src/cofd/dice.rs | 4 ++-- src/cthulhu/dice.rs | 2 +- src/dice.rs | 45 -------------------------------------------- src/lib.rs | 1 - src/logic.rs | 46 ++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 46 insertions(+), 52 deletions(-) delete mode 100644 src/dice.rs diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index 760aa30..3b465f3 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -308,7 +308,7 @@ pub async fn roll_pool(pool: &DicePoolWithContext<'_>) -> Result 0 { @@ -578,7 +578,7 @@ mod tests { let pool = DicePool::new(amounts, DicePoolModifiers::default()); assert_eq!( - crate::dice::calculate_dice_amount(&pool.amounts, &ctx) + crate::logic::calculate_dice_amount(&pool.amounts, &ctx) .await .unwrap(), 10 diff --git a/src/cthulhu/dice.rs b/src/cthulhu/dice.rs index bf4b0c6..6712fee 100644 --- a/src/cthulhu/dice.rs +++ b/src/cthulhu/dice.rs @@ -2,7 +2,7 @@ use crate::context::Context; use crate::db::Variables; use crate::error::{BotError, DiceRollingError}; use crate::parser::{Amount, Element}; -use crate::{dice::calculate_single_die_amount, parser::DiceParsingError}; +use crate::{logic::calculate_single_die_amount, parser::DiceParsingError}; use rand::rngs::StdRng; use rand::Rng; use rand::SeedableRng; diff --git a/src/dice.rs b/src/dice.rs deleted file mode 100644 index ee1c15c..0000000 --- a/src/dice.rs +++ /dev/null @@ -1,45 +0,0 @@ -use crate::context::Context; -use crate::db::Variables; -use crate::error::BotError; -use crate::error::DiceRollingError; -use crate::parser::Amount; -use crate::parser::Element as NewElement; -use futures::stream::{self, StreamExt, TryStreamExt}; -use std::slice; - -/// Calculate the amount of dice to roll by consulting the database -/// and replacing variables with corresponding the amount. Errors out -/// if it cannot find a variable defined, or if the database errors. -pub async fn calculate_single_die_amount( - amount: &Amount, - ctx: &Context<'_>, -) -> Result { - calculate_dice_amount(slice::from_ref(amount), ctx).await -} - -/// Calculate the amount of dice to roll by consulting the database -/// and replacing variables with corresponding amounts. Errors out if -/// it cannot find a variable defined, or if the database errors. -pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result { - let stream = stream::iter(amounts); - let variables = &ctx - .db - .get_user_variables(&ctx.username, ctx.room_id().as_str()) - .await?; - - use DiceRollingError::VariableNotFound; - let dice_amount: i32 = stream - .then(|amount| async move { - match &amount.element { - NewElement::Number(num_dice) => Ok(num_dice * amount.operator.mult()), - NewElement::Variable(variable) => variables - .get(variable) - .ok_or_else(|| VariableNotFound(variable.clone())) - .map(|i| *i), - } - }) - .try_fold(0, |total, num_dice| async move { Ok(total + num_dice) }) - .await?; - - Ok(dice_amount) -} diff --git a/src/lib.rs b/src/lib.rs index d67ade8..7e201ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ pub mod config; pub mod context; pub mod cthulhu; pub mod db; -pub mod dice; pub mod error; mod help; pub mod logic; diff --git a/src/logic.rs b/src/logic.rs index 046590f..d615cf9 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -1,9 +1,12 @@ -use crate::db::Rooms; -use crate::error::BotError; +use crate::context::Context; +use crate::db::{Rooms, Variables}; +use crate::error::{BotError, DiceRollingError}; use crate::matrix; use crate::models::RoomInfo; -use futures::stream::{self, StreamExt}; +use crate::parser::{Amount, Element}; +use futures::stream::{self, StreamExt, TryStreamExt}; use matrix_sdk::{self, identifiers::RoomId, Client}; +use std::slice; /// Record the information about a room, including users in it. pub async fn record_room_information( @@ -46,3 +49,40 @@ pub async fn record_room_information( .into_iter() .collect() } + +/// Calculate the amount of dice to roll by consulting the database +/// and replacing variables with corresponding the amount. Errors out +/// if it cannot find a variable defined, or if the database errors. +pub async fn calculate_single_die_amount( + amount: &Amount, + ctx: &Context<'_>, +) -> Result { + calculate_dice_amount(slice::from_ref(amount), ctx).await +} + +/// Calculate the amount of dice to roll by consulting the database +/// and replacing variables with corresponding amounts. Errors out if +/// it cannot find a variable defined, or if the database errors. +pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result { + let stream = stream::iter(amounts); + let variables = &ctx + .db + .get_user_variables(&ctx.username, ctx.room_id().as_str()) + .await?; + + use DiceRollingError::VariableNotFound; + let dice_amount: i32 = stream + .then(|amount| async move { + match &amount.element { + Element::Number(num_dice) => Ok(num_dice * amount.operator.mult()), + Element::Variable(variable) => variables + .get(variable) + .ok_or_else(|| VariableNotFound(variable.clone())) + .map(|i| *i), + } + }) + .try_fold(0, |total, num_dice| async move { Ok(total + num_dice) }) + .await?; + + Ok(dice_amount) +}