TrinityCore
Loading...
Searching...
No Matches
TradeData.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "TradeData.h"
19#include "Item.h"
20#include "Player.h"
21#include "WorldSession.h"
22
27
29{
30 return !_items[slot].IsEmpty() ? _player->GetItemByGuid(_items[slot]) : nullptr;
31}
32
33bool TradeData::HasItem(ObjectGuid itemGuid) const
34{
35 for (uint8 i = 0; i < TRADE_SLOT_COUNT; ++i)
36 if (_items[i] == itemGuid)
37 return true;
38
39 return false;
40}
41
43{
44 for (uint8 i = 0; i < TRADE_SLOT_COUNT; ++i)
45 if (_items[i] == itemGuid)
46 return TradeSlots(i);
47
48 return TRADE_SLOT_INVALID;
49}
50
55
56void TradeData::SetItem(TradeSlots slot, Item* item, bool update /*= false*/)
57{
58 ObjectGuid itemGuid;
59 if (item)
60 itemGuid = item->GetGUID();
61
62 if (_items[slot] == itemGuid && !update)
63 return;
64
65 _items[slot] = itemGuid;
66
67 SetAccepted(false);
68 GetTraderData()->SetAccepted(false);
69
70 Update();
71
72 // need remove possible trader spell applied to changed item
73 if (slot == TRADE_SLOT_NONTRADED)
75
76 // need remove possible player spell applied (possible move reagent)
77 SetSpell(0);
78}
79
80void TradeData::SetSpell(uint32 spell_id, Item* castItem /*= nullptr*/)
81{
82 ObjectGuid itemGuid = castItem ? castItem->GetGUID() : ObjectGuid::Empty;
83
84 if (_spell == spell_id && _spellCastItem == itemGuid)
85 return;
86
87 _spell = spell_id;
88 _spellCastItem = itemGuid;
89
90 SetAccepted(false);
91 GetTraderData()->SetAccepted(false);
92
93 Update(true); // send spell info to item owner
94 Update(false); // send spell info to caster self
95}
96
98{
99 if (_money == money)
100 return;
101
102 if (!_player->HasEnoughMoney(money))
103 {
104 TradeStatusInfo info;
108 return;
109 }
110
111 _money = money;
112
113 SetAccepted(false);
114 GetTraderData()->SetAccepted(false);
115
116 Update(true);
117}
118
119void TradeData::Update(bool forTrader /*= true*/) const
120{
121 if (forTrader)
122 _trader->GetSession()->SendUpdateTrade(true); // player state for trader
123 else
124 _player->GetSession()->SendUpdateTrade(false); // player state for player
125}
126
127void TradeData::SetAccepted(bool state, bool forTrader /*= false*/)
128{
129 _accepted = state;
130
131 if (!state)
132 {
133 TradeStatusInfo info;
135 if (forTrader)
137 else
139 }
140}
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
@ EQUIP_ERR_NOT_ENOUGH_MONEY
Definition ItemDefines.h:55
@ TRADE_STATUS_BACK_TO_TRADE
@ TRADE_STATUS_CLOSE_WINDOW
TradeSlots
Definition TradeData.h:24
@ TRADE_SLOT_COUNT
Definition TradeData.h:25
@ TRADE_SLOT_INVALID
Definition TradeData.h:28
@ TRADE_SLOT_NONTRADED
Definition TradeData.h:27
Definition Item.h:62
static ObjectGuid const Empty
Definition ObjectGuid.h:140
bool IsEmpty() const
Definition ObjectGuid.h:172
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
WorldSession * GetSession() const
Definition Player.h:1719
bool HasEnoughMoney(uint32 amount) const
Definition Player.h:1410
TradeData * GetTradeData() const
Definition Player.h:1210
Item * GetItemByGuid(ObjectGuid guid) const
Definition Player.cpp:9518
Item * GetSpellCastItem() const
Definition TradeData.cpp:51
void SetMoney(uint32 money)
Definition TradeData.cpp:97
TradeSlots GetTradeSlotForItem(ObjectGuid itemGuid) const
Definition TradeData.cpp:42
void SetSpell(uint32 spell_id, Item *castItem=nullptr)
Definition TradeData.cpp:80
bool HasItem(ObjectGuid itemGuid) const
Definition TradeData.cpp:33
void SetItem(TradeSlots slot, Item *item, bool update=false)
Definition TradeData.cpp:56
Player * _player
Definition TradeData.h:67
void SetAccepted(bool state, bool forTrader=false)
Item * GetItem(TradeSlots slot) const
Definition TradeData.cpp:28
ObjectGuid _items[TRADE_SLOT_COUNT]
Definition TradeData.h:78
bool _accepted
Definition TradeData.h:70
TradeData * GetTraderData() const
Definition TradeData.cpp:23
uint32 _money
Definition TradeData.h:73
ObjectGuid _spellCastItem
Definition TradeData.h:76
uint32 _spell
Definition TradeData.h:75
void Update(bool for_trader=true) const
Player * _trader
Definition TradeData.h:68
void SendTradeStatus(TradeStatusInfo const &status)
void SendUpdateTrade(bool trader_data=true)
TradeStatus Status
Definition Player.h:876
InventoryResult Result
Definition Player.h:878