TradeController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. * If enabled, initialize the custom buylist
  153. */
  154. if (Config.CUSTOM_MERCHANT_TABLES)
  155. {
  156. try
  157. {
  158. int initialSize = _lists.size();
  159. con = L2DatabaseFactory.getInstance().getConnection();
  160. PreparedStatement statement1 = con.prepareStatement("SELECT shop_id, npc_id FROM custom_merchant_shopids");
  161. ResultSet rset1 = statement1.executeQuery();
  162. int itemId, price, maxCount, currentCount, time;
  163. long saveTimer;
  164. while (rset1.next())
  165. {
  166. 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");
  167. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  168. ResultSet rset = statement.executeQuery();
  169. L2TradeList buy1 = new L2TradeList(rset1.getInt("shop_id"));
  170. while (rset.next())
  171. {
  172. itemId = rset.getInt("item_id");
  173. price = rset.getInt("price");
  174. maxCount = rset.getInt("count");
  175. currentCount = rset.getInt("currentCount");
  176. time = rset.getInt("time");
  177. saveTimer = rset.getLong("saveTimer");
  178. L2TradeItem item = new L2TradeItem(itemId);
  179. if (ItemTable.getInstance().getTemplate(itemId) == null)
  180. {
  181. _log.warning("Skipping itemId: "+itemId+" on buylistId: "+buy1.getListId()+", missing data for that item.");
  182. continue;
  183. }
  184. if (price <= -1)
  185. {
  186. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  187. }
  188. if (Config.DEBUG)
  189. {
  190. // debug
  191. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  192. if (diff < 0.8 || diff > 1.2)
  193. {
  194. _log.severe("PRICING DEBUG: TradeListId: "+buy1.getListId()+" - ItemId: "+itemId+" ("+ItemTable.getInstance().getTemplate(itemId).getName()+") diff: "+diff+" - Price: "+price+" - Reference: "+ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  195. }
  196. }
  197. item.setPrice(price);
  198. item.setRestoreDelay(time);
  199. item.setNextRestoreTime(saveTimer);
  200. item.setMaxCount(maxCount);
  201. if (currentCount > -1)
  202. {
  203. item.setCurrentCount(currentCount);
  204. }
  205. else
  206. {
  207. item.setCurrentCount(maxCount);
  208. }
  209. buy1.addItem(item);
  210. }
  211. buy1.setNpcId(rset1.getString("npc_id"));
  212. _lists.put(buy1.getListId(), buy1);
  213. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  214. rset.close();
  215. statement.close();
  216. }
  217. rset1.close();
  218. statement1.close();
  219. _log.config("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  220. }
  221. catch (Exception e)
  222. {
  223. // problem with initializing spawn, go to next one
  224. _log.warning("TradeController: Buylists could not be initialized.");
  225. e.printStackTrace();
  226. }
  227. finally
  228. {
  229. try
  230. {
  231. con.close();
  232. }
  233. catch (Exception e)
  234. {
  235. }
  236. }
  237. }
  238. }
  239. public L2TradeList getBuyList(int listId)
  240. {
  241. return _lists.get(listId);
  242. }
  243. public List<L2TradeList> getBuyListByNpcId(int npcId)
  244. {
  245. List<L2TradeList> lists = new FastList<L2TradeList>();
  246. Collection<L2TradeList> values = _lists.values();
  247. for (L2TradeList list : values)
  248. {
  249. String tradeNpcId = list.getNpcId();
  250. if (tradeNpcId.startsWith("gm"))
  251. continue;
  252. if (npcId == Integer.parseInt(tradeNpcId))
  253. lists.add(list);
  254. }
  255. return lists;
  256. }
  257. public void dataCountStore()
  258. {
  259. java.sql.Connection con = null;
  260. PreparedStatement statement;
  261. try
  262. {
  263. con = L2DatabaseFactory.getInstance().getConnection();
  264. for (L2TradeList list : _lists.values())
  265. {
  266. if (list.hasLimitedStockItem())
  267. {
  268. int listId = list.getListId();
  269. for (L2TradeItem item :list.getItems())
  270. {
  271. if (item.hasLimitedStock() && item.getCurrentCount() < item.getMaxCount()) //needed?
  272. {
  273. statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
  274. statement.setInt(1, item.getCurrentCount());
  275. statement.setInt(2, item.getItemId());
  276. statement.setInt(3, listId);
  277. statement.executeUpdate();
  278. statement.close();
  279. }
  280. }
  281. }
  282. }
  283. }
  284. catch (Exception e)
  285. {
  286. _log.log(Level.SEVERE, "TradeController: Could not store Count Item" );
  287. }
  288. finally
  289. {
  290. try { con.close(); } catch (Exception e) { e.printStackTrace(); }
  291. }
  292. }
  293. /**
  294. * @return
  295. */
  296. public synchronized int getNextId()
  297. {
  298. return _nextListId++;
  299. }
  300. }