/*
* Copyright (C) 2004-2015 L2J Server
*
* This file is part of L2J Server.
*
* L2J Server 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 3 of the License, or
* (at your option) any later version.
*
* L2J Server 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/>.
*/
package com.l2jserver.gameserver.network.clientpackets;
import static com.l2jserver.gameserver.model.actor.L2Npc.INTERACTION_DISTANCE;
import java.util.HashSet;
import com.l2jserver.Config;
import com.l2jserver.gameserver.data.sql.impl.OfflineTradersTable;
import com.l2jserver.gameserver.enums.PrivateStoreType;
import com.l2jserver.gameserver.model.ItemRequest;
import com.l2jserver.gameserver.model.L2Object;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.TradeList;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
import com.l2jserver.gameserver.util.Util;
/**
* This class ...
* @version $Revision: 1.2.2.1.2.5 $ $Date: 2005/03/27 15:29:30 $
*/
public final class RequestPrivateStoreBuy extends L2GameClientPacket
{
private static final String _C__83_REQUESTPRIVATESTOREBUY = "[C] 83 RequestPrivateStoreBuy";
private static final int BATCH_LENGTH = 20; // length of the one item
private int _storePlayerId;
private HashSet<ItemRequest> _items = null;
@Override
protected void readImpl()
{
_storePlayerId = readD();
int count = readD();
if ((count <= 0) || (count > Config.MAX_ITEM_IN_PACKET) || ((count * BATCH_LENGTH) != _buf.remaining()))
{
return;
}
_items = new HashSet<>();
for (int i = 0; i < count; i++)
{
int objectId = readD();
long cnt = readQ();
long price = readQ();
if ((objectId < 1) || (cnt < 1) || (price < 0))
{
_items = null;
return;
}
_items.add(new ItemRequest(objectId, cnt, price));
}
}
@Override
protected void runImpl()
{
L2PcInstance player = getActiveChar();
if (player == null)
{
return;
}
if (_items == null)
{
sendPacket(ActionFailed.STATIC_PACKET);
return;
}
if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("privatestorebuy"))
{
player.sendMessage("You are buying items too fast.");
return;
}
L2Object object = L2World.getInstance().getPlayer(_storePlayerId);
if (object == null)
{
return;
}
if (player.isCursedWeaponEquipped())
{
return;
}
L2PcInstance storePlayer = (L2PcInstance) object;
if (!player.isInsideRadius(storePlayer, INTERACTION_DISTANCE, true, false))
{
return;
}
if ((player.getInstanceId() != storePlayer.getInstanceId()) && (player.getInstanceId() != -1))
{
return;
}
if (!((storePlayer.getPrivateStoreType() == PrivateStoreType.SELL) || (storePlayer.getPrivateStoreType() == PrivateStoreType.PACKAGE_SELL)))
{
return;
}
if (Config.FACTION_SYSTEM_ENABLED)
{
if ((storePlayer.isEvil() && player.isGood()) || (storePlayer.isGood() && player.isEvil()))
{
player.sendMessage("You cant buy from different faction members.");
return;
}
}
TradeList storeList = storePlayer.getSellList();
if (storeList == null)
{
return;
}
if (!player.getAccessLevel().allowTransaction())
{
player.sendMessage("Transactions are disabled for your Access Level.");
sendPacket(ActionFailed.STATIC_PACKET);
return;
}
if (storePlayer.getPrivateStoreType() == PrivateStoreType.PACKAGE_SELL)
{
if (storeList.getItemCount() > _items.size())
{
String msgErr = "[RequestPrivateStoreBuy] player " + getClient().getActiveChar().getName() + " tried to buy less items than sold by package-sell, ban this player for bot usage!";
Util.handleIllegalPlayerAction(getClient().getActiveChar(), msgErr, Config.DEFAULT_PUNISH);
return;
}
}
int result = storeList.privateStoreBuy(player, _items);
if (result > 0)
{
sendPacket(ActionFailed.STATIC_PACKET);
if (result > 1)
{
_log.warning("PrivateStore buy has failed due to invalid list or request. Player: " + player.getName() + ", Private store of: " + storePlayer.getName());
}
return;
}
// Update offline trade record, if realtime saving is enabled
if (Config.OFFLINE_TRADE_ENABLE && Config.STORE_OFFLINE_TRADE_IN_REALTIME && ((storePlayer.getClient() == null) || storePlayer.getClient().isDetached()))
{
OfflineTradersTable.onTransaction(storePlayer, storeList.getItemCount() == 0, false);
}
if (storeList.getItemCount() == 0)
{
storePlayer.setPrivateStoreType(PrivateStoreType.NONE);
storePlayer.broadcastUserInfo();
}
}
@Override
public String getType()
{
return _C__83_REQUESTPRIVATESTOREBUY;
}
@Override
protected boolean triggersOnActionRequest()
{
return false;
}
}