diff --git a/dGame/TradingManager.cpp b/dGame/TradingManager.cpp index 46bcfe6fe..bf64113ec 100644 --- a/dGame/TradingManager.cpp +++ b/dGame/TradingManager.cpp @@ -166,6 +166,17 @@ void Trade::SetAccepted(LWOOBJID participant, bool value) { } if (m_AcceptedA && m_AcceptedB) { + // Live told the players the trade was about to be performed (an empty ServerTradeUpdate with + // bAboutToPerform); their clients show the trade as complete (LWOTradeComponent::SendMessage, + // "SetTradeComplete") + for (auto* const participant : { GetParticipantAEntity(), GetParticipantBEntity() }) { + if (!participant) continue; + GameMessages::ServerTradeUpdate aboutToPerform; + aboutToPerform.target = participant->GetObjectID(); + aboutToPerform.bAboutToPerform = true; + aboutToPerform.Send(participant->GetSystemAddress()); + } + auto* entityB = GetParticipantBEntity(); if (entityB != nullptr) { @@ -418,3 +429,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 582607544..1029e90aa 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 { @@ -138,6 +149,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/dComponents/Vendor/AchievementVendorComponent.cpp b/dGame/dComponents/Vendor/AchievementVendorComponent.cpp index ea2e42612..a92d3da91 100644 --- a/dGame/dComponents/Vendor/AchievementVendorComponent.cpp +++ b/dGame/dComponents/Vendor/AchievementVendorComponent.cpp @@ -65,7 +65,7 @@ void AchievementVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) { } auto* inventoryComponent = buyer->GetComponent(); - if (!inventoryComponent) { + if (!inventoryComponent || !MeetsBuyRequirements(*buyer, itemComp.reqPrecondition)) { SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); return; } diff --git a/dGame/dComponents/Vendor/BaseVendorComponent.cpp b/dGame/dComponents/Vendor/BaseVendorComponent.cpp index 0b50fe081..5cd26e6a0 100644 --- a/dGame/dComponents/Vendor/BaseVendorComponent.cpp +++ b/dGame/dComponents/Vendor/BaseVendorComponent.cpp @@ -19,6 +19,7 @@ #include "Item.h" #include "EconomyLedger.h" #include "VendorPricing.h" +#include "Preconditions.h" #include #include @@ -186,6 +187,11 @@ void BaseVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) { int itemCompID = compRegistryTable->GetByIDAndType(lot, eReplicaComponentType::ITEM); CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID); + if (!MeetsBuyRequirements(*buyer, itemComp.reqPrecondition)) { + SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); + return; + } + // Extra currency that needs to be deducted in case of crafting auto craftingCurrencies = CDItemComponentTable::ParseCraftingCurrencies(itemComp); for (const auto& [crafintCurrencyLOT, crafintCurrencyCount]: craftingCurrencies) { @@ -233,6 +239,11 @@ void BaseVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) { SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_SUCCESS); } +bool BaseVendorComponent::MeetsBuyRequirements(Entity& buyer, const std::string& reqPrecondition) { + if (reqPrecondition.empty()) return true; + return PreconditionExpression(reqPrecondition).Check(&buyer, PreconditionContext::VendorBuy, false); +} + bool BaseVendorComponent::SetupItem(LOT item) { auto* itemComponentTable = CDClientManager::GetTable(); diff --git a/dGame/dComponents/Vendor/BaseVendorComponent.h b/dGame/dComponents/Vendor/BaseVendorComponent.h index 371fef081..aaa5f8cb1 100644 --- a/dGame/dComponents/Vendor/BaseVendorComponent.h +++ b/dGame/dComponents/Vendor/BaseVendorComponent.h @@ -58,6 +58,11 @@ public: void Buy(Entity* buyer, LOT lot, uint32_t count); + // Whether buyer meets the item's own requirements (reqPrecondition) for buying it. The vendor window checks them + // in the vendor buy context and greys the item out without a tooltip (an unnamed function in LWOVendorComponent, + // LWOItemComponent::StaticCheckUseRequirements). + static bool MeetsBuyRequirements(Entity& buyer, const std::string& reqPrecondition); + // SellToVendor / BuybackFromVendor from player (sent from sysAddr), for the item with object ID itemObjID. void SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count); void BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count); diff --git a/dGame/dGameMessages/ObjectMessages.cpp b/dGame/dGameMessages/ObjectMessages.cpp index fc6f75a0e..7873c09e7 100644 --- a/dGame/dGameMessages/ObjectMessages.cpp +++ b/dGame/dGameMessages/ObjectMessages.cpp @@ -128,9 +128,10 @@ namespace GameMessages { }); } - // The property browser was closed without a launch, or the player died with it open - // (LWOPropertyEntranceComponent::onPropertyEntranceSelected) - if (args == u"CancelLaunch" && entity.HasComponent(eReplicaComponentType::PROPERTY_ENTRANCE)) { + // The property browser or the multi-zone menu was closed without a launch, or the player died with it open + // (LWOPropertyEntranceComponent::onPropertyEntranceSelected; + // LWOMultiZoneEntranceComponent::MultiZoneEntranceSelected, Run) + if (args == u"CancelLaunch" && (entity.HasComponent(eReplicaComponentType::PROPERTY_ENTRANCE) || entity.HasComponent(eReplicaComponentType::MULTI_ZONE_ENTRANCE))) { PropertyEntranceComponent::CancelLaunch(*player); } 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/dGame/dUtilities/Preconditions.cpp b/dGame/dUtilities/Preconditions.cpp index 6ce7984a8..dbfabdf71 100644 --- a/dGame/dUtilities/Preconditions.cpp +++ b/dGame/dUtilities/Preconditions.cpp @@ -213,7 +213,10 @@ bool Precondition::CheckValue(Entity* player, const uint32_t value) const { case PreconditionType::DoesNotHaveFlag: return !character->GetPlayerFlag(value); default: - return true; // There are a couple more unknown preconditions. Always return true in this case. + // LWOPreconditionComponent::CheckPrecondition fails a type it does not know, and logs it. No row of + // the table has one. + LOG("Precondition with unknown type %i", static_cast(type)); + return false; } } @@ -286,7 +289,7 @@ std::vector PreconditionExpression::GetItemCosts(Entity* player) const return costs; } -bool PreconditionExpression::Check(Entity* player, const PreconditionContext context) const { +bool PreconditionExpression::Check(Entity* player, const PreconditionContext context, const bool showTooltip) const { // LWOPreconditionComponent::msgCheckAllPreconditions: every group has to pass, a group passes with // any of its alternatives, and only the alternatives of a failed group are reported bool pass = true; @@ -298,6 +301,7 @@ bool PreconditionExpression::Check(Entity* player, const PreconditionContext con if (groupPasses) continue; pass = false; + if (!showTooltip) continue; for (const auto condition : group) { GameMessages::NotifyClientFailedPrecondition failedPrecondition; failedPrecondition.target = player->GetObjectID(); diff --git a/dGame/dUtilities/Preconditions.h b/dGame/dUtilities/Preconditions.h index ac4551282..cafc7af02 100644 --- a/dGame/dUtilities/Preconditions.h +++ b/dGame/dUtilities/Preconditions.h @@ -102,7 +102,9 @@ class PreconditionExpression final public: explicit PreconditionExpression(const std::string& conditions); - bool Check(Entity* player, PreconditionContext context = PreconditionContext::None) const; + // Whether every group passes. With showTooltip, the player is told which preconditions of a failed group they do + // not meet (NotifyClientFailedPrecondition); the client's CheckUseRequirements has the same flag (bShowTooltip). + bool Check(Entity* player, PreconditionContext context = PreconditionContext::None, bool showTooltip = true) const; // The item costs of the HasItem preconditions in this expression that the player meets std::vector GetItemCosts(Entity* player) const; diff --git a/tests/dGameTests/PreconditionCheckTests.cpp b/tests/dGameTests/PreconditionCheckTests.cpp index fb77f2904..ebe5b85c6 100644 --- a/tests/dGameTests/PreconditionCheckTests.cpp +++ b/tests/dGameTests/PreconditionCheckTests.cpp @@ -4,6 +4,7 @@ #include "CDClientDatabase.h" #include "CDComponentsRegistryTable.h" #include "CDItemComponentTable.h" +#include "BaseVendorComponent.h" #include "Character.h" #include "DestroyableComponent.h" #include "Entity.h" @@ -135,3 +136,25 @@ TEST_F(PreconditionCheckTests, ItemsSoldToAVendorAreNotHeld) { EXPECT_FALSE(Preconditions::Check(player.get(), hasNone)); EXPECT_EQ(Preconditions::Get(hasFive).GetItemCost(player.get()), (ItemCost{ BRICK, 5 })); } + +// Buying from a vendor checks the item's requirements in the vendor buy context: rows without that bit (the thinking +// hat one, 252) do not stop a purchase; the level ones (249) and the everywhere ones do +TEST_F(PreconditionCheckTests, VendorBuyChecksTheItemsRequirementsInItsContext) { + const auto notAtVendors = Row(PreconditionType::HasItem, "6194", 1, 252); + const auto level = Row(PreconditionType::HasItem, "6194", 1, 249); + const auto everywhere = Row(PreconditionType::HasItem, "6194", 1, 255); + + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, "")); + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(notAtVendors))); + EXPECT_FALSE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(level))); + EXPECT_FALSE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(notAtVendors) + ";" + std::to_string(everywhere))); + + Give(eInventoryType::ITEMS, 1); + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(level))); + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(notAtVendors) + ";" + std::to_string(everywhere))); +} + +// A type the client does not know fails +TEST_F(PreconditionCheckTests, AnUnknownTypeFails) { + EXPECT_FALSE(Preconditions::Check(player.get(), Row(static_cast(99), "1", 1, 255))); +} diff --git a/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp b/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp index 7e6dd6055..71b823fd4 100644 --- a/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp +++ b/tests/dGameTests/dGameMessagesTests/TradeOfferTests.cpp @@ -172,3 +172,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); +}