char.c 135 KB

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