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.
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 1m52s
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 1m52s
This commit is contained in:
@@ -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<Option<serde_json::Value>, 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);
|
||||
|
||||
Reference in New Issue
Block a user