2018-03-06 22:22:19 +01:00
|
|
|
#include <Common.h>
|
|
|
|
#include <Logging/Logger.h>
|
|
|
|
#include <Database/DatabaseDef.h>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2017-12-08 15:38:25 +01:00
|
|
|
#include "Actor/Player.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
#include "Item.h"
|
2018-03-02 07:22:25 -03:00
|
|
|
#include "Framework.h"
|
|
|
|
#include "Forwards.h"
|
|
|
|
#include "ItemContainer.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-05 21:29:33 +11:00
|
|
|
Sapphire::ItemContainer::ItemContainer( uint16_t storageId, uint16_t maxSize, const std::string& tableName,
|
2018-12-29 00:53:52 +01:00
|
|
|
bool isMultiStorage, FrameworkPtr pFw, bool isPersistentStorage ) :
|
2018-08-29 21:40:59 +02:00
|
|
|
m_id( storageId ),
|
|
|
|
m_size( maxSize ),
|
|
|
|
m_tableName( tableName ),
|
|
|
|
m_bMultiStorage( isMultiStorage ),
|
2018-12-29 00:53:52 +01:00
|
|
|
m_pFw( pFw ),
|
2018-08-29 21:40:59 +02:00
|
|
|
m_isPersistentStorage( isPersistentStorage )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
Sapphire::ItemContainer::~ItemContainer()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
uint16_t Sapphire::ItemContainer::getId() const
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_id;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 21:29:33 +11:00
|
|
|
uint16_t Sapphire::ItemContainer::getEntryCount() const
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-12-05 21:29:33 +11:00
|
|
|
return static_cast< uint16_t >( m_itemMap.size() );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 21:51:43 +11:00
|
|
|
void Sapphire::ItemContainer::removeItem( uint16_t slotId, bool removeFromDb )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-12-29 00:53:52 +01:00
|
|
|
auto pDb = m_pFw->get< Db::DbWorkerPool< Db::ZoneDbConnection > >();
|
2018-08-29 21:40:59 +02:00
|
|
|
ItemMap::iterator it = m_itemMap.find( slotId );
|
|
|
|
|
|
|
|
if( it != m_itemMap.end() )
|
|
|
|
{
|
2018-12-29 21:51:43 +11:00
|
|
|
if( m_isPersistentStorage && removeFromDb )
|
2018-08-29 21:40:59 +02:00
|
|
|
pDb->execute( "DELETE FROM charaglobalitem WHERE itemId = " + std::to_string( it->second->getUId() ) );
|
|
|
|
|
|
|
|
m_itemMap.erase( it );
|
|
|
|
|
2019-01-04 22:37:01 +11:00
|
|
|
Logger::debug( "Dropped item from slot {0}", slotId );
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-04 22:37:01 +11:00
|
|
|
Logger::debug( "Item could not be dropped from slot {0}", slotId );
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
Sapphire::ItemMap& Sapphire::ItemContainer::getItemMap()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_itemMap;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
const Sapphire::ItemMap& Sapphire::ItemContainer::getItemMap() const
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_itemMap;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 21:29:33 +11:00
|
|
|
int16_t Sapphire::ItemContainer::getFreeSlot()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-12-05 21:29:33 +11:00
|
|
|
for( uint16_t slotId = 0; slotId < m_size; slotId++ )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
|
|
|
ItemMap::iterator it = m_itemMap.find( slotId );
|
|
|
|
if( it == m_itemMap.end() ||
|
|
|
|
it->second == nullptr )
|
|
|
|
return slotId;
|
|
|
|
}
|
|
|
|
return -1;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 21:29:33 +11:00
|
|
|
Sapphire::ItemPtr Sapphire::ItemContainer::getItem( uint16_t slotId )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( ( slotId > m_size ) )
|
|
|
|
{
|
2019-01-04 22:37:01 +11:00
|
|
|
Logger::error( "Slot out of range {0}", slotId );
|
2018-08-29 21:40:59 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_itemMap[ slotId ];
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 21:29:33 +11:00
|
|
|
void Sapphire::ItemContainer::setItem( uint16_t slotId, ItemPtr pItem )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( slotId > m_size )
|
|
|
|
return;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
m_itemMap[ slotId ] = pItem;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
2018-07-25 14:03:43 +02:00
|
|
|
|
2018-12-05 21:29:33 +11:00
|
|
|
uint16_t Sapphire::ItemContainer::getMaxSize() const
|
2018-07-25 14:03:43 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_size;
|
2018-07-25 14:03:43 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
std::string Sapphire::ItemContainer::getTableName() const
|
2018-07-25 14:03:43 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_tableName;
|
2018-07-25 14:03:43 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
bool Sapphire::ItemContainer::isMultiStorage() const
|
2018-07-25 14:03:43 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_bMultiStorage;
|
2018-07-25 14:03:43 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
bool Sapphire::ItemContainer::isPersistentStorage() const
|
2018-08-12 22:53:21 +10:00
|
|
|
{
|
2018-12-29 13:05:13 +11:00
|
|
|
return m_isPersistentStorage;
|
2018-08-12 22:53:21 +10:00
|
|
|
}
|
|
|
|
|
2018-07-25 14:03:43 +02:00
|
|
|
|