ItemAuctionManager.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.io.File;
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import javax.xml.parsers.DocumentBuilderFactory;
  32. import org.w3c.dom.Document;
  33. import org.w3c.dom.NamedNodeMap;
  34. import org.w3c.dom.Node;
  35. import com.l2jserver.Config;
  36. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  37. import com.l2jserver.gameserver.model.itemauction.ItemAuctionInstance;
  38. /**
  39. * @author Forsaiken
  40. */
  41. public final class ItemAuctionManager
  42. {
  43. private static final Logger _log = Logger.getLogger(ItemAuctionManager.class.getName());
  44. private final Map<Integer, ItemAuctionInstance> _managerInstances = new HashMap<>();
  45. private final AtomicInteger _auctionIds;
  46. protected ItemAuctionManager()
  47. {
  48. _auctionIds = new AtomicInteger(1);
  49. if (!Config.ALT_ITEM_AUCTION_ENABLED)
  50. {
  51. _log.log(Level.INFO, getClass().getSimpleName() + ": Disabled by config.");
  52. return;
  53. }
  54. try (Connection con = ConnectionFactory.getInstance().getConnection();
  55. Statement s = con.createStatement();
  56. ResultSet rs = s.executeQuery("SELECT auctionId FROM item_auction ORDER BY auctionId DESC LIMIT 0, 1"))
  57. {
  58. if (rs.next())
  59. {
  60. _auctionIds.set(rs.getInt(1) + 1);
  61. }
  62. }
  63. catch (final SQLException e)
  64. {
  65. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Failed loading auctions.", e);
  66. }
  67. final File file = new File(Config.DATAPACK_ROOT + "/data/ItemAuctions.xml");
  68. if (!file.exists())
  69. {
  70. _log.log(Level.WARNING, getClass().getSimpleName() + ": Missing ItemAuctions.xml!");
  71. return;
  72. }
  73. final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  74. factory.setValidating(false);
  75. factory.setIgnoringComments(true);
  76. try
  77. {
  78. final Document doc = factory.newDocumentBuilder().parse(file);
  79. for (Node na = doc.getFirstChild(); na != null; na = na.getNextSibling())
  80. {
  81. if ("list".equalsIgnoreCase(na.getNodeName()))
  82. {
  83. for (Node nb = na.getFirstChild(); nb != null; nb = nb.getNextSibling())
  84. {
  85. if ("instance".equalsIgnoreCase(nb.getNodeName()))
  86. {
  87. final NamedNodeMap nab = nb.getAttributes();
  88. final int instanceId = Integer.parseInt(nab.getNamedItem("id").getNodeValue());
  89. if (_managerInstances.containsKey(instanceId))
  90. {
  91. throw new Exception("Dublicated instanceId " + instanceId);
  92. }
  93. final ItemAuctionInstance instance = new ItemAuctionInstance(instanceId, _auctionIds, nb);
  94. _managerInstances.put(instanceId, instance);
  95. }
  96. }
  97. }
  98. }
  99. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded " + _managerInstances.size() + " instance(s).");
  100. }
  101. catch (Exception e)
  102. {
  103. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Failed loading auctions from xml.", e);
  104. }
  105. }
  106. public final void shutdown()
  107. {
  108. for (ItemAuctionInstance instance : _managerInstances.values())
  109. {
  110. instance.shutdown();
  111. }
  112. }
  113. public final ItemAuctionInstance getManagerInstance(final int instanceId)
  114. {
  115. return _managerInstances.get(instanceId);
  116. }
  117. public final int getNextAuctionId()
  118. {
  119. return _auctionIds.getAndIncrement();
  120. }
  121. public static final void deleteAuction(final int auctionId)
  122. {
  123. try (Connection con = ConnectionFactory.getInstance().getConnection())
  124. {
  125. try (PreparedStatement ps = con.prepareStatement("DELETE FROM item_auction WHERE auctionId=?"))
  126. {
  127. ps.setInt(1, auctionId);
  128. ps.execute();
  129. }
  130. try (PreparedStatement ps = con.prepareStatement("DELETE FROM item_auction_bid WHERE auctionId=?"))
  131. {
  132. ps.setInt(1, auctionId);
  133. ps.execute();
  134. }
  135. }
  136. catch (SQLException e)
  137. {
  138. _log.log(Level.SEVERE, "L2ItemAuctionManagerInstance: Failed deleting auction: " + auctionId, e);
  139. }
  140. }
  141. /**
  142. * Gets the single instance of {@code ItemAuctionManager}.
  143. * @return single instance of {@code ItemAuctionManager}
  144. */
  145. public static final ItemAuctionManager getInstance()
  146. {
  147. return SingletonHolder._instance;
  148. }
  149. private static class SingletonHolder
  150. {
  151. protected static final ItemAuctionManager _instance = new ItemAuctionManager();
  152. }
  153. }