TradeController.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.Collection;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import javolution.util.FastMap;
  25. import net.sf.l2j.Config;
  26. import net.sf.l2j.L2DatabaseFactory;
  27. import net.sf.l2j.gameserver.datatables.ItemTable;
  28. import net.sf.l2j.gameserver.model.L2TradeList;
  29. import net.sf.l2j.gameserver.model.L2TradeList.L2TradeItem;
  30. /**
  31. * This class ...
  32. *
  33. * @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
  34. */
  35. public class TradeController
  36. {
  37. private static Logger _log = Logger.getLogger(TradeController.class.getName());
  38. private static TradeController _instance;
  39. private int _nextListId;
  40. private Map<Integer, L2TradeList> _lists;
  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. if (_instance == null)
  59. {
  60. _instance = new TradeController();
  61. }
  62. return _instance;
  63. }
  64. private TradeController()
  65. {
  66. _lists = new FastMap<Integer, L2TradeList>();
  67. java.sql.Connection con = null;
  68. /*
  69. * Initialize Shop buylist
  70. */
  71. try
  72. {
  73. con = L2DatabaseFactory.getInstance().getConnection();
  74. PreparedStatement statement1 = con.prepareStatement("SELECT shop_id, npc_id FROM merchant_shopids");
  75. ResultSet rset1 = statement1.executeQuery();
  76. int itemId, price, maxCount, currentCount, time;
  77. long saveTimer;
  78. while (rset1.next())
  79. {
  80. 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");
  81. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  82. ResultSet rset = statement.executeQuery();
  83. L2TradeList buy1 = new L2TradeList(rset1.getInt("shop_id"));
  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(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+" ("+ItemTable.getInstance().getTemplate(itemId).getName()+") diff: "+diff+" - Price: "+price+" - Reference: "+ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  109. }
  110. }
  111. item.setPrice(price);
  112. item.setRestoreDelay(time);
  113. item.setNextRestoreTime(saveTimer);
  114. item.setMaxCount(maxCount);
  115. if (currentCount > -1)
  116. {
  117. item.setCurrentCount(currentCount);
  118. }
  119. else
  120. {
  121. item.setCurrentCount(maxCount);
  122. }
  123. buy1.addItem(item);
  124. }
  125. buy1.setNpcId(rset1.getString("npc_id"));
  126. _lists.put(buy1.getListId(), buy1);
  127. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  128. rset.close();
  129. statement.close();
  130. }
  131. rset1.close();
  132. statement1.close();
  133. _log.config("TradeController: Loaded " + _lists.size() + " Buylists.");
  134. }
  135. catch (Exception e)
  136. {
  137. // problem with initializing spawn, go to next one
  138. _log.warning("TradeController: Buylists could not be initialized.");
  139. e.printStackTrace();
  140. }
  141. finally
  142. {
  143. try
  144. {
  145. con.close();
  146. }
  147. catch (Exception e)
  148. {
  149. }
  150. }
  151. }
  152. public L2TradeList getBuyList(int listId)
  153. {
  154. return _lists.get(listId);
  155. }
  156. public List<L2TradeList> getBuyListByNpcId(int npcId)
  157. {
  158. List<L2TradeList> lists = new FastList<L2TradeList>();
  159. Collection<L2TradeList> values = _lists.values();
  160. for (L2TradeList list : values)
  161. {
  162. String tradeNpcId = list.getNpcId();
  163. if (tradeNpcId.startsWith("gm"))
  164. continue;
  165. if (npcId == Integer.parseInt(tradeNpcId))
  166. lists.add(list);
  167. }
  168. return lists;
  169. }
  170. public void dataCountStore()
  171. {
  172. java.sql.Connection con = null;
  173. PreparedStatement statement;
  174. try
  175. {
  176. con = L2DatabaseFactory.getInstance().getConnection();
  177. for (L2TradeList list : _lists.values())
  178. {
  179. if (list.hasLimitedStockItem())
  180. {
  181. int listId = list.getListId();
  182. for (L2TradeItem item :list.getItems())
  183. {
  184. if (item.hasLimitedStock() && item.getCurrentCount() < item.getMaxCount()) //needed?
  185. {
  186. statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
  187. statement.setInt(1, item.getCurrentCount());
  188. statement.setInt(2, item.getItemId());
  189. statement.setInt(3, listId);
  190. statement.executeUpdate();
  191. statement.close();
  192. }
  193. }
  194. }
  195. }
  196. }
  197. catch (Exception e)
  198. {
  199. _log.log(Level.SEVERE, "TradeController: Could not store Count Item" );
  200. }
  201. finally
  202. {
  203. try { con.close(); } catch (Exception e) { e.printStackTrace(); }
  204. }
  205. }
  206. /**
  207. * @return
  208. */
  209. public synchronized int getNextId()
  210. {
  211. return _nextListId++;
  212. }
  213. }