From 85b4c73c1125891b73ebe040242f6f9d876f4585 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Mon, 2 Feb 2026 15:29:53 +0100 Subject: [PATCH] Update user money handling to prevent serialization errors: Adjusted the SQL query to cast money as numeric and modified the UserCharacterWorker to clamp and format the money value before executing the update. This change ensures valid numeric input and enhances error handling for user transactions. --- YpDaemon/src/worker/sql.rs | 2 +- YpDaemon/src/worker/user_character.rs | 10 +++++++++- src/worker/sql.rs | 4 +++- src/worker/user_character.rs | 11 ++++++++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/YpDaemon/src/worker/sql.rs b/YpDaemon/src/worker/sql.rs index 923ba49..0fdab0f 100644 --- a/YpDaemon/src/worker/sql.rs +++ b/YpDaemon/src/worker/sql.rs @@ -1171,7 +1171,7 @@ pub const QUERY_RANDOM_HEIR: &str = r#" pub const QUERY_UPDATE_USER_MONEY: &str = r#" UPDATE falukant_data.falukant_user - SET money = $1, + SET money = $1::numeric, updated_at = NOW() WHERE id = $2; "#; diff --git a/YpDaemon/src/worker/user_character.rs b/YpDaemon/src/worker/user_character.rs index a4c7d0d..01428d1 100644 --- a/YpDaemon/src/worker/user_character.rs +++ b/YpDaemon/src/worker/user_character.rs @@ -698,8 +698,16 @@ impl UserCharacterWorker { .get() .map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?; + let clamped = if !new_amount.is_finite() { + 1000.0 + } else { + const MAX_ABS: f64 = 99_999_999.99; + new_amount.clamp(-MAX_ABS, MAX_ABS) + }; + let money_str = format!("{:.2}", clamped); + conn.prepare("update_user_money", QUERY_UPDATE_USER_MONEY)?; - conn.execute("update_user_money", &[&new_amount, &falukant_user_id])?; + conn.execute("update_user_money", &[&money_str, &falukant_user_id])?; Ok(()) } diff --git a/src/worker/sql.rs b/src/worker/sql.rs index 348fcb8..afb5786 100644 --- a/src/worker/sql.rs +++ b/src/worker/sql.rs @@ -1168,9 +1168,11 @@ pub const QUERY_RANDOM_HEIR: &str = r#" chosen.child_character_id; "#; +/// Aktualisiert den Geldstand eines Users. $1 = Geld als Text (z. B. "1234.56"), wird als numeric gecastet; $2 = user id. +/// Verwendung von Text-Parameter vermeidet "error serializing parameter 0" bei f64/numeric. pub const QUERY_UPDATE_USER_MONEY: &str = r#" UPDATE falukant_data.falukant_user - SET money = $1, + SET money = $1::numeric, updated_at = NOW() WHERE id = $2; "#; diff --git a/src/worker/user_character.rs b/src/worker/user_character.rs index 7a0b4a0..1c20f2d 100644 --- a/src/worker/user_character.rs +++ b/src/worker/user_character.rs @@ -747,8 +747,17 @@ impl UserCharacterWorker { .get() .map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?; + // Endliche Werte und gültiger Bereich für numeric(10,2), sonst Serialisierungsfehler + let clamped = if !new_amount.is_finite() { + 1000.0 + } else { + const MAX_ABS: f64 = 99_999_999.99; + new_amount.clamp(-MAX_ABS, MAX_ABS) + }; + let money_str = format!("{:.2}", clamped); + conn.prepare("update_user_money", QUERY_UPDATE_USER_MONEY)?; - conn.execute("update_user_money", &[&new_amount, &falukant_user_id])?; + conn.execute("update_user_money", &[&money_str, &falukant_user_id])?; Ok(()) }