Leaderboard.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50;
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileReader;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.gmail.nossr50.config.LoadProperties;
  24. import com.gmail.nossr50.datatypes.PlayerStat;
  25. import com.gmail.nossr50.datatypes.SkillType;
  26. import com.gmail.nossr50.datatypes.Tree;
  27. public class Leaderboard
  28. {
  29. static String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users"; //$NON-NLS-1$
  30. protected static final Logger log = Logger.getLogger("Minecraft"); //$NON-NLS-1$
  31. /*
  32. * Read from the file
  33. */
  34. public static void makeLeaderboards()
  35. {
  36. //Make Trees
  37. Tree Mining = new Tree();
  38. Tree WoodCutting = new Tree();
  39. Tree Herbalism = new Tree();
  40. Tree Excavation = new Tree();
  41. Tree Acrobatics = new Tree();
  42. Tree Repair = new Tree();
  43. Tree Swords = new Tree();
  44. Tree Axes = new Tree();
  45. Tree Archery = new Tree();
  46. Tree Unarmed = new Tree();
  47. Tree Taming = new Tree();
  48. Tree Fishing = new Tree();
  49. Tree PowerLevel = new Tree();
  50. //Add Data To Trees
  51. try {
  52. //Open the user file
  53. FileReader file = new FileReader(location);
  54. BufferedReader in = new BufferedReader(file);
  55. String line = ""; //$NON-NLS-1$
  56. ArrayList<String> players = new ArrayList<String>();
  57. while((line = in.readLine()) != null)
  58. {
  59. String[] character = line.split(":"); //$NON-NLS-1$
  60. String p = character[0];
  61. //Prevent the same player from being added multiple times
  62. if(players.contains(p))
  63. continue;
  64. else
  65. players.add(p);
  66. int Plvl = 0;
  67. if(character.length > 1 && m.isInt(character[1]))
  68. {
  69. Mining.add(p, Integer.valueOf(character[1]));
  70. Plvl += Integer.valueOf(character[1]);
  71. }
  72. if(character.length > 5 && m.isInt(character[5])){
  73. WoodCutting.add(p, Integer.valueOf(character[5]));
  74. Plvl += Integer.valueOf(character[5]);
  75. }
  76. if(character.length > 7 && m.isInt(character[7])){
  77. Repair.add(p, Integer.valueOf(character[7]));
  78. Plvl += Integer.valueOf(character[7]);
  79. }
  80. if(character.length > 8 && m.isInt(character[8])){
  81. Unarmed.add(p, Integer.valueOf(character[8]));
  82. Plvl += Integer.valueOf(character[8]);
  83. }
  84. if(character.length > 9 && m.isInt(character[9])){
  85. Herbalism.add(p, Integer.valueOf(character[9]));
  86. Plvl += Integer.valueOf(character[9]);
  87. }
  88. if(character.length > 10 && m.isInt(character[10])){
  89. Excavation.add(p, Integer.valueOf(character[10]));
  90. Plvl += Integer.valueOf(character[10]);
  91. }
  92. if(character.length > 11 && m.isInt(character[11])){
  93. Archery.add(p, Integer.valueOf(character[11]));
  94. Plvl += Integer.valueOf(character[11]);
  95. }
  96. if(character.length > 12 && m.isInt(character[12])){
  97. Swords.add(p, Integer.valueOf(character[12]));
  98. Plvl += Integer.valueOf(character[12]);
  99. }
  100. if(character.length > 13 && m.isInt(character[13])){
  101. Axes.add(p, Integer.valueOf(character[13]));
  102. Plvl += Integer.valueOf(character[13]);
  103. }
  104. if(character.length > 14 && m.isInt(character[14])){
  105. Acrobatics.add(p, Integer.valueOf(character[14]));
  106. Plvl += Integer.valueOf(character[14]);
  107. }
  108. if(character.length > 24 && m.isInt(character[24])){
  109. Taming.add(p, Integer.valueOf(character[24]));
  110. Plvl += Integer.valueOf(character[24]);
  111. }
  112. if(character.length > 34 && m.isInt(character[34]))
  113. {
  114. Fishing.add(p, Integer.valueOf(character[34]));
  115. Plvl += Integer.valueOf(character[34]);
  116. }
  117. PowerLevel.add(p, Plvl);
  118. }
  119. in.close();
  120. } catch (Exception e) {
  121. log.log(Level.SEVERE, "Exception while reading " //$NON-NLS-1$
  122. + location + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$
  123. }
  124. //Write the leader board files
  125. leaderWrite(Mining.inOrder(), SkillType.MINING); //$NON-NLS-1$
  126. leaderWrite(WoodCutting.inOrder(), SkillType.WOODCUTTING); //$NON-NLS-1$
  127. leaderWrite(Repair.inOrder(), SkillType.REPAIR); //$NON-NLS-1$
  128. leaderWrite(Unarmed.inOrder(), SkillType.UNARMED); //$NON-NLS-1$
  129. leaderWrite(Herbalism.inOrder(), SkillType.HERBALISM); //$NON-NLS-1$
  130. leaderWrite(Excavation.inOrder(), SkillType.EXCAVATION); //$NON-NLS-1$
  131. leaderWrite(Archery.inOrder(), SkillType.ARCHERY); //$NON-NLS-1$
  132. leaderWrite(Swords.inOrder(), SkillType.SWORDS); //$NON-NLS-1$
  133. leaderWrite(Axes.inOrder(), SkillType.AXES); //$NON-NLS-1$
  134. leaderWrite(Acrobatics.inOrder(), SkillType.ACROBATICS); //$NON-NLS-1$
  135. leaderWrite(Taming.inOrder(), SkillType.TAMING); //$NON-NLS-1$
  136. leaderWrite(Fishing.inOrder(), SkillType.FISHING); //$NON-NLS-1$
  137. leaderWrite(PowerLevel.inOrder(), SkillType.ALL); //$NON-NLS-1$
  138. }
  139. public static void leaderWrite(PlayerStat[] ps, SkillType skillType)
  140. {
  141. String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$
  142. //CHECK IF THE FILE EXISTS
  143. File theDir = new File(theLocation);
  144. if(!theDir.exists())
  145. {
  146. //properties = new PropertiesFile(location);
  147. FileWriter writer = null;
  148. try {
  149. writer = new FileWriter(theLocation);
  150. } catch (Exception e) {
  151. log.log(Level.SEVERE, "Exception while creating " + theLocation, e); //$NON-NLS-1$
  152. } finally {
  153. try {
  154. if (writer != null) {
  155. writer.close();
  156. }
  157. } catch (IOException e) {
  158. log.log(Level.SEVERE, "Exception while closing writer for " + theLocation, e); //$NON-NLS-1$
  159. }
  160. }
  161. } else {
  162. try {
  163. FileReader file = new FileReader(theLocation);
  164. //HERP
  165. BufferedReader in = new BufferedReader(file);
  166. StringBuilder writer = new StringBuilder();
  167. for(PlayerStat p : ps)
  168. {
  169. if(p.name.equals("$mcMMO_DummyInfo")) //$NON-NLS-1$
  170. continue;
  171. if(p.statVal == 0)
  172. continue;
  173. writer.append(p.name + ":" + p.statVal); //$NON-NLS-1$
  174. writer.append("\r\n"); //$NON-NLS-1$
  175. }
  176. in.close();
  177. //Write the new file
  178. FileWriter out = new FileWriter(theLocation);
  179. out.write(writer.toString());
  180. out.close();
  181. } catch (Exception e) {
  182. log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$ //$NON-NLS-2$
  183. }
  184. }
  185. //Create/open the file
  186. //Loop through backward writing each player
  187. //Close the file
  188. }
  189. public static String[] retrieveInfo(String skillName, int pagenumber)
  190. {
  191. String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillName + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$
  192. try {
  193. FileReader file = new FileReader(theLocation);
  194. BufferedReader in = new BufferedReader(file);
  195. int destination = (pagenumber - 1) * 10; //How many lines to skip through
  196. int x = 0; //how many lines we've gone through
  197. int y = 0; //going through the lines
  198. String line = ""; //$NON-NLS-1$
  199. String[] info = new String[10]; //what to return
  200. while((line = in.readLine()) != null && y < 10)
  201. {
  202. x++;
  203. if(x >= destination && y < 10){
  204. info[y] = line.toString();
  205. y++;
  206. }
  207. }
  208. in.close();
  209. return info;
  210. } catch (Exception e) {
  211. log.log(Level.SEVERE, "Exception while reading " //$NON-NLS-1$
  212. + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$
  213. }
  214. return null; //Shouldn't get here
  215. }
  216. public static void updateLeaderboard(PlayerStat ps, SkillType skillType)
  217. {
  218. if(LoadProperties.useMySQL)
  219. return;
  220. String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$
  221. try {
  222. //Open the file
  223. FileReader file = new FileReader(theLocation);
  224. BufferedReader in = new BufferedReader(file);
  225. StringBuilder writer = new StringBuilder();
  226. String line = ""; //$NON-NLS-1$
  227. Boolean inserted = false;
  228. //While not at the end of the file
  229. while((line = in.readLine()) != null)
  230. {
  231. //Insert the player into the line before it finds a smaller one
  232. if(Integer.valueOf(line.split(":")[1]) < ps.statVal && !inserted) //$NON-NLS-1$
  233. {
  234. writer.append(ps.name + ":" + ps.statVal).append("\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
  235. inserted = true;
  236. }
  237. //Write anything that isn't the player already in the file so we remove the duplicate
  238. if(!line.split(":")[0].equalsIgnoreCase(ps.name)) //$NON-NLS-1$
  239. {
  240. writer.append(line).append("\r\n"); //$NON-NLS-1$
  241. }
  242. }
  243. if(!inserted)
  244. {
  245. writer.append(ps.name + ":" + ps.statVal).append("\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
  246. }
  247. in.close();
  248. //Write the new file
  249. FileWriter out = new FileWriter(theLocation);
  250. out.write(writer.toString());
  251. out.close();
  252. } catch (Exception e) {
  253. log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$ //$NON-NLS-2$
  254. }
  255. }
  256. }