char.c 135 KB

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