mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-17 01:05:28 +00:00
fix: check a trade offer against the items the player holds
A trade used the LOT and the count from the offer as the player sent it. The offer is now resolved when it arrives and again when the trade completes: the LOT comes from the held item, the count stops at the held count, and an offer that names an unknown item, the same item twice or a count of 0 is refused. The other player only receives an item that was taken from the offering player. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -19,7 +19,8 @@ SET(DGAMEMESSAGES_TESTS
|
||||
"HotPropertyMessageTests.cpp"
|
||||
"ClientMessagesTests.cpp"
|
||||
"LoadSequenceTests.cpp"
|
||||
"ChatRoutingTests.cpp")
|
||||
"ChatRoutingTests.cpp"
|
||||
"TradeOfferTests.cpp")
|
||||
|
||||
# Get the folder name and prepend it to the files above
|
||||
get_filename_component(thisFolderName ${CMAKE_CURRENT_SOURCE_DIR} NAME)
|
||||
|
||||
58
tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp
Normal file
58
tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "TradingManager.h"
|
||||
|
||||
namespace {
|
||||
const std::map<LWOOBJID, HeldTradeItem> g_Held = {
|
||||
{ 100, { 1727, 5 } },
|
||||
{ 200, { 3040, 1 } },
|
||||
};
|
||||
|
||||
std::optional<HeldTradeItem> FindHeld(const LWOOBJID itemId) {
|
||||
const auto it = g_Held.find(itemId);
|
||||
if (it == g_Held.end()) return std::nullopt;
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TradeOfferTest, AnOfferOfHeldItemsIsKept) {
|
||||
const std::vector<TradeItem> offered = { { 100, 1727, 3 }, { 200, 3040, 1 } };
|
||||
const auto resolved = Trade::ResolveOffer(offered, FindHeld);
|
||||
ASSERT_TRUE(resolved.has_value());
|
||||
EXPECT_EQ(*resolved, offered);
|
||||
}
|
||||
|
||||
TEST(TradeOfferTest, AnEmptyOfferIsKept) {
|
||||
const auto resolved = Trade::ResolveOffer({}, FindHeld);
|
||||
ASSERT_TRUE(resolved.has_value());
|
||||
EXPECT_TRUE(resolved->empty());
|
||||
}
|
||||
|
||||
TEST(TradeOfferTest, TheLotComesFromTheHeldItem) {
|
||||
const auto resolved = Trade::ResolveOffer({ { 100, 7777, 2 } }, FindHeld);
|
||||
ASSERT_TRUE(resolved.has_value());
|
||||
ASSERT_EQ(resolved->size(), 1);
|
||||
EXPECT_EQ(resolved->front().itemLot, 1727);
|
||||
EXPECT_EQ(resolved->front().itemCount, 2);
|
||||
}
|
||||
|
||||
TEST(TradeOfferTest, TheCountStopsAtTheHeldCount) {
|
||||
const auto resolved = Trade::ResolveOffer({ { 100, 1727, 4000000000U } }, FindHeld);
|
||||
ASSERT_TRUE(resolved.has_value());
|
||||
ASSERT_EQ(resolved->size(), 1);
|
||||
EXPECT_EQ(resolved->front().itemCount, 5);
|
||||
}
|
||||
|
||||
TEST(TradeOfferTest, AnItemOfferedTwiceIsRefused) {
|
||||
EXPECT_FALSE(Trade::ResolveOffer({ { 100, 1727, 3 }, { 100, 1727, 3 } }, FindHeld).has_value());
|
||||
}
|
||||
|
||||
TEST(TradeOfferTest, AnItemThePlayerDoesNotHoldIsRefused) {
|
||||
EXPECT_FALSE(Trade::ResolveOffer({ { 300, 1727, 1 } }, FindHeld).has_value());
|
||||
}
|
||||
|
||||
TEST(TradeOfferTest, ACountOfZeroIsRefused) {
|
||||
EXPECT_FALSE(Trade::ResolveOffer({ { 100, 1727, 0 } }, FindHeld).has_value());
|
||||
}
|
||||
Reference in New Issue
Block a user