Implemented World Pie Alias Setting system for #390 & #340

This commit is contained in:
Rsl1122 2017-10-31 19:37:19 +02:00
parent 7d0554688a
commit 913766214c
7 changed files with 158 additions and 16 deletions

View File

@ -99,6 +99,8 @@ public enum Settings {
// StringList // StringList
HIDE_FACTIONS("Plugins.Factions.HideFactions"), HIDE_FACTIONS("Plugins.Factions.HideFactions"),
HIDE_TOWNS("Plugins.Towny.HideTowns"), HIDE_TOWNS("Plugins.Towny.HideTowns"),
// Config section
WORLD_ALIASES("Customization.WorldAliases"),
// //
// Bungee // Bungee
BUNGEE_IP("Server.IP"), BUNGEE_IP("Server.IP"),

View File

@ -0,0 +1,77 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.HashMap;
import java.util.Map;
/**
* Class responsible for managing Bukkit side config settings for World Aliases.
*
* @author Rsl1122
*/
public class WorldAliasSettings {
private final Plan plugin;
/**
* Constructor.
*
* @param plugin Current instance of Plan.
*/
public WorldAliasSettings(Plan plugin) {
this.plugin = plugin;
}
/**
* Used to get all World aliases in the config
*
* @return Map: Original name, Alias
*/
public Map<String, String> getAliases() {
ConfigurationSection aliasSect = getAliasSection();
Map<String, String> aliasMap = new HashMap<>();
for (String world : aliasSect.getKeys(false)) {
aliasMap.put(world, aliasSect.getString(world));
}
return aliasMap;
}
private ConfigurationSection getAliasSection() {
FileConfiguration config = plugin.getConfig();
return config.getConfigurationSection(Settings.WORLD_ALIASES.getPath());
}
/**
* Adds a new World to the config section.
* <p>
* If exists does not override old value.
*
* @param world World name
*/
public void addWorld(String world) {
ConfigurationSection aliasSect = getAliasSection();
Object previousValue = aliasSect.get(world);
if (previousValue == null) {
aliasSect.set(world, world);
}
plugin.saveConfig();
}
/**
* Used to get alias of a single world.
*
* @param world World name.
* @return Alias.
*/
public String getAlias(String world) {
return getAliasSection().getString(world);
}
}

View File

@ -103,8 +103,11 @@ public abstract class TimeKeeper {
} }
public long getTime(String state) { public long getTime(String state) {
Long time = times.get(state); return times.getOrDefault(state, 0L);
return time != null ? time : 0L; }
public void addTime(String state, long time) {
times.put(state, times.getOrDefault(state, 0L) + time);
} }
public long getTotal() { public long getTotal() {

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.systems.listeners; package main.java.com.djrapitops.plan.systems.listeners;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.WorldAliasSettings;
import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -46,6 +47,8 @@ public class PlanGamemodeChangeListener implements Listener {
String gameMode = event.getNewGameMode().name(); String gameMode = event.getNewGameMode().name();
String worldName = p.getWorld().getName(); String worldName = p.getWorld().getName();
new WorldAliasSettings(plugin).addWorld(worldName);
Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid); Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid);
cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time)); cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));
} }

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.systems.listeners; package main.java.com.djrapitops.plan.systems.listeners;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.WorldAliasSettings;
import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -28,6 +29,8 @@ public class PlanWorldChangeListener implements Listener {
String gameMode = p.getGameMode().name(); String gameMode = p.getGameMode().name();
long time = MiscUtils.getTime(); long time = MiscUtils.getTime();
new WorldAliasSettings(plugin).addWorld(worldName);
Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid); Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid);
cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time)); cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));
} }

View File

