GuildDB.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 <math.h>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <iomanip>
  24. #include <ios>
  25. #include <mysql.h>
  26. #include <assert.h>
  27. #include "../../common/Log.h"
  28. #include "../WorldDatabase.h"
  29. #include "Guild.h"
  30. extern GuildList guild_list;
  31. extern RuleManager rule_manager;
  32. void WorldDatabase::LoadGuilds() {
  33. int32 num_guilds = 0;
  34. Query query;
  35. MYSQL_ROW row;
  36. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `name`, `motd`, `level`, `xp`, `xp_needed`, `formed_on` FROM `guilds`");
  37. while (result && (row = mysql_fetch_row(result))) {
  38. LogWrite(GUILD__DEBUG, 1, "Guilds", "%u. %s", atoul(row[0]), row[1]);
  39. Guild* guild = new Guild;
  40. guild->SetID(atoul(row[0]));
  41. guild->SetName(row[1]);
  42. if (row[2])
  43. guild->SetMOTD(row[2], false);
  44. guild->SetLevel(atoi(row[3]), false);
  45. guild->SetEXPCurrent(atoul(row[4]), false);
  46. guild->SetEXPToNextLevel(atoul(row[5]), false);
  47. guild->SetFormedDate(atoul(row[6]));
  48. LogWrite(GUILD__DEBUG, 3, "Guilds", "\tLoaded %i guild members.", LoadGuildMembers(guild));
  49. LogWrite(GUILD__DEBUG, 3, "Guilds", "\tLoading Guild Ranks...");
  50. LoadGuildRanks(guild);
  51. LogWrite(GUILD__DEBUG, 3, "Guilds", "\tLoading Guild Event Filters...");
  52. LoadGuildEventFilters(guild);
  53. LogWrite(GUILD__DEBUG, 3, "Guilds", "\tLoading Guild Events...");
  54. LoadGuildEvents(guild);
  55. LogWrite(GUILD__DEBUG, 3, "Guilds", "\tLoading Guild Recruiting...");
  56. LoadGuildRecruiting(guild);
  57. guild_list.AddGuild(guild);
  58. num_guilds++;
  59. }
  60. LogWrite(GUILD__INFO, 0, "Guilds", "\tLoaded %u Guild(s)", num_guilds);
  61. }
  62. int32 WorldDatabase::LoadGuildMembers(Guild* guild) {
  63. int32 num_members = 0;
  64. Query query;
  65. MYSQL_ROW row;
  66. char *name;
  67. int32 char_id;
  68. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `char_id`, `recruiter_id`, `guild_status`, `points`, `rank_id`, `member_flags`, `join_date`, `note`, `officer_note`, `recruiting_message`, `recruiter_picture_data` FROM `guild_members` WHERE `guild_id`=%u", guild->GetID());
  69. while (result && (row = mysql_fetch_row(result))) {
  70. char_id = atoul(row[0]);
  71. if (!(name = GetCharacterName(char_id))) {
  72. LogWrite(GUILD__ERROR, 0, "Guilds", "WorldDatabase::LoadGuildMembers Cannot find guild member with character id %u.", char_id);
  73. continue;
  74. }
  75. GuildMember* gm = new GuildMember;
  76. gm->character_id = char_id;
  77. gm->recruiter_id = atoul(row[1]);
  78. gm->guild_status = atoul(row[2]);
  79. gm->points = atof(row[3]);
  80. gm->rank = atoi(row[4]);
  81. gm->member_flags = atoi(row[5]);
  82. gm->join_date = atoul(row[6]);
  83. if (row[7])
  84. gm->note = string(row[7]);
  85. if (row[8])
  86. gm->officer_note = string(row[8]);
  87. if (row[9])
  88. gm->recruiter_description = string(row[9]);
  89. int16 recruiter_picture_data_size = 0;
  90. if (row[10] && (recruiter_picture_data_size = strlen(row[10])) > 0) {
  91. gm->recruiter_picture_data_size = recruiter_picture_data_size / 2;
  92. gm->recruiter_picture_data = new unsigned char[gm->recruiter_picture_data_size];
  93. unsigned char* cpy = gm->recruiter_picture_data;
  94. const char* str = row[10];
  95. char high, low;
  96. for (const char* ptr = str; *ptr; ptr += 2) {
  97. high = tolower(*ptr);
  98. low = tolower(*(ptr+1));
  99. if (isdigit(high))
  100. high = high - '0';
  101. else if (high >= 'a' && high <= 'f')
  102. high = (high - 'a') + 10;
  103. else {
  104. LogWrite(GUILD__ERROR, 0, "Guilds", "Guild mate with id %u has corrupt picture data. Data not loading.", gm->character_id);
  105. safe_delete_array(gm->recruiter_picture_data);
  106. gm->recruiter_picture_data_size = 0;
  107. break;
  108. }
  109. if (isdigit(low))
  110. low = low - '0';
  111. else if (low >= 'a' && low <= 'f')
  112. low = (low - 'a') + 10;
  113. else {
  114. LogWrite(GUILD__ERROR, 0, "Guilds", "Guild mate with id %u has corrupt picture data. Data not loading.", gm->character_id);
  115. safe_delete_array(gm->recruiter_picture_data);
  116. gm->recruiter_picture_data_size = 0;
  117. break;
  118. }
  119. *cpy++ = low | (high << 4);
  120. }
  121. /*for (int16 i = 0; i < gm->recruiter_picture_data_size; i++)
  122. if (i<10)
  123. printf("int:%u hex:%x\n", gm->recruiter_picture_data[i], gm->recruiter_picture_data[i]);*/
  124. }
  125. else {
  126. gm->recruiter_picture_data_size = 0;
  127. gm->recruiter_picture_data = 0;
  128. }
  129. strncpy(gm->name, name, sizeof(gm->name));
  130. gm->account_id = GetCharacterAccountID(char_id);
  131. gm->adventure_class = GetCharacterClass(char_id);
  132. gm->adventure_level = GetCharacterLevel(char_id);
  133. gm->tradeskill_class = 0;
  134. gm->tradeskill_level = 0;
  135. gm->last_login_date = GetCharacterTimeStamp(char_id);
  136. gm->zone = GetZoneDescription(GetCharacterCurrentZoneID(char_id));
  137. gm->recruiting_show_adventure_class = 1;
  138. LoadGuildPointsHistory(guild, gm);
  139. guild->AddGuildMember(gm);
  140. safe_delete_array(name);
  141. num_members++;
  142. }
  143. return num_members;
  144. }
  145. void WorldDatabase::LoadGuildEvents(Guild* guild) {
  146. if (guild) {
  147. Query query;
  148. MYSQL_ROW row;
  149. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `event_id`, `event_date`, `event_type`, `description`, `locked` FROM `guild_events` WHERE `guild_id`=%u AND `display`=1 AND `archived`=0 ORDER BY `event_date` DESC LIMIT 0, %u", guild->GetID(), GUILD_MAX_EVENTS);
  150. while (result && (row = mysql_fetch_row(result)))
  151. guild->AddGuildEvent(atoi64(row[0]), atoul(row[2]), row[3], atoul(row[1]), atoi(row[4]));
  152. }
  153. }
  154. void WorldDatabase::LoadGuildRanks(Guild* guild) {
  155. if (guild) {
  156. LogWrite(GUILD__DEBUG, 3, "Guilds", "Loading Ranks for guild id: %u", guild->GetID());
  157. Query query;
  158. MYSQL_ROW row;
  159. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `rank_id`, `rank_name`, `permission1`, `permission2` FROM `guild_ranks` WHERE `guild_id`=%u", guild->GetID());
  160. while (result && (row = mysql_fetch_row(result))) {
  161. int8 rank_id = atoi(row[0]);
  162. int32 permission1 = atoul(row[2]);
  163. int32 permission2 = atoul(row[3]);
  164. guild->SetRankName(rank_id, row[1], false);
  165. LogWrite(GUILD__DEBUG, 5, "Guilds", "\tLoading rank_id: %i", rank_id);
  166. LogWrite(GUILD__DEBUG, 5, "Guilds", "\tPermission1: %ul, Permission2: %ul", permission1, permission2);
  167. for (int32 i = 0; i <= 44; i++) {
  168. int32 bitwise_val;
  169. if (i < 32) {
  170. bitwise_val = (int32)pow(2.0, (double)(i));
  171. guild->SetPermission(rank_id, i, permission1 & bitwise_val ? 1 : 0, false);
  172. LogWrite(GUILD__DEBUG, 5, "Guilds", "\tSetting Permission %u to %u", i, permission1 & bitwise_val ? 1 : 0);
  173. }
  174. else {
  175. bitwise_val = (int32)pow(2.0, (double)(i - 32));
  176. guild->SetPermission(rank_id, i, permission2 & bitwise_val ? 1 : 0, false);
  177. LogWrite(GUILD__DEBUG, 5, "Guilds", "\tSetting Permission %u to %u", i, permission2 & bitwise_val ? 1 : 0);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. void WorldDatabase::LoadGuildEventFilters(Guild* guild) {
  184. if (guild) {
  185. Query query;
  186. MYSQL_ROW row;
  187. bool event_filter_added = false;
  188. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `event_id`, `retain`, `broadcast` FROM `guild_event_filters` WHERE `guild_id`=%u", guild->GetID());
  189. while (result && (row = mysql_fetch_row(result))) {
  190. guild->SetEventFilter(atoi(row[0]), GUILD_EVENT_FILTER_CATEGORY_RETAIN_HISTORY, atoi(row[1]), false);
  191. guild->SetEventFilter(atoi(row[0]), GUILD_EVENT_FILTER_CATEGORY_BROADCAST, atoi(row[2]), false);
  192. if (!event_filter_added)
  193. event_filter_added = true;
  194. }
  195. if (!event_filter_added)
  196. LoadGuildDefaultEventFilters(guild);
  197. }
  198. }
  199. void WorldDatabase::LoadGuildPointsHistory(Guild* guild, GuildMember* guild_member) {
  200. Query query;
  201. MYSQL_ROW row;
  202. MYSQL_RES* result;
  203. assert(guild);
  204. assert(guild_member);
  205. result = query.RunQuery2(Q_SELECT, "SELECT `points_date`, `modified_by`, `comment`, `points` FROM `guild_points_history` WHERE `guild_id`=%u AND `char_id`=%u ORDER BY `points_date` DESC", guild->GetID(), guild_member->character_id);
  206. while (result && (row = mysql_fetch_row(result)))
  207. guild->AddPointHistory(guild_member, atoul(row[0]), row[1], atof(row[3]), row[2], false);
  208. }
  209. void WorldDatabase::LoadGuildRecruiting(Guild* guild) {
  210. if (guild) {
  211. Query query;
  212. MYSQL_ROW row;
  213. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `short_desc`, `full_desc`, `min_level`, `play_style`, `looking_for`, `descriptive_tag1`, `descriptive_tag2`, `descriptive_tag3`, `descriptive_tag4` FROM `guild_recruiting` WHERE `guild_id`=%u", guild->GetID());
  214. while (result && (row = mysql_fetch_row(result))) {
  215. if (row[0])
  216. guild->SetRecruitingShortDesc(row[0], false);
  217. if (row[1])
  218. guild->SetRecruitingFullDesc(row[1], false);
  219. guild->SetRecruitingMinLevel(atoi(row[2]), false);
  220. guild->SetRecruitingPlayStyle(atoi(row[3]), false);
  221. for (int32 i = 0; i <= 5; i++) {
  222. int32 bitwise_val = (int32)pow(2.0, (double)i);
  223. guild->SetRecruitingFlag(i, atoi(row[4]) & bitwise_val ? 1 : 0, false);
  224. }
  225. guild->SetRecruitingDescTag(0, atoi(row[5]), false);
  226. guild->SetRecruitingDescTag(1, atoi(row[6]), false);
  227. guild->SetRecruitingDescTag(2, atoi(row[7]), false);
  228. guild->SetRecruitingDescTag(3, atoi(row[8]), false);
  229. }
  230. }
  231. }
  232. void WorldDatabase::SaveGuild(Guild* guild, bool new_guild) {
  233. Query query;
  234. assert(guild);
  235. if (new_guild) {
  236. LogWrite(GUILD__DEBUG, 3, "Guilds", "Saving NEW Guild '%s' (%u) data...", guild->GetName(), guild->GetID());
  237. query.RunQuery2(Q_INSERT, "INSERT INTO `guilds` (`name`, `motd`, `level`, `xp`, `xp_needed`, `formed_on`) "
  238. "VALUES ('%s', '%s', %i, %llu, %llu, %u)",
  239. getSafeEscapeString(guild->GetName()).c_str(), getSafeEscapeString(guild->GetMOTD()).c_str(), guild->GetLevel(), guild->GetEXPCurrent(), guild->GetEXPToNextLevel(), guild->GetFormedDate());
  240. guild->SetID(query.GetLastInsertedID());
  241. }
  242. else {
  243. LogWrite(GUILD__DEBUG, 3, "Guilds", "Saving Guild '%s' (%u) data...", guild->GetName(), guild->GetID());
  244. query.RunQuery2(Q_UPDATE, "UPDATE `guilds` "
  245. "SET `name`='%s', `motd`='%s', `level`=%i, `xp`=%llu, `xp_needed`=%llu, `formed_on`=%u WHERE `id`=%u",
  246. getSafeEscapeString(guild->GetName()).c_str(), getSafeEscapeString(guild->GetMOTD()).c_str(), guild->GetLevel(), guild->GetEXPCurrent(), guild->GetEXPToNextLevel(), guild->GetFormedDate(), guild->GetID());
  247. }
  248. guild->SetSaveNeeded(false);
  249. }
  250. void WorldDatabase::SaveGuildMembers(Guild* guild) {
  251. map<int32, GuildMember *>* members;
  252. map<int32, GuildMember *>::iterator itr;
  253. Mutex *mMembers;
  254. GuildMember *gm;
  255. Query query, query2;
  256. assert(guild);
  257. members = guild->GetGuildMembers();
  258. mMembers = guild->GetGuildMembersMutex();
  259. mMembers->readlock(__FUNCTION__, __LINE__);
  260. for (itr = members->begin(); itr != members->end(); itr++) {
  261. gm = itr->second;
  262. LogWrite(GUILD__DEBUG, 5, "Guilds", "Saving Guild Member '%s' (%u) data...", gm->name, gm->character_id);
  263. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_members` (`guild_id`, `char_id`, `recruiter_id`, `guild_status`, `points`, `rank_id`, `member_flags`, `join_date`, `note`, `officer_note`, `recruiting_message`, `recruiter_picture_data`) VALUES (%u, %u, %u, %u, %f, %u, %u, %u, '%s', '%s', '%s', NULL) ON DUPLICATE KEY UPDATE `guild_id`=%u, `recruiter_id`=%u, `guild_status`=%u, `points`=%f, `rank_id`=%u, `member_flags`=%u, `join_date`=%u, `note`='%s', `officer_note`='%s', `recruiting_message`='%s', `recruiter_picture_data`=NULL", guild->GetID(), gm->character_id, gm->recruiter_id, gm->guild_status, gm->points, gm->rank, gm->member_flags, gm->join_date, getSafeEscapeString(gm->note.c_str()).c_str(), getSafeEscapeString(gm->officer_note.c_str()).c_str(), getSafeEscapeString(gm->recruiter_description.c_str()).c_str(), guild->GetID(), gm->recruiter_id, gm->guild_status, gm->points, gm->rank, gm->member_flags, gm->join_date, getSafeEscapeString(gm->note.c_str()).c_str(), getSafeEscapeString(gm->officer_note.c_str()).c_str(), getSafeEscapeString(gm->recruiter_description.c_str()).c_str());
  264. if (gm && gm->recruiter_picture_data_size > 0 && gm->recruiter_picture_data) {
  265. stringstream ss_hex;
  266. stringstream ss_query;
  267. ss_hex.flags(ios::hex);
  268. for (int16 i = 0; i < gm->recruiter_picture_data_size; i++)
  269. ss_hex << setfill('0') << setw(2) << (int)gm->recruiter_picture_data[i];
  270. ss_query << "UPDATE `guild_members` SET `recruiter_picture_data`='" << ss_hex.str() << "' WHERE `char_id`=" << gm->character_id;
  271. query2.RunQuery2(ss_query.str(), Q_UPDATE);
  272. }
  273. }
  274. guild->SetMemberSaveNeeded(false);
  275. mMembers->releasereadlock(__FUNCTION__, __LINE__);
  276. }
  277. void WorldDatabase::SaveGuildEvents(Guild* guild) {
  278. if (guild) {
  279. deque<GuildEvent*>* guild_events = guild->GetGuildEvents();
  280. deque<GuildEvent*>::iterator itr;
  281. for (itr = guild_events->begin(); itr != guild_events->end(); itr++) {
  282. GuildEvent* ge = *itr;
  283. if (!ge->save_needed)
  284. continue;
  285. LogWrite(GUILD__DEBUG, 5, "Guilds", "Saving Guild Events for guild '%s' (%u)...", guild->GetName(), guild->GetID());
  286. Query query;
  287. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_events` (`guild_id`, `event_id`, `event_date`, `event_type`, `description`, `display`, `locked`, `archived`) "
  288. "VALUES (%u, %llu, %u, %u, '%s', 1, %u, 0) "
  289. "ON DUPLICATE KEY UPDATE `locked`=%i",
  290. guild->GetID(), ge->event_id, ge->date, ge->type, getSafeEscapeString(ge->description.c_str()).c_str(), ge->locked, ge->locked);
  291. ge->save_needed = false;
  292. }
  293. guild->SetEventsSaveNeeded(false);
  294. }
  295. }
  296. void WorldDatabase::SaveGuildRanks(Guild* guild) {
  297. if (guild) {
  298. MutexMap<int8, MutexMap<int8, int8>*>* permissions = guild->GetPermissions();
  299. MutexMap<int8, string>* ranks = guild->GetGuildRanks();
  300. MutexMap<int8, string>::iterator ranks_itr = ranks->begin();
  301. while (ranks_itr.Next()) {
  302. int32 permission1 = 0;
  303. int32 permission2 = 0;
  304. for (int32 i = 0; i <= 44; i++) {
  305. if (permissions->count(ranks_itr.first) > 0 && permissions->Get(ranks_itr.first)->count(i) > 0 && permissions->Get(ranks_itr.first)->Get(i)) {
  306. if (i < 32)
  307. permission1 += (int32)pow(2.0, (double)i);
  308. else
  309. permission2 += (int32)pow(2.0, (double)(i - 32));
  310. }
  311. }
  312. LogWrite(GUILD__DEBUG, 5, "Guilds", "Saving Guild Ranks for guild '%s' (%u)...", guild->GetName(), guild->GetID());
  313. Query query;
  314. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_ranks` (`guild_id`, `rank_id`, `rank_name`, `permission1`, `permission2`) "
  315. "VALUES (%u, %u, '%s', %u, %u) "
  316. "ON DUPLICATE KEY UPDATE `rank_name`='%s', `permission1`=%u, permission2=%u",
  317. guild->GetID(), ranks_itr.first, getSafeEscapeString(ranks_itr.second.c_str()).c_str(), permission1, permission2, getSafeEscapeString(ranks_itr.second.c_str()).c_str(), permission1, permission2);
  318. }
  319. guild->SetRanksSaveNeeded(false);
  320. }
  321. }
  322. void WorldDatabase::SaveGuildEventFilters(Guild* guild) {
  323. int32 i;
  324. assert(guild);
  325. for (i = 0; i < 93; i++) {
  326. LogWrite(GUILD__DEBUG, 5, "Guilds", "Saving Guild EventFilters for guild '%s' (%u)...", guild->GetName(), guild->GetID());
  327. Query query;
  328. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_event_filters` (`guild_id`, `event_id`, `retain`, `broadcast`) "
  329. "VALUES (%u, %u, %u, %u) "
  330. "ON DUPLICATE KEY UPDATE `retain`=%u, `broadcast`=%u",
  331. guild->GetID(), i, guild->GetEventFilter(i, GUILD_EVENT_FILTER_CATEGORY_RETAIN_HISTORY), guild->GetEventFilter(i, GUILD_EVENT_FILTER_CATEGORY_BROADCAST), guild->GetEventFilter(i, GUILD_EVENT_FILTER_CATEGORY_RETAIN_HISTORY), guild->GetEventFilter(i, GUILD_EVENT_FILTER_CATEGORY_BROADCAST));
  332. }
  333. guild->SetEventFiltersSaveNeeded(false);
  334. }
  335. void WorldDatabase::SaveGuildPointsHistory(Guild* guild) {
  336. map<int32, GuildMember *> *members;
  337. map<int32, GuildMember *>::iterator itr;
  338. Mutex *mMembers;
  339. deque<PointHistory *> *ph_list;
  340. deque<PointHistory*>::iterator ph_itr;
  341. PointHistory* ph;
  342. assert (guild);
  343. members = guild->GetGuildMembers();
  344. mMembers = guild->GetGuildMembersMutex();
  345. mMembers->readlock(__FUNCTION__, __LINE__);
  346. for (itr = members->begin(); itr != members->end(); itr++) {
  347. ph_list = &itr->second->point_history;
  348. for (ph_itr = ph_list->begin(); ph_itr != ph_list->end(); ph_itr++) {
  349. ph = *ph_itr;
  350. if (!ph->saved_needed)
  351. continue;
  352. LogWrite(GUILD__DEBUG, 5, "Guilds", "Saving Guild Point History for guild '%s' (%u)...", guild->GetName(), guild->GetID());
  353. Query query;
  354. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_points_history` (`guild_id`, `char_id`, `points_date`, `modified_by`, `comment`, `points`) "
  355. "VALUES (%u, %u, %u, '%s', '%s', %f)",
  356. guild->GetID(), itr->first, ph->date, getSafeEscapeString(ph->modified_by.c_str()).c_str(), getSafeEscapeString(ph->comment.c_str()).c_str(), ph->points);
  357. ph->saved_needed = false;
  358. }
  359. }
  360. guild->SetPointsHistorySaveNeeded(false);
  361. mMembers->releasereadlock(__FUNCTION__, __LINE__);
  362. }
  363. void WorldDatabase::SaveGuildRecruiting(Guild* guild) {
  364. if (guild) {
  365. LogWrite(GUILD__DEBUG, 3, "Guilds", "Saving Recruiting info for guild '%s' (%u)...", guild->GetName(), guild->GetID());
  366. Query query;
  367. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_recruiting` (`guild_id`, `short_desc`, `full_desc`, `min_level`, `play_style`, `looking_for`, `descriptive_tag1`, `descriptive_tag2`, `descriptive_tag3`, `descriptive_tag4`) VALUES (%u, '%s', '%s', %u, %u, %u, %u, %u, %u, %u) ON DUPLICATE KEY UPDATE `short_desc`='%s', `full_desc`='%s', `min_level`=%u, `play_style`=%u, `looking_for`=%u, `descriptive_tag1`=%u, `descriptive_tag2`=%u, `descriptive_tag3`=%u, `descriptive_tag4`=%u", guild->GetID(), getSafeEscapeString(guild->GetRecruitingShortDesc().c_str()).c_str(), getSafeEscapeString(guild->GetRecruitingFullDesc().c_str()).c_str(), guild->GetRecruitingMinLevel(), guild->GetRecruitingPlayStyle(), guild->GetRecruitingLookingForPacketValue(), guild->GetRecruitingDescTag(0), guild->GetRecruitingDescTag(1), guild->GetRecruitingDescTag(2), guild->GetRecruitingDescTag(3), getSafeEscapeString(guild->GetRecruitingShortDesc().c_str()).c_str(), getSafeEscapeString(guild->GetRecruitingFullDesc().c_str()).c_str(), guild->GetRecruitingMinLevel(), guild->GetRecruitingPlayStyle(), guild->GetRecruitingLookingForPacketValue(), guild->GetRecruitingDescTag(0), guild->GetRecruitingDescTag(1), guild->GetRecruitingDescTag(2), guild->GetRecruitingDescTag(3));
  368. guild->SetRecruitingSaveNeeded(false);
  369. }
  370. }
  371. void WorldDatabase::DeleteGuild(Guild* guild) {
  372. if (guild) {
  373. LogWrite(GUILD__DEBUG, 3, "Guilds", "Deleting Guild '%s' (%u)...", guild->GetName(), guild->GetID());
  374. Query query;
  375. query.RunQuery2(Q_DELETE, "DELETE FROM `guilds` WHERE `id`=%u", guild->GetID());
  376. }
  377. }
  378. void WorldDatabase::DeleteGuildMember(Guild* guild, int32 character_id) {
  379. if (guild) {
  380. LogWrite(GUILD__DEBUG, 3, "Guilds", "Deleting Character (%u) from guild '%s' (%u)...", character_id, guild->GetName(), guild->GetID());
  381. Query query;
  382. query.RunQuery2(Q_DELETE, "DELETE FROM `guild_members` WHERE `guild_id`=%u AND `char_id`=%u", guild->GetID(), character_id);
  383. }
  384. }
  385. void WorldDatabase::DeleteGuildEvent(Guild* guild, int64 event_id) {
  386. if (guild) {
  387. LogWrite(GUILD__DEBUG, 3, "Guilds", "Deleting Event (%u) from guild '%s' (%u)...", event_id, guild->GetName(), guild->GetID());
  388. Query query;
  389. query.RunQuery2(Q_DELETE, "DELETE FROM `guild_events` WHERE `guild_id`=%u AND `event_id`=%u", guild->GetID(), event_id);
  390. }
  391. }
  392. void WorldDatabase::DeleteGuildPointHistory(Guild* guild, int32 character_id, PointHistory* point_history) {
  393. if (guild && point_history) {
  394. LogWrite(GUILD__DEBUG, 3, "Guilds", "Deleting PointHistory for Character (%u) from guild '%s' (%u)...", character_id, guild->GetName(), guild->GetID());
  395. Query query;
  396. query.RunQuery2(Q_DELETE, "DELETE FROM `guild_points_history` WHERE `guild_id`=%u AND `char_id`=%u AND `points_date`=%u", guild->GetID(), character_id, point_history->date);
  397. }
  398. }
  399. void WorldDatabase::ArchiveGuildEvent(Guild* guild, GuildEvent* guild_event) {
  400. if (guild && guild_event) {
  401. LogWrite(GUILD__DEBUG, 3, "Guilds", "Archiving Event (%u) for guild '%s' (%u)...", guild_event->event_id, guild->GetName(), guild->GetID());
  402. Query query;
  403. query.RunQuery2(Q_UPDATE, "UPDATE `guild_events` SET `archived`=1 WHERE `guild_id`=%u AND `event_id`=%u", guild->GetID(), guild_event->event_id);
  404. }
  405. }
  406. void WorldDatabase::SaveHiddenGuildEvent(Guild* guild, GuildEvent* guild_event) {
  407. if (guild && guild_event) {
  408. LogWrite(GUILD__DEBUG, 3, "Guilds", "Saving Hidden Event (%u) for guild '%s' (%u)...", guild_event->event_id, guild->GetName(), guild->GetID());
  409. Query query;
  410. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_events` (`guild_id`, `event_id`, `event_date`, `event_type`, `description`, `display`, `locked`, `archived`) VALUES (%u, %u, %u, %u, '%s', 0, %u, 0)", guild->GetID(), guild_event->event_id, guild_event->type, guild_event->date, getSafeEscapeString(guild_event->description.c_str()).c_str(), guild_event->locked);
  411. }
  412. }
  413. int32 WorldDatabase::GetGuildIDByCharacterID(int32 char_id) {
  414. if(char_id > 0)
  415. {
  416. LogWrite(GUILD__DEBUG, 3, "Guilds", "Look up guild ID for player ID: '%u'...", char_id);
  417. Query query;
  418. MYSQL_ROW row;
  419. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM guilds, guild_members WHERE guilds.id = guild_members.guild_id AND char_id = %u ", char_id);
  420. while (result && (row = mysql_fetch_row(result))) {
  421. if( row[0] )
  422. return atoul(row[0]);
  423. }
  424. }
  425. return 0;
  426. }
  427. void WorldDatabase::LoadGuildDefaultRanks(Guild* guild) {
  428. if (guild) {
  429. LogWrite(GUILD__DEBUG, 3, "Guilds", "Load/Set Default Ranks for guild '%s' (%u)...", guild->GetName(), guild->GetID());
  430. Query query;
  431. MYSQL_ROW row;
  432. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT DISTINCT `rank_id`, `rank_name`, `permission1`, `permission2` FROM `guild_ranks_defaults`");
  433. while (result && (row = mysql_fetch_row(result))) {
  434. int8 rank_id = atoi(row[0]);
  435. int32 permission1 = atoul(row[2]);
  436. int32 permission2 = atoul(row[3]);
  437. LogWrite(GUILD__DEBUG, 3, "Guilds", "\tSetting RankID %i, permission1: %u, permission2: %u", rank_id, permission1, permission2);
  438. guild->SetRankName(rank_id, row[1], false);
  439. for (int32 i = 0; i <= 44; i++) {
  440. int32 bitwise_val;
  441. if (i < 32) {
  442. bitwise_val = (int32)pow(2.0, (double)i);
  443. guild->SetPermission(rank_id, i, permission1 & bitwise_val ? 1 : 0, false);
  444. }
  445. else {
  446. bitwise_val = (int32)pow(2.0, (double)(i - 32));
  447. guild->SetPermission(rank_id, i, permission2 & bitwise_val ? 1 : 0, false);
  448. }
  449. }
  450. }
  451. }
  452. }
  453. void WorldDatabase::LoadGuildDefaultEventFilters(Guild* guild) {
  454. if (guild) {
  455. LogWrite(GUILD__DEBUG, 3, "Guilds", "Load/Set Default Event Filters for guild '%s' (%u)...", guild->GetName(), guild->GetID());
  456. Query query;
  457. MYSQL_ROW row;
  458. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT DISTINCT `event_id`, `retain`, `broadcast` FROM `guild_event_defaults`");
  459. while (result && (row = mysql_fetch_row(result))) {
  460. LogWrite(GUILD__DEBUG, 3, "Guilds", "\tSetting Event Filter %i, retain: %i, broadcast: %i", atoi(row[0]), atoi(row[1]), atoi(row[2]));
  461. guild->SetEventFilter(atoi(row[0]), GUILD_EVENT_FILTER_CATEGORY_RETAIN_HISTORY, atoi(row[1]), false);
  462. guild->SetEventFilter(atoi(row[0]), GUILD_EVENT_FILTER_CATEGORY_BROADCAST, atoi(row[2]), false);
  463. }
  464. }
  465. }
  466. bool WorldDatabase::AddNewPlayerToServerGuild(int32 account_id, int32 char_id)
  467. {
  468. // Check if this servers rule allow auto-joining Server guild
  469. int8 autojoin = rule_manager.GetGlobalRule(R_World, GuildAutoJoin)->GetInt8();
  470. if( autojoin )
  471. {
  472. // if so, what is the guild ID of the default server guild?
  473. int32 guild_id = rule_manager.GetGlobalRule(R_World, GuildAutoJoinID)->GetInt32();
  474. Guild* guild = 0;
  475. guild = guild_list.GetGuild(guild_id);
  476. if (!guild)
  477. {
  478. // guild was not valid, abort!
  479. LogWrite(GUILD__ERROR, 1, "Guilds", "Guild ID %u not found! Cannot autojoin members!", guild_id);
  480. return false;
  481. }
  482. else
  483. {
  484. // guild was found, so what default Rank to make the players? if not set, use 7 (recruit)
  485. int8 rank_id = rule_manager.GetGlobalRule(R_World, GuildAutoJoinDefaultRankID)->GetInt8();
  486. if(!rank_id)
  487. rank_id = 7;
  488. // assuming all is good, insert the new guild member here...
  489. GuildMember *gm = new GuildMember();
  490. gm->account_id = account_id;
  491. gm->character_id = char_id;
  492. char* name = GetCharacterName(gm->character_id);
  493. strncpy(gm->name, name, sizeof(gm->name));
  494. gm->guild_status = 0;
  495. gm->points = 0.0;
  496. //gm->adventure_class = player->GetAdventureClass();
  497. //gm->adventure_level = player->GetLevel();
  498. //gm->tradeskill_class = player->GetTradeskillClass();
  499. //gm->tradeskill_level = player->GetTSLevel();
  500. gm->rank = rank_id;
  501. gm->zone = string("");
  502. gm->join_date = Timer::GetUnixTimeStamp();
  503. gm->last_login_date = gm->join_date;
  504. gm->recruiter_id = 0;
  505. gm->member_flags = GUILD_MEMBER_FLAGS_NOTIFY_LOGINS;
  506. gm->recruiting_show_adventure_class = 1;
  507. gm->recruiter_picture_data_size = 0;
  508. gm->recruiter_picture_data = 0;
  509. guild->AddGuildMember(gm);
  510. Query query;
  511. query.RunQuery2(Q_INSERT, "INSERT INTO `guild_members` (`guild_id`, `char_id`, `join_date`, `rank_id`) VALUES (%u, %u, %u, %i)",
  512. guild_id, char_id, gm->join_date, rank_id);
  513. LogWrite(GUILD__DEBUG, 3, "Guilds", "Auto-join player (%u) to server guild '%s' (%u) at rank %i...", char_id, guild->GetName(), guild_id, rank_id);
  514. // success!
  515. return true;
  516. }
  517. }
  518. // do not auto-join server guild
  519. return false;
  520. }