RaidBossPointsManager.java 5.9 KB

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