RaidBossPointsManager.java 7.4 KB

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