Enhance event effect handling in EventsWorker: Added logic to store detailed notes for price and production quality changes in notifications. Updated notification JSON structure to include these notes, improving the clarity of event effects communicated to users. Additionally, refined money change logic for the "theft" event to handle insufficient funds scenarios more effectively.
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 1m35s

This commit is contained in:
Torsten Schulz (local)
2026-04-14 11:39:03 +02:00
parent b0ee0da722
commit 48d8e9b293

View File

@@ -644,7 +644,8 @@ impl EventsWorker {
"user_id": user_id,
"title": event.title,
"description": event.description,
"effects": effect_results
"effects": effect_results,
"stored": notification_json.clone()
});
if let Some(cid) = top_character_id {
@@ -737,7 +738,8 @@ impl EventsWorker {
"character_id": character_id,
"title": event.title,
"description": event.description,
"effects": effect_results
"effects": effect_results,
"stored": notification_json
});
broker.publish(notification.to_string());
}
@@ -778,6 +780,8 @@ impl EventsWorker {
// Wende Effekte an
let mut effect_results = Vec::new();
let mut price_change_note: Option<serde_json::Value> = None;
let mut production_quality_note: Option<serde_json::Value> = None;
for effect in &event.effects {
let effect_roll = rng.gen_range(0.0..=1.0);
match effect {
@@ -801,6 +805,20 @@ impl EventsWorker {
"type": "production_quality_change",
"change": change
}));
production_quality_note = Some(json!({
"configured": true,
"applied": true,
"change": change,
"probability": probability,
"range": [*min_change, *max_change]
}));
} else {
production_quality_note = Some(json!({
"configured": true,
"applied": false,
"probability": probability,
"range": [*min_change, *max_change]
}));
}
}
EventEffect::PriceChange {
@@ -815,6 +833,20 @@ impl EventsWorker {
"type": "price_change",
"percent": percent_change
}));
price_change_note = Some(json!({
"configured": true,
"applied": true,
"percent": percent_change,
"probability": probability,
"range": [*min_percent, *max_percent]
}));
} else {
price_change_note = Some(json!({
"configured": true,
"applied": false,
"probability": probability,
"range": [*min_percent, *max_percent]
}));
}
}
EventEffect::TransportSpeedChange {
@@ -951,13 +983,19 @@ impl EventsWorker {
if let Some(uid) = user_id {
// Schreibe Benachrichtigung in die Datenbank mit Event-Details
let notification_json = serde_json::json!({
let mut notification_json = serde_json::json!({
"tr": format!("random_event.{}", event.id),
"event_id": event.id,
"event_type": "regional",
"region_id": region_id,
"effects": effect_results
});
if let Some(ref p) = price_change_note {
notification_json["price_change"] = p.clone();
}
if let Some(ref q) = production_quality_note {
notification_json["production_quality"] = q.clone();
}
if let Err(err) = Self::notify_user(pool, broker, uid, &notification_json.to_string()) {
eprintln!("[EventsWorker] Fehler beim Schreiben der Benachrichtigung für User {}: {}", uid, err);
}
@@ -971,7 +1009,8 @@ impl EventsWorker {
"user_id": uid,
"title": event.title,
"description": event.description,
"effects": effect_results
"effects": effect_results,
"stored": notification_json
});
broker.publish(notification.to_string());
@@ -1263,6 +1302,16 @@ 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);
}
@@ -1296,7 +1345,14 @@ impl EventsWorker {
if event.id == "theft" {
// Wenn kaum Geld vorhanden ist, passiert nichts
if current_money <= 20.0 {
return Ok(None);
return Ok(Some(json!({
"type": "money_change",
"applied": false,
"reason": "insufficient_funds",
"percent": 0.0,
"absolute": 0.0,
"loss": 0.0
})));
}
let max_by_percent = current_money * 0.9;
@@ -1304,7 +1360,14 @@ impl EventsWorker {
let max_loss = max_by_percent.min(max_by_min_left);
if max_loss < 1.0 {
return Ok(None);
return Ok(Some(json!({
"type": "money_change",
"applied": false,
"reason": "insufficient_funds",
"percent": 0.0,
"absolute": 0.0,
"loss": 0.0
})));
}
let loss: f64 = rng.gen_range(1.0..=max_loss);
@@ -1325,6 +1388,7 @@ impl EventsWorker {
return Ok(Some(json!({
"type": "money_change",
"applied": true,
"percent": percent_change,
"absolute": applied_change,
"loss": loss_display