From 747aa81d6228ea608b4c7fa01f7f287e943e5dd0 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 31 Mar 2024 12:35:20 -0600 Subject: [PATCH] Made checking nft value easier --- src/discord/motivate.rs | 4 ++-- src/discord/shop.rs | 15 ++++++++++----- src/inventory/mod.rs | 20 ++++++++++++-------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/discord/motivate.rs b/src/discord/motivate.rs index 5fdbefb..1ee9fe5 100644 --- a/src/discord/motivate.rs +++ b/src/discord/motivate.rs @@ -65,7 +65,7 @@ pub async fn create_motivation_image(motivation: Motivation) -> Result, text_wand.set_font_size(0.07 * (wand.get_image_width() as f64)); text_wand.set_text_alignment(magick_rust::bindings::AlignType_CenterAlign); wand.annotate_image(&text_wand, text_pos_x, text_pos_y, 0.0, &text)?; - Ok(wand.write_image_blob("png")?) + Ok(wand.write_image_blob("jpg")?) } #[command] @@ -97,7 +97,7 @@ async fn motivation(ctx: &Context, msg: &Message, args: Args) -> CommandResult { .content("Today's motivation") .add_file(CreateAttachment::bytes( Cow::from(image), - "motivate.png".to_string(), + "motivate.jpg".to_string(), )), ) .await?; diff --git a/src/discord/shop.rs b/src/discord/shop.rs index f6ade45..8c872df 100644 --- a/src/discord/shop.rs +++ b/src/discord/shop.rs @@ -1,7 +1,7 @@ use crate::discord::get_role; use crate::discord::motivate::create_motivation_image; use crate::error::Error; -use crate::inventory::{InventoryError, ItemData, ItemType, Operation}; +use crate::inventory::{nft_value, InventoryError, ItemData, ItemType, Operation}; use crate::models::motivation::MotivationConfig; use crate::models::task::{Task, TaskType}; use crate::user::{User, UserError}; @@ -251,7 +251,7 @@ async fn use_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult { } ItemType::Nft => { if let Some(ItemData::Nft(path)) = item_data { - let file: tokio::fs::File = match tokio::fs::File::open(path).await { + let file: tokio::fs::File = match tokio::fs::File::open(&path).await { Ok(f) => f, Err(_) => { msg.reply(&ctx.http, "Sorry this was a pump and dump") @@ -259,13 +259,18 @@ async fn use_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult { return Ok(()); } }; + + let value = nft_value(&path); msg.channel_id .send_message( &ctx.http, CreateMessage::new() - .content("Your NFT my good friend:") + .content(format!( + "Your NFT my good friend. It's worth **{} FC**!", + value + )) .add_file( - CreateAttachment::file(&file, "nft.png".to_string()) + CreateAttachment::file(&file, "nft.jpg".to_string()) .await .unwrap(), ), @@ -477,7 +482,7 @@ pub async fn restock_shop(ctx: &Context, global_data: &GlobalData) -> Result<(), hasher.write(&nft); let nft_hash = hasher.finish(); - let path = global_data.cfg.nft_path.join(format!("{}.png", nft_hash)); + let path = global_data.cfg.nft_path.join(format!("{}.jpg", nft_hash)); if path.exists() { continue; diff --git a/src/inventory/mod.rs b/src/inventory/mod.rs index f29e0c5..ae5c7fa 100644 --- a/src/inventory/mod.rs +++ b/src/inventory/mod.rs @@ -108,6 +108,17 @@ impl Display for ItemType { } } +pub fn nft_value(s: &String) -> i64 { + let mut hasher = DefaultHasher::new(); + s.hash(&mut hasher); + + let hash = hasher.finish(); + + let per_value = (hash as f64) / (u64::MAX as f64); + + ((2000.0 * per_value) as i64) - 500 +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct InventorySlot { pub quantity: i64, @@ -132,14 +143,7 @@ impl InventorySlot { match self.item_type { ItemType::Nft => { if let Some(ItemData::Nft(s)) = &self.item_data { - let mut hasher = DefaultHasher::new(); - s.hash(&mut hasher); - - let hash = hasher.finish(); - - let per_value = (hash as f64) / (u64::MAX as f64); - - ((2000.0 * per_value) as i64) - 500 + nft_value(s) } else { 0 }