Leaderboard.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package com.gmail.nossr50.util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.List;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.config.Config;
  13. import com.gmail.nossr50.datatypes.PlayerStat;
  14. import com.gmail.nossr50.datatypes.SkillType;
  15. public class Leaderboard {
  16. private static mcMMO plugin = mcMMO.p;
  17. private static String leaderboardsDirectory = mcMMO.getLeaderboardDirectory();
  18. private final static String location = mcMMO.getUsersFile();
  19. /**
  20. * Create the leaderboards.
  21. */
  22. public static void makeLeaderboards() {
  23. //Make Lists
  24. List<PlayerStat> Mining = new ArrayList<PlayerStat>();
  25. List<PlayerStat> WoodCutting = new ArrayList<PlayerStat>();
  26. List<PlayerStat> Herbalism = new ArrayList<PlayerStat>();
  27. List<PlayerStat> Excavation = new ArrayList<PlayerStat>();
  28. List<PlayerStat> Acrobatics = new ArrayList<PlayerStat>();
  29. List<PlayerStat> Repair = new ArrayList<PlayerStat>();
  30. List<PlayerStat> Swords = new ArrayList<PlayerStat>();
  31. List<PlayerStat> Axes = new ArrayList<PlayerStat>();
  32. List<PlayerStat> Archery = new ArrayList<PlayerStat>();
  33. List<PlayerStat> Unarmed = new ArrayList<PlayerStat>();
  34. List<PlayerStat> Taming = new ArrayList<PlayerStat>();
  35. List<PlayerStat> Fishing = new ArrayList<PlayerStat>();
  36. List<PlayerStat> PowerLevel = new ArrayList<PlayerStat>();
  37. //Add Data To Lists
  38. try {
  39. FileReader file = new FileReader(location);
  40. BufferedReader in = new BufferedReader(file);
  41. String line = "";
  42. ArrayList<String> players = new ArrayList<String>();
  43. while ((line = in.readLine()) != null) {
  44. String[] character = line.split(":");
  45. String p = character[0];
  46. int powerLevel = 0;
  47. //Prevent the same player from being added multiple times
  48. if (players.contains(p)) {
  49. continue;
  50. }
  51. else {
  52. players.add(p);
  53. }
  54. if (character.length > 1 && Misc.isInt(character[1])) {
  55. Mining.add(new PlayerStat(p, Integer.valueOf(character[1])));
  56. powerLevel += Integer.valueOf(character[1]);
  57. }
  58. if (character.length > 5 && Misc.isInt(character[5])) {
  59. WoodCutting.add(new PlayerStat(p, Integer.valueOf(character[5])));
  60. powerLevel += Integer.valueOf(character[5]);
  61. }
  62. if (character.length > 7 && Misc.isInt(character[7])) {
  63. Repair.add(new PlayerStat(p, Integer.valueOf(character[7])));
  64. powerLevel += Integer.valueOf(character[7]);
  65. }
  66. if (character.length > 8 && Misc.isInt(character[8])) {
  67. Unarmed.add(new PlayerStat(p, Integer.valueOf(character[8])));
  68. powerLevel += Integer.valueOf(character[8]);
  69. }
  70. if (character.length > 9 && Misc.isInt(character[9])) {
  71. Herbalism.add(new PlayerStat(p, Integer.valueOf(character[9])));
  72. powerLevel += Integer.valueOf(character[9]);
  73. }
  74. if (character.length > 10 && Misc.isInt(character[10])) {
  75. Excavation.add(new PlayerStat(p, Integer.valueOf(character[10])));
  76. powerLevel += Integer.valueOf(character[10]);
  77. }
  78. if (character.length > 11 && Misc.isInt(character[11])) {
  79. Archery.add(new PlayerStat(p, Integer.valueOf(character[11])));
  80. powerLevel += Integer.valueOf(character[11]);
  81. }
  82. if (character.length > 12 && Misc.isInt(character[12])) {
  83. Swords.add(new PlayerStat(p, Integer.valueOf(character[12])));
  84. powerLevel += Integer.valueOf(character[12]);
  85. }
  86. if (character.length > 13 && Misc.isInt(character[13])) {
  87. Axes.add(new PlayerStat(p, Integer.valueOf(character[13])));
  88. powerLevel += Integer.valueOf(character[13]);
  89. }
  90. if (character.length > 14 && Misc.isInt(character[14])) {
  91. Acrobatics.add(new PlayerStat(p, Integer.valueOf(character[14])));
  92. powerLevel += Integer.valueOf(character[14]);
  93. }
  94. if (character.length > 24 && Misc.isInt(character[24])) {
  95. Taming.add(new PlayerStat(p, Integer.valueOf(character[24])));
  96. powerLevel += Integer.valueOf(character[24]);
  97. }
  98. if (character.length > 34 && Misc.isInt(character[34])) {
  99. Fishing.add(new PlayerStat(p, Integer.valueOf(character[34])));
  100. powerLevel += Integer.valueOf(character[34]);
  101. }
  102. PowerLevel.add(new PlayerStat(p, powerLevel));
  103. }
  104. in.close();
  105. }
  106. catch (Exception e) {
  107. plugin.getLogger().severe(("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString()));
  108. }
  109. //Sort the leader boards
  110. SkillComparator c = new SkillComparator();
  111. Collections.sort(Mining, c);
  112. Collections.sort(WoodCutting, c);
  113. Collections.sort(Repair, c);
  114. Collections.sort(Unarmed, c);
  115. Collections.sort(Herbalism, c);
  116. Collections.sort(Excavation, c);
  117. Collections.sort(Archery, c);
  118. Collections.sort(Swords, c);
  119. Collections.sort(Axes, c);
  120. Collections.sort(Acrobatics, c);
  121. Collections.sort(Taming, c);
  122. Collections.sort(Fishing, c);
  123. Collections.sort(PowerLevel, c);
  124. //Write the leader board files
  125. PlayerStat[] a = new PlayerStat[1];
  126. leaderWrite(Mining.toArray(a), SkillType.MINING);
  127. leaderWrite(WoodCutting.toArray(a), SkillType.WOODCUTTING);
  128. leaderWrite(Repair.toArray(a), SkillType.REPAIR);
  129. leaderWrite(Unarmed.toArray(a), SkillType.UNARMED);
  130. leaderWrite(Herbalism.toArray(a), SkillType.HERBALISM);
  131. leaderWrite(Excavation.toArray(a), SkillType.EXCAVATION);
  132. leaderWrite(Archery.toArray(a), SkillType.ARCHERY);
  133. leaderWrite(Swords.toArray(a), SkillType.SWORDS);
  134. leaderWrite(Axes.toArray(a), SkillType.AXES);
  135. leaderWrite(Acrobatics.toArray(a), SkillType.ACROBATICS);
  136. leaderWrite(Taming.toArray(a), SkillType.TAMING);
  137. leaderWrite(Fishing.toArray(a), SkillType.FISHING);
  138. leaderWrite(PowerLevel.toArray(a), SkillType.ALL);
  139. }
  140. /**
  141. * Write to the leaderboards.
  142. *
  143. * @param ps Stats to write to the leaderboard
  144. * @param skillType Skill type to write the leaderboard of
  145. */
  146. private static void leaderWrite(PlayerStat[] ps, SkillType skillType) {
  147. String theLocation = leaderboardsDirectory + skillType.toString().toLowerCase() + ".mcmmo";
  148. File theDir = new File(theLocation);
  149. //CHECK IF THE FILE EXISTS
  150. if (!theDir.exists()) {
  151. FileWriter writer = null;
  152. try {
  153. writer = new FileWriter(theLocation);
  154. }
  155. catch (Exception e) {
  156. plugin.getLogger().severe(("Exception while creating " + theLocation + e.toString()));
  157. }
  158. finally {
  159. try {
  160. if (writer != null) {
  161. writer.close();
  162. }
  163. }
  164. catch (IOException e) {
  165. plugin.getLogger().severe("Exception while closing writer for " + theLocation + e.toString());
  166. }
  167. }
  168. }
  169. else {
  170. try {
  171. FileReader file = new FileReader(theLocation);
  172. BufferedReader in = new BufferedReader(file);
  173. StringBuilder writer = new StringBuilder();
  174. for (PlayerStat p : ps) {
  175. if (p.name.equals("$mcMMO_DummyInfo")) {
  176. continue;
  177. }
  178. if (p.statVal == 0) {
  179. continue;
  180. }
  181. writer.append(p.name + ":" + p.statVal);
  182. writer.append("\r\n");
  183. }
  184. in.close();
  185. FileWriter out = new FileWriter(theLocation);
  186. out.write(writer.toString());
  187. out.close();
  188. }
  189. catch (Exception e) {
  190. plugin.getLogger().severe("Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString());
  191. }
  192. }
  193. }
  194. /**
  195. * Retrieve leaderboard info.
  196. *
  197. * @param skillName Skill to retrieve info on.
  198. * @param pagenumber Which page in the leaderboards to retrieve
  199. * @return the requested leaderboard information
  200. */
  201. public static String[] retrieveInfo(String skillName, int pagenumber) {
  202. String theLocation = leaderboardsDirectory + skillName.toLowerCase() + ".mcmmo";
  203. try {
  204. FileReader file = new FileReader(theLocation);
  205. BufferedReader in = new BufferedReader(file);
  206. int destination;
  207. //How many lines to skip through
  208. if (pagenumber == 1) {
  209. destination = 0;
  210. }
  211. else {
  212. destination = (pagenumber * 10) - 9;
  213. }
  214. int x = 0; //how many lines we've gone through
  215. int y = 0; //going through the lines
  216. String line = "";
  217. String[] info = new String[10]; //what to return
  218. while ((line = in.readLine()) != null && y < 10) {
  219. x++;
  220. if (x >= destination && y < 10) {
  221. info[y] = line.toString();
  222. y++;
  223. }
  224. }
  225. in.close();
  226. return info;
  227. }
  228. catch (Exception e) {
  229. plugin.getLogger().severe("Exception while reading " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString());
  230. }
  231. return null; //Shouldn't get here
  232. }
  233. /**
  234. * Update the leaderboards.
  235. *
  236. * @param ps Stats to update the leaderboard with.
  237. * @param skillType Skill whose leaderboard is being updated.
  238. */
  239. public static void updateLeaderboard(PlayerStat ps, SkillType skillType) {
  240. if (Config.getInstance().getUseMySQL()) {
  241. return;
  242. }
  243. String theLocation = leaderboardsDirectory + skillType.toString().toLowerCase() + ".mcmmo";
  244. try {
  245. FileReader file = new FileReader(theLocation);
  246. BufferedReader in = new BufferedReader(file);
  247. StringBuilder writer = new StringBuilder();
  248. String line = "";
  249. Boolean inserted = false;
  250. while ((line = in.readLine()) != null) {
  251. //Insert the player into the line before it finds a smaller one
  252. if (Integer.valueOf(line.split(":")[1]) < ps.statVal && !inserted) {
  253. writer.append(ps.name + ":" + ps.statVal).append("\r\n");
  254. inserted = true;
  255. }
  256. //Write anything that isn't the player already in the file so we remove the duplicate
  257. if (!line.split(":")[0].equalsIgnoreCase(ps.name)) {
  258. writer.append(line).append("\r\n");
  259. }
  260. }
  261. if(!inserted) {
  262. writer.append(ps.name + ":" + ps.statVal).append("\r\n");
  263. }
  264. in.close();
  265. //Write the new file
  266. FileWriter out = new FileWriter(theLocation);
  267. out.write(writer.toString());
  268. out.close();
  269. }
  270. catch (Exception e) {
  271. plugin.getLogger().severe("Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString());
  272. }
  273. }
  274. private static class SkillComparator implements Comparator<PlayerStat> {
  275. @Override
  276. public int compare(PlayerStat o1, PlayerStat o2) {
  277. return (o2.statVal - o1.statVal);
  278. }
  279. }
  280. }