9
3

CommandsDB.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "Commands.h"
  25. #include "ConsoleCommands.h"
  26. map<int32, string>* WorldDatabase::GetSpawnTemplateListByName(const char* name)
  27. {
  28. LogWrite(COMMAND__DEBUG, 0, "Command", "Player listing spawn templates by template name ('%s')...", name);
  29. map<int32, string>* ret = 0;
  30. string template_name = "";
  31. Query query;
  32. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name FROM spawn_templates WHERE name RLIKE '%s' LIMIT 0,10", getSafeEscapeString(name).c_str());
  33. if(result && mysql_num_rows(result) > 0)
  34. {
  35. ret = new map<int32, string>;
  36. MYSQL_ROW row;
  37. while(result && (row = mysql_fetch_row(result)))
  38. {
  39. template_name = string(row[1]);
  40. (*ret)[atoul(row[0])] = template_name;
  41. LogWrite(COMMAND__DEBUG, 5, "Command", "\t%u. '%s'", atoul(row[0]), template_name.c_str());
  42. }
  43. }
  44. return ret;
  45. }
  46. map<int32, string>* WorldDatabase::GetSpawnTemplateListByID(int32 location_id)
  47. {
  48. LogWrite(COMMAND__DEBUG, 0, "Command", "Player listing spawn templates by LocaionID: %u...", location_id);
  49. map<int32, string>* ret = 0;
  50. string template_name = "";
  51. Query query;
  52. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name FROM spawn_templates WHERE spawn_location_id = %u", location_id);
  53. if(result && mysql_num_rows(result) > 0)
  54. {
  55. ret = new map<int32, string>;
  56. MYSQL_ROW row;
  57. while(result && (row = mysql_fetch_row(result)))
  58. {
  59. template_name = string(row[1]);
  60. (*ret)[atoul(row[0])] = template_name;
  61. LogWrite(COMMAND__DEBUG, 5, "Command", "\t%u. '%s'", atoul(row[0]), template_name.c_str());
  62. }
  63. }
  64. return ret;
  65. }
  66. int32 WorldDatabase::SaveSpawnTemplate(int32 placement_id, const char* template_name)
  67. {
  68. Query query;
  69. string str_name = getSafeEscapeString(template_name).c_str();
  70. LogWrite(COMMAND__DEBUG, 0, "Command", "Player saving spawn template '%s' for placement_id %u...", str_name.c_str(), placement_id);
  71. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_templates (name, spawn_location_id) VALUES ('%s', %u)", str_name.c_str(), placement_id);
  72. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  73. LogWrite(COMMAND__ERROR, 0, "Command", "Error in SaveSpawnTemplate query '%s': %s", query.GetQuery(), query.GetError());
  74. return 0;
  75. }
  76. int32 ret = query.GetLastInsertedID();
  77. LogWrite(COMMAND__DEBUG, 0, "Command", "Success! Returning TemplateID: %u...", ret);
  78. return ret;
  79. }
  80. bool WorldDatabase::RemoveSpawnTemplate(int32 template_id)
  81. {
  82. Query query;
  83. LogWrite(COMMAND__DEBUG, 0, "Command", "Player removing spawn template ID %u...", template_id);
  84. query.RunQuery2(Q_DELETE, "DELETE FROM spawn_templates WHERE id = %u", template_id);
  85. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  86. LogWrite(COMMAND__ERROR, 0, "Command", "Error in RemoveSpawnTemplate query '%s': %s", query.GetQuery(), query.GetError());
  87. return false;
  88. }
  89. if (query.GetAffectedRows() > 0 )
  90. {
  91. LogWrite(COMMAND__DEBUG, 0, "Command", "Success! Removed spawn template ID %u...", template_id);
  92. return true;
  93. }
  94. return false;
  95. }
  96. int32 WorldDatabase::CreateSpawnFromTemplateByID(Client* client, int32 template_id)
  97. {
  98. Query query;
  99. MYSQL_ROW row;
  100. int32 spawn_location_id = 0;
  101. float new_x = client->GetPlayer()->GetX();
  102. float new_y = client->GetPlayer()->GetY();
  103. float new_z = client->GetPlayer()->GetZ();
  104. float new_heading = client->GetPlayer()->GetHeading();
  105. LogWrite(COMMAND__DEBUG, 0, "Command", "Creating spawn point from templateID %u...", template_id);
  106. LogWrite(COMMAND__DEBUG, 5, "Command", "\tCoords: %.2f %.2f %.2f...", new_x, new_y, new_z);
  107. // find the spawn_location_id in the template we plan to duplicate
  108. Query query1;
  109. MYSQL_RES* result = query1.RunQuery2(Q_SELECT, "SELECT spawn_location_id FROM spawn_templates WHERE id = %u", template_id);
  110. if (result && (row = mysql_fetch_row(result))) {
  111. if (row[0])
  112. spawn_location_id = atoi(row[0]);
  113. }
  114. if( spawn_location_id > 0 )
  115. {
  116. LogWrite(COMMAND__DEBUG, 5, "Command", "\tUsing LocationID: %u...", spawn_location_id);
  117. // insert a new spawn_location_name record
  118. string name = "TemplateGenerated";
  119. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_name (name) VALUES ('%s')", name.c_str());
  120. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  121. LogWrite(COMMAND__ERROR, 0, "Command", "Error in CreateSpawnFromTemplateByID query '%s': %s", query.GetQuery(), query.GetError());
  122. return 0;
  123. }
  124. int32 new_location_id = query.GetLastInsertedID();
  125. LogWrite(COMMAND__DEBUG, 5, "Command", "Created new Spawn Location: '%s' (%u)", name.c_str(), new_location_id);
  126. // get all spawn_location_entries that match the templates spawn_location_id value and insert as new
  127. LogWrite(COMMAND__DEBUG, 5, "Command", "Finding existing spawn_location_entry(s) for location_id %u", spawn_location_id);
  128. Query query2;
  129. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT spawn_id, spawnpercentage FROM spawn_location_entry WHERE spawn_location_id = %u", spawn_location_id);
  130. if(result2 && mysql_num_rows(result2) > 0){
  131. MYSQL_ROW row2;
  132. while(result2 && (row2 = mysql_fetch_row(result2)) && row2[0])
  133. {
  134. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_entry (spawn_id, spawn_location_id, spawnpercentage) VALUES (%u, %u, %i)",
  135. atoul(row2[0]), new_location_id, atoi(row2[1]));
  136. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  137. LogWrite(COMMAND__ERROR, 0, "Command", "Error in CreateSpawnFromTemplateByID query '%s': %s", query.GetQuery(), query.GetError());
  138. return 0;
  139. }
  140. LogWrite(COMMAND__DEBUG, 5, "Command", "Insert Entry for spawn_id %u, location_id %u, percentage %i", atoul(row2[0]), new_location_id, atoi(row2[1]));
  141. }
  142. }
  143. // get all spawn_location_placements that match the templates spawn_location_id value and insert as new
  144. // Note: /spawn templates within current zone_id only, because of spawn_id issues (cannot template an Antonic spawn in Commonlands)
  145. LogWrite(COMMAND__DEBUG, 5, "Command", "Finding existing spawn_location_placement(s) for location_id %u", spawn_location_id);
  146. Query query3;
  147. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT zone_id, x_offset, y_offset, z_offset, respawn, expire_timer, expire_offset, grid_id FROM spawn_location_placement WHERE spawn_location_id = %u", spawn_location_id);
  148. if(result3 && mysql_num_rows(result3) > 0){
  149. MYSQL_ROW row3;
  150. while(result3 && (row3 = mysql_fetch_row(result3)) && row3[0])
  151. {
  152. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_placement (zone_id, spawn_location_id, x, y, z, heading, x_offset, y_offset, z_offset, respawn, expire_timer, expire_offset, grid_id) VALUES (%i, %u, %2f, %2f, %2f, %2f, %2f, %2f, %2f, %i, %i, %i, %u)",
  153. atoi(row3[0]), new_location_id, new_x, new_y, new_z, new_heading, atof(row3[1]), atof(row3[2]), atof(row3[3]), atoi(row3[4]), atoi(row3[5]), atoi(row3[6]), atoul(row3[7]));
  154. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  155. LogWrite(COMMAND__ERROR, 0, "Command", "Error in CreateSpawnFromTemplateByID query '%s': %s", query.GetQuery(), query.GetError());
  156. return 0;
  157. }
  158. LogWrite(COMMAND__DEBUG, 5, "Command", "Insert Placement at new coords for location_id %u", new_location_id);
  159. }
  160. }
  161. LogWrite(COMMAND__DEBUG, 0, "Command", "Success! New spawn(s) from TemplateID %u created from location %u", template_id, spawn_location_id);
  162. return new_location_id;
  163. }
  164. return 0;
  165. }
  166. int32 WorldDatabase::CreateSpawnFromTemplateByName(Client* client, const char* template_name)
  167. {
  168. Query query;
  169. MYSQL_ROW row;
  170. int32 template_id = 0;
  171. int32 spawn_location_id = 0;
  172. float new_x = client->GetPlayer()->GetX();
  173. float new_y = client->GetPlayer()->GetY();
  174. float new_z = client->GetPlayer()->GetZ();
  175. float new_heading = client->GetPlayer()->GetHeading();
  176. LogWrite(COMMAND__DEBUG, 0, "Command", "Creating spawn point from template '%s'...", template_name);
  177. LogWrite(COMMAND__DEBUG, 5, "Command", "\tCoords: %.2f %.2f %.2f...", new_x, new_y, new_z);
  178. // find the spawn_location_id in the template we plan to duplicate
  179. Query query1;
  180. MYSQL_RES* result = query1.RunQuery2(Q_SELECT, "SELECT id, spawn_location_id FROM spawn_templates WHERE name = '%s'", template_name);
  181. if (result && (row = mysql_fetch_row(result))) {
  182. if (row[0])
  183. {
  184. template_id = atoul(row[0]);
  185. spawn_location_id = atoi(row[1]);
  186. }
  187. }
  188. if( spawn_location_id > 0 )
  189. {
  190. LogWrite(COMMAND__DEBUG, 5, "Command", "\tUsing LocationID: %u...", spawn_location_id);
  191. // insert a new spawn_location_name record
  192. string name = "TemplateGenerated";
  193. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_name (name) VALUES ('%s')", name.c_str());
  194. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  195. LogWrite(COMMAND__ERROR, 0, "Command", "Error in CreateSpawnFromTemplateByID query '%s': %s", query.GetQuery(), query.GetError());
  196. return 0;
  197. }
  198. int32 new_location_id = query.GetLastInsertedID();
  199. LogWrite(COMMAND__DEBUG, 5, "Command", "Created new Spawn Location: '%s' (%u)", name.c_str(), new_location_id);
  200. // get all spawn_location_entries that match the templates spawn_location_id value and insert as new
  201. LogWrite(COMMAND__DEBUG, 5, "Command", "Finding existing spawn_location_entry(s) for location_id %u", spawn_location_id);
  202. Query query2;
  203. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT spawn_id, spawnpercentage FROM spawn_location_entry WHERE spawn_location_id = %u", spawn_location_id);
  204. if(result2 && mysql_num_rows(result2) > 0){
  205. MYSQL_ROW row2;
  206. while(result2 && (row2 = mysql_fetch_row(result2)) && row2[0])
  207. {
  208. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_entry (spawn_id, spawn_location_id, spawnpercentage) VALUES (%u, %u, %i)",
  209. atoul(row2[0]), new_location_id, atoi(row2[1]));
  210. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  211. LogWrite(COMMAND__ERROR, 0, "Command", "Error in CreateSpawnFromTemplateByID query '%s': %s", query.GetQuery(), query.GetError());
  212. return 0;
  213. }
  214. LogWrite(COMMAND__DEBUG, 5, "Command", "Insert Entry for spawn_id %u, location_id %u, percentage %i", atoul(row2[0]), new_location_id, atoi(row2[1]));
  215. }
  216. }
  217. // get all spawn_location_placements that match the templates spawn_location_id value and insert as new
  218. // Note: /spawn templates within current zone_id only, because of spawn_id issues (cannot template an Antonic spawn in Commonlands)
  219. LogWrite(COMMAND__DEBUG, 5, "Command", "Finding existing spawn_location_placement(s) for location_id %u", spawn_location_id);
  220. Query query3;
  221. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT zone_id, x_offset, y_offset, z_offset, respawn, expire_timer, expire_offset, grid_id FROM spawn_location_placement WHERE spawn_location_id = %u", spawn_location_id);
  222. if(result3 && mysql_num_rows(result3) > 0){
  223. MYSQL_ROW row3;
  224. while(result3 && (row3 = mysql_fetch_row(result3)) && row3[0])
  225. {
  226. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_placement (zone_id, spawn_location_id, x, y, z, heading, x_offset, y_offset, z_offset, respawn, expire_timer, expire_offset, grid_id) VALUES (%i, %u, %2f, %2f, %2f, %2f, %2f, %2f, %2f, %i, %i, %i, %u)",
  227. atoi(row3[0]), new_location_id, new_x, new_y, new_z, new_heading, atof(row3[1]), atof(row3[2]), atof(row3[3]), atoi(row3[4]), atoi(row3[5]), atoi(row3[6]), atoul(row3[7]));
  228. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  229. LogWrite(COMMAND__ERROR, 0, "Command", "Error in CreateSpawnFromTemplateByID query '%s': %s", query.GetQuery(), query.GetError());
  230. return 0;
  231. }
  232. LogWrite(COMMAND__DEBUG, 5, "Command", "Insert Placement at new coords for location_id %u", new_location_id);
  233. }
  234. }
  235. LogWrite(COMMAND__DEBUG, 0, "Command", "Success! New spawn(s) from TemplateID %u created from location %u", template_id, spawn_location_id);
  236. return new_location_id;
  237. }
  238. return 0;
  239. }
  240. bool WorldDatabase::SaveZoneSafeCoords(int32 zone_id, float x, float y, float z, float heading)
  241. {
  242. Query query;
  243. LogWrite(COMMAND__DEBUG, 0, "Command", "Setting safe coords for zone %u (X: %.2f, Y: %.2f, Z: %.2f, H: %.2f)", zone_id, x, y, z, heading);
  244. query.RunQuery2(Q_UPDATE, "UPDATE zones SET safe_x = %f, safe_y = %f, safe_z = %f, safe_heading = %f WHERE id = %u", x, y, z, heading, zone_id);
  245. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  246. LogWrite(DATABASE__ERROR, 0, "DBCore", "Error in SaveZoneSafeCoords query '%s': %s", query.GetQuery(), query.GetError());
  247. return false;
  248. }
  249. if (query.GetAffectedRows() > 0 )
  250. {
  251. LogWrite(COMMAND__DEBUG, 0, "Command", "Success! Set new safe coordinates in zone ID %u...", zone_id);
  252. return true;
  253. }
  254. LogWrite(COMMAND__ERROR, 0, "Command", "FAILED! Set new safe coordinates in zone ID %u...", zone_id);
  255. return false;
  256. }
  257. bool WorldDatabase::SaveSignZoneToCoords(int32 spawn_id, float x, float y, float z, float heading)
  258. {
  259. Query query;
  260. LogWrite(COMMAND__DEBUG, 0, "Command", "Setting Zone-To coords for Spawn ID %u (X: %.2f, Y: %.2f, Z: %.2f, H: %.2f)", spawn_id, x, y, z, heading);
  261. query.RunQuery2(Q_UPDATE, "UPDATE spawn_signs SET zone_x = %f, zone_y = %f, zone_z = %f, zone_heading = %f WHERE spawn_id = %u", x, y, z, heading, spawn_id);
  262. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  263. LogWrite(DATABASE__ERROR, 0, "DBCore", "Error in SaveSignZoneToCoords query '%s': %s", query.GetQuery(), query.GetError());
  264. return false;
  265. }
  266. if (query.GetAffectedRows() > 0 )
  267. {
  268. LogWrite(COMMAND__DEBUG, 0, "Command", "Success! Set new Zone-To coordinates in zone ID %u...", spawn_id);
  269. return true;
  270. }
  271. LogWrite(COMMAND__ERROR, 0, "Command", "FAILED! Set new Zone-To coordinates in zone ID %u...", spawn_id);
  272. return false;
  273. }