Files
DarkflameServer/dGame/TradingManager.h
Aaron Kimbrell 9bb1ecdd22 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 <noreply@anthropic.com>
2026-11-11 07:00:36 -06:00

153 lines
4.5 KiB
C++

#pragma once
#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;
// The trade window takes neither a bound item nor an equipped one
bool bound = false;
bool equipped = false;
// Nor an item the player may not delete, nor a brick built model that is not approved
bool deletable = true;
bool approved = true;
};
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 {
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, a count of 0,
* a bound or equipped item, an item the player may not delete, a brick built model that is not approved,
* or more items than the trade window has slots
*/
static std::optional<std::vector<TradeItem>> ResolveOffer(
const std::vector<TradeItem>& offered,
const std::function<std::optional<HeldTradeItem>(LWOOBJID)>& findHeld);
/**
* Gives the lookup ResolveOffer needs for the items in an inventory
*/
static std::function<std::optional<HeldTradeItem>(LWOOBJID)> HeldIn(const InventoryComponent& inventory);
/**
* Whether the trade window takes an item for its moderation status. Only a brick built model has one,
* the userModelMod of its config, and only an approved one is taken.
*/
static bool IsApproved(LOT lot, const LwoNameValue& config);
// The item a brick built model is
static constexpr LOT BrickBuiltModelLot = 6662;
// The userModelMod of an approved model
static constexpr int32_t ApprovedModerationStatus = 1;
// The slots one side of the trade window has
static constexpr uint32_t MaxOfferedItems = 8;
explicit Trade(LWOOBJID tradeId, LWOOBJID participantA, LWOOBJID participantB);
~Trade();
LWOOBJID GetTradeId() const;
bool IsParticipant(LWOOBJID playerId) const;
LWOOBJID GetParticipantA() const;
LWOOBJID GetParticipantB() const;
Entity* GetParticipantAEntity() const;
Entity* GetParticipantBEntity() const;
void SetCoins(LWOOBJID participant, uint64_t coins);
void SetItems(LWOOBJID participant, std::vector<TradeItem> items);
void SetAccepted(LWOOBJID participant, bool value);
/**
* Whether a participant has given their final accept
*/
bool GetAccepted(LWOOBJID participant) const;
/**
* Takes back both final accepts. A changed offer does this, as both clients do when one arrives.
*/
void ResetAccepted();
void Complete();
void Cancel(const LWOOBJID canceller);
void SendUpdateToOther(LWOOBJID participant);
private:
LWOOBJID m_TradeId = LWOOBJID_EMPTY;
LWOOBJID m_ParticipantA = LWOOBJID_EMPTY;
LWOOBJID m_ParticipantB = LWOOBJID_EMPTY;
uint64_t m_CoinsA = 0;
uint64_t m_CoinsB = 0;
std::vector<TradeItem> m_ItemsA{};
std::vector<TradeItem> m_ItemsB{};
bool m_AcceptedA = false;
bool m_AcceptedB = false;
};
class TradingManager {
public:
static TradingManager* Instance() {
if (!m_Address) {
m_Address = new TradingManager();
}
return m_Address;
}
const std::unique_ptr<Trade>& GetTrade(LWOOBJID tradeId) const;
const std::unique_ptr<Trade>& GetPlayerTrade(LWOOBJID playerId) const;
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
std::unordered_map<LWOOBJID, std::unique_ptr<Trade>> trades;
};