char.c 104 KB

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