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;
for (auto rule : entry.rules) {
rule.ruleset_id = ruleset.ruleset_id;
rule.notes = m_rule_manager->GetRuleNotesByName(rule.rule_name);
auto existing = RuleValuesRepository::GetWhere(
*m_database, *m_database,
fmt::format("ruleset_id = {}", entry.rule_set.ruleset_id) fmt::format(
"ruleset_id = {} AND rule_name = '{}'",
ruleset.ruleset_id,
Strings::Escape(rule.rule_name)
)
); );
std::unordered_set<std::string> existing_rule_names; if (existing.empty()) {
for (const auto& r : existing_rules) { to_insert.push_back(rule);
existing_rule_names.insert(r.rule_name); }
} }
std::vector<RuleValuesRepository::RuleValues> new_rules; if (!to_insert.empty()) {
for (auto r : entry.rules) { RuleValuesRepository::InsertMany(*m_database, to_insert);
if (existing_rule_names.count(r.rule_name)) { LogInfo("Inserted [{}] new rule(s) into ruleset [{}]", to_insert.size(), ruleset.ruleset_id);
continue;
}
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);
} }
} }
} }

View File

@ -666,3 +666,15 @@ 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

@ -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);