Convert default database from Flatfile to Serialized.

This commit is contained in:
GJ 2014-02-22 14:11:38 -05:00
parent 8deac175d1
commit 3df0d0f6aa
5 changed files with 548 additions and 0 deletions

View File

@ -61,6 +61,9 @@ public class DatabaseManagerFactory {
case FLATFILE: case FLATFILE:
return new FlatfileDatabaseManager(); return new FlatfileDatabaseManager();
case SERIALIZED:
return new SerializedDatabaseManager();
case SQL: case SQL:
return new SQLDatabaseManager(); return new SQLDatabaseManager();

View File

@ -0,0 +1,440 @@
package com.gmail.nossr50.database;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.OfflinePlayer;
import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.database.PlayerStat;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.player.UserData;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Misc;
public class SerializedDatabaseManager 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 final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
protected SerializedDatabaseManager() {
updateLeaderboards();
}
public void purgePowerlessUsers() {
int purgedUsers = 0;
mcMMO.p.getLogger().info("Purging powerless users...");
File usersDirectory = new File(mcMMO.getUsersDirectory());
File[] userFiles = usersDirectory.listFiles();
if (userFiles == null) {
return;
}
for (File file : userFiles) {
if (!file.isFile() || file.isDirectory()) {
continue;
}
UserData data = deserialize(file);
if (data == null) {
continue;
}
boolean powerless = true;
for (int skill : data.getSkillLevels().values()) {
if (skill != 0) {
powerless = false;
break;
}
}
if (powerless && file.delete()) {
purgedUsers++;
Misc.profileCleanup(data.getName());
}
}
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...");
File usersDirectory = new File(mcMMO.getUsersDirectory());
File[] userFiles = usersDirectory.listFiles();
if (userFiles == null) {
return;
}
for (File file : userFiles) {
if (!file.isFile() || file.isDirectory()) {
continue;
}
UserData data = deserialize(file);
if (data == null) {
continue;
}
boolean rewrite = false;
String name = data.getName();
long lastPlayed = data.getLastPlayed();
if (lastPlayed == 0) {
OfflinePlayer player = mcMMO.p.getServer().getOfflinePlayer(name);
lastPlayed = player.getLastPlayed();
rewrite = true;
}
if (currentTime - lastPlayed > PURGE_TIME) {
if (file.delete()) {
removedPlayers++;
Misc.profileCleanup(name);
}
}
if (rewrite) {
data.setLastPlayed(lastPlayed);
serialize(file, data);
}
}
mcMMO.p.getLogger().info("Purged " + removedPlayers + " users from the database.");
}
public boolean removeUser(String playerName) {
boolean worked = false;
File usersDirectory = new File(mcMMO.getUsersDirectory());
File[] userFiles = usersDirectory.listFiles();
if (userFiles == null) {
return worked;
}
for (File file : userFiles) {
if (!file.isFile() || file.isDirectory()) {
continue;
}
UserData data = deserialize(file);
if (data != null && data.getName().equalsIgnoreCase(playerName)) {
mcMMO.p.getLogger().info("User found, removing...");
worked = file.delete();
break;
}
}
return worked;
}
public boolean saveUser(PlayerProfile profile) {
UserData data = new UserData(profile);
return serialize(new File(mcMMO.getUsersDirectory(), data.getName() + ".mcmmoplayer"), data);
}
public List<PlayerStat> readLeaderboard(SkillType skill, int pageNumber, int statsPerPage) {
updateLeaderboards();
List<PlayerStat> statsList = skill == null ? powerLevels : playerStatHash.get(skill);
int fromIndex = (Math.max(pageNumber, 1) - 1) * statsPerPage;
return statsList.subList(Math.min(fromIndex, statsList.size()), Math.min(fromIndex + statsPerPage, statsList.size()));
}
public Map<SkillType, Integer> readRank(String playerName) {
updateLeaderboards();
Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>();
for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
skills.put(skill, getPlayerRank(playerName, playerStatHash.get(skill)));
}
skills.put(null, getPlayerRank(playerName, powerLevels));
return skills;
}
public void newUser(String playerName) {
serialize(new File(mcMMO.getUsersDirectory(), playerName + ".mcmmoplayer"), new UserData(new PlayerProfile(playerName)));
}
public PlayerProfile loadPlayerProfile(String playerName, boolean createNew) {
File usersDirectory = new File(mcMMO.getUsersDirectory());
File[] userFiles = usersDirectory.listFiles();
if (userFiles == null) {
return new PlayerProfile(playerName, createNew);
}
for (File file : userFiles) {
if (!file.isFile() || file.isDirectory()) {
continue;
}
UserData data = deserialize(file);
if (data != null && data.getName().equalsIgnoreCase(playerName)) {
return loadFromData(data);
}
}
return new PlayerProfile(playerName, createNew);
}
public List<String> getStoredUsers() {
ArrayList<String> users = new ArrayList<String>();
File usersDirectory = new File(mcMMO.getUsersDirectory());
File[] userFiles = usersDirectory.listFiles();
if (userFiles == null) {
return users;
}
for (File file : userFiles) {
if (!file.isFile() || file.isDirectory()) {
continue;
}
UserData data = deserialize(file);
if (data == null) {
continue;
}
users.add(data.getName());
}
return users;
}
public void convertUsers(DatabaseManager destination) {
File usersDirectory = new File(mcMMO.getUsersDirectory());
File[] userFiles = usersDirectory.listFiles();
if (userFiles == null) {
//TODO: log issue
return;
}
int convertedUsers = 0;
long startMillis = System.currentTimeMillis();
for (File file : userFiles) {
if (!file.isFile() || file.isDirectory()) {
continue;
}
UserData data = deserialize(file);
if (data == null) {
continue;
}
try {
if (destination.saveUser(loadFromData(data))) {
convertedUsers++;
Misc.printProgress(convertedUsers, progressInterval, startMillis);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public DatabaseType getDatabaseType() {
return DatabaseType.SERIALIZED;
}
private UserData deserialize(File file) {
UserData data = null;
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
data = (UserData) ois.readObject();
ois.close();
}
catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + file.getName() + ": " + e.toString());
}
return data;
}
private boolean serialize(File file, UserData data) {
boolean success = true;
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(data);
oos.close();
}
catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while writing " + file.getName() + ": " + e.toString());
success = false;
}
return success;
}
private PlayerProfile loadFromData(UserData data) {
return new PlayerProfile(data.getName(), data.getSkillLevels(), data.getSkillXp(), data.getAbilityData(), data.getMobHealthbarType());
}
/**
* Update the leader boards.
*/
private void updateLeaderboards() {
// Only update 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;
}
File usersDirectory = new File(mcMMO.getUsersDirectory());
File[] userFiles = usersDirectory.listFiles();
if (userFiles == null) {
return;
}
lastUpdate = System.currentTimeMillis(); // Log when the last update was run
powerLevels.clear(); // Clear old values from the power levels
// Initialize lists
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> alchemy = new ArrayList<PlayerStat>();
for (File file : userFiles) {
if (!file.isFile() || file.isDirectory()) {
continue;
}
UserData data = deserialize(file);
if (data == null) {
continue;
}
int powerLevel = 0;
String playerName = data.getName();
Map<SkillType, Integer> skills = data.getSkillLevels();
powerLevel += putStat(acrobatics, playerName, skills.get(SkillType.ACROBATICS));
powerLevel += putStat(alchemy, playerName, skills.get(SkillType.ALCHEMY));
powerLevel += putStat(archery, playerName, skills.get(SkillType.ARCHERY));
powerLevel += putStat(axes, playerName, skills.get(SkillType.AXES));
powerLevel += putStat(excavation, playerName, skills.get(SkillType.EXCAVATION));
powerLevel += putStat(fishing, playerName, skills.get(SkillType.FISHING));
powerLevel += putStat(herbalism, playerName, skills.get(SkillType.HERBALISM));
powerLevel += putStat(mining, playerName, skills.get(SkillType.MINING));
powerLevel += putStat(repair, playerName, skills.get(SkillType.REPAIR));
powerLevel += putStat(swords, playerName, skills.get(SkillType.SWORDS));
powerLevel += putStat(taming, playerName, skills.get(SkillType.TAMING));
powerLevel += putStat(unarmed, playerName, skills.get(SkillType.UNARMED));
powerLevel += putStat(woodcutting, playerName, skills.get(SkillType.WOODCUTTING));
putStat(powerLevels, playerName, powerLevel);
}
SkillComparator c = new SkillComparator();
Collections.sort(mining, c);
Collections.sort(woodcutting, c);
Collections.sort(repair, c);
Collections.sort(unarmed, c);
Collections.sort(herbalism, c);
Collections.sort(excavation, c);
Collections.sort(archery, c);
Collections.sort(swords, c);
Collections.sort(axes, c);
Collections.sort(acrobatics, c);
Collections.sort(taming, c);
Collections.sort(fishing, c);
Collections.sort(alchemy, c);
Collections.sort(powerLevels, c);
playerStatHash.put(SkillType.MINING, mining);
playerStatHash.put(SkillType.WOODCUTTING, woodcutting);
playerStatHash.put(SkillType.REPAIR, repair);
playerStatHash.put(SkillType.UNARMED, unarmed);
playerStatHash.put(SkillType.HERBALISM, herbalism);
playerStatHash.put(SkillType.EXCAVATION, excavation);
playerStatHash.put(SkillType.ARCHERY, archery);
playerStatHash.put(SkillType.SWORDS, swords);
playerStatHash.put(SkillType.AXES, axes);
playerStatHash.put(SkillType.ACROBATICS, acrobatics);
playerStatHash.put(SkillType.TAMING, taming);
playerStatHash.put(SkillType.FISHING, fishing);
playerStatHash.put(SkillType.ALCHEMY, alchemy);
}
private int putStat(List<PlayerStat> statList, String playerName, int statValue) {
statList.add(new PlayerStat(playerName, statValue));
return statValue;
}
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;
}
currentPos++;
}
return null;
}
private class SkillComparator implements Comparator<PlayerStat> {
@Override
public int compare(PlayerStat o1, PlayerStat o2) {
return (o2.statVal - o1.statVal);
}
}
}

