char.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051
  1. // $Id: char.c,v 1.16 2004/09/23 18:31:16 MouseJstr Exp $
  2. // original : char2.c 2003/03/14 11:58:35 Rev.1.5
  3. //
  4. // original code from athena
  5. // SQL conversion by Jioh L. Jung
  6. // TXT 1.105
  7. #include <sys/types.h>
  8. #ifdef LCCWIN32
  9. #include <winsock.h>
  10. #pragma lib <libmysql.lib>
  11. #else
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <sys/time.h>
  15. #include <sys/ioctl.h>
  16. #include <arpa/inet.h>
  17. #include <netdb.h>
  18. #endif
  19. #include "../common/utils.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <netinet/in.h>
  23. #include <sys/time.h>
  24. #include <time.h>
  25. #include <sys/ioctl.h>
  26. #include <unistd.h>
  27. #include <signal.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. #include <arpa/inet.h>
  31. #include <netdb.h>
  32. #include <stdarg.h>
  33. #include "char.h"
  34. #include "strlib.h"
  35. #include "itemdb.h"
  36. #include "inter.h"
  37. #ifdef MEMWATCH
  38. #include "memwatch.h"
  39. #endif
  40. char char_db[256] = "char";
  41. char cart_db[256] = "cart_inventory";
  42. char inventory_db[256] = "inventory";
  43. char charlog_db[256] = "charlog";
  44. char storage_db[256] = "storage";
  45. char interlog_db[256] = "interlog";
  46. char reg_db[256] = "global_reg_value";
  47. char skill_db[256] = "skill";
  48. char memo_db[256] = "memo";
  49. char guild_db[256] = "guild";
  50. char guild_alliance_db[256] = "guild_alliance";
  51. char guild_castle_db[256] = "guild_castle";
  52. char guild_expulsion_db[256] = "guild_expulsion";
  53. char guild_member_db[256] = "guild_member";
  54. char guild_position_db[256] = "guild_position";
  55. char guild_skill_db[256] = "guild_skill";
  56. char guild_storage_db[256] = "guild_storage";
  57. char party_db[256] = "party";
  58. char pet_db[256] = "pet";
  59. char login_db[256] = "login";
  60. char login_db_account_id[32] = "account_id";
  61. char login_db_level[32] = "level";
  62. int lowest_gm_level = 1;
  63. int user_count_timer;
  64. unsigned char *SQL_CONF_NAME = "conf/inter_athena.conf";
  65. struct mmo_map_server server[MAX_MAP_SERVERS];
  66. int server_fd[MAX_MAP_SERVERS];
  67. int server_freezeflag[MAX_MAP_SERVERS]; // Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed
  68. int anti_freeze_enable = 0;
  69. int ANTI_FREEZE_INTERVAL = 6;
  70. int login_fd, char_fd;
  71. char userid[24];
  72. char passwd[24];
  73. char server_name[20];
  74. char wisp_server_name[24] = "Server";
  75. int login_ip_set_ = 0;
  76. char login_ip_str[128];
  77. int login_ip;
  78. int login_port = 6900;
  79. int char_ip_set_ = 0;
  80. char char_ip_str[128];
  81. int char_ip;
  82. int char_port = 6121;
  83. int char_maintenance;
  84. int char_new;
  85. int name_ignoring_case = 0; // Allow or not identical name for characters but with a different case by [Yor]
  86. 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]
  87. char char_name_letters[1024] = ""; // list of letters/symbols used to authorise or not a name of a character. by [Yor]
  88. int log_char = 1; // loggin char or not [devil]
  89. int log_inter = 1; // loggin inter or not [devil]
  90. char lan_map_ip[128]; // Lan map ip added by kashy
  91. int subnetmaski[4]; // Subnetmask added by kashy
  92. char unknown_char_name[1024] = "Unknown";
  93. char db_path[1024]="db";
  94. //Added for Mugendai's I'm Alive mod
  95. int imalive_on=0;
  96. int imalive_time=60;
  97. //Added by Mugendai for GUI
  98. int flush_on=1;
  99. int flush_time=100;
  100. struct char_session_data{
  101. int account_id,login_id1,login_id2,sex;
  102. int found_char[9];
  103. char email[40]; // e-mail (default: a@a.com) by [Yor]
  104. time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  105. };
  106. #define AUTH_FIFO_SIZE 256
  107. struct {
  108. int account_id,char_id,login_id1,login_id2,ip,char_pos,delflag,sex;
  109. time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  110. } auth_fifo[AUTH_FIFO_SIZE];
  111. int auth_fifo_pos = 0;
  112. int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system)
  113. int char_id_count = 150000;
  114. struct mmo_charstatus *char_dat;
  115. int char_num,char_max;
  116. int max_connect_user = 0;
  117. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  118. int start_zeny = 500;
  119. int start_weapon = 1201;
  120. int start_armor = 2301;
  121. // check for exit signal
  122. // 0 is saving complete
  123. // other is char_id
  124. unsigned int save_flag = 0;
  125. // start point (you can reset point on conf file)
  126. struct point start_point = {"new_1-1.gat", 53, 111};
  127. struct gm_account *gm_account = NULL;
  128. int GM_num = 0;
  129. int console = 0;
  130. //-----------------------------------------------------
  131. // Function to suppress control characters in a string.
  132. //-----------------------------------------------------
  133. int remove_control_chars(unsigned char *str) {
  134. int i;
  135. int change = 0;
  136. for(i = 0; i < strlen(str); i++) {
  137. if (str[i] < 32) {
  138. str[i] = '_';
  139. change = 1;
  140. }
  141. }
  142. return change;
  143. }
  144. //----------------------------------------------------------------------
  145. // Determine if an account (id) is a GM account
  146. // and returns its level (or 0 if it isn't a GM account or if not found)
  147. //----------------------------------------------------------------------
  148. // Removed since nothing GM related goes on in the char server [CLOWNISIUS]
  149. int isGM(int account_id) {
  150. int i;
  151. for(i = 0; i < GM_num; i++)
  152. if (gm_account[i].account_id == account_id)
  153. return gm_account[i].level;
  154. return 0;
  155. }
  156. void read_gm_account(void) {
  157. if (gm_account != NULL)
  158. free(gm_account);
  159. GM_num = 0;
  160. sprintf(tmp_lsql, "SELECT `%s`,`%s` FROM `%s` WHERE `%s`>='%d'",login_db_account_id,login_db_level,login_db,login_db_level,lowest_gm_level);
  161. if (mysql_query(&lmysql_handle, tmp_lsql)) {
  162. printf("DB server Error (select %s to Memory)- %s\n",login_db,mysql_error(&lmysql_handle));
  163. return;
  164. }
  165. lsql_res = mysql_store_result(&lmysql_handle);
  166. if (lsql_res) {
  167. gm_account = calloc(sizeof(struct gm_account) * mysql_num_rows(lsql_res), 1);
  168. while ((lsql_row = mysql_fetch_row(lsql_res))) {
  169. gm_account[GM_num].account_id = atoi(lsql_row[0]);
  170. gm_account[GM_num].level = atoi(lsql_row[1]);
  171. GM_num++;
  172. }
  173. }
  174. mysql_free_result(lsql_res);
  175. }
  176. //=====================================================================================================
  177. int mmo_char_tosql(int char_id, struct mmo_charstatus *p){
  178. int i=0,party_exist,guild_exist;
  179. int eqcount=1;
  180. int noteqcount=1;
  181. char temp_str[32];
  182. struct itemtemp mapitem;
  183. if (char_id!=p->char_id) return 0;
  184. save_flag = p->char_id;
  185. printf("(\033[1;32m%d\033[0m) %s \trequest save char data - ",char_id,char_dat[0].name);
  186. //for(testcount=1;testcount<50;testcount++){//---------------------------test count--------------------
  187. // printf("test count : %d\n", testcount);
  188. // eqcount=1;
  189. // noteqcount=1;
  190. // dbeqcount=1;
  191. // dbnoteqcount=1;
  192. //-----------------------------------------------------------------------------------------------------
  193. //=========================================map inventory data > memory ===============================
  194. //map inventory data
  195. for(i=0;i<MAX_INVENTORY;i++){
  196. if(p->inventory[i].nameid>0){
  197. if(itemdb_isequip(p->inventory[i].nameid)==1){
  198. mapitem.equip[eqcount].flag=0;
  199. mapitem.equip[eqcount].id = p->inventory[i].id;
  200. mapitem.equip[eqcount].nameid=p->inventory[i].nameid;
  201. mapitem.equip[eqcount].amount = p->inventory[i].amount;
  202. mapitem.equip[eqcount].equip = p->inventory[i].equip;
  203. mapitem.equip[eqcount].identify = p->inventory[i].identify;
  204. mapitem.equip[eqcount].refine = p->inventory[i].refine;
  205. mapitem.equip[eqcount].attribute = p->inventory[i].attribute;
  206. mapitem.equip[eqcount].card[0] = p->inventory[i].card[0];
  207. mapitem.equip[eqcount].card[1] = p->inventory[i].card[1];
  208. mapitem.equip[eqcount].card[2] = p->inventory[i].card[2];
  209. mapitem.equip[eqcount].card[3] = p->inventory[i].card[3];
  210. eqcount++;
  211. }
  212. else if(itemdb_isequip(p->inventory[i].nameid)==0){
  213. mapitem.notequip[noteqcount].flag=0;
  214. mapitem.notequip[noteqcount].id = p->inventory[i].id;
  215. mapitem.notequip[noteqcount].nameid=p->inventory[i].nameid;
  216. mapitem.notequip[noteqcount].amount = p->inventory[i].amount;
  217. mapitem.notequip[noteqcount].equip = p->inventory[i].equip;
  218. mapitem.notequip[noteqcount].identify = p->inventory[i].identify;
  219. mapitem.notequip[noteqcount].refine = p->inventory[i].refine;
  220. mapitem.notequip[noteqcount].attribute = p->inventory[i].attribute;
  221. mapitem.notequip[noteqcount].card[0] = p->inventory[i].card[0];
  222. mapitem.notequip[noteqcount].card[1] = p->inventory[i].card[1];
  223. mapitem.notequip[noteqcount].card[2] = p->inventory[i].card[2];
  224. mapitem.notequip[noteqcount].card[3] = p->inventory[i].card[3];
  225. noteqcount++;
  226. }
  227. }
  228. }
  229. //printf("- Save item data to MySQL!\n");
  230. memitemdata_to_sql(mapitem, eqcount, noteqcount, p->char_id,TABLE_INVENTORY);
  231. //=========================================map cart data > memory ====================================
  232. eqcount=1;
  233. noteqcount=1;
  234. //map cart data
  235. for(i=0;i<MAX_CART;i++){
  236. if(p->cart[i].nameid>0){
  237. if(itemdb_isequip(p->cart[i].nameid)==1){
  238. mapitem.equip[eqcount].flag=0;
  239. mapitem.equip[eqcount].id = p->cart[i].id;
  240. mapitem.equip[eqcount].nameid=p->cart[i].nameid;
  241. mapitem.equip[eqcount].amount = p->cart[i].amount;
  242. mapitem.equip[eqcount].equip = p->cart[i].equip;
  243. mapitem.equip[eqcount].identify = p->cart[i].identify;
  244. mapitem.equip[eqcount].refine = p->cart[i].refine;
  245. mapitem.equip[eqcount].attribute = p->cart[i].attribute;
  246. mapitem.equip[eqcount].card[0] = p->cart[i].card[0];
  247. mapitem.equip[eqcount].card[1] = p->cart[i].card[1];
  248. mapitem.equip[eqcount].card[2] = p->cart[i].card[2];
  249. mapitem.equip[eqcount].card[3] = p->cart[i].card[3];
  250. eqcount++;
  251. }
  252. else if(itemdb_isequip(p->cart[i].nameid)==0){
  253. mapitem.notequip[noteqcount].flag=0;
  254. mapitem.notequip[noteqcount].id = p->cart[i].id;
  255. mapitem.notequip[noteqcount].nameid=p->cart[i].nameid;
  256. mapitem.notequip[noteqcount].amount = p->cart[i].amount;
  257. mapitem.notequip[noteqcount].equip = p->cart[i].equip;
  258. mapitem.notequip[noteqcount].identify = p->cart[i].identify;
  259. mapitem.notequip[noteqcount].refine = p->cart[i].refine;
  260. mapitem.notequip[noteqcount].attribute = p->cart[i].attribute;
  261. mapitem.notequip[noteqcount].card[0] = p->cart[i].card[0];
  262. mapitem.notequip[noteqcount].card[1] = p->cart[i].card[1];
  263. mapitem.notequip[noteqcount].card[2] = p->cart[i].card[2];
  264. mapitem.notequip[noteqcount].card[3] = p->cart[i].card[3];
  265. noteqcount++;
  266. }
  267. }
  268. }
  269. //printf("- Save cart data to MySQL!\n");
  270. memitemdata_to_sql(mapitem, eqcount, noteqcount, p->char_id,TABLE_CART);
  271. //=====================================================================================================
  272. //}//---------------------------test count------------------------------
  273. //check party_exist
  274. party_exist=0;
  275. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `party_id` = '%d'",party_db, p->party_id);
  276. if (mysql_query(&mysql_handle, tmp_sql)) {
  277. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  278. }
  279. sql_res = mysql_store_result(&mysql_handle);
  280. sql_row = mysql_fetch_row(sql_res);
  281. if (sql_row) party_exist = atoi(sql_row[0]);
  282. mysql_free_result(sql_res);
  283. //check guild_exist
  284. guild_exist=0;
  285. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id` = '%d'",guild_db, p->guild_id);
  286. if (mysql_query(&mysql_handle, tmp_sql)) {
  287. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  288. }
  289. sql_res = mysql_store_result(&mysql_handle);
  290. sql_row = mysql_fetch_row(sql_res);
  291. if (sql_row) guild_exist = atoi(sql_row[0]);
  292. mysql_free_result(sql_res);
  293. if (guild_exist==0) p->guild_id=0;
  294. if (party_exist==0) p->party_id=0;
  295. //sql query
  296. //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9
  297. //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15
  298. //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21
  299. //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27
  300. //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35
  301. //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`)
  302. //printf("- Save char data to MySQL!\n");
  303. sprintf(tmp_sql ,"UPDATE `%s` SET `class`='%d', `base_level`='%d', `job_level`='%d',"
  304. "`base_exp`='%d', `job_exp`='%d', `zeny`='%d',"
  305. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  306. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  307. "`option`='%d',`karma`='%d',`manner`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',"
  308. "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  309. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d',`partner_id`='%d' WHERE `account_id`='%d' AND `char_id` = '%d'",
  310. char_db, p->class, p->base_level, p->job_level,
  311. p->base_exp, p->job_exp, p->zeny,
  312. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  313. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  314. p->option, p->karma, p->manner, p->party_id, p->guild_id, p->pet_id,
  315. p->hair, p->hair_color, p->clothes_color,
  316. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  317. p->last_point.map, p->last_point.x, p->last_point.y,
  318. p->save_point.map, p->save_point.x, p->save_point.y, p->partner_id, p->account_id, p->char_id
  319. );
  320. if(mysql_query(&mysql_handle, tmp_sql)) {
  321. printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle));
  322. }
  323. //printf("- Save memo data to MySQL!\n");
  324. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  325. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, p->char_id);
  326. if(mysql_query(&mysql_handle, tmp_sql)) {
  327. printf("DB server Error (delete `memo`)- %s\n", mysql_error(&mysql_handle));
  328. }
  329. //insert here.
  330. for(i=0;i<10;i++){
  331. if(p->memo_point[i].map[0]){
  332. sprintf(tmp_sql,"INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ('%d', '%s', '%d', '%d')",
  333. memo_db, char_id, p->memo_point[i].map, p->memo_point[i].x, p->memo_point[i].y);
  334. if(mysql_query(&mysql_handle, tmp_sql))
  335. printf("DB server Error (insert `memo`)- %s\n", mysql_error(&mysql_handle));
  336. }
  337. }
  338. //printf("- Save skill data to MySQL!\n");
  339. //`skill` (`char_id`, `id`, `lv`)
  340. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, p->char_id);
  341. if(mysql_query(&mysql_handle, tmp_sql)) {
  342. printf("DB server Error (delete `skill`)- %s\n", mysql_error(&mysql_handle));
  343. }
  344. //printf("- Insert skill \n");
  345. //insert here.
  346. for(i=0;i<MAX_SKILL;i++){
  347. if(p->skill[i].id){
  348. if (p->skill[i].id && p->skill[i].flag!=1) {
  349. sprintf(tmp_sql,"INSERT delayed INTO `%s`(`char_id`, `id`, `lv`) VALUES ('%d', '%d','%d')",
  350. skill_db, char_id, p->skill[i].id, (p->skill[i].flag==0)?p->skill[i].lv:p->skill[i].flag-2);
  351. if(mysql_query(&mysql_handle, tmp_sql)) {
  352. printf("DB server Error (insert `skill`)- %s\n", mysql_error(&mysql_handle));
  353. }
  354. }
  355. }
  356. }
  357. //printf("- Save global_reg_value data to MySQL!\n");
  358. //`global_reg_value` (`char_id`, `str`, `value`)
  359. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, p->char_id);
  360. if (mysql_query(&mysql_handle, tmp_sql)) {
  361. printf("DB server Error (delete `global_reg_value`)- %s\n", mysql_error(&mysql_handle));
  362. }
  363. //insert here.
  364. for(i=0;i<p->global_reg_num;i++){
  365. if (p->global_reg[i].str) {
  366. if(p->global_reg[i].value !=0){
  367. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`, `str`, `value`) VALUES ('%d', '%s','%d')",
  368. reg_db, char_id, jstrescapecpy(temp_str,(unsigned char*)p->global_reg[i].str), p->global_reg[i].value);
  369. if(mysql_query(&mysql_handle, tmp_sql)) {
  370. printf("DB server Error (insert `global_reg_value`)- %s\n", mysql_error(&mysql_handle));
  371. }
  372. }
  373. }
  374. }
  375. printf("saving char is done.\n");
  376. save_flag = 0;
  377. return 0;
  378. }
  379. int memitemdata_to_sql(struct itemtemp mapitem, int eqcount, int noteqcount, int char_id, int tableswitch){
  380. //equ
  381. int i, j;
  382. int dbeqcount = 1;
  383. int dbnoteqcount = 1;
  384. struct itemtemp dbitem;
  385. char tablename[16];
  386. char selectoption[16];
  387. switch (tableswitch){
  388. case TABLE_INVENTORY:
  389. sprintf(tablename,"%s",inventory_db);
  390. sprintf(selectoption,"char_id");
  391. break;
  392. case TABLE_CART:
  393. sprintf(tablename,"%s",cart_db);
  394. sprintf(selectoption,"char_id");
  395. break;
  396. case TABLE_STORAGE:
  397. sprintf(tablename,"%s",storage_db);
  398. sprintf(selectoption,"account_id");
  399. break;
  400. case TABLE_GUILD_STORAGE:
  401. sprintf(tablename,"%s",guild_storage_db);
  402. sprintf(selectoption,"guild_id");
  403. break;
  404. }
  405. //printf("Working Table : %s \n",tablename);
  406. //=======================================mysql database data > memory===============================================
  407. sprintf(tmp_sql, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3` "
  408. "FROM `%s` WHERE `%s`='%d'",tablename ,selectoption ,char_id);
  409. if (mysql_query(&mysql_handle, tmp_sql)) {
  410. printf("DB server Error (select `%s` to Memory)- %s\n",tablename ,mysql_error(&mysql_handle));
  411. }
  412. sql_res = mysql_store_result(&mysql_handle);
  413. if (sql_res) {
  414. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  415. if (itemdb_isequip(atoi(sql_row[1]))==1){
  416. dbitem.equip[dbeqcount].flag=0;
  417. dbitem.equip[dbeqcount].id = atoi(sql_row[0]);
  418. dbitem.equip[dbeqcount].nameid = atoi(sql_row[1]);
  419. dbitem.equip[dbeqcount].amount = atoi(sql_row[2]);
  420. dbitem.equip[dbeqcount].equip = atoi(sql_row[3]);
  421. dbitem.equip[dbeqcount].identify = atoi(sql_row[4]);
  422. dbitem.equip[dbeqcount].refine = atoi(sql_row[5]);
  423. dbitem.equip[dbeqcount].attribute = atoi(sql_row[6]);
  424. dbitem.equip[dbeqcount].card[0] = atoi(sql_row[7]);
  425. dbitem.equip[dbeqcount].card[1] = atoi(sql_row[8]);
  426. dbitem.equip[dbeqcount].card[2] = atoi(sql_row[9]);
  427. dbitem.equip[dbeqcount].card[3] = atoi(sql_row[10]);
  428. dbeqcount++;
  429. }else {
  430. dbitem.notequip[dbnoteqcount].flag=0;
  431. dbitem.notequip[dbnoteqcount].id = atoi(sql_row[0]);
  432. dbitem.notequip[dbnoteqcount].nameid = atoi(sql_row[1]);
  433. dbitem.notequip[dbnoteqcount].amount = atoi(sql_row[2]);
  434. dbitem.notequip[dbnoteqcount].equip = atoi(sql_row[3]);
  435. dbitem.notequip[dbnoteqcount].identify = atoi(sql_row[4]);
  436. dbitem.notequip[dbnoteqcount].refine = atoi(sql_row[5]);
  437. dbitem.notequip[dbnoteqcount].attribute = atoi(sql_row[6]);
  438. dbitem.notequip[dbnoteqcount].card[0] = atoi(sql_row[7]);
  439. dbitem.notequip[dbnoteqcount].card[1] = atoi(sql_row[8]);
  440. dbitem.notequip[dbnoteqcount].card[2] = atoi(sql_row[9]);
  441. dbitem.notequip[dbnoteqcount].card[3] = atoi(sql_row[10]);
  442. dbnoteqcount++;
  443. }
  444. }
  445. mysql_free_result(sql_res);
  446. }
  447. //==============================================Memory data > SQL ===============================
  448. //======================================Equip ITEM=======================================
  449. if((eqcount==1) && (dbeqcount==1)){//printf("%s Equip Empty\n",tablename);
  450. //item empty
  451. } else {
  452. for(i=1;i<eqcount;i++){
  453. for(j=1;j<dbeqcount;j++){
  454. if(mapitem.equip[i].flag==1) break;
  455. if(!(dbitem.equip[j].flag==1)){
  456. if(mapitem.equip[i].nameid==dbitem.equip[j].nameid){
  457. if ((mapitem.equip[i].equip==dbitem.equip[j].equip) && (mapitem.equip[i].identify==dbitem.equip[j].identify) && (mapitem.equip[i].amount==dbitem.equip[j].amount) &&
  458. (mapitem.equip[i].refine==dbitem.equip[j].refine) && (mapitem.equip[i].attribute==dbitem.equip[j].attribute) && (mapitem.equip[i].card[0]==dbitem.equip[j].card[0]) &&
  459. (mapitem.equip[i].card[1]==dbitem.equip[j].card[1]) && (mapitem.equip[i].card[2]==dbitem.equip[j].card[2]) && (mapitem.equip[i].card[3]==dbitem.equip[j].card[3])) {
  460. mapitem.equip[i].flag = 1;
  461. dbitem.equip[j].flag = 1;
  462. //printf("the same item : %d , equip : %d , i : %d , flag : %d\n", mapitem.equip[i].nameid,mapitem.equip[i].equip , i, mapitem.equip[i].flag); //DEBUG-STRING
  463. } else {
  464. sprintf(tmp_sql,"UPDATE `%s` SET `equip`='%d', `identify`='%d', `refine`='%d',"
  465. "`attribute`='%d', `card0`='%d', `card1`='%d', `card2`='%d', `card3`='%d', `amount`='%d' WHERE `id`='%d' LIMIT 1",
  466. tablename, mapitem.equip[i].equip, mapitem.equip[i].identify, mapitem.equip[i].refine,mapitem.equip[i].attribute, mapitem.equip[i].card[0],
  467. mapitem.equip[i].card[1], mapitem.equip[i].card[2], mapitem.equip[i].card[3], mapitem.equip[i].amount, dbitem.equip[j].id);
  468. //printf("%s\n",tmp_sql);
  469. if(mysql_query(&mysql_handle, tmp_sql))
  470. printf("DB server Error (UPdate `equ %s`)- %s\n", tablename, mysql_error(&mysql_handle));
  471. mapitem.equip[i].flag=1;
  472. dbitem.equip[j].flag=1;
  473. //printf("not the same item : %d ; i : %d ; flag : %d\n", mapitem.equip[i].nameid, i, mapitem.equip[i].flag);
  474. }
  475. }
  476. }
  477. }
  478. }
  479. //printf("dbeqcount = %d\n",dbeqcount);
  480. for(i=1;i<dbeqcount;i++){
  481. //printf("dbitem.equip[i].flag = %d , dbitem.equip[i].id = %d\n",dbitem.equip[i].flag,dbitem.equip[i].id);
  482. if (dbitem.equip[i].flag == 0) {
  483. sprintf(tmp_sql,"DELETE from `%s` where `id`='%d'",tablename , dbitem.equip[i].id);
  484. //printf("%s", tmp_sql);
  485. if(mysql_query(&mysql_handle, tmp_sql))
  486. printf("DB server Error (DELETE `equ %s`)- %s\n", tablename ,mysql_error(&mysql_handle));
  487. }
  488. }
  489. for(i=1;i<eqcount;i++){
  490. if(mapitem.equip[i].flag==0){
  491. sprintf(tmp_sql,"INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)"
  492. " VALUES ( '%d','%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  493. tablename, selectoption, char_id, mapitem.equip[i].nameid, mapitem.equip[i].amount, mapitem.equip[i].equip, mapitem.equip[i].identify, mapitem.equip[i].refine,
  494. mapitem.equip[i].attribute, mapitem.equip[i].card[0], mapitem.equip[i].card[1], mapitem.equip[i].card[2], mapitem.equip[i].card[3]);
  495. //printf("%s", tmp_sql);
  496. if(mysql_query(&mysql_handle, tmp_sql))
  497. printf("DB server Error (INSERT `equ %s`)- %s\n",tablename ,mysql_error(&mysql_handle));
  498. }
  499. }
  500. //======================================DEBUG=================================================
  501. // gettimeofday(&tv,NULL);
  502. // strftime(tmpstr,24,"%Y-%m-%d %H:%M:%S",localtime(&(tv.tv_sec)));
  503. // printf("\n\n");
  504. // printf("Working Table Name : EQU %s, Count : map %3d | db %3d \n",tablename ,eqcount ,dbeqcount);
  505. // printf("*********************************************************************************\n");
  506. // printf("======================================MAP===================Char ID %10d===\n",char_id);
  507. // printf("==flag ===name ===equip===ident===amoun===attri===card0===card1===card2===card3==\n");
  508. // for(j=1;j<eqcount;j++)
  509. // printf("| %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d |\n", mapitem.equip[j].flag,mapitem.equip[j].nameid, mapitem.equip[j].equip, mapitem.equip[j].identify, mapitem.equip[j].refine,mapitem.equip[j].attribute, mapitem.equip[j].card[0], mapitem.equip[j].card[1], mapitem.equip[j].card[2], mapitem.equip[j].card[3]);
  510. // printf("======================================DB=========================================\n");
  511. // printf("==flag ===name ===equip===ident===refin===attri===card0===card1===card2===card3==\n");
  512. // for(j=1;j<dbeqcount;j++)
  513. // printf("| %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d |\n", dbitem.equip[j].flag ,dbitem.equip[j].nameid, dbitem.equip[j].equip, dbitem.equip[j].identify, dbitem.equip[j].amount,dbitem.equip[j].attribute, dbitem.equip[j].card[0], dbitem.equip[j].card[1], dbitem.equip[j].card[2], dbitem.equip[j].card[3]);
  514. // printf("=================================================================================\n");
  515. // printf("=================================================Data Time %s===\n", tmpstr);
  516. // printf("=================================================================================\n");
  517. }
  518. //======================================DEBUG==================================================
  519. //=============================Not Equip ITEM==========================================
  520. if((noteqcount==1) && (dbnoteqcount==1)){
  521. //printf("%s Not Equip Empty\n",tablename);
  522. //item empty
  523. } else {
  524. for(i=1;i<noteqcount;i++){
  525. for(j=1;j<dbnoteqcount;j++){
  526. if(mapitem.notequip[i].flag==1) break;
  527. if(!(dbitem.notequip[j].flag==1)){
  528. if(mapitem.notequip[i].nameid==dbitem.notequip[j].nameid){
  529. if ((mapitem.notequip[i].amount==dbitem.notequip[j].amount) && (mapitem.notequip[i].equip==dbitem.notequip[j].equip) && (mapitem.notequip[i].identify==dbitem.notequip[j].identify)
  530. && (mapitem.notequip[i].attribute==dbitem.notequip[j].attribute))
  531. { mapitem.notequip[i].flag=1;
  532. dbitem.notequip[j].flag=1;
  533. //printf("the same item : %d ; i : %d ; flag : %d\n", mapitem.notequip[i].nameid, i, mapitem.notequip[i].flag); //DEBUG-STRING
  534. }
  535. else{
  536. sprintf(tmp_sql,"UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d',"
  537. "`attribute`='%d' WHERE `%s`='%d' AND `nameid`='%d'",
  538. tablename, mapitem.notequip[i].amount, mapitem.notequip[i].equip, mapitem.notequip[i].identify, mapitem.notequip[i].attribute,
  539. selectoption, char_id, mapitem.notequip[i].nameid);
  540. //printf("%s",tmp_sql);
  541. if(mysql_query(&mysql_handle, tmp_sql))
  542. printf("DB server Error (UPdate `notequ %s`)- %s\n",tablename ,mysql_error(&mysql_handle));
  543. mapitem.notequip[i].flag=1;
  544. dbitem.notequip[j].flag=1;
  545. }
  546. }
  547. }
  548. }
  549. }
  550. //printf("dbnoteqcount = %d\n",dbnoteqcount);
  551. for(i=1;i<dbnoteqcount;i++){
  552. //printf("dbitem.notequip[i].flag = %d , dbitem.notequip[i].id = %d\n",dbitem.notequip[i].flag,dbitem.notequip[i].id);
  553. if(dbitem.notequip[i].flag==0){
  554. sprintf(tmp_sql,"DELETE from `%s` where `id`='%d'", tablename, dbitem.notequip[i].id);
  555. //printf("%s", tmp_sql);
  556. if(mysql_query(&mysql_handle, tmp_sql))
  557. printf("DB server Error (DELETE `notequ %s`)- %s\n", tablename ,mysql_error(&mysql_handle));
  558. }
  559. }
  560. for(i=1;i<noteqcount;i++){
  561. if(mapitem.notequip[i].flag==0) {
  562. sprintf(tmp_sql,"INSERT INTO `%s`( `%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)"
  563. " VALUES ('%d','%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  564. tablename ,selectoption , char_id, mapitem.notequip[i].nameid, mapitem.notequip[i].amount, mapitem.notequip[i].equip, mapitem.notequip[i].identify, mapitem.notequip[i].refine,
  565. mapitem.notequip[i].attribute, mapitem.notequip[i].card[0], mapitem.notequip[i].card[1], mapitem.notequip[i].card[2], mapitem.notequip[i].card[3]);
  566. //printf("%s", tmp_sql);
  567. if(mysql_query(&mysql_handle, tmp_sql))
  568. printf("DB server Error (INSERT `notequ %s`)- %s\n", tablename, mysql_error(&mysql_handle));
  569. }
  570. }
  571. //======================================DEBUG=================================================
  572. // gettimeofday(&tv,NULL);
  573. // strftime(tmpstr,24,"%Y-%m-%d %H:%M:%S",localtime(&(tv.tv_sec)));
  574. // printf("\n\n");
  575. // printf("Working Table Name : Not EQU %s, Count : map %3d | db %3d \n",tablename ,noteqcount ,dbnoteqcount);
  576. // printf("*********************************************************************************\n");
  577. // printf("======================================MAP===================Char ID %10d===\n",char_id);
  578. // printf("==flag ===name ===equip===ident===refin===attri===card0===card1===card2===card3==\n");
  579. // for(j=1;j<noteqcount;j++)
  580. // printf("| %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d |\n", mapitem.notequip[j].flag,mapitem.notequip[j].nameid, mapitem.notequip[j].equip, mapitem.notequip[j].identify, mapitem.notequip[j].refine,mapitem.notequip[j].attribute, mapitem.notequip[j].card[0], mapitem.notequip[j].card[1], mapitem.notequip[j].card[2], mapitem.notequip[j].card[3]);
  581. // printf("======================================DB=========================================\n");
  582. // printf("==flag ===name ===equip===ident===refin===attri===card0===card1===card2===card3==\n");
  583. // for(j=1;j<dbnoteqcount;j++)
  584. // printf("| %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d | %5d |\n", dbitem.notequip[j].flag ,dbitem.notequip[j].nameid, dbitem.notequip[j].equip, dbitem.notequip[j].identify, dbitem.notequip[j].refine,dbitem.notequip[j].attribute, dbitem.notequip[j].card[0], dbitem.notequip[j].card[1], dbitem.notequip[j].card[2], dbitem.notequip[j].card[3]);
  585. // printf("=================================================================================\n");
  586. // printf("=================================================Data Time %s===\n", tmpstr);
  587. // printf("=================================================================================\n");
  588. //
  589. }
  590. return 0;
  591. }
  592. //=====================================================================================================
  593. int mmo_char_fromsql(int char_id, struct mmo_charstatus *p, int online){
  594. int i, n;
  595. memset(p, 0, sizeof(struct mmo_charstatus));
  596. p->char_id = char_id;
  597. printf("Loaded: ");
  598. //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9
  599. //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15
  600. //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21
  601. //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27
  602. //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35
  603. //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`)
  604. //splite 2 parts. cause veeeery long SQL syntax
  605. sprintf(tmp_sql, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  606. "`str`,`agi`,`vit`,`int`,`dex`,`luk`, `max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id);
  607. if (mysql_query(&mysql_handle, tmp_sql)) {
  608. printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle));
  609. }
  610. sql_res = mysql_store_result(&mysql_handle);
  611. if (sql_res) {
  612. sql_row = mysql_fetch_row(sql_res);
  613. p->char_id = char_id;
  614. p->account_id = atoi(sql_row[1]);
  615. p->char_num = atoi(sql_row[2]);
  616. strcpy(p->name, sql_row[3]);
  617. p->class = atoi(sql_row[4]);
  618. p->base_level = atoi(sql_row[5]);
  619. p->job_level = atoi(sql_row[6]);
  620. p->base_exp = atoi(sql_row[7]);
  621. p->job_exp = atoi(sql_row[8]);
  622. p->zeny = atoi(sql_row[9]);
  623. p->str = atoi(sql_row[10]);
  624. p->agi = atoi(sql_row[11]);
  625. p->vit = atoi(sql_row[12]);
  626. p->int_ = atoi(sql_row[13]);
  627. p->dex = atoi(sql_row[14]);
  628. p->luk = atoi(sql_row[15]);
  629. p->max_hp = atoi(sql_row[16]);
  630. p->hp = atoi(sql_row[17]);
  631. p->max_sp = atoi(sql_row[18]);
  632. p->sp = atoi(sql_row[19]);
  633. p->status_point = atoi(sql_row[20]);
  634. p->skill_point = atoi(sql_row[21]);
  635. //free mysql result.
  636. mysql_free_result(sql_res);
  637. } else
  638. printf("char1 - failed\n"); //Error?! ERRRRRR WHAT THAT SAY!?
  639. printf("(\033[1;32m%d\033[0m)\033[1;32m%s\033[0m\t[",p->char_id,p->name);
  640. printf("char1 ");
  641. sprintf(tmp_sql, "SELECT `option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`hair`,`hair_color`,"
  642. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,"
  643. "`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`, `partner_id` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id);
  644. if (mysql_query(&mysql_handle, tmp_sql)) {
  645. printf("DB server Error (select `char2`)- %s\n", mysql_error(&mysql_handle));
  646. }
  647. sql_res = mysql_store_result(&mysql_handle);
  648. if (sql_res) {
  649. sql_row = mysql_fetch_row(sql_res);
  650. p->option = atoi(sql_row[0]); p->karma = atoi(sql_row[1]); p->manner = atoi(sql_row[2]);
  651. p->party_id = atoi(sql_row[3]); p->guild_id = atoi(sql_row[4]); p->pet_id = atoi(sql_row[5]);
  652. p->hair = atoi(sql_row[6]); p->hair_color = atoi(sql_row[7]); p->clothes_color = atoi(sql_row[8]);
  653. p->weapon = atoi(sql_row[9]); p->shield = atoi(sql_row[10]);
  654. p->head_top = atoi(sql_row[11]); p->head_mid = atoi(sql_row[12]); p->head_bottom = atoi(sql_row[13]);
  655. strcpy(p->last_point.map,sql_row[14]); p->last_point.x = atoi(sql_row[15]); p->last_point.y = atoi(sql_row[16]);
  656. strcpy(p->save_point.map,sql_row[17]); p->save_point.x = atoi(sql_row[18]); p->save_point.y = atoi(sql_row[19]);
  657. p->partner_id = atoi(sql_row[20]);
  658. //free mysql result.
  659. mysql_free_result(sql_res);
  660. } else
  661. printf("char2 - failed\n"); //Error?! ERRRRRR WHAT THAT SAY!?
  662. printf("char2 ");
  663. //read memo data
  664. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  665. sprintf(tmp_sql, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`='%d'",memo_db, char_id);
  666. if (mysql_query(&mysql_handle, tmp_sql)) {
  667. printf("DB server Error (select `memo`)- %s\n", mysql_error(&mysql_handle));
  668. }
  669. sql_res = mysql_store_result(&mysql_handle);
  670. if (sql_res) {
  671. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  672. strcpy (p->memo_point[i].map,sql_row[0]);
  673. p->memo_point[i].x=atoi(sql_row[1]);
  674. p->memo_point[i].y=atoi(sql_row[2]);
  675. //i ++;
  676. }
  677. mysql_free_result(sql_res);
  678. }
  679. printf("memo ");
  680. //read inventory
  681. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  682. sprintf(tmp_sql, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`"
  683. "FROM `%s` WHERE `char_id`='%d'",inventory_db, char_id);
  684. if (mysql_query(&mysql_handle, tmp_sql)) {
  685. printf("DB server Error (select `inventory`)- %s\n", mysql_error(&mysql_handle));
  686. }
  687. sql_res = mysql_store_result(&mysql_handle);
  688. if (sql_res) {
  689. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  690. p->inventory[i].id = atoi(sql_row[0]);
  691. p->inventory[i].nameid = atoi(sql_row[1]);
  692. p->inventory[i].amount = atoi(sql_row[2]);
  693. p->inventory[i].equip = atoi(sql_row[3]);
  694. p->inventory[i].identify = atoi(sql_row[4]);
  695. p->inventory[i].refine = atoi(sql_row[5]);
  696. p->inventory[i].attribute = atoi(sql_row[6]);
  697. p->inventory[i].card[0] = atoi(sql_row[7]);
  698. p->inventory[i].card[1] = atoi(sql_row[8]);
  699. p->inventory[i].card[2] = atoi(sql_row[9]);
  700. p->inventory[i].card[3] = atoi(sql_row[10]);
  701. }
  702. mysql_free_result(sql_res);
  703. }
  704. printf("inventory ");
  705. //read cart.
  706. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  707. sprintf(tmp_sql, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`"
  708. "FROM `%s` WHERE `char_id`='%d'",cart_db, char_id);
  709. if (mysql_query(&mysql_handle, tmp_sql)) {
  710. printf("DB server Error (select `cart_inventory`)- %s\n", mysql_error(&mysql_handle));
  711. }
  712. sql_res = mysql_store_result(&mysql_handle);
  713. if (sql_res) {
  714. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  715. p->cart[i].id = atoi(sql_row[0]);
  716. p->cart[i].nameid = atoi(sql_row[1]);
  717. p->cart[i].amount = atoi(sql_row[2]);
  718. p->cart[i].equip = atoi(sql_row[3]);
  719. p->cart[i].identify = atoi(sql_row[4]);
  720. p->cart[i].refine = atoi(sql_row[5]);
  721. p->cart[i].attribute = atoi(sql_row[6]);
  722. p->cart[i].card[0] = atoi(sql_row[7]);
  723. p->cart[i].card[1] = atoi(sql_row[8]);
  724. p->cart[i].card[2] = atoi(sql_row[9]);
  725. p->cart[i].card[3] = atoi(sql_row[10]);
  726. }
  727. mysql_free_result(sql_res);
  728. }
  729. printf("cart ");
  730. //read skill
  731. //`skill` (`char_id`, `id`, `lv`)
  732. sprintf(tmp_sql, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`='%d'",skill_db, char_id);
  733. if (mysql_query(&mysql_handle, tmp_sql)) {
  734. printf("DB server Error (select `skill`)- %s\n", mysql_error(&mysql_handle));
  735. }
  736. sql_res = mysql_store_result(&mysql_handle);
  737. if (sql_res) {
  738. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  739. n = atoi(sql_row[0]);
  740. p->skill[n].id = n; //memory!? shit!.
  741. p->skill[n].lv = atoi(sql_row[1]);
  742. }
  743. mysql_free_result(sql_res);
  744. }
  745. printf("skill ");
  746. //global_reg
  747. //`global_reg_value` (`char_id`, `str`, `value`)
  748. sprintf(tmp_sql, "SELECT `str`, `value` FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, char_id);
  749. if (mysql_query(&mysql_handle, tmp_sql)) {
  750. printf("DB server Error (select `global_reg_value`)- %s\n", mysql_error(&mysql_handle));
  751. }
  752. i = 0;
  753. sql_res = mysql_store_result(&mysql_handle);
  754. if (sql_res) {
  755. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  756. strcpy (p->global_reg[i].str, sql_row[0]);
  757. p->global_reg[i].value = atoi (sql_row[1]);
  758. }
  759. mysql_free_result(sql_res);
  760. }
  761. p->global_reg_num=i;
  762. if (online) {
  763. sprintf(tmp_sql, "UPDATE `%s` SET `online`='%d' WHERE `char_id`='%d'",char_db,online,char_id);
  764. if (mysql_query(&mysql_handle, tmp_sql)) {
  765. printf("DB server Error (set char online)- %s\n", mysql_error(&mysql_handle));
  766. }
  767. }
  768. printf("global_reg]\n"); //ok. all data load successfuly!
  769. //printf("char cloade");
  770. return 1;
  771. }
  772. //==========================================================================================================
  773. int mmo_char_sql_init(void) {
  774. int i;
  775. printf("init start.......\n");
  776. // memory initialize
  777. // no need to set twice size in this routine. but some cause segmentation error. :P
  778. printf("initializing char memory...(%d byte)\n",sizeof(struct mmo_charstatus)*2);
  779. CREATE(char_dat, struct mmo_charstatus, 2);
  780. memset(char_dat, 0, sizeof(struct mmo_charstatus)*2);
  781. /* Initialized in inter.c already [Wizputer]
  782. // DB connection initialized
  783. // for char-server session only
  784. mysql_init(&mysql_handle);
  785. printf("Connect DB server....(char server)\n");
  786. if(!mysql_real_connect(&mysql_handle, char_server_ip, char_server_id, char_server_pw, char_server_db ,char_server_port, (char *)NULL, 0)) {
  787. // SQL connection pointer check
  788. printf("%s\n",mysql_error(&mysql_handle));
  789. exit(1);
  790. } else {
  791. printf("connect success! (char server)\n");
  792. }
  793. */
  794. sprintf(tmp_sql , "SELECT count(*) FROM `%s`", char_db);
  795. if (mysql_query(&mysql_handle, tmp_sql)) {
  796. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  797. }
  798. sql_res = mysql_store_result(&mysql_handle) ;
  799. sql_row = mysql_fetch_row(sql_res);
  800. printf("total char data -> '%s'.......\n",sql_row[0]);
  801. i = atoi (sql_row[0]);
  802. mysql_free_result(sql_res);
  803. if (i !=0) {
  804. sprintf(tmp_sql , "SELECT max(`char_id`) FROM `%s`", char_db);
  805. if (mysql_query(&mysql_handle, tmp_sql)) {
  806. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  807. }
  808. sql_res = mysql_store_result(&mysql_handle) ;
  809. sql_row = mysql_fetch_row(sql_res);
  810. char_id_count = atoi (sql_row[0]);
  811. mysql_free_result(sql_res);
  812. } else
  813. printf("set char_id_count: %d.......\n",char_id_count);
  814. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `online`='1'", char_db);
  815. if (mysql_query(&mysql_handle, tmp_sql)) {
  816. printf("DB server Error (reset_online `%s`)- %s\n", char_db, mysql_error(&mysql_handle));
  817. }
  818. printf("init end.......\n");
  819. return 0;
  820. }
  821. //==========================================================================================================
  822. int make_new_char_sql(int fd, unsigned char *dat) {
  823. struct char_session_data *sd;
  824. char t_name[100];
  825. int i;
  826. //aphostropy error check! - fixed!
  827. jstrescapecpy(t_name, dat);
  828. printf("making new char -");
  829. sd = session[fd]->session_data;
  830. // Check Authorised letters/symbols in the name of the character
  831. if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
  832. for (i = 0; i < strlen(dat); i++)
  833. if (strchr(char_name_letters, dat[i]) == NULL)
  834. return -1;
  835. } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
  836. for (i = 0; i < strlen(dat); i++)
  837. if (strchr(char_name_letters, dat[i]) != NULL)
  838. return -1;
  839. } // else, all letters/symbols are authorised (except control char removed before)
  840. //check stat error
  841. if ((dat[24]+dat[25]+dat[26]+dat[27]+dat[28]+dat[29]!=5*6 ) ||
  842. (dat[30] >= 9) ||
  843. (dat[33] <= 0) || (dat[33] >= 20) ||
  844. (dat[31] >= 9)) {
  845. // check individual stat value
  846. for(i = 24; i <= 29; i++) {
  847. if (dat[i] < 1 || dat[i] > 9) {
  848. return -1;
  849. }
  850. }
  851. // char.log to charlog
  852. sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  853. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  854. charlog_db,"make new char error", sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  855. //query
  856. if (mysql_query(&mysql_handle, tmp_sql)) {
  857. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  858. }
  859. printf("make new char error %d-%d %s %d, %d, %d, %d, %d, %d %d, %d" RETCODE,
  860. fd, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  861. return -1;
  862. }
  863. // char.log to charlog
  864. sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  865. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  866. charlog_db,"make new char", sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  867. //query
  868. if (mysql_query(&mysql_handle, tmp_sql)) {
  869. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  870. }
  871. printf("make new char %d-%d %s %d, %d, %d, %d, %d, %d - %d, %d" RETCODE,
  872. fd, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  873. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `name` = '%s'",char_db, t_name);
  874. if (mysql_query(&mysql_handle, tmp_sql)) {
  875. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  876. return -1;
  877. }
  878. sql_res = mysql_store_result(&mysql_handle);
  879. sql_row = mysql_fetch_row(sql_res);
  880. printf("\033[1;32m name check result : %s -\033[0m ",sql_row[0]);
  881. if (atoi(sql_row[0]) > 0) {
  882. mysql_free_result(sql_res);
  883. return -1;
  884. } else
  885. mysql_free_result(sql_res);
  886. // check char slot.
  887. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d'",char_db, sd->account_id, dat[30]);
  888. if (mysql_query(&mysql_handle, tmp_sql)) {
  889. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  890. }
  891. sql_res = mysql_store_result(&mysql_handle);
  892. sql_row = mysql_fetch_row(sql_res);
  893. //printf("slot check result : %s\n",sql_row[0]);
  894. if (atoi(sql_row[0]) > 0) {
  895. mysql_free_result(sql_res);
  896. return -1;
  897. } else
  898. mysql_free_result(sql_res);
  899. char_id_count++;
  900. // make new char.
  901. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`account_id`,`char_num`,`name`,`zeny`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,`hair`,`hair_color`)"
  902. " VALUES ('%d', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d')",
  903. char_db, char_id_count, sd->account_id , dat[30] , t_name, start_zeny, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29],
  904. (40 * (100 + dat[26])/100) , (40 * (100 + dat[26])/100 ), (11 * (100 + dat[27])/100), (11 * (100 + dat[27])/100), dat[33], dat[31]);
  905. if (mysql_query(&mysql_handle, tmp_sql)) {
  906. printf("DB server Error (insert `char`)- %s\n", mysql_error(&mysql_handle));
  907. }
  908. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  909. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')",
  910. inventory_db, char_id_count, 1201,1,0x02,1); //add Knife
  911. if (mysql_query(&mysql_handle, tmp_sql)) {
  912. printf("DB server Error (insert `inventory`)- %s\n", mysql_error(&mysql_handle));
  913. }
  914. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')",
  915. inventory_db, char_id_count, 2301,1,0x10,1); //add Cotton Shirt
  916. if (mysql_query(&mysql_handle, tmp_sql)) {
  917. printf("DB server Error (insert `inventory`)- %s\n", mysql_error(&mysql_handle));
  918. }
  919. // respawn map and start point set
  920. sprintf(tmp_sql,"UPDATE `%s` SET `last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d' WHERE `char_id` = '%d'",
  921. char_db, start_point.map,start_point.x,start_point.y, start_point.map,start_point.x,start_point.y, char_id_count);
  922. if (mysql_query(&mysql_handle, tmp_sql)) {
  923. printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle));
  924. }
  925. printf("making new char success - id:(\033[1;32m%d\033[0m\tname:\033[1;32%s\033[0m\n", char_id_count, t_name);
  926. return char_id_count;
  927. }
  928. //==========================================================================================================
  929. void mmo_char_sync(void){
  930. printf("mmo_char_sync() - nothing to do\n");
  931. }
  932. // to do
  933. ///////////////////////////
  934. int mmo_char_sync_timer(int tid, unsigned int tick, int id, int data) {
  935. printf("mmo_char_sync_timer() tic - no works to do\n");
  936. return 0;
  937. }
  938. int count_users(void) {
  939. int i, users;
  940. if (login_fd > 0 && session[login_fd]){
  941. users = 0;
  942. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  943. if (server_fd[i] >= 0) {
  944. users += server[i].users;
  945. }
  946. }
  947. return users;
  948. }
  949. return 0;
  950. }
  951. int send_users_tologin(int tid, unsigned int tick, int id, int data) {
  952. int users = count_users();
  953. char buf[16];
  954. if (login_fd > 0 && session[login_fd]) {
  955. // send number of user to login server
  956. WFIFOW(login_fd,0) = 0x2714;
  957. WFIFOL(login_fd,2) = users;
  958. WFIFOSET(login_fd,6);
  959. }
  960. // send number of players to all map-servers
  961. WBUFW(buf,0) = 0x2b00;
  962. WBUFL(buf,2) = users;
  963. mapif_sendall(buf, 6);
  964. return 0;
  965. }
  966. int mmo_char_send006b(int fd, struct char_session_data *sd) {
  967. int i, j, found_num = 0;
  968. struct mmo_charstatus *p = NULL;
  969. // hehe. commented other. anyway there's no need to use older version.
  970. // if use older packet version just uncomment that!
  971. //#ifdef NEW_006b
  972. const int offset = 24;
  973. //#else
  974. // int offset = 4;
  975. //#endif
  976. printf("mmo_char_send006b start.. (account:%d)\n",sd->account_id);
  977. // printf("offset -> %d...\n",offset);
  978. //search char.
  979. sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id` = '%d'",char_db, sd->account_id);
  980. if (mysql_query(&mysql_handle, tmp_sql)) {
  981. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  982. }
  983. sql_res = mysql_store_result(&mysql_handle);
  984. if (sql_res) {
  985. found_num = mysql_num_rows(sql_res);
  986. printf("number of chars: %d\n", found_num);
  987. i = 0;
  988. while((sql_row = mysql_fetch_row(sql_res))) {
  989. sd->found_char[i] = atoi(sql_row[0]);
  990. i++;
  991. }
  992. mysql_free_result(sql_res);
  993. }
  994. // printf("char fetching end (total: %d)....\n", found_num);
  995. for(i = found_num; i < 9; i++)
  996. sd->found_char[i] = -1;
  997. memset(WFIFOP(fd, 0), 0, offset + found_num * 106);
  998. WFIFOW(fd, 0) = 0x6b;
  999. WFIFOW(fd, 2) = offset + found_num * 106;
  1000. printf("(\033[1;13m%d\033[0m) Request Char Data:\n",sd->account_id);
  1001. for(i = 0; i < found_num; i++) {
  1002. mmo_char_fromsql(sd->found_char[i], char_dat, 0);
  1003. p = &char_dat[0];
  1004. j = offset + (i * 106); // increase speed of code
  1005. WFIFOL(fd,j) = p->char_id;
  1006. WFIFOL(fd,j+4) = p->base_exp;
  1007. WFIFOL(fd,j+8) = p->zeny;
  1008. WFIFOL(fd,j+12) = p->job_exp;
  1009. WFIFOL(fd,j+16) = p->job_level;
  1010. WFIFOL(fd,j+20) = 0;
  1011. WFIFOL(fd,j+24) = 0;
  1012. WFIFOL(fd,j+28) = p->option;
  1013. WFIFOL(fd,j+32) = p->karma;
  1014. WFIFOL(fd,j+36) = p->manner;
  1015. WFIFOW(fd,j+40) = p->status_point;
  1016. WFIFOW(fd,j+42) = (p->hp > 0x7fff) ? 0x7fff : p->hp;
  1017. WFIFOW(fd,j+44) = (p->max_hp > 0x7fff) ? 0x7fff : p->max_hp;
  1018. WFIFOW(fd,j+46) = (p->sp > 0x7fff) ? 0x7fff : p->sp;
  1019. WFIFOW(fd,j+48) = (p->max_sp > 0x7fff) ? 0x7fff : p->max_sp;
  1020. WFIFOW(fd,j+50) = DEFAULT_WALK_SPEED; // p->speed;
  1021. WFIFOW(fd,j+52) = p->class;
  1022. WFIFOW(fd,j+54) = p->hair;
  1023. WFIFOW(fd,j+56) = p->weapon;
  1024. WFIFOW(fd,j+58) = p->base_level;
  1025. WFIFOW(fd,j+60) = p->skill_point;
  1026. WFIFOW(fd,j+62) = p->head_bottom;
  1027. WFIFOW(fd,j+64) = p->shield;
  1028. WFIFOW(fd,j+66) = p->head_top;
  1029. WFIFOW(fd,j+68) = p->head_mid;
  1030. WFIFOW(fd,j+70) = p->hair_color;
  1031. WFIFOW(fd,j+72) = p->clothes_color;
  1032. memcpy(WFIFOP(fd,j+74), p->name, 24);
  1033. WFIFOB(fd,j+98) = (p->str > 255) ? 255 : p->str;
  1034. WFIFOB(fd,j+99) = (p->agi > 255) ? 255 : p->agi;
  1035. WFIFOB(fd,j+100) = (p->vit > 255) ? 255 : p->vit;
  1036. WFIFOB(fd,j+101) = (p->int_ > 255) ? 255 : p->int_;
  1037. WFIFOB(fd,j+102) = (p->dex > 255) ? 255 : p->dex;
  1038. WFIFOB(fd,j+103) = (p->luk > 255) ? 255 : p->luk;
  1039. WFIFOB(fd,j+104) = p->char_num;
  1040. }
  1041. WFIFOSET(fd,WFIFOW(fd,2));
  1042. // printf("mmo_char_send006b end..\n");
  1043. return 0;
  1044. }
  1045. int parse_tologin(int fd) {
  1046. int i;
  1047. struct char_session_data *sd;
  1048. // only login-server can have an access to here.
  1049. // so, if it isn't the login-server, we disconnect the session.
  1050. //session eof check!
  1051. if(fd != login_fd)
  1052. session[fd]->eof = 1;
  1053. if(session[fd]->eof) {
  1054. if (fd == login_fd) {
  1055. printf("Char-server can't connect to login-server (connection #%d).\n", fd);
  1056. delete_timer(user_count_timer,send_users_tologin);
  1057. login_fd = -1;
  1058. }
  1059. close(fd);
  1060. delete_session(fd);
  1061. return 0;
  1062. }
  1063. sd = session[fd]->session_data;
  1064. // hehe. no need to set user limite on SQL version. :P
  1065. // but char limitation is good way to maintain server. :D
  1066. while(RFIFOREST(fd) >= 2) {
  1067. // printf("parse_tologin : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd, 0));
  1068. switch(RFIFOW(fd, 0)){
  1069. case 0x2711:
  1070. if (RFIFOREST(fd) < 3)
  1071. return 0;
  1072. if (RFIFOB(fd, 2)) {
  1073. //printf("connect login server error : %d\n", RFIFOB(fd, 2));
  1074. printf("Can not connect to login-server.\n");
  1075. printf("The server communication passwords (default s1/p1) is probably invalid.\n");
  1076. printf("Also, please make sure your login db has the username/password present and the sex of the account is S.\n");
  1077. printf("If you changed the communication passwords, change them back at map_athena.conf and char_athena.conf\n");
  1078. return 0;
  1079. //exit(1); //fixed for server shutdown.
  1080. }else {
  1081. printf("Connected to login-server (connection #%d).\n", fd);
  1082. // if no map-server already connected, display a message...
  1083. for(i = 0; i < MAX_MAP_SERVERS; i++)
  1084. if (server_fd[i] >= 0 && server[i].map[0][0]) // if map-server online and at least 1 map
  1085. break;
  1086. if (i == MAX_MAP_SERVERS)
  1087. printf("Awaiting maps from map-server.\n");
  1088. // send USER COUNT PING to login server.
  1089. printf("add interval tic (send_users_tologin)....\n");
  1090. user_count_timer = add_timer_interval(gettick() + 10, send_users_tologin, 0, 0, 5 * 1000);
  1091. }
  1092. RFIFOSKIP(fd, 3);
  1093. break;
  1094. case 0x2713:
  1095. if(RFIFOREST(fd)<51)
  1096. return 0;
  1097. for(i = 0; i < fd_max; i++) {
  1098. if (session[i] && (sd = session[i]->session_data) && sd->account_id == RFIFOL(fd,2)) {
  1099. if (RFIFOB(fd,6) != 0) {
  1100. WFIFOW(i,0) = 0x6c;
  1101. WFIFOB(i,2) = 0x42;
  1102. WFIFOSET(i,3);
  1103. } else if (max_connect_user == 0 || count_users() < max_connect_user) {
  1104. // if (max_connect_user == 0)
  1105. // printf("max_connect_user (unlimited) -> accepted.\n");
  1106. // else
  1107. // printf("count_users(): %d < max_connect_user (%d) -> accepted.\n", count_users(), max_connect_user);
  1108. sd->connect_until_time = (time_t)RFIFOL(fd,47);
  1109. // send characters to player
  1110. mmo_char_send006b(i, sd);
  1111. } else {
  1112. // refuse connection: too much online players
  1113. // printf("count_users(): %d < max_connect_use (%d) -> fail...\n", count_users(), max_connect_user);
  1114. WFIFOW(i,0) = 0x6c;
  1115. WFIFOW(i,2) = 0;
  1116. WFIFOSET(i,3);
  1117. }
  1118. }
  1119. }
  1120. RFIFOSKIP(fd,51);
  1121. break;
  1122. case 0x2717:
  1123. if (RFIFOREST(fd) < 50)
  1124. return 0;
  1125. for(i = 0; i < fd_max; i++) {
  1126. if (session[i] && (sd = session[i]->session_data)) {
  1127. if (sd->account_id == RFIFOL(fd,2)) {
  1128. sd->connect_until_time = (time_t)RFIFOL(fd,46);
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. RFIFOSKIP(fd,50);
  1134. break;
  1135. /* case 0x2721: // gm reply. I don't want to support this function.
  1136. printf("0x2721:GM reply\n");
  1137. {
  1138. int oldacc, newacc;
  1139. unsigned char buf[64];
  1140. if (RFIFOREST(fd) < 10)
  1141. return 0;
  1142. oldacc = RFIFOL(fd, 2);
  1143. newacc = RFIFOL(fd, 6);
  1144. RFIFOSKIP(fd, 10);
  1145. if (newacc > 0) {
  1146. for(i=0;i<char_num;i++){
  1147. if(char_dat[i].account_id==oldacc)
  1148. char_dat[i].account_id=newacc;
  1149. }
  1150. }
  1151. WBUFW(buf,0)=0x2b0b;
  1152. WBUFL(buf,2)=oldacc;
  1153. WBUFL(buf,6)=newacc;
  1154. mapif_sendall(buf,10);
  1155. // printf("char -> map\n");
  1156. }
  1157. break;
  1158. */
  1159. case 0x2723: // changesex reply (modified by [Yor])
  1160. if (RFIFOREST(fd) < 7)
  1161. return 0;
  1162. {
  1163. int acc, sex;
  1164. unsigned char buf[16];
  1165. acc = RFIFOL(fd,2);
  1166. sex = RFIFOB(fd,6);
  1167. RFIFOSKIP(fd, 7);
  1168. if (acc > 0) {
  1169. sprintf(tmp_sql, "SELECT `char_id`,`class`,`skill_point` FROM `%s` WHERE `account_id` = '%d'",char_db, acc);
  1170. if (mysql_query(&mysql_handle, tmp_sql)) {
  1171. printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle));
  1172. }
  1173. sql_res = mysql_store_result(&mysql_handle);
  1174. if (sql_res) {
  1175. int char_id, jobclass, skill_point, class;
  1176. sql_row = mysql_fetch_row(sql_res);
  1177. char_id = atoi(sql_row[0]);
  1178. jobclass = atoi(sql_row[1]);
  1179. skill_point = atoi(sql_row[2]);
  1180. class = jobclass;
  1181. if (jobclass == 19 || jobclass == 20 ||
  1182. jobclass == 4020 || jobclass == 4021 ||
  1183. jobclass == 4042 || jobclass == 4043) {
  1184. // job modification
  1185. if (jobclass == 19 || jobclass == 20) {
  1186. class = (sex) ? 19 : 20;
  1187. } else if (jobclass == 4020 || jobclass == 4021) {
  1188. class = (sex) ? 4020 : 4021;
  1189. } else if (jobclass == 4042 || jobclass == 4043) {
  1190. class = (sex) ? 4042 : 4043;
  1191. }
  1192. // remove specifical skills of classes 19,20 4020,4021 and 4042,4043
  1193. sprintf(tmp_sql, "SELECT `lv` FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id);
  1194. if (mysql_query(&mysql_handle, tmp_sql)) {
  1195. printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle));
  1196. }
  1197. sql_res = mysql_store_result(&mysql_handle);
  1198. if (sql_res) {
  1199. while(( sql_row = mysql_fetch_row(sql_res))) {
  1200. skill_point += atoi(sql_row[0]);
  1201. }
  1202. }
  1203. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id);
  1204. if (mysql_query(&mysql_handle, tmp_sql)) {
  1205. printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle));
  1206. }
  1207. }
  1208. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  1209. sprintf(tmp_sql, "UPDATE `%s` SET `equip` = '0' WHERE `char_id` = '%d'",inventory_db, char_id);
  1210. if (mysql_query(&mysql_handle, tmp_sql)) {
  1211. printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle));
  1212. }
  1213. sprintf(tmp_sql, "UPDATE `%s` SET `class`='%d' , `skill_point`='%d' , `weapon`='0' , `shield='0' , `head_top`='0' , `head_mid`='0' , `head_bottom`='0' WHERE `char_id` = '%d'",char_db, class, skill_point, char_id);
  1214. if (mysql_query(&mysql_handle, tmp_sql)) {
  1215. printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle));
  1216. }
  1217. }
  1218. }
  1219. // disconnect player if online on char-server
  1220. for(i = 0; i < fd_max; i++) {
  1221. if (session[i] && (sd = session[i]->session_data)) {
  1222. if (sd->account_id == acc) {
  1223. session[i]->eof = 1;
  1224. break;
  1225. }
  1226. }
  1227. }
  1228. WBUFW(buf,0) = 0x2b0d;
  1229. WBUFL(buf,2) = acc;
  1230. WBUFB(buf,6) = sex;
  1231. mapif_sendall(buf, 7);
  1232. }
  1233. break;
  1234. // account_reg2変更通知
  1235. case 0x2729:
  1236. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1237. return 0;
  1238. {
  1239. struct global_reg reg[ACCOUNT_REG2_NUM];
  1240. unsigned char buf[4096];
  1241. int j, p, acc;
  1242. acc = RFIFOL(fd,4);
  1243. for(p = 8, j = 0; p < RFIFOW(fd,2) && j < ACCOUNT_REG2_NUM; p += 36, j++) {
  1244. memcpy(reg[j].str, RFIFOP(fd,p), 32);
  1245. reg[j].value = RFIFOL(fd,p+32);
  1246. }
  1247. // set_account_reg2(acc,j,reg);
  1248. // 同垢ログインを禁止していれば送る必要は無い
  1249. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  1250. WBUFW(buf,0) = 0x2b11;
  1251. mapif_sendall(buf, WBUFW(buf,2));
  1252. RFIFOSKIP(fd, RFIFOW(fd,2));
  1253. // printf("char: save_account_reg_reply\n");
  1254. }
  1255. break;
  1256. // State change of account/ban notification (from login-server) by [Yor]
  1257. case 0x2731:
  1258. if (RFIFOREST(fd) < 11)
  1259. return 0;
  1260. // send to all map-servers to disconnect the player
  1261. {
  1262. unsigned char buf[16];
  1263. WBUFW(buf,0) = 0x2b14;
  1264. WBUFL(buf,2) = RFIFOL(fd,2);
  1265. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  1266. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  1267. mapif_sendall(buf, 11);
  1268. }
  1269. // disconnect player if online on char-server
  1270. for(i = 0; i < fd_max; i++) {
  1271. if (session[i] && (sd = session[i]->session_data)) {
  1272. if (sd->account_id == RFIFOL(fd,2)) {
  1273. session[i]->eof = 1;
  1274. break;
  1275. }
  1276. }
  1277. }
  1278. RFIFOSKIP(fd,11);
  1279. break;
  1280. default:
  1281. printf("set eof.\n");
  1282. session[fd]->eof = 1;
  1283. return 0;
  1284. }
  1285. }
  1286. RFIFOFLUSH(fd);
  1287. return 0;
  1288. }
  1289. //--------------------------------
  1290. // Map-server anti-freeze system
  1291. //--------------------------------
  1292. int map_anti_freeze_system(int tid, unsigned int tick, int id, int data) {
  1293. int i;
  1294. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  1295. if (server_fd[i] >= 0) {// if map-server is online
  1296. printf("map_anti_freeze_system: server #%d, flag: %d.\n", i, server_freezeflag[i]);
  1297. if (server_freezeflag[i]-- < 1) {// Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed
  1298. printf("Map-server anti-freeze system: char-server #%d is frozen -> disconnection.\n", i);
  1299. session[server_fd[i]]->eof = 1;
  1300. sprintf(tmp_sql, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server_fd[i]);
  1301. if (mysql_query(&mysql_handle, tmp_sql)) {
  1302. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1303. }
  1304. }
  1305. }
  1306. }
  1307. return 0;
  1308. }
  1309. int parse_frommap(int fd) {
  1310. int i = 0, j = 0;
  1311. int id;
  1312. // Sometimes fd=0, and it will cause server crash. Don't know why. :(
  1313. if (fd <= 0) {
  1314. printf("parse_frommap error fd=0\n");
  1315. return 0;
  1316. }
  1317. for(id = 0; id < MAX_MAP_SERVERS; id++)
  1318. if (server_fd[id] == fd)
  1319. break;
  1320. if(id == MAX_MAP_SERVERS)
  1321. session[fd]->eof = 1;
  1322. if(session[fd]->eof) {
  1323. if (id < MAX_MAP_SERVERS) {
  1324. memset(&server[id], 0, sizeof(struct mmo_map_server));
  1325. printf("Map-server %d (session #%d) has disconnected.\n", id, fd);
  1326. sprintf(tmp_sql, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server_fd[id]);
  1327. if (mysql_query(&mysql_handle, tmp_sql)) {
  1328. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1329. }
  1330. server_fd[id] = -1;
  1331. }
  1332. close(fd);
  1333. delete_session(fd);
  1334. return 0;
  1335. }
  1336. while(RFIFOREST(fd) >= 2) {
  1337. // printf("parse_frommap : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd,0));
  1338. switch(RFIFOW(fd, 0)) {
  1339. case 0x2af7:
  1340. RFIFOSKIP(fd,2);
  1341. read_gm_account();
  1342. break;
  1343. // mapserver -> map names recv.
  1344. case 0x2afa:
  1345. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1346. return 0;
  1347. memset(server[id].map, 0, sizeof(server[id].map));
  1348. j = 0;
  1349. for(i = 4; i < RFIFOW(fd,2); i += 16) {
  1350. memcpy(server[id].map[j], RFIFOP(fd,i), 16);
  1351. // printf("set map %d.%d : %s\n", id, j, server[id].map[j]);
  1352. j++;
  1353. }
  1354. i = server[id].ip;
  1355. {
  1356. unsigned char *p = (unsigned char *)&server[id].ip;
  1357. printf("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  1358. id, j, p[0], p[1], p[2], p[3], server[id].port);
  1359. printf("Map-server %d loading complete.\n", id);
  1360. }
  1361. WFIFOW(fd,0) = 0x2afb;
  1362. WFIFOB(fd,2) = 0;
  1363. memcpy(WFIFOP(fd,3), wisp_server_name, 24); // name for wisp to player
  1364. WFIFOSET(fd,27);
  1365. {
  1366. unsigned char buf[16384];
  1367. int x;
  1368. if (j == 0) {
  1369. printf("WARNING: Map-Server %d have NO maps.\n", id);
  1370. // Transmitting maps information to the other map-servers
  1371. } else {
  1372. WBUFW(buf,0) = 0x2b04;
  1373. WBUFW(buf,2) = j * 16 + 10;
  1374. WBUFL(buf,4) = server[id].ip;
  1375. WBUFW(buf,8) = server[id].port;
  1376. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 16);
  1377. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  1378. }
  1379. // Transmitting the maps of the other map-servers to the new map-server
  1380. for(x = 0; x < MAX_MAP_SERVERS; x++) {
  1381. if (server_fd[x] >= 0 && x != id) {
  1382. WFIFOW(fd,0) = 0x2b04;
  1383. WFIFOL(fd,4) = server[x].ip;
  1384. WFIFOW(fd,8) = server[x].port;
  1385. j = 0;
  1386. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  1387. if (server[x].map[i][0])
  1388. memcpy(WFIFOP(fd,10+(j++)*16), server[x].map[i], 16);
  1389. if (j > 0) {
  1390. WFIFOW(fd,2) = j * 16 + 10;
  1391. WFIFOSET(fd,WFIFOW(fd,2));
  1392. }
  1393. }
  1394. }
  1395. }
  1396. RFIFOSKIP(fd,RFIFOW(fd,2));
  1397. break;
  1398. // auth request
  1399. case 0x2afc:
  1400. if (RFIFOREST(fd) < 22)
  1401. return 0;
  1402. // printf("(AUTH request) auth_fifo search %d %d %d\n", RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10));
  1403. for(i = 0; i < AUTH_FIFO_SIZE; i++) {
  1404. if (auth_fifo[i].account_id == RFIFOL(fd,2) &&
  1405. auth_fifo[i].char_id == RFIFOL(fd,6) &&
  1406. auth_fifo[i].login_id1 == RFIFOL(fd,10) &&
  1407. #if CMP_AUTHFIFO_LOGIN2 != 0
  1408. // here, it's the only area where it's possible that we doesn't know login_id2 (map-server asks just after 0x72 packet, that doesn't given the value)
  1409. (auth_fifo[i].login_id2 == RFIFOL(fd,14) || RFIFOL(fd,14) == 0) && // relate to the versions higher than 18
  1410. #endif
  1411. (!check_ip_flag || auth_fifo[i].ip == RFIFOL(fd,18)) &&
  1412. !auth_fifo[i].delflag) {
  1413. auth_fifo[i].delflag = 1;
  1414. WFIFOW(fd,0) = 0x2afd;
  1415. WFIFOW(fd,2) = 16 + sizeof(struct mmo_charstatus);
  1416. WFIFOL(fd,4) = RFIFOL(fd,2);
  1417. WFIFOL(fd,8) = auth_fifo[i].login_id2;
  1418. WFIFOL(fd,12) = (unsigned long)auth_fifo[i].connect_until_time;
  1419. mmo_char_fromsql(auth_fifo[i].char_id, char_dat, 1);
  1420. char_dat[0].sex = auth_fifo[i].sex;
  1421. memcpy(WFIFOP(fd,16), &char_dat[0], sizeof(struct mmo_charstatus));
  1422. WFIFOSET(fd, WFIFOW(fd,2));
  1423. //printf("auth_fifo search success (auth #%d, account %d, character: %d).\n", i, RFIFOL(fd,2), RFIFOL(fd,6));
  1424. break;
  1425. }
  1426. }
  1427. if (i == AUTH_FIFO_SIZE) {
  1428. WFIFOW(fd,0) = 0x2afe;
  1429. WFIFOL(fd,2) = RFIFOL(fd,2);
  1430. WFIFOSET(fd,6);
  1431. // printf("(AUTH request) auth_fifo search error!\n");
  1432. }
  1433. RFIFOSKIP(fd,22);
  1434. break;
  1435. // set MAP user
  1436. case 0x2aff:
  1437. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  1438. return 0;
  1439. if (RFIFOW(fd,4) != server[id].users)
  1440. printf("map user: %d\n", RFIFOW(fd,4));
  1441. server[id].users = RFIFOW(fd,4);
  1442. if(anti_freeze_enable)
  1443. server_freezeflag[id] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed
  1444. RFIFOSKIP(fd,RFIFOW(fd,2));
  1445. break;
  1446. // char saving
  1447. case 0x2b01:
  1448. i = 0;
  1449. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1450. return 0;
  1451. //check account
  1452. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",char_db, RFIFOL(fd,4),RFIFOL(fd,8));
  1453. if (mysql_query(&mysql_handle, tmp_sql)) {
  1454. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1455. }
  1456. sql_res = mysql_store_result(&mysql_handle);
  1457. if (sql_res) {
  1458. sql_row = mysql_fetch_row(sql_res);
  1459. if (sql_row)
  1460. i = atoi(sql_row[0]);
  1461. }
  1462. mysql_free_result(sql_res);
  1463. if (i == 1) {
  1464. memcpy(&char_dat[0], RFIFOP(fd,12), sizeof(struct mmo_charstatus));
  1465. mmo_char_tosql(RFIFOL(fd,8), char_dat);
  1466. //save to DB
  1467. }
  1468. RFIFOSKIP(fd,RFIFOW(fd,2));
  1469. break;
  1470. // req char selection
  1471. case 0x2b02:
  1472. if (RFIFOREST(fd) < 18)
  1473. return 0;
  1474. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  1475. auth_fifo_pos = 0;
  1476. // printf("(charselect) auth_fifo set %d - account_id:%08x login_id1:%08x\n", auth_fifo_pos, RFIFOL(fd, 2), RFIFOL(fd, 6));
  1477. auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd, 2);
  1478. auth_fifo[auth_fifo_pos].char_id = 0;
  1479. auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd, 6);
  1480. auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10);
  1481. auth_fifo[auth_fifo_pos].delflag = 2;
  1482. auth_fifo[auth_fifo_pos].char_pos = 0;
  1483. auth_fifo[auth_fifo_pos].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server)
  1484. auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,14);
  1485. auth_fifo_pos++;
  1486. WFIFOW(fd, 0) = 0x2b03;
  1487. WFIFOL(fd, 2) = RFIFOL(fd, 2);
  1488. WFIFOB(fd, 6) = 0;
  1489. WFIFOSET(fd, 7);
  1490. RFIFOSKIP(fd, 18);
  1491. break;
  1492. // request "change map server"
  1493. case 0x2b05:
  1494. if (RFIFOREST(fd) < 49)
  1495. return 0;
  1496. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  1497. auth_fifo_pos = 0;
  1498. WFIFOW(fd, 0) = 0x2b06;
  1499. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 42);
  1500. // printf("(map change) auth_fifo set %d - account_id:%08x login_id1:%08x\n", auth_fifo_pos, RFIFOL(fd, 2), RFIFOL(fd, 6));
  1501. auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd, 2);
  1502. auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd, 6);
  1503. auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10);
  1504. auth_fifo[auth_fifo_pos].char_id = RFIFOL(fd,14);
  1505. auth_fifo[auth_fifo_pos].delflag = 0;
  1506. auth_fifo[auth_fifo_pos].sex = RFIFOB(fd,44);
  1507. auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,45);
  1508. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", char_db, RFIFOL(fd,2), RFIFOL(fd,14));
  1509. if (mysql_query(&mysql_handle, tmp_sql)) {
  1510. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1511. }
  1512. sql_res = mysql_store_result(&mysql_handle);
  1513. if (( sql_row = mysql_fetch_row(sql_res))) {
  1514. i = atoi(sql_row[0]);
  1515. mysql_free_result(sql_res);
  1516. auth_fifo[auth_fifo_pos].char_pos = auth_fifo[auth_fifo_pos].char_id;
  1517. auth_fifo_pos++;
  1518. WFIFOL(fd,6) = 0;
  1519. break;
  1520. }
  1521. if (i == 0)
  1522. WFIFOW(fd,6) = 1;
  1523. WFIFOSET(fd,44);
  1524. RFIFOSKIP(fd,49);
  1525. break;
  1526. // char name check
  1527. case 0x2b08:
  1528. if (RFIFOREST(fd) < 6)
  1529. return 0;
  1530. sprintf(tmp_sql, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, RFIFOL(fd,2));
  1531. if (mysql_query(&mysql_handle, tmp_sql)) {
  1532. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1533. }
  1534. sql_res = mysql_store_result(&mysql_handle);
  1535. sql_row = mysql_fetch_row(sql_res);
  1536. WFIFOW(fd,0) = 0x2b09;
  1537. WFIFOL(fd,2) = RFIFOL(fd,2);
  1538. if (sql_row)
  1539. memcpy(WFIFOP(fd,6), sql_row[0], 24);
  1540. else
  1541. memcpy(WFIFOP(fd,6), unknown_char_name, 24);
  1542. mysql_free_result(sql_res);
  1543. WFIFOSET(fd,30);
  1544. RFIFOSKIP(fd,6);
  1545. break;
  1546. /* // I want become GM - fuck!
  1547. case 0x2b0a:
  1548. if(RFIFOREST(fd)<4)
  1549. return 0;
  1550. if(RFIFOREST(fd)<RFIFOW(fd,2))
  1551. return 0;
  1552. memcpy(WFIFOP(login_fd,2),RFIFOP(fd,2),RFIFOW(fd,2)-2);
  1553. WFIFOW(login_fd,0)=0x2720;
  1554. WFIFOSET(login_fd,RFIFOW(fd,2));
  1555. // printf("char : change gm -> login %d %s %d\n", RFIFOL(fd, 4), RFIFOP(fd, 8), RFIFOW(fd, 2));
  1556. RFIFOSKIP(fd, RFIFOW(fd, 2));
  1557. break;
  1558. */
  1559. // account_reg保存要求
  1560. case 0x2b10:
  1561. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1562. return 0;
  1563. {
  1564. struct global_reg reg[ACCOUNT_REG2_NUM];
  1565. int j,p,acc;
  1566. acc=RFIFOL(fd,4);
  1567. for(p=8,j=0;p<RFIFOW(fd,2) && j<ACCOUNT_REG2_NUM;p+=36,j++){
  1568. memcpy(reg[j].str,RFIFOP(fd,p),32);
  1569. reg[j].value=RFIFOL(fd,p+32);
  1570. }
  1571. // set_account_reg2(acc,j,reg);
  1572. // loginサーバーへ送る
  1573. if (login_fd > 0) { // don't send request if no login-server
  1574. WFIFOW(login_fd, 0) = 0x2728;
  1575. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), RFIFOW(fd,2));
  1576. WFIFOSET(login_fd, WFIFOW(login_fd,2));
  1577. }
  1578. // ワールドへの同垢ログインがなければmapサーバーに送る必要はない
  1579. //memcpy(buf,RFIFOP(fd,0),RFIFOW(fd,2));
  1580. //WBUFW(buf,0)=0x2b11;
  1581. //mapif_sendall(buf,WBUFW(buf,2));
  1582. RFIFOSKIP(fd,RFIFOW(fd,2));
  1583. // printf("char: save_account_reg (from map)\n");
  1584. }
  1585. break;
  1586. // Map server send information to change an email of an account -> login-server
  1587. case 0x2b0c:
  1588. if (RFIFOREST(fd) < 86)
  1589. return 0;
  1590. if (login_fd > 0) { // don't send request if no login-server
  1591. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), 86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  1592. WFIFOW(login_fd,0) = 0x2722;
  1593. WFIFOSET(login_fd, 86);
  1594. }
  1595. RFIFOSKIP(fd, 86);
  1596. break;
  1597. // Receiving from map-server a status change resquest. Transmission to login-server (by Yor)
  1598. case 0x2b0e:
  1599. if (RFIFOREST(fd) < 44)
  1600. return 0;
  1601. {
  1602. char character_name[24];
  1603. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if nobody)
  1604. memcpy(character_name, RFIFOP(fd,6), 24);
  1605. character_name[sizeof(character_name) -1] = '\0';
  1606. // prepare answer
  1607. WFIFOW(fd,0) = 0x2b0f; // answer
  1608. WFIFOL(fd,2) = acc; // who want do operation
  1609. WFIFOW(fd,30) = RFIFOW(fd, 30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  1610. sprintf(tmp_sql, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'",char_db, character_name);
  1611. if (mysql_query(&mysql_handle, tmp_sql)) {
  1612. printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle));
  1613. }
  1614. sql_res = mysql_store_result(&mysql_handle);
  1615. if (sql_res) {
  1616. if (mysql_num_rows(sql_res)) {
  1617. sql_row = mysql_fetch_row(sql_res);
  1618. memcpy(WFIFOP(fd,6), sql_row[1], 24); // put correct name if found
  1619. WFIFOW(fd,32) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1620. switch(RFIFOW(fd, 30)) {
  1621. case 1: // block
  1622. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  1623. if (login_fd > 0) { // don't send request if no login-server
  1624. WFIFOW(login_fd,0) = 0x2724;
  1625. WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value
  1626. WFIFOL(login_fd,6) = 5; // status of the account
  1627. WFIFOSET(login_fd, 10);
  1628. // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 5);
  1629. } else
  1630. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1631. } else
  1632. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1633. break;
  1634. case 2: // ban
  1635. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  1636. if (login_fd > 0) { // don't send request if no login-server
  1637. WFIFOW(login_fd, 0) = 0x2725;
  1638. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  1639. WFIFOW(login_fd, 6) = RFIFOW(fd,32); // year
  1640. WFIFOW(login_fd, 8) = RFIFOW(fd,34); // month
  1641. WFIFOW(login_fd,10) = RFIFOW(fd,36); // day
  1642. WFIFOW(login_fd,12) = RFIFOW(fd,38); // hour
  1643. WFIFOW(login_fd,14) = RFIFOW(fd,40); // minute
  1644. WFIFOW(login_fd,16) = RFIFOW(fd,42); // second
  1645. WFIFOSET(login_fd,18);
  1646. // printf("char : status -> login: account %d, ban: %dy %dm %dd %dh %dmn %ds\n",
  1647. // char_dat[i].account_id, (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), (short)RFIFOW(fd,38), (short)RFIFOW(fd,40), (short)RFIFOW(fd,42));
  1648. } else
  1649. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1650. } else
  1651. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1652. break;
  1653. case 3: // unblock
  1654. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  1655. if (login_fd > 0) { // don't send request if no login-server
  1656. WFIFOW(login_fd,0) = 0x2724;
  1657. WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value
  1658. WFIFOL(login_fd,6) = 0; // status of the account
  1659. WFIFOSET(login_fd, 10);
  1660. // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 0);
  1661. } else
  1662. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1663. } else
  1664. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1665. break;
  1666. case 4: // unban
  1667. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  1668. if (login_fd > 0) { // don't send request if no login-server
  1669. WFIFOW(login_fd, 0) = 0x272a;
  1670. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  1671. WFIFOSET(login_fd, 6);
  1672. // printf("char : status -> login: account %d, unban request\n", char_dat[i].account_id);
  1673. } else
  1674. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1675. } else
  1676. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1677. break;
  1678. case 5: // changesex
  1679. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  1680. if (login_fd > 0) { // don't send request if no login-server
  1681. WFIFOW(login_fd, 0) = 0x2727;
  1682. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  1683. WFIFOSET(login_fd, 6);
  1684. // printf("char : status -> login: account %d, change sex request\n", char_dat[i].account_id);
  1685. } else
  1686. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1687. } else
  1688. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1689. break;
  1690. }
  1691. } else {
  1692. // character name not found
  1693. memcpy(WFIFOP(fd,6), character_name, 24);
  1694. WFIFOW(fd,32) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  1695. }
  1696. // send answer if a player ask, not if the server ask
  1697. if (acc != -1) {
  1698. WFIFOSET(fd, 34);
  1699. }
  1700. }
  1701. }
  1702. RFIFOSKIP(fd, 44);
  1703. break;
  1704. // Recieve rates [Wizputer]
  1705. case 0x2b16:
  1706. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,8))
  1707. return 0;
  1708. sprintf(tmp_sql, "INSERT INTO `ragsrvinfo` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d',`motd`='%s'",
  1709. fd, server_name, RFIFOW(fd,2), RFIFOW(fd,4), RFIFOW(fd,6), RFIFOP(fd,10));
  1710. if (mysql_query(&mysql_handle, tmp_sql)) {
  1711. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1712. }
  1713. RFIFOSKIP(fd,RFIFOW(fd,8));
  1714. break;
  1715. // Character disconnected set online 0 [Wizputer]
  1716. case 0x2b17:
  1717. if (RFIFOREST(fd) < 6 )
  1718. return 0;
  1719. //printf("Setting %d char offline\n",RFIFOL(fd,2));
  1720. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `char_id`='%d'", char_db, RFIFOL(fd,2));
  1721. if (mysql_query(&mysql_handle, tmp_sql))
  1722. printf("DB server Error (update online `%s`)- %s\n", char_db, mysql_error(&mysql_handle));
  1723. RFIFOSKIP(fd,6);
  1724. break;
  1725. default:
  1726. // inter server - packet
  1727. {
  1728. int r = inter_parse_frommap(fd);
  1729. if (r == 1) break; // processed
  1730. if (r == 2) return 0; // need more packet
  1731. }
  1732. // no inter server packet. no char server packet -> disconnect
  1733. printf("parse_frommap: unknown packet %x! \n", RFIFOW(fd,0));
  1734. session[fd]->eof = 1;
  1735. return 0;
  1736. }
  1737. }
  1738. return 0;
  1739. }
  1740. int search_mapserver(char *map) {
  1741. int i, j;
  1742. char temp_map[16];
  1743. int temp_map_len;
  1744. // printf("Searching the map-server for map '%s'... ", map);
  1745. strncpy(temp_map, map, sizeof(temp_map));
  1746. temp_map[sizeof(temp_map)-1] = '\0';
  1747. if (strchr(temp_map, '.') != NULL)
  1748. temp_map[strchr(temp_map, '.') - temp_map + 1] = '\0'; // suppress the '.gat', but conserve the '.' to be sure of the name of the map
  1749. temp_map_len = strlen(temp_map);
  1750. for(i = 0; i < MAX_MAP_SERVERS; i++)
  1751. if (server_fd[i] >= 0)
  1752. for (j = 0; server[i].map[j][0]; j++)
  1753. //printf("%s : %s = %d\n", server[i].map[j], map, strncmp(server[i].map[j], temp_map, temp_map_len));
  1754. if (strncmp(server[i].map[j], temp_map, temp_map_len) == 0) {
  1755. // printf("found -> server #%d.\n", i);
  1756. return i;
  1757. }
  1758. // printf("not found.\n");
  1759. return -1;
  1760. }
  1761. int char_mapif_init(int fd) {
  1762. return inter_mapif_init(fd);
  1763. }
  1764. //-----------------------------------------------------
  1765. // Test to know if an IP come from LAN or WAN. by [Yor]
  1766. //-----------------------------------------------------
  1767. int lan_ip_check(unsigned char *p){
  1768. int i;
  1769. int lancheck = 1;
  1770. int subneti[4];
  1771. unsigned int k0, k1, k2, k3;
  1772. sscanf(lan_map_ip, "%d.%d.%d.%d", &k0, &k1, &k2, &k3);
  1773. subneti[0] = k0; subneti[1] = k1; subneti[2] = k2; subneti[3] = k3;
  1774. // printf("lan_ip_check: to compare: %d.%d.%d.%d, network: %d.%d.%d.%d/%d.%d.%d.%d\n",
  1775. // p[0], p[1], p[2], p[3],
  1776. // subneti[0], subneti[1], subneti[2], subneti[3],
  1777. // subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]);
  1778. for(i = 0; i < 4; i++) {
  1779. if ((subneti[i] & subnetmaski[i]) != (p[i] & subnetmaski[i])) {
  1780. lancheck = 0;
  1781. break;
  1782. }
  1783. }
  1784. // printf("LAN test (result): %s source\033[0m.\n", (lancheck) ? "\033[1;36mLAN" : "\033[1;32mWAN");
  1785. return lancheck;
  1786. }
  1787. int parse_char(int fd) {
  1788. int i, ch = 0;
  1789. char email[40];
  1790. unsigned short cmd;
  1791. struct char_session_data *sd;
  1792. unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr;
  1793. if(login_fd < 0)
  1794. session[fd]->eof = 1;
  1795. if(session[fd]->eof) {
  1796. if (fd == login_fd)
  1797. login_fd = -1;
  1798. close(fd);
  1799. delete_session(fd);
  1800. return 0;
  1801. }
  1802. sd = session[fd]->session_data;
  1803. while(RFIFOREST(fd) >= 2) {
  1804. cmd = RFIFOW(fd,0);
  1805. // crc32のスキップ用
  1806. if( sd==NULL && // 未ログインor管理パケット
  1807. RFIFOREST(fd)>=4 && // 最低バイト数制限 & 0x7530,0x7532管理パケ除去
  1808. RFIFOREST(fd)<=21 && // 最大バイト数制限 & サーバーログイン除去
  1809. cmd!=0x20b && // md5通知パケット除去
  1810. (RFIFOREST(fd)<6 || RFIFOW(fd,4)==0x65) ){ // 次に何かパケットが来てるなら、接続でないとだめ
  1811. RFIFOSKIP(fd,4);
  1812. cmd = RFIFOW(fd,0);
  1813. printf("parse_char : %d crc32 skipped\n",fd);
  1814. if(RFIFOREST(fd)==0)
  1815. return 0;
  1816. }
  1817. // if(cmd<30000 && cmd!=0x187)
  1818. // printf("parse_char : %d %d %d\n",fd,RFIFOREST(fd),cmd);
  1819. // 不正パケットの処理
  1820. // if (sd == NULL && cmd != 0x65 && cmd != 0x20b && cmd != 0x187 &&
  1821. // cmd != 0x2af8 && cmd != 0x7530 && cmd != 0x7532)
  1822. // cmd = 0xffff; // パケットダンプを表示させる
  1823. switch(cmd){
  1824. case 0x20b: //20040622 encryption ragexe correspondence
  1825. if (RFIFOREST(fd) < 19)
  1826. return 0;
  1827. RFIFOSKIP(fd,19);
  1828. break;
  1829. case 0x65: // request to connect
  1830. printf("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10));
  1831. if (RFIFOREST(fd) < 17)
  1832. return 0;
  1833. {
  1834. /*removed from isGM setup
  1835. if (isGM(RFIFOL(fd,2)))
  1836. printf("Account Logged On; Account ID: %d (GM level %d).\n", RFIFOL(fd,2), isGM(RFIFOL(fd,2)));
  1837. else
  1838. printf("Account Logged On; Account ID: %d.\n", RFIFOL(fd,2));
  1839. */
  1840. if (sd == NULL) {
  1841. CREATE(session[fd]->session_data, struct char_session_data, 1);
  1842. sd = session[fd]->session_data;
  1843. sd->connect_until_time = 0; // unknow or illimited (not displaying on map-server)
  1844. }
  1845. sd->account_id = RFIFOL(fd, 2);
  1846. sd->login_id1 = RFIFOL(fd, 6);
  1847. sd->login_id2 = RFIFOL(fd, 10);
  1848. sd->sex = RFIFOB(fd, 16);
  1849. WFIFOL(fd, 0) = RFIFOL(fd, 2);
  1850. WFIFOSET(fd, 4);
  1851. for(i = 0; i < AUTH_FIFO_SIZE; i++) {
  1852. if (auth_fifo[i].account_id == sd->account_id &&
  1853. auth_fifo[i].login_id1 == sd->login_id1 &&
  1854. #if CMP_AUTHFIFO_LOGIN2 != 0
  1855. auth_fifo[i].login_id2 == sd->login_id2 && // relate to the versions higher than 18
  1856. #endif
  1857. (!check_ip_flag || auth_fifo[i].ip == session[fd]->client_addr.sin_addr.s_addr) &&
  1858. auth_fifo[i].delflag == 2) {
  1859. auth_fifo[i].delflag = 1;
  1860. if (max_connect_user == 0 || count_users() < max_connect_user) {
  1861. if (login_fd > 0) { // don't send request if no login-server
  1862. // request to login-server to obtain e-mail/time limit
  1863. WFIFOW(login_fd,0) = 0x2716;
  1864. WFIFOL(login_fd,2) = sd->account_id;
  1865. WFIFOSET(login_fd,6);
  1866. }
  1867. // send characters to player
  1868. mmo_char_send006b(fd, sd);
  1869. } else {
  1870. // refuse connection (over populated)
  1871. WFIFOW(fd,0) = 0x6c;
  1872. WFIFOW(fd,2) = 0;
  1873. WFIFOSET(fd,3);
  1874. }
  1875. // printf("connection request> set delflag 1(o:2)- account_id:%d/login_id1:%d(fifo_id:%d)\n", sd->account_id, sd->login_id1, i);
  1876. break;
  1877. }
  1878. }
  1879. if (i == AUTH_FIFO_SIZE) {
  1880. if (login_fd > 0) { // don't send request if no login-server
  1881. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  1882. WFIFOL(login_fd,2) = sd->account_id;
  1883. WFIFOL(login_fd,6) = sd->login_id1;
  1884. WFIFOL(login_fd,10) = sd->login_id2;
  1885. WFIFOB(login_fd,14) = sd->sex;
  1886. WFIFOL(login_fd,15) = session[fd]->client_addr.sin_addr.s_addr;
  1887. WFIFOSET(login_fd,19);
  1888. } else { // if no login-server, we must refuse connection
  1889. WFIFOW(fd,0) = 0x6c;
  1890. WFIFOW(fd,2) = 0;
  1891. WFIFOSET(fd,3);
  1892. }
  1893. }
  1894. }
  1895. RFIFOSKIP(fd, 17);
  1896. break;
  1897. case 0x66: // char select
  1898. // printf("0x66> request connect - account_id:%d/char_num:%d\n",sd->account_id,RFIFOB(fd, 2));
  1899. if (RFIFOREST(fd) < 3)
  1900. return 0;
  1901. sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'",char_db, sd->account_id, RFIFOB(fd, 2));
  1902. if (mysql_query(&mysql_handle, tmp_sql)) {
  1903. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1904. }
  1905. sql_res = mysql_store_result(&mysql_handle);
  1906. sql_row = mysql_fetch_row(sql_res);
  1907. if (sql_row)
  1908. mmo_char_fromsql(atoi(sql_row[0]), char_dat, 0);
  1909. else {
  1910. mysql_free_result(sql_res);
  1911. RFIFOSKIP(fd, 3);
  1912. break;
  1913. }
  1914. sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')",
  1915. charlog_db, sd->account_id, RFIFOB(fd, 2), char_dat[0].name);
  1916. //query
  1917. if(mysql_query(&mysql_handle, tmp_sql)) {
  1918. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  1919. }
  1920. printf("(\033[1;64m%d\033[0m) char selected (\033[1;32m%d\033[0m) \033[1;32m%s\033[0m" RETCODE, sd->account_id, RFIFOB(fd, 2), char_dat[0].name);
  1921. i = search_mapserver(char_dat[0].last_point.map);
  1922. // if map is not found, we check major cities
  1923. if (i < 0) {
  1924. if ((i = search_mapserver("prontera.gat")) >= 0) { // check is done without 'gat'.
  1925. memcpy(char_dat[0].last_point.map, "prontera.gat", 16);
  1926. char_dat[0].last_point.x = 273; // savepoint coordonates
  1927. char_dat[0].last_point.y = 354;
  1928. } else if ((i = search_mapserver("geffen.gat")) >= 0) { // check is done without 'gat'.
  1929. memcpy(char_dat[0].last_point.map, "geffen.gat", 16);
  1930. char_dat[0].last_point.x = 120; // savepoint coordonates
  1931. char_dat[0].last_point.y = 100;
  1932. } else if ((i = search_mapserver("morocc.gat")) >= 0) { // check is done without 'gat'.
  1933. memcpy(char_dat[0].last_point.map, "morocc.gat", 16);
  1934. char_dat[0].last_point.x = 160; // savepoint coordonates
  1935. char_dat[0].last_point.y = 94;
  1936. } else if ((i = search_mapserver("alberta.gat")) >= 0) { // check is done without 'gat'.
  1937. memcpy(char_dat[0].last_point.map, "alberta.gat", 16);
  1938. char_dat[0].last_point.x = 116; // savepoint coordonates
  1939. char_dat[0].last_point.y = 57;
  1940. } else if ((i = search_mapserver("payon.gat")) >= 0) { // check is done without 'gat'.
  1941. memcpy(char_dat[0].last_point.map, "payon.gat", 16);
  1942. char_dat[0].last_point.x = 87; // savepoint coordonates
  1943. char_dat[0].last_point.y = 117;
  1944. } else if ((i = search_mapserver("izlude.gat")) >= 0) { // check is done without 'gat'.
  1945. memcpy(char_dat[0].last_point.map, "izlude.gat", 16);
  1946. char_dat[0].last_point.x = 94; // savepoint coordonates
  1947. char_dat[0].last_point.y = 103;
  1948. } else {
  1949. int j;
  1950. // get first online server
  1951. i = 0;
  1952. for(j = 0; j < MAX_MAP_SERVERS; j++)
  1953. if (server_fd[j] >= 0 && server[j].map[0][0]) {
  1954. i = j;
  1955. printf("Map-server #%d found with a map: '%s'.\n", j, server[j].map[0]);
  1956. break;
  1957. }
  1958. // if no map-servers are connected, we send: server closed
  1959. if (j == MAX_MAP_SERVERS) {
  1960. WFIFOW(fd,0) = 0x81;
  1961. WFIFOL(fd,2) = 1; // 01 = Server closed
  1962. WFIFOSET(fd,3);
  1963. RFIFOSKIP(fd,3);
  1964. break;
  1965. }
  1966. }
  1967. }
  1968. WFIFOW(fd, 0) =0x71;
  1969. WFIFOL(fd, 2) =char_dat[0].char_id;
  1970. memcpy(WFIFOP(fd, 6), char_dat[0].last_point.map, 16);
  1971. //Lan check added by Kashy
  1972. if (lan_ip_check(p))
  1973. WFIFOL(fd, 22) = inet_addr(lan_map_ip);
  1974. else
  1975. WFIFOL(fd, 22) = server[i].ip;
  1976. WFIFOW(fd, 26) = server[i].port;
  1977. WFIFOSET(fd, 28);
  1978. if (auth_fifo_pos >= AUTH_FIFO_SIZE) {
  1979. auth_fifo_pos = 0;
  1980. }
  1981. // printf("auth_fifo set (auth_fifo_pos:%d) - account_id:%d char_id:%d login_id1:%d\n", auth_fifo_pos, sd->account_id, char_dat[0].char_id, sd->login_id1);
  1982. auth_fifo[auth_fifo_pos].account_id = sd->account_id;
  1983. auth_fifo[auth_fifo_pos].char_id = char_dat[0].char_id;
  1984. auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1;
  1985. auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2;
  1986. auth_fifo[auth_fifo_pos].delflag = 0;
  1987. //auth_fifo[auth_fifo_pos].char_pos = sd->found_char[ch];
  1988. auth_fifo[auth_fifo_pos].char_pos = 0;
  1989. auth_fifo[auth_fifo_pos].sex = sd->sex;
  1990. auth_fifo[auth_fifo_pos].connect_until_time = sd->connect_until_time;
  1991. auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr;
  1992. auth_fifo_pos++;
  1993. // printf("0x66> end\n");
  1994. RFIFOSKIP(fd, 3);
  1995. break;
  1996. case 0x67: // make new
  1997. // printf("0x67>request make new char\n");
  1998. if (RFIFOREST(fd) < 37)
  1999. return 0;
  2000. i = make_new_char_sql(fd, RFIFOP(fd, 2));
  2001. if (i < 0) {
  2002. WFIFOW(fd, 0) = 0x6e;
  2003. WFIFOB(fd, 2) = 0x00;
  2004. WFIFOSET(fd, 3);
  2005. RFIFOSKIP(fd, 37);
  2006. break;
  2007. }
  2008. WFIFOW(fd, 0) = 0x6d;
  2009. memset(WFIFOP(fd, 2), 0x00, 106);
  2010. mmo_char_fromsql(i, char_dat, 0);
  2011. i = 0;
  2012. WFIFOL(fd, 2) = char_dat[i].char_id;
  2013. WFIFOL(fd,2+4) = char_dat[i].base_exp;
  2014. WFIFOL(fd,2+8) = char_dat[i].zeny;
  2015. WFIFOL(fd,2+12) = char_dat[i].job_exp;
  2016. WFIFOL(fd,2+16) = char_dat[i].job_level;
  2017. WFIFOL(fd,2+28) = char_dat[i].karma;
  2018. WFIFOL(fd,2+32) = char_dat[i].manner;
  2019. WFIFOW(fd,2+40) = 0x30;
  2020. WFIFOW(fd,2+42) = (char_dat[i].hp > 0x7fff) ? 0x7fff : char_dat[i].hp;
  2021. WFIFOW(fd,2+44) = (char_dat[i].max_hp > 0x7fff) ? 0x7fff : char_dat[i].max_hp;
  2022. WFIFOW(fd,2+46) = (char_dat[i].sp > 0x7fff) ? 0x7fff : char_dat[i].sp;
  2023. WFIFOW(fd,2+48) = (char_dat[i].max_sp > 0x7fff) ? 0x7fff : char_dat[i].max_sp;
  2024. WFIFOW(fd,2+50) = DEFAULT_WALK_SPEED; // char_dat[i].speed;
  2025. WFIFOW(fd,2+52) = char_dat[i].class;
  2026. WFIFOW(fd,2+54) = char_dat[i].hair;
  2027. WFIFOW(fd,2+58) = char_dat[i].base_level;
  2028. WFIFOW(fd,2+60) = char_dat[i].skill_point;
  2029. WFIFOW(fd,2+64) = char_dat[i].shield;
  2030. WFIFOW(fd,2+66) = char_dat[i].head_top;
  2031. WFIFOW(fd,2+68) = char_dat[i].head_mid;
  2032. WFIFOW(fd,2+70) = char_dat[i].hair_color;
  2033. memcpy(WFIFOP(fd,2+74), char_dat[i].name, 24);
  2034. WFIFOB(fd,2+98) = char_dat[i].str;
  2035. WFIFOB(fd,2+99) = char_dat[i].agi;
  2036. WFIFOB(fd,2+100) = char_dat[i].vit;
  2037. WFIFOB(fd,2+101) = char_dat[i].int_;
  2038. WFIFOB(fd,2+102) = char_dat[i].dex;
  2039. WFIFOB(fd,2+103) = char_dat[i].luk;
  2040. WFIFOB(fd,2+104) = char_dat[i].char_num;
  2041. WFIFOSET(fd, 108);
  2042. RFIFOSKIP(fd, 37);
  2043. //to do
  2044. for(ch = 0; ch < 9; ch++) {
  2045. if (sd->found_char[ch] == -1) {
  2046. sd->found_char[ch] = char_dat[i].char_id;
  2047. break;
  2048. }
  2049. }
  2050. case 0x68: // delete
  2051. if (RFIFOREST(fd) < 46)
  2052. return 0;
  2053. printf("\033[1;31m Request Char Del:\033[0m \033[1;32m%d\033[0m(\033[1;32m%d\033[0m)\n", sd->account_id, RFIFOL(fd, 2));
  2054. memcpy(email, RFIFOP(fd,6), 40);
  2055. sprintf(tmp_sql, "SELECT `email` FROM `%s` WHERE `%s`='%d'",login_db, login_db_account_id, sd->account_id);
  2056. if (mysql_query(&lmysql_handle, tmp_sql)) {
  2057. printf("\033[1;31m DB server Error Delete Char data - %s \033[0m \n", mysql_error(&lmysql_handle));
  2058. }
  2059. sql_res = mysql_store_result(&lmysql_handle);
  2060. if (sql_res) {
  2061. sql_row = mysql_fetch_row(sql_res);
  2062. if (strcmp(email,sql_row[0]) == 0) {
  2063. mysql_free_result(sql_res);
  2064. } else {
  2065. WFIFOW(fd, 0) = 0x70;
  2066. WFIFOB(fd, 2) = 0;
  2067. WFIFOSET(fd, 3);
  2068. RFIFOSKIP(fd, 46);
  2069. mysql_free_result(sql_res);
  2070. break;
  2071. }
  2072. } else {
  2073. WFIFOW(fd, 0) = 0x70;
  2074. WFIFOB(fd, 2) = 0;
  2075. WFIFOSET(fd, 3);
  2076. RFIFOSKIP(fd, 46);
  2077. mysql_free_result(sql_res);
  2078. break;
  2079. }
  2080. sprintf(tmp_sql, "SELECT `name`,`partner_id` FROM `%s` WHERE `char_id`='%d'",char_db, RFIFOL(fd,2));
  2081. if (mysql_query(&mysql_handle, tmp_sql)) {
  2082. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2083. }
  2084. sql_res = mysql_store_result(&mysql_handle);
  2085. if(sql_res)
  2086. sql_row = mysql_fetch_row(sql_res);
  2087. if (sql_res && sql_row[0]) {
  2088. //delete char from SQL
  2089. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",pet_db, RFIFOL(fd, 2));
  2090. if(mysql_query(&mysql_handle, tmp_sql)) {
  2091. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2092. }
  2093. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",inventory_db, RFIFOL(fd, 2));
  2094. if(mysql_query(&mysql_handle, tmp_sql)) {
  2095. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2096. }
  2097. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",cart_db, RFIFOL(fd, 2));
  2098. if(mysql_query(&mysql_handle, tmp_sql)) {
  2099. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2100. }
  2101. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, RFIFOL(fd, 2));
  2102. if(mysql_query(&mysql_handle, tmp_sql)) {
  2103. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2104. }
  2105. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, RFIFOL(fd, 2));
  2106. if(mysql_query(&mysql_handle, tmp_sql)) {
  2107. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2108. }
  2109. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",char_db, RFIFOL(fd, 2));
  2110. if(mysql_query(&mysql_handle, tmp_sql)) {
  2111. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2112. }
  2113. //Divorce [Wizputer]
  2114. if (sql_row[1] != 0) {
  2115. char buf[64];
  2116. sprintf(tmp_sql,"UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d'",char_db,atoi(sql_row[1]));
  2117. if(mysql_query(&mysql_handle, tmp_sql)) {
  2118. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2119. }
  2120. sprintf(tmp_sql,"DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND `char_id`='%d'",inventory_db,WEDDING_RING_M,WEDDING_RING_F,atoi(sql_row[1]));
  2121. if(mysql_query(&mysql_handle, tmp_sql)) {
  2122. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2123. }
  2124. WBUFW(buf,0) = 0x2b12;
  2125. WBUFL(buf,2) = atoi(sql_row[0]);
  2126. WBUFL(buf,6) = atoi(sql_row[1]);
  2127. mapif_sendall(buf,10);
  2128. }
  2129. // Also delete info from guildtables.
  2130. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",guild_member_db, RFIFOL(fd,2));
  2131. if (mysql_query(&mysql_handle, tmp_sql)) {
  2132. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2133. }
  2134. sprintf(tmp_sql, "SELECT `guild_id` FROM `%s` WHERE `master` = '%s'", guild_db, sql_row[0]);
  2135. if (mysql_query(&mysql_handle, tmp_sql) == 0) {
  2136. sql_res = mysql_store_result(&mysql_handle);
  2137. if (sql_res != NULL) {
  2138. if (mysql_num_rows(sql_res) != 0) {
  2139. sql_row = mysql_fetch_row(sql_res);
  2140. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_db, atoi(sql_row[0]));
  2141. if (mysql_query(&mysql_handle, tmp_sql)) {
  2142. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2143. }
  2144. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_member_db, atoi(sql_row[0]));
  2145. if (mysql_query(&mysql_handle, tmp_sql)) {
  2146. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2147. }
  2148. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_castle_db, atoi(sql_row[0]));
  2149. if (mysql_query(&mysql_handle, tmp_sql)) {
  2150. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2151. }
  2152. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_storage_db, atoi(sql_row[0]));
  2153. if (mysql_query(&mysql_handle, tmp_sql)) {
  2154. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2155. }
  2156. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d' OR `alliance_id` = '%d'", guild_alliance_db, atoi(sql_row[0]), atoi(sql_row[0]));
  2157. if (mysql_query(&mysql_handle, tmp_sql)) {
  2158. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2159. }
  2160. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_position_db, atoi(sql_row[0]));
  2161. if (mysql_query(&mysql_handle, tmp_sql)) {
  2162. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2163. }
  2164. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_skill_db, atoi(sql_row[0]));
  2165. if (mysql_query(&mysql_handle, tmp_sql)) {
  2166. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2167. }
  2168. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_expulsion_db, atoi(sql_row[0]));
  2169. if (mysql_query(&mysql_handle, tmp_sql)) {
  2170. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2171. }
  2172. mysql_free_result(sql_res);
  2173. }
  2174. } else {
  2175. if (mysql_errno(&mysql_handle) != 0) {
  2176. printf("Database server error: %s\n", mysql_error(&mysql_handle));
  2177. }
  2178. }
  2179. } else {
  2180. printf("DB server Error - %s\n", mysql_error(&mysql_handle));
  2181. }
  2182. }
  2183. for(i = 0; i < 9; i++) {
  2184. printf("char comp: %d - %d (%d)\n", sd->found_char[i], RFIFOL(fd, 2), sd->account_id);
  2185. if (sd->found_char[i] == RFIFOL(fd, 2)) {
  2186. for(ch = i; ch < 9-1; ch++)
  2187. sd->found_char[ch] = sd->found_char[ch+1];
  2188. sd->found_char[8] = -1;
  2189. break;
  2190. }
  2191. }
  2192. if (i == 9) { // reject
  2193. WFIFOW(fd, 0) = 0x70;
  2194. WFIFOB(fd, 2) = 0;
  2195. WFIFOSET(fd, 3);
  2196. } else { // deleted!
  2197. WFIFOW(fd, 0) = 0x6f;
  2198. WFIFOSET(fd, 2);
  2199. }
  2200. RFIFOSKIP(fd, 46);
  2201. break;
  2202. case 0x2af8: // login as map-server
  2203. if (RFIFOREST(fd) < 60)
  2204. return 0;
  2205. WFIFOW(fd, 0) = 0x2af9;
  2206. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  2207. if (server_fd[i] < 0)
  2208. break;
  2209. }
  2210. if (i == MAX_MAP_SERVERS || strcmp(RFIFOP(fd,2), userid) || strcmp(RFIFOP(fd,26), passwd)) {
  2211. WFIFOB(fd,2) = 3;
  2212. WFIFOSET(fd, 3);
  2213. } else {
  2214. // int len;
  2215. WFIFOB(fd,2) = 0;
  2216. WFIFOSET(fd, 3);
  2217. session[fd]->func_parse = parse_frommap;
  2218. server_fd[i] = fd;
  2219. if(anti_freeze_enable)
  2220. server_freezeflag[i] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed
  2221. server[i].ip = RFIFOL(fd, 54);
  2222. server[i].port = RFIFOW(fd, 58);
  2223. server[i].users = 0;
  2224. memset(server[i].map, 0, sizeof(server[i].map));
  2225. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  2226. char_mapif_init(fd);
  2227. // send gm acccounts level to map-servers
  2228. /* removed by CLOWNISIUS due to isGM
  2229. len = 4;
  2230. WFIFOW(fd,0) = 0x2b15;
  2231. for(i = 0; i < GM_num; i++) {
  2232. WFIFOL(fd,len) = gm_account[i].account_id;
  2233. WFIFOB(fd,len+4) = (unsigned char)gm_account[i].level;
  2234. len += 5;
  2235. }
  2236. WFIFOW(fd,2) = len;
  2237. WFIFOSET(fd,len);*/
  2238. }
  2239. RFIFOSKIP(fd,60);
  2240. break;
  2241. case 0x187: // Alive?
  2242. if (RFIFOREST(fd) < 6) {
  2243. return 0;
  2244. }
  2245. RFIFOSKIP(fd, 6);
  2246. break;
  2247. case 0x7530: // Athena info get
  2248. WFIFOW(fd, 0) = 0x7531;
  2249. WFIFOB(fd, 2) = ATHENA_MAJOR_VERSION;
  2250. WFIFOB(fd, 3) = ATHENA_MINOR_VERSION;
  2251. WFIFOB(fd, 4) = ATHENA_REVISION;
  2252. WFIFOB(fd, 5) = ATHENA_RELEASE_FLAG;
  2253. WFIFOB(fd, 6) = ATHENA_OFFICIAL_FLAG;
  2254. WFIFOB(fd, 7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR;
  2255. WFIFOW(fd, 8) = ATHENA_MOD_VERSION;
  2256. WFIFOSET(fd, 10);
  2257. RFIFOSKIP(fd, 2);
  2258. return 0;
  2259. case 0x7532: // disconnect(default also disconnect)
  2260. default:
  2261. session[fd]->eof = 1;
  2262. return 0;
  2263. }
  2264. }
  2265. RFIFOFLUSH(fd);
  2266. return 0;
  2267. }
  2268. // Console Command Parser [Wizputer]
  2269. int parse_console(char *buf) {
  2270. char *type,*command;
  2271. type = (char *)malloc(64);
  2272. command = (char *)malloc(64);
  2273. memset(type,0,64);
  2274. memset(command,0,64);
  2275. printf("Console: %s\n",buf);
  2276. if ( sscanf(buf, "%[^:]:%[^\n]", type , command ) < 2 )
  2277. sscanf(buf,"%[^\n]",type);
  2278. printf("Type of command: %s || Command: %s \n",type,command);
  2279. if(buf) free(buf);
  2280. if(type) free(type);
  2281. if(command) free(command);
  2282. return 0;
  2283. }
  2284. // MAP send all
  2285. int mapif_sendall(unsigned char *buf, unsigned int len) {
  2286. int i, c;
  2287. int fd;
  2288. c = 0;
  2289. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  2290. if ((fd = server_fd[i]) >= 0) {
  2291. memcpy(WFIFOP(fd,0), buf, len);
  2292. WFIFOSET(fd,len);
  2293. c++;
  2294. }
  2295. }
  2296. return c;
  2297. }
  2298. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len) {
  2299. int i, c;
  2300. int fd;
  2301. c = 0;
  2302. for(i=0, c=0;i<MAX_MAP_SERVERS;i++){
  2303. if ((fd = server_fd[i]) >= 0 && fd != sfd) {
  2304. memcpy(WFIFOP(fd,0), buf, len);
  2305. WFIFOSET(fd, len);
  2306. c++;
  2307. }
  2308. }
  2309. return c;
  2310. }
  2311. int mapif_send(int fd, unsigned char *buf, unsigned int len) {
  2312. int i;
  2313. if (fd >= 0) {
  2314. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  2315. if (fd == server_fd[i]) {
  2316. memcpy(WFIFOP(fd,0), buf, len);
  2317. WFIFOSET(fd,len);
  2318. return 1;
  2319. }
  2320. }
  2321. }
  2322. return 0;
  2323. }
  2324. int check_connect_login_server(int tid, unsigned int tick, int id, int data) {
  2325. if (login_fd <= 0 || session[login_fd] == NULL) {
  2326. printf("Attempt to connect to login-server...\n");
  2327. login_fd = make_connection(login_ip, login_port);
  2328. session[login_fd]->func_parse = parse_tologin;
  2329. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  2330. WFIFOW(login_fd,0) = 0x2710;
  2331. memset(WFIFOP(login_fd,2), 0, 24);
  2332. memcpy(WFIFOP(login_fd,2), userid, strlen(userid) < 24 ? strlen(userid) : 24);
  2333. memset(WFIFOP(login_fd,26), 0, 24);
  2334. memcpy(WFIFOP(login_fd,26), passwd, strlen(passwd) < 24 ? strlen(passwd) : 24);
  2335. WFIFOL(login_fd,50) = 0;
  2336. WFIFOL(login_fd,54) = char_ip;
  2337. WFIFOL(login_fd,58) = char_port;
  2338. memset(WFIFOP(login_fd,60), 0, 20);
  2339. memcpy(WFIFOP(login_fd,60), server_name, strlen(server_name) < 20 ? strlen(server_name) : 20);
  2340. WFIFOW(login_fd,80) = 0;
  2341. WFIFOW(login_fd,82) = char_maintenance;
  2342. WFIFOW(login_fd,84) = char_new;
  2343. WFIFOSET(login_fd,86);
  2344. }
  2345. return 0;
  2346. }
  2347. //----------------------------------------------------------
  2348. // Return numerical value of a switch configuration by [Yor]
  2349. // on/off, english, fran軋is, deutsch, espa�l
  2350. //----------------------------------------------------------
  2351. int config_switch(const char *str) {
  2352. if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0)
  2353. return 1;
  2354. if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0)
  2355. return 0;
  2356. return atoi(str);
  2357. }
  2358. // Lan Support conf reading added by Kashy
  2359. int char_lan_config_read(const char *lancfgName){
  2360. char subnetmask[128];
  2361. char line[1024], w1[1024], w2[1024];
  2362. FILE *fp;
  2363. struct hostent * h = NULL;
  2364. if ((fp = fopen(lancfgName, "r")) == NULL) {
  2365. printf("file not found: %s\n", lancfgName);
  2366. return 1;
  2367. }
  2368. printf("Start reading of Lan Support configuration file\n");
  2369. while(fgets(line, sizeof(line)-1, fp)){
  2370. if (line[0] == '/' && line[1] == '/')
  2371. continue;
  2372. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  2373. continue;
  2374. else if (strcmpi(w1, "lan_map_ip") == 0) {
  2375. h = gethostbyname(w2);
  2376. if (h != NULL) {
  2377. sprintf(lan_map_ip, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  2378. } else {
  2379. strncpy(lan_map_ip, w2, sizeof(lan_map_ip));
  2380. lan_map_ip[sizeof(lan_map_ip)-1] = 0;
  2381. }
  2382. printf("set Lan_map_IP : %s\n", lan_map_ip);
  2383. }
  2384. else if (strcmpi(w1, "subnetmask") == 0) {
  2385. unsigned int k0, k1, k2, k3;
  2386. strcpy(subnetmask, w2);
  2387. sscanf(subnetmask, "%d.%d.%d.%d", &k0, &k1, &k2, &k3);
  2388. subnetmaski[0] = k0; subnetmaski[1] = k1; subnetmaski[2] = k2; subnetmaski[3] = k3;
  2389. printf("set subnetmask : %s\n", w2);
  2390. }
  2391. }
  2392. fclose(fp);
  2393. printf("End reading of Lan Support configuration file\n");
  2394. return 0;
  2395. }
  2396. void do_final(void) {
  2397. printf("Doing final stage...\n");
  2398. //mmo_char_sync();
  2399. //inter_save();
  2400. do_final_itemdb();
  2401. //check SQL save progress.
  2402. //wait until save char complete
  2403. printf("waiting until char saving complete...\n");
  2404. do {
  2405. sleep (0);
  2406. }while (save_flag != 0);
  2407. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `online`='1'", char_db);
  2408. if (mysql_query(&mysql_handle, tmp_sql)) {
  2409. printf("DB server Error (insert `char`)- %s\n", mysql_error(&mysql_handle));
  2410. }
  2411. sprintf(tmp_sql,"DELETE FROM `ragsrvinfo");
  2412. if (mysql_query(&mysql_handle, tmp_sql)) {
  2413. printf("DB server Error (insert `char`)- %s\n", mysql_error(&mysql_handle));
  2414. }
  2415. if(gm_account) free(gm_account);
  2416. if(char_dat) free(char_dat);
  2417. delete_session(login_fd);
  2418. delete_session(char_fd);
  2419. mysql_close(&mysql_handle);
  2420. mysql_close(&lmysql_handle);
  2421. printf("ok! all done...\n");
  2422. }
  2423. void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */
  2424. char line[1024], w1[1024], w2[1024];
  2425. FILE *fp;
  2426. printf("reading configure: %s\n", cfgName);
  2427. if ((fp = fopen(cfgName, "r")) == NULL) {
  2428. printf("file not found: %s\n", cfgName);
  2429. exit(1);
  2430. }
  2431. while(fgets(line, sizeof(line)-1, fp)){
  2432. if(line[0] == '/' && line[1] == '/')
  2433. continue;
  2434. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  2435. continue;
  2436. if(strcmpi(w1, "login_db") == 0) {
  2437. strcpy(login_db, w2);
  2438. }else if(strcmpi(w1,"char_db")==0){
  2439. strcpy(char_db,w2);
  2440. }else if(strcmpi(w1,"cart_db")==0){
  2441. strcpy(cart_db,w2);
  2442. }else if(strcmpi(w1,"inventory_db")==0){
  2443. strcpy(inventory_db,w2);
  2444. }else if(strcmpi(w1,"charlog_db")==0){
  2445. strcpy(charlog_db,w2);
  2446. }else if(strcmpi(w1,"storage_db")==0){
  2447. strcpy(storage_db,w2);
  2448. }else if(strcmpi(w1,"reg_db")==0){
  2449. strcpy(reg_db,w2);
  2450. }else if(strcmpi(w1,"skill_db")==0){
  2451. strcpy(skill_db,w2);
  2452. }else if(strcmpi(w1,"interlog_db")==0){
  2453. strcpy(interlog_db,w2);
  2454. }else if(strcmpi(w1,"memo_db")==0){
  2455. strcpy(memo_db,w2);
  2456. }else if(strcmpi(w1,"guild_db")==0){
  2457. strcpy(guild_db,w2);
  2458. }else if(strcmpi(w1,"guild_alliance_db")==0){
  2459. strcpy(guild_alliance_db,w2);
  2460. }else if(strcmpi(w1,"guild_castle_db")==0){
  2461. strcpy(guild_castle_db,w2);
  2462. }else if(strcmpi(w1,"guild_expulsion_db")==0){
  2463. strcpy(guild_expulsion_db,w2);
  2464. }else if(strcmpi(w1,"guild_member_db")==0){
  2465. strcpy(guild_member_db,w2);
  2466. }else if(strcmpi(w1,"guild_skill_db")==0){
  2467. strcpy(guild_skill_db,w2);
  2468. }else if(strcmpi(w1,"guild_position_db")==0){
  2469. strcpy(guild_position_db,w2);
  2470. }else if(strcmpi(w1,"guild_storage_db")==0){
  2471. strcpy(guild_storage_db,w2);
  2472. }else if(strcmpi(w1,"party_db")==0){
  2473. strcpy(party_db,w2);
  2474. }else if(strcmpi(w1,"pet_db")==0){
  2475. strcpy(pet_db,w2);
  2476. }else if(strcmpi(w1,"db_path")==0){
  2477. strcpy(db_path,w2);
  2478. //Map server option to use SQL db or not
  2479. }else if(strcmpi(w1,"use_sql_db")==0){ // added for sql item_db read for char server [Valaris]
  2480. if (strcmpi(w2, "yes")) {
  2481. db_use_sqldbs = 1;
  2482. } else if (strcmpi(w2, "no")) {
  2483. db_use_sqldbs = 0;
  2484. }
  2485. printf("Using SQL dbs: %s\n",w2);
  2486. //custom columns for login database
  2487. }else if(strcmpi(w1,"login_db_level")==0){
  2488. strcpy(login_db_level,w2);
  2489. }else if(strcmpi(w1,"login_db_account_id")==0){
  2490. strcpy(login_db_account_id,w2);
  2491. }else if(strcmpi(w1,"lowest_gm_level")==0){
  2492. lowest_gm_level = atoi(w2);
  2493. printf("set lowest_gm_level : %s\n",w2);
  2494. }
  2495. }
  2496. fclose(fp);
  2497. printf("reading configure done.....\n");
  2498. }
  2499. int char_config_read(const char *cfgName) {
  2500. struct hostent *h = NULL;
  2501. char line[1024], w1[1024], w2[1024];
  2502. FILE *fp;
  2503. if ((fp = fopen(cfgName, "r")) == NULL) {
  2504. printf("Configuration file not found: %s.\n", cfgName);
  2505. exit(1);
  2506. }
  2507. while(fgets(line, sizeof(line)-1, fp)) {
  2508. if (line[0] == '/' && line[1] == '/')
  2509. continue;
  2510. line[sizeof(line)-1] = '\0';
  2511. if (sscanf(line,"%[^:]: %[^\r\n]", w1, w2) != 2)
  2512. continue;
  2513. remove_control_chars(w1);
  2514. remove_control_chars(w2);
  2515. if (strcmpi(w1, "userid") == 0) {
  2516. memcpy(userid, w2, 24);
  2517. } else if (strcmpi(w1, "passwd") == 0) {
  2518. memcpy(passwd, w2, 24);
  2519. } else if (strcmpi(w1, "server_name") == 0) {
  2520. memcpy(server_name, w2, 16);
  2521. printf("%s server has been intialized\n", w2);
  2522. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  2523. if (strlen(w2) >= 4) {
  2524. memcpy(wisp_server_name, w2, sizeof(wisp_server_name));
  2525. wisp_server_name[sizeof(wisp_server_name) - 1] = '\0';
  2526. }
  2527. } else if (strcmpi(w1, "login_ip") == 0) {
  2528. login_ip_set_ = 1;
  2529. h = gethostbyname (w2);
  2530. if (h != NULL) {
  2531. printf("Login server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  2532. sprintf(login_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  2533. } else
  2534. memcpy(login_ip_str,w2,16);
  2535. } else if (strcmpi(w1, "login_port") == 0) {
  2536. login_port=atoi(w2);
  2537. } else if (strcmpi(w1, "char_ip") == 0) {
  2538. char_ip_set_ = 1;
  2539. h = gethostbyname (w2);
  2540. if(h != NULL) {
  2541. printf("Character server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  2542. sprintf(char_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  2543. } else
  2544. memcpy(char_ip_str, w2, 16);
  2545. } else if (strcmpi(w1, "char_port") == 0) {
  2546. char_port = atoi(w2);
  2547. } else if (strcmpi(w1, "char_maintenance") == 0) {
  2548. char_maintenance = atoi(w2);
  2549. } else if (strcmpi(w1, "char_new")==0){
  2550. char_new = atoi(w2);
  2551. } else if (strcmpi(w1, "max_connect_user") == 0) {
  2552. max_connect_user = atoi(w2);
  2553. if (max_connect_user < 0)
  2554. max_connect_user = 0; // unlimited online players
  2555. } else if (strcmpi(w1, "check_ip_flag") == 0) {
  2556. check_ip_flag = config_switch(w2);
  2557. } else if (strcmpi(w1, "autosave_time") == 0) {
  2558. autosave_interval = atoi(w2)*1000;
  2559. if (autosave_interval <= 0)
  2560. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2561. } else if (strcmpi(w1, "start_point") == 0) {
  2562. char map[32];
  2563. int x, y;
  2564. if (sscanf(w2,"%[^,],%d,%d", map, &x, &y) < 3)
  2565. continue;
  2566. if (strstr(map, ".gat") != NULL) { // Verify at least if '.gat' is in the map name
  2567. memcpy(start_point.map, map, 16);
  2568. start_point.x = x;
  2569. start_point.y = y;
  2570. }
  2571. } else if (strcmpi(w1, "start_zeny") == 0) {
  2572. start_zeny = atoi(w2);
  2573. if (start_zeny < 0)
  2574. start_zeny = 0;
  2575. } else if (strcmpi(w1, "start_weapon") == 0) {
  2576. start_weapon = atoi(w2);
  2577. if (start_weapon < 0)
  2578. start_weapon = 0;
  2579. } else if (strcmpi(w1, "start_armor") == 0) {
  2580. start_armor = atoi(w2);
  2581. if (start_armor < 0)
  2582. start_armor = 0;
  2583. } else if(strcmpi(w1,"imalive_on")==0){ //Added by Mugendai for I'm Alive mod
  2584. imalive_on = atoi(w2); //Added by Mugendai for I'm Alive mod
  2585. } else if(strcmpi(w1,"imalive_time")==0){ //Added by Mugendai for I'm Alive mod
  2586. imalive_time = atoi(w2); //Added by Mugendai for I'm Alive mod
  2587. } else if(strcmpi(w1,"flush_on")==0){ //Added by Mugendai for GUI
  2588. flush_on = atoi(w2); //Added by Mugendai for GUI
  2589. } else if(strcmpi(w1,"flush_time")==0){ //Added by Mugendai for GUI
  2590. flush_time = atoi(w2); //Added by Mugendai for GUI
  2591. } else if(strcmpi(w1,"log_char")==0){ //log char or not [devil]
  2592. log_char = atoi(w2);
  2593. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  2594. strcpy(unknown_char_name, w2);
  2595. unknown_char_name[24] = 0;
  2596. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  2597. name_ignoring_case = config_switch(w2);
  2598. } else if (strcmpi(w1, "char_name_option") == 0) {
  2599. char_name_option = atoi(w2);
  2600. } else if (strcmpi(w1, "char_name_letters") == 0) {
  2601. strcpy(char_name_letters, w2);
  2602. } else if (strcmpi(w1, "check_ip_flag") == 0) {
  2603. check_ip_flag = config_switch(w2);
  2604. // anti-freeze options [Valaris]
  2605. } else if(strcmpi(w1,"anti_freeze_enable")==0){
  2606. anti_freeze_enable = config_switch(w2);
  2607. } else if (strcmpi(w1, "anti_freeze_interval") == 0) {
  2608. ANTI_FREEZE_INTERVAL = atoi(w2);
  2609. if (ANTI_FREEZE_INTERVAL < 5)
  2610. ANTI_FREEZE_INTERVAL = 5; // minimum 5 seconds
  2611. } else if (strcmpi(w1, "import") == 0) {
  2612. char_config_read(w2);
  2613. } else if (strcmpi(w1, "console") == 0) {
  2614. if(strcmpi(w2,"on") == 0 || strcmpi(w2,"yes") == 0 )
  2615. console = 1;
  2616. }
  2617. }
  2618. fclose(fp);
  2619. //Read ItemDB
  2620. do_init_itemdb();
  2621. return 0;
  2622. }
  2623. //-----------------------------------------------------
  2624. //I'm Alive Alert
  2625. //Used to output 'I'm Alive' every few seconds
  2626. //Intended to let frontends know if the app froze
  2627. //-----------------------------------------------------
  2628. int imalive_timer(int tid, unsigned int tick, int id, int data){
  2629. printf("I'm Alive\n");
  2630. return 0;
  2631. }
  2632. //-----------------------------------------------------
  2633. //Flush stdout
  2634. //stdout buffer needs flushed to be seen in GUI
  2635. //-----------------------------------------------------
  2636. int flush_timer(int tid, unsigned int tick, int id, int data){
  2637. fflush(stdout);
  2638. return 0;
  2639. }
  2640. int do_init(int argc, char **argv){
  2641. int i;
  2642. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  2643. memset(&server[i], 0, sizeof(struct mmo_map_server));
  2644. server_fd[i] = -1;
  2645. }
  2646. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  2647. char_lan_config_read((argc > 1) ? argv[1] : LAN_CONF_NAME);
  2648. sql_config_read(SQL_CONF_NAME);
  2649. printf("charserver configuration reading done.....\n");
  2650. inter_init((argc > 2) ? argv[2] : inter_cfgName); // inter server テハア篳ュ
  2651. printf("interserver configuration reading done.....\n");
  2652. printf("start char server initializing.....\n");
  2653. mmo_char_sql_init();
  2654. printf("char server initializing done.....\n");
  2655. printf("set parser -> parse_char().....\n");
  2656. set_defaultparse(parse_char);
  2657. printf("set terminate function -> do_final().....\n");
  2658. set_termfunc(do_final);
  2659. printf("open port %d.....\n",char_port);
  2660. char_fd = make_listen_port(char_port);
  2661. if ((naddr_ != 0) && (login_ip_set_ == 0 || char_ip_set_ == 0)) {
  2662. // The char server should know what IP address it is running on
  2663. // - MouseJstr
  2664. int localaddr = ntohl(addr_[0]);
  2665. unsigned char *ptr = (unsigned char *) &localaddr;
  2666. char buf[16];
  2667. sprintf(buf, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);;
  2668. if (naddr_ != 1)
  2669. printf("Multiple interfaces detected.. using %s as our IP address\n", buf);
  2670. else
  2671. printf("Defaulting to %s as our IP address\n", buf);
  2672. if (login_ip_set_ == 0)
  2673. strcpy(login_ip_str, buf);
  2674. if (char_ip_set_ == 0)
  2675. strcpy(char_ip_str, buf);
  2676. if (ptr[0] == 192 && ptr[1] == 168)
  2677. printf("Firewall detected.. edit lan_support.conf and char_athena.conf");
  2678. }
  2679. login_ip = inet_addr(login_ip_str);
  2680. char_ip = inet_addr(char_ip_str);
  2681. // send ALIVE PING to login server.
  2682. printf("add interval tic (check_connect_login_server)....\n");
  2683. i = add_timer_interval(gettick() + 10, check_connect_login_server, 0, 0, 10 * 1000);
  2684. //no need to set sync timer on SQL version.
  2685. //printf("add interval tic (mmo_char_sync_timer)....\n");
  2686. //i = add_timer_interval(gettick() + 10, mmo_char_sync_timer, 0, 0, autosave_interval);
  2687. add_timer_func_list(map_anti_freeze_system, "map_anti_freeze_system");
  2688. //Added for Mugendais I'm Alive mod
  2689. if(imalive_on)
  2690. add_timer_interval(gettick()+10, imalive_timer,0,0,imalive_time*1000);
  2691. //Added by Mugendai for GUI support
  2692. if(flush_on)
  2693. add_timer_interval(gettick()+10, flush_timer,0,0,flush_time);
  2694. if(anti_freeze_enable > 0) {
  2695. add_timer_func_list(map_anti_freeze_system, "map_anti_freeze_system");
  2696. i = add_timer_interval(gettick() + 1000, map_anti_freeze_system, 0, 0, ANTI_FREEZE_INTERVAL * 1000); // checks every X seconds user specifies
  2697. }
  2698. read_gm_account();
  2699. if ( console ) {
  2700. set_defaultconsoleparse(parse_console);
  2701. start_console();
  2702. }
  2703. printf("char server init func end (now unlimited loop start!)....\n");
  2704. printf("The char-server is \033[1;32mready\033[0m (Server is listening on the port %d).\n\n", char_port);
  2705. return 0;
  2706. }
  2707. #undef mysql_query
  2708. int STDCALL mysql_query(MYSQL *mysql, const char *q);
  2709. int debug_mysql_query(MYSQL *mysql, const char *q) {
  2710. printf("DEBUG_MYSQL: %s\n", q);
  2711. return mysql_query(mysql, q);
  2712. }