2010-10-07 15:35:36 +02:00
/*
2012-01-01 00:32:13 +01:00
* Copyright (C) 2008-2012 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"
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
void WorldSession :: SendTradeStatus ( TradeStatus status )
2008-10-11 14:16:25 -05:00
{
WorldPacket data ;
2009-10-17 15:51:44 -07:00
2011-09-29 12:43:05 +02:00
switch ( status )
2008-10-11 14:16:25 -05:00
{
case TRADE_STATUS_BEGIN_TRADE :
data . Initialize ( SMSG_TRADE_STATUS , 4 + 8 );
data << uint32 ( status );
data << uint64 ( 0 );
break ;
case TRADE_STATUS_OPEN_WINDOW :
data . Initialize ( SMSG_TRADE_STATUS , 4 + 4 );
data << uint32 ( status );
data << uint32 ( 0 ); // added in 2.4.0
break ;
case TRADE_STATUS_CLOSE_WINDOW :
data . Initialize ( SMSG_TRADE_STATUS , 4 + 4 + 1 + 4 );
data << uint32 ( status );
data << uint32 ( 0 );
data << uint8 ( 0 );
data << uint32 ( 0 );
break ;
case TRADE_STATUS_ONLY_CONJURED :
2010-09-21 21:55:16 +02:00
case TRADE_STATUS_NOT_ELIGIBLE :
2008-10-11 14:16:25 -05:00
data . Initialize ( SMSG_TRADE_STATUS , 4 + 1 );
data << uint32 ( status );
data << uint8 ( 0 );
break ;
default :
data . Initialize ( SMSG_TRADE_STATUS , 4 );
data << uint32 ( status );
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*/ )
{
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_NETWORKIO , "WORLD: Ignore Trade %u" , _player -> GetGUIDLow ());
2008-10-11 14:16:25 -05:00
// recvPacket.print_storage();
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleBusyTradeOpcode ( WorldPacket & /*recvPacket*/ )
{
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_NETWORKIO , "WORLD: Busy Trade %u" , _player -> GetGUIDLow ());
2008-10-11 14:16:25 -05:00
// recvPacket.print_storage();
}
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
2010-06-25 13:15:51 +02:00
WorldPacket data ( SMSG_TRADE_STATUS_EXTENDED , 1 + 4 + 4 + 4 + 4 + 4 + 7 * ( 1 + 4 + 4 + 4 + 4 + 8 + 4 + 4 + 4 + 4 + 8 + 4 + 4 + 4 + 4 + 4 + 4 ));
data << uint8 ( trader_data ); // 1 means traders data, 0 means own
data << uint32 ( 0 ); // added in 2.4.0, this value must be equal to value from TRADE_STATUS_OPEN_WINDOW status packet (different value for different players to block multiple trades?)
data << uint32 ( TRADE_SLOT_COUNT ); // trade slots count/number?, = next field in most cases
data << uint32 ( TRADE_SLOT_COUNT ); // trade slots count/number?, = prev field in most cases
data << uint32 ( view_trade -> GetMoney ()); // trader gold
data << uint32 ( view_trade -> GetSpell ()); // spell casted on lowest slot item
2009-10-17 15:51:44 -07:00
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
{
2010-06-25 13:15:51 +02:00
data << uint8 ( i ); // trade slot number, if not specified, then end of packet
2009-10-17 15:51:44 -07:00
2010-06-25 13:15:51 +02:00
if ( Item * item = view_trade -> GetItem ( TradeSlots ( i )))
2008-10-11 14:16:25 -05:00
{
2011-04-28 22:42:33 +02:00
data << uint32 ( item -> GetTemplate () -> ItemId ); // entry
data << uint32 ( item -> GetTemplate () -> DisplayInfoID ); // display id
2010-06-25 13:15:51 +02:00
data << uint32 ( item -> GetCount ()); // stack count
// wrapped: hide stats but show giftcreator name
2010-08-04 10:34:30 +02:00
data << uint32 ( item -> HasFlag ( ITEM_FIELD_FLAGS , ITEM_FLAG_WRAPPED ) ? 1 : 0 );
2010-06-25 13:15:51 +02:00
data << uint64 ( item -> GetUInt64Value ( ITEM_FIELD_GIFTCREATOR ));
// perm. enchantment and gems
data << uint32 ( item -> GetEnchantmentId ( PERM_ENCHANTMENT_SLOT ));
for ( uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT ; enchant_slot < SOCK_ENCHANTMENT_SLOT + MAX_GEM_SOCKETS ; ++ enchant_slot )
data << uint32 ( item -> GetEnchantmentId ( EnchantmentSlot ( enchant_slot )));
2008-10-11 14:16:25 -05:00
// creator
2010-06-25 13:15:51 +02:00
data << uint64 ( item -> GetUInt64Value ( ITEM_FIELD_CREATOR ));
data << uint32 ( item -> GetSpellCharges ()); // charges
data << uint32 ( item -> GetItemSuffixFactor ()); // SuffixFactor
data << uint32 ( item -> GetItemRandomPropertyId ()); // random properties id
2011-04-28 22:42:33 +02:00
data << uint32 ( item -> GetTemplate () -> LockID ); // lock id
2008-10-11 14:16:25 -05:00
// max durability
2010-06-25 13:15:51 +02:00
data << uint32 ( item -> GetUInt32Value ( ITEM_FIELD_MAXDURABILITY ));
2008-10-11 14:16:25 -05:00
// durability
2010-06-25 13:15:51 +02:00
data << uint32 ( item -> GetUInt32Value ( ITEM_FIELD_DURABILITY ));
2008-10-11 14:16:25 -05:00
}
else
{
2010-06-25 13:15:51 +02:00
for ( uint8 j = 0 ; j < 18 ; ++ j )
2008-10-11 14:16:25 -05:00
data << uint32 ( 0 );
}
}
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
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_NETWORKIO , "partner storing: %u" , myItems [ i ] -> GetGUIDLow ());
2011-09-08 15:11:55 +02:00
if ( ! AccountMgr :: IsPlayerAccount ( _player -> GetSession () -> GetSecurity ()) && sWorld -> getBoolConfig ( CONFIG_GM_LOG_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)" ,
2010-06-25 13:15:51 +02:00
_player -> GetName (), _player -> GetSession () -> GetAccountId (),
2011-04-29 19:59:40 +02:00
myItems [ i ] -> GetTemplate () -> Name1 . c_str (), myItems [ i ] -> GetEntry (), myItems [ i ] -> GetCount (),
2010-06-25 13:15:51 +02:00
trader -> GetName (), 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
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_NETWORKIO , "player storing: %u" , hisItems [ i ] -> GetGUIDLow ());
2011-09-08 15:11:55 +02:00
if ( ! AccountMgr :: IsPlayerAccount ( trader -> GetSession () -> GetSecurity ()) && sWorld -> getBoolConfig ( CONFIG_GM_LOG_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)" ,
2010-06-25 13:15:51 +02:00
trader -> GetName (), trader -> GetSession () -> GetAccountId (),
2011-04-29 19:59:40 +02:00
hisItems [ i ] -> GetTemplate () -> Name1 . c_str (), hisItems [ i ] -> GetEntry (), hisItems [ i ] -> GetCount (),
2010-06-25 13:15:51 +02:00
_player -> GetName (), _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 )
2011-04-29 20:47:02 +02:00
sLog -> outError ( "trader can't store item: %u" , myItems [ i ] -> GetGUIDLow ());
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
2011-04-29 20:47:02 +02:00
sLog -> outError ( "player can't take item back: %u" , myItems [ i ] -> GetGUIDLow ());
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 )
2011-04-29 20:47:02 +02:00
sLog -> outError ( "player can't store item: %u" , hisItems [ i ] -> GetGUIDLow ());
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
2011-04-29 20:47:02 +02:00
sLog -> outError ( "trader can't take item back: %u" , hisItems [ i ] -> GetGUIDLow ());
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 )))
{
2010-12-23 23:25:44 +01:00
sLog -> outStaticDebug ( "player trade item %u bag: %u slot: %u" , item -> GetGUIDLow (), 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 )))
{
2010-12-23 23:25:44 +01:00
sLog -> outStaticDebug ( "partner trade item %u bag: %u slot: %u" , item -> GetGUIDLow (), 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 };
2010-06-25 13:15:51 +02:00
bool myCanCompleteTrade = true , hisCanCompleteTrade = true ;
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
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
{
SendNotification ( LANG_NOT_ENOUGH_GOLD );
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
{
2010-06-25 13:15:51 +02:00
trader -> GetSession () -> SendNotification ( LANG_NOT_ENOUGH_GOLD );
his_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 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
{
2010-06-25 13:15:51 +02:00
SendTradeStatus ( TRADE_STATUS_TRADE_CANCELED );
return ;
2008-10-11 14:16:25 -05:00
}
2010-09-21 21:55:16 +02:00
if ( item -> IsBindedNotWith ( trader ))
{
SendTradeStatus ( TRADE_STATUS_NOT_ELIGIBLE );
SendTradeStatus ( TRADE_STATUS_CLOSE_WINDOW /*TRADE_STATUS_TRADE_CANCELED*/ );
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
{
2010-06-25 13:15:51 +02:00
SendTradeStatus ( TRADE_STATUS_TRADE_CANCELED );
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
2010-06-25 13:15:51 +02:00
// not accept if spell can't be casted now (cheating)
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
2010-06-25 13:15:51 +02:00
// not accept if spell can't be casted now (cheating)
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
trader -> GetSession () -> SendTradeStatus ( TRADE_STATUS_TRADE_ACCEPT );
// test if item will fit in each inventory
hisCanCompleteTrade = ( trader -> CanStoreItems ( myItems , TRADE_SLOT_TRADED_COUNT ) == EQUIP_ERR_OK );
myCanCompleteTrade = ( _player -> CanStoreItems ( hisItems , TRADE_SLOT_TRADED_COUNT ) == EQUIP_ERR_OK );
clearAcceptTradeMode ( myItems , hisItems );
2008-10-11 14:16:25 -05:00
// in case of missing space report error
2010-04-07 19:14:10 +02:00
if ( ! myCanCompleteTrade )
2008-10-11 14:16:25 -05:00
{
2010-06-25 13:15:51 +02:00
clearAcceptTradeMode ( my_trade , his_trade );
2008-10-11 14:16:25 -05:00
SendNotification ( LANG_NOT_FREE_TRADE_SLOTS );
2010-06-25 13:15:51 +02:00
trader -> GetSession () -> SendNotification ( LANG_NOT_PARTNER_FREE_TRADE_SLOTS );
my_trade -> SetAccepted ( false );
his_trade -> SetAccepted ( false );
2008-10-11 14:16:25 -05:00
return ;
}
else if ( ! hisCanCompleteTrade )
{
2010-06-25 13:15:51 +02:00
clearAcceptTradeMode ( my_trade , his_trade );
2008-10-11 14:16:25 -05:00
SendNotification ( LANG_NOT_PARTNER_FREE_TRADE_SLOTS );
2010-06-25 13:15:51 +02:00
trader -> GetSession () -> SendNotification ( LANG_NOT_FREE_TRADE_SLOTS );
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
{
2010-04-07 22:59:46 +02:00
myItems [ i ] -> SetUInt64Value ( 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
{
2010-06-25 13:15:51 +02:00
hisItems [ i ] -> SetUInt64Value ( ITEM_FIELD_GIFTCREATOR , trader -> GetGUID ());
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
2010-12-23 23:25:44 +01:00
if ( sWorld -> getBoolConfig ( CONFIG_GM_LOG_TRADE ))
2008-10-11 14:16:25 -05:00
{
2011-09-08 15:11:55 +02:00
if ( ! AccountMgr :: IsPlayerAccount ( _player -> GetSession () -> GetSecurity ()) && my_trade -> GetMoney () > 0 )
2008-11-25 16:36:50 -06:00
{
2011-04-29 20:47:02 +02:00
sLog -> outCommand ( _player -> GetSession () -> GetAccountId (), "GM %s (Account: %u) give money (Amount: %u) to player: %s (Account: %u)" ,
2010-06-25 13:15:51 +02:00
_player -> GetName (), _player -> GetSession () -> GetAccountId (),
my_trade -> GetMoney (),
trader -> GetName (), trader -> GetSession () -> GetAccountId ());
2008-11-25 16:36:50 -06:00
}
2011-09-08 15:11:55 +02:00
if ( ! AccountMgr :: IsPlayerAccount ( trader -> GetSession () -> GetSecurity ()) && his_trade -> GetMoney () > 0 )
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) give money (Amount: %u) to player: %s (Account: %u)" ,
2010-06-25 13:15:51 +02:00
trader -> GetName (), trader -> GetSession () -> GetAccountId (),
his_trade -> GetMoney (),
_player -> GetName (), _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
2010-06-25 13:15:51 +02:00
_player -> ModifyMoney ( - int32 ( my_trade -> GetMoney ()));
_player -> ModifyMoney ( his_trade -> GetMoney ());
trader -> ModifyMoney ( - int32 ( his_trade -> GetMoney ()));
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
2010-06-25 13:15:51 +02:00
trader -> GetSession () -> SendTradeStatus ( TRADE_STATUS_TRADE_COMPLETE );
2008-10-11 14:16:25 -05:00
SendTradeStatus ( TRADE_STATUS_TRADE_COMPLETE );
}
else
{
2010-06-25 13:15:51 +02:00
trader -> GetSession () -> SendTradeStatus ( TRADE_STATUS_TRADE_ACCEPT );
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
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleBeginTradeOpcode ( WorldPacket & /*recvPacket*/ )
{
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
2010-06-25 13:15:51 +02:00
my_trade -> GetTrader () -> GetSession () -> SendTradeStatus ( TRADE_STATUS_OPEN_WINDOW );
2008-10-11 14:16:25 -05:00
SendTradeStatus ( TRADE_STATUS_OPEN_WINDOW );
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: SendCancelTrade ()
{
2010-06-25 13:15:51 +02:00
if ( m_playerRecentlyLogout )
return ;
2008-10-11 14:16:25 -05:00
SendTradeStatus ( TRADE_STATUS_TRADE_CANCELED );
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void WorldSession :: HandleCancelTradeOpcode ( WorldPacket & /*recvPacket*/ )
{
// sended 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 )
{
uint64 ID ;
2011-09-05 14:54:19 +02:00
recvPacket >> ID ;
2009-10-17 15:51:44 -07:00
2012-03-09 13:42:52 +01:00
if ( GetPlayer () -> m_trade )
return ;
2009-05-14 16:50:47 -05:00
if ( ! GetPlayer () -> isAlive ())
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_YOU_DEAD );
return ;
}
2009-10-17 15:51:44 -07:00
2012-02-03 19:02:17 +02:00
if ( GetPlayer () -> HasUnitState ( UNIT_STATE_STUNNED ))
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_YOU_STUNNED );
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
{
SendTradeStatus ( TRADE_STATUS_YOU_LOGOUT );
return ;
}
2009-10-17 15:51:44 -07:00
2009-05-14 16:50:47 -05:00
if ( GetPlayer () -> isInFlight ())
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_TARGET_TO_FAR );
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 ;
}
2010-04-07 22:59:46 +02:00
Player * pOther = ObjectAccessor :: FindPlayer ( ID );
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
{
SendTradeStatus ( TRADE_STATUS_NO_TARGET );
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
{
SendTradeStatus ( TRADE_STATUS_BUSY );
return ;
}
2009-10-17 15:51:44 -07:00
2009-05-14 16:50:47 -05:00
if ( ! pOther -> isAlive ())
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_TARGET_DEAD );
return ;
}
2009-10-17 15:51:44 -07:00
2009-05-14 16:50:47 -05:00
if ( pOther -> isInFlight ())
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_TARGET_TO_FAR );
return ;
}
2009-10-17 15:51:44 -07:00
2012-02-03 19:02:17 +02:00
if ( pOther -> HasUnitState ( UNIT_STATE_STUNNED ))
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_TARGET_STUNNED );
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
{
SendTradeStatus ( TRADE_STATUS_TARGET_LOGOUT );
return ;
}
2009-10-17 15:51:44 -07:00
2009-05-14 16:50:47 -05:00
if ( pOther -> GetSocial () -> HasIgnore ( GetPlayer () -> GetGUIDLow ()))
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_IGNORE_YOU );
return ;
}
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
if ( ! sWorld -> getBoolConfig ( CONFIG_ALLOW_TWO_SIDE_TRADE ) && pOther -> GetTeam () != _player -> GetTeam ())
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_WRONG_FACTION );
return ;
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
if ( ! pOther -> IsWithinDistInMap ( _player , 10.0f , false ))
2008-10-11 14:16:25 -05:00
{
SendTradeStatus ( TRADE_STATUS_TARGET_TO_FAR );
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
2008-10-11 14:16:25 -05:00
WorldPacket data ( SMSG_TRADE_STATUS , 12 );
2010-06-25 13:15:51 +02:00
data << uint32 ( TRADE_STATUS_BEGIN_TRADE );
data << uint64 ( _player -> GetGUID ());
pOther -> GetSession () -> SendPacket ( & data );
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 )
{
uint32 gold ;
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
// gold can be incorrect, but this is checked at trade finished.
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
2008-10-11 14:16:25 -05:00
recvPacket >> tradeSlot ;
recvPacket >> bag ;
recvPacket >> slot ;
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 ;
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
{
SendTradeStatus ( TRADE_STATUS_TRADE_CANCELED );
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
{
SendTradeStatus ( TRADE_STATUS_TRADE_CANCELED );
return ;
}
2009-10-17 15:51:44 -07:00
2010-02-15 13:55:16 +01:00
uint64 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
SendTradeStatus ( TRADE_STATUS_TRADE_CANCELED );
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
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
}