fix(vendor): check every buy cost before taking any

A purchase removed its crafting items before it checked coins and the
alternate currency, so a player short on either lost the items and
still got PURCHASE_FAIL. Coins, the alternate currency and every
crafting item (summed per LOT) are now checked first, and only taken
when all are there. A failure still sends only PURCHASE_FAIL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-28 23:17:28 -06:00
parent e209894e79
commit fb1edd52f3
3 changed files with 149 additions and 29 deletions

View File

@@ -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<uint64_t>(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<uint64_t>(craftingCurrencyCount) * count);
}
Character* character = buyer->GetCharacter();
if (!character || character->GetCoins() < 0 || static_cast<uint64_t>(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<uint32_t>(amount), eInventoryType::ALL); }
void TakeCoins(const uint64_t amount) {
character.SetCoins(character.GetCoins() - static_cast<int64_t>(amount), eLootSourceType::VENDOR, CoinSource::Object(vendor));
}
inventoryComponent->RemoveItem(itemComp.currencyLOT, static_cast<uint32_t>(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<int64_t>(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();

View File

@@ -3,6 +3,7 @@
#include <cmath>
#include <cstdint>
#include <map>
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<uint64_t>(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<int32_t, uint64_t> 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 <typename Payer>
bool CanPay(const BuyCost& cost, const Payer& payer) {
const int64_t coins = payer.GetCoins();
if (coins < 0 || static_cast<uint64_t>(coins) < cost.coins) return false;
for (const auto& [lot, amount] : cost.items) {
if (static_cast<uint64_t>(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 <typename Payer>
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__

View File

@@ -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<uint64_t>(UINT32_MAX));
}
namespace {
struct FakePayer {
int64_t coins = 0;
std::map<int32_t, uint32_t> 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<uint32_t>(amount); }
void TakeCoins(const uint64_t amount) { coins -= static_cast<int64_t>(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));
}