Advertisement
Guest User

AuctionTable v2

a guest
Jan 26th, 2017
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  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 net.sf.l2j.gameserver.l2spike.datatables;
  16.  
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.Map;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import java.util.logging.Logger;
  23.  
  24. import net.sf.l2j.L2DatabaseFactory;
  25. import net.sf.l2j.gameserver.l2spike.auction.AuctionItem;
  26.  
  27. /**
  28. * @author Anarchy
  29. *
  30. */
  31. public class AuctionTable
  32. {
  33. private static Logger log = Logger.getLogger(AuctionTable.class.getName());
  34.  
  35. private Map<Integer, AuctionItem> items;
  36. private int maxId;
  37.  
  38. public static AuctionTable getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42.  
  43. protected AuctionTable()
  44. {
  45. items = new ConcurrentHashMap<>();
  46. maxId = 0;
  47.  
  48. load();
  49. }
  50.  
  51. private void load()
  52. {
  53. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  54. {
  55. PreparedStatement stm = con.prepareStatement("SELECT * FROM auction_table");
  56. ResultSet rset = stm.executeQuery();
  57.  
  58. while (rset.next())
  59. {
  60. int auctionId = rset.getInt("auctionid");
  61. int ownerId = rset.getInt("ownerid");
  62. int itemId = rset.getInt("itemid");
  63. int count = rset.getInt("count");
  64. int enchant = rset.getInt("enchant");
  65. int costId = rset.getInt("costid");
  66. int costCount = rset.getInt("costcount");
  67.  
  68. items.put(auctionId, new AuctionItem(auctionId, ownerId, itemId, count, enchant, costId, costCount));
  69.  
  70. if (auctionId > maxId)
  71. maxId = auctionId;
  72. }
  73.  
  74. rset.close();
  75. stm.close();
  76. }
  77. catch (Exception e)
  78. {
  79. e.printStackTrace();
  80. }
  81.  
  82. log.info("AuctionTable: Loaded "+items.size()+" items.");
  83. }
  84.  
  85. public void addItem(AuctionItem item)
  86. {
  87. items.put(item.getAuctionId(), item);
  88.  
  89. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  90. {
  91. PreparedStatement stm = con.prepareStatement("INSERT INTO auction_table VALUES (?,?,?,?,?,?,?)");
  92. stm.setInt(1, item.getAuctionId());
  93. stm.setInt(2, item.getOwnerId());
  94. stm.setInt(3, item.getItemId());
  95. stm.setInt(4, item.getCount());
  96. stm.setInt(5, item.getEnchant());
  97. stm.setInt(6, item.getCostId());
  98. stm.setInt(7, item.getCostCount());
  99.  
  100. stm.execute();
  101. stm.close();
  102. }
  103. catch (Exception e)
  104. {
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. public void deleteItem(AuctionItem item)
  110. {
  111. items.remove(item.getAuctionId());
  112.  
  113. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  114. {
  115. PreparedStatement stm = con.prepareStatement("DELETE FROM auction_table WHERE auctionid=?");
  116. stm.setInt(1, item.getAuctionId());
  117.  
  118. stm.execute();
  119. stm.close();
  120. }
  121. catch (Exception e)
  122. {
  123. e.printStackTrace();
  124. }
  125. }
  126.  
  127. public AuctionItem getItem(int auctionId)
  128. {
  129. return items.get(auctionId);
  130. }
  131.  
  132. public Map<Integer, AuctionItem> getItems()
  133. {
  134. return items;
  135. }
  136.  
  137. public int getNextAuctionId()
  138. {
  139. maxId++;
  140. return maxId;
  141. }
  142.  
  143. private static class SingletonHolder
  144. {
  145. protected static final AuctionTable _instance = new AuctionTable();
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement