Auction.java 17 KB

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