mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
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>
125 lines
4.2 KiB
C++
125 lines
4.2 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#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(1000));
|
|
// What a negative count becomes
|
|
EXPECT_FALSE(VendorPricing::IsValidBuyCount(static_cast<uint32_t>(-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<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));
|
|
}
|