rewrite: minor code flow improvements

This commit is contained in:
2024-12-10 15:13:58 -05:00
parent 1db3e8b79a
commit f5531a7a01

View File

@@ -17,9 +17,14 @@ pub async fn wager(
} }
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;
let mut wealth = super::get_balance(ctx.author().id, &mut *tx).await?; let mut wealth = super::get_balance(ctx.author().id, &mut *tx).await?;
if wealth < amount {
ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.",
wealth)).await?;
return Ok(());
}
let item = if let Some(item) = item { let item = if let Some(item) = item {
let inventory = Inventory::new(ctx.author().id, Some(super::ID)); let inventory = Inventory::new(ctx.author().id, Some(super::ID));
@@ -43,21 +48,12 @@ pub async fn wager(
None None
}; };
if wealth < amount { let (multiplier, chance) = item.map(|item| item.effects.iter()
ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.", .fold((1.0, 0.5), |(m, c), effect| match effect {
wealth)).await?; Effect::Multiplier(m) => (*m, c),
return Ok(()); Effect::Chance(c) => (m, *c),
} })
).unwrap_or((1.0, 0.5));
let multiplier = item.clone().map(|item| item.effects.iter().fold(1.0, |acc, effect| match effect {
Effect::Multiplier(c) => *c,
_ => acc,
})).unwrap_or(1.0);
let chance = item.map(|item| item.effects.iter().fold(0.5, |acc, effect| match effect {
Effect::Chance(c) => *c,
_ => acc,
})).unwrap_or(0.5);
if rand::thread_rng().gen_bool(chance) { if rand::thread_rng().gen_bool(chance) {
let win = (amount as f64 * multiplier) as i32; let win = (amount as f64 * multiplier) as i32;