mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-16 00:35:27 +00:00
fix: a trade offer takes what the trade window takes
The client's trade window has 8 slots for each player and takes neither a bound item nor an equipped one. An offer with more than 8 items, a bound item or an equipped item is now refused like the other offers the window can not make, when it arrives and again when the trade completes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -36,12 +36,14 @@ std::optional<std::vector<TradeItem>> Trade::ResolveOffer(
|
||||
resolved.reserve(offered.size());
|
||||
std::set<LWOOBJID> seen;
|
||||
|
||||
if (offered.size() > MaxOfferedItems) return std::nullopt;
|
||||
|
||||
for (const auto& tradeItem : offered) {
|
||||
// The client keeps its offer by item ID, so an item is only ever in it once
|
||||
if (tradeItem.itemCount == 0 || !seen.insert(tradeItem.itemId).second) return std::nullopt;
|
||||
|
||||
const auto held = findHeld(tradeItem.itemId);
|
||||
if (!held || held->count == 0) return std::nullopt;
|
||||
if (!held || held->count == 0 || held->bound || held->equipped) return std::nullopt;
|
||||
|
||||
resolved.push_back({ tradeItem.itemId, held->lot, std::min(tradeItem.itemCount, held->count) });
|
||||
}
|
||||
@@ -49,6 +51,14 @@ std::optional<std::vector<TradeItem>> Trade::ResolveOffer(
|
||||
return resolved;
|
||||
}
|
||||
|
||||
std::function<std::optional<HeldTradeItem>(LWOOBJID)> Trade::HeldIn(const InventoryComponent& inventory) {
|
||||
return [&inventory](const LWOOBJID itemId) -> std::optional<HeldTradeItem> {
|
||||
const auto* const item = inventory.FindItemById(itemId);
|
||||
if (!item) return std::nullopt;
|
||||
return HeldTradeItem{ item->GetLot(), item->GetCount(), item->GetBound(), item->IsEquipped() };
|
||||
};
|
||||
}
|
||||
|
||||
Trade::~Trade() {
|
||||
|
||||
}
|
||||
@@ -172,15 +182,7 @@ void Trade::Complete() {
|
||||
}
|
||||
|
||||
// The inventories may have changed since the offers were made, so check the offers against them again
|
||||
const auto holds = [](const InventoryComponent* const inventory) {
|
||||
return [inventory](const LWOOBJID itemId) -> std::optional<HeldTradeItem> {
|
||||
const auto* const item = inventory->FindItemById(itemId);
|
||||
if (!item) return std::nullopt;
|
||||
return HeldTradeItem{ item->GetLot(), item->GetCount() };
|
||||
};
|
||||
};
|
||||
|
||||
if (ResolveOffer(m_ItemsA, holds(inventoryA)) != m_ItemsA || ResolveOffer(m_ItemsB, holds(inventoryB)) != m_ItemsB) {
|
||||
if (ResolveOffer(m_ItemsA, HeldIn(*inventoryA)) != m_ItemsA || ResolveOffer(m_ItemsB, HeldIn(*inventoryB)) != m_ItemsB) {
|
||||
LOG("The items offered by %s and %s no longer match what they hold, aborting trade", characterA->GetName().c_str(), characterB->GetName().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user