9
3

RulesDB.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  36. void WorldDatabase::LoadRuleSets(bool reload) {
  37. RuleSet *rule_set;
  38. Query query;
  39. MYSQL_ROW row;
  40. MYSQL_RES *res;
  41. if (reload)
  42. rule_manager.Flush(true);
  43. /* first load the coded defaults in */
  44. rule_manager.LoadCodedDefaultsIntoRuleSet(rule_manager.GetGlobalRuleSet());
  45. res = query.RunQuery2(Q_SELECT, "SELECT `ruleset_id`,`ruleset_name`\n"
  46. "FROM `rulesets`\n"
  47. "WHERE `ruleset_active`>0");
  48. if (res) {
  49. while ((row = mysql_fetch_row(res))) {
  50. rule_set = new RuleSet();
  51. rule_set->SetID(atoul(row[0]));
  52. rule_set->SetName(row[1]);
  53. if (rule_manager.AddRuleSet(rule_set))
  54. {
  55. LogWrite(RULESYS__DEBUG, 5, "Rules", "\t\tLoading rule set '%s' (%u)", rule_set->GetName(), rule_set->GetID());
  56. LoadRuleSetDetails(rule_set);
  57. }
  58. else {
  59. LogWrite(RULESYS__ERROR, 0, "Rules", "Unable to add rule set '%s'. A ruleset with ID %u already exists.", rule_set->GetName(), rule_set->GetID());
  60. safe_delete(rule_set);
  61. }
  62. }
  63. }
  64. LogWrite(RULESYS__DEBUG, 3, "Rules", "--Loaded %u Rule Sets", rule_manager.GetNumRuleSets());
  65. LoadGlobalRuleSet();
  66. }
  67. void WorldDatabase::LoadRuleSetDetails(RuleSet *rule_set) {
  68. Rule *rule;
  69. Query query;
  70. MYSQL_ROW row;
  71. MYSQL_RES *res;
  72. assert(rule_set);
  73. rule_set->CopyRulesInto(rule_manager.GetGlobalRuleSet());
  74. res = query.RunQuery2(Q_SELECT, "SELECT `rule_category`,`rule_type`,`rule_value`\n"
  75. "FROM `ruleset_details`\n"
  76. "WHERE `ruleset_id`=%u",
  77. rule_set->GetID());
  78. if (res) {
  79. while ((row = mysql_fetch_row(res))) {
  80. if (!(rule = rule_set->GetRule(row[0], row[1]))) {
  81. LogWrite(RULESYS__ERROR, 0, "Rules", "Unknown rule with category '%s' and type '%s'", row[0], row[1]);
  82. continue;
  83. }
  84. LogWrite(RULESYS__DEBUG, 5, "Rules", "---Setting rule category '%s', type '%s' to value: %s", row[0], row[1], row[2]);
  85. rule->SetValue(row[2]);
  86. }
  87. }
  88. }