char.c 148 KB

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