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

@@ -2,17 +2,39 @@
#include "Entity.h"
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <vector>
struct TradeItem {
LWOOBJID itemId;
LOT itemLot;
uint32_t itemCount;
bool operator==(const TradeItem& other) const = default;
};
// What a player holds of an item they offered
struct HeldTradeItem {
LOT lot;
uint32_t count;
};
class Trade {
public:
/**
* Checks the items a player offered against the items they hold.
* @param offered the items as the player sent them
* @param findHeld gives the held item with an ID, or nothing if the player has no such item
* @return the offer with every LOT taken from the held item and every count limited to the held count,
* or nothing if the offer names an item the player does not hold, the same item twice or a count of 0
*/
static std::optional<std::vector<TradeItem>> ResolveOffer(
const std::vector<TradeItem>& offered,
const std::function<std::optional<HeldTradeItem>(LWOOBJID)>& findHeld);
explicit Trade(LWOOBJID tradeId, LWOOBJID participantA, LWOOBJID participantB);
~Trade();