Auction.java 17 KB

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