RaidBossPointsManager.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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
  32. * JIV update 24.8.10
  33. *
  34. */
  35. public class RaidBossPointsManager
  36. {
  37. private final static 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. @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 static final RaidBossPointsManager getInstance()
  47. {
  48. return SingletonHolder._instance;
  49. }
  50. public RaidBossPointsManager()
  51. {
  52. init();
  53. }
  54. private final void init()
  55. {
  56. _list = new FastMap<Integer, Map<Integer, Integer>>();
  57. Connection con = null;
  58. try
  59. {
  60. con = L2DatabaseFactory.getInstance().getConnection();
  61. PreparedStatement statement = con.prepareStatement("SELECT `charId`,`boss_id`,`points` FROM `character_raid_points`");
  62. ResultSet rset = statement.executeQuery();
  63. while(rset.next())
  64. {
  65. int charId = rset.getInt("charId");
  66. int bossId = rset.getInt("boss_id");
  67. int points = rset.getInt("points");
  68. Map<Integer, Integer> values = _list.get(charId);
  69. if (values == null)
  70. {
  71. values = new FastMap<Integer, Integer>();
  72. }
  73. values.put(bossId, points);
  74. _list.put(charId, values);
  75. }
  76. rset.close();
  77. statement.close();
  78. _log.info(getClass().getSimpleName()+": Loaded "+_list.size()+" Characters Raid Points.");
  79. }
  80. catch (SQLException e)
  81. {
  82. _log.log(Level.WARNING, "RaidPointsManager: Couldnt load raid points ", e);
  83. }
  84. finally
  85. {
  86. L2DatabaseFactory.close(con);
  87. }
  88. }
  89. public final void updatePointsInDB(L2PcInstance player, int raidId, int points)
  90. {
  91. Connection con = null;
  92. try
  93. {
  94. con = L2DatabaseFactory.getInstance().getConnection();
  95. PreparedStatement statement;
  96. statement = con.prepareStatement("REPLACE INTO character_raid_points (`charId`,`boss_id`,`points`) VALUES (?,?,?)");
  97. statement.setInt(1, player.getObjectId());
  98. statement.setInt(2, raidId);
  99. statement.setInt(3, points);
  100. statement.executeUpdate();
  101. statement.close();
  102. }
  103. catch (Exception e)
  104. {
  105. _log.log(Level.WARNING, "could not update char raid points:", e);
  106. }
  107. finally
  108. {
  109. L2DatabaseFactory.close(con);
  110. }
  111. }
  112. public final void addPoints(L2PcInstance player, int bossId, int points)
  113. {
  114. int ownerId = player.getObjectId();
  115. Map<Integer, Integer> tmpPoint = _list.get(ownerId);
  116. if(tmpPoint == null)
  117. {
  118. tmpPoint = new FastMap<Integer, Integer>();
  119. tmpPoint.put(bossId, points);
  120. updatePointsInDB(player, bossId, points);
  121. }
  122. else
  123. {
  124. int currentPoins = tmpPoint.containsKey(bossId) ? tmpPoint.get(bossId) : 0;
  125. currentPoins += points;
  126. tmpPoint.put(bossId, currentPoins);
  127. updatePointsInDB(player, bossId, currentPoins);
  128. }
  129. _list.put(ownerId, tmpPoint);
  130. }
  131. public final int getPointsByOwnerId(int ownerId)
  132. {
  133. Map<Integer, Integer> tmpPoint;
  134. tmpPoint = _list.get(ownerId);
  135. int totalPoints = 0;
  136. if (tmpPoint == null || tmpPoint.isEmpty())
  137. return 0;
  138. for(int points : tmpPoint.values())
  139. {
  140. totalPoints += points;
  141. }
  142. return totalPoints;
  143. }
  144. public final Map<Integer, Integer> getList(L2PcInstance player)
  145. {
  146. return _list.get(player.getObjectId());
  147. }
  148. public final void cleanUp()
  149. {
  150. Connection con = null;
  151. try
  152. {
  153. con = L2DatabaseFactory.getInstance().getConnection();
  154. PreparedStatement statement;
  155. statement = con.prepareStatement("DELETE from character_raid_points WHERE charId > 0");
  156. statement.executeUpdate();
  157. statement.close();
  158. _list.clear();
  159. }
  160. catch (Exception e)
  161. {
  162. _log.log(Level.WARNING, "could not clean raid points: ", e);
  163. }
  164. finally
  165. {
  166. L2DatabaseFactory.close(con);
  167. }
  168. }
  169. public final int calculateRanking(int playerObjId)
  170. {
  171. Map<Integer, Integer> rank = getRankList();
  172. if (rank.containsKey(playerObjId))
  173. return rank.get(playerObjId);
  174. return 0;
  175. }
  176. public Map<Integer, Integer> getRankList()
  177. {
  178. Map<Integer, Integer> tmpRanking = new FastMap<Integer, Integer>();
  179. Map<Integer, Integer> tmpPoints = new FastMap<Integer, Integer>();
  180. for(int ownerId : _list.keySet())
  181. {
  182. int totalPoints = getPointsByOwnerId(ownerId);
  183. if(totalPoints != 0)
  184. {
  185. tmpPoints.put(ownerId, totalPoints);
  186. }
  187. }
  188. ArrayList<Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(tmpPoints.entrySet());
  189. Collections.sort(list, _comparator);
  190. int ranking = 1;
  191. for(Map.Entry<Integer, Integer> entry : list)
  192. tmpRanking.put(entry.getKey(), ranking++);
  193. return tmpRanking;
  194. }
  195. @SuppressWarnings("synthetic-access")
  196. private static class SingletonHolder
  197. {
  198. protected static final RaidBossPointsManager _instance = new RaidBossPointsManager();
  199. }
  200. }