RaidBossPointsManager.java 5.8 KB

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