1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 14:37:44 +00:00

inventory fix

This commit is contained in:
collett 2020-06-28 09:27:00 +09:00
parent bd2abe807e
commit d887ecdebe
4 changed files with 48 additions and 30 deletions

View file

@ -1988,7 +1988,7 @@ void Sapphire::Entity::Player::dyeItemFromDyeingInfo()
// TODO: subtract/remove dye used // TODO: subtract/remove dye used
insertInventoryItem( static_cast< Sapphire::Common::InventoryType >( itemToDyeContainer ), static_cast< uint16_t >( itemToDyeSlot ), itemToDye ); insertInventoryItem( static_cast< Sapphire::Common::InventoryType >( itemToDyeContainer ), static_cast< uint16_t >( itemToDyeSlot ), itemToDye );
writeItem( itemToDye ); updateItemDb( itemToDye );
} }
void Sapphire::Entity::Player::resetObjSpawnIndex() void Sapphire::Entity::Player::resetObjSpawnIndex()

View file

@ -920,8 +920,6 @@ namespace Sapphire::Entity
using InvSlotPair = std::pair< uint16_t, int8_t >; using InvSlotPair = std::pair< uint16_t, int8_t >;
using InvSlotPairVec = std::vector< InvSlotPair >; using InvSlotPairVec = std::vector< InvSlotPair >;
ItemPtr createItem( uint32_t catalogId, uint32_t quantity = 1 );
bool loadInventory(); bool loadInventory();
InvSlotPairVec getSlotsOfItemsInInventory( uint32_t catalogId ); InvSlotPairVec getSlotsOfItemsInInventory( uint32_t catalogId );
@ -956,7 +954,11 @@ namespace Sapphire::Entity
void writeInventory( Common::InventoryType type ); 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; void deleteItemDb( ItemPtr pItem ) const;

View file

