RaidBossPointsManager.java 8.3 KB

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