2018-11-24 15:17:18 +11:00
|
|
|
#include "ShopMgr.h"
|
|
|
|
|
2021-11-27 00:53:57 +01:00
|
|
|
#include <Exd/ExdData.h>
|
2018-11-24 15:17:18 +11:00
|
|
|
#include <Actor/Player.h>
|
2021-11-27 00:53:57 +01:00
|
|
|
#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
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
using namespace Sapphire;
|
2022-01-27 21:08:43 +01:00
|
|
|
using namespace Sapphire::World::Manager;
|
2018-11-24 15:17:18 +11:00
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
void ShopMgr::cacheShop( uint32_t shopId )
|
2018-11-24 15:17:18 +11:00
|
|
|
{
|
2021-11-27 00:53:57 +01:00
|
|
|
auto& exdData = Common::Service< Data::ExdData >::ref();
|
2022-01-27 21:24:54 +01:00
|
|
|
auto itemShopList = exdData.getIdList< Excel::Shop >();
|
2021-11-27 00:53:57 +01:00
|
|
|
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 );
|
2021-11-27 00:53:57 +01:00
|
|
|
for( auto shopItemId : shop->data().Item )
|
|
|
|
{
|
2022-01-27 21:24:54 +01:00
|
|
|
auto shopItem = exdData.getRow< Excel::ShopItem >( shopItemId );
|
2021-11-27 00:53:57 +01:00
|
|
|
if( !shopItem )
|
|
|
|
continue;
|
|
|
|
|
2022-01-27 21:24:54 +01:00
|
|
|
auto item = exdData.getRow< Excel::Item >( shopItem->data().ItemId );
|
2021-11-27 00:53:57 +01:00
|
|
|
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
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
uint32_t ShopMgr::getShopItemPrices( uint32_t shopId, uint8_t index )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
bool ShopMgr::purchaseGilShopItem( Entity::Player& player, uint32_t shopId, uint16_t itemId, uint32_t quantity )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2021-11-27 00:53:57 +01:00
|
|
|
auto price = item->data().Price * quantity;
|
2018-12-04 22:27:54 +11:00
|
|
|
|
|
|
|
if( player.getCurrency( Common::CurrencyType::Gil ) < price )
|
2018-11-24 15:17:18 +11:00
|
|
|
return false;
|
|
|
|
|
2021-11-27 00:53:57 +01:00
|
|
|
if( !player.addItem( itemId, quantity ) )
|
2018-11-24 15:17:18 +11:00
|
|
|
return false;
|
|
|
|
|
2018-12-04 22:27:54 +11:00
|
|
|
player.removeCurrency( Common::CurrencyType::Gil, price );
|
2018-11-24 15:17:18 +11:00
|
|
|
|
|
|
|
return true;
|
2018-12-22 22:25:03 +01:00
|
|
|
}
|
2020-05-11 06:25:25 +09:00
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
bool ShopMgr::sellGilShopItem( Entity::Player& player, uint16_t container, uint8_t fromSlot, uint16_t itemId, uint32_t quantity )
|
2020-05-11 06:25:25 +09:00
|
|
|
{
|
2021-11-27 00:53:57 +01:00
|
|
|
auto& exdData = Common::Service< Data::ExdData >::ref();
|
2020-05-11 06:25:25 +09:00
|
|
|
|
2022-01-27 21:24:54 +01:00
|
|
|
auto item = exdData.getRow< Excel::Item >( itemId );
|
2021-11-27 00:53:57 +01:00
|
|
|
if( !item )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto payback = ( item->data().Price ) * quantity;
|
|
|
|
|
|
|
|
auto inventoryItem = player.getItemAt( container, fromSlot );
|
|
|
|
|
|
|
|
// todo: adding stack remove
|
2022-01-23 11:04:26 +01:00
|
|
|
if( quantity > 1 )
|
2021-11-27 00:53:57 +01:00
|
|
|
return false;
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
player.discardItem( ( Common::InventoryType )container, fromSlot );
|
2021-11-27 00:53:57 +01:00
|
|
|
player.addSoldItem( itemId, quantity );
|
|
|
|
|
|
|
|
player.addCurrency( Common::CurrencyType::Gil, payback );
|
|
|
|
|
|
|
|
return true;
|
2020-05-11 06:25:25 +09:00
|
|
|
}
|