View File

@ -2,6 +2,7 @@ package com.gmail.nossr50.datatypes.database;
public enum DatabaseType { public enum DatabaseType {
FLATFILE, FLATFILE,
SERIALIZED,
SQL, SQL,
CUSTOM; CUSTOM;

View File

@ -0,0 +1,98 @@
package com.gmail.nossr50.datatypes.player;
import com.gmail.nossr50.datatypes.MobHealthbarType;
import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.mcMMO;
import org.bukkit.entity.Player;
import java.io.Serializable;
import java.util.HashMap;
import java.util.UUID;
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private UUID playerID;
private String name;
private long lastPlayed;
private HashMap<SkillType, Integer> skillLevels = new HashMap<SkillType, Integer>();
private HashMap<SkillType, Float> skillXp = new HashMap<SkillType, Float>();
private HashMap<AbilityType, Integer> abilityData = new HashMap<AbilityType, Integer>();
private MobHealthbarType mobHealthbarType;
public UserData(McMMOPlayer mcMMOPlayer) {
Player player = mcMMOPlayer.getPlayer();
playerID = player.getUniqueId();
name = player.getName();
lastPlayed = player.getLastPlayed();
for (SkillType skill : SkillType.values()) {
skillLevels.put(skill, mcMMOPlayer.getSkillLevel(skill));
skillXp.put(skill, mcMMOPlayer.getSkillXpLevelRaw(skill));
}
PlayerProfile profile = mcMMOPlayer.getProfile();
for (AbilityType ability : AbilityType.values()) {
abilityData.put(ability, (int) profile.getAbilityDATS(ability));
}
mobHealthbarType = profile.getMobHealthbarType();
}
public UserData(PlayerProfile profile) {
name = profile.getPlayerName();
Player player = mcMMO.p.getServer().getPlayerExact(name);
if (player == null) {
playerID = UUID.fromString(name);
lastPlayed = 0L;
}
else {
playerID = player.getUniqueId();
lastPlayed = player.getLastPlayed();
}
for (SkillType skill : SkillType.values()) {
skillLevels.put(skill, profile.getSkillLevel(skill));
skillXp.put(skill, profile.getSkillXpLevelRaw(skill));
}
for (AbilityType ability : AbilityType.values()) {
abilityData.put(ability, (int) profile.getAbilityDATS(ability));
}
mobHealthbarType = profile.getMobHealthbarType();
}
public String getName() {
return name;
}
public long getLastPlayed() {
return lastPlayed;
}
public void setLastPlayed(long lastPlayed) {
this.lastPlayed = lastPlayed;
}
public HashMap<SkillType, Integer> getSkillLevels() {
return skillLevels;
}
public HashMap<SkillType, Float> getSkillXp() {
return skillXp;
}
public HashMap<AbilityType, Integer> getAbilityData() {
return abilityData;
}
public MobHealthbarType getMobHealthbarType() {
return mobHealthbarType;
}
}

View File

@ -73,6 +73,7 @@ public class mcMMO extends JavaPlugin {
/* File Paths */ /* File Paths */
private static String mainDirectory; private static String mainDirectory;
private static String flatFileDirectory; private static String flatFileDirectory;
private static String usersDirectory;
private static String usersFile; private static String usersFile;
private static String modDirectory; private static String modDirectory;
@ -250,6 +251,10 @@ public class mcMMO extends JavaPlugin {
return flatFileDirectory; return flatFileDirectory;
} }
public static String getUsersDirectory() {
return usersDirectory;
}
public static String getUsersFilePath() { public static String getUsersFilePath() {
return usersFile; return usersFile;
} }
@ -326,6 +331,7 @@ public class mcMMO extends JavaPlugin {
mcmmo = getFile(); mcmmo = getFile();
mainDirectory = getDataFolder().getPath() + File.separator; mainDirectory = getDataFolder().getPath() + File.separator;
flatFileDirectory = mainDirectory + "flatfile" + File.separator; flatFileDirectory = mainDirectory + "flatfile" + File.separator;
usersDirectory = flatFileDirectory + "users" + File.separator;
usersFile = flatFileDirectory + "mcmmo.users"; usersFile = flatFileDirectory + "mcmmo.users";
modDirectory = mainDirectory + "mods" + File.separator; modDirectory = mainDirectory + "mods" + File.separator;
fixFilePaths(); fixFilePaths();