char.c 116 KB

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