Rule injection updates, notes etc

This commit is contained in:
Chris Miles 2025-06-27 18:30:17 -05:00
parent cef1649e9c
commit 7f869442c4
3 changed files with 42 additions and 22 deletions

View File

@ -359,38 +359,44 @@ void WorldContentService::SeedDefaultRulesets()
}; };
for (const auto& entry : rulesets) { for (const auto& entry : rulesets) {
const auto& ruleset = entry.rule_set;
// Ensure ruleset exists
auto existing_sets = RuleSetsRepository::GetWhere( auto existing_sets = RuleSetsRepository::GetWhere(
*m_database, *m_database,
fmt::format("ruleset_id = {}", entry.rule_set.ruleset_id) fmt::format("ruleset_id = {}", ruleset.ruleset_id)
); );
if (existing_sets.empty()) { if (existing_sets.empty()) {
RuleSetsRepository::InsertOne(*m_database, entry.rule_set); RuleSetsRepository::InsertOne(*m_database, ruleset);
LogInfo("Inserted ruleset [{}] - {}", entry.rule_set.ruleset_id, entry.rule_set.name); LogInfo("Inserted ruleset [{}] - {}", ruleset.ruleset_id, ruleset.name);
} }
// Insert rules if not present
if (!entry.rules.empty()) { if (!entry.rules.empty()) {
auto existing_rules = RuleValuesRepository::GetWhere( std::vector<RuleValuesRepository::RuleValues> to_insert;
*m_database,
fmt::format("ruleset_id = {}", entry.rule_set.ruleset_id)
);
std::unordered_set<std::string> existing_rule_names; for (auto rule : entry.rules) {
for (const auto& r : existing_rules) { rule.ruleset_id = ruleset.ruleset_id;
existing_rule_names.insert(r.rule_name); rule.notes = m_rule_manager->GetRuleNotesByName(rule.rule_name);
}
std::vector<RuleValuesRepository::RuleValues> new_rules; auto existing = RuleValuesRepository::GetWhere(
for (auto r : entry.rules) { *m_database,
if (existing_rule_names.count(r.rule_name)) { fmt::format(
continue; "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()) { if (!to_insert.empty()) {
RuleValuesRepository::InsertMany(*m_database, new_rules); RuleValuesRepository::InsertMany(*m_database, to_insert);
LogInfo("Inserted [{}] rules into ruleset [{}]", new_rules.size(), entry.rule_set.ruleset_id); LogInfo("Inserted [{}] new rule(s) into ruleset [{}]", to_insert.size(), ruleset.ruleset_id);
} }
} }
} }

View File

@ -665,4 +665,16 @@ bool RuleManager::GetBoolRule(RuleManager::BoolType t) const
std::string RuleManager::GetStringRule(RuleManager::StringType t) const std::string RuleManager::GetStringRule(RuleManager::StringType t) const
{ {
return m_RuleStringValues[t]; 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 "";
} }

View File

@ -120,9 +120,9 @@ public:
static const uint32 RulesCount = IntRuleCount + RealRuleCount + BoolRuleCount + StringRuleCount; static const uint32 RulesCount = IntRuleCount + RealRuleCount + BoolRuleCount + StringRuleCount;
//fetch routines, you should generally use the Rule* macros instead of this //fetch routines, you should generally use the Rule* macros instead of this
int GetIntRule(IntType t) const; int GetIntRule(IntType t) const;
float GetRealRule(RealType t) const; float GetRealRule(RealType t) const;
bool GetBoolRule(BoolType t) const; bool GetBoolRule(BoolType t) const;
std::string GetStringRule(StringType t) const; std::string GetStringRule(StringType t) const;
//management routines //management routines
@ -134,6 +134,8 @@ public:
static const std::string& GetRuleNotes(RealType t) { return s_RuleInfo[static_cast<int>(t) + IntRuleCount].notes; } static const std::string& GetRuleNotes(RealType t) { return s_RuleInfo[static_cast<int>(t) + IntRuleCount].notes; }
static const std::string& GetRuleNotes(BoolType t) { return s_RuleInfo[static_cast<int>(t) + IntRuleCount + RealRuleCount].notes; } static const std::string& GetRuleNotes(BoolType t) { return s_RuleInfo[static_cast<int>(t) + IntRuleCount + RealRuleCount].notes; }
static const std::string& GetRuleNotes(StringType t) { return s_RuleInfo[static_cast<int>(t) + IntRuleCount + RealRuleCount + StringRuleCount].notes; } static const std::string& GetRuleNotes(StringType t) { return s_RuleInfo[static_cast<int>(t) + IntRuleCount + RealRuleCount + StringRuleCount].notes; }
std::string GetRuleNotesByName(std::string rule_name) const;
static uint32 CountRules() { return RulesCount; } static uint32 CountRules() { return RulesCount; }
static CategoryType FindCategory(const std::string& category_name); static CategoryType FindCategory(const std::string& category_name);
bool ListRules(const std::string& category_name, std::vector<std::string>& l); bool ListRules(const std::string& category_name, std::vector<std::string>& l);