From 9bb1ecdd2271efe9091dba5118d4589ca3c36da9 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Wed, 11 Nov 2026 07:00:36 -0600 Subject: [PATCH] fix(trade): tell the requester what became of a trade request The client puts ServerTradeInitialReply in chat: "Trade invitation sent.", "User not in zone.", "Player out of range." or "Player already trading." (LWOTradeComponent::SendMessage). Live sent INVITESENT, with the invitee's name, to every trade request, also the one that accepts an invitation. The server sent none, so a request to a player not in the world went unanswered. A request to a player who is in a trade with someone else is now refused with ALREADYTRADING. Before, a second trade was made with that player in it, and which of the two their messages went to depended on map order. Out of range is not sent: there is no evidence of the distance. Co-Authored-By: Claude Opus 5.5 --- dGame/TradingManager.cpp | 6 ++++++ dGame/TradingManager.h | 15 +++++++++++++++ dGame/dGameMessages/TradeMessages.cpp | 18 +++++++++++++++++- .../dGameMessagesTests/TradeOfferTests.cpp | 14 ++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/dGame/TradingManager.cpp b/dGame/TradingManager.cpp index 0cd73e2db..d103c07c0 100644 --- a/dGame/TradingManager.cpp +++ b/dGame/TradingManager.cpp @@ -402,3 +402,9 @@ void TradingManager::NewTrade(LWOOBJID participantA, LWOOBJID participantB) { LOG("Created new trade between (%llu) <-> (%llu)", participantA, participantB); } + +eTradeInviteResult TradingManager::InviteResult(const bool inviteeFound, const Trade* const inviteeTrade, const LWOOBJID requester) { + if (!inviteeFound) return eTradeInviteResult::NOT_FOUND; + if (inviteeTrade && !inviteeTrade->IsParticipant(requester)) return eTradeInviteResult::ALREADY_TRADING; + return eTradeInviteResult::INVITE_SENT; +} diff --git a/dGame/TradingManager.h b/dGame/TradingManager.h index 9fbc14801..47f862205 100644 --- a/dGame/TradingManager.h +++ b/dGame/TradingManager.h @@ -30,6 +30,17 @@ struct HeldTradeItem { struct LwoNameValue; +// ServerTradeInitialReply's resultType: what became of a trade request, told to the player who sent it. The client +// puts it in chat ("Trade invitation sent.", "User not in zone.", "Player out of range.", "Player already trading."; +// LWOTradeComponent::SendMessage). +enum class eTradeInviteResult : int32_t { + NOT_FOUND = 0, + INVITE_SENT, + OUT_OF_RANGE, + ALREADY_TRADING, + GENERAL_ERROR +}; + class InventoryComponent; class Trade { @@ -130,6 +141,10 @@ public: void CancelTrade(const LWOOBJID canceller, LWOOBJID tradeId, const bool sendCancelMessage = true); void NewTrade(LWOOBJID participantA, LWOOBJID participantB); + // The reply to requester's trade request to an invitee who is (or is not) in this world: an invitee in a trade + // with someone else is already trading + static eTradeInviteResult InviteResult(bool inviteeFound, const Trade* inviteeTrade, LWOOBJID requester); + private: static TradingManager* m_Address; //For singleton method diff --git a/dGame/dGameMessages/TradeMessages.cpp b/dGame/dGameMessages/TradeMessages.cpp index bf7c5d34b..f4c287388 100644 --- a/dGame/dGameMessages/TradeMessages.cpp +++ b/dGame/dGameMessages/TradeMessages.cpp @@ -136,8 +136,22 @@ namespace GameMessages { } auto* invitee = Game::entityManager->GetEntity(i64Invitee); + const bool inviteeFound = invitee != nullptr && invitee->IsPlayer(); - if (invitee != nullptr && invitee->IsPlayer()) { + // Live told the requester what became of the request (ServerTradeInitialReply), also when the request + // accepts an invitation + const auto result = TradingManager::InviteResult(inviteeFound, inviteeFound ? TradingManager::Instance()->GetPlayerTrade(i64Invitee).get() : nullptr, entity.GetObjectID()); + const auto sendReply = [&]() { + ServerTradeInitialReply reply; + reply.target = entity.GetObjectID(); + reply.i64Invitee = i64Invitee; + reply.resultType = static_cast(result); + if (inviteeFound && invitee->GetCharacter()) reply.wsName = GeneralUtils::UTF8ToUTF16(invitee->GetCharacter()->GetName()); + reply.Send(sysAddr); + }; + if (result != eTradeInviteResult::INVITE_SENT) sendReply(); + + if (result == eTradeInviteResult::INVITE_SENT) { character = invitee->GetCharacter(); if (!character || !character->GetParentUser()) return; @@ -165,6 +179,8 @@ namespace GameMessages { TradingManager::Instance()->NewTrade(entity.GetObjectID(), i64Invitee); } + sendReply(); + ServerTradeInvite invite; invite.target = i64Invitee; invite.bNeedInvitePopUp = bNeedInvitePopUp; diff --git a/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp b/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp index 464049e92..1e61ea68b 100644 --- a/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp +++ b/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp @@ -162,3 +162,17 @@ TEST_F(TradeAcceptTest, AChangedOfferTakesBothAcceptsBack) { EXPECT_TRUE(trade.GetAccepted(20)); EXPECT_FALSE(trade.GetAccepted(10)); } + +// The requester hears what became of a trade request (ServerTradeInitialReply) +TEST_F(TradeAcceptTest, ATradeRequestIsAnsweredAsLiveAnsweredIt) { + EXPECT_EQ(TradingManager::InviteResult(false, nullptr, 10), eTradeInviteResult::NOT_FOUND); + EXPECT_EQ(TradingManager::InviteResult(true, nullptr, 10), eTradeInviteResult::INVITE_SENT); + + // Accepting an invitation is a request to the player who sent it, who is in a trade with the requester + Trade withRequester(1, 20, 10); + EXPECT_EQ(TradingManager::InviteResult(true, &withRequester, 10), eTradeInviteResult::INVITE_SENT); + + // An invitee in a trade with someone else stays in it + Trade withSomeoneElse(2, 20, 30); + EXPECT_EQ(TradingManager::InviteResult(true, &withSomeoneElse, 10), eTradeInviteResult::ALREADY_TRADING); +}