RaidBossPointsManager.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.ArrayList;
  26. import java.util.Comparator;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Map.Entry;
  31. import java.util.concurrent.ConcurrentHashMap;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;
  34. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. /**
  37. * @author Kerberos, JIV
  38. * @version 8/24/10
  39. */
  40. public class RaidBossPointsManager
  41. {
  42. private static final Logger _log = Logger.getLogger(RaidBossPointsManager.class.getName());
  43. private final Map<Integer, Map<Integer, Integer>> _list = new ConcurrentHashMap<>();
  44. public RaidBossPointsManager()
  45. {
  46. init();
  47. }
  48. private final void init()
  49. {
  50. try (Connection con = ConnectionFactory.getInstance().getConnection();
  51. Statement s = con.createStatement();
  52. ResultSet rs = s.executeQuery("SELECT `charId`,`boss_id`,`points` FROM `character_raid_points`"))
  53. {
  54. while (rs.next())
  55. {
  56. int charId = rs.getInt("charId");
  57. int bossId = rs.getInt("boss_id");
  58. int points = rs.getInt("points");
  59. Map<Integer, Integer> values = _list.get(charId);
  60. if (values == null)
  61. {
  62. values = new HashMap<>();
  63. }
  64. values.put(bossId, points);
  65. _list.put(charId, values);
  66. }
  67. _log.info(getClass().getSimpleName() + ": Loaded " + _list.size() + " Characters Raid Points.");
  68. }
  69. catch (SQLException e)
  70. {
  71. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldnt load raid points ", e);
  72. }
  73. }
  74. public final void updatePointsInDB(L2PcInstance player, int raidId, int points)
  75. {
  76. try (Connection con = ConnectionFactory.getInstance().getConnection();
  77. PreparedStatement ps = con.prepareStatement("REPLACE INTO character_raid_points (`charId`,`boss_id`,`points`) VALUES (?,?,?)"))
  78. {
  79. ps.setInt(1, player.getObjectId());
  80. ps.setInt(2, raidId);
  81. ps.setInt(3, points);
  82. ps.executeUpdate();
  83. }
  84. catch (Exception e)
  85. {
  86. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update char raid points for player: " + player, e);
  87. }
  88. }
  89. public final void addPoints(L2PcInstance player, int bossId, int points)
  90. {
  91. final Map<Integer, Integer> tmpPoint = _list.computeIfAbsent(player.getObjectId(), k -> new HashMap<>());
  92. updatePointsInDB(player, bossId, tmpPoint.merge(bossId, points, Integer::sum));
  93. }
  94. public final int getPointsByOwnerId(int ownerId)
  95. {
  96. Map<Integer, Integer> tmpPoint = _list.get(ownerId);
  97. int totalPoints = 0;
  98. if ((tmpPoint == null) || tmpPoint.isEmpty())
  99. {
  100. return 0;
  101. }
  102. for (int points : tmpPoint.values())
  103. {
  104. totalPoints += points;
  105. }
  106. return totalPoints;
  107. }
  108. public final Map<Integer, Integer> getList(L2PcInstance player)
  109. {
  110. return _list.get(player.getObjectId());
  111. }
  112. public final void cleanUp()
  113. {
  114. try (Connection con = ConnectionFactory.getInstance().getConnection();
  115. Statement s = con.createStatement())
  116. {
  117. s.executeUpdate("DELETE from character_raid_points WHERE charId > 0");
  118. _list.clear();
  119. }
  120. catch (Exception e)
  121. {
  122. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't clean raid points", e);
  123. }
  124. }
  125. public final int calculateRanking(int playerObjId)
  126. {
  127. Map<Integer, Integer> rank = getRankList();
  128. if (rank.containsKey(playerObjId))
  129. {
  130. return rank.get(playerObjId);
  131. }
  132. return 0;
  133. }
  134. public Map<Integer, Integer> getRankList()
  135. {
  136. final Map<Integer, Integer> tmpPoints = new HashMap<>();
  137. for (int ownerId : _list.keySet())
  138. {
  139. int totalPoints = getPointsByOwnerId(ownerId);
  140. if (totalPoints != 0)
  141. {
  142. tmpPoints.put(ownerId, totalPoints);
  143. }
  144. }
  145. final List<Entry<Integer, Integer>> list = new ArrayList<>(tmpPoints.entrySet());
  146. list.sort(Comparator.comparing(Entry<Integer, Integer>::getValue).reversed());
  147. int ranking = 1;
  148. final Map<Integer, Integer> tmpRanking = new HashMap<>();
  149. for (Entry<Integer, Integer> entry : list)
  150. {
  151. tmpRanking.put(entry.getKey(), ranking++);
  152. }
  153. return tmpRanking;
  154. }
  155. public static final RaidBossPointsManager getInstance()
  156. {
  157. return SingletonHolder._instance;
  158. }
  159. private static class SingletonHolder
  160. {
  161. protected static final RaidBossPointsManager _instance = new RaidBossPointsManager();
  162. }
  163. }