Auction.java 17 KB

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