9
3

AchievementsDB.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #ifdef WIN32
  17. #include <WinSock2.h>
  18. #include <windows.h>
  19. #endif
  20. #include <mysql.h>
  21. #include <assert.h>
  22. #include "../../common/Log.h"
  23. #include "../WorldDatabase.h"
  24. #include "Achievements.h"
  25. extern MasterAchievementList master_achievement_list;
  26. void WorldDatabase::LoadAchievements()
  27. {
  28. Achievement *achievement;
  29. Query query;
  30. MYSQL_ROW row;
  31. MYSQL_RES *res;
  32. int32 aReqs_total = 0;
  33. int32 aRewards_total = 0;
  34. res = query.RunQuery2(Q_SELECT, "SELECT `achievement_id`,`title`,`uncompleted_text`,`completed_text`,`category`,`expansion`,`icon`,`point_value`,`qty_req`,`hide_achievement`,`unknown3a`,`unknown3b`\n"
  35. "FROM `achievements`");
  36. if (res)
  37. {
  38. while ((row = mysql_fetch_row(res)))
  39. {
  40. achievement = new Achievement();
  41. achievement->SetID(atoul(row[0]));
  42. achievement->SetTitle(row[1]);
  43. achievement->SetUncompletedText(row[2]);
  44. achievement->SetCompletedText(row[3]);
  45. achievement->SetCategory(row[4]);
  46. achievement->SetExpansion(row[5]);
  47. achievement->SetIcon(atoi(row[6]));
  48. achievement->SetPointValue(atoul(row[7]));
  49. achievement->SetQtyReq(atoul(row[8]));
  50. achievement->SetHide( atoi(row[9]) == 0 ? false : true );
  51. achievement->SetUnknown3a(atoul(row[10]));
  52. achievement->SetUnknown3b(atoul(row[11]));
  53. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "\tLoading Achievement: '%s' (%u)", achievement->GetTitle(), achievement->GetID());
  54. if (!master_achievement_list.AddAchievement(achievement))
  55. {
  56. LogWrite(ACHIEVEMENT__ERROR, 0, "Achievements", "Error adding achievement '%s' - duplicate ID: %u", achievement->GetTitle(), achievement->GetID());
  57. safe_delete(achievement);
  58. continue;
  59. }
  60. aReqs_total += LoadAchievementRequirements(achievement);
  61. aRewards_total += LoadAchievementRewards(achievement);
  62. }
  63. }
  64. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "\tLoaded %u achievements", master_achievement_list.Size());
  65. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "\tLoaded %u achievement requirements", aReqs_total);
  66. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "\tLoaded %u achievement rewards", aRewards_total);
  67. }
  68. int32 WorldDatabase::LoadAchievementRequirements(Achievement *achievement)
  69. {
  70. AchievementRequirements *requirements;
  71. Query query;
  72. MYSQL_ROW row;
  73. MYSQL_RES *res;
  74. int16 total = 0;
  75. assert(achievement);
  76. res = query.RunQuery2(Q_SELECT, "SELECT `achievement_id`, `name`, `qty_req` FROM `achievements_requirements` WHERE `achievement_id` = %u", achievement->GetID());
  77. if (res) {
  78. while ((row = mysql_fetch_row(res))) {
  79. requirements = new AchievementRequirements();
  80. requirements->achievement_id = atoul(row[0]);
  81. requirements->name = row[1];
  82. requirements->qty_req = atoul(row[2]);
  83. achievement->AddAchievementRequirement(requirements);
  84. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "Loading Achievements Requirement '%s'", requirements->name.c_str());
  85. total++;
  86. }
  87. }
  88. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "Loaded %u requirements for achievement '%s' ID: %u", total, achievement->GetTitle(), achievement->GetID());
  89. return total;
  90. }
  91. int32 WorldDatabase::LoadAchievementRewards(Achievement *achievement)
  92. {
  93. AchievementRewards *rewards;
  94. Query query;
  95. MYSQL_ROW row;
  96. MYSQL_RES *res;
  97. int16 total = 0;
  98. assert(achievement);
  99. res = query.RunQuery2(Q_SELECT, "SELECT `achievement_id`, `reward` FROM `achievements_rewards` WHERE `achievement_id` = %u", achievement->GetID());
  100. if (res) {
  101. while ((row = mysql_fetch_row(res))) {
  102. rewards = new AchievementRewards();
  103. rewards->achievement_id = atoul(row[0]);
  104. rewards->reward = row[1];
  105. achievement->AddAchievementReward(rewards);
  106. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "Loading Achievements Reward '%s'", rewards->reward.c_str());
  107. total++;
  108. }
  109. }
  110. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "Loaded %u rewards for achievement '%s' ID: %u", total, achievement->GetTitle(), achievement->GetID());
  111. return total;
  112. }
  113. void WorldDatabase::LoadPlayerAchievements(Player *player) {
  114. Achievement *achievement;
  115. Query query;
  116. MYSQL_ROW row;
  117. MYSQL_RES *res;
  118. int32 aReqs_total = 0;
  119. int32 aRewards_total = 0;
  120. int32 total = 0;
  121. assert(player);
  122. res = query.RunQuery2(Q_SELECT, "SELECT `achievement_id`,`title`,`uncompleted_text`,`completed_text`,`category`,`expansion`,`icon`,`point_value`,`qty_req`,`hide_achievement`,`unknown3a`,`unknown3b`\n"
  123. "FROM `achievements`");
  124. if (res)
  125. {
  126. while ((row = mysql_fetch_row(res)))
  127. {
  128. achievement = new Achievement();
  129. achievement->SetID(atoul(row[0]));
  130. achievement->SetTitle(row[1]);
  131. achievement->SetUncompletedText(row[2]);
  132. achievement->SetCompletedText(row[3]);
  133. achievement->SetCategory(row[4]);
  134. achievement->SetExpansion(row[5]);
  135. achievement->SetIcon(atoi(row[6]));
  136. achievement->SetPointValue(atoul(row[7]));
  137. achievement->SetQtyReq(atoul(row[8]));
  138. achievement->SetHide( atoi(row[9]) == 0 ? false : true );
  139. achievement->SetUnknown3a(atoul(row[10]));
  140. achievement->SetUnknown3b(atoul(row[11]));
  141. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "\tLoading Achievement: '%s' (%u)", achievement->GetTitle(), achievement->GetID());
  142. if (!player->GetAchievementList()->AddAchievement(achievement))
  143. {
  144. LogWrite(ACHIEVEMENT__ERROR, 0, "Achievements", "Error adding achievement '%s' - duplicate ID: %u", achievement->GetTitle(), achievement->GetID());
  145. safe_delete(achievement);
  146. continue;
  147. }
  148. total++;
  149. aReqs_total += LoadAchievementRequirements(achievement);
  150. aRewards_total += LoadAchievementRewards(achievement);
  151. }
  152. }
  153. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "\tLoaded %u achievements for '%s'", total, player->GetName());
  154. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "\tLoaded %u achievement requirements for '%s'", aReqs_total, player->GetName());
  155. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "\tLoaded %u achievement rewards for '%s'", aRewards_total, player->GetName());
  156. }
  157. int32 WorldDatabase::LoadPlayerAchievementsUpdates(Player *player) {
  158. AchievementUpdate *update;
  159. Query query;
  160. MYSQL_ROW row;
  161. MYSQL_RES *res;
  162. int32 total = 0;
  163. int32 items_total = 0;
  164. assert(player);
  165. res = query.RunQuery2(Q_SELECT, "SELECT `char_id`, `achievement_id`, `completed_date` FROM character_achievements WHERE char_id = %u ", player->GetCharacterID());
  166. if (res) {
  167. while ((row = mysql_fetch_row(res))) {
  168. update = new AchievementUpdate();
  169. update->SetID(atoul(row[1]));
  170. update->SetCompletedDate(atoul(row[2]));
  171. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "Loading Player Achievement Update for Achievement ID: %u ", update->GetID());
  172. if (!player->GetAchievementUpdateList()->AddAchievementUpdate(update))
  173. {
  174. LogWrite(ACHIEVEMENT__ERROR, 0, "Achievements", "Error adding achievement update %u - diplicate ID", update->GetID());
  175. safe_delete(update);
  176. continue;
  177. }
  178. total++;
  179. items_total += LoadPlayerAchievementsUpdateItems(update, player->GetCharacterID());
  180. }
  181. }
  182. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "Loaded %u player achievement updates", total);
  183. return total;
  184. }
  185. int32 WorldDatabase::LoadPlayerAchievementsUpdateItems(AchievementUpdate *update, int32 player_id) {
  186. AchievementUpdateItems *update_items;
  187. Query query;
  188. MYSQL_ROW row;
  189. MYSQL_RES *res;
  190. int32 total = 0;
  191. assert(update);
  192. res = query.RunQuery2(Q_SELECT, "SELECT `achievement_id`, `items` FROM character_achievements_items WHERE char_id = %u AND achievement_id = %u;", player_id, update->GetID());
  193. if (res) {
  194. while ((row = mysql_fetch_row(res))) {
  195. update_items = new AchievementUpdateItems();
  196. update_items->achievement_id = atoul(row[0]);
  197. update_items->item_update = atoul(row[1]);
  198. update->AddAchievementUpdateItems(update_items);
  199. LogWrite(ACHIEVEMENT__DEBUG, 5, "Achievements", "Loading Player Achievement Update Items for Achievement ID: %u ", update_items->achievement_id);
  200. total++;
  201. }
  202. }
  203. LogWrite(ACHIEVEMENT__DEBUG, 0, "Achievements", "Loaded %u player achievement update items", total);
  204. return total;
  205. }