9
3

GuildDB.cpp 25 KB

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