123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- /*
- * 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.l2jserver.gameserver;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.util.FastList;
- import javolution.util.FastMap;
- import com.l2jserver.Config;
- import com.l2jserver.L2DatabaseFactory;
- import com.l2jserver.gameserver.datatables.ItemTable;
- import com.l2jserver.gameserver.model.L2TradeList;
- import com.l2jserver.gameserver.model.L2TradeList.L2TradeItem;
- /**
- * This class ...
- *
- * @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
- */
- public class TradeController
- {
- private static Logger _log = Logger.getLogger(TradeController.class.getName());
-
- private int _nextListId;
- private Map<Integer, L2TradeList> _lists = new FastMap<>();
-
- public static TradeController getInstance()
- {
- return SingletonHolder._instance;
- }
-
- protected TradeController()
- {
- _lists.clear();
- // Initialize Shop buy list
- try (Connection con = L2DatabaseFactory.getInstance().getConnection();
- Statement s = con.createStatement();
- ResultSet rs1 = s.executeQuery("SELECT shop_id, npc_id FROM merchant_shopids"))
- {
- int itemId, price, maxCount, currentCount, time;
- long saveTimer;
- try (PreparedStatement ps = con.prepareStatement("SELECT item_id, price, shop_id, "
- + L2DatabaseFactory.getInstance().safetyString("order")
- + ", count, currentCount, time, savetimer FROM merchant_buylists WHERE shop_id=? ORDER BY "
- + L2DatabaseFactory.getInstance().safetyString("order") + " ASC"))
- {
- while (rs1.next())
- {
- ps.setString(1, String.valueOf(rs1.getInt("shop_id")));
- try (ResultSet rs2 = ps.executeQuery())
- {
- ps.clearParameters();
-
- int shopId = rs1.getInt("shop_id");
- L2TradeList buy1 = new L2TradeList(shopId);
-
- while (rs2.next())
- {
- itemId = rs2.getInt("item_id");
- price = rs2.getInt("price");
- maxCount = rs2.getInt("count");
- currentCount = rs2.getInt("currentCount");
- time = rs2.getInt("time");
- saveTimer = rs2.getLong("saveTimer");
-
- L2TradeItem item = new L2TradeItem(shopId, itemId);
- if (ItemTable.getInstance().getTemplate(itemId) == null)
- {
- _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item.");
- continue;
- }
-
- if (price <= -1)
- {
- price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
- }
-
- if (Config.DEBUG)
- {
- // debug
- double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
- if (diff < 0.8 || diff > 1.2)
- {
- _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
- + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
- + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
- }
- }
-
- item.setPrice(price);
-
- item.setRestoreDelay(time);
- item.setNextRestoreTime(saveTimer);
- item.setMaxCount(maxCount);
-
- if (currentCount > -1)
- {
- item.setCurrentCount(currentCount);
- }
- else
- {
- item.setCurrentCount(maxCount);
- }
-
- buy1.addItem(item);
- }
-
- buy1.setNpcId(rs1.getString("npc_id"));
- _lists.put(buy1.getListId(), buy1);
- _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
- }
- }
- }
- _log.info("TradeController: Loaded " + _lists.size() + " Buylists.");
- }
- catch (Exception e)
- {
- // problem with initializing spawn, go to next one
- _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
- }
-
- // If enabled, initialize the custom buy list
- if (Config.CUSTOM_MERCHANT_TABLES)
- {
- try (Connection con = L2DatabaseFactory.getInstance().getConnection();
- Statement s = con.createStatement();
- ResultSet rset1 = s.executeQuery("SELECT shop_id, npc_id FROM custom_merchant_shopids"))
- {
- int initialSize = _lists.size();
- int itemId, price, maxCount, currentCount, time;
- long saveTimer;
- PreparedStatement statement = con.prepareStatement("SELECT item_id, price, shop_id, "
- + L2DatabaseFactory.getInstance().safetyString("order")
- + ", count, currentCount, time, savetimer FROM custom_merchant_buylists WHERE shop_id=? ORDER BY "
- + L2DatabaseFactory.getInstance().safetyString("order") + " ASC");
- while (rset1.next())
- {
- statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
- ResultSet rset = statement.executeQuery();
- statement.clearParameters();
-
- int shopId = rset1.getInt("shop_id");
- L2TradeList buy1 = new L2TradeList(shopId);
-
- while (rset.next())
- {
- itemId = rset.getInt("item_id");
- price = rset.getInt("price");
- maxCount = rset.getInt("count");
- currentCount = rset.getInt("currentCount");
- time = rset.getInt("time");
- saveTimer = rset.getLong("saveTimer");
-
- L2TradeItem item = new L2TradeItem(shopId, itemId);
- if (ItemTable.getInstance().getTemplate(itemId) == null)
- {
- _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId()
- + ", missing data for that item.");
- continue;
- }
-
- if (price <= -1)
- {
- price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
- }
-
- if (Config.DEBUG)
- {
- // debug
- double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
- if (diff < 0.8 || diff > 1.2)
- {
- _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
- + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
- + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
- }
- }
-
- item.setPrice(price);
-
- item.setRestoreDelay(time);
- item.setNextRestoreTime(saveTimer);
- item.setMaxCount(maxCount);
-
- if (currentCount > -1)
- {
- item.setCurrentCount(currentCount);
- }
- else
- {
- item.setCurrentCount(maxCount);
- }
-
- buy1.addItem(item);
- }
-
- buy1.setNpcId(rset1.getString("npc_id"));
- _lists.put(buy1.getListId(), buy1);
- _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
-
- rset.close();
- }
- statement.close();
- rset1.close();
-
- _log.info("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
-
- }
- catch (Exception e)
- {
- // problem with initializing spawn, go to next one
- _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
- }
- }
- }
-
- public L2TradeList getBuyList(int listId)
- {
- return _lists.get(listId);
- }
-
- public List<L2TradeList> getBuyListByNpcId(int npcId)
- {
- List<L2TradeList> lists = new FastList<>();
- Collection<L2TradeList> values = _lists.values();
-
- for (L2TradeList list : values)
- {
- String tradeNpcId = list.getNpcId();
- if (tradeNpcId.startsWith("gm"))
- continue;
- if (npcId == Integer.parseInt(tradeNpcId))
- lists.add(list);
- }
- return lists;
- }
-
- public void dataCountStore()
- {
- try (Connection con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount = ? WHERE item_id = ? AND shop_id = ?"))
- {
- for (L2TradeList list : _lists.values())
- {
- if (list.hasLimitedStockItem())
- {
- for (L2TradeItem item : list.getItems())
- {
- long currentCount;
- if (item.hasLimitedStock() && (currentCount = item.getCurrentCount()) < item.getMaxCount())
- {
- statement.setLong(1, currentCount);
- statement.setInt(2, item.getItemId());
- statement.setInt(3, list.getListId());
- statement.executeUpdate();
- statement.clearParameters();
- }
- }
- }
- }
- }
- catch (Exception e)
- {
- _log.log(Level.SEVERE, "TradeController: Could not store Count Item: " + e.getMessage(), e);
- }
- }
-
- /**
- * @return
- */
- public synchronized int getNextId()
- {
- return _nextListId++;
- }
-
- private static class SingletonHolder
- {
- protected static final TradeController _instance = new TradeController();
- }
- }
|