TradeController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 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. while (rset1.next())
  75. {
  76. PreparedStatement statement = con.prepareStatement("SELECT item_id, price, shop_id, "
  77. + L2DatabaseFactory.getInstance().safetyString("order")
  78. + ", count, currentCount, time, savetimer FROM merchant_buylists WHERE shop_id=? ORDER BY "
  79. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC");
  80. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  81. ResultSet rset = statement.executeQuery();
  82. L2TradeList buy1 = new L2TradeList(rset1.getInt("shop_id"));
  83. while (rset.next())
  84. {
  85. itemId = rset.getInt("item_id");
  86. price = rset.getInt("price");
  87. maxCount = rset.getInt("count");
  88. currentCount = rset.getInt("currentCount");
  89. time = rset.getInt("time");
  90. saveTimer = rset.getLong("saveTimer");
  91. L2TradeItem item = new L2TradeItem(itemId);
  92. if (ItemTable.getInstance().getTemplate(itemId) == null)
  93. {
  94. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item.");
  95. continue;
  96. }
  97. if (price <= -1)
  98. {
  99. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  100. }
  101. if (Config.DEBUG)
  102. {
  103. // debug
  104. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  105. if (diff < 0.8 || diff > 1.2)
  106. {
  107. _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
  108. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  109. + " - 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, "
  168. + L2DatabaseFactory.getInstance().safetyString("order")
  169. + ", count, currentCount, time, savetimer FROM custom_merchant_buylists WHERE shop_id=? ORDER BY "
  170. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC");
  171. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  172. ResultSet rset = statement.executeQuery();
  173. L2TradeList buy1 = new L2TradeList(rset1.getInt("shop_id"));
  174. while (rset.next())
  175. {
  176. itemId = rset.getInt("item_id");
  177. price = rset.getInt("price");
  178. maxCount = rset.getInt("count");
  179. currentCount = rset.getInt("currentCount");
  180. time = rset.getInt("time");
  181. saveTimer = rset.getLong("saveTimer");
  182. L2TradeItem item = new L2TradeItem(itemId);
  183. if (ItemTable.getInstance().getTemplate(itemId) == null)
  184. {
  185. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId()
  186. + ", missing data for that item.");
  187. continue;
  188. }
  189. if (price <= -1)
  190. {
  191. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  192. }
  193. if (Config.DEBUG)
  194. {
  195. // debug
  196. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  197. if (diff < 0.8 || diff > 1.2)
  198. {
  199. _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
  200. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  201. + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  202. }
  203. }
  204. item.setPrice(price);
  205. item.setRestoreDelay(time);
  206. item.setNextRestoreTime(saveTimer);
  207. item.setMaxCount(maxCount);
  208. if (currentCount > -1)
  209. {
  210. item.setCurrentCount(currentCount);
  211. }
  212. else
  213. {
  214. item.setCurrentCount(maxCount);
  215. }
  216. buy1.addItem(item);
  217. }
  218. buy1.setNpcId(rset1.getString("npc_id"));
  219. _lists.put(buy1.getListId(), buy1);
  220. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  221. rset.close();
  222. statement.close();
  223. }
  224. rset1.close();
  225. statement1.close();
  226. _log.config("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  227. }
  228. catch (Exception e)
  229. {
  230. // problem with initializing spawn, go to next one
  231. _log.warning("TradeController: Buylists could not be initialized.");
  232. e.printStackTrace();
  233. }
  234. finally
  235. {
  236. try
  237. {
  238. con.close();
  239. }
  240. catch (Exception e)
  241. {
  242. }
  243. }
  244. }
  245. }
  246. public L2TradeList getBuyList(int listId)
  247. {
  248. return _lists.get(listId);
  249. }
  250. public List<L2TradeList> getBuyListByNpcId(int npcId)
  251. {
  252. List<L2TradeList> lists = new FastList<L2TradeList>();
  253. Collection<L2TradeList> values = _lists.values();
  254. for (L2TradeList list : values)
  255. {
  256. String tradeNpcId = list.getNpcId();
  257. if (tradeNpcId.startsWith("gm"))
  258. continue;
  259. if (npcId == Integer.parseInt(tradeNpcId))
  260. lists.add(list);
  261. }
  262. return lists;
  263. }
  264. public void dataCountStore()
  265. {
  266. Connection con = null;
  267. PreparedStatement statement;
  268. int listId;
  269. try
  270. {
  271. con = L2DatabaseFactory.getInstance().getConnection();
  272. for (L2TradeList list : _lists.values())
  273. {
  274. if (list.hasLimitedStockItem())
  275. {
  276. listId = list.getListId();
  277. for (L2TradeItem item : list.getItems())
  278. {
  279. long currentCount;
  280. if (item.hasLimitedStock() && (currentCount = item.getCurrentCount()) < item.getMaxCount())
  281. {
  282. statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
  283. statement.setLong(1, currentCount);
  284. statement.setInt(2, item.getItemId());
  285. statement.setInt(3, listId);
  286. statement.executeUpdate();
  287. statement.close();
  288. }
  289. }
  290. }
  291. }
  292. }
  293. catch (Exception e)
  294. {
  295. _log.log(Level.SEVERE, "TradeController: Could not store Count Item");
  296. }
  297. finally
  298. {
  299. try
  300. {
  301. con.close();
  302. }
  303. catch (Exception e)
  304. {
  305. e.printStackTrace();
  306. }
  307. }
  308. }
  309. /**
  310. * @return
  311. */
  312. public synchronized int getNextId()
  313. {
  314. return _nextListId++;
  315. }
  316. @SuppressWarnings("synthetic-access")
  317. private static class SingletonHolder
  318. {
  319. protected static final TradeController _instance = new TradeController();
  320. }
  321. }