TradeController.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javolution.util.FastList;
  28. import javolution.util.FastMap;
  29. import net.sf.l2j.Config;
  30. import net.sf.l2j.L2DatabaseFactory;
  31. import net.sf.l2j.gameserver.datatables.ItemTable;
  32. import net.sf.l2j.gameserver.model.L2TradeList;
  33. import net.sf.l2j.gameserver.model.L2TradeList.L2TradeItem;
  34. /**
  35. * This class ...
  36. *
  37. * @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
  38. */
  39. public class TradeController
  40. {
  41. private static Logger _log = Logger.getLogger(TradeController.class.getName());
  42. private static TradeController _instance;
  43. private int _nextListId;
  44. private Map<Integer, L2TradeList> _lists;
  45. /** Task launching the function for restore count of Item (Clan Hall) */
  46. /*public class RestoreCount implements Runnable
  47. {
  48. private int _timer;
  49. public RestoreCount(int time)
  50. {
  51. _timer = time;
  52. }
  53. public void run()
  54. {
  55. restoreCount(_timer);
  56. dataTimerSave(_timer);
  57. ThreadPoolManager.getInstance().scheduleGeneral(new RestoreCount(_timer), (long)_timer*60*60*1000);
  58. }
  59. }*/
  60. public static TradeController getInstance()
  61. {
  62. if (_instance == null)
  63. {
  64. _instance = new TradeController();
  65. }
  66. return _instance;
  67. }
  68. private TradeController()
  69. {
  70. _lists = new FastMap<Integer, L2TradeList>();
  71. java.sql.Connection con = null;
  72. /*
  73. * Initialize Shop buylist
  74. */
  75. try
  76. {
  77. con = L2DatabaseFactory.getInstance().getConnection();
  78. PreparedStatement statement1 = con.prepareStatement("SELECT shop_id, npc_id FROM merchant_shopids");
  79. ResultSet rset1 = statement1.executeQuery();
  80. int itemId, price, maxCount, currentCount, time;
  81. long saveTimer;
  82. while (rset1.next())
  83. {
  84. 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");
  85. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  86. ResultSet rset = statement.executeQuery();
  87. L2TradeList buy1 = new L2TradeList(rset1.getInt("shop_id"));
  88. while (rset.next())
  89. {
  90. itemId = rset.getInt("item_id");
  91. price = rset.getInt("price");
  92. maxCount = rset.getInt("count");
  93. currentCount = rset.getInt("currentCount");
  94. time = rset.getInt("time");
  95. saveTimer = rset.getLong("saveTimer");
  96. L2TradeItem item = new L2TradeItem(itemId);
  97. if (ItemTable.getInstance().getTemplate(itemId) == null)
  98. {
  99. _log.warning("Skipping itemId: "+itemId+" on buylistId: "+buy1.getListId()+", missing data for that item.");
  100. continue;
  101. }
  102. if (price <= -1)
  103. {
  104. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  105. }
  106. if (Config.DEBUG)
  107. {
  108. // debug
  109. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  110. if (diff < 0.8 || diff > 1.2)
  111. {
  112. _log.severe("PRICING DEBUG: TradeListId: "+buy1.getListId()+" - ItemId: "+itemId+" ("+ItemTable.getInstance().getTemplate(itemId).getName()+") diff: "+diff+" - Price: "+price+" - Reference: "+ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  113. }
  114. }
  115. item.setPrice(price);
  116. item.setRestoreDelay(time);
  117. item.setNextRestoreTime(saveTimer);
  118. item.setMaxCount(maxCount);
  119. if (currentCount > -1)
  120. {
  121. item.setCurrentCount(currentCount);
  122. }
  123. else
  124. {
  125. item.setCurrentCount(maxCount);
  126. }
  127. buy1.addItem(item);
  128. }
  129. buy1.setNpcId(rset1.getString("npc_id"));
  130. _lists.put(buy1.getListId(), buy1);
  131. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  132. rset.close();
  133. statement.close();
  134. }
  135. rset1.close();
  136. statement1.close();
  137. _log.config("TradeController: Loaded " + _lists.size() + " Buylists.");
  138. }
  139. catch (Exception e)
  140. {
  141. // problem with initializing spawn, go to next one
  142. _log.warning("TradeController: Buylists could not be initialized.");
  143. e.printStackTrace();
  144. }
  145. finally
  146. {
  147. try
  148. {
  149. con.close();
  150. }
  151. catch (Exception e)
  152. {
  153. }
  154. }
  155. }
  156. public L2TradeList getBuyList(int listId)
  157. {
  158. return _lists.get(listId);
  159. }
  160. public List<L2TradeList> getBuyListByNpcId(int npcId)
  161. {
  162. List<L2TradeList> lists = new FastList<L2TradeList>();
  163. Collection<L2TradeList> values = _lists.values();
  164. for (L2TradeList list : values)
  165. {
  166. String tradeNpcId = list.getNpcId();
  167. if (tradeNpcId.startsWith("gm"))
  168. continue;
  169. if (npcId == Integer.parseInt(tradeNpcId))
  170. lists.add(list);
  171. }
  172. return lists;
  173. }
  174. public void dataCountStore()
  175. {
  176. java.sql.Connection con = null;
  177. PreparedStatement statement;
  178. try
  179. {
  180. con = L2DatabaseFactory.getInstance().getConnection();
  181. for (L2TradeList list : _lists.values())
  182. {
  183. if (list.hasLimitedStockItem())
  184. {
  185. int listId = list.getListId();
  186. for (L2TradeItem item :list.getItems())
  187. {
  188. if (item.hasLimitedStock() && item.getCurrentCount() < item.getMaxCount()) //needed?
  189. {
  190. statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
  191. statement.setInt(1, item.getCurrentCount());
  192. statement.setInt(2, item.getItemId());
  193. statement.setInt(3, listId);
  194. statement.executeUpdate();
  195. statement.close();
  196. }
  197. }
  198. }
  199. }
  200. }
  201. catch (Exception e)
  202. {
  203. _log.log(Level.SEVERE, "TradeController: Could not store Count Item" );
  204. }
  205. finally
  206. {
  207. try { con.close(); } catch (Exception e) { e.printStackTrace(); }
  208. }
  209. }
  210. /**
  211. * @return
  212. */
  213. public synchronized int getNextId()
  214. {
  215. return _nextListId++;
  216. }
  217. }