diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/DateObj.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/DateObj.java index 4ce0fca41..9845b4e8b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/DateObj.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/DateObj.java @@ -26,7 +26,7 @@ import java.util.Objects; public class DateObj implements DateHolder { private final long date; - private final T value; + private T value; public DateObj(long date, T value) { this.date = date; @@ -42,6 +42,10 @@ public class DateObj implements DateHolder { return value; } + public void setValue(T value) { + this.value = value; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/DateObjMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/DateObjMutator.java new file mode 100644 index 000000000..8b35d1ce3 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/DateObjMutator.java @@ -0,0 +1,39 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.domain.mutators; + +import com.djrapitops.plan.delivery.domain.DateObj; +import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator; + +import java.util.List; +import java.util.Optional; + +/** + * @author AuroraLS3 + */ +public class DateObjMutator { + + private final List> dateObjects; + + public DateObjMutator(List> dateObjects) {this.dateObjects = dateObjects;} + + public Optional> mostRecent() { + if (dateObjects.isEmpty()) return Optional.empty(); + dateObjects.sort(new DateHolderRecentComparator()); + return Optional.of(dateObjects.get(0)); + } +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java index 411cd7542..38c3fa5ea 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java @@ -17,6 +17,8 @@ package com.djrapitops.plan.placeholder; import com.djrapitops.plan.commands.use.Arguments; +import com.djrapitops.plan.delivery.domain.DateObj; +import com.djrapitops.plan.delivery.domain.mutators.DateObjMutator; import com.djrapitops.plan.delivery.formatting.Formatter; import com.djrapitops.plan.delivery.formatting.Formatters; import com.djrapitops.plan.gathering.ServerUptimeCalculator; @@ -84,6 +86,22 @@ public class ServerPlaceHolders implements Placeholders { Database database = dbSystem.getDatabase(); + placeholders.registerStatic("network_players_online", + parameters -> { + DateObj count = new DateObj<>(System.currentTimeMillis(), 0L); + for (Server proxy : database.query(ServerQueries.fetchProxyServers())) { + ServerUUID proxyUUID = proxy.getUuid(); + new DateObjMutator<>(database.query(TPSQueries.fetchPlayersOnlineOfServer(fiveMinAgo(), now(), proxyUUID))) + .mostRecent() + .ifPresent(playersOnline -> { + if (Math.abs(count.getDate() - playersOnline.getDate()) > TimeUnit.MINUTES.toMillis(2L)) { + count.setValue(count.getValue() + playersOnline.getValue()); + } + }); + } + return count.getValue(); + }); + placeholders.registerStatic("server_players_registered_total", parameters -> database.query(PlayerCountQueries.newPlayerCount(0, now(), getServerUUID(parameters)))); @@ -132,6 +150,14 @@ public class ServerPlaceHolders implements Placeholders { placeholders.registerStatic("network_players_unique_month", parameters -> database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now()))); + placeholders.registerStatic("server_players_online", + parameters -> new DateObjMutator<>( + database.query(TPSQueries.fetchPlayersOnlineOfServer(fiveMinAgo(), now(), getServerUUID(parameters)))) + .mostRecent() + .map(DateObj::getValue) + .map(String::valueOf) + .orElse("-")); + placeholders.registerStatic("server_tps_day", parameters -> decimals.apply(database.query(TPSQueries.averageTPS(dayAgo(), now(), getServerUUID(parameters))))); @@ -270,7 +296,7 @@ public class ServerPlaceHolders implements Placeholders { ); } - interface QueryCreator { + public interface QueryCreator { Query>> apply(Integer number, Long timespan, @Untrusted Arguments parameters); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java index 190563193..6a55cacab 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java @@ -48,6 +48,10 @@ public class MiscUtils { return System.currentTimeMillis(); } + public static long fiveMinAgo() { + return now() - TimeUnit.MINUTES.toMillis(5L); + } + public static long dayAgo() { return now() - TimeUnit.DAYS.toMillis(1L); }