add items
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
use crate::common::{Context, Error};
|
||||
use crate::{common::{Context, Error}, inventory::Inventory};
|
||||
use super::Effect;
|
||||
use rand::Rng;
|
||||
|
||||
/// Put forward an amount of tokens to either lose or earn
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
pub async fn wager(
|
||||
ctx: Context<'_>,
|
||||
amount: i32) -> Result<(), Error>
|
||||
amount: i32,
|
||||
#[autocomplete = "super::autocomplete_inventory"]
|
||||
item: Option<String>) -> Result<(), Error>
|
||||
{
|
||||
// #[min = 1] does not appear to work with prefix commands
|
||||
if amount < 1 {
|
||||
@@ -16,16 +20,50 @@ pub async fn wager(
|
||||
|
||||
let mut wealth = super::get_balance(ctx.author().id, &mut *tx).await?;
|
||||
|
||||
let item = if let Some(item) = item {
|
||||
let inventory = Inventory::new(ctx.author().id, Some(super::ID));
|
||||
|
||||
match super::items::get_item_by_name(&item) {
|
||||
Some(item) => {
|
||||
if let Some(item) = inventory.get_item_of_type(&mut *tx, item.id).await? {
|
||||
inventory.remove_item(&mut *tx, item.id).await?;
|
||||
} else {
|
||||
ctx.reply(format!("You do not have a(n) {} to use.", item.name)).await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Some(item)
|
||||
}
|
||||
None => {
|
||||
ctx.reply("item {item} does not exist.").await?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if wealth < amount {
|
||||
ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.",
|
||||
wealth)).await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if rand::random() {
|
||||
wealth += amount;
|
||||
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) {
|
||||
let win = (amount as f64 * multiplier) as i32;
|
||||
wealth += win;
|
||||
ctx.reply(format!("You just gained **{}** token(s)! You now have **{}**.",
|
||||
amount, wealth)).await?;
|
||||
win, wealth)).await?;
|
||||
} else {
|
||||
wealth -= amount;
|
||||
ctx.reply(format!("You've lost **{}** token(s), you now have **{}**.",
|
||||
|
||||
Reference in New Issue
Block a user