Auction.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. public void run()
  105. {
  106. try
  107. {
  108. endAuction();
  109. } catch (Throwable t) { }
  110. }
  111. }
  112. /** Constructor */
  113. public Auction(int auctionId)
  114. {
  115. _id = auctionId;
  116. load();
  117. startAutoTask();
  118. }
  119. public Auction(int itemId, L2Clan Clan, long delay, int bid, String name)
  120. {
  121. _id = itemId;
  122. _endDate = System.currentTimeMillis() + delay;
  123. _itemId = itemId;
  124. _itemName = name;
  125. _itemType = "ClanHall";
  126. _sellerId = Clan.getLeaderId();
  127. _sellerName = Clan.getLeaderName();
  128. _sellerClanName = Clan.getName();
  129. _startingBid = bid;
  130. }
  131. /** Load auctions */
  132. private void load()
  133. {
  134. java.sql.Connection con = null;
  135. try
  136. {
  137. PreparedStatement statement;
  138. ResultSet rs;
  139. con = L2DatabaseFactory.getInstance().getConnection();
  140. statement = con.prepareStatement("Select * from auction where id = ?");
  141. statement.setInt(1, getId());
  142. rs = statement.executeQuery();
  143. while (rs.next())
  144. {
  145. _currentBid = rs.getInt("currentBid");
  146. _endDate= rs.getLong("endDate");
  147. _itemId = rs.getInt("itemId");
  148. _itemName = rs.getString("itemName");
  149. _itemObjectId = rs.getInt("itemObjectId");
  150. _itemType = rs.getString("itemType");
  151. _sellerId = rs.getInt("sellerId");
  152. _sellerClanName = rs.getString("sellerClanName");
  153. _sellerName = rs.getString("sellerName");
  154. _startingBid = rs.getInt("startingBid");
  155. }
  156. statement.close();
  157. loadBid();
  158. }
  159. catch (Exception e)
  160. {
  161. _log.warning("Exception: Auction.load(): " + e.getMessage());
  162. e.printStackTrace();
  163. }
  164. finally {try { con.close(); } catch (Exception e) {}}
  165. }
  166. /** Load bidders **/
  167. private void loadBid()
  168. {
  169. java.sql.Connection con = null;
  170. try
  171. {
  172. PreparedStatement statement;
  173. ResultSet rs;
  174. con = L2DatabaseFactory.getInstance().getConnection();
  175. statement = con.prepareStatement("SELECT bidderId, bidderName, maxBid, clan_name, time_bid FROM auction_bid WHERE auctionId = ? ORDER BY maxBid DESC");
  176. statement.setInt(1, getId());
  177. rs = statement.executeQuery();
  178. while (rs.next())
  179. {
  180. if (rs.isFirst())
  181. {
  182. _highestBidderId = rs.getInt("bidderId");
  183. _highestBidderName = rs.getString("bidderName");
  184. _highestBidderMaxBid = rs.getInt("maxBid");
  185. }
  186. _bidders.put(rs.getInt("bidderId"), new Bidder(rs.getString("bidderName"), rs.getString("clan_name"), rs.getInt("maxBid"), rs.getLong("time_bid")));
  187. }
  188. statement.close();
  189. }
  190. catch (Exception e)
  191. {
  192. _log.warning("Exception: Auction.loadBid(): " + e.getMessage());
  193. e.printStackTrace();
  194. }
  195. finally {try { con.close(); } catch (Exception e) {}}
  196. }
  197. /** Task Manage */
  198. private void startAutoTask()
  199. {
  200. long currentTime = System.currentTimeMillis();
  201. long taskDelay = 0;
  202. if (_endDate <= currentTime){
  203. _endDate = currentTime + 7*24*60*60*1000;
  204. saveAuctionDate();
  205. }else
  206. taskDelay = _endDate - currentTime;
  207. ThreadPoolManager.getInstance().scheduleGeneral(new AutoEndTask(), taskDelay);
  208. }
  209. public static String getItemTypeName(ItemTypeEnum value)
  210. {
  211. return ItemTypeName[value.ordinal()];
  212. }
  213. /** Save Auction Data End */
  214. private void saveAuctionDate()
  215. {
  216. java.sql.Connection con = null;
  217. try
  218. {
  219. con = L2DatabaseFactory.getInstance().getConnection();
  220. PreparedStatement statement = con.prepareStatement("Update auction set endDate = ? where id = ?");
  221. statement.setLong(1, _endDate);
  222. statement.setInt(2, _id);
  223. statement.execute();
  224. statement.close();
  225. }
  226. catch (Exception e)
  227. {
  228. _log.log(Level.SEVERE, "Exception: saveAuctionDate(): " + e.getMessage(),e);
  229. }
  230. finally {try { con.close(); } catch (Exception e) {}}
  231. }
  232. /** Set a bid */
  233. public void setBid(L2PcInstance bidder, int bid)
  234. {
  235. int requiredAdena = bid;
  236. if (getHighestBidderName().equals(bidder.getClan().getLeaderName()))
  237. requiredAdena = bid - getHighestBidderMaxBid();
  238. if ((getHighestBidderId() >0 && bid > getHighestBidderMaxBid())
  239. || (getHighestBidderId() == 0 && bid >= getStartingBid()))
  240. {
  241. if(takeItem(bidder, 57, requiredAdena))
  242. {
  243. updateInDB(bidder, bid);
  244. bidder.getClan().setAuctionBiddedAt(_id, true);
  245. return;
  246. }
  247. }
  248. bidder.sendMessage("Invalid bid!");
  249. }
  250. /** Return Item in WHC */
  251. private void returnItem(String Clan, int itemId, int quantity, boolean penalty)
  252. {
  253. if (penalty)
  254. quantity *= 0.9; //take 10% tax fee if needed
  255. ClanTable.getInstance().getClanByName(Clan).getWarehouse().addItem("Outbidded", _adenaId, quantity, null, null);
  256. }
  257. /** Take Item in WHC */
  258. private boolean takeItem(L2PcInstance bidder, int itemId, int quantity)
  259. {
  260. if (bidder.getClan() != null && bidder.getClan().getWarehouse().getAdena() >= quantity)
  261. {
  262. bidder.getClan().getWarehouse().destroyItemByItemId("Buy", _adenaId, quantity, bidder, bidder);
  263. return true;
  264. }
  265. bidder.sendMessage("You do not have enough adena");
  266. return false;
  267. }
  268. /** Update auction in DB */
  269. private void updateInDB(L2PcInstance bidder, int bid)
  270. {
  271. java.sql.Connection con = null;
  272. try
  273. {
  274. con = L2DatabaseFactory.getInstance().getConnection();
  275. PreparedStatement statement;
  276. if (getBidders().get(bidder.getClanId()) != null)
  277. {
  278. statement = con.prepareStatement("UPDATE auction_bid SET bidderId=?, bidderName=?, maxBid=?, time_bid=? WHERE auctionId=? AND bidderId=?");
  279. statement.setInt(1, bidder.getClanId());
  280. statement.setString(2, bidder.getClan().getLeaderName());
  281. statement.setInt(3, bid);
  282. statement.setLong(4, System.currentTimeMillis());
  283. statement.setInt(5, getId());
  284. statement.setInt(6, bidder.getClanId());
  285. statement.execute();
  286. statement.close();
  287. }
  288. else
  289. {
  290. statement = con.prepareStatement("INSERT INTO auction_bid (id, auctionId, bidderId, bidderName, maxBid, clan_name, time_bid) VALUES (?, ?, ?, ?, ?, ?, ?)");
  291. statement.setInt(1, IdFactory.getInstance().getNextId());
  292. statement.setInt(2, getId());
  293. statement.setInt(3, bidder.getClanId());
  294. statement.setString(4, bidder.getName());
  295. statement.setInt(5, bid);
  296. statement.setString(6, bidder.getClan().getName());
  297. statement.setLong(7, System.currentTimeMillis());
  298. statement.execute();
  299. statement.close();
  300. if (L2World.getInstance().getPlayer(_highestBidderName) != null)
  301. L2World.getInstance().getPlayer(_highestBidderName).sendMessage("You have been out bidded");
  302. }
  303. _highestBidderId = bidder.getClanId();
  304. _highestBidderMaxBid = bid;
  305. _highestBidderName = bidder.getClan().getLeaderName();
  306. if (_bidders.get(_highestBidderId) == null)
  307. _bidders.put(_highestBidderId, new Bidder(_highestBidderName, bidder.getClan().getName(), bid, Calendar.getInstance().getTimeInMillis()));
  308. else
  309. {
  310. _bidders.get(_highestBidderId).setBid(bid);
  311. _bidders.get(_highestBidderId).setTimeBid(Calendar.getInstance().getTimeInMillis());
  312. }
  313. bidder.sendMessage("You have bidded successfully");
  314. }
  315. catch (Exception e)
  316. {
  317. _log.log(Level.SEVERE, "Exception: Auction.updateInDB(L2PcInstance bidder, int bid): " + e.getMessage());
  318. e.printStackTrace();
  319. }
  320. finally
  321. {
  322. try { con.close(); } catch (Exception e) {}
  323. }
  324. }
  325. /** Remove bids */
  326. private void removeBids()
  327. {
  328. java.sql.Connection con = null;
  329. try
  330. {
  331. con = L2DatabaseFactory.getInstance().getConnection();
  332. PreparedStatement statement;
  333. statement = con.prepareStatement("DELETE FROM auction_bid WHERE auctionId=?");
  334. statement.setInt(1, getId());
  335. statement.execute();
  336. statement.close();
  337. }
  338. catch (Exception e)
  339. {
  340. _log.log(Level.SEVERE, "Exception: Auction.deleteFromDB(): " + e.getMessage(),e);
  341. }
  342. finally
  343. {
  344. try { con.close(); } catch (Exception e) {}
  345. }
  346. for (Bidder b : _bidders.values())
  347. {
  348. if (ClanTable.getInstance().getClanByName(b.getClanName()).getHasHideout() == 0)
  349. returnItem(b.getClanName(), 57, 9*b.getBid()/10, false); // 10 % tax
  350. else
  351. {
  352. if (L2World.getInstance().getPlayer(b.getName()) != null)
  353. L2World.getInstance().getPlayer(b.getName()).sendMessage("Congratulation you have won ClanHall!");
  354. }
  355. ClanTable.getInstance().getClanByName(b.getClanName()).setAuctionBiddedAt(0, true);
  356. }
  357. _bidders.clear();
  358. }
  359. /** Remove auctions */
  360. public void deleteAuctionFromDB()
  361. {
  362. AuctionManager.getInstance().getAuctions().remove(this);
  363. java.sql.Connection con = null;
  364. try
  365. {
  366. con = L2DatabaseFactory.getInstance().getConnection();
  367. PreparedStatement statement;
  368. statement = con.prepareStatement("DELETE FROM auction WHERE itemId=?");
  369. statement.setInt(1, _itemId);
  370. statement.execute();
  371. statement.close();
  372. }
  373. catch (Exception e)
  374. {
  375. _log.log(Level.SEVERE, "Exception: Auction.deleteFromDB(): " + e.getMessage(),e);
  376. }
  377. finally
  378. {
  379. try { con.close(); } catch (Exception e) {}
  380. }
  381. }
  382. /** End of auction */
  383. public void endAuction()
  384. {
  385. if(GameServer.gameServer.getCHManager() != null && GameServer.gameServer.getCHManager().loaded()){
  386. if (_highestBidderId == 0 && _sellerId == 0)
  387. {
  388. startAutoTask();
  389. return;
  390. }
  391. if (_highestBidderId == 0 && _sellerId > 0)
  392. {
  393. /** If seller haven't sell ClanHall, auction removed,
  394. * THIS MUST BE CONFIRMED */
  395. int aucId = AuctionManager.getInstance().getAuctionIndex(_id);
  396. AuctionManager.getInstance().getAuctions().remove(aucId);
  397. return;
  398. }
  399. if (_sellerId > 0)
  400. {
  401. returnItem(_sellerClanName, 57, _highestBidderMaxBid, true);
  402. returnItem(_sellerClanName, 57, ClanHallManager.getInstance().getClanHallById(_itemId).getLease(), false);
  403. }
  404. deleteAuctionFromDB();
  405. L2Clan Clan = ClanTable.getInstance().getClanByName(_bidders.get(_highestBidderId).getClanName());
  406. _bidders.remove(_highestBidderId);
  407. Clan.setAuctionBiddedAt(0, true);
  408. removeBids();
  409. ClanHallManager.getInstance().setOwner(_itemId, Clan);
  410. }else{
  411. /** Task waiting ClanHallManager is loaded every 3s */
  412. ThreadPoolManager.getInstance().scheduleGeneral(new AutoEndTask(), 3000);
  413. }
  414. }
  415. /** Cancel bid */
  416. public void cancelBid(int bidder)
  417. {
  418. java.sql.Connection con = null;
  419. try
  420. {
  421. con = L2DatabaseFactory.getInstance().getConnection();
  422. PreparedStatement statement;
  423. statement = con.prepareStatement("DELETE FROM auction_bid WHERE auctionId=? AND bidderId=?");
  424. statement.setInt(1, getId());
  425. statement.setInt(2, bidder);
  426. statement.execute();
  427. statement.close();
  428. }
  429. catch (Exception e)
  430. {
  431. _log.log(Level.SEVERE, "Exception: Auction.cancelBid(String bidder): " + e.getMessage(),e);
  432. }
  433. finally
  434. {
  435. try { con.close(); } catch (Exception e) {}
  436. }
  437. returnItem(_bidders.get(bidder).getClanName(), 57, _bidders.get(bidder).getBid(), true);
  438. ClanTable.getInstance().getClanByName(_bidders.get(bidder).getClanName()).setAuctionBiddedAt(0, true);
  439. _bidders.clear();
  440. loadBid();
  441. }
  442. /** Cancel auction */
  443. public void cancelAuction()
  444. {
  445. deleteAuctionFromDB();
  446. removeBids();
  447. }
  448. /** Confirm an auction */
  449. public void confirmAuction()
  450. {
  451. AuctionManager.getInstance().getAuctions().add(this);
  452. java.sql.Connection con = null;
  453. try
  454. {
  455. PreparedStatement statement;
  456. con = L2DatabaseFactory.getInstance().getConnection();
  457. statement = con.prepareStatement("INSERT INTO auction (id, sellerId, sellerName, sellerClanName, itemType, itemId, itemObjectId, itemName, itemQuantity, startingBid, currentBid, endDate) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
  458. statement.setInt(1, getId());
  459. statement.setInt(2, _sellerId);
  460. statement.setString(3, _sellerName);
  461. statement.setString(4, _sellerClanName);
  462. statement.setString(5, _itemType);
  463. statement.setInt(6, _itemId);
  464. statement.setInt(7, _itemObjectId);
  465. statement.setString(8, _itemName);
  466. statement.setInt(9, _itemQuantity);
  467. statement.setInt(10, _startingBid);
  468. statement.setInt(11, _currentBid);
  469. statement.setLong(12, _endDate);
  470. statement.execute();
  471. statement.close();
  472. loadBid();
  473. }
  474. catch (Exception e)
  475. {
  476. _log.log(Level.SEVERE, "Exception: Auction.load(): " + e.getMessage(),e);
  477. }
  478. finally {try { con.close(); } catch (Exception e) {}}
  479. }
  480. /** Get var auction */
  481. public final int getId() { return _id; }
  482. public final int getCurrentBid() { return _currentBid; }
  483. public final long getEndDate() { return _endDate; }
  484. public final int getHighestBidderId() { return _highestBidderId; }
  485. public final String getHighestBidderName() { return _highestBidderName; }
  486. public final int getHighestBidderMaxBid() { return _highestBidderMaxBid; }
  487. public final int getItemId() { return _itemId; }
  488. public final String getItemName() { return _itemName; }
  489. public final int getItemObjectId() { return _itemObjectId; }
  490. public final int getItemQuantity() { return _itemQuantity; }
  491. public final String getItemType() { return _itemType; }
  492. public final int getSellerId() { return _sellerId; }
  493. public final String getSellerName() { return _sellerName; }
  494. public final String getSellerClanName() { return _sellerClanName; }
  495. public final int getStartingBid() { return _startingBid; }
  496. public final Map<Integer, Bidder> getBidders(){ return _bidders; }
  497. }