TradeController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.Statement;
  20. import java.util.Collection;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastList;
  26. import javolution.util.FastMap;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.datatables.ItemTable;
  30. import com.l2jserver.gameserver.model.L2TradeList;
  31. import com.l2jserver.gameserver.model.L2TradeList.L2TradeItem;
  32. /**
  33. * This class ...
  34. * @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
  35. */
  36. public class TradeController
  37. {
  38. private static Logger _log = Logger.getLogger(TradeController.class.getName());
  39. private int _nextListId;
  40. private final Map<Integer, L2TradeList> _lists = new FastMap<>();
  41. protected TradeController()
  42. {
  43. _lists.clear();
  44. // Initialize Shop buy list
  45. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  46. Statement s = con.createStatement();
  47. ResultSet rs1 = s.executeQuery("SELECT shop_id, npc_id FROM merchant_shopids"))
  48. {
  49. int itemId, price, maxCount, currentCount, time;
  50. long saveTimer;
  51. 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"))
  52. {
  53. while (rs1.next())
  54. {
  55. ps.setString(1, String.valueOf(rs1.getInt("shop_id")));
  56. try (ResultSet rs2 = ps.executeQuery())
  57. {
  58. ps.clearParameters();
  59. int shopId = rs1.getInt("shop_id");
  60. L2TradeList buy1 = new L2TradeList(shopId);
  61. while (rs2.next())
  62. {
  63. itemId = rs2.getInt("item_id");
  64. price = rs2.getInt("price");
  65. maxCount = rs2.getInt("count");
  66. currentCount = rs2.getInt("currentCount");
  67. time = rs2.getInt("time");
  68. saveTimer = rs2.getLong("saveTimer");
  69. L2TradeItem item = new L2TradeItem(shopId, itemId);
  70. if (ItemTable.getInstance().getTemplate(itemId) == null)
  71. {
  72. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item.");
  73. continue;
  74. }
  75. if (price <= -1)
  76. {
  77. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  78. }
  79. item.setPrice(price);
  80. item.setRestoreDelay(time);
  81. item.setNextRestoreTime(saveTimer);
  82. item.setMaxCount(maxCount);
  83. if (currentCount > -1)
  84. {
  85. item.setCurrentCount(currentCount);
  86. }
  87. else
  88. {
  89. item.setCurrentCount(maxCount);
  90. }
  91. buy1.addItem(item);
  92. }
  93. buy1.setNpcId(rs1.getString("npc_id"));
  94. _lists.put(buy1.getListId(), buy1);
  95. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  96. }
  97. }
  98. }
  99. _log.info("TradeController: Loaded " + _lists.size() + " Buylists.");
  100. }
  101. catch (Exception e)
  102. {
  103. // problem with initializing spawn, go to next one
  104. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  105. }
  106. // If enabled, initialize the custom buy list
  107. if (Config.CUSTOM_MERCHANT_TABLES)
  108. {
  109. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  110. Statement s = con.createStatement();
  111. ResultSet rset1 = s.executeQuery("SELECT shop_id, npc_id FROM custom_merchant_shopids"))
  112. {
  113. int initialSize = _lists.size();
  114. int itemId, price, maxCount, currentCount, time;
  115. long saveTimer;
  116. 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");
  117. while (rset1.next())
  118. {
  119. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  120. ResultSet rset = statement.executeQuery();
  121. statement.clearParameters();
  122. int shopId = rset1.getInt("shop_id");
  123. L2TradeList buy1 = new L2TradeList(shopId);
  124. while (rset.next())
  125. {
  126. itemId = rset.getInt("item_id");
  127. price = rset.getInt("price");
  128. maxCount = rset.getInt("count");
  129. currentCount = rset.getInt("currentCount");
  130. time = rset.getInt("time");
  131. saveTimer = rset.getLong("saveTimer");
  132. L2TradeItem item = new L2TradeItem(shopId, itemId);
  133. if (ItemTable.getInstance().getTemplate(itemId) == null)
  134. {
  135. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item.");
  136. continue;
  137. }
  138. if (price <= -1)
  139. {
  140. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  141. }
  142. item.setPrice(price);
  143. item.setRestoreDelay(time);
  144. item.setNextRestoreTime(saveTimer);
  145. item.setMaxCount(maxCount);
  146. if (currentCount > -1)
  147. {
  148. item.setCurrentCount(currentCount);
  149. }
  150. else
  151. {
  152. item.setCurrentCount(maxCount);
  153. }
  154. buy1.addItem(item);
  155. }
  156. buy1.setNpcId(rset1.getString("npc_id"));
  157. _lists.put(buy1.getListId(), buy1);
  158. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  159. rset.close();
  160. }
  161. statement.close();
  162. rset1.close();
  163. _log.info("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  164. }
  165. catch (Exception e)
  166. {
  167. // problem with initializing spawn, go to next one
  168. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  169. }
  170. }
  171. }
  172. public L2TradeList getBuyList(int listId)
  173. {
  174. return _lists.get(listId);
  175. }
  176. public List<L2TradeList> getBuyListByNpcId(int npcId)
  177. {
  178. List<L2TradeList> lists = new FastList<>();
  179. Collection<L2TradeList> values = _lists.values();
  180. for (L2TradeList list : values)
  181. {
  182. String tradeNpcId = list.getNpcId();
  183. if (tradeNpcId.startsWith("gm"))
  184. {
  185. continue;
  186. }
  187. if (npcId == Integer.parseInt(tradeNpcId))
  188. {
  189. lists.add(list);
  190. }
  191. }
  192. return lists;
  193. }
  194. public void dataCountStore()
  195. {
  196. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  197. PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount = ? WHERE item_id = ? AND shop_id = ?"))
  198. {
  199. for (L2TradeList list : _lists.values())
  200. {
  201. if (list.hasLimitedStockItem())
  202. {
  203. for (L2TradeItem item : list.getItems())
  204. {
  205. long currentCount;
  206. if (item.hasLimitedStock() && ((currentCount = item.getCurrentCount()) < item.getMaxCount()))
  207. {
  208. statement.setLong(1, currentCount);
  209. statement.setInt(2, item.getItemId());
  210. statement.setInt(3, list.getListId());
  211. statement.executeUpdate();
  212. statement.clearParameters();
  213. }
  214. }
  215. }
  216. }
  217. }
  218. catch (Exception e)
  219. {
  220. _log.log(Level.SEVERE, "TradeController: Could not store Count Item: " + e.getMessage(), e);
  221. }
  222. }
  223. /**
  224. * @return
  225. */
  226. public synchronized int getNextId()
  227. {
  228. return _nextListId++;
  229. }
  230. public static TradeController getInstance()
  231. {
  232. return SingletonHolder._instance;
  233. }
  234. private static class SingletonHolder
  235. {
  236. protected static final TradeController _instance = new TradeController();
  237. }
  238. }