TradeController.java 8.3 KB

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