1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-25 14:07:46 +00:00
sapphire/src/world/Manager/ShopMgr.cpp

103 lines
2.5 KiB
C++
Raw Normal View History

2018-11-24 15:17:18 +11:00
#include "ShopMgr.h"
#include <Exd/ExdData.h>
2018-11-24 15:17:18 +11:00
#include <Actor/Player.h>
#include <Inventory/Item.h>
2018-11-24 15:17:18 +11:00
#include <Common.h>
2020-03-01 01:00:57 +11:00
#include <Service.h>
2018-11-24 15:17:18 +11:00
using namespace Sapphire;
using namespace Sapphire::World::Manager;
2018-11-24 15:17:18 +11:00
void ShopMgr::cacheShop( uint32_t shopId )
2018-11-24 15:17:18 +11:00
{
auto& exdData = Common::Service< Data::ExdData >::ref();
2022-01-27 21:24:54 +01:00
auto itemShopList = exdData.getIdList< Excel::Shop >();
uint8_t count = 0;
for( auto itemShop : itemShopList )
{
if( shopId == itemShop )
{
2022-01-27 21:24:54 +01:00
auto shop = exdData.getRow< Excel::Shop >( itemShop );
for( auto shopItemId : shop->data().Item )
{
2022-01-27 21:24:54 +01:00
auto shopItem = exdData.getRow< Excel::ShopItem >( shopItemId );
if( !shopItem )
continue;
2022-01-27 21:24:54 +01:00
auto item = exdData.getRow< Excel::Item >( shopItem->data().ItemId );
if( !item || item->data().Price == 0 )
continue;
m_shopItemPrices[ shopId ][ count ] = item->data().Price;
count++;
}
Logger::debug( "ShopMgr: cached itemShop {0} with {1} items", shopId, count );
break;
}
}
}
2018-11-24 15:17:18 +11:00
uint32_t ShopMgr::getShopItemPrices( uint32_t shopId, uint8_t index )
{
if( index > 40 )
return 0;
auto it = m_shopItemPrices.find( shopId );
if( it != m_shopItemPrices.end() )
{
return it->second[ index ];
}
else
{
cacheShop( shopId );
return getShopItemPrices( shopId, index );
}
}
bool ShopMgr::purchaseGilShopItem( Entity::Player& player, uint32_t shopId, uint16_t itemId, uint32_t quantity )
{
auto& exdData = Common::Service< Data::ExdData >::ref();
2018-11-24 15:17:18 +11:00
2022-01-27 21:24:54 +01:00
auto item = exdData.getRow< Excel::Item >( itemId );
2018-11-24 15:17:18 +11:00
if( !item )
return false;
auto price = item->data().Price * quantity;
if( player.getCurrency( Common::CurrencyType::Gil ) < price )
2018-11-24 15:17:18 +11:00
return false;
if( !player.addItem( itemId, quantity ) )
2018-11-24 15:17:18 +11:00
return false;
player.removeCurrency( Common::CurrencyType::Gil, price );
2018-11-24 15:17:18 +11:00
return true;
}
bool ShopMgr::sellGilShopItem( Entity::Player& player, uint16_t container, uint8_t fromSlot, uint16_t itemId, uint32_t quantity )
{
auto& exdData = Common::Service< Data::ExdData >::ref();
2022-01-27 21:24:54 +01:00
auto item = exdData.getRow< Excel::Item >( itemId );
if( !item )
return false;
auto payback = ( item->data().Price ) * quantity;
auto inventoryItem = player.getItemAt( container, fromSlot );
// todo: adding stack remove
if( quantity > 1 )
return false;
player.discardItem( ( Common::InventoryType )container, fromSlot );
player.addSoldItem( itemId, quantity );
player.addCurrency( Common::CurrencyType::Gil, payback );
return true;
}