2010-10-07 15:35:36 +02:00
/*
2015-01-01 00:27:46 +01:00
* Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-11 14:16:25 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
#include "Common.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "World.h"
#include "ObjectAccessor.h"
#include "Log.h"
#include "Opcodes.h"
#include "Player.h"
#include "Item.h"
2010-06-25 13:15:51 +02:00
#include "Spell.h"
2008-10-11 14:16:25 -05:00
#include "SocialMgr.h"
#include "Language.h"
2011-09-08 15:11:55 +02:00
#include "AccountMgr.h"
2014-11-10 05:08:29 +00:00
#include "TradePackets.h"
2009-10-17 15:51:44 -07:00
2014-08-01 22:06:33 +02:00
void WorldSession :: SendTradeStatus ( TradeStatusInfo const & info )
2008-10-11 14:16:25 -05:00
{
2014-06-17 15:22:13 -05:00
Player * trader = GetPlayer () -> GetTrader ();
2014-08-01 22:06:33 +02:00
WorldPacket data ( SMSG_TRADE_STATUS , 13 );
2014-06-17 15:22:13 -05:00
data . WriteBit ( trader ? ( trader -> GetSession () -> GetBattlenetAccountId () == GetBattlenetAccountId ()) : 0 ); // IsSameBnetAccount, used for trading heirlooms and other battle.net bound items with your other accounts
2014-08-12 00:44:26 +02:00
data . WriteBits ( info . Status , 5 );
2012-07-26 03:31:46 +01:00
2014-08-01 22:06:33 +02:00
switch ( info . Status )
2008-10-11 14:16:25 -05:00
{
case TRADE_STATUS_BEGIN_TRADE :
2014-08-12 00:44:26 +02:00
data . WriteBit ( info . TraderGuid [ 2 ]);
data . WriteBit ( info . TraderGuid [ 4 ]);
data . WriteBit ( info . TraderGuid [ 6 ]);
data . WriteBit ( info . TraderGuid [ 0 ]);
data . WriteBit ( info . TraderGuid [ 1 ]);
data . WriteBit ( info . TraderGuid [ 3 ]);
data . WriteBit ( info . TraderGuid [ 7 ]);
data . WriteBit ( info . TraderGuid [ 5 ]);
data . WriteByteSeq ( info . TraderGuid [ 4 ]);
data . WriteByteSeq ( info . TraderGuid [ 1 ]);
data . WriteByteSeq ( info . TraderGuid [ 2 ]);
data . WriteByteSeq ( info . TraderGuid [ 3 ]);
data . WriteByteSeq ( info . TraderGuid [ 0 ]);
data . WriteByteSeq ( info . TraderGuid [ 7 ]);
data . WriteByteSeq ( info . TraderGuid [ 6 ]);
data . WriteByteSeq ( info . TraderGuid [ 5 ]);
2008-10-11 14:16:25 -05:00
break ;
case TRADE_STATUS_OPEN_WINDOW :
2014-08-01 22:06:33 +02:00
data << uint32 ( 0 ); // CGTradeInfo::m_tradeID
2008-10-11 14:16:25 -05:00
break ;
case TRADE_STATUS_CLOSE_WINDOW :
2014-08-12 00:44:26 +02:00
data . WriteBit ( info . IsTargetResult ); // bool isTargetError; used for: EQUIP_ERR_BAG_FULL, EQUIP_ERR_CANT_CARRY_MORE_OF_THIS, EQUIP_ERR_MISSING_REAGENT, EQUIP_ERR_ITEM_MAX_LIMIT_CATEGORY_COUNT_EXCEEDED
2014-08-01 22:06:33 +02:00
data << uint32 ( info . Result ); // InventoryResult
data << uint32 ( info . ItemLimitCategoryId ); // ItemLimitCategory.dbc entry
2008-10-11 14:16:25 -05:00
break ;
2014-08-01 22:06:33 +02:00
case TRADE_STATUS_WRONG_REALM :
case TRADE_STATUS_NOT_ON_TAPLIST :
data << uint8 ( info . Slot ); // Trade slot; -1 here clears CGTradeInfo::m_tradeMoney
2008-10-11 14:16:25 -05:00
break ;
2012-07-26 01:56:30 +01:00
case TRADE_STATUS_CURRENCY : // Not implemented
case TRADE_STATUS_CURRENCY_NOT_TRADABLE : // Not implemented
2014-06-17 15:22:13 -05:00
// Blizzard never implemented these, you can only trade currency with the field9 & 1 in CurrencyTypes.DBC, and only two test currencies have that flag
data << uint32 ( 0 ); // Trading Currency Id
data << uint32 ( 0 ); // Trading Currency Amount
2008-10-11 14:16:25 -05:00
default :
2012-08-04 13:19:06 -05:00
data . FlushBits ();
2008-10-11 14:16:25 -05:00
break ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
SendPacket ( & data );
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleIgnoreTradeOpcode ( WorldPacket & /*recvPacket*/ )
{
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleBusyTradeOpcode ( WorldPacket & /*recvPacket*/ )
{
}
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
void WorldSession :: SendUpdateTrade ( bool trader_data /*= true*/ )
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
TradeData * view_trade = trader_data ? _player -> GetTradeData () -> GetTraderData () : _player -> GetTradeData ();
2009-10-17 15:51:44 -07:00
2012-07-26 02:17:03 +01:00
ByteBuffer itemData ( 7 * 2 + 7 * 4 + 3 * 4 + 3 * 4 + 1 );
2009-10-17 15:51:44 -07:00
2012-07-26 01:56:30 +01:00
uint8 count = 0 ;
2012-07-26 11:05:48 +02:00
for ( uint8 i = 0 ; i < TRADE_SLOT_COUNT ; ++ i )
if ( view_trade -> GetItem ( TradeSlots ( i )))
++ count ;
2015-03-01 16:21:15 -05:00
WorldPacket data ( SMSG_TRADE_UPDATED , 4 * 6 + 8 + 1 + 3 + count * 70 );
2014-08-01 22:06:33 +02:00
data << uint32 ( 0 ); // CGTradeInfo::m_tradeID
2012-07-26 11:05:48 +02:00
data << uint32 ( 0 ); // unk 2
data << uint64 ( view_trade -> GetMoney ()); // trader gold
data << uint32 ( view_trade -> GetSpell ()); // spell casted on lowest slot item
data << uint32 ( TRADE_SLOT_COUNT ); // trade slots count/number?, = next field in most cases
data << uint32 ( 0 ); // unk 5
data << uint8 ( trader_data ); // 1 means traders data, 0 means own
data << uint32 ( TRADE_SLOT_COUNT ); // trade slots count/number?, = prev field in most cases
data . WriteBits ( count , 22 );
2009-10-17 16:20:24 -07:00
for ( uint8 i = 0 ; i < TRADE_SLOT_COUNT ; ++ i )
2008-10-11 14:16:25 -05:00
{
2012-07-26 01:56:30 +01:00
Item * item = view_trade -> GetItem ( TradeSlots ( i ));
if ( ! item )
continue ;
2009-10-17 15:51:44 -07:00
2014-09-20 11:05:30 +02:00
ObjectGuid giftCreatorGuid = item -> GetGuidValue ( ITEM_FIELD_GIFTCREATOR );
ObjectGuid creatorGuid = item -> GetGuidValue ( ITEM_FIELD_CREATOR );
2012-07-26 01:56:30 +01:00
2012-07-26 11:05:48 +02:00
data . WriteBit ( giftCreatorGuid [ 7 ]);
data . WriteBit ( giftCreatorGuid [ 1 ]);
bool notWrapped = data . WriteBit ( ! item -> HasFlag ( ITEM_FIELD_FLAGS , ITEM_FLAG_WRAPPED ));
data . WriteBit ( giftCreatorGuid [ 3 ]);
2012-07-26 01:56:30 +01:00
if ( notWrapped )
2008-10-11 14:16:25 -05:00
{
2012-07-26 11:05:48 +02:00
data . WriteBit ( creatorGuid [ 7 ]);
data . WriteBit ( creatorGuid [ 1 ]);
data . WriteBit ( creatorGuid [ 4 ]);
data . WriteBit ( creatorGuid [ 6 ]);
data . WriteBit ( creatorGuid [ 2 ]);
data . WriteBit ( creatorGuid [ 3 ]);
data . WriteBit ( creatorGuid [ 5 ]);
2014-12-21 13:35:43 +01:00
data . WriteBit ( item -> GetTemplate () -> GetLockID () != 0 );
2012-07-26 11:05:48 +02:00
data . WriteBit ( creatorGuid [ 0 ]);
2012-07-26 01:56:30 +01:00
itemData . WriteByteSeq ( creatorGuid [ 1 ]);
itemData << uint32 ( item -> GetEnchantmentId ( PERM_ENCHANTMENT_SLOT ));
for ( uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT ; enchant_slot < SOCK_ENCHANTMENT_SLOT + MAX_GEM_SOCKETS /*3*/ ; ++ enchant_slot )
itemData << uint32 ( item -> GetEnchantmentId ( EnchantmentSlot ( enchant_slot )));
itemData << uint32 ( item -> GetUInt32Value ( ITEM_FIELD_MAXDURABILITY ));
itemData . WriteByteSeq ( creatorGuid [ 6 ]);
itemData . WriteByteSeq ( creatorGuid [ 2 ]);
2012-07-26 11:05:48 +02:00
itemData . WriteByteSeq ( creatorGuid [ 7 ]);
2012-07-26 01:56:30 +01:00
itemData . WriteByteSeq ( creatorGuid [ 4 ]);
itemData << uint32 ( item -> GetUInt32Value ( ITEM_FIELD_DURABILITY ));
itemData << uint32 ( item -> GetItemRandomPropertyId ());
itemData . WriteByteSeq ( creatorGuid [ 3 ]);
itemData << uint32 ( 0 ); // unk7
itemData . WriteByteSeq ( creatorGuid [ 0 ]);
itemData << uint32 ( item -> GetSpellCharges ());
itemData << uint32 ( item -> GetItemSuffixFactor ());
itemData . WriteByteSeq ( creatorGuid [ 5 ]);
2008-10-11 14:16:25 -05:00
}
2012-07-26 01:56:30 +01:00
2012-07-26 11:05:48 +02:00
data . WriteBit ( giftCreatorGuid [ 6 ]);
data . WriteBit ( giftCreatorGuid [ 4 ]);
data . WriteBit ( giftCreatorGuid [ 2 ]);
data . WriteBit ( giftCreatorGuid [ 0 ]);
data . WriteBit ( giftCreatorGuid [ 5 ]);
2012-07-26 01:56:30 +01:00
itemData . WriteByteSeq ( giftCreatorGuid [ 6 ]);
itemData . WriteByteSeq ( giftCreatorGuid [ 1 ]);
itemData . WriteByteSeq ( giftCreatorGuid [ 7 ]);
itemData . WriteByteSeq ( giftCreatorGuid [ 4 ]);
2014-12-21 13:35:43 +01:00
itemData << uint32 ( item -> GetTemplate () -> GetId ());
2012-07-26 01:56:30 +01:00
itemData . WriteByteSeq ( giftCreatorGuid [ 0 ]);
itemData << uint32 ( item -> GetCount ());
itemData . WriteByteSeq ( giftCreatorGuid [ 5 ]);
itemData << uint8 ( i );
itemData . WriteByteSeq ( giftCreatorGuid [ 2 ]);
itemData . WriteByteSeq ( giftCreatorGuid [ 3 ]);
2008-10-11 14:16:25 -05:00
}
2012-07-26 01:56:30 +01:00
data . FlushBits ();
data . append ( itemData );
2008-10-11 14:16:25 -05:00
SendPacket ( & data );
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
//==============================================================
// transfer the items to the players
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: moveItems ( Item * myItems [], Item * hisItems [])
{
2010-06-25 13:15:51 +02:00
Player * trader = _player -> GetTrader ();
if ( ! trader )
return ;
for ( uint8 i = 0 ; i < TRADE_SLOT_TRADED_COUNT ; ++ i )
2008-10-11 14:16:25 -05:00
{
ItemPosCountVec traderDst ;
ItemPosCountVec playerDst ;
2010-06-25 13:15:51 +02:00
bool traderCanTrade = ( myItems [ i ] == NULL || trader -> CanStoreItem ( NULL_BAG , NULL_SLOT , traderDst , myItems [ i ], false ) == EQUIP_ERR_OK );
2010-04-07 23:25:02 +02:00
bool playerCanTrade = ( hisItems [ i ] == NULL || _player -> CanStoreItem ( NULL_BAG , NULL_SLOT , playerDst , hisItems [ i ], false ) == EQUIP_ERR_OK );
2010-04-07 22:59:46 +02:00
if ( traderCanTrade && playerCanTrade )
2008-10-11 14:16:25 -05:00
{
// Ok, if trade item exists and can be stored
// If we trade in both directions we had to check, if the trade will work before we actually do it
// A roll back is not possible after we stored it
2010-04-07 19:14:10 +02:00
if ( myItems [ i ])
2008-10-11 14:16:25 -05:00
{
// logging
2014-10-22 21:22:04 +02:00
TC_LOG_DEBUG ( "network" , "partner storing: %s" , myItems [ i ] -> GetGUID (). ToString (). c_str ());
2013-09-27 12:46:01 +02:00
if ( HasPermission ( rbac :: RBAC_PERM_LOG_GM_TRADE ))
2008-11-25 16:36:50 -06:00
{
2010-12-23 23:25:44 +01:00
sLog -> outCommand ( _player -> GetSession () -> GetAccountId (), "GM %s (Account: %u) trade: %s (Entry: %d Count: %u) to player: %s (Account: %u)" ,
2012-10-24 13:29:34 +02:00
_player -> GetName (). c_str (), _player -> GetSession () -> GetAccountId (),
2014-12-21 13:35:43 +01:00
myItems [ i ] -> GetTemplate () -> GetDefaultLocaleName (), myItems [ i ] -> GetEntry (), myItems [ i ] -> GetCount (),
2012-10-24 13:29:34 +02:00
trader -> GetName (). c_str (), trader -> GetSession () -> GetAccountId ());
2008-11-25 16:36:50 -06:00
}
2009-10-17 15:51:44 -07:00
2010-10-09 17:33:19 +02:00
// adjust time (depends on /played)
2010-09-21 21:55:16 +02:00
if ( myItems [ i ] -> HasFlag ( ITEM_FIELD_FLAGS , ITEM_FLAG_BOP_TRADEABLE ))
myItems [ i ] -> SetUInt32Value ( ITEM_FIELD_CREATE_PLAYED_TIME , trader -> GetTotalPlayedTime () - ( _player -> GetTotalPlayedTime () - myItems [ i ] -> GetUInt32Value ( ITEM_FIELD_CREATE_PLAYED_TIME )));
2010-10-09 17:33:19 +02:00
// store
trader -> MoveItemToInventory ( traderDst , myItems [ i ], true , true );
2008-10-11 14:16:25 -05:00
}
2010-04-07 19:14:10 +02:00
if ( hisItems [ i ])
2008-10-11 14:16:25 -05:00
{
// logging
2014-10-22 21:22:04 +02:00
TC_LOG_DEBUG ( "network" , "player storing: %s" , hisItems [ i ] -> GetGUID (). ToString (). c_str ());
2013-09-27 12:46:01 +02:00
if ( HasPermission ( rbac :: RBAC_PERM_LOG_GM_TRADE ))
2008-11-25 16:36:50 -06:00
{
2011-04-29 20:47:02 +02:00
sLog -> outCommand ( trader -> GetSession () -> GetAccountId (), "GM %s (Account: %u) trade: %s (Entry: %d Count: %u) to player: %s (Account: %u)" ,
2012-10-24 13:29:34 +02:00
trader -> GetName (). c_str (), trader -> GetSession () -> GetAccountId (),
2014-12-21 13:35:43 +01:00
hisItems [ i ] -> GetTemplate () -> GetDefaultLocaleName (), hisItems [ i ] -> GetEntry (), hisItems [ i ] -> GetCount (),
2012-10-24 13:29:34 +02:00
_player -> GetName (). c_str (), _player -> GetSession () -> GetAccountId ());
2008-11-25 16:36:50 -06:00
}
2009-10-17 15:51:44 -07:00
2010-10-09 17:33:19 +02:00
// adjust time (depends on /played)
2010-09-21 21:55:16 +02:00
if ( hisItems [ i ] -> HasFlag ( ITEM_FIELD_FLAGS , ITEM_FLAG_BOP_TRADEABLE ))
hisItems [ i ] -> SetUInt32Value ( ITEM_FIELD_CREATE_PLAYED_TIME , _player -> GetTotalPlayedTime () - ( trader -> GetTotalPlayedTime () - hisItems [ i ] -> GetUInt32Value ( ITEM_FIELD_CREATE_PLAYED_TIME )));
2010-10-09 17:33:19 +02:00
// store
_player -> MoveItemToInventory ( playerDst , hisItems [ i ], true , true );
2008-10-11 14:16:25 -05:00
}
}
else
{
// in case of fatal error log error message
// return the already removed items to the original owner
2010-04-07 19:14:10 +02:00
if ( myItems [ i ])
2008-10-11 14:16:25 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! traderCanTrade )
2014-10-22 21:22:04 +02:00
TC_LOG_ERROR ( "network" , "trader can't store item: %s" , myItems [ i ] -> GetGUID (). ToString (). c_str ());
2010-04-07 22:59:46 +02:00
if ( _player -> CanStoreItem ( NULL_BAG , NULL_SLOT , playerDst , myItems [ i ], false ) == EQUIP_ERR_OK )
2008-10-11 14:16:25 -05:00
_player -> MoveItemToInventory ( playerDst , myItems [ i ], true , true );
else
2014-10-27 22:52:19 +00:00
TC_LOG_ERROR ( "network" , "player can't take item back: %s" , myItems [ i ] -> GetGUID (). ToString (). c_str ());
2008-10-11 14:16:25 -05:00
}
// return the already removed items to the original owner
2010-04-07 19:14:10 +02:00
if ( hisItems [ i ])
2008-10-11 14:16:25 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! playerCanTrade )
2014-10-22 21:22:04 +02:00
TC_LOG_ERROR ( "network" , "player can't store item: %s" , hisItems [ i ] -> GetGUID (). ToString (). c_str ());
2010-06-25 13:15:51 +02:00
if ( trader -> CanStoreItem ( NULL_BAG , NULL_SLOT , traderDst , hisItems [ i ], false ) == EQUIP_ERR_OK )
trader -> MoveItemToInventory ( traderDst , hisItems [ i ], true , true );
2008-10-11 14:16:25 -05:00
else
2014-10-22 21:22:04 +02:00
TC_LOG_ERROR ( "network" , "trader can't take item back: %s" , hisItems [ i ] -> GetGUID (). ToString (). c_str ());
2008-10-11 14:16:25 -05:00
}
}
}
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
//==============================================================
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
static void setAcceptTradeMode ( TradeData * myTrade , TradeData * hisTrade , Item * * myItems , Item * * hisItems )
2010-06-25 13:15:51 +02:00
{
myTrade -> SetInAcceptProcess ( true );
hisTrade -> SetInAcceptProcess ( true );
// store items in local list and set 'in-trade' flag
2011-06-12 02:30:32 +02:00
for ( uint8 i = 0 ; i < TRADE_SLOT_TRADED_COUNT ; ++ i )
2010-06-25 13:15:51 +02:00
{
if ( Item * item = myTrade -> GetItem ( TradeSlots ( i )))
{
2014-10-22 21:22:04 +02:00
TC_LOG_DEBUG ( "network" , "player trade %s bag: %u slot: %u" , item -> GetGUID (). ToString (). c_str (), item -> GetBagSlot (), item -> GetSlot ());
2010-06-25 13:15:51 +02:00
//Can return NULL
myItems [ i ] = item ;
myItems [ i ] -> SetInTrade ();
}
if ( Item * item = hisTrade -> GetItem ( TradeSlots ( i )))
{
2014-10-22 21:22:04 +02:00
TC_LOG_DEBUG ( "network" , "partner trade %s bag: %u slot: %u" , item -> GetGUID (). ToString (). c_str (), item -> GetBagSlot (), item -> GetSlot ());
2010-06-25 13:15:51 +02:00
hisItems [ i ] = item ;
hisItems [ i ] -> SetInTrade ();
}
}
}
static void clearAcceptTradeMode ( TradeData * myTrade , TradeData * hisTrade )
{
myTrade -> SetInAcceptProcess ( false );
hisTrade -> SetInAcceptProcess ( false );
}
2011-09-15 14:08:17 +02:00
static void clearAcceptTradeMode ( Item * * myItems , Item * * hisItems )
2010-06-25 13:15:51 +02:00
{
// clear 'in-trade' flag
for ( uint8 i = 0 ; i < TRADE_SLOT_TRADED_COUNT ; ++ i )
{
if ( myItems [ i ])
myItems [ i ] -> SetInTrade ( false );
if ( hisItems [ i ])
hisItems [ i ] -> SetInTrade ( false );
}
}
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleAcceptTradeOpcode ( WorldPacket & /*recvPacket*/ )
{
2010-06-25 13:15:51 +02:00
TradeData * my_trade = _player -> m_trade ;
if ( ! my_trade )
return ;
Player * trader = my_trade -> GetTrader ();
TradeData * his_trade = trader -> m_trade ;
if ( ! his_trade )
return ;
2011-09-15 14:08:17 +02:00
Item * myItems [ TRADE_SLOT_TRADED_COUNT ] = { NULL , NULL , NULL , NULL , NULL , NULL };
Item * hisItems [ TRADE_SLOT_TRADED_COUNT ] = { NULL , NULL , NULL , NULL , NULL , NULL };
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
// set before checks for propertly undo at problems (it already set in to client)
my_trade -> SetAccepted ( true );
2009-10-17 15:51:44 -07:00
2014-08-01 22:06:33 +02:00
TradeStatusInfo info ;
2014-04-25 20:52:17 +02:00
if ( ! _player -> IsWithinDistInMap ( trader , TRADE_DISTANCE , false ))
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TARGET_TO_FAR ;
SendTradeStatus ( info );
2014-04-25 20:52:17 +02:00
my_trade -> SetAccepted ( false );
return ;
}
2008-10-11 14:16:25 -05:00
// not accept case incorrect money amount
2010-08-23 22:58:32 +06:00
if ( ! _player -> HasEnoughMoney ( my_trade -> GetMoney ()))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_CLOSE_WINDOW ;
info . Result = EQUIP_ERR_NOT_ENOUGH_MONEY ;
SendTradeStatus ( info );
2010-06-25 13:15:51 +02:00
my_trade -> SetAccepted ( false , true );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// not accept case incorrect money amount
2010-08-23 21:41:29 -07:00
if ( ! trader -> HasEnoughMoney ( his_trade -> GetMoney ()))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_CLOSE_WINDOW ;
info . Result = EQUIP_ERR_NOT_ENOUGH_MONEY ;
trader -> GetSession () -> SendTradeStatus ( info );
2010-06-25 13:15:51 +02:00
his_trade -> SetAccepted ( false , true );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-02-02 00:16:11 +01:00
if ( _player -> GetMoney () >= uint64 ( MAX_MONEY_AMOUNT ) - his_trade -> GetMoney ())
2013-02-01 11:38:55 +01:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_CLOSE_WINDOW ;
info . Result = EQUIP_ERR_TOO_MUCH_GOLD ;
SendTradeStatus ( info );
2013-02-01 11:38:55 +01:00
my_trade -> SetAccepted ( false , true );
return ;
}
2013-02-02 00:16:11 +01:00
if ( trader -> GetMoney () >= uint64 ( MAX_MONEY_AMOUNT ) - my_trade -> GetMoney ())
2013-02-01 11:38:55 +01:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_CLOSE_WINDOW ;
info . Result = EQUIP_ERR_TOO_MUCH_GOLD ;
trader -> GetSession () -> SendTradeStatus ( info );
2013-02-01 11:38:55 +01:00
his_trade -> SetAccepted ( false , true );
return ;
}
2008-10-11 14:16:25 -05:00
// not accept if some items now can't be trade (cheating)
2010-06-25 13:15:51 +02:00
for ( uint8 i = 0 ; i < TRADE_SLOT_TRADED_COUNT ; ++ i )
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
if ( Item * item = my_trade -> GetItem ( TradeSlots ( i )))
2008-10-11 14:16:25 -05:00
{
2010-09-21 21:55:16 +02:00
if ( ! item -> CanBeTraded ( false , true ))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_CANCELED ;
SendTradeStatus ( info );
2010-06-25 13:15:51 +02:00
return ;
2008-10-11 14:16:25 -05:00
}
2013-02-01 11:38:55 +01:00
2010-09-21 21:55:16 +02:00
if ( item -> IsBindedNotWith ( trader ))
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_CLOSE_WINDOW ;
2014-08-12 00:44:26 +02:00
info . Result = EQUIP_ERR_TRADE_BOUND_ITEM ;
2014-08-01 22:06:33 +02:00
SendTradeStatus ( info );
2010-09-21 21:55:16 +02:00
return ;
}
2008-10-11 14:16:25 -05:00
}
2010-06-25 13:15:51 +02:00
if ( Item * item = his_trade -> GetItem ( TradeSlots ( i )))
2008-10-11 14:16:25 -05:00
{
2010-09-21 21:55:16 +02:00
if ( ! item -> CanBeTraded ( false , true ))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_CANCELED ;
SendTradeStatus ( info );
2010-06-25 13:15:51 +02:00
return ;
2008-10-11 14:16:25 -05:00
}
2010-09-21 21:55:16 +02:00
//if (item->IsBindedNotWith(_player)) // dont mark as invalid when his item isnt good (not exploitable because if item is invalid trade will fail anyway later on the same check)
//{
// SendTradeStatus(TRADE_STATUS_NOT_ELIGIBLE);
// his_trade->SetAccepted(false, true);
// return;
//}
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
if ( his_trade -> IsAccepted ())
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
setAcceptTradeMode ( my_trade , his_trade , myItems , hisItems );
Spell * my_spell = NULL ;
SpellCastTargets my_targets ;
Spell * his_spell = NULL ;
SpellCastTargets his_targets ;
2009-10-17 15:51:44 -07:00
2013-12-25 14:16:55 -03:30
// not accept if spell can't be cast now (cheating)
2010-06-25 13:15:51 +02:00
if ( uint32 my_spell_id = my_trade -> GetSpell ())
2008-10-11 14:16:25 -05:00
{
2011-07-26 23:09:28 +02:00
SpellInfo const * spellEntry = sSpellMgr -> GetSpellInfo ( my_spell_id );
2010-06-25 13:15:51 +02:00
Item * castItem = my_trade -> GetSpellCastItem ();
if ( ! spellEntry || ! his_trade -> GetItem ( TRADE_SLOT_NONTRADED ) ||
2010-08-30 15:25:15 +02:00
( my_trade -> HasSpellCastItem () && ! castItem ))
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
clearAcceptTradeMode ( my_trade , his_trade );
clearAcceptTradeMode ( myItems , hisItems );
my_trade -> SetSpell ( 0 );
return ;
2008-10-11 14:16:25 -05:00
}
2010-06-25 13:15:51 +02:00
2011-07-31 19:03:06 +02:00
my_spell = new Spell ( _player , spellEntry , TRIGGERED_FULL_MASK );
2010-06-25 13:15:51 +02:00
my_spell -> m_CastItem = castItem ;
2011-06-23 13:28:52 +02:00
my_targets . SetTradeItemTarget ( _player );
2010-06-25 13:15:51 +02:00
my_spell -> m_targets = my_targets ;
SpellCastResult res = my_spell -> CheckCast ( true );
if ( res != SPELL_CAST_OK )
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
my_spell -> SendCastResult ( res );
clearAcceptTradeMode ( my_trade , his_trade );
clearAcceptTradeMode ( myItems , hisItems );
delete my_spell ;
my_trade -> SetSpell ( 0 );
return ;
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2013-12-25 14:16:55 -03:30
// not accept if spell can't be cast now (cheating)
2010-06-25 13:15:51 +02:00
if ( uint32 his_spell_id = his_trade -> GetSpell ())
2008-10-11 14:16:25 -05:00
{
2011-07-26 23:09:28 +02:00
SpellInfo const * spellEntry = sSpellMgr -> GetSpellInfo ( his_spell_id );
2010-06-25 13:15:51 +02:00
Item * castItem = his_trade -> GetSpellCastItem ();
2010-08-30 15:25:15 +02:00
if ( ! spellEntry || ! my_trade -> GetItem ( TRADE_SLOT_NONTRADED ) || ( his_trade -> HasSpellCastItem () && ! castItem ))
2010-06-25 13:15:51 +02:00
{
delete my_spell ;
his_trade -> SetSpell ( 0 );
clearAcceptTradeMode ( my_trade , his_trade );
clearAcceptTradeMode ( myItems , hisItems );
return ;
}
2011-07-31 19:03:06 +02:00
his_spell = new Spell ( trader , spellEntry , TRIGGERED_FULL_MASK );
2010-06-25 13:15:51 +02:00
his_spell -> m_CastItem = castItem ;
2011-06-23 13:28:52 +02:00
his_targets . SetTradeItemTarget ( trader );
2010-06-25 13:15:51 +02:00
his_spell -> m_targets = his_targets ;
SpellCastResult res = his_spell -> CheckCast ( true );
if ( res != SPELL_CAST_OK )
{
his_spell -> SendCastResult ( res );
clearAcceptTradeMode ( my_trade , his_trade );
clearAcceptTradeMode ( myItems , hisItems );
delete my_spell ;
delete his_spell ;
his_trade -> SetSpell ( 0 );
return ;
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
// inform partner client
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_ACCEPT ;
trader -> GetSession () -> SendTradeStatus ( info );
2010-06-25 13:15:51 +02:00
// test if item will fit in each inventory
2014-08-01 22:06:33 +02:00
TradeStatusInfo myCanCompleteInfo , hisCanCompleteInfo ;
hisCanCompleteInfo . Result = trader -> CanStoreItems ( myItems , TRADE_SLOT_TRADED_COUNT , & hisCanCompleteInfo . ItemLimitCategoryId );
myCanCompleteInfo . Result = _player -> CanStoreItems ( hisItems , TRADE_SLOT_TRADED_COUNT , & myCanCompleteInfo . ItemLimitCategoryId );
2010-06-25 13:15:51 +02:00
clearAcceptTradeMode ( myItems , hisItems );
2008-10-11 14:16:25 -05:00
// in case of missing space report error
2014-08-01 22:06:33 +02:00
if ( myCanCompleteInfo . Result != EQUIP_ERR_OK )
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
clearAcceptTradeMode ( my_trade , his_trade );
2014-08-01 22:06:33 +02:00
myCanCompleteInfo . Status = TRADE_STATUS_CLOSE_WINDOW ;
trader -> GetSession () -> SendTradeStatus ( myCanCompleteInfo );
myCanCompleteInfo . IsTargetResult = true ;
SendTradeStatus ( myCanCompleteInfo );
2010-06-25 13:15:51 +02:00
my_trade -> SetAccepted ( false );
his_trade -> SetAccepted ( false );
2008-10-11 14:16:25 -05:00
return ;
}
2014-08-01 22:06:33 +02:00
else if ( hisCanCompleteInfo . Result != EQUIP_ERR_OK )
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
clearAcceptTradeMode ( my_trade , his_trade );
2014-08-01 22:06:33 +02:00
hisCanCompleteInfo . Status = TRADE_STATUS_CLOSE_WINDOW ;
SendTradeStatus ( hisCanCompleteInfo );
hisCanCompleteInfo . IsTargetResult = true ;
trader -> GetSession () -> SendTradeStatus ( hisCanCompleteInfo );
2010-06-25 13:15:51 +02:00
my_trade -> SetAccepted ( false );
his_trade -> SetAccepted ( false );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// execute trade: 1. remove
2010-06-25 13:15:51 +02:00
for ( uint8 i = 0 ; i < TRADE_SLOT_TRADED_COUNT ; ++ i )
2008-10-11 14:16:25 -05:00
{
2010-02-15 13:55:16 +01:00
if ( myItems [ i ])
2008-10-11 14:16:25 -05:00
{
2014-09-14 16:14:12 +02:00
myItems [ i ] -> SetGuidValue ( ITEM_FIELD_GIFTCREATOR , _player -> GetGUID ());
2010-06-25 13:15:51 +02:00
_player -> MoveItemFromInventory ( myItems [ i ] -> GetBagSlot (), myItems [ i ] -> GetSlot (), true );
2008-10-11 14:16:25 -05:00
}
2010-02-15 13:55:16 +01:00
if ( hisItems [ i ])
2008-10-11 14:16:25 -05:00
{
2014-09-14 16:14:12 +02:00
hisItems [ i ] -> SetGuidValue ( ITEM_FIELD_GIFTCREATOR , trader -> GetGUID ());
2010-06-25 13:15:51 +02:00
trader -> MoveItemFromInventory ( hisItems [ i ] -> GetBagSlot (), hisItems [ i ] -> GetSlot (), true );
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// execute trade: 2. store
moveItems ( myItems , hisItems );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// logging money
2013-09-27 12:46:01 +02:00
if ( HasPermission ( rbac :: RBAC_PERM_LOG_GM_TRADE ))
2008-10-11 14:16:25 -05:00
{
2013-02-25 11:40:51 +01:00
if ( my_trade -> GetMoney () > 0 )
2008-11-25 16:36:50 -06:00
{
2012-08-17 10:25:29 +02:00
sLog -> outCommand ( _player -> GetSession () -> GetAccountId (), "GM %s (Account: %u) give money (Amount: " UI64FMTD ") to player: %s (Account: %u)" ,
2012-10-24 13:29:34 +02:00
_player -> GetName (). c_str (), _player -> GetSession () -> GetAccountId (),
2010-06-25 13:15:51 +02:00
my_trade -> GetMoney (),
2012-10-24 13:29:34 +02:00
trader -> GetName (). c_str (), trader -> GetSession () -> GetAccountId ());
2008-11-25 16:36:50 -06:00
}
2013-02-25 11:40:51 +01:00
if ( his_trade -> GetMoney () > 0 )
2008-11-25 16:36:50 -06:00
{
2012-08-17 10:25:29 +02:00
sLog -> outCommand ( trader -> GetSession () -> GetAccountId (), "GM %s (Account: %u) give money (Amount: " UI64FMTD ") to player: %s (Account: %u)" ,
2012-10-24 13:29:34 +02:00
trader -> GetName (). c_str (), trader -> GetSession () -> GetAccountId (),
2010-06-25 13:15:51 +02:00
his_trade -> GetMoney (),
2012-10-24 13:29:34 +02:00
_player -> GetName (). c_str (), _player -> GetSession () -> GetAccountId ());
2008-11-25 16:36:50 -06:00
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// update money
2012-07-24 15:27:42 +01:00
_player -> ModifyMoney ( - int64 ( my_trade -> GetMoney ()));
2010-06-25 13:15:51 +02:00
_player -> ModifyMoney ( his_trade -> GetMoney ());
2012-07-24 15:27:42 +01:00
trader -> ModifyMoney ( - int64 ( his_trade -> GetMoney ()));
2010-06-25 13:15:51 +02:00
trader -> ModifyMoney ( my_trade -> GetMoney ());
if ( my_spell )
my_spell -> prepare ( & my_targets );
if ( his_spell )
his_spell -> prepare ( & his_targets );
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
// cleanup
clearAcceptTradeMode ( my_trade , his_trade );
delete _player -> m_trade ;
_player -> m_trade = NULL ;
delete trader -> m_trade ;
trader -> m_trade = NULL ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// desynchronized with the other saves here (SaveInventoryAndGoldToDB() not have own transaction guards)
2010-08-21 03:19:25 +02:00
SQLTransaction trans = CharacterDatabase . BeginTransaction ();
_player -> SaveInventoryAndGoldToDB ( trans );
trader -> SaveInventoryAndGoldToDB ( trans );
CharacterDatabase . CommitTransaction ( trans );
2009-10-17 15:51:44 -07:00
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_COMPLETE ;
trader -> GetSession () -> SendTradeStatus ( info );
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
}
else
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_ACCEPT ;
trader -> GetSession () -> SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleUnacceptTradeOpcode ( WorldPacket & /*recvPacket*/ )
{
2010-06-25 13:15:51 +02:00
TradeData * my_trade = _player -> GetTradeData ();
if ( ! my_trade )
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
my_trade -> SetAccepted ( false , true );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2012-07-24 16:52:13 +01:00
void WorldSession :: HandleBeginTradeOpcode ( WorldPacket & /*recvPacket*/ )
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
TradeData * my_trade = _player -> m_trade ;
if ( ! my_trade )
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2014-08-01 22:06:33 +02:00
TradeStatusInfo info ;
info . Status = TRADE_STATUS_OPEN_WINDOW ;
my_trade -> GetTrader () -> GetSession () -> SendTradeStatus ( info );
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: SendCancelTrade ()
{
2012-05-30 08:01:02 +02:00
if ( PlayerRecentlyLoggedOut () || PlayerLogout ())
2010-06-25 13:15:51 +02:00
return ;
2014-08-01 22:06:33 +02:00
TradeStatusInfo info ;
info . Status = TRADE_STATUS_TRADE_CANCELED ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2014-11-10 05:08:29 +00:00
void WorldSession :: HandleCancelTradeOpcode ( WorldPackets :: Trade :: CancelTrade & /*cancelTrade*/ )
2008-10-11 14:16:25 -05:00
{
2012-07-24 16:52:13 +01:00
// sent also after LOGOUT COMPLETE
2010-04-07 19:14:10 +02:00
if ( _player ) // needed because STATUS_LOGGEDIN_OR_RECENTLY_LOGGOUT
2008-10-11 14:16:25 -05:00
_player -> TradeCancel ( true );
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleInitiateTradeOpcode ( WorldPacket & recvPacket )
{
2012-07-24 16:52:13 +01:00
ObjectGuid guid ;
guid [ 0 ] = recvPacket . ReadBit ();
guid [ 3 ] = recvPacket . ReadBit ();
guid [ 5 ] = recvPacket . ReadBit ();
guid [ 1 ] = recvPacket . ReadBit ();
guid [ 4 ] = recvPacket . ReadBit ();
guid [ 6 ] = recvPacket . ReadBit ();
guid [ 7 ] = recvPacket . ReadBit ();
guid [ 2 ] = recvPacket . ReadBit ();
recvPacket . ReadByteSeq ( guid [ 7 ]);
recvPacket . ReadByteSeq ( guid [ 4 ]);
recvPacket . ReadByteSeq ( guid [ 3 ]);
recvPacket . ReadByteSeq ( guid [ 5 ]);
recvPacket . ReadByteSeq ( guid [ 1 ]);
recvPacket . ReadByteSeq ( guid [ 2 ]);
recvPacket . ReadByteSeq ( guid [ 6 ]);
recvPacket . ReadByteSeq ( guid [ 0 ]);
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
if ( GetPlayer () -> m_trade )
return ;
2014-08-01 22:06:33 +02:00
TradeStatusInfo info ;
2013-06-11 19:54:27 -02:30
if ( ! GetPlayer () -> IsAlive ())
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_YOU_DEAD ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2012-02-03 19:12:41 +02:00
if ( GetPlayer () -> HasUnitState ( UNIT_STATE_STUNNED ))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_YOU_STUNNED ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2009-05-14 16:50:47 -05:00
if ( isLogingOut ())
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_YOU_LOGOUT ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
if ( GetPlayer () -> IsInFlight ())
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TARGET_TO_FAR ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
if ( GetPlayer () -> getLevel () < sWorld -> getIntConfig ( CONFIG_TRADE_LEVEL_REQ ))
2010-03-09 12:58:48 +01:00
{
2010-12-23 23:25:44 +01:00
SendNotification ( GetTrinityString ( LANG_TRADE_REQ ), sWorld -> getIntConfig ( CONFIG_TRADE_LEVEL_REQ ));
2010-03-09 12:58:48 +01:00
return ;
}
2012-07-24 16:52:13 +01:00
Player * pOther = ObjectAccessor :: FindPlayer ( guid );
2009-10-17 15:51:44 -07:00
2009-05-14 16:50:47 -05:00
if ( ! pOther )
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_NO_TARGET ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
if ( pOther == GetPlayer () || pOther -> m_trade )
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_BUSY ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
if ( ! pOther -> IsAlive ())
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TARGET_DEAD ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
if ( pOther -> IsInFlight ())
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TARGET_TO_FAR ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2012-02-03 19:12:41 +02:00
if ( pOther -> HasUnitState ( UNIT_STATE_STUNNED ))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TARGET_STUNNED ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2009-05-14 16:50:47 -05:00
if ( pOther -> GetSession () -> isLogingOut ())
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TARGET_LOGOUT ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2014-10-22 12:52:12 +02:00
if ( pOther -> GetSocial () -> HasIgnore ( GetPlayer () -> GetGUID ()))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_IGNORE_YOU ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2014-12-27 18:10:47 +00:00
if ( pOther -> GetTeam () != _player -> GetTeam () &&
( ! sWorld -> getBoolConfig ( CONFIG_ALLOW_TWO_SIDE_TRADE ) &&
2014-12-29 01:00:16 +01:00
! HasPermission ( rbac :: RBAC_PERM_ALLOW_TWO_SIDE_TRADE )))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_WRONG_FACTION ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2014-04-25 20:52:17 +02:00
if ( ! pOther -> IsWithinDistInMap ( _player , TRADE_DISTANCE , false ))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TARGET_TO_FAR ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
if ( pOther -> getLevel () < sWorld -> getIntConfig ( CONFIG_TRADE_LEVEL_REQ ))
2010-03-09 12:58:48 +01:00
{
2010-12-23 23:25:44 +01:00
SendNotification ( GetTrinityString ( LANG_TRADE_OTHER_REQ ), sWorld -> getIntConfig ( CONFIG_TRADE_LEVEL_REQ ));
2010-03-09 12:58:48 +01:00
return ;
}
2008-10-11 14:16:25 -05:00
// OK start trade
2010-06-25 13:15:51 +02:00
_player -> m_trade = new TradeData ( _player , pOther );
pOther -> m_trade = new TradeData ( pOther , _player );
2009-10-17 15:51:44 -07:00
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_BEGIN_TRADE ;
info . TraderGuid = _player -> GetGUID ();
pOther -> GetSession () -> SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleSetTradeGoldOpcode ( WorldPacket & recvPacket )
{
2012-07-24 15:27:42 +01:00
uint64 gold ;
2008-10-11 14:16:25 -05:00
recvPacket >> gold ;
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
TradeData * my_trade = _player -> GetTradeData ();
if ( ! my_trade )
return ;
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
my_trade -> SetMoney ( gold );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleSetTradeItemOpcode ( WorldPacket & recvPacket )
{
// send update
uint8 tradeSlot ;
uint8 bag ;
uint8 slot ;
2009-10-17 15:51:44 -07:00
2012-07-30 01:15:41 +01:00
recvPacket >> slot ;
2008-10-11 14:16:25 -05:00
recvPacket >> tradeSlot ;
recvPacket >> bag ;
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
TradeData * my_trade = _player -> GetTradeData ();
if ( ! my_trade )
return ;
2014-08-01 22:06:33 +02:00
TradeStatusInfo info ;
2008-10-11 14:16:25 -05:00
// invalid slot number
2010-04-07 19:14:10 +02:00
if ( tradeSlot >= TRADE_SLOT_COUNT )
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_CANCELED ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// check cheating, can't fail with correct client operations
2010-06-25 13:15:51 +02:00
Item * item = _player -> GetItemByPos ( bag , slot );
2010-09-21 21:55:16 +02:00
if ( ! item || ( tradeSlot != TRADE_SLOT_NONTRADED && ! item -> CanBeTraded ( false , true )))
2008-10-11 14:16:25 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_CANCELED ;
SendTradeStatus ( info );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2014-09-14 16:14:12 +02:00
ObjectGuid iGUID = item -> GetGUID ();
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// prevent place single item into many trade slots using cheating and client bugs
2010-06-25 13:15:51 +02:00
if ( my_trade -> HasItem ( iGUID ))
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
// cheating attempt
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_TRADE_CANCELED ;
SendTradeStatus ( info );
2010-06-25 13:15:51 +02:00
return ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2014-09-18 21:15:39 +02:00
if ( tradeSlot != TRADE_SLOT_NONTRADED && item -> IsBindedNotWith ( my_trade -> GetTrader ()))
2014-06-17 15:22:13 -05:00
{
2014-08-01 22:06:33 +02:00
info . Status = TRADE_STATUS_NOT_ON_TAPLIST ;
info . Slot = tradeSlot ;
SendTradeStatus ( info );
2014-06-17 15:22:13 -05:00
return ;
}
2010-06-25 13:15:51 +02:00
my_trade -> SetItem ( TradeSlots ( tradeSlot ), item );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleClearTradeItemOpcode ( WorldPacket & recvPacket )
{
uint8 tradeSlot ;
recvPacket >> tradeSlot ;
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
TradeData * my_trade = _player -> m_trade ;
if ( ! my_trade )
return ;
2008-10-11 14:16:25 -05:00
// invalid slot number
2010-04-07 19:14:10 +02:00
if ( tradeSlot >= TRADE_SLOT_COUNT )
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
my_trade -> SetItem ( TradeSlots ( tradeSlot ), NULL );
2008-10-11 14:16:25 -05:00
}