char.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/strlib.h"
  5. #include "../common/core.h"
  6. #include "../common/timer.h"
  7. #include "../common/mmo.h"
  8. #include "../common/db.h"
  9. #include "../common/malloc.h"
  10. #include "../common/mapindex.h"
  11. #include "../common/showmsg.h"
  12. #include "../common/socket.h"
  13. #include "../common/version.h"
  14. #include "../common/utils.h"
  15. #include "inter.h"
  16. #include "int_guild.h"
  17. #include "int_homun.h"
  18. #include "int_party.h"
  19. #include "int_storage.h"
  20. #include "char.h"
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <signal.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. // private declarations
  29. #define CHAR_CONF_NAME "conf/char_athena.conf"
  30. #define LAN_CONF_NAME "conf/subnet_athena.conf"
  31. #define SQL_CONF_NAME "conf/inter_athena.conf"
  32. char char_db[256] = "char";
  33. char scdata_db[256] = "sc_data";
  34. char cart_db[256] = "cart_inventory";
  35. char inventory_db[256] = "inventory";
  36. char charlog_db[256] = "charlog";
  37. char storage_db[256] = "storage";
  38. char interlog_db[256] = "interlog";
  39. char reg_db[256] = "global_reg_value";
  40. char skill_db[256] = "skill";
  41. char memo_db[256] = "memo";
  42. char guild_db[256] = "guild";
  43. char guild_alliance_db[256] = "guild_alliance";
  44. char guild_castle_db[256] = "guild_castle";
  45. char guild_expulsion_db[256] = "guild_expulsion";
  46. char guild_member_db[256] = "guild_member";
  47. char guild_position_db[256] = "guild_position";
  48. char guild_skill_db[256] = "guild_skill";
  49. char guild_storage_db[256] = "guild_storage";
  50. char party_db[256] = "party";
  51. char pet_db[256] = "pet";
  52. char mail_db[256] = "mail"; // MAIL SYSTEM
  53. char auction_db[256] = "auction"; // Auctions System
  54. char friend_db[256] = "friends";
  55. char hotkey_db[256] = "hotkey";
  56. char quest_db[256] = "quest";
  57. char quest_obj_db[256] = "quest_objective";
  58. //If your code editor is having problems syntax highlighting this file, uncomment this and RECOMMENT IT BEFORE COMPILING
  59. //#undef TXT_SQL_CONVERT
  60. #ifndef TXT_SQL_CONVERT
  61. static DBMap* char_db_; // int char_id -> struct mmo_charstatus*
  62. char db_path[1024] = "db";
  63. int db_use_sqldbs;
  64. struct mmo_map_server {
  65. int fd;
  66. uint32 ip;
  67. uint16 port;
  68. int users;
  69. unsigned short map[MAX_MAP_PER_SERVER];
  70. } server[MAX_MAP_SERVERS];
  71. int login_fd=-1, char_fd=-1;
  72. char userid[24];
  73. char passwd[24];
  74. char server_name[20];
  75. char wisp_server_name[NAME_LENGTH] = "Server";
  76. char login_ip_str[128];
  77. uint32 login_ip = 0;
  78. uint16 login_port = 6900;
  79. char char_ip_str[128];
  80. uint32 char_ip = 0;
  81. char bind_ip_str[128];
  82. uint32 bind_ip = INADDR_ANY;
  83. uint16 char_port = 6121;
  84. int char_maintenance = 0;
  85. bool char_new = true;
  86. int char_new_display = 0;
  87. int name_ignoring_case = 0; // Allow or not identical name for characters but with a different case by [Yor]
  88. int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
  89. char unknown_char_name[NAME_LENGTH] = "Unknown"; // Name to use when the requested name cannot be determined
  90. #define TRIM_CHARS "\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  91. char char_name_letters[1024] = ""; // list of letters/symbols used to authorise or not a name of a character. by [Yor]
  92. bool char_rename = true;
  93. int char_per_account = 0; //Maximum charas per account (default unlimited) [Sirius]
  94. int char_del_level = 0; //From which level u can delete character [Lupus]
  95. int log_char = 1; // loggin char or not [devil]
  96. int log_inter = 1; // loggin inter or not [devil]
  97. #ifdef TXT_SQL_CONVERT
  98. int save_log = 0; //Have the logs be off by default when converting
  99. #else
  100. int save_log = 1;
  101. #endif
  102. static int online_check = 1; //If one, it won't let players connect when their account is already registered online and will send the relevant map server a kick user request. [Skotlex]
  103. // Advanced subnet check [LuzZza]
  104. struct s_subnet {
  105. uint32 mask;
  106. uint32 char_ip;
  107. uint32 map_ip;
  108. } subnet[16];
  109. int subnet_count = 0;
  110. struct char_session_data {
  111. bool auth; // whether the session is authed or not
  112. int account_id, login_id1, login_id2, sex;
  113. int found_char[MAX_CHARS]; // ids of chars on this account
  114. char email[40]; // e-mail (default: a@a.com) by [Yor]
  115. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  116. int gmlevel;
  117. };
  118. int char_num, char_max;
  119. int max_connect_user = 0;
  120. int gm_allow_level = 99;
  121. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  122. int start_zeny = 0;
  123. int start_weapon = 1201;
  124. int start_armor = 2301;
  125. int guild_exp_rate = 100;
  126. //Custom limits for the fame lists. [Skotlex]
  127. int fame_list_size_chemist = MAX_FAME_LIST;
  128. int fame_list_size_smith = MAX_FAME_LIST;
  129. int fame_list_size_taekwon = MAX_FAME_LIST;
  130. // Char-server-side stored fame lists [DracoRPG]
  131. struct fame_list smith_fame_list[MAX_FAME_LIST];
  132. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  133. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  134. // check for exit signal
  135. // 0 is saving complete
  136. // other is char_id
  137. unsigned int save_flag = 0;
  138. // Initial position (it's possible to set it in conf file)
  139. struct point start_point = { 0, 53, 111 };
  140. int console = 0;
  141. //-----------------------------------------------------
  142. // Auth database
  143. //-----------------------------------------------------
  144. #define AUTH_TIMEOUT 30000
  145. struct auth_node {
  146. int account_id;
  147. int char_id;
  148. uint32 login_id1;
  149. uint32 login_id2;
  150. uint32 ip;
  151. int sex;
  152. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  153. int gmlevel;
  154. };
  155. static DBMap* auth_db; // int account_id -> struct auth_node*
  156. //-----------------------------------------------------
  157. // Online User Database
  158. //-----------------------------------------------------
  159. struct online_char_data {
  160. int account_id;
  161. int char_id;
  162. int fd;
  163. int waiting_disconnect;
  164. short server; // -2: unknown server, -1: not connected, 0+: id of server
  165. };
  166. static DBMap* online_char_db; // int account_id -> struct online_char_data*
  167. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr data);
  168. static void* create_online_char_data(DBKey key, va_list args)
  169. {
  170. struct online_char_data* character;
  171. CREATE(character, struct online_char_data, 1);
  172. character->account_id = key.i;
  173. character->char_id = -1;
  174. character->server = -1;
  175. character->fd = -1;
  176. character->waiting_disconnect = -1;
  177. return character;
  178. }
  179. void set_char_charselect(int account_id)
  180. {
  181. struct online_char_data* character;
  182. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  183. if( character->server > -1 )
  184. server[character->server].users--;
  185. character->char_id = -1;
  186. character->server = -1;
  187. if(character->waiting_disconnect != -1) {
  188. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  189. character->waiting_disconnect = -1;
  190. }
  191. if (login_fd > 0 && !session[login_fd]->flag.eof)
  192. {
  193. WFIFOHEAD(login_fd,6);
  194. WFIFOW(login_fd,0) = 0x272c;
  195. WFIFOL(login_fd,2) = account_id;
  196. WFIFOSET(login_fd,6);
  197. }
  198. }
  199. void set_char_online(int map_id, int char_id, int account_id)
  200. {
  201. struct online_char_data* character;
  202. struct mmo_charstatus *cp;
  203. //Update DB
  204. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d'", char_db, char_id) )
  205. Sql_ShowDebug(sql_handle);
  206. //Check to see for online conflicts
  207. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  208. if (online_check && character->char_id != -1 && character->server > -1 && character->server != map_id)
  209. {
  210. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  211. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  212. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  213. }
  214. //Update state data
  215. character->char_id = char_id;
  216. character->server = map_id;
  217. if( character->server > -1 )
  218. server[character->server].users++;
  219. //Get rid of disconnect timer
  220. if(character->waiting_disconnect != -1) {
  221. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  222. character->waiting_disconnect = -1;
  223. }
  224. //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  225. cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  226. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  227. //Notify login server
  228. if (login_fd > 0 && !session[login_fd]->flag.eof)
  229. {
  230. WFIFOHEAD(login_fd,6);
  231. WFIFOW(login_fd,0) = 0x272b;
  232. WFIFOL(login_fd,2) = account_id;
  233. WFIFOSET(login_fd,6);
  234. }
  235. }
  236. void set_char_offline(int char_id, int account_id)
  237. {
  238. struct online_char_data* character;
  239. if ( char_id == -1 )
  240. {
  241. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id) )
  242. Sql_ShowDebug(sql_handle);
  243. }
  244. else
  245. {
  246. struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  247. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  248. if (cp)
  249. idb_remove(char_db_,char_id);
  250. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d'", char_db, char_id) )
  251. Sql_ShowDebug(sql_handle);
  252. }
  253. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  254. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  255. if( character->server > -1 )
  256. server[character->server].users--;
  257. if(character->waiting_disconnect != -1){
  258. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  259. character->waiting_disconnect = -1;
  260. }
  261. //If user is NOT at char screen, delete entry [Kevin]
  262. if(character->char_id != -1)
  263. {
  264. idb_remove(online_char_db, account_id);
  265. }
  266. }
  267. if (login_fd > 0 && !session[login_fd]->flag.eof && (char_id == -1 || character == NULL || character->char_id != -1))
  268. {
  269. WFIFOHEAD(login_fd,6);
  270. WFIFOW(login_fd,0) = 0x272c;
  271. WFIFOL(login_fd,2) = account_id;
  272. WFIFOSET(login_fd,6);
  273. }
  274. }
  275. static int char_db_setoffline(DBKey key, void* data, va_list ap)
  276. {
  277. struct online_char_data* character = (struct online_char_data*)data;
  278. int server = va_arg(ap, int);
  279. if (server == -1) {
  280. character->char_id = -1;
  281. character->server = -1;
  282. if(character->waiting_disconnect != -1){
  283. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  284. character->waiting_disconnect = -1;
  285. }
  286. } else if (character->server == server)
  287. character->server = -2; //In some map server that we aren't connected to.
  288. return 0;
  289. }
  290. static int char_db_kickoffline(DBKey key, void* data, va_list ap)
  291. {
  292. struct online_char_data* character = (struct online_char_data*)data;
  293. int server_id = va_arg(ap, int);
  294. if (server_id > -1 && character->server != server_id)
  295. return 0;
  296. //Kick out any connected characters, and set them offline as appropiate.
  297. if (character->server > -1)
  298. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1);
  299. else if (character->waiting_disconnect == -1)
  300. set_char_offline(character->char_id, character->account_id);
  301. else
  302. return 0; // fail
  303. return 1;
  304. }
  305. void set_all_offline(int id)
  306. {
  307. if (id < 0)
  308. ShowNotice("Sending all users offline.\n");
  309. else
  310. ShowNotice("Sending users of map-server %d offline.\n",id);
  311. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  312. if (id >= 0 || login_fd <= 0 || session[login_fd]->flag.eof)
  313. return;
  314. //Tell login-server to also mark all our characters as offline.
  315. WFIFOHEAD(login_fd,2);
  316. WFIFOW(login_fd,0) = 0x2737;
  317. WFIFOSET(login_fd,2);
  318. }
  319. void set_all_offline_sql(void)
  320. {
  321. //Set all players to 'OFFLINE'
  322. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", char_db) )
  323. Sql_ShowDebug(sql_handle);
  324. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", guild_member_db) )
  325. Sql_ShowDebug(sql_handle);
  326. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", guild_db) )
  327. Sql_ShowDebug(sql_handle);
  328. }
  329. static void* create_charstatus(DBKey key, va_list args)
  330. {
  331. struct mmo_charstatus *cp;
  332. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  333. cp->char_id = key.i;
  334. return cp;
  335. }
  336. #endif //TXT_SQL_CONVERT
  337. int mmo_char_tosql(int char_id, struct mmo_charstatus* p)
  338. {
  339. int i = 0;
  340. int count = 0;
  341. int diff = 0;
  342. char save_status[128]; //For displaying save information. [Skotlex]
  343. struct mmo_charstatus *cp;
  344. StringBuf buf;
  345. if (char_id!=p->char_id) return 0;
  346. #ifndef TXT_SQL_CONVERT
  347. cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus);
  348. #else
  349. cp = (struct mmo_charstatus*)aCalloc(1, sizeof(struct mmo_charstatus));
  350. #endif
  351. StringBuf_Init(&buf);
  352. memset(save_status, 0, sizeof(save_status));
  353. //map inventory data
  354. if( memcmp(p->inventory, cp->inventory, sizeof(p->inventory)) )
  355. {
  356. memitemdata_to_sql(p->inventory, MAX_INVENTORY, p->char_id, TABLE_INVENTORY);
  357. strcat(save_status, " inventory");
  358. }
  359. //map cart data
  360. if( memcmp(p->cart, cp->cart, sizeof(p->cart)) )
  361. {
  362. memitemdata_to_sql(p->cart, MAX_CART, p->char_id, TABLE_CART);
  363. strcat(save_status, " cart");
  364. }
  365. //map storage data
  366. if( memcmp(p->storage.items, cp->storage.items, sizeof(p->storage.items)) )
  367. {
  368. memitemdata_to_sql(p->storage.items, MAX_STORAGE, p->account_id, TABLE_STORAGE);
  369. strcat(save_status, " storage");
  370. }
  371. #ifdef TXT_SQL_CONVERT
  372. { //Insert the barebones to then update the rest.
  373. char esc_name[NAME_LENGTH*2+1];
  374. Sql_EscapeStringLen(sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH));
  375. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`char_id`, `account_id`, `char_num`, `name`) VALUES ('%d', '%d', '%d', '%s')",
  376. char_db, p->char_id, p->account_id, p->slot, esc_name) )
  377. {
  378. Sql_ShowDebug(sql_handle);
  379. }
  380. strcat(save_status, " creation");
  381. }
  382. #endif
  383. if (
  384. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  385. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  386. (p->zeny != cp->zeny) ||
  387. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  388. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  389. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  390. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  391. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  392. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  393. (p->option != cp->option) ||
  394. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  395. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) ||
  396. (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  397. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom)
  398. )
  399. { //Save status
  400. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  401. "`base_exp`='%u', `job_exp`='%u', `zeny`='%d',"
  402. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  403. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  404. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',"
  405. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  406. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d'"
  407. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  408. char_db, p->base_level, p->job_level,
  409. p->base_exp, p->job_exp, p->zeny,
  410. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  411. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  412. p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id,
  413. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  414. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  415. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y,
  416. p->account_id, p->char_id) )
  417. {
  418. Sql_ShowDebug(sql_handle);
  419. }
  420. strcat(save_status, " status");
  421. }
  422. //Values that will seldom change (to speed up saving)
  423. if (
  424. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  425. (p->class_ != cp->class_) ||
  426. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  427. (p->mother != cp->mother) || (p->child != cp->child) ||
  428. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  429. (p->fame != cp->fame)
  430. )
  431. {
  432. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',"
  433. "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',"
  434. "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d',"
  435. "`karma`='%d',`manner`='%d', `fame`='%d'"
  436. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  437. char_db, p->class_,
  438. p->hair, p->hair_color, p->clothes_color,
  439. p->partner_id, p->father, p->mother, p->child,
  440. p->karma, p->manner, p->fame,
  441. p->account_id, p->char_id) )
  442. {
  443. Sql_ShowDebug(sql_handle);
  444. }
  445. strcat(save_status, " status2");
  446. }
  447. //memo points
  448. if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
  449. {
  450. char esc_mapname[NAME_LENGTH*2+1];
  451. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  452. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, p->char_id) )
  453. Sql_ShowDebug(sql_handle);
  454. //insert here.
  455. StringBuf_Clear(&buf);
  456. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db);
  457. for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
  458. {
  459. if( p->memo_point[i].map )
  460. {
  461. if( count )
  462. StringBuf_AppendStr(&buf, ",");
  463. Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
  464. StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
  465. ++count;
  466. }
  467. }
  468. if( count )
  469. {
  470. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  471. Sql_ShowDebug(sql_handle);
  472. }
  473. strcat(save_status, " memo");
  474. }
  475. //FIXME: is this neccessary? [ultramage]
  476. for(i=0;i<MAX_SKILL;i++)
  477. if ((p->skill[i].lv != 0) && (p->skill[i].id == 0))
  478. p->skill[i].id = i; // Fix skill tree
  479. //skills
  480. if( memcmp(p->skill, cp->skill, sizeof(p->skill)) )
  481. {
  482. //`skill` (`char_id`, `id`, `lv`)
  483. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, p->char_id) )
  484. Sql_ShowDebug(sql_handle);
  485. StringBuf_Clear(&buf);
  486. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`) VALUES ", skill_db);
  487. //insert here.
  488. for( i = 0, count = 0; i < MAX_SKILL; ++i )
  489. {
  490. if(p->skill[i].id && p->skill[i].flag!=1)
  491. {
  492. if( count )
  493. StringBuf_AppendStr(&buf, ",");
  494. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->skill[i].id, (p->skill[i].flag == 0 ? p->skill[i].lv : p->skill[i].flag - 2));
  495. ++count;
  496. }
  497. }
  498. if( count )
  499. {
  500. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  501. Sql_ShowDebug(sql_handle);
  502. }
  503. strcat(save_status, " skills");
  504. }
  505. diff = 0;
  506. for(i = 0; i < MAX_FRIENDS; i++){
  507. if(p->friends[i].char_id != cp->friends[i].char_id ||
  508. p->friends[i].account_id != cp->friends[i].account_id){
  509. diff = 1;
  510. break;
  511. }
  512. }
  513. if(diff == 1)
  514. { //Save friends
  515. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id) )
  516. Sql_ShowDebug(sql_handle);
  517. StringBuf_Clear(&buf);
  518. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db);
  519. for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
  520. {
  521. if( p->friends[i].char_id > 0 )
  522. {
  523. if( count )
  524. StringBuf_AppendStr(&buf, ",");
  525. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id);
  526. count++;
  527. }
  528. }
  529. if( count )
  530. {
  531. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  532. Sql_ShowDebug(sql_handle);
  533. else
  534. strcat(save_status, " friends");
  535. }
  536. else //Friend list cleared.
  537. strcat(save_status, " friends");
  538. }
  539. #ifdef HOTKEY_SAVING
  540. // hotkeys
  541. StringBuf_Clear(&buf);
  542. StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", hotkey_db);
  543. diff = 0;
  544. for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
  545. if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
  546. {
  547. if( diff )
  548. StringBuf_AppendStr(&buf, ",");// not the first hotkey
  549. StringBuf_Printf(&buf, "('%d','%u','%u','%u','%u')", char_id, (unsigned int)i, (unsigned int)p->hotkeys[i].type, p->hotkeys[i].id , (unsigned int)p->hotkeys[i].lv);
  550. diff = 1;
  551. }
  552. }
  553. if(diff) {
  554. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  555. Sql_ShowDebug(sql_handle);
  556. else
  557. strcat(save_status, " hotkeys");
  558. }
  559. #endif
  560. StringBuf_Destroy(&buf);
  561. if (save_status[0]!='\0' && save_log)
  562. ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
  563. #ifndef TXT_SQL_CONVERT
  564. memcpy(cp, p, sizeof(struct mmo_charstatus));
  565. #else
  566. aFree(cp);
  567. #endif
  568. return 0;
  569. }
  570. /// Saves an array of 'item' entries into the specified table.
  571. int memitemdata_to_sql(const struct item items[], int max, int id, int tableswitch)
  572. {
  573. StringBuf buf;
  574. SqlStmt* stmt;
  575. int i;
  576. int j;
  577. const char* tablename;
  578. const char* selectoption;
  579. struct item item; // temp storage variable
  580. bool* flag; // bit array for inventory matching
  581. bool found;
  582. switch (tableswitch) {
  583. case TABLE_INVENTORY: tablename = inventory_db; selectoption = "char_id"; break;
  584. case TABLE_CART: tablename = cart_db; selectoption = "char_id"; break;
  585. case TABLE_STORAGE: tablename = storage_db; selectoption = "account_id"; break;
  586. case TABLE_GUILD_STORAGE: tablename = guild_storage_db; selectoption = "guild_id"; break;
  587. default:
  588. ShowError("Invalid table name!\n");
  589. return 1;
  590. }
  591. // The following code compares inventory with current database values
  592. // and performs modification/deletion/insertion only on relevant rows.
  593. // This approach is more complicated than a trivial delete&insert, but
  594. // it significantly reduces cpu load on the database server.
  595. StringBuf_Init(&buf);
  596. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  597. for( j = 0; j < MAX_SLOTS; ++j )
  598. StringBuf_Printf(&buf, ", `card%d`", j);
  599. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id);
  600. stmt = SqlStmt_Malloc(sql_handle);
  601. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  602. || SQL_ERROR == SqlStmt_Execute(stmt) )
  603. {
  604. SqlStmt_ShowDebug(stmt);
  605. SqlStmt_Free(stmt);
  606. StringBuf_Destroy(&buf);
  607. return 1;
  608. }
  609. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  610. SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL);
  611. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  612. SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL);
  613. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  614. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  615. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  616. for( j = 0; j < MAX_SLOTS; ++j )
  617. SqlStmt_BindColumn(stmt, 7+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL);
  618. // bit array indicating which inventory items have already been matched
  619. flag = (bool*) aCallocA(max, sizeof(bool));
  620. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  621. {
  622. found = false;
  623. // search for the presence of the item in the char's inventory
  624. for( i = 0; i < max; ++i )
  625. {
  626. // skip empty and already matched entries
  627. if( items[i].nameid == 0 || flag[i] )
  628. continue;
  629. if( items[i].nameid == item.nameid
  630. && items[i].card[0] == item.card[0]
  631. && items[i].card[2] == item.card[2]
  632. && items[i].card[3] == item.card[3]
  633. ) { //They are the same item.
  634. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  635. if( j == MAX_SLOTS &&
  636. items[i].amount == item.amount &&
  637. items[i].equip == item.equip &&
  638. items[i].identify == item.identify &&
  639. items[i].refine == item.refine &&
  640. items[i].attribute == item.attribute )
  641. ; //Do nothing.
  642. else
  643. {
  644. // update all fields.
  645. StringBuf_Clear(&buf);
  646. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d'",
  647. tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute);
  648. for( j = 0; j < MAX_SLOTS; ++j )
  649. StringBuf_Printf(&buf, ", `card%d`=%d", j, items[i].card[j]);
  650. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  651. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  652. Sql_ShowDebug(sql_handle);
  653. }
  654. found = flag[i] = true; //Item dealt with,
  655. break; //skip to next item in the db.
  656. }
  657. }
  658. if( !found )
  659. {// Item not present in inventory, remove it.
  660. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d'", tablename, item.id) )
  661. Sql_ShowDebug(sql_handle);
  662. }
  663. }
  664. SqlStmt_Free(stmt);
  665. StringBuf_Clear(&buf);
  666. StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`", tablename, selectoption);
  667. for( j = 0; j < MAX_SLOTS; ++j )
  668. StringBuf_Printf(&buf, ", `card%d`", j);
  669. StringBuf_AppendStr(&buf, ") VALUES ");
  670. found = false;
  671. // insert non-matched items into the db as new items
  672. for( i = 0; i < max; ++i )
  673. {
  674. // skip empty and already matched entries
  675. if( items[i].nameid == 0 || flag[i] )
  676. continue;
  677. if( found )
  678. StringBuf_AppendStr(&buf, ",");
  679. else
  680. found = true;
  681. StringBuf_Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d'",
  682. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute);
  683. for( j = 0; j < MAX_SLOTS; ++j )
  684. StringBuf_Printf(&buf, ", '%d'", items[i].card[j]);
  685. StringBuf_AppendStr(&buf, ")");
  686. }
  687. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  688. Sql_ShowDebug(sql_handle);
  689. StringBuf_Destroy(&buf);
  690. aFree(flag);
  691. return 0;
  692. }
  693. int mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p);
  694. #ifndef TXT_SQL_CONVERT
  695. //=====================================================================================================
  696. // Loads the basic character rooster for the given account. Returns total buffer used.
  697. int mmo_chars_fromsql(struct char_session_data* sd, uint8* buf)
  698. {
  699. SqlStmt* stmt;
  700. struct mmo_charstatus p;
  701. int j = 0, i;
  702. stmt = SqlStmt_Malloc(sql_handle);
  703. if( stmt == NULL )
  704. {
  705. SqlStmt_ShowDebug(stmt);
  706. return 0;
  707. }
  708. memset(&p, 0, sizeof(p));
  709. // read char data
  710. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  711. "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  712. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  713. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`,"
  714. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`"
  715. " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", char_db, sd->account_id, MAX_CHARS)
  716. || SQL_ERROR == SqlStmt_Execute(stmt)
  717. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL)
  718. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL)
  719. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL)
  720. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL)
  721. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL)
  722. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL)
  723. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p.base_exp, 0, NULL, NULL)
  724. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p.job_exp, 0, NULL, NULL)
  725. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL)
  726. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL)
  727. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL)
  728. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL)
  729. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL)
  730. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL)
  731. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL)
  732. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_INT, &p.max_hp, 0, NULL, NULL)
  733. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p.hp, 0, NULL, NULL)
  734. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p.max_sp, 0, NULL, NULL)
  735. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p.sp, 0, NULL, NULL)
  736. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_USHORT, &p.status_point, 0, NULL, NULL)
  737. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_USHORT, &p.skill_point, 0, NULL, NULL)
  738. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL)
  739. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL)
  740. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL)
  741. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL)
  742. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL)
  743. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL)
  744. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.weapon, 0, NULL, NULL)
  745. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.shield, 0, NULL, NULL)
  746. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.head_top, 0, NULL, NULL)
  747. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL)
  748. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL)
  749. )
  750. {
  751. SqlStmt_ShowDebug(stmt);
  752. SqlStmt_Free(stmt);
  753. return 0;
  754. }
  755. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ )
  756. {
  757. sd->found_char[i] = p.char_id;
  758. j += mmo_char_tobuf(WBUFP(buf, j), &p);
  759. }
  760. for( ; i < MAX_CHARS; i++ )
  761. sd->found_char[i] = -1;
  762. SqlStmt_Free(stmt);
  763. return j;
  764. }
  765. //=====================================================================================================
  766. int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything)
  767. {
  768. int i,j;
  769. char t_msg[128] = "";
  770. struct mmo_charstatus* cp;
  771. StringBuf buf;
  772. SqlStmt* stmt;
  773. char last_map[MAP_NAME_LENGTH_EXT];
  774. char save_map[MAP_NAME_LENGTH_EXT];
  775. char point_map[MAP_NAME_LENGTH_EXT];
  776. struct point tmp_point;
  777. struct item tmp_item;
  778. struct s_skill tmp_skill;
  779. struct s_friend tmp_friend;
  780. #ifdef HOTKEY_SAVING
  781. struct hotkey tmp_hotkey;
  782. int hotkey_num;
  783. #endif
  784. memset(p, 0, sizeof(struct mmo_charstatus));
  785. if (save_log) ShowInfo("Char load request (%d)\n", char_id);
  786. stmt = SqlStmt_Malloc(sql_handle);
  787. if( stmt == NULL )
  788. {
  789. SqlStmt_ShowDebug(stmt);
  790. return 0;
  791. }
  792. // read char data
  793. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  794. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  795. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  796. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`hair`,"
  797. "`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  798. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`"
  799. " FROM `%s` WHERE `char_id`=? LIMIT 1", char_db)
  800. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  801. || SQL_ERROR == SqlStmt_Execute(stmt)
  802. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL)
  803. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL)
  804. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL)
  805. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL)
  806. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL)
  807. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL)
  808. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL)
  809. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p->base_exp, 0, NULL, NULL)
  810. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &p->job_exp, 0, NULL, NULL)
  811. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL)
  812. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL)
  813. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL)
  814. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL)
  815. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL)
  816. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL)
  817. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL)
  818. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p->max_hp, 0, NULL, NULL)
  819. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p->hp, 0, NULL, NULL)
  820. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p->max_sp, 0, NULL, NULL)
  821. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_INT, &p->sp, 0, NULL, NULL)
  822. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_USHORT, &p->status_point, 0, NULL, NULL)
  823. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_USHORT, &p->skill_point, 0, NULL, NULL)
  824. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL)
  825. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL)
  826. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL)
  827. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL)
  828. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL)
  829. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL)
  830. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL)
  831. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p->hair, 0, NULL, NULL)
  832. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL)
  833. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL)
  834. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->weapon, 0, NULL, NULL)
  835. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->shield, 0, NULL, NULL)
  836. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->head_top, 0, NULL, NULL)
  837. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL)
  838. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL)
  839. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  840. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL)
  841. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL)
  842. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL)
  843. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL)
  844. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL)
  845. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_INT, &p->partner_id, 0, NULL, NULL)
  846. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_INT, &p->father, 0, NULL, NULL)
  847. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_INT, &p->mother, 0, NULL, NULL)
  848. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_INT, &p->child, 0, NULL, NULL)
  849. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_INT, &p->fame, 0, NULL, NULL) )
  850. {
  851. SqlStmt_ShowDebug(stmt);
  852. SqlStmt_Free(stmt);
  853. return 0;
  854. }
  855. if( SQL_ERROR == SqlStmt_NextRow(stmt) )
  856. {
  857. ShowError("Requested non-existant character id: %d!\n", char_id);
  858. SqlStmt_Free(stmt);
  859. return 0;
  860. }
  861. p->last_point.map = mapindex_name2id(last_map);
  862. p->save_point.map = mapindex_name2id(save_map);
  863. strcat(t_msg, " status");
  864. if (!load_everything) // For quick selection of data when displaying the char menu
  865. {
  866. SqlStmt_Free(stmt);
  867. return 1;
  868. }
  869. //read memo data
  870. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  871. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", memo_db, MAX_MEMOPOINTS)
  872. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  873. || SQL_ERROR == SqlStmt_Execute(stmt)
  874. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL)
  875. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL)
  876. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) )
  877. SqlStmt_ShowDebug(stmt);
  878. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  879. {
  880. tmp_point.map = mapindex_name2id(point_map);
  881. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  882. }
  883. strcat(t_msg, " memo");
  884. //read inventory
  885. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  886. StringBuf_Init(&buf);
  887. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  888. for( i = 0; i < MAX_SLOTS; ++i )
  889. StringBuf_Printf(&buf, ", `card%d`", i);
  890. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", inventory_db, MAX_INVENTORY);
  891. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  892. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  893. || SQL_ERROR == SqlStmt_Execute(stmt)
  894. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  895. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  896. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  897. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  898. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  899. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  900. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL) )
  901. SqlStmt_ShowDebug(stmt);
  902. for( i = 0; i < MAX_SLOTS; ++i )
  903. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 7+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  904. SqlStmt_ShowDebug(stmt);
  905. for( i = 0; i < MAX_INVENTORY && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  906. memcpy(&p->inventory[i], &tmp_item, sizeof(tmp_item));
  907. strcat(t_msg, " inventory");
  908. //read cart
  909. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  910. StringBuf_Clear(&buf);
  911. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  912. for( j = 0; j < MAX_SLOTS; ++j )
  913. StringBuf_Printf(&buf, ", `card%d`", j);
  914. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", cart_db, MAX_CART);
  915. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  916. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  917. || SQL_ERROR == SqlStmt_Execute(stmt)
  918. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  919. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  920. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  921. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  922. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  923. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  924. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL) )
  925. SqlStmt_ShowDebug(stmt);
  926. for( i = 0; i < MAX_SLOTS; ++i )
  927. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 7+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  928. SqlStmt_ShowDebug(stmt);
  929. for( i = 0; i < MAX_CART && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  930. memcpy(&p->cart[i], &tmp_item, sizeof(tmp_item));
  931. strcat(t_msg, " cart");
  932. //read storage
  933. storage_fromsql(p->account_id, &p->storage);
  934. strcat(t_msg, " storage");
  935. //read skill
  936. //`skill` (`char_id`, `id`, `lv`)
  937. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`=? LIMIT %d", skill_db, MAX_SKILL)
  938. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  939. || SQL_ERROR == SqlStmt_Execute(stmt)
  940. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_USHORT, &tmp_skill.id, 0, NULL, NULL)
  941. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &tmp_skill.lv, 0, NULL, NULL) )
  942. SqlStmt_ShowDebug(stmt);
  943. tmp_skill.flag = 0;
  944. for( i = 0; i < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  945. {
  946. if( tmp_skill.id < ARRAYLENGTH(p->skill) )
  947. memcpy(&p->skill[tmp_skill.id], &tmp_skill, sizeof(tmp_skill));
  948. else
  949. ShowWarning("mmo_char_fromsql: ignoring invalid skill (id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", tmp_skill.id, tmp_skill.lv, p->name, p->account_id, p->char_id);
  950. }
  951. strcat(t_msg, " skills");
  952. //read friends
  953. //`friends` (`char_id`, `friend_account`, `friend_id`)
  954. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT c.`account_id`, c.`char_id`, c.`name` FROM `%s` c LEFT JOIN `%s` f ON f.`friend_account` = c.`account_id` AND f.`friend_id` = c.`char_id` WHERE f.`char_id`=? LIMIT %d", char_db, friend_db, MAX_FRIENDS)
  955. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  956. || SQL_ERROR == SqlStmt_Execute(stmt)
  957. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL)
  958. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL)
  959. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) )
  960. SqlStmt_ShowDebug(stmt);
  961. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  962. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  963. strcat(t_msg, " friends");
  964. #ifdef HOTKEY_SAVING
  965. //read hotkeys
  966. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  967. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", hotkey_db)
  968. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  969. || SQL_ERROR == SqlStmt_Execute(stmt)
  970. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL)
  971. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL)
  972. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL)
  973. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) )
  974. SqlStmt_ShowDebug(stmt);
  975. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  976. {
  977. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS )
  978. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  979. else
  980. ShowWarning("mmo_char_fromsql: ignoring invalid hotkey (hotkey=%d,type=%u,id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", hotkey_num, tmp_hotkey.type, tmp_hotkey.id, tmp_hotkey.lv, p->name, p->account_id, p->char_id);
  981. }
  982. strcat(t_msg, " hotkeys");
  983. #endif
  984. if (save_log) ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  985. SqlStmt_Free(stmt);
  986. StringBuf_Destroy(&buf);
  987. cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus);
  988. memcpy(cp, p, sizeof(struct mmo_charstatus));
  989. return 1;
  990. }
  991. //==========================================================================================================
  992. int mmo_char_sql_init(void)
  993. {
  994. ShowInfo("Begin Initializing.......\n");
  995. char_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  996. if(char_per_account == 0){
  997. ShowStatus("Chars per Account: 'Unlimited'.......\n");
  998. }else{
  999. ShowStatus("Chars per Account: '%d'.......\n", char_per_account);
  1000. }
  1001. //the 'set offline' part is now in check_login_conn ...
  1002. //if the server connects to loginserver
  1003. //it will dc all off players
  1004. //and send the loginserver the new state....
  1005. // Force all users offline in sql when starting char-server
  1006. // (useful when servers crashs and don't clean the database)
  1007. set_all_offline_sql();
  1008. ShowInfo("Finished initilizing.......\n");
  1009. return 0;
  1010. }
  1011. //-----------------------------------
  1012. // Function to create a new character
  1013. //-----------------------------------
  1014. int make_new_char_sql(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style)
  1015. {
  1016. char name[NAME_LENGTH];
  1017. char esc_name[NAME_LENGTH*2+1];
  1018. unsigned int i;
  1019. int char_id;
  1020. safestrncpy(name, name_, NAME_LENGTH);
  1021. normalize_name(name,TRIM_CHARS);
  1022. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1023. // check length of character name
  1024. if( name[0] == '\0' )
  1025. return -2; // empty character name
  1026. // check content of character name
  1027. if( remove_control_chars(name) )
  1028. return -2; // control chars in name
  1029. // check for reserved names
  1030. if( strcmpi(name, main_chat_nick) == 0 || strcmpi(name, wisp_server_name) == 0 )
  1031. return -1; // nick reserved for internal server messages
  1032. // Check Authorised letters/symbols in the name of the character
  1033. if( char_name_option == 1 ) { // only letters/symbols in char_name_letters are authorised
  1034. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1035. if( strchr(char_name_letters, name[i]) == NULL )
  1036. return -2;
  1037. } else
  1038. if( char_name_option == 2 ) { // letters/symbols in char_name_letters are forbidden
  1039. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1040. if( strchr(char_name_letters, name[i]) != NULL )
  1041. return -2;
  1042. } // else, all letters/symbols are authorised (except control char removed before)
  1043. // check name (already in use?)
  1044. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s'", char_db, esc_name) ) {
  1045. Sql_ShowDebug(sql_handle);
  1046. return -2;
  1047. }
  1048. if( Sql_NumRows(sql_handle) > 0 )
  1049. return -1; // name already exists
  1050. //check other inputs
  1051. if((slot >= MAX_CHARS) // slots
  1052. || (hair_style >= 24) // hair style
  1053. || (hair_color >= 9) // hair color
  1054. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1055. || (str < 1 || str > 9 || agi < 1 || agi > 9 || vit < 1 || vit > 9 || int_ < 1 || int_ > 9 || dex < 1 || dex > 9 || luk < 1 || luk > 9) // individual stat values
  1056. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1057. return -2; // invalid input
  1058. // check the number of already existing chars in this account
  1059. if( char_per_account != 0 ) {
  1060. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id) )
  1061. Sql_ShowDebug(sql_handle);
  1062. if( Sql_NumRows(sql_handle) >= char_per_account )
  1063. return -2; // character account limit exceeded
  1064. }
  1065. // check char slot
  1066. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d'", char_db, sd->account_id, slot) )
  1067. Sql_ShowDebug(sql_handle);
  1068. if( Sql_NumRows(sql_handle) > 0 )
  1069. return -2; // slot already in use
  1070. // validation success, log result
  1071. if (log_char) {
  1072. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1073. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1074. charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1075. Sql_ShowDebug(sql_handle);
  1076. }
  1077. //Insert the new char entry to the database
  1078. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1079. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1080. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1081. char_db, sd->account_id , slot, esc_name, start_zeny, str, agi, vit, int_, dex, luk,
  1082. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1083. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y) )
  1084. {
  1085. Sql_ShowDebug(sql_handle);
  1086. return -2; //No, stop the procedure!
  1087. }
  1088. //Retrieve the newly auto-generated char id
  1089. char_id = (int)Sql_LastInsertId(sql_handle);
  1090. //Give the char the default items
  1091. if (start_weapon > 0) { //add Start Weapon (Knife?)
  1092. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_weapon, 1, 1) )
  1093. Sql_ShowDebug(sql_handle);
  1094. }
  1095. if (start_armor > 0) { //Add default armor (cotton shirt?)
  1096. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_armor, 1, 1) )
  1097. Sql_ShowDebug(sql_handle);
  1098. }
  1099. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1100. return char_id;
  1101. }
  1102. /*----------------------------------------------------------------------------------------------------------*/
  1103. /* Divorce Players */
  1104. /*----------------------------------------------------------------------------------------------------------*/
  1105. int divorce_char_sql(int partner_id1, int partner_id2)
  1106. {
  1107. unsigned char buf[64];
  1108. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d'", char_db, partner_id1, partner_id2) )
  1109. Sql_ShowDebug(sql_handle);
  1110. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND (`char_id`='%d' OR `char_id`='%d')", inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) )
  1111. Sql_ShowDebug(sql_handle);
  1112. WBUFW(buf,0) = 0x2b12;
  1113. WBUFL(buf,2) = partner_id1;
  1114. WBUFL(buf,6) = partner_id2;
  1115. mapif_sendall(buf,10);
  1116. return 0;
  1117. }
  1118. /*----------------------------------------------------------------------------------------------------------*/
  1119. /* Delete char - davidsiaw */
  1120. /*----------------------------------------------------------------------------------------------------------*/
  1121. /* Returns 0 if successful
  1122. * Returns < 0 for error
  1123. */
  1124. int delete_char_sql(int char_id)
  1125. {
  1126. char name[NAME_LENGTH];
  1127. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1128. int account_id, party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id;
  1129. char* data;
  1130. size_t len;
  1131. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name`,`account_id`,`party_id`,`guild_id`,`base_level`,`homun_id`,`partner_id`,`father`,`mother` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  1132. Sql_ShowDebug(sql_handle);
  1133. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1134. {
  1135. ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n");
  1136. Sql_FreeResult(sql_handle);
  1137. return -1;
  1138. }
  1139. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1140. Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data);
  1141. Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data);
  1142. Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data);
  1143. Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data);
  1144. Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data);
  1145. Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data);
  1146. Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data);
  1147. Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data);
  1148. Sql_EscapeStringLen(sql_handle, esc_name, name, min(len, NAME_LENGTH));
  1149. Sql_FreeResult(sql_handle);
  1150. //check for config char del condition [Lupus]
  1151. if( ( char_del_level > 0 && base_level >= char_del_level )
  1152. || ( char_del_level < 0 && base_level <= -char_del_level )
  1153. ) {
  1154. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1155. return -1;
  1156. }
  1157. /* Divorce [Wizputer] */
  1158. if( partner_id )
  1159. divorce_char_sql(char_id, partner_id);
  1160. /* De-addopt [Zephyrus] */
  1161. if( father_id || mother_id )
  1162. { // Char is Baby
  1163. unsigned char buf[64];
  1164. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", char_db, father_id, mother_id) )
  1165. Sql_ShowDebug(sql_handle);
  1166. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410'AND (`char_id`='%d' OR `char_id`='%d')", skill_db, father_id, mother_id) )
  1167. Sql_ShowDebug(sql_handle);
  1168. WBUFW(buf,0) = 0x2b25;
  1169. WBUFL(buf,2) = father_id;
  1170. WBUFL(buf,6) = mother_id;
  1171. WBUFL(buf,10) = char_id; // Baby
  1172. mapif_sendall(buf,14);
  1173. }
  1174. //Make the character leave the party [Skotlex]
  1175. if (party_id)
  1176. inter_party_leave(party_id, account_id, char_id);
  1177. /* delete char's pet */
  1178. //Delete the hatched pet if you have one...
  1179. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incuvate` = '0'", pet_db, char_id) )
  1180. Sql_ShowDebug(sql_handle);
  1181. //Delete all pets that are stored in eggs (inventory + cart)
  1182. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE pet_id IN "
  1183. "(SELECT card1|card2<<2 FROM `%s` WHERE char_id = '%d' AND card0 = -256"
  1184. " UNION"
  1185. " SELECT card1|card2<<2 FROM `%s` WHERE char_id = '%d' AND card0 = -256)",
  1186. pet_db, inventory_db, char_id, cart_db, char_id) )
  1187. Sql_ShowDebug(sql_handle);
  1188. /* remove homunculus */
  1189. if (hom_id)
  1190. mapif_homunculus_delete(hom_id);
  1191. /* delete char's friends list */
  1192. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", friend_db, char_id) )
  1193. Sql_ShowDebug(sql_handle);
  1194. /* delete char from other's friend list */
  1195. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1196. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", friend_db, char_id) )
  1197. Sql_ShowDebug(sql_handle);
  1198. #ifdef HOTKEY_SAVING
  1199. /* delete hotkeys */
  1200. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", hotkey_db, char_id) )
  1201. Sql_ShowDebug(sql_handle);
  1202. #endif
  1203. /* delete inventory */
  1204. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", inventory_db, char_id) )
  1205. Sql_ShowDebug(sql_handle);
  1206. /* delete cart inventory */
  1207. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", cart_db, char_id) )
  1208. Sql_ShowDebug(sql_handle);
  1209. /* delete memo areas */
  1210. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, char_id) )
  1211. Sql_ShowDebug(sql_handle);
  1212. /* delete character registry */
  1213. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'", reg_db, char_id) )
  1214. Sql_ShowDebug(sql_handle);
  1215. /* delete skills */
  1216. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, char_id) )
  1217. Sql_ShowDebug(sql_handle);
  1218. #ifdef ENABLE_SC_SAVING
  1219. /* status changes */
  1220. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, account_id, char_id) )
  1221. Sql_ShowDebug(sql_handle);
  1222. #endif
  1223. if (log_char) {
  1224. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`char_msg`,`name`) VALUES (NOW(), '%d', '%d', 'Deleted char (CID %d)', '%s')",
  1225. charlog_db, account_id, 0, char_id, esc_name) )
  1226. Sql_ShowDebug(sql_handle);
  1227. }
  1228. /* delete character */
  1229. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  1230. Sql_ShowDebug(sql_handle);
  1231. /* No need as we used inter_guild_leave [Skotlex]
  1232. // Also delete info from guildtables.
  1233. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1234. Sql_ShowDebug(sql_handle);
  1235. */
  1236. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `master` = '%s'", guild_db, esc_name) )
  1237. Sql_ShowDebug(sql_handle);
  1238. else if( Sql_NumRows(sql_handle) > 0 )
  1239. mapif_parse_BreakGuild(0,guild_id);
  1240. else if( guild_id )
  1241. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1242. return 0;
  1243. }
  1244. //==========================================================================================================
  1245. int count_users(void)
  1246. {
  1247. int i, users;
  1248. if (login_fd > 0 && session[login_fd]){
  1249. users = 0;
  1250. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  1251. if (server[i].fd > 0) {
  1252. users += server[i].users;
  1253. }
  1254. }
  1255. return users;
  1256. }
  1257. return 0;
  1258. }
  1259. /// Writes char data to the buffer in the format used by the client.
  1260. /// Used in packets 0x6b (chars info) and 0x6d (new char info)
  1261. /// Returns the size (106 or 108)
  1262. int mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p)
  1263. {
  1264. if( buf == NULL || p == NULL )
  1265. return 0;
  1266. WBUFL(buf,0) = p->char_id;
  1267. WBUFL(buf,4) = min(p->base_exp, LONG_MAX);
  1268. WBUFL(buf,8) = p->zeny;
  1269. WBUFL(buf,12) = min(p->job_exp, LONG_MAX);
  1270. WBUFL(buf,16) = p->job_level;
  1271. WBUFL(buf,20) = 0; // probably opt1
  1272. WBUFL(buf,24) = 0; // probably opt2
  1273. WBUFL(buf,28) = p->option;
  1274. WBUFL(buf,32) = p->karma;
  1275. WBUFL(buf,36) = p->manner;
  1276. WBUFW(buf,40) = min(p->status_point, SHRT_MAX);
  1277. WBUFW(buf,42) = min(p->hp, SHRT_MAX);
  1278. WBUFW(buf,44) = min(p->max_hp, SHRT_MAX);
  1279. WBUFW(buf,46) = min(p->sp, SHRT_MAX);
  1280. WBUFW(buf,48) = min(p->max_sp, SHRT_MAX);
  1281. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1282. WBUFW(buf,52) = p->class_;
  1283. WBUFW(buf,54) = p->hair;
  1284. WBUFW(buf,56) = p->option&0x20 ? 0 : p->weapon; //When the weapon is sent and your option is riding, the client crashes on login!?
  1285. WBUFW(buf,58) = p->base_level;
  1286. WBUFW(buf,60) = min(p->skill_point, SHRT_MAX);
  1287. WBUFW(buf,62) = p->head_bottom;
  1288. WBUFW(buf,64) = p->shield;
  1289. WBUFW(buf,66) = p->head_top;
  1290. WBUFW(buf,68) = p->head_mid;
  1291. WBUFW(buf,70) = p->hair_color;
  1292. WBUFW(buf,72) = p->clothes_color;
  1293. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1294. WBUFB(buf,98) = min(p->str, UCHAR_MAX);
  1295. WBUFB(buf,99) = min(p->agi, UCHAR_MAX);
  1296. WBUFB(buf,100) = min(p->vit, UCHAR_MAX);
  1297. WBUFB(buf,101) = min(p->int_, UCHAR_MAX);
  1298. WBUFB(buf,102) = min(p->dex, UCHAR_MAX);
  1299. WBUFB(buf,103) = min(p->luk, UCHAR_MAX);
  1300. WBUFW(buf,104) = p->slot;
  1301. if (char_rename) {
  1302. WBUFW(buf,106) = 1;// Rename bit (0=rename,1=no rename)
  1303. return 108;
  1304. } else {
  1305. return 106;
  1306. }
  1307. }
  1308. int mmo_char_send006b(int fd, struct char_session_data* sd)
  1309. {
  1310. int j;
  1311. if (save_log)
  1312. ShowInfo("Loading Char Data ("CL_BOLD"%d"CL_RESET")\n",sd->account_id);
  1313. j = 24; // offset
  1314. WFIFOHEAD(fd,j + MAX_CHARS*108); // or 106(!)
  1315. WFIFOW(fd,0) = 0x6b;
  1316. memset(WFIFOP(fd,4), 0, 20); // unknown bytes
  1317. j+=mmo_chars_fromsql(sd, WFIFOP(fd,j));
  1318. WFIFOW(fd,2) = j; // packet len
  1319. WFIFOSET(fd,j);
  1320. return 0;
  1321. }
  1322. int char_married(int pl1, int pl2)
  1323. {
  1324. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", char_db, pl1) )
  1325. Sql_ShowDebug(sql_handle);
  1326. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1327. {
  1328. char* data;
  1329. Sql_GetData(sql_handle, 0, &data, NULL);
  1330. if( pl2 == atoi(data) )
  1331. {
  1332. Sql_FreeResult(sql_handle);
  1333. return 1;
  1334. }
  1335. }
  1336. Sql_FreeResult(sql_handle);
  1337. return 0;
  1338. }
  1339. int char_child(int parent_id, int child_id)
  1340. {
  1341. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", char_db, parent_id) )
  1342. Sql_ShowDebug(sql_handle);
  1343. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1344. {
  1345. char* data;
  1346. Sql_GetData(sql_handle, 0, &data, NULL);
  1347. if( child_id == atoi(data) )
  1348. {
  1349. Sql_FreeResult(sql_handle);
  1350. return 1;
  1351. }
  1352. }
  1353. Sql_FreeResult(sql_handle);
  1354. return 0;
  1355. }
  1356. int char_family(int pl1, int pl2, int pl3)
  1357. {
  1358. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", char_db, pl1, pl2, pl3) )
  1359. Sql_ShowDebug(sql_handle);
  1360. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1361. {
  1362. int charid;
  1363. int partnerid;
  1364. int childid;
  1365. char* data;
  1366. Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data);
  1367. Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data);
  1368. Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data);
  1369. if( (pl1 == charid && ((pl2 == partnerid && pl3 == childid ) || (pl2 == childid && pl3 == partnerid))) ||
  1370. (pl1 == partnerid && ((pl2 == charid && pl3 == childid ) || (pl2 == childid && pl3 == charid ))) ||
  1371. (pl1 == childid && ((pl2 == charid && pl3 == partnerid) || (pl2 == partnerid && pl3 == charid ))) )
  1372. {
  1373. Sql_FreeResult(sql_handle);
  1374. return childid;
  1375. }
  1376. }
  1377. Sql_FreeResult(sql_handle);
  1378. return 0;
  1379. }
  1380. static void char_auth_ok(int fd, struct char_session_data *sd)
  1381. {
  1382. struct online_char_data* character;
  1383. if (max_connect_user && count_users() >= max_connect_user && sd->gmlevel < gm_allow_level)
  1384. {
  1385. // refuse connection (over populated)
  1386. WFIFOW(fd,0) = 0x6c;
  1387. WFIFOW(fd,2) = 0;
  1388. WFIFOSET(fd,3);
  1389. return;
  1390. }
  1391. if( online_check && (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1392. { // check if character is not online already. [Skotlex]
  1393. if (character->server > -1)
  1394. { //Character already online. KICK KICK KICK
  1395. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  1396. if (character->waiting_disconnect == -1)
  1397. character->waiting_disconnect = add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  1398. WFIFOW(fd,0) = 0x81;
  1399. WFIFOB(fd,2) = 8;
  1400. WFIFOSET(fd,3);
  1401. return;
  1402. }
  1403. if (character->fd >= 0 && character->fd != fd)
  1404. { //There's already a connection from this account that hasn't picked a char yet.
  1405. WFIFOW(fd,0) = 0x81;
  1406. WFIFOB(fd,2) = 8;
  1407. WFIFOSET(fd,3);
  1408. return;
  1409. }
  1410. character->fd = fd;
  1411. }
  1412. if (login_fd > 0) {
  1413. // request to login-server to obtain e-mail/time limit
  1414. WFIFOHEAD(login_fd,6);
  1415. WFIFOW(login_fd,0) = 0x2716;
  1416. WFIFOL(login_fd,2) = sd->account_id;
  1417. WFIFOSET(login_fd,6);
  1418. }
  1419. // mark session as 'authed'
  1420. sd->auth = true;
  1421. // set char online on charserver
  1422. set_char_charselect(sd->account_id);
  1423. // send characters to player
  1424. mmo_char_send006b(fd, sd);
  1425. }
  1426. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr data);
  1427. int parse_fromlogin(int fd)
  1428. {
  1429. int i;
  1430. struct char_session_data *sd;
  1431. // only login-server can have an access to here.
  1432. // so, if it isn't the login-server, we disconnect the session.
  1433. if( fd != login_fd )
  1434. set_eof(fd);
  1435. if(session[fd]->flag.eof) {
  1436. if (fd == login_fd) {
  1437. ShowWarning("Connection to login-server lost (connection #%d).\n", fd);
  1438. login_fd = -1;
  1439. }
  1440. do_close(fd);
  1441. return 0;
  1442. }
  1443. sd = (struct char_session_data*)session[fd]->session_data;
  1444. while(RFIFOREST(fd) >= 2)
  1445. {
  1446. uint16 command = RFIFOW(fd,0);
  1447. switch( command )
  1448. {
  1449. // acknowledgement of connect-to-loginserver request
  1450. case 0x2711:
  1451. if (RFIFOREST(fd) < 3)
  1452. return 0;
  1453. if (RFIFOB(fd,2)) {
  1454. //printf("connect login server error : %d\n", RFIFOB(fd, 2));
  1455. ShowError("Can not connect to login-server.\n");
  1456. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1457. ShowError("Also, please make sure your login db has the correct communication username/passwords and the gender of the account is S.\n");
  1458. ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n");
  1459. }else {
  1460. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1461. //Send online accounts to login server.
  1462. send_accounts_tologin(-1, gettick(), 0, 0);
  1463. // if no map-server already connected, display a message...
  1464. ARR_FIND( 0, MAX_MAP_SERVERS, i, server[i].fd > 0 && server[i].map[0] );
  1465. if( i == MAX_MAP_SERVERS )
  1466. ShowStatus("Awaiting maps from map-server.\n");
  1467. }
  1468. RFIFOSKIP(fd,3);
  1469. break;
  1470. // acknowledgement of account authentication request
  1471. case 0x2713:
  1472. if (RFIFOREST(fd) < 60)
  1473. return 0;
  1474. {
  1475. int account_id = RFIFOL(fd,2);
  1476. int login_id1 = RFIFOL(fd,6);
  1477. int login_id2 = RFIFOL(fd,10);
  1478. bool result = RFIFOB(fd,14);
  1479. const char* email = (const char*)RFIFOP(fd,15);
  1480. time_t expiration_time = (time_t)RFIFOL(fd,55);
  1481. int gmlevel = RFIFOB(fd,59);
  1482. // find the session with this account id
  1483. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) &&
  1484. sd->account_id == account_id && sd->login_id1 == login_id1 && sd->login_id2 == login_id2 );
  1485. if( i < fd_max )
  1486. {
  1487. if( result ) { // failure
  1488. WFIFOHEAD(i,3);
  1489. WFIFOW(i,0) = 0x6c;
  1490. WFIFOB(i,2) = 0x42;
  1491. WFIFOSET(i,3);
  1492. } else { // success
  1493. memcpy(sd->email, email, 40);
  1494. sd->expiration_time = expiration_time;
  1495. sd->gmlevel = gmlevel;
  1496. char_auth_ok(i, sd);
  1497. }
  1498. }
  1499. }
  1500. RFIFOSKIP(fd,60);
  1501. break;
  1502. // acknowledgement of e-mail/limited time request
  1503. case 0x2717:
  1504. if (RFIFOREST(fd) < 51)
  1505. return 0;
  1506. // find the session with this account id
  1507. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == RFIFOL(fd,2) );
  1508. if( i < fd_max )
  1509. {
  1510. memcpy(sd->email, RFIFOP(fd,6), 40);
  1511. sd->expiration_time = (time_t)RFIFOL(fd,46);
  1512. sd->gmlevel = RFIFOB(fd,50);
  1513. }
  1514. RFIFOSKIP(fd,51);
  1515. break;
  1516. // login-server alive packet
  1517. case 0x2718:
  1518. if (RFIFOREST(fd) < 2)
  1519. return 0;
  1520. RFIFOSKIP(fd,2);
  1521. break;
  1522. // changesex reply
  1523. case 0x2723:
  1524. if (RFIFOREST(fd) < 7)
  1525. return 0;
  1526. {
  1527. unsigned char buf[16];
  1528. int acc = RFIFOL(fd,2);
  1529. int sex = RFIFOB(fd,6);
  1530. RFIFOSKIP(fd,7);
  1531. if( acc > 0 )
  1532. {
  1533. int char_id[MAX_CHARS];
  1534. int class_[MAX_CHARS];
  1535. int guild_id[MAX_CHARS];
  1536. int num;
  1537. int i;
  1538. char* data;
  1539. struct auth_node* node = (struct auth_node*)idb_get(auth_db, acc);
  1540. if( node != NULL )
  1541. node->sex = sex;
  1542. // get characters
  1543. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`class`,`guild_id` FROM `%s` WHERE `account_id` = '%d'", char_db, acc) )
  1544. Sql_ShowDebug(sql_handle);
  1545. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1546. {
  1547. Sql_GetData(sql_handle, 0, &data, NULL); char_id[i] = atoi(data);
  1548. Sql_GetData(sql_handle, 1, &data, NULL); class_[i] = atoi(data);
  1549. Sql_GetData(sql_handle, 2, &data, NULL); guild_id[i] = atoi(data);
  1550. }
  1551. num = i;
  1552. for( i = 0; i < num; ++i )
  1553. {
  1554. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER ||
  1555. class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY ||
  1556. class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER )
  1557. {
  1558. // job modification
  1559. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER )
  1560. class_[i] = (sex ? JOB_BARD : JOB_DANCER);
  1561. else if( class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY )
  1562. class_[i] = (sex ? JOB_CLOWN : JOB_GYPSY);
  1563. else if( class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER )
  1564. class_[i] = (sex ? JOB_BABY_BARD : JOB_BABY_DANCER);
  1565. // remove specifical skills of classes 19,20 4020,4021 and 4042,4043
  1566. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `skill_point` = `skill_point` +"
  1567. " (SELECT SUM(lv) FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330' AND `lv` > '0')"
  1568. " WHERE `char_id` = '%d'",
  1569. char_db, skill_db, char_id[i], char_id[i]) )
  1570. Sql_ShowDebug(sql_handle);
  1571. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'", skill_db, char_id[i]) )
  1572. Sql_ShowDebug(sql_handle);
  1573. }
  1574. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  1575. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `equip` = '0' WHERE `char_id` = '%d'", inventory_db, char_id[i]) )
  1576. Sql_ShowDebug(sql_handle);
  1577. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d', `weapon`='0', `shield`='0', `head_top`='0', `head_mid`='0', `head_bottom`='0' WHERE `char_id`='%d'", char_db, class_[i], char_id[i]) )
  1578. Sql_ShowDebug(sql_handle);
  1579. if( guild_id[i] )// If there is a guild, update the guild_member data [Skotlex]
  1580. inter_guild_sex_changed(guild_id[i], acc, char_id[i], sex);
  1581. }
  1582. Sql_FreeResult(sql_handle);
  1583. }
  1584. // disconnect player if online on char-server
  1585. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == acc );
  1586. if( i < fd_max )
  1587. set_eof(i);
  1588. // notify all mapservers about this change
  1589. WBUFW(buf,0) = 0x2b0d;
  1590. WBUFL(buf,2) = acc;
  1591. WBUFB(buf,6) = sex;
  1592. mapif_sendall(buf, 7);
  1593. }
  1594. break;
  1595. // reply to an account_reg2 registry request
  1596. case 0x2729:
  1597. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1598. return 0;
  1599. { //Receive account_reg2 registry, forward to map servers.
  1600. unsigned char buf[13+ACCOUNT_REG2_NUM*sizeof(struct global_reg)];
  1601. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  1602. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  1603. mapif_sendall(buf, WBUFW(buf,2));
  1604. }
  1605. RFIFOSKIP(fd, RFIFOW(fd,2));
  1606. break;
  1607. // State change of account/ban notification (from login-server)
  1608. case 0x2731:
  1609. if (RFIFOREST(fd) < 11)
  1610. return 0;
  1611. { // send to all map-servers to disconnect the player
  1612. unsigned char buf[11];
  1613. WBUFW(buf,0) = 0x2b14;
  1614. WBUFL(buf,2) = RFIFOL(fd,2);
  1615. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  1616. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  1617. mapif_sendall(buf, 11);
  1618. }
  1619. // disconnect player if online on char-server
  1620. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == RFIFOL(fd,2) );
  1621. if( i < fd_max )
  1622. set_eof(i);
  1623. RFIFOSKIP(fd,11);
  1624. break;
  1625. // Login server request to kick a character out. [Skotlex]
  1626. case 0x2734:
  1627. if (RFIFOREST(fd) < 6)
  1628. return 0;
  1629. {
  1630. int aid = RFIFOL(fd,2);
  1631. struct online_char_data* character = (struct online_char_data*)idb_get(online_char_db, aid);
  1632. RFIFOSKIP(fd,6);
  1633. if( character != NULL )
  1634. {// account is already marked as online!
  1635. if( character->server > -1 )
  1636. { //Kick it from the map server it is on.
  1637. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  1638. if (character->waiting_disconnect == -1)
  1639. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, chardb_waiting_disconnect, character->account_id, 0);
  1640. }
  1641. else
  1642. {// Manual kick from char server.
  1643. struct char_session_data *tsd;
  1644. int i;
  1645. ARR_FIND( 0, fd_max, i, session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid );
  1646. if( i < fd_max )
  1647. {
  1648. WFIFOHEAD(i,3);
  1649. WFIFOW(i,0) = 0x81;
  1650. WFIFOB(i,2) = 2; // "Someone has already logged in with this id"
  1651. WFIFOSET(i,3);
  1652. set_eof(i);
  1653. }
  1654. else //Shouldn't happen, but just in case.
  1655. set_char_offline(-1, aid);
  1656. }
  1657. }
  1658. }
  1659. break;
  1660. // ip address update signal from login server
  1661. case 0x2735:
  1662. {
  1663. unsigned char buf[2];
  1664. uint32 new_ip = 0;
  1665. WBUFW(buf,0) = 0x2b1e;
  1666. mapif_sendall(buf, 2);
  1667. new_ip = host2ip(login_ip_str);
  1668. if (new_ip && new_ip != login_ip) //Update login ip, too.
  1669. login_ip = new_ip;
  1670. new_ip = host2ip(char_ip_str);
  1671. if (new_ip && new_ip != char_ip)
  1672. { //Update ip.
  1673. char_ip = new_ip;
  1674. ShowInfo("Updating IP for [%s].\n", char_ip_str);
  1675. // notify login server about the change
  1676. WFIFOHEAD(fd,6);
  1677. WFIFOW(fd,0) = 0x2736;
  1678. WFIFOL(fd,2) = htonl(char_ip);
  1679. WFIFOSET(fd,6);
  1680. }
  1681. }
  1682. RFIFOSKIP(fd,2);
  1683. break;
  1684. default:
  1685. ShowError("Unknown packet 0x%04x received from login server, disconnecting.\n", command);
  1686. set_eof(fd);
  1687. return 0;
  1688. }
  1689. }
  1690. RFIFOFLUSH(fd);
  1691. return 0;
  1692. }
  1693. int request_accreg2(int account_id, int char_id)
  1694. {
  1695. if (login_fd > 0) {
  1696. WFIFOHEAD(login_fd,10);
  1697. WFIFOW(login_fd,0) = 0x272e;
  1698. WFIFOL(login_fd,2) = account_id;
  1699. WFIFOL(login_fd,6) = char_id;
  1700. WFIFOSET(login_fd,10);
  1701. return 1;
  1702. }
  1703. return 0;
  1704. }
  1705. //Send packet forward to login-server for account saving
  1706. int save_accreg2(unsigned char* buf, int len)
  1707. {
  1708. if (login_fd > 0) {
  1709. WFIFOHEAD(login_fd,len+4);
  1710. memcpy(WFIFOP(login_fd,4), buf, len);
  1711. WFIFOW(login_fd,0) = 0x2728;
  1712. WFIFOW(login_fd,2) = len+4;
  1713. WFIFOSET(login_fd,len+4);
  1714. return 1;
  1715. }
  1716. return 0;
  1717. }
  1718. void char_read_fame_list(void)
  1719. {
  1720. int i;
  1721. char* data;
  1722. size_t len;
  1723. // Empty ranking lists
  1724. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  1725. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  1726. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  1727. // Build Blacksmith ranking list
  1728. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, fame_list_size_smith) )
  1729. Sql_ShowDebug(sql_handle);
  1730. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1731. {
  1732. // char_id
  1733. Sql_GetData(sql_handle, 0, &data, NULL);
  1734. smith_fame_list[i].id = atoi(data);
  1735. // fame
  1736. Sql_GetData(sql_handle, 1, &data, &len);
  1737. smith_fame_list[i].fame = atoi(data);
  1738. // name
  1739. Sql_GetData(sql_handle, 2, &data, &len);
  1740. memcpy(smith_fame_list[i].name, data, min(len, NAME_LENGTH));
  1741. }
  1742. // Build Alchemist ranking list
  1743. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, fame_list_size_chemist) )
  1744. Sql_ShowDebug(sql_handle);
  1745. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1746. {
  1747. // char_id
  1748. Sql_GetData(sql_handle, 0, &data, NULL);
  1749. chemist_fame_list[i].id = atoi(data);
  1750. // fame
  1751. Sql_GetData(sql_handle, 1, &data, &len);
  1752. chemist_fame_list[i].fame = atoi(data);
  1753. // name
  1754. Sql_GetData(sql_handle, 2, &data, &len);
  1755. memcpy(chemist_fame_list[i].name, data, min(len, NAME_LENGTH));
  1756. }
  1757. // Build Taekwon ranking list
  1758. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_TAEKWON, fame_list_size_taekwon) )
  1759. Sql_ShowDebug(sql_handle);
  1760. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1761. {
  1762. // char_id
  1763. Sql_GetData(sql_handle, 0, &data, NULL);
  1764. taekwon_fame_list[i].id = atoi(data);
  1765. // fame
  1766. Sql_GetData(sql_handle, 1, &data, &len);
  1767. taekwon_fame_list[i].fame = atoi(data);
  1768. // name
  1769. Sql_GetData(sql_handle, 2, &data, &len);
  1770. memcpy(taekwon_fame_list[i].name, data, min(len, NAME_LENGTH));
  1771. }
  1772. Sql_FreeResult(sql_handle);
  1773. }
  1774. // Send map-servers the fame ranking lists
  1775. int char_send_fame_list(int fd)
  1776. {
  1777. int i, len = 8;
  1778. unsigned char buf[32000];
  1779. WBUFW(buf,0) = 0x2b1b;
  1780. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  1781. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  1782. len += sizeof(struct fame_list);
  1783. }
  1784. // add blacksmith's block length
  1785. WBUFW(buf, 6) = len;
  1786. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  1787. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  1788. len += sizeof(struct fame_list);
  1789. }
  1790. // add alchemist's block length
  1791. WBUFW(buf, 4) = len;
  1792. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  1793. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  1794. len += sizeof(struct fame_list);
  1795. }
  1796. // add total packet length
  1797. WBUFW(buf, 2) = len;
  1798. if (fd != -1)
  1799. mapif_send(fd, buf, len);
  1800. else
  1801. mapif_sendall(buf, len);
  1802. return 0;
  1803. }
  1804. void char_update_fame_list(int type, int index, int fame)
  1805. {
  1806. unsigned char buf[8];
  1807. WBUFW(buf,0) = 0x2b22;
  1808. WBUFB(buf,2) = type;
  1809. WBUFB(buf,3) = index;
  1810. WBUFL(buf,4) = fame;
  1811. mapif_sendall(buf, 8);
  1812. }
  1813. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  1814. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  1815. int char_loadName(int char_id, char* name)
  1816. {
  1817. char* data;
  1818. size_t len;
  1819. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  1820. Sql_ShowDebug(sql_handle);
  1821. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1822. {
  1823. Sql_GetData(sql_handle, 0, &data, &len);
  1824. memset(name, 0, NAME_LENGTH);
  1825. memcpy(name, data, min(len, NAME_LENGTH));
  1826. return 1;
  1827. }
  1828. else
  1829. {
  1830. memcpy(name, unknown_char_name, NAME_LENGTH);
  1831. }
  1832. return 0;
  1833. }
  1834. int search_mapserver(unsigned short map, uint32 ip, uint16 port);
  1835. int parse_frommap(int fd)
  1836. {
  1837. int i = 0, j = 0;
  1838. int id;
  1839. // Sometimes fd=0, and it will cause server crash. Don't know why. :(
  1840. if (fd <= 0) {
  1841. ShowError("parse_frommap error fd=%d\n", fd);
  1842. return 0;
  1843. }
  1844. ARR_FIND( 0, MAX_MAP_SERVERS, id, server[id].fd == fd );
  1845. if(id == MAX_MAP_SERVERS)
  1846. set_eof(fd);
  1847. if(session[fd]->flag.eof) {
  1848. if (id < MAX_MAP_SERVERS) {
  1849. unsigned char buf[16384];
  1850. ShowStatus("Map-server %d (session #%d) has disconnected.\n", id, fd);
  1851. //Notify other map servers that this one is gone. [Skotlex]
  1852. WBUFW(buf,0) = 0x2b20;
  1853. WBUFL(buf,4) = htonl(server[id].ip);
  1854. WBUFW(buf,8) = htons(server[id].port);
  1855. j = 0;
  1856. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  1857. if (server[id].map[i])
  1858. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  1859. if (j > 0) {
  1860. WBUFW(buf,2) = j * 4 + 10;
  1861. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  1862. }
  1863. memset(&server[id], 0, sizeof(struct mmo_map_server));
  1864. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server[id].fd) )
  1865. Sql_ShowDebug(sql_handle);
  1866. server[id].fd = -1;
  1867. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  1868. }
  1869. do_close(fd);
  1870. return 0;
  1871. }
  1872. while(RFIFOREST(fd) >= 2)
  1873. {
  1874. switch(RFIFOW(fd, 0))
  1875. {
  1876. case 0x2afa: // Receiving map names list from the map-server
  1877. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1878. return 0;
  1879. memset(server[id].map, 0, sizeof(server[id].map));
  1880. j = 0;
  1881. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  1882. server[id].map[j] = RFIFOW(fd,i);
  1883. j++;
  1884. }
  1885. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  1886. id, j, CONVIP(server[id].ip), server[id].port);
  1887. ShowStatus("Map-server %d loading complete.\n", id);
  1888. // send name for wisp to player
  1889. WFIFOHEAD(fd, 3 + NAME_LENGTH);
  1890. WFIFOW(fd,0) = 0x2afb;
  1891. WFIFOB(fd,2) = 0;
  1892. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH);
  1893. WFIFOSET(fd,3+NAME_LENGTH);
  1894. char_send_fame_list(fd); //Send fame list.
  1895. {
  1896. unsigned char buf[16384];
  1897. int x;
  1898. if (j == 0) {
  1899. ShowWarning("Map-server %d has NO maps.\n", id);
  1900. } else {
  1901. // Transmitting maps information to the other map-servers
  1902. WBUFW(buf,0) = 0x2b04;
  1903. WBUFW(buf,2) = j * 4 + 10;
  1904. WBUFL(buf,4) = htonl(server[id].ip);
  1905. WBUFW(buf,8) = htons(server[id].port);
  1906. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  1907. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  1908. }
  1909. // Transmitting the maps of the other map-servers to the new map-server
  1910. for(x = 0; x < MAX_MAP_SERVERS; x++) {
  1911. if (server[x].fd > 0 && x != id) {
  1912. WFIFOHEAD(fd,10 +4*MAX_MAP_PER_SERVER);
  1913. WFIFOW(fd,0) = 0x2b04;
  1914. WFIFOL(fd,4) = htonl(server[x].ip);
  1915. WFIFOW(fd,8) = htons(server[x].port);
  1916. j = 0;
  1917. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  1918. if (server[x].map[i])
  1919. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  1920. if (j > 0) {
  1921. WFIFOW(fd,2) = j * 4 + 10;
  1922. WFIFOSET(fd,WFIFOW(fd,2));
  1923. }
  1924. }
  1925. }
  1926. }
  1927. RFIFOSKIP(fd,RFIFOW(fd,2));
  1928. break;
  1929. case 0x2afc: //Packet command is now used for sc_data request. [Skotlex]
  1930. if (RFIFOREST(fd) < 10)
  1931. return 0;
  1932. {
  1933. #ifdef ENABLE_SC_SAVING
  1934. int aid, cid;
  1935. aid = RFIFOL(fd,2);
  1936. cid = RFIFOL(fd,6);
  1937. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  1938. scdata_db, aid, cid) )
  1939. {
  1940. Sql_ShowDebug(sql_handle);
  1941. break;
  1942. }
  1943. if( Sql_NumRows(sql_handle) > 0 )
  1944. {
  1945. struct status_change_data scdata;
  1946. int count;
  1947. char* data;
  1948. WFIFOHEAD(fd,14+50*sizeof(struct status_change_data));
  1949. WFIFOW(fd,0) = 0x2b1d;
  1950. WFIFOL(fd,4) = aid;
  1951. WFIFOL(fd,8) = cid;
  1952. for( count = 0; count < 50 && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  1953. {
  1954. Sql_GetData(sql_handle, 0, &data, NULL); scdata.type = atoi(data);
  1955. Sql_GetData(sql_handle, 1, &data, NULL); scdata.tick = atoi(data);
  1956. Sql_GetData(sql_handle, 2, &data, NULL); scdata.val1 = atoi(data);
  1957. Sql_GetData(sql_handle, 3, &data, NULL); scdata.val2 = atoi(data);
  1958. Sql_GetData(sql_handle, 4, &data, NULL); scdata.val3 = atoi(data);
  1959. Sql_GetData(sql_handle, 5, &data, NULL); scdata.val4 = atoi(data);
  1960. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &scdata, sizeof(struct status_change_data));
  1961. }
  1962. if (count >= 50)
  1963. ShowWarning("Too many status changes for %d:%d, some of them were not loaded.\n", aid, cid);
  1964. if (count > 0)
  1965. {
  1966. WFIFOW(fd,2) = 14 + count*sizeof(struct status_change_data);
  1967. WFIFOW(fd,12) = count;
  1968. WFIFOSET(fd,WFIFOW(fd,2));
  1969. //Clear the data once loaded.
  1970. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, aid, cid) )
  1971. Sql_ShowDebug(sql_handle);
  1972. }
  1973. }
  1974. Sql_FreeResult(sql_handle);
  1975. #endif
  1976. RFIFOSKIP(fd, 10);
  1977. }
  1978. break;
  1979. case 0x2afe: //set MAP user count
  1980. if (RFIFOREST(fd) < 4)
  1981. return 0;
  1982. if (RFIFOW(fd,2) != server[id].users) {
  1983. server[id].users = RFIFOW(fd,2);
  1984. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  1985. }
  1986. RFIFOSKIP(fd, 4);
  1987. break;
  1988. case 0x2aff: //set MAP users
  1989. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  1990. return 0;
  1991. {
  1992. //TODO: When data mismatches memory, update guild/party online/offline states.
  1993. int i, aid, cid;
  1994. struct online_char_data* character;
  1995. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  1996. server[id].users = RFIFOW(fd,4);
  1997. for(i = 0; i < server[id].users; i++) {
  1998. aid = RFIFOL(fd,6+i*8);
  1999. cid = RFIFOL(fd,6+i*8+4);
  2000. character = (struct online_char_data*)idb_ensure(online_char_db, aid, create_online_char_data);
  2001. if (character->server > -1 && character->server != id)
  2002. {
  2003. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2004. character->account_id, character->char_id, character->server, id, aid, cid);
  2005. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2006. }
  2007. character->server = id;
  2008. character->char_id = cid;
  2009. }
  2010. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2011. RFIFOSKIP(fd,RFIFOW(fd,2));
  2012. }
  2013. break;
  2014. case 0x2b01: // Receive character data from map-server for saving
  2015. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2016. return 0;
  2017. {
  2018. int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8), size = RFIFOW(fd,2);
  2019. struct online_char_data* character;
  2020. if (size - 13 != sizeof(struct mmo_charstatus))
  2021. {
  2022. ShowError("parse_from_map (save-char): Size mismatch! %d != %d\n", size-13, sizeof(struct mmo_charstatus));
  2023. RFIFOSKIP(fd,size);
  2024. break;
  2025. }
  2026. //Check account only if this ain't final save. Final-save goes through because of the char-map reconnect
  2027. if (RFIFOB(fd,12) || (
  2028. (character = (struct online_char_data*)idb_get(online_char_db, aid)) != NULL &&
  2029. character->char_id == cid))
  2030. {
  2031. struct mmo_charstatus char_dat;
  2032. memcpy(&char_dat, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2033. mmo_char_tosql(cid, &char_dat);
  2034. } else { //This may be valid on char-server reconnection, when re-sending characters that already logged off.
  2035. ShowError("parse_from_map (save-char): Received data for non-existant/offline character (%d:%d).\n", aid, cid);
  2036. set_char_online(id, cid, aid);
  2037. }
  2038. if (RFIFOB(fd,12))
  2039. { //Flag? Set character offline after saving [Skotlex]
  2040. set_char_offline(cid, aid);
  2041. WFIFOHEAD(fd,10);
  2042. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  2043. WFIFOL(fd,2) = aid;
  2044. WFIFOL(fd,6) = cid;
  2045. WFIFOSET(fd,10);
  2046. }
  2047. RFIFOSKIP(fd,size);
  2048. }
  2049. break;
  2050. case 0x2b02: // req char selection
  2051. if( RFIFOREST(fd) < 18 )
  2052. return 0;
  2053. {
  2054. struct auth_node* node;
  2055. int account_id = RFIFOL(fd,2);
  2056. uint32 login_id1 = RFIFOL(fd,6);
  2057. uint32 login_id2 = RFIFOL(fd,10);
  2058. uint32 ip = RFIFOL(fd,14);
  2059. RFIFOSKIP(fd,18);
  2060. // create temporary auth entry
  2061. CREATE(node, struct auth_node, 1);
  2062. node->account_id = account_id;
  2063. node->char_id = 0;
  2064. node->login_id1 = login_id1;
  2065. node->login_id2 = login_id2;
  2066. //node->sex = 0;
  2067. node->ip = ntohl(ip);
  2068. //node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  2069. //node->gmlevel = 0;
  2070. idb_put(auth_db, account_id, node);
  2071. //Set char to "@ char select" in online db [Kevin]
  2072. set_char_charselect(account_id);
  2073. WFIFOHEAD(fd,7);
  2074. WFIFOW(fd,0) = 0x2b03;
  2075. WFIFOL(fd,2) = account_id;
  2076. WFIFOB(fd,6) = 0;
  2077. WFIFOSET(fd,7);
  2078. }
  2079. break;
  2080. case 0x2b05: // request "change map server"
  2081. if (RFIFOREST(fd) < 35)
  2082. return 0;
  2083. {
  2084. int map_id, map_fd = -1;
  2085. struct online_char_data* data;
  2086. struct mmo_charstatus* char_data;
  2087. struct mmo_charstatus char_dat;
  2088. map_id = search_mapserver(RFIFOW(fd,18), ntohl(RFIFOL(fd,24)), ntohs(RFIFOW(fd,28))); //Locate mapserver by ip and port.
  2089. if (map_id >= 0)
  2090. map_fd = server[map_id].fd;
  2091. //Char should just had been saved before this packet, so this should be safe. [Skotlex]
  2092. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2093. if (char_data == NULL)
  2094. { //Really shouldn't happen.
  2095. mmo_char_fromsql(RFIFOL(fd,14), &char_dat, true);
  2096. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2097. }
  2098. if (map_fd >= 0 && session[map_fd] && char_data)
  2099. { //Send the map server the auth of this player.
  2100. struct auth_node* node;
  2101. //Update the "last map" as this is where the player must be spawned on the new map server.
  2102. char_data->last_point.map = RFIFOW(fd,18);
  2103. char_data->last_point.x = RFIFOW(fd,20);
  2104. char_data->last_point.y = RFIFOW(fd,22);
  2105. char_data->sex = RFIFOB(fd,30);
  2106. // create temporary auth entry
  2107. CREATE(node, struct auth_node, 1);
  2108. node->account_id = RFIFOL(fd,2);
  2109. node->char_id = RFIFOL(fd,14);
  2110. node->login_id1 = RFIFOL(fd,6);
  2111. node->login_id2 = RFIFOL(fd,10);
  2112. node->sex = RFIFOB(fd,30);
  2113. node->expiration_time = 0; // FIXME
  2114. node->ip = ntohl(RFIFOL(fd,31));
  2115. idb_put(auth_db, RFIFOL(fd,2), node);
  2116. data = (struct online_char_data*)idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
  2117. data->char_id = char_data->char_id;
  2118. data->server = map_id; //Update server where char is.
  2119. //Reply with an ack.
  2120. WFIFOHEAD(fd,30);
  2121. WFIFOW(fd,0) = 0x2b06;
  2122. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2123. WFIFOSET(fd,30);
  2124. } else { //Reply with nak
  2125. WFIFOHEAD(fd,30);
  2126. WFIFOW(fd,0) = 0x2b06;
  2127. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2128. WFIFOL(fd,6) = 0; //Set login1 to 0.
  2129. WFIFOSET(fd,30);
  2130. }
  2131. RFIFOSKIP(fd,35);
  2132. }
  2133. break;
  2134. case 0x2b08: // char name request
  2135. if (RFIFOREST(fd) < 6)
  2136. return 0;
  2137. WFIFOHEAD(fd,30);
  2138. WFIFOW(fd,0) = 0x2b09;
  2139. WFIFOL(fd,2) = RFIFOL(fd,2);
  2140. char_loadName((int)RFIFOL(fd,2), (char*)WFIFOP(fd,6));
  2141. WFIFOSET(fd,30);
  2142. RFIFOSKIP(fd,6);
  2143. break;
  2144. case 0x2b0c: // Map server send information to change an email of an account -> login-server
  2145. if (RFIFOREST(fd) < 86)
  2146. return 0;
  2147. if (login_fd > 0) { // don't send request if no login-server
  2148. WFIFOHEAD(login_fd,86);
  2149. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2150. WFIFOW(login_fd,0) = 0x2722;
  2151. WFIFOSET(login_fd,86);
  2152. }
  2153. RFIFOSKIP(fd, 86);
  2154. break;
  2155. case 0x2b0e: // Request from map-server to change an account's status (will just be forwarded to login server)
  2156. if (RFIFOREST(fd) < 44)
  2157. return 0;
  2158. {
  2159. int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline
  2160. char esc_name[NAME_LENGTH*2+1];
  2161. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  2162. const char* name = (char*)RFIFOP(fd,6); // name of the target character
  2163. int type = RFIFOW(fd,30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  2164. short year = RFIFOW(fd,32);
  2165. short month = RFIFOW(fd,34);
  2166. short day = RFIFOW(fd,36);
  2167. short hour = RFIFOW(fd,38);
  2168. short minute = RFIFOW(fd,40);
  2169. short second = RFIFOW(fd,42);
  2170. RFIFOSKIP(fd,44);
  2171. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  2172. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'", char_db, esc_name) )
  2173. Sql_ShowDebug(sql_handle);
  2174. else
  2175. if( Sql_NumRows(sql_handle) == 0 )
  2176. {
  2177. result = 1; // 1-player not found
  2178. }
  2179. else
  2180. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  2181. Sql_ShowDebug(sql_handle);
  2182. //FIXME: set proper result value?
  2183. else
  2184. {
  2185. char name[NAME_LENGTH];
  2186. int account_id;
  2187. char* data;
  2188. Sql_GetData(sql_handle, 0, &data, NULL); account_id = atoi(data);
  2189. Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(name, data, sizeof(name));
  2190. if( login_fd <= 0 )
  2191. result = 3; // 3-login-server offline
  2192. //FIXME: need to move this check to login server [ultramage]
  2193. // else
  2194. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  2195. // result = 2; // 2-gm level too low
  2196. else
  2197. switch( type ) {
  2198. case 1: // block
  2199. WFIFOHEAD(login_fd,10);
  2200. WFIFOW(login_fd,0) = 0x2724;
  2201. WFIFOL(login_fd,2) = account_id;
  2202. WFIFOL(login_fd,6) = 5; // new account status
  2203. WFIFOSET(login_fd,10);
  2204. break;
  2205. case 2: // ban
  2206. WFIFOHEAD(login_fd,18);
  2207. WFIFOW(login_fd, 0) = 0x2725;
  2208. WFIFOL(login_fd, 2) = account_id;
  2209. WFIFOW(login_fd, 6) = year;
  2210. WFIFOW(login_fd, 8) = month;
  2211. WFIFOW(login_fd,10) = day;
  2212. WFIFOW(login_fd,12) = hour;
  2213. WFIFOW(login_fd,14) = minute;
  2214. WFIFOW(login_fd,16) = second;
  2215. WFIFOSET(login_fd,18);
  2216. break;
  2217. case 3: // unblock
  2218. WFIFOHEAD(login_fd,10);
  2219. WFIFOW(login_fd,0) = 0x2724;
  2220. WFIFOL(login_fd,2) = account_id;
  2221. WFIFOL(login_fd,6) = 0; // new account status
  2222. WFIFOSET(login_fd,10);
  2223. break;
  2224. case 4: // unban
  2225. WFIFOHEAD(login_fd,6);
  2226. WFIFOW(login_fd,0) = 0x272a;
  2227. WFIFOL(login_fd,2) = account_id;
  2228. WFIFOSET(login_fd,6);
  2229. break;
  2230. case 5: // changesex
  2231. WFIFOHEAD(login_fd,6);
  2232. WFIFOW(login_fd,0) = 0x2727;
  2233. WFIFOL(login_fd,2) = account_id;
  2234. WFIFOSET(login_fd,6);
  2235. break;
  2236. }
  2237. }
  2238. Sql_FreeResult(sql_handle);
  2239. // send answer if a player ask, not if the server ask
  2240. if( acc != -1 && type != 5) { // Don't send answer for changesex
  2241. WFIFOHEAD(fd,34);
  2242. WFIFOW(fd, 0) = 0x2b0f;
  2243. WFIFOL(fd, 2) = acc;
  2244. safestrncpy((char*)WFIFOP(fd,6), name, NAME_LENGTH);
  2245. WFIFOW(fd,30) = type;
  2246. WFIFOW(fd,32) = result;
  2247. WFIFOSET(fd,34);
  2248. }
  2249. }
  2250. break;
  2251. case 0x2b10: // Update and send fame ranking list
  2252. if (RFIFOREST(fd) < 11)
  2253. return 0;
  2254. {
  2255. int cid = RFIFOL(fd, 2);
  2256. int fame = RFIFOL(fd, 6);
  2257. char type = RFIFOB(fd, 10);
  2258. int size;
  2259. struct fame_list* list;
  2260. int player_pos;
  2261. int fame_pos;
  2262. switch(type)
  2263. {
  2264. case 1: size = fame_list_size_smith; list = smith_fame_list; break;
  2265. case 2: size = fame_list_size_chemist; list = chemist_fame_list; break;
  2266. case 3: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  2267. default: size = 0; list = NULL; break;
  2268. }
  2269. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  2270. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  2271. if( player_pos == size && fame_pos == size )
  2272. ;// not on list and not enough fame to get on it
  2273. else if( fame_pos == player_pos )
  2274. {// same position
  2275. list[player_pos].fame = fame;
  2276. char_update_fame_list(type, player_pos, fame);
  2277. }
  2278. else
  2279. {// move in the list
  2280. if( player_pos == size )
  2281. {// new ranker - not in the list
  2282. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  2283. list[fame_pos].id = cid;
  2284. list[fame_pos].fame = fame;
  2285. char_loadName(cid, list[fame_pos].name);
  2286. }
  2287. else
  2288. {// already in the list
  2289. if( fame_pos == size )
  2290. --fame_pos;// move to the end of the list
  2291. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  2292. list[fame_pos].fame = fame;
  2293. }
  2294. char_send_fame_list(-1);
  2295. }
  2296. RFIFOSKIP(fd,11);
  2297. }
  2298. break;
  2299. // Divorce chars
  2300. case 0x2b11:
  2301. if( RFIFOREST(fd) < 10 )
  2302. return 0;
  2303. divorce_char_sql(RFIFOL(fd,2), RFIFOL(fd,6));
  2304. RFIFOSKIP(fd,10);
  2305. break;
  2306. case 0x2b16: // Receive rates [Wizputer]
  2307. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,8))
  2308. return 0;
  2309. {
  2310. char motd[256];
  2311. char esc_motd[sizeof(motd)*2+1];
  2312. char esc_server_name[sizeof(server_name)*2+1];
  2313. strncpy(motd, (char*)RFIFOP(fd,10), 255); //First copy it to make sure the motd fits.
  2314. motd[255] = '\0';
  2315. Sql_EscapeString(sql_handle, esc_motd, motd);
  2316. Sql_EscapeString(sql_handle, esc_server_name, server_name);
  2317. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `ragsrvinfo` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d',`motd`='%s'",
  2318. fd, esc_server_name, RFIFOW(fd,2), RFIFOW(fd,4), RFIFOW(fd,6), esc_motd) )
  2319. Sql_ShowDebug(sql_handle);
  2320. RFIFOSKIP(fd,RFIFOW(fd,8));
  2321. }
  2322. break;
  2323. case 0x2b17: // Character disconnected set online 0 [Wizputer]
  2324. if (RFIFOREST(fd) < 6)
  2325. return 0;
  2326. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  2327. RFIFOSKIP(fd,10);
  2328. break;
  2329. case 0x2b18: // Reset all chars to offline [Wizputer]
  2330. set_all_offline(id);
  2331. RFIFOSKIP(fd,2);
  2332. break;
  2333. case 0x2b19: // Character set online [Wizputer]
  2334. if (RFIFOREST(fd) < 10)
  2335. return 0;
  2336. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  2337. RFIFOSKIP(fd,10);
  2338. break;
  2339. case 0x2b1a: // Build and send fame ranking lists [DracoRPG]
  2340. if (RFIFOREST(fd) < 2)
  2341. return 0;
  2342. char_read_fame_list();
  2343. char_send_fame_list(-1);
  2344. RFIFOSKIP(fd,2);
  2345. break;
  2346. case 0x2b1c: //Request to save status change data. [Skotlex]
  2347. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2348. return 0;
  2349. {
  2350. #ifdef ENABLE_SC_SAVING
  2351. int count, aid, cid;
  2352. aid = RFIFOL(fd, 4);
  2353. cid = RFIFOL(fd, 8);
  2354. count = RFIFOW(fd, 12);
  2355. if( count > 0 )
  2356. {
  2357. struct status_change_data data;
  2358. StringBuf buf;
  2359. int i;
  2360. StringBuf_Init(&buf);
  2361. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", scdata_db);
  2362. for( i = 0; i < count; ++i )
  2363. {
  2364. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  2365. if( i > 0 )
  2366. StringBuf_AppendStr(&buf, ", ");
  2367. StringBuf_Printf(&buf, "('%d','%d','%hu','%d','%d','%d','%d','%d')", aid, cid,
  2368. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  2369. }
  2370. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  2371. Sql_ShowDebug(sql_handle);
  2372. StringBuf_Destroy(&buf);
  2373. }
  2374. #endif
  2375. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2376. }
  2377. break;
  2378. case 0x2b23: // map-server alive packet
  2379. WFIFOHEAD(fd,2);
  2380. WFIFOW(fd,0) = 0x2b24;
  2381. WFIFOSET(fd,2);
  2382. RFIFOSKIP(fd,2);
  2383. break;
  2384. case 0x2b26: // auth request from map-server
  2385. if (RFIFOREST(fd) < 19)
  2386. return 0;
  2387. {
  2388. int account_id;
  2389. int char_id;
  2390. int login_id1;
  2391. char sex;
  2392. uint32 ip;
  2393. struct auth_node* node;
  2394. struct mmo_charstatus* cd;
  2395. struct mmo_charstatus char_dat;
  2396. account_id = RFIFOL(fd,2);
  2397. char_id = RFIFOL(fd,6);
  2398. login_id1 = RFIFOL(fd,10);
  2399. sex = RFIFOB(fd,14);
  2400. ip = ntohl(RFIFOL(fd,15));
  2401. RFIFOSKIP(fd,19);
  2402. node = (struct auth_node*)idb_get(auth_db, account_id);
  2403. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  2404. if( cd == NULL )
  2405. { //Really shouldn't happen.
  2406. mmo_char_fromsql(char_id, &char_dat, true);
  2407. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  2408. }
  2409. if( node != NULL && cd != NULL &&
  2410. node->account_id == account_id &&
  2411. node->char_id == char_id &&
  2412. node->login_id1 == login_id1 &&
  2413. node->sex == sex &&
  2414. node->ip == ip )
  2415. {// auth ok
  2416. cd->sex = sex;
  2417. WFIFOHEAD(fd,24 + sizeof(struct mmo_charstatus));
  2418. WFIFOW(fd,0) = 0x2afd;
  2419. WFIFOW(fd,2) = 24 + sizeof(struct mmo_charstatus);
  2420. WFIFOL(fd,4) = account_id;
  2421. WFIFOL(fd,8) = node->login_id1;
  2422. WFIFOL(fd,12) = node->login_id2;
  2423. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  2424. WFIFOL(fd,20) = node->gmlevel;
  2425. memcpy(WFIFOP(fd,24), cd, sizeof(struct mmo_charstatus));
  2426. WFIFOSET(fd, WFIFOW(fd,2));
  2427. // only use the auth once and mark user online
  2428. idb_remove(auth_db, account_id);
  2429. set_char_online(id, char_id, account_id);
  2430. }
  2431. else
  2432. {// auth failed
  2433. WFIFOHEAD(fd,19);
  2434. WFIFOW(fd,0) = 0x2b27;
  2435. WFIFOL(fd,2) = account_id;
  2436. WFIFOL(fd,6) = char_id;
  2437. WFIFOL(fd,10) = login_id1;
  2438. WFIFOB(fd,14) = sex;
  2439. WFIFOL(fd,15) = htonl(ip);
  2440. WFIFOSET(fd,19);
  2441. }
  2442. }
  2443. break;
  2444. case 0x2736: // ip address update
  2445. if (RFIFOREST(fd) < 6) return 0;
  2446. server[id].ip = ntohl(RFIFOL(fd, 2));
  2447. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(server[id].ip));
  2448. RFIFOSKIP(fd,6);
  2449. break;
  2450. default:
  2451. {
  2452. // inter server - packet
  2453. int r = inter_parse_frommap(fd);
  2454. if (r == 1) break; // processed
  2455. if (r == 2) return 0; // need more packet
  2456. // no inter server packet. no char server packet -> disconnect
  2457. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  2458. set_eof(fd);
  2459. return 0;
  2460. }
  2461. } // switch
  2462. } // while
  2463. return 0;
  2464. }
  2465. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  2466. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  2467. int search_mapserver(unsigned short map, uint32 ip, uint16 port)
  2468. {
  2469. int i, j;
  2470. for(i = 0; i < MAX_MAP_SERVERS; i++)
  2471. {
  2472. if (server[i].fd > 0
  2473. && (ip == (uint32)-1 || server[i].ip == ip)
  2474. && (port == (uint16)-1 || server[i].port == port))
  2475. {
  2476. for (j = 0; server[i].map[j]; j++)
  2477. if (server[i].map[j] == map)
  2478. return i;
  2479. }
  2480. }
  2481. return -1;
  2482. }
  2483. int char_mapif_init(int fd)
  2484. {
  2485. return inter_mapif_init(fd);
  2486. }
  2487. //--------------------------------------------
  2488. // Test to know if an IP come from LAN or WAN.
  2489. //--------------------------------------------
  2490. int lan_subnetcheck(uint32 ip)
  2491. {
  2492. int i;
  2493. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  2494. if( i < subnet_count ) {
  2495. ShowInfo("Subnet check [%u.%u.%u.%u]: Matches "CL_CYAN"%u.%u.%u.%u/%u.%u.%u.%u"CL_RESET"\n", CONVIP(ip), CONVIP(subnet[i].char_ip & subnet[i].mask), CONVIP(subnet[i].mask));
  2496. return subnet[i].char_ip;
  2497. } else {
  2498. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", CONVIP(ip));
  2499. return 0;
  2500. }
  2501. }
  2502. int parse_char(int fd)
  2503. {
  2504. int i, ch = 0;
  2505. char email[40];
  2506. unsigned short cmd;
  2507. int map_fd;
  2508. struct char_session_data* sd;
  2509. uint32 ipl = session[fd]->client_addr;
  2510. sd = (struct char_session_data*)session[fd]->session_data;
  2511. // disconnect any player if no login-server.
  2512. if(login_fd < 0)
  2513. set_eof(fd);
  2514. if(session[fd]->flag.eof)
  2515. {
  2516. if( sd != NULL && sd->auth )
  2517. { // already authed client
  2518. struct online_char_data* data = (struct online_char_data*)idb_get(online_char_db, sd->account_id);
  2519. if( data == NULL || data->server == -1) //If it is not in any server, send it offline. [Skotlex]
  2520. set_char_offline(-1,sd->account_id);
  2521. if( data != NULL && data->fd == fd)
  2522. data->fd = -1;
  2523. }
  2524. do_close(fd);
  2525. return 0;
  2526. }
  2527. while( RFIFOREST(fd) >= 2 )
  2528. {
  2529. //For use in packets that depend on an sd being present [Skotlex]
  2530. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL || !sd->auth) { RFIFOSKIP(fd,rest); return 0; } }
  2531. cmd = RFIFOW(fd,0);
  2532. switch( cmd )
  2533. {
  2534. // request to connect
  2535. // 0065 <account id>.L <login id1>.L <login id2>.L <???>.W <sex>.B
  2536. case 0x65:
  2537. if( RFIFOREST(fd) < 17 )
  2538. return 0;
  2539. {
  2540. struct auth_node* node;
  2541. int account_id = RFIFOL(fd,2);
  2542. uint32 login_id1 = RFIFOL(fd,6);
  2543. uint32 login_id2 = RFIFOL(fd,10);
  2544. int sex = RFIFOB(fd,16);
  2545. RFIFOSKIP(fd,17);
  2546. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", account_id, login_id1, login_id2);
  2547. if (sd) {
  2548. //Received again auth packet for already authentified account?? Discard it.
  2549. //TODO: Perhaps log this as a hack attempt?
  2550. //TODO: and perhaps send back a reply?
  2551. break;
  2552. }
  2553. CREATE(session[fd]->session_data, struct char_session_data, 1);
  2554. sd = (struct char_session_data*)session[fd]->session_data;
  2555. sd->account_id = account_id;
  2556. sd->login_id1 = login_id1;
  2557. sd->login_id2 = login_id2;
  2558. sd->sex = sex;
  2559. sd->auth = false; // not authed yet
  2560. // send back account_id
  2561. WFIFOHEAD(fd,4);
  2562. WFIFOL(fd,0) = account_id;
  2563. WFIFOSET(fd,4);
  2564. // search authentification
  2565. node = (struct auth_node*)idb_get(auth_db, account_id);
  2566. if( node != NULL &&
  2567. node->account_id == account_id &&
  2568. node->login_id1 == login_id1 &&
  2569. node->login_id2 == login_id2 &&
  2570. node->ip == ipl )
  2571. {// authentication found (coming from map server)
  2572. idb_remove(auth_db, account_id);
  2573. char_auth_ok(fd, sd);
  2574. }
  2575. else
  2576. {// authentication not found (coming from login server)
  2577. if (login_fd > 0) { // don't send request if no login-server
  2578. WFIFOHEAD(login_fd,19);
  2579. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  2580. WFIFOL(login_fd,2) = sd->account_id;
  2581. WFIFOL(login_fd,6) = sd->login_id1;
  2582. WFIFOL(login_fd,10) = sd->login_id2;
  2583. WFIFOB(login_fd,14) = sd->sex;
  2584. WFIFOL(login_fd,15) = htonl(ipl);
  2585. WFIFOSET(login_fd,19);
  2586. } else { // if no login-server, we must refuse connection
  2587. WFIFOHEAD(fd,3);
  2588. WFIFOW(fd,0) = 0x6c;
  2589. WFIFOB(fd,2) = 0;
  2590. WFIFOSET(fd,3);
  2591. }
  2592. }
  2593. }
  2594. break;
  2595. // char select
  2596. case 0x66:
  2597. FIFOSD_CHECK(3);
  2598. {
  2599. struct mmo_charstatus char_dat;
  2600. struct mmo_charstatus * cp;
  2601. char* data;
  2602. int char_id;
  2603. uint32 subnet_map_ip;
  2604. struct auth_node* node;
  2605. int slot = RFIFOB(fd,2);
  2606. RFIFOSKIP(fd,3);
  2607. if ( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'", char_db, sd->account_id, slot)
  2608. || SQL_SUCCESS != Sql_NextRow(sql_handle)
  2609. || SQL_SUCCESS != Sql_GetData(sql_handle, 0, &data, NULL) )
  2610. {
  2611. //Not found?? May be forged packet.
  2612. Sql_ShowDebug(sql_handle);
  2613. Sql_FreeResult(sql_handle);
  2614. //TODO: perhaps add some reply? (otherwise it hangs the client)
  2615. break;
  2616. }
  2617. char_id = atoi(data);
  2618. Sql_FreeResult(sql_handle);
  2619. mmo_char_fromsql(char_id, &char_dat, true);
  2620. //Have to switch over to the DB instance otherwise data won't propagate [Kevin]
  2621. cp = (struct mmo_charstatus *)idb_get(char_db_, char_id);
  2622. cp->sex = sd->sex;
  2623. if (log_char) {
  2624. char esc_name[NAME_LENGTH*2+1];
  2625. Sql_EscapeStringLen(sql_handle, esc_name, char_dat.name, strnlen(char_dat.name, NAME_LENGTH));
  2626. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')",
  2627. charlog_db, sd->account_id, slot, esc_name) )
  2628. Sql_ShowDebug(sql_handle);
  2629. }
  2630. ShowInfo("Selected char: (Account %d: %d - %s)\n", sd->account_id, slot, char_dat.name);
  2631. // searching map server
  2632. i = search_mapserver(char_dat.last_point.map, -1, -1);
  2633. // if map is not found, we check major cities
  2634. if (i < 0) {
  2635. unsigned short j;
  2636. //First check that there's actually a map server online.
  2637. ARR_FIND( 0, MAX_MAP_SERVERS, j, server[j].fd >= 0 && server[j].map[0] );
  2638. if (j == MAX_MAP_SERVERS) {
  2639. ShowInfo("Connection Closed. No map servers available.\n");
  2640. WFIFOHEAD(fd,3);
  2641. WFIFOW(fd,0) = 0x81;
  2642. WFIFOB(fd,2) = 1; // 01 = Server closed
  2643. WFIFOSET(fd,3);
  2644. break;
  2645. }
  2646. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  2647. cp->last_point.x = 273;
  2648. cp->last_point.y = 354;
  2649. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  2650. cp->last_point.x = 120;
  2651. cp->last_point.y = 100;
  2652. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  2653. cp->last_point.x = 160;
  2654. cp->last_point.y = 94;
  2655. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  2656. cp->last_point.x = 116;
  2657. cp->last_point.y = 57;
  2658. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  2659. cp->last_point.x = 87;
  2660. cp->last_point.y = 117;
  2661. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  2662. cp->last_point.x = 94;
  2663. cp->last_point.y = 103;
  2664. } else {
  2665. ShowInfo("Connection Closed. No map server available that has a major city, and unable to find map-server for '%s'.\n", mapindex_id2name(char_dat.last_point.map));
  2666. WFIFOHEAD(fd,3);
  2667. WFIFOW(fd,0) = 0x81;
  2668. WFIFOB(fd,2) = 1; // 01 = Server closed
  2669. WFIFOSET(fd,3);
  2670. break;
  2671. }
  2672. ShowWarning("Unable to find map-server for '%s', sending to major city '%s'.\n", mapindex_id2name(char_dat.last_point.map), mapindex_id2name(j));
  2673. cp->last_point.map = j;
  2674. }
  2675. //Send NEW auth packet [Kevin]
  2676. //FIXME: is this case even possible? [ultramage]
  2677. if ((map_fd = server[i].fd) < 1 || session[map_fd] == NULL)
  2678. {
  2679. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  2680. server[i].fd = -1;
  2681. memset(&server[i], 0, sizeof(struct mmo_map_server));
  2682. //Send server closed.
  2683. WFIFOHEAD(fd,3);
  2684. WFIFOW(fd,0) = 0x81;
  2685. WFIFOB(fd,2) = 1; // 01 = Server closed
  2686. WFIFOSET(fd,3);
  2687. break;
  2688. }
  2689. //Send player to map
  2690. WFIFOHEAD(fd,28);
  2691. WFIFOW(fd,0) = 0x71;
  2692. WFIFOL(fd,2) = cp->char_id;
  2693. mapindex_getmapname_ext(mapindex_id2name(cp->last_point.map), (char*)WFIFOP(fd,6));
  2694. subnet_map_ip = lan_subnetcheck(ipl);
  2695. WFIFOL(fd,22) = htonl((subnet_map_ip) ? subnet_map_ip : server[i].ip);
  2696. WFIFOW(fd,26) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  2697. WFIFOSET(fd,28);
  2698. // create temporary auth entry
  2699. CREATE(node, struct auth_node, 1);
  2700. node->account_id = sd->account_id;
  2701. node->char_id = cp->char_id;
  2702. node->login_id1 = sd->login_id1;
  2703. node->login_id2 = sd->login_id2;
  2704. node->sex = sd->sex;
  2705. node->expiration_time = sd->expiration_time;
  2706. node->gmlevel = sd->gmlevel;
  2707. node->ip = ipl;
  2708. idb_put(auth_db, sd->account_id, node);
  2709. set_char_online(-2,node->char_id,sd->account_id);
  2710. }
  2711. break;
  2712. // create new char
  2713. // S 0067 <name>.24B <str>.B <agi>.B <vit>.B <int>.B <dex>.B <luk>.B <slot>.B <hair color>.W <hair style>.W
  2714. case 0x67:
  2715. FIFOSD_CHECK(37);
  2716. if( !char_new ) //turn character creation on/off [Kevin]
  2717. i = -2;
  2718. else
  2719. i = make_new_char_sql(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35));
  2720. //'Charname already exists' (-1), 'Char creation denied' (-2) and 'You are underaged' (-3)
  2721. if (i < 0)
  2722. {
  2723. WFIFOHEAD(fd,3);
  2724. WFIFOW(fd,0) = 0x6e;
  2725. switch (i) {
  2726. case -1: WFIFOB(fd,2) = 0x00; break;
  2727. case -2: WFIFOB(fd,2) = 0x02; break;
  2728. case -3: WFIFOB(fd,2) = 0x01; break;
  2729. }
  2730. WFIFOSET(fd,3);
  2731. RFIFOSKIP(fd,37);
  2732. break;
  2733. }
  2734. else
  2735. {
  2736. int len;
  2737. // retrieve data
  2738. struct mmo_charstatus char_dat;
  2739. mmo_char_fromsql(i, &char_dat, false); //Only the short data is needed.
  2740. // send to player
  2741. WFIFOHEAD(fd,110);
  2742. WFIFOW(fd,0) = 0x6d;
  2743. len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat);
  2744. WFIFOSET(fd,len);
  2745. // add new entry to the chars list
  2746. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] == -1 );
  2747. if( ch < MAX_CHARS )
  2748. sd->found_char[ch] = i; // the char_id of the new char
  2749. }
  2750. RFIFOSKIP(fd,37);
  2751. break;
  2752. // delete char
  2753. case 0x68:
  2754. // 2004-04-19aSakexe+ langtype 12 char deletion packet
  2755. case 0x1fb:
  2756. if (cmd == 0x68) FIFOSD_CHECK(46);
  2757. if (cmd == 0x1fb) FIFOSD_CHECK(56);
  2758. {
  2759. int cid = RFIFOL(fd,2);
  2760. WFIFOHEAD(fd,46);
  2761. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  2762. memcpy(email, RFIFOP(fd,6), 40);
  2763. RFIFOSKIP(fd,RFIFOREST(fd)); // hack to make the other deletion packet work
  2764. // Check if e-mail is correct
  2765. if(strcmpi(email, sd->email) && //email does not matches and
  2766. (
  2767. strcmp("a@a.com", sd->email) || //it is not default email, or
  2768. (strcmp("a@a.com", email) && strcmp("", email)) //email sent does not matches default
  2769. )) { //Fail
  2770. WFIFOHEAD(fd,3);
  2771. WFIFOW(fd,0) = 0x70;
  2772. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  2773. WFIFOSET(fd,3);
  2774. break;
  2775. }
  2776. // check if this char exists
  2777. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  2778. if( i == MAX_CHARS )
  2779. { // Such a character does not exist in the account
  2780. WFIFOHEAD(fd,3);
  2781. WFIFOW(fd,0) = 0x70;
  2782. WFIFOB(fd,2) = 0;
  2783. WFIFOSET(fd,3);
  2784. break;
  2785. }
  2786. // remove char from list and compact it
  2787. for(ch = i; ch < MAX_CHARS-1; ch++)
  2788. sd->found_char[ch] = sd->found_char[ch+1];
  2789. sd->found_char[MAX_CHARS-1] = -1;
  2790. /* Delete character */
  2791. if(delete_char_sql(cid)<0){
  2792. //can't delete the char
  2793. //either SQL error or can't delete by some CONFIG conditions
  2794. //del fail
  2795. WFIFOW(fd, 0) = 0x70;
  2796. WFIFOB(fd, 2) = 0;
  2797. WFIFOSET(fd, 3);
  2798. break;
  2799. }
  2800. /* Char successfully deleted.*/
  2801. WFIFOHEAD(fd,2);
  2802. WFIFOW(fd,0) = 0x6f;
  2803. WFIFOSET(fd,2);
  2804. }
  2805. break;
  2806. // client keep-alive packet (every 12 seconds)
  2807. // R 0187 <account ID>.l
  2808. case 0x187:
  2809. if (RFIFOREST(fd) < 6)
  2810. return 0;
  2811. RFIFOSKIP(fd,6);
  2812. break;
  2813. // char rename request
  2814. // R 028d <account ID>.l <char ID>.l <new name>.24B
  2815. case 0x28d:
  2816. FIFOSD_CHECK(34);
  2817. //not implemented
  2818. RFIFOSKIP(fd,34);
  2819. break;
  2820. // log in as map-server
  2821. case 0x2af8:
  2822. if (RFIFOREST(fd) < 60)
  2823. return 0;
  2824. {
  2825. char* l_user = (char*)RFIFOP(fd,2);
  2826. char* l_pass = (char*)RFIFOP(fd,26);
  2827. l_user[23] = '\0';
  2828. l_pass[23] = '\0';
  2829. ARR_FIND( 0, MAX_MAP_SERVERS, i, server[i].fd <= 0 );
  2830. if (i == MAX_MAP_SERVERS || strcmp(l_user, userid) || strcmp(l_pass, passwd)) {
  2831. WFIFOHEAD(fd,3);
  2832. WFIFOW(fd,0) = 0x2af9;
  2833. WFIFOB(fd,2) = 3;
  2834. WFIFOSET(fd,3);
  2835. } else {
  2836. WFIFOHEAD(fd,3);
  2837. WFIFOW(fd,0) = 0x2af9;
  2838. WFIFOB(fd,2) = 0;
  2839. WFIFOSET(fd,3);
  2840. server[i].fd = fd;
  2841. server[i].ip = ntohl(RFIFOL(fd,54));
  2842. server[i].port = ntohs(RFIFOW(fd,58));
  2843. server[i].users = 0;
  2844. memset(server[i].map, 0, sizeof(server[i].map));
  2845. session[fd]->func_parse = parse_frommap;
  2846. session[fd]->flag.server = 1;
  2847. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  2848. char_mapif_init(fd);
  2849. }
  2850. RFIFOSKIP(fd,60);
  2851. }
  2852. return 0; // avoid processing of followup packets here
  2853. // Athena info get
  2854. case 0x7530:
  2855. WFIFOHEAD(fd,10);
  2856. WFIFOW(fd,0) = 0x7531;
  2857. WFIFOB(fd,2) = ATHENA_MAJOR_VERSION;
  2858. WFIFOB(fd,3) = ATHENA_MINOR_VERSION;
  2859. WFIFOB(fd,4) = ATHENA_REVISION;
  2860. WFIFOB(fd,5) = ATHENA_RELEASE_FLAG;
  2861. WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG;
  2862. WFIFOB(fd,7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR;
  2863. WFIFOW(fd,8) = ATHENA_MOD_VERSION;
  2864. WFIFOSET(fd,10);
  2865. RFIFOSKIP(fd,2);
  2866. break;
  2867. // unknown packet received
  2868. default:
  2869. ShowError("parse_char: Received unknown packet "CL_WHITE"0x%x"CL_RESET" from ip '"CL_WHITE"%s"CL_RESET"'! Disconnecting!\n", RFIFOW(fd,0), ip2str(ipl, NULL));
  2870. set_eof(fd);
  2871. return 0;
  2872. }
  2873. }
  2874. RFIFOFLUSH(fd);
  2875. return 0;
  2876. }
  2877. // Console Command Parser [Wizputer]
  2878. int parse_console(char* buf)
  2879. {
  2880. char command[256];
  2881. memset(command, 0, sizeof(command));
  2882. sscanf(buf, "%[^\n]", command);
  2883. //login_log("Console command :%s\n", command);
  2884. if( strcmpi("shutdown", command) == 0 ||
  2885. strcmpi("exit", command) == 0 ||
  2886. strcmpi("quit", command) == 0 ||
  2887. strcmpi("end", command) == 0 )
  2888. runflag = 0;
  2889. else if( strcmpi("alive", command) == 0 ||
  2890. strcmpi("status", command) == 0 )
  2891. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  2892. else if( strcmpi("help", command) == 0 ){
  2893. ShowInfo(CL_BOLD"Help of commands:"CL_RESET"\n");
  2894. ShowInfo(" To shutdown the server:\n");
  2895. ShowInfo(" 'shutdown|exit|qui|end'\n");
  2896. ShowInfo(" To know if server is alive:\n");
  2897. ShowInfo(" 'alive|status'\n");
  2898. }
  2899. return 0;
  2900. }
  2901. // MAP send all
  2902. int mapif_sendall(unsigned char *buf, unsigned int len)
  2903. {
  2904. int i, c;
  2905. c = 0;
  2906. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  2907. int fd;
  2908. if ((fd = server[i].fd) > 0) {
  2909. WFIFOHEAD(fd,len);
  2910. memcpy(WFIFOP(fd,0), buf, len);
  2911. WFIFOSET(fd,len);
  2912. c++;
  2913. }
  2914. }
  2915. return c;
  2916. }
  2917. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len)
  2918. {
  2919. int i, c;
  2920. c = 0;
  2921. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  2922. int fd;
  2923. if ((fd = server[i].fd) > 0 && fd != sfd) {
  2924. WFIFOHEAD(fd,len);
  2925. memcpy(WFIFOP(fd,0), buf, len);
  2926. WFIFOSET(fd,len);
  2927. c++;
  2928. }
  2929. }
  2930. return c;
  2931. }
  2932. int mapif_send(int fd, unsigned char *buf, unsigned int len)
  2933. {
  2934. int i;
  2935. if (fd >= 0) {
  2936. ARR_FIND( 0, MAX_MAP_SERVERS, i, fd == server[i].fd );
  2937. if( i < MAX_MAP_SERVERS )
  2938. {
  2939. WFIFOHEAD(fd,len);
  2940. memcpy(WFIFOP(fd,0), buf, len);
  2941. WFIFOSET(fd,len);
  2942. return 1;
  2943. }
  2944. }
  2945. return 0;
  2946. }
  2947. int broadcast_user_count(int tid, unsigned int tick, int id, intptr data)
  2948. {
  2949. uint8 buf[6];
  2950. int users = count_users();
  2951. // only send an update when needed
  2952. static int prev_users = 0;
  2953. if( prev_users == users )
  2954. return 0;
  2955. prev_users = users;
  2956. if( login_fd > 0 && session[login_fd] )
  2957. {
  2958. // send number of user to login server
  2959. WFIFOHEAD(login_fd,6);
  2960. WFIFOW(login_fd,0) = 0x2714;
  2961. WFIFOL(login_fd,2) = users;
  2962. WFIFOSET(login_fd,6);
  2963. }
  2964. // send number of players to all map-servers
  2965. WBUFW(buf,0) = 0x2b00;
  2966. WBUFL(buf,2) = users;
  2967. mapif_sendall(buf,6);
  2968. return 0;
  2969. }
  2970. /// load this char's account id into the 'online accounts' packet
  2971. static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap)
  2972. {
  2973. struct online_char_data* character = (struct online_char_data*)data;
  2974. int* i = va_arg(ap, int*);
  2975. if(character->server > -1)
  2976. {
  2977. WFIFOL(login_fd,8+(*i)*4) = character->account_id;
  2978. (*i)++;
  2979. return 1;
  2980. }
  2981. return 0;
  2982. }
  2983. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr data)
  2984. {
  2985. if (login_fd > 0 && session[login_fd])
  2986. {
  2987. // send account list to login server
  2988. int users = online_char_db->size(online_char_db);
  2989. int i = 0;
  2990. WFIFOHEAD(login_fd,8+users*4);
  2991. WFIFOW(login_fd,0) = 0x272d;
  2992. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  2993. WFIFOW(login_fd,2) = 8+ i*4;
  2994. WFIFOL(login_fd,4) = i;
  2995. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  2996. }
  2997. return 0;
  2998. }
  2999. int check_connect_login_server(int tid, unsigned int tick, int id, intptr data)
  3000. {
  3001. if (login_fd > 0 && session[login_fd] != NULL)
  3002. return 0;
  3003. ShowInfo("Attempt to connect to login-server...\n");
  3004. login_fd = make_connection(login_ip, login_port);
  3005. if (login_fd == -1)
  3006. { //Try again later. [Skotlex]
  3007. login_fd = 0;
  3008. return 0;
  3009. }
  3010. session[login_fd]->func_parse = parse_fromlogin;
  3011. session[login_fd]->flag.server = 1;
  3012. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3013. WFIFOHEAD(login_fd,86);
  3014. WFIFOW(login_fd,0) = 0x2710;
  3015. memcpy(WFIFOP(login_fd,2), userid, 24);
  3016. memcpy(WFIFOP(login_fd,26), passwd, 24);
  3017. WFIFOL(login_fd,50) = 0;
  3018. WFIFOL(login_fd,54) = htonl(char_ip);
  3019. WFIFOL(login_fd,58) = htons(char_port);
  3020. memcpy(WFIFOP(login_fd,60), server_name, 20);
  3021. WFIFOW(login_fd,80) = 0;
  3022. WFIFOW(login_fd,82) = char_maintenance;
  3023. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  3024. WFIFOSET(login_fd,86);
  3025. return 1;
  3026. }
  3027. // sends a ping packet to login server (will receive pong 0x2718)
  3028. int ping_login_server(int tid, unsigned int tick, int id, intptr data)
  3029. {
  3030. if (login_fd > 0 && session[login_fd] != NULL)
  3031. {
  3032. WFIFOHEAD(login_fd,2);
  3033. WFIFOW(login_fd,0) = 0x2719;
  3034. WFIFOSET(login_fd,2);
  3035. }
  3036. return 0;
  3037. }
  3038. //------------------------------------------------
  3039. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  3040. //replies/disconnect the player we tried to kick. [Skotlex]
  3041. //------------------------------------------------
  3042. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr data)
  3043. {
  3044. struct online_char_data* character;
  3045. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  3046. { //Mark it offline due to timeout.
  3047. character->waiting_disconnect = -1;
  3048. set_char_offline(character->char_id, character->account_id);
  3049. }
  3050. return 0;
  3051. }
  3052. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  3053. {
  3054. struct online_char_data *character= (struct online_char_data*)data;
  3055. if (character->fd != -1)
  3056. return 0; //Still connected
  3057. if (character->server == -2) //Unknown server.. set them offline
  3058. set_char_offline(character->char_id, character->account_id);
  3059. if (character->server < 0)
  3060. //Free data from players that have not been online for a while.
  3061. db_remove(online_char_db, key);
  3062. return 0;
  3063. }
  3064. static int online_data_cleanup(int tid, unsigned int tick, int id, intptr data)
  3065. {
  3066. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  3067. return 0;
  3068. }
  3069. //----------------------------------
  3070. // Reading Lan Support configuration
  3071. // Rewrote: Anvanced subnet check [LuzZza]
  3072. //----------------------------------
  3073. int char_lan_config_read(const char *lancfgName)
  3074. {
  3075. FILE *fp;
  3076. int line_num = 0;
  3077. char line[1024], w1[64], w2[64], w3[64], w4[64];
  3078. if((fp = fopen(lancfgName, "r")) == NULL) {
  3079. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  3080. return 1;
  3081. }
  3082. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  3083. while(fgets(line, sizeof(line), fp))
  3084. {
  3085. line_num++;
  3086. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  3087. continue;
  3088. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4) {
  3089. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  3090. continue;
  3091. }
  3092. remove_control_chars(w1);
  3093. remove_control_chars(w2);
  3094. remove_control_chars(w3);
  3095. remove_control_chars(w4);
  3096. if( strcmpi(w1, "subnet") == 0 )
  3097. {
  3098. subnet[subnet_count].mask = str2ip(w2);
  3099. subnet[subnet_count].char_ip = str2ip(w3);
  3100. subnet[subnet_count].map_ip = str2ip(w4);
  3101. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  3102. {
  3103. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  3104. continue;
  3105. }
  3106. subnet_count++;
  3107. }
  3108. }
  3109. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  3110. fclose(fp);
  3111. return 0;
  3112. }
  3113. #endif //TXT_SQL_CONVERT
  3114. void sql_config_read(const char* cfgName)
  3115. {
  3116. char line[1024], w1[1024], w2[1024];
  3117. FILE* fp;
  3118. ShowInfo("Reading file %s...\n", cfgName);
  3119. if ((fp = fopen(cfgName, "r")) == NULL) {
  3120. ShowError("file not found: %s\n", cfgName);
  3121. return;
  3122. }
  3123. while(fgets(line, sizeof(line), fp))
  3124. {
  3125. if(line[0] == '/' && line[1] == '/')
  3126. continue;
  3127. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3128. continue;
  3129. if(!strcmpi(w1,"char_db"))
  3130. strcpy(char_db,w2);
  3131. else if(!strcmpi(w1,"scdata_db"))
  3132. strcpy(scdata_db,w2);
  3133. else if(!strcmpi(w1,"cart_db"))
  3134. strcpy(cart_db,w2);
  3135. else if(!strcmpi(w1,"inventory_db"))
  3136. strcpy(inventory_db, w2);
  3137. else if(!strcmpi(w1,"charlog_db"))
  3138. strcpy(charlog_db,w2);
  3139. else if(!strcmpi(w1,"storage_db"))
  3140. strcpy(storage_db,w2);
  3141. else if(!strcmpi(w1,"reg_db"))
  3142. strcpy(reg_db,w2);
  3143. else if(!strcmpi(w1,"skill_db"))
  3144. strcpy(skill_db,w2);
  3145. else if(!strcmpi(w1,"interlog_db"))
  3146. strcpy(interlog_db,w2);
  3147. else if(!strcmpi(w1,"memo_db"))
  3148. strcpy(memo_db,w2);
  3149. else if(!strcmpi(w1,"guild_db"))
  3150. strcpy(guild_db,w2);
  3151. else if(!strcmpi(w1,"guild_alliance_db"))
  3152. strcpy(guild_alliance_db,w2);
  3153. else if(!strcmpi(w1,"guild_castle_db"))
  3154. strcpy(guild_castle_db,w2);
  3155. else if(!strcmpi(w1,"guild_expulsion_db"))
  3156. strcpy(guild_expulsion_db,w2);
  3157. else if(!strcmpi(w1,"guild_member_db"))
  3158. strcpy(guild_member_db,w2);
  3159. else if(!strcmpi(w1,"guild_skill_db"))
  3160. strcpy(guild_skill_db,w2);
  3161. else if(!strcmpi(w1,"guild_position_db"))
  3162. strcpy(guild_position_db,w2);
  3163. else if(!strcmpi(w1,"guild_storage_db"))
  3164. strcpy(guild_storage_db,w2);
  3165. else if(!strcmpi(w1,"party_db"))
  3166. strcpy(party_db,w2);
  3167. else if(!strcmpi(w1,"pet_db"))
  3168. strcpy(pet_db,w2);
  3169. else if(!strcmpi(w1,"mail_db"))
  3170. strcpy(mail_db,w2);
  3171. else if(!strcmpi(w1,"auction_db"))
  3172. strcpy(auction_db,w2);
  3173. else if(!strcmpi(w1,"friend_db"))
  3174. strcpy(friend_db,w2);
  3175. else if(!strcmpi(w1,"hotkey_db"))
  3176. strcpy(hotkey_db,w2);
  3177. else if(!strcmpi(w1,"quest_db"))
  3178. strcpy(quest_db,w2);
  3179. else if(!strcmpi(w1,"quest_obj_db"))
  3180. strcpy(quest_obj_db, w2);
  3181. #ifndef TXT_SQL_CONVERT
  3182. else if(!strcmpi(w1,"db_path"))
  3183. strcpy(db_path,w2);
  3184. #endif
  3185. //support the import command, just like any other config
  3186. else if(!strcmpi(w1,"import"))
  3187. sql_config_read(w2);
  3188. }
  3189. fclose(fp);
  3190. ShowInfo("Done reading %s.\n", cfgName);
  3191. }
  3192. #ifndef TXT_SQL_CONVERT
  3193. int char_config_read(const char* cfgName)
  3194. {
  3195. char line[1024], w1[1024], w2[1024];
  3196. FILE* fp = fopen(cfgName, "r");
  3197. if (fp == NULL) {
  3198. ShowError("Configuration file not found: %s.\n", cfgName);
  3199. return 1;
  3200. }
  3201. ShowInfo("Reading configuration file %s...\n", cfgName);
  3202. while(fgets(line, sizeof(line), fp))
  3203. {
  3204. if (line[0] == '/' && line[1] == '/')
  3205. continue;
  3206. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3207. continue;
  3208. remove_control_chars(w1);
  3209. remove_control_chars(w2);
  3210. if(strcmpi(w1,"timestamp_format") == 0) {
  3211. strncpy(timestamp_format, w2, 20);
  3212. } else if(strcmpi(w1,"console_silent")==0){
  3213. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  3214. msg_silent = atoi(w2);
  3215. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  3216. stdout_with_ansisequence = config_switch(w2);
  3217. } else if (strcmpi(w1, "userid") == 0) {
  3218. strncpy(userid, w2, 24);
  3219. } else if (strcmpi(w1, "passwd") == 0) {
  3220. strncpy(passwd, w2, 24);
  3221. } else if (strcmpi(w1, "server_name") == 0) {
  3222. strncpy(server_name, w2, 20);
  3223. server_name[sizeof(server_name) - 1] = '\0';
  3224. ShowStatus("%s server has been initialized\n", w2);
  3225. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  3226. if (strlen(w2) >= 4) {
  3227. memcpy(wisp_server_name, w2, sizeof(wisp_server_name));
  3228. wisp_server_name[sizeof(wisp_server_name) - 1] = '\0';
  3229. }
  3230. } else if (strcmpi(w1, "login_ip") == 0) {
  3231. char ip_str[16];
  3232. login_ip = host2ip(w2);
  3233. if (login_ip) {
  3234. strncpy(login_ip_str, w2, sizeof(login_ip_str));
  3235. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(login_ip, ip_str));
  3236. }
  3237. } else if (strcmpi(w1, "login_port") == 0) {
  3238. login_port = atoi(w2);
  3239. } else if (strcmpi(w1, "char_ip") == 0) {
  3240. char ip_str[16];
  3241. char_ip = host2ip(w2);
  3242. if (char_ip){
  3243. strncpy(char_ip_str, w2, sizeof(char_ip_str));
  3244. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(char_ip, ip_str));
  3245. }
  3246. } else if (strcmpi(w1, "bind_ip") == 0) {
  3247. char ip_str[16];
  3248. bind_ip = host2ip(w2);
  3249. if (bind_ip) {
  3250. strncpy(bind_ip_str, w2, sizeof(bind_ip_str));
  3251. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(bind_ip, ip_str));
  3252. }
  3253. } else if (strcmpi(w1, "char_port") == 0) {
  3254. char_port = atoi(w2);
  3255. } else if (strcmpi(w1, "char_maintenance") == 0) {
  3256. char_maintenance = atoi(w2);
  3257. } else if (strcmpi(w1, "char_new") == 0) {
  3258. char_new = (bool)atoi(w2);
  3259. } else if (strcmpi(w1, "char_new_display") == 0) {
  3260. char_new_display = atoi(w2);
  3261. } else if (strcmpi(w1, "max_connect_user") == 0) {
  3262. max_connect_user = atoi(w2);
  3263. if (max_connect_user < 0)
  3264. max_connect_user = 0; // unlimited online players
  3265. } else if(strcmpi(w1, "gm_allow_level") == 0) {
  3266. gm_allow_level = atoi(w2);
  3267. if(gm_allow_level < 0)
  3268. gm_allow_level = 99;
  3269. } else if (strcmpi(w1, "online_check") == 0) {
  3270. online_check = config_switch(w2);
  3271. } else if (strcmpi(w1, "autosave_time") == 0) {
  3272. autosave_interval = atoi(w2)*1000;
  3273. if (autosave_interval <= 0)
  3274. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  3275. } else if (strcmpi(w1, "save_log") == 0) {
  3276. save_log = config_switch(w2);
  3277. } else if (strcmpi(w1, "start_point") == 0) {
  3278. char map[MAP_NAME_LENGTH_EXT];
  3279. int x, y;
  3280. if (sscanf(w2, "%15[^,],%d,%d", map, &x, &y) < 3)
  3281. continue;
  3282. start_point.map = mapindex_name2id(map);
  3283. if (!start_point.map)
  3284. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  3285. start_point.x = x;
  3286. start_point.y = y;
  3287. } else if (strcmpi(w1, "start_zeny") == 0) {
  3288. start_zeny = atoi(w2);
  3289. if (start_zeny < 0)
  3290. start_zeny = 0;
  3291. } else if (strcmpi(w1, "start_weapon") == 0) {
  3292. start_weapon = atoi(w2);
  3293. if (start_weapon < 0)
  3294. start_weapon = 0;
  3295. } else if (strcmpi(w1, "start_armor") == 0) {
  3296. start_armor = atoi(w2);
  3297. if (start_armor < 0)
  3298. start_armor = 0;
  3299. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  3300. log_char = atoi(w2);
  3301. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  3302. strcpy(unknown_char_name, w2);
  3303. unknown_char_name[NAME_LENGTH-1] = '\0';
  3304. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  3305. name_ignoring_case = config_switch(w2);
  3306. } else if (strcmpi(w1, "char_name_option") == 0) {
  3307. char_name_option = atoi(w2);
  3308. } else if (strcmpi(w1, "char_name_letters") == 0) {
  3309. strcpy(char_name_letters, w2);
  3310. } else if (strcmpi(w1, "char_rename") == 0) {
  3311. char_rename = config_switch(w2);
  3312. } else if (strcmpi(w1, "chars_per_account") == 0) { //maxchars per account [Sirius]
  3313. char_per_account = atoi(w2);
  3314. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  3315. char_del_level = atoi(w2);
  3316. } else if (strcmpi(w1, "console") == 0) {
  3317. console = config_switch(w2);
  3318. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  3319. fame_list_size_chemist = atoi(w2);
  3320. if (fame_list_size_chemist > MAX_FAME_LIST) {
  3321. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  3322. fame_list_size_chemist = MAX_FAME_LIST;
  3323. }
  3324. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  3325. fame_list_size_smith = atoi(w2);
  3326. if (fame_list_size_smith > MAX_FAME_LIST) {
  3327. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  3328. fame_list_size_smith = MAX_FAME_LIST;
  3329. }
  3330. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  3331. fame_list_size_taekwon = atoi(w2);
  3332. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  3333. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  3334. fame_list_size_taekwon = MAX_FAME_LIST;
  3335. }
  3336. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  3337. guild_exp_rate = atoi(w2);
  3338. } else if (strcmpi(w1, "import") == 0) {
  3339. char_config_read(w2);
  3340. }
  3341. }
  3342. fclose(fp);
  3343. ShowInfo("Done reading %s.\n", cfgName);
  3344. return 0;
  3345. }
  3346. void do_final(void)
  3347. {
  3348. ShowInfo("Doing final stage...\n");
  3349. //check SQL save progress.
  3350. //wait until save char complete
  3351. set_all_offline(-1);
  3352. set_all_offline_sql();
  3353. inter_final();
  3354. flush_fifos();
  3355. mapindex_final();
  3356. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `ragsrvinfo`") )
  3357. Sql_ShowDebug(sql_handle);
  3358. if (login_fd > 0)
  3359. do_close(login_fd);
  3360. if (char_fd > 0)
  3361. do_close(char_fd);
  3362. char_db_->destroy(char_db_, NULL);
  3363. online_char_db->destroy(online_char_db, NULL);
  3364. auth_db->destroy(auth_db, NULL);
  3365. Sql_Free(sql_handle);
  3366. ShowInfo("ok! all done...\n");
  3367. }
  3368. //------------------------------
  3369. // Function called when the server
  3370. // has received a crash signal.
  3371. //------------------------------
  3372. void do_abort(void)
  3373. {
  3374. }
  3375. void set_server_type(void)
  3376. {
  3377. SERVER_TYPE = ATHENA_SERVER_CHAR;
  3378. }
  3379. int do_init(int argc, char **argv)
  3380. {
  3381. int i;
  3382. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3383. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3384. server[i].fd = -1;
  3385. }
  3386. //Read map indexes
  3387. mapindex_init();
  3388. start_point.map = mapindex_name2id("new_zone01");
  3389. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  3390. char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME);
  3391. sql_config_read(SQL_CONF_NAME);
  3392. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  3393. ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  3394. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  3395. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  3396. }
  3397. ShowInfo("Finished reading the char-server configuration.\n");
  3398. inter_init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server ÃʱâÈ­
  3399. ShowInfo("Finished reading the inter-server configuration.\n");
  3400. ShowInfo("Initializing char server.\n");
  3401. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  3402. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  3403. mmo_char_sql_init();
  3404. char_read_fame_list(); //Read fame lists.
  3405. ShowInfo("char server initialized.\n");
  3406. set_defaultparse(parse_char);
  3407. if ((naddr_ != 0) && (!login_ip || !char_ip))
  3408. {
  3409. char ip_str[16];
  3410. ip2str(addr_[0], ip_str);
  3411. if (naddr_ > 1)
  3412. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  3413. else
  3414. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  3415. if (!login_ip) {
  3416. strcpy(login_ip_str, ip_str);
  3417. login_ip = str2ip(login_ip_str);
  3418. }
  3419. if (!char_ip) {
  3420. strcpy(char_ip_str, ip_str);
  3421. char_ip = str2ip(char_ip_str);
  3422. }
  3423. }
  3424. // establish char-login connection if not present
  3425. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  3426. add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000);
  3427. // keep the char-login connection alive
  3428. add_timer_func_list(ping_login_server, "ping_login_server");
  3429. add_timer_interval(gettick() + 1000, ping_login_server, 0, 0, ((int)stall_time-2) * 1000);
  3430. // periodically update the overall user count on all mapservers + login server
  3431. add_timer_func_list(broadcast_user_count, "broadcast_user_count");
  3432. add_timer_interval(gettick() + 1000, broadcast_user_count, 0, 0, 5 * 1000);
  3433. // send a list of all online account IDs to login server
  3434. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  3435. add_timer_interval(gettick() + 1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour.
  3436. // ???
  3437. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  3438. // ???
  3439. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  3440. add_timer_interval(gettick() + 1000, online_data_cleanup, 0, 0, 600 * 1000);
  3441. if( console )
  3442. {
  3443. //##TODO invoke a CONSOLE_START plugin event
  3444. }
  3445. //Cleaning the tables for NULL entrys @ startup [Sirius]
  3446. //Chardb clean
  3447. ShowInfo("Cleaning the '%s' table...\n", char_db);
  3448. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", char_db) )
  3449. Sql_ShowDebug(sql_handle);
  3450. //guilddb clean
  3451. ShowInfo("Cleaning the '%s' table...\n", guild_db);
  3452. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", guild_db) )
  3453. Sql_ShowDebug(sql_handle);
  3454. //guildmemberdb clean
  3455. ShowInfo("Cleaning the '%s' table...\n", guild_member_db);
  3456. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", guild_member_db) )
  3457. Sql_ShowDebug(sql_handle);
  3458. ShowInfo("End of char server initilization function.\n");
  3459. ShowInfo("open port %d.....\n",char_port);
  3460. char_fd = make_listen_bind(bind_ip, char_port);
  3461. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  3462. return 0;
  3463. }
  3464. #endif //TXT_SQL_CONVERT