TradeController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. //**
  43. // * Task launching the function for restore count of Item (Clan Hall)
  44. // */
  45. /*public class RestoreCount implements Runnable
  46. {
  47. private int _timer;
  48. public RestoreCount(int time)
  49. {
  50. _timer = time;
  51. }
  52. public void run()
  53. {
  54. restoreCount(_timer);
  55. dataTimerSave(_timer);
  56. ThreadPoolManager.getInstance().scheduleGeneral(new RestoreCount(_timer), (long)_timer*60*60*1000);
  57. }
  58. }*/
  59. public static TradeController getInstance()
  60. {
  61. return SingletonHolder._instance;
  62. }
  63. protected TradeController()
  64. {
  65. _lists.clear();
  66. Connection con = null;
  67. // Initialize Shop buy list
  68. try
  69. {
  70. con = L2DatabaseFactory.getInstance().getConnection();
  71. try (Statement s = con.createStatement();
  72. ResultSet rs1 = s.executeQuery("SELECT shop_id, npc_id FROM merchant_shopids"))
  73. {
  74. int itemId, price, maxCount, currentCount, time;
  75. long saveTimer;
  76. try (PreparedStatement ps = con.prepareStatement("SELECT item_id, price, shop_id, "
  77. + L2DatabaseFactory.getInstance().safetyString("order")
  78. + ", count, currentCount, time, savetimer FROM merchant_buylists WHERE shop_id=? ORDER BY "
  79. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC"))
  80. {
  81. while (rs1.next())
  82. {
  83. ps.setString(1, String.valueOf(rs1.getInt("shop_id")));
  84. try (ResultSet rs2 = ps.executeQuery())
  85. {
  86. ps.clearParameters();
  87. int shopId = rs1.getInt("shop_id");
  88. L2TradeList buy1 = new L2TradeList(shopId);
  89. while (rs2.next())
  90. {
  91. itemId = rs2.getInt("item_id");
  92. price = rs2.getInt("price");
  93. maxCount = rs2.getInt("count");
  94. currentCount = rs2.getInt("currentCount");
  95. time = rs2.getInt("time");
  96. saveTimer = rs2.getLong("saveTimer");
  97. L2TradeItem item = new L2TradeItem(shopId, itemId);
  98. if (ItemTable.getInstance().getTemplate(itemId) == null)
  99. {
  100. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item.");
  101. continue;
  102. }
  103. if (price <= -1)
  104. {
  105. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  106. }
  107. if (Config.DEBUG)
  108. {
  109. // debug
  110. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  111. if (diff < 0.8 || diff > 1.2)
  112. {
  113. _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
  114. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  115. + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  116. }
  117. }
  118. item.setPrice(price);
  119. item.setRestoreDelay(time);
  120. item.setNextRestoreTime(saveTimer);
  121. item.setMaxCount(maxCount);
  122. if (currentCount > -1)
  123. {
  124. item.setCurrentCount(currentCount);
  125. }
  126. else
  127. {
  128. item.setCurrentCount(maxCount);
  129. }
  130. buy1.addItem(item);
  131. }
  132. buy1.setNpcId(rs1.getString("npc_id"));
  133. _lists.put(buy1.getListId(), buy1);
  134. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  135. }
  136. }
  137. }
  138. }
  139. _log.info("TradeController: Loaded " + _lists.size() + " Buylists.");
  140. }
  141. catch (Exception e)
  142. {
  143. // problem with initializing spawn, go to next one
  144. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  145. }
  146. finally
  147. {
  148. L2DatabaseFactory.close(con);
  149. }
  150. // If enabled, initialize the custom buy list
  151. if (Config.CUSTOM_MERCHANT_TABLES)
  152. {
  153. try
  154. {
  155. int initialSize = _lists.size();
  156. con = L2DatabaseFactory.getInstance().getConnection();
  157. PreparedStatement statement1 = con.prepareStatement("SELECT shop_id, npc_id FROM custom_merchant_shopids");
  158. ResultSet rset1 = statement1.executeQuery();
  159. int itemId, price, maxCount, currentCount, time;
  160. long saveTimer;
  161. PreparedStatement statement = con.prepareStatement("SELECT item_id, price, shop_id, "
  162. + L2DatabaseFactory.getInstance().safetyString("order")
  163. + ", count, currentCount, time, savetimer FROM custom_merchant_buylists WHERE shop_id=? ORDER BY "
  164. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC");
  165. while (rset1.next())
  166. {
  167. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  168. ResultSet rset = statement.executeQuery();
  169. statement.clearParameters();
  170. int shopId = rset1.getInt("shop_id");
  171. L2TradeList buy1 = new L2TradeList(shopId);
  172. while (rset.next())
  173. {
  174. itemId = rset.getInt("item_id");
  175. price = rset.getInt("price");
  176. maxCount = rset.getInt("count");
  177. currentCount = rset.getInt("currentCount");
  178. time = rset.getInt("time");
  179. saveTimer = rset.getLong("saveTimer");
  180. L2TradeItem item = new L2TradeItem(shopId, itemId);
  181. if (ItemTable.getInstance().getTemplate(itemId) == null)
  182. {
  183. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId()
  184. + ", missing data for that item.");
  185. continue;
  186. }
  187. if (price <= -1)
  188. {
  189. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  190. }
  191. if (Config.DEBUG)
  192. {
  193. // debug
  194. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  195. if (diff < 0.8 || diff > 1.2)
  196. {
  197. _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
  198. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  199. + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  200. }
  201. }
  202. item.setPrice(price);
  203. item.setRestoreDelay(time);
  204. item.setNextRestoreTime(saveTimer);
  205. item.setMaxCount(maxCount);
  206. if (currentCount > -1)
  207. {
  208. item.setCurrentCount(currentCount);
  209. }
  210. else
  211. {
  212. item.setCurrentCount(maxCount);
  213. }
  214. buy1.addItem(item);
  215. }
  216. buy1.setNpcId(rset1.getString("npc_id"));
  217. _lists.put(buy1.getListId(), buy1);
  218. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  219. rset.close();
  220. }
  221. statement.close();
  222. rset1.close();
  223. statement1.close();
  224. _log.info("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  225. }
  226. catch (Exception e)
  227. {
  228. // problem with initializing spawn, go to next one
  229. _log.log(Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e);
  230. }
  231. finally
  232. {
  233. L2DatabaseFactory.close(con);
  234. }
  235. }
  236. }
  237. public L2TradeList getBuyList(int listId)
  238. {
  239. return _lists.get(listId);
  240. }
  241. public List<L2TradeList> getBuyListByNpcId(int npcId)
  242. {
  243. List<L2TradeList> lists = new FastList<>();
  244. Collection<L2TradeList> values = _lists.values();
  245. for (L2TradeList list : values)
  246. {
  247. String tradeNpcId = list.getNpcId();
  248. if (tradeNpcId.startsWith("gm"))
  249. continue;
  250. if (npcId == Integer.parseInt(tradeNpcId))
  251. lists.add(list);
  252. }
  253. return lists;
  254. }
  255. public void dataCountStore()
  256. {
  257. Connection con = null;
  258. int listId;
  259. try
  260. {
  261. con = L2DatabaseFactory.getInstance().getConnection();
  262. PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
  263. for (L2TradeList list : _lists.values())
  264. {
  265. if (list.hasLimitedStockItem())
  266. {
  267. listId = list.getListId();
  268. for (L2TradeItem item : list.getItems())
  269. {
  270. long currentCount;
  271. if (item.hasLimitedStock() && (currentCount = item.getCurrentCount()) < item.getMaxCount())
  272. {
  273. statement.setLong(1, currentCount);
  274. statement.setInt(2, item.getItemId());
  275. statement.setInt(3, listId);
  276. statement.executeUpdate();
  277. statement.clearParameters();
  278. }
  279. }
  280. }
  281. }
  282. statement.close();
  283. }
  284. catch (Exception e)
  285. {
  286. _log.log(Level.SEVERE, "TradeController: Could not store Count Item: " + e.getMessage(), e);
  287. }
  288. finally
  289. {
  290. L2DatabaseFactory.close(con);
  291. }
  292. }
  293. /**
  294. * @return
  295. */
  296. public synchronized int getNextId()
  297. {
  298. return _nextListId++;
  299. }
  300. private static class SingletonHolder
  301. {
  302. protected static final TradeController _instance = new TradeController();
  303. }
  304. }