RaidBossPointsManager.java 5.7 KB

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