TradeController.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (Config.DEBUG)
  88. {
  89. // debug
  90. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  91. if (diff < 0.8 || diff > 1.2)
  92. {
  93. _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
  94. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  95. + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  96. }
  97. }
  98. item.setPrice(price);
  99. item.setRestoreDelay(time);
  100. item.setNextRestoreTime(saveTimer);
  101. item.setMaxCount(maxCount);
  102. if (currentCount > -1)
  103. {
  104. item.setCurrentCount(currentCount);
  105. }
  106. else
  107. {
  108. item.setCurrentCount(maxCount);
  109. }
  110. buy1.addItem(item);
  111. }
  112. buy1.setNpcId(rs1.getString("npc_id"));
  113. _lists.put(buy1.getListId(), buy1);
  114. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  115. }
  116. }
  117. }
  118. _log.info("TradeController: Loaded " + _lists.size() + " Buylists.");
  119. }
  120. catch (Exception e)
  121. {
  122. // problem with initializing spawn, go to next one
  123. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  124. }
  125. // If enabled, initialize the custom buy list
  126. if (Config.CUSTOM_MERCHANT_TABLES)
  127. {
  128. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  129. Statement s = con.createStatement();
  130. ResultSet rset1 = s.executeQuery("SELECT shop_id, npc_id FROM custom_merchant_shopids"))
  131. {
  132. int initialSize = _lists.size();
  133. int itemId, price, maxCount, currentCount, time;
  134. long saveTimer;
  135. PreparedStatement statement = con.prepareStatement("SELECT item_id, price, shop_id, "
  136. + L2DatabaseFactory.getInstance().safetyString("order")
  137. + ", count, currentCount, time, savetimer FROM custom_merchant_buylists WHERE shop_id=? ORDER BY "
  138. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC");
  139. while (rset1.next())
  140. {
  141. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  142. ResultSet rset = statement.executeQuery();
  143. statement.clearParameters();
  144. int shopId = rset1.getInt("shop_id");
  145. L2TradeList buy1 = new L2TradeList(shopId);
  146. while (rset.next())
  147. {
  148. itemId = rset.getInt("item_id");
  149. price = rset.getInt("price");
  150. maxCount = rset.getInt("count");
  151. currentCount = rset.getInt("currentCount");
  152. time = rset.getInt("time");
  153. saveTimer = rset.getLong("saveTimer");
  154. L2TradeItem item = new L2TradeItem(shopId, itemId);
  155. if (ItemTable.getInstance().getTemplate(itemId) == null)
  156. {
  157. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId()
  158. + ", missing data for that item.");
  159. continue;
  160. }
  161. if (price <= -1)
  162. {
  163. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  164. }
  165. if (Config.DEBUG)
  166. {
  167. // debug
  168. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  169. if (diff < 0.8 || diff > 1.2)
  170. {
  171. _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
  172. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  173. + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  174. }
  175. }
  176. item.setPrice(price);
  177. item.setRestoreDelay(time);
  178. item.setNextRestoreTime(saveTimer);
  179. item.setMaxCount(maxCount);
  180. if (currentCount > -1)
  181. {
  182. item.setCurrentCount(currentCount);
  183. }
  184. else
  185. {
  186. item.setCurrentCount(maxCount);
  187. }
  188. buy1.addItem(item);
  189. }
  190. buy1.setNpcId(rset1.getString("npc_id"));
  191. _lists.put(buy1.getListId(), buy1);
  192. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  193. rset.close();
  194. }
  195. statement.close();
  196. rset1.close();
  197. _log.info("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  198. }
  199. catch (Exception e)
  200. {
  201. // problem with initializing spawn, go to next one
  202. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  203. }
  204. }
  205. }
  206. public L2TradeList getBuyList(int listId)
  207. {
  208. return _lists.get(listId);
  209. }
  210. public List<L2TradeList> getBuyListByNpcId(int npcId)
  211. {
  212. List<L2TradeList> lists = new FastList<>();
  213. Collection<L2TradeList> values = _lists.values();
  214. for (L2TradeList list : values)
  215. {
  216. String tradeNpcId = list.getNpcId();
  217. if (tradeNpcId.startsWith("gm"))
  218. continue;
  219. if (npcId == Integer.parseInt(tradeNpcId))
  220. lists.add(list);
  221. }
  222. return lists;
  223. }
  224. public void dataCountStore()
  225. {
  226. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  227. PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount = ? WHERE item_id = ? AND shop_id = ?"))
  228. {
  229. for (L2TradeList list : _lists.values())
  230. {
  231. if (list.hasLimitedStockItem())
  232. {
  233. for (L2TradeItem item : list.getItems())
  234. {
  235. long currentCount;
  236. if (item.hasLimitedStock() && (currentCount = item.getCurrentCount()) < item.getMaxCount())
  237. {
  238. statement.setLong(1, currentCount);
  239. statement.setInt(2, item.getItemId());
  240. statement.setInt(3, list.getListId());
  241. statement.executeUpdate();
  242. statement.clearParameters();
  243. }
  244. }
  245. }
  246. }
  247. }
  248. catch (Exception e)
  249. {
  250. _log.log(Level.SEVERE, "TradeController: Could not store Count Item: " + e.getMessage(), e);
  251. }
  252. }
  253. /**
  254. * @return
  255. */
  256. public synchronized int getNextId()
  257. {
  258. return _nextListId++;
  259. }
  260. private static class SingletonHolder
  261. {
  262. protected static final TradeController _instance = new TradeController();
  263. }
  264. }