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

@@ -10,6 +10,7 @@ use std::thread;
use std::time::Duration;
use super::base::{BaseWorker, Worker, WorkerState};
use super::death_log;
use crate::worker::sql::{
QUERY_IS_PREVIOUS_DAY_CHARACTER_CREATED,
QUERY_GET_TOWN_REGION_IDS,
@@ -404,6 +405,14 @@ impl CharacterCreationWorker {
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
let death_ctx = match death_log::build_deceased_context(&mut conn, character_id) {
Ok(c) => Some(c),
Err(e) => {
eprintln!("[CharacterCreationWorker] 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])?;
@@ -412,7 +421,15 @@ impl CharacterCreationWorker {
.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"),
)?;
}
// 2) Relationships löschen und betroffene User benachrichtigen
@@ -423,7 +440,15 @@ impl CharacterCreationWorker {
.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"),
)?;
}
}
@@ -437,13 +462,29 @@ impl CharacterCreationWorker {
.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"),
)?;
}
}
@@ -457,24 +498,27 @@ impl CharacterCreationWorker {
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(())
}