2016-12-08 18:31:10 +01:00
|
|
|
package com.djrapitops.plan.command.utils;
|
|
|
|
|
|
|
|
import com.djrapitops.plan.Plan;
|
2016-12-09 23:27:03 +01:00
|
|
|
import com.djrapitops.plan.command.hooks.AdvancedAchievementsHook;
|
2016-12-08 18:31:10 +01:00
|
|
|
import java.io.File;
|
2016-12-09 18:42:09 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2016-12-08 18:31:10 +01:00
|
|
|
import java.util.HashMap;
|
2016-12-09 18:42:09 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2016-12-08 18:31:10 +01:00
|
|
|
import java.util.Scanner;
|
|
|
|
import java.util.UUID;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import static org.bukkit.plugin.java.JavaPlugin.getPlugin;
|
|
|
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
2016-12-10 11:39:06 +01:00
|
|
|
// allData defined by -a argument in InspectCommand
|
|
|
|
// returns data given by each Hook
|
2016-12-08 18:31:10 +01:00
|
|
|
public static HashMap<String, String> getData(boolean allData, String playerName) {
|
|
|
|
HashMap<String, String> data = new HashMap<>();
|
|
|
|
Plan plugin = getPlugin(Plan.class);
|
2016-12-12 16:11:39 +01:00
|
|
|
plugin.getHooks().keySet().parallelStream().forEach((hook) -> {
|
2016-12-08 18:31:10 +01:00
|
|
|
try {
|
|
|
|
if (allData) {
|
|
|
|
data.putAll(plugin.getHooks().get(hook).getAllData(playerName));
|
|
|
|
} else {
|
|
|
|
data.putAll(plugin.getHooks().get(hook).getData(playerName));
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
String toLog = "UTILS-GetData"
|
|
|
|
+ "\nFailed to getData from " + hook
|
|
|
|
+ "\n" + e
|
|
|
|
+ "\ncausing argument: " + playerName;
|
|
|
|
for (StackTraceElement element : e.getStackTrace()) {
|
|
|
|
toLog += "\n " + element;
|
|
|
|
}
|
|
|
|
plugin.logToFile(toLog);
|
|
|
|
}
|
2016-12-12 16:11:39 +01:00
|
|
|
});
|
2016-12-08 18:31:10 +01:00
|
|
|
return data;
|
|
|
|
}
|
2016-12-10 11:39:06 +01:00
|
|
|
|
|
|
|
// Returns data HashMaps for all pplayers in a HashMap.
|
2016-12-08 18:31:10 +01:00
|
|
|
public static HashMap<UUID, HashMap<String, String>> getTotalData() {
|
|
|
|
HashMap<UUID, HashMap<String, String>> playerData = new HashMap<>();
|
2016-12-12 16:11:39 +01:00
|
|
|
|
|
|
|
List<OfflinePlayer> players = new ArrayList<>();
|
|
|
|
players.addAll(Arrays.asList(Bukkit.getOfflinePlayers()));
|
|
|
|
players.parallelStream()
|
|
|
|
.filter((player) -> (playerData.get(player.getUniqueId()) == null))
|
|
|
|
.forEach((player) -> {
|
|
|
|
playerData.put(player.getUniqueId(), getData(true, player.getName()));
|
|
|
|
});
|
2016-12-08 18:31:10 +01:00
|
|
|
return playerData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] getPlaceholdersFileData() {
|
|
|
|
Plan plugin = getPlugin(Plan.class);
|
|
|
|
File placeholdersFile = new File(plugin.getDataFolder(), "placeholders.yml");
|
|
|
|
try {
|
|
|
|
if (!placeholdersFile.exists()) {
|
|
|
|
placeholdersFile.createNewFile();
|
|
|
|
}
|
|
|
|
Scanner filescanner = new Scanner(placeholdersFile);
|
|
|
|
String placeholdersString = "";
|
|
|
|
if (filescanner.hasNextLine()) {
|
|
|
|
placeholdersString = filescanner.nextLine();
|
|
|
|
}
|
|
|
|
String[] returnArray = placeholdersString.split(" ");
|
|
|
|
return returnArray;
|
|
|
|
} catch (Exception e) {
|
|
|
|
plugin.logToFile("Failed to create placeholders.yml\n" + e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-12-10 11:39:06 +01:00
|
|
|
|
2016-12-09 18:42:09 +01:00
|
|
|
public static HashMap<String, String> analyze(HashMap<UUID, HashMap<String, String>> playerData) {
|
|
|
|
Plan plugin = getPlugin(Plan.class);
|
|
|
|
HashMap<String, List<String>> playerDataLists = new HashMap<>();
|
2016-12-10 11:39:06 +01:00
|
|
|
// Ignore following keys (Strings, unprocessable or irrelevant data)
|
2016-12-09 18:42:09 +01:00
|
|
|
String[] ignore = {"ESS-BAN REASON", "ESS-OPPED", "ESS-MUTE TIME", "ESS-LOCATION", "ESS-HUNGER", "ESS-LOCATION WORLD",
|
|
|
|
"ESS-NICKNAME", "ESS-UUID", "FAC-FACTION", "ONT-LAST LOGIN", "TOW-TOWN", "TOW-REGISTERED",
|
|
|
|
"TOW-LAST LOGIN", "TOW-OWNER OF", "TOW-PLOT PERMS", "TOW-PLOT OPTIONS", "TOW-FRIENDS", "ESS-ONLINE SINCE",
|
|
|
|
"ESS-OFFLINE SINCE"};
|
|
|
|
List<String> ignoreKeys = new ArrayList<>();
|
2016-12-09 23:27:03 +01:00
|
|
|
try {
|
|
|
|
AdvancedAchievementsHook aaHook = (AdvancedAchievementsHook) plugin.getHooks().get("AdvancedAchievements");
|
|
|
|
if (!aaHook.isUsingUUID()) {
|
|
|
|
ignoreKeys.add("AAC-ACHIEVEMENTS");
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
ignoreKeys.add("AAC-ACHIEVEMENTS");
|
|
|
|
}
|
2016-12-09 18:42:09 +01:00
|
|
|
ignoreKeys.addAll(Arrays.asList(ignore));
|
2016-12-10 11:39:06 +01:00
|
|
|
|
|
|
|
// Turn playerData into Hashmap of Lists sorted by keys.
|
2016-12-12 16:11:39 +01:00
|
|
|
playerData.keySet().parallelStream().forEach((key) -> {
|
|
|
|
playerData.get(key).keySet().parallelStream()
|
|
|
|
.filter((dataKey) -> !(ignoreKeys.contains(dataKey)))
|
|
|
|
.map((dataKey) -> {
|
|
|
|
if (playerDataLists.get(dataKey) == null) {
|
|
|
|
playerDataLists.put(dataKey, new ArrayList<>());
|
|
|
|
}
|
|
|
|
return dataKey;
|
|
|
|
})
|
|
|
|
.forEach((dataKey) -> {
|
|
|
|
playerDataLists.get(dataKey).add(playerData.get(key).get(dataKey));
|
|
|
|
});
|
|
|
|
});
|
2016-12-09 18:42:09 +01:00
|
|
|
|
2016-12-10 11:39:06 +01:00
|
|
|
// Define analysis method for keys
|
|
|
|
String[] numbers = {"AAC-ACHIEVEMENTS", "ESS-HEALTH", "ESS-XP LEVEL", "FAC-POWER", "FAC-POWER PER HOUR",
|
2016-12-09 18:42:09 +01:00
|
|
|
"FAC-POWER PER DEATH", "SVO-VOTES", "ONT-TOTAL VOTES", "ONT-TOTAL REFERRED", "ECO-BALANCE"};
|
|
|
|
String[] booleanValues = {"ESS-BANNED", "ESS-JAILED", "ESS-MUTED", "ESS-FLYING", "TOW-ONLINE"};
|
|
|
|
String[] timeValues = {"ONT-TOTAL PLAY"};
|
2016-12-10 11:39:06 +01:00
|
|
|
|
|
|
|
List<String> numberKeys = new ArrayList<>();
|
|
|
|
List<String> boolKeys = new ArrayList<>();
|
2016-12-09 18:42:09 +01:00
|
|
|
List<String> timeKeys = new ArrayList<>();
|
2016-12-10 11:39:06 +01:00
|
|
|
|
|
|
|
numberKeys.addAll(Arrays.asList(numbers));
|
|
|
|
boolKeys.addAll(Arrays.asList(booleanValues));
|
2016-12-09 18:42:09 +01:00
|
|
|
timeKeys.addAll(Arrays.asList(timeValues));
|
|
|
|
|
2016-12-10 11:39:06 +01:00
|
|
|
// TODO: Add extrahook analysis methods here
|
2016-12-09 18:42:09 +01:00
|
|
|
HashMap<String, String> analyzedData = new HashMap<>();
|
|
|
|
int errors = 0;
|
|
|
|
HashSet<String> errorTypes = new HashSet<>();
|
|
|
|
|
2016-12-10 11:39:06 +01:00
|
|
|
// Analyze - Go through each key - Go through each point of data in the list.
|
2016-12-09 18:42:09 +01:00
|
|
|
for (String dataKey : playerDataLists.keySet()) {
|
|
|
|
if (numberKeys.contains(dataKey)) {
|
|
|
|
double sum = 0;
|
|
|
|
|
|
|
|
for (String dataPoint : playerDataLists.get(dataKey)) {
|
2016-12-10 11:39:06 +01:00
|
|
|
// Special cases separated.
|
2016-12-09 18:42:09 +01:00
|
|
|
try {
|
|
|
|
if (dataKey.equals("FAC-POWER") || dataKey.equals("AAC-ACHIEVEMENTS")) {
|
|
|
|
sum += Double.parseDouble(dataPoint.split(" ")[0]);
|
|
|
|
} else if (dataKey.equals("ECO-BALANCE")) {
|
|
|
|
sum += Double.parseDouble(DataFormatUtils.removeLetters(dataPoint));
|
|
|
|
} else {
|
|
|
|
sum += Double.parseDouble(dataPoint);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
errors++;
|
|
|
|
errorTypes.add("" + e);
|
|
|
|
}
|
|
|
|
}
|
2016-12-10 11:39:06 +01:00
|
|
|
// Average
|
2016-12-09 22:03:51 +01:00
|
|
|
analyzedData.put(dataKey, "" + (sum * 1.0 / playerData.size()));
|
2016-12-09 18:42:09 +01:00
|
|
|
|
|
|
|
} else if (boolKeys.contains(dataKey)) {
|
|
|
|
int amount = 0;
|
|
|
|
for (String dataPoint : playerDataLists.get(dataKey)) {
|
|
|
|
try {
|
|
|
|
if (Boolean.parseBoolean(dataPoint)) {
|
|
|
|
amount++;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
errors++;
|
|
|
|
errorTypes.add("" + e);
|
|
|
|
}
|
|
|
|
}
|
2016-12-10 11:39:06 +01:00
|
|
|
// Average
|
2016-12-09 22:03:51 +01:00
|
|
|
analyzedData.put(dataKey, "" + ((amount * 1.0 / playerData.size()) * 100) + "%");
|
2016-12-09 18:42:09 +01:00
|
|
|
} else if (timeKeys.contains(dataKey)) {
|
|
|
|
Long time = Long.parseLong("0");
|
|
|
|
for (String dataPoint : playerDataLists.get(dataKey)) {
|
|
|
|
try {
|
|
|
|
time += Long.parseLong(dataPoint);
|
|
|
|
} catch (Exception e) {
|
|
|
|
errors++;
|
|
|
|
errorTypes.add("" + e);
|
|
|
|
}
|
|
|
|
}
|
2016-12-10 11:39:06 +01:00
|
|
|
// Average
|
2016-12-09 22:03:51 +01:00
|
|
|
analyzedData.put(dataKey, "" + (time * 1.0 / playerData.size()));
|
2016-12-09 18:42:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-10 11:39:06 +01:00
|
|
|
// Log errors
|
2016-12-09 18:42:09 +01:00
|
|
|
if (errors > 0) {
|
|
|
|
String log = "ANALYZE\n" + errors + " error(s) occurred while analyzing total data.\nFollowing types:";
|
|
|
|
for (String errorType : errorTypes) {
|
|
|
|
log += "\n " + errorType;
|
|
|
|
}
|
|
|
|
plugin.logToFile(log);
|
|
|
|
}
|
|
|
|
return DataFormatUtils.formatAnalyzed(analyzedData);
|
|
|
|
}
|
2016-12-08 18:31:10 +01:00
|
|
|
}
|