This commit is contained in:
Torsten (PC)
2026-01-14 14:36:57 +01:00
parent cd739fb52e
commit 1fe77c0905
21 changed files with 1267 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
#include "character_creation_worker.h"
#include "connection_guard.h"
#include <iostream>
#include <chrono>
#include <thread>
CharacterCreationWorker::CharacterCreationWorker(ConnectionPool &pool, MessageBroker &broker)
: Worker(pool, broker, "CharacterCreationWorker")
, gen(std::random_device{}())
, dist(2, 3)
{
}
CharacterCreationWorker::~CharacterCreationWorker() {
}
void CharacterCreationWorker::run() {
while (runningWorker) {
setCurrentStep("Check if previous day character was created");
if (!isPreviousDayCharacterCreated()) {
setCurrentStep("Create characters for today");
createCharactersForToday();
}
setCurrentStep("Sleep for 60 seconds");
for (int i = 0; i < 60 && runningWorker; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
setCurrentStep("signalActivity()");
signalActivity();
}
setCurrentStep("Loop done");
}
}
bool CharacterCreationWorker::isPreviousDayCharacterCreated() {
try {
setCurrentStep("Get Database Connection");
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
setCurrentStep("Execute Query");
auto results = db.query(QUERY_IS_PREVIOUS_DAY_CHARACTER_CREATED);
if (!results.empty()) {
std::string created_at_str = results[0].at("created_at");
return true;
}
} catch (const std::exception &e) {
std::cerr << "[CharacterCreationWorker] Fehler in isPreviousDayCharacterCreated: "
<< e.what() << std::endl;
}
setCurrentStep("No previous day character found");
return false;
}
void CharacterCreationWorker::createCharactersForToday() {
loadNames();
if (first_name_cache.empty() || last_name_cache.empty()) {
std::cerr << "Fehler: Namen konnten nicht geladen werden." << std::endl;
return;
}
auto town_ids = getTownRegionIds();
for (auto region_id : town_ids) {
createCharactersForRegion(region_id);
}
}
void CharacterCreationWorker::createCharactersForRegion(int region_id) {
std::vector<int> nobility_stands = {1, 2, 3};
std::vector<std::string> genders = {"male", "female"};
for (auto nobility : nobility_stands) {
for (auto &gender : genders) {
int num_chars = dist(gen);
for (int i = 0; i < num_chars; ++i) {
createCharacter(region_id, gender, nobility);
}
}
}
}
void CharacterCreationWorker::createCharacter(int region_id,
const std::string &gender,
int title_of_nobility) {
int first_name_id = getRandomFromSet(first_name_cache[gender]);
if (first_name_id == -1) {
std::cerr << "Fehler: Kein passender Vorname gefunden." << std::endl;
return;
}
int last_name_id = getRandomFromSet(last_name_cache);
if (last_name_id == -1) {
std::cerr << "Fehler: Kein passender Nachname gefunden." << std::endl;
return;
}
try {
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
db.prepare("insert_character", QUERY_INSERT_CHARACTER);
db.execute("insert_character", {std::to_string(region_id),
std::to_string(first_name_id),
std::to_string(last_name_id),
gender,
std::to_string(title_of_nobility)});
} catch (const std::exception &e) {
std::cerr << "[CharacterCreationWorker] Fehler in createCharacter: "
<< e.what() << std::endl;
}
}
std::vector<int> CharacterCreationWorker::getTownRegionIds() {
try {
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
auto rows = db.query(QUERY_GET_TOWN_REGION_IDS);
std::vector<int> ids;
ids.reserve(rows.size());
for (const auto &row : rows) {
ids.push_back(std::stoi(row.at("id")));
}
return ids;
} catch (const std::exception &e) {
std::cerr << "[CharacterCreationWorker] Fehler in getTownRegionIds: "
<< e.what() << std::endl;
}
return {};
}
void CharacterCreationWorker::loadNames() {
try {
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
auto firstNameRows = db.query(QUERY_LOAD_FIRST_NAMES);
for (const auto &row : firstNameRows) {
first_name_cache[row.at("gender")].insert(std::stoi(row.at("id")));
}
auto lastNameRows = db.query(QUERY_LOAD_LAST_NAMES);
for (const auto &row : lastNameRows) {
last_name_cache.insert(std::stoi(row.at("id")));
}
} catch (const std::exception &e) {
std::cerr << "[CharacterCreationWorker] Fehler in loadNames: "
<< e.what() << std::endl;
}
}
int CharacterCreationWorker::getRandomFromSet(const std::unordered_set<int> &name_set) {
if (name_set.empty()) {
return -1;
}
auto it = name_set.begin();
std::advance(it, std::uniform_int_distribution<int>(0, name_set.size() - 1)(gen));
return *it;
}