Advertisement
PaiPlayer

Untitled

Apr 6th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.77 KB | None | 0 0
  1. /*
  2. * This file is part of the L2J Mobius project.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package org.l2jmobius.gameserver.instancemanager;
  18.  
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.util.Calendar;
  23. import java.util.Collection;
  24. import java.util.LinkedList;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31.  
  32. import org.l2jmobius.commons.database.DatabaseFactory;
  33. import org.l2jmobius.commons.threads.ThreadPool;
  34. import org.l2jmobius.gameserver.data.sql.ClanTable;
  35. import org.l2jmobius.gameserver.data.xml.PetDataTable;
  36. import org.l2jmobius.gameserver.model.PetData;
  37. import org.l2jmobius.gameserver.model.StatSet;
  38. import org.l2jmobius.gameserver.model.World;
  39. import org.l2jmobius.gameserver.model.actor.Player;
  40. import org.l2jmobius.gameserver.model.olympiad.Hero;
  41.  
  42. /**
  43. * @author NviX, PaiPlayer
  44. */
  45. public class RankManager
  46. {
  47. private static final Logger LOGGER = Logger.getLogger(RankManager.class.getName());
  48.  
  49. public static final Long TIME_LIMIT = 2592000000L; // 30 days in milliseconds
  50. public static final long CURRENT_TIME = System.currentTimeMillis();
  51. public static final int PLAYER_LIMIT = 500;
  52.  
  53. private static final String CHARACTER_1ST_CLASS_LIST = "1,10,18,25,44,49,53,192,196,200,208,217"; // letting like this to easily expand at next expansions
  54.  
  55. private static final String SELECT_CHARACTERS = "SELECT charId,char_name,level,race,base_class, clanid FROM characters WHERE (" + CURRENT_TIME + " - cast(lastAccess as signed) < " + TIME_LIMIT + ") AND accesslevel = 0 AND level > 39 AND classid NOT IN (" + CHARACTER_1ST_CLASS_LIST + ") ORDER BY exp DESC, onlinetime DESC LIMIT " + PLAYER_LIMIT;
  56. private static final String SELECT_CHARACTERS_PVP = "SELECT charId,char_name,level,race,base_class, clanid, deaths, kills, pvpkills FROM characters WHERE (" + CURRENT_TIME + " - cast(lastAccess as signed) < " + TIME_LIMIT + ") AND accesslevel = 0 AND level > 39 AND classid NOT IN (" + CHARACTER_1ST_CLASS_LIST + ") ORDER BY kills DESC, onlinetime DESC LIMIT " + PLAYER_LIMIT;
  57. private static final String SELECT_CHARACTERS_BY_RACE = "SELECT charId FROM characters WHERE (" + CURRENT_TIME + " - cast(lastAccess as signed) < " + TIME_LIMIT + ") AND accesslevel = 0 AND level > 39 AND classid NOT IN (" + CHARACTER_1ST_CLASS_LIST + ") AND race = ? ORDER BY exp DESC, onlinetime DESC LIMIT " + PLAYER_LIMIT;
  58. private static final String SELECT_PETS = "SELECT characters.charId, pets.exp, characters.char_name, pets.level as petLevel, characters.race as char_race, characters.level as char_level, characters.clanId, pet_evolves.index, pet_evolves.level as evolveLevel, pets.item_obj_id, item_id FROM characters, items, pets, pet_evolves WHERE pets.ownerId = characters.charId AND pet_evolves.itemObjId = items.object_id AND pet_evolves.itemObjId = pets.item_obj_id AND (" + CURRENT_TIME + " - cast(characters.lastAccess as signed) < " + TIME_LIMIT + ") AND characters.accesslevel = 0 AND pets.level > 39 AND characters.classid NOT IN (" + CHARACTER_1ST_CLASS_LIST + ") ORDER BY pets.exp DESC, characters.onlinetime DESC LIMIT " + PLAYER_LIMIT;
  59. private static final String SELECT_CLANS = "SELECT characters.level, characters.char_name, clan_data.clan_id, clan_data.clan_level, clan_data.clan_name, clan_data.reputation_score, clan_data.exp FROM characters, clan_data WHERE characters.charId = clan_data.leader_id AND characters.clanid = clan_data.clan_id AND dissolving_expiry_time = 0 ORDER BY exp DESC LIMIT " + PLAYER_LIMIT;
  60.  
  61. private static final String GET_CURRENT_CYCLE_DATA = "SELECT characters.char_name, characters.level, characters.base_class, characters.clanid, olympiad_nobles.charId, olympiad_nobles.olympiad_points, olympiad_nobles.competitions_won, olympiad_nobles.competitions_lost FROM characters, olympiad_nobles WHERE characters.charId = olympiad_nobles.charId ORDER BY olympiad_nobles.olympiad_points DESC LIMIT " + PLAYER_LIMIT;
  62. private static final String GET_CHARACTERS_BY_CLASS = "SELECT charId FROM characters WHERE (" + CURRENT_TIME + " - cast(lastAccess as signed) < " + TIME_LIMIT + ") AND accesslevel = 0 AND level > 39 AND characters.base_class = ? AND classid NOT IN (" + CHARACTER_1ST_CLASS_LIST + ") ORDER BY exp DESC, onlinetime DESC LIMIT " + PLAYER_LIMIT;
  63.  
  64. private final Map<Integer, StatSet> _mainList = new ConcurrentHashMap<>();
  65. private Map<Integer, StatSet> _snapshotList = new ConcurrentHashMap<>();
  66. private final Map<Integer, StatSet> _mainOlyList = new ConcurrentHashMap<>();
  67. private Map<Integer, StatSet> _snapshotOlyList = new ConcurrentHashMap<>();
  68. private final Map<Integer, StatSet> _mainPvpList = new ConcurrentHashMap<>();
  69. private Map<Integer, StatSet> _snapshotPvpList = new ConcurrentHashMap<>();
  70. private final Map<Integer, StatSet> _mainPetList = new ConcurrentHashMap<>();
  71. private Map<Integer, StatSet> _snapshotPetList = new ConcurrentHashMap<>();
  72. private final Map<Integer, StatSet> _mainClanList = new ConcurrentHashMap<>();
  73. private Map<Integer, StatSet> _snapshotClanList = new ConcurrentHashMap<>();
  74.  
  75. protected RankManager()
  76. {
  77. final Calendar calendar = Calendar.getInstance();
  78. calendar.set(Calendar.MINUTE, 30);
  79. calendar.set(Calendar.SECOND, 0);
  80. calendar.set(Calendar.HOUR_OF_DAY, 6);
  81. if ((calendar.get(Calendar.HOUR_OF_DAY) >= 6) && (calendar.get(Calendar.MINUTE) >= 30))
  82. {
  83. calendar.add(Calendar.DAY_OF_YEAR, 1);
  84. }
  85. ThreadPool.scheduleAtFixedRate(this::update, calendar.getTimeInMillis() - System.currentTimeMillis(), 86400000); // start 6:30am then update every 1 day
  86. update(); // to run at server startup
  87. }
  88.  
  89. private synchronized void update()
  90. {
  91. // Load charIds All
  92. _snapshotList = _mainList;
  93. _mainList.clear();
  94. _snapshotOlyList = _mainOlyList;
  95. _mainOlyList.clear();
  96. _snapshotPvpList = _mainPvpList;
  97. _mainPvpList.clear();
  98. _snapshotPetList = _mainPetList;
  99. _mainPetList.clear();
  100. _snapshotClanList = _mainClanList;
  101. _mainClanList.clear();
  102.  
  103. try (Connection con = DatabaseFactory.getConnection();
  104. PreparedStatement statement = con.prepareStatement(SELECT_CHARACTERS))
  105. {
  106. try (ResultSet rset = statement.executeQuery())
  107. {
  108. int i = 1;
  109. while (rset.next())
  110. {
  111. final StatSet player = new StatSet();
  112. final int charId = rset.getInt("charId");
  113. final int classId = rset.getInt("base_class");
  114. player.set("charId", charId);
  115. player.set("name", rset.getString("char_name"));
  116. player.set("level", rset.getInt("level"));
  117. player.set("classId", rset.getInt("base_class"));
  118. final int race = rset.getInt("race");
  119. player.set("race", race);
  120.  
  121. loadRaceRank(charId, race, player);
  122. loadClassRank(charId, classId, player);
  123. final int clanId = rset.getInt("clanid");
  124. if (clanId > 0)
  125. {
  126. player.set("clanName", ClanTable.getInstance().getClan(clanId).getName());
  127. }
  128. else
  129. {
  130. player.set("clanName", "");
  131. }
  132.  
  133. _mainList.put(i, player);
  134. i++;
  135. }
  136. }
  137. }
  138. catch (Exception e)
  139. {
  140. LOGGER.log(Level.WARNING, "Could not load chars total rank data: " + this + " - " + e.getMessage(), e);
  141. }
  142.  
  143. // load olympiad data.
  144. try (Connection con = DatabaseFactory.getConnection();
  145. PreparedStatement statement = con.prepareStatement(GET_CURRENT_CYCLE_DATA))
  146. {
  147. try (ResultSet rset = statement.executeQuery())
  148. {
  149. int i = 1;
  150. while (rset.next())
  151. {
  152. final StatSet player = new StatSet();
  153. final int charId = rset.getInt("charId");
  154. player.set("charId", charId);
  155. player.set("name", rset.getString("char_name"));
  156. final int clanId = rset.getInt("clanid");
  157. if (clanId > 0)
  158. {
  159. player.set("clanName", ClanTable.getInstance().getClan(clanId).getName());
  160. }
  161. else
  162. {
  163. player.set("clanName", "");
  164. }
  165. player.set("level", rset.getInt("level"));
  166. final int classId = rset.getInt("base_class");
  167. player.set("classId", classId);
  168. if (clanId > 0)
  169. {
  170. player.set("clanLevel", ClanTable.getInstance().getClan(clanId).getLevel());
  171. }
  172. else
  173. {
  174. player.set("clanLevel", 0);
  175. }
  176. player.set("competitions_won", rset.getInt("competitions_won"));
  177. player.set("competitions_lost", rset.getInt("competitions_lost"));
  178. player.set("olympiad_points", rset.getInt("olympiad_points"));
  179.  
  180. if (Hero.getInstance().getCompleteHeroes().containsKey(charId))
  181. {
  182. final StatSet hero = Hero.getInstance().getCompleteHeroes().get(charId);
  183. player.set("count", hero.getInt("count", 0));
  184. player.set("legend_count", hero.getInt("legend_count", 0));
  185. }
  186. else
  187. {
  188. player.set("count", 0);
  189. player.set("legend_count", 0);
  190. }
  191.  
  192. loadClassRank(charId, classId, player);
  193.  
  194. _mainOlyList.put(i, player);
  195. i++;
  196. }
  197. }
  198. }
  199. catch (Exception e)
  200. {
  201. LOGGER.log(Level.WARNING, "Could not load olympiad total rank data: " + this + " - " + e.getMessage(), e);
  202. }
  203.  
  204. try (Connection con = DatabaseFactory.getConnection();
  205. PreparedStatement statement = con.prepareStatement(SELECT_CHARACTERS_PVP))
  206. {
  207. try (ResultSet rset = statement.executeQuery())
  208. {
  209. int i = 1;
  210. while (rset.next())
  211. {
  212. final StatSet player = new StatSet();
  213. final int charId = rset.getInt("charId");
  214. player.set("charId", charId);
  215. player.set("name", rset.getString("char_name"));
  216. player.set("level", rset.getInt("level"));
  217. player.set("classId", rset.getInt("base_class"));
  218. final int race = rset.getInt("race");
  219. player.set("race", race);
  220. player.set("kills", rset.getInt("kills"));
  221. player.set("deaths", rset.getInt("deaths"));
  222. player.set("points", rset.getInt("pvpkills"));
  223. loadRaceRank(charId, race, player);
  224. final int clanId = rset.getInt("clanid");
  225. if (clanId > 0)
  226. {
  227. player.set("clanName", ClanTable.getInstance().getClan(clanId).getName());
  228. }
  229. else
  230. {
  231. player.set("clanName", "");
  232. }
  233.  
  234. _mainPvpList.put(i, player);
  235. i++;
  236. }
  237. }
  238. }
  239. catch (Exception e)
  240. {
  241. LOGGER.log(Level.WARNING, "Could not load pvp total rank data: " + this + " - " + e.getMessage(), e);
  242. }
  243.  
  244. try (Connection con = DatabaseFactory.getConnection();
  245. PreparedStatement statement = con.prepareStatement(SELECT_PETS))
  246. {
  247. try (ResultSet rset = statement.executeQuery())
  248. {
  249. int i = 1;
  250. while (rset.next())
  251. {
  252. final StatSet pet = new StatSet();
  253. final int controlledItemObjId = rset.getInt("item_obj_id");
  254. pet.set("controlledItemObjId", controlledItemObjId);
  255. pet.set("name", PetDataTable.getInstance().getNameByItemObjectId(controlledItemObjId));
  256. pet.set("ownerId", rset.getInt("charId"));
  257. pet.set("owner_name", rset.getString("char_name"));
  258. pet.set("owner_race", rset.getString("char_race"));
  259. pet.set("owner_level", rset.getInt("char_level"));
  260. pet.set("level", rset.getInt("petLevel"));
  261. pet.set("evolve_level", rset.getInt("evolveLevel"));
  262. pet.set("exp", rset.getLong("exp"));
  263. pet.set("clanName", rset.getInt("clanid") > 0 ? ClanTable.getInstance().getClan(rset.getInt("clanid")).getName() : "");
  264. final PetData petData = PetDataTable.getInstance().getPetDataByItemId(rset.getInt("item_id"));
  265. pet.set("petType", petData.getType());
  266. pet.set("npcId", petData.getNpcId());
  267. _mainPetList.put(i++, pet);
  268. }
  269. }
  270. }
  271. catch (Exception e)
  272. {
  273. LOGGER.log(Level.WARNING, "Could not load pet total rank data: " + this + " - " + e.getMessage(), e);
  274. }
  275.  
  276. try (Connection con = DatabaseFactory.getConnection();
  277. PreparedStatement statement = con.prepareStatement(SELECT_CLANS))
  278. {
  279. try (ResultSet rset = statement.executeQuery())
  280. {
  281. int i = 1;
  282. while (rset.next())
  283. {
  284. final StatSet player = new StatSet();
  285. player.set("char_name", rset.getString("char_name"));
  286. player.set("level", rset.getInt("level"));
  287. player.set("clan_level", rset.getInt("clan_level"));
  288. player.set("clan_name", rset.getString("clan_name"));
  289. player.set("reputation_score", rset.getInt("reputation_score"));
  290. player.set("exp", rset.getLong("exp"));
  291. player.set("clan_id", rset.getInt("clan_id"));
  292. _mainClanList.put(i, player);
  293. i++;
  294. }
  295. }
  296. }
  297. catch (Exception e)
  298. {
  299. LOGGER.log(Level.WARNING, "Could not load clan total rank data: " + this + " - " + e.getMessage(), e);
  300. }
  301. }
  302.  
  303. private void loadClassRank(int charId, int classId, StatSet player)
  304. {
  305. try (Connection con = DatabaseFactory.getConnection();
  306. PreparedStatement ps = con.prepareStatement(GET_CHARACTERS_BY_CLASS))
  307. {
  308. ps.setInt(1, classId);
  309. try (ResultSet rset = ps.executeQuery())
  310. {
  311. int i = 0;
  312. while (rset.next())
  313. {
  314. if (rset.getInt("charId") == charId)
  315. {
  316. player.set("classRank", i + 1);
  317. }
  318. i++;
  319. }
  320. if (i == 0)
  321. {
  322. player.set("classRank", 0);
  323. }
  324. }
  325. }
  326. catch (Exception e)
  327. {
  328. LOGGER.log(Level.WARNING, "Could not load chars classId olympiad rank data: " + this + " - " + e.getMessage(), e);
  329. }
  330. }
  331.  
  332. private void loadRaceRank(int charId, int race, StatSet player)
  333. {
  334. try (Connection con = DatabaseFactory.getConnection();
  335. PreparedStatement ps = con.prepareStatement(SELECT_CHARACTERS_BY_RACE))
  336. {
  337. ps.setInt(1, race);
  338. try (ResultSet rset = ps.executeQuery())
  339. {
  340. int i = 0;
  341. while (rset.next())
  342. {
  343. if (rset.getInt("charId") == charId)
  344. {
  345. player.set("raceRank", i + 1);
  346. }
  347. i++;
  348. }
  349. if (i == 0)
  350. {
  351. player.set("raceRank", 0);
  352. }
  353. }
  354. }
  355. catch (Exception e)
  356. {
  357. LOGGER.log(Level.WARNING, "Could not load chars race rank data: " + this + " - " + e.getMessage(), e);
  358. }
  359. }
  360.  
  361. public Map<Integer, StatSet> getRankList()
  362. {
  363. return _mainList;
  364. }
  365.  
  366. public Map<Integer, StatSet> getSnapshotList()
  367. {
  368. return _snapshotList;
  369. }
  370.  
  371. public Map<Integer, StatSet> getOlyRankList()
  372. {
  373. return _mainOlyList;
  374. }
  375.  
  376. public Map<Integer, StatSet> getSnapshotOlyList()
  377. {
  378. return _snapshotOlyList;
  379. }
  380.  
  381. public Map<Integer, StatSet> getPvpRankList()
  382. {
  383. return _mainPvpList;
  384. }
  385.  
  386. public Map<Integer, StatSet> getSnapshotPvpRankList()
  387. {
  388. return _snapshotPvpList;
  389. }
  390.  
  391. public Map<Integer, StatSet> getPetRankList()
  392. {
  393. return _mainPetList;
  394. }
  395.  
  396. public Map<Integer, StatSet> getSnapshotPetRankList()
  397. {
  398. return _snapshotPetList;
  399. }
  400.  
  401. public Map<Integer, StatSet> getClanRankList()
  402. {
  403. return _mainClanList;
  404. }
  405.  
  406. public Map<Integer, StatSet> getSnapshotClanRankList()
  407. {
  408. return _snapshotClanList;
  409. }
  410.  
  411. public int getPlayerGlobalRank(Player player)
  412. {
  413. final int playerOid = player.getObjectId();
  414. for (Entry<Integer, StatSet> entry : _mainList.entrySet())
  415. {
  416. final StatSet stats = entry.getValue();
  417. if (stats.getInt("charId") != playerOid)
  418. {
  419. continue;
  420. }
  421. return entry.getKey();
  422. }
  423. return 0;
  424. }
  425.  
  426. public int getPlayerRaceRank(Player player)
  427. {
  428. final int playerOid = player.getObjectId();
  429. for (StatSet stats : _mainList.values())
  430. {
  431. if (stats.getInt("charId") != playerOid)
  432. {
  433. continue;
  434. }
  435. return stats.getInt("raceRank");
  436. }
  437. return 0;
  438. }
  439.  
  440. public int getPlayerClassRank(Player player)
  441. {
  442. final int playerOid = player.getObjectId();
  443. for (StatSet stats : _mainList.values())
  444. {
  445. if (stats.getInt("charId") != playerOid)
  446. {
  447. continue;
  448. }
  449. return stats.getInt("classRank");
  450. }
  451. return 0;
  452. }
  453.  
  454. public Collection<Player> getTop50()
  455. {
  456. final List<Player> result = new LinkedList<>();
  457. for (int i = 1; i <= 50; i++)
  458. {
  459. final StatSet rank = _mainList.get(i);
  460. if (rank == null)
  461. {
  462. break;
  463. }
  464.  
  465. final Player player = World.getInstance().getPlayer(rank.getInt("charId"));
  466. if ((player == null) || (player.isOnlineInt() != 1))
  467. {
  468. continue;
  469. }
  470.  
  471. result.add(player);
  472. }
  473. return result;
  474. }
  475.  
  476. public static RankManager getInstance()
  477. {
  478. return SingletonHolder.INSTANCE;
  479. }
  480.  
  481. private static class SingletonHolder
  482. {
  483. protected static final RankManager INSTANCE = new RankManager();
  484. }
  485. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement