LoginDatabase.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. */
  6. #include "../common/debug.h"
  7. #include <iostream>
  8. using namespace std;
  9. #ifdef WIN32
  10. #define WIN32_LEAN_AND_MEAN
  11. #include <windows.h>
  12. #define snprintf _snprintf
  13. #define strncasecmp _strnicmp
  14. #define strcasecmp _stricmp
  15. #else
  16. #include "../common/unix.h"
  17. #include <netinet/in.h>
  18. #endif
  19. #include "../common/Log.h"
  20. #include "LoginDatabase.h"
  21. #include "LoginAccount.h"
  22. #include "../common/MiscFunctions.h"
  23. #include "../common/packet_functions.h"
  24. #include "../common/packet_dump.h"
  25. #include "LWorld.h"
  26. extern LoginDatabase database;
  27. extern LWorldList world_list;
  28. void LoginDatabase::SetZoneInformation(int32 server_id, int32 zone_id, int32 version, PacketStruct* packet){
  29. if(packet){
  30. Query query;
  31. MYSQL_RES* result = 0;
  32. if ( version >= 1212 )
  33. result = query.RunQuery2(Q_SELECT, "SELECT name, description from ls_world_zones where server_id=%i and zone_id=%i", server_id, zone_id);
  34. MYSQL_ROW row;
  35. if(result && (row = mysql_fetch_row(result))) {
  36. if (row[0])
  37. packet->setMediumStringByName("zone", row[0]);
  38. else
  39. packet->setMediumStringByName("zone", " ");
  40. if(row[1])
  41. packet->setMediumStringByName("zonedesc", row[1]);
  42. else
  43. packet->setMediumStringByName("zonedesc", " ");
  44. }
  45. else{
  46. Query query2;
  47. MYSQL_RES* result2 = 0;
  48. if (version < 1212)
  49. result2 = query2.RunQuery2(Q_SELECT, "SELECT file, description from zones where id=%i", zone_id);
  50. else
  51. result2 = query2.RunQuery2(Q_SELECT, "SELECT name, description from zones where id=%i", zone_id);
  52. MYSQL_ROW row2;
  53. if(result2 && (row2 = mysql_fetch_row(result2))) {
  54. if (version != 546 && version < 1212)
  55. {
  56. if (row2[0])
  57. {
  58. int len = strlen(row2[0]);
  59. char* zoneName = new char[len + 2];
  60. strncpy(zoneName, row2[0], len);
  61. zoneName[len] = 0x2E;
  62. zoneName[len + 1] = 0x30;
  63. packet->setMediumStringByName("zone", zoneName);
  64. safe_delete_array(zoneName);
  65. }
  66. else
  67. packet->setMediumStringByName("zone", ".0");
  68. }
  69. else
  70. {
  71. if (row2[0])
  72. packet->setMediumStringByName("zone", row2[0]);
  73. else
  74. packet->setMediumStringByName("zone", " ");
  75. }
  76. if(row2[1])
  77. packet->setMediumStringByName("zonedesc", row2[1]);
  78. else
  79. packet->setMediumStringByName("zonedesc", " ");
  80. }
  81. }
  82. packet->setMediumStringByName("zonename2"," ");
  83. }
  84. }
  85. string LoginDatabase::GetZoneDescription(char* name){
  86. string ret;
  87. Query query;
  88. query.escaped_name = getEscapeString(name);
  89. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT description from zones where file=substring_index('%s', '.', 1)", query.escaped_name);
  90. MYSQL_ROW row;
  91. if((row = mysql_fetch_row(result))) {
  92. ret = string(row[0]);
  93. }
  94. return ret;
  95. }
  96. void LoginDatabase::SetServerZoneDescriptions(int32 server_id, map<int32, LoginZoneUpdate> zone_descriptions){
  97. if(zone_descriptions.size() > 0){
  98. map<int32, LoginZoneUpdate>::iterator zone_itr;
  99. string query_string = "replace into ls_world_zones (server_id, zone_id, name, description) values";
  100. int count=0;
  101. char server_id_str[12] = {0};
  102. sprintf(server_id_str, "%i", server_id);
  103. for(zone_itr = zone_descriptions.begin(); zone_itr != zone_descriptions.end(); zone_itr++, count++){
  104. char zone_id_str[12] = {0};
  105. sprintf(zone_id_str, "%i", zone_itr->first);
  106. if(count > 0)
  107. query_string.append(", ");
  108. query_string.append("(").append(server_id_str).append(",");
  109. query_string.append(zone_id_str).append(",");
  110. query_string.append("'").append(getSafeEscapeString(zone_itr->second.name.c_str()).c_str()).append("', '");
  111. query_string.append(getSafeEscapeString(zone_itr->second.description.c_str()).c_str()).append("')");
  112. }
  113. Query query;
  114. query.RunQuery2(query_string, Q_REPLACE);
  115. }
  116. }
  117. void LoginDatabase::LoadCharacters(LoginAccount* acct, int16 version){
  118. if(acct != NULL)
  119. acct->flushCharacters ( );
  120. Query query;
  121. Query query2;
  122. int32 id = 0;
  123. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT lc.char_id, lc.server_id, lc.name, lc.race, lc.class, lc.gender, lc.deity, lc.body_size, lc.body_age, lc.current_zone_id, lc.level, lc.soga_wing_type, lc.soga_chest_type, lc.soga_legs_type, lc.soga_hair_type, lc.legs_type, lc.chest_type, lc.wing_type, lc.hair_type, unix_timestamp(lc.created_date), unix_timestamp(lc.last_played), lc.id, lw.name, lc.facial_hair_type, lc.soga_facial_hair_type, lc.soga_model_type, lc.model_type from login_characters lc, login_worldservers lw where lw.id = lc.server_id and lc.account_id=%i and lc.deleted=0",acct->getLoginAccountID());
  124. if(result) {
  125. MYSQL_ROW row;
  126. MYSQL_ROW row2;
  127. MYSQL_ROW row3;
  128. while ((row = mysql_fetch_row(result))) {
  129. CharSelectProfile* player = new CharSelectProfile(version);
  130. id = atoul(row[0]);
  131. player->packet->setDataByName("charid", id);
  132. player->packet->setDataByName("server_id", atoul(row[1]));
  133. player->packet->setMediumStringByName("name", row[2]);
  134. player->packet->setDataByName("race", atoi(row[3]));
  135. player->packet->setDataByName("class", atoi(row[4]));
  136. player->packet->setDataByName("gender", atoi(row[5]));
  137. player->packet->setDataByName("deity", atoi(row[6]));
  138. player->packet->setDataByName("body_size", atof(row[7]));
  139. player->packet->setDataByName("body_age", atof(row[8]));
  140. SetZoneInformation(atoi(row[1]), atoi(row[9]), version, player->packet);
  141. player->packet->setDataByName("level", atoi(row[10]));
  142. player->packet->setDataByName("soga_wing_type", atoi(row[11]));
  143. player->packet->setDataByName("soga_chest_type", atoi(row[12]));
  144. player->packet->setDataByName("soga_legs_type", atoi(row[13]));
  145. player->packet->setDataByName("soga_hair_type", atoi(row[14]));
  146. player->packet->setDataByName("legs_type", atoi(row[15]));
  147. player->packet->setDataByName("chest_type", atoi(row[16]));
  148. player->packet->setDataByName("wing_type", atoi(row[17]));
  149. player->packet->setDataByName("hair_type", atoi(row[18]));
  150. player->packet->setDataByName("created_date", atol(row[19]));
  151. if (row[20])
  152. player->packet->setDataByName("last_played", atol(row[20]));
  153. if(version >= 887)
  154. player->packet->setDataByName("version", 6);
  155. else if ( version == 546 )
  156. player->packet->setDataByName("version", 11);
  157. else
  158. player->packet->setDataByName("version", 5);
  159. player->packet->setDataByName("client_version", version);
  160. player->packet->setDataByName("account_id", acct->getLoginAccountID());
  161. player->packet->setDataByName("account_id2", acct->getLoginAccountID());
  162. LoadAppearanceData(atol(row[21]), player->packet);
  163. if(row[22])
  164. player->packet->setMediumStringByName("server_name", row[22]);
  165. player->packet->setDataByName("hair_face_type", atoi(row[23]));
  166. player->packet->setDataByName("soga_hair_face_type", atoi(row[24]));
  167. player->packet->setDataByName("soga_race_type", atoi(row[25]));
  168. player->packet->setDataByName("race_type", atoi(row[26]));
  169. player->packet->setDataByName("unknown3", 57);
  170. player->packet->setDataByName("unknown4", 56);
  171. player->packet->setDataByName("unknown6", 1, 1); //if not here will not display character
  172. player->packet->setDataByName("unknown8", 15);
  173. player->packet->setDataByName("unknown13", 212);
  174. player->packet->setColorByName("unknown14", 0xFF, 0xFF, 0xFF);
  175. uchar tmp[] = {0xFF, 0xFF, 0xFF, 0x61, 0x00, 0x2C, 0x04, 0xA5, 0x09, 0x02, 0x0F, 0x00, 0x00};
  176. for(int y=0;y<sizeof(tmp);y++)
  177. player->packet->setDataByName("unknown11", tmp[y], y);
  178. MYSQL_RES* result3 = query2.RunQuery2(Q_SELECT, "SELECT slot, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue from login_equipment where login_characters_id=%i order by slot",id);
  179. if(result3){
  180. for(int i=0;(row3 = mysql_fetch_row(result3)) && i<24; i++){
  181. player->packet->setEquipmentByName("equip", atoi(row3[1]), atoi(row3[2]), atoi(row3[3]), atoi(row3[4]), atoi(row3[5]), atoi(row3[6]), atoi(row3[7]), atoi(row3[0]));
  182. }
  183. }
  184. acct->addCharacter(player);
  185. }
  186. }
  187. else
  188. LogWrite(LOGIN__ERROR, 0, "Login", "Error in LoadCharacters query '%s': %s", query.GetQuery(), query.GetError());
  189. }
  190. void LoginDatabase::CheckCharacterTimeStamps(LoginAccount* acct){
  191. Query query;
  192. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT char_id, unix_timestamp from login_characters where account_id=%i",acct->getLoginAccountID());
  193. if(result && mysql_num_rows(result) > 0) {
  194. MYSQL_ROW row;
  195. ServerPacket* outpack = new ServerPacket(ServerOP_CharTimeStamp, sizeof(CharacterTimeStamp_Struct));
  196. CharacterTimeStamp_Struct* cts = (CharacterTimeStamp_Struct*) outpack->pBuffer;
  197. cts->account_id = acct->getLoginAccountID();
  198. int32 server_id = 0;
  199. LWorld* world_server = 0;
  200. while ((row = mysql_fetch_row(result))) {
  201. server_id = atoi(row[1]);
  202. if(server_id != 0)
  203. world_server = world_list.FindByAccount(server_id, World);
  204. if(world_server) // If the pointer is 0, the world server must be down, we can't do any updates...
  205. {
  206. cts->char_id = atoi(row[0]);
  207. cts->unix_timestamp = atoi(row[1]);
  208. world_server->SendPacket(outpack);
  209. //Reset for next character
  210. world_server = 0;
  211. server_id = 0;
  212. }
  213. }
  214. safe_delete(outpack);
  215. }
  216. }
  217. void LoginDatabase::SaveCharacterFloats(int32 char_id, char* type, float float1, float float2, float float3){
  218. Query query;
  219. string create_char = string("insert into login_char_colors (login_characters_id, type, red, green, blue, signed_value) values(%i,'%s',%i,%i,%i, 1)");
  220. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, (sint8)(float1*100), (sint8)(float2*100), (sint8)(float3*100));
  221. }
  222. void LoginDatabase::SaveCharacterColors(int32 char_id, char* type, EQ2_Color color){
  223. Query query;
  224. string create_char = string("insert into login_char_colors (login_characters_id, type, red, green, blue) values(%i,'%s',%i,%i,%i)");
  225. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, color.red, color.green, color.blue);
  226. }
  227. void LoginDatabase::LoadAppearanceData(int32 char_id, PacketStruct* char_select_packet){
  228. Query query;
  229. MYSQL_ROW row;
  230. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT type, signed_value, red, green, blue from login_char_colors where login_characters_id = %i",char_id);
  231. while((row = mysql_fetch_row(result))){
  232. if(atoi(row[1]) == 0)
  233. char_select_packet->setColorByName(row[0], atoi(row[2]), atoi(row[3]), atoi(row[4]));
  234. else{
  235. char_select_packet->setDataByName(row[0], atoi(row[2]),0);
  236. char_select_packet->setDataByName(row[0], atoi(row[3]),1);
  237. char_select_packet->setDataByName(row[0], atoi(row[4]),2);
  238. }
  239. }
  240. }
  241. int16 LoginDatabase::GetAppearanceID(string name){
  242. int32 id = 0;
  243. Query query;
  244. MYSQL_ROW row;
  245. query.escaped_name = getEscapeString(name.c_str());
  246. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT appearance_id from appearances where name='%s'", query.escaped_name);
  247. if(result && mysql_num_rows(result) == 1){
  248. row = mysql_fetch_row(result);
  249. id = atoi(row[0]);
  250. }
  251. return id;
  252. }
  253. void LoginDatabase::DeactivateCharID(int32 server_id, int32 char_id, int32 exception_id){
  254. Query query;
  255. query.RunQuery2(Q_UPDATE, "update login_characters set deleted=1 where char_id=%u and server_id=%u and id!=%u",char_id,server_id,exception_id);
  256. }
  257. int32 LoginDatabase::SaveCharacter(PacketStruct* create, LoginAccount* acct, int32 world_charid){
  258. int32 ret_id = 0;
  259. Query query;
  260. string create_char =
  261. string("Insert into login_characters (account_id, server_id, char_id, name, race, class, gender, deity, body_size, body_age, soga_wing_type, soga_chest_type, soga_legs_type, soga_hair_type, soga_facial_hair_type, legs_type, chest_type, wing_type, hair_type, facial_hair_type, soga_model_type, model_type)"
  262. " values(%i, %i, %i, '%s', %i, %i, %i, %i, %f, %f, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i)");
  263. query.RunQuery2(Q_INSERT, create_char.c_str(),
  264. acct->getLoginAccountID(),
  265. create->getType_int32_ByName("server_id"), world_charid,
  266. create->getType_EQ2_16BitString_ByName("name").data.c_str(),
  267. create->getType_int8_ByName("race"),
  268. create->getType_int8_ByName("class"),
  269. create->getType_int8_ByName("gender"),
  270. create->getType_int8_ByName("deity"),
  271. create->getType_float_ByName("body_size"),
  272. create->getType_float_ByName("body_age"),
  273. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_wing_file").data),
  274. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_chest_file").data),
  275. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_legs_file").data),
  276. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_hair_file").data),
  277. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_face_file").data),
  278. GetAppearanceID(create->getType_EQ2_16BitString_ByName("legs_file").data),
  279. GetAppearanceID(create->getType_EQ2_16BitString_ByName("chest_file").data),
  280. GetAppearanceID(create->getType_EQ2_16BitString_ByName("wing_file").data),
  281. GetAppearanceID(create->getType_EQ2_16BitString_ByName("hair_file").data),
  282. GetAppearanceID(create->getType_EQ2_16BitString_ByName("face_file").data),
  283. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_race_file").data),
  284. GetAppearanceID(create->getType_EQ2_16BitString_ByName("race_file").data));
  285. if(query.GetError() && strlen(query.GetError()) > 0){
  286. LogWrite(LOGIN__ERROR, 0, "Login", "Error in SaveCharacter query '%s': %s", query.GetQuery(), query.GetError());
  287. return 0;
  288. }
  289. int32 last_insert_id = query.GetLastInsertedID();
  290. //mark any remaining characters with same id as deleted (creates problems if world deleted their db and started assigning new char ids)
  291. DeactivateCharID(create->getType_int32_ByName("server_id"), world_charid, last_insert_id);
  292. int32 char_id = last_insert_id;
  293. SaveCharacterColors(char_id,"skin_color", create->getType_EQ2_Color_ByName("skin_color"));
  294. SaveCharacterColors(char_id,"eye_color", create->getType_EQ2_Color_ByName("eye_color"));
  295. SaveCharacterColors(char_id,"hair_color1", create->getType_EQ2_Color_ByName("hair_color1"));
  296. SaveCharacterColors(char_id,"hair_color2", create->getType_EQ2_Color_ByName("hair_color2"));
  297. SaveCharacterColors(char_id,"hair_highlight", create->getType_EQ2_Color_ByName("hair_highlight"));
  298. SaveCharacterColors(char_id,"hair_type_color", create->getType_EQ2_Color_ByName("hair_type_color"));
  299. SaveCharacterColors(char_id,"hair_type_highlight_color", create->getType_EQ2_Color_ByName("hair_type_highlight_color"));
  300. SaveCharacterColors(char_id,"hair_face_color", create->getType_EQ2_Color_ByName("hair_face_color"));
  301. SaveCharacterColors(char_id,"hair_face_highlight_color", create->getType_EQ2_Color_ByName("hair_face_highlight_color"));
  302. SaveCharacterColors(char_id,"wing_color1", create->getType_EQ2_Color_ByName("wing_color1"));
  303. SaveCharacterColors(char_id,"wing_color2", create->getType_EQ2_Color_ByName("wing_color2"));
  304. SaveCharacterColors(char_id,"shirt_color", create->getType_EQ2_Color_ByName("shirt_color"));
  305. SaveCharacterColors(char_id,"unknown_chest_color", create->getType_EQ2_Color_ByName("unknown_chest_color"));
  306. SaveCharacterColors(char_id,"pants_color", create->getType_EQ2_Color_ByName("pants_color"));
  307. SaveCharacterColors(char_id,"unknown_legs_color", create->getType_EQ2_Color_ByName("unknown_legs_color"));
  308. SaveCharacterColors(char_id,"unknown9", create->getType_EQ2_Color_ByName("unknown9"));
  309. SaveCharacterFloats(char_id,"eye_type", create->getType_float_ByName("eyes2",0), create->getType_float_ByName("eyes2",1), create->getType_float_ByName("eyes2",2));
  310. SaveCharacterFloats(char_id,"ear_type", create->getType_float_ByName("ears",0), create->getType_float_ByName("ears",1), create->getType_float_ByName("ears",2));
  311. SaveCharacterFloats(char_id,"eye_brow_type", create->getType_float_ByName("eye_brows",0), create->getType_float_ByName("eye_brows",1), create->getType_float_ByName("eye_brows",2));
  312. SaveCharacterFloats(char_id,"cheek_type", create->getType_float_ByName("cheeks",0), create->getType_float_ByName("cheeks",1), create->getType_float_ByName("cheeks",2));
  313. SaveCharacterFloats(char_id,"lip_type", create->getType_float_ByName("lips",0), create->getType_float_ByName("lips",1), create->getType_float_ByName("lips",2));
  314. SaveCharacterFloats(char_id,"chin_type", create->getType_float_ByName("chin",0), create->getType_float_ByName("chin",1), create->getType_float_ByName("chin",2));
  315. SaveCharacterFloats(char_id,"nose_type", create->getType_float_ByName("nose",0), create->getType_float_ByName("nose",1), create->getType_float_ByName("nose",2));
  316. SaveCharacterFloats(char_id,"body_size", create->getType_float_ByName("body_size",0), 0, 0);
  317. SaveCharacterColors(char_id,"soga_skin_color", create->getType_EQ2_Color_ByName("soga_skin_color"));
  318. SaveCharacterColors(char_id,"soga_eye_color", create->getType_EQ2_Color_ByName("soga_eye_color"));
  319. SaveCharacterColors(char_id,"soga_hair_color1", create->getType_EQ2_Color_ByName("soga_hair_color1"));
  320. SaveCharacterColors(char_id,"soga_hair_color2", create->getType_EQ2_Color_ByName("soga_hair_color2"));
  321. SaveCharacterColors(char_id,"soga_hair_highlight", create->getType_EQ2_Color_ByName("soga_hair_highlight"));
  322. SaveCharacterColors(char_id,"soga_hair_type_color", create->getType_EQ2_Color_ByName("soga_hair_type_color"));
  323. SaveCharacterColors(char_id,"soga_hair_type_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_type_highlight_color"));
  324. SaveCharacterColors(char_id,"soga_hair_face_color", create->getType_EQ2_Color_ByName("soga_hair_face_color"));
  325. SaveCharacterColors(char_id,"soga_hair_face_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_face_highlight_color"));
  326. SaveCharacterColors(char_id,"soga_wing_color1", create->getType_EQ2_Color_ByName("soga_wing_color1"));
  327. SaveCharacterColors(char_id,"soga_wing_color2", create->getType_EQ2_Color_ByName("soga_wing_color2"));
  328. SaveCharacterColors(char_id,"soga_shirt_color", create->getType_EQ2_Color_ByName("soga_shirt_color"));
  329. SaveCharacterColors(char_id,"soga_unknown_chest_color", create->getType_EQ2_Color_ByName("soga_unknown_chest_color"));
  330. SaveCharacterColors(char_id,"soga_pants_color", create->getType_EQ2_Color_ByName("soga_pants_color"));
  331. SaveCharacterColors(char_id,"soga_unknown_legs_color", create->getType_EQ2_Color_ByName("soga_unknown_legs_color"));
  332. SaveCharacterColors(char_id,"soga_unknown13", create->getType_EQ2_Color_ByName("soga_unknown13"));
  333. SaveCharacterFloats(char_id,"soga_eye_type", create->getType_float_ByName("soga_eyes2",0), create->getType_float_ByName("soga_eyes2",1), create->getType_float_ByName("soga_eyes2",2));
  334. SaveCharacterFloats(char_id,"soga_ear_type", create->getType_float_ByName("soga_ears",0), create->getType_float_ByName("soga_ears",1), create->getType_float_ByName("soga_ears",2));
  335. SaveCharacterFloats(char_id,"soga_eye_brow_type", create->getType_float_ByName("soga_eye_brows",0), create->getType_float_ByName("soga_eye_brows",1), create->getType_float_ByName("soga_eye_brows",2));
  336. SaveCharacterFloats(char_id,"soga_cheek_type", create->getType_float_ByName("soga_cheeks",0), create->getType_float_ByName("soga_cheeks",1), create->getType_float_ByName("soga_cheeks",2));
  337. SaveCharacterFloats(char_id,"soga_lip_type", create->getType_float_ByName("soga_lips",0), create->getType_float_ByName("soga_lips",1), create->getType_float_ByName("soga_lips",2));
  338. SaveCharacterFloats(char_id,"soga_chin_type", create->getType_float_ByName("soga_chin",0), create->getType_float_ByName("soga_chin",1), create->getType_float_ByName("soga_chin",2));
  339. SaveCharacterFloats(char_id,"soga_nose_type", create->getType_float_ByName("soga_nose",0), create->getType_float_ByName("soga_nose",1), create->getType_float_ByName("soga_nose",2));
  340. return ret_id;
  341. }
  342. bool LoginDatabase::DeleteCharacter(int32 account_id, int32 character_id, int32 server_id){
  343. Query query;
  344. string delete_char = string("delete from login_characters where char_id=%i and account_id=%i and server_id=%i");
  345. query.RunQuery2(Q_DELETE, delete_char.c_str(),character_id,account_id,server_id);
  346. if(!query.GetAffectedRows())
  347. {
  348. //No error just in case ppl try doing stupid stuff
  349. return false;
  350. }
  351. return true;
  352. }
  353. string LoginDatabase::GetCharacterName(int32 char_id, int32 server_id){
  354. Query query;
  355. MYSQL_ROW row;
  356. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name from login_characters where char_id=%lu and server_id=%lu and deleted = 0", char_id,server_id);
  357. if(result && mysql_num_rows(result) == 1){
  358. row = mysql_fetch_row(result);
  359. return string(row[0]);
  360. }
  361. return string("");
  362. }
  363. bool LoginDatabase::UpdateCharacterTimeStamp(int32 account_id, int32 character_id, int32 timestamp_update, int32 server_id){
  364. Query query;
  365. string update_charts = string("update login_characters set unix_timestamp=%lu where char_id=%lu and account_id=%lu and server_id=%lu");
  366. query.RunQuery2(Q_UPDATE, update_charts.c_str(),timestamp_update,character_id,account_id,server_id);
  367. if(!query.GetAffectedRows())
  368. {
  369. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterTimeStamp query '%s': %s", query.GetQuery(), query.GetError());
  370. return false;
  371. }
  372. return true;
  373. }
  374. bool LoginDatabase::UpdateCharacterLevel(int32 account_id, int32 character_id, int8 in_level, int32 server_id){
  375. Query query;
  376. string update_charts = string("update login_characters set level=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  377. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_level,character_id,account_id,server_id);
  378. if(!query.GetAffectedRows())
  379. {
  380. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterLevel query '%s': %s", query.GetQuery(), query.GetError());
  381. return false;
  382. }
  383. return true;
  384. }
  385. bool LoginDatabase::UpdateCharacterRace(int32 account_id, int32 character_id, int16 in_racetype, int8 in_race, int32 server_id){
  386. Query query;
  387. string update_charts = string("update login_characters set race_type=%i, race=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  388. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_racetype,in_race,character_id,account_id,server_id);
  389. if(!query.GetAffectedRows())
  390. {
  391. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterRace query '%s': %s", query.GetQuery(), query.GetError());
  392. return false;
  393. }
  394. return true;
  395. }
  396. bool LoginDatabase::UpdateCharacterZone(int32 account_id, int32 character_id, int32 zone_id, int32 server_id){
  397. Query query;
  398. string update_chars = string("update login_characters set current_zone_id=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  399. query.RunQuery2(Q_UPDATE, update_chars.c_str(), zone_id, character_id, account_id, server_id);
  400. if(!query.GetAffectedRows())
  401. {
  402. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterZone query '%s': %s", query.GetQuery(), query.GetError());
  403. return false;
  404. }
  405. return true;
  406. }
  407. bool LoginDatabase::UpdateCharacterClass(int32 account_id, int32 character_id, int8 in_class, int32 server_id){
  408. Query query;
  409. string update_charts = string("update login_characters set class=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  410. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_class,character_id,account_id,server_id);
  411. if(!query.GetAffectedRows())
  412. {
  413. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterClass query '%s': %s", query.GetQuery(), query.GetError());
  414. return false;
  415. }
  416. return true;
  417. }
  418. bool LoginDatabase::UpdateCharacterGender(int32 account_id, int32 character_id, int8 in_gender, int32 server_id){
  419. Query query;
  420. string update_charts = string("update login_characters set gender=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  421. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_gender,character_id,account_id,server_id);
  422. if(!query.GetAffectedRows())
  423. {
  424. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterClass query '%s': %s", query.GetQuery(), query.GetError());
  425. return false;
  426. }
  427. return true;
  428. }
  429. LoginAccount* LoginDatabase::LoadAccount(const char* name, const char* password, bool attemptAccountCreation){
  430. LoginAccount* acct = NULL;
  431. Query query;
  432. query.escaped_name = getEscapeString(name);
  433. query.escaped_pass = getEscapeString(password);
  434. MYSQL_ROW row;
  435. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id from account where name='%s' and passwd=sha2('%s',512)", query.escaped_name, query.escaped_pass);
  436. if(result){
  437. if (mysql_num_rows(result) == 1){
  438. row = mysql_fetch_row(result);
  439. int32 id = atol(row[0]);
  440. acct = new LoginAccount(id, name, password);
  441. acct->setAuthenticated(true);
  442. }
  443. else if(mysql_num_rows(result) > 0)
  444. LogWrite(LOGIN__ERROR, 0, "Login", "Error in LoginAccount: more than one account returned for '%s'", name);
  445. else if (attemptAccountCreation && !database.GetAccountIDByName(name))
  446. {
  447. Query newquery;
  448. newquery.RunQuery2(Q_INSERT, "insert into account set name='%s',passwd=sha2('%s',512)", query.escaped_name, query.escaped_pass);
  449. // re-run the query for select only not account creation
  450. return LoadAccount(name, password, false);
  451. }
  452. }
  453. return acct;
  454. }
  455. int32 LoginDatabase::GetAccountIDByName(const char* name) {
  456. int32 id = 0;
  457. Query query;
  458. MYSQL_ROW row;
  459. query.escaped_name = getEscapeString(name);
  460. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id from account where name='%s'", query.escaped_name);
  461. if (result && mysql_num_rows(result) == 1) {
  462. row = mysql_fetch_row(result);
  463. id = atoi(row[0]);
  464. }
  465. return id;
  466. }
  467. int32 LoginDatabase::CheckServerAccount(char* name, char* passwd){
  468. int32 id = 0;
  469. Query query;
  470. MYSQL_ROW row;
  471. query.escaped_name = getEscapeString(name);
  472. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT lower(password), id from login_worldservers where account='%s'", query.escaped_name);
  473. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer CheckServerAccount Account=%s\nSHA=%s", (char*)query.escaped_name, passwd);
  474. if(result && mysql_num_rows(result) == 1){
  475. row = mysql_fetch_row(result);
  476. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer CheckServerAccountResult Account=%s\nPassword=%s", (char*)query.escaped_name, (row && row[0]) ? row[0] : "(NULL)");
  477. if (memcmp(row[0], passwd, strnlen(row[0], 256)) == 0)
  478. {
  479. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer CheckServerAccountResultMatch Account=%s", (char*)query.escaped_name);
  480. id = atoi(row[1]);
  481. }
  482. }
  483. return id;
  484. }
  485. void LoginDatabase::GetServerAccounts(vector<LWorld*>* server_list){
  486. Query query;
  487. MYSQL_ROW row;
  488. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, account, name, admin_id from login_worldservers");
  489. while((row = mysql_fetch_row(result))){
  490. LWorld* world = new LWorld(atol(row[0]), row[1], row[2], atoi(row[3]));
  491. world->SetID(world->GetAccountID());
  492. server_list->push_back(world);
  493. }
  494. }
  495. void LoginDatabase::SaveClientLog(char* type, char* message, char* player_name, int16 version){
  496. Query query;
  497. query.escaped_data1 = getEscapeString(message);
  498. query.escaped_name = getEscapeString(player_name);
  499. query.RunQuery2(Q_INSERT, "insert into log_messages (type, message, name, version) values('%s', '%s', '%s', %i)", type, query.escaped_data1, query.escaped_name, version);
  500. }
  501. bool LoginDatabase::VerifyDelete(int32 account_id, int32 character_id, const char* name){
  502. Query query;
  503. query.escaped_name = getEscapeString(name);
  504. query.RunQuery2(Q_UPDATE, "update login_characters set deleted = 1 where char_id=%i and account_id=%i and name='%s'", character_id, account_id, query.escaped_name);
  505. if(query.GetAffectedRows() == 1)
  506. return true;
  507. else
  508. return false;
  509. }
  510. char* LoginDatabase::GetServerAccountName(int32 id){
  511. Query query;
  512. MYSQL_ROW row;
  513. char* name = 0;
  514. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name from login_worldservers where id=%lu", id);
  515. if(result && mysql_num_rows(result) == 1){
  516. row = mysql_fetch_row(result);
  517. if(strlen(row[0]) > 0){
  518. name = new char[strlen(row[0])+1];
  519. strcpy(name, row[0]);
  520. }
  521. }
  522. return name;
  523. }
  524. int32 LoginDatabase::GetRaceID(char* name){
  525. int32 ret = 1487;
  526. Query query;
  527. MYSQL_ROW row;
  528. query.escaped_name = getEscapeString(name);
  529. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT race_type from login_races where name='%s'", query.escaped_name);
  530. if(result && mysql_num_rows(result) == 1){
  531. row = mysql_fetch_row(result);
  532. ret = atol(row[0]);
  533. }
  534. else if(!result || mysql_num_rows(result) == 0)
  535. UpdateRaceID(query.escaped_name);
  536. return ret;
  537. }
  538. void LoginDatabase::UpdateRaceID(char* name){
  539. Query query;
  540. query.RunQuery2(Q_UPDATE, "insert into login_races (name) values('%s')", name);
  541. }
  542. bool LoginDatabase::CheckVersion(char* in_version){
  543. Query query;
  544. query.escaped_data1 = getEscapeString(in_version);
  545. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id from login_versions where version='%s' or version='*'", query.escaped_data1);
  546. if(result && mysql_num_rows(result) > 0)
  547. return true;
  548. else
  549. return false;
  550. }
  551. void LoginDatabase::GetLatestTableVersions(LatestTableVersions* table_versions){
  552. Query query;
  553. MYSQL_ROW row;
  554. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name, max(version) from login_table_versions group by name order by id");
  555. if(result && mysql_num_rows(result) > 0){
  556. table_versions->SetTableSize(mysql_num_rows(result));
  557. }
  558. else // we need to return if theres no result, otherwise it will crash attempting to loop through rows
  559. return;
  560. while((row = mysql_fetch_row(result))){
  561. if(VerifyDataTable(row[0]))
  562. table_versions->AddTable(row[0], atoi(row[1]), GetDataVersion(row[0]));
  563. else
  564. table_versions->AddTable(row[0], atoi(row[1]), 0);
  565. }
  566. }
  567. bool LoginDatabase::VerifyDataTable(char* name){
  568. Query query;
  569. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT table_name from download_tables where table_name='%s'", name);
  570. if(result && mysql_num_rows(result) > 0)
  571. return true;
  572. return false;
  573. }
  574. string LoginDatabase::GetColumnNames(char* name){
  575. Query query;
  576. MYSQL_ROW row;
  577. string columns = "(";
  578. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "show columns from %s", name);
  579. if(result && mysql_num_rows(result) > 0){
  580. int16 i = 0;
  581. while((row = mysql_fetch_row(result))){
  582. if(strcmp(row[0], "table_data_version") != 0){
  583. if(i>0)
  584. columns.append(",");
  585. columns.append(row[0]);
  586. i++;
  587. }
  588. }
  589. }
  590. columns.append(") ");
  591. return columns;
  592. }
  593. TableDataQuery* LoginDatabase::GetTableDataQuery(int32 server_ip, char* name, int16 version){
  594. Query query;
  595. MYSQL_ROW row;
  596. query.escaped_name = getEscapeString(name);
  597. TableDataQuery* table_query = 0;
  598. MYSQL_RES* result = 0;
  599. string columns;
  600. if(VerifyDataTable(query.escaped_name)){
  601. result = query.RunQuery2(Q_SELECT, "SELECT * from %s where table_data_version > %i", query.escaped_name, version);
  602. columns = GetColumnNames(query.escaped_name);
  603. }
  604. if(result && mysql_num_rows(result) > 0){
  605. table_query = new TableDataQuery(query.escaped_name);
  606. table_query->num_queries = mysql_num_rows(result);
  607. table_query->columns_size = columns.length() + 1;
  608. table_query->columns = new char[table_query->columns_size + 1];
  609. table_query->version = GetDataVersion(query.escaped_name);
  610. strcpy(table_query->columns, (char*)columns.c_str());
  611. string query_data;
  612. MYSQL_FIELD* field;
  613. int* int_list = new int[mysql_num_fields(result)];
  614. int16 ndx = 0;
  615. while((field = mysql_fetch_field(result))){
  616. int_list[ndx] = IS_NUM(field->type);
  617. if(strcmp(field->name,"table_data_version") == 0)
  618. int_list[ndx] = 2;
  619. ndx++;
  620. }
  621. ndx = 0;
  622. while((row = mysql_fetch_row(result))){
  623. query_data = "";
  624. for(int i=0;i<mysql_num_fields(result);i++){
  625. if(int_list[i]<2){
  626. if(i>0)
  627. query_data.append(",");
  628. if(!int_list[i]){
  629. query_data.append("'").append(getEscapeString(row[i])).append("'");
  630. }
  631. else
  632. query_data.append(row[i]);
  633. }
  634. }
  635. TableData* new_query = new TableData;
  636. new_query->size = query_data.length() + 1;
  637. new_query->query = new char[query_data.length() + 1];
  638. strcpy(new_query->query, query_data.c_str());
  639. table_query->queries.push_back(new_query);
  640. ndx++;
  641. }
  642. safe_delete_array(int_list);
  643. }
  644. else{
  645. string query2 = string("The user tried to download the following table: ").append(query.escaped_name);
  646. SaveClientLog("Possible Hacking Attempt", (char*)query2.c_str(), "Hacking Data", server_ip);
  647. }
  648. return table_query;
  649. }
  650. TableQuery* LoginDatabase::GetLatestTableQuery(int32 server_ip, char* name, int16 version){
  651. Query query;
  652. MYSQL_ROW row;
  653. query.escaped_name = getEscapeString(name);
  654. TableQuery* table_query = 0;
  655. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT query, version from login_table_versions where name = '%s' and version>=%i order by version", query.escaped_name, version + 1);
  656. if(result && mysql_num_rows(result) > 0){
  657. int16 i = 0;
  658. table_query = new TableQuery;
  659. while((row = mysql_fetch_row(result))){
  660. char* rowdata = row[0];
  661. if(strstr(rowdata, ";")){
  662. char* token = strtok(rowdata,";");
  663. while(token){
  664. char* new_query = new char[strlen(token) + 1];
  665. strcpy(new_query, token);
  666. table_query->AddQuery(new_query);
  667. token = strtok(NULL, ";");
  668. }
  669. }
  670. else
  671. table_query->AddQuery(rowdata);
  672. table_query->latest_version = atoi(row[1]);
  673. }
  674. strcpy(table_query->tablename, name);
  675. table_query->your_version = version;
  676. }
  677. else{
  678. string query2 = string("The following was the DB Query: ").append(query.GetQuery());
  679. SaveClientLog("Possible Hacking Attempt", (char*)query2.c_str(), "Hacking Query", server_ip);
  680. }
  681. return table_query;
  682. }
  683. sint16 LoginDatabase::GetDataVersion(char* name){
  684. Query query;
  685. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(table_data_version) from %s", name);
  686. sint16 ret_version = 0;
  687. if(result && mysql_num_rows(result) > 0) {
  688. MYSQL_ROW row;
  689. row = mysql_fetch_row(result);
  690. if(row[0])
  691. ret_version = atoi(row[0]);
  692. }
  693. return ret_version;
  694. }
  695. void LoginDatabase::RemoveOldWorldServerStats(){
  696. Query query;
  697. query.RunQuery2(Q_DELETE, "delete from login_worldstats where (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(last_update)) > 86400");
  698. }
  699. void LoginDatabase::UpdateWorldServerStats( LWorld* world, sint32 status )
  700. {
  701. if( !world || world->GetAccountID() == 0 )
  702. return;
  703. Query query;
  704. query.RunQuery2(Q_INSERT, "insert into login_worldstats (world_id, world_status, current_players, current_zones, last_update) values(%lu, %i, %i, %i, NOW()) ON DUPLICATE KEY UPDATE current_players=%i,current_zones=%i,world_status=%i,last_update=NOW()",
  705. world->GetAccountID(), status, world->GetPlayerNum(), world->GetZoneNum(), world->GetPlayerNum(), world->GetZoneNum(), status);
  706. }
  707. bool LoginDatabase::ResetWorldServerStatsConnectedTime(LWorld* world){
  708. if(!world || world->GetAccountID() == 0)
  709. return false;
  710. Query query;
  711. string update_stats = string("update login_worldstats set connected_time=now() where world_id=%i and (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(last_update)) > 300");
  712. query.RunQuery2(Q_UPDATE, update_stats.c_str(),world->GetAccountID());
  713. return true;
  714. }
  715. void LoginDatabase::ResetWorldStats ( )
  716. {
  717. Query query;
  718. string update_stats = string("update login_worldstats set world_status=-4, current_players=0, current_zones=0");
  719. query.RunQuery2(update_stats.c_str(), Q_UPDATE);
  720. }
  721. void LoginDatabase::SaveBugReport(int32 world_id, char* category, char* subcategory, char* causes_crash, char* reproducible, char* summary, char* description, char* version, char* player, int32 account_id, char* spawn_name, int32 spawn_id, int32 zone_id){
  722. Query query;
  723. string bug_report = string("insert into bugs (world_id, category, subcategory, causes_crash, reproducible, summary, description, version, player, account_id, spawn_name, spawn_id, zone_id) values(%lu, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %lu, '%s', %lu, %lu)");
  724. query.RunQuery2(Q_INSERT, bug_report.c_str(), world_id, database.getSafeEscapeString(category).c_str(), database.getSafeEscapeString(subcategory).c_str(),
  725. database.getSafeEscapeString(causes_crash).c_str(), database.getSafeEscapeString(reproducible).c_str(), database.getSafeEscapeString(summary).c_str(),
  726. database.getSafeEscapeString(description).c_str(), database.getSafeEscapeString(version).c_str(), database.getSafeEscapeString(player).c_str(), account_id,
  727. database.getSafeEscapeString(spawn_name).c_str(), spawn_id, zone_id);
  728. FixBugReport();
  729. }
  730. void LoginDatabase::FixBugReport(){
  731. Query query;
  732. string bug_report = string("update bugs set description = REPLACE(description,SUBSTRING(description,INSTR(description,'%'), 3),char(CONV(SUBSTRING(description,INSTR(description,'%')+1, 2), 16, 10))), summary = REPLACE(summary,SUBSTRING(summary,INSTR(summary,'%'), 3),char(CONV(SUBSTRING(summary,INSTR(summary,'%')+1, 2), 16, 10)))");
  733. query.RunQuery2(bug_report.c_str(), Q_UPDATE);
  734. }
  735. void LoginDatabase::UpdateWorldIPAddress(int32 world_id, int32 address){
  736. struct in_addr in;
  737. in.s_addr = address;
  738. Query query;
  739. query.RunQuery2(Q_UPDATE, "update login_worldservers set ip_address='%s' where id=%lu", inet_ntoa(in), world_id);
  740. }
  741. void LoginDatabase::UpdateAccountIPAddress(int32 account_id, int32 address){
  742. struct in_addr in;
  743. in.s_addr = address;
  744. Query query;
  745. query.RunQuery2(Q_UPDATE, "update account set ip_address='%s' where id=%lu", inet_ntoa(in), account_id);
  746. }