2
0

TradeController.java 10 KB

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