Fixed gogurt rate sometimes being NaN

This commit is contained in:
Joey Hines 2025-08-05 18:27:59 -06:00
parent 1367e427ed
commit a97335c7e3
Signed by: joeyahines
GPG Key ID: 38BA6F25C94C9382
4 changed files with 24 additions and 12 deletions

2
Cargo.lock generated
View File

@ -1093,7 +1093,7 @@ dependencies = [
[[package]] [[package]]
name = "fren" name = "fren"
version = "2.2.0" version = "2.2.1"
dependencies = [ dependencies = [
"axum 0.8.1", "axum 0.8.1",
"base64 0.22.1", "base64 0.22.1",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "fren" name = "fren"
version = "2.2.0" version = "2.2.1"
edition = "2024" edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -12,8 +12,10 @@ pub async fn contribute_to_gogurt_reserve(
let gogurt_bought = let gogurt_bought =
GogurtReserves::add_contribution(&ctx.data().db, ctx.author().id, contribute_amount)?; GogurtReserves::add_contribution(&ctx.data().db, ctx.author().id, contribute_amount)?;
ctx.reply(format!("You have purchased {gogurt_bought}lbs of Gogurt")) ctx.reply(format!(
.await?; "You have purchased {gogurt_bought:.3}lbs of Gogurt"
))
.await?;
Ok(()) Ok(())
} }
@ -42,7 +44,7 @@ pub async fn gogurt_reserve_stats(ctx: Context<'_>) -> Result<(), Error> {
let contributors = gogurt_reserves.get_contributors(); let contributors = gogurt_reserves.get_contributors();
let total_amount = gogurt_reserves.get_total_amount(); let total_amount = gogurt_reserves.get_total_amount();
let total_worth = gogurt_reserves.get_total_worth(); 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(); let mut msg_builder = MessageBuilder::new();
@ -92,7 +94,7 @@ pub async fn gogurt_reserve_contribution(
let mention = user_id.mention(); let mention = user_id.mention();
if let Some(contribution) = contribution { if let Some(contribution) = contribution {
let worth = gogurt_reserves.convert_pounds_to_fc(*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?; 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 { } else {

View File

@ -12,7 +12,7 @@ use std::f32::consts::PI;
pub struct GogurtReserves { pub struct GogurtReserves {
id: Option<u64>, id: Option<u64>,
pub reserve_contributors: HashMap<UserId, f64>, pub reserve_contributors: HashMap<UserId, f64>,
pub gogurt_rate_per_pound: f64, pub gogurt_rate_per_pound: Option<f64>,
} }
impl j_db::model::JdbModel for GogurtReserves { impl j_db::model::JdbModel for GogurtReserves {
@ -99,15 +99,25 @@ impl GogurtReserves {
} }
pub fn get_total_worth(&self) -> u64 { 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 { 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 { 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 { 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.sin() + (PI as f64 * x).cos() + (PI as f64 * x).sin() + x.cos()
- (x * 4.0).sin() - (x * 4.0).sin()
- (x * 3.0).cos()) - (x * 3.0).cos())
.clamp(0.0, 6.0) .clamp(0.01, 6.0)
} }
pub fn calculate_new_rate(time: i64) -> f64 { pub fn calculate_new_rate(time: i64) -> f64 {
@ -130,7 +140,7 @@ impl GogurtReserves {
let new_rate = Self::calculate_new_rate(time); let new_rate = Self::calculate_new_rate(time);
info!("Updating rate to '{new_rate}'"); info!("Updating rate to '{new_rate}'");
reserves.gogurt_rate_per_pound = new_rate; reserves.gogurt_rate_per_pound = Some(new_rate);
db.insert(reserves)?; db.insert(reserves)?;