Integrate death log functionality into character and event processing: Added death context handling in CharacterCreationWorker and EventsWorker to enhance user notifications related to character deaths. Updated SQL queries for retrieving deceased context and modified notification logic to support detailed death notifications. Enhanced user notification methods to accommodate both short event names and JSON payloads for improved clarity in messaging.

This commit is contained in:
Torsten Schulz (local)
2026-03-30 10:11:55 +02:00
parent 1013af594d
commit ac059f688d
7 changed files with 336 additions and 30 deletions

View File

@@ -8,6 +8,7 @@ use std::sync::Arc;
use std::time::{Duration, Instant};
use super::base::{BaseWorker, Worker, WorkerState};
use super::death_log;
use crate::worker::sql::{
QUERY_GET_RANDOM_USER,
QUERY_GET_RANDOM_INFANT,
@@ -1751,6 +1752,15 @@ impl EventsWorker {
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
// Todes-Kontext (Namen, Ort, Alter, Ehe/Kind/Liebschaft) vor Löschen der Beziehungen
let death_ctx = match death_log::build_deceased_context(&mut conn, character_id) {
Ok(c) => Some(c),
Err(e) => {
eprintln!("[EventsWorker] Todes-Log Kontext: {e}");
None
}
};
// 1) Director löschen und User benachrichtigen
conn.prepare("delete_director", QUERY_DELETE_DIRECTOR)?;
let dir_result = conn.execute("delete_director", &[&character_id])?;
@@ -1759,7 +1769,15 @@ impl EventsWorker {
.get("employer_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
Self::notify_user(pool, broker, user_id, "director_death")?;
let tr = death_ctx.as_ref().map(|c| {
death_log::wrap_death_notification(c, "director_death", user_id)
});
Self::notify_user(
pool,
broker,
user_id,
tr.as_deref().unwrap_or("director_death"),
)?;
}
}
@@ -1771,7 +1789,15 @@ impl EventsWorker {
.get("related_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
Self::notify_user(pool, broker, related_user_id, "relationship_death")?;
let tr = death_ctx.as_ref().map(|c| {
death_log::wrap_death_notification(c, "relationship_death", related_user_id)
});
Self::notify_user(
pool,
broker,
related_user_id,
tr.as_deref().unwrap_or("relationship_death"),
)?;
}
}
@@ -1801,13 +1827,29 @@ impl EventsWorker {
.get("father_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
Self::notify_user(pool, broker, father_user_id, "child_death")?;
let tr = death_ctx.as_ref().map(|c| {
death_log::wrap_death_notification(c, "child_death", father_user_id)
});
Self::notify_user(
pool,
broker,
father_user_id,
tr.as_deref().unwrap_or("child_death"),
)?;
}
if let Some(mother_user_id) = row
.get("mother_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
Self::notify_user(pool, broker, mother_user_id, "child_death")?;
let tr = death_ctx.as_ref().map(|c| {
death_log::wrap_death_notification(c, "child_death", mother_user_id)
});
Self::notify_user(
pool,
broker,
mother_user_id,
tr.as_deref().unwrap_or("child_death"),
)?;
}
}
@@ -1937,28 +1979,32 @@ impl EventsWorker {
Ok(())
}
/// `tr_payload`: kurzer `event`-Name oder JSON-Objekt (Todes-Logs mit `deceased`/`linked`).
fn notify_user(
pool: &ConnectionPool,
broker: &MessageBroker,
user_id: i32,
event_type: &str,
tr_payload: &str,
) -> Result<(), DbError> {
let mut conn = pool
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
conn.prepare("insert_notification", QUERY_INSERT_NOTIFICATION)?;
conn.execute("insert_notification", &[&user_id, &event_type])?;
conn.execute("insert_notification", &[&user_id, &tr_payload])?;
// falukantUpdateStatus
let update_message =
format!(r#"{{"event":"falukantUpdateStatus","user_id":{}}}"#, user_id);
broker.publish(update_message);
// ursprüngliche Benachrichtigung
let message =
format!(r#"{{"event":"{event_type}","user_id":{}}}"#, user_id);
broker.publish(message);
if tr_payload.trim_start().starts_with('{') {
broker.publish(tr_payload.to_string());
} else {
let message =
format!(r#"{{"event":"{tr_payload}","user_id":{user_id}}}"#);
broker.publish(message);
}
Ok(())
}