Füge UndergroundWorker hinzu und implementiere Logik für unterirdische Aufgaben. Aktualisiere CMakeLists.txt, um neue Quell- und Header-Dateien einzuschließen. Verbessere die Fehlerbehandlung in der Datenbank und sende Benachrichtigungen nach bestimmten Ereignissen. Integriere Hilfsfunktionen zur sicheren Verarbeitung von Daten.

This commit is contained in:
Torsten Schulz (local)
2025-08-31 23:11:09 +02:00
committed by Torsten (PC)
parent 1451225978
commit 23c07a3570
18 changed files with 1255 additions and 88 deletions

View File

@@ -5,6 +5,7 @@
#include <thread>
#include <vector>
#include <cmath>
#include "utils.h"
UserCharacterWorker::UserCharacterWorker(ConnectionPool &pool, MessageBroker &broker)
: Worker(pool, broker, "UserCharacterWorker"),
@@ -17,8 +18,11 @@ void UserCharacterWorker::run() {
auto lastExecutionTime = steady_clock::now();
int lastPregnancyDay = -1;
while (runningWorker) {
signalActivity();
// 1h-Block
auto nowSteady = steady_clock::now();
auto elapsed = duration_cast<seconds>(nowSteady - lastExecutionTime).count();
if (elapsed >= 3600) {
@@ -31,20 +35,22 @@ void UserCharacterWorker::run() {
}
lastExecutionTime = nowSteady;
}
{
auto nowSys = system_clock::now();
std::time_t t = system_clock::to_time_t(nowSys);
std::tm local_tm;
localtime_r(&t, &local_tm);
if (local_tm.tm_hour == 6 && local_tm.tm_yday != lastPregnancyDay) {
try {
processPregnancies();
} catch (const std::exception &e) {
std::cerr << "[UserCharacterWorker] Fehler in processPregnancies: " << e.what() << std::endl;
}
lastPregnancyDay = local_tm.tm_yday;
// Schwangerschaftsverarbeitung: initial oder täglich um 06:00 einmal pro Tag
auto nowSys = system_clock::now();
std::time_t t = system_clock::to_time_t(nowSys);
std::tm local_tm;
localtime_r(&t, &local_tm);
if (lastPregnancyDay == -1 || (local_tm.tm_hour == 6 && local_tm.tm_yday != lastPregnancyDay)) {
try {
processPregnancies();
} catch (const std::exception &e) {
std::cerr << "[UserCharacterWorker] Fehler in processPregnancies: " << e.what() << std::endl;
}
lastPregnancyDay = local_tm.tm_yday;
}
std::this_thread::sleep_for(seconds(1));
recalculateKnowledge();
}
@@ -225,22 +231,34 @@ void UserCharacterWorker::recalculateKnowledge() {
void UserCharacterWorker::processPregnancies() {
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
db.prepare("QUERY_AUTOBATISM", QUERY_AUTOBATISM);
db.execute("QUERY_AUTOBATISM");
db.prepare("get_candidates", QUERY_GET_PREGNANCY_CANDIDATES);
auto rows = db.execute("get_candidates");
const nlohmann::json message = {
{"event", "children_update"},
};
for (auto const &row : rows) {
int fatherCid = std::stoi(row.at("father_cid"));
int motherCid = std::stoi(row.at("mother_cid"));
int fatherUid = std::stoi(row.at("father_uid"));
int motherUid = std::stoi(row.at("mother_uid"));
int titleOfNobility = std::stoi(row.at("title_of_nobility"));
int lastName = std::stoi(row.at("last_name"));
int regionId = std::stoi(row.at("region_id"));
for (const auto &row : rows) {
int fatherCid = Utils::optionalStoiOrDefault(row, "father_cid", -1);
int motherCid = Utils::optionalStoiOrDefault(row, "mother_cid", -1);
if (fatherCid < 0 || motherCid < 0) {
continue; // ungültige Daten überspringen
}
int titleOfNobility = Utils::optionalStoiOrDefault(row, "title_of_nobility", 0);
int lastName = Utils::optionalStoiOrDefault(row, "last_name", 0);
int regionId = Utils::optionalStoiOrDefault(row, "region_id", 0);
auto fatherUidOpt = Utils::optionalUid(row.at("father_uid"));
auto motherUidOpt = Utils::optionalUid(row.at("mother_uid"));
// Geschlecht zufällig
std::string gender = (dist(gen) < 0.5) ? "male" : "female";
db.prepare("insert_child", QUERY_INSERT_CHILD);
auto resChild = db.execute("insert_child", {
std::to_string(regionId), // $1
@@ -248,16 +266,30 @@ void UserCharacterWorker::processPregnancies() {
std::to_string(lastName), // $3
std::to_string(titleOfNobility) // $4
});
int childCid = std::stoi(resChild.front().at("child_cid"));
if (resChild.empty()) continue;
int childCid = Utils::optionalStoiOrDefault(resChild.front(), "child_cid", -1);
if (childCid < 0) continue;
db.prepare("insert_relation", QUERY_INSERT_CHILD_RELATION);
auto resRel = db.execute("insert_relation", {
db.execute("insert_relation", {
std::to_string(fatherCid),
std::to_string(motherCid),
std::to_string(childCid)
});
const nlohmann::json message = {{"event", "children_update"}};
sendMessageToFalukantUsers(fatherUid, message);
sendMessageToFalukantUsers(motherUid, message);
if (fatherUidOpt) {
sendMessageToFalukantUsers(*fatherUidOpt, message);
// Sende falukantUpdateStatus nach dem Erstellen des Kindes
nlohmann::json updateMessage = { { "event", "falukantUpdateStatus" } };
sendMessageToFalukantUsers(*fatherUidOpt, updateMessage);
}
if (motherUidOpt) {
sendMessageToFalukantUsers(*motherUidOpt, message);
// Sende falukantUpdateStatus nach dem Erstellen des Kindes
nlohmann::json updateMessage = { { "event", "falukantUpdateStatus" } };
sendMessageToFalukantUsers(*motherUidOpt, updateMessage);
}
}
}