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:
Aaron Kimbrell
2026-11-10 12:57:58 -06:00
parent b88168f2ed
commit c0b7a8f643
5 changed files with 143 additions and 7 deletions

View File

@@ -9,6 +9,8 @@
#include "ePermissionMap.h"
#include "Game.h"
#include "GeneralUtils.h"
#include "InventoryComponent.h"
#include "Item.h"
#include "Logger.h"
#include "TradingManager.h"
#include "User.h"
@@ -233,8 +235,23 @@ namespace GameMessages {
if (trade == nullptr) return;
const auto* const inventoryComponent = entity.GetComponent<InventoryComponent>();
if (!inventoryComponent) return;
// The LOT and the count the trade works with come from the items the player holds
const auto resolvedItems = Trade::ResolveOffer(items, [inventoryComponent](const LWOOBJID itemId) -> std::optional<HeldTradeItem> {
const auto* const item = inventoryComponent->FindItemById(itemId);
if (!item) return std::nullopt;
return HeldTradeItem{ item->GetLot(), item->GetCount() };
});
if (!resolvedItems) {
LOG("Trade update from (%llu) does not match the items they hold, ignoring it", entity.GetObjectID());
return;
}
trade->SetCoins(entity.GetObjectID(), i64Currency);
trade->SetItems(entity.GetObjectID(), items);
trade->SetItems(entity.GetObjectID(), *resolvedItems);
trade->SendUpdateToOther(entity.GetObjectID());
}