@ -1,12 +1,11 @@
package main.java.com.djrapitops.plan.utilities.html.graphs; package main.java.com.djrapitops.plan.utilities.html.graphs;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.WorldAliasSettings;
import main.java.com.djrapitops.plan.data.time.GMTimes; import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.data.time.WorldTimes; import main.java.com.djrapitops.plan.data.time.WorldTimes;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class WorldPieCreator { public class WorldPieCreator {
@ -28,14 +27,16 @@ public class WorldPieCreator {
Map<String, Long> playtimePerWorld = worldTimes.getWorldTimes().entrySet().stream() Map<String, Long> playtimePerWorld = worldTimes.getWorldTimes().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getTotal())); .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getTotal()));
List<String> worlds = new ArrayList<>(playtimePerWorld.keySet()); Map<String, Long> playtimePerAlias = transformToAliases(playtimePerWorld);
List<String> worlds = new ArrayList<>(playtimePerAlias.keySet());
Collections.sort(worlds); Collections.sort(worlds);
int size = playtimePerWorld.size(); int size = playtimePerAlias.size();
for (String worldName : worlds) { for (String alias : worlds) {
seriesBuilder.append("{name:'").append(worldName) seriesBuilder.append("{name:'").append(alias)
.append("',y:").append(playtimePerWorld.getOrDefault(worldName, 0L)) .append("',y:").append(playtimePerAlias.getOrDefault(alias, 0L))
.append(",drilldown: '").append(worldName).append("'"); .append(",drilldown: '").append(alias).append("'");
seriesBuilder.append("}"); seriesBuilder.append("}");
if (i < size - 1) { if (i < size - 1) {
@ -52,6 +53,27 @@ public class WorldPieCreator {
return new String[]{seriesData, drilldownData}; return new String[]{seriesData, drilldownData};
} }
private static Map<String, Long> transformToAliases(Map<String, Long> playtimePerWorld) {
WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance());
Map<String, String> aliases = aliasSettings.getAliases();
Map<String, Long> playtimePerAlias = new HashMap<>();
for (Map.Entry<String, Long> entry : playtimePerWorld.entrySet()) {
String worldName = entry.getKey();
long playtime = entry.getValue();
if (!aliases.containsKey(worldName)) {
aliases.put(worldName, worldName);
aliasSettings.addWorld(worldName);
}
String alias = aliases.get(worldName);
playtimePerAlias.put(alias, playtimePerAlias.getOrDefault(alias, 0L) + playtime);
}
return playtimePerAlias;
}
private static String createDrilldownData(WorldTimes worldTimes) { private static String createDrilldownData(WorldTimes worldTimes) {
StringBuilder drilldownBuilder = new StringBuilder(); StringBuilder drilldownBuilder = new StringBuilder();
int i = 0; int i = 0;
@ -60,15 +82,17 @@ public class WorldPieCreator {
if (gmTimesMap.isEmpty()) { if (gmTimesMap.isEmpty()) {
return "[]"; return "[]";
} }
Map<String, GMTimes> gmTimesAliasMap = transformToGMAliases(gmTimesMap);
int size = gmTimesMap.size(); int size = gmTimesMap.size();
drilldownBuilder.append("["); drilldownBuilder.append("[");
for (Map.Entry<String, GMTimes> world : gmTimesMap.entrySet()) { for (Map.Entry<String, GMTimes> worldAlias : gmTimesAliasMap.entrySet()) {
drilldownBuilder.append("{name:'").append(world.getKey()) drilldownBuilder.append("{name:'").append(worldAlias.getKey())
.append("', id:'").append(world.getKey()) .append("', id:'").append(worldAlias.getKey())
.append("',colors: gmPieColors,"); .append("',colors: gmPieColors,");
drilldownBuilder.append("data: ["); drilldownBuilder.append("data: [");
appendGMTimesForWorld(drilldownBuilder, world); appendGMTimesForWorld(drilldownBuilder, worldAlias);
if (i < size - 1) { if (i < size - 1) {
drilldownBuilder.append(","); drilldownBuilder.append(",");
@ -79,6 +103,34 @@ public class WorldPieCreator {
return drilldownBuilder.toString(); return drilldownBuilder.toString();
} }
private static Map<String, GMTimes> transformToGMAliases(Map<String, GMTimes> gmTimesMap) {
WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance());
Map<String, String> aliases = aliasSettings.getAliases();
Map<String, GMTimes> gmTimesPerAlias = new HashMap<>();
String[] gms = GMTimes.getGMKeyArray();
for (Map.Entry<String, GMTimes> entry : gmTimesMap.entrySet()) {
String worldName = entry.getKey();
GMTimes gmTimes = entry.getValue();
if (!aliases.containsKey(worldName)) {
aliases.put(worldName, worldName);
aliasSettings.addWorld(worldName);
}
String alias = aliases.get(worldName);
GMTimes aliasGMtimes = gmTimesPerAlias.getOrDefault(alias, new GMTimes());
for (String gm : gms) {
aliasGMtimes.addTime(gm, gmTimes.getTime(gm));
}
gmTimesPerAlias.put(alias, aliasGMtimes);
}
return gmTimesPerAlias;
}
private static void appendGMTimesForWorld(StringBuilder drilldownBuilder, Map.Entry<String, GMTimes> world) { private static void appendGMTimesForWorld(StringBuilder drilldownBuilder, Map.Entry<String, GMTimes> world) {
Map<String, Long> gmTimes = world.getValue().getTimes(); Map<String, Long> gmTimes = world.getValue().getTimes();
int smallSize = gmTimes.size(); int smallSize = gmTimes.size();

View File

@ -67,6 +67,8 @@ Customization:
Minutes: '%minutes%m ' Minutes: '%minutes%m '
Seconds: '%seconds%s' Seconds: '%seconds%s'
Zero: '0s' Zero: '0s'
WorldAliases:
world: world
Theme: Theme:
Base: Default Base: Default