TradeController.java 10 KB

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