Leaderboard.java cleanup

This commit is contained in:
GJ 2012-03-07 09:05:08 -05:00
parent 653e06dd03
commit 3301fc3d9d

View File

@ -1,19 +1,21 @@
/* /*
This file is part of mcMMO. * Copyright (C) 2012 Matt 'The Yeti' Burnett & mcMMO Development
* Copyright (C) 2010-2011 'nossr50'
mcMMO is free software: you can redistribute it and/or modify *
it under the terms of the GNU General Public License as published by * This program is free software: you can redistribute it and/or modify
the Free Software Foundation, either version 3 of the License, or * it under the terms of the GNU General Public License as published by
(at your option) any later version. * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
mcMMO is distributed in the hope that it will be useful, *
but WITHOUT ANY WARRANTY; without even the implied warranty of * This program is distributed in the hope that it will be useful,
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
You should have received a copy of the GNU General Public License *
along with mcMMO. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.gmail.nossr50; package com.gmail.nossr50;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -30,16 +32,14 @@ import com.gmail.nossr50.datatypes.PlayerStat;
import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.datatypes.Tree; import com.gmail.nossr50.datatypes.Tree;
public class Leaderboard public class Leaderboard {
{ static String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
static String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users"; //$NON-NLS-1$ protected static final Logger log = Logger.getLogger("Minecraft");
protected static final Logger log = Logger.getLogger("Minecraft"); //$NON-NLS-1$
/* /**
* Read from the file * Create the leaderboards.
*/ */
public static void makeLeaderboards() public static void makeLeaderboards() {
{
//Make Trees //Make Trees
Tree Mining = new Tree(); Tree Mining = new Tree();
Tree WoodCutting = new Tree(); Tree WoodCutting = new Tree();
@ -57,219 +57,257 @@ public class Leaderboard
//Add Data To Trees //Add Data To Trees
try { try {
//Open the user file
FileReader file = new FileReader(location); FileReader file = new FileReader(location);
BufferedReader in = new BufferedReader(file); BufferedReader in = new BufferedReader(file);
String line = ""; //$NON-NLS-1$ String line = "";
ArrayList<String> players = new ArrayList<String>(); ArrayList<String> players = new ArrayList<String>();
while((line = in.readLine()) != null)
{ while (in.readLine() != null) {
String[] character = line.split(":"); //$NON-NLS-1$ line = in.readLine();
String[] character = line.split(":");
String p = character[0]; String p = character[0];
int powerLevel = 0;
//Prevent the same player from being added multiple times //Prevent the same player from being added multiple times
if(players.contains(p)) if (players.contains(p)) {
continue; continue;
else }
else {
players.add(p); players.add(p);
}
int Plvl = 0; if (character.length > 1 && m.isInt(character[1])) {
if(character.length > 1 && m.isInt(character[1]))
{
Mining.add(p, Integer.valueOf(character[1])); Mining.add(p, Integer.valueOf(character[1]));
Plvl += Integer.valueOf(character[1]); powerLevel += Integer.valueOf(character[1]);
}
if(character.length > 5 && m.isInt(character[5])){
WoodCutting.add(p, Integer.valueOf(character[5]));
Plvl += Integer.valueOf(character[5]);
}
if(character.length > 7 && m.isInt(character[7])){
Repair.add(p, Integer.valueOf(character[7]));
Plvl += Integer.valueOf(character[7]);
}
if(character.length > 8 && m.isInt(character[8])){
Unarmed.add(p, Integer.valueOf(character[8]));
Plvl += Integer.valueOf(character[8]);
}
if(character.length > 9 && m.isInt(character[9])){
Herbalism.add(p, Integer.valueOf(character[9]));
Plvl += Integer.valueOf(character[9]);
}
if(character.length > 10 && m.isInt(character[10])){
Excavation.add(p, Integer.valueOf(character[10]));
Plvl += Integer.valueOf(character[10]);
}
if(character.length > 11 && m.isInt(character[11])){
Archery.add(p, Integer.valueOf(character[11]));
Plvl += Integer.valueOf(character[11]);
}
if(character.length > 12 && m.isInt(character[12])){
Swords.add(p, Integer.valueOf(character[12]));
Plvl += Integer.valueOf(character[12]);
}
if(character.length > 13 && m.isInt(character[13])){
Axes.add(p, Integer.valueOf(character[13]));
Plvl += Integer.valueOf(character[13]);
}
if(character.length > 14 && m.isInt(character[14])){
Acrobatics.add(p, Integer.valueOf(character[14]));
Plvl += Integer.valueOf(character[14]);
}
if(character.length > 24 && m.isInt(character[24])){
Taming.add(p, Integer.valueOf(character[24]));
Plvl += Integer.valueOf(character[24]);
}
if(character.length > 34 && m.isInt(character[34]))
{
Fishing.add(p, Integer.valueOf(character[34]));
Plvl += Integer.valueOf(character[34]);
} }
PowerLevel.add(p, Plvl); if (character.length > 5 && m.isInt(character[5])) {
WoodCutting.add(p, Integer.valueOf(character[5]));
powerLevel += Integer.valueOf(character[5]);
}
if (character.length > 7 && m.isInt(character[7])) {
Repair.add(p, Integer.valueOf(character[7]));
powerLevel += Integer.valueOf(character[7]);
}
if (character.length > 8 && m.isInt(character[8])) {
Unarmed.add(p, Integer.valueOf(character[8]));
powerLevel += Integer.valueOf(character[8]);
}
if (character.length > 9 && m.isInt(character[9])) {
Herbalism.add(p, Integer.valueOf(character[9]));
powerLevel += Integer.valueOf(character[9]);
}
if (character.length > 10 && m.isInt(character[10])) {
Excavation.add(p, Integer.valueOf(character[10]));
powerLevel += Integer.valueOf(character[10]);
}
if (character.length > 11 && m.isInt(character[11])) {
Archery.add(p, Integer.valueOf(character[11]));
powerLevel += Integer.valueOf(character[11]);
}
if (character.length > 12 && m.isInt(character[12])) {
Swords.add(p, Integer.valueOf(character[12]));
powerLevel += Integer.valueOf(character[12]);
}
if (character.length > 13 && m.isInt(character[13])) {
Axes.add(p, Integer.valueOf(character[13]));
powerLevel += Integer.valueOf(character[13]);
}
if (character.length > 14 && m.isInt(character[14])) {
Acrobatics.add(p, Integer.valueOf(character[14]));
powerLevel += Integer.valueOf(character[14]);
}
if (character.length > 24 && m.isInt(character[24])) {
Taming.add(p, Integer.valueOf(character[24]));
powerLevel += Integer.valueOf(character[24]);
}
if (character.length > 34 && m.isInt(character[34])) {
Fishing.add(p, Integer.valueOf(character[34]));
powerLevel += Integer.valueOf(character[34]);
}
PowerLevel.add(p, powerLevel);
} }
in.close(); in.close();
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading " //$NON-NLS-1$
+ location + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$
} }
catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
}
//Write the leader board files //Write the leader board files
leaderWrite(Mining.inOrder(), SkillType.MINING); //$NON-NLS-1$ leaderWrite(Mining.inOrder(), SkillType.MINING);
leaderWrite(WoodCutting.inOrder(), SkillType.WOODCUTTING); //$NON-NLS-1$ leaderWrite(WoodCutting.inOrder(), SkillType.WOODCUTTING);
leaderWrite(Repair.inOrder(), SkillType.REPAIR); //$NON-NLS-1$ leaderWrite(Repair.inOrder(), SkillType.REPAIR);
leaderWrite(Unarmed.inOrder(), SkillType.UNARMED); //$NON-NLS-1$ leaderWrite(Unarmed.inOrder(), SkillType.UNARMED);
leaderWrite(Herbalism.inOrder(), SkillType.HERBALISM); //$NON-NLS-1$ leaderWrite(Herbalism.inOrder(), SkillType.HERBALISM);
leaderWrite(Excavation.inOrder(), SkillType.EXCAVATION); //$NON-NLS-1$ leaderWrite(Excavation.inOrder(), SkillType.EXCAVATION);
leaderWrite(Archery.inOrder(), SkillType.ARCHERY); //$NON-NLS-1$ leaderWrite(Archery.inOrder(), SkillType.ARCHERY);
leaderWrite(Swords.inOrder(), SkillType.SWORDS); //$NON-NLS-1$ leaderWrite(Swords.inOrder(), SkillType.SWORDS);
leaderWrite(Axes.inOrder(), SkillType.AXES); //$NON-NLS-1$ leaderWrite(Axes.inOrder(), SkillType.AXES);
leaderWrite(Acrobatics.inOrder(), SkillType.ACROBATICS); //$NON-NLS-1$ leaderWrite(Acrobatics.inOrder(), SkillType.ACROBATICS);
leaderWrite(Taming.inOrder(), SkillType.TAMING); //$NON-NLS-1$ leaderWrite(Taming.inOrder(), SkillType.TAMING);
leaderWrite(Fishing.inOrder(), SkillType.FISHING); //$NON-NLS-1$ leaderWrite(Fishing.inOrder(), SkillType.FISHING);
leaderWrite(PowerLevel.inOrder(), SkillType.ALL); //$NON-NLS-1$ leaderWrite(PowerLevel.inOrder(), SkillType.ALL);
} }
public static void leaderWrite(PlayerStat[] ps, SkillType skillType)
{ /**
String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$ * Write to the leaderboards.
//CHECK IF THE FILE EXISTS *
* @param ps Stats to write to the leaderboard
* @param skillType Skill type to write the leaderboard of
*/
private static void leaderWrite(PlayerStat[] ps, SkillType skillType) {
String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo";
File theDir = new File(theLocation); File theDir = new File(theLocation);
if(!theDir.exists())
{ //CHECK IF THE FILE EXISTS
//properties = new PropertiesFile(location); if (!theDir.exists()) {
FileWriter writer = null; FileWriter writer = null;
try { try {
writer = new FileWriter(theLocation); writer = new FileWriter(theLocation);
} catch (Exception e) { }
log.log(Level.SEVERE, "Exception while creating " + theLocation, e); //$NON-NLS-1$ catch (Exception e) {
} finally { log.log(Level.SEVERE, "Exception while creating " + theLocation, e);
}
finally {
try { try {
if (writer != null) { if (writer != null) {
writer.close(); writer.close();
} }
} catch (IOException e) { }
log.log(Level.SEVERE, "Exception while closing writer for " + theLocation, e); //$NON-NLS-1$ catch (IOException e) {
log.log(Level.SEVERE, "Exception while closing writer for " + theLocation, e);
} }
} }
} else { }
else {
try { try {
FileReader file = new FileReader(theLocation); FileReader file = new FileReader(theLocation);
//HERP
BufferedReader in = new BufferedReader(file); BufferedReader in = new BufferedReader(file);
StringBuilder writer = new StringBuilder(); StringBuilder writer = new StringBuilder();
for(PlayerStat p : ps) for (PlayerStat p : ps) {
{ if (p.name.equals("$mcMMO_DummyInfo")) {
if(p.name.equals("$mcMMO_DummyInfo")) //$NON-NLS-1$
continue; continue;
if(p.statVal == 0) }
if (p.statVal == 0) {
continue; continue;
writer.append(p.name + ":" + p.statVal); //$NON-NLS-1$ }
writer.append("\r\n"); //$NON-NLS-1$
writer.append(p.name + ":" + p.statVal);
writer.append("\r\n");
} }
in.close(); in.close();
//Write the new file
FileWriter out = new FileWriter(theLocation); FileWriter out = new FileWriter(theLocation);
out.write(writer.toString()); out.write(writer.toString());
out.close(); out.close();
} catch (Exception e) { }
log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$ //$NON-NLS-2$ catch (Exception e) {
log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e);
} }
} }
//Create/open the file
//Loop through backward writing each player
//Close the file
} }
public static String[] retrieveInfo(String skillName, int pagenumber) /**
{ * Retrieve leaderboard info.
*
* @param skillName Skill to retrieve info on.
* @param pagenumber Which page in the leaderboards to retrieve
* @return the requested leaderboard information
*/
public static String[] retrieveInfo(String skillName, int pagenumber) {
String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillName + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$ String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillName + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$
try { try {
FileReader file = new FileReader(theLocation); FileReader file = new FileReader(theLocation);
BufferedReader in = new BufferedReader(file); BufferedReader in = new BufferedReader(file);
int destination = (pagenumber - 1) * 10; //How many lines to skip through int destination = (pagenumber - 1) * 10; //How many lines to skip through
int x = 0; //how many lines we've gone through int x = 0; //how many lines we've gone through
int y = 0; //going through the lines int y = 0; //going through the lines
String line = ""; //$NON-NLS-1$ String line = "";
String[] info = new String[10]; //what to return String[] info = new String[10]; //what to return
while((line = in.readLine()) != null && y < 10)
{ while (in.readLine() != null && y < 10) {
line = in.readLine();
x++; x++;
if(x >= destination && y < 10){
if (x >= destination && y < 10) {
info[y] = line.toString(); info[y] = line.toString();
y++; y++;
} }
} }
in.close(); in.close();
return info; return info;
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading " //$NON-NLS-1$
+ theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$
} }
catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading " + theLocation + " (Are you sure you formatted it correctly?)", e);
}
return null; //Shouldn't get here return null; //Shouldn't get here
} }
public static void updateLeaderboard(PlayerStat ps, SkillType skillType)
{ /**
if(LoadProperties.useMySQL) * Update the leaderboards.
*
* @param ps Stats to update the leaderboard with.
* @param skillType Skill whose leaderboard is being updated.
*/
public static void updateLeaderboard(PlayerStat ps, SkillType skillType) {
if (LoadProperties.useMySQL) {
return; return;
String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$ }
String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo";
try { try {
//Open the file
FileReader file = new FileReader(theLocation); FileReader file = new FileReader(theLocation);
BufferedReader in = new BufferedReader(file); BufferedReader in = new BufferedReader(file);
StringBuilder writer = new StringBuilder(); StringBuilder writer = new StringBuilder();
String line = ""; //$NON-NLS-1$ String line = "";
Boolean inserted = false; Boolean inserted = false;
//While not at the end of the file
while((line = in.readLine()) != null) while (in.readLine() != null) {
{ line = in.readLine();
//Insert the player into the line before it finds a smaller one //Insert the player into the line before it finds a smaller one
if(Integer.valueOf(line.split(":")[1]) < ps.statVal && !inserted) //$NON-NLS-1$ if (Integer.valueOf(line.split(":")[1]) < ps.statVal && !inserted) {
{ writer.append(ps.name + ":" + ps.statVal).append("\r\n");
writer.append(ps.name + ":" + ps.statVal).append("\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
inserted = true; inserted = true;
} }
//Write anything that isn't the player already in the file so we remove the duplicate //Write anything that isn't the player already in the file so we remove the duplicate
if(!line.split(":")[0].equalsIgnoreCase(ps.name)) //$NON-NLS-1$ if (!line.split(":")[0].equalsIgnoreCase(ps.name)) {
{ writer.append(line).append("\r\n");
writer.append(line).append("\r\n"); //$NON-NLS-1$
} }
} }
if(!inserted) if(!inserted) {
{ writer.append(ps.name + ":" + ps.statVal).append("\r\n");
writer.append(ps.name + ":" + ps.statVal).append("\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
} }
in.close(); in.close();
//Write the new file //Write the new file
FileWriter out = new FileWriter(theLocation); FileWriter out = new FileWriter(theLocation);
out.write(writer.toString()); out.write(writer.toString());
out.close(); out.close();
} catch (Exception e) { }
log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$ //$NON-NLS-2$ catch (Exception e) {
log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e);
} }
} }
} }