TradeController.java 10 KB

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