char.c 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h"
  5. #include "../common/db.h"
  6. #include "../common/malloc.h"
  7. #include "../common/mapindex.h"
  8. #include "../common/mmo.h"
  9. #include "../common/showmsg.h"
  10. #include "../common/socket.h"
  11. #include "../common/strlib.h"
  12. #include "../common/timer.h"
  13. #include "../common/utils.h"
  14. #include "int_guild.h"
  15. #include "int_homun.h"
  16. #include "int_mercenary.h"
  17. #include "int_elemental.h"
  18. #include "int_party.h"
  19. #include "int_storage.h"
  20. #include "char.h"
  21. #include "inter.h"
  22. #include <sys/types.h>
  23. #include <time.h>
  24. #include <signal.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. // private declarations
  30. #define CHAR_CONF_NAME "conf/char_athena.conf"
  31. #define LAN_CONF_NAME "conf/subnet_athena.conf"
  32. #define SQL_CONF_NAME "conf/inter_athena.conf"
  33. char char_db[256] = "char";
  34. char scdata_db[256] = "sc_data";
  35. char cart_db[256] = "cart_inventory";
  36. char inventory_db[256] = "inventory";
  37. char charlog_db[256] = "charlog";
  38. char storage_db[256] = "storage";
  39. char interlog_db[256] = "interlog";
  40. char reg_db[256] = "global_reg_value";
  41. char skill_db[256] = "skill";
  42. char memo_db[256] = "memo";
  43. char guild_db[256] = "guild";
  44. char guild_alliance_db[256] = "guild_alliance";
  45. char guild_castle_db[256] = "guild_castle";
  46. char guild_expulsion_db[256] = "guild_expulsion";
  47. char guild_member_db[256] = "guild_member";
  48. char guild_position_db[256] = "guild_position";
  49. char guild_skill_db[256] = "guild_skill";
  50. char guild_storage_db[256] = "guild_storage";
  51. char party_db[256] = "party";
  52. char pet_db[256] = "pet";
  53. char mail_db[256] = "mail"; // MAIL SYSTEM
  54. char auction_db[256] = "auction"; // Auctions System
  55. char friend_db[256] = "friends";
  56. char hotkey_db[256] = "hotkey";
  57. char quest_db[256] = "quest";
  58. char homunculus_db[256] = "homunculus";
  59. char skill_homunculus_db[256] = "skill_homunculus";
  60. char mercenary_db[256] = "mercenary";
  61. char mercenary_owner_db[256] = "mercenary_owner";
  62. char ragsrvinfo_db[256] = "ragsrvinfo";
  63. // show loading/saving messages
  64. int save_log = 1;
  65. static DBMap* char_db_; // int char_id -> struct mmo_charstatus*
  66. char db_path[1024] = "db";
  67. int db_use_sqldbs;
  68. struct mmo_map_server {
  69. int fd;
  70. uint32 ip;
  71. uint16 port;
  72. int users;
  73. unsigned short map[MAX_MAP_PER_SERVER];
  74. } server[MAX_MAP_SERVERS];
  75. int login_fd=-1, char_fd=-1;
  76. char userid[24];
  77. char passwd[24];
  78. char server_name[20];
  79. char wisp_server_name[NAME_LENGTH] = "Server";
  80. char login_ip_str[128];
  81. uint32 login_ip = 0;
  82. uint16 login_port = 6900;
  83. char char_ip_str[128];
  84. uint32 char_ip = 0;
  85. char bind_ip_str[128];
  86. uint32 bind_ip = INADDR_ANY;
  87. uint16 char_port = 6121;
  88. int char_maintenance = 0;
  89. bool char_new = true;
  90. int char_new_display = 0;
  91. bool name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  92. int 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]
  93. char unknown_char_name[NAME_LENGTH] = "Unknown"; // Name to use when the requested name cannot be determined
  94. #define TRIM_CHARS "\255\xA0\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  95. char char_name_letters[1024] = ""; // list of letters/symbols allowed (or not) in a character name. by [Yor]
  96. int char_per_account = 0; //Maximum chars per account (default unlimited) [Sirius]
  97. int char_del_level = 0; //From which level u can delete character [Lupus]
  98. int char_del_delay = 86400;
  99. int log_char = 1; // loggin char or not [devil]
  100. int log_inter = 1; // loggin inter or not [devil]
  101. // Advanced subnet check [LuzZza]
  102. struct s_subnet {
  103. uint32 mask;
  104. uint32 char_ip;
  105. uint32 map_ip;
  106. } subnet[16];
  107. int subnet_count = 0;
  108. struct char_session_data {
  109. bool auth; // whether the session is authed or not
  110. int account_id, login_id1, login_id2, sex;
  111. int found_char[MAX_CHARS]; // ids of chars on this account
  112. char email[40]; // e-mail (default: a@a.com) by [Yor]
  113. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  114. int group_id; // permission
  115. uint32 version;
  116. uint8 clienttype;
  117. char new_name[NAME_LENGTH];
  118. char birthdate[10+1]; // YYYY-MM-DD
  119. };
  120. int max_connect_user = 0;
  121. int gm_allow_group = -1;
  122. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  123. int start_zeny = 0;
  124. int start_weapon = 1201;
  125. int start_armor = 2301;
  126. int guild_exp_rate = 100;
  127. //Custom limits for the fame lists. [Skotlex]
  128. int fame_list_size_chemist = MAX_FAME_LIST;
  129. int fame_list_size_smith = MAX_FAME_LIST;
  130. int fame_list_size_taekwon = MAX_FAME_LIST;
  131. // Char-server-side stored fame lists [DracoRPG]
  132. struct fame_list smith_fame_list[MAX_FAME_LIST];
  133. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  134. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  135. // check for exit signal
  136. // 0 is saving complete
  137. // other is char_id
  138. unsigned int save_flag = 0;
  139. // Initial position (it's possible to set it in conf file)
  140. struct point start_point = { 0, 53, 111 };
  141. int console = 0;
  142. //-----------------------------------------------------
  143. // Auth database
  144. //-----------------------------------------------------
  145. #define AUTH_TIMEOUT 30000
  146. struct auth_node {
  147. int account_id;
  148. int char_id;
  149. uint32 login_id1;
  150. uint32 login_id2;
  151. uint32 ip;
  152. int sex;
  153. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  154. int group_id;
  155. unsigned changing_mapservers : 1;
  156. };
  157. static DBMap* auth_db; // int account_id -> struct auth_node*
  158. //-----------------------------------------------------
  159. // Online User Database
  160. //-----------------------------------------------------
  161. struct online_char_data {
  162. int account_id;
  163. int char_id;
  164. int fd;
  165. int waiting_disconnect;
  166. short server; // -2: unknown server, -1: not connected, 0+: id of server
  167. };
  168. static DBMap* online_char_db; // int account_id -> struct online_char_data*
  169. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data);
  170. int delete_char_sql(int char_id);
  171. /**
  172. * @see DBCreateData
  173. */
  174. static DBData create_online_char_data(DBKey key, va_list args)
  175. {
  176. struct online_char_data* character;
  177. CREATE(character, struct online_char_data, 1);
  178. character->account_id = key.i;
  179. character->char_id = -1;
  180. character->server = -1;
  181. character->fd = -1;
  182. character->waiting_disconnect = INVALID_TIMER;
  183. return db_ptr2data(character);
  184. }
  185. void set_char_charselect(int account_id)
  186. {
  187. struct online_char_data* character;
  188. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  189. if( character->server > -1 )
  190. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  191. server[character->server].users--;
  192. character->char_id = -1;
  193. character->server = -1;
  194. if(character->waiting_disconnect != INVALID_TIMER) {
  195. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  196. character->waiting_disconnect = INVALID_TIMER;
  197. }
  198. if (login_fd > 0 && !session[login_fd]->flag.eof)
  199. {
  200. WFIFOHEAD(login_fd,6);
  201. WFIFOW(login_fd,0) = 0x272b;
  202. WFIFOL(login_fd,2) = account_id;
  203. WFIFOSET(login_fd,6);
  204. }
  205. }
  206. void set_char_online(int map_id, int char_id, int account_id)
  207. {
  208. struct online_char_data* character;
  209. struct mmo_charstatus *cp;
  210. //Update DB
  211. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) )
  212. Sql_ShowDebug(sql_handle);
  213. //Check to see for online conflicts
  214. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  215. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  216. {
  217. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  218. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  219. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  220. }
  221. //Update state data
  222. character->char_id = char_id;
  223. character->server = map_id;
  224. if( character->server > -1 )
  225. server[character->server].users++;
  226. //Get rid of disconnect timer
  227. if(character->waiting_disconnect != INVALID_TIMER) {
  228. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  229. character->waiting_disconnect = INVALID_TIMER;
  230. }
  231. //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  232. cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  233. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  234. //Notify login server
  235. if (login_fd > 0 && !session[login_fd]->flag.eof)
  236. {
  237. WFIFOHEAD(login_fd,6);
  238. WFIFOW(login_fd,0) = 0x272b;
  239. WFIFOL(login_fd,2) = account_id;
  240. WFIFOSET(login_fd,6);
  241. }
  242. }
  243. void set_char_offline(int char_id, int account_id)
  244. {
  245. struct online_char_data* character;
  246. if ( char_id == -1 )
  247. {
  248. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id) )
  249. Sql_ShowDebug(sql_handle);
  250. }
  251. else
  252. {
  253. struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  254. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  255. if (cp)
  256. idb_remove(char_db_,char_id);
  257. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) )
  258. Sql_ShowDebug(sql_handle);
  259. }
  260. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  261. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  262. if( character->server > -1 )
  263. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  264. server[character->server].users--;
  265. if(character->waiting_disconnect != INVALID_TIMER){
  266. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  267. character->waiting_disconnect = INVALID_TIMER;
  268. }
  269. if(character->char_id == char_id)
  270. {
  271. character->char_id = -1;
  272. character->server = -1;
  273. }
  274. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  275. }
  276. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  277. if (login_fd > 0 && !session[login_fd]->flag.eof && (char_id == -1 || character == NULL || character->fd == -1))
  278. {
  279. WFIFOHEAD(login_fd,6);
  280. WFIFOW(login_fd,0) = 0x272c;
  281. WFIFOL(login_fd,2) = account_id;
  282. WFIFOSET(login_fd,6);
  283. }
  284. }
  285. /**
  286. * @see DBApply
  287. */
  288. static int char_db_setoffline(DBKey key, DBData *data, va_list ap)
  289. {
  290. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  291. int server = va_arg(ap, int);
  292. if (server == -1) {
  293. character->char_id = -1;
  294. character->server = -1;
  295. if(character->waiting_disconnect != INVALID_TIMER){
  296. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  297. character->waiting_disconnect = INVALID_TIMER;
  298. }
  299. } else if (character->server == server)
  300. character->server = -2; //In some map server that we aren't connected to.
  301. return 0;
  302. }
  303. /**
  304. * @see DBApply
  305. */
  306. static int char_db_kickoffline(DBKey key, DBData *data, va_list ap)
  307. {
  308. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  309. int server_id = va_arg(ap, int);
  310. if (server_id > -1 && character->server != server_id)
  311. return 0;
  312. //Kick out any connected characters, and set them offline as appropriate.
  313. if (character->server > -1)
  314. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1);
  315. else if (character->waiting_disconnect == INVALID_TIMER)
  316. set_char_offline(character->char_id, character->account_id);
  317. else
  318. return 0; // fail
  319. return 1;
  320. }
  321. void set_all_offline(int id)
  322. {
  323. if (id < 0)
  324. ShowNotice("Sending all users offline.\n");
  325. else
  326. ShowNotice("Sending users of map-server %d offline.\n",id);
  327. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  328. if (id >= 0 || login_fd <= 0 || session[login_fd]->flag.eof)
  329. return;
  330. //Tell login-server to also mark all our characters as offline.
  331. WFIFOHEAD(login_fd,2);
  332. WFIFOW(login_fd,0) = 0x2737;
  333. WFIFOSET(login_fd,2);
  334. }
  335. void set_all_offline_sql(void)
  336. {
  337. //Set all players to 'OFFLINE'
  338. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", char_db) )
  339. Sql_ShowDebug(sql_handle);
  340. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", guild_member_db) )
  341. Sql_ShowDebug(sql_handle);
  342. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", guild_db) )
  343. Sql_ShowDebug(sql_handle);
  344. }
  345. /**
  346. * @see DBCreateData
  347. */
  348. static DBData create_charstatus(DBKey key, va_list args)
  349. {
  350. struct mmo_charstatus *cp;
  351. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  352. cp->char_id = key.i;
  353. return db_ptr2data(cp);
  354. }
  355. int inventory_to_sql(const struct item items[], int max, int id);
  356. int mmo_char_tosql(int char_id, struct mmo_charstatus* p)
  357. {
  358. int i = 0;
  359. int count = 0;
  360. int diff = 0;
  361. char save_status[128]; //For displaying save information. [Skotlex]
  362. struct mmo_charstatus *cp;
  363. int errors = 0; //If there are any errors while saving, "cp" will not be updated at the end.
  364. StringBuf buf;
  365. if (char_id!=p->char_id) return 0;
  366. cp = idb_ensure(char_db_, char_id, create_charstatus);
  367. StringBuf_Init(&buf);
  368. memset(save_status, 0, sizeof(save_status));
  369. //map inventory data
  370. if( memcmp(p->inventory, cp->inventory, sizeof(p->inventory)) ) {
  371. if (!inventory_to_sql(p->inventory, MAX_INVENTORY, p->char_id))
  372. strcat(save_status, " inventory");
  373. else
  374. errors++;
  375. }
  376. //map cart data
  377. if( memcmp(p->cart, cp->cart, sizeof(p->cart)) ) {
  378. if (!memitemdata_to_sql(p->cart, MAX_CART, p->char_id, TABLE_CART))
  379. strcat(save_status, " cart");
  380. else
  381. errors++;
  382. }
  383. //map storage data
  384. if( memcmp(p->storage.items, cp->storage.items, sizeof(p->storage.items)) ) {
  385. if (!memitemdata_to_sql(p->storage.items, MAX_STORAGE, p->account_id, TABLE_STORAGE))
  386. strcat(save_status, " storage");
  387. else
  388. errors++;
  389. }
  390. if (
  391. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  392. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  393. (p->zeny != cp->zeny) ||
  394. (p->last_point.map != cp->last_point.map) ||
  395. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  396. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  397. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  398. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  399. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  400. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  401. (p->option != cp->option) ||
  402. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  403. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) ||
  404. (p->ele_id != cp->ele_id) || (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  405. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom) || (p->delete_date != cp->delete_date) ||
  406. (p->rename != cp->rename) || (p->robe != cp->robe)
  407. )
  408. { //Save status
  409. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  410. "`base_exp`='%u', `job_exp`='%u', `zeny`='%d',"
  411. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  412. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  413. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',`elemental_id`='%d',"
  414. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  415. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d',"
  416. "`delete_date`='%lu',`robe`='%d'"
  417. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  418. char_db, p->base_level, p->job_level,
  419. p->base_exp, p->job_exp, p->zeny,
  420. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  421. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  422. p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, p->ele_id,
  423. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  424. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  425. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename,
  426. (unsigned long)p->delete_date, // FIXME: platform-dependent size
  427. p->robe,
  428. p->account_id, p->char_id) )
  429. {
  430. Sql_ShowDebug(sql_handle);
  431. errors++;
  432. } else
  433. strcat(save_status, " status");
  434. }
  435. //Values that will seldom change (to speed up saving)
  436. if (
  437. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  438. (p->class_ != cp->class_) ||
  439. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  440. (p->mother != cp->mother) || (p->child != cp->child) ||
  441. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  442. (p->fame != cp->fame)
  443. )
  444. {
  445. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',"
  446. "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',"
  447. "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d',"
  448. "`karma`='%d',`manner`='%d', `fame`='%d'"
  449. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  450. char_db, p->class_,
  451. p->hair, p->hair_color, p->clothes_color,
  452. p->partner_id, p->father, p->mother, p->child,
  453. p->karma, p->manner, p->fame,
  454. p->account_id, p->char_id) )
  455. {
  456. Sql_ShowDebug(sql_handle);
  457. errors++;
  458. } else
  459. strcat(save_status, " status2");
  460. }
  461. /* Mercenary Owner */
  462. if( (p->mer_id != cp->mer_id) ||
  463. (p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) ||
  464. (p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) ||
  465. (p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) )
  466. {
  467. if (mercenary_owner_tosql(char_id, p))
  468. strcat(save_status, " mercenary");
  469. else
  470. errors++;
  471. }
  472. //memo points
  473. if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
  474. {
  475. char esc_mapname[NAME_LENGTH*2+1];
  476. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  477. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, p->char_id) )
  478. {
  479. Sql_ShowDebug(sql_handle);
  480. errors++;
  481. }
  482. //insert here.
  483. StringBuf_Clear(&buf);
  484. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db);
  485. for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
  486. {
  487. if( p->memo_point[i].map )
  488. {
  489. if( count )
  490. StringBuf_AppendStr(&buf, ",");
  491. Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
  492. StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
  493. ++count;
  494. }
  495. }
  496. if( count )
  497. {
  498. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  499. {
  500. Sql_ShowDebug(sql_handle);
  501. errors++;
  502. }
  503. }
  504. strcat(save_status, " memo");
  505. }
  506. //FIXME: is this neccessary? [ultramage]
  507. for(i=0;i<MAX_SKILL;i++)
  508. if ((p->skill[i].lv != 0) && (p->skill[i].id == 0))
  509. p->skill[i].id = i; // Fix skill tree
  510. //skills
  511. if( memcmp(p->skill, cp->skill, sizeof(p->skill)) )
  512. {
  513. //`skill` (`char_id`, `id`, `lv`)
  514. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, p->char_id) )
  515. {
  516. Sql_ShowDebug(sql_handle);
  517. errors++;
  518. }
  519. StringBuf_Clear(&buf);
  520. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`) VALUES ", skill_db);
  521. //insert here.
  522. for( i = 0, count = 0; i < MAX_SKILL; ++i )
  523. {
  524. if( p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY )
  525. {
  526. if( p->skill[i].flag == SKILL_FLAG_PERMANENT && p->skill[i].lv == 0 )
  527. continue;
  528. if( p->skill[i].flag != SKILL_FLAG_PERMANENT && (p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0) == 0 )
  529. continue;
  530. if( count )
  531. StringBuf_AppendStr(&buf, ",");
  532. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->skill[i].id, (p->skill[i].flag == SKILL_FLAG_PERMANENT ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0));
  533. ++count;
  534. }
  535. }
  536. if( count )
  537. {
  538. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  539. {
  540. Sql_ShowDebug(sql_handle);
  541. errors++;
  542. }
  543. }
  544. strcat(save_status, " skills");
  545. }
  546. diff = 0;
  547. for(i = 0; i < MAX_FRIENDS; i++){
  548. if(p->friends[i].char_id != cp->friends[i].char_id ||
  549. p->friends[i].account_id != cp->friends[i].account_id){
  550. diff = 1;
  551. break;
  552. }
  553. }
  554. if(diff == 1)
  555. { //Save friends
  556. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id) )
  557. {
  558. Sql_ShowDebug(sql_handle);
  559. errors++;
  560. }
  561. StringBuf_Clear(&buf);
  562. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db);
  563. for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
  564. {
  565. if( p->friends[i].char_id > 0 )
  566. {
  567. if( count )
  568. StringBuf_AppendStr(&buf, ",");
  569. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id);
  570. count++;
  571. }
  572. }
  573. if( count )
  574. {
  575. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  576. {
  577. Sql_ShowDebug(sql_handle);
  578. errors++;
  579. }
  580. }
  581. strcat(save_status, " friends");
  582. }
  583. #ifdef HOTKEY_SAVING
  584. // hotkeys
  585. StringBuf_Clear(&buf);
  586. StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", hotkey_db);
  587. diff = 0;
  588. for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
  589. if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
  590. {
  591. if( diff )
  592. StringBuf_AppendStr(&buf, ",");// not the first hotkey
  593. 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);
  594. diff = 1;
  595. }
  596. }
  597. if(diff) {
  598. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  599. {
  600. Sql_ShowDebug(sql_handle);
  601. errors++;
  602. } else
  603. strcat(save_status, " hotkeys");
  604. }
  605. #endif
  606. StringBuf_Destroy(&buf);
  607. if (save_status[0]!='\0' && save_log)
  608. ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
  609. if (!errors)
  610. memcpy(cp, p, sizeof(struct mmo_charstatus));
  611. return 0;
  612. }
  613. /// Saves an array of 'item' entries into the specified table.
  614. int memitemdata_to_sql(const struct item items[], int max, int id, int tableswitch)
  615. {
  616. StringBuf buf;
  617. SqlStmt* stmt;
  618. int i;
  619. int j;
  620. const char* tablename;
  621. const char* selectoption;
  622. struct item item; // temp storage variable
  623. bool* flag; // bit array for inventory matching
  624. bool found;
  625. int errors = 0;
  626. switch (tableswitch) {
  627. case TABLE_INVENTORY: tablename = inventory_db; selectoption = "char_id"; break;
  628. case TABLE_CART: tablename = cart_db; selectoption = "char_id"; break;
  629. case TABLE_STORAGE: tablename = storage_db; selectoption = "account_id"; break;
  630. case TABLE_GUILD_STORAGE: tablename = guild_storage_db; selectoption = "guild_id"; break;
  631. default:
  632. ShowError("Invalid table name!\n");
  633. return 1;
  634. }
  635. // The following code compares inventory with current database values
  636. // and performs modification/deletion/insertion only on relevant rows.
  637. // This approach is more complicated than a trivial delete&insert, but
  638. // it significantly reduces cpu load on the database server.
  639. StringBuf_Init(&buf);
  640. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`");
  641. for( j = 0; j < MAX_SLOTS; ++j )
  642. StringBuf_Printf(&buf, ", `card%d`", j);
  643. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id);
  644. stmt = SqlStmt_Malloc(sql_handle);
  645. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  646. || SQL_ERROR == SqlStmt_Execute(stmt) )
  647. {
  648. SqlStmt_ShowDebug(stmt);
  649. SqlStmt_Free(stmt);
  650. StringBuf_Destroy(&buf);
  651. return 1;
  652. }
  653. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  654. SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL);
  655. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  656. SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL);
  657. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  658. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  659. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  660. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  661. for( j = 0; j < MAX_SLOTS; ++j )
  662. SqlStmt_BindColumn(stmt, 8+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL);
  663. // bit array indicating which inventory items have already been matched
  664. flag = (bool*) aCalloc(max, sizeof(bool));
  665. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  666. {
  667. found = false;
  668. // search for the presence of the item in the char's inventory
  669. for( i = 0; i < max; ++i )
  670. {
  671. // skip empty and already matched entries
  672. if( items[i].nameid == 0 || flag[i] )
  673. continue;
  674. if( items[i].nameid == item.nameid
  675. && items[i].card[0] == item.card[0]
  676. && items[i].card[2] == item.card[2]
  677. && items[i].card[3] == item.card[3]
  678. ) { //They are the same item.
  679. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  680. if( j == MAX_SLOTS &&
  681. items[i].amount == item.amount &&
  682. items[i].equip == item.equip &&
  683. items[i].identify == item.identify &&
  684. items[i].refine == item.refine &&
  685. items[i].attribute == item.attribute &&
  686. items[i].expire_time == item.expire_time )
  687. ; //Do nothing.
  688. else
  689. {
  690. // update all fields.
  691. StringBuf_Clear(&buf);
  692. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u'",
  693. tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time);
  694. for( j = 0; j < MAX_SLOTS; ++j )
  695. StringBuf_Printf(&buf, ", `card%d`=%d", j, items[i].card[j]);
  696. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  697. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  698. {
  699. Sql_ShowDebug(sql_handle);
  700. errors++;
  701. }
  702. }
  703. found = flag[i] = true; //Item dealt with,
  704. break; //skip to next item in the db.
  705. }
  706. }
  707. if( !found )
  708. {// Item not present in inventory, remove it.
  709. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", tablename, item.id) )
  710. {
  711. Sql_ShowDebug(sql_handle);
  712. errors++;
  713. }
  714. }
  715. }
  716. SqlStmt_Free(stmt);
  717. StringBuf_Clear(&buf);
  718. StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `unique_id`", tablename, selectoption);
  719. for( j = 0; j < MAX_SLOTS; ++j )
  720. StringBuf_Printf(&buf, ", `card%d`", j);
  721. StringBuf_AppendStr(&buf, ") VALUES ");
  722. found = false;
  723. // insert non-matched items into the db as new items
  724. for( i = 0; i < max; ++i )
  725. {
  726. // skip empty and already matched entries
  727. if( items[i].nameid == 0 || flag[i] )
  728. continue;
  729. if( found )
  730. StringBuf_AppendStr(&buf, ",");
  731. else
  732. found = true;
  733. StringBuf_Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%"PRIu64"'",
  734. 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].unique_id);
  735. for( j = 0; j < MAX_SLOTS; ++j )
  736. StringBuf_Printf(&buf, ", '%d'", items[i].card[j]);
  737. StringBuf_AppendStr(&buf, ")");
  738. updateLastUid(items[i].unique_id); // Unique Non Stackable Item ID
  739. }
  740. dbUpdateUid(sql_handle); // Unique Non Stackable Item ID
  741. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  742. {
  743. Sql_ShowDebug(sql_handle);
  744. errors++;
  745. }
  746. StringBuf_Destroy(&buf);
  747. aFree(flag);
  748. return errors;
  749. }
  750. /* pretty much a copy of memitemdata_to_sql except it handles inventory_db exclusively,
  751. * - this is required because inventory db is the only one with the 'favorite' column. */
  752. int inventory_to_sql(const struct item items[], int max, int id) {
  753. StringBuf buf;
  754. SqlStmt* stmt;
  755. int i;
  756. int j;
  757. struct item item; // temp storage variable
  758. bool* flag; // bit array for inventory matching
  759. bool found;
  760. int errors = 0;
  761. // The following code compares inventory with current database values
  762. // and performs modification/deletion/insertion only on relevant rows.
  763. // This approach is more complicated than a trivial delete&insert, but
  764. // it significantly reduces cpu load on the database server.
  765. StringBuf_Init(&buf);
  766. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`");
  767. for( j = 0; j < MAX_SLOTS; ++j )
  768. StringBuf_Printf(&buf, ", `card%d`", j);
  769. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`='%d'", inventory_db, id);
  770. stmt = SqlStmt_Malloc(sql_handle);
  771. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  772. || SQL_ERROR == SqlStmt_Execute(stmt) )
  773. {
  774. SqlStmt_ShowDebug(stmt);
  775. SqlStmt_Free(stmt);
  776. StringBuf_Destroy(&buf);
  777. return 1;
  778. }
  779. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  780. SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL);
  781. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  782. SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL);
  783. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  784. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  785. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  786. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  787. SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &item.favorite, 0, NULL, NULL);
  788. for( j = 0; j < MAX_SLOTS; ++j )
  789. SqlStmt_BindColumn(stmt, 9+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL);
  790. // bit array indicating which inventory items have already been matched
  791. flag = (bool*) aCalloc(max, sizeof(bool));
  792. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ) {
  793. found = false;
  794. // search for the presence of the item in the char's inventory
  795. for( i = 0; i < max; ++i ) {
  796. // skip empty and already matched entries
  797. if( items[i].nameid == 0 || flag[i] )
  798. continue;
  799. if( items[i].nameid == item.nameid
  800. && items[i].card[0] == item.card[0]
  801. && items[i].card[2] == item.card[2]
  802. && items[i].card[3] == item.card[3]
  803. ) { //They are the same item.
  804. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  805. if( j == MAX_SLOTS &&
  806. items[i].amount == item.amount &&
  807. items[i].equip == item.equip &&
  808. items[i].identify == item.identify &&
  809. items[i].refine == item.refine &&
  810. items[i].attribute == item.attribute &&
  811. items[i].expire_time == item.expire_time &&
  812. items[i].favorite == item.favorite )
  813. ; //Do nothing.
  814. else {
  815. // update all fields.
  816. StringBuf_Clear(&buf);
  817. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `favorite`='%d'",
  818. inventory_db, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].favorite);
  819. for( j = 0; j < MAX_SLOTS; ++j )
  820. StringBuf_Printf(&buf, ", `card%d`=%d", j, items[i].card[j]);
  821. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  822. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) {
  823. Sql_ShowDebug(sql_handle);
  824. errors++;
  825. }
  826. }
  827. found = flag[i] = true; //Item dealt with,
  828. break; //skip to next item in the db.
  829. }
  830. }
  831. if( !found ) {// Item not present in inventory, remove it.
  832. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", inventory_db, item.id) ) {
  833. Sql_ShowDebug(sql_handle);
  834. errors++;
  835. }
  836. }
  837. }
  838. SqlStmt_Free(stmt);
  839. StringBuf_Clear(&buf);
  840. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `unique_id`", inventory_db);
  841. for( j = 0; j < MAX_SLOTS; ++j )
  842. StringBuf_Printf(&buf, ", `card%d`", j);
  843. StringBuf_AppendStr(&buf, ") VALUES ");
  844. found = false;
  845. // insert non-matched items into the db as new items
  846. for( i = 0; i < max; ++i ) {
  847. // skip empty and already matched entries
  848. if( items[i].nameid == 0 || flag[i] )
  849. continue;
  850. if( found )
  851. StringBuf_AppendStr(&buf, ",");
  852. else
  853. found = true;
  854. StringBuf_Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%d', '%"PRIu64"'",
  855. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].favorite, items[i].unique_id);
  856. for( j = 0; j < MAX_SLOTS; ++j )
  857. StringBuf_Printf(&buf, ", '%d'", items[i].card[j]);
  858. StringBuf_AppendStr(&buf, ")");
  859. updateLastUid(items[i].unique_id);// Unique Non Stackable Item ID
  860. }
  861. dbUpdateUid(sql_handle);
  862. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) {
  863. Sql_ShowDebug(sql_handle);
  864. errors++;
  865. }
  866. StringBuf_Destroy(&buf);
  867. aFree(flag);
  868. return errors;
  869. }
  870. int mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p);
  871. //=====================================================================================================
  872. // Loads the basic character rooster for the given account. Returns total buffer used.
  873. int mmo_chars_fromsql(struct char_session_data* sd, uint8* buf)
  874. {
  875. SqlStmt* stmt;
  876. struct mmo_charstatus p;
  877. int j = 0, i;
  878. char last_map[MAP_NAME_LENGTH_EXT];
  879. stmt = SqlStmt_Malloc(sql_handle);
  880. if( stmt == NULL )
  881. {
  882. SqlStmt_ShowDebug(stmt);
  883. return 0;
  884. }
  885. memset(&p, 0, sizeof(p));
  886. // read char data
  887. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  888. "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  889. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  890. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`,"
  891. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`rename`,`delete_date`,"
  892. "`robe`"
  893. " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", char_db, sd->account_id, MAX_CHARS)
  894. || SQL_ERROR == SqlStmt_Execute(stmt)
  895. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL)
  896. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL)
  897. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL)
  898. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL)
  899. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL)
  900. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL)
  901. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p.base_exp, 0, NULL, NULL)
  902. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p.job_exp, 0, NULL, NULL)
  903. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL)
  904. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL)
  905. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL)
  906. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL)
  907. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL)
  908. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL)
  909. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL)
  910. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_INT, &p.max_hp, 0, NULL, NULL)
  911. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p.hp, 0, NULL, NULL)
  912. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p.max_sp, 0, NULL, NULL)
  913. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p.sp, 0, NULL, NULL)
  914. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p.status_point, 0, NULL, NULL)
  915. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p.skill_point, 0, NULL, NULL)
  916. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL)
  917. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL)
  918. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL)
  919. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL)
  920. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL)
  921. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL)
  922. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.weapon, 0, NULL, NULL)
  923. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.shield, 0, NULL, NULL)
  924. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.head_top, 0, NULL, NULL)
  925. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL)
  926. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL)
  927. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  928. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p.rename, 0, NULL, NULL)
  929. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_UINT32, &p.delete_date, 0, NULL, NULL)
  930. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p.robe, 0, NULL, NULL)
  931. )
  932. {
  933. SqlStmt_ShowDebug(stmt);
  934. SqlStmt_Free(stmt);
  935. return 0;
  936. }
  937. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ )
  938. {
  939. p.last_point.map = mapindex_name2id(last_map);
  940. sd->found_char[i] = p.char_id;
  941. j += mmo_char_tobuf(WBUFP(buf, j), &p);
  942. }
  943. for( ; i < MAX_CHARS; i++ )
  944. sd->found_char[i] = -1;
  945. memset(sd->new_name,0,sizeof(sd->new_name));
  946. SqlStmt_Free(stmt);
  947. return j;
  948. }
  949. //=====================================================================================================
  950. int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything)
  951. {
  952. int i,j;
  953. char t_msg[128] = "";
  954. struct mmo_charstatus* cp;
  955. StringBuf buf;
  956. SqlStmt* stmt;
  957. char last_map[MAP_NAME_LENGTH_EXT];
  958. char save_map[MAP_NAME_LENGTH_EXT];
  959. char point_map[MAP_NAME_LENGTH_EXT];
  960. struct point tmp_point;
  961. struct item tmp_item;
  962. struct s_skill tmp_skill;
  963. struct s_friend tmp_friend;
  964. #ifdef HOTKEY_SAVING
  965. struct hotkey tmp_hotkey;
  966. int hotkey_num;
  967. #endif
  968. memset(p, 0, sizeof(struct mmo_charstatus));
  969. if (save_log) ShowInfo("Char load request (%d)\n", char_id);
  970. stmt = SqlStmt_Malloc(sql_handle);
  971. if( stmt == NULL )
  972. {
  973. SqlStmt_ShowDebug(stmt);
  974. return 0;
  975. }
  976. // read char data
  977. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  978. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  979. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  980. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,"
  981. "`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  982. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`"
  983. " FROM `%s` WHERE `char_id`=? LIMIT 1", char_db)
  984. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  985. || SQL_ERROR == SqlStmt_Execute(stmt)
  986. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL)
  987. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL)
  988. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL)
  989. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL)
  990. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL)
  991. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL)
  992. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL)
  993. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p->base_exp, 0, NULL, NULL)
  994. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &p->job_exp, 0, NULL, NULL)
  995. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL)
  996. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL)
  997. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL)
  998. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL)
  999. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL)
  1000. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL)
  1001. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL)
  1002. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p->max_hp, 0, NULL, NULL)
  1003. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p->hp, 0, NULL, NULL)
  1004. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p->max_sp, 0, NULL, NULL)
  1005. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_INT, &p->sp, 0, NULL, NULL)
  1006. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p->status_point, 0, NULL, NULL)
  1007. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p->skill_point, 0, NULL, NULL)
  1008. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL)
  1009. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL)
  1010. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL)
  1011. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL)
  1012. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL)
  1013. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL)
  1014. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL)
  1015. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_INT, &p->ele_id, 0, NULL, NULL)
  1016. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair, 0, NULL, NULL)
  1017. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL)
  1018. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL)
  1019. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->weapon, 0, NULL, NULL)
  1020. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->shield, 0, NULL, NULL)
  1021. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->head_top, 0, NULL, NULL)
  1022. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL)
  1023. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL)
  1024. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  1025. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL)
  1026. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL)
  1027. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL)
  1028. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL)
  1029. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL)
  1030. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_INT, &p->partner_id, 0, NULL, NULL)
  1031. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_INT, &p->father, 0, NULL, NULL)
  1032. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_INT, &p->mother, 0, NULL, NULL)
  1033. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_INT, &p->child, 0, NULL, NULL)
  1034. || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_INT, &p->fame, 0, NULL, NULL)
  1035. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_SHORT, &p->rename, 0, NULL, NULL)
  1036. || SQL_ERROR == SqlStmt_BindColumn(stmt, 50, SQLDT_UINT32, &p->delete_date, 0, NULL, NULL)
  1037. || SQL_ERROR == SqlStmt_BindColumn(stmt, 51, SQLDT_SHORT, &p->robe, 0, NULL, NULL)
  1038. )
  1039. {
  1040. SqlStmt_ShowDebug(stmt);
  1041. SqlStmt_Free(stmt);
  1042. return 0;
  1043. }
  1044. if( SQL_ERROR == SqlStmt_NextRow(stmt) )
  1045. {
  1046. ShowError("Requested non-existant character id: %d!\n", char_id);
  1047. SqlStmt_Free(stmt);
  1048. return 0;
  1049. }
  1050. p->last_point.map = mapindex_name2id(last_map);
  1051. p->save_point.map = mapindex_name2id(save_map);
  1052. strcat(t_msg, " status");
  1053. if (!load_everything) // For quick selection of data when displaying the char menu
  1054. {
  1055. SqlStmt_Free(stmt);
  1056. return 1;
  1057. }
  1058. //read memo data
  1059. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  1060. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", memo_db, MAX_MEMOPOINTS)
  1061. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1062. || SQL_ERROR == SqlStmt_Execute(stmt)
  1063. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL)
  1064. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL)
  1065. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) )
  1066. SqlStmt_ShowDebug(stmt);
  1067. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1068. {
  1069. tmp_point.map = mapindex_name2id(point_map);
  1070. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  1071. }
  1072. strcat(t_msg, " memo");
  1073. //read inventory
  1074. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, `expire_time`, `favorite`, `unique_id`)
  1075. StringBuf_Init(&buf);
  1076. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `unique_id`");
  1077. for( i = 0; i < MAX_SLOTS; ++i )
  1078. StringBuf_Printf(&buf, ", `card%d`", i);
  1079. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", inventory_db, MAX_INVENTORY);
  1080. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1081. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1082. || SQL_ERROR == SqlStmt_Execute(stmt)
  1083. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1084. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  1085. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1086. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  1087. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1088. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1089. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1090. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1091. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &tmp_item.favorite, 0, NULL, NULL)
  1092. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_ULONGLONG, &tmp_item.unique_id, 0, NULL, NULL) )
  1093. SqlStmt_ShowDebug(stmt);
  1094. for( i = 0; i < MAX_SLOTS; ++i )
  1095. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 10+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1096. SqlStmt_ShowDebug(stmt);
  1097. for( i = 0; i < MAX_INVENTORY && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1098. memcpy(&p->inventory[i], &tmp_item, sizeof(tmp_item));
  1099. strcat(t_msg, " inventory");
  1100. //read cart
  1101. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, expire_time`, `unique_id`)
  1102. StringBuf_Clear(&buf);
  1103. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `unique_id`");
  1104. for( j = 0; j < MAX_SLOTS; ++j )
  1105. StringBuf_Printf(&buf, ", `card%d`", j);
  1106. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", cart_db, MAX_CART);
  1107. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1108. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1109. || SQL_ERROR == SqlStmt_Execute(stmt)
  1110. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1111. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  1112. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1113. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  1114. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1115. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1116. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1117. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1118. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_ULONGLONG, &tmp_item.unique_id, 0, NULL, NULL) )
  1119. SqlStmt_ShowDebug(stmt);
  1120. for( i = 0; i < MAX_SLOTS; ++i )
  1121. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 9+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1122. SqlStmt_ShowDebug(stmt);
  1123. for( i = 0; i < MAX_CART && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1124. memcpy(&p->cart[i], &tmp_item, sizeof(tmp_item));
  1125. strcat(t_msg, " cart");
  1126. //read storage
  1127. storage_fromsql(p->account_id, &p->storage);
  1128. strcat(t_msg, " storage");
  1129. //read skill
  1130. //`skill` (`char_id`, `id`, `lv`)
  1131. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`=? LIMIT %d", skill_db, MAX_SKILL)
  1132. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1133. || SQL_ERROR == SqlStmt_Execute(stmt)
  1134. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_USHORT, &tmp_skill.id, 0, NULL, NULL)
  1135. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &tmp_skill.lv, 0, NULL, NULL) )
  1136. SqlStmt_ShowDebug(stmt);
  1137. tmp_skill.flag = SKILL_FLAG_PERMANENT;
  1138. for( i = 0; i < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1139. {
  1140. if( tmp_skill.id < ARRAYLENGTH(p->skill) )
  1141. memcpy(&p->skill[tmp_skill.id], &tmp_skill, sizeof(tmp_skill));
  1142. else
  1143. 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);
  1144. }
  1145. strcat(t_msg, " skills");
  1146. //read friends
  1147. //`friends` (`char_id`, `friend_account`, `friend_id`)
  1148. 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", char_db, friend_db, MAX_FRIENDS)
  1149. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1150. || SQL_ERROR == SqlStmt_Execute(stmt)
  1151. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL)
  1152. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL)
  1153. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) )
  1154. SqlStmt_ShowDebug(stmt);
  1155. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1156. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  1157. strcat(t_msg, " friends");
  1158. #ifdef HOTKEY_SAVING
  1159. //read hotkeys
  1160. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  1161. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", hotkey_db)
  1162. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1163. || SQL_ERROR == SqlStmt_Execute(stmt)
  1164. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL)
  1165. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL)
  1166. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL)
  1167. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) )
  1168. SqlStmt_ShowDebug(stmt);
  1169. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  1170. {
  1171. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS )
  1172. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  1173. else
  1174. 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);
  1175. }
  1176. strcat(t_msg, " hotkeys");
  1177. #endif
  1178. /* Mercenary Owner DataBase */
  1179. mercenary_owner_fromsql(char_id, p);
  1180. strcat(t_msg, " mercenary");
  1181. if (save_log) ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  1182. SqlStmt_Free(stmt);
  1183. StringBuf_Destroy(&buf);
  1184. cp = idb_ensure(char_db_, char_id, create_charstatus);
  1185. memcpy(cp, p, sizeof(struct mmo_charstatus));
  1186. return 1;
  1187. }
  1188. //==========================================================================================================
  1189. int mmo_char_sql_init(void)
  1190. {
  1191. char_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  1192. ShowStatus("Characters per Account: '%d'.\n", char_per_account);
  1193. //the 'set offline' part is now in check_login_conn ...
  1194. //if the server connects to loginserver
  1195. //it will dc all off players
  1196. //and send the loginserver the new state....
  1197. // Force all users offline in sql when starting char-server
  1198. // (useful when servers crashs and don't clean the database)
  1199. set_all_offline_sql();
  1200. return 0;
  1201. }
  1202. //-----------------------------------
  1203. // Function to change chararcter's names
  1204. //-----------------------------------
  1205. int rename_char_sql(struct char_session_data *sd, int char_id)
  1206. {
  1207. struct mmo_charstatus char_dat;
  1208. char esc_name[NAME_LENGTH*2+1];
  1209. if( sd->new_name[0] == 0 ) // Not ready for rename
  1210. return 2;
  1211. if( !mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed.
  1212. return 2;
  1213. if( char_dat.rename == 0 )
  1214. return 1;
  1215. Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH));
  1216. // check if the char exist
  1217. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` LIKE '%s' LIMIT 1", char_db, esc_name) )
  1218. {
  1219. Sql_ShowDebug(sql_handle);
  1220. return 4;
  1221. }
  1222. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `name` = '%s', `rename` = '%d' WHERE `char_id` = '%d'", char_db, esc_name, --char_dat.rename, char_id) )
  1223. {
  1224. Sql_ShowDebug(sql_handle);
  1225. return 3;
  1226. }
  1227. // Change character's name into guild_db.
  1228. if( char_dat.guild_id )
  1229. inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name);
  1230. safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH);
  1231. memset(sd->new_name,0,sizeof(sd->new_name));
  1232. // log change
  1233. if( log_char )
  1234. {
  1235. 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`)"
  1236. "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')",
  1237. charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) )
  1238. Sql_ShowDebug(sql_handle);
  1239. }
  1240. return 0;
  1241. }
  1242. int check_char_name(char * name, char * esc_name)
  1243. {
  1244. int i;
  1245. // check length of character name
  1246. if( name[0] == '\0' )
  1247. return -2; // empty character name
  1248. /**
  1249. * The client does not allow you to create names with less than 4 characters, however,
  1250. * the use of WPE can bypass this, and this fixes the exploit.
  1251. **/
  1252. if( strlen( name ) < 4 )
  1253. return -2;
  1254. // check content of character name
  1255. if( remove_control_chars(name) )
  1256. return -2; // control chars in name
  1257. // check for reserved names
  1258. if( strcmpi(name, main_chat_nick) == 0 || strcmpi(name, wisp_server_name) == 0 )
  1259. return -1; // nick reserved for internal server messages
  1260. // Check Authorised letters/symbols in the name of the character
  1261. if( char_name_option == 1 )
  1262. { // only letters/symbols in char_name_letters are authorised
  1263. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1264. if( strchr(char_name_letters, name[i]) == NULL )
  1265. return -2;
  1266. }
  1267. else if( char_name_option == 2 )
  1268. { // letters/symbols in char_name_letters are forbidden
  1269. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1270. if( strchr(char_name_letters, name[i]) != NULL )
  1271. return -2;
  1272. }
  1273. if( name_ignoring_case ) {
  1274. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE BINARY `name` = '%s' LIMIT 1", char_db, esc_name) ) {
  1275. Sql_ShowDebug(sql_handle);
  1276. return -2;
  1277. }
  1278. } else {
  1279. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s' LIMIT 1", char_db, esc_name) ) {
  1280. Sql_ShowDebug(sql_handle);
  1281. return -2;
  1282. }
  1283. }
  1284. if( Sql_NumRows(sql_handle) > 0 )
  1285. return -1; // name already exists
  1286. return 0;
  1287. }
  1288. //-----------------------------------
  1289. // Function to create a new character
  1290. //-----------------------------------
  1291. #if PACKETVER >= 20120307
  1292. int make_new_char_sql(struct char_session_data* sd, char* name_, int slot, int hair_color, int hair_style) {
  1293. int str = 1, agi = 1, vit = 1, int_ = 1, dex = 1, luk = 1;
  1294. #else
  1295. int 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) {
  1296. #endif
  1297. char name[NAME_LENGTH];
  1298. char esc_name[NAME_LENGTH*2+1];
  1299. int char_id, flag;
  1300. safestrncpy(name, name_, NAME_LENGTH);
  1301. normalize_name(name,TRIM_CHARS);
  1302. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1303. flag = check_char_name(name,esc_name);
  1304. if( flag < 0 )
  1305. return flag;
  1306. //check other inputs
  1307. #if PACKETVER >= 20120307
  1308. if(slot >= MAX_CHARS)
  1309. #else
  1310. if((slot >= MAX_CHARS) // slots
  1311. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1312. || (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
  1313. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1314. #endif
  1315. return -2; // invalid input
  1316. // check the number of already existing chars in this account
  1317. if( char_per_account != 0 ) {
  1318. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id) )
  1319. Sql_ShowDebug(sql_handle);
  1320. if( Sql_NumRows(sql_handle) >= char_per_account )
  1321. return -2; // character account limit exceeded
  1322. }
  1323. // check char slot
  1324. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' LIMIT 1", char_db, sd->account_id, slot) )
  1325. Sql_ShowDebug(sql_handle);
  1326. if( Sql_NumRows(sql_handle) > 0 )
  1327. return -2; // slot already in use
  1328. // validation success, log result
  1329. if (log_char) {
  1330. 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`)"
  1331. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1332. charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1333. Sql_ShowDebug(sql_handle);
  1334. }
  1335. #if PACKETVER >= 20120307
  1336. //Insert the new char entry to the database
  1337. 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`,"
  1338. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1339. "'%d', '%d', '%s', '%d', '%d','%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1340. char_db, sd->account_id , slot, esc_name, start_zeny, 48, str, agi, vit, int_, dex, luk,
  1341. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1342. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y) )
  1343. {
  1344. Sql_ShowDebug(sql_handle);
  1345. return -2; //No, stop the procedure!
  1346. }
  1347. #else
  1348. //Insert the new char entry to the database
  1349. 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`,"
  1350. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1351. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1352. char_db, sd->account_id , slot, esc_name, start_zeny, str, agi, vit, int_, dex, luk,
  1353. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1354. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y) )
  1355. {
  1356. Sql_ShowDebug(sql_handle);
  1357. return -2; //No, stop the procedure!
  1358. }
  1359. #endif
  1360. //Retrieve the newly auto-generated char id
  1361. char_id = (int)Sql_LastInsertId(sql_handle);
  1362. //Give the char the default items
  1363. if (start_weapon > 0) { //add Start Weapon (Knife?)
  1364. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_weapon, 1, 1) )
  1365. Sql_ShowDebug(sql_handle);
  1366. }
  1367. if (start_armor > 0) { //Add default armor (cotton shirt?)
  1368. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_armor, 1, 1) )
  1369. Sql_ShowDebug(sql_handle);
  1370. }
  1371. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1372. return char_id;
  1373. }
  1374. /*----------------------------------------------------------------------------------------------------------*/
  1375. /* Divorce Players */
  1376. /*----------------------------------------------------------------------------------------------------------*/
  1377. int divorce_char_sql(int partner_id1, int partner_id2)
  1378. {
  1379. unsigned char buf[64];
  1380. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d' LIMIT 2", char_db, partner_id1, partner_id2) )
  1381. Sql_ShowDebug(sql_handle);
  1382. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND (`char_id`='%d' OR `char_id`='%d') LIMIT 2", inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) )
  1383. Sql_ShowDebug(sql_handle);
  1384. WBUFW(buf,0) = 0x2b12;
  1385. WBUFL(buf,2) = partner_id1;
  1386. WBUFL(buf,6) = partner_id2;
  1387. mapif_sendall(buf,10);
  1388. return 0;
  1389. }
  1390. /*----------------------------------------------------------------------------------------------------------*/
  1391. /* Delete char - davidsiaw */
  1392. /*----------------------------------------------------------------------------------------------------------*/
  1393. /* Returns 0 if successful
  1394. * Returns < 0 for error
  1395. */
  1396. int delete_char_sql(int char_id)
  1397. {
  1398. char name[NAME_LENGTH];
  1399. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1400. int account_id, party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id, elemental_id;
  1401. char *data;
  1402. size_t len;
  1403. 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'", char_db, char_id))
  1404. Sql_ShowDebug(sql_handle);
  1405. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1406. {
  1407. ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n");
  1408. Sql_FreeResult(sql_handle);
  1409. return -1;
  1410. }
  1411. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1412. Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data);
  1413. Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data);
  1414. Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data);
  1415. Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data);
  1416. Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data);
  1417. Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data);
  1418. Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data);
  1419. Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data);
  1420. Sql_GetData(sql_handle, 9, &data, NULL);
  1421. elemental_id = atoi(data);
  1422. Sql_EscapeStringLen(sql_handle, esc_name, name, min(len, NAME_LENGTH));
  1423. Sql_FreeResult(sql_handle);
  1424. //check for config char del condition [Lupus]
  1425. // TODO: Move this out to packet processing (0x68/0x1fb).
  1426. if( ( char_del_level > 0 && base_level >= char_del_level )
  1427. || ( char_del_level < 0 && base_level <= -char_del_level )
  1428. ) {
  1429. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1430. return -1;
  1431. }
  1432. /* Divorce [Wizputer] */
  1433. if( partner_id )
  1434. divorce_char_sql(char_id, partner_id);
  1435. /* De-addopt [Zephyrus] */
  1436. if( father_id || mother_id )
  1437. { // Char is Baby
  1438. unsigned char buf[64];
  1439. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", char_db, father_id, mother_id) )
  1440. Sql_ShowDebug(sql_handle);
  1441. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410'AND (`char_id`='%d' OR `char_id`='%d')", skill_db, father_id, mother_id) )
  1442. Sql_ShowDebug(sql_handle);
  1443. WBUFW(buf,0) = 0x2b25;
  1444. WBUFL(buf,2) = father_id;
  1445. WBUFL(buf,6) = mother_id;
  1446. WBUFL(buf,10) = char_id; // Baby
  1447. mapif_sendall(buf,14);
  1448. }
  1449. //Make the character leave the party [Skotlex]
  1450. if (party_id)
  1451. inter_party_leave(party_id, account_id, char_id);
  1452. /* delete char's pet */
  1453. //Delete the hatched pet if you have one...
  1454. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incuvate` = '0'", pet_db, char_id) )
  1455. Sql_ShowDebug(sql_handle);
  1456. //Delete all pets that are stored in eggs (inventory + cart)
  1457. 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", pet_db, pet_db, inventory_db, inventory_db, char_id) )
  1458. Sql_ShowDebug(sql_handle);
  1459. 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", pet_db, pet_db, cart_db, cart_db, char_id) )
  1460. Sql_ShowDebug(sql_handle);
  1461. /* remove homunculus */
  1462. if( hom_id )
  1463. mapif_homunculus_delete(hom_id);
  1464. /* remove elemental */
  1465. if (elemental_id)
  1466. mapif_elemental_delete(elemental_id);
  1467. /* remove mercenary data */
  1468. mercenary_owner_delete(char_id);
  1469. /* delete char's friends list */
  1470. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", friend_db, char_id) )
  1471. Sql_ShowDebug(sql_handle);
  1472. /* delete char from other's friend list */
  1473. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1474. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", friend_db, char_id) )
  1475. Sql_ShowDebug(sql_handle);
  1476. #ifdef HOTKEY_SAVING
  1477. /* delete hotkeys */
  1478. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", hotkey_db, char_id) )
  1479. Sql_ShowDebug(sql_handle);
  1480. #endif
  1481. /* delete inventory */
  1482. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", inventory_db, char_id) )
  1483. Sql_ShowDebug(sql_handle);
  1484. /* delete cart inventory */
  1485. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", cart_db, char_id) )
  1486. Sql_ShowDebug(sql_handle);
  1487. /* delete memo areas */
  1488. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, char_id) )
  1489. Sql_ShowDebug(sql_handle);
  1490. /* delete character registry */
  1491. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'", reg_db, char_id) )
  1492. Sql_ShowDebug(sql_handle);
  1493. /* delete skills */
  1494. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, char_id) )
  1495. Sql_ShowDebug(sql_handle);
  1496. /* delete mails (only received) */
  1497. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `dest_id`='%d'", mail_db, char_id))
  1498. Sql_ShowDebug(sql_handle);
  1499. #ifdef ENABLE_SC_SAVING
  1500. /* status changes */
  1501. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, account_id, char_id) )
  1502. Sql_ShowDebug(sql_handle);
  1503. #endif
  1504. if (log_char) {
  1505. 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')",
  1506. charlog_db, account_id, 0, char_id, esc_name) )
  1507. Sql_ShowDebug(sql_handle);
  1508. }
  1509. /* delete character */
  1510. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  1511. Sql_ShowDebug(sql_handle);
  1512. /* No need as we used inter_guild_leave [Skotlex]
  1513. // Also delete info from guildtables.
  1514. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1515. Sql_ShowDebug(sql_handle);
  1516. */
  1517. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", guild_db, char_id) )
  1518. Sql_ShowDebug(sql_handle);
  1519. else if( Sql_NumRows(sql_handle) > 0 )
  1520. mapif_parse_BreakGuild(0,guild_id);
  1521. else if( guild_id )
  1522. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1523. return 0;
  1524. }
  1525. //---------------------------------------------------------------------
  1526. // This function return the number of online players in all map-servers
  1527. //---------------------------------------------------------------------
  1528. int count_users(void)
  1529. {
  1530. int i, users;
  1531. users = 0;
  1532. for(i = 0; i < ARRAYLENGTH(server); i++) {
  1533. if (server[i].fd > 0) {
  1534. users += server[i].users;
  1535. }
  1536. }
  1537. return users;
  1538. }
  1539. // Writes char data to the buffer in the format used by the client.
  1540. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1541. // Returns the size
  1542. #define MAX_CHAR_BUF 144 //Max size (for WFIFOHEAD calls)
  1543. int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p)
  1544. {
  1545. unsigned short offset = 0;
  1546. uint8* buf;
  1547. if( buffer == NULL || p == NULL )
  1548. return 0;
  1549. buf = WBUFP(buffer,0);
  1550. WBUFL(buf,0) = p->char_id;
  1551. WBUFL(buf,4) = min(p->base_exp, INT32_MAX);
  1552. WBUFL(buf,8) = p->zeny;
  1553. WBUFL(buf,12) = min(p->job_exp, INT32_MAX);
  1554. WBUFL(buf,16) = p->job_level;
  1555. WBUFL(buf,20) = 0; // probably opt1
  1556. WBUFL(buf,24) = 0; // probably opt2
  1557. WBUFL(buf,28) = p->option;
  1558. WBUFL(buf,32) = p->karma;
  1559. WBUFL(buf,36) = p->manner;
  1560. WBUFW(buf,40) = min(p->status_point, INT16_MAX);
  1561. WBUFL(buf,42) = p->hp;
  1562. WBUFL(buf,46) = p->max_hp;
  1563. offset+=4;
  1564. buf = WBUFP(buffer,offset);
  1565. WBUFW(buf,46) = min(p->sp, INT16_MAX);
  1566. WBUFW(buf,48) = min(p->max_sp, INT16_MAX);
  1567. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1568. WBUFW(buf,52) = p->class_;
  1569. WBUFW(buf,54) = p->hair;
  1570. //When the weapon is sent and your option is riding, the client crashes on login!?
  1571. WBUFW(buf,56) = p->option&(0x20|0x80000|0x100000|0x200000|0x400000|0x800000|0x1000000|0x2000000|0x4000000|0x8000000) ? 0 : p->weapon;
  1572. WBUFW(buf,58) = p->base_level;
  1573. WBUFW(buf,60) = min(p->skill_point, INT16_MAX);
  1574. WBUFW(buf,62) = p->head_bottom;
  1575. WBUFW(buf,64) = p->shield;
  1576. WBUFW(buf,66) = p->head_top;
  1577. WBUFW(buf,68) = p->head_mid;
  1578. WBUFW(buf,70) = p->hair_color;
  1579. WBUFW(buf,72) = p->clothes_color;
  1580. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1581. WBUFB(buf,98) = min(p->str, UINT8_MAX);
  1582. WBUFB(buf,99) = min(p->agi, UINT8_MAX);
  1583. WBUFB(buf,100) = min(p->vit, UINT8_MAX);
  1584. WBUFB(buf,101) = min(p->int_, UINT8_MAX);
  1585. WBUFB(buf,102) = min(p->dex, UINT8_MAX);
  1586. WBUFB(buf,103) = min(p->luk, UINT8_MAX);
  1587. WBUFW(buf,104) = p->slot;
  1588. WBUFW(buf,106) = ( p->rename > 0 ) ? 0 : 1;
  1589. offset += 2;
  1590. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1591. mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
  1592. offset += MAP_NAME_LENGTH_EXT;
  1593. #endif
  1594. #if PACKETVER >= 20100803
  1595. WBUFL(buf,124) = TOL(p->delete_date);
  1596. offset += 4;
  1597. #endif
  1598. #if PACKETVER >= 20110111
  1599. WBUFL(buf,128) = p->robe;
  1600. offset += 4;
  1601. #endif
  1602. #if PACKETVER != 20111116 //2011-11-16 wants 136, ask gravity.
  1603. #if PACKETVER >= 20110928
  1604. WBUFL(buf,132) = 0; // change slot feature (0 = disabled, otherwise enabled)
  1605. offset += 4;
  1606. #endif
  1607. #if PACKETVER >= 20111025
  1608. WBUFL(buf,136) = ( p->rename > 0 ) ? 1 : 0; // (0 = disabled, otherwise displays "Add-Ons" sidebar)
  1609. offset += 4;
  1610. #endif
  1611. #endif
  1612. return 106+offset;
  1613. }
  1614. //----------------------------------------
  1615. // Function to send characters to a player
  1616. //----------------------------------------
  1617. int mmo_char_send006b(int fd, struct char_session_data* sd)
  1618. {
  1619. int j, offset = 0;
  1620. #if PACKETVER >= 20100413
  1621. offset += 3;
  1622. #endif
  1623. if (save_log)
  1624. ShowInfo("Loading Char Data ("CL_BOLD"%d"CL_RESET")\n",sd->account_id);
  1625. j = 24 + offset; // offset
  1626. WFIFOHEAD(fd,j + MAX_CHARS*MAX_CHAR_BUF);
  1627. WFIFOW(fd,0) = 0x6b;
  1628. #if PACKETVER >= 20100413
  1629. WFIFOB(fd,4) = MAX_CHARS; // Max slots.
  1630. WFIFOB(fd,5) = MAX_CHARS; // Available slots.
  1631. WFIFOB(fd,6) = MAX_CHARS; // Premium slots.
  1632. #endif
  1633. memset(WFIFOP(fd,4 + offset), 0, 20); // unknown bytes
  1634. j+=mmo_chars_fromsql(sd, WFIFOP(fd,j));
  1635. WFIFOW(fd,2) = j; // packet len
  1636. WFIFOSET(fd,j);
  1637. return 0;
  1638. }
  1639. int char_married(int pl1, int pl2)
  1640. {
  1641. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", char_db, pl1) )
  1642. Sql_ShowDebug(sql_handle);
  1643. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1644. {
  1645. char* data;
  1646. Sql_GetData(sql_handle, 0, &data, NULL);
  1647. if( pl2 == atoi(data) )
  1648. {
  1649. Sql_FreeResult(sql_handle);
  1650. return 1;
  1651. }
  1652. }
  1653. Sql_FreeResult(sql_handle);
  1654. return 0;
  1655. }
  1656. int char_child(int parent_id, int child_id)
  1657. {
  1658. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", char_db, parent_id) )
  1659. Sql_ShowDebug(sql_handle);
  1660. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1661. {
  1662. char* data;
  1663. Sql_GetData(sql_handle, 0, &data, NULL);
  1664. if( child_id == atoi(data) )
  1665. {
  1666. Sql_FreeResult(sql_handle);
  1667. return 1;
  1668. }
  1669. }
  1670. Sql_FreeResult(sql_handle);
  1671. return 0;
  1672. }
  1673. int char_family(int cid1, int cid2, int cid3)
  1674. {
  1675. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", char_db, cid1, cid2, cid3) )
  1676. Sql_ShowDebug(sql_handle);
  1677. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1678. {
  1679. int charid;
  1680. int partnerid;
  1681. int childid;
  1682. char* data;
  1683. Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data);
  1684. Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data);
  1685. Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data);
  1686. if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) ||
  1687. (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) ||
  1688. (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) )
  1689. {
  1690. Sql_FreeResult(sql_handle);
  1691. return childid;
  1692. }
  1693. }
  1694. Sql_FreeResult(sql_handle);
  1695. return 0;
  1696. }
  1697. //----------------------------------------------------------------------
  1698. // Force disconnection of an online player (with account value) by [Yor]
  1699. //----------------------------------------------------------------------
  1700. void disconnect_player(int account_id)
  1701. {
  1702. int i;
  1703. struct char_session_data* sd;
  1704. // disconnect player if online on char-server
  1705. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1706. if( i < fd_max )
  1707. set_eof(i);
  1708. }
  1709. static void char_auth_ok(int fd, struct char_session_data *sd)
  1710. {
  1711. struct online_char_data* character;
  1712. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1713. { // check if character is not online already. [Skotlex]
  1714. if (character->server > -1)
  1715. { //Character already online. KICK KICK KICK
  1716. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  1717. if (character->waiting_disconnect == INVALID_TIMER)
  1718. character->waiting_disconnect = add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  1719. WFIFOHEAD(fd,3);
  1720. WFIFOW(fd,0) = 0x81;
  1721. WFIFOB(fd,2) = 8;
  1722. WFIFOSET(fd,3);
  1723. return;
  1724. }
  1725. if (character->fd >= 0 && character->fd != fd)
  1726. { //There's already a connection from this account that hasn't picked a char yet.
  1727. WFIFOHEAD(fd,3);
  1728. WFIFOW(fd,0) = 0x81;
  1729. WFIFOB(fd,2) = 8;
  1730. WFIFOSET(fd,3);
  1731. return;
  1732. }
  1733. character->fd = fd;
  1734. }
  1735. if (login_fd > 0) {
  1736. // request account data
  1737. WFIFOHEAD(login_fd,6);
  1738. WFIFOW(login_fd,0) = 0x2716;
  1739. WFIFOL(login_fd,2) = sd->account_id;
  1740. WFIFOSET(login_fd,6);
  1741. }
  1742. // mark session as 'authed'
  1743. sd->auth = true;
  1744. // set char online on charserver
  1745. set_char_charselect(sd->account_id);
  1746. // continues when account data is received...
  1747. }
  1748. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data);
  1749. void mapif_server_reset(int id);
  1750. /// Resets all the data.
  1751. void loginif_reset(void)
  1752. {
  1753. int id;
  1754. // TODO kick everyone out and reset everything or wait for connect and try to reaquire locks [FlavioJS]
  1755. for( id = 0; id < ARRAYLENGTH(server); ++id )
  1756. mapif_server_reset(id);
  1757. flush_fifos();
  1758. exit(EXIT_FAILURE);
  1759. }
  1760. /// Checks the conditions for the server to stop.
  1761. /// Releases the cookie when all characters are saved.
  1762. /// If all the conditions are met, it stops the core loop.
  1763. void loginif_check_shutdown(void)
  1764. {
  1765. if( runflag != CHARSERVER_ST_SHUTDOWN )
  1766. return;
  1767. runflag = CORE_ST_STOP;
  1768. }
  1769. /// Called when the connection to Login Server is disconnected.
  1770. void loginif_on_disconnect(void)
  1771. {
  1772. ShowWarning("Connection to Login Server lost.\n\n");
  1773. }
  1774. /// Called when all the connection steps are completed.
  1775. void loginif_on_ready(void)
  1776. {
  1777. int i;
  1778. loginif_check_shutdown();
  1779. //Send online accounts to login server.
  1780. send_accounts_tologin(INVALID_TIMER, gettick(), 0, 0);
  1781. // if no map-server already connected, display a message...
  1782. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd > 0 && server[i].map[0] );
  1783. if( i == ARRAYLENGTH(server) )
  1784. ShowStatus("Awaiting maps from map-server.\n");
  1785. }
  1786. int parse_fromlogin(int fd) {
  1787. struct char_session_data* sd = NULL;
  1788. int i;
  1789. // only process data from the login-server
  1790. if( fd != login_fd ) {
  1791. ShowDebug("parse_fromlogin: Disconnecting invalid session #%d (is not the login-server)\n", fd);
  1792. do_close(fd);
  1793. return 0;
  1794. }
  1795. if( session[fd]->flag.eof ) {
  1796. do_close(fd);
  1797. login_fd = -1;
  1798. loginif_on_disconnect();
  1799. return 0;
  1800. } else if ( session[fd]->flag.ping ) {/* we've reached stall time */
  1801. if( DIFF_TICK(last_tick, session[fd]->rdata_tick) > (stall_time * 2) ) {/* we can't wait any longer */
  1802. set_eof(fd);
  1803. return 0;
  1804. } else if( session[fd]->flag.ping != 2 ) { /* we haven't sent ping out yet */
  1805. WFIFOHEAD(fd,2);// sends a ping packet to login server (will receive pong 0x2718)
  1806. WFIFOW(fd,0) = 0x2719;
  1807. WFIFOSET(fd,2);
  1808. session[fd]->flag.ping = 2;
  1809. }
  1810. }
  1811. sd = (struct char_session_data*)session[fd]->session_data;
  1812. while(RFIFOREST(fd) >= 2) {
  1813. uint16 command = RFIFOW(fd,0);
  1814. switch( command )
  1815. {
  1816. // acknowledgement of connect-to-loginserver request
  1817. case 0x2711:
  1818. if (RFIFOREST(fd) < 3)
  1819. return 0;
  1820. if (RFIFOB(fd,2)) {
  1821. //printf("connect login server error : %d\n", RFIFOB(fd,2));
  1822. ShowError("Can not connect to login-server.\n");
  1823. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1824. ShowError("Also, please make sure your login db has the correct communication username/passwords and the gender of the account is S.\n");
  1825. ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n");
  1826. set_eof(fd);
  1827. return 0;
  1828. } else {
  1829. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1830. loginif_on_ready();
  1831. }
  1832. RFIFOSKIP(fd,3);
  1833. break;
  1834. // acknowledgement of account authentication request
  1835. case 0x2713:
  1836. if (RFIFOREST(fd) < 25)
  1837. return 0;
  1838. {
  1839. int account_id = RFIFOL(fd,2);
  1840. uint32 login_id1 = RFIFOL(fd,6);
  1841. uint32 login_id2 = RFIFOL(fd,10);
  1842. uint8 sex = RFIFOB(fd,14);
  1843. uint8 result = RFIFOB(fd,15);
  1844. int request_id = RFIFOL(fd,16);
  1845. uint32 version = RFIFOL(fd,20);
  1846. uint8 clienttype = RFIFOB(fd,24);
  1847. RFIFOSKIP(fd,25);
  1848. if( session_isActive(request_id) && (sd=(struct char_session_data*)session[request_id]->session_data) &&
  1849. !sd->auth && sd->account_id == account_id && sd->login_id1 == login_id1 && sd->login_id2 == login_id2 && sd->sex == sex )
  1850. {
  1851. int client_fd = request_id;
  1852. sd->version = version;
  1853. sd->clienttype = clienttype;
  1854. switch( result )
  1855. {
  1856. case 0:// ok
  1857. char_auth_ok(client_fd, sd);
  1858. break;
  1859. case 1:// auth failed
  1860. WFIFOHEAD(client_fd,3);
  1861. WFIFOW(client_fd,0) = 0x6c;
  1862. WFIFOB(client_fd,2) = 0;// rejected from server
  1863. WFIFOSET(client_fd,3);
  1864. break;
  1865. }
  1866. }
  1867. }
  1868. break;
  1869. case 0x2717: // account data
  1870. if (RFIFOREST(fd) < 62)
  1871. return 0;
  1872. // find the authenticated session with this account id
  1873. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->auth && sd->account_id == RFIFOL(fd,2) );
  1874. if( i < fd_max )
  1875. {
  1876. int server_id;
  1877. memcpy(sd->email, RFIFOP(fd,6), 40);
  1878. sd->expiration_time = (time_t)RFIFOL(fd,46);
  1879. sd->group_id = RFIFOB(fd,50);
  1880. safestrncpy(sd->birthdate, (const char*)RFIFOP(fd,51), sizeof(sd->birthdate));
  1881. ARR_FIND( 0, ARRAYLENGTH(server), server_id, server[server_id].fd > 0 && server[server_id].map[0] );
  1882. // continued from char_auth_ok...
  1883. if( server_id == ARRAYLENGTH(server) || //server not online, bugreport:2359
  1884. ( max_connect_user && count_users() >= max_connect_user && sd->group_id != gm_allow_group ) ) {
  1885. // refuse connection (over populated)
  1886. WFIFOHEAD(i,3);
  1887. WFIFOW(i,0) = 0x6c;
  1888. WFIFOW(i,2) = 0;
  1889. WFIFOSET(i,3);
  1890. } else {
  1891. // send characters to player
  1892. mmo_char_send006b(i, sd);
  1893. #if PACKETVER >= 20110309
  1894. // PIN code system, disabled
  1895. WFIFOHEAD(i, 12);
  1896. WFIFOW(i, 0) = 0x08B9;
  1897. WFIFOW(i, 2) = 0;
  1898. WFIFOW(i, 4) = 0;
  1899. WFIFOL(i, 6) = sd->account_id;
  1900. WFIFOW(i, 10) = 0;
  1901. WFIFOSET(i, 12);
  1902. #endif
  1903. }
  1904. }
  1905. RFIFOSKIP(fd,62);
  1906. break;
  1907. // login-server alive packet
  1908. case 0x2718:
  1909. if (RFIFOREST(fd) < 2)
  1910. return 0;
  1911. RFIFOSKIP(fd,2);
  1912. session[fd]->flag.ping = 0;
  1913. break;
  1914. // changesex reply
  1915. case 0x2723:
  1916. if (RFIFOREST(fd) < 7)
  1917. return 0;
  1918. {
  1919. unsigned char buf[7];
  1920. int acc = RFIFOL(fd,2);
  1921. int sex = RFIFOB(fd,6);
  1922. RFIFOSKIP(fd,7);
  1923. if( acc > 0 )
  1924. {// TODO: Is this even possible?
  1925. int char_id[MAX_CHARS];
  1926. int class_[MAX_CHARS];
  1927. int guild_id[MAX_CHARS];
  1928. int num;
  1929. char* data;
  1930. struct auth_node* node = (struct auth_node*)idb_get(auth_db, acc);
  1931. if( node != NULL )
  1932. node->sex = sex;
  1933. // get characters
  1934. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`class`,`guild_id` FROM `%s` WHERE `account_id` = '%d'", char_db, acc) )
  1935. Sql_ShowDebug(sql_handle);
  1936. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1937. {
  1938. Sql_GetData(sql_handle, 0, &data, NULL); char_id[i] = atoi(data);
  1939. Sql_GetData(sql_handle, 1, &data, NULL); class_[i] = atoi(data);
  1940. Sql_GetData(sql_handle, 2, &data, NULL); guild_id[i] = atoi(data);
  1941. }
  1942. num = i;
  1943. for( i = 0; i < num; ++i )
  1944. {
  1945. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER ||
  1946. class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY ||
  1947. class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER ||
  1948. class_[i] == JOB_MINSTREL || class_[i] == JOB_WANDERER ||
  1949. class_[i] == JOB_MINSTREL_T || class_[i] == JOB_WANDERER_T ||
  1950. class_[i] == JOB_BABY_MINSTREL || class_[i] == JOB_BABY_WANDERER ||
  1951. class_[i] == JOB_KAGEROU || class_[i] == JOB_OBORO )
  1952. {
  1953. // job modification
  1954. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER )
  1955. class_[i] = (sex ? JOB_BARD : JOB_DANCER);
  1956. else if( class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY )
  1957. class_[i] = (sex ? JOB_CLOWN : JOB_GYPSY);
  1958. else if( class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER )
  1959. class_[i] = (sex ? JOB_BABY_BARD : JOB_BABY_DANCER);
  1960. else if( class_[i] == JOB_MINSTREL || class_[i] == JOB_WANDERER )
  1961. class_[i] = (sex ? JOB_MINSTREL : JOB_WANDERER);
  1962. else if( class_[i] == JOB_MINSTREL_T || class_[i] == JOB_WANDERER_T )
  1963. class_[i] = (sex ? JOB_MINSTREL_T : JOB_WANDERER_T);
  1964. else if( class_[i] == JOB_BABY_MINSTREL || class_[i] == JOB_BABY_WANDERER )
  1965. class_[i] = (sex ? JOB_BABY_MINSTREL : JOB_BABY_WANDERER);
  1966. else if( class_[i] == JOB_KAGEROU || class_[i] == JOB_OBORO )
  1967. class_[i] = (sex ? JOB_KAGEROU : JOB_OBORO);
  1968. }
  1969. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d', `weapon`='0', `shield`='0', `head_top`='0', `head_mid`='0', `head_bottom`='0' WHERE `char_id`='%d'", char_db, class_[i], char_id[i]) )
  1970. Sql_ShowDebug(sql_handle);
  1971. if( guild_id[i] )// If there is a guild, update the guild_member data [Skotlex]
  1972. inter_guild_sex_changed(guild_id[i], acc, char_id[i], sex);
  1973. }
  1974. Sql_FreeResult(sql_handle);
  1975. // disconnect player if online on char-server
  1976. disconnect_player(acc);
  1977. }
  1978. // notify all mapservers about this change
  1979. WBUFW(buf,0) = 0x2b0d;
  1980. WBUFL(buf,2) = acc;
  1981. WBUFB(buf,6) = sex;
  1982. mapif_sendall(buf, 7);
  1983. }
  1984. break;
  1985. // reply to an account_reg2 registry request
  1986. case 0x2729:
  1987. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1988. return 0;
  1989. { //Receive account_reg2 registry, forward to map servers.
  1990. unsigned char buf[13+ACCOUNT_REG2_NUM*sizeof(struct global_reg)];
  1991. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  1992. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  1993. mapif_sendall(buf, WBUFW(buf,2));
  1994. RFIFOSKIP(fd, RFIFOW(fd,2));
  1995. }
  1996. break;
  1997. // State change of account/ban notification (from login-server)
  1998. case 0x2731:
  1999. if (RFIFOREST(fd) < 11)
  2000. return 0;
  2001. { // send to all map-servers to disconnect the player
  2002. unsigned char buf[11];
  2003. WBUFW(buf,0) = 0x2b14;
  2004. WBUFL(buf,2) = RFIFOL(fd,2);
  2005. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  2006. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  2007. mapif_sendall(buf, 11);
  2008. }
  2009. // disconnect player if online on char-server
  2010. disconnect_player(RFIFOL(fd,2));
  2011. RFIFOSKIP(fd,11);
  2012. break;
  2013. // Login server request to kick a character out. [Skotlex]
  2014. case 0x2734:
  2015. if (RFIFOREST(fd) < 6)
  2016. return 0;
  2017. {
  2018. int aid = RFIFOL(fd,2);
  2019. struct online_char_data* character = (struct online_char_data*)idb_get(online_char_db, aid);
  2020. RFIFOSKIP(fd,6);
  2021. if( character != NULL )
  2022. {// account is already marked as online!
  2023. if( character->server > -1 )
  2024. { //Kick it from the map server it is on.
  2025. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2026. if (character->waiting_disconnect == INVALID_TIMER)
  2027. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, chardb_waiting_disconnect, character->account_id, 0);
  2028. }
  2029. else
  2030. {// Manual kick from char server.
  2031. struct char_session_data *tsd;
  2032. int i;
  2033. ARR_FIND( 0, fd_max, i, session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid );
  2034. if( i < fd_max )
  2035. {
  2036. WFIFOHEAD(i,3);
  2037. WFIFOW(i,0) = 0x81;
  2038. WFIFOB(i,2) = 2; // "Someone has already logged in with this id"
  2039. WFIFOSET(i,3);
  2040. set_eof(i);
  2041. }
  2042. else // still moving to the map-server
  2043. set_char_offline(-1, aid);
  2044. }
  2045. }
  2046. idb_remove(auth_db, aid);// reject auth attempts from map-server
  2047. }
  2048. break;
  2049. // ip address update signal from login server
  2050. case 0x2735:
  2051. {
  2052. unsigned char buf[2];
  2053. uint32 new_ip = 0;
  2054. WBUFW(buf,0) = 0x2b1e;
  2055. mapif_sendall(buf, 2);
  2056. new_ip = host2ip(login_ip_str);
  2057. if (new_ip && new_ip != login_ip)
  2058. login_ip = new_ip; //Update login ip, too.
  2059. new_ip = host2ip(char_ip_str);
  2060. if (new_ip && new_ip != char_ip)
  2061. { //Update ip.
  2062. char_ip = new_ip;
  2063. ShowInfo("Updating IP for [%s].\n", char_ip_str);
  2064. // notify login server about the change
  2065. WFIFOHEAD(fd,6);
  2066. WFIFOW(fd,0) = 0x2736;
  2067. WFIFOL(fd,2) = htonl(char_ip);
  2068. WFIFOSET(fd,6);
  2069. }
  2070. RFIFOSKIP(fd,2);
  2071. }
  2072. break;
  2073. default:
  2074. ShowError("Unknown packet 0x%04x received from login-server, disconnecting.\n", command);
  2075. set_eof(fd);
  2076. return 0;
  2077. }
  2078. }
  2079. RFIFOFLUSH(fd);
  2080. return 0;
  2081. }
  2082. int check_connect_login_server(int tid, unsigned int tick, int id, intptr_t data);
  2083. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data);
  2084. void do_init_loginif(void)
  2085. {
  2086. // establish char-login connection if not present
  2087. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  2088. add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000);
  2089. // send a list of all online account IDs to login server
  2090. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  2091. add_timer_interval(gettick() + 1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour
  2092. }
  2093. void do_final_loginif(void)
  2094. {
  2095. if( login_fd != -1 )
  2096. {
  2097. do_close(login_fd);
  2098. login_fd = -1;
  2099. }
  2100. }
  2101. int request_accreg2(int account_id, int char_id)
  2102. {
  2103. if (login_fd > 0) {
  2104. WFIFOHEAD(login_fd,10);
  2105. WFIFOW(login_fd,0) = 0x272e;
  2106. WFIFOL(login_fd,2) = account_id;
  2107. WFIFOL(login_fd,6) = char_id;
  2108. WFIFOSET(login_fd,10);
  2109. return 1;
  2110. }
  2111. return 0;
  2112. }
  2113. //Send packet forward to login-server for account saving
  2114. int save_accreg2(unsigned char* buf, int len)
  2115. {
  2116. if (login_fd > 0) {
  2117. WFIFOHEAD(login_fd,len+4);
  2118. memcpy(WFIFOP(login_fd,4), buf, len);
  2119. WFIFOW(login_fd,0) = 0x2728;
  2120. WFIFOW(login_fd,2) = len+4;
  2121. WFIFOSET(login_fd,len+4);
  2122. return 1;
  2123. }
  2124. return 0;
  2125. }
  2126. void char_read_fame_list(void)
  2127. {
  2128. int i;
  2129. char* data;
  2130. size_t len;
  2131. // Empty ranking lists
  2132. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  2133. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  2134. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  2135. // Build Blacksmith ranking list
  2136. 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", char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, JOB_MECHANIC, JOB_MECHANIC_T, JOB_BABY_MECHANIC, fame_list_size_smith) )
  2137. Sql_ShowDebug(sql_handle);
  2138. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2139. {
  2140. // char_id
  2141. Sql_GetData(sql_handle, 0, &data, NULL);
  2142. smith_fame_list[i].id = atoi(data);
  2143. // fame
  2144. Sql_GetData(sql_handle, 1, &data, &len);
  2145. smith_fame_list[i].fame = atoi(data);
  2146. // name
  2147. Sql_GetData(sql_handle, 2, &data, &len);
  2148. memcpy(smith_fame_list[i].name, data, min(len, NAME_LENGTH));
  2149. }
  2150. // Build Alchemist ranking list
  2151. 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", char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, JOB_GENETIC, JOB_GENETIC_T, JOB_BABY_GENETIC, fame_list_size_chemist) )
  2152. Sql_ShowDebug(sql_handle);
  2153. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2154. {
  2155. // char_id
  2156. Sql_GetData(sql_handle, 0, &data, NULL);
  2157. chemist_fame_list[i].id = atoi(data);
  2158. // fame
  2159. Sql_GetData(sql_handle, 1, &data, &len);
  2160. chemist_fame_list[i].fame = atoi(data);
  2161. // name
  2162. Sql_GetData(sql_handle, 2, &data, &len);
  2163. memcpy(chemist_fame_list[i].name, data, min(len, NAME_LENGTH));
  2164. }
  2165. // Build Taekwon ranking list
  2166. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_TAEKWON, fame_list_size_taekwon) )
  2167. Sql_ShowDebug(sql_handle);
  2168. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2169. {
  2170. // char_id
  2171. Sql_GetData(sql_handle, 0, &data, NULL);
  2172. taekwon_fame_list[i].id = atoi(data);
  2173. // fame
  2174. Sql_GetData(sql_handle, 1, &data, &len);
  2175. taekwon_fame_list[i].fame = atoi(data);
  2176. // name
  2177. Sql_GetData(sql_handle, 2, &data, &len);
  2178. memcpy(taekwon_fame_list[i].name, data, min(len, NAME_LENGTH));
  2179. }
  2180. Sql_FreeResult(sql_handle);
  2181. }
  2182. // Send map-servers the fame ranking lists
  2183. int char_send_fame_list(int fd)
  2184. {
  2185. int i, len = 8;
  2186. unsigned char buf[32000];
  2187. WBUFW(buf,0) = 0x2b1b;
  2188. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  2189. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  2190. len += sizeof(struct fame_list);
  2191. }
  2192. // add blacksmith's block length
  2193. WBUFW(buf, 6) = len;
  2194. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  2195. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  2196. len += sizeof(struct fame_list);
  2197. }
  2198. // add alchemist's block length
  2199. WBUFW(buf, 4) = len;
  2200. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  2201. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  2202. len += sizeof(struct fame_list);
  2203. }
  2204. // add total packet length
  2205. WBUFW(buf, 2) = len;
  2206. if (fd != -1)
  2207. mapif_send(fd, buf, len);
  2208. else
  2209. mapif_sendall(buf, len);
  2210. return 0;
  2211. }
  2212. void char_update_fame_list(int type, int index, int fame)
  2213. {
  2214. unsigned char buf[8];
  2215. WBUFW(buf,0) = 0x2b22;
  2216. WBUFB(buf,2) = type;
  2217. WBUFB(buf,3) = index;
  2218. WBUFL(buf,4) = fame;
  2219. mapif_sendall(buf, 8);
  2220. }
  2221. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  2222. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  2223. int char_loadName(int char_id, char* name)
  2224. {
  2225. char* data;
  2226. size_t len;
  2227. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  2228. Sql_ShowDebug(sql_handle);
  2229. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  2230. {
  2231. Sql_GetData(sql_handle, 0, &data, &len);
  2232. safestrncpy(name, data, NAME_LENGTH);
  2233. return 1;
  2234. }
  2235. else
  2236. {
  2237. safestrncpy(name, unknown_char_name, NAME_LENGTH);
  2238. }
  2239. return 0;
  2240. }
  2241. int search_mapserver(unsigned short map, uint32 ip, uint16 port);
  2242. /// Initializes a server structure.
  2243. void mapif_server_init(int id)
  2244. {
  2245. memset(&server[id], 0, sizeof(server[id]));
  2246. server[id].fd = -1;
  2247. }
  2248. /// Destroys a server structure.
  2249. void mapif_server_destroy(int id)
  2250. {
  2251. if( server[id].fd == -1 )
  2252. {
  2253. do_close(server[id].fd);
  2254. server[id].fd = -1;
  2255. }
  2256. }
  2257. /// Resets all the data related to a server.
  2258. void mapif_server_reset(int id)
  2259. {
  2260. int i,j;
  2261. unsigned char buf[16384];
  2262. int fd = server[id].fd;
  2263. //Notify other map servers that this one is gone. [Skotlex]
  2264. WBUFW(buf,0) = 0x2b20;
  2265. WBUFL(buf,4) = htonl(server[id].ip);
  2266. WBUFW(buf,8) = htons(server[id].port);
  2267. j = 0;
  2268. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2269. if (server[id].map[i])
  2270. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  2271. if (j > 0) {
  2272. WBUFW(buf,2) = j * 4 + 10;
  2273. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2274. }
  2275. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `index`='%d'", ragsrvinfo_db, server[id].fd) )
  2276. Sql_ShowDebug(sql_handle);
  2277. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  2278. mapif_server_destroy(id);
  2279. mapif_server_init(id);
  2280. }
  2281. /// Called when the connection to a Map Server is disconnected.
  2282. void mapif_on_disconnect(int id)
  2283. {
  2284. ShowStatus("Map-server #%d has disconnected.\n", id);
  2285. mapif_server_reset(id);
  2286. }
  2287. int parse_frommap(int fd)
  2288. {
  2289. int i, j;
  2290. int id;
  2291. ARR_FIND( 0, ARRAYLENGTH(server), id, server[id].fd == fd );
  2292. if( id == ARRAYLENGTH(server) )
  2293. {// not a map server
  2294. ShowDebug("parse_frommap: Disconnecting invalid session #%d (is not a map-server)\n", fd);
  2295. do_close(fd);
  2296. return 0;
  2297. }
  2298. if( session[fd]->flag.eof )
  2299. {
  2300. do_close(fd);
  2301. server[id].fd = -1;
  2302. mapif_on_disconnect(id);
  2303. return 0;
  2304. }
  2305. while(RFIFOREST(fd) >= 2)
  2306. {
  2307. switch(RFIFOW(fd,0))
  2308. {
  2309. case 0x2afa: // Receiving map names list from the map-server
  2310. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2311. return 0;
  2312. memset(server[id].map, 0, sizeof(server[id].map));
  2313. j = 0;
  2314. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  2315. server[id].map[j] = RFIFOW(fd,i);
  2316. j++;
  2317. }
  2318. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  2319. id, j, CONVIP(server[id].ip), server[id].port);
  2320. ShowStatus("Map-server %d loading complete.\n", id);
  2321. // send name for wisp to player
  2322. WFIFOHEAD(fd, 3 + NAME_LENGTH);
  2323. WFIFOW(fd,0) = 0x2afb;
  2324. WFIFOB(fd,2) = 0;
  2325. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH);
  2326. WFIFOSET(fd,3+NAME_LENGTH);
  2327. char_send_fame_list(fd); //Send fame list.
  2328. {
  2329. unsigned char buf[16384];
  2330. int x;
  2331. if (j == 0) {
  2332. ShowWarning("Map-server %d has NO maps.\n", id);
  2333. } else {
  2334. // Transmitting maps information to the other map-servers
  2335. WBUFW(buf,0) = 0x2b04;
  2336. WBUFW(buf,2) = j * 4 + 10;
  2337. WBUFL(buf,4) = htonl(server[id].ip);
  2338. WBUFW(buf,8) = htons(server[id].port);
  2339. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  2340. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2341. }
  2342. // Transmitting the maps of the other map-servers to the new map-server
  2343. for(x = 0; x < ARRAYLENGTH(server); x++) {
  2344. if (server[x].fd > 0 && x != id) {
  2345. WFIFOHEAD(fd,10 +4*ARRAYLENGTH(server[x].map));
  2346. WFIFOW(fd,0) = 0x2b04;
  2347. WFIFOL(fd,4) = htonl(server[x].ip);
  2348. WFIFOW(fd,8) = htons(server[x].port);
  2349. j = 0;
  2350. for(i = 0; i < ARRAYLENGTH(server[x].map); i++)
  2351. if (server[x].map[i])
  2352. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  2353. if (j > 0) {
  2354. WFIFOW(fd,2) = j * 4 + 10;
  2355. WFIFOSET(fd,WFIFOW(fd,2));
  2356. }
  2357. }
  2358. }
  2359. }
  2360. RFIFOSKIP(fd,RFIFOW(fd,2));
  2361. break;
  2362. case 0x2afc: //Packet command is now used for sc_data request. [Skotlex]
  2363. if (RFIFOREST(fd) < 10)
  2364. return 0;
  2365. {
  2366. #ifdef ENABLE_SC_SAVING
  2367. int aid, cid;
  2368. aid = RFIFOL(fd,2);
  2369. cid = RFIFOL(fd,6);
  2370. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  2371. scdata_db, aid, cid) )
  2372. {
  2373. Sql_ShowDebug(sql_handle);
  2374. break;
  2375. }
  2376. if( Sql_NumRows(sql_handle) > 0 )
  2377. {
  2378. struct status_change_data scdata;
  2379. int count;
  2380. char* data;
  2381. WFIFOHEAD(fd,14+50*sizeof(struct status_change_data));
  2382. WFIFOW(fd,0) = 0x2b1d;
  2383. WFIFOL(fd,4) = aid;
  2384. WFIFOL(fd,8) = cid;
  2385. for( count = 0; count < 50 && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  2386. {
  2387. Sql_GetData(sql_handle, 0, &data, NULL); scdata.type = atoi(data);
  2388. Sql_GetData(sql_handle, 1, &data, NULL); scdata.tick = atoi(data);
  2389. Sql_GetData(sql_handle, 2, &data, NULL); scdata.val1 = atoi(data);
  2390. Sql_GetData(sql_handle, 3, &data, NULL); scdata.val2 = atoi(data);
  2391. Sql_GetData(sql_handle, 4, &data, NULL); scdata.val3 = atoi(data);
  2392. Sql_GetData(sql_handle, 5, &data, NULL); scdata.val4 = atoi(data);
  2393. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &scdata, sizeof(struct status_change_data));
  2394. }
  2395. if (count >= 50)
  2396. ShowWarning("Too many status changes for %d:%d, some of them were not loaded.\n", aid, cid);
  2397. if (count > 0)
  2398. {
  2399. WFIFOW(fd,2) = 14 + count*sizeof(struct status_change_data);
  2400. WFIFOW(fd,12) = count;
  2401. WFIFOSET(fd,WFIFOW(fd,2));
  2402. //Clear the data once loaded.
  2403. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, aid, cid) )
  2404. Sql_ShowDebug(sql_handle);
  2405. }
  2406. }
  2407. Sql_FreeResult(sql_handle);
  2408. #endif
  2409. RFIFOSKIP(fd, 10);
  2410. }
  2411. break;
  2412. case 0x2afe: //set MAP user count
  2413. if (RFIFOREST(fd) < 4)
  2414. return 0;
  2415. if (RFIFOW(fd,2) != server[id].users) {
  2416. server[id].users = RFIFOW(fd,2);
  2417. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  2418. }
  2419. RFIFOSKIP(fd, 4);
  2420. break;
  2421. case 0x2aff: //set MAP users
  2422. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  2423. return 0;
  2424. {
  2425. //TODO: When data mismatches memory, update guild/party online/offline states.
  2426. int aid, cid;
  2427. struct online_char_data* character;
  2428. server[id].users = RFIFOW(fd,4);
  2429. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  2430. for(i = 0; i < server[id].users; i++) {
  2431. aid = RFIFOL(fd,6+i*8);
  2432. cid = RFIFOL(fd,6+i*8+4);
  2433. character = idb_ensure(online_char_db, aid, create_online_char_data);
  2434. if( character->server > -1 && character->server != id )
  2435. {
  2436. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2437. character->account_id, character->char_id, character->server, id, aid, cid);
  2438. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2439. }
  2440. character->server = id;
  2441. character->char_id = cid;
  2442. }
  2443. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2444. RFIFOSKIP(fd,RFIFOW(fd,2));
  2445. }
  2446. break;
  2447. case 0x2b01: // Receive character data from map-server for saving
  2448. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2449. return 0;
  2450. {
  2451. int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8), size = RFIFOW(fd,2);
  2452. struct online_char_data* character;
  2453. if (size - 13 != sizeof(struct mmo_charstatus))
  2454. {
  2455. ShowError("parse_from_map (save-char): Size mismatch! %d != %d\n", size-13, sizeof(struct mmo_charstatus));
  2456. RFIFOSKIP(fd,size);
  2457. break;
  2458. }
  2459. //Check account only if this ain't final save. Final-save goes through because of the char-map reconnect
  2460. if (RFIFOB(fd,12) || (
  2461. (character = (struct online_char_data*)idb_get(online_char_db, aid)) != NULL &&
  2462. character->char_id == cid))
  2463. {
  2464. struct mmo_charstatus char_dat;
  2465. memcpy(&char_dat, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2466. mmo_char_tosql(cid, &char_dat);
  2467. } else { //This may be valid on char-server reconnection, when re-sending characters that already logged off.
  2468. ShowError("parse_from_map (save-char): Received data for non-existant/offline character (%d:%d).\n", aid, cid);
  2469. set_char_online(id, cid, aid);
  2470. }
  2471. if (RFIFOB(fd,12))
  2472. { //Flag, set character offline after saving. [Skotlex]
  2473. set_char_offline(cid, aid);
  2474. WFIFOHEAD(fd,10);
  2475. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  2476. WFIFOL(fd,2) = aid;
  2477. WFIFOL(fd,6) = cid;
  2478. WFIFOSET(fd,10);
  2479. }
  2480. RFIFOSKIP(fd,size);
  2481. }
  2482. break;
  2483. case 0x2b02: // req char selection
  2484. if( RFIFOREST(fd) < 18 )
  2485. return 0;
  2486. {
  2487. struct auth_node* node;
  2488. int account_id = RFIFOL(fd,2);
  2489. uint32 login_id1 = RFIFOL(fd,6);
  2490. uint32 login_id2 = RFIFOL(fd,10);
  2491. uint32 ip = RFIFOL(fd,14);
  2492. RFIFOSKIP(fd,18);
  2493. if( runflag != CHARSERVER_ST_RUNNING )
  2494. {
  2495. WFIFOHEAD(fd,7);
  2496. WFIFOW(fd,0) = 0x2b03;
  2497. WFIFOL(fd,2) = account_id;
  2498. WFIFOB(fd,6) = 0;// not ok
  2499. WFIFOSET(fd,7);
  2500. }
  2501. else
  2502. {
  2503. // create temporary auth entry
  2504. CREATE(node, struct auth_node, 1);
  2505. node->account_id = account_id;
  2506. node->char_id = 0;
  2507. node->login_id1 = login_id1;
  2508. node->login_id2 = login_id2;
  2509. //node->sex = 0;
  2510. node->ip = ntohl(ip);
  2511. //node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  2512. //node->gmlevel = 0;
  2513. idb_put(auth_db, account_id, node);
  2514. //Set char to "@ char select" in online db [Kevin]
  2515. set_char_charselect(account_id);
  2516. WFIFOHEAD(fd,7);
  2517. WFIFOW(fd,0) = 0x2b03;
  2518. WFIFOL(fd,2) = account_id;
  2519. WFIFOB(fd,6) = 1;// ok
  2520. WFIFOSET(fd,7);
  2521. }
  2522. }
  2523. break;
  2524. case 0x2b05: // request "change map server"
  2525. if (RFIFOREST(fd) < 39)
  2526. return 0;
  2527. {
  2528. int map_id, map_fd = -1;
  2529. struct online_char_data* data;
  2530. struct mmo_charstatus* char_data;
  2531. struct mmo_charstatus char_dat;
  2532. map_id = search_mapserver(RFIFOW(fd,18), ntohl(RFIFOL(fd,24)), ntohs(RFIFOW(fd,28))); //Locate mapserver by ip and port.
  2533. if (map_id >= 0)
  2534. map_fd = server[map_id].fd;
  2535. //Char should just had been saved before this packet, so this should be safe. [Skotlex]
  2536. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2537. if (char_data == NULL) { //Really shouldn't happen.
  2538. mmo_char_fromsql(RFIFOL(fd,14), &char_dat, true);
  2539. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2540. }
  2541. if( runflag == CHARSERVER_ST_RUNNING &&
  2542. session_isActive(map_fd) &&
  2543. char_data )
  2544. { //Send the map server the auth of this player.
  2545. struct auth_node* node;
  2546. //Update the "last map" as this is where the player must be spawned on the new map server.
  2547. char_data->last_point.map = RFIFOW(fd,18);
  2548. char_data->last_point.x = RFIFOW(fd,20);
  2549. char_data->last_point.y = RFIFOW(fd,22);
  2550. char_data->sex = RFIFOB(fd,30);
  2551. // create temporary auth entry
  2552. CREATE(node, struct auth_node, 1);
  2553. node->account_id = RFIFOL(fd,2);
  2554. node->char_id = RFIFOL(fd,14);
  2555. node->login_id1 = RFIFOL(fd,6);
  2556. node->login_id2 = RFIFOL(fd,10);
  2557. node->sex = RFIFOB(fd,30);
  2558. node->expiration_time = 0; // FIXME (this thing isn't really supported we could as well purge it instead of fixing)
  2559. node->ip = ntohl(RFIFOL(fd,31));
  2560. node->group_id = RFIFOL(fd,35);
  2561. node->changing_mapservers = 1;
  2562. idb_put(auth_db, RFIFOL(fd,2), node);
  2563. data = idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
  2564. data->char_id = char_data->char_id;
  2565. data->server = map_id; //Update server where char is.
  2566. //Reply with an ack.
  2567. WFIFOHEAD(fd,30);
  2568. WFIFOW(fd,0) = 0x2b06;
  2569. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2570. WFIFOSET(fd,30);
  2571. } else { //Reply with nak
  2572. WFIFOHEAD(fd,30);
  2573. WFIFOW(fd,0) = 0x2b06;
  2574. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2575. WFIFOL(fd,6) = 0; //Set login1 to 0.
  2576. WFIFOSET(fd,30);
  2577. }
  2578. RFIFOSKIP(fd,39);
  2579. }
  2580. break;
  2581. case 0x2b07: // Remove RFIFOL(fd,6) (friend_id) from RFIFOL(fd,2) (char_id) friend list [Ind]
  2582. if (RFIFOREST(fd) < 10)
  2583. return 0;
  2584. {
  2585. int char_id, friend_id;
  2586. char_id = RFIFOL(fd,2);
  2587. friend_id = RFIFOL(fd,6);
  2588. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `friend_id`='%d' LIMIT 1",
  2589. friend_db, char_id, friend_id) ) {
  2590. Sql_ShowDebug(sql_handle);
  2591. break;
  2592. }
  2593. RFIFOSKIP(fd,10);
  2594. }
  2595. break;
  2596. case 0x2b08: // char name request
  2597. if (RFIFOREST(fd) < 6)
  2598. return 0;
  2599. WFIFOHEAD(fd,30);
  2600. WFIFOW(fd,0) = 0x2b09;
  2601. WFIFOL(fd,2) = RFIFOL(fd,2);
  2602. char_loadName((int)RFIFOL(fd,2), (char*)WFIFOP(fd,6));
  2603. WFIFOSET(fd,30);
  2604. RFIFOSKIP(fd,6);
  2605. break;
  2606. case 0x2b0c: // Map server send information to change an email of an account -> login-server
  2607. if (RFIFOREST(fd) < 86)
  2608. return 0;
  2609. if (login_fd > 0) { // don't send request if no login-server
  2610. WFIFOHEAD(login_fd,86);
  2611. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2612. WFIFOW(login_fd,0) = 0x2722;
  2613. WFIFOSET(login_fd,86);
  2614. }
  2615. RFIFOSKIP(fd, 86);
  2616. break;
  2617. case 0x2b0e: // Request from map-server to change an account's status (will just be forwarded to login server)
  2618. if (RFIFOREST(fd) < 44)
  2619. return 0;
  2620. {
  2621. int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline
  2622. char esc_name[NAME_LENGTH*2+1];
  2623. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  2624. const char* name = (char*)RFIFOP(fd,6); // name of the target character
  2625. int type = RFIFOW(fd,30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  2626. short year = RFIFOW(fd,32);
  2627. short month = RFIFOW(fd,34);
  2628. short day = RFIFOW(fd,36);
  2629. short hour = RFIFOW(fd,38);
  2630. short minute = RFIFOW(fd,40);
  2631. short second = RFIFOW(fd,42);
  2632. RFIFOSKIP(fd,44);
  2633. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  2634. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'", char_db, esc_name) )
  2635. Sql_ShowDebug(sql_handle);
  2636. else
  2637. if( Sql_NumRows(sql_handle) == 0 )
  2638. {
  2639. result = 1; // 1-player not found
  2640. }
  2641. else
  2642. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  2643. Sql_ShowDebug(sql_handle);
  2644. //FIXME: set proper result value?
  2645. else
  2646. {
  2647. char name[NAME_LENGTH];
  2648. int account_id;
  2649. char* data;
  2650. Sql_GetData(sql_handle, 0, &data, NULL); account_id = atoi(data);
  2651. Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(name, data, sizeof(name));
  2652. if( login_fd <= 0 )
  2653. result = 3; // 3-login-server offline
  2654. //FIXME: need to move this check to login server [ultramage]
  2655. // else
  2656. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  2657. // result = 2; // 2-gm level too low
  2658. else
  2659. switch( type ) {
  2660. case 1: // block
  2661. WFIFOHEAD(login_fd,10);
  2662. WFIFOW(login_fd,0) = 0x2724;
  2663. WFIFOL(login_fd,2) = account_id;
  2664. WFIFOL(login_fd,6) = 5; // new account status
  2665. WFIFOSET(login_fd,10);
  2666. break;
  2667. case 2: // ban
  2668. WFIFOHEAD(login_fd,18);
  2669. WFIFOW(login_fd, 0) = 0x2725;
  2670. WFIFOL(login_fd, 2) = account_id;
  2671. WFIFOW(login_fd, 6) = year;
  2672. WFIFOW(login_fd, 8) = month;
  2673. WFIFOW(login_fd,10) = day;
  2674. WFIFOW(login_fd,12) = hour;
  2675. WFIFOW(login_fd,14) = minute;
  2676. WFIFOW(login_fd,16) = second;
  2677. WFIFOSET(login_fd,18);
  2678. break;
  2679. case 3: // unblock
  2680. WFIFOHEAD(login_fd,10);
  2681. WFIFOW(login_fd,0) = 0x2724;
  2682. WFIFOL(login_fd,2) = account_id;
  2683. WFIFOL(login_fd,6) = 0; // new account status
  2684. WFIFOSET(login_fd,10);
  2685. break;
  2686. case 4: // unban
  2687. WFIFOHEAD(login_fd,6);
  2688. WFIFOW(login_fd,0) = 0x272a;
  2689. WFIFOL(login_fd,2) = account_id;
  2690. WFIFOSET(login_fd,6);
  2691. break;
  2692. case 5: // changesex
  2693. WFIFOHEAD(login_fd,6);
  2694. WFIFOW(login_fd,0) = 0x2727;
  2695. WFIFOL(login_fd,2) = account_id;
  2696. WFIFOSET(login_fd,6);
  2697. break;
  2698. }
  2699. }
  2700. Sql_FreeResult(sql_handle);
  2701. // send answer if a player ask, not if the server ask
  2702. if( acc != -1 && type != 5) { // Don't send answer for changesex
  2703. WFIFOHEAD(fd,34);
  2704. WFIFOW(fd, 0) = 0x2b0f;
  2705. WFIFOL(fd, 2) = acc;
  2706. safestrncpy((char*)WFIFOP(fd,6), name, NAME_LENGTH);
  2707. WFIFOW(fd,30) = type;
  2708. WFIFOW(fd,32) = result;
  2709. WFIFOSET(fd,34);
  2710. }
  2711. }
  2712. break;
  2713. case 0x2b10: // Update and send fame ranking list
  2714. if (RFIFOREST(fd) < 11)
  2715. return 0;
  2716. {
  2717. int cid = RFIFOL(fd, 2);
  2718. int fame = RFIFOL(fd, 6);
  2719. char type = RFIFOB(fd, 10);
  2720. int size;
  2721. struct fame_list* list;
  2722. int player_pos;
  2723. int fame_pos;
  2724. switch(type)
  2725. {
  2726. case 1: size = fame_list_size_smith; list = smith_fame_list; break;
  2727. case 2: size = fame_list_size_chemist; list = chemist_fame_list; break;
  2728. case 3: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  2729. default: size = 0; list = NULL; break;
  2730. }
  2731. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  2732. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  2733. if( player_pos == size && fame_pos == size )
  2734. ;// not on list and not enough fame to get on it
  2735. else if( fame_pos == player_pos )
  2736. {// same position
  2737. list[player_pos].fame = fame;
  2738. char_update_fame_list(type, player_pos, fame);
  2739. }
  2740. else
  2741. {// move in the list
  2742. if( player_pos == size )
  2743. {// new ranker - not in the list
  2744. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  2745. list[fame_pos].id = cid;
  2746. list[fame_pos].fame = fame;
  2747. char_loadName(cid, list[fame_pos].name);
  2748. }
  2749. else
  2750. {// already in the list
  2751. if( fame_pos == size )
  2752. --fame_pos;// move to the end of the list
  2753. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  2754. list[fame_pos].fame = fame;
  2755. }
  2756. char_send_fame_list(-1);
  2757. }
  2758. RFIFOSKIP(fd,11);
  2759. }
  2760. break;
  2761. // Divorce chars
  2762. case 0x2b11:
  2763. if( RFIFOREST(fd) < 10 )
  2764. return 0;
  2765. divorce_char_sql(RFIFOL(fd,2), RFIFOL(fd,6));
  2766. RFIFOSKIP(fd,10);
  2767. break;
  2768. case 0x2b16: // Receive rates [Wizputer]
  2769. if( RFIFOREST(fd) < 14 )
  2770. return 0;
  2771. {
  2772. char esc_server_name[sizeof(server_name)*2+1];
  2773. Sql_EscapeString(sql_handle, esc_server_name, server_name);
  2774. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d'",
  2775. ragsrvinfo_db, fd, esc_server_name, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)) )
  2776. Sql_ShowDebug(sql_handle);
  2777. RFIFOSKIP(fd,14);
  2778. }
  2779. break;
  2780. case 0x2b17: // Character disconnected set online 0 [Wizputer]
  2781. if (RFIFOREST(fd) < 6)
  2782. return 0;
  2783. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  2784. RFIFOSKIP(fd,10);
  2785. break;
  2786. case 0x2b18: // Reset all chars to offline [Wizputer]
  2787. set_all_offline(id);
  2788. RFIFOSKIP(fd,2);
  2789. break;
  2790. case 0x2b19: // Character set online [Wizputer]
  2791. if (RFIFOREST(fd) < 10)
  2792. return 0;
  2793. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  2794. RFIFOSKIP(fd,10);
  2795. break;
  2796. case 0x2b1a: // Build and send fame ranking lists [DracoRPG]
  2797. if (RFIFOREST(fd) < 2)
  2798. return 0;
  2799. char_read_fame_list();
  2800. char_send_fame_list(-1);
  2801. RFIFOSKIP(fd,2);
  2802. break;
  2803. case 0x2b1c: //Request to save status change data. [Skotlex]
  2804. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2805. return 0;
  2806. {
  2807. #ifdef ENABLE_SC_SAVING
  2808. int count, aid, cid;
  2809. aid = RFIFOL(fd, 4);
  2810. cid = RFIFOL(fd, 8);
  2811. count = RFIFOW(fd, 12);
  2812. if( count > 0 )
  2813. {
  2814. struct status_change_data data;
  2815. StringBuf buf;
  2816. int i;
  2817. StringBuf_Init(&buf);
  2818. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", scdata_db);
  2819. for( i = 0; i < count; ++i )
  2820. {
  2821. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  2822. if( i > 0 )
  2823. StringBuf_AppendStr(&buf, ", ");
  2824. StringBuf_Printf(&buf, "('%d','%d','%hu','%d','%d','%d','%d','%d')", aid, cid,
  2825. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  2826. }
  2827. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  2828. Sql_ShowDebug(sql_handle);
  2829. StringBuf_Destroy(&buf);
  2830. }
  2831. #endif
  2832. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2833. }
  2834. break;
  2835. case 0x2b23: // map-server alive packet
  2836. WFIFOHEAD(fd,2);
  2837. WFIFOW(fd,0) = 0x2b24;
  2838. WFIFOSET(fd,2);
  2839. RFIFOSKIP(fd,2);
  2840. break;
  2841. case 0x2b26: // auth request from map-server
  2842. if (RFIFOREST(fd) < 19)
  2843. return 0;
  2844. {
  2845. int account_id;
  2846. int char_id;
  2847. int login_id1;
  2848. char sex;
  2849. uint32 ip;
  2850. struct auth_node* node;
  2851. struct mmo_charstatus* cd;
  2852. struct mmo_charstatus char_dat;
  2853. account_id = RFIFOL(fd,2);
  2854. char_id = RFIFOL(fd,6);
  2855. login_id1 = RFIFOL(fd,10);
  2856. sex = RFIFOB(fd,14);
  2857. ip = ntohl(RFIFOL(fd,15));
  2858. RFIFOSKIP(fd,19);
  2859. node = (struct auth_node*)idb_get(auth_db, account_id);
  2860. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  2861. if( cd == NULL )
  2862. { //Really shouldn't happen.
  2863. mmo_char_fromsql(char_id, &char_dat, true);
  2864. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  2865. }
  2866. if( runflag == CHARSERVER_ST_RUNNING &&
  2867. cd != NULL &&
  2868. node != NULL &&
  2869. node->account_id == account_id &&
  2870. node->char_id == char_id &&
  2871. node->login_id1 == login_id1 &&
  2872. node->sex == sex /*&&
  2873. node->ip == ip*/ )
  2874. {// auth ok
  2875. cd->sex = sex;
  2876. WFIFOHEAD(fd,25 + sizeof(struct mmo_charstatus));
  2877. WFIFOW(fd,0) = 0x2afd;
  2878. WFIFOW(fd,2) = 25 + sizeof(struct mmo_charstatus);
  2879. WFIFOL(fd,4) = account_id;
  2880. WFIFOL(fd,8) = node->login_id1;
  2881. WFIFOL(fd,12) = node->login_id2;
  2882. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  2883. WFIFOL(fd,20) = node->group_id;
  2884. WFIFOB(fd,24) = node->changing_mapservers;
  2885. memcpy(WFIFOP(fd,25), cd, sizeof(struct mmo_charstatus));
  2886. WFIFOSET(fd, WFIFOW(fd,2));
  2887. // only use the auth once and mark user online
  2888. idb_remove(auth_db, account_id);
  2889. set_char_online(id, char_id, account_id);
  2890. }
  2891. else
  2892. {// auth failed
  2893. WFIFOHEAD(fd,19);
  2894. WFIFOW(fd,0) = 0x2b27;
  2895. WFIFOL(fd,2) = account_id;
  2896. WFIFOL(fd,6) = char_id;
  2897. WFIFOL(fd,10) = login_id1;
  2898. WFIFOB(fd,14) = sex;
  2899. WFIFOL(fd,15) = htonl(ip);
  2900. WFIFOSET(fd,19);
  2901. }
  2902. }
  2903. break;
  2904. case 0x2736: // ip address update
  2905. if (RFIFOREST(fd) < 6) return 0;
  2906. server[id].ip = ntohl(RFIFOL(fd, 2));
  2907. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(server[id].ip));
  2908. RFIFOSKIP(fd,6);
  2909. break;
  2910. case 0x3008:
  2911. if( RFIFOREST(fd) < RFIFOW(fd,4) )
  2912. return 0;/* packet wasn't fully received yet (still fragmented) */
  2913. else {
  2914. int sfd;/* stat server fd */
  2915. RFIFOSKIP(fd, 2);/* we skip first 2 bytes which are the 0x3008, so we end up with a buffer equal to the one we send */
  2916. if( (sfd = make_connection(host2ip("stats.rathena.org"),(uint16)25421,true) ) == -1 ) {
  2917. RFIFOSKIP(fd, RFIFOW(fd,2) );/* skip this packet */
  2918. break;/* connection not possible, we drop the report */
  2919. }
  2920. session[sfd]->flag.server = 1;/* to ensure we won't drop our own packet */
  2921. WFIFOHEAD(sfd, RFIFOW(fd,2) );
  2922. memcpy((char*)WFIFOP(sfd,0), (char*)RFIFOP(fd, 0), RFIFOW(fd,2));
  2923. WFIFOSET(sfd, RFIFOW(fd,2) );
  2924. flush_fifo(sfd);
  2925. do_close(sfd);
  2926. RFIFOSKIP(fd, RFIFOW(fd,2) );/* skip this packet */
  2927. }
  2928. break;
  2929. default:
  2930. {
  2931. // inter server - packet
  2932. int r = inter_parse_frommap(fd);
  2933. if (r == 1) break; // processed
  2934. if (r == 2) return 0; // need more packet
  2935. // no inter server packet. no char server packet -> disconnect
  2936. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  2937. set_eof(fd);
  2938. return 0;
  2939. }
  2940. } // switch
  2941. } // while
  2942. return 0;
  2943. }
  2944. void do_init_mapif(void)
  2945. {
  2946. int i;
  2947. for( i = 0; i < ARRAYLENGTH(server); ++i )
  2948. mapif_server_init(i);
  2949. }
  2950. void do_final_mapif(void)
  2951. {
  2952. int i;
  2953. for( i = 0; i < ARRAYLENGTH(server); ++i )
  2954. mapif_server_destroy(i);
  2955. }
  2956. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  2957. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  2958. int search_mapserver(unsigned short map, uint32 ip, uint16 port)
  2959. {
  2960. int i, j;
  2961. for(i = 0; i < ARRAYLENGTH(server); i++)
  2962. {
  2963. if (server[i].fd > 0
  2964. && (ip == (uint32)-1 || server[i].ip == ip)
  2965. && (port == (uint16)-1 || server[i].port == port))
  2966. {
  2967. for (j = 0; server[i].map[j]; j++)
  2968. if (server[i].map[j] == map)
  2969. return i;
  2970. }
  2971. }
  2972. return -1;
  2973. }
  2974. // Initialization process (currently only initialization inter_mapif)
  2975. static int char_mapif_init(int fd)
  2976. {
  2977. return inter_mapif_init(fd);
  2978. }
  2979. //--------------------------------------------
  2980. // Test to know if an IP come from LAN or WAN.
  2981. //--------------------------------------------
  2982. int lan_subnetcheck(uint32 ip)
  2983. {
  2984. int i;
  2985. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  2986. if( i < subnet_count ) {
  2987. 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));
  2988. return subnet[i].map_ip;
  2989. } else {
  2990. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", CONVIP(ip));
  2991. return 0;
  2992. }
  2993. }
  2994. /// @param result
  2995. /// 0 (0x718): An unknown error has occurred.
  2996. /// 1: none/success
  2997. /// 3 (0x719): A database error occurred.
  2998. /// 4 (0x71a): To delete a character you must withdraw from the guild.
  2999. /// 5 (0x71b): To delete a character you must withdraw from the party.
  3000. /// Any (0x718): An unknown error has occurred.
  3001. void char_delete2_ack(int fd, int char_id, uint32 result, time_t delete_date)
  3002. {// HC: <0828>.W <char id>.L <Msg:0-5>.L <deleteDate>.L
  3003. WFIFOHEAD(fd,14);
  3004. WFIFOW(fd,0) = 0x828;
  3005. WFIFOL(fd,2) = char_id;
  3006. WFIFOL(fd,6) = result;
  3007. WFIFOL(fd,10) = TOL(delete_date);
  3008. WFIFOSET(fd,14);
  3009. }
  3010. /// @param result
  3011. /// 0 (0x718): An unknown error has occurred.
  3012. /// 1: none/success
  3013. /// 2 (0x71c): Due to system settings can not be deleted.
  3014. /// 3 (0x719): A database error occurred.
  3015. /// 4 (0x71d): Deleting not yet possible time.
  3016. /// 5 (0x71e): Date of birth do not match.
  3017. /// Any (0x718): An unknown error has occurred.
  3018. void char_delete2_accept_ack(int fd, int char_id, uint32 result)
  3019. {// HC: <082a>.W <char id>.L <Msg:0-5>.L
  3020. WFIFOHEAD(fd,10);
  3021. WFIFOW(fd,0) = 0x82a;
  3022. WFIFOL(fd,2) = char_id;
  3023. WFIFOL(fd,6) = result;
  3024. WFIFOSET(fd,10);
  3025. }
  3026. /// @param result
  3027. /// 1 (0x718): none/success, (if char id not in deletion process): An unknown error has occurred.
  3028. /// 2 (0x719): A database error occurred.
  3029. /// Any (0x718): An unknown error has occurred.
  3030. void char_delete2_cancel_ack(int fd, int char_id, uint32 result)
  3031. {// HC: <082c>.W <char id>.L <Msg:1-2>.L
  3032. WFIFOHEAD(fd,10);
  3033. WFIFOW(fd,0) = 0x82c;
  3034. WFIFOL(fd,2) = char_id;
  3035. WFIFOL(fd,6) = result;
  3036. WFIFOSET(fd,10);
  3037. }
  3038. static void char_delete2_req(int fd, struct char_session_data* sd)
  3039. {// CH: <0827>.W <char id>.L
  3040. int char_id, i;
  3041. char* data;
  3042. time_t delete_date;
  3043. char_id = RFIFOL(fd,2);
  3044. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3045. if( i == MAX_CHARS )
  3046. {// character not found
  3047. char_delete2_ack(fd, char_id, 3, 0);
  3048. return;
  3049. }
  3050. if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) )
  3051. {
  3052. Sql_ShowDebug(sql_handle);
  3053. char_delete2_ack(fd, char_id, 3, 0);
  3054. return;
  3055. }
  3056. Sql_GetData(sql_handle, 0, &data, NULL); delete_date = strtoul(data, NULL, 10);
  3057. if( delete_date ) {// character already queued for deletion
  3058. char_delete2_ack(fd, char_id, 0, 0);
  3059. return;
  3060. }
  3061. /*
  3062. // Aegis imposes these checks probably to avoid dead member
  3063. // entries in guilds/parties, otherwise they are not required.
  3064. // TODO: Figure out how these are enforced during waiting.
  3065. if( guild_id )
  3066. {// character in guild
  3067. char_delete2_ack(fd, char_id, 4, 0);
  3068. return;
  3069. }
  3070. if( party_id )
  3071. {// character in party
  3072. char_delete2_ack(fd, char_id, 5, 0);
  3073. return;
  3074. }
  3075. */
  3076. // success
  3077. delete_date = time(NULL)+char_del_delay;
  3078. if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='%lu' WHERE `char_id`='%d'", char_db, (unsigned long)delete_date, char_id) )
  3079. {
  3080. Sql_ShowDebug(sql_handle);
  3081. char_delete2_ack(fd, char_id, 3, 0);
  3082. return;
  3083. }
  3084. char_delete2_ack(fd, char_id, 1, delete_date);
  3085. }
  3086. static void char_delete2_accept(int fd, struct char_session_data* sd)
  3087. {// CH: <0829>.W <char id>.L <birth date:YYMMDD>.6B
  3088. char birthdate[8+1];
  3089. int char_id, i, k;
  3090. unsigned int base_level;
  3091. char* data;
  3092. time_t delete_date;
  3093. char_id = RFIFOL(fd,2);
  3094. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, char_id);
  3095. // construct "YY-MM-DD"
  3096. birthdate[0] = RFIFOB(fd,6);
  3097. birthdate[1] = RFIFOB(fd,7);
  3098. birthdate[2] = '-';
  3099. birthdate[3] = RFIFOB(fd,8);
  3100. birthdate[4] = RFIFOB(fd,9);
  3101. birthdate[5] = '-';
  3102. birthdate[6] = RFIFOB(fd,10);
  3103. birthdate[7] = RFIFOB(fd,11);
  3104. birthdate[8] = 0;
  3105. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3106. if( i == MAX_CHARS )
  3107. {// character not found
  3108. char_delete2_accept_ack(fd, char_id, 3);
  3109. return;
  3110. }
  3111. if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `base_level`,`delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) )
  3112. {// data error
  3113. Sql_ShowDebug(sql_handle);
  3114. char_delete2_accept_ack(fd, char_id, 3);
  3115. return;
  3116. }
  3117. Sql_GetData(sql_handle, 0, &data, NULL); base_level = (unsigned int)strtoul(data, NULL, 10);
  3118. Sql_GetData(sql_handle, 1, &data, NULL); delete_date = strtoul(data, NULL, 10);
  3119. if( !delete_date || delete_date>time(NULL) )
  3120. {// not queued or delay not yet passed
  3121. char_delete2_accept_ack(fd, char_id, 4);
  3122. return;
  3123. }
  3124. if( strcmp(sd->birthdate+2, birthdate) ) // +2 to cut off the century
  3125. {// birth date is wrong
  3126. char_delete2_accept_ack(fd, char_id, 5);
  3127. return;
  3128. }
  3129. if( ( char_del_level > 0 && base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && base_level <= (unsigned int)(-char_del_level) ) )
  3130. {// character level config restriction
  3131. char_delete2_accept_ack(fd, char_id, 2);
  3132. return;
  3133. }
  3134. // success
  3135. if( delete_char_sql(char_id) < 0 )
  3136. {
  3137. char_delete2_accept_ack(fd, char_id, 3);
  3138. return;
  3139. }
  3140. // refresh character list cache
  3141. for(k = i; k < MAX_CHARS-1; k++)
  3142. {
  3143. sd->found_char[k] = sd->found_char[k+1];
  3144. }
  3145. sd->found_char[MAX_CHARS-1] = -1;
  3146. char_delete2_accept_ack(fd, char_id, 1);
  3147. }
  3148. static void char_delete2_cancel(int fd, struct char_session_data* sd)
  3149. {// CH: <082b>.W <char id>.L
  3150. int char_id, i;
  3151. char_id = RFIFOL(fd,2);
  3152. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3153. if( i == MAX_CHARS )
  3154. {// character not found
  3155. char_delete2_cancel_ack(fd, char_id, 2);
  3156. return;
  3157. }
  3158. // there is no need to check, whether or not the character was
  3159. // queued for deletion, as the client prints an error message by
  3160. // itself, if it was not the case (@see char_delete2_cancel_ack)
  3161. if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='0' WHERE `char_id`='%d'", char_db, char_id) )
  3162. {
  3163. Sql_ShowDebug(sql_handle);
  3164. char_delete2_cancel_ack(fd, char_id, 2);
  3165. return;
  3166. }
  3167. char_delete2_cancel_ack(fd, char_id, 1);
  3168. }
  3169. int parse_char(int fd)
  3170. {
  3171. int i, ch;
  3172. char email[40];
  3173. unsigned short cmd;
  3174. int map_fd;
  3175. struct char_session_data* sd;
  3176. uint32 ipl = session[fd]->client_addr;
  3177. sd = (struct char_session_data*)session[fd]->session_data;
  3178. // disconnect any player if no login-server.
  3179. if(login_fd < 0)
  3180. set_eof(fd);
  3181. if(session[fd]->flag.eof)
  3182. {
  3183. if( sd != NULL && sd->auth )
  3184. { // already authed client
  3185. struct online_char_data* data = (struct online_char_data*)idb_get(online_char_db, sd->account_id);
  3186. if( data != NULL && data->fd == fd)
  3187. data->fd = -1;
  3188. if( data == NULL || data->server == -1) //If it is not in any server, send it offline. [Skotlex]
  3189. set_char_offline(-1,sd->account_id);
  3190. }
  3191. do_close(fd);
  3192. return 0;
  3193. }
  3194. while( RFIFOREST(fd) >= 2 )
  3195. {
  3196. //For use in packets that depend on an sd being present [Skotlex]
  3197. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL || !sd->auth) { RFIFOSKIP(fd,rest); return 0; } }
  3198. cmd = RFIFOW(fd,0);
  3199. switch( cmd )
  3200. {
  3201. // request to connect
  3202. // 0065 <account id>.L <login id1>.L <login id2>.L <???>.W <sex>.B
  3203. case 0x65:
  3204. if( RFIFOREST(fd) < 17 )
  3205. return 0;
  3206. {
  3207. struct auth_node* node;
  3208. int account_id = RFIFOL(fd,2);
  3209. uint32 login_id1 = RFIFOL(fd,6);
  3210. uint32 login_id2 = RFIFOL(fd,10);
  3211. int sex = RFIFOB(fd,16);
  3212. RFIFOSKIP(fd,17);
  3213. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", account_id, login_id1, login_id2);
  3214. if (sd) {
  3215. //Received again auth packet for already authentified account?? Discard it.
  3216. //TODO: Perhaps log this as a hack attempt?
  3217. //TODO: and perhaps send back a reply?
  3218. break;
  3219. }
  3220. CREATE(session[fd]->session_data, struct char_session_data, 1);
  3221. sd = (struct char_session_data*)session[fd]->session_data;
  3222. sd->account_id = account_id;
  3223. sd->login_id1 = login_id1;
  3224. sd->login_id2 = login_id2;
  3225. sd->sex = sex;
  3226. sd->auth = false; // not authed yet
  3227. // send back account_id
  3228. WFIFOHEAD(fd,4);
  3229. WFIFOL(fd,0) = account_id;
  3230. WFIFOSET(fd,4);
  3231. if( runflag != CHARSERVER_ST_RUNNING )
  3232. {
  3233. WFIFOHEAD(fd,3);
  3234. WFIFOW(fd,0) = 0x6c;
  3235. WFIFOB(fd,2) = 0;// rejected from server
  3236. WFIFOSET(fd,3);
  3237. break;
  3238. }
  3239. // search authentification
  3240. node = (struct auth_node*)idb_get(auth_db, account_id);
  3241. if( node != NULL &&
  3242. node->account_id == account_id &&
  3243. node->login_id1 == login_id1 &&
  3244. node->login_id2 == login_id2 /*&&
  3245. node->ip == ipl*/ )
  3246. {// authentication found (coming from map server)
  3247. idb_remove(auth_db, account_id);
  3248. char_auth_ok(fd, sd);
  3249. }
  3250. else
  3251. {// authentication not found (coming from login server)
  3252. if (login_fd > 0) { // don't send request if no login-server
  3253. WFIFOHEAD(login_fd,23);
  3254. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  3255. WFIFOL(login_fd,2) = sd->account_id;
  3256. WFIFOL(login_fd,6) = sd->login_id1;
  3257. WFIFOL(login_fd,10) = sd->login_id2;
  3258. WFIFOB(login_fd,14) = sd->sex;
  3259. WFIFOL(login_fd,15) = htonl(ipl);
  3260. WFIFOL(login_fd,19) = fd;
  3261. WFIFOSET(login_fd,23);
  3262. } else { // if no login-server, we must refuse connection
  3263. WFIFOHEAD(fd,3);
  3264. WFIFOW(fd,0) = 0x6c;
  3265. WFIFOB(fd,2) = 0;
  3266. WFIFOSET(fd,3);
  3267. }
  3268. }
  3269. }
  3270. break;
  3271. // char select
  3272. case 0x66:
  3273. FIFOSD_CHECK(3);
  3274. {
  3275. struct mmo_charstatus char_dat;
  3276. struct mmo_charstatus *cd;
  3277. char* data;
  3278. int char_id;
  3279. uint32 subnet_map_ip;
  3280. struct auth_node* node;
  3281. int slot = RFIFOB(fd,2);
  3282. RFIFOSKIP(fd,3);
  3283. if ( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'", char_db, sd->account_id, slot)
  3284. || SQL_SUCCESS != Sql_NextRow(sql_handle)
  3285. || SQL_SUCCESS != Sql_GetData(sql_handle, 0, &data, NULL) )
  3286. { //Not found?? May be forged packet.
  3287. Sql_ShowDebug(sql_handle);
  3288. Sql_FreeResult(sql_handle);
  3289. WFIFOHEAD(fd,3);
  3290. WFIFOW(fd,0) = 0x6c;
  3291. WFIFOB(fd,2) = 0; // rejected from server
  3292. WFIFOSET(fd,3);
  3293. break;
  3294. }
  3295. char_id = atoi(data);
  3296. Sql_FreeResult(sql_handle);
  3297. mmo_char_fromsql(char_id, &char_dat, true);
  3298. //Have to switch over to the DB instance otherwise data won't propagate [Kevin]
  3299. cd = (struct mmo_charstatus *)idb_get(char_db_, char_id);
  3300. cd->sex = sd->sex;
  3301. if (log_char) {
  3302. char esc_name[NAME_LENGTH*2+1];
  3303. Sql_EscapeStringLen(sql_handle, esc_name, char_dat.name, strnlen(char_dat.name, NAME_LENGTH));
  3304. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')",
  3305. charlog_db, sd->account_id, slot, esc_name) )
  3306. Sql_ShowDebug(sql_handle);
  3307. }
  3308. ShowInfo("Selected char: (Account %d: %d - %s)\n", sd->account_id, slot, char_dat.name);
  3309. // searching map server
  3310. i = search_mapserver(cd->last_point.map, -1, -1);
  3311. // if map is not found, we check major cities
  3312. if (i < 0 || !cd->last_point.map) {
  3313. unsigned short j;
  3314. //First check that there's actually a map server online.
  3315. ARR_FIND( 0, ARRAYLENGTH(server), j, server[j].fd >= 0 && server[j].map[0] );
  3316. if (j == ARRAYLENGTH(server)) {
  3317. ShowInfo("Connection Closed. No map servers available.\n");
  3318. WFIFOHEAD(fd,3);
  3319. WFIFOW(fd,0) = 0x81;
  3320. WFIFOB(fd,2) = 1; // 01 = Server closed
  3321. WFIFOSET(fd,3);
  3322. break;
  3323. }
  3324. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  3325. cd->last_point.x = 273;
  3326. cd->last_point.y = 354;
  3327. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  3328. cd->last_point.x = 120;
  3329. cd->last_point.y = 100;
  3330. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  3331. cd->last_point.x = 160;
  3332. cd->last_point.y = 94;
  3333. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  3334. cd->last_point.x = 116;
  3335. cd->last_point.y = 57;
  3336. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  3337. cd->last_point.x = 87;
  3338. cd->last_point.y = 117;
  3339. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  3340. cd->last_point.x = 94;
  3341. cd->last_point.y = 103;
  3342. } else {
  3343. ShowInfo("Connection Closed. No map server available that has a major city, and unable to find map-server for '%s'.\n", mapindex_id2name(cd->last_point.map));
  3344. WFIFOHEAD(fd,3);
  3345. WFIFOW(fd,0) = 0x81;
  3346. WFIFOB(fd,2) = 1; // 01 = Server closed
  3347. WFIFOSET(fd,3);
  3348. break;
  3349. }
  3350. ShowWarning("Unable to find map-server for '%s', sending to major city '%s'.\n", mapindex_id2name(cd->last_point.map), mapindex_id2name(j));
  3351. cd->last_point.map = j;
  3352. }
  3353. //Send NEW auth packet [Kevin]
  3354. //FIXME: is this case even possible? [ultramage]
  3355. if ((map_fd = server[i].fd) < 1 || session[map_fd] == NULL)
  3356. {
  3357. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  3358. server[i].fd = -1;
  3359. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3360. //Send server closed.
  3361. WFIFOHEAD(fd,3);
  3362. WFIFOW(fd,0) = 0x81;
  3363. WFIFOB(fd,2) = 1; // 01 = Server closed
  3364. WFIFOSET(fd,3);
  3365. break;
  3366. }
  3367. //Send player to map
  3368. WFIFOHEAD(fd,28);
  3369. WFIFOW(fd,0) = 0x71;
  3370. WFIFOL(fd,2) = cd->char_id;
  3371. mapindex_getmapname_ext(mapindex_id2name(cd->last_point.map), (char*)WFIFOP(fd,6));
  3372. subnet_map_ip = lan_subnetcheck(ipl); // Advanced subnet check [LuzZza]
  3373. WFIFOL(fd,22) = htonl((subnet_map_ip) ? subnet_map_ip : server[i].ip);
  3374. WFIFOW(fd,26) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  3375. WFIFOSET(fd,28);
  3376. // create temporary auth entry
  3377. CREATE(node, struct auth_node, 1);
  3378. node->account_id = sd->account_id;
  3379. node->char_id = cd->char_id;
  3380. node->login_id1 = sd->login_id1;
  3381. node->login_id2 = sd->login_id2;
  3382. node->sex = sd->sex;
  3383. node->expiration_time = sd->expiration_time;
  3384. node->group_id = sd->group_id;
  3385. node->ip = ipl;
  3386. idb_put(auth_db, sd->account_id, node);
  3387. set_char_online(-2,node->char_id,sd->account_id);
  3388. }
  3389. break;
  3390. // create new char
  3391. #if PACKETVER >= 20120307
  3392. // S 0970 <name>.24B <slot>.B <hair color>.W <hair style>.W
  3393. case 0x970:
  3394. FIFOSD_CHECK(31);
  3395. #else
  3396. // S 0067 <name>.24B <str>.B <agi>.B <vit>.B <int>.B <dex>.B <luk>.B <slot>.B <hair color>.W <hair style>.W
  3397. case 0x67:
  3398. FIFOSD_CHECK(37);
  3399. #endif
  3400. if( !char_new ) //turn character creation on/off [Kevin]
  3401. i = -2;
  3402. else
  3403. #if PACKETVER >= 20120307
  3404. i = make_new_char_sql(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOW(fd,27),RFIFOW(fd,29));
  3405. #else
  3406. i = make_new_char_sql(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35));
  3407. #endif
  3408. //'Charname already exists' (-1), 'Char creation denied' (-2) and 'You are underaged' (-3)
  3409. if (i < 0)
  3410. {
  3411. WFIFOHEAD(fd,3);
  3412. WFIFOW(fd,0) = 0x6e;
  3413. switch (i) {
  3414. case -1: WFIFOB(fd,2) = 0x00; break;
  3415. case -2: WFIFOB(fd,2) = 0xFF; break;
  3416. case -3: WFIFOB(fd,2) = 0x01; break;
  3417. }
  3418. WFIFOSET(fd,3);
  3419. }
  3420. else
  3421. {
  3422. int len;
  3423. // retrieve data
  3424. struct mmo_charstatus char_dat;
  3425. mmo_char_fromsql(i, &char_dat, false); //Only the short data is needed.
  3426. // send to player
  3427. WFIFOHEAD(fd,2+MAX_CHAR_BUF);
  3428. WFIFOW(fd,0) = 0x6d;
  3429. len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat);
  3430. WFIFOSET(fd,len);
  3431. // add new entry to the chars list
  3432. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] == -1 );
  3433. if( ch < MAX_CHARS )
  3434. sd->found_char[ch] = i; // the char_id of the new char
  3435. }
  3436. #if PACKETVER >= 20120307
  3437. RFIFOSKIP(fd,31);
  3438. #else
  3439. RFIFOSKIP(fd,37);
  3440. #endif
  3441. break;
  3442. // delete char
  3443. case 0x68:
  3444. // 2004-04-19aSakexe+ langtype 12 char deletion packet
  3445. case 0x1fb:
  3446. if (cmd == 0x68) FIFOSD_CHECK(46);
  3447. if (cmd == 0x1fb) FIFOSD_CHECK(56);
  3448. {
  3449. int cid = RFIFOL(fd,2);
  3450. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  3451. memcpy(email, RFIFOP(fd,6), 40);
  3452. RFIFOSKIP(fd,( cmd == 0x68) ? 46 : 56);
  3453. // Check if e-mail is correct
  3454. if(strcmpi(email, sd->email) && //email does not matches and
  3455. (
  3456. strcmp("a@a.com", sd->email) || //it is not default email, or
  3457. (strcmp("a@a.com", email) && strcmp("", email)) //email sent does not matches default
  3458. )) { //Fail
  3459. WFIFOHEAD(fd,3);
  3460. WFIFOW(fd,0) = 0x70;
  3461. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3462. WFIFOSET(fd,3);
  3463. break;
  3464. }
  3465. // check if this char exists
  3466. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  3467. if( i == MAX_CHARS )
  3468. { // Such a character does not exist in the account
  3469. WFIFOHEAD(fd,3);
  3470. WFIFOW(fd,0) = 0x70;
  3471. WFIFOB(fd,2) = 0;
  3472. WFIFOSET(fd,3);
  3473. break;
  3474. }
  3475. // remove char from list and compact it
  3476. for(ch = i; ch < MAX_CHARS-1; ch++)
  3477. sd->found_char[ch] = sd->found_char[ch+1];
  3478. sd->found_char[MAX_CHARS-1] = -1;
  3479. /* Delete character */
  3480. if(delete_char_sql(cid)<0){
  3481. //can't delete the char
  3482. //either SQL error or can't delete by some CONFIG conditions
  3483. //del fail
  3484. WFIFOHEAD(fd,3);
  3485. WFIFOW(fd, 0) = 0x70;
  3486. WFIFOB(fd, 2) = 0;
  3487. WFIFOSET(fd, 3);
  3488. break;
  3489. }
  3490. /* Char successfully deleted.*/
  3491. WFIFOHEAD(fd,2);
  3492. WFIFOW(fd,0) = 0x6f;
  3493. WFIFOSET(fd,2);
  3494. }
  3495. break;
  3496. // client keep-alive packet (every 12 seconds)
  3497. // R 0187 <account ID>.l
  3498. case 0x187:
  3499. if (RFIFOREST(fd) < 6)
  3500. return 0;
  3501. RFIFOSKIP(fd,6);
  3502. break;
  3503. // char rename request
  3504. // R 028d <account ID>.l <char ID>.l <new name>.24B
  3505. case 0x28d:
  3506. FIFOSD_CHECK(34);
  3507. {
  3508. int i, aid = RFIFOL(fd,2), cid =RFIFOL(fd,6);
  3509. char name[NAME_LENGTH];
  3510. char esc_name[NAME_LENGTH*2+1];
  3511. safestrncpy(name, (char *)RFIFOP(fd,10), NAME_LENGTH);
  3512. RFIFOSKIP(fd,34);
  3513. if( aid != sd->account_id )
  3514. break;
  3515. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  3516. if( i == MAX_CHARS )
  3517. break;
  3518. normalize_name(name,TRIM_CHARS);
  3519. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  3520. if( !check_char_name(name,esc_name) )
  3521. {
  3522. i = 1;
  3523. safestrncpy(sd->new_name, name, NAME_LENGTH);
  3524. }
  3525. else
  3526. i = 0;
  3527. WFIFOHEAD(fd, 4);
  3528. WFIFOW(fd,0) = 0x28e;
  3529. WFIFOW(fd,2) = i;
  3530. WFIFOSET(fd,4);
  3531. }
  3532. break;
  3533. //Confirm change name.
  3534. // 0x28f <char_id>.L
  3535. case 0x28f:
  3536. // 0: Sucessfull
  3537. // 1: This character's name has already been changed. You cannot change a character's name more than once.
  3538. // 2: User information is not correct.
  3539. // 3: You have failed to change this character's name.
  3540. // 4: Another user is using this character name, so please select another one.
  3541. FIFOSD_CHECK(6);
  3542. {
  3543. int i;
  3544. int cid = RFIFOL(fd,2);
  3545. RFIFOSKIP(fd,6);
  3546. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  3547. if( i == MAX_CHARS )
  3548. break;
  3549. i = rename_char_sql(sd, cid);
  3550. WFIFOHEAD(fd, 4);
  3551. WFIFOW(fd,0) = 0x290;
  3552. WFIFOW(fd,2) = i;
  3553. WFIFOSET(fd,4);
  3554. }
  3555. break;
  3556. // captcha code request (not implemented)
  3557. // R 07e5 <?>.w <aid>.l
  3558. case 0x7e5:
  3559. WFIFOHEAD(fd,5);
  3560. WFIFOW(fd,0) = 0x7e9;
  3561. WFIFOW(fd,2) = 5;
  3562. WFIFOB(fd,4) = 1;
  3563. WFIFOSET(fd,5);
  3564. RFIFOSKIP(fd,8);
  3565. break;
  3566. // captcha code check (not implemented)
  3567. // R 07e7 <len>.w <aid>.l <code>.b10 <?>.b14
  3568. case 0x7e7:
  3569. WFIFOHEAD(fd,5);
  3570. WFIFOW(fd,0) = 0x7e9;
  3571. WFIFOW(fd,2) = 5;
  3572. WFIFOB(fd,4) = 1;
  3573. WFIFOSET(fd,5);
  3574. RFIFOSKIP(fd,32);
  3575. break;
  3576. // deletion timer request
  3577. case 0x827:
  3578. FIFOSD_CHECK(6);
  3579. char_delete2_req(fd, sd);
  3580. RFIFOSKIP(fd,6);
  3581. break;
  3582. // deletion accept request
  3583. case 0x829:
  3584. FIFOSD_CHECK(12);
  3585. char_delete2_accept(fd, sd);
  3586. RFIFOSKIP(fd,12);
  3587. break;
  3588. // deletion cancel request
  3589. case 0x82b:
  3590. FIFOSD_CHECK(6);
  3591. char_delete2_cancel(fd, sd);
  3592. RFIFOSKIP(fd,6);
  3593. break;
  3594. // login as map-server
  3595. case 0x2af8:
  3596. if (RFIFOREST(fd) < 60)
  3597. return 0;
  3598. {
  3599. char* l_user = (char*)RFIFOP(fd,2);
  3600. char* l_pass = (char*)RFIFOP(fd,26);
  3601. l_user[23] = '\0';
  3602. l_pass[23] = '\0';
  3603. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd <= 0 );
  3604. if( runflag != CHARSERVER_ST_RUNNING ||
  3605. i == ARRAYLENGTH(server) ||
  3606. strcmp(l_user, userid) != 0 ||
  3607. strcmp(l_pass, passwd) != 0 )
  3608. {
  3609. WFIFOHEAD(fd,3);
  3610. WFIFOW(fd,0) = 0x2af9;
  3611. WFIFOB(fd,2) = 3;
  3612. WFIFOSET(fd,3);
  3613. } else {
  3614. WFIFOHEAD(fd,3);
  3615. WFIFOW(fd,0) = 0x2af9;
  3616. WFIFOB(fd,2) = 0;
  3617. WFIFOSET(fd,3);
  3618. server[i].fd = fd;
  3619. server[i].ip = ntohl(RFIFOL(fd,54));
  3620. server[i].port = ntohs(RFIFOW(fd,58));
  3621. server[i].users = 0;
  3622. memset(server[i].map, 0, sizeof(server[i].map));
  3623. session[fd]->func_parse = parse_frommap;
  3624. session[fd]->flag.server = 1;
  3625. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3626. char_mapif_init(fd);
  3627. }
  3628. RFIFOSKIP(fd,60);
  3629. }
  3630. return 0; // avoid processing of followup packets here
  3631. // unknown packet received
  3632. default:
  3633. ShowError("parse_char: Received unknown packet "CL_WHITE"0x%x"CL_RESET" from ip '"CL_WHITE"%s"CL_RESET"'! Disconnecting!\n", RFIFOW(fd,0), ip2str(ipl, NULL));
  3634. set_eof(fd);
  3635. return 0;
  3636. }
  3637. }
  3638. RFIFOFLUSH(fd);
  3639. return 0;
  3640. }
  3641. // Console Command Parser [Wizputer]
  3642. int parse_console(const char* command)
  3643. {
  3644. ShowNotice("Console command: %s\n", command);
  3645. if( strcmpi("shutdown", command) == 0 || strcmpi("exit", command) == 0 || strcmpi("quit", command) == 0 || strcmpi("end", command) == 0 )
  3646. runflag = 0;
  3647. else if( strcmpi("alive", command) == 0 || strcmpi("status", command) == 0 )
  3648. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  3649. else if( strcmpi("help", command) == 0 )
  3650. {
  3651. ShowInfo("To shutdown the server:\n");
  3652. ShowInfo(" 'shutdown|exit|quit|end'\n");
  3653. ShowInfo("To know if server is alive:\n");
  3654. ShowInfo(" 'alive|status'\n");
  3655. }
  3656. return 0;
  3657. }
  3658. int mapif_sendall(unsigned char *buf, unsigned int len)
  3659. {
  3660. int i, c;
  3661. c = 0;
  3662. for(i = 0; i < ARRAYLENGTH(server); i++) {
  3663. int fd;
  3664. if ((fd = server[i].fd) > 0) {
  3665. WFIFOHEAD(fd,len);
  3666. memcpy(WFIFOP(fd,0), buf, len);
  3667. WFIFOSET(fd,len);
  3668. c++;
  3669. }
  3670. }
  3671. return c;
  3672. }
  3673. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len)
  3674. {
  3675. int i, c;
  3676. c = 0;
  3677. for(i = 0; i < ARRAYLENGTH(server); i++) {
  3678. int fd;
  3679. if ((fd = server[i].fd) > 0 && fd != sfd) {
  3680. WFIFOHEAD(fd,len);
  3681. memcpy(WFIFOP(fd,0), buf, len);
  3682. WFIFOSET(fd,len);
  3683. c++;
  3684. }
  3685. }
  3686. return c;
  3687. }
  3688. int mapif_send(int fd, unsigned char *buf, unsigned int len)
  3689. {
  3690. if (fd >= 0) {
  3691. int i;
  3692. ARR_FIND( 0, ARRAYLENGTH(server), i, fd == server[i].fd );
  3693. if( i < ARRAYLENGTH(server) )
  3694. {
  3695. WFIFOHEAD(fd,len);
  3696. memcpy(WFIFOP(fd,0), buf, len);
  3697. WFIFOSET(fd,len);
  3698. return 1;
  3699. }
  3700. }
  3701. return 0;
  3702. }
  3703. int broadcast_user_count(int tid, unsigned int tick, int id, intptr_t data)
  3704. {
  3705. uint8 buf[6];
  3706. int users = count_users();
  3707. // only send an update when needed
  3708. static int prev_users = 0;
  3709. if( prev_users == users )
  3710. return 0;
  3711. prev_users = users;
  3712. if( login_fd > 0 && session[login_fd] )
  3713. {
  3714. // send number of user to login server
  3715. WFIFOHEAD(login_fd,6);
  3716. WFIFOW(login_fd,0) = 0x2714;
  3717. WFIFOL(login_fd,2) = users;
  3718. WFIFOSET(login_fd,6);
  3719. }
  3720. // send number of players to all map-servers
  3721. WBUFW(buf,0) = 0x2b00;
  3722. WBUFL(buf,2) = users;
  3723. mapif_sendall(buf,6);
  3724. return 0;
  3725. }
  3726. /**
  3727. * Load this character's account id into the 'online accounts' packet
  3728. * @see DBApply
  3729. */
  3730. static int send_accounts_tologin_sub(DBKey key, DBData *data, va_list ap)
  3731. {
  3732. struct online_char_data* character = db_data2ptr(data);
  3733. int* i = va_arg(ap, int*);
  3734. if(character->server > -1)
  3735. {
  3736. WFIFOL(login_fd,8+(*i)*4) = character->account_id;
  3737. (*i)++;
  3738. return 1;
  3739. }
  3740. return 0;
  3741. }
  3742. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data)
  3743. {
  3744. if (login_fd > 0 && session[login_fd])
  3745. {
  3746. // send account list to login server
  3747. int users = online_char_db->size(online_char_db);
  3748. int i = 0;
  3749. WFIFOHEAD(login_fd,8+users*4);
  3750. WFIFOW(login_fd,0) = 0x272d;
  3751. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  3752. WFIFOW(login_fd,2) = 8+ i*4;
  3753. WFIFOL(login_fd,4) = i;
  3754. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  3755. }
  3756. return 0;
  3757. }
  3758. int check_connect_login_server(int tid, unsigned int tick, int id, intptr_t data)
  3759. {
  3760. if (login_fd > 0 && session[login_fd] != NULL)
  3761. return 0;
  3762. ShowInfo("Attempt to connect to login-server...\n");
  3763. login_fd = make_connection(login_ip, login_port, false);
  3764. if (login_fd == -1)
  3765. { //Try again later. [Skotlex]
  3766. login_fd = 0;
  3767. return 0;
  3768. }
  3769. session[login_fd]->func_parse = parse_fromlogin;
  3770. session[login_fd]->flag.server = 1;
  3771. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3772. WFIFOHEAD(login_fd,86);
  3773. WFIFOW(login_fd,0) = 0x2710;
  3774. memcpy(WFIFOP(login_fd,2), userid, 24);
  3775. memcpy(WFIFOP(login_fd,26), passwd, 24);
  3776. WFIFOL(login_fd,50) = 0;
  3777. WFIFOL(login_fd,54) = htonl(char_ip);
  3778. WFIFOW(login_fd,58) = htons(char_port);
  3779. memcpy(WFIFOP(login_fd,60), server_name, 20);
  3780. WFIFOW(login_fd,80) = 0;
  3781. WFIFOW(login_fd,82) = char_maintenance;
  3782. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  3783. WFIFOSET(login_fd,86);
  3784. return 1;
  3785. }
  3786. //------------------------------------------------
  3787. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  3788. //replies/disconnect the player we tried to kick. [Skotlex]
  3789. //------------------------------------------------
  3790. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data)
  3791. {
  3792. struct online_char_data* character;
  3793. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  3794. { //Mark it offline due to timeout.
  3795. character->waiting_disconnect = INVALID_TIMER;
  3796. set_char_offline(character->char_id, character->account_id);
  3797. }
  3798. return 0;
  3799. }
  3800. /**
  3801. * @see DBApply
  3802. */
  3803. static int online_data_cleanup_sub(DBKey key, DBData *data, va_list ap)
  3804. {
  3805. struct online_char_data *character= db_data2ptr(data);
  3806. if (character->fd != -1)
  3807. return 0; //Character still connected
  3808. if (character->server == -2) //Unknown server.. set them offline
  3809. set_char_offline(character->char_id, character->account_id);
  3810. if (character->server < 0)
  3811. //Free data from players that have not been online for a while.
  3812. db_remove(online_char_db, key);
  3813. return 0;
  3814. }
  3815. static int online_data_cleanup(int tid, unsigned int tick, int id, intptr_t data)
  3816. {
  3817. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  3818. return 0;
  3819. }
  3820. //----------------------------------
  3821. // Reading Lan Support configuration
  3822. // Rewrote: Anvanced subnet check [LuzZza]
  3823. //----------------------------------
  3824. int char_lan_config_read(const char *lancfgName)
  3825. {
  3826. FILE *fp;
  3827. int line_num = 0;
  3828. char line[1024], w1[64], w2[64], w3[64], w4[64];
  3829. if((fp = fopen(lancfgName, "r")) == NULL) {
  3830. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  3831. return 1;
  3832. }
  3833. while(fgets(line, sizeof(line), fp)) {
  3834. line_num++;
  3835. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  3836. continue;
  3837. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4) {
  3838. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  3839. continue;
  3840. }
  3841. remove_control_chars(w1);
  3842. remove_control_chars(w2);
  3843. remove_control_chars(w3);
  3844. remove_control_chars(w4);
  3845. if( strcmpi(w1, "subnet") == 0 )
  3846. {
  3847. subnet[subnet_count].mask = str2ip(w2);
  3848. subnet[subnet_count].char_ip = str2ip(w3);
  3849. subnet[subnet_count].map_ip = str2ip(w4);
  3850. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  3851. {
  3852. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  3853. continue;
  3854. }
  3855. subnet_count++;
  3856. }
  3857. }
  3858. if( subnet_count > 1 ) /* only useful if there is more than 1 */
  3859. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  3860. fclose(fp);
  3861. return 0;
  3862. }
  3863. void sql_config_read(const char* cfgName)
  3864. {
  3865. char line[1024], w1[1024], w2[1024];
  3866. FILE* fp;
  3867. if ((fp = fopen(cfgName, "r")) == NULL) {
  3868. ShowError("File not found: %s\n", cfgName);
  3869. return;
  3870. }
  3871. while(fgets(line, sizeof(line), fp))
  3872. {
  3873. if(line[0] == '/' && line[1] == '/')
  3874. continue;
  3875. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3876. continue;
  3877. if(!strcmpi(w1,"char_db"))
  3878. safestrncpy(char_db, w2, sizeof(char_db));
  3879. else if(!strcmpi(w1,"scdata_db"))
  3880. safestrncpy(scdata_db, w2, sizeof(scdata_db));
  3881. else if(!strcmpi(w1,"cart_db"))
  3882. safestrncpy(cart_db, w2, sizeof(cart_db));
  3883. else if(!strcmpi(w1,"inventory_db"))
  3884. safestrncpy(inventory_db, w2, sizeof(inventory_db));
  3885. else if(!strcmpi(w1,"charlog_db"))
  3886. safestrncpy(charlog_db, w2, sizeof(charlog_db));
  3887. else if(!strcmpi(w1,"storage_db"))
  3888. safestrncpy(storage_db, w2, sizeof(storage_db));
  3889. else if(!strcmpi(w1,"reg_db"))
  3890. safestrncpy(reg_db, w2, sizeof(reg_db));
  3891. else if(!strcmpi(w1,"skill_db"))
  3892. safestrncpy(skill_db, w2, sizeof(skill_db));
  3893. else if(!strcmpi(w1,"interlog_db"))
  3894. safestrncpy(interlog_db, w2, sizeof(interlog_db));
  3895. else if(!strcmpi(w1,"memo_db"))
  3896. safestrncpy(memo_db, w2, sizeof(memo_db));
  3897. else if(!strcmpi(w1,"guild_db"))
  3898. safestrncpy(guild_db, w2, sizeof(guild_db));
  3899. else if(!strcmpi(w1,"guild_alliance_db"))
  3900. safestrncpy(guild_alliance_db, w2, sizeof(guild_alliance_db));
  3901. else if(!strcmpi(w1,"guild_castle_db"))
  3902. safestrncpy(guild_castle_db, w2, sizeof(guild_castle_db));
  3903. else if(!strcmpi(w1,"guild_expulsion_db"))
  3904. safestrncpy(guild_expulsion_db, w2, sizeof(guild_expulsion_db));
  3905. else if(!strcmpi(w1,"guild_member_db"))
  3906. safestrncpy(guild_member_db, w2, sizeof(guild_member_db));
  3907. else if(!strcmpi(w1,"guild_skill_db"))
  3908. safestrncpy(guild_skill_db, w2, sizeof(guild_skill_db));
  3909. else if(!strcmpi(w1,"guild_position_db"))
  3910. safestrncpy(guild_position_db, w2, sizeof(guild_position_db));
  3911. else if(!strcmpi(w1,"guild_storage_db"))
  3912. safestrncpy(guild_storage_db, w2, sizeof(guild_storage_db));
  3913. else if(!strcmpi(w1,"party_db"))
  3914. safestrncpy(party_db, w2, sizeof(party_db));
  3915. else if(!strcmpi(w1,"pet_db"))
  3916. safestrncpy(pet_db, w2, sizeof(pet_db));
  3917. else if(!strcmpi(w1,"mail_db"))
  3918. safestrncpy(mail_db, w2, sizeof(mail_db));
  3919. else if(!strcmpi(w1,"auction_db"))
  3920. safestrncpy(auction_db, w2, sizeof(auction_db));
  3921. else if(!strcmpi(w1,"friend_db"))
  3922. safestrncpy(friend_db, w2, sizeof(friend_db));
  3923. else if(!strcmpi(w1,"hotkey_db"))
  3924. safestrncpy(hotkey_db, w2, sizeof(hotkey_db));
  3925. else if(!strcmpi(w1,"quest_db"))
  3926. safestrncpy(quest_db,w2,sizeof(quest_db));
  3927. else if(!strcmpi(w1,"homunculus_db"))
  3928. safestrncpy(homunculus_db,w2,sizeof(homunculus_db));
  3929. else if(!strcmpi(w1,"skill_homunculus_db"))
  3930. safestrncpy(skill_homunculus_db,w2,sizeof(skill_homunculus_db));
  3931. else if(!strcmpi(w1,"mercenary_db"))
  3932. safestrncpy(mercenary_db,w2,sizeof(mercenary_db));
  3933. else if(!strcmpi(w1,"mercenary_owner_db"))
  3934. safestrncpy(mercenary_owner_db,w2,sizeof(mercenary_owner_db));
  3935. //support the import command, just like any other config
  3936. else if(!strcmpi(w1,"import"))
  3937. sql_config_read(w2);
  3938. }
  3939. fclose(fp);
  3940. ShowInfo("Done reading %s.\n", cfgName);
  3941. }
  3942. int char_config_read(const char* cfgName)
  3943. {
  3944. char line[1024], w1[1024], w2[1024];
  3945. FILE* fp = fopen(cfgName, "r");
  3946. if (fp == NULL) {
  3947. ShowError("Configuration file not found: %s.\n", cfgName);
  3948. return 1;
  3949. }
  3950. while(fgets(line, sizeof(line), fp)) {
  3951. if (line[0] == '/' && line[1] == '/')
  3952. continue;
  3953. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3954. continue;
  3955. remove_control_chars(w1);
  3956. remove_control_chars(w2);
  3957. if(strcmpi(w1,"timestamp_format") == 0) {
  3958. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  3959. } else if(strcmpi(w1,"console_silent")==0){
  3960. msg_silent = atoi(w2);
  3961. if( msg_silent ) /* only bother if its actually enabled */
  3962. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  3963. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  3964. stdout_with_ansisequence = config_switch(w2);
  3965. } else if (strcmpi(w1, "userid") == 0) {
  3966. safestrncpy(userid, w2, sizeof(userid));
  3967. } else if (strcmpi(w1, "passwd") == 0) {
  3968. safestrncpy(passwd, w2, sizeof(passwd));
  3969. } else if (strcmpi(w1, "server_name") == 0) {
  3970. safestrncpy(server_name, w2, sizeof(server_name));
  3971. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  3972. if (strlen(w2) >= 4) {
  3973. safestrncpy(wisp_server_name, w2, sizeof(wisp_server_name));
  3974. }
  3975. } else if (strcmpi(w1, "login_ip") == 0) {
  3976. char ip_str[16];
  3977. login_ip = host2ip(w2);
  3978. if (login_ip) {
  3979. safestrncpy(login_ip_str, w2, sizeof(login_ip_str));
  3980. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(login_ip, ip_str));
  3981. }
  3982. } else if (strcmpi(w1, "login_port") == 0) {
  3983. login_port = atoi(w2);
  3984. } else if (strcmpi(w1, "char_ip") == 0) {
  3985. char ip_str[16];
  3986. char_ip = host2ip(w2);
  3987. if (char_ip){
  3988. safestrncpy(char_ip_str, w2, sizeof(char_ip_str));
  3989. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(char_ip, ip_str));
  3990. }
  3991. } else if (strcmpi(w1, "bind_ip") == 0) {
  3992. char ip_str[16];
  3993. bind_ip = host2ip(w2);
  3994. if (bind_ip) {
  3995. safestrncpy(bind_ip_str, w2, sizeof(bind_ip_str));
  3996. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(bind_ip, ip_str));
  3997. }
  3998. } else if (strcmpi(w1, "char_port") == 0) {
  3999. char_port = atoi(w2);
  4000. } else if (strcmpi(w1, "char_maintenance") == 0) {
  4001. char_maintenance = atoi(w2);
  4002. } else if (strcmpi(w1, "char_new") == 0) {
  4003. char_new = (bool)atoi(w2);
  4004. } else if (strcmpi(w1, "char_new_display") == 0) {
  4005. char_new_display = atoi(w2);
  4006. } else if (strcmpi(w1, "max_connect_user") == 0) {
  4007. max_connect_user = atoi(w2);
  4008. if (max_connect_user < 0)
  4009. max_connect_user = 0; // unlimited online players
  4010. } else if(strcmpi(w1, "gm_allow_group") == 0) {
  4011. gm_allow_group = atoi(w2);
  4012. } else if (strcmpi(w1, "autosave_time") == 0) {
  4013. autosave_interval = atoi(w2)*1000;
  4014. if (autosave_interval <= 0)
  4015. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  4016. } else if (strcmpi(w1, "save_log") == 0) {
  4017. save_log = config_switch(w2);
  4018. } else if (strcmpi(w1, "start_point") == 0) {
  4019. char map[MAP_NAME_LENGTH_EXT];
  4020. int x, y;
  4021. if (sscanf(w2, "%15[^,],%d,%d", map, &x, &y) < 3)
  4022. continue;
  4023. start_point.map = mapindex_name2id(map);
  4024. if (!start_point.map)
  4025. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  4026. start_point.x = x;
  4027. start_point.y = y;
  4028. } else if (strcmpi(w1, "start_zeny") == 0) {
  4029. start_zeny = atoi(w2);
  4030. if (start_zeny < 0)
  4031. start_zeny = 0;
  4032. } else if (strcmpi(w1, "start_weapon") == 0) {
  4033. start_weapon = atoi(w2);
  4034. if (start_weapon < 0)
  4035. start_weapon = 0;
  4036. } else if (strcmpi(w1, "start_armor") == 0) {
  4037. start_armor = atoi(w2);
  4038. if (start_armor < 0)
  4039. start_armor = 0;
  4040. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  4041. log_char = atoi(w2);
  4042. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  4043. safestrncpy(unknown_char_name, w2, sizeof(unknown_char_name));
  4044. unknown_char_name[NAME_LENGTH-1] = '\0';
  4045. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  4046. name_ignoring_case = (bool)config_switch(w2);
  4047. } else if (strcmpi(w1, "char_name_option") == 0) {
  4048. char_name_option = atoi(w2);
  4049. } else if (strcmpi(w1, "char_name_letters") == 0) {
  4050. safestrncpy(char_name_letters, w2, sizeof(char_name_letters));
  4051. } else if (strcmpi(w1, "chars_per_account") == 0) { //maxchars per account [Sirius]
  4052. char_per_account = atoi(w2);
  4053. if( char_per_account == 0 || char_per_account > MAX_CHARS ) {
  4054. if( char_per_account > MAX_CHARS )
  4055. ShowWarning("Max chars per account '%d' exceeded limit. Defaulting to '%d'.\n", char_per_account, MAX_CHARS);
  4056. char_per_account = MAX_CHARS;
  4057. }
  4058. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  4059. char_del_level = atoi(w2);
  4060. } else if (strcmpi(w1, "char_del_delay") == 0) {
  4061. char_del_delay = atoi(w2);
  4062. } else if(strcmpi(w1,"db_path")==0) {
  4063. safestrncpy(db_path, w2, sizeof(db_path));
  4064. } else if (strcmpi(w1, "console") == 0) {
  4065. console = config_switch(w2);
  4066. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  4067. fame_list_size_chemist = atoi(w2);
  4068. if (fame_list_size_chemist > MAX_FAME_LIST) {
  4069. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  4070. fame_list_size_chemist = MAX_FAME_LIST;
  4071. }
  4072. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  4073. fame_list_size_smith = atoi(w2);
  4074. if (fame_list_size_smith > MAX_FAME_LIST) {
  4075. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  4076. fame_list_size_smith = MAX_FAME_LIST;
  4077. }
  4078. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  4079. fame_list_size_taekwon = atoi(w2);
  4080. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  4081. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  4082. fame_list_size_taekwon = MAX_FAME_LIST;
  4083. }
  4084. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  4085. guild_exp_rate = atoi(w2);
  4086. } else if (strcmpi(w1, "import") == 0) {
  4087. char_config_read(w2);
  4088. }
  4089. }
  4090. fclose(fp);
  4091. ShowInfo("Done reading %s.\n", cfgName);
  4092. return 0;
  4093. }
  4094. void do_final(void)
  4095. {
  4096. ShowStatus("Terminating...\n");
  4097. set_all_offline(-1);
  4098. set_all_offline_sql();
  4099. inter_final();
  4100. flush_fifos();
  4101. do_final_mapif();
  4102. do_final_loginif();
  4103. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s`", ragsrvinfo_db) )
  4104. Sql_ShowDebug(sql_handle);
  4105. char_db_->destroy(char_db_, NULL);
  4106. online_char_db->destroy(online_char_db, NULL);
  4107. auth_db->destroy(auth_db, NULL);
  4108. if( char_fd != -1 )
  4109. {
  4110. do_close(char_fd);
  4111. char_fd = -1;
  4112. }
  4113. Sql_Free(sql_handle);
  4114. mapindex_final();
  4115. ShowStatus("Finished.\n");
  4116. }
  4117. //------------------------------
  4118. // Function called when the server
  4119. // has received a crash signal.
  4120. //------------------------------
  4121. void do_abort(void)
  4122. {
  4123. }
  4124. void set_server_type(void)
  4125. {
  4126. SERVER_TYPE = ATHENA_SERVER_CHAR;
  4127. }
  4128. /// Called when a terminate signal is received.
  4129. void do_shutdown(void)
  4130. {
  4131. if( runflag != CHARSERVER_ST_SHUTDOWN )
  4132. {
  4133. int id;
  4134. runflag = CHARSERVER_ST_SHUTDOWN;
  4135. ShowStatus("Shutting down...\n");
  4136. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  4137. for( id = 0; id < ARRAYLENGTH(server); ++id )
  4138. mapif_server_reset(id);
  4139. loginif_check_shutdown();
  4140. flush_fifos();
  4141. runflag = CORE_ST_STOP;
  4142. }
  4143. }
  4144. int do_init(int argc, char **argv)
  4145. {
  4146. //Read map indexes
  4147. mapindex_init();
  4148. start_point.map = mapindex_name2id("new_zone01");
  4149. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  4150. char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME);
  4151. sql_config_read(SQL_CONF_NAME);
  4152. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  4153. ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  4154. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  4155. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  4156. }
  4157. inter_init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server configuration
  4158. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  4159. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  4160. mmo_char_sql_init();
  4161. char_read_fame_list(); //Read fame lists.
  4162. if ((naddr_ != 0) && (!login_ip || !char_ip))
  4163. {
  4164. char ip_str[16];
  4165. ip2str(addr_[0], ip_str);
  4166. if (naddr_ > 1)
  4167. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  4168. else
  4169. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  4170. if (!login_ip) {
  4171. safestrncpy(login_ip_str, ip_str, sizeof(login_ip_str));
  4172. login_ip = str2ip(login_ip_str);
  4173. }
  4174. if (!char_ip) {
  4175. safestrncpy(char_ip_str, ip_str, sizeof(char_ip_str));
  4176. char_ip = str2ip(char_ip_str);
  4177. }
  4178. }
  4179. do_init_loginif();
  4180. do_init_mapif();
  4181. // periodically update the overall user count on all mapservers + login server
  4182. add_timer_func_list(broadcast_user_count, "broadcast_user_count");
  4183. add_timer_interval(gettick() + 1000, broadcast_user_count, 0, 0, 5 * 1000);
  4184. // Timer to clear (online_char_db)
  4185. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  4186. // Online Data timers (checking if char still connected)
  4187. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  4188. add_timer_interval(gettick() + 1000, online_data_cleanup, 0, 0, 600 * 1000);
  4189. if( console )
  4190. {
  4191. //##TODO invoke a CONSOLE_START plugin event
  4192. }
  4193. //Cleaning the tables for NULL entrys @ startup [Sirius]
  4194. //Chardb clean
  4195. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", char_db) )
  4196. Sql_ShowDebug(sql_handle);
  4197. //guilddb clean
  4198. 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'", guild_db) )
  4199. Sql_ShowDebug(sql_handle);
  4200. //guildmemberdb clean
  4201. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", guild_member_db) )
  4202. Sql_ShowDebug(sql_handle);
  4203. set_defaultparse(parse_char);
  4204. char_fd = make_listen_bind(bind_ip, char_port);
  4205. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  4206. if( runflag != CORE_ST_STOP )
  4207. {
  4208. shutdown_callback = do_shutdown;
  4209. runflag = CHARSERVER_ST_RUNNING;
  4210. }
  4211. return 0;
  4212. }