TrinityCore
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 "Random.h"
22#include "TradePackets.h"
23#include "WorldSession.h"
24
26{
27 return _trader->GetTradeData();
28}
29
31{
32 return !_items[slot].IsEmpty() ? _player->GetItemByGuid(_items[slot]) : nullptr;
33}
34
35bool TradeData::HasItem(ObjectGuid itemGuid) const
36{
37 for (uint8 i = 0; i < TRADE_SLOT_COUNT; ++i)
38 if (_items[i] == itemGuid)
39 return true;
40
41 return false;
42}
43
45{
46 for (uint8 i = 0; i < TRADE_SLOT_COUNT; ++i)
47 if (_items[i] == itemGuid)
48 return TradeSlots(i);
49
50 return TRADE_SLOT_INVALID;
51}
52
54{
56}
57
58void TradeData::SetItem(TradeSlots slot, Item* item, bool update /*= false*/)
59{
60 ObjectGuid itemGuid;
61 if (item)
62 itemGuid = item->GetGUID();
63
64 if (_items[slot] == itemGuid && !update)
65 return;
66
67 _items[slot] = itemGuid;
68
69 SetAccepted(false);
70 GetTraderData()->SetAccepted(false);
71
73
74 Update();
75
76 // need remove possible trader spell applied to changed item
77 if (slot == TRADE_SLOT_NONTRADED)
79
80 // need remove possible player spell applied (possible move reagent)
81 SetSpell(0);
82}
83
84void TradeData::SetSpell(uint32 spell_id, Item* castItem /*= nullptr*/)
85{
86 ObjectGuid itemGuid = castItem ? castItem->GetGUID() : ObjectGuid::Empty;
87
88 if (_spell == spell_id && _spellCastItem == itemGuid)
89 return;
90
91 _spell = spell_id;
92 _spellCastItem = itemGuid;
93
94 SetAccepted(false);
95 GetTraderData()->SetAccepted(false);
96
98
99 Update(true); // send spell info to item owner
100 Update(false); // send spell info to caster self
101}
102
104{
105 if (_money == money)
106 return;
107
108 if (!_player->HasEnoughMoney(money))
109 {
114 return;
115 }
116
117 _money = money;
118
119 SetAccepted(false);
120 GetTraderData()->SetAccepted(false);
121
123
124 Update(true);
125}
126
127void TradeData::Update(bool forTrader /*= true*/) const
128{
129 if (forTrader)
130 _trader->GetSession()->SendUpdateTrade(true); // player state for trader
131 else
132 _player->GetSession()->SendUpdateTrade(false); // player state for player
133}
134
135void TradeData::SetAccepted(bool state, bool forTrader /*= false*/)
136{
137 _accepted = state;
138
139 if (!state)
140 {
143 if (forTrader)
145 else
147 }
148}
149
151{
153}
uint8_t uint8
Definition: Define.h:144
uint64_t uint64
Definition: Define.h:141
uint32_t uint32
Definition: Define.h:142
@ EQUIP_ERR_NOT_ENOUGH_MONEY
Definition: ItemDefines.h:56
uint32 rand32()
Definition: Random.cpp:70
@ TRADE_STATUS_FAILED
@ TRADE_STATUS_UNACCEPTED
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:170
static ObjectGuid const Empty
Definition: ObjectGuid.h:274
bool IsEmpty() const
Definition: ObjectGuid.h:319
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
WorldSession * GetSession() const
Definition: Player.h:2101
TradeData * GetTradeData() const
Definition: Player.h:1498
Item * GetItemByGuid(ObjectGuid guid) const
Definition: Player.cpp:9566
bool HasEnoughMoney(uint64 amount) const
Definition: Player.h:1740
Item * GetSpellCastItem() const
Definition: TradeData.cpp:53
TradeSlots GetTradeSlotForItem(ObjectGuid itemGuid) const
Definition: TradeData.cpp:44
void SetSpell(uint32 spell_id, Item *castItem=nullptr)
Definition: TradeData.cpp:84
void SetMoney(uint64 money)
Definition: TradeData.cpp:103
bool HasItem(ObjectGuid itemGuid) const
Definition: TradeData.cpp:35
uint32 _serverStateIndex
Definition: TradeData.h:87
void SetItem(TradeSlots slot, Item *item, bool update=false)
Definition: TradeData.cpp:58
Player * _player
Definition: TradeData.h:73
void SetAccepted(bool state, bool forTrader=false)
Definition: TradeData.cpp:135
Item * GetItem(TradeSlots slot) const
Definition: TradeData.cpp:30
ObjectGuid _items[TRADE_SLOT_COUNT]
Definition: TradeData.h:84
bool _accepted
Definition: TradeData.h:76
TradeData * GetTraderData() const
Definition: TradeData.cpp:25
ObjectGuid _spellCastItem
Definition: TradeData.h:82
uint32 _spell
Definition: TradeData.h:81
void Update(bool for_trader=true) const
Definition: TradeData.cpp:127
uint64 _money
Definition: TradeData.h:79
Player * _trader
Definition: TradeData.h:74
void UpdateServerStateIndex()
Definition: TradeData.cpp:150
void SendTradeStatus(WorldPackets::Trade::TradeStatus &status)
void SendUpdateTrade(bool trader_data=true)