ItemAuction.java 15 KB

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