char.c 122 KB

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