ItemAuction.java 15 KB

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