diff options
Diffstat (limited to 'src/char/char.c')
-rw-r--r-- | src/char/char.c | 3477 |
1 files changed, 1658 insertions, 1819 deletions
diff --git a/src/char/char.c b/src/char/char.c index aac469c5b..cb3007ff9 100644 --- a/src/char/char.c +++ b/src/char/char.c @@ -4,7 +4,6 @@ #include "../common/cbasetypes.h" #include "../common/core.h" #include "../common/db.h" -#include "../common/lock.h" #include "../common/malloc.h" #include "../common/mapindex.h" #include "../common/mmo.h" @@ -17,10 +16,9 @@ #include "inter.h" #include "int_guild.h" #include "int_homun.h" -#include "int_pet.h" +#include "int_mercenary.h" #include "int_party.h" #include "int_storage.h" -#include "int_status.h" #include "char.h" #include <sys/types.h> @@ -34,22 +32,43 @@ // private declarations #define CHAR_CONF_NAME "conf/char_athena.conf" #define LAN_CONF_NAME "conf/subnet_athena.conf" - -char char_txt[1024] = "save/athena.txt"; -char friends_txt[1024] = "save/friends.txt"; -char hotkeys_txt[1024] = "save/hotkeys.txt"; -char char_log_filename[1024] = "log/char.log"; +#define SQL_CONF_NAME "conf/inter_athena.conf" + +char char_db[256] = "char"; +char scdata_db[256] = "sc_data"; +char cart_db[256] = "cart_inventory"; +char inventory_db[256] = "inventory"; +char charlog_db[256] = "charlog"; +char storage_db[256] = "storage"; +char interlog_db[256] = "interlog"; +char reg_db[256] = "global_reg_value"; +char skill_db[256] = "skill"; +char memo_db[256] = "memo"; +char guild_db[256] = "guild"; +char guild_alliance_db[256] = "guild_alliance"; +char guild_castle_db[256] = "guild_castle"; +char guild_expulsion_db[256] = "guild_expulsion"; +char guild_member_db[256] = "guild_member"; +char guild_position_db[256] = "guild_position"; +char guild_skill_db[256] = "guild_skill"; +char guild_storage_db[256] = "guild_storage"; +char party_db[256] = "party"; +char pet_db[256] = "pet"; +char mail_db[256] = "mail"; // MAIL SYSTEM +char auction_db[256] = "auction"; // Auctions System +char friend_db[256] = "friends"; +char hotkey_db[256] = "hotkey"; +char quest_db[256] = "quest"; // show loading/saving messages -#ifndef TXT_SQL_CONVERT int save_log = 1; -#endif -//If your code editor is having problems syntax highlighting this file, uncomment this and RECOMMENT IT BEFORE COMPILING -//#undef TXT_SQL_CONVERT -#ifndef TXT_SQL_CONVERT +static DBMap* char_db_; // int char_id -> struct mmo_charstatus* + char db_path[1024] = "db"; +int db_use_sqldbs; + struct mmo_map_server { int fd; uint32 ip; @@ -75,12 +94,10 @@ int char_maintenance = 0; bool char_new = true; int char_new_display = 0; -int email_creation = 0; // disabled by default - bool name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor] int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor] char unknown_char_name[NAME_LENGTH] = "Unknown"; // Name to use when the requested name cannot be determined -#define TRIM_CHARS "\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex] +#define TRIM_CHARS "\255\xA0\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex] char char_name_letters[1024] = ""; // list of letters/symbols allowed (or not) in a character name. by [Yor] int char_per_account = 0; //Maximum charas per account (default unlimited) [Sirius] @@ -107,13 +124,10 @@ struct char_session_data { int gmlevel; uint32 version; uint8 clienttype; + char new_name[NAME_LENGTH]; char birthdate[10+1]; // YYYY-MM-DD }; -int char_id_count = START_CHAR_NUM; -struct character_data *char_dat; - -int char_num, char_max; int max_connect_user = 0; int gm_allow_level = 99; int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; @@ -132,17 +146,14 @@ struct fame_list smith_fame_list[MAX_FAME_LIST]; struct fame_list chemist_fame_list[MAX_FAME_LIST]; struct fame_list taekwon_fame_list[MAX_FAME_LIST]; +// check for exit signal +// 0 is saving complete +// other is char_id +unsigned int save_flag = 0; + // Initial position (it's possible to set it in conf file) struct point start_point = { 0, 53, 111 }; -// online players by [Yor] -char online_txt_filename[1024] = "online.txt"; -char online_html_filename[1024] = "online.html"; -int online_sorting_option = 0; // sorting option to display online players in online files -int online_display_option = 1; // display options: to know which columns must be displayed -int online_refresh_html = 20; // refresh time (in sec) of the html file in the explorer -int online_gm_display_min_level = 20; // minimum GM level to display 'GM' when we want to display it - int console = 0; //----------------------------------------------------- @@ -159,6 +170,7 @@ struct auth_node { int sex; time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) int gmlevel; + unsigned changing_mapservers : 1; }; static DBMap* auth_db; // int account_id -> struct auth_node* @@ -177,6 +189,7 @@ struct online_char_data { static DBMap* online_char_db; // int account_id -> struct online_char_data* static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data); +int delete_char_sql(int char_id); static void* create_online_char_data(DBKey key, va_list args) { @@ -221,7 +234,12 @@ void set_char_charselect(int account_id) void set_char_online(int map_id, int char_id, int account_id) { struct online_char_data* character; + struct mmo_charstatus *cp; + //Update DB + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) ) + Sql_ShowDebug(sql_handle); + //Check to see for online conflicts character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data); if( character->char_id != -1 && character->server > -1 && character->server != map_id ) @@ -244,6 +262,10 @@ void set_char_online(int map_id, int char_id, int account_id) character->waiting_disconnect = INVALID_TIMER; } + //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it. + cp = (struct mmo_charstatus*)idb_get(char_db_,char_id); + inter_guild_CharOnline(char_id, cp?cp->guild_id:-1); + //Notify login server if (login_fd > 0 && !session[login_fd]->flag.eof) { @@ -258,6 +280,22 @@ void set_char_offline(int char_id, int account_id) { struct online_char_data* character; + if ( char_id == -1 ) + { + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id) ) + Sql_ShowDebug(sql_handle); + } + else + { + struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id); + inter_guild_CharOffline(char_id, cp?cp->guild_id:-1); + if (cp) + idb_remove(char_db_,char_id); + + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) ) + Sql_ShowDebug(sql_handle); + } + if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL) { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex] if( character->server > -1 ) @@ -339,913 +377,904 @@ void set_all_offline(int id) WFIFOSET(login_fd,2); } -//------------------------------ -// Writing function of logs file -//------------------------------ -int char_log(char *fmt, ...) +void set_all_offline_sql(void) { - if(log_char) - { - FILE *logfp; - va_list ap; - time_t raw_time; - char tmpstr[2048]; - - va_start(ap, fmt); - - logfp = fopen(char_log_filename, "a"); - if (logfp) { - if (fmt[0] == '\0') // jump a line if no message - fprintf(logfp, "\n"); - else { - time(&raw_time); - strftime(tmpstr, 24, "%d-%m-%Y %H:%M:%S", localtime(&raw_time)); - sprintf(tmpstr + 19, ": %s", fmt); - vfprintf(logfp, tmpstr, ap); - } - fclose(logfp); - } - va_end(ap); - } - return 0; + //Set all players to 'OFFLINE' + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", char_db) ) + Sql_ShowDebug(sql_handle); + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", guild_member_db) ) + Sql_ShowDebug(sql_handle); + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", guild_db) ) + Sql_ShowDebug(sql_handle); } - -/// Find all characters for given session and update the session character cache. -int char_find_characters(struct char_session_data* sd) +static void* create_charstatus(DBKey key, va_list args) { - int i, found_num = 0; + struct mmo_charstatus *cp; + cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus)); + cp->char_id = key.i; + return cp; +} - for( i = 0; i < char_num; i++ ) - {// find character entries and save them - if( char_dat[i].status.account_id == sd->account_id ) - { - sd->found_char[found_num++] = i; - if( found_num >= MAX_CHARS ) - { - break; - } - } - } +int mmo_char_tosql(int char_id, struct mmo_charstatus* p) +{ + int i = 0; + int count = 0; + int diff = 0; + char save_status[128]; //For displaying save information. [Skotlex] + struct mmo_charstatus *cp; + int errors = 0; //If there are any errors while saving, "cp" will not be updated at the end. + StringBuf buf; - for( i = found_num; i < MAX_CHARS; i++ ) - {// fill remaining blanks - sd->found_char[i] = -1; - } + if (char_id!=p->char_id) return 0; - return found_num; -} + cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus); + StringBuf_Init(&buf); + memset(save_status, 0, sizeof(save_status)); -/// Search character data from given session. -struct mmo_charstatus* search_session_character(struct char_session_data* sd, int char_id) -{ - int i; + //map inventory data + if( memcmp(p->inventory, cp->inventory, sizeof(p->inventory)) ) + { + if (!memitemdata_to_sql(p->inventory, MAX_INVENTORY, p->char_id, TABLE_INVENTORY)) + strcat(save_status, " inventory"); + else + errors++; + } - ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == char_id ); - if( i == MAX_CHARS ) + //map cart data + if( memcmp(p->cart, cp->cart, sizeof(p->cart)) ) { - return NULL; + if (!memitemdata_to_sql(p->cart, MAX_CART, p->char_id, TABLE_CART)) + strcat(save_status, " cart"); + else + errors++; } - return &char_dat[sd->found_char[i]].status; -} + //map storage data + if( memcmp(p->storage.items, cp->storage.items, sizeof(p->storage.items)) ) + { + if (!memitemdata_to_sql(p->storage.items, MAX_STORAGE, p->account_id, TABLE_STORAGE)) + strcat(save_status, " storage"); + else + errors++; + } -//Search character data from the aid/cid givem -struct mmo_charstatus* search_character(int aid, int cid) -{ - int i; - for (i = 0; i < char_num; i++) { - if (char_dat[i].status.char_id == cid && char_dat[i].status.account_id == aid) - return &char_dat[i].status; + if ( + (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) || + (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) || + (p->zeny != cp->zeny) || + (p->last_point.map != cp->last_point.map) || + (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) || + (p->max_hp != cp->max_hp) || (p->hp != cp->hp) || + (p->max_sp != cp->max_sp) || (p->sp != cp->sp) || + (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) || + (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) || + (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) || + (p->option != cp->option) || + (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) || + (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) || + (p->shield != cp->shield) || (p->head_top != cp->head_top) || + (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom) || (p->delete_date != cp->delete_date) || + (p->rename != cp->rename) || (p->robe != cp->robe) + ) + { //Save status + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d'," + "`base_exp`='%u', `job_exp`='%u', `zeny`='%d'," + "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d'," + "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d'," + "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d'," + "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d'," + "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d'," + "`delete_date`='%lu',`robe`='%d'" + " WHERE `account_id`='%d' AND `char_id` = '%d'", + char_db, p->base_level, p->job_level, + p->base_exp, p->job_exp, p->zeny, + p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point, + p->str, p->agi, p->vit, p->int_, p->dex, p->luk, + p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, + p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom, + mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y, + mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename, + (unsigned long)p->delete_date, // FIXME: platform-dependent size + p->robe, + p->account_id, p->char_id) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } else + strcat(save_status, " status"); } - return NULL; -} - -struct mmo_charstatus* search_character_byname(char* character_name) -{ - int i = search_character_index(character_name); - if (i == -1) return NULL; - return &char_dat[i].status; -} -// Searches if the given character is online, and returns the fd of the -// map-server it is connected to. -int search_character_online(int aid, int cid) -{ - //Look for online character. - struct online_char_data* character; - character = (struct online_char_data*)idb_get(online_char_db, aid); - if(character && - character->char_id == cid && - character->server > -1) - return server[character->server].fd; - return -1; -} + //Values that will seldom change (to speed up saving) + if ( + (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) || + (p->class_ != cp->class_) || + (p->partner_id != cp->partner_id) || (p->father != cp->father) || + (p->mother != cp->mother) || (p->child != cp->child) || + (p->karma != cp->karma) || (p->manner != cp->manner) || + (p->fame != cp->fame) + ) + { + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d'," + "`hair`='%d',`hair_color`='%d',`clothes_color`='%d'," + "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d'," + "`karma`='%d',`manner`='%d', `fame`='%d'" + " WHERE `account_id`='%d' AND `char_id` = '%d'", + char_db, p->class_, + p->hair, p->hair_color, p->clothes_color, + p->partner_id, p->father, p->mother, p->child, + p->karma, p->manner, p->fame, + p->account_id, p->char_id) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } else + strcat(save_status, " status2"); + } -//---------------------------------------------- -// Search an character id -// (return character index or -1 (if not found)) -// If exact character name is not found, -// the function checks without case sensitive -// and returns index if only 1 character is found -// and similar to the searched name. -//---------------------------------------------- -int search_character_index(char* character_name) -{ - int i, quantity, index; - - quantity = 0; - index = -1; - for(i = 0; i < char_num; i++) { - // Without case sensitive check (increase the number of similar character names found) - if (stricmp(char_dat[i].status.name, character_name) == 0) { - // Strict comparison (if found, we finish the function immediatly with correct value) - if (strcmp(char_dat[i].status.name, character_name) == 0) - return i; - quantity++; - index = i; - } + /* Mercenary Owner */ + if( (p->mer_id != cp->mer_id) || + (p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) || + (p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) || + (p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) ) + { + if (mercenary_owner_tosql(char_id, p)) + strcat(save_status, " mercenary"); + else + errors++; } - // Here, the exact character name is not found - // We return the found index of a similar account ONLY if there is 1 similar character - if (quantity == 1) - return index; - // Exact character name is not found and 0 or more than 1 similar characters have been found ==> we say not found - return -1; -} + //memo points + if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) ) + { + char esc_mapname[NAME_LENGTH*2+1]; -/*--------------------------------------------------- - Make a data line for friends list - --------------------------------------------------*/ -int mmo_friends_list_data_str(char *str, struct mmo_charstatus *p) -{ - int i; - char *str_p = str; - str_p += sprintf(str_p, "%d", p->char_id); + //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`) + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, p->char_id) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } - for (i=0;i<MAX_FRIENDS;i++){ - if (p->friends[i].account_id > 0 && p->friends[i].char_id > 0 && p->friends[i].name[0]) - str_p += sprintf(str_p, ",%d,%d,%s", p->friends[i].account_id, p->friends[i].char_id, p->friends[i].name); + //insert here. + StringBuf_Clear(&buf); + StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db); + for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i ) + { + if( p->memo_point[i].map ) + { + if( count ) + StringBuf_AppendStr(&buf, ","); + Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map)); + StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y); + ++count; + } + } + if( count ) + { + if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } + } + strcat(save_status, " memo"); } - str_p += '\0'; + //FIXME: is this neccessary? [ultramage] + for(i=0;i<MAX_SKILL;i++) + if ((p->skill[i].lv != 0) && (p->skill[i].id == 0)) + p->skill[i].id = i; // Fix skill tree - return 0; -} -/*--------------------------------------------------- - Make a data line for hotkeys list - --------------------------------------------------*/ -int mmo_hotkeys_tostr(char *str, struct mmo_charstatus *p) -{ -#ifdef HOTKEY_SAVING - int i; - char *str_p = str; - str_p += sprintf(str_p, "%d", p->char_id); - for (i=0;i<MAX_HOTKEYS;i++) - str_p += sprintf(str_p, ",%d,%d,%d", p->hotkeys[i].type, p->hotkeys[i].id, p->hotkeys[i].lv); - str_p += '\0'; -#endif - - return 0; -} - -//------------------------------------------------- -// Function to create the character line (for save) -//------------------------------------------------- -int mmo_char_tostr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int reg_num) -{ - int i,j; - char *str_p = str; - - str_p += sprintf(str_p, - "%d\t%d,%d\t%s\t%d,%d,%d\t%u,%u,%d" //Up to Zeny field - "\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" //Up to Skill Point - "\t%d,%d,%d\t%d,%d,%d,%d" //Up to hom id - "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d" //Up to robe - "\t%d,%d,%d\t%d,%d,%d" //last point + save point - ",%d,%d,%d,%d,%d,%lu\t", //Family info + delete date - p->char_id, p->account_id, p->slot, p->name, // - p->class_, p->base_level, p->job_level, - p->base_exp, p->job_exp, p->zeny, - p->hp, p->max_hp, p->sp, p->max_sp, - p->str, p->agi, p->vit, p->int_, p->dex, p->luk, - p->status_point, p->skill_point, - p->option, p->karma, p->manner, // - p->party_id, p->guild_id, p->pet_id, p->hom_id, - p->hair, p->hair_color, p->clothes_color, - p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom, p->robe, - p->last_point.map, p->last_point.x, p->last_point.y, // - p->save_point.map, p->save_point.x, p->save_point.y, - p->partner_id,p->father,p->mother,p->child,p->fame, // - (unsigned long)p->delete_date); // FIXME: platform-dependent size - for(i = 0; i < MAX_MEMOPOINTS; i++) - if (p->memo_point[i].map) { - str_p += sprintf(str_p, "%d,%d,%d ", p->memo_point[i].map, p->memo_point[i].x, p->memo_point[i].y); - } - *(str_p++) = '\t'; - - for(i = 0; i < MAX_INVENTORY; i++) - if (p->inventory[i].nameid) { - str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d", - p->inventory[i].id,p->inventory[i].nameid,p->inventory[i].amount,p->inventory[i].equip, - p->inventory[i].identify,p->inventory[i].refine,p->inventory[i].attribute); - for(j=0; j<MAX_SLOTS; j++) - str_p += sprintf(str_p,",%d",p->inventory[i].card[j]); - str_p += sprintf(str_p," "); - } - *(str_p++) = '\t'; - - for(i = 0; i < MAX_CART; i++) - if (p->cart[i].nameid) { - str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d", - p->cart[i].id,p->cart[i].nameid,p->cart[i].amount,p->cart[i].equip, - p->cart[i].identify,p->cart[i].refine,p->cart[i].attribute); - for(j=0; j<MAX_SLOTS; j++) - str_p += sprintf(str_p,",%d",p->cart[i].card[j]); - str_p += sprintf(str_p," "); + //skills + if( memcmp(p->skill, cp->skill, sizeof(p->skill)) ) + { + //`skill` (`char_id`, `id`, `lv`) + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, p->char_id) ) + { + Sql_ShowDebug(sql_handle); + errors++; } - *(str_p++) = '\t'; - for(i = 0; i < MAX_SKILL; i++) - if (p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY) { - str_p += sprintf(str_p, "%d,%d ", p->skill[i].id, (p->skill[i].flag == SKILL_FLAG_PERMANENT) ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0); + StringBuf_Clear(&buf); + StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`) VALUES ", skill_db); + //insert here. + for( i = 0, count = 0; i < MAX_SKILL; ++i ) + { + if( p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY ) + { + if( p->skill[i].flag == SKILL_FLAG_PERMANENT && p->skill[i].lv == 0 ) + continue; + if( p->skill[i].flag != SKILL_FLAG_PERMANENT && (p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0) == 0 ) + continue; + if( count ) + StringBuf_AppendStr(&buf, ","); + StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->skill[i].id, (p->skill[i].flag == SKILL_FLAG_PERMANENT ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0)); + ++count; + } + } + if( count ) + { + if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } } - *(str_p++) = '\t'; - - for(i = 0; i < reg_num; i++) - if (reg[i].str[0]) - str_p += sprintf(str_p, "%s,%s ", reg[i].str, reg[i].value); - *(str_p++) = '\t'; - *str_p = '\0'; - return 0; -} -#endif //TXT_SQL_CONVERT -//------------------------------------------------------------------------- -// Function to set the character from the line (at read of characters file) -//------------------------------------------------------------------------- -int mmo_char_fromstr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int *reg_num) -{ - char tmp_str[3][128]; //To avoid deleting chars with too long names. - int tmp_int[256]; - unsigned int tmp_uint[2]; //To read exp.... - int next, len, i, j; - unsigned long tmp_ulong[1]; - - // initilialise character - memset(p, '\0', sizeof(struct mmo_charstatus)); - -// Char structure of version 14797 (robe) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d,%d" - "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d,%d,%d,%lu%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], &tmp_int[26], &tmp_int[44], - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], &tmp_int[47], - &tmp_int[45], &tmp_int[35], &tmp_int[36], - &tmp_int[46], &tmp_int[37], &tmp_int[38], &tmp_int[39], - &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &tmp_ulong[0], &next) != 50) - { - tmp_int[47] = 0; // robe -// Char structure of version 14700 (delete date) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" - "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d,%d,%d,%lu%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], &tmp_int[26], &tmp_int[44], - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], - &tmp_int[45], &tmp_int[35], &tmp_int[36], - &tmp_int[46], &tmp_int[37], &tmp_int[38], &tmp_int[39], - &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &tmp_ulong[0], &next) != 49) - { - tmp_ulong[0] = 0; // delete date -// Char structure of version 1500 (homun + mapindex maps) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" - "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d,%d,%d%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], &tmp_int[26], &tmp_int[44], - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], - &tmp_int[45], &tmp_int[35], &tmp_int[36], - &tmp_int[46], &tmp_int[37], &tmp_int[38], &tmp_int[39], - &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &next) != 48) - { - tmp_int[44] = 0; //Hom ID. -// Char structure of version 1488 (fame field addition) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" - "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d,%d%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], &tmp_int[26], - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], - tmp_str[1], &tmp_int[35], &tmp_int[36], - tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], - &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &next) != 47) - { - tmp_int[43] = 0; //Fame -// Char structure of version 1363 (family data addition) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" - "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], &tmp_int[26], - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], - tmp_str[1], &tmp_int[35], &tmp_int[36], // - tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], - &tmp_int[40], &tmp_int[41], &tmp_int[42], &next) != 46) - { - tmp_int[40] = 0; // father - tmp_int[41] = 0; // mother - tmp_int[42] = 0; // child -// Char structure version 1008 (marriage partner addition) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" - "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], &tmp_int[26], - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], - tmp_str[1], &tmp_int[35], &tmp_int[36], // - tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], &next) != 43) - { - tmp_int[39] = 0; // partner id -// Char structure version 384 (pet addition) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" - "\t%127[^,],%d,%d\t%127[^,],%d,%d%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], &tmp_int[26], - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], - tmp_str[1], &tmp_int[35], &tmp_int[36], // - tmp_str[2], &tmp_int[37], &tmp_int[38], &next) != 42) - { - tmp_int[26] = 0; // pet id -// Char structure of a version 1 (original data structure) - if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" - "\t%d,%d,%d\t%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" - "\t%127[^,],%d,%d\t%127[^,],%d,%d%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // - &tmp_int[3], &tmp_int[4], &tmp_int[5], - &tmp_uint[0], &tmp_uint[1], &tmp_int[8], - &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], - &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], - &tmp_int[19], &tmp_int[20], - &tmp_int[21], &tmp_int[22], &tmp_int[23], // - &tmp_int[24], &tmp_int[25], // - &tmp_int[27], &tmp_int[28], &tmp_int[29], - &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], - tmp_str[1], &tmp_int[35], &tmp_int[36], // - tmp_str[2], &tmp_int[37], &tmp_int[38], &next) != 41) - { - ShowError("Char-loading: Unrecognized character data version, info lost!\n"); - ShowDebug("Character info: %s\n", str); - return 0; + strcat(save_status, " skills"); } - } // Char structure version 384 (pet addition) - } // Char structure version 1008 (marriage partner addition) - } // Char structure of version 1363 (family data addition) - } // Char structure of version 1488 (fame field addition) - //Convert save data from string to integer for older formats - tmp_int[45] = mapindex_name2id(tmp_str[1]); - tmp_int[46] = mapindex_name2id(tmp_str[2]); - } // Char structure of version 1500 (homun + mapindex maps) - } // Char structure of version 14700 (delete date) - } // Char structure of version 14797 (robe) - - safestrncpy(p->name, tmp_str[0], NAME_LENGTH); //Overflow protection [Skotlex] - p->char_id = tmp_int[0]; - p->account_id = tmp_int[1]; - p->slot = tmp_int[2]; - p->class_ = tmp_int[3]; - p->base_level = tmp_int[4]; - p->job_level = tmp_int[5]; - p->base_exp = tmp_uint[0]; - p->job_exp = tmp_uint[1]; - p->zeny = tmp_int[8]; - p->hp = tmp_int[9]; - p->max_hp = tmp_int[10]; - p->sp = tmp_int[11]; - p->max_sp = tmp_int[12]; - p->str = tmp_int[13]; - p->agi = tmp_int[14]; - p->vit = tmp_int[15]; - p->int_ = tmp_int[16]; - p->dex = tmp_int[17]; - p->luk = tmp_int[18]; - p->status_point = tmp_int[19]; - p->skill_point = tmp_int[20]; - p->option = tmp_int[21]; - p->karma = tmp_int[22]; - p->manner = tmp_int[23]; - p->party_id = tmp_int[24]; - p->guild_id = tmp_int[25]; - p->pet_id = tmp_int[26]; - p->hair = tmp_int[27]; - p->hair_color = tmp_int[28]; - p->clothes_color = tmp_int[29]; - p->weapon = tmp_int[30]; - p->shield = tmp_int[31]; - p->head_top = tmp_int[32]; - p->head_mid = tmp_int[33]; - p->head_bottom = tmp_int[34]; - p->last_point.x = tmp_int[35]; - p->last_point.y = tmp_int[36]; - p->save_point.x = tmp_int[37]; - p->save_point.y = tmp_int[38]; - p->partner_id = tmp_int[39]; - p->father = tmp_int[40]; - p->mother = tmp_int[41]; - p->child = tmp_int[42]; - p->fame = tmp_int[43]; - p->hom_id = tmp_int[44]; - p->last_point.map = tmp_int[45]; - p->save_point.map = tmp_int[46]; - p->delete_date = tmp_ulong[0]; - p->robe = tmp_int[47]; - -#ifndef TXT_SQL_CONVERT - // Some checks - for(i = 0; i < char_num; i++) { - if (char_dat[i].status.char_id == p->char_id) { - ShowError(CL_RED"mmmo_auth_init: a character has an identical id to another.\n"); - ShowError(" character id #%d -> new character not readed.\n", p->char_id); - ShowError(" Character saved in log file."CL_RESET"\n"); - return -1; - } else if (strcmp(char_dat[i].status.name, p->name) == 0) { - ShowError(CL_RED"mmmo_auth_init: a character name already exists.\n"); - ShowError(" character name '%s' -> new character not read.\n", p->name); - ShowError(" Character saved in log file."CL_RESET"\n"); - return -2; + + diff = 0; + for(i = 0; i < MAX_FRIENDS; i++){ + if(p->friends[i].char_id != cp->friends[i].char_id || + p->friends[i].account_id != cp->friends[i].account_id){ + diff = 1; + break; } } - if (strcmpi(wisp_server_name, p->name) == 0) { - ShowWarning("mmo_auth_init: ******WARNING: character name has wisp server name.\n"); - ShowWarning(" Character name '%s' = wisp server name '%s'.\n", p->name, wisp_server_name); - ShowWarning(" Character readed. Suggestion: change the wisp server name.\n"); - char_log("mmo_auth_init: ******WARNING: character name has wisp server name: Character name '%s' = wisp server name '%s'.\n", - p->name, wisp_server_name); - } -#endif //TXT_SQL_CONVERT - if (str[next] == '\n' || str[next] == '\r') - return 1; // 新規データ - - next++; - - for(i = 0; str[next] && str[next] != '\t'; i++) { - //mapindex memo format - if (sscanf(str+next, "%d,%d,%d%n", &tmp_int[2], &tmp_int[0], &tmp_int[1], &len) != 3) - { //Old string-based memo format. - if (sscanf(str+next, "%[^,],%d,%d%n", tmp_str[0], &tmp_int[0], &tmp_int[1], &len) != 3) - return -3; - tmp_int[2] = mapindex_name2id(tmp_str[0]); + if(diff == 1) + { //Save friends + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id) ) + { + Sql_ShowDebug(sql_handle); + errors++; } - if (i < MAX_MEMOPOINTS) - { //Avoid overflowing (but we must also read through all saved memos) - p->memo_point[i].x = tmp_int[0]; - p->memo_point[i].y = tmp_int[1]; - p->memo_point[i].map = tmp_int[2]; + + StringBuf_Clear(&buf); + StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db); + for( i = 0, count = 0; i < MAX_FRIENDS; ++i ) + { + if( p->friends[i].char_id > 0 ) + { + if( count ) + StringBuf_AppendStr(&buf, ","); + StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id); + count++; + } + } + if( count ) + { + if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } } - next += len; - if (str[next] == ' ') - next++; + strcat(save_status, " friends"); } - next++; - - for(i = 0; str[next] && str[next] != '\t'; i++) { - if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], - &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8) +#ifdef HOTKEY_SAVING + // hotkeys + StringBuf_Clear(&buf); + StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", hotkey_db); + diff = 0; + for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){ + if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey))) { - p->inventory[i].id = tmp_int[0]; - p->inventory[i].nameid = tmp_int[1]; - p->inventory[i].amount = tmp_int[2]; - p->inventory[i].equip = tmp_int[3]; - p->inventory[i].identify = tmp_int[4]; - p->inventory[i].refine = tmp_int[5]; - p->inventory[i].attribute = tmp_int[6]; - - for(j = 0; j < MAX_SLOTS && tmp_str[0][0] && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++) - p->inventory[i].card[j] = tmp_int[0]; - - next += len; - if (str[next] == ' ') - next++; - } else // invalid structure - return -4; + if( diff ) + StringBuf_AppendStr(&buf, ",");// not the first hotkey + 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); + diff = 1; + } } - next++; - - for(i = 0; str[next] && str[next] != '\t'; i++) { - if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n", - &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], - &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8) + if(diff) { + if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) { - p->cart[i].id = tmp_int[0]; - p->cart[i].nameid = tmp_int[1]; - p->cart[i].amount = tmp_int[2]; - p->cart[i].equip = tmp_int[3]; - p->cart[i].identify = tmp_int[4]; - p->cart[i].refine = tmp_int[5]; - p->cart[i].attribute = tmp_int[6]; - - for(j = 0; j < MAX_SLOTS && tmp_str[0][0] && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++) - p->cart[i].card[j] = tmp_int[0]; - - next += len; - if (str[next] == ' ') - next++; - } else // invalid structure - return -5; + Sql_ShowDebug(sql_handle); + errors++; + } else + strcat(save_status, " hotkeys"); } +#endif + StringBuf_Destroy(&buf); + if (save_status[0]!='\0' && save_log) + ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status); + if (!errors) + memcpy(cp, p, sizeof(struct mmo_charstatus)); + return 0; +} + +/// Saves an array of 'item' entries into the specified table. +int memitemdata_to_sql(const struct item items[], int max, int id, int tableswitch) +{ + StringBuf buf; + SqlStmt* stmt; + int i; + int j; + const char* tablename; + const char* selectoption; + struct item item; // temp storage variable + bool* flag; // bit array for inventory matching + bool found; + int errors = 0; + + switch (tableswitch) { + case TABLE_INVENTORY: tablename = inventory_db; selectoption = "char_id"; break; + case TABLE_CART: tablename = cart_db; selectoption = "char_id"; break; + case TABLE_STORAGE: tablename = storage_db; selectoption = "account_id"; break; + case TABLE_GUILD_STORAGE: tablename = guild_storage_db; selectoption = "guild_id"; break; + default: + ShowError("Invalid table name!\n"); + return 1; + } + - next++; + // The following code compares inventory with current database values + // and performs modification/deletion/insertion only on relevant rows. + // This approach is more complicated than a trivial delete&insert, but + // it significantly reduces cpu load on the database server. - for(i = 0; str[next] && str[next] != '\t'; i++) { - if (sscanf(str + next, "%d,%d%n", &tmp_int[0], &tmp_int[1], &len) != 2) - return -6; - p->skill[tmp_int[0]].id = tmp_int[0]; - p->skill[tmp_int[0]].lv = tmp_int[1]; - next += len; - if (str[next] == ' ') - next++; + StringBuf_Init(&buf); + StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`"); + for( j = 0; j < MAX_SLOTS; ++j ) + StringBuf_Printf(&buf, ", `card%d`", j); + StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id); + + stmt = SqlStmt_Malloc(sql_handle); + if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) + || SQL_ERROR == SqlStmt_Execute(stmt) ) + { + SqlStmt_ShowDebug(stmt); + SqlStmt_Free(stmt); + StringBuf_Destroy(&buf); + return 1; } - next++; + SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL); + SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL); + SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL); + SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL); + SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL); + SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL); + SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL); + SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL); + for( j = 0; j < MAX_SLOTS; ++j ) + SqlStmt_BindColumn(stmt, 8+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL); + + // bit array indicating which inventory items have already been matched + flag = (bool*) aCallocA(max, sizeof(bool)); + + while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ) + { + found = false; + // search for the presence of the item in the char's inventory + for( i = 0; i < max; ++i ) + { + // skip empty and already matched entries + if( items[i].nameid == 0 || flag[i] ) + continue; - for(i = 0; str[next] && str[next] != '\t' && str[next] != '\n' && str[next] != '\r'; i++) { // global_reg実装以前のathena.txt互換のため一応'\n'チェック - if (sscanf(str + next, "%[^,],%[^ ] %n", reg[i].str, reg[i].value, &len) != 2) { - // because some scripts are not correct, the str can be "". So, we must check that. - // If it's, we must not refuse the character, but just this REG value. - // Character line will have something like: nov_2nd_cos,9 ,9 nov_1_2_cos_c,1 (here, ,9 is not good) - if (str[next] == ',' && sscanf(str + next, ",%[^ ] %n", reg[i].value, &len) == 1) - i--; - else - return -7; + if( items[i].nameid == item.nameid + && items[i].card[0] == item.card[0] + && items[i].card[2] == item.card[2] + && items[i].card[3] == item.card[3] + ) { //They are the same item. + ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] ); + if( j == MAX_SLOTS && + items[i].amount == item.amount && + items[i].equip == item.equip && + items[i].identify == item.identify && + items[i].refine == item.refine && + items[i].attribute == item.attribute && + items[i].expire_time == item.expire_time ) + ; //Do nothing. + else + { + // update all fields. + StringBuf_Clear(&buf); + StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u'", + tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time); + for( j = 0; j < MAX_SLOTS; ++j ) + StringBuf_Printf(&buf, ", `card%d`=%d", j, items[i].card[j]); + StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id); + + if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } + } + + found = flag[i] = true; //Item dealt with, + break; //skip to next item in the db. + } + } + if( !found ) + {// Item not present in inventory, remove it. + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", tablename, item.id) ) + { + Sql_ShowDebug(sql_handle); + errors++; + } } - next += len; - if (str[next] == ' ') - next++; } - *reg_num = i; + SqlStmt_Free(stmt); - return 1; -} + StringBuf_Clear(&buf); + StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`", tablename, selectoption); + for( j = 0; j < MAX_SLOTS; ++j ) + StringBuf_Printf(&buf, ", `card%d`", j); + StringBuf_AppendStr(&buf, ") VALUES "); -//--------------------------------- -// Function to read friend list -//--------------------------------- -int parse_friend_txt(struct mmo_charstatus *p) -{ - char line[1024], temp[1024]; - int pos = 0, count = 0, next; - int i,len; - FILE *fp; + found = false; + // insert non-matched items into the db as new items + for( i = 0; i < max; ++i ) + { + // skip empty and already matched entries + if( items[i].nameid == 0 || flag[i] ) + continue; - // Open the file and look for the ID - fp = fopen(friends_txt, "r"); + if( found ) + StringBuf_AppendStr(&buf, ","); + else + found = true; - if(fp == NULL) - return -1; - - while(fgets(line, sizeof(line), fp)) + StringBuf_Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u'", + id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time); + for( j = 0; j < MAX_SLOTS; ++j ) + StringBuf_Printf(&buf, ", '%d'", items[i].card[j]); + StringBuf_AppendStr(&buf, ")"); + } + + if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) { - if(line[0] == '/' && line[1] == '/') - continue; - if (sscanf(line, "%d%n",&i, &pos) < 1 || i != p->char_id) - continue; //Not this line... - //Read friends - len = strlen(line); - next = pos; - for (count = 0; next < len && count < MAX_FRIENDS; count++) - { //Read friends. - if (sscanf(line+next, ",%d,%d,%23[^,^\n]%n",&p->friends[count].account_id,&p->friends[count].char_id, p->friends[count].name, &pos) < 3) - { //Invalid friend? - memset(&p->friends[count], 0, sizeof(p->friends[count])); - break; - } - next+=pos; - //What IF the name contains a comma? while the next field is not a - //number, we assume it belongs to the current name. [Skotlex] - //NOTE: Of course, this will fail if someone sets their name to something like - //Bob,2005 but... meh, it's the problem of parsing a text file (encasing it in " - //won't do as quotes are also valid name chars!) - while(next < len && sscanf(line+next, ",%23[^,^\n]%n", temp, &pos) > 0) - { - if (atoi(temp)) //We read the next friend, just continue. - break; - //Append the name. - next+=pos; - i = strlen(p->friends[count].name); - if (i + strlen(temp) +1 < NAME_LENGTH) - { - p->friends[count].name[i] = ','; - strcpy(p->friends[count].name+i+1, temp); - } - } //End Guess Block - } //Friend's for. - break; //Found friends. + Sql_ShowDebug(sql_handle); + errors++; } - fclose(fp); - return count; + + StringBuf_Destroy(&buf); + aFree(flag); + + return errors; } -//--------------------------------- -// Function to read hotkey list -//--------------------------------- -int parse_hotkey_txt(struct mmo_charstatus *p) +int mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p); + +//===================================================================================================== +// Loads the basic character rooster for the given account. Returns total buffer used. +int mmo_chars_fromsql(struct char_session_data* sd, uint8* buf) { -#ifdef HOTKEY_SAVING - char line[1024]; - int pos = 0, count = 0, next; - int i,len; - int type, id, lv; - FILE *fp; + SqlStmt* stmt; + struct mmo_charstatus p; + int j = 0, i; + char last_map[MAP_NAME_LENGTH_EXT]; - // Open the file and look for the ID - fp = fopen(hotkeys_txt, "r"); - if(fp == NULL) - return -1; - - while(fgets(line, sizeof(line), fp)) + stmt = SqlStmt_Malloc(sql_handle); + if( stmt == NULL ) { - if(line[0] == '/' && line[1] == '/') + SqlStmt_ShowDebug(stmt); + return 0; + } + memset(&p, 0, sizeof(p)); + + // read char data + if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT " + "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`," + "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`," + "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`," + "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`rename`,`delete_date`," + "`robe`" + " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", char_db, sd->account_id, MAX_CHARS) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p.base_exp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p.job_exp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_INT, &p.max_hp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p.hp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p.max_sp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p.sp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p.status_point, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p.skill_point, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.weapon, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.shield, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.head_top, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p.rename, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_UINT32, &p.delete_date, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p.robe, 0, NULL, NULL) + ) + { + SqlStmt_ShowDebug(stmt); + SqlStmt_Free(stmt); + return 0; + } + for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ ) + { + if( p.delete_date && p.delete_date < time(NULL) ) { + delete_char_sql(p.char_id); + i--; continue; - if (sscanf(line, "%d%n",&i, &pos) < 1 || i != p->char_id) - continue; //Not this line... - //Read hotkeys - len = strlen(line); - next = pos; - for (count = 0; next < len && count < MAX_HOTKEYS; count++) - { - if (sscanf(line+next, ",%d,%d,%d%n",&type,&id,&lv, &pos) < 3) - //Invalid entry? - break; - p->hotkeys[count].type = type; - p->hotkeys[count].id = id; - p->hotkeys[count].lv = lv; - next+=pos; } - break; //Found hotkeys. + p.last_point.map = mapindex_name2id(last_map); + sd->found_char[i] = p.char_id; + j += mmo_char_tobuf(WBUFP(buf, j), &p); } - fclose(fp); - return count; -#else - return 0; -#endif -} + for( ; i < MAX_CHARS; i++ ) + sd->found_char[i] = -1; + memset(sd->new_name,0,sizeof(sd->new_name)); + SqlStmt_Free(stmt); + return j; +} -#ifndef TXT_SQL_CONVERT -//--------------------------------- -// Function to read characters file -//--------------------------------- -int mmo_char_init(void) +//===================================================================================================== +int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything) { - char line[65536]; - int ret, line_count; - FILE* fp; + int i,j; + char t_msg[128] = ""; + struct mmo_charstatus* cp; + StringBuf buf; + SqlStmt* stmt; + char last_map[MAP_NAME_LENGTH_EXT]; + char save_map[MAP_NAME_LENGTH_EXT]; + char point_map[MAP_NAME_LENGTH_EXT]; + struct point tmp_point; + struct item tmp_item; + struct s_skill tmp_skill; + struct s_friend tmp_friend; +#ifdef HOTKEY_SAVING + struct hotkey tmp_hotkey; + int hotkey_num; +#endif - char_num = 0; - char_max = 0; - char_dat = NULL; + memset(p, 0, sizeof(struct mmo_charstatus)); + + if (save_log) ShowInfo("Char load request (%d)\n", char_id); - fp = fopen(char_txt, "r"); + stmt = SqlStmt_Malloc(sql_handle); + if( stmt == NULL ) + { + SqlStmt_ShowDebug(stmt); + return 0; + } - if (fp == NULL) { - ShowError("Characters file not found: %s.\n", char_txt); - char_log("Characters file not found: %s.\n", char_txt); - char_log("Id for the next created character: %d.\n", char_id_count); + // read char data + if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT " + "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`," + "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`," + "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`hair`," + "`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`," + "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`" + " FROM `%s` WHERE `char_id`=? LIMIT 1", char_db) + || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p->base_exp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &p->job_exp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p->max_hp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p->hp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p->max_sp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_INT, &p->sp, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p->status_point, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p->skill_point, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p->hair, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->weapon, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->shield, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->head_top, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_INT, &p->partner_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_INT, &p->father, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_INT, &p->mother, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_INT, &p->child, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_INT, &p->fame, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_SHORT, &p->rename, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_UINT32, &p->delete_date, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 50, SQLDT_SHORT, &p->robe, 0, NULL, NULL) + ) + { + SqlStmt_ShowDebug(stmt); + SqlStmt_Free(stmt); return 0; } + if( SQL_ERROR == SqlStmt_NextRow(stmt) ) + { + ShowError("Requested non-existant character id: %d!\n", char_id); + SqlStmt_Free(stmt); + return 0; + } + p->last_point.map = mapindex_name2id(last_map); + p->save_point.map = mapindex_name2id(save_map); - line_count = 0; - while(fgets(line, sizeof(line), fp)) + strcat(t_msg, " status"); + + if (!load_everything) // For quick selection of data when displaying the char menu { - int i, j; - line_count++; + SqlStmt_Free(stmt); + return 1; + } - if (line[0] == '/' && line[1] == '/') - continue; + //read memo data + //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`) + if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", memo_db, MAX_MEMOPOINTS) + || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + + for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i ) + { + tmp_point.map = mapindex_name2id(point_map); + memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point)); + } + strcat(t_msg, " memo"); + + //read inventory + //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`) + StringBuf_Init(&buf); + StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`"); + for( i = 0; i < MAX_SLOTS; ++i ) + StringBuf_Printf(&buf, ", `card%d`", i); + StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", inventory_db, MAX_INVENTORY); + + if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) + || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + for( i = 0; i < MAX_SLOTS; ++i ) + if( SQL_ERROR == SqlStmt_BindColumn(stmt, 8+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + + for( i = 0; i < MAX_INVENTORY && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i ) + memcpy(&p->inventory[i], &tmp_item, sizeof(tmp_item)); + + strcat(t_msg, " inventory"); + + //read cart + //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`) + StringBuf_Clear(&buf); + StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`"); + for( j = 0; j < MAX_SLOTS; ++j ) + StringBuf_Printf(&buf, ", `card%d`", j); + StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", cart_db, MAX_CART); + + if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) + || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + for( i = 0; i < MAX_SLOTS; ++i ) + if( SQL_ERROR == SqlStmt_BindColumn(stmt, 8+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + + for( i = 0; i < MAX_CART && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i ) + memcpy(&p->cart[i], &tmp_item, sizeof(tmp_item)); + strcat(t_msg, " cart"); + + //read storage + storage_fromsql(p->account_id, &p->storage); + strcat(t_msg, " storage"); + + //read skill + //`skill` (`char_id`, `id`, `lv`) + if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`=? LIMIT %d", skill_db, MAX_SKILL) + || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_USHORT, &tmp_skill.id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &tmp_skill.lv, 0, NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + tmp_skill.flag = SKILL_FLAG_PERMANENT; + + for( i = 0; i < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i ) + { + if( tmp_skill.id < ARRAYLENGTH(p->skill) ) + memcpy(&p->skill[tmp_skill.id], &tmp_skill, sizeof(tmp_skill)); + else + 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); + } + strcat(t_msg, " skills"); + + //read friends + //`friends` (`char_id`, `friend_account`, `friend_id`) + if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT c.`account_id`, c.`char_id`, c.`name` FROM `%s` c LEFT JOIN `%s` f ON f.`friend_account` = c.`account_id` AND f.`friend_id` = c.`char_id` WHERE f.`char_id`=? LIMIT %d", char_db, friend_db, MAX_FRIENDS) + || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + + for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i ) + memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend)); + strcat(t_msg, " friends"); - j = 0; - if (sscanf(line, "%d\t%%newid%%%n", &i, &j) == 1 && j > 0) { - if (char_id_count < i) - char_id_count = i; - continue; - } +#ifdef HOTKEY_SAVING + //read hotkeys + //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl` + if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", hotkey_db) + || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0) + || SQL_ERROR == SqlStmt_Execute(stmt) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL) + || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) ) + SqlStmt_ShowDebug(stmt); + + while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ) + { + if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS ) + memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey)); + else + 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); + } + strcat(t_msg, " hotkeys"); +#endif - if (char_num >= char_max) { - char_max += 256; - char_dat = (struct character_data*)aRealloc(char_dat, sizeof(struct character_data) * char_max); - if (!char_dat) { - ShowFatalError("Out of memory: mmo_char_init (realloc of char_dat).\n"); - char_log("Out of memory: mmo_char_init (realloc of char_dat).\n"); - exit(EXIT_FAILURE); - } - } + /* Mercenary Owner DataBase */ + mercenary_owner_fromsql(char_id, p); + strcat(t_msg, " mercenary"); - ret = mmo_char_fromstr(line, &char_dat[char_num].status, char_dat[char_num].global, &char_dat[char_num].global_num); - // Initialize friends list - parse_friend_txt(&char_dat[char_num].status); // Grab friends for the character - // Initialize hotkey list - parse_hotkey_txt(&char_dat[char_num].status); // Grab hotkeys for the character - - if (ret > 0) { // negative value or zero for errors - if (char_dat[char_num].status.char_id >= char_id_count) - char_id_count = char_dat[char_num].status.char_id + 1; - char_num++; - } else { - ShowError("mmo_char_init: in characters file, unable to read the line #%d.\n", line_count); - ShowError(" -> Character saved in log file.\n"); - switch (ret) { - case -1: - char_log("Duplicate character id in the next character line (character not readed):\n"); - break; - case -2: - char_log("Duplicate character name in the next character line (character not readed):\n"); - break; - case -3: - char_log("Invalid memo point structure in the next character line (character not readed):\n"); - break; - case -4: - char_log("Invalid inventory item structure in the next character line (character not readed):\n"); - break; - case -5: - char_log("Invalid cart item structure in the next character line (character not readed):\n"); - break; - case -6: - char_log("Invalid skill structure in the next character line (character not readed):\n"); - break; - case -7: - char_log("Invalid register structure in the next character line (character not readed):\n"); - break; - default: // 0 - char_log("Unabled to get a character in the next line - Basic structure of line (before inventory) is incorrect (character not readed):\n"); - break; - } - char_log("%s", line); - } - } - fclose(fp); + if (save_log) ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly! + SqlStmt_Free(stmt); + StringBuf_Destroy(&buf); - if (char_num == 0) { - ShowNotice("mmo_char_init: No character found in %s.\n", char_txt); - char_log("mmo_char_init: No character found in %s.\n", char_txt); - } else if (char_num == 1) { - ShowStatus("mmo_char_init: 1 character read in %s.\n", char_txt); - char_log("mmo_char_init: 1 character read in %s.\n", char_txt); - } else { - ShowStatus("mmo_char_init: %d characters read in %s.\n", char_num, char_txt); - char_log("mmo_char_init: %d characters read in %s.\n", char_num, char_txt); + cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus); + memcpy(cp, p, sizeof(struct mmo_charstatus)); + return 1; +} + +//========================================================================================================== +int mmo_char_sql_init(void) +{ + ShowInfo("Begin Initializing.......\n"); + char_db_= idb_alloc(DB_OPT_RELEASE_DATA); + + if(char_per_account == 0){ + ShowStatus("Chars per Account: 'Unlimited'.......\n"); + }else{ + ShowStatus("Chars per Account: '%d'.......\n", char_per_account); } - char_log("Id for the next created character: %d.\n", char_id_count); + //the 'set offline' part is now in check_login_conn ... + //if the server connects to loginserver + //it will dc all off players + //and send the loginserver the new state.... + + // Force all users offline in sql when starting char-server + // (useful when servers crashs and don't clean the database) + set_all_offline_sql(); + + ShowInfo("Finished initilizing.......\n"); return 0; } -//--------------------------------------------------------- -// Function to save characters in files (speed up by [Yor]) -//--------------------------------------------------------- -void mmo_char_sync(void) +//----------------------------------- +// Function to change chararcter's names +//----------------------------------- +int rename_char_sql(struct char_session_data *sd, int char_id) { - char line[65536],f_line[1024]; - int i, j, k; - int lock; - FILE *fp,*f_fp; - CREATE_BUFFER(id, int, char_num); - - // Sorting before save (by [Yor]) - for(i = 0; i < char_num; i++) { - id[i] = i; - for(j = 0; j < i; j++) { - if ((char_dat[i].status.account_id < char_dat[id[j]].status.account_id) || - // if same account id, we sort by slot. - (char_dat[i].status.account_id == char_dat[id[j]].status.account_id && - char_dat[i].status.slot < char_dat[id[j]].status.slot)) { - for(k = i; k > j; k--) - id[k] = id[k-1]; - id[j] = i; // id[i] - break; - } - } - } + struct mmo_charstatus char_dat; + char esc_name[NAME_LENGTH*2+1]; - // Data save - fp = lock_fopen(char_txt, &lock); - if (fp == NULL) { - ShowWarning("Server cannot save characters.\n"); - char_log("WARNING: Server cannot save characters.\n"); - } else { - for(i = 0; i < char_num; i++) { - mmo_char_tostr(line, &char_dat[id[i]].status, char_dat[id[i]].global, char_dat[id[i]].global_num); // use of sorted index - fprintf(fp, "%s\n", line); - } - fprintf(fp, "%d\t%%newid%%\n", char_id_count); - lock_fclose(fp, char_txt, &lock); - } + if( sd->new_name[0] == 0 ) // Not ready for rename + return 2; + + if( !mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed. + return 2; - // Friends List data save (davidsiaw) - f_fp = lock_fopen(friends_txt, &lock); - for(i = 0; i < char_num; i++) { - mmo_friends_list_data_str(f_line, &char_dat[id[i]].status); - fprintf(f_fp, "%s\n", f_line); - } + if( char_dat.rename == 0 ) + return 1; - lock_fclose(f_fp, friends_txt, &lock); + Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH)); -#ifdef HOTKEY_SAVING - // Hotkey List data save (Skotlex) - f_fp = lock_fopen(hotkeys_txt, &lock); - for(i = 0; i < char_num; i++) { - mmo_hotkeys_tostr(f_line, &char_dat[id[i]].status); - fprintf(f_fp, "%s\n", f_line); + // check if the char exist + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` LIKE '%s' LIMIT 1", char_db, esc_name) ) + { + Sql_ShowDebug(sql_handle); + return 4; } - lock_fclose(f_fp, hotkeys_txt, &lock); -#endif + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `name` = '%s', `rename` = '%d' WHERE `char_id` = '%d'", char_db, esc_name, --char_dat.rename, char_id) ) + { + Sql_ShowDebug(sql_handle); + return 3; + } - DELETE_BUFFER(id); + // Change character's name into guild_db. + if( char_dat.guild_id ) + inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name); - return; -} + safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH); + memset(sd->new_name,0,sizeof(sd->new_name)); + + // log change + if( log_char ) + { + 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`)" + "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')", + charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) ) + Sql_ShowDebug(sql_handle); + } -//---------------------------------------------------- -// Function to save (in a periodic way) datas in files -//---------------------------------------------------- -int mmo_char_sync_timer(int tid, unsigned int tick, int id, intptr_t data) -{ - if (save_log) - ShowInfo("Saving all files...\n"); - mmo_char_sync(); - inter_save(); return 0; } -int check_char_name(char * name) +int check_char_name(char * name, char * esc_name) { int i; // check length of character name if( name[0] == '\0' ) return -2; // empty character name - + /** + * The client does not allow you to create names with less than 4 characters, however, + * the use of WPE can bypass this, and this fixes the exploit. + **/ + if( strlen( name ) < 4 ) + return -2; // check content of character name if( remove_control_chars(name) ) return -2; // control chars in name @@ -1267,17 +1296,18 @@ int check_char_name(char * name) if( strchr(char_name_letters, name[i]) != NULL ) return -2; } - - // check name (already in use?) - if( name_ignoring_case ) - { - ARR_FIND( 0, char_num, i, strncmp(char_dat[i].status.name, name, NAME_LENGTH) == 0 ); - } - else - { - ARR_FIND( 0, char_num, i, strncmpi(char_dat[i].status.name, name, NAME_LENGTH) == 0 ); + if( name_ignoring_case ) { + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE BINARY `name` = '%s' LIMIT 1", char_db, esc_name) ) { + Sql_ShowDebug(sql_handle); + return -2; + } + } else { + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s' LIMIT 1", char_db, esc_name) ) { + Sql_ShowDebug(sql_handle); + return -2; + } } - if( i < char_num ) + if( Sql_NumRows(sql_handle) > 0 ) return -1; // name already exists return 0; @@ -1286,15 +1316,17 @@ int check_char_name(char * name) //----------------------------------- // Function to create a new character //----------------------------------- -int make_new_char(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) +int make_new_char_sql(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style) { char name[NAME_LENGTH]; - int i, flag; - + char esc_name[NAME_LENGTH*2+1]; + int char_id, flag; + safestrncpy(name, name_, NAME_LENGTH); normalize_name(name,TRIM_CHARS); + Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH)); - flag = check_char_name(name); + flag = check_char_name(name,esc_name); if( flag < 0 ) return flag; @@ -1307,471 +1339,226 @@ int make_new_char(struct char_session_data* sd, char* name_, int str, int agi, i // check the number of already existing chars in this account if( char_per_account != 0 ) { - ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == -1 ); - - if( i >= char_per_account ) + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id) ) + Sql_ShowDebug(sql_handle); + if( Sql_NumRows(sql_handle) >= char_per_account ) return -2; // character account limit exceeded } // check char slot - ARR_FIND( 0, char_num, i, char_dat[i].status.account_id == sd->account_id && char_dat[i].status.slot == slot ); - if( i < char_num ) + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' LIMIT 1", char_db, sd->account_id, slot) ) + Sql_ShowDebug(sql_handle); + if( Sql_NumRows(sql_handle) > 0 ) return -2; // slot already in use - if (char_num >= char_max) { - char_max += 256; - RECREATE(char_dat, struct character_data, char_max); - if (!char_dat) { - ShowFatalError("Out of memory: make_new_char (realloc of char_dat).\n"); - char_log("Out of memory: make_new_char (realloc of char_dat).\n"); - exit(EXIT_FAILURE); - } + // validation success, log result + if (log_char) { + 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`)" + "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", + charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) ) + Sql_ShowDebug(sql_handle); } - // validation success, log result - char_log("make new char: account: %d, slot %d, name: %s, stats: %d/%d/%d/%d/%d/%d, hair: %d, hair color: %d.\n", - sd->account_id, slot, name, str, agi, vit, int_, dex, luk, hair_style, hair_color); - - i = char_num; - memset(&char_dat[i], 0, sizeof(struct character_data)); - - char_dat[i].status.char_id = char_id_count++; - char_dat[i].status.account_id = sd->account_id; - char_dat[i].status.slot = slot; - safestrncpy(char_dat[i].status.name,name,NAME_LENGTH); - char_dat[i].status.class_ = 0; - char_dat[i].status.base_level = 1; - char_dat[i].status.job_level = 1; - char_dat[i].status.base_exp = 0; - char_dat[i].status.job_exp = 0; - char_dat[i].status.zeny = start_zeny; - char_dat[i].status.str = str; - char_dat[i].status.agi = agi; - char_dat[i].status.vit = vit; - char_dat[i].status.int_ = int_; - char_dat[i].status.dex = dex; - char_dat[i].status.luk = luk; - char_dat[i].status.max_hp = 40 * (100 + char_dat[i].status.vit) / 100; - char_dat[i].status.max_sp = 11 * (100 + char_dat[i].status.int_) / 100; - char_dat[i].status.hp = char_dat[i].status.max_hp; - char_dat[i].status.sp = char_dat[i].status.max_sp; - char_dat[i].status.status_point = 0; - char_dat[i].status.skill_point = 0; - char_dat[i].status.option = 0; - char_dat[i].status.karma = 0; - char_dat[i].status.manner = 0; - char_dat[i].status.party_id = 0; - char_dat[i].status.guild_id = 0; - char_dat[i].status.hair = hair_style; - char_dat[i].status.hair_color = hair_color; - char_dat[i].status.clothes_color = 0; - char_dat[i].status.inventory[0].nameid = start_weapon; // Knife - char_dat[i].status.inventory[0].amount = 1; - char_dat[i].status.inventory[0].identify = 1; - char_dat[i].status.inventory[1].nameid = start_armor; // Cotton Shirt - char_dat[i].status.inventory[1].amount = 1; - char_dat[i].status.inventory[1].identify = 1; - char_dat[i].status.weapon = 0; // W_FIST - char_dat[i].status.shield = 0; - char_dat[i].status.head_top = 0; - char_dat[i].status.head_mid = 0; - char_dat[i].status.head_bottom = 0; - memcpy(&char_dat[i].status.last_point, &start_point, sizeof(start_point)); - memcpy(&char_dat[i].status.save_point, &start_point, sizeof(start_point)); - char_num++; - - ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, i, slot, name); - mmo_char_sync(); - return i; + //Insert the new char entry to the database + 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`," + "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES (" + "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')", + char_db, sd->account_id , slot, esc_name, start_zeny, str, agi, vit, int_, dex, luk, + (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color, + mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y) ) + { + Sql_ShowDebug(sql_handle); + return -2; //No, stop the procedure! + } + //Retrieve the newly auto-generated char id + char_id = (int)Sql_LastInsertId(sql_handle); + //Give the char the default items + if (start_weapon > 0) { //add Start Weapon (Knife?) + if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_weapon, 1, 1) ) + Sql_ShowDebug(sql_handle); + } + if (start_armor > 0) { //Add default armor (cotton shirt?) + if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_armor, 1, 1) ) + Sql_ShowDebug(sql_handle); + } + + ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name); + return char_id; } -//---------------------------------------------------- -// This function return the name of the job (by [Yor]) -//---------------------------------------------------- -char * job_name(int class_) +/*----------------------------------------------------------------------------------------------------------*/ +/* Divorce Players */ +/*----------------------------------------------------------------------------------------------------------*/ +int divorce_char_sql(int partner_id1, int partner_id2) { - switch (class_) { - case JOB_NOVICE: return "Novice"; - case JOB_SWORDMAN: return "Swordsman"; - case JOB_MAGE: return "Mage"; - case JOB_ARCHER: return "Archer"; - case JOB_ACOLYTE: return "Acolyte"; - case JOB_MERCHANT: return "Merchant"; - case JOB_THIEF: return "Thief"; - case JOB_KNIGHT: return "Knight"; - case JOB_PRIEST: return "Priest"; - case JOB_WIZARD: return "Wizard"; - case JOB_BLACKSMITH: return "Blacksmith"; - case JOB_HUNTER: return "Hunter"; - case JOB_ASSASSIN: return "Assassin"; - case JOB_KNIGHT2: return "Peco-Knight"; - case JOB_CRUSADER: return "Crusader"; - case JOB_MONK: return "Monk"; - case JOB_SAGE: return "Sage"; - case JOB_ROGUE: return "Rogue"; - case JOB_ALCHEMIST: return "Alchemist"; - case JOB_BARD: return "Bard"; - case JOB_DANCER: return "Dancer"; - case JOB_CRUSADER2: return "Peco-Crusader"; - case JOB_WEDDING: return "Wedding"; - case JOB_SUPER_NOVICE: return "Super Novice"; - case JOB_GUNSLINGER: return "Gunslinger"; - case JOB_NINJA: return "Ninja"; - case JOB_XMAS: return "Christmas"; - case JOB_NOVICE_HIGH: return "Novice High"; - case JOB_SWORDMAN_HIGH: return "Swordsman High"; - case JOB_MAGE_HIGH: return "Mage High"; - case JOB_ARCHER_HIGH: return "Archer High"; - case JOB_ACOLYTE_HIGH: return "Acolyte High"; - case JOB_MERCHANT_HIGH: return "Merchant High"; - case JOB_THIEF_HIGH: return "Thief High"; - case JOB_LORD_KNIGHT: return "Lord Knight"; - case JOB_HIGH_PRIEST: return "High Priest"; - case JOB_HIGH_WIZARD: return "High Wizard"; - case JOB_WHITESMITH: return "Whitesmith"; - case JOB_SNIPER: return "Sniper"; - case JOB_ASSASSIN_CROSS: return "Assassin Cross"; - case JOB_LORD_KNIGHT2: return "Peko Knight"; - case JOB_PALADIN: return "Paladin"; - case JOB_CHAMPION: return "Champion"; - case JOB_PROFESSOR: return "Professor"; - case JOB_STALKER: return "Stalker"; - case JOB_CREATOR: return "Creator"; - case JOB_CLOWN: return "Clown"; - case JOB_GYPSY: return "Gypsy"; - case JOB_PALADIN2: return "Peko Paladin"; - case JOB_BABY: return "Baby Novice"; - case JOB_BABY_SWORDMAN: return "Baby Swordsman"; - case JOB_BABY_MAGE: return "Baby Mage"; - case JOB_BABY_ARCHER: return "Baby Archer"; - case JOB_BABY_ACOLYTE: return "Baby Acolyte"; - case JOB_BABY_MERCHANT: return "Baby Merchant"; - case JOB_BABY_THIEF: return "Baby Thief"; - case JOB_BABY_KNIGHT: return "Baby Knight"; - case JOB_BABY_PRIEST: return "Baby Priest"; - case JOB_BABY_WIZARD: return "Baby Wizard"; - case JOB_BABY_BLACKSMITH: return "Baby Blacksmith"; - case JOB_BABY_HUNTER: return "Baby Hunter"; - case JOB_BABY_ASSASSIN: return "Baby Assassin"; - case JOB_BABY_KNIGHT2: return "Baby Peco Knight"; - case JOB_BABY_CRUSADER: return "Baby Crusader"; - case JOB_BABY_MONK: return "Baby Monk"; - case JOB_BABY_SAGE: return "Baby Sage"; - case JOB_BABY_ROGUE: return "Baby Rogue"; - case JOB_BABY_ALCHEMIST: return "Baby Alchemist"; - case JOB_BABY_BARD: return "Baby Bard"; - case JOB_BABY_DANCER: return "Baby Dancer"; - case JOB_BABY_CRUSADER2: return "Baby Peco Crusader"; - case JOB_SUPER_BABY: return "Super Baby"; - case JOB_TAEKWON: return "Taekwon"; - case JOB_STAR_GLADIATOR: return "Star Gladiator"; - case JOB_STAR_GLADIATOR2: return "Flying Star Gladiator"; - case JOB_SOUL_LINKER: return "Soul Linker"; - } - return "Unknown Job"; + unsigned char buf[64]; + + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d' LIMIT 2", char_db, partner_id1, partner_id2) ) + Sql_ShowDebug(sql_handle); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND (`char_id`='%d' OR `char_id`='%d') LIMIT 2", inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) ) + Sql_ShowDebug(sql_handle); + + WBUFW(buf,0) = 0x2b12; + WBUFL(buf,2) = partner_id1; + WBUFL(buf,6) = partner_id2; + mapif_sendall(buf,10); + + return 0; } -static int create_online_files_sub(DBKey key, void* data, va_list va) +/*----------------------------------------------------------------------------------------------------------*/ +/* Delete char - davidsiaw */ +/*----------------------------------------------------------------------------------------------------------*/ +/* Returns 0 if successful + * Returns < 0 for error + */ +int delete_char_sql(int char_id) { - struct online_char_data *character; - int* players; - int *id; - int j,k,l; - character = (struct online_char_data*) data; - players = va_arg(va, int*); - id = va_arg(va, int*); - - // check if map-server is online - if (character->server == -1 || character->char_id == -1) { //Character not currently online. + char name[NAME_LENGTH]; + char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped. + int account_id, party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id; + char* data; + size_t len; + + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name`,`account_id`,`party_id`,`guild_id`,`base_level`,`homun_id`,`partner_id`,`father`,`mother` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) ) + Sql_ShowDebug(sql_handle); + + if( SQL_SUCCESS != Sql_NextRow(sql_handle) ) + { + ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n"); + Sql_FreeResult(sql_handle); return -1; } - - j = character->server; - if (server[j].fd < 0) { - server[j].users = 0; - return -1; + + Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH); + Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data); + Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data); + Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data); + Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data); + Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data); + Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data); + Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data); + Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data); + + Sql_EscapeStringLen(sql_handle, esc_name, name, min(len, NAME_LENGTH)); + Sql_FreeResult(sql_handle); + + //check for config char del condition [Lupus] + // TODO: Move this out to packet processing (0x68/0x1fb). + if( ( char_del_level > 0 && base_level >= char_del_level ) + || ( char_del_level < 0 && base_level <= -char_del_level ) + ) { + ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level); + return -1; } - // search position of character in char_dat and sort online characters. - for(j = 0; j < char_num; j++) { - if (char_dat[j].status.char_id != character->char_id) - continue; - id[*players] = j; - // use sorting option - switch (online_sorting_option) { - case 1: // by name (without case sensitive) - for(k = 0; k < *players; k++) - if (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0 || - // if same name, we sort with case sensitive. - (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) == 0 && - strcmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) { - for(l = *players; l > k; l--) - id[l] = id[l-1]; - id[k] = j; // id[*players] - break; - } - break; - case 2: // by zeny - for(k = 0; k < *players; k++) - if (char_dat[j].status.zeny < char_dat[id[k]].status.zeny || - // if same number of zenys, we sort by name. - (char_dat[j].status.zeny == char_dat[id[k]].status.zeny && - stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) { - for(l = *players; l > k; l--) - id[l] = id[l-1]; - id[k] = j; // id[*players] - break; - } - break; - case 3: // by base level - for(k = 0; k < *players; k++) - if (char_dat[j].status.base_level < char_dat[id[k]].status.base_level || - // if same base level, we sort by base exp. - (char_dat[j].status.base_level == char_dat[id[k]].status.base_level && - char_dat[j].status.base_exp < char_dat[id[k]].status.base_exp)) { - for(l = *players; l > k; l--) - id[l] = id[l-1]; - id[k] = j; // id[*players] - break; - } - break; - case 4: // by job (and job level) - for(k = 0; k < *players; k++) - if (char_dat[j].status.class_ < char_dat[id[k]].status.class_ || - // if same job, we sort by job level. - (char_dat[j].status.class_ == char_dat[id[k]].status.class_ && - char_dat[j].status.job_level < char_dat[id[k]].status.job_level) || - // if same job and job level, we sort by job exp. - (char_dat[j].status.class_ == char_dat[id[k]].status.class_ && - char_dat[j].status.job_level == char_dat[id[k]].status.job_level && - char_dat[j].status.job_exp < char_dat[id[k]].status.job_exp)) { - for(l = *players; l > k; l--) - id[l] = id[l-1]; - id[k] = j; // id[*players] - break; - } - break; - case 5: // by location map name - { - const char *map1, *map2; - map1 = mapindex_id2name(char_dat[j].status.last_point.map); - - for(k = 0; k < *players; k++) { - map2 = mapindex_id2name(char_dat[id[k]].status.last_point.map); - if (!map1 || !map2 || //Avoid sorting if either one failed to resolve. - stricmp(map1, map2) < 0 || - // if same map name, we sort by name. - (stricmp(map1, map2) == 0 && - stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) { - for(l = *players; l > k; l--) - id[l] = id[l-1]; - id[k] = j; // id[*players] - break; - } - } - } - break; - default: // 0 or invalid value: no sorting - break; - } - (*players)++; - break; + + /* Divorce [Wizputer] */ + if( partner_id ) + divorce_char_sql(char_id, partner_id); + + /* De-addopt [Zephyrus] */ + if( father_id || mother_id ) + { // Char is Baby + unsigned char buf[64]; + + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", char_db, father_id, mother_id) ) + Sql_ShowDebug(sql_handle); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410'AND (`char_id`='%d' OR `char_id`='%d')", skill_db, father_id, mother_id) ) + Sql_ShowDebug(sql_handle); + + WBUFW(buf,0) = 0x2b25; + WBUFL(buf,2) = father_id; + WBUFL(buf,6) = mother_id; + WBUFL(buf,10) = char_id; // Baby + mapif_sendall(buf,14); } - return 0; -} -//------------------------------------------------------------- -// Function to create the online files (txt and html). by [Yor] -//------------------------------------------------------------- -void create_online_files(void) -{ - unsigned int k, j; // for loop with strlen comparing - int i, l; // for loops - int players; // count the number of players - FILE *fp; // for the txt file - FILE *fp2; // for the html file - char temp[256]; // to prepare what we must display - time_t time_server; // for number of seconds - struct tm *datetime; // variable for time in structure ->tm_mday, ->tm_sec, ... - int id[4096]; - - if (online_display_option == 0) // we display nothing, so return - return; - // Get number of online players, id of each online players, and verify if a server is offline - players = 0; - online_char_db->foreach(online_char_db, create_online_files_sub, &players, &id); - - // write files - fp = fopen(online_txt_filename, "w"); - if (fp != NULL) { - fp2 = fopen(online_html_filename, "w"); - if (fp2 != NULL) { - // get time - time(&time_server); // get time in seconds since 1/1/1970 - datetime = localtime(&time_server); // convert seconds in structure - strftime(temp, sizeof(temp), "%d %b %Y %X", datetime); // like sprintf, but only for date/time (05 dec 2003 15:12:52) - // write heading - fprintf(fp2, "<HTML>\n"); - fprintf(fp2, " <META http-equiv=\"Refresh\" content=\"%d\">\n", online_refresh_html); // update on client explorer every x seconds - fprintf(fp2, " <HEAD>\n"); - fprintf(fp2, " <TITLE>Online Players on %s</TITLE>\n", server_name); - fprintf(fp2, " </HEAD>\n"); - fprintf(fp2, " <BODY>\n"); - fprintf(fp2, " <H3>Online Players on %s (%s):</H3>\n", server_name, temp); - fprintf(fp, "Online Players on %s (%s):\n", server_name, temp); - fprintf(fp, "\n"); - - for (i = 0; i < players; i++) { - // if it's the first player - if (i == 0) { - j = 0; // count the number of characters for the txt version and to set the separate line - fprintf(fp2, " <table border=\"1\" cellspacing=\"1\">\n"); - fprintf(fp2, " <tr>\n"); - if ((online_display_option & 1) || (online_display_option & 64)) { - fprintf(fp2, " <td><b>Name</b></td>\n"); - if (online_display_option & 64) { - fprintf(fp, "Name "); // 30 - j += 30; - } else { - fprintf(fp, "Name "); // 25 - j += 25; - } - } - if ((online_display_option & 6) == 6) { - fprintf(fp2, " <td><b>Job (levels)</b></td>\n"); - fprintf(fp, "Job Levels "); // 27 - j += 27; - } else if (online_display_option & 2) { - fprintf(fp2, " <td><b>Job</b></td>\n"); - fprintf(fp, "Job "); // 19 - j += 19; - } else if (online_display_option & 4) { - fprintf(fp2, " <td><b>Levels</b></td>\n"); - fprintf(fp, " Levels "); // 8 - j += 8; - } - if (online_display_option & 24) { // 8 or 16 - fprintf(fp2, " <td><b>Location</b></td>\n"); - if (online_display_option & 16) { - fprintf(fp, "Location ( x , y ) "); // 23 - j += 23; - } else { - fprintf(fp, "Location "); // 13 - j += 13; - } - } - if (online_display_option & 32) { - fprintf(fp2, " <td ALIGN=CENTER><b>zenys</b></td>\n"); - fprintf(fp, " Zenys "); // 16 - j += 16; - } - fprintf(fp2, " </tr>\n"); - fprintf(fp, "\n"); - for (k = 0; k < j; k++) - fprintf(fp, "-"); - fprintf(fp, "\n"); - } - fprintf(fp2, " <tr>\n"); - // get id of the character (more speed) - j = id[i]; - // displaying the character name - if ((online_display_option & 1) || (online_display_option & 64)) { // without/with 'GM' display - safestrncpy(temp, char_dat[j].status.name, sizeof(temp)); - //l = isGM(char_dat[j].status.account_id); - l = 0; //FIXME: how to get the gm level? - if (online_display_option & 64) { - if (l >= online_gm_display_min_level) - fprintf(fp, "%-24s (GM) ", temp); - else - fprintf(fp, "%-24s ", temp); - } else - fprintf(fp, "%-24s ", temp); - // name of the character in the html (no < >, because that create problem in html code) - fprintf(fp2, " <td>"); - if ((online_display_option & 64) && l >= online_gm_display_min_level) - fprintf(fp2, "<b>"); - for (k = 0; k < strlen(temp); k++) { - switch(temp[k]) { - case '<': // < - fprintf(fp2, "<"); - break; - case '>': // > - fprintf(fp2, ">"); - break; - default: - fprintf(fp2, "%c", temp[k]); - break; - }; - } - if ((online_display_option & 64) && l >= online_gm_display_min_level) - fprintf(fp2, "</b> (GM)"); - fprintf(fp2, "</td>\n"); - } - // displaying of the job - if (online_display_option & 6) { - char * jobname = job_name(char_dat[j].status.class_); - if ((online_display_option & 6) == 6) { - fprintf(fp2, " <td>%s %d/%d</td>\n", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level); - fprintf(fp, "%-18s %3d/%3d ", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level); - } else if (online_display_option & 2) { - fprintf(fp2, " <td>%s</td>\n", jobname); - fprintf(fp, "%-18s ", jobname); - } else if (online_display_option & 4) { - fprintf(fp2, " <td>%d/%d</td>\n", char_dat[j].status.base_level, char_dat[j].status.job_level); - fprintf(fp, "%3d/%3d ", char_dat[j].status.base_level, char_dat[j].status.job_level); - } - } - // displaying of the map - if (online_display_option & 24) { // 8 or 16 - // prepare map name - safestrncpy(temp, mapindex_id2name(char_dat[j].status.last_point.map), sizeof(temp)); - // write map name - if (online_display_option & 16) { // map-name AND coordinates - fprintf(fp2, " <td>%s (%d, %d)</td>\n", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y); - fprintf(fp, "%-12s (%3d,%3d) ", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y); - } else { - fprintf(fp2, " <td>%s</td>\n", temp); - fprintf(fp, "%-12s ", temp); - } - } - // displaying nimber of zenys - if (online_display_option & 32) { - // write number of zenys - if (char_dat[j].status.zeny == 0) { // if no zeny - fprintf(fp2, " <td ALIGN=RIGHT>no zeny</td>\n"); - fprintf(fp, " no zeny "); - } else { - fprintf(fp2, " <td ALIGN=RIGHT>%d z</td>\n", char_dat[j].status.zeny); - fprintf(fp, "%13d z ", char_dat[j].status.zeny); - } - } - fprintf(fp, "\n"); - fprintf(fp2, " </tr>\n"); - } - // If we display at least 1 player - if (players > 0) { - fprintf(fp2, " </table>\n"); - fprintf(fp, "\n"); - } + //Make the character leave the party [Skotlex] + if (party_id) + inter_party_leave(party_id, account_id, char_id); - // Displaying number of online players - if (players == 0) { - fprintf(fp2, " <p>No user is online.</p>\n"); - fprintf(fp, "No user is online.\n"); - } else if (players == 1) { - fprintf(fp2, " <p>%d user is online.</p>\n", players); - fprintf(fp, "%d user is online.\n", players); - } else { - fprintf(fp2, " <p>%d users are online.</p>\n", players); - fprintf(fp, "%d users are online.\n", players); - } - fprintf(fp2, " </BODY>\n"); - fprintf(fp2, "</HTML>\n"); - fclose(fp2); - } - fclose(fp); + /* delete char's pet */ + //Delete the hatched pet if you have one... + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incuvate` = '0'", pet_db, char_id) ) + Sql_ShowDebug(sql_handle); + + //Delete all pets that are stored in eggs (inventory + cart) + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = -256", pet_db, pet_db, inventory_db, inventory_db, char_id) ) + Sql_ShowDebug(sql_handle); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = -256", pet_db, pet_db, cart_db, cart_db, char_id) ) + Sql_ShowDebug(sql_handle); + + /* remove homunculus */ + if( hom_id ) + mapif_homunculus_delete(hom_id); + + /* remove mercenary data */ + mercenary_owner_delete(char_id); + + /* delete char's friends list */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", friend_db, char_id) ) + Sql_ShowDebug(sql_handle); + + /* delete char from other's friend list */ + //NOTE: Won't this cause problems for people who are already online? [Skotlex] + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", friend_db, char_id) ) + Sql_ShowDebug(sql_handle); + +#ifdef HOTKEY_SAVING + /* delete hotkeys */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", hotkey_db, char_id) ) + Sql_ShowDebug(sql_handle); +#endif + + /* delete inventory */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", inventory_db, char_id) ) + Sql_ShowDebug(sql_handle); + + /* delete cart inventory */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", cart_db, char_id) ) + Sql_ShowDebug(sql_handle); + + /* delete memo areas */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, char_id) ) + Sql_ShowDebug(sql_handle); + + /* delete character registry */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'", reg_db, char_id) ) + Sql_ShowDebug(sql_handle); + + /* delete skills */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, char_id) ) + Sql_ShowDebug(sql_handle); + +#ifdef ENABLE_SC_SAVING + /* status changes */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, account_id, char_id) ) + Sql_ShowDebug(sql_handle); +#endif + + if (log_char) { + 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')", + charlog_db, account_id, 0, char_id, esc_name) ) + Sql_ShowDebug(sql_handle); } - return; + /* delete character */ + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", char_db, char_id) ) + Sql_ShowDebug(sql_handle); + + /* No need as we used inter_guild_leave [Skotlex] + // Also delete info from guildtables. + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) ) + Sql_ShowDebug(sql_handle); + */ + + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", guild_db, char_id) ) + Sql_ShowDebug(sql_handle); + else if( Sql_NumRows(sql_handle) > 0 ) + mapif_parse_BreakGuild(0,guild_id); + else if( guild_id ) + inter_guild_leave(guild_id, account_id, char_id);// Leave your guild. + return 0; } //--------------------------------------------------------------------- @@ -1828,7 +1615,10 @@ int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p) WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed; WBUFW(buf,52) = p->class_; WBUFW(buf,54) = p->hair; - WBUFW(buf,56) = p->option&0x20 ? 0 : p->weapon; //When the weapon is sent and your option is riding, the client crashes on login!? + + //When the weapon is sent and your option is riding, the client crashes on login!? + WBUFW(buf,56) = p->option&(0x20|0x80000|0x100000|0x200000|0x400000|0x800000|0x1000000|0x2000000|0x4000000|0x8000000) ? 0 : p->weapon; + WBUFW(buf,58) = p->base_level; WBUFW(buf,60) = min(p->skill_point, INT16_MAX); WBUFW(buf,62) = p->head_bottom; @@ -1861,14 +1651,17 @@ int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p) WBUFL(buf,128) = p->robe; offset += 4; #endif -#if PACKETVER >= 20110928 - WBUFL(buf,132) = 0; // change slot feature (0 = disabled, otherwise enabled) - offset += 4; -#endif -#if PACKETVER >= 20111025 - WBUFL(buf,136) = 0; // unknown purpose (0 = disabled, otherwise displays "Add-Ons" sidebar) - offset += 4; +#if PACKETVER != 20111116 //2011-11-16 wants 136, ask gravity. + #if PACKETVER >= 20110928 + WBUFL(buf,132) = 0; // change slot feature (0 = disabled, otherwise enabled) + offset += 4; + #endif + #if PACKETVER >= 20111025 + WBUFL(buf,136) = 0; // unknown purpose (0 = disabled, otherwise displays "Add-Ons" sidebar) + offset += 4; + #endif #endif + return 106+offset; } @@ -1877,15 +1670,16 @@ int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p) //---------------------------------------- int mmo_char_send006b(int fd, struct char_session_data* sd) { - int i, j, found_num, offset = 0; + int j, offset = 0; #if PACKETVER >= 20100413 offset += 3; #endif - found_num = char_find_characters(sd); + if (save_log) + ShowInfo("Loading Char Data ("CL_BOLD"%d"CL_RESET")\n",sd->account_id); j = 24 + offset; // offset - WFIFOHEAD(fd,j + found_num*MAX_CHAR_BUF); + WFIFOHEAD(fd,j + MAX_CHARS*MAX_CHAR_BUF); WFIFOW(fd,0) = 0x6b; #if PACKETVER >= 20100413 WFIFOB(fd,4) = MAX_CHARS; // Max slots. @@ -1893,80 +1687,75 @@ int mmo_char_send006b(int fd, struct char_session_data* sd) WFIFOB(fd,6) = MAX_CHARS; // Premium slots. #endif memset(WFIFOP(fd,4 + offset), 0, 20); // unknown bytes - for(i = 0; i < found_num; i++) - j += mmo_char_tobuf(WFIFOP(fd,j), &char_dat[sd->found_char[i]].status); + j+=mmo_chars_fromsql(sd, WFIFOP(fd,j)); WFIFOW(fd,2) = j; // packet len WFIFOSET(fd,j); return 0; } -// 離婚(char削除時に使用) -int char_divorce(struct mmo_charstatus *cs) +int char_married(int pl1, int pl2) { - if (cs == NULL) - return 0; + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", char_db, pl1) ) + Sql_ShowDebug(sql_handle); + else if( SQL_SUCCESS == Sql_NextRow(sql_handle) ) + { + char* data; - if (cs->partner_id > 0){ - int i, j; - for(i = 0; i < char_num; i++) { - if (char_dat[i].status.char_id == cs->partner_id && char_dat[i].status.partner_id == cs->char_id) { - cs->partner_id = 0; - char_dat[i].status.partner_id = 0; - for(j = 0; j < MAX_INVENTORY; j++) - { - if (char_dat[i].status.inventory[j].nameid == WEDDING_RING_M || char_dat[i].status.inventory[j].nameid == WEDDING_RING_F) - memset(&char_dat[i].status.inventory[j], 0, sizeof(char_dat[i].status.inventory[0])); - if (cs->inventory[j].nameid == WEDDING_RING_M || cs->inventory[j].nameid == WEDDING_RING_F) - memset(&cs->inventory[j], 0, sizeof(cs->inventory[0])); - } - return 0; - } + Sql_GetData(sql_handle, 0, &data, NULL); + if( pl2 == atoi(data) ) + { + Sql_FreeResult(sql_handle); + return 1; } } + Sql_FreeResult(sql_handle); return 0; } -int char_married(int pl1, int pl2) -{ - return (char_dat[pl1].status.char_id == char_dat[pl2].status.partner_id && char_dat[pl2].status.char_id == char_dat[pl1].status.partner_id); -} - int char_child(int parent_id, int child_id) { - return (char_dat[parent_id].status.child == char_dat[child_id].status.char_id && - ((char_dat[parent_id].status.char_id == char_dat[child_id].status.father) || - (char_dat[parent_id].status.char_id == char_dat[child_id].status.mother))); + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", char_db, parent_id) ) + Sql_ShowDebug(sql_handle); + else if( SQL_SUCCESS == Sql_NextRow(sql_handle) ) + { + char* data; + + Sql_GetData(sql_handle, 0, &data, NULL); + if( child_id == atoi(data) ) + { + Sql_FreeResult(sql_handle); + return 1; + } + } + Sql_FreeResult(sql_handle); + return 0; } int char_family(int cid1, int cid2, int cid3) { - int i, idx1 = -1, idx2 =-1;//, idx3 =-1; - for(i = 0; i < char_num && (idx1 == -1 || idx2 == -1/* || idx3 == 1*/); i++) - { - if (char_dat[i].status.char_id == cid1) - idx1 = i; - if (char_dat[i].status.char_id == cid2) - idx2 = i; -// if (char_dat[i].status.char_id == cid2) -// idx3 = i; + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", char_db, cid1, cid2, cid3) ) + Sql_ShowDebug(sql_handle); + else while( SQL_SUCCESS == Sql_NextRow(sql_handle) ) + { + int charid; + int partnerid; + int childid; + char* data; + + Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data); + Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data); + Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data); + + if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) || + (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) || + (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) ) + { + Sql_FreeResult(sql_handle); + return childid; + } } - if (idx1 == -1 || idx2 == -1/* || idx3 == -1*/) - return 0; //Some character not found?? - - //Unless the dbs are corrupted, these 3 checks should suffice, even though - //we could do a lot more checks and force cross-reference integrity. - if(char_dat[idx1].status.partner_id == cid2 && - char_dat[idx1].status.child == cid3) - return cid3; //cid1/cid2 parents. cid3 child. - - if(char_dat[idx1].status.partner_id == cid3 && - char_dat[idx1].status.child == cid2) - return cid2; //cid1/cid3 parents. cid2 child. - - if(char_dat[idx2].status.partner_id == cid3 && - char_dat[idx2].status.child == cid1) - return cid1; //cid2/cid3 parents. cid1 child. + Sql_FreeResult(sql_handle); return 0; } @@ -1984,45 +1773,6 @@ void disconnect_player(int account_id) set_eof(i); } -// キャラ削除に伴うデータ削除 -static int char_delete(struct mmo_charstatus *cs) -{ - int j; - - // ペット削除 - if (cs->pet_id) - inter_pet_delete(cs->pet_id); - if (cs->hom_id) - inter_homun_delete(cs->hom_id); - for (j = 0; j < MAX_INVENTORY; j++) - if (cs->inventory[j].card[0] == (short)0xff00) - inter_pet_delete(MakeDWord(cs->inventory[j].card[1],cs->inventory[j].card[2])); - for (j = 0; j < MAX_CART; j++) - if (cs->cart[j].card[0] == (short)0xff00) - inter_pet_delete( MakeDWord(cs->cart[j].card[1],cs->cart[j].card[2]) ); - // ギルド脱退 - if (cs->guild_id) - inter_guild_leave(cs->guild_id, cs->account_id, cs->char_id); - // パーティー脱退 - if (cs->party_id) - inter_party_leave(cs->party_id, cs->account_id, cs->char_id); - // 離婚 - if (cs->partner_id){ - // 離婚情報をmapに通知 - unsigned char buf[10]; - WBUFW(buf,0) = 0x2b12; - WBUFL(buf,2) = cs->char_id; - WBUFL(buf,6) = cs->partner_id; - mapif_sendall(buf,10); - // 離婚 - char_divorce(cs); - } -#ifdef ENABLE_SC_SAVING - status_delete_scdata(cs->account_id, cs->char_id); -#endif - return 0; -} - static void char_auth_ok(int fd, struct char_session_data *sd) { struct online_char_data* character; @@ -2085,6 +1835,7 @@ void loginif_reset(void) /// Checks the conditions for the server to stop. +/// Releases the cookie when all characters are saved. /// If all the conditions are met, it stops the core loop. void loginif_check_shutdown(void) { @@ -2107,7 +1858,7 @@ void loginif_on_ready(void) int i; loginif_check_shutdown(); - + //Send online accounts to login server. send_accounts_tologin(INVALID_TIMER, gettick(), 0, 0); @@ -2122,7 +1873,7 @@ int parse_fromlogin(int fd) { struct char_session_data* sd = NULL; int i; - + // only process data from the login-server if( fd != login_fd ) { @@ -2157,7 +1908,7 @@ int parse_fromlogin(int fd) //printf("connect login server error : %d\n", RFIFOB(fd,2)); ShowError("Can not connect to login-server.\n"); ShowError("The server communication passwords (default s1/p1) are probably invalid.\n"); - ShowError("Also, please make sure your accounts file (default: accounts.txt) has the correct communication username/passwords and the gender of the account is S.\n"); + ShowError("Also, please make sure your login db has the correct communication username/passwords and the gender of the account is S.\n"); ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n"); set_eof(fd); return 0; @@ -2213,22 +1964,21 @@ int parse_fromlogin(int fd) ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->auth && sd->account_id == RFIFOL(fd,2) ); if( i < fd_max ) { + int server_id; memcpy(sd->email, RFIFOP(fd,6), 40); sd->expiration_time = (time_t)RFIFOL(fd,46); sd->gmlevel = RFIFOB(fd,50); safestrncpy(sd->birthdate, (const char*)RFIFOP(fd,51), sizeof(sd->birthdate)); - + ARR_FIND( 0, ARRAYLENGTH(server), server_id, server[server_id].fd > 0 && server[server_id].map[0] ); // continued from char_auth_ok... - if( max_connect_user && count_users() >= max_connect_user && sd->gmlevel < gm_allow_level ) - { + if( server_id == ARRAYLENGTH(server) || //server not online, bugreport:2359 + ( max_connect_user && count_users() >= max_connect_user && sd->gmlevel < gm_allow_level ) ) { // refuse connection (over populated) WFIFOHEAD(i,3); WFIFOW(i,0) = 0x6c; WFIFOW(i,2) = 0; WFIFOSET(i,3); - } - else - { + } else { // send characters to player mmo_char_send006b(i, sd); #if PACKETVER >= 20110309 @@ -2258,7 +2008,6 @@ int parse_fromlogin(int fd) if (RFIFOREST(fd) < 7) return 0; { - int j; unsigned char buf[7]; int acc = RFIFOL(fd,2); @@ -2267,57 +2016,65 @@ int parse_fromlogin(int fd) if( acc > 0 ) {// TODO: Is this even possible? + int char_id[MAX_CHARS]; + int class_[MAX_CHARS]; + int guild_id[MAX_CHARS]; + int num; + char* data; + struct auth_node* node = (struct auth_node*)idb_get(auth_db, acc); if( node != NULL ) node->sex = sex; - ARR_FIND( 0, char_num, i, char_dat[i].status.account_id == acc ); - if( i < char_num ) + // get characters + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`class`,`guild_id` FROM `%s` WHERE `account_id` = '%d'", char_db, acc) ) + Sql_ShowDebug(sql_handle); + for( i = 0; i < MAX_CHARS && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i ) + { + Sql_GetData(sql_handle, 0, &data, NULL); char_id[i] = atoi(data); + Sql_GetData(sql_handle, 1, &data, NULL); class_[i] = atoi(data); + Sql_GetData(sql_handle, 2, &data, NULL); guild_id[i] = atoi(data); + } + num = i; + for( i = 0; i < num; ++i ) { - int jobclass = char_dat[i].status.class_; - char_dat[i].status.sex = sex; - if (jobclass == JOB_BARD || jobclass == JOB_DANCER || - jobclass == JOB_CLOWN || jobclass == JOB_GYPSY || - jobclass == JOB_BABY_BARD || jobclass == JOB_BABY_DANCER) { + if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER || + class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY || + class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER || + class_[i] == JOB_WANDERER || class_[i] == JOB_WANDERER_T || + class_[i] == JOB_MINSTREL || class_[i] == JOB_MINSTREL_T ) + { // job modification - if (jobclass == JOB_BARD || jobclass == JOB_DANCER) { - char_dat[i].status.class_ = (sex) ? JOB_BARD : JOB_DANCER; - } else if (jobclass == JOB_CLOWN || jobclass == JOB_GYPSY) { - char_dat[i].status.class_ = (sex) ? JOB_CLOWN : JOB_GYPSY; - } else if (jobclass == JOB_BABY_BARD || jobclass == JOB_BABY_DANCER) { - char_dat[i].status.class_ = (sex) ? JOB_BABY_BARD : JOB_BABY_DANCER; - } - // remove specifical skills of classes 19, 4020 and 4042 - for(j = 315; j <= 322; j++) { - if (char_dat[i].status.skill[j].id > 0 && char_dat[i].status.skill[j].flag == SKILL_FLAG_PERMANENT) { - char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv; - char_dat[i].status.skill[j].id = 0; - char_dat[i].status.skill[j].lv = 0; - } - } - // remove specifical skills of classes 20, 4021 and 4043 - for(j = 323; j <= 330; j++) { - if (char_dat[i].status.skill[j].id > 0 && char_dat[i].status.skill[j].flag == SKILL_FLAG_PERMANENT) { - char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv; - char_dat[i].status.skill[j].id = 0; - char_dat[i].status.skill[j].lv = 0; - } - } + if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER ) + class_[i] = (sex ? JOB_BARD : JOB_DANCER); + else if( class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY ) + class_[i] = (sex ? JOB_CLOWN : JOB_GYPSY); + else if( class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER ) + class_[i] = (sex ? JOB_BABY_BARD : JOB_BABY_DANCER); + else if( class_[i] == JOB_MINSTREL || class_[i] == JOB_WANDERER ) + class_[i] = (sex ? JOB_MINSTREL : JOB_WANDERER); + else if( class_[i] == JOB_MINSTREL_T || class_[i] == JOB_WANDERER_T ) + class_[i] = (sex ? JOB_MINSTREL_T : JOB_WANDERER_T); + // remove specifical skills of classes 19,20 4020,4021 and 4042,4043 + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `skill_point` = `skill_point` +" + " (SELECT SUM(lv) FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330' AND `lv` > '0')" + " WHERE `char_id` = '%d'", + char_db, skill_db, char_id[i], char_id[i]) ) + Sql_ShowDebug(sql_handle); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'", skill_db, char_id[i]) ) + Sql_ShowDebug(sql_handle); } // to avoid any problem with equipment and invalid sex, equipment is unequiped. - for (j = 0; j < MAX_INVENTORY; j++) { - if (char_dat[i].status.inventory[j].nameid && char_dat[i].status.inventory[j].equip) - char_dat[i].status.inventory[j].equip = 0; - } - char_dat[i].status.weapon = 0; - char_dat[i].status.shield = 0; - char_dat[i].status.head_top = 0; - char_dat[i].status.head_mid = 0; - char_dat[i].status.head_bottom = 0; - - if (char_dat[i].status.guild_id) //If there is a guild, update the guild_member data [Skotlex] - inter_guild_sex_changed(char_dat[i].status.guild_id, acc, char_dat[i].status.char_id, sex); + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `equip` = '0' WHERE `char_id` = '%d'", inventory_db, char_id[i]) ) + Sql_ShowDebug(sql_handle); + if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d', `weapon`='0', `shield`='0', `head_top`='0', `head_mid`='0', `head_bottom`='0' WHERE `char_id`='%d'", char_db, class_[i], char_id[i]) ) + Sql_ShowDebug(sql_handle); + + if( guild_id[i] )// If there is a guild, update the guild_member data [Skotlex] + inter_guild_sex_changed(guild_id[i], acc, char_id[i], sex); } + Sql_FreeResult(sql_handle); + // disconnect player if online on char-server disconnect_player(acc); } @@ -2495,124 +2252,62 @@ int save_accreg2(unsigned char* buf, int len) return 0; } -//Receive Registry information for a character. -int char_parse_Registry(int account_id, int char_id, unsigned char *buf, int buf_len) -{ - int i,j,p,len; - for (i = 0; i < char_num; i++) { - if (char_dat[i].status.account_id == account_id && char_dat[i].status.char_id == char_id) - break; - } - if(i >= char_num) //Character not found? - return 1; - for(j=0,p=0;j<GLOBAL_REG_NUM && p<buf_len;j++){ - sscanf((char*)WBUFP(buf,p), "%31c%n",char_dat[i].global[j].str,&len); - char_dat[i].global[j].str[len]='\0'; - p +=len+1; //+1 to skip the '\0' between strings. - sscanf((char*)WBUFP(buf,p), "%255c%n",char_dat[i].global[j].value,&len); - char_dat[i].global[j].value[len]='\0'; - p +=len+1; - } - char_dat[i].global_num = j; - return 0; -} - -//Reply to map server with acc reg values. -int char_account_reg_reply(int fd,int account_id,int char_id) -{ - int i,j,p; - WFIFOHEAD(fd, GLOBAL_REG_NUM*288 + 13); - WFIFOW(fd,0)=0x3804; - WFIFOL(fd,4)=account_id; - WFIFOL(fd,8)=char_id; - WFIFOB(fd,12)=3; //Type 3: char acc reg. - for (i = 0;i < char_num; i++) { - if (char_dat[i].status.account_id == account_id && char_dat[i].status.char_id == char_id) - break; - } - if(i >= char_num){ //Character not found? Sent empty packet. - WFIFOW(fd,2)=13; - }else{ - for (p=13,j = 0; j < char_dat[i].global_num; j++) { - if (char_dat[i].global[j].str[0]) { - p+= sprintf((char*)WFIFOP(fd,p), "%s", char_dat[i].global[j].str)+1; //We add 1 to consider the '\0' in place. - p+= sprintf((char*)WFIFOP(fd,p), "%s", char_dat[i].global[j].value)+1; - } - } - WFIFOW(fd,2)=p; - } - WFIFOSET(fd,WFIFOW(fd,2)); - return 0; -} - void char_read_fame_list(void) { - int i, j, k; - struct fame_list fame_item; - CREATE_BUFFER(id, int, char_num); - - for(i = 0; i < char_num; i++) { - id[i] = i; - for(j = 0; j < i; j++) { - if (char_dat[i].status.fame > char_dat[id[j]].status.fame) { - for(k = i; k > j; k--) - id[k] = id[k-1]; - id[j] = i; // id[i] - break; - } - } - } + int i; + char* data; + size_t len; // Empty ranking lists memset(smith_fame_list, 0, sizeof(smith_fame_list)); memset(chemist_fame_list, 0, sizeof(chemist_fame_list)); memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list)); // Build Blacksmith ranking list - for (i = 0, j = 0; i < char_num && j < fame_list_size_smith; i++) { - if (char_dat[id[i]].status.fame && ( - char_dat[id[i]].status.class_ == JOB_BLACKSMITH || - char_dat[id[i]].status.class_ == JOB_WHITESMITH || - char_dat[id[i]].status.class_ == JOB_BABY_BLACKSMITH)) - { - fame_item.id = char_dat[id[i]].status.char_id; - fame_item.fame = char_dat[id[i]].status.fame; - safestrncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH); - - memcpy(&smith_fame_list[j],&fame_item,sizeof(struct fame_list)); - j++; - } + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, fame_list_size_smith) ) + Sql_ShowDebug(sql_handle); + for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i ) + { + // char_id + Sql_GetData(sql_handle, 0, &data, NULL); + smith_fame_list[i].id = atoi(data); + // fame + Sql_GetData(sql_handle, 1, &data, &len); + smith_fame_list[i].fame = atoi(data); + // name + Sql_GetData(sql_handle, 2, &data, &len); + memcpy(smith_fame_list[i].name, data, min(len, NAME_LENGTH)); } // Build Alchemist ranking list - for (i = 0, j = 0; i < char_num && j < fame_list_size_chemist; i++) { - if (char_dat[id[i]].status.fame && ( - char_dat[id[i]].status.class_ == JOB_ALCHEMIST || - char_dat[id[i]].status.class_ == JOB_CREATOR || - char_dat[id[i]].status.class_ == JOB_BABY_ALCHEMIST)) - { - fame_item.id = char_dat[id[i]].status.char_id; - fame_item.fame = char_dat[id[i]].status.fame; - safestrncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH); - - memcpy(&chemist_fame_list[j],&fame_item,sizeof(struct fame_list)); - - j++; - } + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, fame_list_size_chemist) ) + Sql_ShowDebug(sql_handle); + for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i ) + { + // char_id + Sql_GetData(sql_handle, 0, &data, NULL); + chemist_fame_list[i].id = atoi(data); + // fame + Sql_GetData(sql_handle, 1, &data, &len); + chemist_fame_list[i].fame = atoi(data); + // name + Sql_GetData(sql_handle, 2, &data, &len); + memcpy(chemist_fame_list[i].name, data, min(len, NAME_LENGTH)); } // Build Taekwon ranking list - for (i = 0, j = 0; i < char_num && j < fame_list_size_taekwon; i++) { - if (char_dat[id[i]].status.fame && - char_dat[id[i]].status.class_ == JOB_TAEKWON) - { - fame_item.id = char_dat[id[i]].status.char_id; - fame_item.fame = char_dat[id[i]].status.fame; - safestrncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH); - - memcpy(&taekwon_fame_list[j],&fame_item,sizeof(struct fame_list)); - - j++; - } + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_TAEKWON, fame_list_size_taekwon) ) + Sql_ShowDebug(sql_handle); + for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i ) + { + // char_id + Sql_GetData(sql_handle, 0, &data, NULL); + taekwon_fame_list[i].id = atoi(data); + // fame + Sql_GetData(sql_handle, 1, &data, &len); + taekwon_fame_list[i].fame = atoi(data); + // name + Sql_GetData(sql_handle, 2, &data, &len); + memcpy(taekwon_fame_list[i].name, data, min(len, NAME_LENGTH)); } - DELETE_BUFFER(id); + Sql_FreeResult(sql_handle); } // Send map-servers the fame ranking lists @@ -2666,12 +2361,15 @@ void char_update_fame_list(int type, int index, int fame) //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name) int char_loadName(int char_id, char* name) { - int j; + char* data; + size_t len; - ARR_FIND( 0, char_num, j, char_dat[j].status.char_id == char_id ); - if( j < char_num ) + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) ) + Sql_ShowDebug(sql_handle); + else if( SQL_SUCCESS == Sql_NextRow(sql_handle) ) { - safestrncpy(name, char_dat[j].status.name, NAME_LENGTH); + Sql_GetData(sql_handle, 0, &data, &len); + safestrncpy(name, data, NAME_LENGTH); return 1; } else @@ -2721,8 +2419,9 @@ void mapif_server_reset(int id) WBUFW(buf,2) = j * 4 + 10; mapif_sendallwos(fd, buf, WBUFW(buf,2)); } + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server[id].fd) ) + Sql_ShowDebug(sql_handle); online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server. - create_online_files(); mapif_server_destroy(id); mapif_server_init(id); } @@ -2775,9 +2474,7 @@ int parse_frommap(int fd) ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n", id, j, CONVIP(server[id].ip), server[id].port); ShowStatus("Map-server %d loading complete.\n", id); - char_log("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d. Map-server %d loading complete.\n", - id, j, CONVIP(server[id].ip), server[id].port, id); - + // send name for wisp to player WFIFOHEAD(fd, 3 + NAME_LENGTH); WFIFOW(fd,0) = 0x2afb; @@ -2792,7 +2489,6 @@ int parse_frommap(int fd) int x; if (j == 0) { ShowWarning("Map-server %d has NO maps.\n", id); - char_log("WARNING: Map-server %d has NO maps.\n", id); } else { // Transmitting maps information to the other map-servers WBUFW(buf,0) = 0x2b04; @@ -2829,23 +2525,48 @@ int parse_frommap(int fd) { #ifdef ENABLE_SC_SAVING int aid, cid; - struct scdata *data; aid = RFIFOL(fd,2); cid = RFIFOL(fd,6); - data = status_search_scdata(aid, cid); - if (data->count > 0) - { //Deliver status change data. - WFIFOHEAD(fd,14 + data->count*sizeof(struct status_change_data)); + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", + scdata_db, aid, cid) ) + { + Sql_ShowDebug(sql_handle); + break; + } + if( Sql_NumRows(sql_handle) > 0 ) + { + struct status_change_data scdata; + int count; + char* data; + + WFIFOHEAD(fd,14+50*sizeof(struct status_change_data)); WFIFOW(fd,0) = 0x2b1d; - WFIFOW(fd,2) = 14 + data->count*sizeof(struct status_change_data); WFIFOL(fd,4) = aid; WFIFOL(fd,8) = cid; - WFIFOW(fd,12) = data->count; - for (i = 0; i < data->count; i++) - memcpy(WFIFOP(fd,14+i*sizeof(struct status_change_data)), &data->data[i], sizeof(struct status_change_data)); - WFIFOSET(fd, WFIFOW(fd,2)); - status_delete_scdata(aid, cid); //Data sent, so it needs be discarded now. + for( count = 0; count < 50 && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count ) + { + Sql_GetData(sql_handle, 0, &data, NULL); scdata.type = atoi(data); + Sql_GetData(sql_handle, 1, &data, NULL); scdata.tick = atoi(data); + Sql_GetData(sql_handle, 2, &data, NULL); scdata.val1 = atoi(data); + Sql_GetData(sql_handle, 3, &data, NULL); scdata.val2 = atoi(data); + Sql_GetData(sql_handle, 4, &data, NULL); scdata.val3 = atoi(data); + Sql_GetData(sql_handle, 5, &data, NULL); scdata.val4 = atoi(data); + memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &scdata, sizeof(struct status_change_data)); + } + if (count >= 50) + ShowWarning("Too many status changes for %d:%d, some of them were not loaded.\n", aid, cid); + if (count > 0) + { + WFIFOW(fd,2) = 14 + count*sizeof(struct status_change_data); + WFIFOW(fd,12) = count; + WFIFOSET(fd,WFIFOW(fd,2)); + + //Clear the data once loaded. + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, aid, cid) ) + Sql_ShowDebug(sql_handle); + } } + Sql_FreeResult(sql_handle); #endif RFIFOSKIP(fd, 10); } @@ -2894,7 +2615,7 @@ int parse_frommap(int fd) return 0; { int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8), size = RFIFOW(fd,2); - struct mmo_charstatus* cs; + struct online_char_data* character; if (size - 13 != sizeof(struct mmo_charstatus)) { @@ -2902,10 +2623,17 @@ int parse_frommap(int fd) RFIFOSKIP(fd,size); break; } - if( ( cs = search_character(aid, cid) ) != NULL ) + //Check account only if this ain't final save. Final-save goes through because of the char-map reconnect + if (RFIFOB(fd,12) || ( + (character = (struct online_char_data*)idb_get(online_char_db, aid)) != NULL && + character->char_id == cid)) { - memcpy(cs, RFIFOP(fd,13), sizeof(struct mmo_charstatus)); - storage_save(cs->account_id, &cs->storage); + struct mmo_charstatus char_dat; + memcpy(&char_dat, RFIFOP(fd,13), sizeof(struct mmo_charstatus)); + mmo_char_tosql(cid, &char_dat); + } else { //This may be valid on char-server reconnection, when re-sending characters that already logged off. + ShowError("parse_from_map (save-char): Received data for non-existant/offline character (%d:%d).\n", aid, cid); + set_char_online(id, cid, aid); } if (RFIFOB(fd,12)) @@ -2932,7 +2660,7 @@ int parse_frommap(int fd) uint32 login_id2 = RFIFOL(fd,10); uint32 ip = RFIFOL(fd,14); RFIFOSKIP(fd,18); - + if( runflag != CHARSERVER_ST_RUNNING ) { WFIFOHEAD(fd,7); @@ -2968,19 +2696,25 @@ int parse_frommap(int fd) break; case 0x2b05: // request "change map server" - if (RFIFOREST(fd) < 35) + if (RFIFOREST(fd) < 39) return 0; { int map_id, map_fd = -1; struct online_char_data* data; struct mmo_charstatus* char_data; + struct mmo_charstatus char_dat; map_id = search_mapserver(RFIFOW(fd,18), ntohl(RFIFOL(fd,24)), ntohs(RFIFOW(fd,28))); //Locate mapserver by ip and port. if (map_id >= 0) map_fd = server[map_id].fd; - - char_data = search_character(RFIFOL(fd,2), RFIFOL(fd,14)); - + //Char should just had been saved before this packet, so this should be safe. [Skotlex] + char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14)); + if (char_data == NULL) + { //Really shouldn't happen. + mmo_char_fromsql(RFIFOL(fd,14), &char_dat, true); + char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14)); + } + if( runflag == CHARSERVER_ST_RUNNING && session_isActive(map_fd) && char_data ) @@ -3000,8 +2734,10 @@ int parse_frommap(int fd) node->login_id1 = RFIFOL(fd,6); node->login_id2 = RFIFOL(fd,10); node->sex = RFIFOB(fd,30); - node->expiration_time = 0; // FIXME + node->expiration_time = 0; // FIXME (this thing isn't really supported we could as well purge it instead of fixing) node->ip = ntohl(RFIFOL(fd,31)); + node->gmlevel = RFIFOL(fd,35); + node->changing_mapservers = 1; idb_put(auth_db, RFIFOL(fd,2), node); data = (struct online_char_data*)idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data); @@ -3024,6 +2760,22 @@ int parse_frommap(int fd) } break; + case 0x2b07: // Remove RFIFOL(fd,6) (friend_id) from RFIFOL(fd,2) (char_id) friend list [Ind] + if (RFIFOREST(fd) < 10) + return 0; + { + int char_id, friend_id; + char_id = RFIFOL(fd,2); + friend_id = RFIFOL(fd,6); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `friend_id`='%d' LIMIT 1", + friend_db, char_id, friend_id) ) { + Sql_ShowDebug(sql_handle); + break; + } + RFIFOSKIP(fd,10); + } + break; + case 0x2b08: // char name request if (RFIFOREST(fd) < 6) return 0; @@ -3054,7 +2806,7 @@ int parse_frommap(int fd) return 0; { int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline - char character_name[NAME_LENGTH]; + char esc_name[NAME_LENGTH*2+1]; int acc = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request) const char* name = (char*)RFIFOP(fd,6); // name of the target character @@ -3067,19 +2819,26 @@ int parse_frommap(int fd) short second = RFIFOW(fd,42); RFIFOSKIP(fd,44); - safestrncpy(character_name, name, NAME_LENGTH); - i = search_character_index(character_name); - if( i < 0 ) + Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH)); + if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'", char_db, esc_name) ) + Sql_ShowDebug(sql_handle); + else + if( Sql_NumRows(sql_handle) == 0 ) { result = 1; // 1-player not found } else + if( SQL_SUCCESS != Sql_NextRow(sql_handle) ) + Sql_ShowDebug(sql_handle); + //FIXME: set proper result value? + else { char name[NAME_LENGTH]; int account_id; + char* data; - account_id = char_dat[i].status.account_id; - safestrncpy(name, char_dat[i].status.name, NAME_LENGTH); + Sql_GetData(sql_handle, 0, &data, NULL); account_id = atoi(data); + Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(name, data, sizeof(name)); if( login_fd <= 0 ) result = 3; // 3-login-server offline @@ -3130,6 +2889,8 @@ int parse_frommap(int fd) } } + Sql_FreeResult(sql_handle); + // send answer if a player ask, not if the server ask if( acc != -1 && type != 5) { // Don't send answer for changesex WFIFOHEAD(fd,34); @@ -3196,11 +2957,28 @@ int parse_frommap(int fd) } break; + // Divorce chars + case 0x2b11: + if( RFIFOREST(fd) < 10 ) + return 0; + + divorce_char_sql(RFIFOL(fd,2), RFIFOL(fd,6)); + RFIFOSKIP(fd,10); + break; + case 0x2b16: // Receive rates [Wizputer] if( RFIFOREST(fd) < 14 ) return 0; - // Txt doesn't need this packet, so just skip it + { + char esc_server_name[sizeof(server_name)*2+1]; + + Sql_EscapeString(sql_handle, esc_server_name, server_name); + + if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `ragsrvinfo` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d'", + fd, esc_server_name, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)) ) + Sql_ShowDebug(sql_handle); RFIFOSKIP(fd,14); + } break; case 0x2b17: // Character disconnected set online 0 [Wizputer] @@ -3236,20 +3014,31 @@ int parse_frommap(int fd) { #ifdef ENABLE_SC_SAVING int count, aid, cid; - struct scdata *data; aid = RFIFOL(fd, 4); cid = RFIFOL(fd, 8); count = RFIFOW(fd, 12); - data = status_search_scdata(aid, cid); - if (data->count != count) + if( count > 0 ) { - data->count = count; - data->data = (struct status_change_data*)aRealloc(data->data, count*sizeof(struct status_change_data)); + struct status_change_data data; + StringBuf buf; + int i; + + StringBuf_Init(&buf); + StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", scdata_db); + for( i = 0; i < count; ++i ) + { + memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data)); + if( i > 0 ) + StringBuf_AppendStr(&buf, ", "); + StringBuf_Printf(&buf, "('%d','%d','%hu','%d','%d','%d','%d','%d')", aid, cid, + data.type, data.tick, data.val1, data.val2, data.val3, data.val4); + } + if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) + Sql_ShowDebug(sql_handle); + StringBuf_Destroy(&buf); } - for (i = 0; i < count; i++) - memcpy (&data->data[i], RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data)); #endif RFIFOSKIP(fd, RFIFOW(fd, 2)); } @@ -3274,6 +3063,7 @@ int parse_frommap(int fd) uint32 ip; struct auth_node* node; struct mmo_charstatus* cd; + struct mmo_charstatus char_dat; account_id = RFIFOL(fd,2); char_id = RFIFOL(fd,6); @@ -3283,10 +3073,15 @@ int parse_frommap(int fd) RFIFOSKIP(fd,19); node = (struct auth_node*)idb_get(auth_db, account_id); - cd = search_character(account_id, char_id); + cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id); + if( cd == NULL ) + { //Really shouldn't happen. + mmo_char_fromsql(char_id, &char_dat, true); + cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id); + } if( runflag == CHARSERVER_ST_RUNNING && cd != NULL && - node != NULL && + node != NULL && node->account_id == account_id && node->char_id == char_id && node->login_id1 == login_id1 && @@ -3295,16 +3090,16 @@ int parse_frommap(int fd) {// auth ok cd->sex = sex; - WFIFOHEAD(fd,24 + sizeof(struct mmo_charstatus)); + WFIFOHEAD(fd,25 + sizeof(struct mmo_charstatus)); WFIFOW(fd,0) = 0x2afd; - WFIFOW(fd,2) = 24 + sizeof(struct mmo_charstatus); + WFIFOW(fd,2) = 25 + sizeof(struct mmo_charstatus); WFIFOL(fd,4) = account_id; WFIFOL(fd,8) = node->login_id1; WFIFOL(fd,12) = node->login_id2; WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT" WFIFOL(fd,20) = node->gmlevel; - storage_load(cd->account_id, &cd->storage); //FIXME: storage is used as a temp buffer here - memcpy(WFIFOP(fd,24), cd, sizeof(struct mmo_charstatus)); + WFIFOB(fd,24) = node->changing_mapservers; + memcpy(WFIFOP(fd,25), cd, sizeof(struct mmo_charstatus)); WFIFOSET(fd, WFIFOW(fd,2)); // only use the auth once and mark user online @@ -3460,18 +3255,31 @@ void char_delete2_cancel_ack(int fd, int char_id, uint32 result) static void char_delete2_req(int fd, struct char_session_data* sd) {// CH: <0827>.W <char id>.L - int char_id; - struct mmo_charstatus* cs; + int char_id, i, guild_id, party_id; + char* data; + time_t delete_date; char_id = RFIFOL(fd,2); - if( ( cs = search_session_character(sd, char_id) ) == NULL ) + ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id ); + if( i == MAX_CHARS ) {// character not found char_delete2_ack(fd, char_id, 3, 0); return; } - if( cs->delete_date ) + if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `guild_id`,`party_id`,`delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) ) + { + Sql_ShowDebug(sql_handle); + char_delete2_ack(fd, char_id, 3, 0); + return; + } + + Sql_GetData(sql_handle, 0, &data, NULL); guild_id = atoi(data); + Sql_GetData(sql_handle, 1, &data, NULL); party_id = atoi(data); + Sql_GetData(sql_handle, 2, &data, NULL); delete_date = strtoul(data, NULL, 10); + + if( delete_date ) {// character already queued for deletion char_delete2_ack(fd, char_id, 0, 0); return; @@ -3481,13 +3289,13 @@ static void char_delete2_req(int fd, struct char_session_data* sd) // Aegis imposes these checks probably to avoid dead member // entries in guilds/parties, otherwise they are not required. // TODO: Figure out how these are enforced during waiting. - if( cs->guild_id ) + if( guild_id ) {// character in guild char_delete2_ack(fd, char_id, 4, 0); return; } - if( cs->party_id ) + if( party_id ) {// character in party char_delete2_ack(fd, char_id, 5, 0); return; @@ -3495,17 +3303,26 @@ static void char_delete2_req(int fd, struct char_session_data* sd) */ // success - cs->delete_date = time(NULL)+char_del_delay; + delete_date = time(NULL)+char_del_delay; - char_delete2_ack(fd, char_id, 1, cs->delete_date); + if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='%lu' WHERE `char_id`='%d'", char_db, (unsigned long)delete_date, char_id) ) + { + Sql_ShowDebug(sql_handle); + char_delete2_ack(fd, char_id, 3, 0); + return; + } + + char_delete2_ack(fd, char_id, 1, delete_date); } static void char_delete2_accept(int fd, struct char_session_data* sd) {// CH: <0829>.W <char id>.L <birth date:YYMMDD>.6B char birthdate[8+1]; - int char_id, i; - struct mmo_charstatus* cs; + int char_id, i, k; + unsigned int base_level; + char* data; + time_t delete_date; char_id = RFIFOL(fd,2); @@ -3522,15 +3339,24 @@ static void char_delete2_accept(int fd, struct char_session_data* sd) birthdate[7] = RFIFOB(fd,11); birthdate[8] = 0; - ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == char_id ); + ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id ); if( i == MAX_CHARS ) {// character not found char_delete2_accept_ack(fd, char_id, 3); return; } - cs = &char_dat[sd->found_char[i]].status; - if( !cs->delete_date || cs->delete_date>time(NULL) ) + if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `base_level`,`delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) ) + {// data error + Sql_ShowDebug(sql_handle); + char_delete2_accept_ack(fd, char_id, 3); + return; + } + + Sql_GetData(sql_handle, 0, &data, NULL); base_level = (unsigned int)strtoul(data, NULL, 10); + Sql_GetData(sql_handle, 1, &data, NULL); delete_date = strtoul(data, NULL, 10); + + if( !delete_date || delete_date>time(NULL) ) {// not queued or delay not yet passed char_delete2_accept_ack(fd, char_id, 4); return; @@ -3542,49 +3368,25 @@ static void char_delete2_accept(int fd, struct char_session_data* sd) return; } - if( ( char_del_level > 0 && cs->base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && cs->base_level <= (unsigned int)(-char_del_level) ) ) + if( ( char_del_level > 0 && base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && base_level <= (unsigned int)(-char_del_level) ) ) {// character level config restriction char_delete2_accept_ack(fd, char_id, 2); return; } // success - char_delete(cs); - - // drop character entry - if( --char_num > 0 && sd->found_char[i] != char_num ) + if( delete_char_sql(char_id) < 0 ) { - int s, c; - - // move the last entry to the place of the deleted character - memcpy(&char_dat[sd->found_char[i]], &char_dat[char_num], sizeof(struct mmo_charstatus)); - - // scan currently online accounts, if the moved character - // entry requires an update of the cached character list - for( s = 0; s < fd_max; s++ ) - { - struct char_session_data* osd; - - if( session[s] && ( osd = (struct char_session_data*)session[s]->session_data ) != NULL && osd->account_id == char_dat[char_num].status.account_id ) - { - for( c = 0; c < MAX_CHARS; c++ ) - { - if( osd->found_char[c] == char_num ) - { - osd->found_char[c] = sd->found_char[i]; - break; - } - } - break; - } - } - - // wipe the last entry - memset(&char_dat[char_num], 0, sizeof(struct mmo_charstatus)); + char_delete2_accept_ack(fd, char_id, 3); + return; } // refresh character list cache - char_find_characters(sd); + for(k = i; k < MAX_CHARS-1; k++) + { + sd->found_char[k] = sd->found_char[k+1]; + } + sd->found_char[MAX_CHARS-1] = -1; char_delete2_accept_ack(fd, char_id, 1); } @@ -3592,12 +3394,12 @@ static void char_delete2_accept(int fd, struct char_session_data* sd) static void char_delete2_cancel(int fd, struct char_session_data* sd) {// CH: <082b>.W <char id>.L - int char_id; - struct mmo_charstatus* cs; + int char_id, i; char_id = RFIFOL(fd,2); - if( ( cs = search_session_character(sd, char_id) ) == NULL ) + ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id ); + if( i == MAX_CHARS ) {// character not found char_delete2_cancel_ack(fd, char_id, 2); return; @@ -3606,7 +3408,12 @@ static void char_delete2_cancel(int fd, struct char_session_data* sd) // there is no need to check, whether or not the character was // queued for deletion, as the client prints an error message by // itself, if it was not the case (@see char_delete2_cancel_ack) - cs->delete_date = 0; + if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='0' WHERE `char_id`='%d'", char_db, char_id) ) + { + Sql_ShowDebug(sql_handle); + char_delete2_cancel_ack(fd, char_id, 2); + return; + } char_delete2_cancel_ack(fd, char_id, 1); } @@ -3732,37 +3539,46 @@ int parse_char(int fd) case 0x66: FIFOSD_CHECK(3); { + struct mmo_charstatus char_dat; struct mmo_charstatus *cd; + char* data; + int char_id; uint32 subnet_map_ip; struct auth_node* node; int slot = RFIFOB(fd,2); RFIFOSKIP(fd,3); - // if we activated email creation and email is default email - if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online - WFIFOHEAD(fd,3); - WFIFOW(fd,0) = 0x70; - WFIFOB(fd,2) = 0; // 00 = Incorrect Email address - WFIFOSET(fd,3); - break; - } - // otherwise, load the character - ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] >= 0 && char_dat[sd->found_char[ch]].status.slot == slot ); - if (ch == MAX_CHARS) + if ( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'", char_db, sd->account_id, slot) + || SQL_SUCCESS != Sql_NextRow(sql_handle) + || SQL_SUCCESS != Sql_GetData(sql_handle, 0, &data, NULL) ) { //Not found?? May be forged packet. + Sql_ShowDebug(sql_handle); + Sql_FreeResult(sql_handle); WFIFOHEAD(fd,3); WFIFOW(fd,0) = 0x6c; WFIFOB(fd,2) = 0; // rejected from server WFIFOSET(fd,3); break; } - cd = &char_dat[sd->found_char[ch]].status; - char_log("Character Selected, Account ID: %d, Character Slot: %d, Character Name: %s.\n", sd->account_id, slot, cd->name); + char_id = atoi(data); + Sql_FreeResult(sql_handle); + mmo_char_fromsql(char_id, &char_dat, true); + + //Have to switch over to the DB instance otherwise data won't propagate [Kevin] + cd = (struct mmo_charstatus *)idb_get(char_db_, char_id); cd->sex = sd->sex; - ShowInfo("Selected char: (Account %d: %d - %s)\n", sd->account_id, slot, cd->name); + if (log_char) { + char esc_name[NAME_LENGTH*2+1]; + + Sql_EscapeStringLen(sql_handle, esc_name, char_dat.name, strnlen(char_dat.name, NAME_LENGTH)); + if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')", + charlog_db, sd->account_id, slot, esc_name) ) + Sql_ShowDebug(sql_handle); + } + ShowInfo("Selected char: (Account %d: %d - %s)\n", sd->account_id, slot, char_dat.name); // searching map server i = search_mapserver(cd->last_point.map, -1, -1); @@ -3846,6 +3662,9 @@ int parse_char(int fd) node->gmlevel = sd->gmlevel; node->ip = ipl; idb_put(auth_db, sd->account_id, node); + + set_char_online(-2,node->char_id,sd->account_id); + } break; @@ -3857,7 +3676,7 @@ int parse_char(int fd) if( !char_new ) //turn character creation on/off [Kevin] i = -2; else - i = make_new_char(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35)); + i = make_new_char_sql(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35)); //'Charname already exists' (-1), 'Char creation denied' (-2) and 'You are underaged' (-3) if (i < 0) @@ -3874,17 +3693,20 @@ int parse_char(int fd) else { int len; + // retrieve data + struct mmo_charstatus char_dat; + mmo_char_fromsql(i, &char_dat, false); //Only the short data is needed. // send to player WFIFOHEAD(fd,2+MAX_CHAR_BUF); WFIFOW(fd,0) = 0x6d; - len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat[i].status); + len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat); WFIFOSET(fd,len); // add new entry to the chars list ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] == -1 ); if( ch < MAX_CHARS ) - sd->found_char[ch] = i; // position of the new char in the char_dat[] array + sd->found_char[ch] = i; // the char_id of the new char } RFIFOSKIP(fd,37); @@ -3898,56 +3720,17 @@ int parse_char(int fd) if (cmd == 0x1fb) FIFOSD_CHECK(56); { int cid = RFIFOL(fd,2); - struct mmo_charstatus* cs = NULL; ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid); memcpy(email, RFIFOP(fd,6), 40); - RFIFOSKIP(fd,( cmd == 0x68 ) ? 46 : 56); - - if (e_mail_check(email) == 0) - safestrncpy(email, "a@a.com", sizeof(email)); // default e-mail - - // BEGIN HACK: "change email using the char deletion 'confirm email' menu" - // if we activated email creation and email is default email - if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online - // if sended email is incorrect e-mail - if (strcmp(email, "a@a.com") == 0) { - WFIFOHEAD(fd,3); - WFIFOW(fd,0) = 0x70; - WFIFOB(fd,2) = 0; // 00 = Incorrect Email address - WFIFOSET(fd,3); - break; - } - // we change the packet to set it like selection. - ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == cid ); - if( i < MAX_CHARS ) - { - // we save new e-mail - memcpy(sd->email, email, 40); - // we send new e-mail to login-server ('online' login-server is checked before) - WFIFOHEAD(login_fd,46); - WFIFOW(login_fd,0) = 0x2715; - WFIFOL(login_fd,2) = sd->account_id; - memcpy(WFIFOP(login_fd, 6), email, 40); - WFIFOSET(login_fd,46); - - // change value to put new packet (char selection) - RFIFOSKIP(fd,-3); //FIXME: Will this work? Messing with the received buffer is ugly anyway... - RFIFOW(fd,0) = 0x66; - RFIFOB(fd,2) = char_dat[sd->found_char[i]].status.slot; - // not send packet, it's modify of actual packet - } else { - WFIFOHEAD(fd,3); - WFIFOW(fd,0) = 0x70; - WFIFOB(fd,2) = 0; // 00 = Incorrect Email address - WFIFOSET(fd,3); - } - break; - } - // END HACK - - // otherwise, we delete the character - if (strcmpi(email, sd->email) != 0) { // if it's an invalid email + RFIFOSKIP(fd,( cmd == 0x68) ? 46 : 56); + + // Check if e-mail is correct + if(strcmpi(email, sd->email) && //email does not matches and + ( + strcmp("a@a.com", sd->email) || //it is not default email, or + (strcmp("a@a.com", email) && strcmp("", email)) //email sent does not matches default + )) { //Fail WFIFOHEAD(fd,3); WFIFOW(fd,0) = 0x70; WFIFOB(fd,2) = 0; // 00 = Incorrect Email address @@ -3956,7 +3739,7 @@ int parse_char(int fd) } // check if this char exists - ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == cid ); + ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid ); if( i == MAX_CHARS ) { // Such a character does not exist in the account WFIFOHEAD(fd,3); @@ -3966,45 +3749,22 @@ int parse_char(int fd) break; } - // deletion process - cs = &char_dat[sd->found_char[i]].status; - - //check for config char del condition [Lupus] - if( ( char_del_level > 0 && cs->base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && cs->base_level <= (unsigned int)(-char_del_level) ) ) - { - WFIFOHEAD(fd,3); - WFIFOW(fd,0) = 0x70; - WFIFOB(fd,2) = 1; // This character cannot be deleted. - WFIFOSET(fd,3); - break; - } - - char_delete(cs); - if (sd->found_char[i] != char_num - 1) { - int j, k; - struct char_session_data *sd2; - memcpy(&char_dat[sd->found_char[i]], &char_dat[char_num-1], sizeof(struct mmo_charstatus)); - // Correct moved character reference in the character's owner - for (j = 0; j < fd_max; j++) { - if (session[j] && (sd2 = (struct char_session_data*)session[j]->session_data) && - sd2->account_id == char_dat[char_num-1].status.account_id) { - for (k = 0; k < MAX_CHARS; k++) { - if (sd2->found_char[k] == char_num-1) { - sd2->found_char[k] = sd->found_char[i]; - break; - } - } - break; - } - } - } - char_num--; - // remove char from list and compact it for(ch = i; ch < MAX_CHARS-1; ch++) sd->found_char[ch] = sd->found_char[ch+1]; sd->found_char[MAX_CHARS-1] = -1; - + + /* Delete character */ + if(delete_char_sql(cid)<0){ + //can't delete the char + //either SQL error or can't delete by some CONFIG conditions + //del fail + WFIFOHEAD(fd,3); + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; + WFIFOSET(fd, 3); + break; + } /* Char successfully deleted.*/ WFIFOHEAD(fd,2); WFIFOW(fd,0) = 0x6f; @@ -4025,8 +3785,32 @@ int parse_char(int fd) case 0x28d: FIFOSD_CHECK(34); { - //not implemented + int i, aid = RFIFOL(fd,2), cid =RFIFOL(fd,6); + char name[NAME_LENGTH]; + char esc_name[NAME_LENGTH*2+1]; + safestrncpy(name, (char *)RFIFOP(fd,10), NAME_LENGTH); RFIFOSKIP(fd,34); + + if( aid != sd->account_id ) + break; + ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid ); + if( i == MAX_CHARS ) + break; + + normalize_name(name,TRIM_CHARS); + Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH)); + if( !check_char_name(name,esc_name) ) + { + i = 1; + safestrncpy(sd->new_name, name, NAME_LENGTH); + } + else + i = 0; + + WFIFOHEAD(fd, 4); + WFIFOW(fd,0) = 0x28e; + WFIFOW(fd,2) = i; + WFIFOSET(fd,4); } break; //Confirm change name. @@ -4039,8 +3823,19 @@ int parse_char(int fd) // 4: Another user is using this character name, so please select another one. FIFOSD_CHECK(6); { - //not implemented + int i; + int cid = RFIFOL(fd,2); RFIFOSKIP(fd,6); + + ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid ); + if( i == MAX_CHARS ) + break; + i = rename_char_sql(sd, cid); + + WFIFOHEAD(fd, 4); + WFIFOW(fd,0) = 0x290; + WFIFOW(fd,2) = i; + WFIFOSET(fd,4); } break; @@ -4237,9 +4032,6 @@ int broadcast_user_count(int tid, unsigned int tick, int id, intptr_t data) WBUFL(buf,2) = users; mapif_sendall(buf,6); - // refresh online files (txt and html) - create_online_files(); - return 0; } @@ -4409,9 +4201,86 @@ int char_lan_config_read(const char *lancfgName) fclose(fp); return 0; } -#endif //TXT_SQL_CONVERT -int char_config_read(const char *cfgName) +void sql_config_read(const char* cfgName) +{ + char line[1024], w1[1024], w2[1024]; + FILE* fp; + + ShowInfo("Reading file %s...\n", cfgName); + + if ((fp = fopen(cfgName, "r")) == NULL) { + ShowError("file not found: %s\n", cfgName); + return; + } + + while(fgets(line, sizeof(line), fp)) + { + if(line[0] == '/' && line[1] == '/') + continue; + + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + + if(!strcmpi(w1,"char_db")) + safestrncpy(char_db, w2, sizeof(char_db)); + else if(!strcmpi(w1,"scdata_db")) + safestrncpy(scdata_db, w2, sizeof(scdata_db)); + else if(!strcmpi(w1,"cart_db")) + safestrncpy(cart_db, w2, sizeof(cart_db)); + else if(!strcmpi(w1,"inventory_db")) + safestrncpy(inventory_db, w2, sizeof(inventory_db)); + else if(!strcmpi(w1,"charlog_db")) + safestrncpy(charlog_db, w2, sizeof(charlog_db)); + else if(!strcmpi(w1,"storage_db")) + safestrncpy(storage_db, w2, sizeof(storage_db)); + else if(!strcmpi(w1,"reg_db")) + safestrncpy(reg_db, w2, sizeof(reg_db)); + else if(!strcmpi(w1,"skill_db")) + safestrncpy(skill_db, w2, sizeof(skill_db)); + else if(!strcmpi(w1,"interlog_db")) + safestrncpy(interlog_db, w2, sizeof(interlog_db)); + else if(!strcmpi(w1,"memo_db")) + safestrncpy(memo_db, w2, sizeof(memo_db)); + else if(!strcmpi(w1,"guild_db")) + safestrncpy(guild_db, w2, sizeof(guild_db)); + else if(!strcmpi(w1,"guild_alliance_db")) + safestrncpy(guild_alliance_db, w2, sizeof(guild_alliance_db)); + else if(!strcmpi(w1,"guild_castle_db")) + safestrncpy(guild_castle_db, w2, sizeof(guild_castle_db)); + else if(!strcmpi(w1,"guild_expulsion_db")) + safestrncpy(guild_expulsion_db, w2, sizeof(guild_expulsion_db)); + else if(!strcmpi(w1,"guild_member_db")) + safestrncpy(guild_member_db, w2, sizeof(guild_member_db)); + else if(!strcmpi(w1,"guild_skill_db")) + safestrncpy(guild_skill_db, w2, sizeof(guild_skill_db)); + else if(!strcmpi(w1,"guild_position_db")) + safestrncpy(guild_position_db, w2, sizeof(guild_position_db)); + else if(!strcmpi(w1,"guild_storage_db")) + safestrncpy(guild_storage_db, w2, sizeof(guild_storage_db)); + else if(!strcmpi(w1,"party_db")) + safestrncpy(party_db, w2, sizeof(party_db)); + else if(!strcmpi(w1,"pet_db")) + safestrncpy(pet_db, w2, sizeof(pet_db)); + else if(!strcmpi(w1,"mail_db")) + safestrncpy(mail_db, w2, sizeof(mail_db)); + else if(!strcmpi(w1,"auction_db")) + safestrncpy(auction_db, w2, sizeof(auction_db)); + else if(!strcmpi(w1,"friend_db")) + safestrncpy(friend_db, w2, sizeof(friend_db)); + else if(!strcmpi(w1,"hotkey_db")) + safestrncpy(hotkey_db, w2, sizeof(hotkey_db)); + else if(!strcmpi(w1,"quest_db")) + safestrncpy(quest_db,w2,sizeof(quest_db)); + //support the import command, just like any other config + else if(!strcmpi(w1,"import")) + sql_config_read(w2); + } + fclose(fp); + ShowInfo("Done reading %s.\n", cfgName); +} + +int char_config_read(const char* cfgName) { char line[1024], w1[1024], w2[1024]; FILE* fp = fopen(cfgName, "r"); @@ -4437,7 +4306,6 @@ int char_config_read(const char *cfgName) } else if(strcmpi(w1,"console_silent")==0){ ShowInfo("Console Silent Setting: %d\n", atoi(w2)); msg_silent = atoi(w2); -#ifndef TXT_SQL_CONVERT } else if(strcmpi(w1,"stdout_with_ansisequence")==0){ stdout_with_ansisequence = config_switch(w2); } else if (strcmpi(w1, "userid") == 0) { @@ -4482,18 +4350,6 @@ int char_config_read(const char *cfgName) char_new = (bool)atoi(w2); } else if (strcmpi(w1, "char_new_display") == 0) { char_new_display = atoi(w2); - } else if (strcmpi(w1, "email_creation") == 0) { - email_creation = config_switch(w2); - } else if (strcmpi(w1, "scdata_txt") == 0) { //By Skotlex - safestrncpy(scdata_txt, w2, sizeof(scdata_txt)); -#endif - } else if (strcmpi(w1, "char_txt") == 0) { - safestrncpy(char_txt, w2, sizeof(char_txt)); - } else if (strcmpi(w1, "friends_txt") == 0) { //By davidsiaw - safestrncpy(friends_txt, w2, sizeof(friends_txt)); - } else if (strcmpi(w1, "hotkeys_txt") == 0) { //By davidsiaw - safestrncpy(hotkeys_txt, w2, sizeof(hotkeys_txt)); -#ifndef TXT_SQL_CONVERT } else if (strcmpi(w1, "max_connect_user") == 0) { max_connect_user = atoi(w2); if (max_connect_user < 0) @@ -4535,8 +4391,6 @@ int char_config_read(const char *cfgName) } else if (strcmpi(w1, "unknown_char_name") == 0) { safestrncpy(unknown_char_name, w2, sizeof(unknown_char_name)); unknown_char_name[NAME_LENGTH-1] = '\0'; - } else if (strcmpi(w1, "char_log_filename") == 0) { - safestrncpy(char_log_filename, w2, sizeof(char_log_filename)); } else if (strcmpi(w1, "name_ignoring_case") == 0) { name_ignoring_case = (bool)config_switch(w2); } else if (strcmpi(w1, "char_name_option") == 0) { @@ -4549,23 +4403,6 @@ int char_config_read(const char *cfgName) char_del_level = atoi(w2); } else if (strcmpi(w1, "char_del_delay") == 0) { char_del_delay = atoi(w2); -// online files options - } else if (strcmpi(w1, "online_txt_filename") == 0) { - safestrncpy(online_txt_filename, w2, sizeof(online_txt_filename)); - } else if (strcmpi(w1, "online_html_filename") == 0) { - safestrncpy(online_html_filename, w2, sizeof(online_html_filename)); - } else if (strcmpi(w1, "online_sorting_option") == 0) { - online_sorting_option = atoi(w2); - } else if (strcmpi(w1, "online_display_option") == 0) { - online_display_option = atoi(w2); - } else if (strcmpi(w1, "online_gm_display_min_level") == 0) { // minimum GM level to display 'GM' when we want to display it - online_gm_display_min_level = atoi(w2); - if (online_gm_display_min_level < 5) // send online file every 5 seconds to player is enough - online_gm_display_min_level = 5; - } else if (strcmpi(w1, "online_refresh_html") == 0) { - online_refresh_html = atoi(w2); - if (online_refresh_html < 1) - online_refresh_html = 1; } else if(strcmpi(w1,"db_path")==0) { safestrncpy(db_path, w2, sizeof(db_path)); } else if (strcmpi(w1, "console") == 0) { @@ -4590,7 +4427,6 @@ int char_config_read(const char *cfgName) } } else if (strcmpi(w1, "guild_exp_rate") == 0) { guild_exp_rate = atoi(w2); -#endif //TXT_SQL_CONVERT } else if (strcmpi(w1, "import") == 0) { char_config_read(w2); } @@ -4601,41 +4437,36 @@ int char_config_read(const char *cfgName) return 0; } -#ifndef TXT_SQL_CONVERT void do_final(void) { ShowStatus("Terminating...\n"); - mmo_char_sync(); - inter_save(); set_all_offline(-1); + set_all_offline_sql(); + + inter_final(); + flush_fifos(); do_final_mapif(); do_final_loginif(); - // write online players files with no player - online_char_db->clear(online_char_db, NULL); - create_online_files(); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `ragsrvinfo`") ) + Sql_ShowDebug(sql_handle); + char_db_->destroy(char_db_, NULL); online_char_db->destroy(online_char_db, NULL); auth_db->destroy(auth_db, NULL); - - if(char_dat) aFree(char_dat); - + if( char_fd != -1 ) { do_close(char_fd); char_fd = -1; } -#ifdef ENABLE_SC_SAVING - status_final(); -#endif - inter_final(); + Sql_Free(sql_handle); mapindex_final(); - char_log("----End of char-server (normal end with closing of all files).\n"); ShowStatus("Finished.\n"); } @@ -4679,28 +4510,24 @@ int do_init(int argc, char **argv) char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]); char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME); + sql_config_read(SQL_CONF_NAME); if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) { ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n"); - ShowNotice("Please edit your save/account.txt file to create a proper inter-server user/password (gender 'S')\n"); + ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n"); ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n"); } ShowInfo("Finished reading the char-server configuration.\n"); - // a newline in the log... - char_log(""); - char_log("The char-server starting...\n"); - + inter_init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server テハア篳ュ + ShowInfo("Finished reading the inter-server configuration.\n"); + ShowInfo("Initializing char server.\n"); auth_db = idb_alloc(DB_OPT_RELEASE_DATA); online_char_db = idb_alloc(DB_OPT_RELEASE_DATA); - mmo_char_init(); + mmo_char_sql_init(); char_read_fame_list(); //Read fame lists. -#ifdef ENABLE_SC_SAVING - status_init(); -#endif - inter_init_txt((argc > 2) ? argv[2] : inter_cfgName); // inter server 初期化 ShowInfo("char server initialized.\n"); if ((naddr_ != 0) && (!login_ip || !char_ip)) @@ -4736,18 +4563,32 @@ int do_init(int argc, char **argv) add_timer_func_list(online_data_cleanup, "online_data_cleanup"); add_timer_interval(gettick() + 1000, online_data_cleanup, 0, 0, 600 * 1000); - // periodic flush of all saved data to disk - add_timer_func_list(mmo_char_sync_timer, "mmo_char_sync_timer"); - add_timer_interval(gettick() + 1000, mmo_char_sync_timer, 0, 0, autosave_interval); - if( console ) { //##TODO invoke a CONSOLE_START plugin event } + //Cleaning the tables for NULL entrys @ startup [Sirius] + //Chardb clean + ShowInfo("Cleaning the '%s' table...\n", char_db); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", char_db) ) + Sql_ShowDebug(sql_handle); + + //guilddb clean + ShowInfo("Cleaning the '%s' table...\n", guild_db); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", guild_db) ) + Sql_ShowDebug(sql_handle); + + //guildmemberdb clean + ShowInfo("Cleaning the '%s' table...\n", guild_member_db); + if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", guild_member_db) ) + Sql_ShowDebug(sql_handle); + + ShowInfo("End of char server initilization function.\n"); + set_defaultparse(parse_char); + ShowInfo("open port %d.....\n",char_port); char_fd = make_listen_bind(bind_ip, char_port); - char_log("The char-server is ready (Server is listening on the port %d).\n", char_port); ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port); if( runflag != CORE_ST_STOP ) @@ -4758,5 +4599,3 @@ int do_init(int argc, char **argv) return 0; } - -#endif //TXT_SQL_CONVERT |