//-------------------------------------------------------- // Save account_reg to sql (type=2) int inter_accreg_tosql(int account_id,struct accreg *reg){ int j; char temp_str[64]; //Needs be twice the source to ensure it fits [Skotlex] char temp_str2[512]; if (account_id<=0) return 0; reg->account_id=account_id; //`global_reg_value` (`type`, `account_id`, `char_id`, `str`, `value`) sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=2 AND `account_id`='%d'",reg_db, account_id); if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } if (reg->reg_num<=0) return 0; for(j=0;j<reg->reg_num;j++){ if(reg->reg[j].str != NULL){ sprintf(tmp_sql,"INSERT INTO `%s` (`type`, `account_id`, `str`, `value`) VALUES (2,'%d', '%s','%s')", reg_db, reg->account_id, jstrescapecpy(temp_str,reg->reg[j].str), jstrescapecpy(temp_str2,reg->reg[j].value)); if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } return 0; }
int log_branch(struct map_session_data *sd) { #ifndef TXT_ONLY char t_name[NAME_LENGTH*2]; #endif FILE *logfp; if(!log_config.enable_logs) return 0; nullpo_retr(0, sd); #ifndef TXT_ONLY if(log_config.sql_logs > 0) { sprintf(tmp_sql, "INSERT DELAYED INTO `%s` (`branch_date`, `account_id`, `char_id`, `char_name`, `map`) VALUES (NOW(), '%d', '%d', '%s', '%s')", log_config.log_branch_db, sd->status.account_id, sd->status.char_id, jstrescapecpy(t_name, sd->status.name), mapindex_id2name(sd->mapindex)); if(mysql_query(&logmysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&logmysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); return 0; } return 1; } #endif if((logfp=fopen(log_config.log_branch,"a+")) == NULL) return 0; time(&curtime); strftime(timestring, 254, "%m/%d/%Y %H:%M:%S", localtime(&curtime)); fprintf(logfp,"%s - %s[%d:%d]\t%s%s", timestring, sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(sd->mapindex), RETCODE); fclose(logfp); return 1; }
//-------------------------------------------------------- // Save registry to sql int inter_accreg_tosql(int account_id, int char_id, struct accreg *reg, int type) { int j; char temp_str[64]; //Needs be twice the source to ensure it fits [Skotlex] char temp_str2[512]; if (account_id<=0) return 0; reg->account_id=account_id; reg->char_id = char_id; switch (type) { case 3: //Char Reg //`global_reg_value` (`type`, `account_id`, `char_id`, `str`, `value`) sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, char_id); break; case 2: //Account Reg //`global_reg_value` (`type`, `account_id`, `char_id`, `str`, `value`) sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=2 AND `account_id`='%d'",reg_db, account_id); break; case 1: //Account2 Reg ShowError("inter_accreg_tosql: Char server shouldn't handle type 1 registry values (##). That is the login server's work!\n"); return 0; default: ShowError("inter_accreg_tosql: Invalid type %d\n", type); return 0; } if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } if (reg->reg_num<=0) return 0; for(j=0; j<reg->reg_num; j++) { if(reg->reg[j].str != NULL) { sprintf(tmp_sql,"INSERT INTO `%s` (`type`, `account_id`, `char_id`, `str`, `value`) VALUES ('%d','%d','%d','%s','%s')", reg_db, type, type!=3?reg->account_id:0, type==3?reg->char_id:0, jstrescapecpy(temp_str,reg->reg[j].str), jstrescapecpy(temp_str2,reg->reg[j].value)); if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } return 1; }
// Search for the party according to its name struct party* search_partyname(char *str) { struct party *p=NULL; int leader_id = 0; char newstr[100]; //To support ' and \'s jstrescapecpy(newstr, str); //To support ' and \'s sprintf(tmp_sql,"SELECT `party_id`, `name`,`exp`,`item`,`leader_id` FROM `party` WHERE BINARY `name`='%s'",newstr); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (select `party`)- %s\n", mysql_error(&mysql_handle) ); return p; } sql_res = mysql_store_result(&mysql_handle) ; if (mysql_num_rows(sql_res)<=0) { mysql_free_result(sql_res); return p; } sql_row = mysql_fetch_row(sql_res); p = party_pt; p->party_id = atoi(sql_row[0]); strcpy(p->name, sql_row[1]); p->exp = atoi(sql_row[2]); p->item = atoi(sql_row[3]); leader_id = atoi(sql_row[4]); mysql_free_result(sql_res); // Load members sprintf(tmp_sql,"SELECT `account_id`, `name`,`base_level`,`last_map`,`online` FROM `char` WHERE `party_id`='%d'",p->party_id); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (select `party`)- %s\n", mysql_error(&mysql_handle) ); return 0; } sql_res = mysql_store_result(&mysql_handle) ; if (mysql_num_rows(sql_res)>0) { int i; for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ struct party_member *m = &p->member[i]; m->account_id = atoi(sql_row[0]); if (m->account_id == leader_id) m->leader = 1; else m->leader = 0; strncpy(m->name,sql_row[1],sizeof(m->name)); m->lv = atoi(sql_row[2]); strncpy(m->map,sql_row[3],sizeof(m->map)); m->online = atoi(sql_row[4]); } printf("- %d members found in party %d \n",i,p->party_id); } mysql_free_result(sql_res); return p; }
int log_atcommand(struct map_session_data *sd, const char *message) { FILE *logfp; #ifndef TXT_ONLY char t_name[NAME_LENGTH*2]; char t_msg[CHAT_SIZE*2+1]; //These are the contents of an @ call, so there shouldn't be overflow danger here? #endif if(!log_config.enable_logs) return 0; nullpo_retr(0, sd); #ifndef TXT_ONLY if(log_config.sql_logs > 0) { if (strlen(message) > CHAT_SIZE) { if (battle_config.error_log) ShowError("log atcommand: Received message too long from player %s (%d:%d)!\n", sd->status.name, sd->status.account_id, sd->status.char_id); return 0; } sprintf(tmp_sql, "INSERT DELAYED INTO `%s` (`atcommand_date`, `account_id`, `char_id`, `char_name`, `map`, `command`) VALUES(NOW(), '%d', '%d', '%s', '%s', '%s') ", log_config.log_gm_db, sd->status.account_id, sd->status.char_id, jstrescapecpy(t_name, sd->status.name), mapindex_id2name(sd->mapindex), jstrescapecpy(t_msg, (char *)message)); if(mysql_query(&logmysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&logmysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); return 0; } return 1; } #endif if((logfp=fopen(log_config.log_gm,"a+")) == NULL) return 0; time(&curtime); strftime(timestring, 254, "%m/%d/%Y %H:%M:%S", localtime(&curtime)); fprintf(logfp,"%s - %s[%d]: %s%s",timestring,sd->status.name,sd->status.account_id,message,RETCODE); fclose(logfp); return 1; }
int mapif_save_homunculus(int fd, int account_id, struct s_homunculus *hd) { int flag =1; char t_name[NAME_LENGTH*2]; jstrescapecpy(t_name, hd->name); if(hd->hom_id==0) // new homunculus { sprintf(tmp_sql, "INSERT INTO `homunculus` " "(`char_id`, `class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`) " "VALUES ('%d', '%d', '%s', '%d', '%u', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", hd->char_id, hd->class_,t_name,hd->level,hd->exp,hd->intimacy,hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk, hd->hp,hd->max_hp,hd->sp,hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize); } else { sprintf(tmp_sql, "UPDATE `homunculus` SET `char_id`='%d', `class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'", hd->char_id, hd->class_,t_name,hd->level,hd->exp,hd->intimacy,hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk, hd->hp,hd->max_hp,hd->sp,hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id); } if(mysql_query(&mysql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); flag = 0; } if(hd->hom_id==0 && flag!=0) hd->hom_id = (int)mysql_insert_id(&mysql_handle); // new homunculus else { flag = mapif_save_homunculus_skills(hd); mapif_saved_homunculus(fd, account_id, flag); } return flag; }
// Wisp/page request to send int mapif_parse_WisRequest(int fd) { struct WisData* wd; static int wisid = 0; char name[NAME_LENGTH], t_name[NAME_LENGTH*2]; //Needs space to allocate names with escaped chars [Skotlex] if ( fd <= 0 ) {return 0;} // check if we have a valid fd if (RFIFOW(fd,2)-52 >= sizeof(wd->msg)) { ShowWarning("inter: Wis message size too long.\n"); return 0; } else if (RFIFOW(fd,2)-52 <= 0) { // normaly, impossible, but who knows... ShowError("inter: Wis message doesn't exist.\n"); return 0; } memcpy(name, RFIFOP(fd,28), NAME_LENGTH); //Received name may be too large and not contain \0! [Skotlex] name[NAME_LENGTH-1]= '\0'; sprintf (tmp_sql, "SELECT `name` FROM `%s` WHERE `name`='%s'", char_db, jstrescapecpy(t_name, name)); if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } sql_res = mysql_store_result(&mysql_handle); // search if character exists before to ask all map-servers if (!(sql_row = mysql_fetch_row(sql_res))) { unsigned char buf[27]; WBUFW(buf, 0) = 0x3802; memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), NAME_LENGTH); WBUFB(buf,26) = 1; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target mapif_send(fd, buf, 27); // Character exists. So, ask all map-servers } else { // to be sure of the correct name, rewrite it memset(name, 0, NAME_LENGTH); strncpy(name, sql_row[0], NAME_LENGTH); // if source is destination, don't ask other servers. if (strcmp((char*)RFIFOP(fd,4),name) == 0) { unsigned char buf[27]; WBUFW(buf, 0) = 0x3802; memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), NAME_LENGTH); WBUFB(buf,26) = 1; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target mapif_send(fd, buf, 27); } else { CREATE(wd, struct WisData, 1); // Whether the failure of previous wisp/page transmission (timeout) check_ttl_wisdata(); wd->id = ++wisid; wd->fd = fd; wd->len= RFIFOW(fd,2)-52; memcpy(wd->src, RFIFOP(fd, 4), NAME_LENGTH); memcpy(wd->dst, RFIFOP(fd,28), NAME_LENGTH); memcpy(wd->msg, RFIFOP(fd,52), wd->len); wd->tick = gettick(); numdb_insert(wis_db, wd->id, wd); mapif_wis_message(wd); } } //Freeing ... O.o if(sql_res){ mysql_free_result(sql_res); } return 0; }
// Save interlog into sql int inter_log(char *fmt,...) { char str[255]; char temp_str[510]; //Needs be twice as long as str[] //Skotlex va_list ap; va_start(ap,fmt); vsprintf(str,fmt,ap); sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `log`) VALUES (NOW(), '%s')",interlog_db, jstrescapecpy(temp_str,str)); if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } va_end(ap); return 0; }
int log_chat(char *type, int type_id, int src_charid, int src_accid, char *map, int x, int y, char *dst_charname, char *message) { FILE *logfp; #ifndef TXT_ONLY char t_charname[NAME_LENGTH*2]; char t_msg[CHAT_SIZE*2+1]; //Chat line fully escaped, with an extra space just in case. #endif //Check ON/OFF if(log_config.chat <= 0) return 0; //Deactivated #ifndef TXT_ONLY if(log_config.sql_logs > 0) { if (strlen(message) > CHAT_SIZE) { if (battle_config.error_log) ShowError("log chat: Received message too long from type %d (%d:%d)!\n", type_id, src_accid, src_charid); return 0; } sprintf(tmp_sql, "INSERT DELAYED INTO `%s` (`time`, `type`, `type_id`, `src_charid`, `src_accountid`, `src_map`, `src_map_x`, `src_map_y`, `dst_charname`, `message`) VALUES (NOW(), '%s', '%d', '%d', '%d', '%s', '%d', '%d', '%s', '%s')", log_config.log_chat_db, type, type_id, src_charid, src_accid, map, x, y, jstrescapecpy(t_charname, dst_charname), jstrescapecpy(t_msg, message)); if(mysql_query(&logmysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&logmysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); return 0; } return 1; } #endif if((logfp = fopen(log_config.log_chat, "a+")) == NULL) return 0; time(&curtime); strftime(timestring, 254, "%m/%d/%Y %H:%M:%S", localtime(&curtime)); //DATE - type,type_id,src_charid,src_accountid,src_map,src_x,src_y,dst_charname,message fprintf(logfp, "%s - %s,%d,%d,%d,%s,%d,%d,%s,%s%s", timestring, type, type_id, src_charid, src_accid, map, x, y, dst_charname, message, RETCODE); fclose(logfp); return 1; }
int charsave_savechar(int charid, struct mmo_charstatus *c){ int i,j; char *str_p; char tmp_str[64]; char tmp_str2[512]; //First save the 'char' sprintf(tmp_sql ,"UPDATE `char` SET `class`='%d', `base_level`='%d', `job_level`='%d'," "`base_exp`='%d', `job_exp`='%d', `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',`karma`='%d',`manner`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d'," "`hair`='%d',`hair_color`='%d',`clothes_color`='%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'," "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d', `fame`='%d'" "WHERE `account_id`='%d' AND `char_id` = '%d'", c->class_, c->base_level, c->job_level, c->base_exp, c->job_exp, c->zeny, c->max_hp, c->hp, c->max_sp, c->sp, c->status_point, c->skill_point, c->str, c->agi, c->vit, c->int_, c->dex, c->luk, c->option, c->karma, c->manner, c->party_id, c->guild_id, c->pet_id, c->hair, c->hair_color, c->clothes_color, c->weapon, c->shield, c->head_top, c->head_mid, c->head_bottom, c->last_point.map, c->last_point.x, c->last_point.y, c->save_point.map, c->save_point.x, c->save_point.y, c->partner_id, c->father, c->mother, c->child, c->fame, c->account_id, c->char_id ); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } //Save the inventory sprintf(tmp_sql, "DELETE FROM `inventory` WHERE `char_id` = '%d'", charid); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } for(i = 0; i < MAX_INVENTORY; i++){ if(c->inventory[i].nameid > 0){ str_p = tmp_sql; str_p += sprintf(str_p, "INSERT INTO `inventory` (`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`"); for (j = 0; j < MAX_SLOTS; j++) str_p += sprintf(str_p, ", `card%d`", j); str_p += sprintf(str_p, ") VALUES ('%d', '%d', '%d', '%d', '%d', '%d', '%d'", charid, c->inventory[i].nameid, c->inventory[i].amount, c->inventory[i].equip, c->inventory[i].identify, c->inventory[i].refine, c->inventory[i].attribute); for (j = 0; j < MAX_SLOTS; j++) str_p += sprintf(str_p, ", '%d'", c->inventory[i].card[j]); strcat(tmp_sql,")"); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } //Save the cart sprintf(tmp_sql, "DELETE FROM `cart_inventory` WHERE `char_id` = '%d'", charid); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } for(i = 0; i < MAX_CART; i++){ if(c->cart[i].nameid > 0){ str_p = tmp_sql; str_p += sprintf(str_p, "INSERT INTO `cart_inventory` (`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`"); for (j = 0; j < MAX_SLOTS; j++) str_p += sprintf(str_p, ", `card%d`", j); str_p += sprintf(str_p, ") VALUES ('%d', '%d', '%d', '%d', '%d', '%d', '%d'", charid, c->cart[i].nameid, c->cart[i].amount, c->cart[i].equip, c->cart[i].identify, c->cart[i].refine, c->cart[i].attribute); for (j = 0; j < MAX_SLOTS; j++) str_p += sprintf(str_p, ", '%d'", c->cart[i].card[j]); strcat(tmp_sql,")"); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } //Save memo points sprintf(tmp_sql, "DELETE FROM `memo` WHERE `char_id` = '%d'", charid); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } for(i = 0; i < MAX_MEMOPOINTS; i++){ if(c->memo_point[i].map != "" && c->memo_point[i].x > 0 && c->memo_point[i].y > 0){ sprintf(tmp_sql, "INSERT INTO `memo` ( `char_id`, `map`, `x`, `y` ) VALUES ('%d', '%s', '%d', '%d')", charid, c->memo_point[i].map, c->memo_point[i].x, c->memo_point[i].y); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } //Save skills sprintf(tmp_sql, "DELETE FROM `skill` WHERE `char_id` = '%d'", charid); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } for(i = 0; i < MAX_SKILL; i++){ if(c->skill[i].id > 0){ sprintf(tmp_sql, "INSERT INTO `skill` (`char_id`, `id`, `lv`) VALUES ('%d', '%d', '%d')", charid, c->skill[i].id, c->skill[i].lv); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } //global_reg_value saving sprintf(tmp_sql, "DELETE FROM `global_reg_value` WHERE `type`=3 AND `char_id` = '%d'", charid); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } for(i = 0; i < c->global_reg_num; i++){ if(c->global_reg[i].str){ if(c->global_reg[i].value){ //jstrescapecpy(tmp_str, c->global_reg[i].str); sprintf(tmp_sql, "INSERT INTO `global_reg_value` (`char_id`, `str`, `value`) VALUES ('%d', '%s', '%s')", charid, jstrescapecpy(tmp_str,c->global_reg[i].str), jstrescapecpy(tmp_str2,c->global_reg[i].value)); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } } //friendlist saving sprintf(tmp_sql, "DELETE FROM `friends` WHERE `char_id` = '%d'", charid); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } for(i = 0; i < MAX_FRIENDS; i++){ if(c->friends[i].char_id > 0){ sprintf(tmp_sql, "INSERT INTO `friends` (`char_id`, `friend_account`, `friend_id`) VALUES ('%d','%d','%d')", charid, c->friends[i].account_id, c->friends[i].char_id); if(mysql_query(&charsql_handle, tmp_sql)){ ShowSQL("DB error - %s\n",mysql_error(&charsql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } } ShowInfo("charsql_savechar(): saving of '%d' (%s) complete.\n", charid, c->name); return 0; }
// パーティ脱退要求 int mapif_parse_PartyLeave(int fd,int party_id,int account_id) { char t_member[2*NAME_LENGTH]; struct party *p = party_pt; if (p == NULL) { ShowFatalError("int_party: out of memory !\n"); return 0; } inter_party_fromsql(party_id, p); if (p->party_id >= 0) { int i; for (i = 0; i < MAX_PARTY; i++) { if (p->member[i].account_id == account_id) { mapif_party_leaved(party_id, account_id, p->member[i].name); // Update char information, does the name need encoding? sprintf (tmp_sql, "UPDATE `%s` SET `party_id`='0' WHERE `party_id`='%d' AND `name`='%s'", char_db, party_id, jstrescapecpy(t_member,p->member[i].name)); if (mysql_query (&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } if (p->member[i].leader == 1){ int j; for (j = 0; j < MAX_PARTY; j++) { if (p->member[j].account_id > 0 && j != i) { mapif_party_leaved(party_id, p->member[j].account_id, p->member[j].name); } } // we'll skip name-checking and just reset everyone with the same party id [celest] // -- if anything goes wrong just uncomment the section above ^^; sprintf (tmp_sql, "UPDATE `%s` SET `party_id`='0' WHERE `party_id`='%d'", char_db, party_id); if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } // Delete the party, if has no member. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `party_id`='%d'", party_db, party_id); if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } memset(p, 0, sizeof(struct party)); } else memset(&p->member[i], 0, sizeof(struct party_member)); break; } } if (party_check_empty(p) == 0) mapif_party_info(-1,p);// まだ人がいるのでデータ送信 //else // inter_party_tosql(party_id,p); // Break the party if no member } else { sprintf(tmp_sql, "UPDATE `%s` SET `party_id`='0' WHERE `party_id`='%d' AND `account_id`='%d' AND `online`='1'", char_db, party_id, account_id); if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } } return 0; }
// Save party to mysql int inter_party_tosql(int party_id,struct party *p) { // 'party' ('party_id','name','exp','item','leader') char t_name[NAME_LENGTH*2], t_member[NAME_LENGTH*2]; //Required for jstrescapecpy [Skotlex] int party_member = 0, party_online_member = 0; int party_exist = 0; int i; #ifdef NOISY ShowInfo("Save party request (\033[1;64m%d\033[0m).\n", party_id); #endif jstrescapecpy(t_name, p->name); if (p == NULL || party_id == 0 || p->party_id == 0 || party_id != p->party_id) { ShowError("Party pointer or party_id error\n"); return 0; } // Check if party exists sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `party_id`='%d'", party_db, party_id); // TBR if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); return 0; } sql_res = mysql_store_result(&mysql_handle); if (sql_res != NULL && mysql_num_rows(sql_res) > 0) { sql_row = mysql_fetch_row(sql_res); party_exist = atoi (sql_row[0]); //printf("- Check if party %d exists : %s\n",party_id,party_exist==0?"No":"Yes"); } mysql_free_result(sql_res) ; //resource free if (party_exist > 0) { // Check members in party sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `party_id`='%d'", char_db, party_id); // TBR if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); return 0; } sql_res = mysql_store_result(&mysql_handle) ; if (sql_res != NULL && mysql_num_rows(sql_res) > 0) { sql_row = mysql_fetch_row(sql_res); party_member = atoi (sql_row[0]); // printf("- Check members in party %d : %d \n",party_id,party_member); } mysql_free_result(sql_res) ; //resource free for (i = 0; i < MAX_PARTY; i++) if (p->member[i].account_id > 0) party_online_member++; //if (party_online_member == 0) // printf("- No member online \n"); //else printf("- Some member %d online \n", party_online_member); if (party_member <= 0 && party_online_member == 0) { // Delete the party, if has no member. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `party_id`='%d'", party_db, party_id); if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } // printf("No member in party %d, break it \n",party_id); memset(p, 0, sizeof(struct party)); return 0; } else { char *tmp = tmp_sql; tmp_sql[0] = '\0'; // Update party information, if exists for (i = 0; i < MAX_PARTY; i++) { if (p->member[i].account_id > 0){ if (tmp_sql[0] == '\0') tmp += sprintf(tmp_sql, "UPDATE `%s` SET `party_id`='%d' WHERE (`account_id` = '%d' AND `name` = '%s')", char_db, party_id, p->member[i].account_id, jstrescapecpy(t_member, p->member[i].name)); else tmp += sprintf(tmp, " OR (`account_id` = '%d' AND `name` = '%s')", p->member[i].account_id, jstrescapecpy(t_member, p->member[i].name)); } } //printf("%s",tmp_sql); if (tmp_sql[0] != '\0' && mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } sprintf(tmp_sql, "UPDATE `%s` SET `name`='%s', `exp`='%d', `item`='%d' WHERE `party_id`='%d'", party_db, t_name, p->exp, p->item, party_id); if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } // printf("- Update party %d information \n",party_id); } } else { // Add new party, if not exist int leader_id = 0; for (i = 0; i < MAX_PARTY && ((p->member[i].account_id > 0 && p->member[i].leader == 0) || (p->member[i].account_id < 0)); i++) ; if (i < MAX_PARTY) leader_id = p->member[i].account_id; sprintf(tmp_sql, "INSERT INTO `%s` (`party_id`, `name`, `exp`, `item`, `leader_id`) VALUES ('%d', '%s', '%d', '%d', '%d')", party_db, party_id, t_name, p->exp, p->item, leader_id); if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); return 0; } sprintf(tmp_sql,"UPDATE `%s` SET `party_id`='%d' WHERE `account_id`='%d' AND `name`='%s'", char_db, party_id,leader_id, jstrescapecpy(t_member, p->member[i].name)); if (mysql_query(&mysql_handle, tmp_sql)) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } //printf("- Insert new party %d \n",party_id); } if (save_log) ShowInfo("Party Saved (%d - %s)\n", party_id, p->name); return 0; }
// Search for the party according to its name struct party* search_partyname(char *str) { struct party *p=NULL; int leader_id = 0; char t_name[NAME_LENGTH*2]; sprintf(tmp_sql,"SELECT `party_id`, `name`,`exp`,`item`,`leader_id` FROM `%s` WHERE `name`='%s'",party_db, jstrescapecpy(t_name,str)); if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); } sql_res = mysql_store_result(&mysql_handle) ; if (sql_res==NULL || mysql_num_rows(sql_res)<=0) { mysql_free_result(sql_res); return p; } sql_row = mysql_fetch_row(sql_res); p = party_pt; p->party_id = atoi(sql_row[0]); memcpy(p->name, sql_row[1], NAME_LENGTH-1); p->exp = atoi(sql_row[2]); p->item = atoi(sql_row[3]); leader_id = atoi(sql_row[4]); mysql_free_result(sql_res); // Load members sprintf(tmp_sql,"SELECT `account_id`, `name`,`base_level`,`last_map`,`online` FROM `%s` WHERE `party_id`='%d'",char_db, p->party_id); if(mysql_query(&mysql_handle, tmp_sql) ) { ShowSQL("DB error - %s\n",mysql_error(&mysql_handle)); ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql); return 0; } sql_res = mysql_store_result(&mysql_handle) ; if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { int i; for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ struct party_member *m = &p->member[i]; m->account_id = atoi(sql_row[0]); if (m->account_id == leader_id) m->leader = 1; else m->leader = 0; memcpy(m->name,sql_row[1],NAME_LENGTH-1); m->lv = atoi(sql_row[2]); strncpy(m->map,sql_row[3],MAP_NAME_LENGTH-1); m->online = atoi(sql_row[4]); } ShowDebug("- %d members found in party %d \n",i,p->party_id); } mysql_free_result(sql_res); return p; }
// パーティ脱退要求 int mapif_parse_PartyLeave(int fd,int party_id,int account_id) { struct party *p = party_pt; if(p==NULL){ printf("int_party: out of memory !\n"); return 0; } inter_party_fromsql(party_id, p); if(p->party_id >= 0){ int i,j; for(i=0;i<MAX_PARTY;i++){ if(p->member[i].account_id==account_id){ //printf("p->member[i].account_id = %d , account_id = %d \n",p->member[i].account_id,account_id); mapif_party_leaved(party_id,account_id,p->member[i].name); char t_memname[256]; jstrescapecpy(t_memname, p->member[i].name); // Update char information, does the name need encoding? sprintf(tmp_sql,"UPDATE `char` SET `party_id`='0', `online`='1' WHERE `party_id`='%d' AND BINARY `name`='%s';", party_id, t_memname); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); } printf("Delete member %s from MySQL \n", p->member[i].name); if (p->member[i].leader==1){ for(j=0;j<MAX_PARTY;j++) { //printf("j = %d , p->member[j].account_id = %d , p->member[j].account_id = %d \n",j,p->member[j].account_id,p->member[j].account_id); if(p->member[j].account_id>0&&j!=i){ mapif_party_leaved(party_id,p->member[j].account_id,p->member[j].name); // Update char information, does the name need encoding? char t_memname[256]; jstrescapecpy(t_memname,p->member[j].name); sprintf(tmp_sql,"UPDATE `char` SET `party_id`='0', `online`='1' WHERE `party_id`='%d' AND BINARY `name`='%s';", party_id, t_memname); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); } printf("Delete member %s from MySQL \n", p->member[j].name); } } // Delete the party, if has no member. sprintf(tmp_sql,"DELETE FROM `party` WHERE `party_id`='%d'",party_id); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); } printf("Leader breaks party %d \n",party_id); memset(p, 0, sizeof(struct party)); }else memset(&p->member[i],0,sizeof(struct party_member)); break; } } if( party_check_empty(p)==0 ) mapif_party_info(-1,p);// まだ人がいるのでデータ送信 /* else inter_party_tosql(party_id,p); // Break the party if no member */ } return 0; }
// Save party to mysql int inter_party_tosql(int party_id,struct party *p) { // 'party' ('party_id','name','exp','item','leader') char t_name[100]; int party_member = 0, party_online_member = 0; int party_exist = 0; int leader_id = 0; printf("Request save party: %d.......\n",party_id); jstrescapecpy(t_name, p->name); if (p==NULL || party_id==0 || p->party_id ==0 || party_id!=p->party_id) { printf("- Party pointer or party_id error \n"); return 0; } // Check if party exists sprintf(tmp_sql,"SELECT count(*) FROM `party` WHERE `party_id`='%d'",party_id); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); return 0; } sql_res = mysql_store_result(&mysql_handle) ; if (mysql_num_rows(sql_res)>0) { sql_row = mysql_fetch_row(sql_res); party_exist = atoi (sql_row[0]); printf("- Check if party %d exists : %s\n",party_id,party_exist==0?"No":"Yes"); } mysql_free_result(sql_res) ; //resource free if (party_exist >0){ // Check members in party sprintf(tmp_sql,"SELECT count(*) FROM `char` WHERE `party_id`='%d'",party_id); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); return 0; } sql_res = mysql_store_result(&mysql_handle) ; if (mysql_num_rows(sql_res)>0) { sql_row = mysql_fetch_row(sql_res); party_member = atoi (sql_row[0]); printf("- Check members in party %d : %d \n",party_id,party_member); } mysql_free_result(sql_res) ; //resource free party_online_member = 0; while (party_online_member<MAX_PARTY&&p->member[party_online_member].account_id<=0) party_online_member++; if (party_online_member==MAX_PARTY) printf("- No member online \n"); else printf("- Some member %d online \n", party_online_member); if (party_member <= 0 && party_online_member == MAX_PARTY) { // Delete the party, if has no member. sprintf(tmp_sql,"DELETE FROM `party` WHERE `party_id`='%d'",party_id); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); } printf("No member in party %d, break it \n",party_id); memset(p, 0, sizeof(struct party)); return 0; } else { // Update party information, if exists int i=0; for (i=0;i<MAX_PARTY;i++){ if (p->member[i].account_id>0){ sprintf(tmp_sql,"UPDATE `char` SET `party_id`='%d', `online`='%d' WHERE `account_id`='%d' AND BINARY `name`='%s';",party_id, p->member[i].online, p->member[i].account_id, jstrescape(p->member[i].name)); //printf("%s",tmp_sql); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); } } } sprintf(tmp_sql,"UPDATE `party` SET `name`='%s', `exp`='%d', `item`='%d', `leader_id`=`leader_id` WHERE `party_id`='%d';",t_name,p->exp,p->item,party_id); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (inset/update `party`)- %s\n", mysql_error(&mysql_handle) ); } printf("- Update party %d information \n",party_id); } } else { // Add new party, if not exist int i = 0; while (i<MAX_PARTY&&((p->member[i].account_id>0&&p->member[i].leader==0)||(p->member[i].account_id<0))) i++; if (i<MAX_PARTY) leader_id = p->member[i].account_id; sprintf(tmp_sql,"INSERT INTO `party` (`party_id`, `name`, `exp`, `item`, `leader_id`) VALUES ('%d', '%s', '%d', '%d', '%d');",party_id, t_name, p->exp, p->item,leader_id); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (inset/update `party`)- %s\n", mysql_error(&mysql_handle) ); } sprintf(tmp_sql,"UPDATE `char` SET `party_id`='%d', `online`='1' WHERE `account_id`='%d' AND BINARY `name`='%s';",party_id,leader_id, jstrescape(p->member[i].name)); if(mysql_query(&mysql_handle, tmp_sql) ) { printf("DB server Error (inset/update `party`)- %s\n", mysql_error(&mysql_handle) ); } printf("- Insert new party %d \n",party_id); } printf("Party save success.......\n"); return 0; }