Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. package l2f.gameserver.model.entity.olympiad;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.concurrent.ConcurrentHashMap;
  13.  
  14. import l2f.commons.dbutils.DbUtils;
  15. import l2f.gameserver.Announcements;
  16. import l2f.gameserver.Config;
  17. import l2f.gameserver.dao.OlympiadNobleDAO;
  18. import l2f.gameserver.database.DatabaseFactory;
  19. import l2f.gameserver.instancemanager.ServerVariables;
  20. import l2f.gameserver.model.base.ClassId;
  21. import l2f.gameserver.network.serverpackets.SystemMessage;
  22. import l2f.gameserver.network.serverpackets.SystemMessage2;
  23. import l2f.gameserver.network.serverpackets.components.SystemMsg;
  24. import l2f.gameserver.templates.StatsSet;
  25.  
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28.  
  29. public class OlympiadDatabase
  30. {
  31. private static final Logger _log = LoggerFactory.getLogger(OlympiadDatabase.class);
  32.  
  33. public static synchronized void loadNoblesRank()
  34. {
  35. Olympiad._noblesRank = new ConcurrentHashMap<Integer, Integer>();
  36. Map<Integer, Integer> tmpPlace = new HashMap<Integer, Integer>();
  37.  
  38. Connection con = null;
  39. PreparedStatement statement = null;
  40. ResultSet rset = null;
  41. try
  42. {
  43. con = DatabaseFactory.getInstance().getConnection();
  44. statement = con.prepareStatement(OlympiadNobleDAO.GET_ALL_CLASSIFIED_NOBLESS);
  45. rset = statement.executeQuery();
  46. int place = 1;
  47. while (rset.next())
  48. tmpPlace.put(rset.getInt(Olympiad.CHAR_ID), place++);
  49.  
  50. }
  51. catch(Exception e)
  52. {
  53. _log.error("Olympiad System: Error!", e);
  54. }
  55. finally
  56. {
  57. DbUtils.closeQuietly(con, statement, rset);
  58. }
  59.  
  60. int rank1 = (int) Math.round(tmpPlace.size() * 0.01);
  61. int rank2 = (int) Math.round(tmpPlace.size() * 0.10);
  62. int rank3 = (int) Math.round(tmpPlace.size() * 0.25);
  63. int rank4 = (int) Math.round(tmpPlace.size() * 0.50);
  64.  
  65. if (rank1 == 0)
  66. {
  67. rank1 = 1;
  68. rank2++;
  69. rank3++;
  70. rank4++;
  71. }
  72.  
  73. for (int charId : tmpPlace.keySet())
  74. if (tmpPlace.get(charId) <= rank1)
  75. Olympiad._noblesRank.put(charId, 1);
  76. else if (tmpPlace.get(charId) <= rank2)
  77. Olympiad._noblesRank.put(charId, 2);
  78. else if (tmpPlace.get(charId) <= rank3)
  79. Olympiad._noblesRank.put(charId, 3);
  80. else if (tmpPlace.get(charId) <= rank4)
  81. Olympiad._noblesRank.put(charId, 4);
  82. else
  83. Olympiad._noblesRank.put(charId, 5);
  84. }
  85.  
  86. /**
  87. * Сбрасывает информацию о ноблесах, сохраняя очки за предыдущий период
  88. */
  89. public static synchronized void cleanupNobles()
  90. {
  91. _log.info("Olympiad: Calculating last period...");
  92. Connection con = null;
  93. PreparedStatement statement = null;
  94. try
  95. {
  96. con = DatabaseFactory.getInstance().getConnection();
  97. statement = con.prepareStatement(OlympiadNobleDAO.OLYMPIAD_CALCULATE_LAST_PERIOD);
  98. statement.setInt(1, Config.OLYMPIAD_BATTLES_FOR_REWARD);
  99. statement.execute();
  100. DbUtils.close(statement);
  101.  
  102. statement = con.prepareStatement(OlympiadNobleDAO.OLYMPIAD_CLEANUP_NOBLES);
  103. statement.setInt(1, Config.OLYMPIAD_POINTS_DEFAULT);
  104. statement.execute();
  105. }
  106. catch(Exception e)
  107. {
  108. _log.error("Olympiad System: Couldn't calculate last period!", e);
  109. }
  110. finally
  111. {
  112. DbUtils.closeQuietly(con, statement);
  113. }
  114.  
  115. for (Integer nobleId : Olympiad._nobles.keySet())
  116. {
  117. StatsSet nobleInfo = Olympiad._nobles.get(nobleId);
  118. int points = nobleInfo.getInteger(Olympiad.POINTS);
  119. int compDone = nobleInfo.getInteger(Olympiad.COMP_DONE);
  120. nobleInfo.set(Olympiad.POINTS, Config.OLYMPIAD_POINTS_DEFAULT);
  121. if (compDone >= Config.OLYMPIAD_BATTLES_FOR_REWARD)
  122. {
  123. nobleInfo.set(Olympiad.POINTS_PAST, points);
  124. nobleInfo.set(Olympiad.POINTS_PAST_STATIC, points);
  125. }
  126. else
  127. {
  128. nobleInfo.set(Olympiad.POINTS_PAST, 0);
  129. nobleInfo.set(Olympiad.POINTS_PAST_STATIC, 0);
  130. }
  131. nobleInfo.set(Olympiad.COMP_DONE, 0);
  132. nobleInfo.set(Olympiad.COMP_WIN, 0);
  133. nobleInfo.set(Olympiad.COMP_LOOSE, 0);
  134. nobleInfo.set(Olympiad.GAME_CLASSES_COUNT, 0);
  135. nobleInfo.set(Olympiad.GAME_NOCLASSES_COUNT, 0);
  136. nobleInfo.set(Olympiad.GAME_TEAM_COUNT, 0);
  137. }
  138. }
  139.  
  140. public static List<String> getClassLeaderBoard(int classId)
  141. {
  142. List<String> names = new ArrayList<String>();
  143.  
  144. try (Connection con = DatabaseFactory.getInstance().getConnection();
  145. PreparedStatement statement = con.prepareStatement(classId == 132 ? OlympiadNobleDAO.GET_EACH_PAST_CLASS_LEADER_SOULHOUND : OlympiadNobleDAO.GET_EACH_PAST_CLASS_LEADER))
  146. {
  147. statement.setInt(1, classId);
  148.  
  149. try (ResultSet rset = statement.executeQuery())
  150. {
  151. while (rset.next())
  152. names.add(rset.getString(Olympiad.CHAR_NAME));
  153. }
  154. }
  155. catch (SQLException e)
  156. {
  157. _log.error("Olympiad System: Couldn't get old noble ranking from db!", e);
  158. }
  159.  
  160. return names;
  161. }
  162.  
  163. /**
  164. * Returning List of Character Names
  165. * Names are ordered DESC by olympiad_points(current Period)
  166. * Name is taken into consideration only if base class = classId
  167. * @param classId Id of the Base Class we is looking for
  168. * @return Names of the best players
  169. */
  170. public static List<String> getClassLeaderBoardCurrent(int classId)
  171. {
  172. List<String> names = new ArrayList<String>();
  173.  
  174. try (Connection con = DatabaseFactory.getInstance().getConnection();
  175. PreparedStatement statement = con.prepareStatement(classId == 132 ? OlympiadNobleDAO.GET_EACH_CURRENT_CLASS_LEADER_SOULHOUND : OlympiadNobleDAO.GET_EACH_CURRENT_CLASS_LEADER))
  176. {
  177. statement.setInt(1, classId);
  178. statement.setInt(2, Config.OLYMPIAD_BATTLES_FOR_REWARD);
  179.  
  180. try (ResultSet rset = statement.executeQuery())
  181. {
  182. while (rset.next())
  183. names.add(rset.getString(Olympiad.CHAR_NAME));
  184. }
  185. }
  186. catch (SQLException e)
  187. {
  188. _log.error("Olympiad System: Couldn't get current noble ranking from db!", e);
  189. }
  190.  
  191. return names;
  192. }
  193.  
  194. public static synchronized void sortHerosToBe()
  195. {
  196. if (Olympiad._period != 1)
  197. return;
  198.  
  199. Olympiad._heroesToBe = new ArrayList<StatsSet>();
  200.  
  201. Connection con = null;
  202. PreparedStatement statement = null;
  203. ResultSet rset = null;
  204. try
  205. {
  206. con = DatabaseFactory.getInstance().getConnection();
  207. StatsSet hero;
  208.  
  209. for (ClassId id : ClassId.VALUES)
  210. {
  211. if (id.getId() == 133)
  212. continue;
  213. if (id.level() == 3)
  214. {
  215. statement = con.prepareStatement(id.getId() == 132 ? OlympiadNobleDAO.OLYMPIAD_GET_HEROS_SOULHOUND : OlympiadNobleDAO.OLYMPIAD_GET_HEROS);
  216. statement.setInt(1, id.getId());
  217. statement.setInt(2, Config.OLYMPIAD_BATTLES_FOR_REWARD);
  218. rset = statement.executeQuery();
  219.  
  220. if (rset.next())
  221. {
  222. hero = new StatsSet();
  223. hero.set(Olympiad.CLASS_ID, id.getId());
  224. hero.set(Olympiad.CHAR_ID, rset.getInt(Olympiad.CHAR_ID));
  225. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  226.  
  227. Olympiad._heroesToBe.add(hero);
  228. }
  229. DbUtils.close(statement, rset);
  230. }
  231. }
  232. }
  233. catch(Exception e)
  234. {
  235. _log.error("Olympiad System: Couldnt heros from db!", e);
  236. }
  237. finally
  238. {
  239. DbUtils.closeQuietly(con, statement, rset);
  240. }
  241. }
  242.  
  243. public static synchronized void saveNobleData(int nobleId)
  244. {
  245. OlympiadNobleDAO.getInstance().replace(nobleId);
  246. }
  247.  
  248. public static synchronized void saveNobleData()
  249. {
  250. if (Olympiad._nobles == null)
  251. return;
  252. for (Integer nobleId : Olympiad._nobles.keySet())
  253. saveNobleData(nobleId);
  254. }
  255.  
  256. public static synchronized void setNewOlympiadEnd()
  257. {
  258. Announcements.getInstance().announceToAll(new SystemMessage(SystemMsg.ROUND_S1_OF_THE_GRAND_OLYMPIAD_GAMES_HAS_STARTED).addNumber(Olympiad._currentCycle));
  259.  
  260. Calendar currentTime = Calendar.getInstance();
  261. currentTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  262. currentTime.set(Calendar.AM_PM, Calendar.AM);
  263. currentTime.set(Calendar.HOUR, 12);
  264. currentTime.set(Calendar.MINUTE, 0);
  265. currentTime.set(Calendar.SECOND, 0);
  266. if (currentTime.getTimeInMillis() < System.currentTimeMillis())
  267. currentTime.add(Calendar.WEEK_OF_MONTH, 1);
  268.  
  269. _olympiadEnd = currentTime.getTimeInMillis();
  270. 
  271. Calendar nextChange = Calendar.getInstance();
  272. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  273. scheduleWeeklyChange();
  274.  
  275. Olympiad._isOlympiadEnd = false;
  276. Announcements.getInstance().announceToAll(new SystemMessage2(SystemMsg.OLYMPIAD_PERIOD_S1_HAS_STARTED).addInteger(Olympiad._currentCycle));
  277. }
  278.  
  279. public static void save()
  280. {
  281. saveNobleData();
  282. ServerVariables.set("Olympiad_CurrentCycle", Olympiad._currentCycle);
  283. ServerVariables.set("Olympiad_Period", Olympiad._period);
  284. ServerVariables.set("Olympiad_End", Olympiad._olympiadEnd);
  285. ServerVariables.set("Olympiad_ValdationEnd", Olympiad._validationEnd);
  286. ServerVariables.set("Olympiad_NextWeeklyChange", Olympiad._nextWeeklyChange);
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement