Auction.java 17 KB

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