ItemAuctionInstance.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright (C) 2004-2014 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.ResultSet;
  23. import java.sql.SQLException;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Collection;
  28. import java.util.Comparator;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import java.util.concurrent.ScheduledFuture;
  33. import java.util.concurrent.TimeUnit;
  34. import java.util.concurrent.atomic.AtomicInteger;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. import org.w3c.dom.NamedNodeMap;
  38. import org.w3c.dom.Node;
  39. import com.l2jserver.Config;
  40. import com.l2jserver.L2DatabaseFactory;
  41. import com.l2jserver.gameserver.ThreadPoolManager;
  42. import com.l2jserver.gameserver.datatables.CharNameTable;
  43. import com.l2jserver.gameserver.enums.ItemLocation;
  44. import com.l2jserver.gameserver.instancemanager.ItemAuctionManager;
  45. import com.l2jserver.gameserver.model.L2World;
  46. import com.l2jserver.gameserver.model.StatsSet;
  47. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  48. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  49. import com.l2jserver.gameserver.network.SystemMessageId;
  50. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  51. import com.l2jserver.util.Rnd;
  52. public final class ItemAuctionInstance
  53. {
  54. protected static final Logger _log = Logger.getLogger(ItemAuctionInstance.class.getName());
  55. private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss dd.MM.yy");
  56. private static final long START_TIME_SPACE = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);
  57. private static final long FINISH_TIME_SPACE = TimeUnit.MILLISECONDS.convert(10, TimeUnit.MINUTES);
  58. // SQL queries
  59. private static final String SELECT_AUCTION_ID_BY_INSTANCE_ID = "SELECT auctionId FROM item_auction WHERE instanceId = ?";
  60. private static final String SELECT_AUCTION_INFO = "SELECT auctionItemId, startingTime, endingTime, auctionStateId FROM item_auction WHERE auctionId = ? ";
  61. private static final String DELETE_AUCTION_INFO_BY_AUCTION_ID = "DELETE FROM item_auction WHERE auctionId = ?";
  62. private static final String DELETE_AUCTION_BID_INFO_BY_AUCTION_ID = "DELETE FROM item_auction_bid WHERE auctionId = ?";
  63. private static final String SELECT_PLAYERS_ID_BY_AUCTION_ID = "SELECT playerObjId, playerBid FROM item_auction_bid WHERE auctionId = ?";
  64. /**
  65. * Cached comparator to avoid initialization on each loop run.
  66. */
  67. private static final Comparator<ItemAuction> itemAuctionComparator = new Comparator<ItemAuction>()
  68. {
  69. @Override
  70. public final int compare(final ItemAuction o1, final ItemAuction o2)
  71. {
  72. return Long.valueOf(o2.getStartingTime()).compareTo(Long.valueOf(o1.getStartingTime()));
  73. }
  74. };
  75. private final int _instanceId;
  76. private final AtomicInteger _auctionIds;
  77. private final Map<Integer, ItemAuction> _auctions;
  78. private final ArrayList<AuctionItem> _items;
  79. private final AuctionDateGenerator _dateGenerator;
  80. private ItemAuction _currentAuction;
  81. private ItemAuction _nextAuction;
  82. private ScheduledFuture<?> _stateTask;
  83. public ItemAuctionInstance(final int instanceId, final AtomicInteger auctionIds, final Node node) throws Exception
  84. {
  85. _instanceId = instanceId;
  86. _auctionIds = auctionIds;
  87. _auctions = new HashMap<>();
  88. _items = new ArrayList<>();
  89. final NamedNodeMap nanode = node.getAttributes();
  90. final StatsSet generatorConfig = new StatsSet();
  91. for (int i = nanode.getLength(); i-- > 0;)
  92. {
  93. final Node n = nanode.item(i);
  94. if (n != null)
  95. {
  96. generatorConfig.set(n.getNodeName(), n.getNodeValue());
  97. }
  98. }
  99. _dateGenerator = new AuctionDateGenerator(generatorConfig);
  100. for (Node na = node.getFirstChild(); na != null; na = na.getNextSibling())
  101. {
  102. try
  103. {
  104. if ("item".equalsIgnoreCase(na.getNodeName()))
  105. {
  106. final NamedNodeMap naa = na.getAttributes();
  107. final int auctionItemId = Integer.parseInt(naa.getNamedItem("auctionItemId").getNodeValue());
  108. final int auctionLenght = Integer.parseInt(naa.getNamedItem("auctionLenght").getNodeValue());
  109. final long auctionInitBid = Integer.parseInt(naa.getNamedItem("auctionInitBid").getNodeValue());
  110. final int itemId = Integer.parseInt(naa.getNamedItem("itemId").getNodeValue());
  111. final int itemCount = Integer.parseInt(naa.getNamedItem("itemCount").getNodeValue());
  112. if (auctionLenght < 1)
  113. {
  114. throw new IllegalArgumentException("auctionLenght < 1 for instanceId: " + _instanceId + ", itemId " + itemId);
  115. }
  116. final StatsSet itemExtra = new StatsSet();
  117. final AuctionItem item = new AuctionItem(auctionItemId, auctionLenght, auctionInitBid, itemId, itemCount, itemExtra);
  118. if (!item.checkItemExists())
  119. {
  120. throw new IllegalArgumentException("Item with id " + itemId + " not found");
  121. }
  122. for (final AuctionItem tmp : _items)
  123. {
  124. if (tmp.getAuctionItemId() == auctionItemId)
  125. {
  126. throw new IllegalArgumentException("Dublicated auction item id " + auctionItemId);
  127. }
  128. }
  129. _items.add(item);
  130. for (Node nb = na.getFirstChild(); nb != null; nb = nb.getNextSibling())
  131. {
  132. if ("extra".equalsIgnoreCase(nb.getNodeName()))
  133. {
  134. final NamedNodeMap nab = nb.getAttributes();
  135. for (int i = nab.getLength(); i-- > 0;)
  136. {
  137. final Node n = nab.item(i);
  138. if (n != null)
  139. {
  140. itemExtra.set(n.getNodeName(), n.getNodeValue());
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. catch (final IllegalArgumentException e)
  148. {
  149. _log.log(Level.WARNING, getClass().getSimpleName() + ": Failed loading auction item", e);
  150. }
  151. }
  152. if (_items.isEmpty())
  153. {
  154. throw new IllegalArgumentException("No items defined");
  155. }
  156. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  157. PreparedStatement ps = con.prepareStatement(SELECT_AUCTION_ID_BY_INSTANCE_ID))
  158. {
  159. ps.setInt(1, _instanceId);
  160. try (ResultSet rset = ps.executeQuery())
  161. {
  162. while (rset.next())
  163. {
  164. final int auctionId = rset.getInt(1);
  165. try
  166. {
  167. final ItemAuction auction = loadAuction(auctionId);
  168. if (auction != null)
  169. {
  170. _auctions.put(auctionId, auction);
  171. }
  172. else
  173. {
  174. ItemAuctionManager.deleteAuction(auctionId);
  175. }
  176. }
  177. catch (final SQLException e)
  178. {
  179. _log.log(Level.WARNING, getClass().getSimpleName() + ": Failed loading auction: " + auctionId, e);
  180. }
  181. }
  182. }
  183. }
  184. catch (final SQLException e)
  185. {
  186. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Failed loading auctions.", e);
  187. return;
  188. }
  189. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded " + _items.size() + " item(s) and registered " + _auctions.size() + " auction(s) for instance " + _instanceId + ".");
  190. checkAndSetCurrentAndNextAuction();
  191. }
  192. public final ItemAuction getCurrentAuction()
  193. {
  194. return _currentAuction;
  195. }
  196. public final ItemAuction getNextAuction()
  197. {
  198. return _nextAuction;
  199. }
  200. public final void shutdown()
  201. {
  202. final ScheduledFuture<?> stateTask = _stateTask;
  203. if (stateTask != null)
  204. {
  205. stateTask.cancel(false);
  206. }
  207. }
  208. private final AuctionItem getAuctionItem(final int auctionItemId)
  209. {
  210. for (int i = _items.size(); i-- > 0;)
  211. {
  212. final AuctionItem item = _items.get(i);
  213. if (item.getAuctionItemId() == auctionItemId)
  214. {
  215. return item;
  216. }
  217. }
  218. return null;
  219. }
  220. final void checkAndSetCurrentAndNextAuction()
  221. {
  222. final ItemAuction[] auctions = _auctions.values().toArray(new ItemAuction[_auctions.size()]);
  223. ItemAuction currentAuction = null;
  224. ItemAuction nextAuction = null;
  225. switch (auctions.length)
  226. {
  227. case 0:
  228. {
  229. nextAuction = createAuction(System.currentTimeMillis() + START_TIME_SPACE);
  230. break;
  231. }
  232. case 1:
  233. {
  234. switch (auctions[0].getAuctionState())
  235. {
  236. case CREATED:
  237. {
  238. if (auctions[0].getStartingTime() < (System.currentTimeMillis() + START_TIME_SPACE))
  239. {
  240. currentAuction = auctions[0];
  241. nextAuction = createAuction(System.currentTimeMillis() + START_TIME_SPACE);
  242. }
  243. else
  244. {
  245. nextAuction = auctions[0];
  246. }
  247. break;
  248. }
  249. case STARTED:
  250. {
  251. currentAuction = auctions[0];
  252. nextAuction = createAuction(Math.max(currentAuction.getEndingTime() + FINISH_TIME_SPACE, System.currentTimeMillis() + START_TIME_SPACE));
  253. break;
  254. }
  255. case FINISHED:
  256. {
  257. currentAuction = auctions[0];
  258. nextAuction = createAuction(System.currentTimeMillis() + START_TIME_SPACE);
  259. break;
  260. }
  261. default:
  262. throw new IllegalArgumentException();
  263. }
  264. break;
  265. }
  266. default:
  267. {
  268. Arrays.sort(auctions, itemAuctionComparator);
  269. // just to make sure we won't skip any auction because of little different times
  270. final long currentTime = System.currentTimeMillis();
  271. for (final ItemAuction auction : auctions)
  272. {
  273. if (auction.getAuctionState() == ItemAuctionState.STARTED)
  274. {
  275. currentAuction = auction;
  276. break;
  277. }
  278. else if (auction.getStartingTime() <= currentTime)
  279. {
  280. currentAuction = auction;
  281. break; // only first
  282. }
  283. }
  284. for (final ItemAuction auction : auctions)
  285. {
  286. if ((auction.getStartingTime() > currentTime) && (currentAuction != auction))
  287. {
  288. nextAuction = auction;
  289. break;
  290. }
  291. }
  292. if (nextAuction == null)
  293. {
  294. nextAuction = createAuction(System.currentTimeMillis() + START_TIME_SPACE);
  295. }
  296. break;
  297. }
  298. }
  299. _auctions.put(nextAuction.getAuctionId(), nextAuction);
  300. _currentAuction = currentAuction;
  301. _nextAuction = nextAuction;
  302. if ((currentAuction != null) && (currentAuction.getAuctionState() != ItemAuctionState.FINISHED))
  303. {
  304. if (currentAuction.getAuctionState() == ItemAuctionState.STARTED)
  305. {
  306. setStateTask(ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleAuctionTask(currentAuction), Math.max(currentAuction.getEndingTime() - System.currentTimeMillis(), 0L)));
  307. }
  308. else
  309. {
  310. setStateTask(ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleAuctionTask(currentAuction), Math.max(currentAuction.getStartingTime() - System.currentTimeMillis(), 0L)));
  311. }
  312. _log.log(Level.INFO, getClass().getSimpleName() + ": Schedule current auction " + currentAuction.getAuctionId() + " for instance " + _instanceId);
  313. }
  314. else
  315. {
  316. setStateTask(ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleAuctionTask(nextAuction), Math.max(nextAuction.getStartingTime() - System.currentTimeMillis(), 0L)));
  317. _log.log(Level.INFO, getClass().getSimpleName() + ": Schedule next auction " + nextAuction.getAuctionId() + " on " + DATE_FORMAT.format(new Date(nextAuction.getStartingTime())) + " for instance " + _instanceId);
  318. }
  319. }
  320. public final ItemAuction getAuction(final int auctionId)
  321. {
  322. return _auctions.get(auctionId);
  323. }
  324. public final ItemAuction[] getAuctionsByBidder(final int bidderObjId)
  325. {
  326. final Collection<ItemAuction> auctions = getAuctions();
  327. final ArrayList<ItemAuction> stack = new ArrayList<>(auctions.size());
  328. for (final ItemAuction auction : getAuctions())
  329. {
  330. if (auction.getAuctionState() != ItemAuctionState.CREATED)
  331. {
  332. final ItemAuctionBid bid = auction.getBidFor(bidderObjId);
  333. if (bid != null)
  334. {
  335. stack.add(auction);
  336. }
  337. }
  338. }
  339. return stack.toArray(new ItemAuction[stack.size()]);
  340. }
  341. public final Collection<ItemAuction> getAuctions()
  342. {
  343. final Collection<ItemAuction> auctions;
  344. synchronized (_auctions)
  345. {
  346. auctions = _auctions.values();
  347. }
  348. return auctions;
  349. }
  350. private final class ScheduleAuctionTask implements Runnable
  351. {
  352. private final ItemAuction _auction;
  353. public ScheduleAuctionTask(final ItemAuction auction)
  354. {
  355. _auction = auction;
  356. }
  357. @Override
  358. public final void run()
  359. {
  360. try
  361. {
  362. runImpl();
  363. }
  364. catch (final Exception e)
  365. {
  366. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Failed scheduling auction " + _auction.getAuctionId(), e);
  367. }
  368. }
  369. private final void runImpl() throws Exception
  370. {
  371. final ItemAuctionState state = _auction.getAuctionState();
  372. switch (state)
  373. {
  374. case CREATED:
  375. {
  376. if (!_auction.setAuctionState(state, ItemAuctionState.STARTED))
  377. {
  378. throw new IllegalStateException("Could not set auction state: " + ItemAuctionState.STARTED.toString() + ", expected: " + state.toString());
  379. }
  380. _log.log(Level.INFO, getClass().getSimpleName() + ": Auction " + _auction.getAuctionId() + " has started for instance " + _auction.getInstanceId());
  381. checkAndSetCurrentAndNextAuction();
  382. break;
  383. }
  384. case STARTED:
  385. {
  386. switch (_auction.getAuctionEndingExtendState())
  387. {
  388. case EXTEND_BY_5_MIN:
  389. {
  390. if (_auction.getScheduledAuctionEndingExtendState() == ItemAuctionExtendState.INITIAL)
  391. {
  392. _auction.setScheduledAuctionEndingExtendState(ItemAuctionExtendState.EXTEND_BY_5_MIN);
  393. setStateTask(ThreadPoolManager.getInstance().scheduleGeneral(this, Math.max(_auction.getEndingTime() - System.currentTimeMillis(), 0L)));
  394. return;
  395. }
  396. break;
  397. }
  398. case EXTEND_BY_3_MIN:
  399. {
  400. if (_auction.getScheduledAuctionEndingExtendState() != ItemAuctionExtendState.EXTEND_BY_3_MIN)
  401. {
  402. _auction.setScheduledAuctionEndingExtendState(ItemAuctionExtendState.EXTEND_BY_3_MIN);
  403. setStateTask(ThreadPoolManager.getInstance().scheduleGeneral(this, Math.max(_auction.getEndingTime() - System.currentTimeMillis(), 0L)));
  404. return;
  405. }
  406. break;
  407. }
  408. case EXTEND_BY_CONFIG_PHASE_A:
  409. {
  410. if (_auction.getScheduledAuctionEndingExtendState() != ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_B)
  411. {
  412. _auction.setScheduledAuctionEndingExtendState(ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_B);
  413. setStateTask(ThreadPoolManager.getInstance().scheduleGeneral(this, Math.max(_auction.getEndingTime() - System.currentTimeMillis(), 0L)));
  414. return;
  415. }
  416. break;
  417. }
  418. case EXTEND_BY_CONFIG_PHASE_B:
  419. {
  420. if (_auction.getScheduledAuctionEndingExtendState() != ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_A)
  421. {
  422. _auction.setScheduledAuctionEndingExtendState(ItemAuctionExtendState.EXTEND_BY_CONFIG_PHASE_A);
  423. setStateTask(ThreadPoolManager.getInstance().scheduleGeneral(this, Math.max(_auction.getEndingTime() - System.currentTimeMillis(), 0L)));
  424. return;
  425. }
  426. }
  427. }
  428. if (!_auction.setAuctionState(state, ItemAuctionState.FINISHED))
  429. {
  430. throw new IllegalStateException("Could not set auction state: " + ItemAuctionState.FINISHED.toString() + ", expected: " + state.toString());
  431. }
  432. onAuctionFinished(_auction);
  433. checkAndSetCurrentAndNextAuction();
  434. break;
  435. }
  436. default:
  437. throw new IllegalStateException("Invalid state: " + state);
  438. }
  439. }
  440. }
  441. final void onAuctionFinished(final ItemAuction auction)
  442. {
  443. auction.broadcastToAllBiddersInternal(SystemMessage.getSystemMessage(SystemMessageId.S1_AUCTION_ENDED).addInt(auction.getAuctionId()));
  444. final ItemAuctionBid bid = auction.getHighestBid();
  445. if (bid != null)
  446. {
  447. final L2ItemInstance item = auction.createNewItemInstance();
  448. final L2PcInstance player = bid.getPlayer();
  449. if (player != null)
  450. {
  451. player.getWarehouse().addItem("ItemAuction", item, null, null);
  452. player.sendPacket(SystemMessageId.WON_BID_ITEM_CAN_BE_FOUND_IN_WAREHOUSE);
  453. _log.log(Level.INFO, getClass().getSimpleName() + ": Auction " + auction.getAuctionId() + " has finished. Highest bid by " + player.getName() + " for instance " + _instanceId);
  454. }
  455. else
  456. {
  457. item.setOwnerId(bid.getPlayerObjId());
  458. item.setItemLocation(ItemLocation.WAREHOUSE);
  459. item.updateDatabase();
  460. L2World.getInstance().removeObject(item);
  461. _log.log(Level.INFO, getClass().getSimpleName() + ": Auction " + auction.getAuctionId() + " has finished. Highest bid by " + CharNameTable.getInstance().getNameById(bid.getPlayerObjId()) + " for instance " + _instanceId);
  462. }
  463. // Clean all canceled bids
  464. auction.clearCanceledBids();
  465. }
  466. else
  467. {
  468. _log.log(Level.INFO, getClass().getSimpleName() + ": Auction " + auction.getAuctionId() + " has finished. There have not been any bid for instance " + _instanceId);
  469. }
  470. }
  471. final void setStateTask(final ScheduledFuture<?> future)
  472. {
  473. final ScheduledFuture<?> stateTask = _stateTask;
  474. if (stateTask != null)
  475. {
  476. stateTask.cancel(false);
  477. }
  478. _stateTask = future;
  479. }
  480. private final ItemAuction createAuction(final long after)
  481. {
  482. final AuctionItem auctionItem = _items.get(Rnd.get(_items.size()));
  483. final long startingTime = _dateGenerator.nextDate(after);
  484. final long endingTime = startingTime + TimeUnit.MILLISECONDS.convert(auctionItem.getAuctionLength(), TimeUnit.MINUTES);
  485. final ItemAuction auction = new ItemAuction(_auctionIds.getAndIncrement(), _instanceId, startingTime, endingTime, auctionItem);
  486. auction.storeMe();
  487. return auction;
  488. }
  489. private final ItemAuction loadAuction(final int auctionId) throws SQLException
  490. {
  491. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  492. {
  493. int auctionItemId = 0;
  494. long startingTime = 0;
  495. long endingTime = 0;
  496. byte auctionStateId = 0;
  497. try (PreparedStatement ps = con.prepareStatement(SELECT_AUCTION_INFO))
  498. {
  499. ps.setInt(1, auctionId);
  500. try (ResultSet rset = ps.executeQuery())
  501. {
  502. if (!rset.next())
  503. {
  504. _log.log(Level.WARNING, getClass().getSimpleName() + ": Auction data not found for auction: " + auctionId);
  505. return null;
  506. }
  507. auctionItemId = rset.getInt(1);
  508. startingTime = rset.getLong(2);
  509. endingTime = rset.getLong(3);
  510. auctionStateId = rset.getByte(4);
  511. }
  512. }
  513. if (startingTime >= endingTime)
  514. {
  515. _log.log(Level.WARNING, getClass().getSimpleName() + ": Invalid starting/ending paramaters for auction: " + auctionId);
  516. return null;
  517. }
  518. final AuctionItem auctionItem = getAuctionItem(auctionItemId);
  519. if (auctionItem == null)
  520. {
  521. _log.log(Level.WARNING, getClass().getSimpleName() + ": AuctionItem: " + auctionItemId + ", not found for auction: " + auctionId);
  522. return null;
  523. }
  524. final ItemAuctionState auctionState = ItemAuctionState.stateForStateId(auctionStateId);
  525. if (auctionState == null)
  526. {
  527. _log.log(Level.WARNING, getClass().getSimpleName() + ": Invalid auctionStateId: " + auctionStateId + ", for auction: " + auctionId);
  528. return null;
  529. }
  530. if ((auctionState == ItemAuctionState.FINISHED) && (startingTime < (System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(Config.ALT_ITEM_AUCTION_EXPIRED_AFTER, TimeUnit.DAYS))))
  531. {
  532. _log.log(Level.INFO, getClass().getSimpleName() + ": Clearing expired auction: " + auctionId);
  533. try (PreparedStatement ps = con.prepareStatement(DELETE_AUCTION_INFO_BY_AUCTION_ID))
  534. {
  535. ps.setInt(1, auctionId);
  536. ps.execute();
  537. }
  538. try (PreparedStatement ps = con.prepareStatement(DELETE_AUCTION_BID_INFO_BY_AUCTION_ID))
  539. {
  540. ps.setInt(1, auctionId);
  541. ps.execute();
  542. }
  543. return null;
  544. }
  545. final ArrayList<ItemAuctionBid> auctionBids = new ArrayList<>();
  546. try (PreparedStatement ps = con.prepareStatement(SELECT_PLAYERS_ID_BY_AUCTION_ID))
  547. {
  548. ps.setInt(1, auctionId);
  549. try (ResultSet rs = ps.executeQuery())
  550. {
  551. while (rs.next())
  552. {
  553. final int playerObjId = rs.getInt(1);
  554. final long playerBid = rs.getLong(2);
  555. final ItemAuctionBid bid = new ItemAuctionBid(playerObjId, playerBid);
  556. auctionBids.add(bid);
  557. }
  558. }
  559. }
  560. return new ItemAuction(auctionId, _instanceId, startingTime, endingTime, auctionItem, auctionBids, auctionState);
  561. }
  562. }
  563. }