diff --git a/dGame/dComponents/Vendor/BaseVendorComponent.cpp b/dGame/dComponents/Vendor/BaseVendorComponent.cpp index 5cd26e6a0..ec8ab52e5 100644 --- a/dGame/dComponents/Vendor/BaseVendorComponent.cpp +++ b/dGame/dComponents/Vendor/BaseVendorComponent.cpp @@ -192,45 +192,41 @@ void BaseVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) { return; } - // Extra currency that needs to be deducted in case of crafting - auto craftingCurrencies = CDItemComponentTable::ParseCraftingCurrencies(itemComp); - for (const auto& [crafintCurrencyLOT, crafintCurrencyCount]: craftingCurrencies) { - if (inventoryComponent->GetLotCount(crafintCurrencyLOT) < (static_cast(crafintCurrencyCount) * count)) { - SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); - return; - } - } - - bool success = true; - for (const auto& [crafintCurrencyLOT, crafintCurrencyCount]: craftingCurrencies) { - success = inventoryComponent->RemoveItem(crafintCurrencyLOT, crafintCurrencyCount * count, eInventoryType::ALL); - if (!success) break; - } - - if (!success) { - SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); - return; - } - float buyScalar = GetBuyScalar(); - const uint64_t coinCost = VendorPricing::GetPriceTotal(itemComp.baseValue, buyScalar, count); + VendorPricing::BuyCost cost{}; + cost.coins = VendorPricing::GetPriceTotal(itemComp.baseValue, buyScalar, count); + if (Inventory::IsValidItem(itemComp.currencyLOT)) { + cost.AddItem(itemComp.currencyLOT, VendorPricing::GetPricePerItem(itemComp.altCurrencyCost, buyScalar, count)); + } + // Extra items taken when the item is crafted + for (const auto& [craftingCurrencyLOT, craftingCurrencyCount] : CDItemComponentTable::ParseCraftingCurrencies(itemComp)) { + cost.AddItem(craftingCurrencyLOT, static_cast(craftingCurrencyCount) * count); + } Character* character = buyer->GetCharacter(); - if (!character || character->GetCoins() < 0 || static_cast(character->GetCoins()) < coinCost) { + if (!character) { SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); return; } - if (Inventory::IsValidItem(itemComp.currencyLOT)) { - const uint64_t altCurrencyCost = VendorPricing::GetPricePerItem(itemComp.altCurrencyCost, buyScalar, count); - if (inventoryComponent->GetLotCount(itemComp.currencyLOT) < altCurrencyCost) { - SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); - return; + // Every cost is checked before any is taken, so a player short on one keeps all of them + struct Payer { + Character& character; + InventoryComponent& inventory; + Entity& vendor; + int64_t GetCoins() const { return character.GetCoins(); } + uint32_t GetLotCount(const LOT lot) const { return inventory.GetLotCount(lot); } + void TakeItem(const LOT lot, const uint64_t amount) { inventory.RemoveItem(lot, static_cast(amount), eInventoryType::ALL); } + void TakeCoins(const uint64_t amount) { + character.SetCoins(character.GetCoins() - static_cast(amount), eLootSourceType::VENDOR, CoinSource::Object(vendor)); } - inventoryComponent->RemoveItem(itemComp.currencyLOT, static_cast(altCurrencyCost), eInventoryType::ALL); + } payer{ *character, *inventoryComponent, *m_Parent }; + if (!VendorPricing::TryPay(cost, payer)) { + SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); + return; } + const uint64_t coinCost = cost.coins; - character->SetCoins(character->GetCoins() - static_cast(coinCost), eLootSourceType::VENDOR, CoinSource::Object(*m_Parent)); // Live sent the vendor's LOT, and the coins paid when there were any, with the bought items LootMetrics metrics{}; metrics.sourceLot = m_Parent->GetLOT(); diff --git a/dGame/dUtilities/VendorPricing.h b/dGame/dUtilities/VendorPricing.h index 23b9e5294..27ca84038 100644 --- a/dGame/dUtilities/VendorPricing.h +++ b/dGame/dUtilities/VendorPricing.h @@ -3,6 +3,7 @@ #include #include +#include namespace VendorPricing { // The most of one item a vendor sells at once, the count the client's buy dialog stops at @@ -41,6 +42,45 @@ namespace VendorPricing { if (!(price > 0.0f)) return 0; return static_cast(price); } + + /** + * Everything one purchase costs: coins, and a count of each item LOT taken from the inventory + */ + struct BuyCost { + uint64_t coins = 0; + std::map items; + + // Costs that name the same LOT add up, so the check sees the whole need + void AddItem(const int32_t lot, const uint64_t amount) { + if (amount == 0) return; + items[lot] += amount; + } + }; + + /** + * Whether the payer has every cost. Payer needs GetCoins() and GetLotCount(lot). + */ + template + bool CanPay(const BuyCost& cost, const Payer& payer) { + const int64_t coins = payer.GetCoins(); + if (coins < 0 || static_cast(coins) < cost.coins) return false; + for (const auto& [lot, amount] : cost.items) { + if (static_cast(payer.GetLotCount(lot)) < amount) return false; + } + return true; + } + + /** + * Checks every cost first and takes them only when all are there, items before coins. + * Payer also needs TakeItem(lot, amount) and TakeCoins(amount). Returns false with nothing taken. + */ + template + bool TryPay(const BuyCost& cost, Payer& payer) { + if (!CanPay(cost, payer)) return false; + for (const auto& [lot, amount] : cost.items) payer.TakeItem(lot, amount); + payer.TakeCoins(cost.coins); + return true; + } } #endif //!__VENDORPRICING__H__ diff --git a/tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp b/tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp index 28c2f9790..2071af874 100644 --- a/tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp +++ b/tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp @@ -38,3 +38,87 @@ TEST(VendorPricingTest, LargePricesDoNotWrap) { EXPECT_EQ(VendorPricing::GetPriceTotal(8388608, 1.0f, 1000), 8388608000ULL); EXPECT_GT(VendorPricing::GetPricePerItem(UINT32_MAX, 1.0f, VendorPricing::MaxBuyCount), static_cast(UINT32_MAX)); } + +namespace { + struct FakePayer { + int64_t coins = 0; + std::map items; + int64_t GetCoins() const { return coins; } + uint32_t GetLotCount(const int32_t lot) const { + const auto it = items.find(lot); + return it == items.end() ? 0 : it->second; + } + void TakeItem(const int32_t lot, const uint64_t amount) { items[lot] -= static_cast(amount); } + void TakeCoins(const uint64_t amount) { coins -= static_cast(amount); } + }; + + // 1000 coins, 45 of an alt currency and 2 of a crafting item + VendorPricing::BuyCost MixedCost() { + VendorPricing::BuyCost cost{}; + cost.coins = 1000; + cost.AddItem(8318, 45); + cost.AddItem(3000, 2); + return cost; + } + + FakePayer RichPayer() { + FakePayer payer{}; + payer.coins = 5000; + payer.items = { { 8318, 100 }, { 3000, 10 } }; + return payer; + } +} + +TEST(VendorPricingTest, ShortOnCoinsTakesNothing) { + auto payer = RichPayer(); + payer.coins = 999; + EXPECT_FALSE(VendorPricing::TryPay(MixedCost(), payer)); + EXPECT_EQ(payer.coins, 999); + EXPECT_EQ(payer.GetLotCount(8318), 100); + EXPECT_EQ(payer.GetLotCount(3000), 10); +} + +TEST(VendorPricingTest, ShortOnAltCurrencyTakesNothing) { + auto payer = RichPayer(); + payer.items[8318] = 44; + EXPECT_FALSE(VendorPricing::TryPay(MixedCost(), payer)); + EXPECT_EQ(payer.coins, 5000); + EXPECT_EQ(payer.GetLotCount(8318), 44); + EXPECT_EQ(payer.GetLotCount(3000), 10); +} + +TEST(VendorPricingTest, ShortOnCraftingItemsTakesNothing) { + auto payer = RichPayer(); + payer.items[3000] = 1; + EXPECT_FALSE(VendorPricing::TryPay(MixedCost(), payer)); + EXPECT_EQ(payer.coins, 5000); + EXPECT_EQ(payer.GetLotCount(8318), 100); + EXPECT_EQ(payer.GetLotCount(3000), 1); +} + +TEST(VendorPricingTest, EnoughOfEverythingTakesAll) { + auto payer = RichPayer(); + EXPECT_TRUE(VendorPricing::TryPay(MixedCost(), payer)); + EXPECT_EQ(payer.coins, 4000); + EXPECT_EQ(payer.GetLotCount(8318), 55); + EXPECT_EQ(payer.GetLotCount(3000), 8); +} + +TEST(VendorPricingTest, CostsOnOneLotAddUp) { + // An alt currency that is also a crafting item needs both amounts + auto cost = MixedCost(); + cost.AddItem(8318, 60); + auto payer = RichPayer(); + EXPECT_FALSE(VendorPricing::TryPay(cost, payer)); + EXPECT_EQ(payer.GetLotCount(8318), 100); + payer.items[8318] = 105; + EXPECT_TRUE(VendorPricing::TryPay(cost, payer)); + EXPECT_EQ(payer.GetLotCount(8318), 0); +} + +TEST(VendorPricingTest, NegativeCoinsNeverPay) { + auto payer = RichPayer(); + payer.coins = -1; + VendorPricing::BuyCost cost{}; + EXPECT_FALSE(VendorPricing::TryPay(cost, payer)); +}