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 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`=%u, `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','%llu','%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 = (unsigned int)strtoul(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 = (uint64)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. sprintf(line, "%s/exp_guild.txt", db_path);
  579. fp=fopen(line,"r");
  580. if(fp==NULL){
  581. ShowError("can't read %s\n", line);
  582. return 1;
  583. }
  584. i=0;
  585. while(fgets(line, sizeof(line), fp) && i < 100)
  586. {
  587. if(line[0]=='/' && line[1]=='/')
  588. continue;
  589. guild_exp[i]=(unsigned int)atof(line);
  590. i++;
  591. }
  592. fclose(fp);
  593. return 0;
  594. }
  595. int inter_guild_CharOnline(int char_id, int guild_id)
  596. {
  597. struct guild *g;
  598. int i;
  599. if (guild_id == -1) {
  600. //Get guild_id from the database
  601. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT guild_id FROM `%s` WHERE char_id='%d'", char_db, char_id) )
  602. {
  603. Sql_ShowDebug(sql_handle);
  604. return 0;
  605. }
  606. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  607. {
  608. char* data;
  609. Sql_GetData(sql_handle, 0, &data, NULL);
  610. guild_id = atoi(data);
  611. }
  612. else
  613. {
  614. guild_id = 0;
  615. }
  616. Sql_FreeResult(sql_handle);
  617. }
  618. if (guild_id == 0)
  619. return 0; //No guild...
  620. g = inter_guild_fromsql(guild_id);
  621. if(!g) {
  622. ShowError("Character %d's guild %d not found!\n", char_id, guild_id);
  623. return 0;
  624. }
  625. //Member has logged in before saving, tell saver not to delete
  626. if(g->save_flag & GS_REMOVE)
  627. g->save_flag &= ~GS_REMOVE;
  628. //Set member online
  629. ARR_FIND( 0, g->max_member, i, g->member[i].char_id == char_id );
  630. if( i < g->max_member )
  631. {
  632. g->member[i].online = 1;
  633. g->member[i].modified = GS_MEMBER_MODIFIED;
  634. }
  635. return 1;
  636. }
  637. int inter_guild_CharOffline(int char_id, int guild_id)
  638. {
  639. struct guild *g=NULL;
  640. int online_count, i;
  641. if (guild_id == -1)
  642. {
  643. //Get guild_id from the database
  644. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT guild_id FROM `%s` WHERE char_id='%d'", char_db, char_id) )
  645. {
  646. Sql_ShowDebug(sql_handle);
  647. return 0;
  648. }
  649. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  650. {
  651. char* data;
  652. Sql_GetData(sql_handle, 0, &data, NULL);
  653. guild_id = atoi(data);
  654. }
  655. else
  656. {
  657. guild_id = 0;
  658. }
  659. Sql_FreeResult(sql_handle);
  660. }
  661. if (guild_id == 0)
  662. return 0; //No guild...
  663. //Character has a guild, set character offline and check if they were the only member online
  664. g = inter_guild_fromsql(guild_id);
  665. if (g == NULL) //Guild not found?
  666. return 0;
  667. //Set member offline
  668. ARR_FIND( 0, g->max_member, i, g->member[i].char_id == char_id );
  669. if( i < g->max_member )
  670. {
  671. g->member[i].online = 0;
  672. g->member[i].modified = GS_MEMBER_MODIFIED;
  673. }
  674. online_count = 0;
  675. for( i = 0; i < g->max_member; i++ )
  676. if( g->member[i].online )
  677. online_count++;
  678. // Remove guild from memory if no players online
  679. if( online_count == 0 )
  680. g->save_flag |= GS_REMOVE;
  681. return 1;
  682. }
  683. // Initialize guild sql
  684. int inter_guild_sql_init(void)
  685. {
  686. //Initialize the guild cache
  687. guild_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  688. //Read exp file
  689. inter_guild_ReadEXP();
  690. add_timer_func_list(guild_save_timer, "guild_save_timer");
  691. add_timer(gettick() + 10000, guild_save_timer, 0, 0);
  692. return 0;
  693. }
  694. static int guild_db_final(DBKey key, void *data, va_list ap)
  695. {
  696. struct guild *g = (struct guild*)data;
  697. if (g->save_flag&GS_MASK) {
  698. inter_guild_tosql(g, g->save_flag&GS_MASK);
  699. return 1;
  700. }
  701. return 0;
  702. }
  703. void inter_guild_sql_final(void)
  704. {
  705. guild_db_->destroy(guild_db_, guild_db_final);
  706. return;
  707. }
  708. // Get guild_id by its name. Returns 0 if not found, -1 on error.
  709. int search_guildname(char *str)
  710. {
  711. int guild_id;
  712. char esc_name[NAME_LENGTH*2+1];
  713. Sql_EscapeStringLen(sql_handle, esc_name, str, safestrnlen(str, NAME_LENGTH));
  714. //Lookup guilds with the same name
  715. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT guild_id FROM `%s` WHERE name='%s'", guild_db, esc_name) )
  716. {
  717. Sql_ShowDebug(sql_handle);
  718. return -1;
  719. }
  720. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  721. {
  722. char* data;
  723. Sql_GetData(sql_handle, 0, &data, NULL);
  724. guild_id = atoi(data);
  725. }
  726. else
  727. {
  728. guild_id = 0;
  729. }
  730. Sql_FreeResult(sql_handle);
  731. return guild_id;
  732. }
  733. // Check if guild is empty
  734. static bool guild_check_empty(struct guild *g)
  735. {
  736. int i;
  737. ARR_FIND( 0, g->max_member, i, g->member[i].account_id > 0 );
  738. if( i < g->max_member)
  739. return false; // not empty
  740. //Let the calling function handle the guild removal in case they need
  741. //to do something else with it before freeing the data. [Skotlex]
  742. return true;
  743. }
  744. unsigned int guild_nextexp(int level)
  745. {
  746. if (level == 0)
  747. return 1;
  748. if (level < 100 && level > 0) // Change by hack
  749. return guild_exp[level-1];
  750. return 0;
  751. }
  752. int guild_checkskill(struct guild *g,int id)
  753. {
  754. int idx = id - GD_SKILLBASE;
  755. if(idx < 0 || idx >= MAX_GUILDSKILL)
  756. return 0;
  757. return g->skill[idx].lv;
  758. }
  759. int guild_calcinfo(struct guild *g)
  760. {
  761. int i,c;
  762. unsigned int nextexp;
  763. struct guild before = *g; // Save guild current values
  764. if(g->guild_lv<=0)
  765. g->guild_lv = 1;
  766. nextexp = guild_nextexp(g->guild_lv);
  767. // Consume guild exp and increase guild level
  768. while(g->exp >= nextexp && nextexp > 0){ //fixed guild exp overflow [Kevin]
  769. g->exp-=nextexp;
  770. g->guild_lv++;
  771. g->skill_point++;
  772. nextexp = guild_nextexp(g->guild_lv);
  773. }
  774. // Save next exp step
  775. g->next_exp = nextexp;
  776. // Set the max number of members, Guild Extention skill - currently adds 6 to max per skill lv.
  777. g->max_member = 16 + guild_checkskill(g, GD_EXTENSION) * 6;
  778. if(g->max_member > MAX_GUILD)
  779. {
  780. 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);
  781. g->max_member = MAX_GUILD;
  782. }
  783. // Compute the guild average level level
  784. g->average_lv=0;
  785. g->connect_member=0;
  786. for(i=c=0;i<g->max_member;i++)
  787. {
  788. if(g->member[i].account_id>0)
  789. {
  790. if (g->member[i].lv >= 0)
  791. {
  792. g->average_lv+=g->member[i].lv;
  793. c++;
  794. }
  795. else
  796. {
  797. 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);
  798. }
  799. if(g->member[i].online)
  800. g->connect_member++;
  801. }
  802. }
  803. if(c)
  804. g->average_lv /= c;
  805. // Check if guild stats has change
  806. if(g->max_member != before.max_member || g->guild_lv != before.guild_lv || g->skill_point != before.skill_point )
  807. {
  808. g->save_flag |= GS_LEVEL;
  809. mapif_guild_info(-1,g);
  810. return 1;
  811. }
  812. return 0;
  813. }
  814. //-------------------------------------------------------------------
  815. // Packet sent to map server
  816. int mapif_guild_created(int fd,int account_id,struct guild *g)
  817. {
  818. WFIFOHEAD(fd, 10);
  819. WFIFOW(fd,0)=0x3830;
  820. WFIFOL(fd,2)=account_id;
  821. if(g != NULL)
  822. {
  823. WFIFOL(fd,6)=g->guild_id;
  824. ShowInfo("int_guild: Guild created (%d - %s)\n",g->guild_id,g->name);
  825. } else
  826. WFIFOL(fd,6)=0;
  827. WFIFOSET(fd,10);
  828. return 0;
  829. }
  830. // Guild not found
  831. int mapif_guild_noinfo(int fd,int guild_id)
  832. {
  833. unsigned char buf[12];
  834. WBUFW(buf,0)=0x3831;
  835. WBUFW(buf,2)=8;
  836. WBUFL(buf,4)=guild_id;
  837. ShowWarning("int_guild: info not found %d\n",guild_id);
  838. if(fd<0)
  839. mapif_sendall(buf,8);
  840. else
  841. mapif_send(fd,buf,8);
  842. return 0;
  843. }
  844. // Send guild info
  845. int mapif_guild_info(int fd,struct guild *g)
  846. {
  847. unsigned char buf[8+sizeof(struct guild)];
  848. WBUFW(buf,0)=0x3831;
  849. WBUFW(buf,2)=4+sizeof(struct guild);
  850. memcpy(buf+4,g,sizeof(struct guild));
  851. if(fd<0)
  852. mapif_sendall(buf,WBUFW(buf,2));
  853. else
  854. mapif_send(fd,buf,WBUFW(buf,2));
  855. return 0;
  856. }
  857. // ACK member add
  858. int mapif_guild_memberadded(int fd,int guild_id,int account_id,int char_id,int flag)
  859. {
  860. WFIFOHEAD(fd, 15);
  861. WFIFOW(fd,0)=0x3832;
  862. WFIFOL(fd,2)=guild_id;
  863. WFIFOL(fd,6)=account_id;
  864. WFIFOL(fd,10)=char_id;
  865. WFIFOB(fd,14)=flag;
  866. WFIFOSET(fd,15);
  867. return 0;
  868. }
  869. // ACK member leave
  870. int mapif_guild_withdraw(int guild_id,int account_id,int char_id,int flag, const char *name, const char *mes)
  871. {
  872. unsigned char buf[55+NAME_LENGTH];
  873. WBUFW(buf, 0)=0x3834;
  874. WBUFL(buf, 2)=guild_id;
  875. WBUFL(buf, 6)=account_id;
  876. WBUFL(buf,10)=char_id;
  877. WBUFB(buf,14)=flag;
  878. memcpy(WBUFP(buf,15),mes,40);
  879. memcpy(WBUFP(buf,55),name,NAME_LENGTH);
  880. mapif_sendall(buf,55+NAME_LENGTH);
  881. ShowInfo("int_guild: guild withdraw (%d - %d: %s - %s)\n",guild_id,account_id,name,mes);
  882. return 0;
  883. }
  884. // Send short member's info
  885. int mapif_guild_memberinfoshort(struct guild *g,int idx)
  886. {
  887. unsigned char buf[19];
  888. WBUFW(buf, 0)=0x3835;
  889. WBUFL(buf, 2)=g->guild_id;
  890. WBUFL(buf, 6)=g->member[idx].account_id;
  891. WBUFL(buf,10)=g->member[idx].char_id;
  892. WBUFB(buf,14)=(unsigned char)g->member[idx].online;
  893. WBUFW(buf,15)=g->member[idx].lv;
  894. WBUFW(buf,17)=g->member[idx].class_;
  895. mapif_sendall(buf,19);
  896. return 0;
  897. }
  898. // Send guild broken
  899. int mapif_guild_broken(int guild_id,int flag)
  900. {
  901. unsigned char buf[7];
  902. WBUFW(buf,0)=0x3836;
  903. WBUFL(buf,2)=guild_id;
  904. WBUFB(buf,6)=flag;
  905. mapif_sendall(buf,7);
  906. ShowInfo("int_guild: Guild broken (%d)\n",guild_id);
  907. return 0;
  908. }
  909. // Send guild message
  910. int mapif_guild_message(int guild_id,int account_id,char *mes,int len, int sfd)
  911. {
  912. unsigned char buf[512];
  913. if (len > 500)
  914. len = 500;
  915. WBUFW(buf,0)=0x3837;
  916. WBUFW(buf,2)=len+12;
  917. WBUFL(buf,4)=guild_id;
  918. WBUFL(buf,8)=account_id;
  919. memcpy(WBUFP(buf,12),mes,len);
  920. mapif_sendallwos(sfd, buf,len+12);
  921. return 0;
  922. }
  923. // Send basic info
  924. int mapif_guild_basicinfochanged(int guild_id,int type,const void *data,int len)
  925. {
  926. unsigned char buf[2048];
  927. if (len > 2038)
  928. len = 2038;
  929. WBUFW(buf, 0)=0x3839;
  930. WBUFW(buf, 2)=len+10;
  931. WBUFL(buf, 4)=guild_id;
  932. WBUFW(buf, 8)=type;
  933. memcpy(WBUFP(buf,10),data,len);
  934. mapif_sendall(buf,len+10);
  935. return 0;
  936. }
  937. // Send member info
  938. int mapif_guild_memberinfochanged(int guild_id,int account_id,int char_id, int type,const void *data,int len)
  939. {
  940. unsigned char buf[2048];
  941. if (len > 2030)
  942. len = 2030;
  943. WBUFW(buf, 0)=0x383a;
  944. WBUFW(buf, 2)=len+18;
  945. WBUFL(buf, 4)=guild_id;
  946. WBUFL(buf, 8)=account_id;
  947. WBUFL(buf,12)=char_id;
  948. WBUFW(buf,16)=type;
  949. memcpy(WBUFP(buf,18),data,len);
  950. mapif_sendall(buf,len+18);
  951. return 0;
  952. }
  953. // ACK guild skill up
  954. int mapif_guild_skillupack(int guild_id,int skill_num,int account_id)
  955. {
  956. unsigned char buf[14];
  957. WBUFW(buf, 0)=0x383c;
  958. WBUFL(buf, 2)=guild_id;
  959. WBUFL(buf, 6)=skill_num;
  960. WBUFL(buf,10)=account_id;
  961. mapif_sendall(buf,14);
  962. return 0;
  963. }
  964. // ACK guild alliance
  965. int mapif_guild_alliance(int guild_id1,int guild_id2,int account_id1,int account_id2,int flag,const char *name1,const char *name2)
  966. {
  967. unsigned char buf[19+2*NAME_LENGTH];
  968. WBUFW(buf, 0)=0x383d;
  969. WBUFL(buf, 2)=guild_id1;
  970. WBUFL(buf, 6)=guild_id2;
  971. WBUFL(buf,10)=account_id1;
  972. WBUFL(buf,14)=account_id2;
  973. WBUFB(buf,18)=flag;
  974. memcpy(WBUFP(buf,19),name1,NAME_LENGTH);
  975. memcpy(WBUFP(buf,19+NAME_LENGTH),name2,NAME_LENGTH);
  976. mapif_sendall(buf,19+2*NAME_LENGTH);
  977. return 0;
  978. }
  979. // Send a guild position desc
  980. int mapif_guild_position(struct guild *g,int idx)
  981. {
  982. unsigned char buf[12 + sizeof(struct guild_position)];
  983. WBUFW(buf,0)=0x383b;
  984. WBUFW(buf,2)=sizeof(struct guild_position)+12;
  985. WBUFL(buf,4)=g->guild_id;
  986. WBUFL(buf,8)=idx;
  987. memcpy(WBUFP(buf,12),&g->position[idx],sizeof(struct guild_position));
  988. mapif_sendall(buf,WBUFW(buf,2));
  989. return 0;
  990. }
  991. // Send the guild notice
  992. int mapif_guild_notice(struct guild *g)
  993. {
  994. unsigned char buf[256];
  995. WBUFW(buf,0)=0x383e;
  996. WBUFL(buf,2)=g->guild_id;
  997. memcpy(WBUFP(buf,6),g->mes1,60);
  998. memcpy(WBUFP(buf,66),g->mes2,120);
  999. mapif_sendall(buf,186);
  1000. return 0;
  1001. }
  1002. // Send emblem data
  1003. int mapif_guild_emblem(struct guild *g)
  1004. {
  1005. unsigned char buf[12 + sizeof(g->emblem_data)];
  1006. WBUFW(buf,0)=0x383f;
  1007. WBUFW(buf,2)=g->emblem_len+12;
  1008. WBUFL(buf,4)=g->guild_id;
  1009. WBUFL(buf,8)=g->emblem_id;
  1010. memcpy(WBUFP(buf,12),g->emblem_data,g->emblem_len);
  1011. mapif_sendall(buf,WBUFW(buf,2));
  1012. return 0;
  1013. }
  1014. int mapif_guild_master_changed(struct guild *g, int aid, int cid)
  1015. {
  1016. unsigned char buf[14];
  1017. WBUFW(buf,0)=0x3843;
  1018. WBUFL(buf,2)=g->guild_id;
  1019. WBUFL(buf,6)=aid;
  1020. WBUFL(buf,10)=cid;
  1021. mapif_sendall(buf,14);
  1022. return 0;
  1023. }
  1024. int mapif_guild_castle_dataload(int castle_id,int index,int value)
  1025. {
  1026. unsigned char buf[9];
  1027. WBUFW(buf, 0)=0x3840;
  1028. WBUFW(buf, 2)=castle_id;
  1029. WBUFB(buf, 4)=index;
  1030. WBUFL(buf, 5)=value;
  1031. mapif_sendall(buf,9);
  1032. return 0;
  1033. }
  1034. int mapif_guild_castle_datasave(int castle_id,int index,int value)
  1035. {
  1036. unsigned char buf[9];
  1037. WBUFW(buf, 0)=0x3841;
  1038. WBUFW(buf, 2)=castle_id;
  1039. WBUFB(buf, 4)=index;
  1040. WBUFL(buf, 5)=value;
  1041. mapif_sendall(buf,9);
  1042. return 0;
  1043. }
  1044. int mapif_guild_castle_alldataload(int fd)
  1045. {
  1046. struct guild_castle s_gc;
  1047. struct guild_castle* gc = &s_gc;
  1048. int i;
  1049. int len;
  1050. char* data;
  1051. WFIFOHEAD(fd, 4 + MAX_GUILDCASTLE*sizeof(struct guild_castle));
  1052. WFIFOW(fd, 0) = 0x3842;
  1053. if( SQL_ERROR == Sql_Query(sql_handle,
  1054. "SELECT `castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`,"
  1055. " `visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`"
  1056. " FROM `%s` ORDER BY `castle_id`", guild_castle_db) )
  1057. Sql_ShowDebug(sql_handle);
  1058. for( i = 0, len = 4; i < MAX_GUILDCASTLE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1059. {
  1060. memset(gc, 0, sizeof(struct guild_castle));
  1061. Sql_GetData(sql_handle, 0, &data, NULL); gc->castle_id = atoi(data);
  1062. Sql_GetData(sql_handle, 1, &data, NULL); gc->guild_id = atoi(data);
  1063. Sql_GetData(sql_handle, 2, &data, NULL); gc->economy = atoi(data);
  1064. Sql_GetData(sql_handle, 3, &data, NULL); gc->defense = atoi(data);
  1065. Sql_GetData(sql_handle, 4, &data, NULL); gc->triggerE = atoi(data);
  1066. Sql_GetData(sql_handle, 5, &data, NULL); gc->triggerD = atoi(data);
  1067. Sql_GetData(sql_handle, 6, &data, NULL); gc->nextTime = atoi(data);
  1068. Sql_GetData(sql_handle, 7, &data, NULL); gc->payTime = atoi(data);
  1069. Sql_GetData(sql_handle, 8, &data, NULL); gc->createTime = atoi(data);
  1070. Sql_GetData(sql_handle, 9, &data, NULL); gc->visibleC = atoi(data);
  1071. Sql_GetData(sql_handle, 10, &data, NULL); gc->guardian[0].visible = atoi(data);
  1072. Sql_GetData(sql_handle, 11, &data, NULL); gc->guardian[1].visible = atoi(data);
  1073. Sql_GetData(sql_handle, 12, &data, NULL); gc->guardian[2].visible = atoi(data);
  1074. Sql_GetData(sql_handle, 13, &data, NULL); gc->guardian[3].visible = atoi(data);
  1075. Sql_GetData(sql_handle, 14, &data, NULL); gc->guardian[4].visible = atoi(data);
  1076. Sql_GetData(sql_handle, 15, &data, NULL); gc->guardian[5].visible = atoi(data);
  1077. Sql_GetData(sql_handle, 16, &data, NULL); gc->guardian[6].visible = atoi(data);
  1078. Sql_GetData(sql_handle, 17, &data, NULL); gc->guardian[7].visible = atoi(data);
  1079. memcpy(WFIFOP(fd, len), gc, sizeof(struct guild_castle));
  1080. len += sizeof(struct guild_castle);
  1081. }
  1082. Sql_FreeResult(sql_handle);
  1083. WFIFOW(fd, 2) = len;
  1084. WFIFOSET(fd, len);
  1085. return 0;
  1086. }
  1087. //-------------------------------------------------------------------
  1088. // Packet received from map server
  1089. // ギルド作成要求
  1090. int mapif_parse_CreateGuild(int fd,int account_id,char *name,struct guild_member *master)
  1091. {
  1092. struct guild *g;
  1093. int i=0;
  1094. #ifdef NOISY
  1095. ShowInfo("Creating Guild (%s)\n", name);
  1096. #endif
  1097. if(search_guildname(name) != 0){
  1098. ShowInfo("int_guild: guild with same name exists [%s]\n",name);
  1099. mapif_guild_created(fd,account_id,NULL);
  1100. return 0;
  1101. }
  1102. // Check Authorised letters/symbols in the name of the character
  1103. if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
  1104. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1105. if (strchr(char_name_letters, name[i]) == NULL) {
  1106. mapif_guild_created(fd,account_id,NULL);
  1107. return 0;
  1108. }
  1109. } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
  1110. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1111. if (strchr(char_name_letters, name[i]) != NULL) {
  1112. mapif_guild_created(fd,account_id,NULL);
  1113. return 0;
  1114. }
  1115. }
  1116. g = (struct guild *)aMalloc(sizeof(struct guild));
  1117. memset(g,0,sizeof(struct guild));
  1118. memcpy(g->name,name,NAME_LENGTH);
  1119. memcpy(g->master,master->name,NAME_LENGTH);
  1120. memcpy(&g->member[0],master,sizeof(struct guild_member));
  1121. g->member[0].modified = GS_MEMBER_MODIFIED;
  1122. // Set default positions
  1123. g->position[0].mode=0x11;
  1124. strcpy(g->position[0].name,"GuildMaster");
  1125. strcpy(g->position[MAX_GUILDPOSITION-1].name,"Newbie");
  1126. g->position[0].modified = g->position[MAX_GUILDPOSITION-1].modified = GS_POSITION_MODIFIED;
  1127. for(i=1;i<MAX_GUILDPOSITION-1;i++) {
  1128. sprintf(g->position[i].name,"Position %d",i+1);
  1129. g->position[i].modified = GS_POSITION_MODIFIED;
  1130. }
  1131. // Initialize guild property
  1132. g->max_member=16;
  1133. g->average_lv=master->lv;
  1134. g->connect_member=1;
  1135. for(i=0;i<MAX_GUILDSKILL;i++)
  1136. g->skill[i].id=i + GD_SKILLBASE;
  1137. g->guild_id= -1; //Request to create guild.
  1138. // Create the guild
  1139. if (!inter_guild_tosql(g,GS_BASIC|GS_POSITION|GS_SKILL)) {
  1140. //Failed to Create guild....
  1141. ShowError("Failed to create Guild %s (Guild Master: %s)\n", g->name, g->master);
  1142. mapif_guild_created(fd,account_id,NULL);
  1143. aFree(g);
  1144. return 0;
  1145. }
  1146. ShowInfo("Created Guild %d - %s (Guild Master: %s)\n", g->guild_id, g->name, g->master);
  1147. //Add to cache
  1148. idb_put(guild_db_, g->guild_id, g);
  1149. // Report to client
  1150. mapif_guild_created(fd,account_id,g);
  1151. mapif_guild_info(fd,g);
  1152. if(log_inter)
  1153. inter_log("guild %s (id=%d) created by master %s (id=%d)\n",
  1154. name, g->guild_id, master->name, master->account_id );
  1155. return 0;
  1156. }
  1157. // Return guild info to client
  1158. int mapif_parse_GuildInfo(int fd,int guild_id)
  1159. {
  1160. struct guild * g = inter_guild_fromsql(guild_id); //We use this because on start-up the info of castle-owned guilds is requied. [Skotlex]
  1161. if(g)
  1162. {
  1163. if (!guild_calcinfo(g))
  1164. mapif_guild_info(fd,g);
  1165. }
  1166. else
  1167. mapif_guild_noinfo(fd,guild_id); // Failed to load info
  1168. return 0;
  1169. }
  1170. // Add member to guild
  1171. int mapif_parse_GuildAddMember(int fd,int guild_id,struct guild_member *m)
  1172. {
  1173. struct guild * g;
  1174. int i;
  1175. g = inter_guild_fromsql(guild_id);
  1176. if(g==NULL){
  1177. // Failed to add
  1178. mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,1);
  1179. return 0;
  1180. }
  1181. // Find an empty slot
  1182. for(i=0;i<g->max_member;i++)
  1183. {
  1184. if(g->member[i].account_id==0)
  1185. {
  1186. memcpy(&g->member[i],m,sizeof(struct guild_member));
  1187. g->member[i].modified = (GS_MEMBER_NEW | GS_MEMBER_MODIFIED);
  1188. mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,0);
  1189. if (!guild_calcinfo(g)) //Send members if it was not invoked.
  1190. mapif_guild_info(-1,g);
  1191. g->save_flag |= GS_MEMBER;
  1192. if (g->save_flag&GS_REMOVE)
  1193. g->save_flag&=~GS_REMOVE;
  1194. return 0;
  1195. }
  1196. }
  1197. // Failed to add
  1198. mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,1);
  1199. return 0;
  1200. }
  1201. // Delete member from guild
  1202. int mapif_parse_GuildLeave(int fd, int guild_id, int account_id, int char_id, int flag, const char *mes)
  1203. {
  1204. int i, j;
  1205. struct guild* g = inter_guild_fromsql(guild_id);
  1206. if( g == NULL )
  1207. {
  1208. // Unknown guild, just update the player
  1209. 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) )
  1210. Sql_ShowDebug(sql_handle);
  1211. // mapif_guild_withdraw(guild_id,account_id,char_id,flag,g->member[i].name,mes);
  1212. return 0;
  1213. }
  1214. // Find the member
  1215. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == account_id && g->member[i].char_id == char_id );
  1216. if( i == g->max_member )
  1217. {
  1218. //TODO
  1219. return 0;
  1220. }
  1221. if( flag )
  1222. { // Write expulsion reason
  1223. // Find an empty slot
  1224. ARR_FIND( 0, MAX_GUILDEXPULSION, j, g->expulsion[j].account_id == 0 );
  1225. if( j == MAX_GUILDEXPULSION )
  1226. {
  1227. // Expulsion list is full, flush the oldest one
  1228. for( j = 0; j < MAX_GUILDEXPULSION - 1; j++ )
  1229. g->expulsion[j] = g->expulsion[j+1];
  1230. j = MAX_GUILDEXPULSION-1;
  1231. }
  1232. // Save the expulsion entry
  1233. g->expulsion[j].account_id = account_id;
  1234. safestrncpy(g->expulsion[j].name, g->member[i].name, NAME_LENGTH);
  1235. safestrncpy(g->expulsion[j].mes, mes, 40);
  1236. }
  1237. mapif_guild_withdraw(guild_id,account_id,char_id,flag,g->member[i].name,mes);
  1238. inter_guild_removemember_tosql(g->member[i].account_id,g->member[i].char_id);
  1239. memset(&g->member[i],0,sizeof(struct guild_member));
  1240. if( guild_check_empty(g) )
  1241. mapif_parse_BreakGuild(-1,guild_id); //Break the guild.
  1242. else {
  1243. //Update member info.
  1244. if (!guild_calcinfo(g))
  1245. mapif_guild_info(fd,g);
  1246. g->save_flag |= GS_EXPULSION;
  1247. }
  1248. return 0;
  1249. }
  1250. // Change member info
  1251. int mapif_parse_GuildChangeMemberInfoShort(int fd,int guild_id,int account_id,int char_id,int online,int lv,int class_)
  1252. {
  1253. // Could speed up by manipulating only guild_member
  1254. struct guild * g;
  1255. int i,sum,c;
  1256. int prev_count, prev_alv;
  1257. g = inter_guild_fromsql(guild_id);
  1258. if(g==NULL)
  1259. return 0;
  1260. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == account_id && g->member[i].char_id == char_id );
  1261. if( i < g->max_member )
  1262. {
  1263. g->member[i].online = online;
  1264. g->member[i].lv = lv;
  1265. g->member[i].class_ = class_;
  1266. g->member[i].modified = GS_MEMBER_MODIFIED;
  1267. mapif_guild_memberinfoshort(g,i);
  1268. }
  1269. prev_count = g->connect_member;
  1270. prev_alv = g->average_lv;
  1271. g->average_lv = 0;
  1272. g->connect_member = 0;
  1273. c = 0;
  1274. sum = 0;
  1275. for( i = 0; i < g->max_member; i++ )
  1276. {
  1277. if( g->member[i].account_id > 0 )
  1278. {
  1279. sum += g->member[i].lv;
  1280. c++;
  1281. }
  1282. if( g->member[i].online )
  1283. g->connect_member++;
  1284. }
  1285. if( c ) // this check should always succeed...
  1286. {
  1287. g->average_lv = sum / c;
  1288. if( g->connect_member != prev_count || g->average_lv != prev_alv )
  1289. g->save_flag |= GS_CONNECT;
  1290. if( g->save_flag & GS_REMOVE )
  1291. g->save_flag &= ~GS_REMOVE;
  1292. }
  1293. g->save_flag |= GS_MEMBER; //Update guild member data
  1294. return 0;
  1295. }
  1296. // BreakGuild
  1297. int mapif_parse_BreakGuild(int fd,int guild_id)
  1298. {
  1299. struct guild * g;
  1300. g = inter_guild_fromsql(guild_id);
  1301. if(g==NULL)
  1302. return 0;
  1303. // Delete guild from sql
  1304. //printf("- Delete guild %d from guild\n",guild_id);
  1305. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_db, guild_id) )
  1306. Sql_ShowDebug(sql_handle);
  1307. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_member_db, guild_id) )
  1308. Sql_ShowDebug(sql_handle);
  1309. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_castle_db, guild_id) )
  1310. Sql_ShowDebug(sql_handle);
  1311. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_storage_db, guild_id) )
  1312. Sql_ShowDebug(sql_handle);
  1313. 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) )
  1314. Sql_ShowDebug(sql_handle);
  1315. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_position_db, guild_id) )
  1316. Sql_ShowDebug(sql_handle);
  1317. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_skill_db, guild_id) )
  1318. Sql_ShowDebug(sql_handle);
  1319. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_expulsion_db, guild_id) )
  1320. Sql_ShowDebug(sql_handle);
  1321. //printf("- Update guild %d of char\n",guild_id);
  1322. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `guild_id`='0' WHERE `guild_id`='%d'", char_db, guild_id) )
  1323. Sql_ShowDebug(sql_handle);
  1324. mapif_guild_broken(guild_id,0);
  1325. if(log_inter)
  1326. inter_log("guild %s (id=%d) broken\n",g->name,guild_id);
  1327. //Remove the guild from memory. [Skotlex]
  1328. idb_remove(guild_db_, guild_id);
  1329. return 0;
  1330. }
  1331. // Forward Guild message to others map servers
  1332. int mapif_parse_GuildMessage(int fd,int guild_id,int account_id,char *mes,int len)
  1333. {
  1334. return mapif_guild_message(guild_id,account_id,mes,len, fd);
  1335. }
  1336. // Modification of the guild
  1337. int mapif_parse_GuildBasicInfoChange(int fd,int guild_id,int type,const char *data,int len)
  1338. {
  1339. struct guild * g;
  1340. short dw=*((short *)data);
  1341. g = inter_guild_fromsql(guild_id);
  1342. if(g==NULL)
  1343. return 0;
  1344. switch(type)
  1345. {
  1346. case GBI_GUILDLV:
  1347. if(dw>0 && g->guild_lv+dw<=50)
  1348. {
  1349. g->guild_lv+=dw;
  1350. g->skill_point+=dw;
  1351. }
  1352. else if(dw<0 && g->guild_lv+dw>=1)
  1353. g->guild_lv+=dw;
  1354. mapif_guild_info(-1,g);
  1355. g->save_flag |= GS_LEVEL;
  1356. return 0;
  1357. default:
  1358. ShowError("int_guild: GuildBasicInfoChange: Unknown type %d\n",type);
  1359. break;
  1360. }
  1361. mapif_guild_basicinfochanged(guild_id,type,data,len);
  1362. return 0;
  1363. }
  1364. // Modification of the guild
  1365. int mapif_parse_GuildMemberInfoChange(int fd,int guild_id,int account_id,int char_id,int type,const char *data,int len)
  1366. {
  1367. // Could make some improvement in speed, because only change guild_member
  1368. int i;
  1369. struct guild * g;
  1370. g = inter_guild_fromsql(guild_id);
  1371. if(g==NULL)
  1372. return 0;
  1373. // Search the member
  1374. for(i=0;i<g->max_member;i++)
  1375. if( g->member[i].account_id==account_id &&
  1376. g->member[i].char_id==char_id )
  1377. break;
  1378. // Not Found
  1379. if(i==g->max_member){
  1380. ShowWarning("int_guild: GuildMemberChange: Not found %d,%d in guild (%d - %s)\n",
  1381. account_id,char_id,guild_id,g->name);
  1382. return 0;
  1383. }
  1384. switch(type)
  1385. {
  1386. case GMI_POSITION:
  1387. {
  1388. g->member[i].position=*((int *)data);
  1389. g->member[i].modified = GS_MEMBER_MODIFIED;
  1390. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1391. g->save_flag |= GS_MEMBER;
  1392. break;
  1393. }
  1394. case GMI_EXP:
  1395. { // EXP
  1396. unsigned int exp, old_exp=g->member[i].exp;
  1397. g->member[i].exp=*((unsigned int *)data);
  1398. g->member[i].modified = GS_MEMBER_MODIFIED;
  1399. if (g->member[i].exp > old_exp)
  1400. {
  1401. exp = g->member[i].exp - old_exp;
  1402. // Compute gained exp
  1403. if (guild_exp_rate != 100)
  1404. exp = exp*guild_exp_rate/100;
  1405. // Update guild exp
  1406. if (exp > UINT_MAX - g->exp)
  1407. g->exp = UINT_MAX;
  1408. else
  1409. g->exp+=exp;
  1410. guild_calcinfo(g);
  1411. mapif_guild_basicinfochanged(guild_id,GBI_EXP,&g->exp,4);
  1412. g->save_flag |= GS_LEVEL;
  1413. }
  1414. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1415. g->save_flag |= GS_MEMBER;
  1416. break;
  1417. }
  1418. case GMI_HAIR:
  1419. {
  1420. g->member[i].hair=*((int *)data);
  1421. g->member[i].modified = GS_MEMBER_MODIFIED;
  1422. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1423. g->save_flag |= GS_MEMBER; //Save new data.
  1424. break;
  1425. }
  1426. case GMI_HAIR_COLOR:
  1427. {
  1428. g->member[i].hair_color=*((int *)data);
  1429. g->member[i].modified = GS_MEMBER_MODIFIED;
  1430. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1431. g->save_flag |= GS_MEMBER; //Save new data.
  1432. break;
  1433. }
  1434. case GMI_GENDER:
  1435. {
  1436. g->member[i].gender=*((int *)data);
  1437. g->member[i].modified = GS_MEMBER_MODIFIED;
  1438. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1439. g->save_flag |= GS_MEMBER; //Save new data.
  1440. break;
  1441. }
  1442. case GMI_CLASS:
  1443. {
  1444. g->member[i].class_=*((int *)data);
  1445. g->member[i].modified = GS_MEMBER_MODIFIED;
  1446. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1447. g->save_flag |= GS_MEMBER; //Save new data.
  1448. break;
  1449. }
  1450. case GMI_LEVEL:
  1451. {
  1452. g->member[i].lv=*((int *)data);
  1453. g->member[i].modified = GS_MEMBER_MODIFIED;
  1454. mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len);
  1455. g->save_flag |= GS_MEMBER; //Save new data.
  1456. break;
  1457. }
  1458. default:
  1459. ShowError("int_guild: GuildMemberInfoChange: Unknown type %d\n",type);
  1460. break;
  1461. }
  1462. return 0;
  1463. }
  1464. int inter_guild_sex_changed(int guild_id,int account_id,int char_id, int gender)
  1465. {
  1466. return mapif_parse_GuildMemberInfoChange(0, guild_id, account_id, char_id, GMI_GENDER, (const char*)&gender, sizeof(gender));
  1467. }
  1468. int inter_guild_charname_changed(int guild_id,int account_id, int char_id, char *name)
  1469. {
  1470. struct guild *g;
  1471. int i, flag = 0;
  1472. g = inter_guild_fromsql(guild_id);
  1473. if( g == NULL )
  1474. {
  1475. ShowError("inter_guild_charrenamed: Can't find guild %d.\n", guild_id);
  1476. return 0;
  1477. }
  1478. ARR_FIND(0, g->max_member, i, g->member[i].char_id == char_id);
  1479. if( i == g->max_member )
  1480. {
  1481. ShowError("inter_guild_charrenamed: Can't find character %d in the guild\n", char_id);
  1482. return 0;
  1483. }
  1484. if( !strcmp(g->member[i].name, g->master) )
  1485. {
  1486. safestrncpy(g->master, name, NAME_LENGTH);
  1487. flag |= GS_BASIC;
  1488. }
  1489. safestrncpy(g->member[i].name, name, NAME_LENGTH);
  1490. g->member[i].modified = GS_MEMBER_MODIFIED;
  1491. flag |= GS_MEMBER;
  1492. if( !inter_guild_tosql(g, flag) )
  1493. return 0;
  1494. mapif_guild_info(-1,g);
  1495. return 0;
  1496. }
  1497. // Change a position desc
  1498. int mapif_parse_GuildPosition(int fd,int guild_id,int idx,struct guild_position *p)
  1499. {
  1500. // Could make some improvement in speed, because only change guild_position
  1501. struct guild * g;
  1502. g = inter_guild_fromsql(guild_id);
  1503. if(g==NULL || idx<0 || idx>=MAX_GUILDPOSITION)
  1504. return 0;
  1505. memcpy(&g->position[idx],p,sizeof(struct guild_position));
  1506. mapif_guild_position(g,idx);
  1507. g->position[idx].modified = GS_POSITION_MODIFIED;
  1508. g->save_flag |= GS_POSITION; // Change guild_position
  1509. return 0;
  1510. }
  1511. // Guild Skill UP
  1512. int mapif_parse_GuildSkillUp(int fd,int guild_id,int skill_num,int account_id,int max)
  1513. {
  1514. struct guild * g;
  1515. int idx = skill_num - GD_SKILLBASE;
  1516. g = inter_guild_fromsql(guild_id);
  1517. if(g == NULL || idx < 0 || idx >= MAX_GUILDSKILL)
  1518. return 0;
  1519. if(g->skill_point>0 && g->skill[idx].id>0 && g->skill[idx].lv<max )
  1520. {
  1521. g->skill[idx].lv++;
  1522. g->skill_point--;
  1523. if (!guild_calcinfo(g))
  1524. mapif_guild_info(-1,g);
  1525. mapif_guild_skillupack(guild_id,skill_num,account_id);
  1526. g->save_flag |= (GS_LEVEL|GS_SKILL); // Change guild & guild_skill
  1527. }
  1528. return 0;
  1529. }
  1530. //Manual deletion of an alliance when partnering guild does not exists. [Skotlex]
  1531. static int mapif_parse_GuildDeleteAlliance(struct guild *g, int guild_id, int account_id1, int account_id2, int flag)
  1532. {
  1533. int i;
  1534. char name[NAME_LENGTH];
  1535. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id );
  1536. if( i == MAX_GUILDALLIANCE )
  1537. return -1;
  1538. strcpy(name, g->alliance[i].name);
  1539. g->alliance[i].guild_id=0;
  1540. mapif_guild_alliance(g->guild_id,guild_id,account_id1,account_id2,flag,g->name,name);
  1541. g->save_flag |= GS_ALLIANCE;
  1542. return 0;
  1543. }
  1544. // Alliance modification
  1545. int mapif_parse_GuildAlliance(int fd,int guild_id1,int guild_id2,int account_id1,int account_id2,int flag)
  1546. {
  1547. // Could speed up
  1548. struct guild *g[2];
  1549. int j,i;
  1550. g[0] = inter_guild_fromsql(guild_id1);
  1551. g[1] = inter_guild_fromsql(guild_id2);
  1552. if(g[0] && g[1]==NULL && (flag & GUILD_ALLIANCE_REMOVE)) //Requested to remove an alliance with a not found guild.
  1553. return mapif_parse_GuildDeleteAlliance(g[0], guild_id2, account_id1, account_id2, flag); //Try to do a manual removal of said guild.
  1554. if(g[0]==NULL || g[1]==NULL)
  1555. return 0;
  1556. if(flag&GUILD_ALLIANCE_REMOVE)
  1557. {
  1558. // Remove alliance/opposition, in case of alliance, remove on both side
  1559. for(i=0;i<2-(flag&GUILD_ALLIANCE_TYPE_MASK);i++)
  1560. {
  1561. 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) );
  1562. if( j < MAX_GUILDALLIANCE )
  1563. g[i]->alliance[j].guild_id = 0;
  1564. }
  1565. }
  1566. else
  1567. {
  1568. // Add alliance, in case of alliance, add on both side
  1569. for(i=0;i<2-(flag&GUILD_ALLIANCE_TYPE_MASK);i++)
  1570. {
  1571. // Search an empty slot
  1572. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == 0 );
  1573. if( j < MAX_GUILDALLIANCE )
  1574. {
  1575. g[i]->alliance[j].guild_id=g[1-i]->guild_id;
  1576. memcpy(g[i]->alliance[j].name,g[1-i]->name,NAME_LENGTH);
  1577. // Set alliance type
  1578. g[i]->alliance[j].opposition = flag&GUILD_ALLIANCE_TYPE_MASK;
  1579. }
  1580. }
  1581. }
  1582. // Send on all map the new alliance/opposition
  1583. mapif_guild_alliance(guild_id1,guild_id2,account_id1,account_id2,flag,g[0]->name,g[1]->name);
  1584. // Mark the two guild to be saved
  1585. g[0]->save_flag |= GS_ALLIANCE;
  1586. g[1]->save_flag |= GS_ALLIANCE;
  1587. return 0;
  1588. }
  1589. // Change guild message
  1590. int mapif_parse_GuildNotice(int fd,int guild_id,const char *mes1,const char *mes2)
  1591. {
  1592. struct guild *g;
  1593. g = inter_guild_fromsql(guild_id);
  1594. if(g==NULL)
  1595. return 0;
  1596. memcpy(g->mes1,mes1,60);
  1597. memcpy(g->mes2,mes2,120);
  1598. g->save_flag |= GS_MES; //Change mes of guild
  1599. return mapif_guild_notice(g);
  1600. }
  1601. int mapif_parse_GuildEmblem(int fd,int len,int guild_id,int dummy,const char *data)
  1602. {
  1603. struct guild * g;
  1604. g = inter_guild_fromsql(guild_id);
  1605. if(g==NULL)
  1606. return 0;
  1607. if (len > sizeof(g->emblem_data))
  1608. len = sizeof(g->emblem_data);
  1609. memcpy(g->emblem_data,data,len);
  1610. g->emblem_len=len;
  1611. g->emblem_id++;
  1612. g->save_flag |= GS_EMBLEM; //Change guild
  1613. return mapif_guild_emblem(g);
  1614. }
  1615. int mapif_parse_GuildCastleDataLoad(int fd,int castle_id,int index)
  1616. {
  1617. struct guild_castle gc;
  1618. if (!inter_guildcastle_fromsql(castle_id, &gc)) {
  1619. return mapif_guild_castle_dataload(castle_id,0,0);
  1620. }
  1621. switch(index){
  1622. case 1: return mapif_guild_castle_dataload(gc.castle_id,index,gc.guild_id); break;
  1623. case 2: return mapif_guild_castle_dataload(gc.castle_id,index,gc.economy); break;
  1624. case 3: return mapif_guild_castle_dataload(gc.castle_id,index,gc.defense); break;
  1625. case 4: return mapif_guild_castle_dataload(gc.castle_id,index,gc.triggerE); break;
  1626. case 5: return mapif_guild_castle_dataload(gc.castle_id,index,gc.triggerD); break;
  1627. case 6: return mapif_guild_castle_dataload(gc.castle_id,index,gc.nextTime); break;
  1628. case 7: return mapif_guild_castle_dataload(gc.castle_id,index,gc.payTime); break;
  1629. case 8: return mapif_guild_castle_dataload(gc.castle_id,index,gc.createTime); break;
  1630. case 9: return mapif_guild_castle_dataload(gc.castle_id,index,gc.visibleC); break;
  1631. case 10:
  1632. case 11:
  1633. case 12:
  1634. case 13:
  1635. case 14:
  1636. case 15:
  1637. case 16:
  1638. case 17:
  1639. return mapif_guild_castle_dataload(gc.castle_id,index,gc.guardian[index-10].visible); break;
  1640. default:
  1641. ShowError("mapif_parse_GuildCastleDataLoad ERROR!! (Not found index=%d)\n", index);
  1642. return 0;
  1643. }
  1644. }
  1645. int mapif_parse_GuildCastleDataSave(int fd,int castle_id,int index,int value)
  1646. {
  1647. struct guild_castle gc;
  1648. if(!inter_guildcastle_fromsql(castle_id, &gc))
  1649. return mapif_guild_castle_datasave(castle_id,index,value);
  1650. switch(index){
  1651. case 1:
  1652. if( gc.guild_id!=value ){
  1653. int gid=(value)?value:gc.guild_id;
  1654. struct guild *g = (struct guild*)idb_get(guild_db_, gid);
  1655. if(log_inter)
  1656. inter_log("guild %s (id=%d) %s castle id=%d\n",
  1657. (g)?g->name:"??" ,gid, (value)?"occupy":"abandon", castle_id);
  1658. }
  1659. gc.guild_id = value;
  1660. if(gc.guild_id == 0) {
  1661. //Delete guardians.
  1662. memset(&gc.guardian, 0, sizeof(gc.guardian));
  1663. }
  1664. break;
  1665. case 2: gc.economy = value; break;
  1666. case 3: gc.defense = value; break;
  1667. case 4: gc.triggerE = value; break;
  1668. case 5: gc.triggerD = value; break;
  1669. case 6: gc.nextTime = value; break;
  1670. case 7: gc.payTime = value; break;
  1671. case 8: gc.createTime = value; break;
  1672. case 9: gc.visibleC = value; break;
  1673. case 10:
  1674. case 11:
  1675. case 12:
  1676. case 13:
  1677. case 14:
  1678. case 15:
  1679. case 16:
  1680. case 17:
  1681. gc.guardian[index-10].visible = value; break;
  1682. default:
  1683. ShowError("mapif_parse_GuildCastleDataSave ERROR!! (Not found index=%d)\n", index);
  1684. return 0;
  1685. }
  1686. inter_guildcastle_tosql(&gc);
  1687. mapif_guild_castle_datasave(gc.castle_id,index,value);
  1688. return 0;
  1689. }
  1690. int mapif_parse_GuildMasterChange(int fd, int guild_id, const char* name, int len)
  1691. {
  1692. struct guild * g;
  1693. struct guild_member gm;
  1694. int pos;
  1695. g = inter_guild_fromsql(guild_id);
  1696. if(g==NULL || len > NAME_LENGTH)
  1697. return 0;
  1698. // Find member (name)
  1699. for (pos = 0; pos < g->max_member && strncmp(g->member[pos].name, name, len); pos++);
  1700. if (pos == g->max_member)
  1701. return 0; //Character not found??
  1702. // Switch current and old GM
  1703. memcpy(&gm, &g->member[pos], sizeof (struct guild_member));
  1704. memcpy(&g->member[pos], &g->member[0], sizeof(struct guild_member));
  1705. memcpy(&g->member[0], &gm, sizeof(struct guild_member));
  1706. // Switch positions
  1707. g->member[pos].position = g->member[0].position;
  1708. g->member[pos].modified = GS_MEMBER_MODIFIED;
  1709. g->member[0].position = 0; //Position 0: guild Master.
  1710. g->member[0].modified = GS_MEMBER_MODIFIED;
  1711. strncpy(g->master, name, len);
  1712. if (len < NAME_LENGTH)
  1713. g->master[len] = '\0';
  1714. ShowInfo("int_guild: Guildmaster Changed to %s (Guild %d - %s)\n",g->master, guild_id, g->name);
  1715. g->save_flag |= (GS_BASIC|GS_MEMBER); //Save main data and member data.
  1716. return mapif_guild_master_changed(g, g->member[0].account_id, g->member[0].char_id);
  1717. }
  1718. // map server からの通信
  1719. // ・1パケットのみ解析すること
  1720. // ・パケット長データはinter.cにセットしておくこと
  1721. // ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない
  1722. // ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない
  1723. int inter_guild_parse_frommap(int fd)
  1724. {
  1725. RFIFOHEAD(fd);
  1726. switch(RFIFOW(fd,0)) {
  1727. case 0x3030: mapif_parse_CreateGuild(fd,RFIFOL(fd,4),(char*)RFIFOP(fd,8),(struct guild_member *)RFIFOP(fd,32)); break;
  1728. case 0x3031: mapif_parse_GuildInfo(fd,RFIFOL(fd,2)); break;
  1729. case 0x3032: mapif_parse_GuildAddMember(fd,RFIFOL(fd,4),(struct guild_member *)RFIFOP(fd,8)); break;
  1730. case 0x3033: mapif_parse_GuildMasterChange(fd,RFIFOL(fd,4),(const char*)RFIFOP(fd,8),RFIFOW(fd,2)-8); break;
  1731. case 0x3034: mapif_parse_GuildLeave(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),(const char*)RFIFOP(fd,15)); break;
  1732. 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;
  1733. case 0x3036: mapif_parse_BreakGuild(fd,RFIFOL(fd,2)); break;
  1734. case 0x3037: mapif_parse_GuildMessage(fd,RFIFOL(fd,4),RFIFOL(fd,8),(char*)RFIFOP(fd,12),RFIFOW(fd,2)-12); break;
  1735. case 0x3039: mapif_parse_GuildBasicInfoChange(fd,RFIFOL(fd,4),RFIFOW(fd,8),(const char*)RFIFOP(fd,10),RFIFOW(fd,2)-10); break;
  1736. 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;
  1737. case 0x303B: mapif_parse_GuildPosition(fd,RFIFOL(fd,4),RFIFOL(fd,8),(struct guild_position *)RFIFOP(fd,12)); break;
  1738. case 0x303C: mapif_parse_GuildSkillUp(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14)); break;
  1739. case 0x303D: mapif_parse_GuildAlliance(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14),RFIFOB(fd,18)); break;
  1740. case 0x303E: mapif_parse_GuildNotice(fd,RFIFOL(fd,2),(const char*)RFIFOP(fd,6),(const char*)RFIFOP(fd,66)); break;
  1741. case 0x303F: mapif_parse_GuildEmblem(fd,RFIFOW(fd,2)-12,RFIFOL(fd,4),RFIFOL(fd,8),(const char*)RFIFOP(fd,12)); break;
  1742. case 0x3040: mapif_parse_GuildCastleDataLoad(fd,RFIFOW(fd,2),RFIFOB(fd,4)); break;
  1743. case 0x3041: mapif_parse_GuildCastleDataSave(fd,RFIFOW(fd,2),RFIFOB(fd,4),RFIFOL(fd,5)); break;
  1744. default:
  1745. return 0;
  1746. }
  1747. return 1;
  1748. }
  1749. // processes a mapserver connection event
  1750. int inter_guild_mapif_init(int fd)
  1751. {
  1752. return mapif_guild_castle_alldataload(fd);
  1753. }
  1754. // サーバーから脱退要求(キャラ削除用)
  1755. int inter_guild_leave(int guild_id, int account_id, int char_id)
  1756. {
  1757. return mapif_parse_GuildLeave(-1, guild_id, account_id, char_id, 0, "** Character Deleted **");
  1758. }
  1759. int inter_guild_broken(int guild_id)
  1760. {
  1761. return mapif_guild_broken(guild_id, 0);
  1762. }
  1763. #endif //TXT_SQL_CONVERT