RecipeDB.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 item_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.item_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 AND r.`bench` IN ('chemistry_table','work_desk','forge','stove and keg','sewing_table','woodworking_table','work_bench')");
  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.GetSInt32(i++));
  54. recipe->SetTier(recipe->GetLevel() / 10);
  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. if (device == "chemistry_table") {
  65. device = "Chemistry Table";
  66. deviceID = 3;
  67. }
  68. else if (device == "work_desk") {
  69. device = "Engraved Desk";
  70. deviceID = 4;
  71. }
  72. else if (device == "forge") {
  73. device = "Forge";
  74. deviceID = 2;
  75. }
  76. else if (device == "stove and keg") {
  77. device = "Stove & Keg";
  78. deviceID = 7;
  79. }
  80. else if (device == "sewing_table") {
  81. device = "Sewing Table & Mannequin";
  82. deviceID = 1;
  83. }
  84. else if (device == "woodworking_table") {
  85. device = "Woodworking Table";
  86. deviceID = 6;
  87. }
  88. else if (device == "work_bench") {
  89. device = "Work Bench";
  90. deviceID = 5;
  91. }
  92. recipe->SetDevice(device.c_str());
  93. recipe->SetUnknown2(deviceID);
  94. recipe->SetClasses(res.GetInt64(i++));
  95. recipe->SetUnknown3(0);
  96. recipe->SetUnknown4(0);
  97. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading recipe: %s (%u)", recipe->GetName(), recipe->GetID());
  98. recipe->SetProductID(res.GetInt32(i++));
  99. recipe->SetProductName(res.GetString(i++));
  100. recipe->SetProductQuantity(res.GetInt8(i++));
  101. recipe->SetPrimaryComponentTitle(res.GetString(i++));
  102. recipe->SetFuelComponentTitle(res.GetString(i++));
  103. recipe->SetFuelComponentQuantity(res.GetInt8(i++));
  104. recipe->SetBuildComponentTitle(res.GetString(i++));
  105. recipe->SetBuild1ComponentQuantity(res.GetInt8(i++));
  106. recipe->SetBuild2ComponentTitle(res.GetString(i++));
  107. recipe->SetBuild2ComponentQuantity(res.GetInt8(i++));
  108. recipe->SetBuild3ComponentTitle(res.GetString(i++));
  109. recipe->SetBuild3ComponentQuantity(res.GetInt8(i++));
  110. recipe->SetBuild4ComponentTitle(res.GetString(i++));
  111. recipe->SetBuild4ComponentQuantity(res.GetInt8(i++));
  112. LogWrite(TRADESKILL__DEBUG, 7, "Recipes", "Loading recipe: %s (%u)", recipe->GetName(), recipe->GetID());
  113. if (!master_recipe_list.AddRecipe(recipe)) {
  114. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding recipe '%s' - duplicate ID: %u", recipe->GetName(), recipe->GetID());
  115. delete recipe;
  116. continue;
  117. }
  118. //Products/By-Products
  119. for (int8 stage = 0; stage < 5; stage++) {
  120. RecipeProducts* rp = new RecipeProducts;
  121. rp->product_id = res.GetInt32(i);
  122. rp->product_qty = res.GetInt8(i + 5);
  123. rp->byproduct_id = res.GetInt32(i + 10);
  124. rp->byproduct_qty = res.GetInt8(i + 15);
  125. recipe->products[stage] = rp;
  126. i++;
  127. }
  128. //Advance i past all the product info
  129. //i += 15;
  130. }
  131. LoadRecipeComponents();
  132. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "\tLoaded %u recipes", master_recipe_list.Size());
  133. }
  134. void WorldDatabase::LoadRecipeBooks(){
  135. Recipe *recipe;
  136. Query query;
  137. MYSQL_ROW row;
  138. MYSQL_RES *res;
  139. res = query.RunQuery2(Q_SELECT, "SELECT id, name, tradeskill_default_level FROM items WHERE item_type='Recipe'");
  140. if (res){
  141. while ((row = mysql_fetch_row(res))){
  142. recipe = new Recipe();
  143. recipe->SetBookID(atoul(row[0]));
  144. recipe->SetBookName(row[1]);
  145. recipe->SetLevel(atoi(row[2]));
  146. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading Recipe Books: %s (%u)", recipe->GetBookName(), recipe->GetBookID());
  147. if (!master_recipebook_list.AddRecipeBook(recipe)){
  148. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding Recipe Book '%s' - duplicate ID: %u", recipe->GetBookName(), recipe->GetBookID());
  149. safe_delete(recipe);
  150. continue;
  151. }
  152. }
  153. }
  154. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "\tLoaded %u Recipe Books ", master_recipebook_list.Size());
  155. }
  156. void WorldDatabase::LoadPlayerRecipes(Player *player){
  157. Recipe *recipe;
  158. Query query;
  159. MYSQL_ROW row;
  160. MYSQL_RES *res;
  161. int16 total = 0;
  162. assert(player);
  163. res = query.RunQuery2(Q_SELECT, "SELECT recipe_id, highest_stage FROM character_recipes WHERE char_id = %u", player->GetCharacterID());
  164. if (res) {
  165. while ((row = mysql_fetch_row(res))){
  166. recipe = new Recipe(master_recipe_list.GetRecipe(atoul(row[0])));
  167. recipe->SetHighestStage(atoi(row[1]));
  168. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading recipe: %s (%u) for player: %s (%u)", recipe->GetName(), recipe->GetID(), player->GetName(), player->GetCharacterID());
  169. if (!player->GetRecipeList()->AddRecipe(recipe)){
  170. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding recipe %u to player '%s' - duplicate ID", recipe->GetID(), player->GetName());
  171. safe_delete(recipe);
  172. continue;
  173. }
  174. total++;
  175. }
  176. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "Loaded %u recipes for player: %s (%u)", total, player->GetName(), player->GetCharacterID());
  177. }
  178. }
  179. int32 WorldDatabase::LoadPlayerRecipeBooks(int32 char_id, Player *player) {
  180. assert(player);
  181. LogWrite(TRADESKILL__DEBUG, 0, "Recipes", "Loading Character Recipe Books for player '%s' ...", player->GetName());
  182. Query query;
  183. MYSQL_ROW row;
  184. MYSQL_RES *res;
  185. int32 count = 0;
  186. int32 old_id = 0;
  187. int32 new_id = 0;
  188. Recipe* recipe;
  189. res = query.RunQuery2(Q_SELECT, "SELECT recipebook_id FROM character_recipe_books WHERE char_id = %u", char_id);
  190. if (res && mysql_num_rows(res) > 0) {
  191. while (res && (row = mysql_fetch_row(res))){
  192. count++;
  193. new_id = atoul(row[0]);
  194. if(new_id == old_id)
  195. continue;
  196. Item* item = master_item_list.GetItem(new_id);
  197. if (!item)
  198. continue;
  199. recipe = new Recipe();
  200. recipe->SetBookID(new_id);
  201. recipe->SetBookName(item->name.c_str());
  202. LogWrite(TRADESKILL__DEBUG, 5, "Recipes", "Loading Recipe Books: %s (%u)", recipe->GetBookName(), recipe->GetBookID());
  203. if (!player->GetRecipeBookList()->AddRecipeBook(recipe)) {
  204. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error adding player Recipe Book '%s' - duplicate ID: %u", recipe->GetBookName(), recipe->GetBookID());
  205. safe_delete(recipe);
  206. continue;
  207. }
  208. old_id = new_id;
  209. }
  210. }
  211. return count;
  212. }
  213. void WorldDatabase::SavePlayerRecipeBook(Player* player, int32 recipebook_id){
  214. Query query;
  215. query.AddQueryAsync(player->GetCharacterID(), this, Q_INSERT, "INSERT INTO character_recipe_books (char_id, recipebook_id) VALUES(%u, %u)", player->GetCharacterID(), recipebook_id);
  216. //if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  217. //LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error in SavePlayerRecipeBook query '%s' : %s", query.GetQuery(), query.GetError());
  218. }
  219. void WorldDatabase::SavePlayerRecipe(Player* player, int32 recipe_id) {
  220. Query query;
  221. query.AddQueryAsync(player->GetCharacterID(), this, Q_INSERT, "INSERT INTO character_recipes (char_id, recipe_id) VALUES (%u, %u)", player->GetCharacterID(), recipe_id);
  222. //if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  223. //LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error in SavePlayerRecipeBook query '%s' : %s", query.GetQuery(), query.GetError());
  224. }
  225. void WorldDatabase::LoadRecipeComponents() {
  226. DatabaseResult res;
  227. bool status = database_new.Select(&res,
  228. "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"
  229. "FROM recipe r\n"
  230. "INNER JOIN (select comp_list, item_id FROM recipe_comp_list_item GROUP BY comp_list) as pc ON r.primary_comp_list = pc.comp_list\n"
  231. "INNER JOIN (select comp_list, item_id FROM recipe_comp_list_item GROUP BY comp_list) as fc ON r.fuel_comp_list = fc.comp_list\n"
  232. "LEFT JOIN recipe_secondary_comp rsc ON rsc.recipe_id = r.id\n"
  233. "LEFT JOIN (select comp_list, item_id FROM recipe_comp_list_item GROUP BY comp_list) as sc ON rsc.comp_list = sc.comp_list\n"
  234. "WHERE r.bHaveAllProducts\n"
  235. "ORDER BY r.id, rsc.`index` ASC");
  236. if (!status) {
  237. return;
  238. }
  239. Recipe* recipe = 0;
  240. int32 id = 0;
  241. while (res.Next()) {
  242. int32 tmp = res.GetInt32(0);
  243. if (id != tmp) {
  244. id = tmp;
  245. recipe = master_recipe_list.GetRecipe(id);
  246. if (!recipe) {
  247. continue;
  248. }
  249. recipe->AddBuildComp(res.GetInt32(1), 0);
  250. recipe->AddBuildComp(res.GetInt32(2), 5);
  251. }
  252. if (recipe && !res.IsNull(3)) {
  253. recipe->AddBuildComp(res.GetInt32(3), res.GetInt8(4));
  254. }
  255. //else
  256. //LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error loading `recipe_build_comps`, Recipe ID: %u", id);
  257. }
  258. }
  259. void WorldDatabase::UpdatePlayerRecipe(Player* player, int32 recipe_id, int8 highest_stage) {
  260. Query query;
  261. 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);
  262. }
  263. /*
  264. ALTER TABLE `character_recipes`
  265. ADD COLUMN `highest_stage` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `recipe_id`;
  266. */