small fixes, rename
This commit is contained in:
parent
2c2c78114f
commit
052d8359c4
4 changed files with 54 additions and 30 deletions
24
src/dice.rs
24
src/dice.rs
|
|
@ -99,10 +99,20 @@ impl Expression {
|
|||
bail!("Too many dice.")
|
||||
};
|
||||
write!(w, "d")?;
|
||||
let size = size_node.evaluate(rng, w, precedence)? as i64;
|
||||
if size < 1 {
|
||||
bail!("Invalid die size.")
|
||||
let size = size_node.evaluate(rng, w, precedence)?;
|
||||
if size.is_nan() {
|
||||
bail!("dNaN isn't rollable, sorry.")
|
||||
}
|
||||
if size.is_infinite() {
|
||||
bail!("dinf isn't rollable, sorry.")
|
||||
}
|
||||
if size < 1.0 {
|
||||
bail!("Die size is smaller than 1!")
|
||||
}
|
||||
if size > i64::MAX as f64 {
|
||||
bail!("Die size is too big!")
|
||||
}
|
||||
let size = size as i64;
|
||||
|
||||
let mut rolls = (0..count)
|
||||
.map(|_| rng.random_range(1..=size))
|
||||
|
|
@ -118,9 +128,10 @@ impl Expression {
|
|||
for fragment in fragments {
|
||||
comparers.push(fragment.comparer(rng, w)?);
|
||||
}
|
||||
for i in 0..DICE_POOL_LIMIT {
|
||||
if i >= rolls.len() {
|
||||
break;
|
||||
let mut i = 0;
|
||||
while i < rolls.len() {
|
||||
if i > DICE_POOL_LIMIT {
|
||||
bail!("Explosion added too many dice.")
|
||||
}
|
||||
if comparers.len() == 0 {
|
||||
// Explode on max size dice
|
||||
|
|
@ -136,6 +147,7 @@ impl Expression {
|
|||
}
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
24
src/main.rs
24
src/main.rs
|
|
@ -4,10 +4,7 @@ mod parsing;
|
|||
use anyhow::bail;
|
||||
use dotenv::dotenv;
|
||||
use rand::{Rng, SeedableRng};
|
||||
use serenity::all::{
|
||||
Command, CommandInteraction, CommandOptionType, CreateCommandOption, CreateComponent,
|
||||
FullEvent, Interaction, MessageFlags, ResolvedOption, ResolvedValue,
|
||||
};
|
||||
use serenity::all::{Command, CommandInteraction, CommandOptionType, CreateCommandOption, CreateComponent, FullEvent, Interaction, InteractionContext, MessageFlags, ResolvedOption, ResolvedValue};
|
||||
use serenity::builder::{
|
||||
CreateCommand, CreateContainer, CreateContainerComponent, CreateInteractionResponse,
|
||||
CreateInteractionResponseFollowup, CreateInteractionResponseMessage, CreateTextDisplay,
|
||||
|
|
@ -96,7 +93,10 @@ impl EventHandler for Handler {
|
|||
CommandOptionType::String,
|
||||
"seed",
|
||||
"Roll with a fixed seed. This bot uses xoshiro256++.",
|
||||
)),
|
||||
))
|
||||
.add_context(InteractionContext::Guild)
|
||||
.add_context(InteractionContext::BotDm)
|
||||
.add_context(InteractionContext::PrivateChannel),
|
||||
)
|
||||
.await;
|
||||
|
||||
|
|
@ -189,7 +189,19 @@ pub async fn roll(ctx: &Context, command: &CommandInteraction) -> anyhow::Result
|
|||
let seed = fixed_seed.unwrap_or_else(|| rand::rng().next_u64());
|
||||
let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(seed);
|
||||
|
||||
let ev = (expression.average(&mut rng)? * 10.0).round() / 10.0;
|
||||
let average = match expression.average(&mut rng) {
|
||||
Ok(res) => res,
|
||||
Err(err) => {
|
||||
command
|
||||
.create_followup(
|
||||
&ctx.http,
|
||||
CreateInteractionResponseFollowup::new().content(format!("{}", err)),
|
||||
)
|
||||
.await?;
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
let ev = (average * 10.0).round() / 10.0;
|
||||
|
||||
let mut text_components = Vec::with_capacity((repeat + 2) as usize);
|
||||
if let Some(description) = description {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue