char.c 124 KB

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