Enhance user notification handling in EventsWorker: Updated the notification process to write user notifications to the database and send WebSocket alerts for each affected user in a region. Improved logging to reflect the number of users notified during regional event processing.
This commit is contained in:
@@ -541,7 +541,10 @@ impl EventsWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sende Benachrichtigung
|
// Schreibe Benachrichtigung in die Datenbank
|
||||||
|
Self::notify_user(pool, broker, user_id, &format!("random_event.{}", event.id))?;
|
||||||
|
|
||||||
|
// Sende Benachrichtigung über WebSocket
|
||||||
let notification = json!({
|
let notification = json!({
|
||||||
"event": "random_event",
|
"event": "random_event",
|
||||||
"event_id": event.id,
|
"event_id": event.id,
|
||||||
@@ -869,21 +872,50 @@ impl EventsWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sende Benachrichtigung an alle Spieler mit Branches in dieser Region
|
// Finde alle betroffenen User in dieser Region (User mit Branches)
|
||||||
|
const QUERY_GET_AFFECTED_USERS: &str = r#"
|
||||||
|
SELECT DISTINCT b.falukant_user_id AS user_id
|
||||||
|
FROM falukant_data.branch b
|
||||||
|
WHERE b.region_id = $1
|
||||||
|
AND b.falukant_user_id IS NOT NULL;
|
||||||
|
"#;
|
||||||
|
|
||||||
|
conn.prepare("get_affected_users", QUERY_GET_AFFECTED_USERS)?;
|
||||||
|
let user_rows = conn.execute("get_affected_users", &[®ion_id])?;
|
||||||
|
|
||||||
|
// Sende Benachrichtigung an jeden betroffenen User einzeln
|
||||||
|
let mut notified_users = 0;
|
||||||
|
for row in user_rows {
|
||||||
|
let user_id: Option<i32> = row
|
||||||
|
.get("user_id")
|
||||||
|
.and_then(|v| v.parse::<i32>().ok());
|
||||||
|
|
||||||
|
if let Some(uid) = user_id {
|
||||||
|
// Schreibe Benachrichtigung in die Datenbank
|
||||||
|
if let Err(err) = Self::notify_user(pool, broker, uid, &format!("random_event.{}", event.id)) {
|
||||||
|
eprintln!("[EventsWorker] Fehler beim Schreiben der Benachrichtigung für User {}: {}", uid, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sende Benachrichtigung über WebSocket
|
||||||
let notification = json!({
|
let notification = json!({
|
||||||
"event": "random_event",
|
"event": "random_event",
|
||||||
"event_id": event.id,
|
"event_id": event.id,
|
||||||
"event_type": "regional",
|
"event_type": "regional",
|
||||||
"region_id": region_id,
|
"region_id": region_id,
|
||||||
|
"user_id": uid,
|
||||||
"title": event.title,
|
"title": event.title,
|
||||||
"description": event.description,
|
"description": event.description,
|
||||||
"effects": effect_results
|
"effects": effect_results
|
||||||
});
|
});
|
||||||
|
|
||||||
broker.publish(notification.to_string());
|
broker.publish(notification.to_string());
|
||||||
|
notified_users += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"[EventsWorker] Regionales Ereignis '{}' für Region {} verarbeitet",
|
"[EventsWorker] Regionales Ereignis '{}' für Region {} verarbeitet, {} User benachrichtigt",
|
||||||
event.id, region_id
|
event.id, region_id, notified_users
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user