//------------------------------------------------ //Guild bound items pull for offline characters [Akinari] //Revised by [Mhalicot] //------------------------------------------------ int mapif_parse_ItemBoundRetrieve_sub(int fd) { #ifdef GP_BOUND_ITEMS StringBuf buf; SqlStmt* stmt; struct item item; int j, i=0, s=0, bound_qt=0; struct item items[MAX_INVENTORY]; unsigned int bound_item[MAX_INVENTORY] = {0}; int char_id = RFIFOL(fd,2); int aid = RFIFOL(fd,6); int guild_id = RFIFOW(fd,10); StrBuf->Init(&buf); StrBuf->AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`"); for( j = 0; j < MAX_SLOTS; ++j ) StrBuf->Printf(&buf, ", `card%d`", j); StrBuf->Printf(&buf, " FROM `%s` WHERE `char_id`='%d' AND `bound` = '%d'",inventory_db,char_id,IBT_GUILD); stmt = SQL->StmtMalloc(sql_handle); if( SQL_ERROR == SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf)) || SQL_ERROR == SQL->StmtExecute(stmt) ) { Sql_ShowDebug(sql_handle); SQL->StmtFree(stmt); StrBuf->Destroy(&buf); return 1; } SQL->StmtBindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 8, SQLDT_UCHAR, &item.bound, 0, NULL, NULL); SQL->StmtBindColumn(stmt, 9, SQLDT_UINT64, &item.unique_id, 0, NULL, NULL); for( j = 0; j < MAX_SLOTS; ++j ) SQL->StmtBindColumn(stmt, 10+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL); while( SQL_SUCCESS == SQL->StmtNextRow(stmt) ) { memcpy(&items[i],&item,sizeof(struct item)); i++; } SQL->FreeResult(sql_handle); if(!i) { //No items found - No need to continue StrBuf->Destroy(&buf); SQL->StmtFree(stmt); return 0; } //First we delete the character's items StrBuf->Clear(&buf); StrBuf->Printf(&buf, "DELETE FROM `%s` WHERE",inventory_db); for(j=0; j<i; j++) { if( j ) StrBuf->AppendStr(&buf, " OR"); StrBuf->Printf(&buf, " `id`=%d",items[j].id); if( items[j].bound && items[j].equip ) { // Only the items that are also stored in `char` `equip` if( items[j].equip&EQP_HAND_R || items[j].equip&EQP_HAND_L || items[j].equip&EQP_HEAD_TOP || items[j].equip&EQP_HEAD_MID || items[j].equip&EQP_HEAD_LOW || items[j].equip&EQP_GARMENT ) { bound_item[bound_qt] = items[j].equip; bound_qt++; } } } if( SQL_ERROR == SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf)) || SQL_ERROR == SQL->StmtExecute(stmt) ) { Sql_ShowDebug(sql_handle); SQL->StmtFree(stmt); StrBuf->Destroy(&buf); return 1; } // Removes any view id that was set by an item that was removed if( bound_qt ) { #define CHECK_REMOVE(var,mask,token) do { /* Verifies equip bitmasks (see item.equip) and handles the sql statement */ \ if ((var)&(mask)) { \ if ((var) != (mask) && s) StrBuf->AppendStr(&buf, ","); \ StrBuf->AppendStr(&buf,"`"#token"`='0'"); \ (var) &= ~(mask); \ s++; \ } \ } while(0) StrBuf->Clear(&buf); StrBuf->Printf(&buf, "UPDATE `%s` SET ", char_db); for( j = 0; j < bound_qt; j++ ) { // Equips can be at more than one slot at the same time CHECK_REMOVE(bound_item[j],EQP_HAND_R,weapon); CHECK_REMOVE(bound_item[j],EQP_HAND_L,shield); CHECK_REMOVE(bound_item[j],EQP_HEAD_TOP,head_top); CHECK_REMOVE(bound_item[j],EQP_HEAD_MID,head_mid); CHECK_REMOVE(bound_item[j],EQP_HEAD_LOW,head_bottom); CHECK_REMOVE(bound_item[j],EQP_GARMENT,robe); } StrBuf->Printf(&buf, " WHERE `char_id`='%d'", char_id); if( SQL_ERROR == SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf)) || SQL_ERROR == SQL->StmtExecute(stmt) ) { Sql_ShowDebug(sql_handle); SQL->StmtFree(stmt); StrBuf->Destroy(&buf); return 1; } #undef CHECK_REMOVE } //Now let's update the guild storage with those deleted items /// TODO/FIXME: /// This approach is basically the same as the one from memitemdata_to_sql, but /// the latter compares current database values and this is not needed in this case /// maybe sometime separate memitemdata_to_sql into different methods in order to use /// call that function here as well [Panikon] StrBuf->Clear(&buf); StrBuf->Printf(&buf,"INSERT INTO `%s` (`guild_id`,`nameid`,`amount`,`equip`,`identify`,`refine`," "`attribute`,`expire_time`,`bound`,`unique_id`", guild_storage_db); for( s = 0; s < MAX_SLOTS; ++s ) StrBuf->Printf(&buf, ", `card%d`", s); StrBuf->AppendStr(&buf," ) VALUES "); for( j = 0; j < i; ++j ) { if( j ) StrBuf->AppendStr(&buf, ","); StrBuf->Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%"PRIu64"'", guild_id, items[j].nameid, items[j].amount, items[j].equip, items[j].identify, items[j].refine, items[j].attribute, items[j].expire_time, items[j].bound, items[j].unique_id); for( s = 0; s < MAX_SLOTS; ++s ) StrBuf->Printf(&buf, ", '%d'", items[j].card[s]); StrBuf->AppendStr(&buf, ")"); } if( SQL_ERROR == SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf)) || SQL_ERROR == SQL->StmtExecute(stmt) ) { Sql_ShowDebug(sql_handle); SQL->StmtFree(stmt); StrBuf->Destroy(&buf); return 1; } StrBuf->Destroy(&buf); SQL->StmtFree(stmt); //Finally reload storage and tell map we're done mapif_load_guild_storage(fd,aid,guild_id,0); // If character is logged in char, disconnect disconnect_player(aid); #endif return 0; }
/** * ZI 0x3056 <char_id>.L <account_id>.L <guild_id>.W * Pulls guild bound items for offline characters * @author [Akinari] */ bool mapif_parse_itembound_retrieve(int fd) { StringBuf buf; SqlStmt* stmt; unsigned short i = 0, count = 0; struct item item, items[MAX_INVENTORY]; int j, guild_id = RFIFOW(fd,10); uint32 char_id = RFIFOL(fd,2), account_id = RFIFOL(fd,6); StringBuf_Init(&buf); // Get bound items from player's inventory StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`"); for( j = 0; j < MAX_SLOTS; ++j ) StringBuf_Printf(&buf, ", `card%d`", j); for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) { StringBuf_Printf(&buf, ", `option_id%d`", j); StringBuf_Printf(&buf, ", `option_val%d`", j); StringBuf_Printf(&buf, ", `option_parm%d`", j); } StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`='%d' AND `bound` = %d", schema_config.inventory_db,char_id, BOUND_GUILD); 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); mapif_itembound_ack(fd,account_id,guild_id); return true; } SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL); SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &item.nameid, 0, NULL, NULL); SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL); SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &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); SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &item.bound, 0, NULL, NULL); for( j = 0; j < MAX_SLOTS; ++j ) SqlStmt_BindColumn(stmt, 9+j, SQLDT_USHORT, &item.card[j], 0, NULL, NULL); for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) { SqlStmt_BindColumn(stmt, 9+MAX_SLOTS+j*3, SQLDT_SHORT, &item.option[j].id, 0, NULL, NULL); SqlStmt_BindColumn(stmt, 10+MAX_SLOTS+j*3, SQLDT_SHORT, &item.option[j].value, 0, NULL, NULL); SqlStmt_BindColumn(stmt, 11+MAX_SLOTS+j*3, SQLDT_CHAR, &item.option[j].param, 0, NULL, NULL); } memset(&items, 0, sizeof(items)); while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ) memcpy(&items[count++], &item, sizeof(struct item)); Sql_FreeResult(sql_handle); ShowInfo("Found '" CL_WHITE "%d" CL_RESET "' guild bound item(s) from CID = " CL_WHITE "%d" CL_RESET ", AID = %d, Guild ID = " CL_WHITE "%d" CL_RESET ".\n", count, char_id, account_id, guild_id); if (!count) { //No items found - No need to continue StringBuf_Destroy(&buf); SqlStmt_Free(stmt); mapif_itembound_ack(fd,account_id,guild_id); return true; } char_set_session_flag(account_id, 1); // Delete bound items from player's inventory StringBuf_Clear(&buf); StringBuf_Printf(&buf, "DELETE FROM `%s` WHERE `char_id` = %d AND `bound` = %d",schema_config.inventory_db, char_id, BOUND_GUILD); if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) || SQL_ERROR == SqlStmt_Execute(stmt) ) { SqlStmt_ShowDebug(stmt); SqlStmt_Free(stmt); StringBuf_Destroy(&buf); mapif_itembound_ack(fd,account_id,guild_id); return true; } // Send the deleted items to map-server to store them in guild storage [Cydh] mapif_itembound_store2gstorage(fd, guild_id, items, count); // Verifies equip bitmasks (see item.equip) and handles the sql statement #define CHECK_REMOVE(var,mask,token,num) {\ if ((var)&(mask) && !(j&(num))) {\ if (j)\ StringBuf_AppendStr(&buf, ",");\ StringBuf_AppendStr(&buf, "`"#token"`='0'");\ j |= (1<<num);\ }\ } StringBuf_Clear(&buf); j = 0; for (i = 0; i < count && i < MAX_INVENTORY; i++) { if (!&items[i] || !items[i].equip) continue; // Equips can be at more than one slot at the same time CHECK_REMOVE(items[i].equip, EQP_HAND_R, weapon, 0); CHECK_REMOVE(items[i].equip, EQP_HAND_L, shield, 1); CHECK_REMOVE(items[i].equip, EQP_HEAD_TOP|EQP_COSTUME_HEAD_TOP, head_top, 2); CHECK_REMOVE(items[i].equip, EQP_HEAD_MID|EQP_COSTUME_HEAD_MID, head_mid, 3); CHECK_REMOVE(items[i].equip, EQP_HEAD_LOW|EQP_COSTUME_HEAD_LOW, head_bottom, 4); CHECK_REMOVE(items[i].equip, EQP_GARMENT|EQP_COSTUME_GARMENT, robe, 5); } #undef CHECK_REMOVE // Update player's view if (j) { StringBuf buf2; StringBuf_Init(&buf2); StringBuf_Printf(&buf2, "UPDATE `%s` SET %s WHERE `char_id`='%d'", schema_config.char_db, StringBuf_Value(&buf), char_id); if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) || SQL_ERROR == SqlStmt_Execute(stmt) ) { SqlStmt_ShowDebug(stmt); SqlStmt_Free(stmt); StringBuf_Destroy(&buf); StringBuf_Destroy(&buf2); mapif_itembound_ack(fd,account_id,guild_id); return true; } StringBuf_Destroy(&buf2); } StringBuf_Destroy(&buf); SqlStmt_Free(stmt); char_unset_session_flag(account_id, 1); return false; }