ItemAuctionManager.java 5.1 KB

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