Implement notification system for character death events: Added a new method in UserCharacterWorker to notify users of character deaths, including directors, relationships, and children. Updated SQL queries to support the insertion of notifications with event types, enhancing user engagement and real-time updates.

This commit is contained in:
Torsten Schulz (local)
2026-03-22 09:48:16 +01:00
parent 4086e9a207
commit c209c41b52
2 changed files with 58 additions and 4 deletions

View File

@@ -464,7 +464,7 @@ impl CharacterCreationWorker {
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
conn.prepare("insert_notification", QUERY_INSERT_NOTIFICATION)?;
conn.execute("insert_notification", &[&user_id])?;
conn.execute("insert_notification", &[&user_id, &event_type])?;
// falukantUpdateStatus
let update_message =

View File

@@ -36,6 +36,7 @@ use crate::worker::sql::{
QUERY_GET_PREGNANCY_CANDIDATES,
QUERY_INSERT_CHILD,
QUERY_INSERT_CHILD_RELATION,
QUERY_INSERT_NOTIFICATION,
QUERY_DELETE_DIRECTOR,
QUERY_DELETE_RELATIONSHIP,
QUERY_DELETE_CHILD_RELATION,
@@ -609,6 +610,26 @@ impl UserCharacterWorker {
self.base.broker.publish(update_status);
}
/// Wie `EventsWorker::notify_user`: Eintrag in `falukant_log.notification` (`tr` = event_type) + WS.
fn notify_user_death(
&self,
conn: &mut crate::db::DbConnection,
user_id: i32,
event_type: &str,
) -> Result<(), DbError> {
if user_id <= 0 {
return Ok(());
}
conn.execute("insert_notification", &[&user_id, &event_type])?;
let update_message =
format!(r#"{{"event":"falukantUpdateStatus","user_id":{}}}"#, user_id);
self.base.broker.publish(update_message);
let message =
format!(r#"{{"event":"{event_type}","user_id":{}}}"#, user_id);
self.base.broker.publish(message);
Ok(())
}
// Todes- und Erb-Logik
fn handle_character_death(&mut self, character_id: i32) -> Result<(), DbError> {
self.set_heir(character_id)?;
@@ -633,11 +654,44 @@ impl UserCharacterWorker {
conn.prepare("delete_debtors_prism", QUERY_DELETE_DEBTORS_PRISM)?;
conn.prepare("delete_political_office", QUERY_DELETE_POLITICAL_OFFICE)?;
conn.prepare("delete_election_candidate", QUERY_DELETE_ELECTION_CANDIDATE)?;
conn.prepare("insert_notification", QUERY_INSERT_NOTIFICATION)?;
let dir_result = conn.execute("delete_director", &[&character_id])?;
for row in dir_result {
if let Some(user_id) = row
.get("employer_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
self.notify_user_death(&mut conn, user_id, "director_death")?;
}
}
let rel_result = conn.execute("delete_relationship", &[&character_id])?;
for row in rel_result {
if let Some(related_user_id) = row
.get("related_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
self.notify_user_death(&mut conn, related_user_id, "relationship_death")?;
}
}
conn.execute("delete_director", &[&character_id])?;
conn.execute("delete_relationship", &[&character_id])?;
conn.execute("delete_child_relation_by_parent", &[&character_id])?;
conn.execute("delete_child_relation", &[&character_id])?;
let child_result = conn.execute("delete_child_relation", &[&character_id])?;
for row in child_result {
if let Some(father_user_id) = row
.get("father_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
self.notify_user_death(&mut conn, father_user_id, "child_death")?;
}
if let Some(mother_user_id) = row
.get("mother_user_id")
.and_then(|v| v.parse::<i32>().ok())
{
self.notify_user_death(&mut conn, mother_user_id, "child_death")?;
}
}
conn.execute("delete_knowledge", &[&character_id])?;
conn.execute("delete_debtors_prism", &[&character_id])?;
conn.execute("delete_political_office", &[&character_id])?;