ItemAuctionManager.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.instancemanager;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.util.concurrent.atomic.AtomicInteger;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javax.xml.parsers.DocumentBuilderFactory;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.L2DatabaseFactory;
  31. import com.l2jserver.gameserver.model.itemauction.ItemAuctionInstance;
  32. /**
  33. * @author Forsaiken
  34. */
  35. public final class ItemAuctionManager
  36. {
  37. private static final Logger _log = Logger.getLogger(ItemAuctionManager.class.getName());
  38. public static final ItemAuctionManager getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42. private final TIntObjectHashMap<ItemAuctionInstance> _managerInstances;
  43. private final AtomicInteger _auctionIds;
  44. private ItemAuctionManager()
  45. {
  46. _managerInstances = new TIntObjectHashMap<ItemAuctionInstance>();
  47. _auctionIds = new AtomicInteger(1);
  48. if (!Config.ALT_ITEM_AUCTION_ENABLED)
  49. {
  50. _log.log(Level.INFO, "ItemAuctionManager: Disabled by config.");
  51. return;
  52. }
  53. Connection con = null;
  54. try
  55. {
  56. con = L2DatabaseFactory.getInstance().getConnection();
  57. PreparedStatement statement = con.prepareStatement("SELECT auctionId FROM item_auction ORDER BY auctionId DESC LIMIT 0, 1");
  58. ResultSet rset = statement.executeQuery();
  59. if (rset.next())
  60. _auctionIds.set(rset.getInt(1) + 1);
  61. rset.close();
  62. statement.close();
  63. }
  64. catch (final SQLException e)
  65. {
  66. _log.log(Level.SEVERE, "ItemAuctionManager: Failed loading auctions.", e);
  67. }
  68. finally
  69. {
  70. L2DatabaseFactory.close(con);
  71. }
  72. final File file = new File(Config.DATAPACK_ROOT + "/data/ItemAuctions.xml");
  73. if (!file.exists())
  74. {
  75. _log.log(Level.WARNING, "ItemAuctionManager: Missing ItemAuctions.xml!");
  76. return;
  77. }
  78. final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  79. factory.setValidating(false);
  80. factory.setIgnoringComments(true);
  81. try
  82. {
  83. final Document doc = factory.newDocumentBuilder().parse(file);
  84. for (Node na = doc.getFirstChild(); na != null; na = na.getNextSibling())
  85. {
  86. if ("list".equalsIgnoreCase(na.getNodeName()))
  87. {
  88. for (Node nb = na.getFirstChild(); nb != null; nb = nb.getNextSibling())
  89. {
  90. if ("instance".equalsIgnoreCase(nb.getNodeName()))
  91. {
  92. final NamedNodeMap nab = nb.getAttributes();
  93. final int instanceId = Integer.parseInt(nab.getNamedItem("id").getNodeValue());
  94. if (_managerInstances.containsKey(instanceId))
  95. throw new Exception("Dublicated instanceId " + instanceId);
  96. final ItemAuctionInstance instance = new ItemAuctionInstance(instanceId, _auctionIds, nb);
  97. _managerInstances.put(instanceId, instance);
  98. }
  99. }
  100. }
  101. }
  102. _log.log(Level.INFO, "ItemAuctionManager: Loaded " + _managerInstances.size() + " instance(s).");
  103. }
  104. catch (Exception e)
  105. {
  106. _log.log(Level.SEVERE, "ItemAuctionManager: Failed loading auctions from xml.", e);
  107. }
  108. }
  109. public final void shutdown()
  110. {
  111. final ItemAuctionInstance[] instances = _managerInstances.values(new ItemAuctionInstance[0]);
  112. for (final ItemAuctionInstance instance : instances)
  113. {
  114. instance.shutdown();
  115. }
  116. }
  117. public final ItemAuctionInstance getManagerInstance(final int instanceId)
  118. {
  119. return _managerInstances.get(instanceId);
  120. }
  121. public final int getNextAuctionId()
  122. {
  123. return _auctionIds.getAndIncrement();
  124. }
  125. public final static void deleteAuction(final int auctionId)
  126. {
  127. Connection con = null;
  128. try
  129. {
  130. con = L2DatabaseFactory.getInstance().getConnection();
  131. PreparedStatement statement = con.prepareStatement("DELETE FROM item_auction WHERE auctionId=?");
  132. statement.setInt(1, auctionId);
  133. statement.execute();
  134. statement.close();
  135. statement = con.prepareStatement("DELETE FROM item_auction_bid WHERE auctionId=?");
  136. statement.setInt(1, auctionId);
  137. statement.execute();
  138. statement.close();
  139. }
  140. catch (final SQLException e)
  141. {
  142. _log.log(Level.SEVERE, "L2ItemAuctionManagerInstance: Failed deleting auction: " + auctionId, e);
  143. }
  144. finally
  145. {
  146. L2DatabaseFactory.close(con);
  147. }
  148. }
  149. @SuppressWarnings("synthetic-access")
  150. private static class SingletonHolder
  151. {
  152. protected static final ItemAuctionManager _instance = new ItemAuctionManager();
  153. }
  154. }