L2TradeList.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.model;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.util.Collection;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.concurrent.RejectedExecutionException;
  23. import java.util.concurrent.atomic.AtomicLong;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.L2DatabaseFactory;
  27. import com.l2jserver.gameserver.ThreadPoolManager;
  28. import com.l2jserver.gameserver.datatables.ItemTable;
  29. import com.l2jserver.gameserver.templates.item.L2Item;
  30. import javolution.util.FastMap;
  31. /**
  32. * This class ...
  33. *
  34. * @version $Revision: 1.4.2.1.2.5 $ $Date: 2005/03/27 15:29:33 $
  35. */
  36. public class L2TradeList
  37. {
  38. private Map<Integer, L2TradeItem> _items;
  39. private int _listId;
  40. private String _buystorename, _sellstorename;
  41. private boolean _hasLimitedStockItem;
  42. private String _npcId;
  43. public L2TradeList(int listId)
  44. {
  45. _items = new FastMap<Integer, L2TradeItem>();
  46. _listId = listId;
  47. }
  48. public void setNpcId(String id)
  49. {
  50. _npcId = id;
  51. }
  52. public String getNpcId()
  53. {
  54. return _npcId;
  55. }
  56. public void addItem(L2TradeItem item)
  57. {
  58. _items.put(item.getItemId(), item);
  59. if (item.hasLimitedStock())
  60. {
  61. this.setHasLimitedStockItem(true);
  62. }
  63. }
  64. public void replaceItem(int itemID, long price)
  65. {
  66. L2TradeItem item = _items.get(itemID);
  67. if (item != null)
  68. {
  69. item.setPrice(price);
  70. }
  71. }
  72. public void removeItem(int itemID)
  73. {
  74. _items.remove(itemID);
  75. }
  76. /**
  77. * @return Returns the listId.
  78. */
  79. public int getListId()
  80. {
  81. return _listId;
  82. }
  83. /**
  84. * @param hasLimitedStockItem The hasLimitedStockItem to set.
  85. */
  86. public void setHasLimitedStockItem(boolean hasLimitedStockItem)
  87. {
  88. _hasLimitedStockItem = hasLimitedStockItem;
  89. }
  90. /**
  91. * @return Returns the hasLimitedStockItem.
  92. */
  93. public boolean hasLimitedStockItem()
  94. {
  95. return _hasLimitedStockItem;
  96. }
  97. public void setSellStoreName(String name)
  98. {
  99. _sellstorename = name;
  100. }
  101. public String getSellStoreName()
  102. {
  103. return _sellstorename;
  104. }
  105. public void setBuyStoreName(String name)
  106. {
  107. _buystorename = name;
  108. }
  109. public String getBuyStoreName()
  110. {
  111. return _buystorename;
  112. }
  113. /**
  114. * @return Returns the items.
  115. */
  116. public Collection<L2TradeItem> getItems()
  117. {
  118. return _items.values();
  119. }
  120. public List<L2TradeItem> getItems(int start, int end)
  121. {
  122. List<L2TradeItem> list = new LinkedList<L2TradeItem>();
  123. list.addAll(_items.values());
  124. return list.subList(start, end);
  125. }
  126. public long getPriceForItemId(int itemId)
  127. {
  128. L2TradeItem item = _items.get(itemId);
  129. if (item != null)
  130. {
  131. return item.getPrice();
  132. }
  133. return -1;
  134. }
  135. public L2TradeItem getItemById(int itemId)
  136. {
  137. return _items.get(itemId);
  138. }
  139. /*
  140. public boolean countDecrease(int itemId)
  141. {
  142. L2TradeItem item = _items.get(itemId);
  143. if (item != null)
  144. {
  145. return item.hasLimitedStock();
  146. }
  147. return false;
  148. }*/
  149. public boolean containsItemId(int itemId)
  150. {
  151. return _items.containsKey(itemId);
  152. }
  153. /**
  154. * Itens representation for trade lists
  155. *
  156. * @author KenM
  157. */
  158. public static class L2TradeItem
  159. {
  160. private static final Logger _log = Logger.getLogger(L2TradeItem.class.getName());
  161. private final int _listId;
  162. private final int _itemId;
  163. private final L2Item _template;
  164. private long _price;
  165. // count related
  166. private AtomicLong _currentCount = new AtomicLong();
  167. private long _maxCount = -1;
  168. private long _restoreDelay;
  169. private long _nextRestoreTime;
  170. public L2TradeItem(int listId, int itemId)
  171. {
  172. _listId = listId;
  173. _itemId = itemId;
  174. _template = ItemTable.getInstance().getTemplate(itemId);
  175. }
  176. /**
  177. * @return Returns the itemId.
  178. */
  179. public int getItemId()
  180. {
  181. return _itemId;
  182. }
  183. /**
  184. * @param price The price to set.
  185. */
  186. public void setPrice(long price)
  187. {
  188. _price = price;
  189. }
  190. /**
  191. * @return Returns the price.
  192. */
  193. public long getPrice()
  194. {
  195. return _price;
  196. }
  197. public L2Item getTemplate()
  198. {
  199. return _template;
  200. }
  201. /**
  202. * @param currentCount The currentCount to set.
  203. */
  204. public void setCurrentCount(long currentCount)
  205. {
  206. _currentCount.set(currentCount);
  207. }
  208. public boolean decreaseCount(long val)
  209. {
  210. return _currentCount.addAndGet(-val) >= 0;
  211. }
  212. /**
  213. * @return Returns the currentCount.
  214. */
  215. public long getCurrentCount()
  216. {
  217. if (this.hasLimitedStock() && this.isPendingStockUpdate())
  218. {
  219. this.restoreInitialCount();
  220. }
  221. long ret = _currentCount.get();
  222. return ret > 0 ? ret : 0;
  223. }
  224. public boolean isPendingStockUpdate()
  225. {
  226. return System.currentTimeMillis() >= _nextRestoreTime;
  227. }
  228. public void restoreInitialCount()
  229. {
  230. this.setCurrentCount(this.getMaxCount());
  231. _nextRestoreTime = _nextRestoreTime + this.getRestoreDelay();
  232. // consume until next update is on future
  233. if (this.isPendingStockUpdate() && this.getRestoreDelay() > 0)
  234. {
  235. _nextRestoreTime = System.currentTimeMillis() + this.getRestoreDelay();
  236. }
  237. // exec asynchronously
  238. try
  239. {
  240. ThreadPoolManager.getInstance().executeTask(new TimerSave());
  241. }
  242. catch (RejectedExecutionException e)
  243. {
  244. // during shutdown executeTask() failed
  245. saveDataTimer();
  246. }
  247. }
  248. /**
  249. * @param maxCount The maxCount to set.
  250. */
  251. public void setMaxCount(long maxCount)
  252. {
  253. _maxCount = maxCount;
  254. }
  255. /**
  256. * @return Returns the maxCount.
  257. */
  258. public long getMaxCount()
  259. {
  260. return _maxCount;
  261. }
  262. public boolean hasLimitedStock()
  263. {
  264. return this.getMaxCount() > -1;
  265. }
  266. /**
  267. * @param restoreDelay The restoreDelay to set (in hours)
  268. */
  269. public void setRestoreDelay(long restoreDelay)
  270. {
  271. _restoreDelay = restoreDelay * 60 * 60 * 1000;
  272. }
  273. /**
  274. * @return Returns the restoreDelay (in milis)
  275. */
  276. public long getRestoreDelay()
  277. {
  278. return _restoreDelay;
  279. }
  280. /**
  281. * For resuming when server loads
  282. * @param nextRestoreTime The nextRestoreTime to set.
  283. */
  284. public void setNextRestoreTime(long nextRestoreTime)
  285. {
  286. _nextRestoreTime = nextRestoreTime;
  287. }
  288. /**
  289. * @return Returns the nextRestoreTime.
  290. */
  291. public long getNextRestoreTime()
  292. {
  293. return _nextRestoreTime;
  294. }
  295. class TimerSave implements Runnable
  296. {
  297. /**
  298. * @see java.lang.Runnable#run()
  299. */
  300. public void run()
  301. {
  302. L2TradeItem.this.saveDataTimer();
  303. }
  304. }
  305. protected void saveDataTimer()
  306. {
  307. Connection con = null;
  308. try
  309. {
  310. con = L2DatabaseFactory.getInstance().getConnection();
  311. PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET savetimer =? WHERE shop_id =? and item_id =?");
  312. statement.setLong(1, _nextRestoreTime);
  313. statement.setInt(2, _listId);
  314. statement.setInt(3, _itemId);
  315. statement.executeUpdate();
  316. statement.close();
  317. }
  318. catch (Exception e)
  319. {
  320. _log.log(Level.SEVERE, "L2TradeItem: Could not update Timer save in Buylist");
  321. }
  322. finally
  323. {
  324. L2DatabaseFactory.close(con);
  325. }
  326. }
  327. }
  328. }