mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-06-26 18:54:44 +02:00
Use a single manager to handle our databases.
This commit is contained in:
@ -1,63 +1,79 @@
|
||||
package com.gmail.nossr50.database;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.util.Misc;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.database.PlayerStat;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
|
||||
public class DatabaseManager {
|
||||
private final mcMMO plugin;
|
||||
private final boolean isUsingSQL;
|
||||
private File usersFile;
|
||||
public interface DatabaseManager {
|
||||
public final long PURGE_TIME = 2630000000L * Config.getInstance().getOldUsersCutoff();
|
||||
|
||||
public DatabaseManager(final mcMMO plugin, final boolean isUsingSQL) {
|
||||
this.plugin = plugin;
|
||||
this.isUsingSQL = isUsingSQL;
|
||||
/**
|
||||
* Purge users with 0 power level from the database.
|
||||
*/
|
||||
public void purgePowerlessUsers();
|
||||
|
||||
if (isUsingSQL) {
|
||||
SQLDatabaseManager.checkConnected();
|
||||
SQLDatabaseManager.createStructure();
|
||||
}
|
||||
else {
|
||||
usersFile = new File(mcMMO.getUsersFilePath());
|
||||
createFlatfileDatabase();
|
||||
FlatfileDatabaseManager.updateLeaderboards();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Purge users who haven't logged on in over a certain time frame from the database.
|
||||
*/
|
||||
public void purgeOldUsers();
|
||||
|
||||
public void purgePowerlessUsers() {
|
||||
plugin.getLogger().info("Purging powerless users...");
|
||||
plugin.getLogger().info("Purged " + (isUsingSQL ? SQLDatabaseManager.purgePowerlessSQL() : FlatfileDatabaseManager.purgePowerlessFlatfile()) + " users from the database.");
|
||||
}
|
||||
/**
|
||||
* Remove a user from the database.
|
||||
*
|
||||
* @param playerName The name of the user to remove
|
||||
* @return true if the user was successfully removed, false otherwise
|
||||
*/
|
||||
public boolean removeUser(String playerName);
|
||||
|
||||
public void purgeOldUsers() {
|
||||
plugin.getLogger().info("Purging old users...");
|
||||
plugin.getLogger().info("Purged " + (isUsingSQL ? SQLDatabaseManager.purgeOldSQL() : FlatfileDatabaseManager.removeOldFlatfileUsers()) + " users from the database.");
|
||||
}
|
||||
/**
|
||||
* Save a user to the database.
|
||||
*
|
||||
* @param profile The profile of the player to save
|
||||
*/
|
||||
public void saveUser(PlayerProfile profile);
|
||||
|
||||
public boolean removeUser(String playerName) {
|
||||
if (isUsingSQL ? SQLDatabaseManager.removeUserSQL(playerName) : FlatfileDatabaseManager.removeFlatFileUser(playerName)) {
|
||||
Misc.profileCleanup(playerName);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Retrieve leaderboard info.
|
||||
*
|
||||
* @param skillName The skill to retrieve info on
|
||||
* @param pageNumber Which page in the leaderboards to retrieve
|
||||
* @param statsPerPage The number of stats per page
|
||||
* @return the requested leaderboard information
|
||||
*/
|
||||
public List<PlayerStat> readLeaderboard(String skillName, int pageNumber, int statsPerPage);
|
||||
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Retrieve rank info.
|
||||
*
|
||||
* @param playerName The name of the user to retrieve the rankings for
|
||||
* @return the requested rank information
|
||||
*/
|
||||
public Map<String, Integer> readRank(String playerName);
|
||||
|
||||
private void createFlatfileDatabase() {
|
||||
if (usersFile.exists()) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Add a new user to the database.
|
||||
*
|
||||
* @param playerName The name of the player to be added to the database
|
||||
*/
|
||||
public void newUser(String playerName);
|
||||
|
||||
usersFile.getParentFile().mkdir();
|
||||
/**
|
||||
* Load a player from the database.
|
||||
*
|
||||
* @param playerName The name of the player to load from the database
|
||||
* @return The player's data
|
||||
*/
|
||||
public List<String> loadPlayerData(String playerName);
|
||||
|
||||
try {
|
||||
plugin.debug("Creating mcmmo.users file...");
|
||||
new File(mcMMO.getUsersFilePath()).createNewFile();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Convert player data to a different storage format.
|
||||
*
|
||||
* @param data The player's data
|
||||
* @return true if the conversion was successful, false otherwise
|
||||
* @throws Exception
|
||||
*/
|
||||
public boolean convert(String[] data) throws Exception;
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.gmail.nossr50.database;
|
||||
|
||||
import com.gmail.nossr50.config.Config;
|
||||
|
||||
public class DatabaseManagerFactory {
|
||||
public static DatabaseManager getDatabaseManager() {
|
||||
return Config.getInstance().getUseMySQL() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package com.gmail.nossr50.database;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
@ -11,26 +13,376 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.MobHealthbarType;
|
||||
import com.gmail.nossr50.datatypes.database.PlayerStat;
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
import com.gmail.nossr50.datatypes.skills.AbilityType;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||
import com.gmail.nossr50.util.StringUtils;
|
||||
import com.gmail.nossr50.datatypes.spout.huds.HudType;
|
||||
import com.gmail.nossr50.util.Misc;
|
||||
import com.gmail.nossr50.util.player.UserManager;
|
||||
|
||||
public final class FlatfileDatabaseManager {
|
||||
private static HashMap<SkillType, List<PlayerStat>> playerStatHash = new HashMap<SkillType, List<PlayerStat>>();
|
||||
private static List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
|
||||
private static long lastUpdate = 0;
|
||||
public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
private final HashMap<SkillType, List<PlayerStat>> playerStatHash = new HashMap<SkillType, List<PlayerStat>>();
|
||||
private final List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
|
||||
private long lastUpdate = 0;
|
||||
|
||||
private static final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
|
||||
private static final long ONE_MONTH = 2630000000L;
|
||||
private final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
|
||||
private final File usersFile;
|
||||
|
||||
private FlatfileDatabaseManager() {}
|
||||
public FlatfileDatabaseManager() {
|
||||
usersFile = new File(mcMMO.getUsersFilePath());
|
||||
createDatabase();
|
||||
updateLeaderboards();
|
||||
}
|
||||
|
||||
public void purgePowerlessUsers() {
|
||||
int purgedUsers = 0;
|
||||
|
||||
mcMMO.p.getLogger().info("Purging powerless users...");
|
||||
|
||||
for (PlayerStat stat : powerLevels) {
|
||||
if (stat.statVal == 0 && mcMMO.p.getServer().getPlayerExact(stat.name) == null && removeUser(stat.name)) {
|
||||
purgedUsers++;
|
||||
}
|
||||
}
|
||||
|
||||
mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
|
||||
}
|
||||
|
||||
public void purgeOldUsers() {
|
||||
int removedPlayers = 0;
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
mcMMO.p.getLogger().info("Purging old users...");
|
||||
|
||||
for (McMMOPlayer mcMMOPlayer : UserManager.getPlayers().values()) {
|
||||
Player player = mcMMOPlayer.getPlayer();
|
||||
|
||||
if (currentTime - player.getLastPlayed() > PURGE_TIME && removeUser(player.getName())) {
|
||||
removedPlayers++;
|
||||
}
|
||||
}
|
||||
|
||||
mcMMO.p.getLogger().info("Purged " + removedPlayers + " users from the database.");
|
||||
}
|
||||
|
||||
public boolean removeUser(String playerName) {
|
||||
boolean worked = false;
|
||||
|
||||
BufferedReader in = null;
|
||||
FileWriter out = null;
|
||||
String usersFilePath = mcMMO.getUsersFilePath();
|
||||
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(usersFilePath));
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line = "";
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
// Write out the same file but when we get to the player we want to remove, we skip his line.
|
||||
if (!worked && line.split(":")[0].equalsIgnoreCase(playerName)) {
|
||||
mcMMO.p.getLogger().info("User found, removing...");
|
||||
worked = true;
|
||||
continue; // Skip the player
|
||||
}
|
||||
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
|
||||
out = new FileWriter(usersFilePath); // Write out the new file
|
||||
out.write(writer.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Misc.profileCleanup(playerName);
|
||||
|
||||
return worked;
|
||||
}
|
||||
|
||||
public void saveUser(PlayerProfile profile) {
|
||||
String playerName = profile.getPlayerName();
|
||||
|
||||
BufferedReader in = null;
|
||||
FileWriter out = null;
|
||||
String usersFilePath = mcMMO.getUsersFilePath();
|
||||
|
||||
try {
|
||||
// Open the file
|
||||
in = new BufferedReader(new FileReader(usersFilePath));
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line;
|
||||
|
||||
// While not at the end of the file
|
||||
while ((line = in.readLine()) != null) {
|
||||
// Read the line in and copy it to the output it's not the player we want to edit
|
||||
if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
else {
|
||||
// Otherwise write the new player information
|
||||
writer.append(playerName).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.MINING)).append(":");
|
||||
writer.append(":");
|
||||
writer.append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.MINING)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.WOODCUTTING)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.WOODCUTTING)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.REPAIR)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.UNARMED)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.HERBALISM)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.EXCAVATION)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.ARCHERY)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.SWORDS)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.AXES)).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.ACROBATICS)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.REPAIR)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.UNARMED)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.HERBALISM)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.EXCAVATION)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.ARCHERY)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.SWORDS)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.AXES)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.ACROBATICS)).append(":");
|
||||
writer.append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.TAMING)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.TAMING)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.BERSERK)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.GIGA_DRILL_BREAKER)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.TREE_FELLER)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.GREEN_TERRA)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.SERRATED_STRIKES)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.SKULL_SPLITTER)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.SUPER_BREAKER)).append(":");
|
||||
HudType hudType = profile.getHudType();
|
||||
writer.append(hudType == null ? "STANDARD" : hudType.toString()).append(":");
|
||||
writer.append(profile.getSkillLevel(SkillType.FISHING)).append(":");
|
||||
writer.append(profile.getSkillXpLevel(SkillType.FISHING)).append(":");
|
||||
writer.append((int) profile.getSkillDATS(AbilityType.BLAST_MINING)).append(":");
|
||||
writer.append(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR).append(":");
|
||||
MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
|
||||
writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":");
|
||||
writer.append("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Write the new file
|
||||
out = new FileWriter(usersFilePath);
|
||||
out.write(writer.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<PlayerStat> readLeaderboard(String skillName, int pageNumber, int statsPerPage) {
|
||||
updateLeaderboards();
|
||||
List<PlayerStat> statsList = skillName.equalsIgnoreCase("all") ? powerLevels : playerStatHash.get(SkillType.getSkill(skillName));
|
||||
int fromIndex = (Math.max(pageNumber, 1) - 1) * statsPerPage;
|
||||
|
||||
return statsList.subList(Math.min(fromIndex, statsList.size()), Math.min(fromIndex + statsPerPage, statsList.size()));
|
||||
}
|
||||
|
||||
public Map<String, Integer> readRank(String playerName) {
|
||||
updateLeaderboards();
|
||||
|
||||
Map<String, Integer> skills = new HashMap<String, Integer>();
|
||||
|
||||
for (SkillType skill : SkillType.nonChildSkills()) {
|
||||
skills.put(skill.name(), getPlayerRank(playerName, playerStatHash.get(skill)));
|
||||
}
|
||||
|
||||
skills.put("ALL", getPlayerRank(playerName, powerLevels));
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
public void newUser(String playerName) {
|
||||
try {
|
||||
// Open the file to write the player
|
||||
BufferedWriter out = new BufferedWriter(new FileWriter(mcMMO.getUsersFilePath(), true));
|
||||
|
||||
// Add the player to the end
|
||||
out.append(playerName).append(":");
|
||||
out.append("0:"); // Mining
|
||||
out.append(":");
|
||||
out.append(":");
|
||||
out.append("0:"); // Xp
|
||||
out.append("0:"); // Woodcutting
|
||||
out.append("0:"); // WoodCuttingXp
|
||||
out.append("0:"); // Repair
|
||||
out.append("0:"); // Unarmed
|
||||
out.append("0:"); // Herbalism
|
||||
out.append("0:"); // Excavation
|
||||
out.append("0:"); // Archery
|
||||
out.append("0:"); // Swords
|
||||
out.append("0:"); // Axes
|
||||
out.append("0:"); // Acrobatics
|
||||
out.append("0:"); // RepairXp
|
||||
out.append("0:"); // UnarmedXp
|
||||
out.append("0:"); // HerbalismXp
|
||||
out.append("0:"); // ExcavationXp
|
||||
out.append("0:"); // ArcheryXp
|
||||
out.append("0:"); // SwordsXp
|
||||
out.append("0:"); // AxesXp
|
||||
out.append("0:"); // AcrobaticsXp
|
||||
out.append(":");
|
||||
out.append("0:"); // Taming
|
||||
out.append("0:"); // TamingXp
|
||||
out.append("0:"); // DATS
|
||||
out.append("0:"); // DATS
|
||||
out.append("0:"); // DATS
|
||||
out.append("0:"); // DATS
|
||||
out.append("0:"); // DATS
|
||||
out.append("0:"); // DATS
|
||||
out.append("0:"); // DATS
|
||||
out.append("STANDARD").append(":"); // HUD
|
||||
out.append("0:"); // Fishing
|
||||
out.append("0:"); // FishingXp
|
||||
out.append("0:"); // Blast Mining
|
||||
out.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":"); // LastLogin
|
||||
out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD
|
||||
|
||||
// Add more in the same format as the line above
|
||||
|
||||
out.newLine();
|
||||
out.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> loadPlayerData(String playerName) {
|
||||
List<String> playerData = new ArrayList<String>();
|
||||
try {
|
||||
// Open the user file
|
||||
FileReader file = new FileReader(mcMMO.getUsersFilePath());
|
||||
BufferedReader in = new BufferedReader(file);
|
||||
String line;
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
// Find if the line contains the player we want.
|
||||
String[] character = line.split(":");
|
||||
|
||||
if (!character[0].equalsIgnoreCase(playerName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skill levels
|
||||
playerData.add(character[24]); // Taming
|
||||
playerData.add(character[1]); // Mining
|
||||
playerData.add(character[7]); // Repair
|
||||
playerData.add(character[5]); // Woodcutting
|
||||
playerData.add(character[8]); // Unarmed
|
||||
playerData.add(character[9]); // Herbalism
|
||||
playerData.add(character[10]); // Excavation
|
||||
playerData.add(character[11]); // Archery
|
||||
playerData.add(character[12]); // Swords
|
||||
playerData.add(character[13]); // Axes
|
||||
playerData.add(character[14]); // Acrobatics
|
||||
playerData.add(character[34]); // Fishing
|
||||
|
||||
// Experience
|
||||
playerData.add(character[25]); // Taming
|
||||
playerData.add(character[4]); // Mining
|
||||
playerData.add(character[15]); // Repair
|
||||
playerData.add(character[6]); // Woodcutting
|
||||
playerData.add(character[16]); // Unarmed
|
||||
playerData.add(character[17]); // Herbalism
|
||||
playerData.add(character[18]); // Excavation
|
||||
playerData.add(character[19]); // Archery
|
||||
playerData.add(character[20]); // Swords
|
||||
playerData.add(character[21]); // Axes
|
||||
playerData.add(character[22]); // Acrobatics
|
||||
playerData.add(character[35]); // Fishing
|
||||
|
||||
// Cooldowns
|
||||
playerData.add(null); // Taming
|
||||
playerData.add(character[32]); // SuperBreaker
|
||||
playerData.add(null); // Repair
|
||||
playerData.add(character[28]); // Tree Feller
|
||||
playerData.add(character[26]); // Beserk
|
||||
playerData.add(character[29]); // Green Terra
|
||||
playerData.add(character[27]); // Giga Drill Breaker
|
||||
playerData.add(null); // Archery
|
||||
playerData.add(character[30]); // Serrated Strikes
|
||||
playerData.add(character[31]); // Skull Splitter
|
||||
playerData.add(null); // Acrobatics
|
||||
playerData.add(character[36]); // Blast Mining
|
||||
|
||||
playerData.add(character.length > 33 ? character[33] : null); // HudType
|
||||
playerData.add(character.length > 38 ? character[38] : null); // MobHealthBar
|
||||
}
|
||||
|
||||
in.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return playerData;
|
||||
}
|
||||
|
||||
public boolean convert(String[] character) throws Exception {
|
||||
// Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean checkConnected() {
|
||||
// Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the leader boards.
|
||||
*/
|
||||
public static void updateLeaderboards() {
|
||||
* Update the leader boards.
|
||||
*/
|
||||
private void updateLeaderboards() {
|
||||
// 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
|
||||
if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
|
||||
return;
|
||||
@ -41,18 +393,18 @@ public final class FlatfileDatabaseManager {
|
||||
powerLevels.clear(); // Clear old values from the power levels
|
||||
|
||||
// Initialize lists
|
||||
List<PlayerStat> mining = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> mining = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> woodcutting = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> herbalism = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> excavation = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> acrobatics = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> repair = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> swords = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> axes = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> archery = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> unarmed = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> taming = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> fishing = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> herbalism = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> excavation = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> acrobatics = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> repair = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> swords = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> axes = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> archery = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> unarmed = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> taming = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> fishing = new ArrayList<PlayerStat>();
|
||||
|
||||
// Read from the FlatFile database and fill our arrays with information
|
||||
try {
|
||||
@ -62,7 +414,6 @@ public final class FlatfileDatabaseManager {
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
String[] data = line.split(":");
|
||||
|
||||
String playerName = data[0];
|
||||
int powerLevel = 0;
|
||||
|
||||
@ -124,160 +475,29 @@ public final class FlatfileDatabaseManager {
|
||||
playerStatHash.put(SkillType.FISHING, fishing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve leaderboard info.
|
||||
*
|
||||
* @param skillType Skill to retrieve info on.
|
||||
* @param pageNumber Which page in the leaderboards to retrieve
|
||||
* @return the requested leaderboard information
|
||||
*/
|
||||
public static List<PlayerStat> retrieveInfo(String skillType, int pageNumber, int statsPerPage) {
|
||||
List<PlayerStat> statsList = skillType.equalsIgnoreCase("all") ? powerLevels : playerStatHash.get(SkillType.getSkill(skillType));
|
||||
int fromIndex = (Math.max(pageNumber, 1) - 1) * statsPerPage;
|
||||
private void createDatabase() {
|
||||
if (usersFile.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
return statsList.subList(Math.min(fromIndex, statsList.size()), Math.min(fromIndex + statsPerPage, statsList.size()));
|
||||
}
|
||||
|
||||
public static boolean removeFlatFileUser(String playerName) {
|
||||
boolean worked = false;
|
||||
|
||||
BufferedReader in = null;
|
||||
FileWriter out = null;
|
||||
String usersFilePath = mcMMO.getUsersFilePath();
|
||||
usersFile.getParentFile().mkdir();
|
||||
|
||||
try {
|
||||
FileReader file = new FileReader(usersFilePath);
|
||||
in = new BufferedReader(file);
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line = "";
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
// Write out the same file but when we get to the player we want to remove, we skip his line.
|
||||
if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
else {
|
||||
mcMMO.p.getLogger().info("User found, removing...");
|
||||
worked = true;
|
||||
continue; // Skip the player
|
||||
}
|
||||
}
|
||||
|
||||
out = new FileWriter(usersFilePath); // Write out the new file
|
||||
out.write(writer.toString());
|
||||
mcMMO.p.debug("Creating mcmmo.users file...");
|
||||
new File(mcMMO.getUsersFilePath()).createNewFile();
|
||||
}
|
||||
catch (Exception e) {
|
||||
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return worked;
|
||||
}
|
||||
|
||||
public static int purgePowerlessFlatfile() {
|
||||
mcMMO.p.getLogger().info("Purging powerless users...");
|
||||
|
||||
int purgedUsers = 0;
|
||||
|
||||
for (PlayerStat stat : powerLevels) {
|
||||
if (stat.statVal == 0 && !mcMMO.p.getServer().getOfflinePlayer(stat.name).isOnline() && removeFlatFileUser(stat.name)) {
|
||||
purgedUsers++;
|
||||
}
|
||||
}
|
||||
|
||||
return purgedUsers;
|
||||
}
|
||||
|
||||
public static int removeOldFlatfileUsers() {
|
||||
int removedPlayers = 0;
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long purgeTime = ONE_MONTH * Config.getInstance().getOldUsersCutoff();
|
||||
|
||||
BufferedReader in = null;
|
||||
FileWriter out = null;
|
||||
String usersFilePath = mcMMO.getUsersFilePath();
|
||||
|
||||
try {
|
||||
FileReader file = new FileReader(usersFilePath);
|
||||
in = new BufferedReader(file);
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line = "";
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
// Write out the same file but when we get to the player we want to remove, we skip his line.
|
||||
String[] splitLine = line.split(":");
|
||||
|
||||
if (splitLine.length > 37) {
|
||||
if (currentTime - (StringUtils.getLong(line.split(":")[37]) * 1000) <= purgeTime) {
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
else {
|
||||
mcMMO.p.getLogger().info("User found, removing...");
|
||||
removedPlayers++;
|
||||
continue; // Skip the player
|
||||
}
|
||||
}
|
||||
else {
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
out = new FileWriter(usersFilePath); // Write out the new file
|
||||
out.write(writer.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return removedPlayers;
|
||||
}
|
||||
|
||||
private static Integer getPlayerRank(String playerName, List<PlayerStat> statsList) {
|
||||
int currentPos = 1;
|
||||
|
||||
private Integer getPlayerRank(String playerName, List<PlayerStat> statsList) {
|
||||
if (statsList == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int currentPos = 1;
|
||||
|
||||
for (PlayerStat stat : statsList) {
|
||||
if (stat.name.equalsIgnoreCase(playerName)) {
|
||||
return currentPos;
|
||||
@ -289,36 +509,18 @@ public final class FlatfileDatabaseManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Map<String, Integer> getPlayerRanks(String playerName) {
|
||||
updateLeaderboards();
|
||||
|
||||
Map<String, Integer> skills = new HashMap<String, Integer>();
|
||||
|
||||
for (SkillType skill : SkillType.nonChildSkills()) {
|
||||
skills.put(skill.name(), getPlayerRank(playerName, playerStatHash.get(skill)));
|
||||
private int loadStat(List<PlayerStat> statList, String playerName, String[] data, int dataIndex) {
|
||||
if (data.length <= dataIndex) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
skills.put("ALL", getPlayerRank(playerName, powerLevels));
|
||||
int statValue = Integer.parseInt(data[dataIndex]);
|
||||
statList.add(new PlayerStat(playerName, statValue));
|
||||
|
||||
return skills;
|
||||
return statValue;
|
||||
}
|
||||
|
||||
public static List<PlayerStat> getPlayerStats(String skillName) {
|
||||
return (skillName.equalsIgnoreCase("all")) ? powerLevels : playerStatHash.get(SkillType.getSkill(skillName));
|
||||
}
|
||||
|
||||
private static int loadStat(List<PlayerStat> statList, String playerName, String[] data, int dataIndex) {
|
||||
if (data.length > dataIndex) {
|
||||
int statValue = Integer.parseInt(data[dataIndex]);
|
||||
|
||||
statList.add(new PlayerStat(playerName, statValue));
|
||||
return statValue;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static class SkillComparator implements Comparator<PlayerStat> {
|
||||
private class SkillComparator implements Comparator<PlayerStat> {
|
||||
@Override
|
||||
public int compare(PlayerStat o1, PlayerStat o2) {
|
||||
return (o2.statVal - o1.statVal);
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user