ItemAuction.java 15 KB

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