Enhance relationship deletion process in character management: Implement detailed logging for deleted relationships, including related user and character IDs, and relationship types. Introduce special notifications for engaged relationships to improve user awareness during character events.

This commit is contained in:
Torsten Schulz (local)
2026-01-22 09:35:21 +01:00
parent 70d8e53216
commit 7305a71438
4 changed files with 114 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ use crate::message_broker::MessageBroker;
use rand::distributions::{Distribution, Uniform}; use rand::distributions::{Distribution, Uniform};
use rand::rngs::StdRng; use rand::rngs::StdRng;
use rand::{thread_rng, Rng, SeedableRng}; use rand::{thread_rng, Rng, SeedableRng};
use serde_json::json;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
@@ -418,11 +419,38 @@ impl CharacterCreationWorker {
conn.prepare("delete_relationship", QUERY_DELETE_RELATIONSHIP)?; conn.prepare("delete_relationship", QUERY_DELETE_RELATIONSHIP)?;
let rel_result = conn.execute("delete_relationship", &[&character_id])?; let rel_result = conn.execute("delete_relationship", &[&character_id])?;
for row in rel_result { for row in rel_result {
if let Some(related_user_id) = row let related_user_id = row
.get("related_user_id") .get("related_user_id")
.and_then(|v| v.parse::<i32>().ok()) .and_then(|v| v.parse::<i32>().ok());
{ let related_character_id = row
Self::notify_user(pool, broker, related_user_id, "relationship_death")?; .get("related_character_id")
.and_then(|v| v.parse::<i32>().ok());
let relationship_type_tr = row
.get("relationship_type_tr")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
// Logging: Relationship wurde gelöscht
eprintln!(
"[CharacterCreationWorker] Relationship gelöscht: character_id={}, related_character_id={}, related_user_id={:?}, relationship_type={:?}",
character_id,
related_character_id.unwrap_or(-1),
related_user_id,
relationship_type_tr
);
if let Some(uid) = related_user_id {
// Spezielle Notification für Verlobungen
if relationship_type_tr.as_deref() == Some("engaged") {
use crate::worker::insert_notification;
let notification_json = serde_json::json!({
"tr": "relationship.engaged_character_death",
"character_id": related_character_id
});
insert_notification(pool, uid, &notification_json.to_string(), related_character_id)?;
} else {
Self::notify_user(pool, broker, uid, "relationship_death")?;
}
} }
} }

View File

@@ -1779,11 +1779,37 @@ impl EventsWorker {
conn.prepare("delete_relationship", QUERY_DELETE_RELATIONSHIP)?; conn.prepare("delete_relationship", QUERY_DELETE_RELATIONSHIP)?;
let rel_result = conn.execute("delete_relationship", &[&character_id])?; let rel_result = conn.execute("delete_relationship", &[&character_id])?;
for row in rel_result { for row in rel_result {
if let Some(related_user_id) = row let related_user_id = row
.get("related_user_id") .get("related_user_id")
.and_then(|v| v.parse::<i32>().ok()) .and_then(|v| v.parse::<i32>().ok());
{ let related_character_id = row
Self::notify_user(pool, broker, related_user_id, "relationship_death", None)?; .get("related_character_id")
.and_then(|v| v.parse::<i32>().ok());
let relationship_type_tr = row
.get("relationship_type_tr")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
// Logging: Relationship wurde gelöscht
eprintln!(
"[EventsWorker] Relationship gelöscht: character_id={}, related_character_id={}, related_user_id={:?}, relationship_type={:?}",
character_id,
related_character_id.unwrap_or(-1),
related_user_id,
relationship_type_tr
);
if let Some(uid) = related_user_id {
// Spezielle Notification für Verlobungen
if relationship_type_tr.as_deref() == Some("engaged") {
let notification_json = serde_json::json!({
"tr": "relationship.engaged_character_death",
"character_id": related_character_id
});
Self::notify_user(pool, broker, uid, &notification_json.to_string(), related_character_id)?;
} else {
Self::notify_user(pool, broker, uid, "relationship_death", None)?;
}
} }
} }

View File

@@ -726,9 +726,18 @@ pub const QUERY_DELETE_RELATIONSHIP: &str = r#"
WITH deleted AS ( WITH deleted AS (
DELETE FROM falukant_data.relationship DELETE FROM falukant_data.relationship
WHERE character1_id = $1 OR character2_id = $1 WHERE character1_id = $1 OR character2_id = $1
RETURNING CASE WHEN character1_id = $1 THEN character2_id ELSE character1_id END AS related_character_id, relationship_type_id RETURNING
CASE WHEN character1_id = $1 THEN character2_id ELSE character1_id END AS related_character_id,
relationship_type_id
) )
SELECT c.user_id AS related_user_id FROM deleted d JOIN falukant_data.character c ON c.id = d.related_character_id; SELECT
c.user_id AS related_user_id,
d.related_character_id,
d.relationship_type_id,
rt.tr AS relationship_type_tr
FROM deleted d
JOIN falukant_data.character c ON c.id = d.related_character_id
LEFT JOIN falukant_type.relationship rt ON rt.id = d.relationship_type_id;
"#; "#;
pub const QUERY_GET_USER_ID: &str = r#" pub const QUERY_GET_USER_ID: &str = r#"

View File

@@ -3,6 +3,7 @@ use crate::message_broker::MessageBroker;
use rand::distributions::{Distribution, Uniform}; use rand::distributions::{Distribution, Uniform};
use rand::rngs::StdRng; use rand::rngs::StdRng;
use rand::SeedableRng; use rand::SeedableRng;
use serde_json::json;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@@ -578,7 +579,46 @@ impl UserCharacterWorker {
conn.prepare("delete_election_candidate", QUERY_DELETE_ELECTION_CANDIDATE)?; conn.prepare("delete_election_candidate", QUERY_DELETE_ELECTION_CANDIDATE)?;
conn.execute("delete_director", &[&character_id])?; conn.execute("delete_director", &[&character_id])?;
conn.execute("delete_relationship", &[&character_id])?;
// Relationships löschen mit Logging und spezieller Notification für Verlobungen
let rel_result = conn.execute("delete_relationship", &[&character_id])?;
for row in rel_result {
let related_user_id = row
.get("related_user_id")
.and_then(|v| v.parse::<i32>().ok());
let related_character_id = row
.get("related_character_id")
.and_then(|v| v.parse::<i32>().ok());
let relationship_type_tr = row
.get("relationship_type_tr")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
// Logging: Relationship wurde gelöscht
eprintln!(
"[UserCharacterWorker] Relationship gelöscht: character_id={}, related_character_id={}, related_user_id={:?}, relationship_type={:?}",
character_id,
related_character_id.unwrap_or(-1),
related_user_id,
relationship_type_tr
);
if let Some(uid) = related_user_id {
use crate::worker::insert_notification;
// Spezielle Notification für Verlobungen
if relationship_type_tr.as_deref() == Some("engaged") {
let notification_json = serde_json::json!({
"tr": "relationship.engaged_character_death",
"character_id": related_character_id
});
insert_notification(&self.base.pool, uid, &notification_json.to_string(), related_character_id)?;
} else {
use crate::worker::insert_notification;
insert_notification(&self.base.pool, uid, "relationship_death", None)?;
}
}
}
conn.execute("delete_child_relation", &[&character_id])?; conn.execute("delete_child_relation", &[&character_id])?;
conn.execute("delete_knowledge", &[&character_id])?; conn.execute("delete_knowledge", &[&character_id])?;
conn.execute("delete_debtors_prism", &[&character_id])?; conn.execute("delete_debtors_prism", &[&character_id])?;