2
0

ItemAuction.java 15 KB

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