Add random heir fallback logic in EventsWorker: Implement a new SQL query to select a random NPC heir from the same region when no children are available. Enhance error handling to log cases where no heir is found, ensuring better user feedback and maintaining character associations. Update UserCharacterWorker to utilize this new logic for heir assignment.

This commit is contained in:
Torsten Schulz (local)
2026-01-07 10:14:24 +01:00
parent b7eafe28a1
commit f96e474f32
3 changed files with 76 additions and 8 deletions

View File

@@ -28,6 +28,7 @@ use crate::worker::sql::{
QUERY_COUNT_CHILDREN,
QUERY_GET_HEIR,
QUERY_RANDOM_HEIR,
QUERY_GET_RANDOM_HEIR_FROM_REGION,
QUERY_SET_CHARACTER_USER,
QUERY_CLEAR_CHARACTER_USER,
QUERY_UPDATE_USER_MONEY,
@@ -608,6 +609,12 @@ impl UserCharacterWorker {
new_money = self.calculate_new_money(falukant_user_id, heir_id > 0)?;
}
// Wenn es gar keine Kinder gibt, nimm einen zufälligen NPC in der Region (Alter 1014 Tage).
if heir_id < 1 {
heir_id = self.get_random_heir_from_region(character_id)?;
new_money = self.calculate_new_money(falukant_user_id, heir_id > 0)?;
}
if heir_id > 0 {
// Erst die alte Zuordnung lösen (Unique-Constraint safety), dann den Erben zuweisen.
self.clear_character_user(character_id)?;
@@ -669,6 +676,23 @@ impl UserCharacterWorker {
.unwrap_or(-1))
}
fn get_random_heir_from_region(&mut self, deceased_character_id: i32) -> Result<i32, DbError> {
let mut conn = self
.base
.pool
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
conn.prepare("random_heir_region", QUERY_GET_RANDOM_HEIR_FROM_REGION)?;
let rows = conn.execute("random_heir_region", &[&deceased_character_id])?;
Ok(rows
.first()
.and_then(|r| r.get("child_character_id"))
.and_then(|v| v.parse::<i32>().ok())
.unwrap_or(-1))
}
fn set_new_character(
&mut self,
falukant_user_id: i32,