Auction.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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.entity;
  16. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.ADENA_ID;
  17. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.util.Calendar;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.datatables.ClanTable;
  28. import com.l2jserver.gameserver.idfactory.IdFactory;
  29. import com.l2jserver.gameserver.instancemanager.AuctionManager;
  30. import com.l2jserver.gameserver.instancemanager.ClanHallManager;
  31. import com.l2jserver.gameserver.model.L2Clan;
  32. import com.l2jserver.gameserver.model.L2World;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.network.SystemMessageId;
  35. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  36. import javolution.util.FastMap;
  37. public class Auction
  38. {
  39. protected static final Logger _log = Logger.getLogger(Auction.class.getName());
  40. private int _id = 0;
  41. private long _endDate;
  42. private int _highestBidderId = 0;
  43. private String _highestBidderName = "";
  44. private long _highestBidderMaxBid = 0;
  45. private int _itemId = 0;
  46. private String _itemName = "";
  47. private int _itemObjectId = 0;
  48. private long _itemQuantity = 0;
  49. private String _itemType = "";
  50. private int _sellerId = 0;
  51. private String _sellerClanName = "";
  52. private String _sellerName = "";
  53. private long _currentBid = 0;
  54. private long _startingBid = 0;
  55. private Map<Integer, Bidder> _bidders = new FastMap<Integer, Bidder>();
  56. private static final String[] ItemTypeName =
  57. {
  58. "ClanHall"
  59. };
  60. public static enum ItemTypeEnum
  61. {
  62. ClanHall
  63. }
  64. public class Bidder
  65. {
  66. private String _name; //TODO replace with objid
  67. private String _clanName;
  68. private long _bid;
  69. private Calendar _timeBid;
  70. public Bidder(String name, String clanName, long bid, long timeBid)
  71. {
  72. _name = name;
  73. _clanName = clanName;
  74. _bid = bid;
  75. _timeBid = Calendar.getInstance();
  76. _timeBid.setTimeInMillis(timeBid);
  77. }
  78. public String getName()
  79. {
  80. return _name;
  81. }
  82. public String getClanName()
  83. {
  84. return _clanName;
  85. }
  86. public long getBid()
  87. {
  88. return _bid;
  89. }
  90. public Calendar getTimeBid()
  91. {
  92. return _timeBid;
  93. }
  94. public void setTimeBid(long timeBid)
  95. {
  96. _timeBid.setTimeInMillis(timeBid);
  97. }
  98. public void setBid(long bid)
  99. {
  100. _bid = bid;
  101. }
  102. }
  103. /** Task Sheduler for endAuction */
  104. public class AutoEndTask implements Runnable
  105. {
  106. public AutoEndTask()
  107. {
  108. }
  109. public void run()
  110. {
  111. try
  112. {
  113. endAuction();
  114. }
  115. catch (Exception e)
  116. {
  117. _log.log(Level.SEVERE, "", e);
  118. }
  119. }
  120. }
  121. /** Constructor */
  122. public Auction(int auctionId)
  123. {
  124. _id = auctionId;
  125. load();
  126. startAutoTask();
  127. }
  128. public Auction(int itemId, L2Clan Clan, long delay, long bid, String name)
  129. {
  130. _id = itemId;
  131. _endDate = System.currentTimeMillis() + delay;
  132. _itemId = itemId;
  133. _itemName = name;
  134. _itemType = "ClanHall";
  135. _sellerId = Clan.getLeaderId();
  136. _sellerName = Clan.getLeaderName();
  137. _sellerClanName = Clan.getName();
  138. _startingBid = bid;
  139. }
  140. /** Load auctions */
  141. private void load()
  142. {
  143. Connection con = null;
  144. try
  145. {
  146. PreparedStatement statement;
  147. ResultSet rs;
  148. con = L2DatabaseFactory.getInstance().getConnection();
  149. statement = con.prepareStatement("Select * from auction where id = ?");
  150. statement.setInt(1, getId());
  151. rs = statement.executeQuery();
  152. while (rs.next())
  153. {
  154. _currentBid = rs.getLong("currentBid");
  155. _endDate = rs.getLong("endDate");
  156. _itemId = rs.getInt("itemId");
  157. _itemName = rs.getString("itemName");
  158. _itemObjectId = rs.getInt("itemObjectId");
  159. _itemType = rs.getString("itemType");
  160. _sellerId = rs.getInt("sellerId");
  161. _sellerClanName = rs.getString("sellerClanName");
  162. _sellerName = rs.getString("sellerName");
  163. _startingBid = rs.getLong("startingBid");
  164. }
  165. statement.close();
  166. loadBid();
  167. }
  168. catch (Exception e)
  169. {
  170. _log.warning("Exception: Auction.load(): " + e.getMessage());
  171. e.printStackTrace();
  172. }
  173. finally
  174. {
  175. try
  176. {
  177. con.close();
  178. }
  179. catch (Exception e)
  180. {
  181. }
  182. }
  183. }
  184. /** Load bidders **/
  185. private void loadBid()
  186. {
  187. _highestBidderId = 0;
  188. _highestBidderName = "";
  189. _highestBidderMaxBid = 0;
  190. Connection con = null;
  191. try
  192. {
  193. PreparedStatement statement;
  194. ResultSet rs;
  195. con = L2DatabaseFactory.getInstance().getConnection();
  196. statement = con.prepareStatement("SELECT bidderId, bidderName, maxBid, clan_name, time_bid FROM auction_bid WHERE auctionId = ? ORDER BY maxBid DESC");
  197. statement.setInt(1, getId());
  198. rs = statement.executeQuery();
  199. while (rs.next())
  200. {
  201. if (rs.isFirst())
  202. {
  203. _highestBidderId = rs.getInt("bidderId");
  204. _highestBidderName = rs.getString("bidderName");
  205. _highestBidderMaxBid = rs.getLong("maxBid");
  206. }
  207. _bidders.put(rs.getInt("bidderId"), new Bidder(rs.getString("bidderName"), rs.getString("clan_name"), rs.getLong("maxBid"), rs.getLong("time_bid")));
  208. }
  209. statement.close();
  210. }
  211. catch (Exception e)
  212. {
  213. _log.warning("Exception: Auction.loadBid(): " + e.getMessage());
  214. e.printStackTrace();
  215. }
  216. finally
  217. {
  218. try
  219. {
  220. con.close();
  221. }
  222. catch (Exception e)
  223. {
  224. }
  225. }
  226. }
  227. /** Task Manage */
  228. private void startAutoTask()
  229. {
  230. long currentTime = System.currentTimeMillis();
  231. long taskDelay = 0;
  232. if (_endDate <= currentTime)
  233. {
  234. _endDate = currentTime + 7 * 24 * 60 * 60 * 1000;
  235. saveAuctionDate();
  236. }
  237. else
  238. taskDelay = _endDate - currentTime;
  239. ThreadPoolManager.getInstance().scheduleGeneral(new AutoEndTask(), taskDelay);
  240. }
  241. public static String getItemTypeName(ItemTypeEnum value)
  242. {
  243. return ItemTypeName[value.ordinal()];
  244. }
  245. /** Save Auction Data End */
  246. private void saveAuctionDate()
  247. {
  248. Connection con = null;
  249. try
  250. {
  251. con = L2DatabaseFactory.getInstance().getConnection();
  252. PreparedStatement statement = con.prepareStatement("Update auction set endDate = ? where id = ?");
  253. statement.setLong(1, _endDate);
  254. statement.setInt(2, _id);
  255. statement.execute();
  256. statement.close();
  257. }
  258. catch (Exception e)
  259. {
  260. _log.log(Level.SEVERE, "Exception: saveAuctionDate(): " + e.getMessage(), e);
  261. }
  262. finally
  263. {
  264. try
  265. {
  266. con.close();
  267. }
  268. catch (Exception e)
  269. {
  270. }
  271. }
  272. }
  273. /** Set a bid */
  274. public synchronized void setBid(L2PcInstance bidder, long bid)
  275. {
  276. long requiredAdena = bid;
  277. if (getHighestBidderName().equals(bidder.getClan().getLeaderName()))
  278. requiredAdena = bid - getHighestBidderMaxBid();
  279. if ((getHighestBidderId() > 0 && bid > getHighestBidderMaxBid()) || (getHighestBidderId() == 0 && bid >= getStartingBid()))
  280. {
  281. if (takeItem(bidder, requiredAdena))
  282. {
  283. updateInDB(bidder, bid);
  284. bidder.getClan().setAuctionBiddedAt(_id, true);
  285. return;
  286. }
  287. }
  288. if ((bid < getStartingBid()) || (bid <= getHighestBidderMaxBid()))
  289. bidder.sendPacket(new SystemMessage(SystemMessageId.BID_PRICE_MUST_BE_HIGHER));
  290. }
  291. /** Return Item in WHC */
  292. private void returnItem(String Clan, long quantity, boolean penalty)
  293. {
  294. if (penalty)
  295. quantity *= 0.9; //take 10% tax fee if needed
  296. // avoid overflow on return
  297. final long limit = MAX_ADENA - ClanTable.getInstance().getClanByName(Clan).getWarehouse().getAdena();
  298. quantity = Math.min(quantity, limit);
  299. ClanTable.getInstance().getClanByName(Clan).getWarehouse().addItem("Outbidded", ADENA_ID, quantity, null, null);
  300. }
  301. /** Take Item in WHC */
  302. private boolean takeItem(L2PcInstance bidder, long quantity)
  303. {
  304. if (bidder.getClan() != null && bidder.getClan().getWarehouse().getAdena() >= quantity)
  305. {
  306. bidder.getClan().getWarehouse().destroyItemByItemId("Buy", ADENA_ID, quantity, bidder, bidder);
  307. return true;
  308. }
  309. bidder.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_ADENA_IN_CWH));
  310. return false;
  311. }
  312. /** Update auction in DB */
  313. private void updateInDB(L2PcInstance bidder, long bid)
  314. {
  315. Connection con = null;
  316. try
  317. {
  318. con = L2DatabaseFactory.getInstance().getConnection();
  319. PreparedStatement statement;
  320. if (getBidders().get(bidder.getClanId()) != null)
  321. {
  322. statement = con.prepareStatement("UPDATE auction_bid SET bidderId=?, bidderName=?, maxBid=?, time_bid=? WHERE auctionId=? AND bidderId=?");
  323. statement.setInt(1, bidder.getClanId());
  324. statement.setString(2, bidder.getClan().getLeaderName());
  325. statement.setLong(3, bid);
  326. statement.setLong(4, System.currentTimeMillis());
  327. statement.setInt(5, getId());
  328. statement.setInt(6, bidder.getClanId());
  329. statement.execute();
  330. statement.close();
  331. }
  332. else
  333. {
  334. statement = con.prepareStatement("INSERT INTO auction_bid (id, auctionId, bidderId, bidderName, maxBid, clan_name, time_bid) VALUES (?, ?, ?, ?, ?, ?, ?)");
  335. statement.setInt(1, IdFactory.getInstance().getNextId());
  336. statement.setInt(2, getId());
  337. statement.setInt(3, bidder.getClanId());
  338. statement.setString(4, bidder.getName());
  339. statement.setLong(5, bid);
  340. statement.setString(6, bidder.getClan().getName());
  341. statement.setLong(7, System.currentTimeMillis());
  342. statement.execute();
  343. statement.close();
  344. if (L2World.getInstance().getPlayer(_highestBidderName) != null)
  345. L2World.getInstance().getPlayer(_highestBidderName).sendMessage("You have been out bidded");
  346. }
  347. _highestBidderId = bidder.getClanId();
  348. _highestBidderMaxBid = bid;
  349. _highestBidderName = bidder.getClan().getLeaderName();
  350. if (_bidders.get(_highestBidderId) == null)
  351. _bidders.put(_highestBidderId, new Bidder(_highestBidderName, bidder.getClan().getName(), bid, Calendar.getInstance().getTimeInMillis()));
  352. else
  353. {
  354. _bidders.get(_highestBidderId).setBid(bid);
  355. _bidders.get(_highestBidderId).setTimeBid(Calendar.getInstance().getTimeInMillis());
  356. }
  357. bidder.sendPacket(new SystemMessage(SystemMessageId.BID_IN_CLANHALL_AUCTION));
  358. }
  359. catch (Exception e)
  360. {
  361. _log.log(Level.SEVERE, "Exception: Auction.updateInDB(L2PcInstance bidder, int bid): " + e.getMessage());
  362. e.printStackTrace();
  363. }
  364. finally
  365. {
  366. try
  367. {
  368. con.close();
  369. }
  370. catch (Exception e)
  371. {
  372. }
  373. }
  374. }
  375. /** Remove bids */
  376. private void removeBids()
  377. {
  378. Connection con = null;
  379. try
  380. {
  381. con = L2DatabaseFactory.getInstance().getConnection();
  382. PreparedStatement statement;
  383. statement = con.prepareStatement("DELETE FROM auction_bid WHERE auctionId=?");
  384. statement.setInt(1, getId());
  385. statement.execute();
  386. statement.close();
  387. }
  388. catch (Exception e)
  389. {
  390. _log.log(Level.SEVERE, "Exception: Auction.deleteFromDB(): " + e.getMessage(), e);
  391. }
  392. finally
  393. {
  394. try
  395. {
  396. con.close();
  397. }
  398. catch (Exception e)
  399. {
  400. }
  401. }
  402. for (Bidder b : _bidders.values())
  403. {
  404. if (ClanTable.getInstance().getClanByName(b.getClanName()).getHasHideout() == 0)
  405. returnItem(b.getClanName(), b.getBid(), true); // 10 % tax
  406. else
  407. {
  408. if (L2World.getInstance().getPlayer(b.getName()) != null)
  409. L2World.getInstance().getPlayer(b.getName()).sendMessage("Congratulation you have won ClanHall!");
  410. }
  411. ClanTable.getInstance().getClanByName(b.getClanName()).setAuctionBiddedAt(0, true);
  412. }
  413. _bidders.clear();
  414. }
  415. /** Remove auctions */
  416. public void deleteAuctionFromDB()
  417. {
  418. AuctionManager.getInstance().getAuctions().remove(this);
  419. Connection con = null;
  420. try
  421. {
  422. con = L2DatabaseFactory.getInstance().getConnection();
  423. PreparedStatement statement;
  424. statement = con.prepareStatement("DELETE FROM auction WHERE itemId=?");
  425. statement.setInt(1, _itemId);
  426. statement.execute();
  427. statement.close();
  428. }
  429. catch (Exception e)
  430. {
  431. _log.log(Level.SEVERE, "Exception: Auction.deleteFromDB(): " + e.getMessage(), e);
  432. }
  433. finally
  434. {
  435. try
  436. {
  437. con.close();
  438. }
  439. catch (Exception e)
  440. {
  441. }
  442. }
  443. }
  444. /** End of auction */
  445. public void endAuction()
  446. {
  447. if (ClanHallManager.getInstance().loaded())
  448. {
  449. if (_highestBidderId == 0 && _sellerId == 0)
  450. {
  451. startAutoTask();
  452. return;
  453. }
  454. if (_highestBidderId == 0 && _sellerId > 0)
  455. {
  456. /** If seller haven't sell ClanHall, auction removed,
  457. * THIS MUST BE CONFIRMED */
  458. int aucId = AuctionManager.getInstance().getAuctionIndex(_id);
  459. AuctionManager.getInstance().getAuctions().remove(aucId);
  460. return;
  461. }
  462. if (_sellerId > 0)
  463. {
  464. returnItem(_sellerClanName, _highestBidderMaxBid, true);
  465. returnItem(_sellerClanName, ClanHallManager.getInstance().getClanHallById(_itemId).getLease(), false);
  466. }
  467. deleteAuctionFromDB();
  468. L2Clan Clan = ClanTable.getInstance().getClanByName(_bidders.get(_highestBidderId).getClanName());
  469. _bidders.remove(_highestBidderId);
  470. Clan.setAuctionBiddedAt(0, true);
  471. removeBids();
  472. ClanHallManager.getInstance().setOwner(_itemId, Clan);
  473. }
  474. else
  475. {
  476. /** Task waiting ClanHallManager is loaded every 3s */
  477. ThreadPoolManager.getInstance().scheduleGeneral(new AutoEndTask(), 3000);
  478. }
  479. }
  480. /** Cancel bid */
  481. public synchronized void cancelBid(int bidder)
  482. {
  483. Connection con = null;
  484. try
  485. {
  486. con = L2DatabaseFactory.getInstance().getConnection();
  487. PreparedStatement statement;
  488. statement = con.prepareStatement("DELETE FROM auction_bid WHERE auctionId=? AND bidderId=?");
  489. statement.setInt(1, getId());
  490. statement.setInt(2, bidder);
  491. statement.execute();
  492. statement.close();
  493. }
  494. catch (Exception e)
  495. {
  496. _log.log(Level.SEVERE, "Exception: Auction.cancelBid(String bidder): " + e.getMessage(), e);
  497. }
  498. finally
  499. {
  500. try
  501. {
  502. con.close();
  503. }
  504. catch (Exception e)
  505. {
  506. }
  507. }
  508. returnItem(_bidders.get(bidder).getClanName(), _bidders.get(bidder).getBid(), true);
  509. ClanTable.getInstance().getClanByName(_bidders.get(bidder).getClanName()).setAuctionBiddedAt(0, true);
  510. _bidders.clear();
  511. loadBid();
  512. }
  513. /** Cancel auction */
  514. public void cancelAuction()
  515. {
  516. deleteAuctionFromDB();
  517. removeBids();
  518. }
  519. /** Confirm an auction */
  520. public void confirmAuction()
  521. {
  522. AuctionManager.getInstance().getAuctions().add(this);
  523. Connection con = null;
  524. try
  525. {
  526. PreparedStatement statement;
  527. con = L2DatabaseFactory.getInstance().getConnection();
  528. statement = con.prepareStatement("INSERT INTO auction (id, sellerId, sellerName, sellerClanName, itemType, itemId, itemObjectId, itemName, itemQuantity, startingBid, currentBid, endDate) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
  529. statement.setInt(1, getId());
  530. statement.setInt(2, _sellerId);
  531. statement.setString(3, _sellerName);
  532. statement.setString(4, _sellerClanName);
  533. statement.setString(5, _itemType);
  534. statement.setInt(6, _itemId);
  535. statement.setInt(7, _itemObjectId);
  536. statement.setString(8, _itemName);
  537. statement.setLong(9, _itemQuantity);
  538. statement.setLong(10, _startingBid);
  539. statement.setLong(11, _currentBid);
  540. statement.setLong(12, _endDate);
  541. statement.execute();
  542. statement.close();
  543. loadBid();
  544. }
  545. catch (Exception e)
  546. {
  547. _log.log(Level.SEVERE, "Exception: Auction.load(): " + e.getMessage(), e);
  548. }
  549. finally
  550. {
  551. try
  552. {
  553. con.close();
  554. }
  555. catch (Exception e)
  556. {
  557. }
  558. }
  559. }
  560. /** Get var auction */
  561. public final int getId()
  562. {
  563. return _id;
  564. }
  565. public final long getCurrentBid()
  566. {
  567. return _currentBid;
  568. }
  569. public final long getEndDate()
  570. {
  571. return _endDate;
  572. }
  573. public final int getHighestBidderId()
  574. {
  575. return _highestBidderId;
  576. }
  577. public final String getHighestBidderName()
  578. {
  579. return _highestBidderName;
  580. }
  581. public final long getHighestBidderMaxBid()
  582. {
  583. return _highestBidderMaxBid;
  584. }
  585. public final int getItemId()
  586. {
  587. return _itemId;
  588. }
  589. public final String getItemName()
  590. {
  591. return _itemName;
  592. }
  593. public final int getItemObjectId()
  594. {
  595. return _itemObjectId;
  596. }
  597. public final long getItemQuantity()
  598. {
  599. return _itemQuantity;
  600. }
  601. public final String getItemType()
  602. {
  603. return _itemType;
  604. }
  605. public final int getSellerId()
  606. {
  607. return _sellerId;
  608. }
  609. public final String getSellerName()
  610. {
  611. return _sellerName;
  612. }
  613. public final String getSellerClanName()
  614. {
  615. return _sellerClanName;
  616. }
  617. public final long getStartingBid()
  618. {
  619. return _startingBid;
  620. }
  621. public final Map<Integer, Bidder> getBidders()
  622. {
  623. return _bidders;
  624. }
  625. }