From 791f934a2dbe9e4019a8e976fcf83d27cf0aa31d Mon Sep 17 00:00:00 2001 From: NotAdam Date: Thu, 26 Jul 2018 23:12:02 +1000 Subject: [PATCH] fix loading of currency/crystal items --- .../sapphire_zone/Actor/PlayerInventory.cpp | 84 ++++--------------- src/servers/sapphire_zone/Actor/PlayerSql.cpp | 63 +------------- 2 files changed, 20 insertions(+), 127 deletions(-) diff --git a/src/servers/sapphire_zone/Actor/PlayerInventory.cpp b/src/servers/sapphire_zone/Actor/PlayerInventory.cpp index 23220637..c1161064 100644 --- a/src/servers/sapphire_zone/Actor/PlayerInventory.cpp +++ b/src/servers/sapphire_zone/Actor/PlayerInventory.cpp @@ -58,10 +58,10 @@ void Core::Entity::Player::initInventory() setupContainer( GearSet0, 13, "charaitemgearset", true ); // gil contianer - setupContainer( Currency, 11, "charaitemcurrency", false ); + setupContainer( Currency, 11, "charaiteminventory", false ); // crystals?? - setupContainer( Crystal, 11, "charaitemcrystal", false ); + setupContainer( Crystal, 11, "charaiteminventory", false ); // armory weapons - 0 setupContainer( ArmoryMain, 34, "charaiteminventory", true ); @@ -219,20 +219,22 @@ void Core::Entity::Player::unequipItem( Common::EquipSlot equipSlotId, ItemPtr p // TODO: these next functions are so similar that they could likely be simplified void Core::Entity::Player::addCurrency( CurrencyType type, uint32_t amount ) { - auto currItem = m_inventoryMap[Currency]->getItem( static_cast< uint8_t >( type ) - 1 ); + auto slot = static_cast< uint8_t >( static_cast< uint8_t >( type ) - 1 ); + auto currItem = m_inventoryMap[Currency]->getItem( slot ); if( !currItem ) { // TODO: map currency type to itemid currItem = createItem( 1 ); - m_inventoryMap[Currency]->setItem( static_cast< uint8_t >( type ) - 1, currItem ); - updateCurrencyDb(); + m_inventoryMap[Currency]->setItem( slot, currItem ); } uint32_t currentAmount = currItem->getStackSize(); currItem->setStackSize( currentAmount + amount ); updateItemDb( currItem ); + updateContainer( Currency, slot, currItem ); + auto invUpdate = boost::make_shared< UpdateInventorySlotPacket >( getId(), static_cast< uint8_t >( type ) - 1, Common::InventoryType::Currency, @@ -272,7 +274,6 @@ void Core::Entity::Player::addCrystal( Common::CrystalType type, uint32_t amount // TODO: map currency type to itemid currItem = createItem( static_cast< uint8_t >( type ) + 1 ); m_inventoryMap[Crystal]->setItem( static_cast< uint8_t >( type ) - 1, currItem ); - updateCrystalDb(); } uint32_t currentAmount = currItem->getStackSize(); @@ -281,6 +282,8 @@ void Core::Entity::Player::addCrystal( Common::CrystalType type, uint32_t amount updateItemDb( currItem ); + updateBagDb( Crystal ); + auto invUpdate = boost::make_shared< UpdateInventorySlotPacket >( getId(), static_cast< uint8_t >( type ) - 1, @@ -435,69 +438,16 @@ uint32_t Core::Entity::Player::getCrystal( CrystalType type ) } -void Core::Entity::Player::updateCurrencyDb() -{ - auto pDb = g_fw.get< Db::DbWorkerPool< Db::CharaDbConnection > >(); - int32_t firstItemPos = -1; - std::string query = "UPDATE charaitemcurrency SET "; - - for( int32_t i = 0; i <= 11; i++ ) - { - auto currItem = m_inventoryMap[Currency]->getItem( i ); - - if( currItem ) - { - if( firstItemPos == -1 ) - firstItemPos = i; - - if( i > firstItemPos ) - query += ", "; - - query += "container_" + std::to_string( i ) + " = " + std::to_string( currItem->getUId() ); - } - } - - query += " WHERE CharacterId = " + std::to_string( getId() ); - - pDb->execute( query ); -} - - -void Core::Entity::Player::updateCrystalDb() -{ - auto pDb = g_fw.get< Db::DbWorkerPool< Db::CharaDbConnection > >(); - int32_t firstItemPos = -1; - std::string query = "UPDATE charaitemcrystal SET "; - - for( int32_t i = 0; i <= 11; i++ ) - { - auto currItem = m_inventoryMap[Crystal]->getItem( i ); - - if( currItem ) - { - if( firstItemPos == -1 ) - firstItemPos = i; - - if( i > firstItemPos ) - query += ", "; - - query += "container_" + std::to_string( i ) + " = " + std::to_string( currItem->getUId() ); - } - } - - query += " WHERE CharacterId = " + std::to_string( getId() ); - - pDb->execute( query ); -} - void Core::Entity::Player::updateBagDb( InventoryType type ) { auto pDb = g_fw.get< Db::DbWorkerPool< Db::CharaDbConnection > >(); std::string query = "UPDATE charaiteminventory SET "; - for( int32_t i = 0; i <= 34; i++ ) + auto container = m_inventoryMap[type]; + + for( int32_t i = 0; i <= container->getMaxSize(); i++ ) { - auto currItem = m_inventoryMap[type]->getItem( i ); + auto currItem = container->getItem( i ); if( i > 0 ) query += ", "; @@ -518,9 +468,11 @@ void Core::Entity::Player::updateMannequinDb( InventoryType type ) auto pDb = g_fw.get< Db::DbWorkerPool< Db::CharaDbConnection > >(); std::string query = "UPDATE charaitemgearset SET "; - for( int32_t i = 0; i <= 13; i++ ) + auto container = m_inventoryMap[type]; + + for( int32_t i = 0; i <= container->getMaxSize(); i++ ) { - auto currItem = m_inventoryMap[type]->getItem( i ); + auto currItem = container->getItem( i ); if( i > 0 ) query += ", "; @@ -661,8 +613,8 @@ bool Core::Entity::Player::updateContainer( uint16_t containerId, uint8_t slotId switch( containerType ) { case Armory: - case CurrencyCrystal: case Bag: + case CurrencyCrystal: { updateBagDb( static_cast< InventoryType >( containerId ) ); break; diff --git a/src/servers/sapphire_zone/Actor/PlayerSql.cpp b/src/servers/sapphire_zone/Actor/PlayerSql.cpp index bed620ca..98d897de 100644 --- a/src/servers/sapphire_zone/Actor/PlayerSql.cpp +++ b/src/servers/sapphire_zone/Actor/PlayerSql.cpp @@ -627,7 +627,7 @@ bool Core::Entity::Player::loadInventory() } /////////////////////////////////////////////////////////////////////////////////////////////////////// - // Load Bags + // Load everything auto bagRes = pDb->query( "SELECT storageId, " "container_0, container_1, container_2, container_3, container_4, " "container_5, container_6, container_7, container_8, container_9, " @@ -643,7 +643,7 @@ bool Core::Entity::Player::loadInventory() while( bagRes->next() ) { uint16_t storageId = bagRes->getUInt16( 1 ); - for( uint32_t i = 1; i <= 35; i++ ) + for( uint32_t i = 1; i <= m_inventoryMap[storageId]->getMaxSize(); i++ ) { uint64_t uItemId = bagRes->getUInt64( i + 1 ); if( uItemId == 0 ) @@ -658,64 +658,5 @@ bool Core::Entity::Player::loadInventory() } } - - /////////////////////////////////////////////////////////////////////////////////////////////////////// - // Load Currency - auto curRes = pDb->query( "SELECT storageId, " - "container_0, container_1, container_2, container_3, container_4, " - "container_5, container_6, container_7, container_8, container_9, " - "container_10, container_11 " - "FROM charaitemcurrency " \ - "WHERE CharacterId = " + std::to_string( getId() ) + " " \ - "ORDER BY storageId ASC;" ); - - while( curRes->next() ) - { - uint16_t storageId = curRes->getUInt16( 1 ); - for( uint32_t i = 1; i <= 12; i++ ) - { - uint64_t uItemId = curRes->getUInt64( i + 1 ); - if( uItemId == 0 ) - continue; - - ItemPtr pItem = Items::Util::loadItem( uItemId ); - - if( pItem == nullptr ) - continue; - - m_inventoryMap[storageId]->getItemMap()[i - 1] = pItem; - } - } - - - /////////////////////////////////////////////////////////////////////////////////////////////////////// - // Load Crystals - auto crystalRes = pDb->query( "SELECT storageId, " - "container_0, container_1, container_2, container_3, container_4, " - "container_5, container_6, container_7, container_8, container_9, " - "container_10, container_11, container_12, container_13, container_14, " - "container_15, container_16, container_17 " - "FROM charaitemcrystal " \ - "WHERE CharacterId = " + std::to_string( getId() ) + " " \ - "ORDER BY storageId ASC;" ); - - while( crystalRes->next() ) - { - uint16_t storageId = crystalRes->getUInt16( 1 ); - for( int32_t i = 1; i <= 17; i++ ) - { - uint64_t uItemId = crystalRes->getUInt64( i + 1 ); - if( uItemId == 0 ) - continue; - - ItemPtr pItem = Items::Util::loadItem( uItemId ); - - if( pItem == nullptr ) - continue; - - m_inventoryMap[storageId]->getItemMap()[i - 1] = pItem; - } - } - return true; }