RulesDB.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <assert.h>
  17. #include "../../common/Log.h"
  18. #include "../WorldDatabase.h"
  19. extern RuleManager rule_manager;
  20. void WorldDatabase::LoadGlobalRuleSet() {
  21. Query query;
  22. MYSQL_ROW row;
  23. MYSQL_RES *res;
  24. int32 rule_set_id = 0;
  25. res = query.RunQuery2(Q_SELECT, "SELECT `variable_value`\n"
  26. "FROM `variables`\n"
  27. "WHERE `variable_name`='default_ruleset_id'");
  28. if (res && (row = mysql_fetch_row(res)))
  29. {
  30. rule_set_id = atoul(row[0]);
  31. LogWrite(RULESYS__DEBUG, 5, "Rules", "\t\tLoading Global Ruleset id %i", rule_set_id);
  32. }
  33. if (rule_set_id > 0 && !rule_manager.SetGlobalRuleSet(rule_set_id))
  34. LogWrite(RULESYS__ERROR, 0, "Rules", "Error loading global rule set. A rule set with ID %u does not exist.", rule_set_id);
  35. else if(rule_set_id == 0)
  36. LogWrite(RULESYS__ERROR, 0, "Rules", "Variables table is missing default_ruleset_id variable name, this means the global rules will be code-default, database entries not used. Use query such as \"insert into variables set variable_name='default_ruleset_id',variable_value='1',comment='Default ruleset';\" to resolve.");
  37. }
  38. void WorldDatabase::LoadRuleSets(bool reload) {
  39. RuleSet *rule_set;
  40. Query query;
  41. MYSQL_ROW row;
  42. MYSQL_RES *res;
  43. if (reload)
  44. rule_manager.Flush(true);
  45. /* first load the coded defaults in */
  46. rule_manager.LoadCodedDefaultsIntoRuleSet(rule_manager.GetGlobalRuleSet());
  47. res = query.RunQuery2(Q_SELECT, "SELECT `ruleset_id`,`ruleset_name`\n"
  48. "FROM `rulesets`\n"
  49. "WHERE `ruleset_active`>0");
  50. if (res) {
  51. while ((row = mysql_fetch_row(res))) {
  52. rule_set = new RuleSet();
  53. rule_set->SetID(atoul(row[0]));
  54. rule_set->SetName(row[1]);
  55. if (rule_manager.AddRuleSet(rule_set))
  56. {
  57. LogWrite(RULESYS__DEBUG, 5, "Rules", "\t\tLoading rule set '%s' (%u)", rule_set->GetName(), rule_set->GetID());
  58. LoadRuleSetDetails(rule_set);
  59. }
  60. else {
  61. LogWrite(RULESYS__ERROR, 0, "Rules", "Unable to add rule set '%s'. A ruleset with ID %u already exists.", rule_set->GetName(), rule_set->GetID());
  62. safe_delete(rule_set);
  63. }
  64. }
  65. }
  66. LogWrite(RULESYS__DEBUG, 3, "Rules", "--Loaded %u Rule Sets", rule_manager.GetNumRuleSets());
  67. LoadGlobalRuleSet();
  68. }
  69. void WorldDatabase::LoadRuleSetDetails(RuleSet *rule_set) {
  70. Rule *rule;
  71. Query query;
  72. MYSQL_ROW row;
  73. MYSQL_RES *res;
  74. assert(rule_set);
  75. rule_set->CopyRulesInto(rule_manager.GetGlobalRuleSet());
  76. res = query.RunQuery2(Q_SELECT, "SELECT `rule_category`,`rule_type`,`rule_value`\n"
  77. "FROM `ruleset_details`\n"
  78. "WHERE `ruleset_id`=%u",
  79. rule_set->GetID());
  80. if (res) {
  81. while ((row = mysql_fetch_row(res))) {
  82. if (!(rule = rule_set->GetRule(row[0], row[1]))) {
  83. LogWrite(RULESYS__WARNING, 0, "Rules", "Unknown rule with category '%s' and type '%s'", row[0], row[1]);
  84. continue;
  85. }
  86. LogWrite(RULESYS__DEBUG, 5, "Rules", "---Setting rule category '%s', type '%s' to value: %s", row[0], row[1], row[2]);
  87. rule->SetValue(row[2]);
  88. }
  89. }
  90. }