From 43eb6ed0ab5e26fdc780c0c0c50dd8fd5e5333b4 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 24 Apr 2026 10:00:45 +0200 Subject: [PATCH] Refactor event effect handling in EventsWorker: Improved logic for handling empty effect results to prevent unnecessary database writes. Enhanced error handling for retrieving current money, ensuring robust behavior when user data is unavailable. Simplified the response for insufficient funds scenarios in the "theft" event, returning `None` instead of a detailed JSON response. --- src/worker/events.rs | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/worker/events.rs b/src/worker/events.rs index 25240c8..55396ca 100644 --- a/src/worker/events.rs +++ b/src/worker/events.rs @@ -622,6 +622,11 @@ impl EventsWorker { } } + // Kein tatsächlich angewendeter Effekt -> keine persistente/WS-Notification. + if effect_results.is_empty() { + return Ok(()); + } + // Schreibe Benachrichtigung in die Datenbank mit Event-Details // If any effect contains a character_id, include it at top-level for the notification let top_character_id = effect_results.iter().find_map(|eff| { @@ -1307,20 +1312,19 @@ impl EventsWorker { rng: &mut impl Rng, ) -> Result, DbError> { if effect_roll >= probability { - if event.id == "theft" { - return Ok(Some(json!({ - "type": "money_change", - "applied": false, - "reason": "probability_miss", - "percent": 0.0, - "absolute": 0.0, - "loss": 0.0 - }))); - } return Ok(None); } - let current_money = Self::get_current_money(conn, user_id).unwrap_or(0.0); + let current_money = match Self::get_current_money(conn, user_id) { + Ok(m) => m, + Err(err) => { + eprintln!( + "[EventsWorker] Konnte Geldstand für user_id={} nicht laden ({}), überspringe money_change", + user_id, err + ); + return Ok(None); + } + }; // Spezialfall: Unerwarteter Geldsegen -> absoluter Zufallsbetrag 1..500 if event.id == "windfall" { @@ -1350,14 +1354,7 @@ impl EventsWorker { if event.id == "theft" { // Wenn kaum Geld vorhanden ist, passiert nichts if current_money <= 20.0 { - return Ok(Some(json!({ - "type": "money_change", - "applied": false, - "reason": "insufficient_funds", - "percent": 0.0, - "absolute": 0.0, - "loss": 0.0 - }))); + return Ok(None); } let max_by_percent = current_money * 0.9; @@ -1365,14 +1362,7 @@ impl EventsWorker { let max_loss = max_by_percent.min(max_by_min_left); if max_loss < 1.0 { - return Ok(Some(json!({ - "type": "money_change", - "applied": false, - "reason": "insufficient_funds", - "percent": 0.0, - "absolute": 0.0, - "loss": 0.0 - }))); + return Ok(None); } let loss: f64 = rng.gen_range(1.0..=max_loss);