ItemAuctionManager.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2004-2013 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.concurrent.atomic.AtomicInteger;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javax.xml.parsers.DocumentBuilderFactory;
  30. import org.w3c.dom.Document;
  31. import org.w3c.dom.NamedNodeMap;
  32. import org.w3c.dom.Node;
  33. import com.l2jserver.Config;
  34. import com.l2jserver.L2DatabaseFactory;
  35. import com.l2jserver.gameserver.model.itemauction.ItemAuctionInstance;
  36. import gnu.trove.map.hash.TIntObjectHashMap;
  37. /**
  38. * @author Forsaiken
  39. */
  40. public final class ItemAuctionManager
  41. {
  42. private static final Logger _log = Logger.getLogger(ItemAuctionManager.class.getName());
  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, getClass().getSimpleName() + ": 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. {
  60. _auctionIds.set(rset.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. final ItemAuctionInstance[] instances = _managerInstances.values(new ItemAuctionInstance[0]);
  109. for (final ItemAuctionInstance instance : instances)
  110. {
  111. instance.shutdown();
  112. }
  113. }
  114. public final ItemAuctionInstance getManagerInstance(final int instanceId)
  115. {
  116. return _managerInstances.get(instanceId);
  117. }
  118. public final int getNextAuctionId()
  119. {
  120. return _auctionIds.getAndIncrement();
  121. }
  122. public static final void deleteAuction(final int auctionId)
  123. {
  124. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  125. {
  126. try (PreparedStatement statement = con.prepareStatement("DELETE FROM item_auction WHERE auctionId=?"))
  127. {
  128. statement.setInt(1, auctionId);
  129. statement.execute();
  130. }
  131. try (PreparedStatement statement = con.prepareStatement("DELETE FROM item_auction_bid WHERE auctionId=?"))
  132. {
  133. statement.setInt(1, auctionId);
  134. statement.execute();
  135. }
  136. }
  137. catch (SQLException e)
  138. {
  139. _log.log(Level.SEVERE, "L2ItemAuctionManagerInstance: Failed deleting auction: " + auctionId, e);
  140. }
  141. }
  142. public static final ItemAuctionManager getInstance()
  143. {
  144. return SingletonHolder._instance;
  145. }
  146. private static class SingletonHolder
  147. {
  148. protected static final ItemAuctionManager _instance = new ItemAuctionManager();
  149. }
  150. }