RaidBossPointsManager.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. statement = con.prepareStatement("SELECT * FROM `character_raid_points` WHERE `charId`=?");
  61. for(FastList.Node<Integer> n = _chars.head(), end = _chars.tail(); (n = n.getNext()) != end;)
  62. {
  63. int charId = n.getValue();
  64. FastMap<Integer, Integer> values = new FastMap<Integer, Integer>();
  65. statement.setInt(1, charId);
  66. rset = statement.executeQuery();
  67. statement.clearParameters();
  68. while(rset.next())
  69. {
  70. values.put(rset.getInt("boss_id"), rset.getInt("points"));
  71. }
  72. rset.close();
  73. _list.put(charId, values);
  74. }
  75. statement.close();
  76. }
  77. catch (SQLException e)
  78. {
  79. _log.warning("RaidPointsManager: Couldnt load raid points ");
  80. }
  81. catch (Exception e)
  82. {
  83. _log.warning(e.getMessage());
  84. }
  85. finally
  86. {
  87. try
  88. {
  89. con.close();
  90. }
  91. catch(Exception e)
  92. {
  93. _log.warning(e.getMessage());
  94. }
  95. }
  96. }
  97. public final static void updatePointsInDB(L2PcInstance player, int raidId, int points)
  98. {
  99. Connection con = null;
  100. try
  101. {
  102. con = L2DatabaseFactory.getInstance().getConnection();
  103. PreparedStatement statement;
  104. statement = con.prepareStatement("REPLACE INTO character_raid_points (`charId`,`boss_id`,`points`) VALUES (?,?,?)");
  105. statement.setInt(1, player.getObjectId());
  106. statement.setInt(2, raidId);
  107. statement.setInt(3, points);
  108. statement.executeUpdate();
  109. statement.close();
  110. } catch (Exception e) {
  111. _log.log(Level.WARNING, "could not update char raid points:", e);
  112. } finally {
  113. try { con.close(); } catch (Exception e) {}
  114. }
  115. }
  116. public final static void addPoints(L2PcInstance player, int bossId, int points)
  117. {
  118. int ownerId = player.getObjectId();
  119. Map<Integer, Integer> tmpPoint;
  120. if (_list == null)
  121. _list = new FastMap<Integer, Map<Integer, Integer>>();
  122. tmpPoint = _list.get(ownerId);
  123. if(tmpPoint == null || tmpPoint.isEmpty())
  124. {
  125. tmpPoint = new FastMap<Integer, Integer>();
  126. tmpPoint.put(bossId, points);
  127. updatePointsInDB(player, bossId, points);
  128. }
  129. else
  130. {
  131. int currentPoins = tmpPoint.containsKey(bossId) ? tmpPoint.get(bossId).intValue() : 0;
  132. tmpPoint.remove(bossId);
  133. tmpPoint.put(bossId, currentPoins == 0 ? points : currentPoins + points);
  134. updatePointsInDB(player, bossId, currentPoins == 0 ? points : currentPoins + points);
  135. }
  136. _list.remove(ownerId);
  137. _list.put(ownerId, tmpPoint);
  138. }
  139. public final static int getPointsByOwnerId(int ownerId)
  140. {
  141. Map<Integer, Integer> tmpPoint;
  142. if (_list == null)
  143. _list = new FastMap<Integer, Map<Integer, Integer>>();
  144. tmpPoint = _list.get(ownerId);
  145. int totalPoints = 0;
  146. if (tmpPoint == null || tmpPoint.isEmpty())
  147. return 0;
  148. for(int bossId : tmpPoint.keySet())
  149. {
  150. totalPoints += tmpPoint.get(bossId);
  151. }
  152. return totalPoints;
  153. }
  154. public final static Map<Integer, Integer> getList(L2PcInstance player)
  155. {
  156. return _list.get(player.getObjectId());
  157. }
  158. public final static void cleanUp()
  159. {
  160. Connection con = null;
  161. try
  162. {
  163. con = L2DatabaseFactory.getInstance().getConnection();
  164. PreparedStatement statement;
  165. statement = con.prepareStatement("DELETE from character_raid_points WHERE charId > 0");
  166. statement.executeUpdate();
  167. statement.close();
  168. _list.clear();
  169. _list = new FastMap<Integer, Map<Integer, Integer>>();
  170. } catch (Exception e) {
  171. _log.log(Level.WARNING, "could not clean raid points: ", e);
  172. } finally {
  173. try { con.close(); } catch (Exception e) {}
  174. }
  175. }
  176. public final static int calculateRanking(int playerObjId)
  177. {
  178. Map<Integer, Integer> tmpRanking = new FastMap<Integer, Integer>();
  179. Map<Integer, Integer> tmpPoints = new FastMap<Integer, Integer>();
  180. int totalPoints;
  181. for(int ownerId : _list.keySet())
  182. {
  183. totalPoints = getPointsByOwnerId(ownerId);
  184. if(totalPoints != 0)
  185. {
  186. tmpPoints.put(ownerId, totalPoints);
  187. }
  188. }
  189. ArrayList<Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(tmpPoints.entrySet());
  190. Collections.sort(list, _comparator);
  191. int ranking = 1;
  192. for(Map.Entry<Integer, Integer> entry : list)
  193. tmpRanking.put(entry.getKey(), ranking++);
  194. if (tmpRanking.containsKey(playerObjId))
  195. return tmpRanking.get(playerObjId);
  196. return 0;
  197. }
  198. public static Map<Integer, Integer> getRankList()
  199. {
  200. Map<Integer, Integer> tmpRanking = new FastMap<Integer, Integer>();
  201. Map<Integer, Integer> tmpPoints = new FastMap<Integer, Integer>();
  202. int totalPoints;
  203. for(int ownerId : _list.keySet())
  204. {
  205. totalPoints = getPointsByOwnerId(ownerId);
  206. if(totalPoints != 0)
  207. {
  208. tmpPoints.put(ownerId, totalPoints);
  209. }
  210. }
  211. ArrayList<Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(tmpPoints.entrySet());
  212. Collections.sort(list, _comparator);
  213. int ranking = 1;
  214. for(Map.Entry<Integer, Integer> entry : list)
  215. tmpRanking.put(entry.getKey(), ranking++);
  216. return tmpRanking;
  217. }
  218. }