TradeController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastList;
  30. import javolution.util.FastMap;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.datatables.ItemTable;
  34. import com.l2jserver.gameserver.model.L2TradeList;
  35. import com.l2jserver.gameserver.model.L2TradeList.L2TradeItem;
  36. public class TradeController
  37. {
  38. private static final 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. try (PreparedStatement ps = 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. {
  118. while (rset1.next())
  119. {
  120. ps.setString(1, String.valueOf(rset1.getInt("shop_id")));
  121. try (ResultSet rset = ps.executeQuery())
  122. {
  123. ps.clearParameters();
  124. int shopId = rset1.getInt("shop_id");
  125. L2TradeList buy1 = new L2TradeList(shopId);
  126. while (rset.next())
  127. {
  128. itemId = rset.getInt("item_id");
  129. price = rset.getInt("price");
  130. maxCount = rset.getInt("count");
  131. currentCount = rset.getInt("currentCount");
  132. time = rset.getInt("time");
  133. saveTimer = rset.getLong("saveTimer");
  134. L2TradeItem item = new L2TradeItem(shopId, itemId);
  135. if (ItemTable.getInstance().getTemplate(itemId) == null)
  136. {
  137. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item.");
  138. continue;
  139. }
  140. if (price <= -1)
  141. {
  142. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  143. }
  144. item.setPrice(price);
  145. item.setRestoreDelay(time);
  146. item.setNextRestoreTime(saveTimer);
  147. item.setMaxCount(maxCount);
  148. if (currentCount > -1)
  149. {
  150. item.setCurrentCount(currentCount);
  151. }
  152. else
  153. {
  154. item.setCurrentCount(maxCount);
  155. }
  156. buy1.addItem(item);
  157. }
  158. buy1.setNpcId(rset1.getString("npc_id"));
  159. _lists.put(buy1.getListId(), buy1);
  160. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  161. }
  162. }
  163. }
  164. _log.info("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  165. }
  166. catch (Exception e)
  167. {
  168. // problem with initializing spawn, go to next one
  169. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  170. }
  171. }
  172. }
  173. public L2TradeList getBuyList(int listId)
  174. {
  175. return _lists.get(listId);
  176. }
  177. public List<L2TradeList> getBuyListByNpcId(int npcId)
  178. {
  179. List<L2TradeList> lists = new FastList<>();
  180. Collection<L2TradeList> values = _lists.values();
  181. for (L2TradeList list : values)
  182. {
  183. String tradeNpcId = list.getNpcId();
  184. if (tradeNpcId.startsWith("gm"))
  185. {
  186. continue;
  187. }
  188. if (npcId == Integer.parseInt(tradeNpcId))
  189. {
  190. lists.add(list);
  191. }
  192. }
  193. return lists;
  194. }
  195. public void dataCountStore()
  196. {
  197. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  198. PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount = ? WHERE item_id = ? AND shop_id = ?"))
  199. {
  200. for (L2TradeList list : _lists.values())
  201. {
  202. if (list.hasLimitedStockItem())
  203. {
  204. for (L2TradeItem item : list.getItems())
  205. {
  206. long currentCount;
  207. if (item.hasLimitedStock() && ((currentCount = item.getCurrentCount()) < item.getMaxCount()))
  208. {
  209. statement.setLong(1, currentCount);
  210. statement.setInt(2, item.getItemId());
  211. statement.setInt(3, list.getListId());
  212. statement.executeUpdate();
  213. statement.clearParameters();
  214. }
  215. }
  216. }
  217. }
  218. }
  219. catch (Exception e)
  220. {
  221. _log.log(Level.SEVERE, "TradeController: Could not store Count Item: " + e.getMessage(), e);
  222. }
  223. }
  224. /**
  225. * @return
  226. */
  227. public synchronized int getNextId()
  228. {
  229. return _nextListId++;
  230. }
  231. public static TradeController getInstance()
  232. {
  233. return SingletonHolder._instance;
  234. }
  235. private static class SingletonHolder
  236. {
  237. protected static final TradeController _instance = new TradeController();
  238. }
  239. }