char.cpp 135 KB

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