Enhance production certificate logic and event handling: Updated the daily recalculation process in FalukantFamilyWorker to decouple it from family migration, ensuring consistent execution. Introduced a new mechanism to reset certificates upon succession without heirs, improving user experience. Refined SQL queries for better data retrieval and event publishing, including distinct character selection logic. Enhanced documentation for clarity on the updated processes and their implications.
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 2m55s
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 2m55s
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//! Produktionszertifikat: tägliche Neuberechnung von `falukant_user.certificate` im **FalukantFamilyWorker-Daily-Tick**
|
||||
//! (nicht in einem eigenen Worker-Thread). Spec: `docs/FALUKANT_PRODUCTION_CERTIFICATE.md` und
|
||||
//! Produktionszertifikat: tägliche Neuberechnung von `falukant_user.certificate` im **FalukantFamilyWorker**-24h-Tick
|
||||
//! (`run_iteration`, unabhängig von der Familien-Schema-Migration; nicht in einem eigenen Worker-Thread). Spec: `docs/FALUKANT_PRODUCTION_CERTIFICATE.md` und
|
||||
//! „Falukant: Produktionszertifikate – Fach- und Integrationsspezifikation“.
|
||||
|
||||
use crate::db::{DbError, Row};
|
||||
@@ -7,7 +7,8 @@ use crate::message_broker::MessageBroker;
|
||||
|
||||
use super::base::BaseWorker;
|
||||
use crate::worker::sql::{
|
||||
QUERY_GET_PRODUCTION_CERTIFICATE_INPUT_ROWS, QUERY_UPDATE_FALUKANT_USER_CERTIFICATE,
|
||||
QUERY_GET_FALUKANT_USER_CERT_AND_EVENT, QUERY_GET_PRODUCTION_CERTIFICATE_INPUT_ROWS,
|
||||
QUERY_UPDATE_FALUKANT_USER_CERTIFICATE,
|
||||
};
|
||||
|
||||
/// Wenn `money` darunter liegt, gilt der Spieler als bankrott → Zertifikat auf Stufe 1 (Spec §4.7).
|
||||
@@ -48,7 +49,7 @@ pub fn run_daily(base: &BaseWorker, broker: &MessageBroker) -> Result<(), DbErro
|
||||
// Bankrott: Herabsetzung (Spec §4.7)
|
||||
if money <= BANKRUPTCY_MONEY_THRESHOLD && current > 1 {
|
||||
conn.execute("cert_upd", &[&1_i32, &fu_id])?;
|
||||
publish_certificate_event(broker, event_uid, current, 1);
|
||||
publish_certificate_event(broker, event_uid, current, 1, "bankruptcy");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -88,17 +89,59 @@ pub fn run_daily(base: &BaseWorker, broker: &MessageBroker) -> Result<(), DbErro
|
||||
|
||||
if new_certificate != current {
|
||||
conn.execute("cert_upd", &[&new_certificate, &fu_id])?;
|
||||
publish_certificate_event(broker, event_uid, current, new_certificate);
|
||||
publish_certificate_event(
|
||||
broker,
|
||||
event_uid,
|
||||
current,
|
||||
new_certificate,
|
||||
"daily_recalculation",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn publish_certificate_event(broker: &MessageBroker, user_id: i32, old_c: i32, new_c: i32) {
|
||||
/// Nach Tod ohne gültigen Erben: Zertifikat auf Stufe 1 (Spec: Erbfolge bricht ab).
|
||||
pub fn reset_certificate_on_succession_no_heir(
|
||||
base: &BaseWorker,
|
||||
broker: &MessageBroker,
|
||||
falukant_user_id: i32,
|
||||
) -> Result<(), DbError> {
|
||||
if falukant_user_id < 1 {
|
||||
return Ok(());
|
||||
}
|
||||
let pool = &base.pool;
|
||||
let mut conn = pool
|
||||
.get()
|
||||
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
|
||||
conn.prepare("cert_get_ev", QUERY_GET_FALUKANT_USER_CERT_AND_EVENT)?;
|
||||
let rows = conn.execute("cert_get_ev", &[&falukant_user_id])?;
|
||||
let row = match rows.first() {
|
||||
Some(r) => r,
|
||||
None => return Ok(()),
|
||||
};
|
||||
let current = parse_i32(row, "certificate", 1).clamp(1, 5);
|
||||
let event_uid = parse_i32(row, "event_user_id", falukant_user_id);
|
||||
if current <= 1 {
|
||||
return Ok(());
|
||||
}
|
||||
conn.prepare("cert_upd", QUERY_UPDATE_FALUKANT_USER_CERTIFICATE)?;
|
||||
conn.execute("cert_upd", &[&1_i32, &falukant_user_id])?;
|
||||
publish_certificate_event(broker, event_uid, current, 1, "succession_no_heir");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn publish_certificate_event(
|
||||
broker: &MessageBroker,
|
||||
user_id: i32,
|
||||
old_c: i32,
|
||||
new_c: i32,
|
||||
reason: &str,
|
||||
) {
|
||||
let msg = format!(
|
||||
r#"{{"event":"falukantUpdateProductionCertificate","user_id":{},"reason":"daily_recalculation","old_certificate":{},"new_certificate":{}}}"#,
|
||||
user_id, old_c, new_c
|
||||
r#"{{"event":"falukantUpdateProductionCertificate","user_id":{},"reason":"{}","old_certificate":{},"new_certificate":{}}}"#,
|
||||
user_id, reason, old_c, new_c
|
||||
);
|
||||
broker.publish(msg);
|
||||
let status = format!(r#"{{"event":"falukantUpdateStatus","user_id":{}}}"#, user_id);
|
||||
|
||||
Reference in New Issue
Block a user