CollectionsDB.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "Collections.h"
  25. extern MasterCollectionList master_collection_list;
  26. void WorldDatabase::LoadCollections()
  27. {
  28. Collection *collection;
  29. Query query;
  30. MYSQL_ROW row;
  31. MYSQL_RES *res;
  32. int32 cItems_total = 0;
  33. int32 cItems_rewards = 0;
  34. res = query.RunQuery2(Q_SELECT, "SELECT `id`,`collection_name`,`collection_category`,`level`\n"
  35. "FROM `collections`");
  36. if (res)
  37. {
  38. while ((row = mysql_fetch_row(res)))
  39. {
  40. collection = new Collection();
  41. collection->SetID(atoul(row[0]));
  42. collection->SetName(row[1]);
  43. collection->SetCategory(row[2]);
  44. collection->SetLevel(atoi(row[3]));
  45. LogWrite(COLLECTION__DEBUG, 5, "Collect", "\tLoading Collection: '%s' (%u)", collection->GetName(),collection->GetID());
  46. if (!master_collection_list.AddCollection(collection))
  47. {
  48. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error adding collection '%s' - duplicate ID: %u", collection->GetName(),collection->GetID());
  49. safe_delete(collection);
  50. continue;
  51. }
  52. cItems_total += LoadCollectionItems(collection);
  53. cItems_rewards += LoadCollectionRewards(collection);
  54. }
  55. }
  56. LogWrite(COLLECTION__DEBUG, 0, "Collect", "\tLoaded %u collections", master_collection_list.Size());
  57. LogWrite(COLLECTION__DEBUG, 0, "Collect", "\tLoaded %u collection items", cItems_total);
  58. LogWrite(COLLECTION__DEBUG, 0, "Collect", "\tLoaded %u collection rewards", cItems_rewards);
  59. }
  60. int32 WorldDatabase::LoadCollectionItems(Collection *collection)
  61. {
  62. struct CollectionItem *collection_item;
  63. Item *item;
  64. Query query;
  65. MYSQL_ROW row;
  66. MYSQL_RES *res;
  67. int32 total = 0;
  68. assert(collection);
  69. res = query.RunQuery2(Q_SELECT, "SELECT `item_id`,`item_index`\n"
  70. "FROM `collection_details`\n"
  71. "WHERE `collection_id`=%u\n"
  72. "ORDER BY `item_index` ASC",
  73. collection->GetID());
  74. if (res)
  75. {
  76. while ((row = mysql_fetch_row(res)))
  77. {
  78. if ((item = master_item_list.GetItem(atoul(row[0]))))
  79. {
  80. collection_item = new struct CollectionItem;
  81. collection_item->item = atoul(row[0]);
  82. collection_item->index = atoi(row[1]);
  83. collection_item->found = 0;
  84. LogWrite(COLLECTION__DEBUG, 5, "Collect", "\tLoading Collection Item: (%u)", atoul(row[0])); //LogWrite(COLLECTION__DEBUG, 5, "Collect", "\tLoading Collection Item: '%s' (%u)", master_item_list.GetItem(collection_item->item)->name.c_str(), atoul(row[0]));
  85. collection->AddCollectionItem(collection_item);
  86. total++;
  87. }
  88. }
  89. }
  90. if(query.GetErrorNumber())
  91. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error Loading Collection Items, Query: %s, Error: %s", query.GetQuery(), query.GetError());
  92. return total;
  93. }
  94. int32 WorldDatabase::LoadCollectionRewards(Collection *collection)
  95. {
  96. struct CollectionRewardItem *reward_item;
  97. Query query;
  98. MYSQL_ROW row;
  99. MYSQL_RES *res;
  100. int32 total = 0;
  101. assert(collection);
  102. res = query.RunQuery2(Q_SELECT, "SELECT `reward_type`,`reward_value`,`reward_quantity`\n"
  103. "FROM `collection_rewards`\n"
  104. "WHERE `collection_id`=%u",
  105. collection->GetID());
  106. if (res)
  107. {
  108. while ((row = mysql_fetch_row(res)))
  109. {
  110. LogWrite(COLLECTION__DEBUG, 5, "Collect", "\tLoading Collection Reward: Type: %s, Val: %s, Qty: %u", row[0], row[1], atoi(row[2]));
  111. if (!strcasecmp(row[0], "Item"))
  112. {
  113. reward_item = new struct CollectionRewardItem;
  114. reward_item->item = master_item_list.GetItem(atoul(row[1]));
  115. reward_item->quantity = atoi(row[2]);
  116. collection->AddRewardItem(reward_item);
  117. total++;
  118. }
  119. else if (!strcasecmp(row[0], "Selectable"))
  120. {
  121. reward_item = new struct CollectionRewardItem;
  122. reward_item->item = master_item_list.GetItem(atoul(row[1]));
  123. reward_item->quantity = atoi(row[2]);
  124. collection->AddSelectableRewardItem(reward_item);
  125. total++;
  126. }
  127. else if (!strcasecmp(row[0], "Coin"))
  128. {
  129. collection->SetRewardCoin(atoi64(row[1]));
  130. total++;
  131. }
  132. else if (!strcasecmp(row[0], "XP"))
  133. {
  134. collection->SetRewardXP(atoi64(row[1]));
  135. total++;
  136. }
  137. else
  138. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error adding collection reward to collection '%s'. Unknown reward type '%s'", collection->GetName(), row[0]);
  139. }
  140. }
  141. if(query.GetErrorNumber())
  142. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error Loading Collection Rewards, Query: %s, Error: %s", query.GetQuery(), query.GetError());
  143. return total;
  144. }
  145. void WorldDatabase::LoadPlayerCollections(Player *player)
  146. {
  147. Collection *collection;
  148. Query query;
  149. MYSQL_ROW row;
  150. MYSQL_RES *res;
  151. assert(player);
  152. res = query.RunQuery2(Q_SELECT, "SELECT `collection_id`,`completed` FROM `character_collections` WHERE `char_id`=%u", player->GetCharacterID());
  153. if (res)
  154. {
  155. while ((row = mysql_fetch_row(res)))
  156. {
  157. collection = new Collection(master_collection_list.GetCollection(atoul(row[0])));
  158. collection->SetCompleted(atoi(row[1]));
  159. if (!player->GetCollectionList()->AddCollection(collection))
  160. {
  161. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error adding collection %u to player '%s' - duplicate ID\n", collection->GetID(), player->GetName());
  162. safe_delete(collection);
  163. continue;
  164. }
  165. LoadPlayerCollectionItems(player, collection);
  166. }
  167. }
  168. if(query.GetErrorNumber())
  169. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error Loading Character Collections, Query: %s, Error: %s", query.GetQuery(), query.GetError());
  170. }
  171. void WorldDatabase::LoadPlayerCollectionItems(Player *player, Collection *collection)
  172. {
  173. struct CollectionItem *collection_item;
  174. Query query;
  175. MYSQL_ROW row;
  176. MYSQL_RES *res;
  177. assert(player);
  178. assert(collection);
  179. res = query.RunQuery2(Q_SELECT, "SELECT `collection_item_id`\n"
  180. "FROM `character_collection_items`\n"
  181. "WHERE `char_id`=%u\n"
  182. "AND `collection_id`=%u",
  183. player->GetCharacterID(), collection->GetID());
  184. if (res)
  185. {
  186. while ((row = mysql_fetch_row(res)))
  187. {
  188. if ((collection_item = collection->GetCollectionItemByItemID(atoul(row[0]))))
  189. collection_item->found = true;
  190. else
  191. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error Loading character collection items. Item ID %u does not exist in collection %s", atoul(row[0]), collection->GetName());
  192. }
  193. }
  194. if(query.GetErrorNumber())
  195. LogWrite(COLLECTION__ERROR, 0, "Collect", "Error Loading Character Collection Items, Query: %s, Error: %s", query.GetQuery(), query.GetError());
  196. }
  197. void WorldDatabase::SavePlayerCollections(Client *client)
  198. {
  199. map<int32, Collection *> *collections;
  200. map<int32, Collection *>::iterator itr;
  201. Collection *collection;
  202. assert(client);
  203. collections = client->GetPlayer()->GetCollectionList()->GetCollections();
  204. for (itr = collections->begin(); itr != collections->end(); itr++)
  205. {
  206. collection = itr->second;
  207. if (collection->GetSaveNeeded())
  208. {
  209. SavePlayerCollection(client, collection);
  210. SavePlayerCollectionItems(client, collection);
  211. collection->SetSaveNeeded(false);
  212. }
  213. }
  214. }
  215. void WorldDatabase::SavePlayerCollection(Client *client, Collection *collection)
  216. {
  217. Query query;
  218. assert(client);
  219. assert(collection);
  220. query.RunQuery2(Q_UPDATE, "INSERT INTO `character_collections` (`char_id`,`collection_id`,`completed`)\n"
  221. "VALUES (%u,%u,0)\n"
  222. "ON DUPLICATE KEY UPDATE `completed`=%i",
  223. client->GetPlayer()->GetCharacterID(), collection->GetID(),
  224. collection->GetCompleted() ? 1 : 0);
  225. }
  226. void WorldDatabase::SavePlayerCollectionItems(Client *client, Collection *collection)
  227. {
  228. vector<struct CollectionItem *> *collection_items;
  229. vector<struct CollectionItem *>::iterator itr;
  230. struct CollectionItem *collection_item;
  231. assert(client);
  232. assert(collection);
  233. collection_items = collection->GetCollectionItems();
  234. for (itr = collection_items->begin(); itr != collection_items->end(); itr++)
  235. {
  236. collection_item = *itr;
  237. if (collection_item->found > 0)
  238. SavePlayerCollectionItem(client, collection, collection_item->item);
  239. }
  240. }
  241. void WorldDatabase::SavePlayerCollectionItem(Client *client, Collection *collection, int32 item_id)
  242. {
  243. Query query;
  244. assert(client);
  245. assert(collection);
  246. //assert(item);
  247. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO `character_collection_items` (`char_id`,`collection_id`,`collection_item_id`)\n"
  248. "VALUES (%u,%u,%u)",
  249. client->GetPlayer()->GetCharacterID(), collection->GetID(), item_id);
  250. }