2
0

TradeController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 com.l2jserver.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 com.l2jserver.Config;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.datatables.ItemTable;
  27. import com.l2jserver.gameserver.model.L2TradeList;
  28. import com.l2jserver.gameserver.model.L2TradeList.L2TradeItem;
  29. import javolution.util.FastList;
  30. import javolution.util.FastMap;
  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. PreparedStatement statement = con.prepareStatement("SELECT item_id, price, shop_id, "
  75. + L2DatabaseFactory.getInstance().safetyString("order")
  76. + ", count, currentCount, time, savetimer FROM merchant_buylists WHERE shop_id=? ORDER BY "
  77. + L2DatabaseFactory.getInstance().safetyString("order") + " ASC");
  78. while (rset1.next())
  79. {
  80. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  81. ResultSet rset = statement.executeQuery();
  82. statement.clearParameters();
  83. int shopId = rset1.getInt("shop_id");
  84. L2TradeList buy1 = new L2TradeList(shopId);
  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(shopId, 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 + " ("
  110. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  111. + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  112. }
  113. }
  114. item.setPrice(price);
  115. item.setRestoreDelay(time);
  116. item.setNextRestoreTime(saveTimer);
  117. item.setMaxCount(maxCount);
  118. if (currentCount > -1)
  119. {
  120. item.setCurrentCount(currentCount);
  121. }
  122. else
  123. {
  124. item.setCurrentCount(maxCount);
  125. }
  126. buy1.addItem(item);
  127. }
  128. buy1.setNpcId(rset1.getString("npc_id"));
  129. _lists.put(buy1.getListId(), buy1);
  130. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  131. rset.close();
  132. }
  133. statement.close();
  134. rset1.close();
  135. statement1.close();
  136. _log.config("TradeController: Loaded " + _lists.size() + " Buylists.");
  137. }
  138. catch (Exception e)
  139. {
  140. // problem with initializing spawn, go to next one
  141. _log.warning("TradeController: Buylists could not be initialized.");
  142. e.printStackTrace();
  143. }
  144. finally
  145. {
  146. try
  147. {
  148. con.close();
  149. }
  150. catch (Exception e)
  151. {
  152. }
  153. }
  154. /*
  155. * If enabled, initialize the custom buylist
  156. */
  157. if (Config.CUSTOM_MERCHANT_TABLES)
  158. {
  159. try
  160. {
  161. int initialSize = _lists.size();
  162. con = L2DatabaseFactory.getInstance().getConnection();
  163. PreparedStatement statement1 = con.prepareStatement("SELECT shop_id, npc_id FROM custom_merchant_shopids");
  164. ResultSet rset1 = statement1.executeQuery();
  165. int itemId, price, maxCount, currentCount, time;
  166. long saveTimer;
  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. while (rset1.next())
  172. {
  173. statement.setString(1, String.valueOf(rset1.getInt("shop_id")));
  174. ResultSet rset = statement.executeQuery();
  175. statement.clearParameters();
  176. int shopId = rset1.getInt("shop_id");
  177. L2TradeList buy1 = new L2TradeList(shopId);
  178. while (rset.next())
  179. {
  180. itemId = rset.getInt("item_id");
  181. price = rset.getInt("price");
  182. maxCount = rset.getInt("count");
  183. currentCount = rset.getInt("currentCount");
  184. time = rset.getInt("time");
  185. saveTimer = rset.getLong("saveTimer");
  186. L2TradeItem item = new L2TradeItem(shopId, itemId);
  187. if (ItemTable.getInstance().getTemplate(itemId) == null)
  188. {
  189. _log.warning("Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId()
  190. + ", missing data for that item.");
  191. continue;
  192. }
  193. if (price <= -1)
  194. {
  195. price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  196. }
  197. if (Config.DEBUG)
  198. {
  199. // debug
  200. double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice();
  201. if (diff < 0.8 || diff > 1.2)
  202. {
  203. _log.severe("PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " ("
  204. + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price
  205. + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice());
  206. }
  207. }
  208. item.setPrice(price);
  209. item.setRestoreDelay(time);
  210. item.setNextRestoreTime(saveTimer);
  211. item.setMaxCount(maxCount);
  212. if (currentCount > -1)
  213. {
  214. item.setCurrentCount(currentCount);
  215. }
  216. else
  217. {
  218. item.setCurrentCount(maxCount);
  219. }
  220. buy1.addItem(item);
  221. }
  222. buy1.setNpcId(rset1.getString("npc_id"));
  223. _lists.put(buy1.getListId(), buy1);
  224. _nextListId = Math.max(_nextListId, buy1.getListId() + 1);
  225. rset.close();
  226. }
  227. statement.close();
  228. rset1.close();
  229. statement1.close();
  230. _log.config("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists.");
  231. }
  232. catch (Exception e)
  233. {
  234. // problem with initializing spawn, go to next one
  235. _log.warning("TradeController: Buylists could not be initialized.");
  236. e.printStackTrace();
  237. }
  238. finally
  239. {
  240. try
  241. {
  242. con.close();
  243. }
  244. catch (Exception e)
  245. {
  246. }
  247. }
  248. }
  249. }
  250. public L2TradeList getBuyList(int listId)
  251. {
  252. return _lists.get(listId);
  253. }
  254. public List<L2TradeList> getBuyListByNpcId(int npcId)
  255. {
  256. List<L2TradeList> lists = new FastList<L2TradeList>();
  257. Collection<L2TradeList> values = _lists.values();
  258. for (L2TradeList list : values)
  259. {
  260. String tradeNpcId = list.getNpcId();
  261. if (tradeNpcId.startsWith("gm"))
  262. continue;
  263. if (npcId == Integer.parseInt(tradeNpcId))
  264. lists.add(list);
  265. }
  266. return lists;
  267. }
  268. public void dataCountStore()
  269. {
  270. Connection con = null;
  271. int listId;
  272. try
  273. {
  274. con = L2DatabaseFactory.getInstance().getConnection();
  275. PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
  276. for (L2TradeList list : _lists.values())
  277. {
  278. if (list.hasLimitedStockItem())
  279. {
  280. listId = list.getListId();
  281. for (L2TradeItem item : list.getItems())
  282. {
  283. long currentCount;
  284. if (item.hasLimitedStock() && (currentCount = item.getCurrentCount()) < item.getMaxCount())
  285. {
  286. statement.setLong(1, currentCount);
  287. statement.setInt(2, item.getItemId());
  288. statement.setInt(3, listId);
  289. statement.executeUpdate();
  290. statement.clearParameters();
  291. }
  292. }
  293. }
  294. }
  295. statement.close();
  296. }
  297. catch (Exception e)
  298. {
  299. _log.log(Level.SEVERE, "TradeController: Could not store Count Item");
  300. }
  301. finally
  302. {
  303. try
  304. {
  305. con.close();
  306. }
  307. catch (Exception e)
  308. {
  309. e.printStackTrace();
  310. }
  311. }
  312. }
  313. /**
  314. * @return
  315. */
  316. public synchronized int getNextId()
  317. {
  318. return _nextListId++;
  319. }
  320. @SuppressWarnings("synthetic-access")
  321. private static class SingletonHolder
  322. {
  323. protected static final TradeController _instance = new TradeController();
  324. }
  325. }