Leaderboard.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package com.gmail.nossr50.database;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Comparator;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import org.bukkit.Bukkit;
  12. import com.gmail.nossr50.mcMMO;
  13. import com.gmail.nossr50.config.Config;
  14. import com.gmail.nossr50.skills.utilities.SkillType;
  15. import com.gmail.nossr50.util.Misc;
  16. public final class Leaderboard {
  17. private static HashMap<SkillType, List<PlayerStat>> playerStatHash = new HashMap<SkillType, List<PlayerStat>>();
  18. private static List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
  19. private static long lastUpdate = 0;
  20. private Leaderboard() {}
  21. /**
  22. * Update the leader boards.
  23. */
  24. public static void updateLeaderboards() {
  25. if(System.currentTimeMillis() < lastUpdate + 600000) {
  26. return; //Only update FFS leaderboards every 10 minutes.. this puts a lot of strain on the server (depending on the size of the database) and should not be done frequently
  27. }
  28. lastUpdate = System.currentTimeMillis(); //Log when the last update was run
  29. //Initialize lists
  30. List<PlayerStat> mining, woodcutting, herbalism, excavation, acrobatics, repair, swords, axes, archery, unarmed, taming, fishing;
  31. mining = new ArrayList<PlayerStat>();
  32. woodcutting = new ArrayList<PlayerStat>();
  33. herbalism = new ArrayList<PlayerStat>();
  34. excavation = new ArrayList<PlayerStat>();
  35. acrobatics = new ArrayList<PlayerStat>();
  36. repair = new ArrayList<PlayerStat>();
  37. swords = new ArrayList<PlayerStat>();
  38. axes = new ArrayList<PlayerStat>();
  39. archery = new ArrayList<PlayerStat>();
  40. unarmed = new ArrayList<PlayerStat>();
  41. taming = new ArrayList<PlayerStat>();
  42. fishing = new ArrayList<PlayerStat>();
  43. powerLevels = new ArrayList<PlayerStat>();
  44. //Read from the FlatFile database and fill our arrays with information
  45. try {
  46. FileReader file = new FileReader(mcMMO.getUsersFilePath());
  47. BufferedReader in = new BufferedReader(file);
  48. String line = "";
  49. ArrayList<String> players = new ArrayList<String>();
  50. while ((line = in.readLine()) != null) {
  51. String[] character = line.split(":");
  52. String p = character[0];
  53. int powerLevel = 0;
  54. //Prevent the same player from being added multiple times (I'd like to note that this shouldn't happen...)
  55. if (players.contains(p)) {
  56. continue;
  57. }
  58. players.add(p);
  59. if (character.length > 1 && Misc.isInt(character[1])) {
  60. mining.add(new PlayerStat(p, Integer.valueOf(character[1])));
  61. powerLevel += Integer.valueOf(character[1]);
  62. }
  63. if (character.length > 5 && Misc.isInt(character[5])) {
  64. woodcutting.add(new PlayerStat(p, Integer.valueOf(character[5])));
  65. powerLevel += Integer.valueOf(character[5]);
  66. }
  67. if (character.length > 7 && Misc.isInt(character[7])) {
  68. repair.add(new PlayerStat(p, Integer.valueOf(character[7])));
  69. powerLevel += Integer.valueOf(character[7]);
  70. }
  71. if (character.length > 8 && Misc.isInt(character[8])) {
  72. unarmed.add(new PlayerStat(p, Integer.valueOf(character[8])));
  73. powerLevel += Integer.valueOf(character[8]);
  74. }
  75. if (character.length > 9 && Misc.isInt(character[9])) {
  76. herbalism.add(new PlayerStat(p, Integer.valueOf(character[9])));
  77. powerLevel += Integer.valueOf(character[9]);
  78. }
  79. if (character.length > 10 && Misc.isInt(character[10])) {
  80. excavation.add(new PlayerStat(p, Integer.valueOf(character[10])));
  81. powerLevel += Integer.valueOf(character[10]);
  82. }
  83. if (character.length > 11 && Misc.isInt(character[11])) {
  84. archery.add(new PlayerStat(p, Integer.valueOf(character[11])));
  85. powerLevel += Integer.valueOf(character[11]);
  86. }
  87. if (character.length > 12 && Misc.isInt(character[12])) {
  88. swords.add(new PlayerStat(p, Integer.valueOf(character[12])));
  89. powerLevel += Integer.valueOf(character[12]);
  90. }
  91. if (character.length > 13 && Misc.isInt(character[13])) {
  92. axes.add(new PlayerStat(p, Integer.valueOf(character[13])));
  93. powerLevel += Integer.valueOf(character[13]);
  94. }
  95. if (character.length > 14 && Misc.isInt(character[14])) {
  96. acrobatics.add(new PlayerStat(p, Integer.valueOf(character[14])));
  97. powerLevel += Integer.valueOf(character[14]);
  98. }
  99. if (character.length > 24 && Misc.isInt(character[24])) {
  100. taming.add(new PlayerStat(p, Integer.valueOf(character[24])));
  101. powerLevel += Integer.valueOf(character[24]);
  102. }
  103. if (character.length > 34 && Misc.isInt(character[34])) {
  104. fishing.add(new PlayerStat(p, Integer.valueOf(character[34])));
  105. powerLevel += Integer.valueOf(character[34]);
  106. }
  107. powerLevels.add(new PlayerStat(p, powerLevel));
  108. }
  109. in.close();
  110. }
  111. catch (Exception e) {
  112. mcMMO.p.getLogger().severe(("Exception while reading " + mcMMO.getUsersFilePath() + " (Are you sure you formatted it correctly?)" + e.toString()));
  113. }
  114. SkillComparator c = new SkillComparator();
  115. Collections.sort(mining, c);
  116. Collections.sort(woodcutting, c);
  117. Collections.sort(repair, c);
  118. Collections.sort(unarmed, c);
  119. Collections.sort(herbalism, c);
  120. Collections.sort(excavation, c);
  121. Collections.sort(archery, c);
  122. Collections.sort(swords, c);
  123. Collections.sort(axes, c);
  124. Collections.sort(acrobatics, c);
  125. Collections.sort(taming, c);
  126. Collections.sort(fishing, c);
  127. Collections.sort(powerLevels, c);
  128. playerStatHash.put(SkillType.MINING, mining);
  129. playerStatHash.put(SkillType.WOODCUTTING, woodcutting);
  130. playerStatHash.put(SkillType.REPAIR, repair);
  131. playerStatHash.put(SkillType.UNARMED, unarmed);
  132. playerStatHash.put(SkillType.HERBALISM, herbalism);
  133. playerStatHash.put(SkillType.EXCAVATION, excavation);
  134. playerStatHash.put(SkillType.ARCHERY, archery);
  135. playerStatHash.put(SkillType.SWORDS, swords);
  136. playerStatHash.put(SkillType.AXES, axes);
  137. playerStatHash.put(SkillType.ACROBATICS, acrobatics);
  138. playerStatHash.put(SkillType.TAMING, taming);
  139. playerStatHash.put(SkillType.FISHING, fishing);
  140. }
  141. /**
  142. * Retrieve leaderboard info.
  143. *
  144. * @param skillType Skill to retrieve info on.
  145. * @param pageNumber Which page in the leaderboards to retrieve
  146. * @return the requested leaderboard information
  147. */
  148. public static String[] retrieveInfo(String skillType, int pageNumber) {
  149. String[] info = new String[10];
  150. List<PlayerStat> statsList;
  151. if (skillType.equalsIgnoreCase("all")) {
  152. statsList = powerLevels;
  153. }
  154. else {
  155. statsList = playerStatHash.get(SkillType.getSkill(skillType));
  156. }
  157. int destination;
  158. // How many lines to skip through
  159. if (pageNumber == 1) {
  160. destination = 0;
  161. }
  162. else {
  163. destination = (pageNumber * 10) - 9;
  164. }
  165. int currentPos = 0;
  166. for (PlayerStat ps : statsList) {
  167. if (currentPos == 10) {
  168. break;
  169. }
  170. if (destination > 1) {
  171. destination--;
  172. continue;
  173. }
  174. info[currentPos] = ps.name + ":" + ps.statVal;
  175. currentPos++;
  176. }
  177. return info;
  178. }
  179. public static int[] getPlayerRank(String playerName) {
  180. int currentPos = 1;
  181. if (powerLevels != null) {
  182. for (PlayerStat stat : powerLevels) {
  183. if (stat.name.equalsIgnoreCase(playerName)) {
  184. return new int[] {currentPos, stat.statVal};
  185. }
  186. currentPos++;
  187. continue;
  188. }
  189. return new int[] {0, 0};
  190. }
  191. return new int[] {0, 0};
  192. }
  193. public static int[] getPlayerRank(String playerName, SkillType skillType) {
  194. int currentPos = 1;
  195. List<PlayerStat> statsList = playerStatHash.get(skillType);
  196. if (statsList != null) {
  197. for (PlayerStat stat : statsList) {
  198. if (stat.name.equalsIgnoreCase(playerName)) {
  199. return new int[] {currentPos, stat.statVal};
  200. }
  201. currentPos++;
  202. continue;
  203. }
  204. return new int[] {0, 0};
  205. }
  206. return new int[] {0, 0};
  207. }
  208. private static class SkillComparator implements Comparator<PlayerStat> {
  209. @Override
  210. public int compare(PlayerStat o1, PlayerStat o2) {
  211. return (o2.statVal - o1.statVal);
  212. }
  213. }
  214. public static boolean removeFlatFileUser(String playerName) {
  215. boolean worked = false;
  216. BufferedReader in = null;
  217. FileWriter out = null;
  218. String usersFilePath = mcMMO.getUsersFilePath();
  219. try {
  220. FileReader file = new FileReader(usersFilePath);
  221. in = new BufferedReader(file);
  222. StringBuilder writer = new StringBuilder();
  223. String line = "";
  224. while ((line = in.readLine()) != null) {
  225. /* Write out the same file but when we get to the player we want to remove, we skip his line. */
  226. if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
  227. writer.append(line).append("\r\n");
  228. }
  229. else {
  230. mcMMO.p.getLogger().info("User found, removing...");
  231. worked = true;
  232. continue; //Skip the player
  233. }
  234. }
  235. out = new FileWriter(usersFilePath); //Write out the new file
  236. out.write(writer.toString());
  237. }
  238. catch (Exception e) {
  239. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  240. }
  241. finally {
  242. if (in != null) {
  243. try {
  244. in.close();
  245. }
  246. catch (IOException ex) {
  247. ex.printStackTrace();
  248. }
  249. }
  250. if (out != null) {
  251. try {
  252. out.close();
  253. }
  254. catch (IOException ex) {
  255. ex.printStackTrace();
  256. }
  257. }
  258. }
  259. return worked;
  260. }
  261. public static void purgePowerlessFlatfile() {
  262. mcMMO.p.getLogger().info("Purging powerless users...");
  263. int purgedUsers = 0;
  264. for (PlayerStat stat : powerLevels) {
  265. if (stat.statVal == 0 && removeFlatFileUser(stat.name) && !Bukkit.getOfflinePlayer(stat.name).isOnline()) {
  266. purgedUsers++;
  267. }
  268. }
  269. mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
  270. }
  271. public static void purgeOldFlatfile() {
  272. mcMMO.p.getLogger().info("Purging old users...");
  273. int purgedUsers = removeOldFlatfileUsers();
  274. mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
  275. }
  276. private static int removeOldFlatfileUsers() {
  277. int removedPlayers = 0;
  278. long currentTime = System.currentTimeMillis();
  279. long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff();
  280. BufferedReader in = null;
  281. FileWriter out = null;
  282. String usersFilePath = mcMMO.getUsersFilePath();
  283. try {
  284. FileReader file = new FileReader(usersFilePath);
  285. in = new BufferedReader(file);
  286. StringBuilder writer = new StringBuilder();
  287. String line = "";
  288. while ((line = in.readLine()) != null) {
  289. /* Write out the same file but when we get to the player we want to remove, we skip his line. */
  290. String[] splitLine = line.split(":");
  291. if (splitLine.length > 37) {
  292. if (currentTime - (Misc.getLong(line.split(":")[37]) * 1000) <= purgeTime) {
  293. writer.append(line).append("\r\n");
  294. }
  295. else {
  296. mcMMO.p.getLogger().info("User found, removing...");
  297. removedPlayers++;
  298. continue; //Skip the player
  299. }
  300. }
  301. else {
  302. writer.append(line).append("\r\n");
  303. }
  304. }
  305. out = new FileWriter(usersFilePath); //Write out the new file
  306. out.write(writer.toString());
  307. }
  308. catch (Exception e) {
  309. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  310. }
  311. finally {
  312. if (in != null) {
  313. try {
  314. in.close();
  315. }
  316. catch (IOException ex) {
  317. ex.printStackTrace();
  318. }
  319. }
  320. if (out != null) {
  321. try {
  322. out.close();
  323. }
  324. catch (IOException ex) {
  325. ex.printStackTrace();
  326. }
  327. }
  328. }
  329. return removedPlayers;
  330. }
  331. }