Auction.java 17 KB

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