RecipeDB.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "Recipe.h"
  25. extern MasterRecipeList master_recipe_list;
  26. extern MasterRecipeBookList master_recipebook_list;
  27. void WorldDatabase::LoadRecipes() {
  28. DatabaseResult res;
  29. bool status = database_new.Select(&res,
  30. "SELECT r.`id`,r.`level`,r.`icon`,r.`skill_level`,r.`technique`,r.`knowledge`,r.`name`,i.`name` as `book`,r.`bench`,ipc.`adventure_classes`, "
  31. "r.`stage4_id`, r.`name`, r.`stage4_qty`, pcl.`name` as primary_comp_title, fcl.name as `fuel_comp_title`, r.fuel_comp_qty, "
  32. "bc.`name` AS build_comp_title, bc.qty AS build_comp_qty, bc2.`name` AS build2_comp_title, bc2.qty AS build2_comp_qty, "
  33. "bc3.`name` AS build3_comp_title, bc3.qty AS build3_comp_qty, bc4.`name` AS build4_comp_title, bc4.qty AS build4_comp_qty,\n"
  34. "r.stage0_id, r.stage1_id, r.stage2_id, r.stage3_id, r.stage4_id, r.stage0_qty, r.stage1_qty, r.stage2_qty, r.stage3_qty, r.stage4_qty,\n"
  35. "r.stage0_byp_id, r.stage1_byp_id, r.stage2_byp_id, r.stage3_byp_id, r.stage4_byp_id, r.stage0_byp_qty, r.stage1_byp_qty, r.stage2_byp_qty, r.stage3_byp_qty, r.stage4_byp_qty\n"
  36. "FROM `recipe` r\n"
  37. "LEFT JOIN ((SELECT recipe_id, soe_recipe_crc FROM item_details_recipe_items GROUP BY soe_recipe_crc) as idri) ON idri.soe_recipe_crc = r.soe_id\n"
  38. "LEFT JOIN items i ON idri.recipe_id = i.id\n"
  39. "INNER JOIN items ipc ON r.stage4_id = ipc.id\n"
  40. "INNER JOIN recipe_comp_list pcl ON r.primary_comp_list = pcl.id\n"
  41. "INNER JOIN recipe_comp_list fcl ON r.fuel_comp_list = fcl.id\n"
  42. "LEFT JOIN (SELECT rsc.recipe_id, rsc.comp_list, rsc.`index`, rcl.`name`, rsc.qty FROM recipe_secondary_comp rsc INNER JOIN recipe_comp_list rcl ON rcl.id = rsc.comp_list WHERE `index` = 0) AS bc ON bc.recipe_id = r.id\n"
  43. "LEFT JOIN (SELECT rsc.recipe_id, rsc.comp_list, rsc.`index`, rcl.`name`, rsc.qty FROM recipe_secondary_comp rsc INNER JOIN recipe_comp_list rcl ON rcl.id = rsc.comp_list WHERE `index` = 1) AS bc2 ON bc2.recipe_id = r.id\n"
  44. "LEFT JOIN (SELECT rsc.recipe_id, rsc.comp_list, rsc.`index`, rcl.`name`, rsc.qty FROM recipe_secondary_comp rsc INNER JOIN recipe_comp_list rcl ON rcl.id = rsc.comp_list WHERE `index` = 2) AS bc3 ON bc3.recipe_id = r.id\n"
  45. "LEFT JOIN (SELECT rsc.recipe_id, rsc.comp_list, rsc.`index`, rcl.`name`, rsc.qty FROM recipe_secondary_comp rsc INNER JOIN recipe_comp_list rcl ON rcl.id = rsc.comp_list WHERE `index` = 3) AS bc4 ON bc4.recipe_id = r.id\n"
  46. "WHERE r.bHaveAllProducts");
  47. if (!status)
  48. return;
  49. while (res.Next()) {
  50. int32 i = 0;
  51. Recipe* recipe = new Recipe();
  52. recipe->SetID(res.GetInt32(i++));
  53. recipe->SetLevel(res.GetInt32(i++));
  54. recipe->SetTier(recipe->GetLevel() / 10 + 1);
  55. recipe->SetIcon(res.GetInt32(i++));
  56. recipe->SetSkill(res.GetInt32(i++));
  57. recipe->SetTechnique(res.GetInt32(i++));
  58. recipe->SetKnowledge(res.GetInt32(i++));
  59. recipe->SetName(res.GetString(i++));
  60. recipe->SetBook(res.GetString(i++));
  61. //Convert the device string
  62. string device = res.GetString(i++);
  63. int32 deviceID = 0;
  64. int8 deviceSubType = 0;
  65. recipe->SetDevice(GetDeviceName(device).c_str());
  66. recipe->SetUnknown2(deviceID);
  67. recipe->SetDevice_Sub_Type(deviceSubType);
  68. recipe->SetClasses(res.GetInt64(i++));
  69. recipe->SetUnknown3(0);
  70. recipe->SetUnknown4(0);
  71. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading recipe: %s (%u)", recipe->GetName(), recipe->GetID());
  72. recipe->SetProductID(res.GetInt32(i++));
  73. recipe->SetProductName(res.GetString(i++));
  74. recipe->SetProductQuantity(res.GetInt8(i++));
  75. recipe->SetPrimaryComponentTitle(res.GetString(i++));
  76. recipe->SetFuelComponentTitle(res.GetString(i++));
  77. recipe->SetFuelComponentQuantity(res.GetInt8(i++));
  78. recipe->SetBuildComponentTitle(res.GetString(i++));
  79. recipe->SetBuild1ComponentQuantity(res.GetInt8(i++));
  80. recipe->SetBuild2ComponentTitle(res.GetString(i++));
  81. recipe->SetBuild2ComponentQuantity(res.GetInt8(i++));
  82. recipe->SetBuild3ComponentTitle(res.GetString(i++));
  83. recipe->SetBuild3ComponentQuantity(res.GetInt8(i++));
  84. recipe->SetBuild4ComponentTitle(res.GetString(i++));
  85. recipe->SetBuild4ComponentQuantity(res.GetInt8(i++));
  86. LogWrite(TRADESKILL__DEBUG, 7, "Recipes", "Loading recipe: %s (%u)", recipe->GetName(), recipe->GetID());
  87. if (!master_recipe_list.AddRecipe(recipe)) {
  88. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding recipe '%s' - duplicate ID: %u", recipe->GetName(), recipe->GetID());
  89. delete recipe;
  90. continue;
  91. }
  92. //Products/By-Products
  93. for (int8 stage = 0; stage < 5; stage++) {
  94. RecipeProducts* rp = new RecipeProducts;
  95. rp->product_id = res.GetInt32(i);
  96. rp->product_qty = res.GetInt8(i + 5);
  97. rp->byproduct_id = res.GetInt32(i + 10);
  98. rp->byproduct_qty = res.GetInt8(i + 15);
  99. recipe->products[stage] = rp;
  100. i++;
  101. }
  102. //Advance i past all the product info
  103. //i += 15;
  104. }
  105. LoadRecipeComponents();
  106. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "\tLoaded %u recipes", master_recipe_list.Size());
  107. }
  108. void WorldDatabase::LoadRecipeBooks(){
  109. Recipe *recipe;
  110. Query query;
  111. MYSQL_ROW row;
  112. MYSQL_RES *res;
  113. res = query.RunQuery2(Q_SELECT, "SELECT id, name, tradeskill_default_level FROM items WHERE item_type='Recipe'");
  114. if (res){
  115. while ((row = mysql_fetch_row(res))){
  116. recipe = new Recipe();
  117. recipe->SetBookID(atoul(row[0]));
  118. recipe->SetBookName(row[1]);
  119. recipe->SetLevel(atoi(row[2]));
  120. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading Recipe Books: %s (%u)", recipe->GetBookName(), recipe->GetBookID());
  121. if (!master_recipebook_list.AddRecipeBook(recipe)){
  122. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding Recipe Book '%s' - duplicate ID: %u", recipe->GetBookName(), recipe->GetBookID());
  123. safe_delete(recipe);
  124. continue;
  125. }
  126. }
  127. }
  128. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "\tLoaded %u Recipe Books ", master_recipebook_list.Size());
  129. }
  130. void WorldDatabase::LoadPlayerRecipes(Player *player){
  131. Recipe *recipe;
  132. Query query;
  133. MYSQL_ROW row;
  134. MYSQL_RES *res;
  135. int16 total = 0;
  136. assert(player);
  137. res = query.RunQuery2(Q_SELECT, "SELECT recipe_id, highest_stage FROM character_recipes WHERE char_id = %u", player->GetCharacterID());
  138. if (res) {
  139. while ((row = mysql_fetch_row(res))){
  140. int32 recipe_id = atoul(row[0]);
  141. Recipe* master_recipe = master_recipe_list.GetRecipe(recipe_id);
  142. if(!master_recipe) {
  143. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding recipe %u to player '%s' - duplicate ID", atoul(row[0]), player->GetName());
  144. continue;
  145. }
  146. recipe = new Recipe(master_recipe);
  147. recipe->SetHighestStage(atoi(row[1]));
  148. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading recipe: %s (%u) for player: %s (%u)", recipe->GetName(), recipe->GetID(), player->GetName(), player->GetCharacterID());
  149. if (!player->GetRecipeList()->AddRecipe(recipe)){
  150. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding recipe %u to player '%s' - duplicate ID", recipe->GetID(), player->GetName());
  151. safe_delete(recipe);
  152. continue;
  153. }
  154. total++;
  155. }
  156. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "Loaded %u recipes for player: %s (%u)", total, player->GetName(), player->GetCharacterID());
  157. }
  158. }
  159. int32 WorldDatabase::LoadPlayerRecipeBooks(int32 char_id, Player *player) {
  160. assert(player);
  161. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "Loading Character Recipe Books for player '%s' ...", player->GetName());
  162. Query query;
  163. MYSQL_ROW row;
  164. MYSQL_RES *res;
  165. int32 count = 0;
  166. int32 old_id = 0;
  167. int32 new_id = 0;
  168. Recipe* recipe;
  169. res = query.RunQuery2(Q_SELECT, "SELECT recipebook_id FROM character_recipe_books WHERE char_id = %u", char_id);
  170. if (res && mysql_num_rows(res) > 0) {
  171. while (res && (row = mysql_fetch_row(res))){
  172. count++;
  173. new_id = atoul(row[0]);
  174. if(new_id == old_id)
  175. continue;
  176. Item* item = master_item_list.GetItem(new_id);
  177. if (!item)
  178. continue;
  179. recipe = new Recipe();
  180. recipe->SetBookID(new_id);
  181. recipe->SetBookName(item->name.c_str());
  182. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading Recipe Books: %s (%u)", recipe->GetBookName(), recipe->GetBookID());
  183. if (!player->GetRecipeBookList()->AddRecipeBook(recipe)) {
  184. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding player Recipe Book '%s' - duplicate ID: %u", recipe->GetBookName(), recipe->GetBookID());
  185. safe_delete(recipe);
  186. continue;
  187. }
  188. old_id = new_id;
  189. }
  190. }
  191. return count;
  192. }
  193. void WorldDatabase::SavePlayerRecipeBook(Player* player, int32 recipebook_id){
  194. Query query;
  195. query.AddQueryAsync(player->GetCharacterID(), this, Q_INSERT, "INSERT INTO character_recipe_books (char_id, recipebook_id) VALUES(%u, %u)", player->GetCharacterID(), recipebook_id);
  196. //if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  197. //LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error in SavePlayerRecipeBook query '%s' : %s", query.GetQuery(), query.GetError());
  198. }
  199. void WorldDatabase::SavePlayerRecipe(Player* player, int32 recipe_id) {
  200. Query query;
  201. query.AddQueryAsync(player->GetCharacterID(), this, Q_INSERT, "INSERT INTO character_recipes (char_id, recipe_id) VALUES (%u, %u)", player->GetCharacterID(), recipe_id);
  202. //if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  203. //LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error in SavePlayerRecipeBook query '%s' : %s", query.GetQuery(), query.GetError());
  204. }
  205. void WorldDatabase::LoadRecipeComponents() {
  206. DatabaseResult res;
  207. bool status = database_new.Select(&res,
  208. "SELECT r.id, pc.item_id AS primary_comp, fc.item_id AS fuel_comp, sc.item_id as secondary_comp, rsc.`index` + 1 AS slot\n"
  209. "FROM recipe r\n"
  210. "INNER JOIN (select comp_list, item_id FROM recipe_comp_list_item ) as pc ON r.primary_comp_list = pc.comp_list\n"
  211. "INNER JOIN (select comp_list, item_id FROM recipe_comp_list_item ) as fc ON r.fuel_comp_list = fc.comp_list\n"
  212. "LEFT JOIN recipe_secondary_comp rsc ON rsc.recipe_id = r.id\n"
  213. "LEFT JOIN (select comp_list, item_id FROM recipe_comp_list_item) as sc ON rsc.comp_list = sc.comp_list\n"
  214. "WHERE r.bHaveAllProducts\n"
  215. "ORDER BY r.id, rsc.`index` ASC");
  216. if (!status) {
  217. return;
  218. }
  219. Recipe* recipe = 0;
  220. int32 id = 0;
  221. while (res.Next()) {
  222. int32 tmp = res.GetInt32(0);
  223. if (id != tmp) {
  224. id = tmp;
  225. recipe = master_recipe_list.GetRecipe(id);
  226. if (!recipe) {
  227. continue;
  228. }
  229. recipe->AddBuildComp(res.GetInt32(1), 0);
  230. recipe->AddBuildComp(res.GetInt32(2), 5);
  231. }
  232. if (recipe && !res.IsNull(3)) {
  233. recipe->AddBuildComp(res.GetInt32(3), res.GetInt8(4));
  234. }
  235. //else
  236. //LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error loading `recipe_build_comps`, Recipe ID: %u", id);
  237. }
  238. }
  239. void WorldDatabase::UpdatePlayerRecipe(Player* player, int32 recipe_id, int8 highest_stage) {
  240. Query query;
  241. query.AddQueryAsync(player->GetCharacterID(), this, Q_UPDATE, "UPDATE `character_recipes` SET `highest_stage` = %i WHERE `char_id` = %u AND `recipe_id` = %u", highest_stage, player->GetCharacterID(), recipe_id);
  242. }
  243. /*
  244. ALTER TABLE `character_recipes`
  245. ADD COLUMN `highest_stage` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `recipe_id`;
  246. */