int_guild.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/mmo.h"
  5. #include "../common/malloc.h"
  6. #include "../common/socket.h"
  7. #include "../common/db.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/strlib.h"
  10. #include "../common/timer.h"
  11. #include "char.h"
  12. #include "inter.h"
  13. #include "int_guild.h"
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #define GS_MEMBER_UNMODIFIED 0x00
  18. #define GS_MEMBER_MODIFIED 0x01
  19. #define GS_MEMBER_NEW 0x02
  20. #define GS_POSITION_UNMODIFIED 0x00
  21. #define GS_POSITION_MODIFIED 0x01
  22. // LSB = 0 => Alliance, LSB = 1 => Opposition
  23. #define GUILD_ALLIANCE_TYPE_MASK 0x01
  24. #define GUILD_ALLIANCE_REMOVE 0x08
  25. static const char dataToHex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  26. #ifndef TXT_SQL_CONVERT
  27. //Guild cache
  28. static DBMap* guild_db_; // int guild_id -> struct guild*
  29. struct guild_castle castles[MAX_GUILDCASTLE];
  30. static unsigned int guild_exp[100];
  31. int mapif_parse_GuildLeave(int fd,int guild_id,int account_id,int char_id,int flag,const char *mes);
  32. int mapif_guild_broken(int guild_id,int flag);
  33. static bool guild_check_empty(struct guild *g);
  34. int guild_calcinfo(struct guild *g);
  35. int mapif_guild_basicinfochanged(int guild_id,int type,const void *data,int len);
  36. int mapif_guild_info(int fd,struct guild *g);
  37. int guild_break_sub(int key,void *data,va_list ap);
  38. int inter_guild_tosql(struct guild *g,int flag);
  39. static int guild_save_timer(int tid, unsigned int tick, int id, intptr_t data)
  40. {
  41. static int last_id = 0; //To know in which guild we were.
  42. int state = 0; //0: Have not reached last guild. 1: Reached last guild, ready for save. 2: Some guild saved, don't do further saving.
  43. DBIterator* iter;
  44. DBKey key;
  45. struct guild* g;
  46. if( last_id == 0 ) //Save the first guild in the list.
  47. state = 1;
  48. iter = guild_db_->iterator(guild_db_);
  49. for( g = (struct guild*)iter->first(iter,&key); iter->exists(iter); g = (struct guild*)iter->next(iter,&key) )
  50. {
  51. if( state == 0 && g->guild_id == last_id )
  52. state++; //Save next guild in the list.
  53. else
  54. if( state == 1 && g->save_flag&GS_MASK )
  55. {
  56. inter_guild_tosql(g, g->save_flag&GS_MASK);
  57. g->save_flag &= ~GS_MASK;
  58. //Some guild saved.
  59. last_id = g->guild_id;
  60. state++;
  61. }
  62. if( g->save_flag == GS_REMOVE )
  63. {// Nothing to save, guild is ready for removal.
  64. if (save_log)
  65. ShowInfo("Guild Unloaded (%d - %s)\n", g->guild_id, g->name);
  66. db_remove(guild_db_, key);
  67. }
  68. }
  69. iter->destroy(iter);
  70. if( state != 2 ) //Reached the end of the guild db without saving.
  71. last_id = 0; //Reset guild saved, return to beginning.
  72. state = guild_db_->size(guild_db_);
  73. if( state < 1 ) state = 1; //Calculate the time slot for the next save.
  74. add_timer(tick + autosave_interval/state, guild_save_timer, 0, 0);
  75. return 0;
  76. }
  77. int inter_guild_removemember_tosql(int account_id, int char_id)
  78. {
  79. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `account_id` = '%d' and `char_id` = '%d'", guild_member_db, account_id, char_id) )
  80. Sql_ShowDebug(sql_handle);
  81. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `guild_id` = '0' WHERE `char_id` = '%d'", char_db, char_id) )
  82. Sql_ShowDebug(sql_handle);
  83. return 0;
  84. }
  85. #endif //TXT_SQL_CONVERT
  86. // Save guild into sql
  87. int inter_guild_tosql(struct guild *g,int flag)
  88. {
  89. // Table guild (GS_BASIC_MASK)
  90. // GS_EMBLEM `emblem_len`,`emblem_id`,`emblem_data`
  91. // GS_CONNECT `connect_member`,`average_lv`
  92. // GS_MES `mes1`,`mes2`
  93. // GS_LEVEL `guild_lv`,`max_member`,`exp`,`next_exp`,`skill_point`
  94. // GS_BASIC `name`,`master`,`char_id`
  95. // GS_MEMBER `guild_member` (`guild_id`,`account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name`)
  96. // GS_POSITION `guild_position` (`guild_id`,`position`,`name`,`mode`,`exp_mode`)
  97. // GS_ALLIANCE `guild_alliance` (`guild_id`,`opposition`,`alliance_id`,`name`)
  98. // GS_EXPULSION `guild_expulsion` (`guild_id`,`account_id`,`name`,`mes`)
  99. // GS_SKILL `guild_skill` (`guild_id`,`id`,`lv`)
  100. // temporary storage for str convertion. They must be twice the size of the
  101. // original string to ensure no overflows will occur. [Skotlex]
  102. char t_info[256];
  103. char esc_name[NAME_LENGTH*2+1];
  104. char esc_master[NAME_LENGTH*2+1];
  105. char new_guild = 0;
  106. int i=0;
  107. if (g->guild_id<=0 && g->guild_id != -1) return 0;
  108. #ifdef NOISY
  109. ShowInfo("Save guild request ("CL_BOLD"%d"CL_RESET" - flag 0x%x).",g->guild_id, flag);
  110. #endif
  111. Sql_EscapeStringLen(sql_handle, esc_name, g->name, strnlen(g->name, NAME_LENGTH));
  112. Sql_EscapeStringLen(sql_handle, esc_master, g->master, strnlen(g->master, NAME_LENGTH));
  113. *t_info = '\0';
  114. #ifndef TXT_SQL_CONVERT
  115. // Insert a new guild the guild
  116. if (flag&GS_BASIC && g->guild_id == -1)
  117. {
  118. strcat(t_info, " guild_create");
  119. // Create a new guild
  120. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` "
  121. "(`name`,`master`,`guild_lv`,`max_member`,`average_lv`,`char_id`) "
  122. "VALUES ('%s', '%s', '%d', '%d', '%d', '%d')",
  123. guild_db, esc_name, esc_master, g->guild_lv, g->max_member, g->average_lv, g->member[0].char_id) )
  124. {
  125. Sql_ShowDebug(sql_handle);
  126. if (g->guild_id == -1)
  127. return 0; //Failed to create guild!
  128. }
  129. else
  130. {
  131. g->guild_id = (int)Sql_LastInsertId(sql_handle);
  132. new_guild = 1;
  133. }
  134. }
  135. #else
  136. // Insert a new guild the guild
  137. if (flag&GS_BASIC)
  138. {
  139. strcat(t_info, " guild_create");
  140. // Since the PK is guild id + master id, a replace will not be enough if we are overwriting data, we need to wipe the previous guild.
  141. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` where `guild_id` = '%d'", guild_db, g->guild_id) )
  142. Sql_ShowDebug(sql_handle);
  143. // Create a new guild
  144. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` "
  145. "(`guild_id`,`name`,`master`,`guild_lv`,`max_member`,`average_lv`,`char_id`) "
  146. "VALUES ('%d', '%s', '%s', '%d', '%d', '%d', '%d')",
  147. guild_db, g->guild_id, esc_name, esc_master, g->guild_lv, g->max_member, g->average_lv, g->member[0].char_id) )
  148. {
  149. Sql_ShowDebug(sql_handle);
  150. return 0; //Failed to create guild.
  151. }
  152. }
  153. #endif //TXT_SQL_CONVERT
  154. // If we need an update on an existing guild or more update on the new guild
  155. if (((flag & GS_BASIC_MASK) && !new_guild) || ((flag & (GS_BASIC_MASK & ~GS_BASIC)) && new_guild))
  156. {
  157. StringBuf buf;
  158. bool add_comma = false;
  159. StringBuf_Init(&buf);
  160. StringBuf_Printf(&buf, "UPDATE `%s` SET ", guild_db);
  161. if (flag & GS_EMBLEM)
  162. {
  163. char emblem_data[sizeof(g->emblem_data)*2+1];
  164. char* pData = emblem_data;
  165. strcat(t_info, " emblem");
  166. // Convert emblem_data to hex
  167. //TODO: why not use binary directly? [ultramage]
  168. for(i=0; i<g->emblem_len; i++){
  169. *pData++ = dataToHex[(g->emblem_data[i] >> 4) & 0x0F];
  170. *pData++ = dataToHex[g->emblem_data[i] & 0x0F];
  171. }
  172. *pData = 0;
  173. StringBuf_Printf(&buf, "`emblem_len`=%d, `emblem_id`=%d, `emblem_data`='%s'", g->emblem_len, g->emblem_id, emblem_data);
  174. add_comma = true;
  175. }
  176. if (flag & GS_BASIC)
  177. {
  178. strcat(t_info, " basic");
  179. if( add_comma )
  180. StringBuf_AppendStr(&buf, ", ");
  181. else
  182. add_comma = true;
  183. StringBuf_Printf(&buf, "`name`='%s', `master`='%s', `char_id`=%d", esc_name, esc_master, g->member[0].char_id);
  184. }
  185. if (flag & GS_CONNECT)
  186. {
  187. strcat(t_info, " connect");
  188. if( add_comma )
  189. StringBuf_AppendStr(&buf, ", ");
  190. else
  191. add_comma = true;
  192. StringBuf_Printf(&buf, "`connect_member`=%d, `average_lv`=%d", g->connect_member, g->average_lv);
  193. }
  194. if (flag & GS_MES)
  195. {
  196. char esc_mes1[sizeof(g->mes1)*2+1];
  197. char esc_mes2[sizeof(g->mes2)*2+1];
  198. strcat(t_info, " mes");
  199. if( add_comma )
  200. StringBuf_AppendStr(&buf, ", ");
  201. else
  202. add_comma = true;
  203. Sql_EscapeStringLen(sql_handle, esc_mes1, g->mes1, strnlen(g->mes1, sizeof(g->mes1)));
  204. Sql_EscapeStringLen(sql_handle, esc_mes2, g->mes2, strnlen(g->mes2, sizeof(g->mes2)));
  205. StringBuf_Printf(&buf, "`mes1`='%s', `mes2`='%s'", esc_mes1, esc_mes2);
  206. }
  207. if (flag & GS_LEVEL)
  208. {
  209. strcat(t_info, " level");
  210. if( add_comma )
  211. StringBuf_AppendStr(&buf, ", ");
  212. else
  213. add_comma = true;
  214. StringBuf_Printf(&buf, "`guild_lv`=%d, `skill_point`=%d, `exp`=%"PRIu64", `next_exp`=%u, `max_member`=%d", g->guild_lv, g->skill_point, g->exp, g->next_exp, g->max_member);
  215. }
  216. StringBuf_Printf(&buf, " WHERE `guild_id`=%d", g->guild_id);
  217. if( SQL_ERROR == Sql_Query(sql_handle, "%s", StringBuf_Value(&buf)) )
  218. Sql_ShowDebug(sql_handle);
  219. StringBuf_Destroy(&buf);
  220. }
  221. if (flag&GS_MEMBER)
  222. {
  223. struct guild_member *m;
  224. strcat(t_info, " members");
  225. // Update only needed players
  226. for(i=0;i<g->max_member;i++){
  227. m = &g->member[i];
  228. #ifndef TXT_SQL_CONVERT
  229. if (!m->modified)
  230. continue;
  231. #endif
  232. if(m->account_id) {
  233. //Since nothing references guild member table as foreign keys, it's safe to use REPLACE INTO
  234. Sql_EscapeStringLen(sql_handle, esc_name, m->name, strnlen(m->name, NAME_LENGTH));
  235. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name`) "
  236. "VALUES ('%d','%d','%d','%d','%d','%d','%d','%d','%"PRIu64"','%d','%d','%d','%s')",
  237. guild_member_db, g->guild_id, m->account_id, m->char_id,
  238. m->hair, m->hair_color, m->gender,
  239. m->class_, m->lv, m->exp, m->exp_payper, m->online, m->position, esc_name) )
  240. Sql_ShowDebug(sql_handle);
  241. if (m->modified & GS_MEMBER_NEW)
  242. {
  243. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `guild_id` = '%d' WHERE `char_id` = '%d'",
  244. char_db, g->guild_id, m->char_id) )
  245. Sql_ShowDebug(sql_handle);
  246. }
  247. m->modified = GS_MEMBER_UNMODIFIED;
  248. }
  249. }
  250. }
  251. if (flag&GS_POSITION){
  252. strcat(t_info, " positions");
  253. //printf("- Insert guild %d to guild_position\n",g->guild_id);
  254. for(i=0;i<MAX_GUILDPOSITION;i++){
  255. struct guild_position *p = &g->position[i];
  256. #ifndef TXT_SQL_CONVERT
  257. if (!p->modified)
  258. continue;
  259. #endif
  260. Sql_EscapeStringLen(sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH));
  261. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`position`,`name`,`mode`,`exp_mode`) VALUES ('%d','%d','%s','%d','%d')",
  262. guild_position_db, g->guild_id, i, esc_name, p->mode, p->exp_mode) )
  263. Sql_ShowDebug(sql_handle);
  264. p->modified = GS_POSITION_UNMODIFIED;
  265. }
  266. }
  267. if (flag&GS_ALLIANCE)
  268. {
  269. // Delete current alliances
  270. // NOTE: no need to do it on both sides since both guilds in memory had
  271. // their info changed, not to mention this would also mess up oppositions!
  272. // [Skotlex]
  273. //if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id`='%d' OR `alliance_id`='%d'", guild_alliance_db, g->guild_id, g->guild_id) )
  274. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id`='%d'", guild_alliance_db, g->guild_id) )
  275. {
  276. Sql_ShowDebug(sql_handle);
  277. }
  278. else
  279. {
  280. //printf("- Insert guild %d to guild_alliance\n",g->guild_id);
  281. for(i=0;i<MAX_GUILDALLIANCE;i++)
  282. {
  283. struct guild_alliance *a=&g->alliance[i];
  284. if(a->guild_id>0)
  285. {
  286. Sql_EscapeStringLen(sql_handle, esc_name, a->name, strnlen(a->name, NAME_LENGTH));
  287. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`opposition`,`alliance_id`,`name`) "
  288. "VALUES ('%d','%d','%d','%s')",
  289. guild_alliance_db, g->guild_id, a->opposition, a->guild_id, esc_name) )
  290. Sql_ShowDebug(sql_handle);
  291. }
  292. }
  293. }
  294. }
  295. if (flag&GS_EXPULSION){
  296. strcat(t_info, " expulsions");
  297. //printf("- Insert guild %d to guild_expulsion\n",g->guild_id);
  298. for(i=0;i<MAX_GUILDEXPULSION;i++){
  299. struct guild_expulsion *e=&g->expulsion[i];
  300. if(e->account_id>0){
  301. char esc_mes[sizeof(e->mes)*2+1];
  302. Sql_EscapeStringLen(sql_handle, esc_name, e->name, strnlen(e->name, NAME_LENGTH));
  303. Sql_EscapeStringLen(sql_handle, esc_mes, e->mes, strnlen(e->mes, sizeof(e->mes)));
  304. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`account_id`,`name`,`mes`) "
  305. "VALUES ('%d','%d','%s','%s')", guild_expulsion_db, g->guild_id, e->account_id, esc_name, esc_mes) )
  306. Sql_ShowDebug(sql_handle);
  307. }
  308. }
  309. }
  310. if (flag&GS_SKILL){
  311. strcat(t_info, " skills");
  312. //printf("- Insert guild %d to guild_skill\n",g->guild_id);
  313. for(i=0;i<MAX_GUILDSKILL;i++){
  314. if (g->skill[i].id>0 && g->skill[i].lv>0){
  315. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`id`,`lv`) VALUES ('%d','%d','%d')",
  316. guild_skill_db, g->guild_id, g->skill[i].id, g->skill[i].lv) )
  317. Sql_ShowDebug(sql_handle);
  318. }
  319. }
  320. }
  321. if (save_log)
  322. ShowInfo("Saved guild (%d - %s):%s\n",g->guild_id,g->name,t_info);
  323. return 1;
  324. }
  325. #ifndef TXT_SQL_CONVERT
  326. // Read guild from sql
  327. struct guild * inter_guild_fromsql(int guild_id)
  328. {
  329. struct guild *g;
  330. char* data;
  331. size_t len;
  332. char* p;
  333. int i;
  334. if( guild_id <= 0 )
  335. return NULL;
  336. g = (struct guild*)idb_get(guild_db_, guild_id);
  337. if( g )
  338. return g;
  339. #ifdef NOISY
  340. ShowInfo("Guild load request (%d)...\n", guild_id);
  341. #endif
  342. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT g.`name`,c.`name`,g.`guild_lv`,g.`connect_member`,g.`max_member`,g.`average_lv`,g.`exp`,g.`next_exp`,g.`skill_point`,g.`mes1`,g.`mes2`,g.`emblem_len`,g.`emblem_id`,g.`emblem_data` "
  343. "FROM `%s` g LEFT JOIN `%s` c ON c.`char_id` = g.`char_id` WHERE g.`guild_id`='%d'", guild_db, char_db, guild_id) )
  344. {
  345. Sql_ShowDebug(sql_handle);
  346. return NULL;
  347. }
  348. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  349. return NULL;// Guild does not exists.
  350. CREATE(g, struct guild, 1);
  351. g->guild_id = guild_id;
  352. Sql_GetData(sql_handle, 0, &data, &len); memcpy(g->name, data, min(len, NAME_LENGTH));
  353. Sql_GetData(sql_handle, 1, &data, &len); memcpy(g->master, data, min(len, NAME_LENGTH));
  354. Sql_GetData(sql_handle, 2, &data, NULL); g->guild_lv = atoi(data);
  355. Sql_GetData(sql_handle, 3, &data, NULL); g->connect_member = atoi(data);
  356. Sql_GetData(sql_handle, 4, &data, NULL); g->max_member = atoi(data);
  357. if( g->max_member > MAX_GUILD )
  358. { // Fix reduction of MAX_GUILD [PoW]
  359. ShowWarning("Guild %d:%s specifies higher capacity (%d) than MAX_GUILD (%d)\n", guild_id, g->name, g->max_member, MAX_GUILD);
  360. g->max_member = MAX_GUILD;
  361. }
  362. Sql_GetData(sql_handle, 5, &data, NULL); g->average_lv = atoi(data);
  363. Sql_GetData(sql_handle, 6, &data, NULL); g->exp = strtoull(data, NULL, 10);
  364. Sql_GetData(sql_handle, 7, &data, NULL); g->next_exp = (unsigned int)strtoul(data, NULL, 10);
  365. Sql_GetData(sql_handle, 8, &data, NULL); g->skill_point = atoi(data);
  366. Sql_GetData(sql_handle, 9, &data, &len); memcpy(g->mes1, data, min(len, sizeof(g->mes1)));
  367. Sql_GetData(sql_handle, 10, &data, &len); memcpy(g->mes2, data, min(len, sizeof(g->mes2)));
  368. Sql_GetData(sql_handle, 11, &data, &len); g->emblem_len = atoi(data);
  369. Sql_GetData(sql_handle, 12, &data, &len); g->emblem_id = atoi(data);
  370. Sql_GetData(sql_handle, 13, &data, &len);
  371. // convert emblem data from hexadecimal to binary
  372. //TODO: why not store it in the db as binary directly? [ultramage]
  373. for( i = 0, p = g->emblem_data; i < g->emblem_len; ++i, ++p )
  374. {
  375. if( *data >= '0' && *data <= '9' )
  376. *p = *data - '0';
  377. else if( *data >= 'a' && *data <= 'f' )
  378. *p = *data - 'a' + 10;
  379. else if( *data >= 'A' && *data <= 'F' )
  380. *p = *data - 'A' + 10;
  381. *p <<= 4;
  382. ++data;
  383. if( *data >= '0' && *data <= '9' )
  384. *p |= *data - '0';
  385. else if( *data >= 'a' && *data <= 'f' )
  386. *p |= *data - 'a' + 10;
  387. else if( *data >= 'A' && *data <= 'F' )
  388. *p |= *data - 'A' + 10;
  389. ++data;
  390. }
  391. // load guild member info
  392. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name` "
  393. "FROM `%s` WHERE `guild_id`='%d' ORDER BY `position`", guild_member_db, guild_id) )
  394. {
  395. Sql_ShowDebug(sql_handle);
  396. aFree(g);
  397. return NULL;
  398. }
  399. for( i = 0; i < g->max_member && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  400. {
  401. struct guild_member* m = &g->member[i];
  402. Sql_GetData(sql_handle, 0, &data, NULL); m->account_id = atoi(data);
  403. Sql_GetData(sql_handle, 1, &data, NULL); m->char_id = atoi(data);
  404. Sql_GetData(sql_handle, 2, &data, NULL); m->hair = atoi(data);
  405. Sql_GetData(sql_handle, 3, &data, NULL); m->hair_color = atoi(data);
  406. Sql_GetData(sql_handle, 4, &data, NULL); m->gender = atoi(data);
  407. Sql_GetData(sql_handle, 5, &data, NULL); m->class_ = atoi(data);
  408. Sql_GetData(sql_handle, 6, &data, NULL); m->lv = atoi(data);
  409. Sql_GetData(sql_handle, 7, &data, NULL); m->exp = strtoull(data, NULL, 10);
  410. Sql_GetData(sql_handle, 8, &data, NULL); m->exp_payper = (unsigned int)atoi(data);
  411. Sql_GetData(sql_handle, 9, &data, NULL); m->online = atoi(data);
  412. Sql_GetData(sql_handle, 10, &data, NULL); m->position = atoi(data);
  413. if( m->position >= MAX_GUILDPOSITION ) // Fix reduction of MAX_GUILDPOSITION [PoW]
  414. m->position = MAX_GUILDPOSITION - 1;
  415. Sql_GetData(sql_handle, 11, &data, &len); memcpy(m->name, data, min(len, NAME_LENGTH));
  416. m->modified = GS_MEMBER_UNMODIFIED;
  417. }
  418. //printf("- Read guild_position %d from sql \n",guild_id);
  419. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `position`,`name`,`mode`,`exp_mode` FROM `%s` WHERE `guild_id`='%d'", guild_position_db, guild_id) )
  420. {
  421. Sql_ShowDebug(sql_handle);
  422. aFree(g);
  423. return NULL;
  424. }
  425. while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  426. {
  427. int position;
  428. struct guild_position* p;
  429. Sql_GetData(sql_handle, 0, &data, NULL); position = atoi(data);
  430. if( position < 0 || position >= MAX_GUILDPOSITION )
  431. continue;// invalid position
  432. p = &g->position[position];
  433. Sql_GetData(sql_handle, 1, &data, &len); memcpy(p->name, data, min(len, NAME_LENGTH));
  434. Sql_GetData(sql_handle, 2, &data, NULL); p->mode = atoi(data);
  435. Sql_GetData(sql_handle, 3, &data, NULL); p->exp_mode = atoi(data);
  436. p->modified = GS_POSITION_UNMODIFIED;
  437. }
  438. //printf("- Read guild_alliance %d from sql \n",guild_id);
  439. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `opposition`,`alliance_id`,`name` FROM `%s` WHERE `guild_id`='%d'", guild_alliance_db, guild_id) )
  440. {
  441. Sql_ShowDebug(sql_handle);
  442. aFree(g);
  443. return NULL;
  444. }
  445. for( i = 0; i < MAX_GUILDALLIANCE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  446. {
  447. struct guild_alliance* a = &g->alliance[i];
  448. Sql_GetData(sql_handle, 0, &data, NULL); a->opposition = atoi(data);
  449. Sql_GetData(sql_handle, 1, &data, NULL); a->guild_id = atoi(data);
  450. Sql_GetData(sql_handle, 2, &data, &len); memcpy(a->name, data, min(len, NAME_LENGTH));
  451. }
  452. //printf("- Read guild_expulsion %d from sql \n",guild_id);
  453. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`name`,`mes` FROM `%s` WHERE `guild_id`='%d'", guild_expulsion_db, guild_id) )
  454. {
  455. Sql_ShowDebug(sql_handle);
  456. aFree(g);
  457. return NULL;
  458. }
  459. for( i = 0; i < MAX_GUILDEXPULSION && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  460. {
  461. struct guild_expulsion *e = &g->expulsion[i];
  462. Sql_GetData(sql_handle, 0, &data, NULL); e->account_id = atoi(data);
  463. Sql_GetData(sql_handle, 1, &data, &len); memcpy(e->name, data, min(len, NAME_LENGTH));
  464. Sql_GetData(sql_handle, 2, &data, &len); memcpy(e->mes, data, min(len, sizeof(e->mes)));
  465. }
  466. //printf("- Read guild_skill %d from sql \n",guild_id);
  467. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`lv` FROM `%s` WHERE `guild_id`='%d' ORDER BY `id`", guild_skill_db, guild_id) )
  468. {
  469. Sql_ShowDebug(sql_handle);
  470. aFree(g);
  471. return NULL;
  472. }
  473. for(i = 0; i < MAX_GUILDSKILL; i++)
  474. { //Skill IDs must always be initialized. [Skotlex]
  475. g->skill[i].id = i + GD_SKILLBASE;
  476. }
  477. while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  478. {
  479. int id;
  480. Sql_GetData(sql_handle, 0, &data, NULL); id = atoi(data) - GD_SKILLBASE;
  481. if( id < 0 && id >= MAX_GUILDSKILL )
  482. continue;// invalid guild skill
  483. Sql_GetData(sql_handle, 1, &data, NULL); g->skill[id].lv = atoi(data);
  484. }
  485. Sql_FreeResult(sql_handle);
  486. idb_put(guild_db_, guild_id, g); //Add to cache
  487. g->save_flag |= GS_REMOVE; //But set it to be removed, in case it is not needed for long.
  488. if (save_log)
  489. ShowInfo("Guild loaded (%d - %s)\n", guild_id, g->name);
  490. return g;
  491. }
  492. #endif //TXT_SQL_CONVERT
  493. int inter_guildcastle_tosql(struct guild_castle *gc)
  494. {
  495. // `guild_castle` (`castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`, `visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`)
  496. if (gc==NULL) return 0;
  497. #ifdef GUILD_DEBUG
  498. ShowDebug("Save guild_castle (%d)\n", gc->castle_id);
  499. #endif
  500. // sql_query("DELETE FROM `%s` WHERE `castle_id`='%d'",guild_castle_db, gc->castle_id);
  501. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` "
  502. "(`castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`,"
  503. "`visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`)"
  504. "VALUES ('%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d')",
  505. guild_castle_db, gc->castle_id, gc->guild_id, gc->economy, gc->defense, gc->triggerE, gc->triggerD, gc->nextTime, gc->payTime, gc->createTime, gc->visibleC,
  506. gc->guardian[0].visible, gc->guardian[1].visible, gc->guardian[2].visible, gc->guardian[3].visible, gc->guardian[4].visible, gc->guardian[5].visible, gc->guardian[6].visible, gc->guardian[7].visible) )
  507. Sql_ShowDebug(sql_handle);
  508. #ifndef TXT_SQL_CONVERT
  509. memcpy(&castles[gc->castle_id],gc,sizeof(struct guild_castle));
  510. #endif //TXT_SQL_CONVERT
  511. return 0;
  512. }
  513. #ifndef TXT_SQL_CONVERT
  514. // Read guild_castle from sql
  515. int inter_guildcastle_fromsql(int castle_id,struct guild_castle *gc)
  516. {
  517. static int castles_init=0;
  518. char* data;
  519. if (gc==NULL) return 0;
  520. if (castle_id==-1) return 0;
  521. if(!castles_init)
  522. {
  523. int i;
  524. for(i=0;i<MAX_GUILDCASTLE;i++)
  525. castles[i].castle_id=-1;
  526. castles_init = 1;
  527. }
  528. if(castles[castle_id].castle_id == castle_id)
  529. {
  530. memcpy(gc,&castles[castle_id],sizeof(struct guild_castle));
  531. return 1;
  532. }
  533. memset(gc,0,sizeof(struct guild_castle));
  534. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`, "
  535. "`visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`"
  536. " FROM `%s` WHERE `castle_id`='%d'", guild_castle_db, castle_id) )
  537. {
  538. Sql_ShowDebug(sql_handle);
  539. return 0;
  540. }
  541. // ARU: This needs to be set even if there are no SQL results
  542. gc->castle_id=castle_id;
  543. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  544. {
  545. Sql_FreeResult(sql_handle);
  546. return 1; //Assume empty castle.
  547. }
  548. Sql_GetData(sql_handle, 1, &data, NULL); gc->guild_id = atoi(data);
  549. Sql_GetData(sql_handle, 2, &data, NULL); gc->economy = atoi(data);
  550. Sql_GetData(sql_handle, 3, &data, NULL); gc->defense = atoi(data);
  551. Sql_GetData(sql_handle, 4, &data, NULL); gc->triggerE = atoi(data);
  552. Sql_GetData(sql_handle, 5, &data, NULL); gc->triggerD = atoi(data);
  553. Sql_GetData(sql_handle, 6, &data, NULL); gc->nextTime = atoi(data);
  554. Sql_GetData(sql_handle, 7, &data, NULL); gc->payTime = atoi(data);
  555. Sql_GetData(sql_handle, 8, &data, NULL); gc->createTime = atoi(data);
  556. Sql_GetData(sql_handle, 9, &data, NULL); gc->visibleC = atoi(data);
  557. Sql_GetData(sql_handle, 10, &data, NULL); gc->guardian[0].visible = atoi(data);
  558. Sql_GetData(sql_handle, 11, &data, NULL); gc->guardian[1].visible = atoi(data);
  559. Sql_GetData(sql_handle, 12, &data, NULL); gc->guardian[2].visible = atoi(data);
  560. Sql_GetData(sql_handle, 13, &data, NULL); gc->guardian[3].visible = atoi(data);
  561. Sql_GetData(sql_handle, 14, &data, NULL); gc->guardian[4].visible = atoi(data);
  562. Sql_GetData(sql_handle, 15, &data, NULL); gc->guardian[5].visible = atoi(data);
  563. Sql_GetData(sql_handle, 16, &data, NULL); gc->guardian[6].visible = atoi(data);
  564. Sql_GetData(sql_handle, 17, &data, NULL); gc->guardian[7].visible = atoi(data);
  565. Sql_FreeResult(sql_handle);
  566. memcpy(&castles[castle_id],gc,sizeof(struct guild_castle));
  567. if( save_log )
  568. ShowInfo("Loaded Castle %d (guild %d)\n", castle_id, gc->guild_id);
  569. return 1;
  570. }
  571. // Read exp_guild.txt
  572. int inter_guild_ReadEXP(void)
  573. {
  574. int i;
  575. FILE *fp;
  576. char line[1024];
  577. for (i=0;i<100;i++) guild_exp[i]=0;
  578. //this is going to be discussed, temp fix
  579. sprintf(line, "%s/pre-re/exp_guild.txt", db_path);
  580. fp=fopen(line,"r");
  581. if(fp==NULL){
  582. ShowError("can't read %s\n", line);
  583. return 1;
  584. }
  585. i=0;
  586. while(fgets(line, sizeof(line), fp) && i < 100)
  587. {
  588. if(line[0]=='/' && line[1]=='/')
  589. continue;
  590. guild_exp[i]=(unsigned int)atof(line);
  591. i++;
  592. }
  593. fclose(fp);
  594. return 0;
  595. }
  596. int inter_guild_CharOnline(int char_id, int guild_id)
  597. {
  598. struct guild *g;
  599. int i;
  600. if (guild_id == -1) {
  601. //Get guild_id from the database
  602. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT guild_id FROM `%s` WHERE char_id='%d'", char_db, char_id) )
  603. {
  604. Sql_ShowDebug(sql_handle);
  605. return 0;
  606. }
  607. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  608. {
  609. char* data;
  610. Sql_GetData(sql_handle, 0, &data, NULL);
  611. guild_id = atoi(data);
  612. }
  613. else
  614. {
  615. guild_id = 0;
  616. }
  617. Sql_FreeResult(sql_handle);
  618. }
  619. if (guild_id == 0)
  620. return 0; //No guild...
  621. g = inter_guild_fromsql(guild_id);
  622. if(!g) {
  623. ShowError("Character %d's guild %d not found!\n", char_id, guild_id);
  624. return 0;
  625. }
  626. //Member has logged in before saving, tell saver not to delete
  627. if(g->save_flag & GS_REMOVE)
  628. g->save_flag &= ~GS_REMOVE;
  629. //Set member online
  630. ARR_FIND( 0, g->max_member, i, g->member[i].char_id == char_id );
  631. if( i < g->max_member )
  632. {
  633. g->member[i].online = 1;
  634. g->member[i].modified = GS_MEMBER_MODIFIED;
  635. }
  636. return 1;
  637. }
  638. int inter_guild_CharOffline(int char_id, int guild_id)
  639. {
  640. struct guild *g=NULL;
  641. int online_count, i;
  642. if (guild_id == -1)
  643. {
  644. //Get guild_id from the database
  645. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT guild_id FROM `%s` WHERE char_id='%d'", char_db, char_id) )
  646. {
  647. Sql_ShowDebug(sql_handle);
  648. return 0;
  649. }
  650. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  651. {
  652. char* data;
  653. Sql_GetData(sql_handle, 0, &data, NULL);
  654. guild_id = atoi(data);
  655. }
  656. else
  657. {
  658. guild_id = 0;
  659. }
  660. Sql_FreeResult(sql_handle);
  661. }
  662. if (guild_id == 0)
  663. return 0; //No guild...
  664. //Character has a guild, set character offline and check if they were the only member online
  665. g = inter_guild_fromsql(guild_id);
  666. if (g == NULL) //Guild not found?
  667. return 0;
  668. //Set member offline
  669. ARR_FIND( 0, g->max_member, i, g->member[i].char_id == char_id );
  670. if( i < g->max_member )
  671. {
  672. g->member[i].online = 0;
  673. g->member[i].modified = GS_MEMBER_MODIFIED;
  674. }
  675. online_count = 0;
  676. for( i = 0; i < g->max_member; i++ )
  677. if( g->member[i].online )
  678. online_count++;
  679. // Remove guild from memory if no players online
  680. if( online_count == 0 )
  681. g->save_flag |= GS_REMOVE;
  682. return 1;
  683. }
  684. // Initialize guild sql
  685. int inter_guild_sql_init(void)
  686. {
  687. //Initialize the guild cache
  688. guild_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  689. //Read exp file
  690. inter_guild_ReadEXP();
  691. add_timer_func_list(guild_save_timer, "guild_save_timer");
  692. add_timer(gettick() + 10000, guild_save_timer, 0, 0);
  693. return 0;
  694. }
  695. static int guild_db_final(DBKey key, void *data, va_list ap)
  696. {
  697. struct guild *g = (struct guild*)data;
  698. if (g->save_flag&GS_MASK) {
  699. inter_guild_tosql(g, g->save_flag&GS_MASK);
  700. return 1;
  701. }
  702. return 0;
  703. }
  704. void inter_guild_sql_final(void)
  705. {
  706. guild_db_->destroy(guild_db_, guild_db_final);
  707. return;
  708. }
  709. // Get guild_id by its name. Returns 0 if not found, -1 on error.
  710. int search_guildname(char *str)
  711. {
  712. int guild_id;
  713. char esc_name[NAME_LENGTH*2+1];
  714. Sql_EscapeStringLen(sql_handle, esc_name, str, safestrnlen(str, NAME_LENGTH));
  715. //Lookup guilds with the same name
  716. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT guild_id FROM `%s` WHERE name='%s'", guild_db, esc_name) )
  717. {
  718. Sql_ShowDebug(sql_handle);
  719. return -1;
  720. }
  721. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  722. {
  723. char* data;
  724. Sql_GetData(sql_handle, 0, &data, NULL);
  725. guild_id = atoi(data);
  726. }
  727. else
  728. {
  729. guild_id = 0;
  730. }
  731. Sql_FreeResult(sql_handle);
  732. return guild_id;
  733. }
  734. // Check if guild is empty
  735. static bool guild_check_empty(struct guild *g)
  736. {
  737. int i;
  738. ARR_FIND( 0, g->max_member, i, g->member[i].account_id > 0 );
  739. if( i < g->max_member)
  740. return false; // not empty
  741. //Let the calling function handle the guild removal in case they need
  742. //to do something else with it before freeing the data. [Skotlex]
  743. return true;
  744. }
  745. unsigned int guild_nextexp(int level)
  746. {
  747. if (level == 0)
  748. return 1;
  749. if (level < 100 && level > 0) // Change by hack
  750. return guild_exp[level-1];
  751. return 0;
  752. }
  753. int guild_checkskill(struct guild *g,int id)
  754. {
  755. int idx = id - GD_SKILLBASE;
  756. if(idx < 0 || idx >= MAX_GUILDSKILL)
  757. return 0;
  758. return g->skill[idx].lv;
  759. }
  760. int guild_calcinfo(struct guild *g)
  761. {
  762. int i,c;
  763. unsigned int nextexp;
  764. struct guild before = *g; // Save guild current values
  765. if(g->guild_lv<=0)
  766. g->guild_lv = 1;
  767. nextexp = guild_nextexp(g->guild_lv);
  768. // Consume guild exp and increase guild level
  769. while(g->exp >= nextexp && nextexp > 0){ //fixed guild exp overflow [Kevin]
  770. g->exp-=nextexp;
  771. g->guild_lv++;
  772. g->skill_point++;
  773. nextexp = guild_nextexp(g->guild_lv);
  774. }
  775. // Save next exp step
  776. g->next_exp = nextexp;
  777. // Set the max number of members, Guild Extention skill - currently adds 6 to max per skill lv.
  778. g->max_member = 16 + guild_checkskill(g, GD_EXTENSION) * 6;
  779. if(g->max_member > MAX_GUILD)
  780. {
  781. ShowError("Guild %d:%s has capacity for too many guild members (%d), max supported is %d\n", g->guild_id, g->name, g->max_member, MAX_GUILD);
  782. g->max_member = MAX_GUILD;
  783. }
  784. // Compute the guild average level level
  785. g->average_lv=0;
  786. g->connect_member=0;
  787. for(i=c=0;i<g->max_member;i++)
  788. {
  789. if(g->member[i].account_id>0)
  790. {
  791. if (g->member[i].lv >= 0)
  792. {
  793. g->average_lv+=g->member[i].lv;
  794. c++;
  795. }
  796. else
  797. {
  798. ShowWarning("Guild %d:%s, member %d:%s has an invalid level %d\n", g->guild_id, g->name, g->member[i].char_id, g->member[i].name, g->member[i].lv);
  799. }
  800. if(g->member[i].online)
  801. g->connect_member++;
  802. }
  803. }
  804. if(c)
  805. g->average_lv /= c;
  806. // Check if guild stats has change
  807. if(g->max_member != before.max_member || g->guild_lv != before.guild_lv || g->skill_point != before.skill_point )
  808. {
  809. g->save_flag |= GS_LEVEL;
  810. mapif_guild_info(-1,g);
  811. return 1;
  812. }
  813. return 0;
  814. }
  815. //-------------------------------------------------------------------
  816. // Packet sent to map server
  817. int mapif_guild_created(int fd,int account_id,struct guild *g)
  818. {
  819. WFIFOHEAD(fd, 10);
  820. WFIFOW(fd,0)=0x3830;
  821. WFIFOL(fd,2)=account_id;
  822. if(g != NULL)
  823. {
  824. WFIFOL(fd,6)=g->guild_id;
  825. ShowInfo("int_guild: Guild created (%d - %s)\n",g->guild_id,g->name);
  826. } else
  827. WFIFOL(fd,6)=0;
  828. WFIFOSET(fd,10);
  829. return 0;
  830. }
  831. // Guild not found
  832. int mapif_guild_noinfo(int fd,int guild_id)
  833. {
  834. unsigned char buf[12];
  835. WBUFW(buf,0)=0x3831;
  836. WBUFW(buf,2)=8;
  837. WBUFL(buf,4)=guild_id;
  838. ShowWarning("int_guild: info not found %d\n",guild_id);
  839. if(fd<0)
  840. mapif_sendall(buf,8);
  841. else
  842. mapif_send(fd,buf,8);
  843. return 0;
  844. }
  845. // Send guild info
  846. int mapif_guild_info(int fd,struct guild *g)
  847. {
  848. unsigned char buf[8+sizeof(struct guild)];
  849. WBUFW(buf,0)=0x3831;
  850. WBUFW(buf,2)=4+sizeof(struct guild);
  851. memcpy(buf+4,g,sizeof(struct guild));
  852. if(fd<0)
  853. mapif_sendall(buf,WBUFW(buf,2));
  854. else
  855. mapif_send(fd,buf,WBUFW(buf,2));
  856. return 0;
  857. }
  858. // ACK member add
  859. int mapif_guild_memberadded(int fd,int guild_id,int account_id,int char_id,int flag)
  860. {
  861. WFIFOHEAD(fd, 15);
  862. WFIFOW(fd,0)=0x3832;
  863. WFIFOL(fd,2)=guild_id;
  864. WFIFOL(fd,6)=account_id;
  865. WFIFOL(fd,10)=char_id;
  866. WFIFOB(fd,14)=flag;
  867. WFIFOSET(fd,15);
  868. return 0;
  869. }
  870. // ACK member leave
  871. int mapif_guild_withdraw(int guild_id,int account_id,int char_id,int flag, const char *name, const char *mes)
  872. {
  873. unsigned char buf[55+NAME_LENGTH];
  874. WBUFW(buf, 0)=0x3834;
  875. WBUFL(buf, 2)=guild_id;
  876. WBUFL(buf, 6)=account_id;
  877. WBUFL(buf,10)=char_id;
  878. WBUFB(buf,14)=flag;
  879. memcpy(WBUFP(buf,15),mes,40);
  880. memcpy(WBUFP(buf,55),name,NAME_LENGTH);
  881. mapif_sendall(buf,55+NAME_LENGTH);
  882. ShowInfo("int_guild: guild withdraw (%d - %d: %s - %s)\n",guild_id,account_id,name,mes);
  883. return 0;
  884. }
  885. // Send short member's info
  886. int mapif_guild_memberinfoshort(struct guild *g,int idx)
  887. {
  888. unsigned char buf[19];
  889. WBUFW(buf, 0)=0x3835;
  890. WBUFL(buf, 2)=g->guild_id;
  891. WBUFL(buf, 6)=g->member[idx].account_id;
  892. WBUFL(buf,10)=g->member[idx].char_id;
  893. WBUFB(buf,14)=(unsigned char)g->member[idx].online;
  894. WBUFW(buf,15)=g->member[idx].lv;
  895. WBUFW(buf,17)=g->member[idx].class_;
  896. mapif_sendall(buf,19);
  897. return 0;
  898. }
  899. // Send guild broken
  900. int mapif_guild_broken(int guild_id,int flag)
  901. {
  902. unsigned char buf[7];
  903. WBUFW(buf,0)=0x3836;
  904. WBUFL(buf,2)=guild_id;
  905. WBUFB(buf,6)=flag;
  906. mapif_sendall(buf,7);
  907. ShowInfo("int_guild: Guild broken (%d)\n",guild_id);
  908. return 0;
  909. }
  910. // Send guild message
  911. int mapif_guild_message(int guild_id,int account_id,char *mes,int len, int sfd)
  912. {
  913. unsigned char buf[512];
  914. if (len > 500)
  915. len = 500;
  916. WBUFW(buf,0)=0x3837;
  917. WBUFW(buf,2)=len+12;
  918. WBUFL(buf,4)=guild_id;
  919. WBUFL(buf,8)=account_id;
  920. memcpy(WBUFP(buf,12),mes,len);
  921. mapif_sendallwos(sfd, buf,len+12);
  922. return 0;
  923. }
  924. // Send basic info
  925. int mapif_guild_basicinfochanged(int guild_id,int type,const void *data,int len)
  926. {
  927. unsigned char buf[2048];
  928. if (len > 2038)
  929. len = 2038;
  930. WBUFW(buf, 0)=0x3839;
  931. WBUFW(buf, 2)=len+10;
  932. WBUFL(buf, 4)=guild_id;
  933. WBUFW(buf, 8)=type;
  934. memcpy(WBUFP(buf,10),data,len);
  935. mapif_sendall(buf,len+10);
  936. return 0;
  937. }
  938. // Send member info
  939. int mapif_guild_memberinfochanged(int guild_id,int account_id,int char_id, int type,const void *data,int len)
  940. {
  941. unsigned char buf[2048];
  942. if (len > 2030)
  943. len = 2030;
  944. WBUFW(buf, 0)=0x383a;
  945. WBUFW(buf, 2)=len+18;
  946. WBUFL(buf, 4)=guild_id;
  947. WBUFL(buf, 8)=account_id;
  948. WBUFL(buf,12)=char_id;
  949. WBUFW(buf,16)=type;
  950. memcpy(WBUFP(buf,18),data,len);
  951. mapif_sendall(buf,len+18);
  952. return 0;
  953. }
  954. // ACK guild skill up
  955. int mapif_guild_skillupack(int guild_id,int skill_num,int account_id)
  956. {
  957. unsigned char buf[14];
  958. WBUFW(buf, 0)=0x383c;
  959. WBUFL(buf, 2)=guild_id;
  960. WBUFL(buf, 6)=skill_num;
  961. WBUFL(buf,10)=account_id;
  962. mapif_sendall(buf,14);
  963. return 0;
  964. }
  965. // ACK guild alliance
  966. int mapif_guild_alliance(int guild_id1,int guild_id2,int account_id1,int account_id2,int flag,const char *name1,const char *name2)
  967. {
  968. unsigned char buf[19+2*NAME_LENGTH];
  969. WBUFW(buf, 0)=0x383d;
  970. WBUFL(buf, 2)=guild_id1;
  971. WBUFL(buf, 6)=guild_id2;
  972. WBUFL(buf,10)=account_id1;
  973. WBUFL(buf,14)=account_id2;
  974. WBUFB(buf,18)=flag;
  975. memcpy(WBUFP(buf,19),name1,NAME_LENGTH);
  976. memcpy(WBUFP(buf,19+NAME_LENGTH),name2,NAME_LENGTH);
  977. mapif_sendall(buf,19+2*NAME_LENGTH);
  978. return 0;
  979. }
  980. // Send a guild position desc
  981. int mapif_guild_position(struct guild *g,int idx)
  982. {
  983. unsigned char buf[12 + sizeof(struct guild_position)];
  984. WBUFW(buf,0)=0x383b;
  985. WBUFW(buf,2)=sizeof(struct guild_position)+12;
  986. WBUFL(buf,4)=g->guild_id;
  987. WBUFL(buf,8)=idx;
  988. memcpy(WBUFP(buf,12),&g->position[idx],sizeof(struct guild_position));
  989. mapif_sendall(buf,WBUFW(buf,2));
  990. return 0;
  991. }
  992. // Send the guild notice
  993. int mapif_guild_notice(struct guild *g)
  994. {
  995. unsigned char buf[256];
  996. WBUFW(buf,0)=0x383e;
  997. WBUFL(buf,2)=g->guild_id;
  998. memcpy(WBUFP(buf,6),g->mes1,MAX_GUILDMES1);
  999. memcpy(WBUFP(buf,66),g->mes2,MAX_GUILDMES2);
  1000. mapif_sendall(buf,186);
  1001. return 0;
  1002. }
  1003. // Send emblem data
  1004. int mapif_guild_emblem(struct guild *g)
  1005. {
  1006. unsigned char buf[12 + sizeof(g->emblem_data)];
  1007. WBUFW(buf,0)=0x383f;
  1008. WBUFW(buf,2)=g->emblem_len+12;
  1009. WBUFL(buf,4)=g->guild_id;
  1010. WBUFL(buf,8)=g->emblem_id;
  1011. memcpy(WBUFP(buf,12),g->emblem_data,g->emblem_len);
  1012. mapif_sendall(buf,WBUFW(buf,2));
  1013. return 0;
  1014. }
  1015. int mapif_guild_master_changed(struct guild *g, int aid, int cid)
  1016. {
  1017. unsigned char buf[14];
  1018. WBUFW(buf,0)=0x3843;
  1019. WBUFL(buf,2)=g->guild_id;
  1020. WBUFL(buf,6)=aid;
  1021. WBUFL(buf,10)=cid;
  1022. mapif_sendall(buf,14);
  1023. return 0;
  1024. }
  1025. int mapif_guild_castle_dataload(int castle_id,int index,int value)
  1026. {
  1027. unsigned char buf[9];
  1028. WBUFW(buf, 0)=0x3840;
  1029. WBUFW(buf, 2)=castle_id;
  1030. WBUFB(buf, 4)=index;
  1031. WBUFL(buf, 5)=value;
  1032. mapif_sendall(buf,9);
  1033. return 0;
  1034. }
  1035. int mapif_guild_castle_datasave(int castle_id,int index,int value)
  1036. {
  1037. unsigned char buf[9];
  1038. WBUFW(buf, 0)=0x3841;
  1039. WBUFW(buf, 2)=castle_id;
  1040. WBUFB(buf, 4)=index;
  1041. WBUFL(buf, 5)=value;
  1042. mapif_sendall(buf,9);
  1043. return 0;
  1044. }
  1045. int mapif_guild_castle_alldataload(int fd)
  1046. {
  1047. struct guild_castle s_gc;
  1048. struct guild_castle* gc = &s_gc;
  1049. int i;
  1050. int len;
  1051. char* data;
  1052. WFIFOHEAD(fd, 4 + MAX_GUILDCASTLE*sizeof(struct guild_castle));
  1053. WFIFOW(fd, 0) = 0x3842;
  1054. if( SQL_ERROR == Sql_Query(sql_handle,
  1055. "SELECT `castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`,"
  1056. " `visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`"
  1057. " FROM `%s` ORDER BY `castle_id`", guild_castle_db) )
  1058. Sql_ShowDebug(sql_handle);
  1059. for( i = 0, len = 4; i < MAX_GUILDCASTLE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1060. {
  1061. memset(gc, 0, sizeof(struct guild_castle));
  1062. Sql_GetData(sql_handle, 0, &data, NULL); gc->castle_id = atoi(data);
  1063. Sql_GetData(sql_handle, 1, &data, NULL); gc->guild_id = atoi(data);
  1064. Sql_GetData(sql_handle, 2, &data, NULL); gc->economy = atoi(data);
  1065. Sql_GetData(sql_handle, 3, &data, NULL); gc->defense = atoi(data);
  1066. Sql_GetData(sql_handle, 4, &data, NULL); gc->triggerE = atoi(data);
  1067. Sql_GetData(sql_handle, 5, &data, NULL); gc->triggerD = atoi(data);
  1068. Sql_GetData(sql_handle, 6, &data, NULL); gc->nextTime = atoi(data);
  1069. Sql_GetData(sql_handle, 7, &data, NULL); gc->payTime = atoi(data);
  1070. Sql_GetData(sql_handle, 8, &data, NULL); gc->createTime = atoi(data);
  1071. Sql_GetData(sql_handle, 9, &data, NULL); gc->visibleC = atoi(data);
  1072. Sql_GetData(sql_handle, 10, &data, NULL); gc->guardian[0].visible = atoi(data);
  1073. Sql_GetData(sql_handle, 11, &data, NULL); gc->guardian[1].visible = atoi(data);
  1074. Sql_GetData(sql_handle, 12, &data, NULL); gc->guardian[2].visible = atoi(data);
  1075. Sql_GetData(sql_handle, 13, &data, NULL); gc->guardian[3].visible = atoi(data);
  1076. Sql_GetData(sql_handle, 14, &data, NULL); gc->guardian[4].visible = atoi(data);
  1077. Sql_GetData(sql_handle, 15, &data, NULL); gc->guardian[5].visible = atoi(data);
  1078. Sql_GetData(sql_handle, 16, &data, NULL); gc->guardian[6].visible = atoi(data);
  1079. Sql_GetData(sql_handle, 17, &data, NULL); gc->guardian[7].visible = atoi(data);
  1080. memcpy(WFIFOP(fd, len), gc, sizeof(struct guild_castle));
  1081. len += sizeof(struct guild_castle);
  1082. }
  1083. Sql_FreeResult(sql_handle);
  1084. WFIFOW(fd, 2) = len;
  1085. WFIFOSET(fd, len);
  1086. return 0;
  1087. }
  1088. //-------------------------------------------------------------------
  1089. // Packet received from map server
  1090. // ギルド作成要求
  1091. int mapif_parse_CreateGuild(int fd,int account_id,char *name,struct guild_member *master)
  1092. {
  1093. struct guild *g;
  1094. int i=0;
  1095. #ifdef NOISY
  1096. ShowInfo("Creating Guild (%s)\n", name);
  1097. #endif
  1098. if(search_guildname(name) != 0){
  1099. ShowInfo("int_guild: guild with same name exists [%s]\n",name);
  1100. mapif_guild_created(fd,account_id,NULL);
  1101. return 0;
  1102. }
  1103. // Check Authorised letters/symbols in the name of the character
  1104. if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
  1105. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1106. if (strchr(char_name_letters, name[i]) == NULL) {
  1107. mapif_guild_created(fd,account_id,NULL);
  1108. return 0;
  1109. }
  1110. } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
  1111. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1112. if (strchr(char_name_letters, name[i]) != NULL) {
  1113. mapif_guild_created(fd,account_id,NULL);
  1114. return 0;
  1115. }
  1116. }
  1117. g = (struct guild *)aMalloc(sizeof(struct guild));
  1118. memset(g,0,sizeof(struct guild));
  1119. memcpy(g->name,name,NAME_LENGTH);
  1120. memcpy(g->master,master->name,NAME_LENGTH);
  1121. memcpy(&g->member[0],master,sizeof(struct guild_member));
  1122. g->member[0].modified = GS_MEMBER_MODIFIED;
  1123. // Set default positions
  1124. g->position[0].mode=0x11;
  1125. strcpy(g->position[0].name,"GuildMaster");
  1126. strcpy(g->position[MAX_GUILDPOSITION-1].name,"Newbie");
  1127. g->position[0].modified = g->position[MAX_GUILDPOSITION-1].modified = GS_POSITION_MODIFIED;
  1128. for(i=1;i<MAX_GUILDPOSITION-1;i++) {
  1129. sprintf(g->position[i].name,"Position %d",i+1);
  1130. g->position[i].modified = GS_POSITION_MODIFIED;
  1131. }
  1132. // Initialize guild property
  1133. g->max_member=16;
  1134. g->average_lv=master->lv;
  1135. g->connect_member=1;
  1136. for(i=0;i<MAX_GUILDSKILL;i++)
  1137. g->skill[i].id=i + GD_SKILLBASE;
  1138. g->guild_id= -1; //Request to create guild.
  1139. // Create the guild
  1140. if (!inter_guild_tosql(g,GS_BASIC|GS_POSITION|GS_SKILL)) {
  1141. //Failed to Create guild....
  1142. ShowError("Failed to create Guild %s (Guild Master: %s)\n", g->name, g->master);
  1143. mapif_guild_created(fd,account_id,NULL);
  1144. aFree(g);
  1145. return 0;
  1146. }
  1147. ShowInfo("Created Guild %d - %s (Guild Master: %s)\n", g->guild_id, g->name, g->master);
  1148. //Add to cache
  1149. idb_put(guild_db_, g->guild_id, g);
  1150. // Report to client
  1151. mapif_guild_created(fd,account_id,g);
  1152. mapif_guild_info(fd,g);
  1153. if(log_inter)
  1154. inter_log("guild %s (id=%d) created by master %s (id=%d)\n",
  1155. name, g->guild_id, master->name, master->account_id );
  1156. return 0;
  1157. }
  1158. // Return guild info to client
  1159. int mapif_parse_GuildInfo(int fd,int guild_id)
  1160. {
  1161. struct guild * g = inter_guild_fromsql(guild_id); //We use this because on start-up the info of castle-owned guilds is requied. [Skotlex]
  1162. if(g)
  1163. {
  1164. if (!guild_calcinfo(g))
  1165. mapif_guild_info(fd,g);
  1166. }
  1167. else
  1168. mapif_guild_noinfo(fd,guild_id); // Failed to load info
  1169. return 0;
  1170. }
  1171. // Add member to guild
  1172. int mapif_parse_GuildAddMember(int fd,int guild_id,struct guild_member *m)
  1173. {
  1174. struct guild * g;
  1175. int i;
  1176. g = inter_guild_fromsql(guild_id);
  1177. if(g==NULL){
  1178. // Failed to add
  1179. mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,1);
  1180. return 0;
  1181. }
  1182. // Find an empty slot
  1183. for(i=0;i<g->max_member;i++)
  1184. {
  1185. if(g->member[i].account_id==0)
  1186. {
  1187. memcpy(&g->member[i],m,sizeof(struct guild_member));
  1188. g->member[i].modified = (GS_MEMBER_NEW | GS_MEMBER_MODIFIED);
  1189. mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,0);
  1190. if (!guild_calcinfo(g)) //Send members if it was not invoked.
  1191. mapif_guild_info(-1,g);
  1192. g->save_flag |= GS_MEMBER;
  1193. if (g->save_flag&GS_REMOVE)
  1194. g->save_flag&=~GS_REMOVE;
  1195. return 0;
  1196. }
  1197. }
  1198. // Failed to add
  1199. mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,1);
  1200. return 0;
  1201. }
  1202. // Delete member from guild
  1203. int mapif_parse_GuildLeave(int fd, int guild_id, int account_id, int char_id, int flag, const char *mes)
  1204. {
  1205. int i, j;
  1206. struct guild* g = inter_guild_fromsql(guild_id);
  1207. if( g == NULL )
  1208. {
  1209. // Unknown guild, just update the player
  1210. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `guild_id`='0' WHERE `account_id`='%d' AND `char_id`='%d'", char_db, account_id, char_id) )
  1211. Sql_ShowDebug(sql_handle);
  1212. // mapif_guild_withdraw(guild_id,account_id,char_id,flag,g->member[i].name,mes);
  1213. return 0;
  1214. }
  1215. // Find the member
  1216. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == account_id && g->member[i].char_id == char_id );
  1217. if( i == g->max_member )
  1218. {
  1219. //TODO
  1220. return 0;
  1221. }
  1222. if( flag )
  1223. { // Write expulsion reason
  1224. // Find an empty slot
  1225. ARR_FIND( 0, MAX_GUILDEXPULSION, j, g->expulsion[j].account_id == 0 );
  1226. if( j == MAX_GUILDEXPULSION )
  1227. {
  1228. // Expulsion list is full, flush the oldest one
  1229. for( j = 0; j < MAX_GUILDEXPULSION - 1; j++ )
  1230. g->expulsion[j] = g->expulsion[j+1];
  1231. j = MAX_GUILDEXPULSION-1;
  1232. }
  1233. // Save the expulsion entry
  1234. g->expulsion[j].account_id = account_id;
  1235. safestrncpy(g->expulsion[j].name, g->member[i].name, NAME_LENGTH);
  1236. safestrncpy(g->expulsion[j].mes, mes, 40);
  1237. }
  1238. mapif_guild_withdraw(guild_id,account_id,char_id,flag,g->member[i].name,mes);
  1239. inter_guild_removemember_tosql(g->member[i].account_id,g->member[i].char_id);
  1240. memset(&g->member[i],0,sizeof(struct guild_member));
  1241. if( guild_check_empty(g) )
  1242. mapif_parse_BreakGuild(-1,guild_id); //Break the guild.
  1243. else {
  1244. //Update member info.
  1245. if (!guild_calcinfo(g))
  1246. mapif_guild_info(fd,g);
  1247. g->save_flag |= GS_EXPULSION;
  1248. }
  1249. return 0;
  1250. }
  1251. // Change member info
  1252. int mapif_parse_GuildChangeMemberInfoShort(int fd,int guild_id,int account_id,int char_id,int online,int lv,int class_)
  1253. {
  1254. // Could speed up by manipulating only guild_member
  1255. struct guild * g;
  1256. int i,sum,c;
  1257. int prev_count, prev_alv;
  1258. g = inter_guild_fromsql(guild_id);
  1259. if(g==NULL)
  1260. return 0;
  1261. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == account_id && g->member[i].char_id == char_id );
  1262. if( i < g->max_member )
  1263. {
  1264. g->member[i].online = online;
  1265. g->member[i].lv = lv;
  1266. g->member[i].class_ = class_;
  1267. g->member[i].modified = GS_MEMBER_MODIFIED;
  1268. mapif_guild_memberinfoshort(g,i);
  1269. }
  1270. prev_count = g->connect_member;
  1271. prev_alv = g->average_lv;
  1272. g->average_lv = 0;
  1273. g->connect_member = 0;
  1274. c = 0;
  1275. sum = 0;
  1276. for( i = 0; i < g->max_member; i++ )
  1277. {
  1278. if( g->member[i].account_id > 0 )
  1279. {
  1280. sum += g->member[i].lv;
  1281. c++;
  1282. }
  1283. if( g->member[i].online )
  1284. g->connect_member++;
  1285. }
  1286. if( c ) // this check should always succeed...
  1287. {
  1288. g->average_lv = sum / c;
  1289. if( g->connect_member != prev_count || g->average_lv != prev_alv )
  1290. g->save_flag |= GS_CONNECT;
  1291. if( g->save_flag & GS_REMOVE )
  1292. g->save_flag &= ~GS_REMOVE;
  1293. }
  1294. g->save_flag |= GS_MEMBER; //Update guild member data
  1295. return 0;
  1296. }
  1297. // BreakGuild
  1298. int mapif_parse_BreakGuild(int fd,int guild_id)
  1299. {
  1300. struct guild * g;
  1301. g = inter_guild_fromsql(guild_id);
  1302. if(g==NULL)
  1303. return 0;
  1304. // Delete guild from sql
  1305. //printf("- Delete guild %d from guild\n",guild_id);
  1306. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_db, guild_id) )
  1307. Sql_ShowDebug(sql_handle);
  1308. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_member_db, guild_id) )
  1309. Sql_ShowDebug(sql_handle);
  1310. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_castle_db, guild_id) )
  1311. Sql_ShowDebug(sql_handle);
  1312. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_storage_db, guild_id) )
  1313. Sql_ShowDebug(sql_handle);
  1314. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d' OR `alliance_id` = '%d'", guild_alliance_db, guild_id, guild_id) )
  1315. Sql_ShowDebug(sql_handle);
  1316. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_position_db, guild_id) )
  1317. Sql_ShowDebug(sql_handle);
  1318. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_skill_db, guild_id) )
  1319. Sql_ShowDebug(sql_handle);
  1320. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_expulsion_db, guild_id) )
  1321. Sql_ShowDebug(sql_handle);
  1322. //printf("- Update guild %d of char\n",guild_id);
  1323. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `guild_id`='0' WHERE `guild_id`='%d'", char_db, guild_id) )
  1324. Sql_ShowDebug(sql_handle);
  1325. mapif_guild_broken(guild_id,0);
  1326. if(log_inter)
  1327. inter_log("guild %s (id=%d) broken\n",g->name,guild_id);
  1328. //Remove the guild from memory. [Skotlex]
  1329. idb_remove(guild_db_, guild_id);
  1330. return 0;
  1331. }
  1332. // Forward Guild message to others map servers
  1333. int mapif_parse_GuildMessage(int fd,int guild_id,int account_id,char *mes,int len)
  1334. {
  1335. return mapif_guild_message(guild_id,account_id,mes,len, fd);
  1336. }
  1337. // Modification of the guild
  1338. int mapif_parse_GuildBasicInfoChange(int fd,int guild_id,int type,const char *data,int len)
  1339. {
  1340. struct guild * g;
  1341. short dw=*((short *)data);
  1342. g = inter_guild_fromsql(guild_id);
  1343. if(g==NULL)
  1344. return 0;
  1345. switch(type)
  1346. {
  1347. case GBI_GUILDLV:
  1348. if(dw>0 && g->guild_lv+dw<=50)
  1349. {
  1350. g->guild_lv+=dw;
  1351. g->skill_point+=dw;
  1352. }
  1353. else if(dw<0 && g->guild_lv+dw>=1)
  1354. g->guild_lv+=dw;
  1355. mapif_guild_info(-1,g);
  1356. g->save_flag |= GS_LEVEL;
  1357. return 0;
  1358. default:
  1359. ShowError("int_guild: GuildBasicInfoChange: Unknown type %d\n",type);
  1360. break;
  1361. }
  1362. mapif_guild_basicinfochanged(guild_id,type,data,len);
  1363. return 0;
  1364. }
  1365. // Modification of the guild
  1366. int mapif_parse_GuildMemberInfoChange(int fd,int guild_id,int account_id,int char_id,int type,const char *data,int len)
  1367. {
  1368. // Could make some improvement in speed, because only change guild_member
  1369. int i;
  1370. struct guild * g;
  1371. g = inter_guild_fromsql(guild_id);
  1372. if(g==NULL)
  1373. return 0;
  1374. // Search the member
  1375. for(i=0;i<g->max_member;i++)
  1376. if( g->member[i].account_id==account_id &&
  1377. g->member[i].char_id==char_id )
  1378. break;
  1379. // Not Found
  1380. if(i==g->max_member){
  1381. ShowWarning("int_guild: GuildMemberChange: Not found %d,%d in guild (%d - %s)\n",
  1382. account_id,char_id,guild_id,g->name);
  1383. return 0;
  1384. }
  1385. switch(type)
  1386. {
  1387. case GMI_POSITION:
  1388. {
  1389. g->member[i].position=*((short *)data);
  1390. g->member[i].modified = GS_MEMBER_MODIFIED;
  1391. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1392. g->save_flag |= GS_MEMBER;
  1393. break;
  1394. }
  1395. case GMI_EXP:
  1396. { // EXP
  1397. uint64 exp, old_exp=g->member[i].exp;
  1398. g->member[i].exp=*((uint64 *)data);
  1399. g->member[i].modified = GS_MEMBER_MODIFIED;
  1400. if (g->member[i].exp > old_exp)
  1401. {
  1402. exp = g->member[i].exp - old_exp;
  1403. // Compute gained exp
  1404. if (guild_exp_rate != 100)
  1405. exp = exp*guild_exp_rate/100;
  1406. // Update guild exp
  1407. if (exp > UINT64_MAX - g->exp)
  1408. g->exp = UINT64_MAX;
  1409. else
  1410. g->exp+=exp;
  1411. guild_calcinfo(g);
  1412. mapif_guild_basicinfochanged(guild_id,GBI_EXP,&g->exp,sizeof(g->exp));
  1413. g->save_flag |= GS_LEVEL;
  1414. }
  1415. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1416. g->save_flag |= GS_MEMBER;
  1417. break;
  1418. }
  1419. case GMI_HAIR:
  1420. {
  1421. g->member[i].hair=*((short *)data);
  1422. g->member[i].modified = GS_MEMBER_MODIFIED;
  1423. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1424. g->save_flag |= GS_MEMBER; //Save new data.
  1425. break;
  1426. }
  1427. case GMI_HAIR_COLOR:
  1428. {
  1429. g->member[i].hair_color=*((short *)data);
  1430. g->member[i].modified = GS_MEMBER_MODIFIED;
  1431. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1432. g->save_flag |= GS_MEMBER; //Save new data.
  1433. break;
  1434. }
  1435. case GMI_GENDER:
  1436. {
  1437. g->member[i].gender=*((short *)data);
  1438. g->member[i].modified = GS_MEMBER_MODIFIED;
  1439. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1440. g->save_flag |= GS_MEMBER; //Save new data.
  1441. break;
  1442. }
  1443. case GMI_CLASS:
  1444. {
  1445. g->member[i].class_=*((short *)data);
  1446. g->member[i].modified = GS_MEMBER_MODIFIED;
  1447. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1448. g->save_flag |= GS_MEMBER; //Save new data.
  1449. break;
  1450. }
  1451. case GMI_LEVEL:
  1452. {
  1453. g->member[i].lv=*((short *)data);
  1454. g->member[i].modified = GS_MEMBER_MODIFIED;
  1455. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1456. g->save_flag |= GS_MEMBER; //Save new data.
  1457. break;
  1458. }
  1459. default:
  1460. ShowError("int_guild: GuildMemberInfoChange: Unknown type %d\n",type);
  1461. break;
  1462. }
  1463. return 0;
  1464. }
  1465. int inter_guild_sex_changed(int guild_id,int account_id,int char_id, short gender)
  1466. {
  1467. return mapif_parse_GuildMemberInfoChange(0, guild_id, account_id, char_id, GMI_GENDER, (const char*)&gender, sizeof(gender));
  1468. }
  1469. int inter_guild_charname_changed(int guild_id,int account_id, int char_id, char *name)
  1470. {
  1471. struct guild *g;
  1472. int i, flag = 0;
  1473. g = inter_guild_fromsql(guild_id);
  1474. if( g == NULL )
  1475. {
  1476. ShowError("inter_guild_charrenamed: Can't find guild %d.\n", guild_id);
  1477. return 0;
  1478. }
  1479. ARR_FIND(0, g->max_member, i, g->member[i].char_id == char_id);
  1480. if( i == g->max_member )
  1481. {
  1482. ShowError("inter_guild_charrenamed: Can't find character %d in the guild\n", char_id);
  1483. return 0;
  1484. }
  1485. if( !strcmp(g->member[i].name, g->master) )
  1486. {
  1487. safestrncpy(g->master, name, NAME_LENGTH);
  1488. flag |= GS_BASIC;
  1489. }
  1490. safestrncpy(g->member[i].name, name, NAME_LENGTH);
  1491. g->member[i].modified = GS_MEMBER_MODIFIED;
  1492. flag |= GS_MEMBER;
  1493. if( !inter_guild_tosql(g, flag) )
  1494. return 0;
  1495. mapif_guild_info(-1,g);
  1496. return 0;
  1497. }
  1498. // Change a position desc
  1499. int mapif_parse_GuildPosition(int fd,int guild_id,int idx,struct guild_position *p)
  1500. {
  1501. // Could make some improvement in speed, because only change guild_position
  1502. struct guild * g;
  1503. g = inter_guild_fromsql(guild_id);
  1504. if(g==NULL || idx<0 || idx>=MAX_GUILDPOSITION)
  1505. return 0;
  1506. memcpy(&g->position[idx],p,sizeof(struct guild_position));
  1507. mapif_guild_position(g,idx);
  1508. g->position[idx].modified = GS_POSITION_MODIFIED;
  1509. g->save_flag |= GS_POSITION; // Change guild_position
  1510. return 0;
  1511. }
  1512. // Guild Skill UP
  1513. int mapif_parse_GuildSkillUp(int fd,int guild_id,int skill_num,int account_id,int max)
  1514. {
  1515. struct guild * g;
  1516. int idx = skill_num - GD_SKILLBASE;
  1517. g = inter_guild_fromsql(guild_id);
  1518. if(g == NULL || idx < 0 || idx >= MAX_GUILDSKILL)
  1519. return 0;
  1520. if(g->skill_point>0 && g->skill[idx].id>0 && g->skill[idx].lv<max )
  1521. {
  1522. g->skill[idx].lv++;
  1523. g->skill_point--;
  1524. if (!guild_calcinfo(g))
  1525. mapif_guild_info(-1,g);
  1526. mapif_guild_skillupack(guild_id,skill_num,account_id);
  1527. g->save_flag |= (GS_LEVEL|GS_SKILL); // Change guild & guild_skill
  1528. }
  1529. return 0;
  1530. }
  1531. //Manual deletion of an alliance when partnering guild does not exists. [Skotlex]
  1532. static int mapif_parse_GuildDeleteAlliance(struct guild *g, int guild_id, int account_id1, int account_id2, int flag)
  1533. {
  1534. int i;
  1535. char name[NAME_LENGTH];
  1536. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id );
  1537. if( i == MAX_GUILDALLIANCE )
  1538. return -1;
  1539. strcpy(name, g->alliance[i].name);
  1540. g->alliance[i].guild_id=0;
  1541. mapif_guild_alliance(g->guild_id,guild_id,account_id1,account_id2,flag,g->name,name);
  1542. g->save_flag |= GS_ALLIANCE;
  1543. return 0;
  1544. }
  1545. // Alliance modification
  1546. int mapif_parse_GuildAlliance(int fd,int guild_id1,int guild_id2,int account_id1,int account_id2,int flag)
  1547. {
  1548. // Could speed up
  1549. struct guild *g[2];
  1550. int j,i;
  1551. g[0] = inter_guild_fromsql(guild_id1);
  1552. g[1] = inter_guild_fromsql(guild_id2);
  1553. if(g[0] && g[1]==NULL && (flag & GUILD_ALLIANCE_REMOVE)) //Requested to remove an alliance with a not found guild.
  1554. return mapif_parse_GuildDeleteAlliance(g[0], guild_id2, account_id1, account_id2, flag); //Try to do a manual removal of said guild.
  1555. if(g[0]==NULL || g[1]==NULL)
  1556. return 0;
  1557. if(flag&GUILD_ALLIANCE_REMOVE)
  1558. {
  1559. // Remove alliance/opposition, in case of alliance, remove on both side
  1560. for(i=0;i<2-(flag&GUILD_ALLIANCE_TYPE_MASK);i++)
  1561. {
  1562. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == g[1-i]->guild_id && g[i]->alliance[j].opposition == (flag&GUILD_ALLIANCE_TYPE_MASK) );
  1563. if( j < MAX_GUILDALLIANCE )
  1564. g[i]->alliance[j].guild_id = 0;
  1565. }
  1566. }
  1567. else
  1568. {
  1569. // Add alliance, in case of alliance, add on both side
  1570. for(i=0;i<2-(flag&GUILD_ALLIANCE_TYPE_MASK);i++)
  1571. {
  1572. // Search an empty slot
  1573. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == 0 );
  1574. if( j < MAX_GUILDALLIANCE )
  1575. {
  1576. g[i]->alliance[j].guild_id=g[1-i]->guild_id;
  1577. memcpy(g[i]->alliance[j].name,g[1-i]->name,NAME_LENGTH);
  1578. // Set alliance type
  1579. g[i]->alliance[j].opposition = flag&GUILD_ALLIANCE_TYPE_MASK;
  1580. }
  1581. }
  1582. }
  1583. // Send on all map the new alliance/opposition
  1584. mapif_guild_alliance(guild_id1,guild_id2,account_id1,account_id2,flag,g[0]->name,g[1]->name);
  1585. // Mark the two guild to be saved
  1586. g[0]->save_flag |= GS_ALLIANCE;
  1587. g[1]->save_flag |= GS_ALLIANCE;
  1588. return 0;
  1589. }
  1590. // Change guild message
  1591. int mapif_parse_GuildNotice(int fd,int guild_id,const char *mes1,const char *mes2)
  1592. {
  1593. struct guild *g;
  1594. g = inter_guild_fromsql(guild_id);
  1595. if(g==NULL)
  1596. return 0;
  1597. memcpy(g->mes1,mes1,MAX_GUILDMES1);
  1598. memcpy(g->mes2,mes2,MAX_GUILDMES2);
  1599. g->save_flag |= GS_MES; //Change mes of guild
  1600. return mapif_guild_notice(g);
  1601. }
  1602. int mapif_parse_GuildEmblem(int fd,int len,int guild_id,int dummy,const char *data)
  1603. {
  1604. struct guild * g;
  1605. g = inter_guild_fromsql(guild_id);
  1606. if(g==NULL)
  1607. return 0;
  1608. if (len > sizeof(g->emblem_data))
  1609. len = sizeof(g->emblem_data);
  1610. memcpy(g->emblem_data,data,len);
  1611. g->emblem_len=len;
  1612. g->emblem_id++;
  1613. g->save_flag |= GS_EMBLEM; //Change guild
  1614. return mapif_guild_emblem(g);
  1615. }
  1616. int mapif_parse_GuildCastleDataLoad(int fd,int castle_id,int index)
  1617. {
  1618. struct guild_castle gc;
  1619. if (!inter_guildcastle_fromsql(castle_id, &gc)) {
  1620. return mapif_guild_castle_dataload(castle_id,0,0);
  1621. }
  1622. switch(index){
  1623. case 1: return mapif_guild_castle_dataload(gc.castle_id,index,gc.guild_id); break;
  1624. case 2: return mapif_guild_castle_dataload(gc.castle_id,index,gc.economy); break;
  1625. case 3: return mapif_guild_castle_dataload(gc.castle_id,index,gc.defense); break;
  1626. case 4: return mapif_guild_castle_dataload(gc.castle_id,index,gc.triggerE); break;
  1627. case 5: return mapif_guild_castle_dataload(gc.castle_id,index,gc.triggerD); break;
  1628. case 6: return mapif_guild_castle_dataload(gc.castle_id,index,gc.nextTime); break;
  1629. case 7: return mapif_guild_castle_dataload(gc.castle_id,index,gc.payTime); break;
  1630. case 8: return mapif_guild_castle_dataload(gc.castle_id,index,gc.createTime); break;
  1631. case 9: return mapif_guild_castle_dataload(gc.castle_id,index,gc.visibleC); break;
  1632. case 10:
  1633. case 11:
  1634. case 12:
  1635. case 13:
  1636. case 14:
  1637. case 15:
  1638. case 16:
  1639. case 17:
  1640. return mapif_guild_castle_dataload(gc.castle_id,index,gc.guardian[index-10].visible); break;
  1641. default:
  1642. ShowError("mapif_parse_GuildCastleDataLoad ERROR!! (Not found index=%d)\n", index);
  1643. return 0;
  1644. }
  1645. }
  1646. int mapif_parse_GuildCastleDataSave(int fd,int castle_id,int index,int value)
  1647. {
  1648. struct guild_castle gc;
  1649. if(!inter_guildcastle_fromsql(castle_id, &gc))
  1650. return mapif_guild_castle_datasave(castle_id,index,value);
  1651. switch(index){
  1652. case 1:
  1653. if( gc.guild_id!=value ){
  1654. int gid=(value)?value:gc.guild_id;
  1655. struct guild *g = (struct guild*)idb_get(guild_db_, gid);
  1656. if(log_inter)
  1657. inter_log("guild %s (id=%d) %s castle id=%d\n",
  1658. (g)?g->name:"??" ,gid, (value)?"occupy":"abandon", castle_id);
  1659. }
  1660. gc.guild_id = value;
  1661. if(gc.guild_id == 0) {
  1662. //Delete guardians.
  1663. memset(&gc.guardian, 0, sizeof(gc.guardian));
  1664. }
  1665. break;
  1666. case 2: gc.economy = value; break;
  1667. case 3: gc.defense = value; break;
  1668. case 4: gc.triggerE = value; break;
  1669. case 5: gc.triggerD = value; break;
  1670. case 6: gc.nextTime = value; break;
  1671. case 7: gc.payTime = value; break;
  1672. case 8: gc.createTime = value; break;
  1673. case 9: gc.visibleC = value; break;
  1674. case 10:
  1675. case 11:
  1676. case 12:
  1677. case 13:
  1678. case 14:
  1679. case 15:
  1680. case 16:
  1681. case 17:
  1682. gc.guardian[index-10].visible = value; break;
  1683. default:
  1684. ShowError("mapif_parse_GuildCastleDataSave ERROR!! (Not found index=%d)\n", index);
  1685. return 0;
  1686. }
  1687. inter_guildcastle_tosql(&gc);
  1688. mapif_guild_castle_datasave(gc.castle_id,index,value);
  1689. return 0;
  1690. }
  1691. int mapif_parse_GuildMasterChange(int fd, int guild_id, const char* name, int len)
  1692. {
  1693. struct guild * g;
  1694. struct guild_member gm;
  1695. int pos;
  1696. g = inter_guild_fromsql(guild_id);
  1697. if(g==NULL || len > NAME_LENGTH)
  1698. return 0;
  1699. // Find member (name)
  1700. for (pos = 0; pos < g->max_member && strncmp(g->member[pos].name, name, len); pos++);
  1701. if (pos == g->max_member)
  1702. return 0; //Character not found??
  1703. // Switch current and old GM
  1704. memcpy(&gm, &g->member[pos], sizeof (struct guild_member));
  1705. memcpy(&g->member[pos], &g->member[0], sizeof(struct guild_member));
  1706. memcpy(&g->member[0], &gm, sizeof(struct guild_member));
  1707. // Switch positions
  1708. g->member[pos].position = g->member[0].position;
  1709. g->member[pos].modified = GS_MEMBER_MODIFIED;
  1710. g->member[0].position = 0; //Position 0: guild Master.
  1711. g->member[0].modified = GS_MEMBER_MODIFIED;
  1712. strncpy(g->master, name, len);
  1713. if (len < NAME_LENGTH)
  1714. g->master[len] = '\0';
  1715. ShowInfo("int_guild: Guildmaster Changed to %s (Guild %d - %s)\n",g->master, guild_id, g->name);
  1716. g->save_flag |= (GS_BASIC|GS_MEMBER); //Save main data and member data.
  1717. return mapif_guild_master_changed(g, g->member[0].account_id, g->member[0].char_id);
  1718. }
  1719. // map server からの通信
  1720. // ・1パケットのみ解析すること
  1721. // ・パケット長データはinter.cにセットしておくこと
  1722. // ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない
  1723. // ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない
  1724. int inter_guild_parse_frommap(int fd)
  1725. {
  1726. RFIFOHEAD(fd);
  1727. switch(RFIFOW(fd,0)) {
  1728. case 0x3030: mapif_parse_CreateGuild(fd,RFIFOL(fd,4),(char*)RFIFOP(fd,8),(struct guild_member *)RFIFOP(fd,32)); break;
  1729. case 0x3031: mapif_parse_GuildInfo(fd,RFIFOL(fd,2)); break;
  1730. case 0x3032: mapif_parse_GuildAddMember(fd,RFIFOL(fd,4),(struct guild_member *)RFIFOP(fd,8)); break;
  1731. case 0x3033: mapif_parse_GuildMasterChange(fd,RFIFOL(fd,4),(const char*)RFIFOP(fd,8),RFIFOW(fd,2)-8); break;
  1732. case 0x3034: mapif_parse_GuildLeave(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),(const char*)RFIFOP(fd,15)); break;
  1733. case 0x3035: mapif_parse_GuildChangeMemberInfoShort(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),RFIFOW(fd,15),RFIFOW(fd,17)); break;
  1734. case 0x3036: mapif_parse_BreakGuild(fd,RFIFOL(fd,2)); break;
  1735. case 0x3037: mapif_parse_GuildMessage(fd,RFIFOL(fd,4),RFIFOL(fd,8),(char*)RFIFOP(fd,12),RFIFOW(fd,2)-12); break;
  1736. case 0x3039: mapif_parse_GuildBasicInfoChange(fd,RFIFOL(fd,4),RFIFOW(fd,8),(const char*)RFIFOP(fd,10),RFIFOW(fd,2)-10); break;
  1737. case 0x303A: mapif_parse_GuildMemberInfoChange(fd,RFIFOL(fd,4),RFIFOL(fd,8),RFIFOL(fd,12),RFIFOW(fd,16),(const char*)RFIFOP(fd,18),RFIFOW(fd,2)-18); break;
  1738. case 0x303B: mapif_parse_GuildPosition(fd,RFIFOL(fd,4),RFIFOL(fd,8),(struct guild_position *)RFIFOP(fd,12)); break;
  1739. case 0x303C: mapif_parse_GuildSkillUp(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14)); break;
  1740. case 0x303D: mapif_parse_GuildAlliance(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14),RFIFOB(fd,18)); break;
  1741. case 0x303E: mapif_parse_GuildNotice(fd,RFIFOL(fd,2),(const char*)RFIFOP(fd,6),(const char*)RFIFOP(fd,66)); break;
  1742. case 0x303F: mapif_parse_GuildEmblem(fd,RFIFOW(fd,2)-12,RFIFOL(fd,4),RFIFOL(fd,8),(const char*)RFIFOP(fd,12)); break;
  1743. case 0x3040: mapif_parse_GuildCastleDataLoad(fd,RFIFOW(fd,2),RFIFOB(fd,4)); break;
  1744. case 0x3041: mapif_parse_GuildCastleDataSave(fd,RFIFOW(fd,2),RFIFOB(fd,4),RFIFOL(fd,5)); break;
  1745. default:
  1746. return 0;
  1747. }
  1748. return 1;
  1749. }
  1750. // processes a mapserver connection event
  1751. int inter_guild_mapif_init(int fd)
  1752. {
  1753. return mapif_guild_castle_alldataload(fd);
  1754. }
  1755. // サーバーから脱退要求(キャラ削除用)
  1756. int inter_guild_leave(int guild_id, int account_id, int char_id)
  1757. {
  1758. return mapif_parse_GuildLeave(-1, guild_id, account_id, char_id, 0, "** Character Deleted **");
  1759. }
  1760. int inter_guild_broken(int guild_id)
  1761. {
  1762. return mapif_guild_broken(guild_id, 0);
  1763. }
  1764. #endif //TXT_SQL_CONVERT