char.cpp 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #pragma warning(disable:4800)
  4. #include <time.h>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "../common/cbasetypes.h"
  10. #include "../common/core.h"
  11. #include "../common/db.h"
  12. #include "../common/malloc.h"
  13. #include "../common/mapindex.h"
  14. #include "../common/mmo.h"
  15. #include "../common/random.h"
  16. #include "../common/showmsg.h"
  17. #include "../common/socket.h"
  18. #include "../common/strlib.h"
  19. #include "../common/timer.h"
  20. #include "../common/cli.h"
  21. #include "int_guild.h"
  22. #include "int_homun.h"
  23. #include "int_mail.h"
  24. #include "int_mercenary.h"
  25. #include "int_elemental.h"
  26. #include "int_party.h"
  27. #include "int_storage.h"
  28. #include "inter.h"
  29. #include "char_logif.h"
  30. #include "char_mapif.h"
  31. #include "char_cnslif.h"
  32. #include "char_clif.h"
  33. //definition of exported var declared in .h
  34. int login_fd=-1; //login file descriptor
  35. int char_fd=-1; //char file descriptor
  36. struct Schema_Config schema_config;
  37. struct CharServ_Config charserv_config;
  38. struct mmo_map_server map_server[MAX_MAP_SERVERS];
  39. //Custom limits for the fame lists. [Skotlex]
  40. int fame_list_size_chemist = MAX_FAME_LIST;
  41. int fame_list_size_smith = MAX_FAME_LIST;
  42. int fame_list_size_taekwon = MAX_FAME_LIST;
  43. // Char-server-side stored fame lists [DracoRPG]
  44. struct fame_list smith_fame_list[MAX_FAME_LIST];
  45. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  46. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  47. #define CHAR_MAX_MSG 300 //max number of msg_conf
  48. static char* msg_table[CHAR_MAX_MSG]; // Login Server messages_conf
  49. // check for exit signal
  50. // 0 is saving complete
  51. // other is char_id
  52. unsigned int save_flag = 0;
  53. // Advanced subnet check [LuzZza]
  54. struct s_subnet {
  55. uint32 mask;
  56. uint32 char_ip;
  57. uint32 map_ip;
  58. } subnet[16];
  59. int subnet_count = 0;
  60. int char_chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data);
  61. DBMap* auth_db; // uint32 account_id -> struct auth_node*
  62. DBMap* online_char_db; // uint32 account_id -> struct online_char_data*
  63. DBMap* char_db_; // uint32 char_id -> struct mmo_charstatus*
  64. DBMap* char_get_authdb() { return auth_db; }
  65. DBMap* char_get_onlinedb() { return online_char_db; }
  66. DBMap* char_get_chardb() { return char_db_; }
  67. /**
  68. * @see DBCreateData
  69. */
  70. DBData char_create_online_data(DBKey key, va_list args){
  71. struct online_char_data* character;
  72. CREATE(character, struct online_char_data, 1);
  73. character->account_id = key.i;
  74. character->char_id = -1;
  75. character->server = -1;
  76. character->fd = -1;
  77. character->waiting_disconnect = INVALID_TIMER;
  78. return db_ptr2data(character);
  79. }
  80. void char_set_charselect(uint32 account_id) {
  81. struct online_char_data* character;
  82. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, char_create_online_data);
  83. if( character->server > -1 )
  84. if( map_server[character->server].users > 0 ) // Prevent this value from going negative.
  85. map_server[character->server].users--;
  86. character->char_id = -1;
  87. character->server = -1;
  88. if(character->waiting_disconnect != INVALID_TIMER) {
  89. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  90. character->waiting_disconnect = INVALID_TIMER;
  91. }
  92. chlogif_send_setacconline(account_id);
  93. }
  94. void char_set_char_online(int map_id, uint32 char_id, uint32 account_id) {
  95. struct online_char_data* character;
  96. struct mmo_charstatus *cp;
  97. //Update DB
  98. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1', `last_login`=NOW() WHERE `char_id`='%d' LIMIT 1", schema_config.char_db, char_id) )
  99. Sql_ShowDebug(sql_handle);
  100. //Check to see for online conflicts
  101. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, char_create_online_data);
  102. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  103. {
  104. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  105. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  106. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  107. }
  108. //Update state data
  109. character->char_id = char_id;
  110. character->server = map_id;
  111. if( character->server > -1 )
  112. map_server[character->server].users++;
  113. //Get rid of disconnect timer
  114. if(character->waiting_disconnect != INVALID_TIMER) {
  115. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  116. character->waiting_disconnect = INVALID_TIMER;
  117. }
  118. //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  119. cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  120. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  121. //Notify login server
  122. chlogif_send_setacconline(account_id);
  123. }
  124. void char_set_char_offline(uint32 char_id, uint32 account_id){
  125. struct online_char_data* character;
  126. if ( char_id == -1 )
  127. {
  128. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", schema_config.char_db, account_id) )
  129. Sql_ShowDebug(sql_handle);
  130. }
  131. else
  132. {
  133. struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  134. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  135. if (cp)
  136. idb_remove(char_db_,char_id);
  137. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d' LIMIT 1", schema_config.char_db, char_id) )
  138. Sql_ShowDebug(sql_handle);
  139. }
  140. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  141. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  142. if( character->server > -1 )
  143. if( map_server[character->server].users > 0 ) // Prevent this value from going negative.
  144. map_server[character->server].users--;
  145. if(character->waiting_disconnect != INVALID_TIMER){
  146. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  147. character->waiting_disconnect = INVALID_TIMER;
  148. }
  149. if(character->char_id == char_id)
  150. {
  151. character->char_id = -1;
  152. character->server = -1;
  153. // needed if player disconnects completely since Skotlex did not want to free the session
  154. character->pincode_success = false;
  155. }
  156. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  157. }
  158. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  159. if (char_id == -1 || character == NULL || character->fd == -1){
  160. chlogif_send_setaccoffline(login_fd,account_id);
  161. }
  162. }
  163. /**
  164. * @see DBApply
  165. */
  166. int char_db_setoffline(DBKey key, DBData *data, va_list ap) {
  167. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  168. int server = va_arg(ap, int);
  169. if (server == -1) {
  170. character->char_id = -1;
  171. character->server = -1;
  172. if(character->waiting_disconnect != INVALID_TIMER){
  173. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  174. character->waiting_disconnect = INVALID_TIMER;
  175. }
  176. } else if (character->server == server)
  177. character->server = -2; //In some map server that we aren't connected to.
  178. return 0;
  179. }
  180. /**
  181. * @see DBApply
  182. */
  183. static int char_db_kickoffline(DBKey key, DBData *data, va_list ap){
  184. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  185. int server_id = va_arg(ap, int);
  186. if (server_id > -1 && character->server != server_id)
  187. return 0;
  188. //Kick out any connected characters, and set them offline as appropriate.
  189. if (character->server > -1)
  190. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 1);
  191. else if (character->waiting_disconnect == INVALID_TIMER)
  192. char_set_char_offline(character->char_id, character->account_id);
  193. else
  194. return 0; // fail
  195. return 1;
  196. }
  197. void char_set_all_offline(int id){
  198. if (id < 0)
  199. ShowNotice("Sending all users offline.\n");
  200. else
  201. ShowNotice("Sending users of map-server %d offline.\n",id);
  202. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  203. if (id >= 0 || !chlogif_isconnected())
  204. return;
  205. //Tell login-server to also mark all our characters as offline.
  206. chlogif_send_setallaccoffline(login_fd);
  207. }
  208. void char_set_all_offline_sql(void){
  209. //Set all players to 'OFFLINE'
  210. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", schema_config.char_db) )
  211. Sql_ShowDebug(sql_handle);
  212. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", schema_config.guild_member_db) )
  213. Sql_ShowDebug(sql_handle);
  214. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", schema_config.guild_db) )
  215. Sql_ShowDebug(sql_handle);
  216. }
  217. /**
  218. * @see DBCreateData
  219. */
  220. static DBData char_create_charstatus(DBKey key, va_list args) {
  221. struct mmo_charstatus *cp;
  222. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  223. cp->char_id = key.i;
  224. return db_ptr2data(cp);
  225. }
  226. int char_mmo_char_tosql(uint32 char_id, struct mmo_charstatus* p){
  227. int i = 0;
  228. int count = 0;
  229. int diff = 0;
  230. char save_status[128]; //For displaying save information. [Skotlex]
  231. struct mmo_charstatus *cp;
  232. int errors = 0; //If there are any errors while saving, "cp" will not be updated at the end.
  233. StringBuf buf;
  234. if (char_id!=p->char_id) return 0;
  235. cp = (struct mmo_charstatus *)idb_ensure(char_db_, char_id, char_create_charstatus);
  236. StringBuf_Init(&buf);
  237. memset(save_status, 0, sizeof(save_status));
  238. if (
  239. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  240. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  241. (p->zeny != cp->zeny) ||
  242. (p->last_point.map != cp->last_point.map) ||
  243. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  244. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  245. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  246. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  247. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  248. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  249. (p->option != cp->option) ||
  250. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  251. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) ||
  252. (p->ele_id != cp->ele_id) || (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  253. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom) || (p->delete_date != cp->delete_date) ||
  254. (p->rename != cp->rename) || (p->robe != cp->robe) || (p->character_moves != cp->character_moves) ||
  255. (p->unban_time != cp->unban_time) || (p->font != cp->font) || (p->uniqueitem_counter != cp->uniqueitem_counter) ||
  256. (p->hotkey_rowshift != cp->hotkey_rowshift) || (p->clan_id != cp->clan_id ) || (p->title_id != cp->title_id) ||
  257. (p->show_equip != cp->show_equip)
  258. )
  259. { //Save status
  260. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  261. "`base_exp`='%u', `job_exp`='%u', `zeny`='%d',"
  262. "`max_hp`='%u',`hp`='%u',`max_sp`='%u',`sp`='%u',`status_point`='%d',`skill_point`='%d',"
  263. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  264. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',`elemental_id`='%d',"
  265. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  266. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d',"
  267. "`delete_date`='%lu',`robe`='%d',`moves`='%d',`font`='%u',`uniqueitem_counter`='%u',"
  268. "`hotkey_rowshift`='%d', `clan_id`='%d', `title_id`='%lu', `show_equip`='%d'"
  269. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  270. schema_config.char_db, p->base_level, p->job_level,
  271. p->base_exp, p->job_exp, p->zeny,
  272. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  273. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  274. p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, p->ele_id,
  275. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  276. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  277. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename,
  278. (unsigned long)p->delete_date, // FIXME: platform-dependent size
  279. p->robe, p->character_moves, p->font, p->uniqueitem_counter,
  280. p->hotkey_rowshift, p->clan_id, p->title_id, p->show_equip,
  281. p->account_id, p->char_id) )
  282. {
  283. Sql_ShowDebug(sql_handle);
  284. errors++;
  285. } else
  286. strcat(save_status, " status");
  287. }
  288. //Values that will seldom change (to speed up saving)
  289. if (
  290. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  291. (p->body != cp->body) || (p->class_ != cp->class_) ||
  292. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  293. (p->mother != cp->mother) || (p->child != cp->child) ||
  294. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  295. (p->fame != cp->fame)
  296. )
  297. {
  298. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',"
  299. "`hair`='%d', `hair_color`='%d', `clothes_color`='%d', `body`='%d',"
  300. "`partner_id`='%u', `father`='%u', `mother`='%u', `child`='%u',"
  301. "`karma`='%d',`manner`='%d', `fame`='%d'"
  302. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  303. schema_config.char_db, p->class_,
  304. p->hair, p->hair_color, p->clothes_color, p->body,
  305. p->partner_id, p->father, p->mother, p->child,
  306. p->karma, p->manner, p->fame,
  307. p->account_id, p->char_id) )
  308. {
  309. Sql_ShowDebug(sql_handle);
  310. errors++;
  311. } else
  312. strcat(save_status, " status2");
  313. }
  314. /* Mercenary Owner */
  315. if( (p->mer_id != cp->mer_id) ||
  316. (p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) ||
  317. (p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) ||
  318. (p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) )
  319. {
  320. if (mercenary_owner_tosql(char_id, p))
  321. strcat(save_status, " mercenary");
  322. else
  323. errors++;
  324. }
  325. //memo points
  326. if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
  327. {
  328. char esc_mapname[NAME_LENGTH*2+1];
  329. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  330. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.memo_db, p->char_id) )
  331. {
  332. Sql_ShowDebug(sql_handle);
  333. errors++;
  334. }
  335. //insert here.
  336. StringBuf_Clear(&buf);
  337. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", schema_config.memo_db);
  338. for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
  339. {
  340. if( p->memo_point[i].map )
  341. {
  342. if( count )
  343. StringBuf_AppendStr(&buf, ",");
  344. Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
  345. StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
  346. ++count;
  347. }
  348. }
  349. if( count )
  350. {
  351. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  352. {
  353. Sql_ShowDebug(sql_handle);
  354. errors++;
  355. }
  356. }
  357. strcat(save_status, " memo");
  358. }
  359. //skills
  360. if( memcmp(p->skill, cp->skill, sizeof(p->skill)) )
  361. {
  362. //`skill` (`char_id`, `id`, `lv`)
  363. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.skill_db, p->char_id) )
  364. {
  365. Sql_ShowDebug(sql_handle);
  366. errors++;
  367. }
  368. StringBuf_Clear(&buf);
  369. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`,`flag`) VALUES ", schema_config.skill_db);
  370. //insert here.
  371. for( i = 0, count = 0; i < MAX_SKILL; ++i ) {
  372. if( p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY ) {
  373. if( p->skill[i].lv == 0 && ( p->skill[i].flag == SKILL_FLAG_PERM_GRANTED || p->skill[i].flag == SKILL_FLAG_PERMANENT ) )
  374. continue;
  375. if( p->skill[i].flag != SKILL_FLAG_PERMANENT && p->skill[i].flag != SKILL_FLAG_PERM_GRANTED && (p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0) == 0 )
  376. continue;
  377. if( count )
  378. StringBuf_AppendStr(&buf, ",");
  379. StringBuf_Printf(&buf, "('%d','%d','%d','%d')", char_id, p->skill[i].id,
  380. ( (p->skill[i].flag == SKILL_FLAG_PERMANENT || p->skill[i].flag == SKILL_FLAG_PERM_GRANTED) ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0),
  381. p->skill[i].flag == SKILL_FLAG_PERM_GRANTED ? p->skill[i].flag : 0);/* other flags do not need to be saved */
  382. ++count;
  383. }
  384. }
  385. if( count )
  386. {
  387. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  388. {
  389. Sql_ShowDebug(sql_handle);
  390. errors++;
  391. }
  392. }
  393. strcat(save_status, " skills");
  394. }
  395. diff = 0;
  396. for(i = 0; i < MAX_FRIENDS; i++){
  397. if(p->friends[i].char_id != cp->friends[i].char_id ||
  398. p->friends[i].account_id != cp->friends[i].account_id){
  399. diff = 1;
  400. break;
  401. }
  402. }
  403. if(diff == 1)
  404. { //Save friends
  405. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.friend_db, char_id) )
  406. {
  407. Sql_ShowDebug(sql_handle);
  408. errors++;
  409. }
  410. StringBuf_Clear(&buf);
  411. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", schema_config.friend_db);
  412. for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
  413. {
  414. if( p->friends[i].char_id > 0 )
  415. {
  416. if( count )
  417. StringBuf_AppendStr(&buf, ",");
  418. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id);
  419. count++;
  420. }
  421. }
  422. if( count )
  423. {
  424. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  425. {
  426. Sql_ShowDebug(sql_handle);
  427. errors++;
  428. }
  429. }
  430. strcat(save_status, " friends");
  431. }
  432. #ifdef HOTKEY_SAVING
  433. // hotkeys
  434. StringBuf_Clear(&buf);
  435. StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", schema_config.hotkey_db);
  436. diff = 0;
  437. for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
  438. if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
  439. {
  440. if( diff )
  441. StringBuf_AppendStr(&buf, ",");// not the first hotkey
  442. 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);
  443. diff = 1;
  444. }
  445. }
  446. if(diff) {
  447. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  448. {
  449. Sql_ShowDebug(sql_handle);
  450. errors++;
  451. } else
  452. strcat(save_status, " hotkeys");
  453. }
  454. #endif
  455. StringBuf_Destroy(&buf);
  456. if (save_status[0]!='\0' && charserv_config.save_log)
  457. ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
  458. if (!errors)
  459. memcpy(cp, p, sizeof(struct mmo_charstatus));
  460. return 0;
  461. }
  462. /// Saves an array of 'item' entries into the specified table.
  463. int char_memitemdata_to_sql(const struct item items[], int max, int id, enum storage_type tableswitch, uint8 stor_id) {
  464. StringBuf buf;
  465. SqlStmt* stmt;
  466. int i, j, offset = 0, errors = 0;
  467. const char *tablename, *selectoption, *printname;
  468. struct item item; // temp storage variable
  469. bool* flag; // bit array for inventory matching
  470. bool found;
  471. switch (tableswitch) {
  472. case TABLE_INVENTORY:
  473. printname = "Inventory";
  474. tablename = schema_config.inventory_db;
  475. selectoption = "char_id";
  476. break;
  477. case TABLE_CART:
  478. printname = "Cart";
  479. tablename = schema_config.cart_db;
  480. selectoption = "char_id";
  481. break;
  482. case TABLE_STORAGE:
  483. printname = inter_premiumStorage_getPrintableName(stor_id);
  484. tablename = inter_premiumStorage_getTableName(stor_id);
  485. selectoption = "account_id";
  486. break;
  487. case TABLE_GUILD_STORAGE:
  488. printname = "Guild Storage";
  489. tablename = schema_config.guild_storage_db;
  490. selectoption = "guild_id";
  491. break;
  492. default:
  493. ShowError("Invalid table name!\n");
  494. return 1;
  495. }
  496. // The following code compares inventory with current database values
  497. // and performs modification/deletion/insertion only on relevant rows.
  498. // This approach is more complicated than a trivial delete&insert, but
  499. // it significantly reduces cpu load on the database server.
  500. StringBuf_Init(&buf);
  501. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`");
  502. if (tableswitch == TABLE_INVENTORY) {
  503. StringBuf_Printf(&buf, ", `favorite`");
  504. offset = 1;
  505. }
  506. for( i = 0; i < MAX_SLOTS; ++i )
  507. StringBuf_Printf(&buf, ", `card%d`", i);
  508. for( i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  509. StringBuf_Printf(&buf, ", `option_id%d`", i);
  510. StringBuf_Printf(&buf, ", `option_val%d`", i);
  511. StringBuf_Printf(&buf, ", `option_parm%d`", i);
  512. }
  513. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id);
  514. stmt = SqlStmt_Malloc(sql_handle);
  515. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  516. || SQL_ERROR == SqlStmt_Execute(stmt) )
  517. {
  518. SqlStmt_ShowDebug(stmt);
  519. SqlStmt_Free(stmt);
  520. StringBuf_Destroy(&buf);
  521. return 1;
  522. }
  523. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  524. SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &item.nameid, 0, NULL, NULL);
  525. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  526. SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &item.equip, 0, NULL, NULL);
  527. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  528. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  529. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  530. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  531. SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &item.bound, 0, NULL, NULL);
  532. SqlStmt_BindColumn(stmt, 9, SQLDT_UINT64, &item.unique_id, 0, NULL, NULL);
  533. if (tableswitch == TABLE_INVENTORY)
  534. SqlStmt_BindColumn(stmt, 10, SQLDT_CHAR, &item.favorite, 0, NULL, NULL);
  535. for( i = 0; i < MAX_SLOTS; ++i )
  536. SqlStmt_BindColumn(stmt, 10+offset+i, SQLDT_USHORT, &item.card[i], 0, NULL, NULL);
  537. for( i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  538. SqlStmt_BindColumn(stmt, 10+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].id, 0, NULL, NULL);
  539. SqlStmt_BindColumn(stmt, 11+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].value, 0, NULL, NULL);
  540. SqlStmt_BindColumn(stmt, 12+offset+MAX_SLOTS+i*3, SQLDT_CHAR, &item.option[i].param, 0, NULL, NULL);
  541. }
  542. // bit array indicating which inventory items have already been matched
  543. flag = (bool*) aCalloc(max, sizeof(bool));
  544. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  545. {
  546. found = false;
  547. // search for the presence of the item in the char's inventory
  548. for( i = 0; i < max; ++i )
  549. {
  550. // skip empty and already matched entries
  551. if( items[i].nameid == 0 || flag[i] )
  552. continue;
  553. if( items[i].nameid == item.nameid
  554. && items[i].card[0] == item.card[0]
  555. && items[i].card[2] == item.card[2]
  556. && items[i].card[3] == item.card[3]
  557. && items[i].unique_id == item.unique_id
  558. ) { //They are the same item.
  559. int k;
  560. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  561. ARR_FIND( 0, MAX_ITEM_RDM_OPT, k, items[i].option[k].id != item.option[k].id || items[i].option[k].value != item.option[k].value || items[i].option[k].param != item.option[k].param );
  562. if( j == MAX_SLOTS &&
  563. k == MAX_ITEM_RDM_OPT &&
  564. items[i].amount == item.amount &&
  565. items[i].equip == item.equip &&
  566. items[i].identify == item.identify &&
  567. items[i].refine == item.refine &&
  568. items[i].attribute == item.attribute &&
  569. items[i].expire_time == item.expire_time &&
  570. items[i].bound == item.bound &&
  571. (tableswitch != TABLE_INVENTORY || items[i].favorite == item.favorite) )
  572. ; //Do nothing.
  573. else
  574. {
  575. // update all fields.
  576. StringBuf_Clear(&buf);
  577. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `bound`='%d', `unique_id`='%" PRIu64 "'",
  578. tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound, items[i].unique_id);
  579. if (tableswitch == TABLE_INVENTORY)
  580. StringBuf_Printf(&buf, ", `favorite`='%d'", items[i].favorite);
  581. for( j = 0; j < MAX_SLOTS; ++j )
  582. StringBuf_Printf(&buf, ", `card%d`=%hu", j, items[i].card[j]);
  583. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  584. StringBuf_Printf(&buf, ", `option_id%d`=%d", j, items[i].option[j].id);
  585. StringBuf_Printf(&buf, ", `option_val%d`=%d", j, items[i].option[j].value);
  586. StringBuf_Printf(&buf, ", `option_parm%d`=%d", j, items[i].option[j].param);
  587. }
  588. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  589. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  590. {
  591. Sql_ShowDebug(sql_handle);
  592. errors++;
  593. }
  594. }
  595. found = flag[i] = true; //Item dealt with,
  596. break; //skip to next item in the db.
  597. }
  598. }
  599. if( !found )
  600. {// Item not present in inventory, remove it.
  601. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", tablename, item.id) )
  602. {
  603. Sql_ShowDebug(sql_handle);
  604. errors++;
  605. }
  606. }
  607. }
  608. SqlStmt_Free(stmt);
  609. StringBuf_Clear(&buf);
  610. StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`", tablename, selectoption);
  611. if (tableswitch == TABLE_INVENTORY)
  612. StringBuf_Printf(&buf, ", `favorite`");
  613. for( j = 0; j < MAX_SLOTS; ++j )
  614. StringBuf_Printf(&buf, ", `card%d`", j);
  615. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  616. StringBuf_Printf(&buf, ", `option_id%d`", j);
  617. StringBuf_Printf(&buf, ", `option_val%d`", j);
  618. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  619. }
  620. StringBuf_AppendStr(&buf, ") VALUES ");
  621. found = false;
  622. // insert non-matched items into the db as new items
  623. for( i = 0; i < max; ++i )
  624. {
  625. // skip empty and already matched entries
  626. if( items[i].nameid == 0 || flag[i] )
  627. continue;
  628. if( found )
  629. StringBuf_AppendStr(&buf, ",");
  630. else
  631. found = true;
  632. StringBuf_Printf(&buf, "('%d', '%hu', '%d', '%d', '%d', '%d', '%d', '%u', '%d', '%" PRIu64 "'",
  633. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound, items[i].unique_id);
  634. if (tableswitch == TABLE_INVENTORY)
  635. StringBuf_Printf(&buf, ", '%d'", items[i].favorite);
  636. for( j = 0; j < MAX_SLOTS; ++j )
  637. StringBuf_Printf(&buf, ", '%hu'", items[i].card[j]);
  638. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  639. StringBuf_Printf(&buf, ", '%d'", items[i].option[j].id);
  640. StringBuf_Printf(&buf, ", '%d'", items[i].option[j].value);
  641. StringBuf_Printf(&buf, ", '%d'", items[i].option[j].param);
  642. }
  643. StringBuf_AppendStr(&buf, ")");
  644. }
  645. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  646. {
  647. Sql_ShowDebug(sql_handle);
  648. errors++;
  649. }
  650. ShowInfo("Saved %s (%d) data to table %s for %s: %d\n", printname, stor_id, tablename, selectoption, id);
  651. StringBuf_Destroy(&buf);
  652. aFree(flag);
  653. return errors;
  654. }
  655. bool char_memitemdata_from_sql(struct s_storage* p, int max, int id, enum storage_type tableswitch, uint8 stor_id) {
  656. StringBuf buf;
  657. SqlStmt* stmt;
  658. int i,j, offset = 0;
  659. struct item item, *storage;
  660. const char *tablename, *selectoption, *printname;
  661. switch (tableswitch) {
  662. case TABLE_INVENTORY:
  663. printname = "Inventory";
  664. tablename = schema_config.inventory_db;
  665. selectoption = "char_id";
  666. storage = p->u.items_inventory;
  667. break;
  668. case TABLE_CART:
  669. printname = "Cart";
  670. tablename = schema_config.cart_db;
  671. selectoption = "char_id";
  672. storage = p->u.items_cart;
  673. break;
  674. case TABLE_STORAGE:
  675. printname = "Storage";
  676. tablename = inter_premiumStorage_getTableName(stor_id);
  677. selectoption = "account_id";
  678. storage = p->u.items_storage;
  679. break;
  680. case TABLE_GUILD_STORAGE:
  681. printname = "Guild Storage";
  682. tablename = schema_config.guild_storage_db;
  683. selectoption = "guild_id";
  684. storage = p->u.items_guild;
  685. break;
  686. default:
  687. ShowError("Invalid table name!\n");
  688. return false;
  689. }
  690. memset(p, 0, sizeof(struct s_storage)); //clean up memory
  691. p->id = id;
  692. p->type = tableswitch;
  693. p->stor_id = stor_id;
  694. p->max_amount = inter_premiumStorage_getMax(p->stor_id);
  695. stmt = SqlStmt_Malloc(sql_handle);
  696. if (stmt == NULL) {
  697. SqlStmt_ShowDebug(stmt);
  698. return false;
  699. }
  700. StringBuf_Init(&buf);
  701. StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`,`expire_time`,`bound`,`unique_id`");
  702. if (tableswitch == TABLE_INVENTORY) {
  703. StringBuf_Printf(&buf, ", `favorite`");
  704. offset = 1;
  705. }
  706. for( j = 0; j < MAX_SLOTS; ++j )
  707. StringBuf_Printf(&buf, ",`card%d`", j);
  708. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  709. StringBuf_Printf(&buf, ", `option_id%d`", j);
  710. StringBuf_Printf(&buf, ", `option_val%d`", j);
  711. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  712. }
  713. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`=? ORDER BY `nameid`", tablename, selectoption );
  714. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  715. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &id, 0)
  716. || SQL_ERROR == SqlStmt_Execute(stmt) )
  717. {
  718. SqlStmt_ShowDebug(stmt);
  719. SqlStmt_Free(stmt);
  720. StringBuf_Destroy(&buf);
  721. return false;
  722. }
  723. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  724. SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &item.nameid, 0, NULL, NULL);
  725. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  726. SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &item.equip, 0, NULL, NULL);
  727. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  728. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  729. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  730. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  731. SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &item.bound, 0, NULL, NULL);
  732. SqlStmt_BindColumn(stmt, 9, SQLDT_ULONGLONG, &item.unique_id, 0, NULL, NULL);
  733. if (tableswitch == TABLE_INVENTORY)
  734. SqlStmt_BindColumn(stmt, 10, SQLDT_CHAR, &item.favorite, 0, NULL, NULL);
  735. for( i = 0; i < MAX_SLOTS; ++i )
  736. SqlStmt_BindColumn(stmt, 10+offset+i, SQLDT_USHORT, &item.card[i], 0, NULL, NULL);
  737. for( i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  738. SqlStmt_BindColumn(stmt, 10+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].id, 0, NULL, NULL);
  739. SqlStmt_BindColumn(stmt, 11+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].value, 0, NULL, NULL);
  740. SqlStmt_BindColumn(stmt, 12+offset+MAX_SLOTS+i*3, SQLDT_CHAR, &item.option[i].param, 0, NULL, NULL);
  741. }
  742. for( i = 0; i < max && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  743. memcpy(&storage[i], &item, sizeof(item));
  744. p->amount = i;
  745. ShowInfo("Loaded %s data from table %s for %s: %d (total: %d)\n", printname, tablename, selectoption, id, p->amount);
  746. SqlStmt_FreeResult(stmt);
  747. SqlStmt_Free(stmt);
  748. StringBuf_Destroy(&buf);
  749. return true;
  750. }
  751. /**
  752. * Returns the correct gender ID for the given character and enum value.
  753. *
  754. * If the per-character sex is defined but not supported by the current packetver, the database entries are corrected.
  755. *
  756. * @param sd Character data, if available.
  757. * @param p Character status.
  758. * @param sex Character sex (database enum)
  759. *
  760. * @retval SEX_MALE if the per-character sex is male
  761. * @retval SEX_FEMALE if the per-character sex is female
  762. * @retval SEX_ACCOUNT if the per-character sex is not defined or the current PACKETVER doesn't support it.
  763. */
  764. int char_mmo_gender(const struct char_session_data *sd, const struct mmo_charstatus *p, char sex)
  765. {
  766. #if PACKETVER >= 20141016
  767. (void)sd; (void)p; // Unused
  768. switch (sex) {
  769. case 'M':
  770. return SEX_MALE;
  771. case 'F':
  772. return SEX_FEMALE;
  773. case 'U':
  774. default:
  775. return SEX_ACCOUNT;
  776. }
  777. #else
  778. if (sex == 'M' || sex == 'F') {
  779. if (!sd) {
  780. // sd is not available, there isn't much we can do. Just return and print a warning.
  781. ShowWarning("Character '%s' (CID: %d, AID: %d) has sex '%c', but PACKETVER does not support per-character sex. Defaulting to 'U'.\n",
  782. p->name, p->char_id, p->account_id, sex);
  783. return SEX_ACCOUNT;
  784. }
  785. if ((sex == 'M' && sd->sex == SEX_FEMALE)
  786. || (sex == 'F' && sd->sex == SEX_MALE)) {
  787. ShowWarning("Changing sex of character '%s' (CID: %d, AID: %d) to 'U' due to incompatible PACKETVER.\n", p->name, p->char_id, p->account_id);
  788. chlogif_parse_ackchangecharsex(p->char_id, sd->sex);
  789. } else {
  790. ShowInfo("Resetting sex of character '%s' (CID: %d, AID: %d) to 'U' due to incompatible PACKETVER.\n", p->name, p->char_id, p->account_id);
  791. }
  792. if (SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `sex` = 'U' WHERE `char_id` = '%d'", schema_config.char_db, p->char_id)) {
  793. Sql_ShowDebug(sql_handle);
  794. }
  795. }
  796. return SEX_ACCOUNT;
  797. #endif
  798. }
  799. int char_mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p);
  800. //=====================================================================================================
  801. // Loads the basic character rooster for the given account. Returns total buffer used.
  802. int char_mmo_chars_fromsql(struct char_session_data* sd, uint8* buf) {
  803. SqlStmt* stmt;
  804. struct mmo_charstatus p;
  805. int j = 0, i;
  806. char last_map[MAP_NAME_LENGTH_EXT];
  807. char sex[2];
  808. stmt = SqlStmt_Malloc(sql_handle);
  809. if( stmt == NULL ) {
  810. SqlStmt_ShowDebug(stmt);
  811. return 0;
  812. }
  813. memset(&p, 0, sizeof(p));
  814. for( i = 0; i < MAX_CHARS; i++ ) {
  815. sd->found_char[i] = -1;
  816. sd->unban_time[i] = 0;
  817. }
  818. // read char data
  819. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  820. "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  821. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  822. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`,"
  823. "`clothes_color`,`body`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`rename`,`delete_date`,"
  824. "`robe`,`moves`,`unban_time`,`font`,`uniqueitem_counter`,`sex`,`hotkey_rowshift`,`title_id`,`show_equip`"
  825. " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", schema_config.char_db, sd->account_id, MAX_CHARS)
  826. || SQL_ERROR == SqlStmt_Execute(stmt)
  827. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL)
  828. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL)
  829. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL)
  830. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL)
  831. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL)
  832. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL)
  833. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p.base_exp, 0, NULL, NULL)
  834. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p.job_exp, 0, NULL, NULL)
  835. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL)
  836. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL)
  837. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL)
  838. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL)
  839. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL)
  840. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL)
  841. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL)
  842. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_UINT, &p.max_hp, 0, NULL, NULL)
  843. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_UINT, &p.hp, 0, NULL, NULL)
  844. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_UINT, &p.max_sp, 0, NULL, NULL)
  845. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_UINT, &p.sp, 0, NULL, NULL)
  846. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p.status_point, 0, NULL, NULL)
  847. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p.skill_point, 0, NULL, NULL)
  848. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL)
  849. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL)
  850. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL)
  851. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL)
  852. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL)
  853. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL)
  854. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.body, 0, NULL, NULL)
  855. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.weapon, 0, NULL, NULL)
  856. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.shield, 0, NULL, NULL)
  857. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_top, 0, NULL, NULL)
  858. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL)
  859. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL)
  860. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  861. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p.rename, 0, NULL, NULL)
  862. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_UINT32, &p.delete_date, 0, NULL, NULL)
  863. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p.robe, 0, NULL, NULL)
  864. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_UINT, &p.character_moves, 0, NULL, NULL)
  865. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_LONG, &p.unban_time, 0, NULL, NULL)
  866. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_UCHAR, &p.font, 0, NULL, NULL)
  867. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_UINT, &p.uniqueitem_counter, 0, NULL, NULL)
  868. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_ENUM, &sex, sizeof(sex), NULL, NULL)
  869. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_UCHAR, &p.hotkey_rowshift, 0, NULL, NULL)
  870. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_ULONG, &p.title_id, 0, NULL, NULL)
  871. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_UINT16, &p.show_equip, 0, NULL, NULL)
  872. )
  873. {
  874. SqlStmt_ShowDebug(stmt);
  875. SqlStmt_Free(stmt);
  876. return 0;
  877. }
  878. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ )
  879. {
  880. p.last_point.map = mapindex_name2id(last_map);
  881. sd->found_char[p.slot] = p.char_id;
  882. sd->unban_time[p.slot] = p.unban_time;
  883. p.sex = char_mmo_gender(sd, &p, sex[0]);
  884. j += char_mmo_char_tobuf(WBUFP(buf, j), &p);
  885. // Addon System
  886. // store the required info into the session
  887. sd->char_moves[p.slot] = p.character_moves;
  888. }
  889. memset(sd->new_name,0,sizeof(sd->new_name));
  890. SqlStmt_Free(stmt);
  891. return j;
  892. }
  893. //=====================================================================================================
  894. int char_mmo_char_fromsql(uint32 char_id, struct mmo_charstatus* p, bool load_everything) {
  895. int i;
  896. struct mmo_charstatus* cp;
  897. SqlStmt* stmt;
  898. char last_map[MAP_NAME_LENGTH_EXT];
  899. char save_map[MAP_NAME_LENGTH_EXT];
  900. char point_map[MAP_NAME_LENGTH_EXT];
  901. struct point tmp_point;
  902. struct s_skill tmp_skill;
  903. uint16 skill_count = 0;
  904. struct s_friend tmp_friend;
  905. #ifdef HOTKEY_SAVING
  906. struct hotkey tmp_hotkey;
  907. int hotkey_num;
  908. #endif
  909. StringBuf msg_buf;
  910. char sex[2];
  911. memset(p, 0, sizeof(struct mmo_charstatus));
  912. if (charserv_config.save_log) ShowInfo("Char load request (%d)\n", char_id);
  913. stmt = SqlStmt_Malloc(sql_handle);
  914. if( stmt == NULL )
  915. {
  916. SqlStmt_ShowDebug(stmt);
  917. return 0;
  918. }
  919. // read char data
  920. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  921. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  922. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  923. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,"
  924. "`hair_color`,`clothes_color`,`body`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  925. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`, `moves`,"
  926. "`unban_time`,`font`,`uniqueitem_counter`,`sex`,`hotkey_rowshift`,`clan_id`,`title_id`,`show_equip`"
  927. " FROM `%s` WHERE `char_id`=? LIMIT 1", schema_config.char_db)
  928. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  929. || SQL_ERROR == SqlStmt_Execute(stmt)
  930. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL)
  931. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL)
  932. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL)
  933. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL)
  934. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL)
  935. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL)
  936. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL)
  937. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p->base_exp, 0, NULL, NULL)
  938. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &p->job_exp, 0, NULL, NULL)
  939. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL)
  940. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL)
  941. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL)
  942. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL)
  943. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL)
  944. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL)
  945. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL)
  946. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_UINT, &p->max_hp, 0, NULL, NULL)
  947. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_UINT, &p->hp, 0, NULL, NULL)
  948. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_UINT, &p->max_sp, 0, NULL, NULL)
  949. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p->sp, 0, NULL, NULL)
  950. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p->status_point, 0, NULL, NULL)
  951. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p->skill_point, 0, NULL, NULL)
  952. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL)
  953. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL)
  954. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL)
  955. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL)
  956. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL)
  957. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL)
  958. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL)
  959. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_INT, &p->ele_id, 0, NULL, NULL)
  960. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair, 0, NULL, NULL)
  961. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL)
  962. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL)
  963. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->body, 0, NULL, NULL)
  964. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->weapon, 0, NULL, NULL)
  965. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->shield, 0, NULL, NULL)
  966. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_top, 0, NULL, NULL)
  967. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL)
  968. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL)
  969. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  970. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL)
  971. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL)
  972. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL)
  973. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL)
  974. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL)
  975. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_UINT32, &p->partner_id, 0, NULL, NULL)
  976. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_UINT32, &p->father, 0, NULL, NULL)
  977. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_UINT32, &p->mother, 0, NULL, NULL)
  978. || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_UINT32, &p->child, 0, NULL, NULL)
  979. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_INT, &p->fame, 0, NULL, NULL)
  980. || SQL_ERROR == SqlStmt_BindColumn(stmt, 50, SQLDT_SHORT, &p->rename, 0, NULL, NULL)
  981. || SQL_ERROR == SqlStmt_BindColumn(stmt, 51, SQLDT_UINT32, &p->delete_date, 0, NULL, NULL)
  982. || SQL_ERROR == SqlStmt_BindColumn(stmt, 52, SQLDT_SHORT, &p->robe, 0, NULL, NULL)
  983. || SQL_ERROR == SqlStmt_BindColumn(stmt, 53, SQLDT_UINT32, &p->character_moves, 0, NULL, NULL)
  984. || SQL_ERROR == SqlStmt_BindColumn(stmt, 54, SQLDT_LONG, &p->unban_time, 0, NULL, NULL)
  985. || SQL_ERROR == SqlStmt_BindColumn(stmt, 55, SQLDT_UCHAR, &p->font, 0, NULL, NULL)
  986. || SQL_ERROR == SqlStmt_BindColumn(stmt, 56, SQLDT_UINT, &p->uniqueitem_counter, 0, NULL, NULL)
  987. || SQL_ERROR == SqlStmt_BindColumn(stmt, 57, SQLDT_ENUM, &sex, sizeof(sex), NULL, NULL)
  988. || SQL_ERROR == SqlStmt_BindColumn(stmt, 58, SQLDT_UCHAR, &p->hotkey_rowshift, 0, NULL, NULL)
  989. || SQL_ERROR == SqlStmt_BindColumn(stmt, 59, SQLDT_INT, &p->clan_id, 0, NULL, NULL)
  990. || SQL_ERROR == SqlStmt_BindColumn(stmt, 60, SQLDT_ULONG, &p->title_id, 0, NULL, NULL)
  991. || SQL_ERROR == SqlStmt_BindColumn(stmt, 61, SQLDT_UINT16, &p->show_equip, 0, NULL, NULL)
  992. )
  993. {
  994. SqlStmt_ShowDebug(stmt);
  995. SqlStmt_Free(stmt);
  996. return 0;
  997. }
  998. if( SQL_ERROR == SqlStmt_NextRow(stmt) )
  999. {
  1000. ShowError("Requested non-existant character id: %d!\n", char_id);
  1001. SqlStmt_Free(stmt);
  1002. return 0;
  1003. }
  1004. p->sex = char_mmo_gender(NULL, p, sex[0]);
  1005. p->last_point.map = mapindex_name2id(last_map);
  1006. p->save_point.map = mapindex_name2id(save_map);
  1007. if( p->last_point.map == 0 ) {
  1008. p->last_point.map = mapindex_name2id(charserv_config.default_map);
  1009. p->last_point.x = charserv_config.default_map_x;
  1010. p->last_point.y = charserv_config.default_map_y;
  1011. }
  1012. if( p->save_point.map == 0 ) {
  1013. p->save_point.map = mapindex_name2id(charserv_config.default_map);
  1014. p->save_point.x = charserv_config.default_map_x;
  1015. p->save_point.y = charserv_config.default_map_y;
  1016. }
  1017. StringBuf_Init(&msg_buf);
  1018. StringBuf_AppendStr(&msg_buf, " status");
  1019. if (!load_everything) // For quick selection of data when displaying the char menu
  1020. {
  1021. SqlStmt_Free(stmt);
  1022. StringBuf_Destroy(&msg_buf);
  1023. return 1;
  1024. }
  1025. //read memo data
  1026. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  1027. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", schema_config.memo_db, MAX_MEMOPOINTS)
  1028. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1029. || SQL_ERROR == SqlStmt_Execute(stmt)
  1030. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL)
  1031. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL)
  1032. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) )
  1033. SqlStmt_ShowDebug(stmt);
  1034. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1035. {
  1036. tmp_point.map = mapindex_name2id(point_map);
  1037. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  1038. }
  1039. StringBuf_AppendStr(&msg_buf, " memo");
  1040. //read skill
  1041. //`skill` (`char_id`, `id`, `lv`)
  1042. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv`,`flag` FROM `%s` WHERE `char_id`=? LIMIT %d", schema_config.skill_db, MAX_SKILL)
  1043. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1044. || SQL_ERROR == SqlStmt_Execute(stmt)
  1045. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_UINT16, &tmp_skill.id , 0, NULL, NULL)
  1046. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UINT8 , &tmp_skill.lv , 0, NULL, NULL)
  1047. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT8 , &tmp_skill.flag, 0, NULL, NULL) )
  1048. SqlStmt_ShowDebug(stmt);
  1049. if( tmp_skill.flag != SKILL_FLAG_PERM_GRANTED )
  1050. tmp_skill.flag = SKILL_FLAG_PERMANENT;
  1051. for( i = 0; skill_count < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ ) {
  1052. if( tmp_skill.id > 0 && tmp_skill.id < MAX_SKILL_ID ) {
  1053. memcpy(&p->skill[i], &tmp_skill, sizeof(tmp_skill));
  1054. skill_count++;
  1055. }
  1056. else
  1057. 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);
  1058. }
  1059. StringBuf_Printf(&msg_buf, " %d skills", skill_count);
  1060. //read friends
  1061. //`friends` (`char_id`, `friend_account`, `friend_id`)
  1062. 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", schema_config.char_db, schema_config.friend_db, MAX_FRIENDS)
  1063. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1064. || SQL_ERROR == SqlStmt_Execute(stmt)
  1065. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL)
  1066. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL)
  1067. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) )
  1068. SqlStmt_ShowDebug(stmt);
  1069. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1070. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  1071. StringBuf_AppendStr(&msg_buf, " friends");
  1072. #ifdef HOTKEY_SAVING
  1073. //read hotkeys
  1074. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  1075. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", schema_config.hotkey_db)
  1076. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1077. || SQL_ERROR == SqlStmt_Execute(stmt)
  1078. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL)
  1079. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL)
  1080. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL)
  1081. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) )
  1082. SqlStmt_ShowDebug(stmt);
  1083. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  1084. {
  1085. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS )
  1086. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  1087. else
  1088. 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);
  1089. }
  1090. StringBuf_AppendStr(&msg_buf, " hotkeys");
  1091. #endif
  1092. /* Mercenary Owner DataBase */
  1093. mercenary_owner_fromsql(char_id, p);
  1094. StringBuf_AppendStr(&msg_buf, " mercenary");
  1095. if (charserv_config.save_log)
  1096. ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, StringBuf_Value(&msg_buf)); //ok. all data load successfully!
  1097. SqlStmt_Free(stmt);
  1098. cp = (struct mmo_charstatus *)idb_ensure(char_db_, char_id, char_create_charstatus);
  1099. memcpy(cp, p, sizeof(struct mmo_charstatus));
  1100. StringBuf_Destroy(&msg_buf);
  1101. return 1;
  1102. }
  1103. //==========================================================================================================
  1104. int char_mmo_sql_init(void) {
  1105. char_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  1106. ShowStatus("Characters per Account: '%d'.\n", charserv_config.char_config.char_per_account);
  1107. //the 'set offline' part is now in check_login_conn ...
  1108. //if the server connects to loginserver
  1109. //it will dc all off players
  1110. //and send the loginserver the new state....
  1111. // Force all users offline in sql when starting char-server
  1112. // (useful when servers crashs and don't clean the database)
  1113. char_set_all_offline_sql();
  1114. return 0;
  1115. }
  1116. //-----------------------------------
  1117. // Function to change chararcter's names
  1118. //-----------------------------------
  1119. int char_rename_char_sql(struct char_session_data *sd, uint32 char_id)
  1120. {
  1121. struct mmo_charstatus char_dat;
  1122. char esc_name[NAME_LENGTH*2+1];
  1123. if( sd->new_name[0] == 0 ) // Not ready for rename
  1124. return 2;
  1125. if( !char_mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed.
  1126. return 2;
  1127. // If the new name is exactly the same as the old one
  1128. if( !strcmp( sd->new_name, char_dat.name ) )
  1129. return 0;
  1130. if( char_dat.rename == 0 )
  1131. return 1;
  1132. if( !charserv_config.char_config.char_rename_party && char_dat.party_id ){
  1133. return 6;
  1134. }
  1135. if( !charserv_config.char_config.char_rename_guild && char_dat.guild_id ){
  1136. return 5;
  1137. }
  1138. Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH));
  1139. switch( char_check_char_name( sd->new_name, esc_name ) ){
  1140. case 0:
  1141. break;
  1142. case -1:
  1143. // character already exists
  1144. return 4;
  1145. default:
  1146. return 8;
  1147. }
  1148. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `name` = '%s', `rename` = '%d' WHERE `char_id` = '%d'", schema_config.char_db, esc_name, --char_dat.rename, char_id) )
  1149. {
  1150. Sql_ShowDebug(sql_handle);
  1151. return 3;
  1152. }
  1153. // Update party and party members with the new player name
  1154. if( char_dat.party_id )
  1155. inter_party_charname_changed(char_dat.party_id, char_id, sd->new_name);
  1156. // Change character's name into guild_db.
  1157. if( char_dat.guild_id )
  1158. inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name);
  1159. safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH);
  1160. memset(sd->new_name,0,sizeof(sd->new_name));
  1161. // log change
  1162. if( charserv_config.log_char )
  1163. {
  1164. 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`)"
  1165. "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')",
  1166. schema_config.charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) )
  1167. Sql_ShowDebug(sql_handle);
  1168. }
  1169. return 0;
  1170. }
  1171. int char_check_char_name(char * name, char * esc_name)
  1172. {
  1173. int i;
  1174. // check length of character name
  1175. if( name[0] == '\0' )
  1176. return -2; // empty character name
  1177. /**
  1178. * The client does not allow you to create names with less than 4 characters, however,
  1179. * the use of WPE can bypass this, and this fixes the exploit.
  1180. **/
  1181. if( strlen( name ) < 4 )
  1182. return -2;
  1183. // check content of character name
  1184. if( remove_control_chars(name) )
  1185. return -2; // control chars in name
  1186. // check for reserved names
  1187. if( strcmpi(name, charserv_config.wisp_server_name) == 0 )
  1188. return -1; // nick reserved for internal server messages
  1189. // check for the channel symbol
  1190. if( name[0] == '#' )
  1191. return -2;
  1192. // Check Authorised letters/symbols in the name of the character
  1193. if( charserv_config.char_config.char_name_option == 1 )
  1194. { // only letters/symbols in char_name_letters are authorised
  1195. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1196. if( strchr(charserv_config.char_config.char_name_letters, name[i]) == NULL )
  1197. return -2;
  1198. }
  1199. else if( charserv_config.char_config.char_name_option == 2 )
  1200. { // letters/symbols in char_name_letters are forbidden
  1201. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1202. if( strchr(charserv_config.char_config.char_name_letters, name[i]) != NULL )
  1203. return -2;
  1204. }
  1205. if( charserv_config.char_config.name_ignoring_case ) {
  1206. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE BINARY `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1207. Sql_ShowDebug(sql_handle);
  1208. return -2;
  1209. }
  1210. } else {
  1211. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1212. Sql_ShowDebug(sql_handle);
  1213. return -2;
  1214. }
  1215. }
  1216. if( Sql_NumRows(sql_handle) > 0 )
  1217. return -1; // name already exists
  1218. return 0;
  1219. }
  1220. //-----------------------------------
  1221. // Function to create a new character
  1222. //-----------------------------------
  1223. #if PACKETVER >= 20120307
  1224. #if PACKETVER >= 20151001
  1225. int char_make_new_char_sql(struct char_session_data* sd, char* name_, int slot, int hair_color, int hair_style, short start_job, short unknown, int sex) { // TODO: Unknown byte
  1226. #else
  1227. int char_make_new_char_sql(struct char_session_data* sd, char* name_, int slot, int hair_color, int hair_style) {
  1228. #endif
  1229. int str = 1, agi = 1, vit = 1, int_ = 1, dex = 1, luk = 1;
  1230. #else
  1231. int char_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) {
  1232. #endif
  1233. char name[NAME_LENGTH];
  1234. char esc_name[NAME_LENGTH*2+1];
  1235. struct point tmp_start_point[MAX_STARTPOINT];
  1236. struct startitem tmp_start_items[MAX_STARTITEM];
  1237. uint32 char_id;
  1238. int flag, k, start_point_idx = rnd() % charserv_config.start_point_count;
  1239. safestrncpy(name, name_, NAME_LENGTH);
  1240. normalize_name(name,TRIM_CHARS);
  1241. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1242. memset(tmp_start_point, 0, MAX_STARTPOINT * sizeof(struct point));
  1243. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1244. memcpy(tmp_start_point, charserv_config.start_point, MAX_STARTPOINT * sizeof(struct point));
  1245. memcpy(tmp_start_items, charserv_config.start_items, MAX_STARTITEM * sizeof(struct startitem));
  1246. flag = char_check_char_name(name,esc_name);
  1247. if( flag < 0 )
  1248. return flag;
  1249. //check other inputs
  1250. #if PACKETVER >= 20120307
  1251. #if PACKETVER >= 20151001
  1252. switch(sex) {
  1253. case SEX_FEMALE:
  1254. sex = 'F';
  1255. break;
  1256. case SEX_MALE:
  1257. sex = 'M';
  1258. break;
  1259. default:
  1260. sex = 'U';
  1261. break;
  1262. }
  1263. #endif
  1264. if(slot < 0 || slot >= sd->char_slots)
  1265. #else
  1266. if((slot < 0 || slot >= sd->char_slots) // slots
  1267. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1268. || (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
  1269. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1270. #endif
  1271. #if PACKETVER >= 20100413
  1272. return -4; // invalid slot
  1273. #else
  1274. return -2; // invalid input
  1275. #endif
  1276. // check the number of already existing chars in this account
  1277. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", schema_config.char_db, sd->account_id) )
  1278. Sql_ShowDebug(sql_handle);
  1279. #ifdef VIP_ENABLE
  1280. if( Sql_NumRows(sql_handle) >= MAX_CHARS )
  1281. return -2; // character account limit exceeded
  1282. #else
  1283. if( Sql_NumRows(sql_handle) >= sd->char_slots )
  1284. return -2; // character account limit exceeded
  1285. #endif
  1286. // check char slot
  1287. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' LIMIT 1", schema_config.char_db, sd->account_id, slot) )
  1288. Sql_ShowDebug(sql_handle);
  1289. if( Sql_NumRows(sql_handle) > 0 )
  1290. return -2; // slot already in use
  1291. // validation success, log result
  1292. if (charserv_config.log_char) {
  1293. 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`)"
  1294. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1295. schema_config.charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1296. Sql_ShowDebug(sql_handle);
  1297. }
  1298. #if PACKETVER >= 20151001
  1299. if (start_job != JOB_NOVICE && start_job != JOB_SUMMONER)
  1300. return -2; // Invalid job
  1301. // Check for Doram based information.
  1302. if (start_job == JOB_SUMMONER) { // Check for just this job for now.
  1303. memset(tmp_start_point, 0, MAX_STARTPOINT * sizeof(struct point));
  1304. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1305. memcpy(tmp_start_point, charserv_config.start_point_doram, MAX_STARTPOINT * sizeof(struct point));
  1306. memcpy(tmp_start_items, charserv_config.start_items_doram, MAX_STARTITEM * sizeof(struct startitem));
  1307. start_point_idx = rnd() % charserv_config.start_point_count_doram;
  1308. }
  1309. #endif
  1310. //Insert the new char entry to the database
  1311. #if PACKETVER >= 20151001
  1312. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `class`, `zeny`, `status_point`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1313. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`, `sex`) VALUES ("
  1314. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%u', '%u', '%u', '%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d', '%c')",
  1315. schema_config.char_db, sd->account_id , slot, esc_name, start_job, charserv_config.start_zeny, 48, str, agi, vit, int_, dex, luk,
  1316. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1317. mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, sex) )
  1318. #elif PACKETVER >= 20120307
  1319. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `status_point`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1320. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1321. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%u', '%u', '%u', '%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1322. schema_config.char_db, sd->account_id , slot, esc_name, charserv_config.start_zeny, 48, str, agi, vit, int_, dex, luk,
  1323. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1324. mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y) )
  1325. #else
  1326. 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`,"
  1327. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1328. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%u', '%u', '%u', '%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1329. schema_config.char_db, sd->account_id , slot, esc_name, charserv_config.start_zeny, str, agi, vit, int_, dex, luk,
  1330. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1331. mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y) )
  1332. #endif
  1333. {
  1334. Sql_ShowDebug(sql_handle);
  1335. return -2; //No, stop the procedure!
  1336. }
  1337. //Retrieve the newly auto-generated char id
  1338. char_id = (int)Sql_LastInsertId(sql_handle);
  1339. //Give the char the default items
  1340. for (k = 0; k <= MAX_STARTITEM && tmp_start_items[k].nameid != 0; k++) {
  1341. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%hu', '%hu', '%hu', '%d')", schema_config.inventory_db, char_id, tmp_start_items[k].nameid, tmp_start_items[k].amount, tmp_start_items[k].pos, 1) )
  1342. Sql_ShowDebug(sql_handle);
  1343. }
  1344. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1345. return char_id;
  1346. }
  1347. /*----------------------------------------------------------------------------------------------------------*/
  1348. /* Divorce Players */
  1349. /*----------------------------------------------------------------------------------------------------------*/
  1350. int char_divorce_char_sql(int partner_id1, int partner_id2){
  1351. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d' LIMIT 2", schema_config.char_db, partner_id1, partner_id2) )
  1352. Sql_ShowDebug(sql_handle);
  1353. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%hu' OR `nameid`='%hu') AND (`char_id`='%d' OR `char_id`='%d') LIMIT 2", schema_config.inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) )
  1354. Sql_ShowDebug(sql_handle);
  1355. chmapif_send_ackdivorce(partner_id1, partner_id2);
  1356. return 0;
  1357. }
  1358. /*----------------------------------------------------------------------------------------------------------*/
  1359. /* Delete char - davidsiaw */
  1360. /*----------------------------------------------------------------------------------------------------------*/
  1361. /* Returns 0 if successful
  1362. * Returns < 0 for error
  1363. */
  1364. enum e_char_del_response char_delete(struct char_session_data* sd, uint32 char_id){
  1365. char name[NAME_LENGTH];
  1366. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1367. uint32 account_id;
  1368. int party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id, elemental_id;
  1369. time_t delete_date;
  1370. char *data;
  1371. size_t len;
  1372. int i, k;
  1373. ARR_FIND(0, MAX_CHARS, i, sd->found_char[i] == char_id);
  1374. // Such a character does not exist in the account
  1375. if (i == MAX_CHARS) {
  1376. ShowInfo("Char deletion aborted: %s, Account ID: %u, Character ID: %u\n", name, sd->account_id, char_id);
  1377. return CHAR_DELETE_NOTFOUND;
  1378. }
  1379. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `name`,`account_id`,`party_id`,`guild_id`,`base_level`,`homun_id`,`partner_id`,`father`,`mother`,`elemental_id`,`delete_date` FROM `%s` WHERE `account_id`='%u' AND `char_id`='%u'", schema_config.char_db, sd->account_id, char_id)){
  1380. Sql_ShowDebug(sql_handle);
  1381. return CHAR_DELETE_DATABASE;
  1382. }
  1383. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1384. {
  1385. ShowInfo("Char deletion aborted: %s, Account ID: %u, Character ID: %u\n", name, sd->account_id, char_id);
  1386. Sql_FreeResult(sql_handle);
  1387. return CHAR_DELETE_NOTFOUND;
  1388. }
  1389. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1390. Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data);
  1391. Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data);
  1392. Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data);
  1393. Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data);
  1394. Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data);
  1395. Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data);
  1396. Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data);
  1397. Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data);
  1398. Sql_GetData(sql_handle, 9, &data, NULL); elemental_id = atoi(data);
  1399. Sql_GetData(sql_handle,10, &data, NULL); delete_date = strtoul(data, NULL, 10);
  1400. Sql_EscapeStringLen(sql_handle, esc_name, name, zmin(len, NAME_LENGTH));
  1401. Sql_FreeResult(sql_handle);
  1402. //check for config char del condition [Lupus]
  1403. if( ( charserv_config.char_config.char_del_level > 0 && base_level >= charserv_config.char_config.char_del_level )
  1404. || ( charserv_config.char_config.char_del_level < 0 && base_level <= -charserv_config.char_config.char_del_level )
  1405. ) {
  1406. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1407. return CHAR_DELETE_BASELEVEL;
  1408. }
  1409. if (charserv_config.char_config.char_del_restriction&CHAR_DEL_RESTRICT_GUILD && guild_id) // character is in guild
  1410. {
  1411. ShowInfo("Char deletion aborted: %s, Guild ID: %i\n", name, guild_id);
  1412. return CHAR_DELETE_GUILD;
  1413. }
  1414. if (charserv_config.char_config.char_del_restriction&CHAR_DEL_RESTRICT_PARTY && party_id) // character is in party
  1415. {
  1416. ShowInfo("Char deletion aborted: %s, Party ID: %i\n", name, party_id);
  1417. return CHAR_DELETE_PARTY;
  1418. }
  1419. if( charserv_config.char_config.char_del_delay > 0 && ( !delete_date || delete_date > time(NULL) ) ){ // not queued or delay not yet passed
  1420. ShowInfo("Char deletion aborted: %s, Time was not set or has not been reached ye\n", name );
  1421. return CHAR_DELETE_TIME;
  1422. }
  1423. /* Divorce [Wizputer] */
  1424. if( partner_id )
  1425. char_divorce_char_sql(char_id, partner_id);
  1426. /* De-addopt [Zephyrus] */
  1427. if( father_id || mother_id )
  1428. { // Char is Baby
  1429. unsigned char buf[64];
  1430. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", schema_config.char_db, father_id, mother_id) )
  1431. Sql_ShowDebug(sql_handle);
  1432. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410' AND (`char_id`='%d' OR `char_id`='%d')", schema_config.skill_db, father_id, mother_id) )
  1433. Sql_ShowDebug(sql_handle);
  1434. WBUFW(buf,0) = 0x2b25;
  1435. WBUFL(buf,2) = father_id;
  1436. WBUFL(buf,6) = mother_id;
  1437. WBUFL(buf,10) = char_id; // Baby
  1438. chmapif_sendall(buf,14);
  1439. }
  1440. //Make the character leave the party [Skotlex]
  1441. if (party_id)
  1442. inter_party_leave(party_id, account_id, char_id, name);
  1443. /* delete char's pet */
  1444. //Delete the hatched pet if you have one...
  1445. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incubate` = '0'", schema_config.pet_db, char_id) )
  1446. Sql_ShowDebug(sql_handle);
  1447. //Delete all pets that are stored in eggs (inventory + cart)
  1448. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = 256", schema_config.pet_db, schema_config.pet_db, schema_config.inventory_db, schema_config.inventory_db, char_id) )
  1449. Sql_ShowDebug(sql_handle);
  1450. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = 256", schema_config.pet_db, schema_config.pet_db, schema_config.cart_db, schema_config.cart_db, char_id) )
  1451. Sql_ShowDebug(sql_handle);
  1452. /* remove homunculus */
  1453. if( hom_id )
  1454. mapif_homunculus_delete(hom_id);
  1455. /* remove elemental */
  1456. if (elemental_id)
  1457. mapif_elemental_delete(elemental_id);
  1458. /* remove mercenary data */
  1459. mercenary_owner_delete(char_id);
  1460. /* delete char's friends list */
  1461. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.friend_db, char_id) )
  1462. Sql_ShowDebug(sql_handle);
  1463. /* delete char from other's friend list */
  1464. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1465. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", schema_config.friend_db, char_id) )
  1466. Sql_ShowDebug(sql_handle);
  1467. #ifdef HOTKEY_SAVING
  1468. /* delete hotkeys */
  1469. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.hotkey_db, char_id) )
  1470. Sql_ShowDebug(sql_handle);
  1471. #endif
  1472. /* delete inventory */
  1473. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.inventory_db, char_id) )
  1474. Sql_ShowDebug(sql_handle);
  1475. /* delete cart inventory */
  1476. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.cart_db, char_id) )
  1477. Sql_ShowDebug(sql_handle);
  1478. /* delete memo areas */
  1479. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.memo_db, char_id) )
  1480. Sql_ShowDebug(sql_handle);
  1481. /* delete character registry */
  1482. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_str_table, char_id) )
  1483. Sql_ShowDebug(sql_handle);
  1484. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_num_table, char_id) )
  1485. Sql_ShowDebug(sql_handle);
  1486. /* delete skills */
  1487. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.skill_db, char_id) )
  1488. Sql_ShowDebug(sql_handle);
  1489. /* delete mail attachments (only received) */
  1490. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` IN ( SELECT `id` FROM `%s` WHERE `dest_id`='%d' )", schema_config.mail_attachment_db, schema_config.mail_db, char_id))
  1491. Sql_ShowDebug(sql_handle);
  1492. /* delete mails (only received) */
  1493. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `dest_id`='%d'", schema_config.mail_db, char_id))
  1494. Sql_ShowDebug(sql_handle);
  1495. #ifdef ENABLE_SC_SAVING
  1496. /* status changes */
  1497. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", schema_config.scdata_db, account_id, char_id) )
  1498. Sql_ShowDebug(sql_handle);
  1499. #endif
  1500. /* bonus_scripts */
  1501. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.bonus_script_db, char_id) )
  1502. Sql_ShowDebug(sql_handle);
  1503. /* Achievement Data */
  1504. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.achievement_table, char_id))
  1505. Sql_ShowDebug(sql_handle);
  1506. if (charserv_config.log_char) {
  1507. 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')",
  1508. schema_config.charlog_db, account_id, 0, char_id, esc_name) )
  1509. Sql_ShowDebug(sql_handle);
  1510. }
  1511. /* delete character */
  1512. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1513. Sql_ShowDebug(sql_handle);
  1514. /* No need as we used inter_guild_leave [Skotlex]
  1515. // Also delete info from guildtables.
  1516. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1517. Sql_ShowDebug(sql_handle);
  1518. */
  1519. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.guild_db, char_id) )
  1520. Sql_ShowDebug(sql_handle);
  1521. else if( Sql_NumRows(sql_handle) > 0 )
  1522. mapif_parse_BreakGuild(0,guild_id);
  1523. else if( guild_id )
  1524. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1525. // refresh character list cache
  1526. for(k = i; k < MAX_CHARS-1; k++){
  1527. sd->found_char[k] = sd->found_char[k+1];
  1528. }
  1529. sd->found_char[MAX_CHARS-1] = -1;
  1530. return CHAR_DELETE_OK;
  1531. }
  1532. /**
  1533. * This function parse all map-serv attached to this char-serv and increase user count
  1534. * @return numbers of total users
  1535. */
  1536. int char_count_users(void)
  1537. {
  1538. int i, users;
  1539. users = 0;
  1540. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  1541. if (map_server[i].fd > 0) {
  1542. users += map_server[i].users;
  1543. }
  1544. }
  1545. return users;
  1546. }
  1547. // Writes char data to the buffer in the format used by the client.
  1548. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1549. // Returns the size
  1550. int char_mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p)
  1551. {
  1552. unsigned short offset = 0;
  1553. uint8* buf;
  1554. if( buffer == NULL || p == NULL )
  1555. return 0;
  1556. buf = WBUFP(buffer,0);
  1557. WBUFL(buf,0) = p->char_id;
  1558. #if PACKETVER >= 20170830
  1559. WBUFQ(buf,4) = u64min((uint64)p->base_exp, INT64_MAX);
  1560. offset += 4;
  1561. buf = WBUFP(buffer, offset);
  1562. #else
  1563. WBUFL(buf,4) = umin(p->base_exp, INT32_MAX);
  1564. #endif
  1565. WBUFL(buf,8) = p->zeny;
  1566. #if PACKETVER >= 20170830
  1567. WBUFQ(buf,12) = u64min((uint64)p->job_exp, INT64_MAX);
  1568. offset += 4;
  1569. buf = WBUFP(buffer, offset);
  1570. #else
  1571. WBUFL(buf,12) = umin(p->job_exp, INT32_MAX);
  1572. #endif
  1573. WBUFL(buf,16) = p->job_level;
  1574. WBUFL(buf,20) = 0; // probably opt1
  1575. WBUFL(buf,24) = 0; // probably opt2
  1576. WBUFL(buf,28) = p->option;
  1577. WBUFL(buf,32) = p->karma;
  1578. WBUFL(buf,36) = p->manner;
  1579. WBUFW(buf,40) = umin(p->status_point, INT16_MAX);
  1580. WBUFL(buf,42) = p->hp;
  1581. WBUFL(buf,46) = p->max_hp;
  1582. offset+=4;
  1583. buf = WBUFP(buffer,offset);
  1584. WBUFW(buf,46) = min(p->sp, INT16_MAX);
  1585. WBUFW(buf,48) = min(p->max_sp, INT16_MAX);
  1586. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1587. WBUFW(buf,52) = p->class_;
  1588. WBUFW(buf,54) = p->hair;
  1589. #if PACKETVER >= 20141022
  1590. WBUFW(buf,56) = p->body;
  1591. offset+=2;
  1592. buf = WBUFP(buffer,offset);
  1593. #endif
  1594. //When the weapon is sent and your option is riding, the client crashes on login!?
  1595. WBUFW(buf,56) = p->option&(0x20|0x80000|0x100000|0x200000|0x400000|0x800000|0x1000000|0x2000000|0x4000000|0x8000000) ? 0 : p->weapon;
  1596. WBUFW(buf,58) = p->base_level;
  1597. WBUFW(buf,60) = umin(p->skill_point, INT16_MAX);
  1598. WBUFW(buf,62) = p->head_bottom;
  1599. WBUFW(buf,64) = p->shield;
  1600. WBUFW(buf,66) = p->head_top;
  1601. WBUFW(buf,68) = p->head_mid;
  1602. WBUFW(buf,70) = p->hair_color;
  1603. WBUFW(buf,72) = p->clothes_color;
  1604. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1605. WBUFB(buf,98) = (unsigned char)u16min(p->str, UINT8_MAX);
  1606. WBUFB(buf,99) = (unsigned char)u16min(p->agi, UINT8_MAX);
  1607. WBUFB(buf,100) = (unsigned char)u16min(p->vit, UINT8_MAX);
  1608. WBUFB(buf,101) = (unsigned char)u16min(p->int_, UINT8_MAX);
  1609. WBUFB(buf,102) = (unsigned char)u16min(p->dex, UINT8_MAX);
  1610. WBUFB(buf,103) = (unsigned char)u16min(p->luk, UINT8_MAX);
  1611. WBUFW(buf,104) = p->slot;
  1612. WBUFW(buf,106) = ( p->rename > 0 ) ? 0 : 1;
  1613. offset += 2;
  1614. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1615. mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), WBUFCP(buf,108));
  1616. offset += MAP_NAME_LENGTH_EXT;
  1617. #endif
  1618. #if PACKETVER >= 20100803
  1619. #if PACKETVER_CHAR_DELETEDATE
  1620. WBUFL(buf,124) = (p->delete_date?TOL(p->delete_date-time(NULL)):0);
  1621. #else
  1622. WBUFL(buf,124) = TOL(p->delete_date);
  1623. #endif
  1624. offset += 4;
  1625. #endif
  1626. #if PACKETVER >= 20110111
  1627. WBUFL(buf,128) = p->robe;
  1628. offset += 4;
  1629. #endif
  1630. #if PACKETVER != 20111116 //2011-11-16 wants 136, ask gravity.
  1631. #if PACKETVER >= 20110928
  1632. // change slot feature (0 = disabled, otherwise enabled)
  1633. if( (charserv_config.charmove_config.char_move_enabled)==0 )
  1634. WBUFL(buf,132) = 0;
  1635. else if( charserv_config.charmove_config.char_moves_unlimited )
  1636. WBUFL(buf,132) = 1;
  1637. else
  1638. WBUFL(buf,132) = max( 0, (int)p->character_moves );
  1639. offset += 4;
  1640. #endif
  1641. #if PACKETVER >= 20111025
  1642. WBUFL(buf,136) = ( p->rename > 0 ) ? 1 : 0; // (0 = disabled, otherwise displays "Add-Ons" sidebar)
  1643. offset += 4;
  1644. #endif
  1645. #if PACKETVER >= 20141016
  1646. WBUFB(buf,140) = p->sex;// sex - (0 = female, 1 = male, 99 = logindefined)
  1647. offset += 1;
  1648. #endif
  1649. #endif
  1650. return 106+offset;
  1651. }
  1652. int char_married(int pl1, int pl2)
  1653. {
  1654. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, pl1) )
  1655. Sql_ShowDebug(sql_handle);
  1656. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1657. {
  1658. char* data;
  1659. Sql_GetData(sql_handle, 0, &data, NULL);
  1660. if( pl2 == atoi(data) )
  1661. {
  1662. Sql_FreeResult(sql_handle);
  1663. return 1;
  1664. }
  1665. }
  1666. Sql_FreeResult(sql_handle);
  1667. return 0;
  1668. }
  1669. int char_child(int parent_id, int child_id)
  1670. {
  1671. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, parent_id) )
  1672. Sql_ShowDebug(sql_handle);
  1673. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1674. {
  1675. char* data;
  1676. Sql_GetData(sql_handle, 0, &data, NULL);
  1677. if( child_id == atoi(data) )
  1678. {
  1679. Sql_FreeResult(sql_handle);
  1680. return 1;
  1681. }
  1682. }
  1683. Sql_FreeResult(sql_handle);
  1684. return 0;
  1685. }
  1686. int char_family(int cid1, int cid2, int cid3)
  1687. {
  1688. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", schema_config.char_db, cid1, cid2, cid3) )
  1689. Sql_ShowDebug(sql_handle);
  1690. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1691. {
  1692. int charid;
  1693. int partnerid;
  1694. int childid;
  1695. char* data;
  1696. Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data);
  1697. Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data);
  1698. Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data);
  1699. if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) ||
  1700. (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) ||
  1701. (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) )
  1702. {
  1703. Sql_FreeResult(sql_handle);
  1704. return childid;
  1705. }
  1706. }
  1707. Sql_FreeResult(sql_handle);
  1708. return 0;
  1709. }
  1710. //----------------------------------------------------------------------
  1711. // Force disconnection of an online player (with account value) by [Yor]
  1712. //----------------------------------------------------------------------
  1713. void char_disconnect_player(uint32 account_id)
  1714. {
  1715. int i;
  1716. struct char_session_data* sd;
  1717. // disconnect player if online on char-server
  1718. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1719. if( i < fd_max )
  1720. set_eof(i);
  1721. }
  1722. /**
  1723. * Set 'flag' value of char_session_data
  1724. * @param account_id
  1725. * @param value
  1726. * @param set True: set the value by using '|= val', False: unset the value by using '&= ~val'
  1727. **/
  1728. void char_set_session_flag_(int account_id, int val, bool set) {
  1729. int i;
  1730. struct char_session_data* sd;
  1731. ARR_FIND(0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id);
  1732. if (i < fd_max) {
  1733. if (set)
  1734. sd->flag |= val;
  1735. else
  1736. sd->flag &= ~val;
  1737. }
  1738. }
  1739. void char_auth_ok(int fd, struct char_session_data *sd) {
  1740. struct online_char_data* character;
  1741. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1742. { // check if character is not online already. [Skotlex]
  1743. if (character->server > -1)
  1744. { //Character already online. KICK KICK KICK
  1745. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  1746. if (character->waiting_disconnect == INVALID_TIMER)
  1747. character->waiting_disconnect = add_timer(gettick()+20000, char_chardb_waiting_disconnect, character->account_id, 0);
  1748. chclif_send_auth_result(fd,8);
  1749. return;
  1750. }
  1751. if (character->fd >= 0 && character->fd != fd)
  1752. { //There's already a connection from this account that hasn't picked a char yet.
  1753. chclif_send_auth_result(fd,8);
  1754. return;
  1755. }
  1756. character->fd = fd;
  1757. }
  1758. chlogif_send_reqaccdata(login_fd,sd); // request account data
  1759. // mark session as 'authed'
  1760. sd->auth = true;
  1761. // set char online on charserver
  1762. char_set_charselect(sd->account_id);
  1763. // continues when account data is received...
  1764. }
  1765. void char_read_fame_list(void)
  1766. {
  1767. int i;
  1768. char* data;
  1769. size_t len;
  1770. // Empty ranking lists
  1771. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  1772. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  1773. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  1774. // Build Blacksmith ranking list
  1775. 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' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, JOB_MECHANIC, JOB_MECHANIC_T, JOB_BABY_MECHANIC, fame_list_size_smith) )
  1776. Sql_ShowDebug(sql_handle);
  1777. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1778. {
  1779. // char_id
  1780. Sql_GetData(sql_handle, 0, &data, NULL);
  1781. smith_fame_list[i].id = atoi(data);
  1782. // fame
  1783. Sql_GetData(sql_handle, 1, &data, &len);
  1784. smith_fame_list[i].fame = atoi(data);
  1785. // name
  1786. Sql_GetData(sql_handle, 2, &data, &len);
  1787. memcpy(smith_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1788. }
  1789. // Build Alchemist ranking list
  1790. 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' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, JOB_GENETIC, JOB_GENETIC_T, JOB_BABY_GENETIC, fame_list_size_chemist) )
  1791. Sql_ShowDebug(sql_handle);
  1792. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1793. {
  1794. // char_id
  1795. Sql_GetData(sql_handle, 0, &data, NULL);
  1796. chemist_fame_list[i].id = atoi(data);
  1797. // fame
  1798. Sql_GetData(sql_handle, 1, &data, &len);
  1799. chemist_fame_list[i].fame = atoi(data);
  1800. // name
  1801. Sql_GetData(sql_handle, 2, &data, &len);
  1802. memcpy(chemist_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1803. }
  1804. // Build Taekwon ranking list
  1805. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_TAEKWON, JOB_BABY_TAEKWON, fame_list_size_taekwon) )
  1806. Sql_ShowDebug(sql_handle);
  1807. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1808. {
  1809. // char_id
  1810. Sql_GetData(sql_handle, 0, &data, NULL);
  1811. taekwon_fame_list[i].id = atoi(data);
  1812. // fame
  1813. Sql_GetData(sql_handle, 1, &data, &len);
  1814. taekwon_fame_list[i].fame = atoi(data);
  1815. // name
  1816. Sql_GetData(sql_handle, 2, &data, &len);
  1817. memcpy(taekwon_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1818. }
  1819. Sql_FreeResult(sql_handle);
  1820. }
  1821. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  1822. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  1823. int char_loadName(uint32 char_id, char* name){
  1824. char* data;
  1825. size_t len;
  1826. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1827. Sql_ShowDebug(sql_handle);
  1828. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1829. {
  1830. Sql_GetData(sql_handle, 0, &data, &len);
  1831. safestrncpy(name, data, NAME_LENGTH);
  1832. return 1;
  1833. }
  1834. else
  1835. {
  1836. safestrncpy(name, charserv_config.char_config.unknown_char_name, NAME_LENGTH);
  1837. }
  1838. return 0;
  1839. }
  1840. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  1841. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  1842. int char_search_mapserver(unsigned short map, uint32 ip, uint16 port){
  1843. int i, j;
  1844. for(i = 0; i < ARRAYLENGTH(map_server); i++)
  1845. {
  1846. if (map_server[i].fd > 0
  1847. && (ip == (uint32)-1 || map_server[i].ip == ip)
  1848. && (port == (uint16)-1 || map_server[i].port == port))
  1849. {
  1850. for (j = 0; map_server[i].map[j]; j++)
  1851. if (map_server[i].map[j] == map)
  1852. return i;
  1853. }
  1854. }
  1855. return -1;
  1856. }
  1857. /**
  1858. * Test to know if an IP come from LAN or WAN.
  1859. * @param ip: ip to check if in auth network
  1860. * @return 0 if from wan, or subnet_map_ip if lan
  1861. **/
  1862. int char_lan_subnetcheck(uint32 ip){
  1863. int i;
  1864. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  1865. if( i < subnet_count ) {
  1866. 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));
  1867. return subnet[i].map_ip;
  1868. } else {
  1869. ShowInfo("Subnet check [%u.%u.%u.%u]: " CL_CYAN "WAN" CL_RESET "\n", CONVIP(ip));
  1870. return 0;
  1871. }
  1872. }
  1873. // Console Command Parser [Wizputer]
  1874. //FIXME to be remove (moved to cnslif / will be done once map/char/login, all have their cnslif interface ready)
  1875. int parse_console(const char* buf){
  1876. return cnslif_parse(buf);
  1877. }
  1878. #if PACKETVER_SUPPORTS_PINCODE
  1879. //------------------------------------------------
  1880. //Pincode system
  1881. //------------------------------------------------
  1882. int char_pincode_compare( int fd, struct char_session_data* sd, char* pin ){
  1883. if( strcmp( sd->pincode, pin ) == 0 ){
  1884. sd->pincode_try = 0;
  1885. return 1;
  1886. }else{
  1887. chclif_pincode_sendstate( fd, sd, PINCODE_WRONG );
  1888. if( charserv_config.pincode_config.pincode_maxtry && ++sd->pincode_try >= charserv_config.pincode_config.pincode_maxtry ){
  1889. chlogif_pincode_notifyLoginPinError( sd->account_id );
  1890. }
  1891. return 0;
  1892. }
  1893. }
  1894. void char_pincode_decrypt( uint32 userSeed, char* pin ){
  1895. int i;
  1896. char tab[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  1897. char *buf;
  1898. for( i = 1; i < 10; i++ ){
  1899. int pos;
  1900. uint32 multiplier = 0x3498, baseSeed = 0x881234;
  1901. userSeed = baseSeed + userSeed * multiplier;
  1902. pos = userSeed % ( i + 1 );
  1903. if( i != pos ){
  1904. tab[i] ^= tab[pos];
  1905. tab[pos] ^= tab[i];
  1906. tab[i] ^= tab[pos];
  1907. }
  1908. }
  1909. buf = (char *)aMalloc( sizeof(char) * ( PINCODE_LENGTH + 1 ) );
  1910. memset( buf, 0, PINCODE_LENGTH + 1 );
  1911. for( i = 0; i < PINCODE_LENGTH; i++ ){
  1912. sprintf( buf + i, "%d", tab[pin[i] - '0'] );
  1913. }
  1914. strcpy( pin, buf );
  1915. aFree( buf );
  1916. }
  1917. #endif
  1918. //------------------------------------------------
  1919. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  1920. //replies/disconnect the player we tried to kick. [Skotlex]
  1921. //------------------------------------------------
  1922. int char_chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data)
  1923. {
  1924. struct online_char_data* character;
  1925. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  1926. { //Mark it offline due to timeout.
  1927. character->waiting_disconnect = INVALID_TIMER;
  1928. char_set_char_offline(character->char_id, character->account_id);
  1929. }
  1930. return 0;
  1931. }
  1932. /**
  1933. * @see DBApply
  1934. */
  1935. static int char_online_data_cleanup_sub(DBKey key, DBData *data, va_list ap)
  1936. {
  1937. struct online_char_data *character= (struct online_char_data *)db_data2ptr(data);
  1938. if (character->fd != -1)
  1939. return 0; //Character still connected
  1940. if (character->server == -2) //Unknown server.. set them offline
  1941. char_set_char_offline(character->char_id, character->account_id);
  1942. if (character->server < 0)
  1943. //Free data from players that have not been online for a while.
  1944. db_remove(online_char_db, key);
  1945. return 0;
  1946. }
  1947. static int char_online_data_cleanup(int tid, unsigned int tick, int id, intptr_t data){
  1948. online_char_db->foreach(online_char_db, char_online_data_cleanup_sub);
  1949. return 0;
  1950. }
  1951. static int char_clan_member_cleanup( int tid, unsigned int tick, int id, intptr_t data ){
  1952. // Auto removal is disabled
  1953. if( charserv_config.clan_remove_inactive_days <= 0 ){
  1954. return 0;
  1955. }
  1956. if( SQL_ERROR == Sql_Query( sql_handle, "UPDATE `%s` SET `clan_id`='0' WHERE `online`='0' AND `clan_id`<>'0' AND `last_login` IS NOT NULL AND `last_login` <= NOW() - INTERVAL %d DAY", schema_config.char_db, charserv_config.clan_remove_inactive_days ) ){
  1957. Sql_ShowDebug(sql_handle);
  1958. }
  1959. return 0;
  1960. }
  1961. //----------------------------------
  1962. // Reading Lan Support configuration
  1963. // Rewrote: Anvanced subnet check [LuzZza]
  1964. //----------------------------------
  1965. int char_lan_config_read(const char *lancfgName) {
  1966. FILE *fp;
  1967. int line_num = 0, s_subnet=ARRAYLENGTH(subnet);
  1968. char line[1024], w1[64], w2[64], w3[64], w4[64];
  1969. if((fp = fopen(lancfgName, "r")) == NULL) {
  1970. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  1971. return 1;
  1972. }
  1973. while(fgets(line, sizeof(line), fp)) {
  1974. line_num++;
  1975. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  1976. continue;
  1977. if(sscanf(line,"%63[^:]: %63[^:]:%63[^:]:%63[^\r\n]", w1, w2, w3, w4) != 4) {
  1978. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  1979. continue;
  1980. }
  1981. remove_control_chars(w1);
  1982. remove_control_chars(w2);
  1983. remove_control_chars(w3);
  1984. remove_control_chars(w4);
  1985. if( strcmpi(w1, "subnet") == 0 ){
  1986. if(subnet_count>=s_subnet) { //We skip instead of break in case we want to add other conf in that file.
  1987. ShowError("%s: Too many subnets defined, skipping line %d...\n", lancfgName, line_num);
  1988. continue;
  1989. }
  1990. subnet[subnet_count].mask = str2ip(w2);
  1991. subnet[subnet_count].char_ip = str2ip(w3);
  1992. subnet[subnet_count].map_ip = str2ip(w4);
  1993. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  1994. {
  1995. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  1996. continue;
  1997. }
  1998. subnet_count++;
  1999. }
  2000. }
  2001. if( subnet_count > 1 ) /* only useful if there is more than 1 */
  2002. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  2003. fclose(fp);
  2004. return 0;
  2005. }
  2006. /**
  2007. * Check if our table are all ok in sqlserver
  2008. * Char tables to check
  2009. * @return 0:fail, 1:success
  2010. */
  2011. bool char_checkdb(void){
  2012. int i;
  2013. const char* sqltable[] = {
  2014. schema_config.char_db, schema_config.hotkey_db, schema_config.scdata_db, schema_config.cart_db,
  2015. schema_config.inventory_db, schema_config.charlog_db,
  2016. schema_config.char_reg_str_table, schema_config.char_reg_num_table, schema_config.acc_reg_str_table,
  2017. schema_config.acc_reg_num_table, schema_config.skill_db, schema_config.interlog_db, schema_config.memo_db,
  2018. schema_config.guild_db, schema_config.guild_alliance_db, schema_config.guild_castle_db,
  2019. schema_config.guild_expulsion_db, schema_config.guild_member_db,
  2020. schema_config.guild_skill_db, schema_config.guild_position_db, schema_config.guild_storage_db,
  2021. schema_config.party_db, schema_config.pet_db, schema_config.friend_db, schema_config.mail_db,
  2022. schema_config.auction_db, schema_config.quest_db, schema_config.homunculus_db, schema_config.skill_homunculus_db,
  2023. schema_config.mercenary_db, schema_config.mercenary_owner_db,
  2024. schema_config.elemental_db, schema_config.ragsrvinfo_db, schema_config.skillcooldown_db, schema_config.bonus_script_db,
  2025. schema_config.clan_table, schema_config.clan_alliance_table, schema_config.mail_attachment_db, schema_config.achievement_table
  2026. };
  2027. ShowInfo("Start checking DB integrity\n");
  2028. for (i=0; i<ARRAYLENGTH(sqltable); i++){ //check if they all exist and we can acces them in sql-server
  2029. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` LIMIT 1;", sqltable[i]) ){
  2030. Sql_ShowDebug(sql_handle);
  2031. return false;
  2032. }
  2033. }
  2034. //checking char_db
  2035. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,"
  2036. "`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,"
  2037. "`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,"
  2038. "`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,`hair_color`,`clothes_color`,`weapon`,"
  2039. "`shield`,`head_top`,`head_mid`,`head_bottom`,`robe`,`last_map`,`last_x`,`last_y`,`save_map`,"
  2040. "`save_x`,`save_y`,`partner_id`,`online`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,"
  2041. "`moves`,`unban_time`,`font`,`sex`,`hotkey_rowshift`,`clan_id`,`last_login`,`title_id`,`show_equip`"
  2042. " FROM `%s` LIMIT 1;", schema_config.char_db) ){
  2043. Sql_ShowDebug(sql_handle);
  2044. return false;
  2045. }
  2046. //checking charlog_db
  2047. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`char_msg`,`account_id`,`char_num`,`name`,"
  2048. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`"
  2049. " FROM `%s` LIMIT 1;", schema_config.charlog_db) ){
  2050. Sql_ShowDebug(sql_handle);
  2051. return false;
  2052. }
  2053. //checking char_reg_str_table
  2054. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_str_table) ) {
  2055. Sql_ShowDebug(sql_handle);
  2056. return false;
  2057. }
  2058. //checking char_reg_num_table
  2059. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_num_table) ) {
  2060. Sql_ShowDebug(sql_handle);
  2061. return false;
  2062. }
  2063. //checking global_acc_reg_str_table
  2064. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_str_table) ) {
  2065. Sql_ShowDebug(sql_handle);
  2066. return false;
  2067. }
  2068. //checking global_acc_reg_num_table
  2069. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_num_table) ) {
  2070. Sql_ShowDebug(sql_handle);
  2071. return false;
  2072. }
  2073. //checking hotkey_db
  2074. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`hotkey`,`type`,`itemskill_id`,`skill_lvl`"
  2075. " FROM `%s` LIMIT 1;", schema_config.hotkey_db) ){
  2076. Sql_ShowDebug(sql_handle);
  2077. return false;
  2078. }
  2079. //checking scdata_db
  2080. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`type`,`tick`,`val1`,`val2`,`val3`,`val4`"
  2081. " FROM `%s` LIMIT 1;", schema_config.scdata_db) ){
  2082. Sql_ShowDebug(sql_handle);
  2083. return false;
  2084. }
  2085. //checking skill_db
  2086. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`id`,`lv`,`flag` FROM `%s` LIMIT 1;", schema_config.skill_db) ){
  2087. Sql_ShowDebug(sql_handle);
  2088. return false;
  2089. }
  2090. //checking interlog_db
  2091. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`log` FROM `%s` LIMIT 1;", schema_config.interlog_db) ){
  2092. Sql_ShowDebug(sql_handle);
  2093. return false;
  2094. }
  2095. //checking memo_db
  2096. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `memo_id`,`char_id`,`map`,`x`,`y` FROM `%s` LIMIT 1;", schema_config.memo_db) ){
  2097. Sql_ShowDebug(sql_handle);
  2098. return false;
  2099. }
  2100. //checking guild_db
  2101. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`name`,`char_id`,`master`,`guild_lv`,"
  2102. "`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`mes1`,`mes2`,"
  2103. "`emblem_len`,`emblem_id`,`emblem_data`,`last_master_change`"
  2104. " FROM `%s` LIMIT 1;", schema_config.guild_db) ){
  2105. Sql_ShowDebug(sql_handle);
  2106. return false;
  2107. }
  2108. //checking guild_alliance_db
  2109. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`opposition`,`alliance_id`,`name` FROM `%s` LIMIT 1;", schema_config.guild_alliance_db) ){
  2110. Sql_ShowDebug(sql_handle);
  2111. return false;
  2112. }
  2113. //checking guild_castle_db
  2114. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `castle_id`,`guild_id`,`economy`,`defense`,`triggerE`,"
  2115. "`triggerD`,`nextTime`,`payTime`,`createTime`,`visibleC`,`visibleG0`,`visibleG1`,`visibleG2`,"
  2116. "`visibleG3`,`visibleG4`,`visibleG5`,`visibleG6`,`visibleG7` "
  2117. " FROM `%s` LIMIT 1;", schema_config.guild_castle_db) ){
  2118. Sql_ShowDebug(sql_handle);
  2119. return false;
  2120. }
  2121. //checking guild_expulsion_db
  2122. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`account_id`,`name`,`mes` FROM `%s` LIMIT 1;", schema_config.guild_expulsion_db) ){
  2123. Sql_ShowDebug(sql_handle);
  2124. return false;
  2125. }
  2126. //checking guild_member_db
  2127. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`account_id`,`char_id`,`hair`,"
  2128. "`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name`"
  2129. " FROM `%s` LIMIT 1;", schema_config.guild_member_db) ){
  2130. Sql_ShowDebug(sql_handle);
  2131. return false;
  2132. }
  2133. //checking guild_skill_db
  2134. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.guild_skill_db) ){
  2135. Sql_ShowDebug(sql_handle);
  2136. return false;
  2137. }
  2138. //checking guild_position_db
  2139. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`position`,`name`,`mode`,`exp_mode` FROM `%s` LIMIT 1;", schema_config.guild_position_db) ){
  2140. Sql_ShowDebug(sql_handle);
  2141. return false;
  2142. }
  2143. //checking party_db
  2144. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `party_id`,`name`,`exp`,`item`,`leader_id`,`leader_char` FROM `%s` LIMIT 1;", schema_config.party_db) ){
  2145. Sql_ShowDebug(sql_handle);
  2146. return false;
  2147. }
  2148. //checking pet_db
  2149. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `pet_id`,`class`,`name`,`account_id`,`char_id`,`level`,"
  2150. "`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`"
  2151. " FROM `%s` LIMIT 1;", schema_config.pet_db) ){
  2152. Sql_ShowDebug(sql_handle);
  2153. return false;
  2154. }
  2155. //checking friend_db
  2156. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`friend_account`,`friend_id` FROM `%s` LIMIT 1;", schema_config.friend_db) ){
  2157. Sql_ShowDebug(sql_handle);
  2158. return false;
  2159. }
  2160. //checking mail_db
  2161. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,"
  2162. "`title`,`message`,`time`,`status`,`zeny`,`type`"
  2163. " FROM `%s` LIMIT 1;", schema_config.mail_db) ){
  2164. Sql_ShowDebug(sql_handle);
  2165. return false;
  2166. }
  2167. //checking mail_attachment_db
  2168. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`index`,`nameid`,`amount`,`refine`,`attribute`,`identify`,"
  2169. "`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`unique_id`, `bound`"
  2170. " FROM `%s` LIMIT 1;", schema_config.mail_attachment_db) ){
  2171. Sql_ShowDebug(sql_handle);
  2172. return false;
  2173. }
  2174. //checking auction_db
  2175. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `auction_id`,`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,"
  2176. "`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`card0`,`card1`,"
  2177. "`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`unique_id` "
  2178. "FROM `%s` LIMIT 1;", schema_config.auction_db) ){
  2179. Sql_ShowDebug(sql_handle);
  2180. return false;
  2181. }
  2182. //checking quest_db
  2183. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`quest_id`,`state`,`time`,`count1`,`count2`,`count3` from `%s`;", schema_config.quest_db) ){
  2184. Sql_ShowDebug(sql_handle);
  2185. return false;
  2186. }
  2187. //checking homunculus_db
  2188. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`char_id`,`class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`,"
  2189. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`alive`,`rename_flag`,`vaporize` "
  2190. " FROM `%s` LIMIT 1;", schema_config.homunculus_db) ){
  2191. Sql_ShowDebug(sql_handle);
  2192. return false;
  2193. }
  2194. //checking skill_homunculus_db
  2195. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.skill_homunculus_db) ){
  2196. Sql_ShowDebug(sql_handle);
  2197. return false;
  2198. }
  2199. //checking mercenary_db
  2200. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `mer_id`,`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time` FROM `%s` LIMIT 1;", schema_config.mercenary_db) ){
  2201. Sql_ShowDebug(sql_handle);
  2202. return false;
  2203. }
  2204. //checking mercenary_owner_db
  2205. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`merc_id`,`arch_calls`,`arch_faith`,"
  2206. "`spear_calls`,`spear_faith`,`sword_calls`,`sword_faith`"
  2207. " FROM `%s` LIMIT 1;", schema_config.mercenary_owner_db) ){
  2208. Sql_ShowDebug(sql_handle);
  2209. return false;
  2210. }
  2211. //checking elemental_db
  2212. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `ele_id`,`char_id`,`class`,`mode`,`hp`,`sp`,`max_hp`,`max_sp`,"
  2213. "`atk1`,`atk2`,`matk`,`aspd`,`def`,`mdef`,`flee`,`hit`,`life_time` "
  2214. " FROM `%s` LIMIT 1;", schema_config.elemental_db) ){
  2215. Sql_ShowDebug(sql_handle);
  2216. return false;
  2217. }
  2218. //checking ragsrvinfo_db
  2219. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `index`,`name`,`exp`,`jexp`,`drop` FROM `%s` LIMIT 1;", schema_config.ragsrvinfo_db) ){
  2220. Sql_ShowDebug(sql_handle);
  2221. return false;
  2222. }
  2223. //checking skillcooldown_db
  2224. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`skill`,`tick` FROM `%s` LIMIT 1;", schema_config.skillcooldown_db) ){
  2225. Sql_ShowDebug(sql_handle);
  2226. return false;
  2227. }
  2228. //checking bonus_script_db
  2229. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`script`,`tick`,`flag`,`type`,`icon` FROM `%s` LIMIT 1;", schema_config.bonus_script_db) ){
  2230. Sql_ShowDebug(sql_handle);
  2231. return false;
  2232. }
  2233. //checking cart_db
  2234. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2235. "`attribute`,`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`expire_time`,`bound`,`unique_id`"
  2236. " FROM `%s` LIMIT 1;", schema_config.cart_db) ){
  2237. Sql_ShowDebug(sql_handle);
  2238. return false;
  2239. }
  2240. //checking inventory_db
  2241. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2242. "`attribute`,`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`expire_time`,`favorite`,`bound`,`unique_id`"
  2243. " FROM `%s` LIMIT 1;", schema_config.inventory_db) ){
  2244. Sql_ShowDebug(sql_handle);
  2245. return false;
  2246. }
  2247. //checking guild_storage_db
  2248. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`guild_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2249. "`attribute`,`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`expire_time`,`bound`,`unique_id`"
  2250. " FROM `%s` LIMIT 1;", schema_config.guild_storage_db) ){
  2251. Sql_ShowDebug(sql_handle);
  2252. return false;
  2253. }
  2254. //checking clan table
  2255. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `clan_id`,`name`,`master`,`mapname`,`max_member`"
  2256. " FROM `%s` LIMIT 1;", schema_config.clan_table) ){
  2257. Sql_ShowDebug(sql_handle);
  2258. return false;
  2259. }
  2260. //checking clan alliance table
  2261. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `clan_id`,`opposition`,`alliance_id`,`name`"
  2262. " FROM `%s` LIMIT 1;", schema_config.clan_alliance_table) ){
  2263. Sql_ShowDebug(sql_handle);
  2264. return false;
  2265. }
  2266. //checking achievement_table
  2267. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`id`,`count1`,`count2`,`count3`,`count4`,`count5`,`count6`,`count7`,`count8`,`count9`,`count10`,`completed`,`rewarded`"
  2268. " FROM `%s` LIMIT 1;", schema_config.achievement_table)) {
  2269. Sql_ShowDebug(sql_handle);
  2270. return false;
  2271. }
  2272. Sql_FreeResult(sql_handle);
  2273. ShowInfo("DB integrity check finished with success\n");
  2274. return true;
  2275. }
  2276. void char_sql_config_read(const char* cfgName) {
  2277. char line[1024], w1[1024], w2[1024];
  2278. FILE* fp;
  2279. if ((fp = fopen(cfgName, "r")) == NULL) {
  2280. ShowError("File not found: %s\n", cfgName);
  2281. return;
  2282. }
  2283. while(fgets(line, sizeof(line), fp)) {
  2284. if(line[0] == '/' && line[1] == '/')
  2285. continue;
  2286. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2287. continue;
  2288. if(!strcmpi(w1,"char_db"))
  2289. safestrncpy(schema_config.char_db, w2, sizeof(schema_config.char_db));
  2290. else if(!strcmpi(w1,"scdata_db"))
  2291. safestrncpy(schema_config.scdata_db, w2, sizeof(schema_config.scdata_db));
  2292. else if(!strcmpi(w1,"cart_db"))
  2293. safestrncpy(schema_config.cart_db, w2, sizeof(schema_config.cart_db));
  2294. else if(!strcmpi(w1,"inventory_db"))
  2295. safestrncpy(schema_config.inventory_db, w2, sizeof(schema_config.inventory_db));
  2296. else if(!strcmpi(w1,"charlog_db"))
  2297. safestrncpy(schema_config.charlog_db, w2, sizeof(schema_config.charlog_db));
  2298. else if(!strcmpi(w1,"skill_db"))
  2299. safestrncpy(schema_config.skill_db, w2, sizeof(schema_config.skill_db));
  2300. else if(!strcmpi(w1,"interlog_db"))
  2301. safestrncpy(schema_config.interlog_db, w2, sizeof(schema_config.interlog_db));
  2302. else if(!strcmpi(w1,"memo_db"))
  2303. safestrncpy(schema_config.memo_db, w2, sizeof(schema_config.memo_db));
  2304. else if(!strcmpi(w1,"guild_db"))
  2305. safestrncpy(schema_config.guild_db, w2, sizeof(schema_config.guild_db));
  2306. else if(!strcmpi(w1,"guild_alliance_db"))
  2307. safestrncpy(schema_config.guild_alliance_db, w2, sizeof(schema_config.guild_alliance_db));
  2308. else if(!strcmpi(w1,"guild_castle_db"))
  2309. safestrncpy(schema_config.guild_castle_db, w2, sizeof(schema_config.guild_castle_db));
  2310. else if(!strcmpi(w1,"guild_expulsion_db"))
  2311. safestrncpy(schema_config.guild_expulsion_db, w2, sizeof(schema_config.guild_expulsion_db));
  2312. else if(!strcmpi(w1,"guild_member_db"))
  2313. safestrncpy(schema_config.guild_member_db, w2, sizeof(schema_config.guild_member_db));
  2314. else if(!strcmpi(w1,"guild_skill_db"))
  2315. safestrncpy(schema_config.guild_skill_db, w2, sizeof(schema_config.guild_skill_db));
  2316. else if(!strcmpi(w1,"guild_position_db"))
  2317. safestrncpy(schema_config.guild_position_db, w2, sizeof(schema_config.guild_position_db));
  2318. else if(!strcmpi(w1,"guild_storage_db"))
  2319. safestrncpy(schema_config.guild_storage_db, w2, sizeof(schema_config.guild_storage_db));
  2320. else if(!strcmpi(w1,"party_db"))
  2321. safestrncpy(schema_config.party_db, w2, sizeof(schema_config.party_db));
  2322. else if(!strcmpi(w1,"pet_db"))
  2323. safestrncpy(schema_config.pet_db, w2, sizeof(schema_config.pet_db));
  2324. else if(!strcmpi(w1,"mail_db"))
  2325. safestrncpy(schema_config.mail_db, w2, sizeof(schema_config.mail_db));
  2326. else if (!strcmpi(w1, "mail_attachment_db"))
  2327. safestrncpy(schema_config.mail_attachment_db, w2, sizeof(schema_config.mail_attachment_db));
  2328. else if(!strcmpi(w1,"auction_db"))
  2329. safestrncpy(schema_config.auction_db, w2, sizeof(schema_config.auction_db));
  2330. else if(!strcmpi(w1,"friend_db"))
  2331. safestrncpy(schema_config.friend_db, w2, sizeof(schema_config.friend_db));
  2332. else if(!strcmpi(w1,"hotkey_db"))
  2333. safestrncpy(schema_config.hotkey_db, w2, sizeof(schema_config.hotkey_db));
  2334. else if(!strcmpi(w1,"quest_db"))
  2335. safestrncpy(schema_config.quest_db,w2,sizeof(schema_config.quest_db));
  2336. else if(!strcmpi(w1,"homunculus_db"))
  2337. safestrncpy(schema_config.homunculus_db,w2,sizeof(schema_config.homunculus_db));
  2338. else if(!strcmpi(w1,"skill_homunculus_db"))
  2339. safestrncpy(schema_config.skill_homunculus_db,w2,sizeof(schema_config.skill_homunculus_db));
  2340. else if(!strcmpi(w1,"mercenary_db"))
  2341. safestrncpy(schema_config.mercenary_db,w2,sizeof(schema_config.mercenary_db));
  2342. else if(!strcmpi(w1,"mercenary_owner_db"))
  2343. safestrncpy(schema_config.mercenary_owner_db,w2,sizeof(schema_config.mercenary_owner_db));
  2344. else if(!strcmpi(w1,"elemental_db"))
  2345. safestrncpy(schema_config.elemental_db,w2,sizeof(schema_config.elemental_db));
  2346. else if(!strcmpi(w1,"skillcooldown_db"))
  2347. safestrncpy(schema_config.skillcooldown_db, w2, sizeof(schema_config.skillcooldown_db));
  2348. else if(!strcmpi(w1,"bonus_script_db"))
  2349. safestrncpy(schema_config.bonus_script_db, w2, sizeof(schema_config.bonus_script_db));
  2350. else if(!strcmpi(w1,"char_reg_num_table"))
  2351. safestrncpy(schema_config.char_reg_num_table, w2, sizeof(schema_config.char_reg_num_table));
  2352. else if(!strcmpi(w1,"char_reg_str_table"))
  2353. safestrncpy(schema_config.char_reg_str_table, w2, sizeof(schema_config.char_reg_str_table));
  2354. else if(!strcmpi(w1,"acc_reg_str_table"))
  2355. safestrncpy(schema_config.acc_reg_str_table, w2, sizeof(schema_config.acc_reg_str_table));
  2356. else if(!strcmpi(w1,"acc_reg_num_table"))
  2357. safestrncpy(schema_config.acc_reg_num_table, w2, sizeof(schema_config.acc_reg_num_table));
  2358. else if(!strcmpi(w1,"clan_table"))
  2359. safestrncpy(schema_config.clan_table, w2, sizeof(schema_config.clan_table));
  2360. else if(!strcmpi(w1,"clan_alliance_table"))
  2361. safestrncpy(schema_config.clan_alliance_table, w2, sizeof(schema_config.clan_alliance_table));
  2362. else if(!strcmpi(w1,"achievement_table"))
  2363. safestrncpy(schema_config.achievement_table, w2, sizeof(schema_config.achievement_table));
  2364. //support the import command, just like any other config
  2365. else if(!strcmpi(w1,"import"))
  2366. char_sql_config_read(w2);
  2367. }
  2368. fclose(fp);
  2369. ShowInfo("Done reading %s.\n", cfgName);
  2370. }
  2371. void char_set_default_sql(){
  2372. // schema_config.db_use_sqldbs;
  2373. safestrncpy(schema_config.db_path,"db",sizeof(schema_config.db_path));
  2374. safestrncpy(schema_config.char_db,"char",sizeof(schema_config.char_db));
  2375. safestrncpy(schema_config.scdata_db,"sc_data",sizeof(schema_config.scdata_db));
  2376. safestrncpy(schema_config.cart_db,"cart_inventory",sizeof(schema_config.cart_db));
  2377. safestrncpy(schema_config.inventory_db,"inventory",sizeof(schema_config.inventory_db));
  2378. safestrncpy(schema_config.charlog_db,"charlog",sizeof(schema_config.charlog_db));
  2379. safestrncpy(schema_config.storage_db,"storage",sizeof(schema_config.storage_db));
  2380. safestrncpy(schema_config.interlog_db,"interlog",sizeof(schema_config.interlog_db));
  2381. safestrncpy(schema_config.skill_db,"skill",sizeof(schema_config.skill_db));
  2382. safestrncpy(schema_config.memo_db,"memo",sizeof(schema_config.memo_db));
  2383. safestrncpy(schema_config.guild_db,"guild",sizeof(schema_config.guild_db));
  2384. safestrncpy(schema_config.guild_alliance_db,"guild_alliance",sizeof(schema_config.guild_alliance_db));
  2385. safestrncpy(schema_config.guild_castle_db,"guild_castle",sizeof(schema_config.guild_castle_db));
  2386. safestrncpy(schema_config.guild_expulsion_db,"guild_expulsion",sizeof(schema_config.guild_expulsion_db));
  2387. safestrncpy(schema_config.guild_member_db,"guild_member",sizeof(schema_config.guild_member_db));
  2388. safestrncpy(schema_config.guild_position_db,"guild_position",sizeof(schema_config.guild_position_db));
  2389. safestrncpy(schema_config.guild_skill_db,"guild_skill",sizeof(schema_config.guild_skill_db));
  2390. safestrncpy(schema_config.guild_storage_db,"guild_storage",sizeof(schema_config.guild_storage_db));
  2391. safestrncpy(schema_config.party_db,"party",sizeof(schema_config.party_db));
  2392. safestrncpy(schema_config.pet_db,"pet",sizeof(schema_config.pet_db));
  2393. safestrncpy(schema_config.mail_db,"mail",sizeof(schema_config.mail_db)); // MAIL SYSTEM
  2394. safestrncpy(schema_config.auction_db,"auction",sizeof(schema_config.auction_db)); // Auctions System
  2395. safestrncpy(schema_config.friend_db,"friends",sizeof(schema_config.friend_db));
  2396. safestrncpy(schema_config.hotkey_db,"hotkey",sizeof(schema_config.hotkey_db));
  2397. safestrncpy(schema_config.quest_db,"quest",sizeof(schema_config.quest_db));
  2398. safestrncpy(schema_config.homunculus_db,"homunculus",sizeof(schema_config.homunculus_db));
  2399. safestrncpy(schema_config.skill_homunculus_db,"skill_homunculus",sizeof(schema_config.skill_homunculus_db));
  2400. safestrncpy(schema_config.mercenary_db,"mercenary",sizeof(schema_config.mercenary_db));
  2401. safestrncpy(schema_config.mercenary_owner_db,"mercenary_owner",sizeof(schema_config.mercenary_owner_db));
  2402. safestrncpy(schema_config.ragsrvinfo_db,"ragsrvinfo",sizeof(schema_config.ragsrvinfo_db));
  2403. safestrncpy(schema_config.skillcooldown_db,"skillcooldown",sizeof(schema_config.skillcooldown_db));
  2404. safestrncpy(schema_config.bonus_script_db,"bonus_script",sizeof(schema_config.bonus_script_db));
  2405. safestrncpy(schema_config.char_reg_num_table,"char_reg_num",sizeof(schema_config.char_reg_num_table));
  2406. safestrncpy(schema_config.char_reg_str_table,"char_reg_str",sizeof(schema_config.char_reg_str_table));
  2407. safestrncpy(schema_config.acc_reg_str_table,"acc_reg_str",sizeof(schema_config.acc_reg_str_table));
  2408. safestrncpy(schema_config.acc_reg_num_table,"acc_reg_num",sizeof(schema_config.acc_reg_num_table));
  2409. safestrncpy(schema_config.clan_table,"clan",sizeof(schema_config.clan_table));
  2410. safestrncpy(schema_config.clan_table,"clan_alliance",sizeof(schema_config.clan_alliance_table));
  2411. safestrncpy(schema_config.achievement_table,"achievement",sizeof(schema_config.achievement_table));
  2412. }
  2413. //set default config
  2414. void char_set_defaults(){
  2415. #if PACKETVER_SUPPORTS_PINCODE
  2416. charserv_config.pincode_config.pincode_enabled = true;
  2417. charserv_config.pincode_config.pincode_changetime = 0;
  2418. charserv_config.pincode_config.pincode_maxtry = 3;
  2419. charserv_config.pincode_config.pincode_force = true;
  2420. charserv_config.pincode_config.pincode_allow_repeated = false;
  2421. charserv_config.pincode_config.pincode_allow_sequential = false;
  2422. #endif
  2423. charserv_config.charmove_config.char_move_enabled = true;
  2424. charserv_config.charmove_config.char_movetoused = true;
  2425. charserv_config.charmove_config.char_moves_unlimited = false;
  2426. charserv_config.char_config.char_per_account = 0; //Maximum chars per account (default unlimited) [Sirius]
  2427. charserv_config.char_config.char_del_level = 0; //From which level u can delete character [Lupus]
  2428. charserv_config.char_config.char_del_delay = 86400;
  2429. #if PACKETVER >= 20100803
  2430. charserv_config.char_config.char_del_option = CHAR_DEL_BIRTHDATE;
  2431. #else
  2432. charserv_config.char_config.char_del_option = CHAR_DEL_EMAIL;
  2433. #endif
  2434. charserv_config.char_config.char_del_restriction = CHAR_DEL_RESTRICT_ALL;
  2435. // charserv_config.userid[24];
  2436. // charserv_config.passwd[24];
  2437. // charserv_config.server_name[20];
  2438. safestrncpy(charserv_config.wisp_server_name,"Server",sizeof(charserv_config.wisp_server_name));
  2439. // charserv_config.login_ip_str[128];
  2440. charserv_config.login_ip = 0;
  2441. charserv_config.login_port = 6900;
  2442. // charserv_config.char_ip_str[128];
  2443. charserv_config.char_ip = 0;
  2444. // charserv_config.bind_ip_str[128];
  2445. charserv_config.bind_ip = INADDR_ANY;
  2446. charserv_config.char_port = 6121;
  2447. charserv_config.char_maintenance = 0;
  2448. charserv_config.char_new = true;
  2449. charserv_config.char_new_display = 0;
  2450. charserv_config.char_config.name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  2451. charserv_config.char_config.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]
  2452. safestrncpy(charserv_config.char_config.unknown_char_name,"Unknown",sizeof(charserv_config.char_config.unknown_char_name)); // Name to use when the requested name cannot be determined
  2453. safestrncpy(charserv_config.char_config.char_name_letters,"",sizeof(charserv_config.char_config.char_name_letters)); // list of letters/symbols allowed (or not) in a character name. by [Yor]
  2454. charserv_config.save_log = 1; // show loading/saving messages
  2455. charserv_config.log_char = 1; // loggin char or not [devil]
  2456. charserv_config.log_inter = 1; // loggin inter or not [devil]
  2457. charserv_config.char_check_db =1;
  2458. // See const.h to change the default values
  2459. charserv_config.start_point[0].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2460. charserv_config.start_point[0].x = MAP_DEFAULT_X;
  2461. charserv_config.start_point[0].y = MAP_DEFAULT_Y;
  2462. charserv_config.start_point_count = 1;
  2463. #if PACKETVER >= 20151001
  2464. charserv_config.start_point_doram[0].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2465. charserv_config.start_point_doram[0].x = MAP_DEFAULT_X;
  2466. charserv_config.start_point_doram[0].y = MAP_DEFAULT_Y;
  2467. charserv_config.start_point_count_doram = 1;
  2468. #endif
  2469. charserv_config.start_items[0].nameid = 1201;
  2470. charserv_config.start_items[0].amount = 1;
  2471. charserv_config.start_items[0].pos = 2;
  2472. charserv_config.start_items[1].nameid = 2301;
  2473. charserv_config.start_items[1].amount = 1;
  2474. charserv_config.start_items[1].pos = 16;
  2475. #if PACKETVER >= 20150101
  2476. charserv_config.start_items_doram[0].nameid = 1681;
  2477. charserv_config.start_items_doram[0].amount = 1;
  2478. charserv_config.start_items_doram[0].pos = 2;
  2479. charserv_config.start_items_doram[1].nameid = 2301;
  2480. charserv_config.start_items_doram[1].amount = 1;
  2481. charserv_config.start_items_doram[1].pos = 16;
  2482. #endif
  2483. charserv_config.console = 0;
  2484. charserv_config.max_connect_user = -1;
  2485. charserv_config.gm_allow_group = -1;
  2486. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2487. charserv_config.start_zeny = 0;
  2488. charserv_config.guild_exp_rate = 100;
  2489. safestrncpy(charserv_config.default_map, "prontera", MAP_NAME_LENGTH);
  2490. charserv_config.default_map_x = 156;
  2491. charserv_config.default_map_y = 191;
  2492. charserv_config.clan_remove_inactive_days = 14;
  2493. charserv_config.mail_return_days = 14;
  2494. charserv_config.mail_delete_days = 14;
  2495. }
  2496. /**
  2497. * Split start_point configuration values.
  2498. * @param w1_value: Value from w1
  2499. * @param w2_value: Value from w2
  2500. * @param start: Start point reference
  2501. * @param count: Start point count reference
  2502. */
  2503. static void char_config_split_startpoint(char *w1_value, char *w2_value, struct point start_point[MAX_STARTPOINT], short *count)
  2504. {
  2505. char *lineitem, **fields;
  2506. int i = 0, fields_length = 3 + 1;
  2507. (*count) = 0; // Reset to begin reading
  2508. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2509. if (fields == NULL)
  2510. return; // Failed to allocate memory.
  2511. lineitem = strtok(w2_value, ":");
  2512. while (lineitem != NULL && (*count) < MAX_STARTPOINT) {
  2513. int n = sv_split(lineitem, strlen(lineitem), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE);
  2514. if (n + 1 < fields_length) {
  2515. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2516. lineitem = strtok(NULL, ":"); //next lineitem
  2517. continue;
  2518. }
  2519. start_point[i].map = mapindex_name2id(fields[1]);
  2520. if (!start_point[i].map) {
  2521. ShowError("Start point %s not found in map-index cache. Setting to default location.\n", start_point[i].map);
  2522. start_point[i].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2523. start_point[i].x = MAP_DEFAULT_X;
  2524. start_point[i].y = MAP_DEFAULT_Y;
  2525. } else {
  2526. start_point[i].x = max(0, atoi(fields[2]));
  2527. start_point[i].y = max(0, atoi(fields[3]));
  2528. }
  2529. (*count)++;
  2530. lineitem = strtok(NULL, ":"); //next lineitem
  2531. i++;
  2532. }
  2533. aFree(fields);
  2534. }
  2535. /**
  2536. * Split start_items configuration values.
  2537. * @param w1_value: Value from w1
  2538. * @param w2_value: Value from w2
  2539. * @param start: Start item reference
  2540. */
  2541. static void char_config_split_startitem(char *w1_value, char *w2_value, struct startitem start_items[MAX_STARTITEM])
  2542. {
  2543. char *lineitem, **fields;
  2544. int i = 0, fields_length = 3 + 1;
  2545. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2546. if (fields == NULL)
  2547. return; // Failed to allocate memory.
  2548. lineitem = strtok(w2_value, ":");
  2549. while (lineitem != NULL && i < MAX_STARTITEM) {
  2550. int n = sv_split(lineitem, strlen(lineitem), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE);
  2551. if (n + 1 < fields_length) {
  2552. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2553. lineitem = strtok(NULL, ":"); //next lineitem
  2554. continue;
  2555. }
  2556. // TODO: Item ID verification
  2557. start_items[i].nameid = max(0, atoi(fields[1]));
  2558. start_items[i].amount = max(0, atoi(fields[2]));
  2559. start_items[i].pos = max(0, atoi(fields[3]));
  2560. lineitem = strtok(NULL, ":"); //next lineitem
  2561. i++;
  2562. }
  2563. aFree(fields);
  2564. }
  2565. bool char_config_read(const char* cfgName, bool normal){
  2566. char line[1024], w1[1024], w2[1024];
  2567. FILE* fp = fopen(cfgName, "r");
  2568. if (fp == NULL) {
  2569. ShowError("Configuration file not found: %s.\n", cfgName);
  2570. return false;
  2571. }
  2572. while(fgets(line, sizeof(line), fp)) {
  2573. if (line[0] == '/' && line[1] == '/')
  2574. continue;
  2575. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2576. continue;
  2577. remove_control_chars(w1);
  2578. remove_control_chars(w2);
  2579. // Config that loaded only when server started, not by reloading config file
  2580. if (normal) {
  2581. if (strcmpi(w1, "userid") == 0) {
  2582. safestrncpy(charserv_config.userid, w2, sizeof(charserv_config.userid));
  2583. } else if (strcmpi(w1, "passwd") == 0) {
  2584. safestrncpy(charserv_config.passwd, w2, sizeof(charserv_config.passwd));
  2585. } else if (strcmpi(w1, "server_name") == 0) {
  2586. safestrncpy(charserv_config.server_name, w2, sizeof(charserv_config.server_name));
  2587. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  2588. if (strlen(w2) >= 4) {
  2589. safestrncpy(charserv_config.wisp_server_name, w2, sizeof(charserv_config.wisp_server_name));
  2590. }
  2591. } else if (strcmpi(w1, "login_ip") == 0) {
  2592. charserv_config.login_ip = host2ip(w2);
  2593. if (charserv_config.login_ip) {
  2594. char ip_str[16];
  2595. safestrncpy(charserv_config.login_ip_str, w2, sizeof(charserv_config.login_ip_str));
  2596. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(charserv_config.login_ip, ip_str));
  2597. }
  2598. } else if (strcmpi(w1, "login_port") == 0) {
  2599. charserv_config.login_port = atoi(w2);
  2600. } else if (strcmpi(w1, "char_ip") == 0) {
  2601. charserv_config.char_ip = host2ip(w2);
  2602. if (charserv_config.char_ip) {
  2603. char ip_str[16];
  2604. safestrncpy(charserv_config.char_ip_str, w2, sizeof(charserv_config.char_ip_str));
  2605. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(charserv_config.char_ip, ip_str));
  2606. }
  2607. } else if (strcmpi(w1, "bind_ip") == 0) {
  2608. charserv_config.bind_ip = host2ip(w2);
  2609. if (charserv_config.bind_ip) {
  2610. char ip_str[16];
  2611. safestrncpy(charserv_config.bind_ip_str, w2, sizeof(charserv_config.bind_ip_str));
  2612. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(charserv_config.bind_ip, ip_str));
  2613. }
  2614. } else if (strcmpi(w1, "char_port") == 0) {
  2615. charserv_config.char_port = atoi(w2);
  2616. } else if (strcmpi(w1, "console") == 0) {
  2617. charserv_config.console = config_switch(w2);
  2618. }
  2619. }
  2620. if(strcmpi(w1,"timestamp_format") == 0) {
  2621. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  2622. } else if(strcmpi(w1,"console_silent")==0){
  2623. msg_silent = atoi(w2);
  2624. if( msg_silent ) /* only bother if its actually enabled */
  2625. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  2626. } else if (strcmpi(w1, "console_msg_log") == 0) {
  2627. console_msg_log = atoi(w2);
  2628. } else if (strcmpi(w1, "console_log_filepath") == 0) {
  2629. safestrncpy(console_log_filepath, w2, sizeof(console_log_filepath));
  2630. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  2631. stdout_with_ansisequence = config_switch(w2);
  2632. } else if (strcmpi(w1, "char_maintenance") == 0) {
  2633. charserv_config.char_maintenance = atoi(w2);
  2634. } else if (strcmpi(w1, "char_new") == 0) {
  2635. charserv_config.char_new = (bool)atoi(w2);
  2636. } else if (strcmpi(w1, "char_new_display") == 0) {
  2637. charserv_config.char_new_display = atoi(w2);
  2638. } else if (strcmpi(w1, "max_connect_user") == 0) {
  2639. charserv_config.max_connect_user = atoi(w2);
  2640. if (charserv_config.max_connect_user < -1)
  2641. charserv_config.max_connect_user = -1;
  2642. } else if(strcmpi(w1, "gm_allow_group") == 0) {
  2643. charserv_config.gm_allow_group = atoi(w2);
  2644. } else if (strcmpi(w1, "autosave_time") == 0) {
  2645. charserv_config.autosave_interval = atoi(w2)*1000;
  2646. if (charserv_config.autosave_interval <= 0)
  2647. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2648. } else if (strcmpi(w1, "save_log") == 0) {
  2649. charserv_config.save_log = config_switch(w2);
  2650. #ifdef RENEWAL
  2651. } else if (strcmpi(w1, "start_point") == 0) {
  2652. #else
  2653. } else if (strcmpi(w1, "start_point_pre") == 0) {
  2654. #endif
  2655. char_config_split_startpoint(w1, w2, charserv_config.start_point, &charserv_config.start_point_count);
  2656. #if PACKETVER >= 20151001
  2657. } else if (strcmpi(w1, "start_point_doram") == 0) {
  2658. char_config_split_startpoint(w1, w2, charserv_config.start_point_doram, &charserv_config.start_point_count_doram);
  2659. #endif
  2660. } else if (strcmpi(w1, "start_zeny") == 0) {
  2661. charserv_config.start_zeny = atoi(w2);
  2662. if (charserv_config.start_zeny < 0)
  2663. charserv_config.start_zeny = 0;
  2664. } else if (strcmpi(w1, "start_items") == 0) {
  2665. char_config_split_startitem(w1, w2, charserv_config.start_items);
  2666. #if PACKETVER >= 20151001
  2667. } else if (strcmpi(w1, "start_items_doram") == 0) {
  2668. char_config_split_startitem(w1, w2, charserv_config.start_items_doram);
  2669. #endif
  2670. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  2671. charserv_config.log_char = atoi(w2);
  2672. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  2673. safestrncpy(charserv_config.char_config.unknown_char_name, w2, sizeof(charserv_config.char_config.unknown_char_name));
  2674. charserv_config.char_config.unknown_char_name[NAME_LENGTH-1] = '\0';
  2675. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  2676. charserv_config.char_config.name_ignoring_case = (bool)config_switch(w2);
  2677. } else if (strcmpi(w1, "char_name_option") == 0) {
  2678. charserv_config.char_config.char_name_option = atoi(w2);
  2679. } else if (strcmpi(w1, "char_name_letters") == 0) {
  2680. safestrncpy(charserv_config.char_config.char_name_letters, w2, sizeof(charserv_config.char_config.char_name_letters));
  2681. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  2682. charserv_config.char_config.char_del_level = atoi(w2);
  2683. } else if (strcmpi(w1, "char_del_delay") == 0) {
  2684. charserv_config.char_config.char_del_delay = atoi(w2);
  2685. } else if (strcmpi(w1, "char_del_option") == 0) {
  2686. charserv_config.char_config.char_del_option = atoi(w2);
  2687. } else if (strcmpi(w1, "char_del_restriction") == 0) {
  2688. charserv_config.char_config.char_del_restriction = atoi(w2);
  2689. } else if (strcmpi(w1, "char_rename_party") == 0) {
  2690. charserv_config.char_config.char_rename_party = (bool)config_switch(w2);
  2691. } else if (strcmpi(w1, "char_rename_guild") == 0) {
  2692. charserv_config.char_config.char_rename_guild = (bool)config_switch(w2);
  2693. } else if(strcmpi(w1,"db_path")==0) {
  2694. safestrncpy(schema_config.db_path, w2, sizeof(schema_config.db_path));
  2695. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  2696. fame_list_size_chemist = atoi(w2);
  2697. if (fame_list_size_chemist > MAX_FAME_LIST) {
  2698. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  2699. fame_list_size_chemist = MAX_FAME_LIST;
  2700. }
  2701. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  2702. fame_list_size_smith = atoi(w2);
  2703. if (fame_list_size_smith > MAX_FAME_LIST) {
  2704. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  2705. fame_list_size_smith = MAX_FAME_LIST;
  2706. }
  2707. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  2708. fame_list_size_taekwon = atoi(w2);
  2709. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  2710. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  2711. fame_list_size_taekwon = MAX_FAME_LIST;
  2712. }
  2713. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  2714. charserv_config.guild_exp_rate = atoi(w2);
  2715. } else if (strcmpi(w1, "pincode_enabled") == 0) {
  2716. #if PACKETVER_SUPPORTS_PINCODE
  2717. charserv_config.pincode_config.pincode_enabled = config_switch(w2);
  2718. } else if (strcmpi(w1, "pincode_changetime") == 0) {
  2719. charserv_config.pincode_config.pincode_changetime = atoi(w2)*60*60*24;
  2720. } else if (strcmpi(w1, "pincode_maxtry") == 0) {
  2721. charserv_config.pincode_config.pincode_maxtry = atoi(w2);
  2722. } else if (strcmpi(w1, "pincode_force") == 0) {
  2723. charserv_config.pincode_config.pincode_force = config_switch(w2);
  2724. } else if (strcmpi(w1, "pincode_allow_repeated") == 0) {
  2725. charserv_config.pincode_config.pincode_allow_repeated = config_switch(w2);
  2726. } else if (strcmpi(w1, "pincode_allow_sequential") == 0) {
  2727. charserv_config.pincode_config.pincode_allow_sequential = config_switch(w2);
  2728. #else
  2729. if( config_switch(w2) ) {
  2730. ShowWarning("pincode_enabled requires PACKETVER 20110309 or higher.\n");
  2731. }
  2732. #endif
  2733. } else if (strcmpi(w1, "char_move_enabled") == 0) {
  2734. charserv_config.charmove_config.char_move_enabled = config_switch(w2);
  2735. } else if (strcmpi(w1, "char_movetoused") == 0) {
  2736. charserv_config.charmove_config.char_movetoused = config_switch(w2);
  2737. } else if (strcmpi(w1, "char_moves_unlimited") == 0) {
  2738. charserv_config.charmove_config.char_moves_unlimited = config_switch(w2);
  2739. } else if (strcmpi(w1, "char_checkdb") == 0) {
  2740. charserv_config.char_check_db = config_switch(w2);
  2741. } else if (strcmpi(w1, "default_map") == 0) {
  2742. safestrncpy(charserv_config.default_map, w2, MAP_NAME_LENGTH);
  2743. } else if (strcmpi(w1, "default_map_x") == 0) {
  2744. charserv_config.default_map_x = atoi(w2);
  2745. } else if (strcmpi(w1, "default_map_y") == 0) {
  2746. charserv_config.default_map_y = atoi(w2);
  2747. } else if (strcmpi(w1, "clan_remove_inactive_days") == 0) {
  2748. charserv_config.clan_remove_inactive_days = atoi(w2);
  2749. } else if (strcmpi(w1, "mail_return_days") == 0) {
  2750. charserv_config.mail_return_days = atoi(w2);
  2751. } else if (strcmpi(w1, "mail_delete_days") == 0) {
  2752. charserv_config.mail_delete_days = atoi(w2);
  2753. } else if (strcmpi(w1, "import") == 0) {
  2754. char_config_read(w2, normal);
  2755. }
  2756. }
  2757. fclose(fp);
  2758. ShowInfo("Done reading %s.\n", cfgName);
  2759. return true;
  2760. }
  2761. /**
  2762. * Checks for values out of range.
  2763. */
  2764. static void char_config_adjust() {
  2765. #if PACKETVER < 20100803
  2766. if (charserv_config.char_config.char_del_option&CHAR_DEL_BIRTHDATE) {
  2767. ShowWarning("conf/char_athena.conf:char_del_option birthdate is enabled but it requires PACKETVER 2010-08-03 or newer, defaulting to email...\n");
  2768. charserv_config.char_config.char_del_option &= ~CHAR_DEL_BIRTHDATE;
  2769. }
  2770. #endif
  2771. }
  2772. /*
  2773. * Message conf function
  2774. */
  2775. int char_msg_config_read(const char *cfgName){
  2776. return _msg_config_read(cfgName,CHAR_MAX_MSG,msg_table);
  2777. }
  2778. const char* char_msg_txt(int msg_number){
  2779. return _msg_txt(msg_number,CHAR_MAX_MSG,msg_table);
  2780. }
  2781. void char_do_final_msg(void){
  2782. _do_final_msg(CHAR_MAX_MSG,msg_table);
  2783. }
  2784. void do_final(void)
  2785. {
  2786. ShowStatus("Terminating...\n");
  2787. char_set_all_offline(-1);
  2788. char_set_all_offline_sql();
  2789. inter_final();
  2790. flush_fifos();
  2791. do_final_msg();
  2792. do_final_chmapif();
  2793. do_final_chlogif();
  2794. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s`", schema_config.ragsrvinfo_db) )
  2795. Sql_ShowDebug(sql_handle);
  2796. char_db_->destroy(char_db_, NULL);
  2797. online_char_db->destroy(online_char_db, NULL);
  2798. auth_db->destroy(auth_db, NULL);
  2799. if( char_fd != -1 )
  2800. {
  2801. do_close(char_fd);
  2802. char_fd = -1;
  2803. }
  2804. Sql_Free(sql_handle);
  2805. mapindex_final();
  2806. ShowStatus("Finished.\n");
  2807. }
  2808. void set_server_type(void){
  2809. SERVER_TYPE = ATHENA_SERVER_CHAR;
  2810. }
  2811. //------------------------------
  2812. // Function called when the server
  2813. // has received a crash signal.
  2814. //------------------------------
  2815. void do_abort(void)
  2816. {
  2817. }
  2818. /// Called when a terminate signal is received.
  2819. void do_shutdown(void) {
  2820. if( runflag != CHARSERVER_ST_SHUTDOWN )
  2821. {
  2822. int id;
  2823. runflag = CHARSERVER_ST_SHUTDOWN;
  2824. ShowStatus("Shutting down...\n");
  2825. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  2826. for( id = 0; id < ARRAYLENGTH(map_server); ++id )
  2827. chmapif_server_reset(id);
  2828. chlogif_check_shutdown();
  2829. flush_fifos();
  2830. runflag = CORE_ST_STOP;
  2831. }
  2832. }
  2833. int do_init(int argc, char **argv)
  2834. {
  2835. //Read map indexes
  2836. runflag = CHARSERVER_ST_STARTING;
  2837. mapindex_init();
  2838. // Init default value
  2839. CHAR_CONF_NAME = "conf/char_athena.conf";
  2840. LAN_CONF_NAME = "conf/subnet_athena.conf";
  2841. SQL_CONF_NAME = "conf/inter_athena.conf";
  2842. MSG_CONF_NAME_EN = "conf/msg_conf/char_msg.conf";
  2843. safestrncpy(console_log_filepath, "./log/char-msg_log.log", sizeof(console_log_filepath));
  2844. cli_get_options(argc,argv);
  2845. char_set_defaults();
  2846. char_config_read(CHAR_CONF_NAME, true);
  2847. char_config_adjust();
  2848. char_lan_config_read(LAN_CONF_NAME);
  2849. char_set_default_sql();
  2850. char_sql_config_read(SQL_CONF_NAME);
  2851. msg_config_read(MSG_CONF_NAME_EN);
  2852. // Skip this check if the server is run with run-once flag
  2853. if (runflag!=CORE_ST_STOP && strcmp(charserv_config.userid, "s1")==0 && strcmp(charserv_config.passwd, "p1")==0) {
  2854. ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  2855. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  2856. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  2857. }
  2858. inter_init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server configuration
  2859. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  2860. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  2861. char_mmo_sql_init();
  2862. char_read_fame_list(); //Read fame lists.
  2863. if ((naddr_ != 0) && (!(charserv_config.login_ip) || !(charserv_config.char_ip) ))
  2864. {
  2865. char ip_str[16];
  2866. ip2str(addr_[0], ip_str);
  2867. if (naddr_ > 1)
  2868. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  2869. else
  2870. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  2871. if (!(charserv_config.login_ip) ) {
  2872. safestrncpy(charserv_config.login_ip_str, ip_str, sizeof(charserv_config.login_ip_str));
  2873. charserv_config.login_ip = str2ip(charserv_config.login_ip_str);
  2874. }
  2875. if (!(charserv_config.char_ip)) {
  2876. safestrncpy(charserv_config.char_ip_str, ip_str, sizeof(charserv_config.char_ip_str));
  2877. charserv_config.char_ip = str2ip(charserv_config.char_ip_str);
  2878. }
  2879. }
  2880. do_init_chlogif();
  2881. do_init_chmapif();
  2882. // periodically update the overall user count on all mapservers + login server
  2883. add_timer_func_list(chlogif_broadcast_user_count, "broadcast_user_count");
  2884. add_timer_interval(gettick() + 1000, chlogif_broadcast_user_count, 0, 0, 5 * 1000);
  2885. // Timer to clear (online_char_db)
  2886. add_timer_func_list(char_chardb_waiting_disconnect, "chardb_waiting_disconnect");
  2887. // Online Data timers (checking if char still connected)
  2888. add_timer_func_list(char_online_data_cleanup, "online_data_cleanup");
  2889. add_timer_interval(gettick() + 1000, char_online_data_cleanup, 0, 0, 600 * 1000);
  2890. // periodically remove players that have not logged in for a long time from clans
  2891. add_timer_func_list(char_clan_member_cleanup, "clan_member_cleanup");
  2892. add_timer_interval(gettick() + 1000, char_clan_member_cleanup, 0, 0, 60 * 60 * 1000); // every 60 minutes
  2893. // periodically check if mails need to be returned to their sender
  2894. add_timer_func_list(mail_return_timer, "mail_return_timer");
  2895. add_timer_interval(gettick() + 1000, mail_return_timer, 0, 0, 5 * 60 * 1000); // every 5 minutes
  2896. // periodically check if mails need to be deleted completely
  2897. add_timer_func_list(mail_delete_timer, "mail_delete_timer");
  2898. add_timer_interval(gettick() + 1000, mail_delete_timer, 0, 0, 5 * 60 * 1000); // every 5 minutes
  2899. //check db tables
  2900. if(charserv_config.char_check_db && char_checkdb() == 0){
  2901. ShowFatalError("char : A tables is missing in sql-server, please fix it, see (sql-files main.sql for structure) \n");
  2902. exit(EXIT_FAILURE);
  2903. }
  2904. //Cleaning the tables for NULL entrys @ startup [Sirius]
  2905. //Chardb clean
  2906. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", schema_config.char_db) )
  2907. Sql_ShowDebug(sql_handle);
  2908. //guilddb clean
  2909. 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'", schema_config.guild_db) )
  2910. Sql_ShowDebug(sql_handle);
  2911. //guildmemberdb clean
  2912. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", schema_config.guild_member_db) )
  2913. Sql_ShowDebug(sql_handle);
  2914. set_defaultparse(chclif_parse);
  2915. if( (char_fd = make_listen_bind(charserv_config.bind_ip,charserv_config.char_port)) == -1 ) {
  2916. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",charserv_config.char_port);
  2917. exit(EXIT_FAILURE);
  2918. }
  2919. if( runflag != CORE_ST_STOP )
  2920. {
  2921. shutdown_callback = do_shutdown;
  2922. runflag = CHARSERVER_ST_RUNNING;
  2923. }
  2924. do_init_chcnslif();
  2925. mapindex_check_mapdefault(charserv_config.default_map);
  2926. ShowInfo("Default map: '" CL_WHITE "%s %d,%d" CL_RESET "'\n", charserv_config.default_map, charserv_config.default_map_x, charserv_config.default_map_y);
  2927. ShowStatus("The char-server is " CL_GREEN "ready" CL_RESET " (Server is listening on the port %d).\n\n", charserv_config.char_port);
  2928. return 0;
  2929. }