Files
DarkflameServer/dGame/dUtilities/VendorPricing.h
Aaron Kimbrell fb1edd52f3 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>
2026-11-28 23:17:28 -06:00

87 lines
2.7 KiB
C++

#ifndef __VENDORPRICING__H__
#define __VENDORPRICING__H__
#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
constexpr uint32_t MaxBuyCount = 999;
/**
* 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<uint32_t>(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<float>(unitValue) * scalar);
if (!(unitPrice > 0.0f)) return 0;
return static_cast<uint64_t>(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<float>(unitValue) * scalar) * count);
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__