TradeController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. *
  35. * @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
  36. */
  37. public class TradeController
  38. {
  39. private static Logger _log = Logger.getLogger(TradeController.class.getName());
  40. private int _nextListId;
  41. private Map<Integer, L2TradeList> _lists = new FastMap<>();
  42. public static TradeController getInstance()
  43. {
  44. return SingletonHolder._instance;
  45. }
  46. protected TradeController()
  47. {
  48. _lists.clear();
  49. // Initialize Shop buy list
  50. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  51. Statement s = con.createStatement();
  52. ResultSet rs1 = s.executeQuery("SELECT shop_id, npc_id FROM merchant_shopids"))
  53. {
  54. int itemId, price, maxCount, currentCount, time;
  55. long saveTimer;
  56. try (PreparedStatement ps = con.prepareStatement("SELECT item_id, price, shop_id, "
  57. + L2DatabaseFactory.getInstance().safetyString("order")
  58. + ", count, currentCount, time, savetimer FROM merchant_buylists WHERE shop_id=? ORDER BY "
  59. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC"))
  60. {
  61. while (rs1.next())
  62. {
  63. ps.setString(1, String.valueOf(rs1.getInt("shop_id")));
  64. try (ResultSet rs2 = ps.executeQuery())
  65. {
  66. ps.clearParameters();
  67. int shopId = rs1.getInt("shop_id");
  68. L2TradeList buy1 = new L2TradeList(shopId);
  69. while (rs2.next())
  70. {
  71. itemId = rs2.getInt("item_id");
  72. price = rs2.getInt("price");
  73. maxCount = rs2.getInt("count");
  74. currentCount = rs2.getInt("currentCount");
  75. time = rs2.getInt("time");
  76. saveTimer = rs2.getLong("saveTimer");
  77. L2TradeItem item = new L2TradeItem(shopId, itemId);
  78. if (ItemTable.getInstance().getTemplate(itemId) == null)
  79. {
  80. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item.");
  81. continue;
  82. }
  83. if (price <= -1)
  84. {
  85. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  86. }
  87. item.setPrice(price);
  88. item.setRestoreDelay(time);
  89. item.setNextRestoreTime(saveTimer);
  90. item.setMaxCount(maxCount);
  91. if (currentCount > -1)
  92. {
  93. item.setCurrentCount(currentCount);
  94. }
  95. else
  96. {
  97. item.setCurrentCount(maxCount);
  98. }
  99. buy1.addItem(item);
  100. }
  101. buy1.setNpcId(rs1.getString("npc_id"));
  102. _lists.put(buy1.getListId(), buy1);
  103. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  104. }
  105. }
  106. }
  107. _log.info("TradeController: Loaded " + _lists.size() + " Buylists.");
  108. }
  109. catch (Exception e)
  110. {
  111. // problem with initializing spawn, go to next one
  112. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  113. }
  114. // If enabled, initialize the custom buy list
  115. if (Config.CUSTOM_MERCHANT_TABLES)
  116. {
  117. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  118. Statement s = con.createStatement();
  119. ResultSet rset1 = s.executeQuery("SELECT shop_id, npc_id FROM custom_merchant_shopids"))
  120. {
  121. int initialSize = _lists.size();
  122. int itemId, price, maxCount, currentCount, time;
  123. long saveTimer;
  124. PreparedStatement statement = con.prepareStatement("SELECT item_id, price, shop_id, "
  125. + L2DatabaseFactory.getInstance().safetyString("order")
  126. + ", count, currentCount, time, savetimer FROM custom_merchant_buylists WHERE shop_id=? ORDER BY "
  127. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC");
  128. while (rset1.next())
  129. {
  130. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  131. ResultSet rset = statement.executeQuery();
  132. statement.clearParameters();
  133. int shopId = rset1.getInt("shop_id");
  134. L2TradeList buy1 = new L2TradeList(shopId);
  135. while (rset.next())
  136. {
  137. itemId = rset.getInt("item_id");
  138. price = rset.getInt("price");
  139. maxCount = rset.getInt("count");
  140. currentCount = rset.getInt("currentCount");
  141. time = rset.getInt("time");
  142. saveTimer = rset.getLong("saveTimer");
  143. L2TradeItem item = new L2TradeItem(shopId, itemId);
  144. if (ItemTable.getInstance().getTemplate(itemId) == null)
  145. {
  146. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId()
  147. + ", missing data for that item.");
  148. continue;
  149. }
  150. if (price <= -1)
  151. {
  152. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  153. }
  154. item.setPrice(price);
  155. item.setRestoreDelay(time);
  156. item.setNextRestoreTime(saveTimer);
  157. item.setMaxCount(maxCount);
  158. if (currentCount > -1)
  159. {
  160. item.setCurrentCount(currentCount);
  161. }
  162. else
  163. {
  164. item.setCurrentCount(maxCount);
  165. }
  166. buy1.addItem(item);
  167. }
  168. buy1.setNpcId(rset1.getString("npc_id"));
  169. _lists.put(buy1.getListId(), buy1);
  170. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  171. rset.close();
  172. }
  173. statement.close();
  174. rset1.close();
  175. _log.info("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  176. }
  177. catch (Exception e)
  178. {
  179. // problem with initializing spawn, go to next one
  180. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  181. }
  182. }
  183. }
  184. public L2TradeList getBuyList(int listId)
  185. {
  186. return _lists.get(listId);
  187. }
  188. public List<L2TradeList> getBuyListByNpcId(int npcId)
  189. {
  190. List<L2TradeList> lists = new FastList<>();
  191. Collection<L2TradeList> values = _lists.values();
  192. for (L2TradeList list : values)
  193. {
  194. String tradeNpcId = list.getNpcId();
  195. if (tradeNpcId.startsWith("gm"))
  196. continue;
  197. if (npcId == Integer.parseInt(tradeNpcId))
  198. lists.add(list);
  199. }
  200. return lists;
  201. }
  202. public void dataCountStore()
  203. {
  204. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  205. PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount = ? WHERE item_id = ? AND shop_id = ?"))
  206. {
  207. for (L2TradeList list : _lists.values())
  208. {
  209. if (list.hasLimitedStockItem())
  210. {
  211. for (L2TradeItem item : list.getItems())
  212. {
  213. long currentCount;
  214. if (item.hasLimitedStock() && (currentCount = item.getCurrentCount()) < item.getMaxCount())
  215. {
  216. statement.setLong(1, currentCount);
  217. statement.setInt(2, item.getItemId());
  218. statement.setInt(3, list.getListId());
  219. statement.executeUpdate();
  220. statement.clearParameters();
  221. }
  222. }
  223. }
  224. }
  225. }
  226. catch (Exception e)
  227. {
  228. _log.log(Level.SEVERE, "TradeController: Could not store Count Item: " + e.getMessage(), e);
  229. }
  230. }
  231. /**
  232. * @return
  233. */
  234. public synchronized int getNextId()
  235. {
  236. return _nextListId++;
  237. }
  238. private static class SingletonHolder
  239. {
  240. protected static final TradeController _instance = new TradeController();
  241. }
  242. }