TradeController.java 13 KB

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