Enhance event handling for money changes in EventsWorker: Introduced special cases for 'windfall' and 'theft' events, allowing for random monetary adjustments based on current user funds. Updated logic to ensure accurate percentage and absolute changes while maintaining existing functionality for standard money change events.
This commit is contained in:
@@ -523,11 +523,76 @@ impl EventsWorker {
|
||||
min_absolute_positive,
|
||||
} => {
|
||||
if effect_roll < *probability {
|
||||
let current_money =
|
||||
Self::get_current_money(&mut conn, user_id).unwrap_or(0.0);
|
||||
|
||||
// Spezialfall: Unerwarteter Geldsegen -> absoluter Zufallsbetrag 1..500
|
||||
if event.id == "windfall" {
|
||||
let absolute_change: f64 = rng.gen_range(1.0..=500.0);
|
||||
let percent_change = if current_money > 0.0 {
|
||||
(absolute_change / current_money) * 100.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
let applied_change = Self::apply_money_change(
|
||||
&mut conn,
|
||||
user_id,
|
||||
percent_change,
|
||||
Some(absolute_change),
|
||||
)
|
||||
.unwrap_or(absolute_change);
|
||||
|
||||
effect_results.push(json!({
|
||||
"type": "money_change",
|
||||
"percent": percent_change,
|
||||
"absolute": applied_change
|
||||
}));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Spezialfall: Diebstahl -> Verlust > 0, max. 90% und mind. 20 Geld übrig
|
||||
if event.id == "theft" {
|
||||
// Wenn kaum Geld vorhanden ist, passiert nichts
|
||||
if current_money <= 20.0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let max_by_percent = current_money * 0.9;
|
||||
let max_by_min_left = current_money - 20.0;
|
||||
let max_loss = max_by_percent.min(max_by_min_left);
|
||||
|
||||
if max_loss < 1.0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let loss: f64 = rng.gen_range(1.0..=max_loss);
|
||||
let percent_change = -(loss / current_money) * 100.0;
|
||||
let absolute_change = -loss;
|
||||
|
||||
let applied_change = Self::apply_money_change(
|
||||
&mut conn,
|
||||
user_id,
|
||||
percent_change,
|
||||
Some(absolute_change),
|
||||
)
|
||||
.unwrap_or(absolute_change);
|
||||
|
||||
effect_results.push(json!({
|
||||
"type": "money_change",
|
||||
"percent": percent_change,
|
||||
"absolute": applied_change
|
||||
}));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Standardfall: prozentuale Änderung, optional mit Mindestbetrag für positive Änderungen
|
||||
let percent_change = rng.gen_range(*min_percent..=*max_percent);
|
||||
let current_money = Self::get_current_money(&mut conn, user_id).unwrap_or(0.0);
|
||||
let computed_change = current_money * (percent_change / 100.0);
|
||||
let effective_change = match min_absolute_positive {
|
||||
Some(min_abs) if percent_change > 0.0 && computed_change < *min_abs => *min_abs,
|
||||
Some(min_abs) if percent_change > 0.0 && computed_change < *min_abs => {
|
||||
*min_abs
|
||||
}
|
||||
_ => computed_change,
|
||||
};
|
||||
let absolute_change = Self::apply_money_change(
|
||||
|
||||
Reference in New Issue
Block a user