RaidBossPointsManager.java 7.4 KB

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