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]]
name = "fren"
version = "2.2.0"
version = "2.2.1"
dependencies = [
"axum 0.8.1",
"base64 0.22.1",

View File

@ -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

View File

@ -12,7 +12,9 @@ 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"))
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 {

View File

@ -12,7 +12,7 @@ use std::f32::consts::PI;
pub struct GogurtReserves {
id: Option<u64>,
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 {
@ -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)?;