ItemAuction.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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.itemauction;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.SQLException;
  19. import java.util.ArrayList;
  20. import java.util.concurrent.TimeUnit;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.ThreadPoolManager;
  26. import com.l2jserver.gameserver.instancemanager.ItemAuctionManager;
  27. import com.l2jserver.gameserver.model.ItemInfo;
  28. import com.l2jserver.gameserver.model.L2World;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  31. import com.l2jserver.gameserver.network.SystemMessageId;
  32. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  33. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  34. /**
  35. * @author Forsaiken
  36. */
  37. public final class ItemAuction
  38. {
  39. static final Logger _log = Logger.getLogger(ItemAuctionManager.class.getName());
  40. private static final long ENDING_TIME_EXTEND_5 = TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES);
  41. private static final long ENDING_TIME_EXTEND_3 = TimeUnit.MILLISECONDS.convert(3, TimeUnit.MINUTES);
  42. private final int _auctionId;
  43. private final int _instanceId;
  44. private final long _startingTime;
  45. private volatile long _endingTime;
  46. private final AuctionItem _auctionItem;
  47. private final ArrayList<ItemAuctionBid> _auctionBids;
  48. private final Object _auctionStateLock;
  49. private volatile ItemAuctionState _auctionState;
  50. private volatile ItemAuctionExtendState _scheduledAuctionEndingExtendState;
  51. private volatile ItemAuctionExtendState _auctionEndingExtendState;
  52. private ItemInfo _itemInfo;
  53. private ItemAuctionBid _highestBid;
  54. private int _lastBidPlayerObjId;
  55. // SQL
  56. private static final String DELETE_ITEM_AUCTION_BID = "DELETE FROM item_auction_bid WHERE auctionId = ? AND playerObjId = ?";
  57. private static final String INSERT_ITEM_AUCTION_BID = "INSERT INTO item_auction_bid (auctionId, playerObjId, playerBid) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE playerBid = ?";
  58. public ItemAuction(final int auctionId, final int instanceId, final long startingTime, final long endingTime, final AuctionItem auctionItem)
  59. {
  60. this(auctionId, instanceId, startingTime, endingTime, auctionItem, new ArrayList<ItemAuctionBid>(), ItemAuctionState.CREATED);
  61. }
  62. public ItemAuction(final int auctionId, final int instanceId, final long startingTime, final long endingTime, final AuctionItem auctionItem, final ArrayList<ItemAuctionBid> auctionBids, final ItemAuctionState auctionState)
  63. {
  64. _auctionId = auctionId;
  65. _instanceId = instanceId;
  66. _startingTime = startingTime;
  67. _endingTime = endingTime;
  68. _auctionItem = auctionItem;
  69. _auctionBids = auctionBids;
  70. _auctionState = auctionState;
  71. _auctionStateLock = new Object();
  72. _scheduledAuctionEndingExtendState = ItemAuctionExtendState.INITIAL;
  73. _auctionEndingExtendState = ItemAuctionExtendState.INITIAL;
  74. final L2ItemInstance item = _auctionItem.createNewItemInstance();
  75. _itemInfo = new ItemInfo(item);
  76. L2World.getInstance().removeObject(item);
  77. for (final ItemAuctionBid bid : _auctionBids)
  78. {
  79. if (_highestBid == null || _highestBid.getLastBid() < bid.getLastBid())
  80. _highestBid = bid;
  81. }
  82. }
  83. public final ItemAuctionState getAuctionState()
  84. {
  85. final ItemAuctionState auctionState;
  86. synchronized (_auctionStateLock)
  87. {
  88. auctionState = _auctionState;
  89. }
  90. return auctionState;
  91. }
  92. public final boolean setAuctionState(final ItemAuctionState expected, final ItemAuctionState wanted)
  93. {
  94. synchronized (_auctionStateLock)
  95. {
  96. if (_auctionState != expected)
  97. return false;
  98. _auctionState = wanted;
  99. storeMe();
  100. return true;
  101. }
  102. }
  103. public final int getAuctionId()
  104. {
  105. return _auctionId;
  106. }
  107. public final int getInstanceId()
  108. {
  109. return _instanceId;
  110. }
  111. public final ItemInfo getItemInfo()
  112. {
  113. return _itemInfo;
  114. }
  115. public final L2ItemInstance createNewItemInstance()
  116. {
  117. return _auctionItem.createNewItemInstance();
  118. }
  119. public final long getAuctionInitBid()
  120. {
  121. return _auctionItem.getAuctionInitBid();
  122. }
  123. public final ItemAuctionBid getHighestBid()
  124. {
  125. return _highestBid;
  126. }
  127. public final ItemAuctionExtendState getAuctionEndingExtendState()
  128. {
  129. return _auctionEndingExtendState;
  130. }
  131. public final ItemAuctionExtendState getScheduledAuctionEndingExtendState()
  132. {
  133. return _scheduledAuctionEndingExtendState;
  134. }
  135. public final void setScheduledAuctionEndingExtendState(ItemAuctionExtendState state)
  136. {
  137. _scheduledAuctionEndingExtendState = state;
  138. }
  139. public final long getStartingTime()
  140. {
  141. return _startingTime;
  142. }
  143. public final long getEndingTime()
  144. {
  145. return _endingTime;
  146. }
  147. public final long getStartingTimeRemaining()
  148. {
  149. return Math.max(getEndingTime() - System.currentTimeMillis(), 0L);
  150. }
  151. public final long getFinishingTimeRemaining()
  152. {
  153. return Math.max(getEndingTime() - System.currentTimeMillis(), 0L);
  154. }
  155. public final void storeMe()
  156. {
  157. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  158. PreparedStatement statement = con.prepareStatement("INSERT INTO item_auction (auctionId,instanceId,auctionItemId,startingTime,endingTime,auctionStateId) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE auctionStateId=?"))
  159. {
  160. statement.setInt(1, _auctionId);
  161. statement.setInt(2, _instanceId);
  162. statement.setInt(3, _auctionItem.getAuctionItemId());
  163. statement.setLong(4, _startingTime);
  164. statement.setLong(5, _endingTime);
  165. statement.setByte(6, _auctionState.getStateId());
  166. statement.setByte(7, _auctionState.getStateId());
  167. statement.execute();
  168. }
  169. catch (final SQLException e)
  170. {
  171. _log.log(Level.WARNING, "", e);
  172. }
  173. }
  174. public final int getAndSetLastBidPlayerObjectId(final int playerObjId)
  175. {
  176. final int lastBid = _lastBidPlayerObjId;
  177. _lastBidPlayerObjId = playerObjId;
  178. return lastBid;
  179. }
  180. private final void updatePlayerBid(final ItemAuctionBid bid, final boolean delete)
  181. {
  182. // TODO nBd maybe move such stuff to you db updater :D
  183. updatePlayerBidInternal(bid, delete);
  184. }
  185. final void updatePlayerBidInternal(final ItemAuctionBid bid, final boolean delete)
  186. {
  187. final String query = delete ? DELETE_ITEM_AUCTION_BID : INSERT_ITEM_AUCTION_BID;
  188. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  189. PreparedStatement ps = con.prepareStatement(query))
  190. {
  191. ps.setInt(1, _auctionId);
  192. ps.setInt(2, bid.getPlayerObjId());
  193. if (!delete)
  194. {
  195. ps.setLong(3, bid.getLastBid());
  196. ps.setLong(4, bid.getLastBid());
  197. }
  198. ps.execute();
  199. }
  200. catch (SQLException e)
  201. {
  202. _log.log(Level.WARNING, "", e);
  203. }
  204. }
  205. public final void registerBid(final L2PcInstance player, final long newBid)
  206. {
  207. if (player == null)
  208. throw new NullPointerException();
  209. if (newBid < getAuctionInitBid())
  210. {
  211. player.sendPacket(SystemMessageId.BID_PRICE_MUST_BE_HIGHER);
  212. return;
  213. }
  214. if (newBid > 100000000000L)
  215. {
  216. player.sendPacket(SystemMessageId.BID_CANT_EXCEED_100_BILLION);
  217. return;
  218. }
  219. if (getAuctionState() != ItemAuctionState.STARTED)
  220. return;
  221. final int playerObjId = player.getObjectId();
  222. synchronized (_auctionBids)
  223. {
  224. if (_highestBid != null && newBid < _highestBid.getLastBid())
  225. {
  226. player.sendPacket(SystemMessageId.BID_MUST_BE_HIGHER_THAN_CURRENT_BID);
  227. return;
  228. }
  229. ItemAuctionBid bid = getBidFor(playerObjId);
  230. if (bid == null)
  231. {
  232. if (!reduceItemCount(player, newBid))
  233. {
  234. player.sendPacket(SystemMessageId.NOT_ENOUGH_ADENA_FOR_THIS_BID);
  235. return;
  236. }
  237. bid = new ItemAuctionBid(playerObjId, newBid);
  238. _auctionBids.add(bid);
  239. }
  240. else
  241. {
  242. if (!bid.isCanceled())
  243. {
  244. if (newBid < bid.getLastBid()) // just another check
  245. {
  246. player.sendPacket(SystemMessageId.BID_MUST_BE_HIGHER_THAN_CURRENT_BID);
  247. return;
  248. }
  249. if (!reduceItemCount(player, newBid - bid.getLastBid()))
  250. {
  251. player.sendPacket(SystemMessageId.NOT_ENOUGH_ADENA_FOR_THIS_BID);
  252. return;
  253. }
  254. }
  255. else if (!reduceItemCount(player, newBid))
  256. {
  257. player.sendPacket(SystemMessageId.NOT_ENOUGH_ADENA_FOR_THIS_BID);
  258. return;
  259. }
  260. bid.setLastBid(newBid);
  261. }
  262. onPlayerBid(player, bid);
  263. updatePlayerBid(bid, false);
  264. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.SUBMITTED_A_BID);
  265. sm.addItemNumber(newBid);
  266. player.sendPacket(sm);
  267. return;
  268. }
  269. }
  270. private final void onPlayerBid(final L2PcInstance player, final ItemAuctionBid bid)
  271. {
  272. if (_highestBid == null)
  273. {
  274. _highestBid = bid;
  275. }
  276. else if (_highestBid.getLastBid() < bid.getLastBid())
  277. {
  278. final L2PcInstance old = _highestBid.getPlayer();
  279. if (old != null)
  280. old.sendPacket(SystemMessageId.YOU_HAVE_BEEN_OUTBID);
  281. _highestBid = bid;
  282. }
  283. if (getEndingTime() - System.currentTimeMillis() <= (1000 * 60 * 10)) // 10 minutes
  284. {
  285. switch (_auctionEndingExtendState)
  286. {
  287. case INITIAL:
  288. {
  289. _auctionEndingExtendState = ItemAuctionExtendState.EXTEND_BY_5_MIN;
  290. _endingTime += ENDING_TIME_EXTEND_5;
  291. broadcastToAllBidders(SystemMessage.getSystemMessage(SystemMessageId.BIDDER_EXISTS_AUCTION_TIME_EXTENDED_BY_5_MINUTES));
  292. break;
  293. }
  294. case EXTEND_BY_5_MIN:
  295. {
  296. if (getAndSetLastBidPlayerObjectId(player.getObjectId()) != player.getObjectId())
  297. {
  298. _auctionEndingExtendState = ItemAuctionExtendState.EXTEND_BY_3_MIN;
  299. _endingTime += ENDING_TIME_EXTEND_3;
  300. broadcastToAllBidders(SystemMessage.getSystemMessage(SystemMessageId.BIDDER_EXISTS_AUCTION_TIME_EXTENDED_BY_3_MINUTES));
  301. }
  302. break;
  303. }
  304. case EXTEND_BY_3_MIN:
  305. if (Config.ALT_ITEM_AUCTION_TIME_EXTENDS_ON_BID > 0)
  306. {
  307. if (getAndSetLastBidPlayerObjectId(player.getObjectId()) != player.getObjectId())
  308. {
  309. _auctionEndingExtendState = ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_A;
  310. _endingTime += Config.ALT_ITEM_AUCTION_TIME_EXTENDS_ON_BID;
  311. }
  312. }
  313. break;
  314. case EXTEND_BY_CONFIG_PHASE_A:
  315. {
  316. if (getAndSetLastBidPlayerObjectId(player.getObjectId()) != player.getObjectId())
  317. {
  318. if (_scheduledAuctionEndingExtendState == ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_B)
  319. {
  320. _auctionEndingExtendState = ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_B;
  321. _endingTime += Config.ALT_ITEM_AUCTION_TIME_EXTENDS_ON_BID;
  322. }
  323. }
  324. break;
  325. }
  326. case EXTEND_BY_CONFIG_PHASE_B:
  327. {
  328. if (getAndSetLastBidPlayerObjectId(player.getObjectId()) != player.getObjectId())
  329. {
  330. if (_scheduledAuctionEndingExtendState == ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_A)
  331. {
  332. _endingTime += Config.ALT_ITEM_AUCTION_TIME_EXTENDS_ON_BID;
  333. _auctionEndingExtendState = ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_A;
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340. public final void broadcastToAllBidders(final L2GameServerPacket packet)
  341. {
  342. ThreadPoolManager.getInstance().executeTask(new Runnable()
  343. {
  344. @Override
  345. public final void run()
  346. {
  347. broadcastToAllBiddersInternal(packet);
  348. }
  349. });
  350. }
  351. public final void broadcastToAllBiddersInternal(final L2GameServerPacket packet)
  352. {
  353. for (int i = _auctionBids.size(); i-- > 0;)
  354. {
  355. final ItemAuctionBid bid = _auctionBids.get(i);
  356. if (bid != null)
  357. {
  358. final L2PcInstance player = bid.getPlayer();
  359. if (player != null)
  360. player.sendPacket(packet);
  361. }
  362. }
  363. }
  364. public final boolean cancelBid(final L2PcInstance player)
  365. {
  366. if (player == null)
  367. throw new NullPointerException();
  368. switch (getAuctionState())
  369. {
  370. case CREATED:
  371. return false;
  372. case FINISHED:
  373. if (_startingTime < System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(Config.ALT_ITEM_AUCTION_EXPIRED_AFTER, TimeUnit.DAYS))
  374. return false;
  375. break;
  376. }
  377. final int playerObjId = player.getObjectId();
  378. synchronized (_auctionBids)
  379. {
  380. if (_highestBid == null)
  381. return false;
  382. final int bidIndex = getBidIndexFor(playerObjId);
  383. if (bidIndex == -1)
  384. return false;
  385. final ItemAuctionBid bid = _auctionBids.get(bidIndex);
  386. if (bid.getPlayerObjId() == _highestBid.getPlayerObjId())
  387. {
  388. // can't return winning bid
  389. if (getAuctionState() == ItemAuctionState.FINISHED)
  390. return false;
  391. player.sendPacket(SystemMessageId.HIGHEST_BID_BUT_RESERVE_NOT_MET);
  392. return true;
  393. }
  394. if (bid.isCanceled())
  395. return false;
  396. increaseItemCount(player, bid.getLastBid());
  397. bid.cancelBid();
  398. // delete bid from database if auction already finished
  399. updatePlayerBid(bid, getAuctionState() == ItemAuctionState.FINISHED);
  400. player.sendPacket(SystemMessageId.CANCELED_BID);
  401. }
  402. return true;
  403. }
  404. public final void clearCanceledBids()
  405. {
  406. if (getAuctionState() != ItemAuctionState.FINISHED)
  407. throw new IllegalStateException("Attempt to clear canceled bids for non-finished auction");
  408. synchronized (_auctionBids)
  409. {
  410. for (ItemAuctionBid bid : _auctionBids)
  411. {
  412. if (bid == null || !bid.isCanceled())
  413. continue;
  414. updatePlayerBid(bid, true);
  415. }
  416. }
  417. }
  418. private final boolean reduceItemCount(final L2PcInstance player, final long count)
  419. {
  420. if (!player.reduceAdena("ItemAuction", count, player, true))
  421. {
  422. player.sendPacket(SystemMessageId.NOT_ENOUGH_ADENA_FOR_THIS_BID);
  423. return false;
  424. }
  425. return true;
  426. }
  427. private final void increaseItemCount(final L2PcInstance player, final long count)
  428. {
  429. player.addAdena("ItemAuction", count, player, true);
  430. }
  431. /**
  432. * Returns the last bid for the given player or -1 if he did not made one yet.
  433. *
  434. * @param player The player that made the bid
  435. * @return The last bid the player made or -1
  436. */
  437. public final long getLastBid(final L2PcInstance player)
  438. {
  439. final ItemAuctionBid bid = getBidFor(player.getObjectId());
  440. return bid != null ? bid.getLastBid() : -1L;
  441. }
  442. public final ItemAuctionBid getBidFor(final int playerObjId)
  443. {
  444. final int index = getBidIndexFor(playerObjId);
  445. return index != -1 ? _auctionBids.get(index) : null;
  446. }
  447. private final int getBidIndexFor(final int playerObjId)
  448. {
  449. for (int i = _auctionBids.size(); i-- > 0;)
  450. {
  451. final ItemAuctionBid bid = _auctionBids.get(i);
  452. if (bid != null && bid.getPlayerObjId() == playerObjId)
  453. return i;
  454. }
  455. return -1;
  456. }
  457. }