mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-21 20:22:45 +02:00
[Debt] Moved SRP violation in Session
Session#getLongestWorldPlayed was a violation of Single Responsibility Principle. The method was moved to WorldAliasSettings, although still a violation, it makes more sense there than inside the Session class. It also allows creation of Session without a WorldAliasSettings dependency.
This commit is contained in:
parent
e12f258a33
commit
3625156b36
|
@ -4,8 +4,6 @@ import com.djrapitops.plan.data.store.containers.DataContainer;
|
|||
import com.djrapitops.plan.data.store.keys.SessionKeys;
|
||||
import com.djrapitops.plan.data.store.objects.DateHolder;
|
||||
import com.djrapitops.plan.data.time.WorldTimes;
|
||||
import com.djrapitops.plan.system.settings.WorldAliasSettings;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -58,7 +56,7 @@ public class Session extends DataContainer implements DateHolder {
|
|||
getValue(SessionKeys.END).orElse(System.currentTimeMillis()) - getUnsafe(SessionKeys.START));
|
||||
putSupplier(SessionKeys.ACTIVE_TIME, () -> getUnsafe(SessionKeys.LENGTH) - getUnsafe(SessionKeys.AFK_TIME));
|
||||
|
||||
putSupplier(SessionKeys.LONGEST_WORLD_PLAYED, this::getLongestWorldPlayed);
|
||||
putRawData(SessionKeys.LONGEST_WORLD_PLAYED, "Key is Deprecated, use WorldAliasSettings#getLongestWorldPlayed(Session) instead.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,7 +99,7 @@ public class Session extends DataContainer implements DateHolder {
|
|||
getValue(SessionKeys.END).orElse(System.currentTimeMillis()) - getUnsafe(SessionKeys.START));
|
||||
putSupplier(SessionKeys.ACTIVE_TIME, () -> getUnsafe(SessionKeys.LENGTH) - getUnsafe(SessionKeys.AFK_TIME));
|
||||
|
||||
putSupplier(SessionKeys.LONGEST_WORLD_PLAYED, this::getLongestWorldPlayed);
|
||||
putRawData(SessionKeys.LONGEST_WORLD_PLAYED, "Key is Deprecated, use WorldAliasSettings#getLongestWorldPlayed(Session) instead.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -220,35 +218,4 @@ public class Session extends DataContainer implements DateHolder {
|
|||
private long getAfkTime() {
|
||||
return afkTime;
|
||||
}
|
||||
|
||||
@Deprecated // TODO Move this method elsewhere
|
||||
private String getLongestWorldPlayed() {
|
||||
WorldAliasSettings worldAliasSettings = null; // TODO
|
||||
Map<String, String> aliases = worldAliasSettings.getAliases();
|
||||
if (worldTimes == null) {
|
||||
return "No World Time Data";
|
||||
}
|
||||
if (!supports(SessionKeys.END)) {
|
||||
return "Current: " + aliases.get(worldTimes.getCurrentWorld());
|
||||
}
|
||||
|
||||
Map<String, Long> playtimePerAlias = new HashMap<>(); //TODO Call WorldAliasSettings#getPlaytimePerAlias(WorldTimes)
|
||||
long total = worldTimes.getTotal();
|
||||
|
||||
long longest = 0;
|
||||
String theWorld = "-";
|
||||
for (Map.Entry<String, Long> entry : playtimePerAlias.entrySet()) {
|
||||
String world = entry.getKey();
|
||||
long time = entry.getValue();
|
||||
if (time > longest) {
|
||||
longest = time;
|
||||
theWorld = world;
|
||||
}
|
||||
}
|
||||
|
||||
double quotient = longest * 1.0 / total;
|
||||
|
||||
Formatter<Double> percentageFormatter = null; // TODO
|
||||
return theWorld + " (" + percentageFormatter.apply(quotient) + ")";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,10 +4,14 @@
|
|||
*/
|
||||
package com.djrapitops.plan.system.settings;
|
||||
|
||||
import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.data.store.keys.SessionKeys;
|
||||
import com.djrapitops.plan.data.time.GMTimes;
|
||||
import com.djrapitops.plan.data.time.WorldTimes;
|
||||
import com.djrapitops.plan.system.processing.Processing;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatters;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
|
@ -30,18 +34,22 @@ import java.util.stream.Collectors;
|
|||
public class WorldAliasSettings {
|
||||
|
||||
private final Lazy<PlanConfig> config;
|
||||
private final Formatter<Double> percentageFormatter;
|
||||
private final Processing processing;
|
||||
private final ErrorHandler errorHandler;
|
||||
|
||||
@Inject
|
||||
public WorldAliasSettings(
|
||||
Lazy<PlanConfig> config,
|
||||
Formatters formatters,
|
||||
Processing processing,
|
||||
ErrorHandler errorHandler
|
||||
) {
|
||||
this.config = config;
|
||||
this.processing = processing;
|
||||
this.errorHandler = errorHandler;
|
||||
|
||||
percentageFormatter = formatters.percentage();
|
||||
}
|
||||
|
||||
private ConfigNode getAliasSection() {
|
||||
|
@ -139,4 +147,33 @@ public class WorldAliasSettings {
|
|||
}
|
||||
return gmTimesPerAlias;
|
||||
}
|
||||
|
||||
public String getLongestWorldPlayed(Session session) {
|
||||
Map<String, String> aliases = getAliases();
|
||||
if (!session.supports(SessionKeys.WORLD_TIMES)) {
|
||||
return "No World Time Data";
|
||||
}
|
||||
WorldTimes worldTimes = session.getUnsafe(SessionKeys.WORLD_TIMES);
|
||||
if (!session.supports(SessionKeys.END)) {
|
||||
return "Current: " + aliases.get(worldTimes.getCurrentWorld());
|
||||
}
|
||||
|
||||
Map<String, Long> playtimePerAlias = getPlaytimePerAlias(worldTimes);
|
||||
long total = worldTimes.getTotal();
|
||||
|
||||
long longest = 0;
|
||||
String theWorld = "-";
|
||||
for (Map.Entry<String, Long> entry : playtimePerAlias.entrySet()) {
|
||||
String world = entry.getKey();
|
||||
long time = entry.getValue();
|
||||
if (time > longest) {
|
||||
longest = time;
|
||||
theWorld = world;
|
||||
}
|
||||
}
|
||||
|
||||
double quotient = longest * 1.0 / total;
|
||||
|
||||
return theWorld + " (" + percentageFormatter.apply(quotient) + ")";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public class Accordions {
|
|||
true, sessions,
|
||||
serverNamesSupplier, HashMap::new,
|
||||
appendWorldPercentage, maxSessions,
|
||||
theme, graphs, tables,
|
||||
config.getWorldAliasSettings(), theme, graphs, tables,
|
||||
formatters.year(), formatters.timeAmount()
|
||||
);
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ public class Accordions {
|
|||
false, sessions,
|
||||
serverNamesSupplier, playerNamesSupplier,
|
||||
appendWorldPercentage, maxSessions,
|
||||
theme, graphs, tables,
|
||||
config.getWorldAliasSettings(), theme, graphs, tables,
|
||||
formatters.year(), formatters.timeAmount()
|
||||
);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import com.djrapitops.plan.data.container.Session;
|
|||
import com.djrapitops.plan.data.store.keys.SessionKeys;
|
||||
import com.djrapitops.plan.data.store.objects.DateHolder;
|
||||
import com.djrapitops.plan.data.time.WorldTimes;
|
||||
import com.djrapitops.plan.system.settings.WorldAliasSettings;
|
||||
import com.djrapitops.plan.system.settings.theme.Theme;
|
||||
import com.djrapitops.plan.system.settings.theme.ThemeVal;
|
||||
import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator;
|
||||
|
@ -36,6 +37,7 @@ public class SessionAccordion extends Accordion {
|
|||
private final boolean appendWorldPercentage;
|
||||
private final int maxSessions;
|
||||
|
||||
private final WorldAliasSettings worldAliasSettings;
|
||||
private final Theme theme;
|
||||
private final Graphs graphs;
|
||||
private final HtmlTables tables;
|
||||
|
@ -47,6 +49,7 @@ public class SessionAccordion extends Accordion {
|
|||
Supplier<Map<UUID, String>> playerNamesSupplier,
|
||||
boolean appendWorldPercentage,
|
||||
int maxSessions,
|
||||
WorldAliasSettings worldAliasSettings,
|
||||
Theme theme,
|
||||
Graphs graphs,
|
||||
HtmlTables tables,
|
||||
|
@ -61,6 +64,7 @@ public class SessionAccordion extends Accordion {
|
|||
this.playerNamesSupplier = playerNamesSupplier;
|
||||
this.appendWorldPercentage = appendWorldPercentage;
|
||||
this.maxSessions = maxSessions;
|
||||
this.worldAliasSettings = worldAliasSettings;
|
||||
this.theme = theme;
|
||||
this.graphs = graphs;
|
||||
this.tables = tables;
|
||||
|
@ -101,7 +105,7 @@ public class SessionAccordion extends Accordion {
|
|||
|
||||
WorldTimes worldTimes = session.getValue(SessionKeys.WORLD_TIMES).orElse(new WorldTimes(new HashMap<>()));
|
||||
WorldPie worldPie = graphs.pie().worldPie(worldTimes);
|
||||
String longestWorldPlayed = session.getValue(SessionKeys.LONGEST_WORLD_PLAYED).orElse("Unknown");
|
||||
String longestWorldPlayed = worldAliasSettings.getLongestWorldPlayed(session);
|
||||
|
||||
boolean hasEnded = session.supports(SessionKeys.END);
|
||||
String sessionEnd = hasEnded ? yearFormatter.apply(() -> session.getUnsafe(SessionKeys.END)) : "Online";
|
||||
|
@ -173,7 +177,7 @@ public class SessionAccordion extends Accordion {
|
|||
|
||||
WorldTimes worldTimes = session.getValue(SessionKeys.WORLD_TIMES).orElse(new WorldTimes(new HashMap<>()));
|
||||
WorldPie worldPie = graphs.pie().worldPie(worldTimes);
|
||||
String longestWorldPlayed = session.getValue(SessionKeys.LONGEST_WORLD_PLAYED).orElse("Unknown");
|
||||
String longestWorldPlayed = worldAliasSettings.getLongestWorldPlayed(session);
|
||||
|
||||
boolean hasEnded = session.supports(SessionKeys.END);
|
||||
String sessionEnd = hasEnded ? yearFormatter.apply(() -> session.getValue(SessionKeys.END).orElse(0L)) : "Online";
|
||||
|
|
|
@ -110,7 +110,7 @@ public class HtmlTables {
|
|||
public TableContainer playerSessionTable(String playerName, List<Session> sessions) {
|
||||
return new PlayerSessionTable(
|
||||
playerName, sessions,
|
||||
config.getNumber(Settings.MAX_SESSIONS), formatters.year(), formatters.timeAmount()
|
||||
config.getNumber(Settings.MAX_SESSIONS), config.getWorldAliasSettings(), formatters.year(), formatters.timeAmount()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -124,7 +124,7 @@ public class HtmlTables {
|
|||
public TableContainer serverSessionTable(Map<UUID, String> playerNames, List<Session> sessions) {
|
||||
return new ServerSessionTable(
|
||||
playerNames, sessions,
|
||||
config.getNumber(Settings.MAX_SESSIONS), formatters.year(), formatters.timeAmount()
|
||||
config.getNumber(Settings.MAX_SESSIONS), config.getWorldAliasSettings(), formatters.year(), formatters.timeAmount()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.djrapitops.plan.data.container.Session;
|
|||
import com.djrapitops.plan.data.element.TableContainer;
|
||||
import com.djrapitops.plan.data.store.keys.SessionKeys;
|
||||
import com.djrapitops.plan.data.store.objects.DateHolder;
|
||||
import com.djrapitops.plan.system.settings.WorldAliasSettings;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.html.Html;
|
||||
|
||||
|
@ -18,6 +19,7 @@ import java.util.List;
|
|||
class PlayerSessionTable extends TableContainer {
|
||||
|
||||
private final int maxSessions;
|
||||
private final WorldAliasSettings worldAliasSettings;
|
||||
private final Formatter<DateHolder> yearFormatter;
|
||||
private final Formatter<Long> timeAmountFormatter;
|
||||
|
||||
|
@ -26,6 +28,7 @@ class PlayerSessionTable extends TableContainer {
|
|||
|
||||
PlayerSessionTable(String playerName, List<Session> sessions,
|
||||
int maxSessions,
|
||||
WorldAliasSettings worldAliasSettings,
|
||||
Formatter<DateHolder> yearFormatter,
|
||||
Formatter<Long> timeAmountFormatter
|
||||
) {
|
||||
|
@ -33,6 +36,7 @@ class PlayerSessionTable extends TableContainer {
|
|||
this.playerName = playerName;
|
||||
this.sessions = sessions;
|
||||
this.maxSessions = maxSessions;
|
||||
this.worldAliasSettings = worldAliasSettings;
|
||||
this.yearFormatter = yearFormatter;
|
||||
this.timeAmountFormatter = timeAmountFormatter;
|
||||
|
||||
|
@ -52,7 +56,7 @@ class PlayerSessionTable extends TableContainer {
|
|||
String length = session.supports(SessionKeys.END)
|
||||
? timeAmountFormatter.apply(session.getValue(SessionKeys.LENGTH).orElse(0L))
|
||||
: "Online";
|
||||
String world = session.getValue(SessionKeys.LONGEST_WORLD_PLAYED).orElse("Unknown");
|
||||
String world = worldAliasSettings.getLongestWorldPlayed(session);
|
||||
|
||||
String toolTip = "Session ID: " + session.getValue(SessionKeys.DB_ID)
|
||||
.map(id -> Integer.toString(id))
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.djrapitops.plan.data.container.Session;
|
|||
import com.djrapitops.plan.data.element.TableContainer;
|
||||
import com.djrapitops.plan.data.store.keys.SessionKeys;
|
||||
import com.djrapitops.plan.data.store.objects.DateHolder;
|
||||
import com.djrapitops.plan.system.settings.WorldAliasSettings;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.html.Html;
|
||||
|
||||
|
@ -20,6 +21,7 @@ import java.util.UUID;
|
|||
class ServerSessionTable extends TableContainer {
|
||||
|
||||
private final int maxSessions;
|
||||
private final WorldAliasSettings worldAliasSettings;
|
||||
private final Formatter<DateHolder> yearFormatter;
|
||||
private final Formatter<Long> timeAmountFormatter;
|
||||
|
||||
|
@ -29,6 +31,7 @@ class ServerSessionTable extends TableContainer {
|
|||
ServerSessionTable(
|
||||
Map<UUID, String> playerNames, List<Session> sessions,
|
||||
int maxSessions,
|
||||
WorldAliasSettings worldAliasSettings,
|
||||
Formatter<DateHolder> yearFormatter,
|
||||
Formatter<Long> timeAmountFormatter
|
||||
) {
|
||||
|
@ -36,6 +39,7 @@ class ServerSessionTable extends TableContainer {
|
|||
this.playerNames = playerNames;
|
||||
this.sessions = sessions;
|
||||
this.maxSessions = maxSessions;
|
||||
this.worldAliasSettings = worldAliasSettings;
|
||||
this.yearFormatter = yearFormatter;
|
||||
this.timeAmountFormatter = timeAmountFormatter;
|
||||
|
||||
|
@ -53,7 +57,7 @@ class ServerSessionTable extends TableContainer {
|
|||
String length = session.supports(SessionKeys.END)
|
||||
? timeAmountFormatter.apply(session.getValue(SessionKeys.LENGTH).orElse(0L))
|
||||
: "Online";
|
||||
String world = session.getValue(SessionKeys.LONGEST_WORLD_PLAYED).orElse("Unknown");
|
||||
String world = worldAliasSettings.getLongestWorldPlayed(session);
|
||||
|
||||
String toolTip = "Session ID: " + session.getValue(SessionKeys.DB_ID)
|
||||
.map(id -> Integer.toString(id))
|
||||
|
|
Loading…
Reference in New Issue
Block a user