Auction.java 17 KB

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