char.cpp 132 KB

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