char.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <time.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "../common/cbasetypes.h"
  9. #include "../common/core.h"
  10. #include "../common/db.h"
  11. #include "../common/malloc.h"
  12. #include "../common/mapindex.h"
  13. #include "../common/mmo.h"
  14. #include "../common/showmsg.h"
  15. #include "../common/socket.h"
  16. #include "../common/strlib.h"
  17. #include "../common/timer.h"
  18. #include "../common/cli.h"
  19. #include "int_guild.h"
  20. #include "int_homun.h"
  21. #include "int_mercenary.h"
  22. #include "int_elemental.h"
  23. #include "int_party.h"
  24. #include "int_storage.h"
  25. #include "inter.h"
  26. #include "char_logif.h"
  27. #include "char_mapif.h"
  28. #include "char_cnslif.h"
  29. #include "char_clif.h"
  30. //definition of exported var declared in .h
  31. int login_fd=-1; //login file descriptor
  32. int char_fd=-1; //char file descriptor
  33. struct Schema_Config schema_config;
  34. struct CharServ_Config charserv_config;
  35. struct mmo_map_server map_server[MAX_MAP_SERVERS];
  36. //Custom limits for the fame lists. [Skotlex]
  37. int fame_list_size_chemist = MAX_FAME_LIST;
  38. int fame_list_size_smith = MAX_FAME_LIST;
  39. int fame_list_size_taekwon = MAX_FAME_LIST;
  40. // Char-server-side stored fame lists [DracoRPG]
  41. struct fame_list smith_fame_list[MAX_FAME_LIST];
  42. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  43. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  44. #define CHAR_MAX_MSG 300 //max number of msg_conf
  45. static char* msg_table[CHAR_MAX_MSG]; // Login Server messages_conf
  46. // check for exit signal
  47. // 0 is saving complete
  48. // other is char_id
  49. unsigned int save_flag = 0;
  50. // Advanced subnet check [LuzZza]
  51. struct s_subnet {
  52. uint32 mask;
  53. uint32 char_ip;
  54. uint32 map_ip;
  55. } subnet[16];
  56. int subnet_count = 0;
  57. int char_chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data);
  58. DBMap* auth_db; // uint32 account_id -> struct auth_node*
  59. DBMap* online_char_db; // uint32 account_id -> struct online_char_data*
  60. DBMap* char_db_; // uint32 char_id -> struct mmo_charstatus*
  61. DBMap* char_get_authdb() { return auth_db; }
  62. DBMap* char_get_onlinedb() { return online_char_db; }
  63. DBMap* char_get_chardb() { return char_db_; }
  64. /**
  65. * @see DBCreateData
  66. */
  67. DBData char_create_online_data(DBKey key, va_list args){
  68. struct online_char_data* character;
  69. CREATE(character, struct online_char_data, 1);
  70. character->account_id = key.i;
  71. character->char_id = -1;
  72. character->server = -1;
  73. character->fd = -1;
  74. character->waiting_disconnect = INVALID_TIMER;
  75. return db_ptr2data(character);
  76. }
  77. void char_set_charselect(uint32 account_id) {
  78. struct online_char_data* character;
  79. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, char_create_online_data);
  80. if( character->server > -1 )
  81. if( map_server[character->server].users > 0 ) // Prevent this value from going negative.
  82. map_server[character->server].users--;
  83. character->char_id = -1;
  84. character->server = -1;
  85. if(character->waiting_disconnect != INVALID_TIMER) {
  86. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  87. character->waiting_disconnect = INVALID_TIMER;
  88. }
  89. chlogif_send_setacconline(account_id);
  90. }
  91. void char_set_char_online(int map_id, uint32 char_id, uint32 account_id) {
  92. struct online_char_data* character;
  93. struct mmo_charstatus *cp;
  94. //Update DB
  95. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d' LIMIT 1", schema_config.char_db, char_id) )
  96. Sql_ShowDebug(sql_handle);
  97. //Check to see for online conflicts
  98. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, char_create_online_data);
  99. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  100. {
  101. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  102. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  103. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  104. }
  105. //Update state data
  106. character->char_id = char_id;
  107. character->server = map_id;
  108. if( character->server > -1 )
  109. map_server[character->server].users++;
  110. //Get rid of disconnect timer
  111. if(character->waiting_disconnect != INVALID_TIMER) {
  112. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  113. character->waiting_disconnect = INVALID_TIMER;
  114. }
  115. //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  116. cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  117. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  118. //Notify login server
  119. chlogif_send_setacconline(account_id);
  120. }
  121. void char_set_char_offline(uint32 char_id, uint32 account_id){
  122. struct online_char_data* character;
  123. if ( char_id == -1 )
  124. {
  125. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", schema_config.char_db, account_id) )
  126. Sql_ShowDebug(sql_handle);
  127. }
  128. else
  129. {
  130. struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  131. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  132. if (cp)
  133. idb_remove(char_db_,char_id);
  134. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d' LIMIT 1", schema_config.char_db, char_id) )
  135. Sql_ShowDebug(sql_handle);
  136. }
  137. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  138. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  139. if( character->server > -1 )
  140. if( map_server[character->server].users > 0 ) // Prevent this value from going negative.
  141. map_server[character->server].users--;
  142. if(character->waiting_disconnect != INVALID_TIMER){
  143. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  144. character->waiting_disconnect = INVALID_TIMER;
  145. }
  146. if(character->char_id == char_id)
  147. {
  148. character->char_id = -1;
  149. character->server = -1;
  150. // needed if player disconnects completely since Skotlex did not want to free the session
  151. character->pincode_success = false;
  152. }
  153. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  154. }
  155. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  156. if (char_id == -1 || character == NULL || character->fd == -1){
  157. chlogif_send_setaccoffline(login_fd,account_id);
  158. }
  159. }
  160. /**
  161. * @see DBApply
  162. */
  163. int char_db_setoffline(DBKey key, DBData *data, va_list ap) {
  164. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  165. int server = va_arg(ap, int);
  166. if (server == -1) {
  167. character->char_id = -1;
  168. character->server = -1;
  169. if(character->waiting_disconnect != INVALID_TIMER){
  170. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  171. character->waiting_disconnect = INVALID_TIMER;
  172. }
  173. } else if (character->server == server)
  174. character->server = -2; //In some map server that we aren't connected to.
  175. return 0;
  176. }
  177. /**
  178. * @see DBApply
  179. */
  180. static int char_db_kickoffline(DBKey key, DBData *data, va_list ap){
  181. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  182. int server_id = va_arg(ap, int);
  183. if (server_id > -1 && character->server != server_id)
  184. return 0;
  185. //Kick out any connected characters, and set them offline as appropriate.
  186. if (character->server > -1)
  187. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 1);
  188. else if (character->waiting_disconnect == INVALID_TIMER)
  189. char_set_char_offline(character->char_id, character->account_id);
  190. else
  191. return 0; // fail
  192. return 1;
  193. }
  194. void char_set_all_offline(int id){
  195. if (id < 0)
  196. ShowNotice("Sending all users offline.\n");
  197. else
  198. ShowNotice("Sending users of map-server %d offline.\n",id);
  199. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  200. if (id >= 0 || !chlogif_isconnected())
  201. return;
  202. //Tell login-server to also mark all our characters as offline.
  203. chlogif_send_setallaccoffline(login_fd);
  204. }
  205. void char_set_all_offline_sql(void){
  206. //Set all players to 'OFFLINE'
  207. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", schema_config.char_db) )
  208. Sql_ShowDebug(sql_handle);
  209. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", schema_config.guild_member_db) )
  210. Sql_ShowDebug(sql_handle);
  211. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", schema_config.guild_db) )
  212. Sql_ShowDebug(sql_handle);
  213. }
  214. /**
  215. * @see DBCreateData
  216. */
  217. static DBData char_create_charstatus(DBKey key, va_list args) {
  218. struct mmo_charstatus *cp;
  219. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  220. cp->char_id = key.i;
  221. return db_ptr2data(cp);
  222. }
  223. int char_inventory_to_sql(const struct item items[], int max, int id);
  224. int char_mmo_char_tosql(uint32 char_id, struct mmo_charstatus* p){
  225. int i = 0;
  226. int count = 0;
  227. int diff = 0;
  228. char save_status[128]; //For displaying save information. [Skotlex]
  229. struct mmo_charstatus *cp;
  230. int errors = 0; //If there are any errors while saving, "cp" will not be updated at the end.
  231. StringBuf buf;
  232. if (char_id!=p->char_id) return 0;
  233. cp = (struct mmo_charstatus *)idb_ensure(char_db_, char_id, char_create_charstatus);
  234. StringBuf_Init(&buf);
  235. memset(save_status, 0, sizeof(save_status));
  236. //map inventory data
  237. if( memcmp(p->inventory, cp->inventory, sizeof(p->inventory)) ) {
  238. if (!char_inventory_to_sql(p->inventory, MAX_INVENTORY, p->char_id))
  239. strcat(save_status, " inventory");
  240. else
  241. errors++;
  242. }
  243. //map cart data
  244. if( memcmp(p->cart, cp->cart, sizeof(p->cart)) ) {
  245. if (!char_memitemdata_to_sql(p->cart, MAX_CART, p->char_id, TABLE_CART))
  246. strcat(save_status, " cart");
  247. else
  248. errors++;
  249. }
  250. //map storage data
  251. if( memcmp(p->storage.items, cp->storage.items, sizeof(p->storage.items)) ) {
  252. if (!char_memitemdata_to_sql(p->storage.items, MAX_STORAGE, p->account_id, TABLE_STORAGE))
  253. strcat(save_status, " storage");
  254. else
  255. errors++;
  256. }
  257. if (
  258. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  259. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  260. (p->zeny != cp->zeny) ||
  261. (p->last_point.map != cp->last_point.map) ||
  262. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  263. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  264. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  265. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  266. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  267. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  268. (p->option != cp->option) ||
  269. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  270. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) ||
  271. (p->ele_id != cp->ele_id) || (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  272. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom) || (p->delete_date != cp->delete_date) ||
  273. (p->rename != cp->rename) || (p->robe != cp->robe) || (p->character_moves != cp->character_moves) ||
  274. (p->unban_time != cp->unban_time) || (p->font != cp->font) || (p->uniqueitem_counter != cp->uniqueitem_counter) ||
  275. (p->hotkey_rowshift != cp->hotkey_rowshift)
  276. )
  277. { //Save status
  278. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  279. "`base_exp`='%u', `job_exp`='%u', `zeny`='%d',"
  280. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  281. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  282. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',`elemental_id`='%d',"
  283. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  284. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d',"
  285. "`delete_date`='%lu',`robe`='%d',`moves`='%d',`font`='%u',`uniqueitem_counter`='%u',"
  286. "`hotkey_rowshift`='%d'"
  287. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  288. schema_config.char_db, p->base_level, p->job_level,
  289. p->base_exp, p->job_exp, p->zeny,
  290. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  291. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  292. p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, p->ele_id,
  293. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  294. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  295. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename,
  296. (unsigned long)p->delete_date, // FIXME: platform-dependent size
  297. p->robe, p->character_moves, p->font, p->uniqueitem_counter,
  298. p->hotkey_rowshift,
  299. p->account_id, p->char_id) )
  300. {
  301. Sql_ShowDebug(sql_handle);
  302. errors++;
  303. } else
  304. strcat(save_status, " status");
  305. }
  306. //Values that will seldom change (to speed up saving)
  307. if (
  308. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  309. (p->body != cp->body) || (p->class_ != cp->class_) ||
  310. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  311. (p->mother != cp->mother) || (p->child != cp->child) ||
  312. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  313. (p->fame != cp->fame)
  314. )
  315. {
  316. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',"
  317. "`hair`='%d', `hair_color`='%d', `clothes_color`='%d', `body`='%d',"
  318. "`partner_id`='%u', `father`='%u', `mother`='%u', `child`='%u',"
  319. "`karma`='%d',`manner`='%d', `fame`='%d'"
  320. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  321. schema_config.char_db, p->class_,
  322. p->hair, p->hair_color, p->clothes_color, p->body,
  323. p->partner_id, p->father, p->mother, p->child,
  324. p->karma, p->manner, p->fame,
  325. p->account_id, p->char_id) )
  326. {
  327. Sql_ShowDebug(sql_handle);
  328. errors++;
  329. } else
  330. strcat(save_status, " status2");
  331. }
  332. /* Mercenary Owner */
  333. if( (p->mer_id != cp->mer_id) ||
  334. (p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) ||
  335. (p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) ||
  336. (p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) )
  337. {
  338. if (mercenary_owner_tosql(char_id, p))
  339. strcat(save_status, " mercenary");
  340. else
  341. errors++;
  342. }
  343. //memo points
  344. if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
  345. {
  346. char esc_mapname[NAME_LENGTH*2+1];
  347. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  348. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.memo_db, p->char_id) )
  349. {
  350. Sql_ShowDebug(sql_handle);
  351. errors++;
  352. }
  353. //insert here.
  354. StringBuf_Clear(&buf);
  355. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", schema_config.memo_db);
  356. for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
  357. {
  358. if( p->memo_point[i].map )
  359. {
  360. if( count )
  361. StringBuf_AppendStr(&buf, ",");
  362. Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
  363. StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
  364. ++count;
  365. }
  366. }
  367. if( count )
  368. {
  369. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  370. {
  371. Sql_ShowDebug(sql_handle);
  372. errors++;
  373. }
  374. }
  375. strcat(save_status, " memo");
  376. }
  377. //skills
  378. if( memcmp(p->skill, cp->skill, sizeof(p->skill)) )
  379. {
  380. //`skill` (`char_id`, `id`, `lv`)
  381. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.skill_db, p->char_id) )
  382. {
  383. Sql_ShowDebug(sql_handle);
  384. errors++;
  385. }
  386. StringBuf_Clear(&buf);
  387. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`,`flag`) VALUES ", schema_config.skill_db);
  388. //insert here.
  389. for( i = 0, count = 0; i < MAX_SKILL; ++i ) {
  390. if( p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY ) {
  391. if( p->skill[i].lv == 0 && ( p->skill[i].flag == SKILL_FLAG_PERM_GRANTED || p->skill[i].flag == SKILL_FLAG_PERMANENT ) )
  392. continue;
  393. if( p->skill[i].flag != SKILL_FLAG_PERMANENT && p->skill[i].flag != SKILL_FLAG_PERM_GRANTED && (p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0) == 0 )
  394. continue;
  395. if( count )
  396. StringBuf_AppendStr(&buf, ",");
  397. StringBuf_Printf(&buf, "('%d','%d','%d','%d')", char_id, p->skill[i].id,
  398. ( (p->skill[i].flag == SKILL_FLAG_PERMANENT || p->skill[i].flag == SKILL_FLAG_PERM_GRANTED) ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0),
  399. p->skill[i].flag == SKILL_FLAG_PERM_GRANTED ? p->skill[i].flag : 0);/* other flags do not need to be saved */
  400. ++count;
  401. }
  402. }
  403. if( count )
  404. {
  405. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  406. {
  407. Sql_ShowDebug(sql_handle);
  408. errors++;
  409. }
  410. }
  411. strcat(save_status, " skills");
  412. }
  413. diff = 0;
  414. for(i = 0; i < MAX_FRIENDS; i++){
  415. if(p->friends[i].char_id != cp->friends[i].char_id ||
  416. p->friends[i].account_id != cp->friends[i].account_id){
  417. diff = 1;
  418. break;
  419. }
  420. }
  421. if(diff == 1)
  422. { //Save friends
  423. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.friend_db, char_id) )
  424. {
  425. Sql_ShowDebug(sql_handle);
  426. errors++;
  427. }
  428. StringBuf_Clear(&buf);
  429. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", schema_config.friend_db);
  430. for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
  431. {
  432. if( p->friends[i].char_id > 0 )
  433. {
  434. if( count )
  435. StringBuf_AppendStr(&buf, ",");
  436. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id);
  437. count++;
  438. }
  439. }
  440. if( count )
  441. {
  442. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  443. {
  444. Sql_ShowDebug(sql_handle);
  445. errors++;
  446. }
  447. }
  448. strcat(save_status, " friends");
  449. }
  450. #ifdef HOTKEY_SAVING
  451. // hotkeys
  452. StringBuf_Clear(&buf);
  453. StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", schema_config.hotkey_db);
  454. diff = 0;
  455. for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
  456. if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
  457. {
  458. if( diff )
  459. StringBuf_AppendStr(&buf, ",");// not the first hotkey
  460. StringBuf_Printf(&buf, "('%d','%u','%u','%u','%u')", char_id, (unsigned int)i, (unsigned int)p->hotkeys[i].type, p->hotkeys[i].id , (unsigned int)p->hotkeys[i].lv);
  461. diff = 1;
  462. }
  463. }
  464. if(diff) {
  465. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  466. {
  467. Sql_ShowDebug(sql_handle);
  468. errors++;
  469. } else
  470. strcat(save_status, " hotkeys");
  471. }
  472. #endif
  473. StringBuf_Destroy(&buf);
  474. if (save_status[0]!='\0' && charserv_config.save_log)
  475. ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
  476. if (!errors)
  477. memcpy(cp, p, sizeof(struct mmo_charstatus));
  478. return 0;
  479. }
  480. /// Saves an array of 'item' entries into the specified table.
  481. int char_memitemdata_to_sql(const struct item items[], int max, int id, int tableswitch){
  482. StringBuf buf;
  483. SqlStmt* stmt;
  484. int i;
  485. int j;
  486. const char* tablename;
  487. const char* selectoption;
  488. struct item item; // temp storage variable
  489. bool* flag; // bit array for inventory matching
  490. bool found;
  491. int errors = 0;
  492. switch (tableswitch) {
  493. case TABLE_INVENTORY: tablename = schema_config.inventory_db; selectoption = "char_id"; break;
  494. case TABLE_CART: tablename = schema_config.cart_db; selectoption = "char_id"; break;
  495. case TABLE_STORAGE: tablename = schema_config.storage_db; selectoption = "account_id"; break;
  496. case TABLE_GUILD_STORAGE: tablename = schema_config.guild_storage_db; selectoption = "guild_id"; break;
  497. default:
  498. ShowError("Invalid table name!\n");
  499. return 1;
  500. }
  501. // The following code compares inventory with current database values
  502. // and performs modification/deletion/insertion only on relevant rows.
  503. // This approach is more complicated than a trivial delete&insert, but
  504. // it significantly reduces cpu load on the database server.
  505. StringBuf_Init(&buf);
  506. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`");
  507. for( j = 0; j < MAX_SLOTS; ++j )
  508. StringBuf_Printf(&buf, ", `card%d`", j);
  509. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id);
  510. stmt = SqlStmt_Malloc(sql_handle);
  511. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  512. || SQL_ERROR == SqlStmt_Execute(stmt) )
  513. {
  514. SqlStmt_ShowDebug(stmt);
  515. SqlStmt_Free(stmt);
  516. StringBuf_Destroy(&buf);
  517. return 1;
  518. }
  519. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  520. SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &item.nameid, 0, NULL, NULL);
  521. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  522. SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &item.equip, 0, NULL, NULL);
  523. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  524. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  525. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  526. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  527. SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &item.bound, 0, NULL, NULL);
  528. SqlStmt_BindColumn(stmt, 9, SQLDT_UINT64, &item.unique_id, 0, NULL, NULL);
  529. for( j = 0; j < MAX_SLOTS; ++j )
  530. SqlStmt_BindColumn(stmt, 10+j, SQLDT_USHORT, &item.card[j], 0, NULL, NULL);
  531. // bit array indicating which inventory items have already been matched
  532. flag = (bool*) aCalloc(max, sizeof(bool));
  533. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  534. {
  535. found = false;
  536. // search for the presence of the item in the char's inventory
  537. for( i = 0; i < max; ++i )
  538. {
  539. // skip empty and already matched entries
  540. if( items[i].nameid == 0 || flag[i] )
  541. continue;
  542. if( items[i].nameid == item.nameid
  543. && items[i].card[0] == item.card[0]
  544. && items[i].card[2] == item.card[2]
  545. && items[i].card[3] == item.card[3]
  546. ) { //They are the same item.
  547. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  548. if( j == MAX_SLOTS &&
  549. items[i].amount == item.amount &&
  550. items[i].equip == item.equip &&
  551. items[i].identify == item.identify &&
  552. items[i].refine == item.refine &&
  553. items[i].attribute == item.attribute &&
  554. items[i].expire_time == item.expire_time &&
  555. items[i].bound == item.bound &&
  556. items[i].unique_id == item.unique_id )
  557. ; //Do nothing.
  558. else
  559. {
  560. // update all fields.
  561. StringBuf_Clear(&buf);
  562. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `bound`='%d', `unique_id`='%"PRIu64"'",
  563. tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound, items[i].unique_id);
  564. for( j = 0; j < MAX_SLOTS; ++j )
  565. StringBuf_Printf(&buf, ", `card%d`=%hu", j, items[i].card[j]);
  566. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  567. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  568. {
  569. Sql_ShowDebug(sql_handle);
  570. errors++;
  571. }
  572. }
  573. found = flag[i] = true; //Item dealt with,
  574. break; //skip to next item in the db.
  575. }
  576. }
  577. if( !found )
  578. {// Item not present in inventory, remove it.
  579. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", tablename, item.id) )
  580. {
  581. Sql_ShowDebug(sql_handle);
  582. errors++;
  583. }
  584. }
  585. }
  586. SqlStmt_Free(stmt);
  587. StringBuf_Clear(&buf);
  588. StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`", tablename, selectoption);
  589. for( j = 0; j < MAX_SLOTS; ++j )
  590. StringBuf_Printf(&buf, ", `card%d`", j);
  591. StringBuf_AppendStr(&buf, ") VALUES ");
  592. found = false;
  593. // insert non-matched items into the db as new items
  594. for( i = 0; i < max; ++i )
  595. {
  596. // skip empty and already matched entries
  597. if( items[i].nameid == 0 || flag[i] )
  598. continue;
  599. if( found )
  600. StringBuf_AppendStr(&buf, ",");
  601. else
  602. found = true;
  603. StringBuf_Printf(&buf, "('%d', '%hu', '%d', '%d', '%d', '%d', '%d', '%u', '%d', '%"PRIu64"'",
  604. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound, items[i].unique_id);
  605. for( j = 0; j < MAX_SLOTS; ++j )
  606. StringBuf_Printf(&buf, ", '%hu'", items[i].card[j]);
  607. StringBuf_AppendStr(&buf, ")");
  608. }
  609. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  610. {
  611. Sql_ShowDebug(sql_handle);
  612. errors++;
  613. }
  614. StringBuf_Destroy(&buf);
  615. aFree(flag);
  616. return errors;
  617. }
  618. /* pretty much a copy of memitemdata_to_sql except it handles inventory_db exclusively,
  619. * - this is required because inventory db is the only one with the 'favorite' column. */
  620. int char_inventory_to_sql(const struct item items[], int max, int id) {
  621. StringBuf buf;
  622. SqlStmt* stmt;
  623. int i;
  624. int j;
  625. struct item item; // temp storage variable
  626. bool* flag; // bit array for inventory matching
  627. bool found;
  628. int errors = 0;
  629. // The following code compares inventory with current database values
  630. // and performs modification/deletion/insertion only on relevant rows.
  631. // This approach is more complicated than a trivial delete&insert, but
  632. // it significantly reduces cpu load on the database server.
  633. StringBuf_Init(&buf);
  634. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `bound`, `unique_id`");
  635. for( j = 0; j < MAX_SLOTS; ++j )
  636. StringBuf_Printf(&buf, ", `card%d`", j);
  637. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`='%d'", schema_config.inventory_db, id);
  638. stmt = SqlStmt_Malloc(sql_handle);
  639. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  640. || SQL_ERROR == SqlStmt_Execute(stmt) )
  641. {
  642. SqlStmt_ShowDebug(stmt);
  643. SqlStmt_Free(stmt);
  644. StringBuf_Destroy(&buf);
  645. return 1;
  646. }
  647. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  648. SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &item.nameid, 0, NULL, NULL);
  649. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  650. SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &item.equip, 0, NULL, NULL);
  651. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  652. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  653. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  654. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  655. SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &item.favorite, 0, NULL, NULL);
  656. SqlStmt_BindColumn(stmt, 9, SQLDT_CHAR, &item.bound, 0, NULL, NULL);
  657. SqlStmt_BindColumn(stmt, 10,SQLDT_UINT64, &item.unique_id, 0, NULL, NULL);
  658. for( j = 0; j < MAX_SLOTS; ++j )
  659. SqlStmt_BindColumn(stmt, 11+j, SQLDT_USHORT, &item.card[j], 0, NULL, NULL);
  660. // bit array indicating which inventory items have already been matched
  661. flag = (bool*) aCalloc(max, sizeof(bool));
  662. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ) {
  663. found = false;
  664. // search for the presence of the item in the char's inventory
  665. for( i = 0; i < max; ++i ) {
  666. // skip empty and already matched entries
  667. if( items[i].nameid == 0 || flag[i] )
  668. continue;
  669. if( items[i].nameid == item.nameid
  670. && items[i].card[0] == item.card[0]
  671. && items[i].card[2] == item.card[2]
  672. && items[i].card[3] == item.card[3]
  673. ) { //They are the same item.
  674. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  675. if( j == MAX_SLOTS &&
  676. items[i].amount == item.amount &&
  677. items[i].equip == item.equip &&
  678. items[i].identify == item.identify &&
  679. items[i].refine == item.refine &&
  680. items[i].attribute == item.attribute &&
  681. items[i].expire_time == item.expire_time &&
  682. items[i].favorite == item.favorite &&
  683. items[i].bound == item.bound &&
  684. items[i].unique_id == item.unique_id )
  685. ; //Do nothing.
  686. else {
  687. // update all fields.
  688. StringBuf_Clear(&buf);
  689. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `favorite`='%d', `bound`='%d', `unique_id`='%"PRIu64"'",
  690. schema_config.inventory_db, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].favorite, items[i].bound, items[i].unique_id);
  691. for( j = 0; j < MAX_SLOTS; ++j )
  692. StringBuf_Printf(&buf, ", `card%d`=%hu", j, items[i].card[j]);
  693. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  694. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) {
  695. Sql_ShowDebug(sql_handle);
  696. errors++;
  697. }
  698. }
  699. found = flag[i] = true; //Item dealt with,
  700. break; //skip to next item in the db.
  701. }
  702. }
  703. if( !found ) {// Item not present in inventory, remove it.
  704. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", schema_config.inventory_db, item.id) ) {
  705. Sql_ShowDebug(sql_handle);
  706. errors++;
  707. }
  708. }
  709. }
  710. SqlStmt_Free(stmt);
  711. StringBuf_Clear(&buf);
  712. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `bound`, `unique_id`", schema_config.inventory_db);
  713. for( j = 0; j < MAX_SLOTS; ++j )
  714. StringBuf_Printf(&buf, ", `card%d`", j);
  715. StringBuf_AppendStr(&buf, ") VALUES ");
  716. found = false;
  717. // insert non-matched items into the db as new items
  718. for( i = 0; i < max; ++i ) {
  719. // skip empty and already matched entries
  720. if( items[i].nameid == 0 || flag[i] )
  721. continue;
  722. if( found )
  723. StringBuf_AppendStr(&buf, ",");
  724. else
  725. found = true;
  726. StringBuf_Printf(&buf, "('%d', '%hu', '%d', '%d', '%d', '%d', '%d', '%u', '%d', '%d', '%"PRIu64"'",
  727. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].favorite, items[i].bound, items[i].unique_id);
  728. for( j = 0; j < MAX_SLOTS; ++j )
  729. StringBuf_Printf(&buf, ", '%hu'", items[i].card[j]);
  730. StringBuf_AppendStr(&buf, ")");
  731. }
  732. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) {
  733. Sql_ShowDebug(sql_handle);
  734. errors++;
  735. }
  736. StringBuf_Destroy(&buf);
  737. aFree(flag);
  738. return errors;
  739. }
  740. /**
  741. * Returns the correct gender ID for the given character and enum value.
  742. *
  743. * If the per-character sex is defined but not supported by the current packetver, the database entries are corrected.
  744. *
  745. * @param sd Character data, if available.
  746. * @param p Character status.
  747. * @param sex Character sex (database enum)
  748. *
  749. * @retval SEX_MALE if the per-character sex is male
  750. * @retval SEX_FEMALE if the per-character sex is female
  751. * @retval 99 if the per-character sex is not defined or the current PACKETVER doesn't support it.
  752. */
  753. int char_mmo_gender(const struct char_session_data *sd, const struct mmo_charstatus *p, char sex)
  754. {
  755. #if PACKETVER >= 20141016
  756. (void)sd; (void)p; // Unused
  757. switch (sex) {
  758. case 'M':
  759. return SEX_MALE;
  760. case 'F':
  761. return SEX_FEMALE;
  762. case 'U':
  763. default:
  764. return 99;
  765. }
  766. #else
  767. if (sex == 'M' || sex == 'F') {
  768. if (!sd) {
  769. // sd is not available, there isn't much we can do. Just return and print a warning.
  770. ShowWarning("Character '%s' (CID: %d, AID: %d) has sex '%c', but PACKETVER does not support per-character sex. Defaulting to 'U'.\n",
  771. p->name, p->char_id, p->account_id, sex);
  772. return 99;
  773. }
  774. if ((sex == 'M' && sd->sex == SEX_FEMALE)
  775. || (sex == 'F' && sd->sex == SEX_MALE)) {
  776. ShowWarning("Changing sex of character '%s' (CID: %d, AID: %d) to 'U' due to incompatible PACKETVER.\n", p->name, p->char_id, p->account_id);
  777. chlogif_parse_ackchangecharsex(p->char_id, sd->sex);
  778. } else {
  779. ShowInfo("Resetting sex of character '%s' (CID: %d, AID: %d) to 'U' due to incompatible PACKETVER.\n", p->name, p->char_id, p->account_id);
  780. }
  781. if (SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `sex` = 'U' WHERE `char_id` = '%d'", schema_config.char_db, p->char_id)) {
  782. Sql_ShowDebug(sql_handle);
  783. }
  784. }
  785. return 99;
  786. #endif
  787. }
  788. int char_mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p);
  789. //=====================================================================================================
  790. // Loads the basic character rooster for the given account. Returns total buffer used.
  791. int char_mmo_chars_fromsql(struct char_session_data* sd, uint8* buf) {
  792. SqlStmt* stmt;
  793. struct mmo_charstatus p;
  794. int j = 0, i;
  795. char last_map[MAP_NAME_LENGTH_EXT];
  796. char sex[2];
  797. stmt = SqlStmt_Malloc(sql_handle);
  798. if( stmt == NULL ) {
  799. SqlStmt_ShowDebug(stmt);
  800. return 0;
  801. }
  802. memset(&p, 0, sizeof(p));
  803. for( i = 0; i < MAX_CHARS; i++ ) {
  804. sd->found_char[i] = -1;
  805. sd->unban_time[i] = 0;
  806. }
  807. // read char data
  808. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  809. "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  810. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  811. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`,"
  812. "`clothes_color`,`body`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`rename`,`delete_date`,"
  813. "`robe`,`moves`,`unban_time`,`font`,`uniqueitem_counter`,`sex`,`hotkey_rowshift`"
  814. " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", schema_config.char_db, sd->account_id, MAX_CHARS)
  815. || SQL_ERROR == SqlStmt_Execute(stmt)
  816. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL)
  817. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL)
  818. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL)
  819. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL)
  820. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL)
  821. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL)
  822. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p.base_exp, 0, NULL, NULL)
  823. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p.job_exp, 0, NULL, NULL)
  824. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL)
  825. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL)
  826. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL)
  827. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL)
  828. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL)
  829. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL)
  830. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL)
  831. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_INT, &p.max_hp, 0, NULL, NULL)
  832. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p.hp, 0, NULL, NULL)
  833. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p.max_sp, 0, NULL, NULL)
  834. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p.sp, 0, NULL, NULL)
  835. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p.status_point, 0, NULL, NULL)
  836. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p.skill_point, 0, NULL, NULL)
  837. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL)
  838. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL)
  839. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL)
  840. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL)
  841. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL)
  842. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL)
  843. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.body, 0, NULL, NULL)
  844. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.weapon, 0, NULL, NULL)
  845. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.shield, 0, NULL, NULL)
  846. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_top, 0, NULL, NULL)
  847. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL)
  848. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL)
  849. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  850. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p.rename, 0, NULL, NULL)
  851. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_UINT32, &p.delete_date, 0, NULL, NULL)
  852. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p.robe, 0, NULL, NULL)
  853. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_UINT, &p.character_moves, 0, NULL, NULL)
  854. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_LONG, &p.unban_time, 0, NULL, NULL)
  855. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_UCHAR, &p.font, 0, NULL, NULL)
  856. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_UINT, &p.uniqueitem_counter, 0, NULL, NULL)
  857. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_ENUM, &sex, sizeof(sex), NULL, NULL)
  858. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_UCHAR, &p.hotkey_rowshift, 0, NULL, NULL)
  859. )
  860. {
  861. SqlStmt_ShowDebug(stmt);
  862. SqlStmt_Free(stmt);
  863. return 0;
  864. }
  865. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ )
  866. {
  867. p.last_point.map = mapindex_name2id(last_map);
  868. sd->found_char[p.slot] = p.char_id;
  869. sd->unban_time[p.slot] = p.unban_time;
  870. p.sex = char_mmo_gender(sd, &p, sex[0]);
  871. j += char_mmo_char_tobuf(WBUFP(buf, j), &p);
  872. // Addon System
  873. // store the required info into the session
  874. sd->char_moves[p.slot] = p.character_moves;
  875. }
  876. memset(sd->new_name,0,sizeof(sd->new_name));
  877. SqlStmt_Free(stmt);
  878. return j;
  879. }
  880. //=====================================================================================================
  881. int char_mmo_char_fromsql(uint32 char_id, struct mmo_charstatus* p, bool load_everything) {
  882. int i,j;
  883. struct mmo_charstatus* cp;
  884. StringBuf buf;
  885. SqlStmt* stmt;
  886. char last_map[MAP_NAME_LENGTH_EXT];
  887. char save_map[MAP_NAME_LENGTH_EXT];
  888. char point_map[MAP_NAME_LENGTH_EXT];
  889. struct point tmp_point;
  890. struct item tmp_item;
  891. struct s_skill tmp_skill;
  892. uint16 skill_count = 0;
  893. struct s_friend tmp_friend;
  894. #ifdef HOTKEY_SAVING
  895. struct hotkey tmp_hotkey;
  896. int hotkey_num;
  897. #endif
  898. StringBuf msg_buf;
  899. char sex[2];
  900. memset(p, 0, sizeof(struct mmo_charstatus));
  901. if (charserv_config.save_log) ShowInfo("Char load request (%d)\n", char_id);
  902. stmt = SqlStmt_Malloc(sql_handle);
  903. if( stmt == NULL )
  904. {
  905. SqlStmt_ShowDebug(stmt);
  906. return 0;
  907. }
  908. // read char data
  909. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  910. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  911. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  912. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,"
  913. "`hair_color`,`clothes_color`,`body`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  914. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`, `moves`,"
  915. "`unban_time`,`font`,`uniqueitem_counter`,`sex`,`hotkey_rowshift`"
  916. " FROM `%s` WHERE `char_id`=? LIMIT 1", schema_config.char_db)
  917. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  918. || SQL_ERROR == SqlStmt_Execute(stmt)
  919. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL)
  920. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL)
  921. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL)
  922. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL)
  923. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL)
  924. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL)
  925. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL)
  926. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p->base_exp, 0, NULL, NULL)
  927. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &p->job_exp, 0, NULL, NULL)
  928. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL)
  929. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL)
  930. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL)
  931. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL)
  932. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL)
  933. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL)
  934. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL)
  935. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p->max_hp, 0, NULL, NULL)
  936. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p->hp, 0, NULL, NULL)
  937. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p->max_sp, 0, NULL, NULL)
  938. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_INT, &p->sp, 0, NULL, NULL)
  939. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p->status_point, 0, NULL, NULL)
  940. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p->skill_point, 0, NULL, NULL)
  941. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL)
  942. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL)
  943. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL)
  944. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL)
  945. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL)
  946. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL)
  947. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL)
  948. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_INT, &p->ele_id, 0, NULL, NULL)
  949. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair, 0, NULL, NULL)
  950. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL)
  951. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL)
  952. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->body, 0, NULL, NULL)
  953. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->weapon, 0, NULL, NULL)
  954. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->shield, 0, NULL, NULL)
  955. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_top, 0, NULL, NULL)
  956. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL)
  957. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL)
  958. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  959. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL)
  960. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL)
  961. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL)
  962. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL)
  963. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL)
  964. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_UINT32, &p->partner_id, 0, NULL, NULL)
  965. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_UINT32, &p->father, 0, NULL, NULL)
  966. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_UINT32, &p->mother, 0, NULL, NULL)
  967. || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_UINT32, &p->child, 0, NULL, NULL)
  968. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_INT, &p->fame, 0, NULL, NULL)
  969. || SQL_ERROR == SqlStmt_BindColumn(stmt, 50, SQLDT_SHORT, &p->rename, 0, NULL, NULL)
  970. || SQL_ERROR == SqlStmt_BindColumn(stmt, 51, SQLDT_UINT32, &p->delete_date, 0, NULL, NULL)
  971. || SQL_ERROR == SqlStmt_BindColumn(stmt, 52, SQLDT_SHORT, &p->robe, 0, NULL, NULL)
  972. || SQL_ERROR == SqlStmt_BindColumn(stmt, 53, SQLDT_UINT32, &p->character_moves, 0, NULL, NULL)
  973. || SQL_ERROR == SqlStmt_BindColumn(stmt, 54, SQLDT_LONG, &p->unban_time, 0, NULL, NULL)
  974. || SQL_ERROR == SqlStmt_BindColumn(stmt, 55, SQLDT_UCHAR, &p->font, 0, NULL, NULL)
  975. || SQL_ERROR == SqlStmt_BindColumn(stmt, 56, SQLDT_UINT, &p->uniqueitem_counter, 0, NULL, NULL)
  976. || SQL_ERROR == SqlStmt_BindColumn(stmt, 57, SQLDT_ENUM, &sex, sizeof(sex), NULL, NULL)
  977. || SQL_ERROR == SqlStmt_BindColumn(stmt, 58, SQLDT_UCHAR, &p->hotkey_rowshift, 0, NULL, NULL)
  978. )
  979. {
  980. SqlStmt_ShowDebug(stmt);
  981. SqlStmt_Free(stmt);
  982. return 0;
  983. }
  984. if( SQL_ERROR == SqlStmt_NextRow(stmt) )
  985. {
  986. ShowError("Requested non-existant character id: %d!\n", char_id);
  987. SqlStmt_Free(stmt);
  988. return 0;
  989. }
  990. p->sex = char_mmo_gender(NULL, p, sex[0]);
  991. p->last_point.map = mapindex_name2id(last_map);
  992. p->save_point.map = mapindex_name2id(save_map);
  993. if( p->last_point.map == 0 ) {
  994. p->last_point.map = mapindex_name2id(charserv_config.default_map);
  995. p->last_point.x = charserv_config.default_map_x;
  996. p->last_point.y = charserv_config.default_map_y;
  997. }
  998. if( p->save_point.map == 0 ) {
  999. p->save_point.map = mapindex_name2id(charserv_config.default_map);
  1000. p->save_point.x = charserv_config.default_map_x;
  1001. p->save_point.y = charserv_config.default_map_y;
  1002. }
  1003. StringBuf_Init(&msg_buf);
  1004. StringBuf_AppendStr(&msg_buf, " status");
  1005. if (!load_everything) // For quick selection of data when displaying the char menu
  1006. {
  1007. SqlStmt_Free(stmt);
  1008. StringBuf_Destroy(&msg_buf);
  1009. return 1;
  1010. }
  1011. //read memo data
  1012. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  1013. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", schema_config.memo_db, MAX_MEMOPOINTS)
  1014. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1015. || SQL_ERROR == SqlStmt_Execute(stmt)
  1016. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL)
  1017. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL)
  1018. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) )
  1019. SqlStmt_ShowDebug(stmt);
  1020. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1021. {
  1022. tmp_point.map = mapindex_name2id(point_map);
  1023. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  1024. }
  1025. StringBuf_AppendStr(&msg_buf, " memo");
  1026. //read inventory
  1027. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, `expire_time`, `favorite`, `unique_id`)
  1028. StringBuf_Init(&buf);
  1029. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `bound`, `unique_id`");
  1030. for( i = 0; i < MAX_SLOTS; ++i )
  1031. StringBuf_Printf(&buf, ", `card%d`", i);
  1032. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", schema_config.inventory_db, MAX_INVENTORY);
  1033. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1034. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1035. || SQL_ERROR == SqlStmt_Execute(stmt)
  1036. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1037. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &tmp_item.nameid, 0, NULL, NULL)
  1038. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1039. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &tmp_item.equip, 0, NULL, NULL)
  1040. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1041. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1042. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1043. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1044. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &tmp_item.favorite, 0, NULL, NULL)
  1045. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_CHAR, &tmp_item.bound, 0, NULL, NULL)
  1046. || SQL_ERROR == SqlStmt_BindColumn(stmt,10, SQLDT_UINT64, &tmp_item.unique_id, 0, NULL, NULL) )
  1047. SqlStmt_ShowDebug(stmt);
  1048. for( i = 0; i < MAX_SLOTS; ++i )
  1049. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 11+i, SQLDT_USHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1050. SqlStmt_ShowDebug(stmt);
  1051. for( i = 0; i < MAX_INVENTORY && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1052. memcpy(&p->inventory[i], &tmp_item, sizeof(tmp_item));
  1053. StringBuf_AppendStr(&msg_buf, " inventory");
  1054. //read cart
  1055. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, expire_time`, `unique_id`)
  1056. StringBuf_Clear(&buf);
  1057. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`");
  1058. for( j = 0; j < MAX_SLOTS; ++j )
  1059. StringBuf_Printf(&buf, ", `card%d`", j);
  1060. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", schema_config.cart_db, MAX_CART);
  1061. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1062. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1063. || SQL_ERROR == SqlStmt_Execute(stmt)
  1064. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1065. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &tmp_item.nameid, 0, NULL, NULL)
  1066. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1067. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &tmp_item.equip, 0, NULL, NULL)
  1068. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1069. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1070. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1071. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1072. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &tmp_item.bound, 0, NULL, NULL)
  1073. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_UINT64, &tmp_item.unique_id, 0, NULL, NULL) )
  1074. SqlStmt_ShowDebug(stmt);
  1075. for( i = 0; i < MAX_SLOTS; ++i )
  1076. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 10+i, SQLDT_USHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1077. SqlStmt_ShowDebug(stmt);
  1078. for( i = 0; i < MAX_CART && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1079. memcpy(&p->cart[i], &tmp_item, sizeof(tmp_item));
  1080. StringBuf_AppendStr(&msg_buf, " cart");
  1081. //read storage
  1082. storage_fromsql(p->account_id, &p->storage);
  1083. StringBuf_AppendStr(&msg_buf, " storage");
  1084. //read skill
  1085. //`skill` (`char_id`, `id`, `lv`)
  1086. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv`,`flag` FROM `%s` WHERE `char_id`=? LIMIT %d", schema_config.skill_db, MAX_SKILL)
  1087. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1088. || SQL_ERROR == SqlStmt_Execute(stmt)
  1089. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_UINT16, &tmp_skill.id , 0, NULL, NULL)
  1090. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UINT8 , &tmp_skill.lv , 0, NULL, NULL)
  1091. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT8 , &tmp_skill.flag, 0, NULL, NULL) )
  1092. SqlStmt_ShowDebug(stmt);
  1093. if( tmp_skill.flag != SKILL_FLAG_PERM_GRANTED )
  1094. tmp_skill.flag = SKILL_FLAG_PERMANENT;
  1095. for( i = 0; skill_count < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ ) {
  1096. if( tmp_skill.id > 0 && tmp_skill.id < MAX_SKILL_ID ) {
  1097. memcpy(&p->skill[i], &tmp_skill, sizeof(tmp_skill));
  1098. skill_count++;
  1099. }
  1100. else
  1101. ShowWarning("mmo_char_fromsql: ignoring invalid skill (id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", tmp_skill.id, tmp_skill.lv, p->name, p->account_id, p->char_id);
  1102. }
  1103. StringBuf_Printf(&msg_buf, " %d skills", skill_count);
  1104. //read friends
  1105. //`friends` (`char_id`, `friend_account`, `friend_id`)
  1106. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT c.`account_id`, c.`char_id`, c.`name` FROM `%s` c LEFT JOIN `%s` f ON f.`friend_account` = c.`account_id` AND f.`friend_id` = c.`char_id` WHERE f.`char_id`=? LIMIT %d", schema_config.char_db, schema_config.friend_db, MAX_FRIENDS)
  1107. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1108. || SQL_ERROR == SqlStmt_Execute(stmt)
  1109. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL)
  1110. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL)
  1111. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) )
  1112. SqlStmt_ShowDebug(stmt);
  1113. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1114. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  1115. StringBuf_AppendStr(&msg_buf, " friends");
  1116. #ifdef HOTKEY_SAVING
  1117. //read hotkeys
  1118. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  1119. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", schema_config.hotkey_db)
  1120. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1121. || SQL_ERROR == SqlStmt_Execute(stmt)
  1122. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL)
  1123. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL)
  1124. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL)
  1125. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) )
  1126. SqlStmt_ShowDebug(stmt);
  1127. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  1128. {
  1129. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS )
  1130. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  1131. else
  1132. ShowWarning("mmo_char_fromsql: ignoring invalid hotkey (hotkey=%d,type=%u,id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", hotkey_num, tmp_hotkey.type, tmp_hotkey.id, tmp_hotkey.lv, p->name, p->account_id, p->char_id);
  1133. }
  1134. StringBuf_AppendStr(&msg_buf, " hotkeys");
  1135. #endif
  1136. /* Mercenary Owner DataBase */
  1137. mercenary_owner_fromsql(char_id, p);
  1138. StringBuf_AppendStr(&msg_buf, " mercenary");
  1139. if (charserv_config.save_log) ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, StringBuf_Value(&msg_buf)); //ok. all data load successfuly!
  1140. SqlStmt_Free(stmt);
  1141. StringBuf_Destroy(&buf);
  1142. cp = (struct mmo_charstatus *)idb_ensure(char_db_, char_id, char_create_charstatus);
  1143. memcpy(cp, p, sizeof(struct mmo_charstatus));
  1144. StringBuf_Destroy(&msg_buf);
  1145. return 1;
  1146. }
  1147. //==========================================================================================================
  1148. int char_mmo_sql_init(void) {
  1149. char_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  1150. ShowStatus("Characters per Account: '%d'.\n", charserv_config.char_config.char_per_account);
  1151. //the 'set offline' part is now in check_login_conn ...
  1152. //if the server connects to loginserver
  1153. //it will dc all off players
  1154. //and send the loginserver the new state....
  1155. // Force all users offline in sql when starting char-server
  1156. // (useful when servers crashs and don't clean the database)
  1157. char_set_all_offline_sql();
  1158. return 0;
  1159. }
  1160. //-----------------------------------
  1161. // Function to change chararcter's names
  1162. //-----------------------------------
  1163. int char_rename_char_sql(struct char_session_data *sd, uint32 char_id)
  1164. {
  1165. struct mmo_charstatus char_dat;
  1166. char esc_name[NAME_LENGTH*2+1];
  1167. if( sd->new_name[0] == 0 ) // Not ready for rename
  1168. return 2;
  1169. if( !char_mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed.
  1170. return 2;
  1171. if( char_dat.rename == 0 )
  1172. return 1;
  1173. Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH));
  1174. // check if the char exist
  1175. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` LIKE '%s' LIMIT 1", schema_config.char_db, esc_name) )
  1176. {
  1177. Sql_ShowDebug(sql_handle);
  1178. return 4;
  1179. }
  1180. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `name` = '%s', `rename` = '%d' WHERE `char_id` = '%d'", schema_config.char_db, esc_name, --char_dat.rename, char_id) )
  1181. {
  1182. Sql_ShowDebug(sql_handle);
  1183. return 3;
  1184. }
  1185. // Change character's name into guild_db.
  1186. if( char_dat.guild_id )
  1187. inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name);
  1188. safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH);
  1189. memset(sd->new_name,0,sizeof(sd->new_name));
  1190. // log change
  1191. if( charserv_config.log_char )
  1192. {
  1193. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1194. "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')",
  1195. schema_config.charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) )
  1196. Sql_ShowDebug(sql_handle);
  1197. }
  1198. return 0;
  1199. }
  1200. int char_check_char_name(char * name, char * esc_name)
  1201. {
  1202. int i;
  1203. // check length of character name
  1204. if( name[0] == '\0' )
  1205. return -2; // empty character name
  1206. /**
  1207. * The client does not allow you to create names with less than 4 characters, however,
  1208. * the use of WPE can bypass this, and this fixes the exploit.
  1209. **/
  1210. if( strlen( name ) < 4 )
  1211. return -2;
  1212. // check content of character name
  1213. if( remove_control_chars(name) )
  1214. return -2; // control chars in name
  1215. // check for reserved names
  1216. if( strcmpi(name, charserv_config.wisp_server_name) == 0 )
  1217. return -1; // nick reserved for internal server messages
  1218. // Check Authorised letters/symbols in the name of the character
  1219. if( charserv_config.char_config.char_name_option == 1 )
  1220. { // only letters/symbols in char_name_letters are authorised
  1221. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1222. if( strchr(charserv_config.char_config.char_name_letters, name[i]) == NULL )
  1223. return -2;
  1224. }
  1225. else if( charserv_config.char_config.char_name_option == 2 )
  1226. { // letters/symbols in char_name_letters are forbidden
  1227. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1228. if( strchr(charserv_config.char_config.char_name_letters, name[i]) != NULL )
  1229. return -2;
  1230. }
  1231. if( charserv_config.char_config.name_ignoring_case ) {
  1232. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE BINARY `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1233. Sql_ShowDebug(sql_handle);
  1234. return -2;
  1235. }
  1236. } else {
  1237. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1238. Sql_ShowDebug(sql_handle);
  1239. return -2;
  1240. }
  1241. }
  1242. if( Sql_NumRows(sql_handle) > 0 )
  1243. return -1; // name already exists
  1244. return 0;
  1245. }
  1246. //-----------------------------------
  1247. // Function to create a new character
  1248. //-----------------------------------
  1249. #if PACKETVER >= 20120307
  1250. #if PACKETVER >= 20151001
  1251. int char_make_new_char_sql(struct char_session_data* sd, char* name_, int slot, int hair_color, int hair_style, short start_job, short unknown, int sex) { // TODO: Unknown byte
  1252. #else
  1253. int char_make_new_char_sql(struct char_session_data* sd, char* name_, int slot, int hair_color, int hair_style) {
  1254. #endif
  1255. int str = 1, agi = 1, vit = 1, int_ = 1, dex = 1, luk = 1;
  1256. #else
  1257. int char_make_new_char_sql(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style) {
  1258. #endif
  1259. char name[NAME_LENGTH];
  1260. char esc_name[NAME_LENGTH*2+1];
  1261. struct point tmp_start_point[MAX_STARTPOINT];
  1262. struct startitem tmp_start_items[MAX_STARTITEM];
  1263. uint32 char_id;
  1264. int flag, k, start_point_idx = rand() % charserv_config.start_point_count;
  1265. safestrncpy(name, name_, NAME_LENGTH);
  1266. normalize_name(name,TRIM_CHARS);
  1267. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1268. memset(tmp_start_point, 0, MAX_STARTPOINT * sizeof(struct point));
  1269. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1270. memcpy(tmp_start_point, charserv_config.start_point, MAX_STARTPOINT * sizeof(struct point));
  1271. memcpy(tmp_start_items, charserv_config.start_items, MAX_STARTITEM * sizeof(struct startitem));
  1272. flag = char_check_char_name(name,esc_name);
  1273. if( flag < 0 )
  1274. return flag;
  1275. //check other inputs
  1276. #if PACKETVER >= 20120307
  1277. #if PACKETVER >= 20151001
  1278. switch(sex) {
  1279. case SEX_FEMALE:
  1280. sex = 'F';
  1281. break;
  1282. case SEX_MALE:
  1283. sex = 'M';
  1284. break;
  1285. default:
  1286. sex = 'U';
  1287. break;
  1288. }
  1289. #endif
  1290. if(slot < 0 || slot >= sd->char_slots)
  1291. #else
  1292. if((slot < 0 || slot >= sd->char_slots) // slots
  1293. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1294. || (str < 1 || str > 9 || agi < 1 || agi > 9 || vit < 1 || vit > 9 || int_ < 1 || int_ > 9 || dex < 1 || dex > 9 || luk < 1 || luk > 9) // individual stat values
  1295. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1296. #endif
  1297. #if PACKETVER >= 20100413
  1298. return -4; // invalid slot
  1299. #else
  1300. return -2; // invalid input
  1301. #endif
  1302. // check the number of already existing chars in this account
  1303. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", schema_config.char_db, sd->account_id) )
  1304. Sql_ShowDebug(sql_handle);
  1305. if( Sql_NumRows(sql_handle) >= sd->char_slots )
  1306. return -2; // character account limit exceeded
  1307. // check char slot
  1308. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' LIMIT 1", schema_config.char_db, sd->account_id, slot) )
  1309. Sql_ShowDebug(sql_handle);
  1310. if( Sql_NumRows(sql_handle) > 0 )
  1311. return -2; // slot already in use
  1312. // validation success, log result
  1313. if (charserv_config.log_char) {
  1314. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1315. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1316. schema_config.charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1317. Sql_ShowDebug(sql_handle);
  1318. }
  1319. #if PACKETVER >= 20151001
  1320. if (start_job != JOB_NOVICE && start_job != JOB_SUMMONER)
  1321. return -2; // Invalid job
  1322. // Check for Doram based information.
  1323. if (start_job == JOB_SUMMONER) { // Check for just this job for now.
  1324. memset(tmp_start_point, 0, MAX_STARTPOINT * sizeof(struct point));
  1325. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1326. memcpy(tmp_start_point, charserv_config.start_point_doram, MAX_STARTPOINT * sizeof(struct point));
  1327. memcpy(tmp_start_items, charserv_config.start_items_doram, MAX_STARTITEM * sizeof(struct startitem));
  1328. start_point_idx = rand() % charserv_config.start_point_count_doram;
  1329. }
  1330. #endif
  1331. //Insert the new char entry to the database
  1332. #if PACKETVER >= 20151001
  1333. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `class`, `zeny`, `status_point`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1334. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`, `sex`) VALUES ("
  1335. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d', '%c')",
  1336. schema_config.char_db, sd->account_id , slot, esc_name, start_job, charserv_config.start_zeny, 48, str, agi, vit, int_, dex, luk,
  1337. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1338. mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, sex) )
  1339. #elif PACKETVER >= 20120307
  1340. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `status_point`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1341. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1342. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1343. schema_config.char_db, sd->account_id , slot, esc_name, charserv_config.start_zeny, 48, str, agi, vit, int_, dex, luk,
  1344. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1345. mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y) )
  1346. #else
  1347. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1348. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1349. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1350. schema_config.char_db, sd->account_id , slot, esc_name, charserv_config.start_zeny, str, agi, vit, int_, dex, luk,
  1351. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1352. mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y) )
  1353. #endif
  1354. {
  1355. Sql_ShowDebug(sql_handle);
  1356. return -2; //No, stop the procedure!
  1357. }
  1358. //Retrieve the newly auto-generated char id
  1359. char_id = (int)Sql_LastInsertId(sql_handle);
  1360. //Give the char the default items
  1361. for (k = 0; k <= MAX_STARTITEM && tmp_start_items[k].nameid != 0; k ++) {
  1362. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%hu', '%hu', '%hu', '%d')", schema_config.inventory_db, char_id, tmp_start_items[k].nameid, tmp_start_items[k].amount, tmp_start_items[k].pos, 1) )
  1363. Sql_ShowDebug(sql_handle);
  1364. }
  1365. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1366. return char_id;
  1367. }
  1368. /*----------------------------------------------------------------------------------------------------------*/
  1369. /* Divorce Players */
  1370. /*----------------------------------------------------------------------------------------------------------*/
  1371. int char_divorce_char_sql(int partner_id1, int partner_id2){
  1372. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d' LIMIT 2", schema_config.char_db, partner_id1, partner_id2) )
  1373. Sql_ShowDebug(sql_handle);
  1374. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%hu' OR `nameid`='%hu') AND (`char_id`='%d' OR `char_id`='%d') LIMIT 2", schema_config.inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) )
  1375. Sql_ShowDebug(sql_handle);
  1376. chmapif_send_ackdivorce(partner_id1, partner_id2);
  1377. return 0;
  1378. }
  1379. /*----------------------------------------------------------------------------------------------------------*/
  1380. /* Delete char - davidsiaw */
  1381. /*----------------------------------------------------------------------------------------------------------*/
  1382. /* Returns 0 if successful
  1383. * Returns < 0 for error
  1384. */
  1385. int char_delete_char_sql(uint32 char_id){
  1386. char name[NAME_LENGTH];
  1387. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1388. uint32 account_id;
  1389. int party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id, elemental_id;
  1390. char *data;
  1391. size_t len;
  1392. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `name`,`account_id`,`party_id`,`guild_id`,`base_level`,`homun_id`,`partner_id`,`father`,`mother`,`elemental_id` FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id))
  1393. Sql_ShowDebug(sql_handle);
  1394. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1395. {
  1396. ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n");
  1397. Sql_FreeResult(sql_handle);
  1398. return -1;
  1399. }
  1400. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1401. Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data);
  1402. Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data);
  1403. Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data);
  1404. Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data);
  1405. Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data);
  1406. Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data);
  1407. Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data);
  1408. Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data);
  1409. Sql_GetData(sql_handle, 9, &data, NULL); elemental_id = atoi(data);
  1410. Sql_EscapeStringLen(sql_handle, esc_name, name, zmin(len, NAME_LENGTH));
  1411. Sql_FreeResult(sql_handle);
  1412. //check for config char del condition [Lupus]
  1413. // TODO: Move this out to packet processing (0x68/0x1fb).
  1414. if( ( charserv_config.char_config.char_del_level > 0 && base_level >= charserv_config.char_config.char_del_level )
  1415. || ( charserv_config.char_config.char_del_level < 0 && base_level <= -charserv_config.char_config.char_del_level )
  1416. ) {
  1417. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1418. return -1;
  1419. }
  1420. /* Divorce [Wizputer] */
  1421. if( partner_id )
  1422. char_divorce_char_sql(char_id, partner_id);
  1423. /* De-addopt [Zephyrus] */
  1424. if( father_id || mother_id )
  1425. { // Char is Baby
  1426. unsigned char buf[64];
  1427. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", schema_config.char_db, father_id, mother_id) )
  1428. Sql_ShowDebug(sql_handle);
  1429. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410'AND (`char_id`='%d' OR `char_id`='%d')", schema_config.skill_db, father_id, mother_id) )
  1430. Sql_ShowDebug(sql_handle);
  1431. WBUFW(buf,0) = 0x2b25;
  1432. WBUFL(buf,2) = father_id;
  1433. WBUFL(buf,6) = mother_id;
  1434. WBUFL(buf,10) = char_id; // Baby
  1435. chmapif_sendall(buf,14);
  1436. }
  1437. //Make the character leave the party [Skotlex]
  1438. if (party_id)
  1439. inter_party_leave(party_id, account_id, char_id);
  1440. /* delete char's pet */
  1441. //Delete the hatched pet if you have one...
  1442. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incubate` = '0'", schema_config.pet_db, char_id) )
  1443. Sql_ShowDebug(sql_handle);
  1444. //Delete all pets that are stored in eggs (inventory + cart)
  1445. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = 256", schema_config.pet_db, schema_config.pet_db, schema_config.inventory_db, schema_config.inventory_db, char_id) )
  1446. Sql_ShowDebug(sql_handle);
  1447. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = 256", schema_config.pet_db, schema_config.pet_db, schema_config.cart_db, schema_config.cart_db, char_id) )
  1448. Sql_ShowDebug(sql_handle);
  1449. /* remove homunculus */
  1450. if( hom_id )
  1451. mapif_homunculus_delete(hom_id);
  1452. /* remove elemental */
  1453. if (elemental_id)
  1454. mapif_elemental_delete(elemental_id);
  1455. /* remove mercenary data */
  1456. mercenary_owner_delete(char_id);
  1457. /* delete char's friends list */
  1458. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.friend_db, char_id) )
  1459. Sql_ShowDebug(sql_handle);
  1460. /* delete char from other's friend list */
  1461. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1462. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", schema_config.friend_db, char_id) )
  1463. Sql_ShowDebug(sql_handle);
  1464. #ifdef HOTKEY_SAVING
  1465. /* delete hotkeys */
  1466. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.hotkey_db, char_id) )
  1467. Sql_ShowDebug(sql_handle);
  1468. #endif
  1469. /* delete inventory */
  1470. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.inventory_db, char_id) )
  1471. Sql_ShowDebug(sql_handle);
  1472. /* delete cart inventory */
  1473. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.cart_db, char_id) )
  1474. Sql_ShowDebug(sql_handle);
  1475. /* delete memo areas */
  1476. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.memo_db, char_id) )
  1477. Sql_ShowDebug(sql_handle);
  1478. /* delete character registry */
  1479. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_str_table, char_id) )
  1480. Sql_ShowDebug(sql_handle);
  1481. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_num_table, char_id) )
  1482. Sql_ShowDebug(sql_handle);
  1483. /* delete skills */
  1484. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.skill_db, char_id) )
  1485. Sql_ShowDebug(sql_handle);
  1486. /* delete mails (only received) */
  1487. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `dest_id`='%d'", schema_config.mail_db, char_id))
  1488. Sql_ShowDebug(sql_handle);
  1489. #ifdef ENABLE_SC_SAVING
  1490. /* status changes */
  1491. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", schema_config.scdata_db, account_id, char_id) )
  1492. Sql_ShowDebug(sql_handle);
  1493. #endif
  1494. /* bonus_scripts */
  1495. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.bonus_script_db, char_id) )
  1496. Sql_ShowDebug(sql_handle);
  1497. if (charserv_config.log_char) {
  1498. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`char_msg`,`name`) VALUES (NOW(), '%d', '%d', 'Deleted char (CID %d)', '%s')",
  1499. schema_config.charlog_db, account_id, 0, char_id, esc_name) )
  1500. Sql_ShowDebug(sql_handle);
  1501. }
  1502. /* delete character */
  1503. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1504. Sql_ShowDebug(sql_handle);
  1505. /* No need as we used inter_guild_leave [Skotlex]
  1506. // Also delete info from guildtables.
  1507. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1508. Sql_ShowDebug(sql_handle);
  1509. */
  1510. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.guild_db, char_id) )
  1511. Sql_ShowDebug(sql_handle);
  1512. else if( Sql_NumRows(sql_handle) > 0 )
  1513. mapif_parse_BreakGuild(0,guild_id);
  1514. else if( guild_id )
  1515. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1516. return 0;
  1517. }
  1518. /**
  1519. * This function parse all map-serv attached to this char-serv and increase user count
  1520. * @return numbers of total users
  1521. */
  1522. int char_count_users(void)
  1523. {
  1524. int i, users;
  1525. users = 0;
  1526. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  1527. if (map_server[i].fd > 0) {
  1528. users += map_server[i].users;
  1529. }
  1530. }
  1531. return users;
  1532. }
  1533. // Writes char data to the buffer in the format used by the client.
  1534. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1535. // Returns the size
  1536. int char_mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p)
  1537. {
  1538. unsigned short offset = 0;
  1539. uint8* buf;
  1540. if( buffer == NULL || p == NULL )
  1541. return 0;
  1542. buf = WBUFP(buffer,0);
  1543. WBUFL(buf,0) = p->char_id;
  1544. WBUFL(buf,4) = umin(p->base_exp, INT32_MAX);
  1545. WBUFL(buf,8) = p->zeny;
  1546. WBUFL(buf,12) = umin(p->job_exp, INT32_MAX);
  1547. WBUFL(buf,16) = p->job_level;
  1548. WBUFL(buf,20) = 0; // probably opt1
  1549. WBUFL(buf,24) = 0; // probably opt2
  1550. WBUFL(buf,28) = p->option;
  1551. WBUFL(buf,32) = p->karma;
  1552. WBUFL(buf,36) = p->manner;
  1553. WBUFW(buf,40) = umin(p->status_point, INT16_MAX);
  1554. WBUFL(buf,42) = p->hp;
  1555. WBUFL(buf,46) = p->max_hp;
  1556. offset+=4;
  1557. buf = WBUFP(buffer,offset);
  1558. WBUFW(buf,46) = min(p->sp, INT16_MAX);
  1559. WBUFW(buf,48) = min(p->max_sp, INT16_MAX);
  1560. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1561. WBUFW(buf,52) = p->class_;
  1562. WBUFW(buf,54) = p->hair;
  1563. #if PACKETVER >= 20141022
  1564. WBUFW(buf,56) = p->body;
  1565. offset+=2;
  1566. buf = WBUFP(buffer,offset);
  1567. #endif
  1568. //When the weapon is sent and your option is riding, the client crashes on login!?
  1569. WBUFW(buf,56) = p->option&(0x20|0x80000|0x100000|0x200000|0x400000|0x800000|0x1000000|0x2000000|0x4000000|0x8000000) ? 0 : p->weapon;
  1570. WBUFW(buf,58) = p->base_level;
  1571. WBUFW(buf,60) = umin(p->skill_point, INT16_MAX);
  1572. WBUFW(buf,62) = p->head_bottom;
  1573. WBUFW(buf,64) = p->shield;
  1574. WBUFW(buf,66) = p->head_top;
  1575. WBUFW(buf,68) = p->head_mid;
  1576. WBUFW(buf,70) = p->hair_color;
  1577. WBUFW(buf,72) = p->clothes_color;
  1578. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1579. WBUFB(buf,98) = (unsigned char)u16min(p->str, UINT8_MAX);
  1580. WBUFB(buf,99) = (unsigned char)u16min(p->agi, UINT8_MAX);
  1581. WBUFB(buf,100) = (unsigned char)u16min(p->vit, UINT8_MAX);
  1582. WBUFB(buf,101) = (unsigned char)u16min(p->int_, UINT8_MAX);
  1583. WBUFB(buf,102) = (unsigned char)u16min(p->dex, UINT8_MAX);
  1584. WBUFB(buf,103) = (unsigned char)u16min(p->luk, UINT8_MAX);
  1585. WBUFW(buf,104) = p->slot;
  1586. WBUFW(buf,106) = ( p->rename > 0 ) ? 0 : 1;
  1587. offset += 2;
  1588. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1589. mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
  1590. offset += MAP_NAME_LENGTH_EXT;
  1591. #endif
  1592. #if PACKETVER >= 20100803
  1593. #if PACKETVER > 20130000 && PACKETVER < 20141016 || PACKETVER >= 20150826 && PACKETVER < 20151001
  1594. WBUFL(buf,124) = (p->delete_date?TOL(p->delete_date-time(NULL)):0);
  1595. #else
  1596. WBUFL(buf,124) = TOL(p->delete_date);
  1597. #endif
  1598. offset += 4;
  1599. #endif
  1600. #if PACKETVER >= 20110111
  1601. WBUFL(buf,128) = p->robe;
  1602. offset += 4;
  1603. #endif
  1604. #if PACKETVER != 20111116 //2011-11-16 wants 136, ask gravity.
  1605. #if PACKETVER >= 20110928
  1606. // change slot feature (0 = disabled, otherwise enabled)
  1607. if( (charserv_config.charmove_config.char_move_enabled)==0 )
  1608. WBUFL(buf,132) = 0;
  1609. else if( charserv_config.charmove_config.char_moves_unlimited )
  1610. WBUFL(buf,132) = 1;
  1611. else
  1612. WBUFL(buf,132) = max( 0, (int)p->character_moves );
  1613. offset += 4;
  1614. #endif
  1615. #if PACKETVER >= 20111025
  1616. WBUFL(buf,136) = ( p->rename > 0 ) ? 1 : 0; // (0 = disabled, otherwise displays "Add-Ons" sidebar)
  1617. offset += 4;
  1618. #endif
  1619. #if PACKETVER >= 20141016
  1620. WBUFB(buf,140) = p->sex;// sex - (0 = female, 1 = male, 99 = logindefined)
  1621. offset += 1;
  1622. #endif
  1623. #endif
  1624. return 106+offset;
  1625. }
  1626. int char_married(int pl1, int pl2)
  1627. {
  1628. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, pl1) )
  1629. Sql_ShowDebug(sql_handle);
  1630. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1631. {
  1632. char* data;
  1633. Sql_GetData(sql_handle, 0, &data, NULL);
  1634. if( pl2 == atoi(data) )
  1635. {
  1636. Sql_FreeResult(sql_handle);
  1637. return 1;
  1638. }
  1639. }
  1640. Sql_FreeResult(sql_handle);
  1641. return 0;
  1642. }
  1643. int char_child(int parent_id, int child_id)
  1644. {
  1645. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, parent_id) )
  1646. Sql_ShowDebug(sql_handle);
  1647. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1648. {
  1649. char* data;
  1650. Sql_GetData(sql_handle, 0, &data, NULL);
  1651. if( child_id == atoi(data) )
  1652. {
  1653. Sql_FreeResult(sql_handle);
  1654. return 1;
  1655. }
  1656. }
  1657. Sql_FreeResult(sql_handle);
  1658. return 0;
  1659. }
  1660. int char_family(int cid1, int cid2, int cid3)
  1661. {
  1662. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", schema_config.char_db, cid1, cid2, cid3) )
  1663. Sql_ShowDebug(sql_handle);
  1664. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1665. {
  1666. int charid;
  1667. int partnerid;
  1668. int childid;
  1669. char* data;
  1670. Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data);
  1671. Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data);
  1672. Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data);
  1673. if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) ||
  1674. (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) ||
  1675. (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) )
  1676. {
  1677. Sql_FreeResult(sql_handle);
  1678. return childid;
  1679. }
  1680. }
  1681. Sql_FreeResult(sql_handle);
  1682. return 0;
  1683. }
  1684. //----------------------------------------------------------------------
  1685. // Force disconnection of an online player (with account value) by [Yor]
  1686. //----------------------------------------------------------------------
  1687. void char_disconnect_player(uint32 account_id)
  1688. {
  1689. int i;
  1690. struct char_session_data* sd;
  1691. // disconnect player if online on char-server
  1692. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1693. if( i < fd_max )
  1694. set_eof(i);
  1695. }
  1696. /**
  1697. * Set 'flag' value of char_session_data
  1698. * @param account_id
  1699. * @param value
  1700. * @param set True: set the value by using '|= val', False: unset the value by using '&= ~val'
  1701. **/
  1702. void char_set_session_flag_(int account_id, int val, bool set) {
  1703. int i;
  1704. struct char_session_data* sd;
  1705. ARR_FIND(0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id);
  1706. if (i < fd_max) {
  1707. if (set)
  1708. sd->flag |= val;
  1709. else
  1710. sd->flag &= ~val;
  1711. }
  1712. }
  1713. void char_auth_ok(int fd, struct char_session_data *sd) {
  1714. struct online_char_data* character;
  1715. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1716. { // check if character is not online already. [Skotlex]
  1717. if (character->server > -1)
  1718. { //Character already online. KICK KICK KICK
  1719. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  1720. if (character->waiting_disconnect == INVALID_TIMER)
  1721. character->waiting_disconnect = add_timer(gettick()+20000, char_chardb_waiting_disconnect, character->account_id, 0);
  1722. chclif_send_auth_result(fd,8);
  1723. return;
  1724. }
  1725. if (character->fd >= 0 && character->fd != fd)
  1726. { //There's already a connection from this account that hasn't picked a char yet.
  1727. chclif_send_auth_result(fd,8);
  1728. return;
  1729. }
  1730. character->fd = fd;
  1731. }
  1732. chlogif_send_reqaccdata(login_fd,sd); // request account data
  1733. // mark session as 'authed'
  1734. sd->auth = true;
  1735. // set char online on charserver
  1736. char_set_charselect(sd->account_id);
  1737. // continues when account data is received...
  1738. }
  1739. void char_read_fame_list(void)
  1740. {
  1741. int i;
  1742. char* data;
  1743. size_t len;
  1744. // Empty ranking lists
  1745. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  1746. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  1747. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  1748. // Build Blacksmith ranking list
  1749. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, JOB_MECHANIC, JOB_MECHANIC_T, JOB_BABY_MECHANIC, fame_list_size_smith) )
  1750. Sql_ShowDebug(sql_handle);
  1751. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1752. {
  1753. // char_id
  1754. Sql_GetData(sql_handle, 0, &data, NULL);
  1755. smith_fame_list[i].id = atoi(data);
  1756. // fame
  1757. Sql_GetData(sql_handle, 1, &data, &len);
  1758. smith_fame_list[i].fame = atoi(data);
  1759. // name
  1760. Sql_GetData(sql_handle, 2, &data, &len);
  1761. memcpy(smith_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1762. }
  1763. // Build Alchemist ranking list
  1764. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, JOB_GENETIC, JOB_GENETIC_T, JOB_BABY_GENETIC, fame_list_size_chemist) )
  1765. Sql_ShowDebug(sql_handle);
  1766. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1767. {
  1768. // char_id
  1769. Sql_GetData(sql_handle, 0, &data, NULL);
  1770. chemist_fame_list[i].id = atoi(data);
  1771. // fame
  1772. Sql_GetData(sql_handle, 1, &data, &len);
  1773. chemist_fame_list[i].fame = atoi(data);
  1774. // name
  1775. Sql_GetData(sql_handle, 2, &data, &len);
  1776. memcpy(chemist_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1777. }
  1778. // Build Taekwon ranking list
  1779. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_TAEKWON, fame_list_size_taekwon) )
  1780. Sql_ShowDebug(sql_handle);
  1781. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1782. {
  1783. // char_id
  1784. Sql_GetData(sql_handle, 0, &data, NULL);
  1785. taekwon_fame_list[i].id = atoi(data);
  1786. // fame
  1787. Sql_GetData(sql_handle, 1, &data, &len);
  1788. taekwon_fame_list[i].fame = atoi(data);
  1789. // name
  1790. Sql_GetData(sql_handle, 2, &data, &len);
  1791. memcpy(taekwon_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1792. }
  1793. Sql_FreeResult(sql_handle);
  1794. }
  1795. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  1796. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  1797. int char_loadName(uint32 char_id, char* name){
  1798. char* data;
  1799. size_t len;
  1800. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1801. Sql_ShowDebug(sql_handle);
  1802. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1803. {
  1804. Sql_GetData(sql_handle, 0, &data, &len);
  1805. safestrncpy(name, data, NAME_LENGTH);
  1806. return 1;
  1807. }
  1808. else
  1809. {
  1810. safestrncpy(name, charserv_config.char_config.unknown_char_name, NAME_LENGTH);
  1811. }
  1812. return 0;
  1813. }
  1814. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  1815. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  1816. int char_search_mapserver(unsigned short map, uint32 ip, uint16 port){
  1817. int i, j;
  1818. for(i = 0; i < ARRAYLENGTH(map_server); i++)
  1819. {
  1820. if (map_server[i].fd > 0
  1821. && (ip == (uint32)-1 || map_server[i].ip == ip)
  1822. && (port == (uint16)-1 || map_server[i].port == port))
  1823. {
  1824. for (j = 0; map_server[i].map[j]; j++)
  1825. if (map_server[i].map[j] == map)
  1826. return i;
  1827. }
  1828. }
  1829. return -1;
  1830. }
  1831. /**
  1832. * Test to know if an IP come from LAN or WAN.
  1833. * @param ip: ip to check if in auth network
  1834. * @return 0 if from wan, or subnet_map_ip if lan
  1835. **/
  1836. int char_lan_subnetcheck(uint32 ip){
  1837. int i;
  1838. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  1839. if( i < subnet_count ) {
  1840. ShowInfo("Subnet check [%u.%u.%u.%u]: Matches "CL_CYAN"%u.%u.%u.%u/%u.%u.%u.%u"CL_RESET"\n", CONVIP(ip), CONVIP(subnet[i].char_ip & subnet[i].mask), CONVIP(subnet[i].mask));
  1841. return subnet[i].map_ip;
  1842. } else {
  1843. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", CONVIP(ip));
  1844. return 0;
  1845. }
  1846. }
  1847. // Console Command Parser [Wizputer]
  1848. //FIXME to be remove (moved to cnslif / will be done once map/char/login, all have their cnslif interface ready)
  1849. int parse_console(const char* buf){
  1850. return cnslif_parse(buf);
  1851. }
  1852. #if PACKETVER_SUPPORTS_PINCODE
  1853. //------------------------------------------------
  1854. //Pincode system
  1855. //------------------------------------------------
  1856. int char_pincode_compare( int fd, struct char_session_data* sd, char* pin ){
  1857. if( strcmp( sd->pincode, pin ) == 0 ){
  1858. sd->pincode_try = 0;
  1859. return 1;
  1860. }else{
  1861. chclif_pincode_sendstate( fd, sd, PINCODE_WRONG );
  1862. if( charserv_config.pincode_config.pincode_maxtry && ++sd->pincode_try >= charserv_config.pincode_config.pincode_maxtry ){
  1863. chlogif_pincode_notifyLoginPinError( sd->account_id );
  1864. }
  1865. return 0;
  1866. }
  1867. }
  1868. void char_pincode_decrypt( uint32 userSeed, char* pin ){
  1869. int i;
  1870. char tab[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  1871. char *buf;
  1872. for( i = 1; i < 10; i++ ){
  1873. int pos;
  1874. uint32 multiplier = 0x3498, baseSeed = 0x881234;
  1875. userSeed = baseSeed + userSeed * multiplier;
  1876. pos = userSeed % ( i + 1 );
  1877. if( i != pos ){
  1878. tab[i] ^= tab[pos];
  1879. tab[pos] ^= tab[i];
  1880. tab[i] ^= tab[pos];
  1881. }
  1882. }
  1883. buf = (char *)aMalloc( sizeof(char) * ( PINCODE_LENGTH + 1 ) );
  1884. memset( buf, 0, PINCODE_LENGTH + 1 );
  1885. for( i = 0; i < PINCODE_LENGTH; i++ ){
  1886. sprintf( buf + i, "%d", tab[pin[i] - '0'] );
  1887. }
  1888. strcpy( pin, buf );
  1889. aFree( buf );
  1890. }
  1891. #endif
  1892. //------------------------------------------------
  1893. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  1894. //replies/disconnect the player we tried to kick. [Skotlex]
  1895. //------------------------------------------------
  1896. int char_chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data)
  1897. {
  1898. struct online_char_data* character;
  1899. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  1900. { //Mark it offline due to timeout.
  1901. character->waiting_disconnect = INVALID_TIMER;
  1902. char_set_char_offline(character->char_id, character->account_id);
  1903. }
  1904. return 0;
  1905. }
  1906. /**
  1907. * @see DBApply
  1908. */
  1909. static int char_online_data_cleanup_sub(DBKey key, DBData *data, va_list ap)
  1910. {
  1911. struct online_char_data *character= (struct online_char_data *)db_data2ptr(data);
  1912. if (character->fd != -1)
  1913. return 0; //Character still connected
  1914. if (character->server == -2) //Unknown server.. set them offline
  1915. char_set_char_offline(character->char_id, character->account_id);
  1916. if (character->server < 0)
  1917. //Free data from players that have not been online for a while.
  1918. db_remove(online_char_db, key);
  1919. return 0;
  1920. }
  1921. static int char_online_data_cleanup(int tid, unsigned int tick, int id, intptr_t data){
  1922. online_char_db->foreach(online_char_db, char_online_data_cleanup_sub);
  1923. return 0;
  1924. }
  1925. //----------------------------------
  1926. // Reading Lan Support configuration
  1927. // Rewrote: Anvanced subnet check [LuzZza]
  1928. //----------------------------------
  1929. int char_lan_config_read(const char *lancfgName) {
  1930. FILE *fp;
  1931. int line_num = 0, s_subnet=ARRAYLENGTH(subnet);
  1932. char line[1024], w1[64], w2[64], w3[64], w4[64];
  1933. if((fp = fopen(lancfgName, "r")) == NULL) {
  1934. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  1935. return 1;
  1936. }
  1937. while(fgets(line, sizeof(line), fp)) {
  1938. line_num++;
  1939. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  1940. continue;
  1941. if(sscanf(line,"%63[^:]: %63[^:]:%63[^:]:%63[^\r\n]", w1, w2, w3, w4) != 4) {
  1942. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  1943. continue;
  1944. }
  1945. remove_control_chars(w1);
  1946. remove_control_chars(w2);
  1947. remove_control_chars(w3);
  1948. remove_control_chars(w4);
  1949. if( strcmpi(w1, "subnet") == 0 ){
  1950. if(subnet_count>=s_subnet) { //We skip instead of break in case we want to add other conf in that file.
  1951. ShowError("%s: Too many subnets defined, skipping line %d...\n", lancfgName, line_num);
  1952. continue;
  1953. }
  1954. subnet[subnet_count].mask = str2ip(w2);
  1955. subnet[subnet_count].char_ip = str2ip(w3);
  1956. subnet[subnet_count].map_ip = str2ip(w4);
  1957. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  1958. {
  1959. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  1960. continue;
  1961. }
  1962. subnet_count++;
  1963. }
  1964. }
  1965. if( subnet_count > 1 ) /* only useful if there is more than 1 */
  1966. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  1967. fclose(fp);
  1968. return 0;
  1969. }
  1970. /**
  1971. * Check if our table are all ok in sqlserver
  1972. * Char tables to check
  1973. * @return 0:fail, 1:success
  1974. */
  1975. bool char_checkdb(void){
  1976. int i;
  1977. const char* sqltable[] = {
  1978. schema_config.char_db, schema_config.hotkey_db, schema_config.scdata_db, schema_config.cart_db,
  1979. schema_config.inventory_db, schema_config.charlog_db, schema_config.storage_db,
  1980. schema_config.char_reg_str_table, schema_config.char_reg_num_table, schema_config.acc_reg_str_table,
  1981. schema_config.acc_reg_num_table, schema_config.skill_db, schema_config.interlog_db, schema_config.memo_db,
  1982. schema_config.guild_db, schema_config.guild_alliance_db, schema_config.guild_castle_db,
  1983. schema_config.guild_expulsion_db, schema_config.guild_member_db,
  1984. schema_config.guild_skill_db, schema_config.guild_position_db, schema_config.guild_storage_db,
  1985. schema_config.party_db, schema_config.pet_db, schema_config.friend_db, schema_config.mail_db,
  1986. schema_config.auction_db, schema_config.quest_db, schema_config.homunculus_db, schema_config.skill_homunculus_db,
  1987. schema_config.mercenary_db, schema_config.mercenary_owner_db,
  1988. schema_config.elemental_db, schema_config.ragsrvinfo_db, schema_config.skillcooldown_db, schema_config.bonus_script_db
  1989. };
  1990. ShowInfo("Start checking DB integrity\n");
  1991. for (i=0; i<ARRAYLENGTH(sqltable); i++){ //check if they all exist and we can acces them in sql-server
  1992. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` LIMIT 1;", sqltable[i]) )
  1993. return false;
  1994. }
  1995. //checking char_db
  1996. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,"
  1997. "`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,"
  1998. "`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,"
  1999. "`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,`hair_color`,`clothes_color`,`weapon`,"
  2000. "`shield`,`head_top`,`head_mid`,`head_bottom`,`robe`,`last_map`,`last_x`,`last_y`,`save_map`,"
  2001. "`save_x`,`save_y`,`partner_id`,`online`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,"
  2002. "`moves`,`unban_time`,`font`"
  2003. #if PACKETVER >= 20141016
  2004. ",`sex`"
  2005. #endif
  2006. " FROM `%s` LIMIT 1;", schema_config.char_db) ){
  2007. Sql_ShowDebug(sql_handle);
  2008. return false;
  2009. }
  2010. //checking charlog_db
  2011. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`char_msg`,`account_id`,`char_num`,`name`,"
  2012. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`"
  2013. " FROM `%s` LIMIT 1;", schema_config.charlog_db) ){
  2014. Sql_ShowDebug(sql_handle);
  2015. return false;
  2016. }
  2017. //checking char_reg_str_table
  2018. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_str_table) ) {
  2019. Sql_ShowDebug(sql_handle);
  2020. return false;
  2021. }
  2022. //checking char_reg_num_table
  2023. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_num_table) ) {
  2024. Sql_ShowDebug(sql_handle);
  2025. return false;
  2026. }
  2027. //checking global_acc_reg_str_table
  2028. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_str_table) ) {
  2029. Sql_ShowDebug(sql_handle);
  2030. return false;
  2031. }
  2032. //checking global_acc_reg_num_table
  2033. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_num_table) ) {
  2034. Sql_ShowDebug(sql_handle);
  2035. return false;
  2036. }
  2037. //checking hotkey_db
  2038. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`hotkey`,`type`,`itemskill_id`,`skill_lvl`"
  2039. " FROM `%s` LIMIT 1;", schema_config.hotkey_db) ){
  2040. Sql_ShowDebug(sql_handle);
  2041. return false;
  2042. }
  2043. //checking scdata_db
  2044. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`type`,`tick`,`val1`,`val2`,`val3`,`val4`"
  2045. " FROM `%s` LIMIT 1;", schema_config.scdata_db) ){
  2046. Sql_ShowDebug(sql_handle);
  2047. return false;
  2048. }
  2049. //checking skill_db
  2050. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`id`,`lv`,`flag` FROM `%s` LIMIT 1;", schema_config.skill_db) ){
  2051. Sql_ShowDebug(sql_handle);
  2052. return false;
  2053. }
  2054. //checking interlog_db
  2055. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`log` FROM `%s` LIMIT 1;", schema_config.interlog_db) ){
  2056. Sql_ShowDebug(sql_handle);
  2057. return false;
  2058. }
  2059. //checking memo_db
  2060. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `memo_id`,`char_id`,`map`,`x`,`y` FROM `%s` LIMIT 1;", schema_config.memo_db) ){
  2061. Sql_ShowDebug(sql_handle);
  2062. return false;
  2063. }
  2064. //checking guild_db
  2065. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`name`,`char_id`,`master`,`guild_lv`,"
  2066. "`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`mes1`,`mes2`,"
  2067. "`emblem_len`,`emblem_id`,`emblem_data`"
  2068. " FROM `%s` LIMIT 1;", schema_config.guild_db) ){
  2069. Sql_ShowDebug(sql_handle);
  2070. return false;
  2071. }
  2072. //checking guild_alliance_db
  2073. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`opposition`,`alliance_id`,`name` FROM `%s` LIMIT 1;", schema_config.guild_alliance_db) ){
  2074. Sql_ShowDebug(sql_handle);
  2075. return false;
  2076. }
  2077. //checking guild_castle_db
  2078. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `castle_id`,`guild_id`,`economy`,`defense`,`triggerE`,"
  2079. "`triggerD`,`nextTime`,`payTime`,`createTime`,`visibleC`,`visibleG0`,`visibleG1`,`visibleG2`,"
  2080. "`visibleG3`,`visibleG4`,`visibleG5`,`visibleG6`,`visibleG7` "
  2081. " FROM `%s` LIMIT 1;", schema_config.guild_castle_db) ){
  2082. Sql_ShowDebug(sql_handle);
  2083. return false;
  2084. }
  2085. //checking guild_expulsion_db
  2086. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`account_id`,`name`,`mes` FROM `%s` LIMIT 1;", schema_config.guild_expulsion_db) ){
  2087. Sql_ShowDebug(sql_handle);
  2088. return false;
  2089. }
  2090. //checking guild_member_db
  2091. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`account_id`,`char_id`,`hair`,"
  2092. "`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name`"
  2093. " FROM `%s` LIMIT 1;", schema_config.guild_member_db) ){
  2094. Sql_ShowDebug(sql_handle);
  2095. return false;
  2096. }
  2097. //checking guild_skill_db
  2098. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.guild_skill_db) ){
  2099. Sql_ShowDebug(sql_handle);
  2100. return false;
  2101. }
  2102. //checking guild_position_db
  2103. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`position`,`name`,`mode`,`exp_mode` FROM `%s` LIMIT 1;", schema_config.guild_position_db) ){
  2104. Sql_ShowDebug(sql_handle);
  2105. return false;
  2106. }
  2107. //checking party_db
  2108. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `party_id`,`name`,`exp`,`item`,`leader_id`,`leader_char` FROM `%s` LIMIT 1;", schema_config.party_db) ){
  2109. Sql_ShowDebug(sql_handle);
  2110. return false;
  2111. }
  2112. //checking pet_db
  2113. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `pet_id`,`class`,`name`,`account_id`,`char_id`,`level`,"
  2114. "`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`"
  2115. " FROM `%s` LIMIT 1;", schema_config.pet_db) ){
  2116. Sql_ShowDebug(sql_handle);
  2117. return false;
  2118. }
  2119. //checking friend_db
  2120. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`friend_account`,`friend_id` FROM `%s` LIMIT 1;", schema_config.friend_db) ){
  2121. Sql_ShowDebug(sql_handle);
  2122. return false;
  2123. }
  2124. //checking mail_db
  2125. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,"
  2126. "`title`,`message`,`time`,`status`,`zeny`,`nameid`,`amount`,`refine`,`attribute`,`identify`,"
  2127. "`card0`,`card1`,`card2`,`card3`,`unique_id`, `bound`"
  2128. " FROM `%s` LIMIT 1;", schema_config.mail_db) ){
  2129. Sql_ShowDebug(sql_handle);
  2130. return false;
  2131. }
  2132. //checking auction_db
  2133. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `auction_id`,`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,"
  2134. "`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`card0`,`card1`,"
  2135. "`card2`,`card3`,`unique_id` "
  2136. "FROM `%s` LIMIT 1;", schema_config.auction_db) ){
  2137. Sql_ShowDebug(sql_handle);
  2138. return false;
  2139. }
  2140. //checking quest_db
  2141. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`quest_id`,`state`,`time`,`count1`,`count2`,`count3` from `%s`;", schema_config.quest_db) ){
  2142. Sql_ShowDebug(sql_handle);
  2143. return false;
  2144. }
  2145. //checking homunculus_db
  2146. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`char_id`,`class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`,"
  2147. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`alive`,`rename_flag`,`vaporize` "
  2148. " FROM `%s` LIMIT 1;", schema_config.homunculus_db) ){
  2149. Sql_ShowDebug(sql_handle);
  2150. return false;
  2151. }
  2152. //checking skill_homunculus_db
  2153. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.skill_homunculus_db) ){
  2154. Sql_ShowDebug(sql_handle);
  2155. return false;
  2156. }
  2157. //checking mercenary_db
  2158. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `mer_id`,`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time` FROM `%s` LIMIT 1;", schema_config.mercenary_db) ){
  2159. Sql_ShowDebug(sql_handle);
  2160. return false;
  2161. }
  2162. //checking mercenary_owner_db
  2163. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`merc_id`,`arch_calls`,`arch_faith`,"
  2164. "`spear_calls`,`spear_faith`,`sword_calls`,`sword_faith`"
  2165. " FROM `%s` LIMIT 1;", schema_config.mercenary_owner_db) ){
  2166. Sql_ShowDebug(sql_handle);
  2167. return false;
  2168. }
  2169. //checking elemental_db
  2170. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `ele_id`,`char_id`,`class`,`mode`,`hp`,`sp`,`max_hp`,`max_sp`,"
  2171. "`atk1`,`atk2`,`matk`,`aspd`,`def`,`mdef`,`flee`,`hit`,`life_time` "
  2172. " FROM `%s` LIMIT 1;", schema_config.elemental_db) ){
  2173. Sql_ShowDebug(sql_handle);
  2174. return false;
  2175. }
  2176. //checking ragsrvinfo_db
  2177. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `index`,`name`,`exp`,`jexp`,`drop` FROM `%s` LIMIT 1;", schema_config.ragsrvinfo_db) ){
  2178. Sql_ShowDebug(sql_handle);
  2179. return false;
  2180. }
  2181. //checking skillcooldown_db
  2182. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`skill`,`tick` FROM `%s` LIMIT 1;", schema_config.skillcooldown_db) ){
  2183. Sql_ShowDebug(sql_handle);
  2184. return false;
  2185. }
  2186. //checking bonus_script_db
  2187. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`script`,`tick`,`flag`,`type`,`icon` FROM `%s` LIMIT 1;", schema_config.bonus_script_db) ){
  2188. Sql_ShowDebug(sql_handle);
  2189. return false;
  2190. }
  2191. //checking cart_db
  2192. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2193. "`attribute`,`card0`,`card1`,`card2`,`card3`,`expire_time`,`bound`,`unique_id`"
  2194. " FROM `%s` LIMIT 1;", schema_config.cart_db) ){
  2195. Sql_ShowDebug(sql_handle);
  2196. return false;
  2197. }
  2198. //checking inventory_db
  2199. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2200. "`attribute`,`card0`,`card1`,`card2`,`card3`,`expire_time`,`favorite`,`bound`,`unique_id`"
  2201. " FROM `%s` LIMIT 1;", schema_config.inventory_db) ){
  2202. Sql_ShowDebug(sql_handle);
  2203. return false;
  2204. }
  2205. //checking storage_db
  2206. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`account_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2207. "`attribute`,`card0`,`card1`,`card2`,`card3`,`expire_time`,`bound`,`unique_id`"
  2208. " FROM `%s` LIMIT 1;", schema_config.storage_db) ){
  2209. Sql_ShowDebug(sql_handle);
  2210. return false;
  2211. }
  2212. //checking guild_storage_db
  2213. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`guild_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2214. "`attribute`,`card0`,`card1`,`card2`,`card3`,`expire_time`,`bound`,`unique_id`"
  2215. " FROM `%s` LIMIT 1;", schema_config.guild_storage_db) ){
  2216. Sql_ShowDebug(sql_handle);
  2217. return false;
  2218. }
  2219. Sql_FreeResult(sql_handle);
  2220. ShowInfo("DB integrity check finished with success\n");
  2221. return true;
  2222. }
  2223. void char_sql_config_read(const char* cfgName) {
  2224. char line[1024], w1[1024], w2[1024];
  2225. FILE* fp;
  2226. if ((fp = fopen(cfgName, "r")) == NULL) {
  2227. ShowError("File not found: %s\n", cfgName);
  2228. return;
  2229. }
  2230. while(fgets(line, sizeof(line), fp)) {
  2231. if(line[0] == '/' && line[1] == '/')
  2232. continue;
  2233. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2234. continue;
  2235. if(!strcmpi(w1,"char_db"))
  2236. safestrncpy(schema_config.char_db, w2, sizeof(schema_config.char_db));
  2237. else if(!strcmpi(w1,"scdata_db"))
  2238. safestrncpy(schema_config.scdata_db, w2, sizeof(schema_config.scdata_db));
  2239. else if(!strcmpi(w1,"cart_db"))
  2240. safestrncpy(schema_config.cart_db, w2, sizeof(schema_config.cart_db));
  2241. else if(!strcmpi(w1,"inventory_db"))
  2242. safestrncpy(schema_config.inventory_db, w2, sizeof(schema_config.inventory_db));
  2243. else if(!strcmpi(w1,"charlog_db"))
  2244. safestrncpy(schema_config.charlog_db, w2, sizeof(schema_config.charlog_db));
  2245. else if(!strcmpi(w1,"storage_db"))
  2246. safestrncpy(schema_config.storage_db, w2, sizeof(schema_config.storage_db));
  2247. else if(!strcmpi(w1,"skill_db"))
  2248. safestrncpy(schema_config.skill_db, w2, sizeof(schema_config.skill_db));
  2249. else if(!strcmpi(w1,"interlog_db"))
  2250. safestrncpy(schema_config.interlog_db, w2, sizeof(schema_config.interlog_db));
  2251. else if(!strcmpi(w1,"memo_db"))
  2252. safestrncpy(schema_config.memo_db, w2, sizeof(schema_config.memo_db));
  2253. else if(!strcmpi(w1,"guild_db"))
  2254. safestrncpy(schema_config.guild_db, w2, sizeof(schema_config.guild_db));
  2255. else if(!strcmpi(w1,"guild_alliance_db"))
  2256. safestrncpy(schema_config.guild_alliance_db, w2, sizeof(schema_config.guild_alliance_db));
  2257. else if(!strcmpi(w1,"guild_castle_db"))
  2258. safestrncpy(schema_config.guild_castle_db, w2, sizeof(schema_config.guild_castle_db));
  2259. else if(!strcmpi(w1,"guild_expulsion_db"))
  2260. safestrncpy(schema_config.guild_expulsion_db, w2, sizeof(schema_config.guild_expulsion_db));
  2261. else if(!strcmpi(w1,"guild_member_db"))
  2262. safestrncpy(schema_config.guild_member_db, w2, sizeof(schema_config.guild_member_db));
  2263. else if(!strcmpi(w1,"guild_skill_db"))
  2264. safestrncpy(schema_config.guild_skill_db, w2, sizeof(schema_config.guild_skill_db));
  2265. else if(!strcmpi(w1,"guild_position_db"))
  2266. safestrncpy(schema_config.guild_position_db, w2, sizeof(schema_config.guild_position_db));
  2267. else if(!strcmpi(w1,"guild_storage_db"))
  2268. safestrncpy(schema_config.guild_storage_db, w2, sizeof(schema_config.guild_storage_db));
  2269. else if(!strcmpi(w1,"party_db"))
  2270. safestrncpy(schema_config.party_db, w2, sizeof(schema_config.party_db));
  2271. else if(!strcmpi(w1,"pet_db"))
  2272. safestrncpy(schema_config.pet_db, w2, sizeof(schema_config.pet_db));
  2273. else if(!strcmpi(w1,"mail_db"))
  2274. safestrncpy(schema_config.mail_db, w2, sizeof(schema_config.mail_db));
  2275. else if(!strcmpi(w1,"auction_db"))
  2276. safestrncpy(schema_config.auction_db, w2, sizeof(schema_config.auction_db));
  2277. else if(!strcmpi(w1,"friend_db"))
  2278. safestrncpy(schema_config.friend_db, w2, sizeof(schema_config.friend_db));
  2279. else if(!strcmpi(w1,"hotkey_db"))
  2280. safestrncpy(schema_config.hotkey_db, w2, sizeof(schema_config.hotkey_db));
  2281. else if(!strcmpi(w1,"quest_db"))
  2282. safestrncpy(schema_config.quest_db,w2,sizeof(schema_config.quest_db));
  2283. else if(!strcmpi(w1,"homunculus_db"))
  2284. safestrncpy(schema_config.homunculus_db,w2,sizeof(schema_config.homunculus_db));
  2285. else if(!strcmpi(w1,"skill_homunculus_db"))
  2286. safestrncpy(schema_config.skill_homunculus_db,w2,sizeof(schema_config.skill_homunculus_db));
  2287. else if(!strcmpi(w1,"mercenary_db"))
  2288. safestrncpy(schema_config.mercenary_db,w2,sizeof(schema_config.mercenary_db));
  2289. else if(!strcmpi(w1,"mercenary_owner_db"))
  2290. safestrncpy(schema_config.mercenary_owner_db,w2,sizeof(schema_config.mercenary_owner_db));
  2291. else if(!strcmpi(w1,"elemental_db"))
  2292. safestrncpy(schema_config.elemental_db,w2,sizeof(schema_config.elemental_db));
  2293. else if(!strcmpi(w1,"skillcooldown_db"))
  2294. safestrncpy(schema_config.skillcooldown_db, w2, sizeof(schema_config.skillcooldown_db));
  2295. else if(!strcmpi(w1,"bonus_script_db"))
  2296. safestrncpy(schema_config.bonus_script_db, w2, sizeof(schema_config.bonus_script_db));
  2297. else if(!strcmpi(w1,"char_reg_num_table"))
  2298. safestrncpy(schema_config.char_reg_num_table, w2, sizeof(schema_config.char_reg_num_table));
  2299. else if(!strcmpi(w1,"char_reg_str_table"))
  2300. safestrncpy(schema_config.char_reg_str_table, w2, sizeof(schema_config.char_reg_str_table));
  2301. else if(!strcmpi(w1,"acc_reg_str_table"))
  2302. safestrncpy(schema_config.acc_reg_str_table, w2, sizeof(schema_config.acc_reg_str_table));
  2303. else if(!strcmpi(w1,"acc_reg_num_table"))
  2304. safestrncpy(schema_config.acc_reg_num_table, w2, sizeof(schema_config.acc_reg_num_table));
  2305. //support the import command, just like any other config
  2306. else if(!strcmpi(w1,"import"))
  2307. char_sql_config_read(w2);
  2308. }
  2309. fclose(fp);
  2310. ShowInfo("Done reading %s.\n", cfgName);
  2311. }
  2312. void char_set_default_sql(){
  2313. // schema_config.db_use_sqldbs;
  2314. safestrncpy(schema_config.db_path,"db",sizeof(schema_config.db_path));
  2315. safestrncpy(schema_config.char_db,"char",sizeof(schema_config.char_db));
  2316. safestrncpy(schema_config.scdata_db,"sc_data",sizeof(schema_config.scdata_db));
  2317. safestrncpy(schema_config.cart_db,"cart_inventory",sizeof(schema_config.cart_db));
  2318. safestrncpy(schema_config.inventory_db,"inventory",sizeof(schema_config.inventory_db));
  2319. safestrncpy(schema_config.charlog_db,"charlog",sizeof(schema_config.charlog_db));
  2320. safestrncpy(schema_config.storage_db,"storage",sizeof(schema_config.storage_db));
  2321. safestrncpy(schema_config.interlog_db,"interlog",sizeof(schema_config.interlog_db));
  2322. safestrncpy(schema_config.skill_db,"skill",sizeof(schema_config.skill_db));
  2323. safestrncpy(schema_config.memo_db,"memo",sizeof(schema_config.memo_db));
  2324. safestrncpy(schema_config.guild_db,"guild",sizeof(schema_config.guild_db));
  2325. safestrncpy(schema_config.guild_alliance_db,"guild_alliance",sizeof(schema_config.guild_alliance_db));
  2326. safestrncpy(schema_config.guild_castle_db,"guild_castle",sizeof(schema_config.guild_castle_db));
  2327. safestrncpy(schema_config.guild_expulsion_db,"guild_expulsion",sizeof(schema_config.guild_expulsion_db));
  2328. safestrncpy(schema_config.guild_member_db,"guild_member",sizeof(schema_config.guild_member_db));
  2329. safestrncpy(schema_config.guild_position_db,"guild_position",sizeof(schema_config.guild_position_db));
  2330. safestrncpy(schema_config.guild_skill_db,"guild_skill",sizeof(schema_config.guild_skill_db));
  2331. safestrncpy(schema_config.guild_storage_db,"guild_storage",sizeof(schema_config.guild_storage_db));
  2332. safestrncpy(schema_config.party_db,"party",sizeof(schema_config.party_db));
  2333. safestrncpy(schema_config.pet_db,"pet",sizeof(schema_config.pet_db));
  2334. safestrncpy(schema_config.mail_db,"mail",sizeof(schema_config.mail_db)); // MAIL SYSTEM
  2335. safestrncpy(schema_config.auction_db,"auction",sizeof(schema_config.auction_db)); // Auctions System
  2336. safestrncpy(schema_config.friend_db,"friends",sizeof(schema_config.friend_db));
  2337. safestrncpy(schema_config.hotkey_db,"hotkey",sizeof(schema_config.hotkey_db));
  2338. safestrncpy(schema_config.quest_db,"quest",sizeof(schema_config.quest_db));
  2339. safestrncpy(schema_config.homunculus_db,"homunculus",sizeof(schema_config.homunculus_db));
  2340. safestrncpy(schema_config.skill_homunculus_db,"skill_homunculus",sizeof(schema_config.skill_homunculus_db));
  2341. safestrncpy(schema_config.mercenary_db,"mercenary",sizeof(schema_config.mercenary_db));
  2342. safestrncpy(schema_config.mercenary_owner_db,"mercenary_owner",sizeof(schema_config.mercenary_owner_db));
  2343. safestrncpy(schema_config.ragsrvinfo_db,"ragsrvinfo",sizeof(schema_config.ragsrvinfo_db));
  2344. safestrncpy(schema_config.skillcooldown_db,"skillcooldown",sizeof(schema_config.skillcooldown_db));
  2345. safestrncpy(schema_config.bonus_script_db,"bonus_script",sizeof(schema_config.bonus_script_db));
  2346. safestrncpy(schema_config.char_reg_num_table,"char_reg_num",sizeof(schema_config.char_reg_num_table));
  2347. safestrncpy(schema_config.char_reg_str_table,"char_reg_str",sizeof(schema_config.char_reg_str_table));
  2348. safestrncpy(schema_config.acc_reg_str_table,"acc_reg_str",sizeof(schema_config.acc_reg_str_table));
  2349. safestrncpy(schema_config.acc_reg_num_table,"acc_reg_num",sizeof(schema_config.acc_reg_num_table));
  2350. }
  2351. //set default config
  2352. void char_set_defaults(){
  2353. #if PACKETVER_SUPPORTS_PINCODE
  2354. charserv_config.pincode_config.pincode_enabled = true;
  2355. charserv_config.pincode_config.pincode_changetime = 0;
  2356. charserv_config.pincode_config.pincode_maxtry = 3;
  2357. charserv_config.pincode_config.pincode_force = true;
  2358. charserv_config.pincode_config.pincode_allow_repeated = false;
  2359. charserv_config.pincode_config.pincode_allow_sequential = false;
  2360. #endif
  2361. charserv_config.charmove_config.char_move_enabled = true;
  2362. charserv_config.charmove_config.char_movetoused = true;
  2363. charserv_config.charmove_config.char_moves_unlimited = false;
  2364. charserv_config.char_config.char_per_account = 0; //Maximum chars per account (default unlimited) [Sirius]
  2365. charserv_config.char_config.char_del_level = 0; //From which level u can delete character [Lupus]
  2366. charserv_config.char_config.char_del_delay = 86400;
  2367. #if PACKETVER >= 20100803
  2368. charserv_config.char_config.char_del_option = 2;
  2369. #else
  2370. charserv_config.char_config.char_del_option = 1;
  2371. #endif
  2372. // charserv_config.userid[24];
  2373. // charserv_config.passwd[24];
  2374. // charserv_config.server_name[20];
  2375. safestrncpy(charserv_config.wisp_server_name,"Server",sizeof(charserv_config.wisp_server_name));
  2376. // charserv_config.login_ip_str[128];
  2377. charserv_config.login_ip = 0;
  2378. charserv_config.login_port = 6900;
  2379. // charserv_config.char_ip_str[128];
  2380. charserv_config.char_ip = 0;
  2381. // charserv_config.bind_ip_str[128];
  2382. charserv_config.bind_ip = INADDR_ANY;
  2383. charserv_config.char_port = 6121;
  2384. charserv_config.char_maintenance = 0;
  2385. charserv_config.char_new = true;
  2386. charserv_config.char_new_display = 0;
  2387. charserv_config.char_config.name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  2388. charserv_config.char_config.char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
  2389. safestrncpy(charserv_config.char_config.unknown_char_name,"Unknown",sizeof(charserv_config.char_config.unknown_char_name)); // Name to use when the requested name cannot be determined
  2390. safestrncpy(charserv_config.char_config.char_name_letters,"",sizeof(charserv_config.char_config.char_name_letters)); // list of letters/symbols allowed (or not) in a character name. by [Yor]
  2391. charserv_config.save_log = 1; // show loading/saving messages
  2392. charserv_config.log_char = 1; // loggin char or not [devil]
  2393. charserv_config.log_inter = 1; // loggin inter or not [devil]
  2394. charserv_config.char_check_db =1;
  2395. // See const.h to change the default values
  2396. charserv_config.start_point[0].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2397. charserv_config.start_point[0].x = MAP_DEFAULT_X;
  2398. charserv_config.start_point[0].y = MAP_DEFAULT_Y;
  2399. charserv_config.start_point_count = 1;
  2400. #if PACKETVER >= 20151001
  2401. charserv_config.start_point_doram[0].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2402. charserv_config.start_point_doram[0].x = MAP_DEFAULT_X;
  2403. charserv_config.start_point_doram[0].y = MAP_DEFAULT_Y;
  2404. charserv_config.start_point_count_doram = 1;
  2405. #endif
  2406. charserv_config.start_items[0].nameid = 1201;
  2407. charserv_config.start_items[0].amount = 1;
  2408. charserv_config.start_items[0].pos = 2;
  2409. charserv_config.start_items[1].nameid = 2301;
  2410. charserv_config.start_items[1].amount = 1;
  2411. charserv_config.start_items[1].pos = 16;
  2412. #if PACKETVER >= 20150101
  2413. charserv_config.start_items_doram[0].nameid = 1681;
  2414. charserv_config.start_items_doram[0].amount = 1;
  2415. charserv_config.start_items_doram[0].pos = 2;
  2416. charserv_config.start_items_doram[1].nameid = 2301;
  2417. charserv_config.start_items_doram[1].amount = 1;
  2418. charserv_config.start_items_doram[1].pos = 16;
  2419. #endif
  2420. charserv_config.console = 0;
  2421. charserv_config.max_connect_user = -1;
  2422. charserv_config.gm_allow_group = -1;
  2423. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2424. charserv_config.start_zeny = 0;
  2425. charserv_config.guild_exp_rate = 100;
  2426. safestrncpy(charserv_config.default_map, "prontera", MAP_NAME_LENGTH);
  2427. charserv_config.default_map_x = 156;
  2428. charserv_config.default_map_y = 191;
  2429. }
  2430. /**
  2431. * Split start_point configuration values.
  2432. * @param w1_value: Value from w1
  2433. * @param w2_value: Value from w2
  2434. * @param start: Start point reference
  2435. * @param count: Start point count reference
  2436. */
  2437. static void char_config_split_startpoint(char *w1_value, char *w2_value, struct point start_point[MAX_STARTPOINT], short *count)
  2438. {
  2439. char *lineitem, **fields;
  2440. int i = 0, fields_length = 3 + 1;
  2441. (*count) = 0; // Reset to begin reading
  2442. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2443. if (fields == NULL)
  2444. return; // Failed to allocate memory.
  2445. lineitem = strtok(w2_value, ":");
  2446. while (lineitem != NULL && (*count) < MAX_STARTPOINT) {
  2447. int n = sv_split(lineitem, strlen(lineitem), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE);
  2448. if (n + 1 < fields_length) {
  2449. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2450. lineitem = strtok(NULL, ":"); //next lineitem
  2451. continue;
  2452. }
  2453. start_point[i].map = mapindex_name2id(fields[1]);
  2454. if (!start_point[i].map) {
  2455. ShowError("Start point %s not found in map-index cache. Setting to default location.\n", start_point[i].map);
  2456. start_point[i].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2457. start_point[i].x = MAP_DEFAULT_X;
  2458. start_point[i].y = MAP_DEFAULT_Y;
  2459. } else {
  2460. start_point[i].x = max(0, atoi(fields[2]));
  2461. start_point[i].y = max(0, atoi(fields[3]));
  2462. }
  2463. (*count)++;
  2464. lineitem = strtok(NULL, ":"); //next lineitem
  2465. i++;
  2466. }
  2467. aFree(fields);
  2468. }
  2469. /**
  2470. * Split start_items configuration values.
  2471. * @param w1_value: Value from w1
  2472. * @param w2_value: Value from w2
  2473. * @param start: Start item reference
  2474. */
  2475. static void char_config_split_startitem(char *w1_value, char *w2_value, struct startitem start_items[MAX_STARTITEM])
  2476. {
  2477. char *lineitem, **fields;
  2478. int i = 0, fields_length = 3 + 1;
  2479. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2480. if (fields == NULL)
  2481. return; // Failed to allocate memory.
  2482. lineitem = strtok(w2_value, ":");
  2483. while (lineitem != NULL && i < MAX_STARTITEM) {
  2484. int n = sv_split(lineitem, strlen(lineitem), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE);
  2485. if (n + 1 < fields_length) {
  2486. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2487. lineitem = strtok(NULL, ":"); //next lineitem
  2488. continue;
  2489. }
  2490. // TODO: Item ID verification
  2491. start_items[i].nameid = max(0, atoi(fields[1]));
  2492. start_items[i].amount = max(0, atoi(fields[2]));
  2493. start_items[i].pos = max(0, atoi(fields[3]));
  2494. lineitem = strtok(NULL, ":"); //next lineitem
  2495. i++;
  2496. }
  2497. aFree(fields);
  2498. }
  2499. bool char_config_read(const char* cfgName, bool normal){
  2500. char line[1024], w1[1024], w2[1024];
  2501. FILE* fp = fopen(cfgName, "r");
  2502. if (fp == NULL) {
  2503. ShowError("Configuration file not found: %s.\n", cfgName);
  2504. return false;
  2505. }
  2506. while(fgets(line, sizeof(line), fp)) {
  2507. if (line[0] == '/' && line[1] == '/')
  2508. continue;
  2509. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2510. continue;
  2511. remove_control_chars(w1);
  2512. remove_control_chars(w2);
  2513. // Config that loaded only when server started, not by reloading config file
  2514. if (normal) {
  2515. if (strcmpi(w1, "userid") == 0) {
  2516. safestrncpy(charserv_config.userid, w2, sizeof(charserv_config.userid));
  2517. } else if (strcmpi(w1, "passwd") == 0) {
  2518. safestrncpy(charserv_config.passwd, w2, sizeof(charserv_config.passwd));
  2519. } else if (strcmpi(w1, "server_name") == 0) {
  2520. safestrncpy(charserv_config.server_name, w2, sizeof(charserv_config.server_name));
  2521. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  2522. if (strlen(w2) >= 4) {
  2523. safestrncpy(charserv_config.wisp_server_name, w2, sizeof(charserv_config.wisp_server_name));
  2524. }
  2525. } else if (strcmpi(w1, "login_ip") == 0) {
  2526. charserv_config.login_ip = host2ip(w2);
  2527. if (charserv_config.login_ip) {
  2528. char ip_str[16];
  2529. safestrncpy(charserv_config.login_ip_str, w2, sizeof(charserv_config.login_ip_str));
  2530. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(charserv_config.login_ip, ip_str));
  2531. }
  2532. } else if (strcmpi(w1, "login_port") == 0) {
  2533. charserv_config.login_port = atoi(w2);
  2534. } else if (strcmpi(w1, "char_ip") == 0) {
  2535. charserv_config.char_ip = host2ip(w2);
  2536. if (charserv_config.char_ip) {
  2537. char ip_str[16];
  2538. safestrncpy(charserv_config.char_ip_str, w2, sizeof(charserv_config.char_ip_str));
  2539. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(charserv_config.char_ip, ip_str));
  2540. }
  2541. } else if (strcmpi(w1, "bind_ip") == 0) {
  2542. charserv_config.bind_ip = host2ip(w2);
  2543. if (charserv_config.bind_ip) {
  2544. char ip_str[16];
  2545. safestrncpy(charserv_config.bind_ip_str, w2, sizeof(charserv_config.bind_ip_str));
  2546. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(charserv_config.bind_ip, ip_str));
  2547. }
  2548. } else if (strcmpi(w1, "char_port") == 0) {
  2549. charserv_config.char_port = atoi(w2);
  2550. } else if (strcmpi(w1, "console") == 0) {
  2551. charserv_config.console = config_switch(w2);
  2552. }
  2553. }
  2554. if(strcmpi(w1,"timestamp_format") == 0) {
  2555. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  2556. } else if(strcmpi(w1,"console_silent")==0){
  2557. msg_silent = atoi(w2);
  2558. if( msg_silent ) /* only bother if its actually enabled */
  2559. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  2560. } else if (strcmpi(w1, "console_msg_log") == 0) {
  2561. console_msg_log = atoi(w2);
  2562. } else if (strcmpi(w1, "console_log_filepath") == 0) {
  2563. safestrncpy(console_log_filepath, w2, sizeof(console_log_filepath));
  2564. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  2565. stdout_with_ansisequence = config_switch(w2);
  2566. } else if (strcmpi(w1, "char_maintenance") == 0) {
  2567. charserv_config.char_maintenance = atoi(w2);
  2568. } else if (strcmpi(w1, "char_new") == 0) {
  2569. charserv_config.char_new = (bool)atoi(w2);
  2570. } else if (strcmpi(w1, "char_new_display") == 0) {
  2571. charserv_config.char_new_display = atoi(w2);
  2572. } else if (strcmpi(w1, "max_connect_user") == 0) {
  2573. charserv_config.max_connect_user = atoi(w2);
  2574. if (charserv_config.max_connect_user < -1)
  2575. charserv_config.max_connect_user = -1;
  2576. } else if(strcmpi(w1, "gm_allow_group") == 0) {
  2577. charserv_config.gm_allow_group = atoi(w2);
  2578. } else if (strcmpi(w1, "autosave_time") == 0) {
  2579. charserv_config.autosave_interval = atoi(w2)*1000;
  2580. if (charserv_config.autosave_interval <= 0)
  2581. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2582. } else if (strcmpi(w1, "save_log") == 0) {
  2583. charserv_config.save_log = config_switch(w2);
  2584. #ifdef RENEWAL
  2585. } else if (strcmpi(w1, "start_point") == 0) {
  2586. #else
  2587. } else if (strcmpi(w1, "start_point_pre") == 0) {
  2588. #endif
  2589. char_config_split_startpoint(w1, w2, charserv_config.start_point, &charserv_config.start_point_count);
  2590. #if PACKETVER >= 20151001
  2591. } else if (strcmpi(w1, "start_point_doram") == 0) {
  2592. char_config_split_startpoint(w1, w2, charserv_config.start_point_doram, &charserv_config.start_point_count_doram);
  2593. #endif
  2594. } else if (strcmpi(w1, "start_zeny") == 0) {
  2595. charserv_config.start_zeny = atoi(w2);
  2596. if (charserv_config.start_zeny < 0)
  2597. charserv_config.start_zeny = 0;
  2598. } else if (strcmpi(w1, "start_items") == 0) {
  2599. char_config_split_startitem(w1, w2, charserv_config.start_items);
  2600. #if PACKETVER >= 20151001
  2601. } else if (strcmpi(w1, "start_items_doram") == 0) {
  2602. char_config_split_startitem(w1, w2, charserv_config.start_items_doram);
  2603. #endif
  2604. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  2605. charserv_config.log_char = atoi(w2);
  2606. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  2607. safestrncpy(charserv_config.char_config.unknown_char_name, w2, sizeof(charserv_config.char_config.unknown_char_name));
  2608. charserv_config.char_config.unknown_char_name[NAME_LENGTH-1] = '\0';
  2609. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  2610. charserv_config.char_config.name_ignoring_case = (bool)config_switch(w2);
  2611. } else if (strcmpi(w1, "char_name_option") == 0) {
  2612. charserv_config.char_config.char_name_option = atoi(w2);
  2613. } else if (strcmpi(w1, "char_name_letters") == 0) {
  2614. safestrncpy(charserv_config.char_config.char_name_letters, w2, sizeof(charserv_config.char_config.char_name_letters));
  2615. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  2616. charserv_config.char_config.char_del_level = atoi(w2);
  2617. } else if (strcmpi(w1, "char_del_delay") == 0) {
  2618. charserv_config.char_config.char_del_delay = atoi(w2);
  2619. } else if (strcmpi(w1, "char_del_option") == 0) {
  2620. charserv_config.char_config.char_del_option = atoi(w2);
  2621. } else if(strcmpi(w1,"db_path")==0) {
  2622. safestrncpy(schema_config.db_path, w2, sizeof(schema_config.db_path));
  2623. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  2624. fame_list_size_chemist = atoi(w2);
  2625. if (fame_list_size_chemist > MAX_FAME_LIST) {
  2626. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  2627. fame_list_size_chemist = MAX_FAME_LIST;
  2628. }
  2629. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  2630. fame_list_size_smith = atoi(w2);
  2631. if (fame_list_size_smith > MAX_FAME_LIST) {
  2632. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  2633. fame_list_size_smith = MAX_FAME_LIST;
  2634. }
  2635. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  2636. fame_list_size_taekwon = atoi(w2);
  2637. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  2638. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  2639. fame_list_size_taekwon = MAX_FAME_LIST;
  2640. }
  2641. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  2642. charserv_config.guild_exp_rate = atoi(w2);
  2643. } else if (strcmpi(w1, "pincode_enabled") == 0) {
  2644. #if PACKETVER_SUPPORTS_PINCODE
  2645. charserv_config.pincode_config.pincode_enabled = config_switch(w2);
  2646. } else if (strcmpi(w1, "pincode_changetime") == 0) {
  2647. charserv_config.pincode_config.pincode_changetime = atoi(w2)*60*60*24;
  2648. } else if (strcmpi(w1, "pincode_maxtry") == 0) {
  2649. charserv_config.pincode_config.pincode_maxtry = atoi(w2);
  2650. } else if (strcmpi(w1, "pincode_force") == 0) {
  2651. charserv_config.pincode_config.pincode_force = config_switch(w2);
  2652. } else if (strcmpi(w1, "pincode_allow_repeated") == 0) {
  2653. charserv_config.pincode_config.pincode_allow_repeated = config_switch(w2);
  2654. } else if (strcmpi(w1, "pincode_allow_sequential") == 0) {
  2655. charserv_config.pincode_config.pincode_allow_sequential = config_switch(w2);
  2656. #else
  2657. if( config_switch(w2) ) {
  2658. ShowWarning("pincode_enabled requires PACKETVER 20110309 or higher.\n");
  2659. }
  2660. #endif
  2661. } else if (strcmpi(w1, "char_move_enabled") == 0) {
  2662. charserv_config.charmove_config.char_move_enabled = config_switch(w2);
  2663. } else if (strcmpi(w1, "char_movetoused") == 0) {
  2664. charserv_config.charmove_config.char_movetoused = config_switch(w2);
  2665. } else if (strcmpi(w1, "char_moves_unlimited") == 0) {
  2666. charserv_config.charmove_config.char_moves_unlimited = config_switch(w2);
  2667. } else if (strcmpi(w1, "char_checkdb") == 0) {
  2668. charserv_config.char_check_db = config_switch(w2);
  2669. } else if (strcmpi(w1, "default_map") == 0) {
  2670. safestrncpy(charserv_config.default_map, w2, MAP_NAME_LENGTH);
  2671. } else if (strcmpi(w1, "default_map_x") == 0) {
  2672. charserv_config.default_map_x = atoi(w2);
  2673. } else if (strcmpi(w1, "default_map_y") == 0) {
  2674. charserv_config.default_map_y = atoi(w2);
  2675. } else if (strcmpi(w1, "import") == 0) {
  2676. char_config_read(w2, normal);
  2677. }
  2678. }
  2679. fclose(fp);
  2680. ShowInfo("Done reading %s.\n", cfgName);
  2681. return true;
  2682. }
  2683. /*
  2684. * Message conf function
  2685. */
  2686. int char_msg_config_read(char *cfgName){
  2687. return _msg_config_read(cfgName,CHAR_MAX_MSG,msg_table);
  2688. }
  2689. const char* char_msg_txt(int msg_number){
  2690. return _msg_txt(msg_number,CHAR_MAX_MSG,msg_table);
  2691. }
  2692. void char_do_final_msg(void){
  2693. _do_final_msg(CHAR_MAX_MSG,msg_table);
  2694. }
  2695. void do_final(void)
  2696. {
  2697. ShowStatus("Terminating...\n");
  2698. char_set_all_offline(-1);
  2699. char_set_all_offline_sql();
  2700. inter_final();
  2701. flush_fifos();
  2702. do_final_msg();
  2703. do_final_chmapif();
  2704. do_final_chlogif();
  2705. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s`", schema_config.ragsrvinfo_db) )
  2706. Sql_ShowDebug(sql_handle);
  2707. char_db_->destroy(char_db_, NULL);
  2708. online_char_db->destroy(online_char_db, NULL);
  2709. auth_db->destroy(auth_db, NULL);
  2710. if( char_fd != -1 )
  2711. {
  2712. do_close(char_fd);
  2713. char_fd = -1;
  2714. }
  2715. Sql_Free(sql_handle);
  2716. mapindex_final();
  2717. ShowStatus("Finished.\n");
  2718. }
  2719. void set_server_type(void){
  2720. SERVER_TYPE = ATHENA_SERVER_CHAR;
  2721. }
  2722. //------------------------------
  2723. // Function called when the server
  2724. // has received a crash signal.
  2725. //------------------------------
  2726. void do_abort(void)
  2727. {
  2728. }
  2729. /// Called when a terminate signal is received.
  2730. void do_shutdown(void) {
  2731. if( runflag != CHARSERVER_ST_SHUTDOWN )
  2732. {
  2733. int id;
  2734. runflag = CHARSERVER_ST_SHUTDOWN;
  2735. ShowStatus("Shutting down...\n");
  2736. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  2737. for( id = 0; id < ARRAYLENGTH(map_server); ++id )
  2738. chmapif_server_reset(id);
  2739. chlogif_check_shutdown();
  2740. flush_fifos();
  2741. runflag = CORE_ST_STOP;
  2742. }
  2743. }
  2744. int do_init(int argc, char **argv)
  2745. {
  2746. //Read map indexes
  2747. runflag = CHARSERVER_ST_STARTING;
  2748. mapindex_init();
  2749. // Init default value
  2750. CHAR_CONF_NAME = "conf/char_athena.conf";
  2751. LAN_CONF_NAME = "conf/subnet_athena.conf";
  2752. SQL_CONF_NAME = "conf/inter_athena.conf";
  2753. MSG_CONF_NAME_EN = "conf/msg_conf/char_msg.conf";
  2754. safestrncpy(console_log_filepath, "./log/char-msg_log.log", sizeof(console_log_filepath));
  2755. cli_get_options(argc,argv);
  2756. char_set_defaults();
  2757. char_config_read(CHAR_CONF_NAME, true);
  2758. char_lan_config_read(LAN_CONF_NAME);
  2759. char_set_default_sql();
  2760. char_sql_config_read(SQL_CONF_NAME);
  2761. msg_config_read(MSG_CONF_NAME_EN);
  2762. if (strcmp(charserv_config.userid, "s1")==0 && strcmp(charserv_config.passwd, "p1")==0) {
  2763. ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  2764. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  2765. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  2766. }
  2767. inter_init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server configuration
  2768. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  2769. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  2770. char_mmo_sql_init();
  2771. char_read_fame_list(); //Read fame lists.
  2772. if ((naddr_ != 0) && (!(charserv_config.login_ip) || !(charserv_config.char_ip) ))
  2773. {
  2774. char ip_str[16];
  2775. ip2str(addr_[0], ip_str);
  2776. if (naddr_ > 1)
  2777. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  2778. else
  2779. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  2780. if (!(charserv_config.login_ip) ) {
  2781. safestrncpy(charserv_config.login_ip_str, ip_str, sizeof(charserv_config.login_ip_str));
  2782. charserv_config.login_ip = str2ip(charserv_config.login_ip_str);
  2783. }
  2784. if (!(charserv_config.char_ip)) {
  2785. safestrncpy(charserv_config.char_ip_str, ip_str, sizeof(charserv_config.char_ip_str));
  2786. charserv_config.char_ip = str2ip(charserv_config.char_ip_str);
  2787. }
  2788. }
  2789. do_init_chlogif();
  2790. do_init_chmapif();
  2791. // periodically update the overall user count on all mapservers + login server
  2792. add_timer_func_list(chlogif_broadcast_user_count, "broadcast_user_count");
  2793. add_timer_interval(gettick() + 1000, chlogif_broadcast_user_count, 0, 0, 5 * 1000);
  2794. // Timer to clear (online_char_db)
  2795. add_timer_func_list(char_chardb_waiting_disconnect, "chardb_waiting_disconnect");
  2796. // Online Data timers (checking if char still connected)
  2797. add_timer_func_list(char_online_data_cleanup, "online_data_cleanup");
  2798. add_timer_interval(gettick() + 1000, char_online_data_cleanup, 0, 0, 600 * 1000);
  2799. //check db tables
  2800. if(charserv_config.char_check_db && char_checkdb() == 0){
  2801. ShowFatalError("char : A tables is missing in sql-server, please fix it, see (sql-files main.sql for structure) \n");
  2802. exit(EXIT_FAILURE);
  2803. }
  2804. //Cleaning the tables for NULL entrys @ startup [Sirius]
  2805. //Chardb clean
  2806. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", schema_config.char_db) )
  2807. Sql_ShowDebug(sql_handle);
  2808. //guilddb clean
  2809. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", schema_config.guild_db) )
  2810. Sql_ShowDebug(sql_handle);
  2811. //guildmemberdb clean
  2812. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", schema_config.guild_member_db) )
  2813. Sql_ShowDebug(sql_handle);
  2814. set_defaultparse(chclif_parse);
  2815. if( (char_fd = make_listen_bind(charserv_config.bind_ip,charserv_config.char_port)) == -1 ) {
  2816. ShowFatalError("Failed to bind to port '"CL_WHITE"%d"CL_RESET"'\n",charserv_config.char_port);
  2817. exit(EXIT_FAILURE);
  2818. }
  2819. if( runflag != CORE_ST_STOP )
  2820. {
  2821. shutdown_callback = do_shutdown;
  2822. runflag = CHARSERVER_ST_RUNNING;
  2823. }
  2824. do_init_chcnslif();
  2825. mapindex_check_mapdefault(charserv_config.default_map);
  2826. ShowInfo("Default map: '"CL_WHITE"%s %d,%d"CL_RESET"'\n", charserv_config.default_map, charserv_config.default_map_x, charserv_config.default_map_y);
  2827. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", charserv_config.char_port);
  2828. return 0;
  2829. }