mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-17 17:25:27 +00:00
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:
53
dGame/dComponents/Vendor/BaseVendorComponent.cpp
vendored
53
dGame/dComponents/Vendor/BaseVendorComponent.cpp
vendored
@@ -18,6 +18,7 @@
|
||||
#include "InventoryMessages.h"
|
||||
#include "Item.h"
|
||||
#include "EconomyLedger.h"
|
||||
#include "VendorPricing.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
@@ -284,15 +285,36 @@ void BaseVendorComponent::SellToVendor(Entity& player, const SystemAddress& sysA
|
||||
return;
|
||||
}
|
||||
|
||||
float sellScalar = GetSellScalar();
|
||||
if (Inventory::IsValidItem(itemComp.currencyLOT)) {
|
||||
const auto altCurrency = static_cast<uint32_t>(itemComp.altCurrencyCost * sellScalar) * count;
|
||||
inv->AddItem(itemComp.currencyLOT, std::floor(altCurrency), eLootSourceType::VENDOR); // Return alt currencies like faction tokens.
|
||||
// Only a count the stack holds can be sold, and an item the vendor already has can not be sold again
|
||||
const auto* const itemInventory = item->GetInventory();
|
||||
if (!VendorPricing::IsValidStackCount(count, item->GetCount()) || !itemInventory || itemInventory->GetType() == eInventoryType::VENDOR_BUYBACK) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
MakeRoomInBuyback(*inv, *item, static_cast<uint32_t>(std::max(count, 0)));
|
||||
const float sellScalar = GetSellScalar();
|
||||
const LOT soldLot = item->GetLot();
|
||||
const uint32_t heldBefore = itemInventory->GetLotCount(soldLot);
|
||||
|
||||
MakeRoomInBuyback(*inv, *item, static_cast<uint32_t>(count));
|
||||
inv->MoveItemToInventory(item, eInventoryType::VENDOR_BUYBACK, count, true, false, true);
|
||||
character->SetCoins(std::floor(character->GetCoins() + (static_cast<uint32_t>(itemComp.baseValue * sellScalar) * count)), eLootSourceType::VENDOR, CoinSource::Object(*m_Parent));
|
||||
|
||||
// Pay for the items that left the inventory
|
||||
const uint32_t heldAfter = itemInventory->GetLotCount(soldLot);
|
||||
const uint32_t soldCount = heldBefore > heldAfter ? heldBefore - heldAfter : 0;
|
||||
if (soldCount == 0) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Inventory::IsValidItem(itemComp.currencyLOT)) {
|
||||
const uint64_t altCurrency = VendorPricing::GetPricePerItem(itemComp.altCurrencyCost, sellScalar, soldCount);
|
||||
// Return alt currencies like faction tokens.
|
||||
if (altCurrency > 0) inv->AddItem(itemComp.currencyLOT, static_cast<uint32_t>(std::min<uint64_t>(altCurrency, UINT32_MAX)), eLootSourceType::VENDOR);
|
||||
}
|
||||
|
||||
const uint64_t coins = VendorPricing::GetPricePerItem(itemComp.baseValue, sellScalar, soldCount);
|
||||
character->SetCoins(character->GetCoins() + static_cast<int64_t>(coins), eLootSourceType::VENDOR, CoinSource::Object(*m_Parent));
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -311,18 +333,25 @@ void BaseVendorComponent::BuybackFromVendor(Entity& player, const SystemAddress&
|
||||
int itemCompID = compRegistryTable->GetByIDAndType(item->GetLot(), eReplicaComponentType::ITEM);
|
||||
CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID);
|
||||
|
||||
float sellScalar = GetSellScalar();
|
||||
// Only an item the vendor holds for the player can be bought back, and only as many as there are
|
||||
const auto* const itemInventory = item->GetInventory();
|
||||
if (!VendorPricing::IsValidStackCount(count, item->GetCount()) || !itemInventory || itemInventory->GetType() != eInventoryType::VENDOR_BUYBACK) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto cost = static_cast<uint32_t>(std::floor(((itemComp.baseValue * sellScalar) * count)));
|
||||
const float sellScalar = GetSellScalar();
|
||||
|
||||
if (character->GetCoins() < cost) {
|
||||
const uint64_t cost = VendorPricing::GetPriceTotal(itemComp.baseValue, sellScalar, count);
|
||||
|
||||
if (character->GetCoins() < 0 || static_cast<uint64_t>(character->GetCoins()) < cost) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Inventory::IsValidItem(itemComp.currencyLOT)) {
|
||||
const uint32_t altCurrencyCost = std::floor(itemComp.altCurrencyCost * sellScalar) * count;
|
||||
if (inv->GetLotCount(itemComp.currencyLOT) < altCurrencyCost || !inv->RemoveItem(itemComp.currencyLOT, altCurrencyCost, eInventoryType::ALL)) {
|
||||
const uint64_t altCurrencyCost = VendorPricing::GetPricePerItem(itemComp.altCurrencyCost, sellScalar, count);
|
||||
if (inv->GetLotCount(itemComp.currencyLOT) < altCurrencyCost || !inv->RemoveItem(itemComp.currencyLOT, static_cast<uint32_t>(altCurrencyCost), eInventoryType::ALL)) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
@@ -330,7 +359,7 @@ void BaseVendorComponent::BuybackFromVendor(Entity& player, const SystemAddress&
|
||||
|
||||
//inv->RemoveItem(count, -1, iObjID);
|
||||
inv->MoveItemToInventory(item, Inventory::FindInventoryTypeForLot(item->GetLot()), count, true, false);
|
||||
character->SetCoins(character->GetCoins() - cost, eLootSourceType::VENDOR, CoinSource::Object(*m_Parent));
|
||||
character->SetCoins(character->GetCoins() - static_cast<int64_t>(cost), eLootSourceType::VENDOR, CoinSource::Object(*m_Parent));
|
||||
//Game::entityManager->SerializeEntity(player); // so inventory updates
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
}
|
||||
|
||||
46
dGame/dUtilities/VendorPricing.h
Normal file
46
dGame/dUtilities/VendorPricing.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef __VENDORPRICING__H__
|
||||
#define __VENDORPRICING__H__
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace VendorPricing {
|
||||
// The most of one item a vendor sells at once, a full stack is 999
|
||||
constexpr uint32_t MaxBuyCount = 1000;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
#endif //!__VENDORPRICING__H__
|
||||
@@ -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)
|
||||
|
||||
40
tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp
Normal file
40
tests/dGameTests/dGameMessagesTests/VendorPricingTests.cpp
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user