void WorldSession::HandleAuctionSellItem( WorldPacket & recv_data )
{
	if (!_player->IsInWorld())
		return;

	uint64 guid,item;
	uint32 bid, buyout, etime, unk1, unk2;	// etime is in minutes

	recv_data >> guid >> unk1 >> item;
	recv_data >> unk2;
	recv_data >> bid >> buyout >> etime;

	Creature * pCreature = _player->GetMapMgr()->GetCreature(GET_LOWGUID_PART(guid));
	if(  !pCreature || !pCreature->auctionHouse )
		return;		// NPC doesn't exist or isn't an auctioneer

	// Get item
	Item * pItem = _player->GetItemInterface()->GetItemByGUID(item);
    if( !pItem || pItem->IsSoulbound() || pItem->IsConjured() )
	{
		WorldPacket data(SMSG_AUCTION_COMMAND_RESULT, 8);
		data << uint32(0);
		data << uint32(AUCTION_CREATE);
		data << uint32(AUCTION_ERROR_ITEM);
		SendPacket(&data);
		return;
	};
	
	if (m_gmData->rank > RANK_NO_RANK && m_gmData->rank < RANK_COADMIN)
	{
		SystemMessage("GMs cannot post auctions");
		WorldPacket data(SMSG_AUCTION_COMMAND_RESULT, 8);
		data << uint32(0);
		data << uint32(AUCTION_CREATE);
		data << uint32(AUCTION_ERROR_ITEM);
		SendPacket(&data);
		return;
	};

	AuctionHouse * ah = pCreature->auctionHouse;

	uint32 item_worth = pItem->GetProto()->SellPrice * pItem->GetStackCount();
	uint32 item_deposit = (uint32)(item_worth * ah->deposit_percent) * (uint32)(etime / 240.0f); // deposit is per 4 hours

	if( !_player->HasGold(item_deposit) ) // player cannot afford deposit
	{
		WorldPacket data(SMSG_AUCTION_COMMAND_RESULT, 8);
		data << uint32(0);
		data << uint32(AUCTION_CREATE);
		data << uint32(AUCTION_ERROR_MONEY);
		SendPacket(&data);
		return;
	}

	pItem = _player->GetItemInterface()->SafeRemoveAndRetreiveItemByGuid(item, false);
	if (!pItem){
		WorldPacket data(SMSG_AUCTION_COMMAND_RESULT, 8);
		data << uint32(0);
		data << uint32(AUCTION_CREATE);
		data << uint32(AUCTION_ERROR_ITEM);
		SendPacket(&data);
		return;
	};

	if( pItem->IsInWorld() )
	{
		pItem->RemoveFromWorld();
	}

	pItem->SetOwner(NULL);
	pItem->m_isDirty = true;
	pItem->SaveToDB(INVENTORY_SLOT_NOT_SET, 0, true, NULL);

	// Create auction
	Auction * auct = new Auction;
	auct->BuyoutPrice = buyout;
	auct->ExpiryTime = (uint32)UNIXTIME + (etime * 60);
	auct->HighestBid = bid;
	auct->HighestBidder = 0;	// hm
	auct->Id = sAuctionMgr.GenerateAuctionId();
	auct->Owner = _player->GetLowGUID();
	auct->pItem = pItem;
	auct->Deleted = false;
	auct->DeletedReason = 0;
	auct->DepositAmount = item_deposit;

	// remove deposit
	_player->ModGold( -(int32)item_deposit );

	// Add and save auction to DB
	ah->AddAuction(auct);
	auct->SaveToDB(ah->GetID());

	// Send result packet
	WorldPacket data(SMSG_AUCTION_COMMAND_RESULT, 8);
	data << auct->Id;
	data << uint32(AUCTION_CREATE);
	data << uint32(AUCTION_ERROR_NONE);
	SendPacket(&data);
}
void WorldSession::HandleAuctionSellItem( WorldPacket & recv_data )
{
	if (!_player->IsInWorld())
	{ 
		return;
	}

	uint64 guid,item;
	uint32 bid, buyout, etime, unk1, unk2;	// etime is in minutes

	recv_data >> guid >> unk1 >> item;
	recv_data >> unk2;
	recv_data >> bid >> buyout >> etime;

	Creature * pCreature = _player->GetMapMgr()->GetCreature( guid );
	if(  !pCreature || !pCreature->auctionHouse )
		return;		// NPC doesnt exist or isnt an auctioneer

	// Get item
	Item * pItem = _player->GetItemInterface()->GetItemByGUID(item);
	if( !pItem || pItem->IsSoulbound() || pItem->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAG_CONJURED ) )
	{
		sStackWolrdPacket( data,SMSG_AUCTION_COMMAND_RESULT, 50);
		data << uint32(0);
		data << uint32(AUCTION_CREATE);
		data << uint32(AUCTION_ERROR_ITEM);
		SendPacket(&data);
		return;
	};

	AuctionHouse * ah = pCreature->auctionHouse;

	uint32 item_worth = pItem->GetProto()->SellPrice * pItem->GetUInt32Value(ITEM_FIELD_STACK_COUNT);
	uint32 item_deposit = (uint32)(item_worth * ah->deposit_percent) * (uint32)(etime / 240.0f); // deposit is per 4 hours

	if (_player->GetGold() < item_deposit)	// player cannot afford deposit
	{
		sStackWolrdPacket( data,SMSG_AUCTION_COMMAND_RESULT, 50);
		data << uint32(0);
		data << uint32(AUCTION_CREATE);
		data << uint32(AUCTION_ERROR_MONEY);
		SendPacket(&data);
		return;
	}

	pItem = _player->GetItemInterface()->SafeRemoveAndRetreiveItemByGuid(item, false);
	if (!pItem){
		sStackWolrdPacket( data, SMSG_AUCTION_COMMAND_RESULT, 50);
		data << uint32(0);
		data << uint32(AUCTION_CREATE);
		data << uint32(AUCTION_ERROR_ITEM);
		SendPacket(&data);
		return;
	};

	if( pItem->IsInWorld() )
	{
		pItem->RemoveFromWorld();
	}

	pItem->SetOwner(NULL);
	pItem->m_isDirty = true;
	pItem->SaveToDB(INVENTORY_SLOT_NOT_SET, 0, true, NULL);

	// Create auction
	Auction * auct = new Auction;
	auct->BuyoutPrice = buyout;
	auct->ExpiryTime = (uint32)UNIXTIME + (etime * 60);
	auct->StartingPrice = bid;
	auct->HighestBid = 0;
	auct->HighestBidder = 0;	// hm
	auct->Id = sAuctionMgr.GenerateAuctionId();
	auct->Owner = _player->GetLowGUID();
	auct->pItem = pItem;
	auct->Deleted = false;
	auct->DeletedReason = 0;
	auct->DepositAmount = item_deposit;

	// remove deposit
	_player->ModGold(-(int32)item_deposit);

	// Add and save auction to DB
	ah->AddAuction(auct);
	auct->SaveToDB(ah->GetID());

	// Send result packet
	sStackWolrdPacket( data,SMSG_AUCTION_COMMAND_RESULT, 50);
	data << auct->Id;
	data << uint32(AUCTION_CREATE);
	data << uint32(AUCTION_ERROR_NONE);
	SendPacket(&data);
}
void WorldSession::HandleAuctionSellItem(WorldPacket& recv_data)
{
    CHECK_INWORLD_RETURN

    uint64 auctioneer, bid, buyout;
    uint32 itemsCount, etime;
    recv_data >> auctioneer;
    recv_data >> itemsCount;

    uint64 itemGUIDs[MAX_AUCTION_ITEMS]; // 160 slot = 4x 36 slot bag + backpack 16 slot
    uint32 count[MAX_AUCTION_ITEMS];
    
    for (uint32 i = 0; i < itemsCount; ++i)
    {
        recv_data >> itemGUIDs[i];
        recv_data >> count[i];

        if (!itemGUIDs[i] || !count[i] || count[i] > 1000)
            return;
    }

    recv_data >> bid;
    recv_data >> buyout;
    recv_data >> etime;

    if (!bid || !etime)
        return;

    Creature* pCreature = _player->GetMapMgr()->GetCreature(GET_LOWGUID_PART(auctioneer));
    if (!pCreature || !pCreature->auctionHouse)
        return;        // NPC doesn't exist or isn't an auctioneer

    AuctionHouse* ah = pCreature->auctionHouse;

    etime *= MINUTE;

    switch (etime)
    {
    case 1 * MIN_AUCTION_TIME:
    case 2 * MIN_AUCTION_TIME:
    case 4 * MIN_AUCTION_TIME:
        break;
    default:
        return;
    }

    Item* items[MAX_AUCTION_ITEMS];

    uint32 finalCount = 0;

    for (uint32 i = 0; i < itemsCount; ++i)
    {
        Item* item = _player->GetItemInterface()->GetItemByGUID(itemGUIDs[i]);

        if (!item)
        {
            _player->SendAuctionCommandResult(NULL, AUCTION_CREATE, ERR_AUCTION_ITEM_NOT_FOUND);
            return;
        }


        items[i] = item;
        finalCount += count[i];
    }

    if (!finalCount)
    {
        _player->SendAuctionCommandResult(NULL, AUCTION_CREATE, ERR_AUCTION_DATABASE_ERROR);
        return;
    }

    for (uint32 i = 0; i < itemsCount; ++i)
    {
        Item* item = items[i];

        if (item->GetStackCount() < finalCount)
        {
            _player->SendAuctionCommandResult(NULL, AUCTION_CREATE, ERR_AUCTION_DATABASE_ERROR);
            return;
        }
    }

    for (uint32 i = 0; i < itemsCount; ++i)
    {
        Item* item = items[i];
        uint32 auctionTime = uint32(etime);

        AuctionHouse* ah = pCreature->auctionHouse;

        uint32 item_worth = item->GetProto()->SellPrice * item->GetStackCount();
        uint32 item_deposit = (uint32)(item_worth * ah->deposit_percent) * (uint32)(etime / 240.0f); // deposit is per 4 hours

        if (!_player->HasGold((uint64)item_deposit))
        {
            _player->SendAuctionCommandResult(NULL, AUCTION_CREATE, ERR_AUCTION_NOT_ENOUGHT_MONEY);
            return;
        }

        _player->TakeGold(-int32(item_deposit));

        item = _player->GetItemInterface()->SafeRemoveAndRetreiveItemByGuid(itemGUIDs[i], false);
        if (!item)
        {
            _player->SendAuctionCommandResult(NULL, AUCTION_CREATE, ERR_AUCTION_ITEM_NOT_FOUND);
            return;
        };

        if (item->IsInWorld())
        {
            item->RemoveFromWorld();
        }

        item->SetOwner(NULL);
        item->m_isDirty = true;
        item->SaveToDB(INVENTORY_SLOT_NOT_SET, 0, true, NULL);

        // Create auction
        Auction* auct = new Auction;
        auct->BuyoutPrice = buyout;
        auct->ExpiryTime = (uint32)UNIXTIME + (etime * 60);
        auct->StartingPrice = bid;
        auct->HighestBid = 0;
        auct->HighestBidder = 0;    // hm
        auct->Id = sAuctionMgr.GenerateAuctionId();
        auct->Owner = _player->GetLowGUID();
        auct->pItem = item;
        auct->Deleted = false;
        auct->DeletedReason = 0;
        auct->DepositAmount = item_deposit;

        // Add and save auction to DB
        ah->AddAuction(auct);
        auct->SaveToDB(ah->GetID());

        // Send result packet
        _player->SendAuctionCommandResult(NULL, AUCTION_CREATE, ERR_AUCTION_OK);
    }

    ah->SendOwnerListPacket(_player, &recv_data);
}