diff --git a/src/world/Actor/Player.cpp b/src/world/Actor/Player.cpp index 2027633c..f64f6afc 100644 --- a/src/world/Actor/Player.cpp +++ b/src/world/Actor/Player.cpp @@ -1988,7 +1988,7 @@ void Sapphire::Entity::Player::dyeItemFromDyeingInfo() // TODO: subtract/remove dye used insertInventoryItem( static_cast< Sapphire::Common::InventoryType >( itemToDyeContainer ), static_cast< uint16_t >( itemToDyeSlot ), itemToDye ); - writeItem( itemToDye ); + updateItemDb( itemToDye ); } void Sapphire::Entity::Player::resetObjSpawnIndex() diff --git a/src/world/Actor/Player.h b/src/world/Actor/Player.h index 32660358..41565b32 100644 --- a/src/world/Actor/Player.h +++ b/src/world/Actor/Player.h @@ -920,8 +920,6 @@ namespace Sapphire::Entity using InvSlotPair = std::pair< uint16_t, int8_t >; using InvSlotPairVec = std::vector< InvSlotPair >; - ItemPtr createItem( uint32_t catalogId, uint32_t quantity = 1 ); - bool loadInventory(); InvSlotPairVec getSlotsOfItemsInInventory( uint32_t catalogId ); @@ -956,7 +954,11 @@ namespace Sapphire::Entity void writeInventory( Common::InventoryType type ); - void writeItem( ItemPtr pItem ) const; + ItemPtr createTempItem( uint32_t catalogId, uint32_t quantity = 1 ); + + void updateItemDb( ItemPtr pItem ) const; + + void writeItemDb( ItemPtr pItem ) const; void deleteItemDb( ItemPtr pItem ) const; diff --git a/src/world/Actor/PlayerInventory.cpp b/src/world/Actor/PlayerInventory.cpp index 9d5c3656..f94d0237 100644 --- a/src/world/Actor/PlayerInventory.cpp +++ b/src/world/Actor/PlayerInventory.cpp @@ -306,13 +306,13 @@ void Sapphire::Entity::Player::addCurrency( CurrencyType type, uint32_t amount, if( !currItem ) { // TODO: map currency type to itemid - currItem = createItem( 1 ); + currItem = createTempItem( 1 ); m_storageMap[ Currency ]->setItem( slot, currItem ); } uint32_t currentAmount = currItem->getStackSize(); currItem->setStackSize( currentAmount + amount ); - writeItem( currItem ); + updateItemDb( currItem ); updateContainer( Currency, slot, currItem ); @@ -352,7 +352,7 @@ void Sapphire::Entity::Player::removeCurrency( Common::CurrencyType type, uint32 currItem->setStackSize( 0 ); else currItem->setStackSize( currentAmount - amount ); - writeItem( currItem ); + updateItemDb( currItem ); auto invUpdate = std::make_shared< UpdateInventorySlotPacket >( getId(), static_cast< uint8_t >( type ) - 1, @@ -369,7 +369,7 @@ void Sapphire::Entity::Player::addCrystal( Common::CrystalType type, uint32_t am if( !currItem ) { // TODO: map currency type to itemid - currItem = createItem( static_cast< uint8_t >( type ) + 1 ); + currItem = createTempItem( static_cast< uint8_t >( type ) + 1 ); m_storageMap[ Crystal ]->setItem( static_cast< uint8_t >( type ) - 1, currItem ); } @@ -377,7 +377,7 @@ void Sapphire::Entity::Player::addCrystal( Common::CrystalType type, uint32_t am currItem->setStackSize( currentAmount + amount ); - writeItem( currItem ); + updateItemDb( currItem ); writeInventory( Crystal ); @@ -417,7 +417,7 @@ void Sapphire::Entity::Player::removeCrystal( Common::CrystalType type, uint32_t else currItem->setStackSize( currentAmount - amount ); - writeItem( currItem ); + updateItemDb( currItem ); auto invUpdate = std::make_shared< UpdateInventorySlotPacket >( getId(), static_cast< uint8_t >( type ) - 1, @@ -523,8 +523,11 @@ void Sapphire::Entity::Player::writeInventory( InventoryType type ) db.execute( query ); } -void Sapphire::Entity::Player::writeItem( Sapphire::ItemPtr pItem ) const +void Sapphire::Entity::Player::updateItemDb( Sapphire::ItemPtr pItem ) const { + if( pItem->getUId() == 0 ) + writeItemDb( pItem ); + auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref(); auto stmt = db.getPreparedStatement( Db::CHARA_ITEMGLOBAL_UP ); @@ -540,6 +543,9 @@ void Sapphire::Entity::Player::writeItem( Sapphire::ItemPtr pItem ) const void Sapphire::Entity::Player::deleteItemDb( Sapphire::ItemPtr item ) const { + if( item->getUId() == 0 ) + return; + auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref(); auto stmt = db.getPreparedStatement( Db::CHARA_ITEMGLOBAL_DELETE ); @@ -619,7 +625,7 @@ Sapphire::ItemPtr Sapphire::Entity::Player::addItem( ItemPtr itemToAdd, bool sil itemToAdd->setStackSize( 0 ); item->setStackSize( newStackSize ); - writeItem( item ); + updateItemDb( item ); if( !silent ) { @@ -662,6 +668,8 @@ Sapphire::ItemPtr Sapphire::Entity::Player::addItem( ItemPtr itemToAdd, bool sil if( !foundFreeSlot ) return nullptr; + writeItemDb( itemToAdd ); + auto storage = m_storageMap[ freeBagSlot.first ]; storage->setItem( freeBagSlot.second, itemToAdd ); @@ -696,7 +704,7 @@ Sapphire::ItemPtr Sapphire::Entity::Player::addItem( uint32_t catalogId, uint32_ { if( catalogId == 0 ) return nullptr; - auto item = createItem( catalogId, quantity ); + auto item = createTempItem( catalogId, quantity ); item->setHq( isHq ); return addItem( item, silent, canMerge, sendLootMessage ); } @@ -799,7 +807,7 @@ void Sapphire::Entity::Player::splitItem( uint16_t fromInventoryId, uint8_t from updateContainer( fromInventoryId, fromSlotId, fromItem ); updateContainer( toInventoryId, toSlot, newItem ); - writeItem( fromItem ); + updateItemDb( fromItem ); } void Sapphire::Entity::Player::mergeItem( uint16_t fromInventoryId, uint8_t fromSlotId, @@ -826,14 +834,14 @@ void Sapphire::Entity::Player::mergeItem( uint16_t fromInventoryId, uint8_t from else { fromItem->setStackSize( stackOverflow ); - writeItem( fromItem ); + updateItemDb( fromItem ); + updateContainer( fromInventoryId, fromSlotId, fromItem ); } toItem->setStackSize( stackSize ); - writeItem( toItem ); + updateItemDb( toItem ); - updateContainer( fromInventoryId, fromSlotId, fromItem ); updateContainer( toInventoryId, toSlot, toItem ); } diff --git a/src/world/Actor/PlayerSql.cpp b/src/world/Actor/PlayerSql.cpp index 69df7a54..b1f056f1 100644 --- a/src/world/Actor/PlayerSql.cpp +++ b/src/world/Actor/PlayerSql.cpp @@ -618,11 +618,9 @@ void Sapphire::Entity::Player::insertQuest( uint16_t questId, uint8_t index, uin db.execute( stmt ); } -Sapphire::ItemPtr Sapphire::Entity::Player::createItem( uint32_t catalogId, uint32_t quantity ) +Sapphire::ItemPtr Sapphire::Entity::Player::createTempItem( uint32_t catalogId, uint32_t quantity ) { auto& exdData = Common::Service< Data::ExdDataGenerated >::ref(); - auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref(); - auto& itemMgr = Common::Service< World::Manager::ItemMgr >::ref(); auto itemInfo = exdData.get< Sapphire::Data::Item >( catalogId ); @@ -632,22 +630,32 @@ Sapphire::ItemPtr Sapphire::Entity::Player::createItem( uint32_t catalogId, uint if( !itemInfo ) return nullptr; - uint8_t flags = 0; - - ItemPtr pItem = make_Item( itemMgr.getNextUId(), catalogId ); + ItemPtr pItem = make_Item( 0, catalogId ); pItem->setStackSize( quantity ); - db.execute( "INSERT INTO charaglobalitem ( CharacterId, itemId, catalogId, stack, flags ) VALUES ( " + - std::to_string( getId() ) + ", " + - std::to_string( pItem->getUId() ) + ", " + - std::to_string( pItem->getId() ) + ", " + - std::to_string( quantity ) + ", " + - std::to_string( flags ) + ");" ); - return pItem; } +void Sapphire::Entity::Player::writeItemDb( Sapphire::ItemPtr pItem ) const +{ + if( pItem->getUId() == 0 ) + { + auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref(); + auto& itemMgr = Common::Service< World::Manager::ItemMgr >::ref(); + + uint8_t flags = 0; + pItem->setUId( itemMgr.getNextUId() ); + std::string sql = "INSERT INTO charaglobalitem ( CharacterId, itemId, catalogId, stack, flags ) VALUES ( " + + std::to_string( getId() ) + ", " + + std::to_string( pItem->getUId() ) + ", " + + std::to_string( pItem->getId() ) + ", " + + std::to_string( pItem->getStackSize() ) + ", " + + std::to_string( flags ) + ");"; + db.directExecute( sql ); + } +} + bool Sapphire::Entity::Player::loadInventory() { auto& itemMgr = Common::Service< World::Manager::ItemMgr >::ref();