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-11-29 16:55:48 +01:00
|
|
|
extern Sapphire::Framework g_fw;
|
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-07 23:34:45 +11:00
|
|
|
bool isMultiStorage, bool isPersistentStorage ) :
|
2018-08-29 21:40:59 +02:00
|
|
|
m_id( storageId ),
|
|
|
|
m_size( maxSize ),
|
|
|
|
m_tableName( tableName ),
|
|
|
|
m_bMultiStorage( isMultiStorage ),
|
|
|
|
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-05 21:29:33 +11:00
|
|
|
void Sapphire::ItemContainer::removeItem( uint16_t slotId )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
auto pLog = g_fw.get< Logger >();
|
2018-09-09 23:56:22 +02:00
|
|
|
auto pDb = g_fw.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() )
|
|
|
|
{
|
|
|
|
if( m_isPersistentStorage )
|
|
|
|
pDb->execute( "DELETE FROM charaglobalitem WHERE itemId = " + std::to_string( it->second->getUId() ) );
|
|
|
|
|
|
|
|
m_itemMap.erase( it );
|
|
|
|
|
|
|
|
pLog->debug( "Dropped item from slot " + std::to_string( slotId ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pLog->debug( "Item could not be dropped from slot " + std::to_string( slotId ) );
|
|
|
|
}
|
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 ) )
|
|
|
|
{
|
|
|
|
auto pLog = g_fw.get< Logger >();
|
|
|
|
pLog->error( "Slot out of range " + std::to_string( slotId ) );
|
|
|
|
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-08-29 21:40:59 +02:00
|
|
|
return m_isPersistentStorage;
|
2018-08-12 22:53:21 +10:00
|
|
|
}
|
|
|
|
|
2018-07-25 14:03:43 +02:00
|
|
|
|