Auction.java 17 KB

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