fix: pay a vendor sale by the items that left the inventory

Selling paid for the count in the message while the move stopped at
what the stack held. The count now has to be between 1 and the stack's
count, the item can not be one the vendor already holds for buy back,
and the coins and alternate currency are worked out from the number of
items that left the inventory.

Buying back needs an item in the buy back inventory and a count the
stack holds. Both prices are worked out as 64 bit numbers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-10 13:01:17 -06:00
parent fe4a1e5e98
commit 2d24608186
4 changed files with 129 additions and 13 deletions

View File

@@ -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)

View File

@@ -0,0 +1,40 @@
#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(1001));
// 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));
}