From a97335c7e3afa8ba2e91bc52b509f740276754b4 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Tue, 5 Aug 2025 18:27:59 -0600 Subject: [PATCH] Fixed gogurt rate sometimes being NaN --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/discord/stonks.rs | 10 ++++++---- src/models/gogurt_reserves.rs | 22 ++++++++++++++++------ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8daa267..5d0aeb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ dependencies = [ [[package]] name = "fren" -version = "2.2.0" +version = "2.2.1" dependencies = [ "axum 0.8.1", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index 9bba96b..f50bba8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fren" -version = "2.2.0" +version = "2.2.1" edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/discord/stonks.rs b/src/discord/stonks.rs index d745ddb..aefa973 100644 --- a/src/discord/stonks.rs +++ b/src/discord/stonks.rs @@ -12,8 +12,10 @@ pub async fn contribute_to_gogurt_reserve( let gogurt_bought = GogurtReserves::add_contribution(&ctx.data().db, ctx.author().id, contribute_amount)?; - ctx.reply(format!("You have purchased {gogurt_bought}lbs of Gogurt")) - .await?; + ctx.reply(format!( + "You have purchased {gogurt_bought:.3}lbs of Gogurt" + )) + .await?; Ok(()) } @@ -42,7 +44,7 @@ pub async fn gogurt_reserve_stats(ctx: Context<'_>) -> Result<(), Error> { let contributors = gogurt_reserves.get_contributors(); let total_amount = gogurt_reserves.get_total_amount(); let total_worth = gogurt_reserves.get_total_worth(); - let going_rate = gogurt_reserves.gogurt_rate_per_pound; + let going_rate = gogurt_reserves.gogurt_rate_per_pound(); let mut msg_builder = MessageBuilder::new(); @@ -92,7 +94,7 @@ pub async fn gogurt_reserve_contribution( let mention = user_id.mention(); if let Some(contribution) = contribution { let worth = gogurt_reserves.convert_pounds_to_fc(*contribution); - let rate = gogurt_reserves.gogurt_rate_per_pound; + let rate = gogurt_reserves.gogurt_rate_per_pound(); ctx.reply(format!("{mention}'s contribution is **{contribution:.3} lb** of Gogurt Brand Gogurt worth **{worth} FC** at the current rate of **{rate:.3} FC/lb**")).await?; } else { diff --git a/src/models/gogurt_reserves.rs b/src/models/gogurt_reserves.rs index 1623651..123b1da 100644 --- a/src/models/gogurt_reserves.rs +++ b/src/models/gogurt_reserves.rs @@ -12,7 +12,7 @@ use std::f32::consts::PI; pub struct GogurtReserves { id: Option, pub reserve_contributors: HashMap, - pub gogurt_rate_per_pound: f64, + pub gogurt_rate_per_pound: Option, } impl j_db::model::JdbModel for GogurtReserves { @@ -99,15 +99,25 @@ impl GogurtReserves { } pub fn get_total_worth(&self) -> u64 { - (self.get_total_amount() * self.gogurt_rate_per_pound) as u64 + (self.get_total_amount() * self.gogurt_rate_per_pound()) as u64 } pub fn convert_pounds_to_fc(&self, pounds: f64) -> u64 { - (pounds * self.gogurt_rate_per_pound) as u64 + (pounds * self.gogurt_rate_per_pound()) as u64 } pub fn convert_fc_to_pounds(&self, fc: u64) -> f64 { - fc as f64 / self.gogurt_rate_per_pound + fc as f64 / self.gogurt_rate_per_pound() + } + + pub fn gogurt_rate_per_pound(&self) -> f64 { + if let Some(rate_per_pound) = self.gogurt_rate_per_pound + && rate_per_pound.is_normal() + { + rate_per_pound + } else { + 0.01 + } } pub fn market_function(time: i64) -> f64 { @@ -115,7 +125,7 @@ impl GogurtReserves { (x.sin() + (PI as f64 * x).cos() + (PI as f64 * x).sin() + x.cos() - (x * 4.0).sin() - (x * 3.0).cos()) - .clamp(0.0, 6.0) + .clamp(0.01, 6.0) } pub fn calculate_new_rate(time: i64) -> f64 { @@ -130,7 +140,7 @@ impl GogurtReserves { let new_rate = Self::calculate_new_rate(time); info!("Updating rate to '{new_rate}'"); - reserves.gogurt_rate_per_pound = new_rate; + reserves.gogurt_rate_per_pound = Some(new_rate); db.insert(reserves)?;