diff --git a/dGame/dComponents/Vendor/BaseVendorComponent.cpp b/dGame/dComponents/Vendor/BaseVendorComponent.cpp index 448f5d430..b03a11d3b 100644 --- a/dGame/dComponents/Vendor/BaseVendorComponent.cpp +++ b/dGame/dComponents/Vendor/BaseVendorComponent.cpp @@ -18,6 +18,7 @@ #include "InventoryMessages.h" #include "Item.h" #include "EconomyLedger.h" +#include "VendorPricing.h" #include #include @@ -284,15 +285,36 @@ void BaseVendorComponent::SellToVendor(Entity& player, const SystemAddress& sysA return; } - float sellScalar = GetSellScalar(); - if (Inventory::IsValidItem(itemComp.currencyLOT)) { - const auto altCurrency = static_cast(itemComp.altCurrencyCost * sellScalar) * count; - inv->AddItem(itemComp.currencyLOT, std::floor(altCurrency), eLootSourceType::VENDOR); // Return alt currencies like faction tokens. + // Only a count the stack holds can be sold, and an item the vendor already has can not be sold again + const auto* const itemInventory = item->GetInventory(); + if (!VendorPricing::IsValidStackCount(count, item->GetCount()) || !itemInventory || itemInventory->GetType() == eInventoryType::VENDOR_BUYBACK) { + SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_FAIL); + return; } - MakeRoomInBuyback(*inv, *item, static_cast(std::max(count, 0))); + const float sellScalar = GetSellScalar(); + const LOT soldLot = item->GetLot(); + const uint32_t heldBefore = itemInventory->GetLotCount(soldLot); + + MakeRoomInBuyback(*inv, *item, static_cast(count)); inv->MoveItemToInventory(item, eInventoryType::VENDOR_BUYBACK, count, true, false, true); - character->SetCoins(std::floor(character->GetCoins() + (static_cast(itemComp.baseValue * sellScalar) * count)), eLootSourceType::VENDOR, CoinSource::Object(*m_Parent)); + + // Pay for the items that left the inventory + const uint32_t heldAfter = itemInventory->GetLotCount(soldLot); + const uint32_t soldCount = heldBefore > heldAfter ? heldBefore - heldAfter : 0; + if (soldCount == 0) { + SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_FAIL); + return; + } + + if (Inventory::IsValidItem(itemComp.currencyLOT)) { + const uint64_t altCurrency = VendorPricing::GetPricePerItem(itemComp.altCurrencyCost, sellScalar, soldCount); + // Return alt currencies like faction tokens. + if (altCurrency > 0) inv->AddItem(itemComp.currencyLOT, static_cast(std::min(altCurrency, UINT32_MAX)), eLootSourceType::VENDOR); + } + + const uint64_t coins = VendorPricing::GetPricePerItem(itemComp.baseValue, sellScalar, soldCount); + character->SetCoins(character->GetCoins() + static_cast(coins), eLootSourceType::VENDOR, CoinSource::Object(*m_Parent)); SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_SUCCESS); } @@ -311,18 +333,25 @@ void BaseVendorComponent::BuybackFromVendor(Entity& player, const SystemAddress& int itemCompID = compRegistryTable->GetByIDAndType(item->GetLot(), eReplicaComponentType::ITEM); CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID); - float sellScalar = GetSellScalar(); + // Only an item the vendor holds for the player can be bought back, and only as many as there are + const auto* const itemInventory = item->GetInventory(); + if (!VendorPricing::IsValidStackCount(count, item->GetCount()) || !itemInventory || itemInventory->GetType() != eInventoryType::VENDOR_BUYBACK) { + SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL); + return; + } - const auto cost = static_cast(std::floor(((itemComp.baseValue * sellScalar) * count))); + const float sellScalar = GetSellScalar(); - if (character->GetCoins() < cost) { + const uint64_t cost = VendorPricing::GetPriceTotal(itemComp.baseValue, sellScalar, count); + + if (character->GetCoins() < 0 || static_cast(character->GetCoins()) < cost) { SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL); return; } if (Inventory::IsValidItem(itemComp.currencyLOT)) { - const uint32_t altCurrencyCost = std::floor(itemComp.altCurrencyCost * sellScalar) * count; - if (inv->GetLotCount(itemComp.currencyLOT) < altCurrencyCost || !inv->RemoveItem(itemComp.currencyLOT, altCurrencyCost, eInventoryType::ALL)) { + const uint64_t altCurrencyCost = VendorPricing::GetPricePerItem(itemComp.altCurrencyCost, sellScalar, count); + if (inv->GetLotCount(itemComp.currencyLOT) < altCurrencyCost || !inv->RemoveItem(itemComp.currencyLOT, static_cast(altCurrencyCost), eInventoryType::ALL)) { SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL); return; } @@ -330,7 +359,7 @@ void BaseVendorComponent::BuybackFromVendor(Entity& player, const SystemAddress& //inv->RemoveItem(count, -1, iObjID); inv->MoveItemToInventory(item, Inventory::FindInventoryTypeForLot(item->GetLot()), count, true, false); - character->SetCoins(character->GetCoins() - cost, eLootSourceType::VENDOR, CoinSource::Object(*m_Parent)); + character->SetCoins(character->GetCoins() - static_cast(cost), eLootSourceType::VENDOR, CoinSource::Object(*m_Parent)); //Game::entityManager->SerializeEntity(player); // so inventory updates SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_SUCCESS); } diff --git a/dGame/dUtilities/VendorPricing.h b/dGame/dUtilities/VendorPricing.h new file mode 100644 index 000000000..a6471e501 --- /dev/null +++ b/dGame/dUtilities/VendorPricing.h @@ -0,0 +1,46 @@ +#ifndef __VENDORPRICING__H__ +#define __VENDORPRICING__H__ + +#include +#include + +namespace VendorPricing { + // The most of one item a vendor sells at once, a full stack is 999 + constexpr uint32_t MaxBuyCount = 1000; + + /** + * Whether a count a player asked to move out of a stack is one the stack can give + */ + constexpr bool IsValidStackCount(const int32_t count, const uint32_t stackCount) { + return count > 0 && static_cast(count) <= stackCount; + } + + /** + * Whether a count a player asked to buy is one a vendor sells + */ + constexpr bool IsValidBuyCount(const uint32_t count) { + return count > 0 && count <= MaxBuyCount; + } + + /** + * The price of a number of items where each item's scaled price is rounded down on its own. + * A price is never negative. + */ + inline uint64_t GetPricePerItem(const uint32_t unitValue, const float scalar, const uint32_t count) { + const float unitPrice = std::floor(static_cast(unitValue) * scalar); + if (!(unitPrice > 0.0f)) return 0; + return static_cast(unitPrice) * count; + } + + /** + * The price of a number of items where the scaled price of all of them together is rounded down. + * A price is never negative. + */ + inline uint64_t GetPriceTotal(const uint32_t unitValue, const float scalar, const uint32_t count) { + const float price = std::floor((static_cast(unitValue) * scalar) * count); + if (!(price > 0.0f)) return 0; + return static_cast(price); + } +} + +#endif //!__VENDORPRICING__H__ diff --git a/tests/dGameTests/dGameMessagesTests/CMakeLists.txt b/tests/dGameTests/dGameMessagesTests/CMakeLists.txt index 2b5eec9b6..d304907d6 100644 --- a/tests/dGameTests/dGameMessagesTests/CMakeLists.txt +++ b/tests/dGameTests/dGameMessagesTests/CMakeLists.txt @@ -20,7 +20,8 @@ SET(DGAMEMESSAGES_TESTS "ClientMessagesTests.cpp" "LoadSequenceTests.cpp" "ChatRoutingTests.cpp" - "TradeOfferTests.cpp") + "TradeOfferTests.cpp" + "VendorPricingTests.cpp") # Get the folder name and prepend it to the files above get_filename_component(thisFolderName ${CMAKE_CURRENT_SOURCE_DIR} NAME) diff --git a/tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp b/tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp new file mode 100644 index 000000000..eee02d0c2 --- /dev/null +++ b/tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp @@ -0,0 +1,40 @@ +#include + +#include "VendorPricing.h" + +TEST(VendorPricingTest, AStackGivesWhatItHolds) { + EXPECT_TRUE(VendorPricing::IsValidStackCount(1, 1)); + EXPECT_TRUE(VendorPricing::IsValidStackCount(1, 5)); + EXPECT_TRUE(VendorPricing::IsValidStackCount(5, 5)); + EXPECT_FALSE(VendorPricing::IsValidStackCount(6, 5)); + EXPECT_FALSE(VendorPricing::IsValidStackCount(0, 5)); + EXPECT_FALSE(VendorPricing::IsValidStackCount(-1, 5)); + EXPECT_FALSE(VendorPricing::IsValidStackCount(INT32_MIN, 5)); + EXPECT_FALSE(VendorPricing::IsValidStackCount(INT32_MAX, 5)); + EXPECT_FALSE(VendorPricing::IsValidStackCount(1, 0)); +} + +TEST(VendorPricingTest, AVendorSellsUpToAFullStackAtOnce) { + EXPECT_TRUE(VendorPricing::IsValidBuyCount(1)); + EXPECT_TRUE(VendorPricing::IsValidBuyCount(999)); + EXPECT_FALSE(VendorPricing::IsValidBuyCount(0)); + EXPECT_FALSE(VendorPricing::IsValidBuyCount(1001)); + // What a negative count becomes + EXPECT_FALSE(VendorPricing::IsValidBuyCount(static_cast(-1))); +} + +TEST(VendorPricingTest, PricesRoundDown) { + // 15 * 0.1 is 1.5 an item + EXPECT_EQ(VendorPricing::GetPricePerItem(15, 0.1f, 3), 3); + EXPECT_EQ(VendorPricing::GetPriceTotal(15, 0.1f, 3), 4); + EXPECT_EQ(VendorPricing::GetPricePerItem(100, 1.0f, 7), 700); + EXPECT_EQ(VendorPricing::GetPriceTotal(100, 1.0f, 7), 700); + EXPECT_EQ(VendorPricing::GetPricePerItem(100, 0.0f, 7), 0); + EXPECT_EQ(VendorPricing::GetPriceTotal(100, -1.0f, 7), 0); +} + +TEST(VendorPricingTest, LargePricesDoNotWrap) { + EXPECT_EQ(VendorPricing::GetPricePerItem(4000000, 1.0f, 2000), 8000000000ULL); + EXPECT_EQ(VendorPricing::GetPriceTotal(8388608, 1.0f, 1000), 8388608000ULL); + EXPECT_GT(VendorPricing::GetPricePerItem(UINT32_MAX, 1.0f, VendorPricing::MaxBuyCount), static_cast(UINT32_MAX)); +}