From 7f869442c4954c0c18e599489322ef33ee359acd Mon Sep 17 00:00:00 2001 From: Chris Miles Date: Fri, 27 Jun 2025 18:30:17 -0500 Subject: [PATCH] Rule injection updates, notes etc --- common/content/world_content_service.cpp | 44 ++++++++++++++---------- common/rulesys.cpp | 12 +++++++ common/rulesys.h | 8 +++-- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/common/content/world_content_service.cpp b/common/content/world_content_service.cpp index 06d0f9ac0..f173f79ba 100644 --- a/common/content/world_content_service.cpp +++ b/common/content/world_content_service.cpp @@ -359,38 +359,44 @@ void WorldContentService::SeedDefaultRulesets() }; for (const auto& entry : rulesets) { + const auto& ruleset = entry.rule_set; + + // Ensure ruleset exists auto existing_sets = RuleSetsRepository::GetWhere( *m_database, - fmt::format("ruleset_id = {}", entry.rule_set.ruleset_id) + fmt::format("ruleset_id = {}", ruleset.ruleset_id) ); if (existing_sets.empty()) { - RuleSetsRepository::InsertOne(*m_database, entry.rule_set); - LogInfo("Inserted ruleset [{}] - {}", entry.rule_set.ruleset_id, entry.rule_set.name); + RuleSetsRepository::InsertOne(*m_database, ruleset); + LogInfo("Inserted ruleset [{}] - {}", ruleset.ruleset_id, ruleset.name); } + // Insert rules if not present if (!entry.rules.empty()) { - auto existing_rules = RuleValuesRepository::GetWhere( - *m_database, - fmt::format("ruleset_id = {}", entry.rule_set.ruleset_id) - ); + std::vector to_insert; - std::unordered_set existing_rule_names; - for (const auto& r : existing_rules) { - existing_rule_names.insert(r.rule_name); - } + for (auto rule : entry.rules) { + rule.ruleset_id = ruleset.ruleset_id; + rule.notes = m_rule_manager->GetRuleNotesByName(rule.rule_name); - std::vector new_rules; - for (auto r : entry.rules) { - if (existing_rule_names.count(r.rule_name)) { - continue; + auto existing = RuleValuesRepository::GetWhere( + *m_database, + fmt::format( + "ruleset_id = {} AND rule_name = '{}'", + ruleset.ruleset_id, + Strings::Escape(rule.rule_name) + ) + ); + + if (existing.empty()) { + to_insert.push_back(rule); } - new_rules.push_back(r); } - if (!new_rules.empty()) { - RuleValuesRepository::InsertMany(*m_database, new_rules); - LogInfo("Inserted [{}] rules into ruleset [{}]", new_rules.size(), entry.rule_set.ruleset_id); + if (!to_insert.empty()) { + RuleValuesRepository::InsertMany(*m_database, to_insert); + LogInfo("Inserted [{}] new rule(s) into ruleset [{}]", to_insert.size(), ruleset.ruleset_id); } } } diff --git a/common/rulesys.cpp b/common/rulesys.cpp index 84043ca99..c54be7128 100644 --- a/common/rulesys.cpp +++ b/common/rulesys.cpp @@ -665,4 +665,16 @@ bool RuleManager::GetBoolRule(RuleManager::BoolType t) const std::string RuleManager::GetStringRule(RuleManager::StringType t) const { return m_RuleStringValues[t]; +} + +std::string RuleManager::GetRuleNotesByName(std::string rule_name) const +{ + for (const auto &r : s_RuleInfo) { + if (Strings::EqualFold(r.name, rule_name)) { + return r.notes; + } + } + + LogRulesDetail("Unable to find rule notes for '{}'.", rule_name); + return ""; } \ No newline at end of file diff --git a/common/rulesys.h b/common/rulesys.h index b2dc00b34..76f72edd1 100644 --- a/common/rulesys.h +++ b/common/rulesys.h @@ -120,9 +120,9 @@ public: static const uint32 RulesCount = IntRuleCount + RealRuleCount + BoolRuleCount + StringRuleCount; //fetch routines, you should generally use the Rule* macros instead of this - int GetIntRule(IntType t) const; - float GetRealRule(RealType t) const; - bool GetBoolRule(BoolType t) const; + int GetIntRule(IntType t) const; + float GetRealRule(RealType t) const; + bool GetBoolRule(BoolType t) const; std::string GetStringRule(StringType t) const; //management routines @@ -134,6 +134,8 @@ public: static const std::string& GetRuleNotes(RealType t) { return s_RuleInfo[static_cast(t) + IntRuleCount].notes; } static const std::string& GetRuleNotes(BoolType t) { return s_RuleInfo[static_cast(t) + IntRuleCount + RealRuleCount].notes; } static const std::string& GetRuleNotes(StringType t) { return s_RuleInfo[static_cast(t) + IntRuleCount + RealRuleCount + StringRuleCount].notes; } + std::string GetRuleNotesByName(std::string rule_name) const; + static uint32 CountRules() { return RulesCount; } static CategoryType FindCategory(const std::string& category_name); bool ListRules(const std::string& category_name, std::vector& l);