char.c 135 KB

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