ItemAuctionManager.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.sql.Statement;
  23. import java.util.concurrent.atomic.AtomicInteger;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.NamedNodeMap;
  29. import org.w3c.dom.Node;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.L2DatabaseFactory;
  32. import com.l2jserver.gameserver.model.itemauction.ItemAuctionInstance;
  33. /**
  34. * @author Forsaiken
  35. */
  36. public final class ItemAuctionManager
  37. {
  38. private static final Logger _log = Logger.getLogger(ItemAuctionManager.class.getName());
  39. public static final ItemAuctionManager getInstance()
  40. {
  41. return SingletonHolder._instance;
  42. }
  43. private final TIntObjectHashMap<ItemAuctionInstance> _managerInstances;
  44. private final AtomicInteger _auctionIds;
  45. protected ItemAuctionManager()
  46. {
  47. _managerInstances = new TIntObjectHashMap<>();
  48. _auctionIds = new AtomicInteger(1);
  49. if (!Config.ALT_ITEM_AUCTION_ENABLED)
  50. {
  51. _log.log(Level.INFO, "ItemAuctionManager: Disabled by config.");
  52. return;
  53. }
  54. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  55. Statement statement = con.createStatement();
  56. ResultSet rset = statement.executeQuery("SELECT auctionId FROM item_auction ORDER BY auctionId DESC LIMIT 0, 1"))
  57. {
  58. if (rset.next())
  59. _auctionIds.set(rset.getInt(1) + 1);
  60. }
  61. catch (final SQLException e)
  62. {
  63. _log.log(Level.SEVERE, "ItemAuctionManager: Failed loading auctions.", e);
  64. }
  65. final File file = new File(Config.DATAPACK_ROOT + "/data/ItemAuctions.xml");
  66. if (!file.exists())
  67. {
  68. _log.log(Level.WARNING, "ItemAuctionManager: Missing ItemAuctions.xml!");
  69. return;
  70. }
  71. final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  72. factory.setValidating(false);
  73. factory.setIgnoringComments(true);
  74. try
  75. {
  76. final Document doc = factory.newDocumentBuilder().parse(file);
  77. for (Node na = doc.getFirstChild(); na != null; na = na.getNextSibling())
  78. {
  79. if ("list".equalsIgnoreCase(na.getNodeName()))
  80. {
  81. for (Node nb = na.getFirstChild(); nb != null; nb = nb.getNextSibling())
  82. {
  83. if ("instance".equalsIgnoreCase(nb.getNodeName()))
  84. {
  85. final NamedNodeMap nab = nb.getAttributes();
  86. final int instanceId = Integer.parseInt(nab.getNamedItem("id").getNodeValue());
  87. if (_managerInstances.containsKey(instanceId))
  88. throw new Exception("Dublicated instanceId " + instanceId);
  89. final ItemAuctionInstance instance = new ItemAuctionInstance(instanceId, _auctionIds, nb);
  90. _managerInstances.put(instanceId, instance);
  91. }
  92. }
  93. }
  94. }
  95. _log.log(Level.INFO, "ItemAuctionManager: Loaded " + _managerInstances.size() + " instance(s).");
  96. }
  97. catch (Exception e)
  98. {
  99. _log.log(Level.SEVERE, "ItemAuctionManager: 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. private static class SingletonHolder
  139. {
  140. protected static final ItemAuctionManager _instance = new ItemAuctionManager();
  141. }
  142. }