Enhance EventsWorker: Introduce EVENT_RATE_SCALE constant to adjust event frequency, improving control over event triggering rates.
This commit is contained in:
@@ -147,7 +147,6 @@ impl BaseWorker {
|
||||
"#;
|
||||
|
||||
conn.prepare("update_money", QUERY_UPDATE_MONEY)?;
|
||||
use postgres::types::ToSql;
|
||||
|
||||
// Validate float to avoid passing NaN/inf which the postgres client
|
||||
// may fail to serialize with an unclear error message.
|
||||
@@ -179,6 +178,13 @@ impl BaseWorker {
|
||||
// numeric(10,2) allows values with absolute < 10^8 (100_000_000)
|
||||
const MAX_ABS: f64 = 100_000_000.0 - 0.01; // leave room for scale
|
||||
|
||||
let allowed = MAX_ABS - current_money;
|
||||
|
||||
eprintln!(
|
||||
"[BaseWorker] money clamp debug: current_money={}, money_change={}, tentative={}, allowed_max_delta={}",
|
||||
current_money, money_change, tentative, allowed
|
||||
);
|
||||
|
||||
let adjusted_money_change = if tentative >= MAX_ABS {
|
||||
let clipped = MAX_ABS - current_money;
|
||||
eprintln!(
|
||||
@@ -197,6 +203,11 @@ impl BaseWorker {
|
||||
money_change
|
||||
};
|
||||
|
||||
eprintln!(
|
||||
"[BaseWorker] money clamp debug result: adjusted_money_change={}",
|
||||
adjusted_money_change
|
||||
);
|
||||
|
||||
// Send exact types matching the DB function signature:
|
||||
let uid_i32: i32 = falukant_user_id;
|
||||
let money_str = format!("{:.2}", adjusted_money_change);
|
||||
|
||||
@@ -83,6 +83,9 @@ struct StorageDamageInfo {
|
||||
}
|
||||
|
||||
impl EventsWorker {
|
||||
// Globaler Skalierungsfaktor für Ereignisfrequenz (1.0 = unverändert).
|
||||
// Setze auf 0.05, um Ereignisse auf 1/20 der ursprünglichen Häufigkeit zu reduzieren.
|
||||
const EVENT_RATE_SCALE: f64 = 0.05;
|
||||
pub fn new(pool: ConnectionPool, broker: MessageBroker) -> Self {
|
||||
Self {
|
||||
base: BaseWorker::new("EventsWorker", pool, broker),
|
||||
@@ -367,6 +370,10 @@ impl EventsWorker {
|
||||
}
|
||||
}
|
||||
|
||||
// Globaler Skalierungsfaktor für Ereignisfrequenz (1.0 = unverändert).
|
||||
// Setze auf 0.05, um Ereignisse auf 1/20 der ursprünglichen Häufigkeit zu reduzieren.
|
||||
const EVENT_RATE_SCALE: f64 = 0.05;
|
||||
|
||||
fn check_and_trigger_events_inner(
|
||||
pool: &ConnectionPool,
|
||||
broker: &MessageBroker,
|
||||
@@ -378,11 +385,13 @@ impl EventsWorker {
|
||||
for event in events {
|
||||
// Zufällige Prüfung basierend auf Wahrscheinlichkeit
|
||||
let roll = rng.gen_range(0.0..=1.0);
|
||||
if roll < event.probability_per_minute {
|
||||
let effective_prob = event.probability_per_minute * EVENT_RATE_SCALE;
|
||||
if roll < effective_prob {
|
||||
eprintln!(
|
||||
"[EventsWorker] Ereignis '{}' wurde ausgelöst (Wahrscheinlichkeit: {:.2}%)",
|
||||
"[EventsWorker] Ereignis '{}' wurde ausgelöst (Wahrscheinlichkeit: {:.4}% -> skaliert {:.4}%)",
|
||||
event.id,
|
||||
event.probability_per_minute * 100.0
|
||||
event.probability_per_minute * 100.0,
|
||||
effective_prob * 100.0
|
||||
);
|
||||
|
||||
match event.event_type {
|
||||
@@ -544,16 +553,26 @@ impl EventsWorker {
|
||||
}
|
||||
|
||||
// Schreibe Benachrichtigung in die Datenbank mit Event-Details
|
||||
let notification_json = serde_json::json!({
|
||||
// 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| {
|
||||
eff.get("character_id").and_then(|v| v.as_i64()).map(|n| n as i32)
|
||||
});
|
||||
|
||||
let mut notification_json = serde_json::json!({
|
||||
"tr": format!("random_event.{}", event.id),
|
||||
"event_id": event.id,
|
||||
"event_type": "personal",
|
||||
"effects": effect_results
|
||||
});
|
||||
|
||||
if let Some(cid) = top_character_id {
|
||||
notification_json["character_id"] = serde_json::json!(cid);
|
||||
}
|
||||
|
||||
Self::notify_user(pool, broker, user_id, ¬ification_json.to_string())?;
|
||||
|
||||
// Sende Benachrichtigung über WebSocket
|
||||
let notification = json!({
|
||||
let mut notification = json!({
|
||||
"event": "random_event",
|
||||
"event_id": event.id,
|
||||
"event_type": "personal",
|
||||
@@ -563,6 +582,10 @@ impl EventsWorker {
|
||||
"effects": effect_results
|
||||
});
|
||||
|
||||
if let Some(cid) = top_character_id {
|
||||
notification["character_id"] = json!(cid);
|
||||
}
|
||||
|
||||
broker.publish(notification.to_string());
|
||||
eprintln!(
|
||||
"[EventsWorker] Persönliches Ereignis '{}' für Spieler {} verarbeitet",
|
||||
|
||||
Reference in New Issue
Block a user