<?php
/*
* 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 3 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/>.
*/
package com.l2emu.gameserver.datatables;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.l2emu.Config;
import com.l2emu.L2DatabaseFactory;
import com.l2emu.gameserver.idfactory.IdFactory;
import com.l2emu.gameserver.model.L2ManufactureItem;
import com.l2emu.gameserver.model.L2ManufactureList;
import com.l2emu.gameserver.model.L2World;
import com.l2emu.gameserver.model.TradeList.TradeItem;
import com.l2emu.gameserver.model.actor.instance.L2PcInstance;
public class OfflineTradersTable
{
private final static Log _log = LogFactory.getLog(OfflineTradersTable.class);
public static void storeTradeItems()
{
System.out.println("Store offline traders...");
Connection con = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement stm1 = con.prepareStatement("insert into `offline_characters` values(?,?,?,?,?)");
PreparedStatement stm2 = con.prepareStatement("insert into `offline_shops` values(?,?,?,?)");
for(L2PcInstance pc : L2World.getInstance().getAllPlayers().values())
{
if(pc.isOfflineTrade())
synchronized(pc)
{
int shopId = IdFactory.getInstance().getNextId();
stm1.setInt(1, pc.getObjectId());
stm1.setInt(2, shopId);
stm1.setInt(3, pc.getPrivateStoreType());
String title = "";
int isPackage = 0;
if(pc.getPrivateStoreType()==L2PcInstance.STORE_PRIVATE_BUY)
{
title = pc.getBuyList().getTitle();
isPackage = pc.getBuyList().isPackaged()?1:0;
}
else if(pc.getPrivateStoreType()==L2PcInstance.STORE_PRIVATE_SELL ||
pc.getPrivateStoreType()== L2PcInstance.STORE_PRIVATE_PACKAGE_SELL)
{
title = pc.getSellList().getTitle();
isPackage = pc.getSellList().isPackaged()||pc.getPrivateStoreType()== L2PcInstance.STORE_PRIVATE_PACKAGE_SELL ?1:0;
}
else
title = pc.getCreateList().getStoreName();
stm1.setInt(4, isPackage);
stm1.setString(5, title);
stm1.execute();
stm2.setInt(1, shopId);
switch(pc.getPrivateStoreType())
{
case L2PcInstance.STORE_PRIVATE_BUY:
for(TradeItem it: pc.getBuyList().getItems() )
{
stm2.setInt(2, it.getItem().getItemId());
stm2.setLong(3, it.getCount());
stm2.setLong(4, it.getPrice());
stm2.execute();
}
break;
case L2PcInstance.STORE_PRIVATE_PACKAGE_SELL:
case L2PcInstance.STORE_PRIVATE_SELL:
for(TradeItem it: pc.getSellList().getItems())
{
stm2.setInt(2, it.getObjectId());
stm2.setLong(3, it.getCount());
stm2.setLong(4, it.getPrice());
stm2.execute();
}
break;
case L2PcInstance.STORE_PRIVATE_MANUFACTURE:
for(L2ManufactureItem it: pc.getCreateList().getList())
{
stm2.setInt(2, it.getRecipeId() );
stm2.setInt(3, 0);
stm2.setLong(4, it.getCost());
stm2.execute();
}
break;
}
}
}
stm1.close();
stm2.close();
}
catch (Exception e)
{
System.out.println("OfflineTradersTable[storeTradeItems()]: Error while saving offline traders: " + e);
e.printStackTrace();
}
finally
{
L2DatabaseFactory.close(con);
}
}
public static void loadOffliners()
{
System.out.print("Load offline traders...");
Connection con = null;
int nTraders=0;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement stm = con.prepareStatement("Select charId, shopid, mode, packaged, title from offline_characters");
PreparedStatement stm2 = con.prepareStatement("select itemid, count, price from offline_shops where shopid=?");
ResultSet rs = stm.executeQuery();
while(rs.next())
{
L2PcInstance offliner = L2PcInstance.load(rs.getInt(1));
if(offliner!=null) try
{
stm2.setInt(1, rs.getInt(2));
ResultSet rs2 = stm2.executeQuery();
while (rs2.next())
{
switch(rs.getInt(3))
{
case L2PcInstance.STORE_PRIVATE_BUY:
offliner.getBuyList().addItemByItemId(rs2.getInt(1), rs2.getInt(2), rs2.getInt(3));
break;
case L2PcInstance.STORE_PRIVATE_SELL:
offliner.getSellList().addItem(rs2.getInt(1), rs2.getInt(2), rs2.getInt(3));
break;
case L2PcInstance.STORE_PRIVATE_MANUFACTURE:
offliner.getCreateList().add(new L2ManufactureItem(rs2.getInt(1),rs2.getInt(3)));
break;
}
}
rs2.close();
offliner.spawnMe();
offliner.sitDown();
if (Config.OFFLINE_SET_NAME_COLOR)
offliner.getAppearance().setNameColor(Config.OFFLINE_NAME_COLOR);
offliner.setPrivateStoreType(rs.getInt(3));
switch(rs.getInt(3))
{
case L2PcInstance.STORE_PRIVATE_BUY:
offliner.getBuyList().setTitle(rs.getString(5));
offliner.getBuyList().setPackaged(rs.getInt(4)!=0);
break;
case L2PcInstance.STORE_PRIVATE_SELL:
offliner.getSellList().setTitle(rs.getString(5));
offliner.getSellList().setPackaged(rs.getInt(4)!=0);
break;
case L2PcInstance.STORE_PRIVATE_MANUFACTURE:
offliner.getCreateList().setStoreName(rs.getString(5));
break;
}
offliner.setOfflineTrade(true);
offliner.broadcastUserInfo();
nTraders++;
}
catch(Exception e)
{
e.printStackTrace();
offliner.decayMe();
offliner = null;
}
}
stm2.close();
rs.close();
stm.close();
stm = con.prepareStatement("delete from offline_characters");
stm.execute();
stm.close();
stm = con.prepareStatement("delete from offline_shops");
stm.execute();
stm.close();
_log.info(nTraders+" trader(s) loaded");
}
catch (Exception e)
{
_log.error("OfflineTradersTable[loadOffliners()]: Error while loading offline traders: " + e);
}
finally
{
L2DatabaseFactory.close(con);
}
}
}