@ -306,13 +306,13 @@ void Sapphire::Entity::Player::addCurrency( CurrencyType type, uint32_t amount,
if( !currItem ) if( !currItem )
{ {
// TODO: map currency type to itemid // TODO: map currency type to itemid
currItem = createItem( 1 ); currItem = createTempItem( 1 );
m_storageMap[ Currency ]->setItem( slot, currItem ); m_storageMap[ Currency ]->setItem( slot, currItem );
} }
uint32_t currentAmount = currItem->getStackSize(); uint32_t currentAmount = currItem->getStackSize();
currItem->setStackSize( currentAmount + amount ); currItem->setStackSize( currentAmount + amount );
writeItem( currItem ); updateItemDb( currItem );
updateContainer( Currency, slot, currItem ); updateContainer( Currency, slot, currItem );
@ -352,7 +352,7 @@ void Sapphire::Entity::Player::removeCurrency( Common::CurrencyType type, uint32
currItem->setStackSize( 0 ); currItem->setStackSize( 0 );
else else
currItem->setStackSize( currentAmount - amount ); currItem->setStackSize( currentAmount - amount );
writeItem( currItem ); updateItemDb( currItem );
auto invUpdate = std::make_shared< UpdateInventorySlotPacket >( getId(), auto invUpdate = std::make_shared< UpdateInventorySlotPacket >( getId(),
static_cast< uint8_t >( type ) - 1, static_cast< uint8_t >( type ) - 1,
@ -369,7 +369,7 @@ void Sapphire::Entity::Player::addCrystal( Common::CrystalType type, uint32_t am
if( !currItem ) if( !currItem )
{ {
// TODO: map currency type to itemid // 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 ); 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 ); currItem->setStackSize( currentAmount + amount );
writeItem( currItem ); updateItemDb( currItem );
writeInventory( Crystal ); writeInventory( Crystal );
@ -417,7 +417,7 @@ void Sapphire::Entity::Player::removeCrystal( Common::CrystalType type, uint32_t
else else
currItem->setStackSize( currentAmount - amount ); currItem->setStackSize( currentAmount - amount );
writeItem( currItem ); updateItemDb( currItem );
auto invUpdate = std::make_shared< UpdateInventorySlotPacket >( getId(), auto invUpdate = std::make_shared< UpdateInventorySlotPacket >( getId(),
static_cast< uint8_t >( type ) - 1, static_cast< uint8_t >( type ) - 1,
@ -523,8 +523,11 @@ void Sapphire::Entity::Player::writeInventory( InventoryType type )
db.execute( query ); 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& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref();
auto stmt = db.getPreparedStatement( Db::CHARA_ITEMGLOBAL_UP ); 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 void Sapphire::Entity::Player::deleteItemDb( Sapphire::ItemPtr item ) const
{ {
if( item->getUId() == 0 )
return;
auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref(); auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref();
auto stmt = db.getPreparedStatement( Db::CHARA_ITEMGLOBAL_DELETE ); 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 ); itemToAdd->setStackSize( 0 );
item->setStackSize( newStackSize ); item->setStackSize( newStackSize );
writeItem( item ); updateItemDb( item );
if( !silent ) if( !silent )
{ {
@ -662,6 +668,8 @@ Sapphire::ItemPtr Sapphire::Entity::Player::addItem( ItemPtr itemToAdd, bool sil
if( !foundFreeSlot ) if( !foundFreeSlot )
return nullptr; return nullptr;
writeItemDb( itemToAdd );
auto storage = m_storageMap[ freeBagSlot.first ]; auto storage = m_storageMap[ freeBagSlot.first ];
storage->setItem( freeBagSlot.second, itemToAdd ); storage->setItem( freeBagSlot.second, itemToAdd );
@ -696,7 +704,7 @@ Sapphire::ItemPtr Sapphire::Entity::Player::addItem( uint32_t catalogId, uint32_
{ {
if( catalogId == 0 ) if( catalogId == 0 )
return nullptr; return nullptr;
auto item = createItem( catalogId, quantity ); auto item = createTempItem( catalogId, quantity );
item->setHq( isHq ); item->setHq( isHq );
return addItem( item, silent, canMerge, sendLootMessage ); 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( fromInventoryId, fromSlotId, fromItem );
updateContainer( toInventoryId, toSlot, newItem ); updateContainer( toInventoryId, toSlot, newItem );
writeItem( fromItem ); updateItemDb( fromItem );
} }
void Sapphire::Entity::Player::mergeItem( uint16_t fromInventoryId, uint8_t fromSlotId, 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 else
{ {
fromItem->setStackSize( stackOverflow ); fromItem->setStackSize( stackOverflow );
writeItem( fromItem ); updateItemDb( fromItem );
updateContainer( fromInventoryId, fromSlotId, fromItem );
} }
toItem->setStackSize( stackSize ); toItem->setStackSize( stackSize );
writeItem( toItem ); updateItemDb( toItem );
updateContainer( fromInventoryId, fromSlotId, fromItem );
updateContainer( toInventoryId, toSlot, toItem ); updateContainer( toInventoryId, toSlot, toItem );
} }

View file

@ -618,11 +618,9 @@ void Sapphire::Entity::Player::insertQuest( uint16_t questId, uint8_t index, uin
db.execute( stmt ); 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& 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 ); auto itemInfo = exdData.get< Sapphire::Data::Item >( catalogId );
@ -632,22 +630,32 @@ Sapphire::ItemPtr Sapphire::Entity::Player::createItem( uint32_t catalogId, uint
if( !itemInfo ) if( !itemInfo )
return nullptr; return nullptr;
uint8_t flags = 0; ItemPtr pItem = make_Item( 0, catalogId );
ItemPtr pItem = make_Item( itemMgr.getNextUId(), catalogId );
pItem->setStackSize( quantity ); 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; 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() bool Sapphire::Entity::Player::loadInventory()
{ {
auto& itemMgr = Common::Service< World::Manager::ItemMgr >::ref(); auto& itemMgr = Common::Service< World::Manager::ItemMgr >::ref();