char.c 110 KB

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