mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-02 22:47:37 +01:00
Add plan_server_players_online and plan_network_players_online placeholders
Affects issues: - Close #3693
This commit is contained in:
parent
7e46c6ef63
commit
7c2c82ed0e
@ -26,7 +26,7 @@ import java.util.Objects;
|
||||
public class DateObj<T> 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<T> implements DateHolder {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<T> {
|
||||
|
||||
private final List<DateObj<T>> dateObjects;
|
||||
|
||||
public DateObjMutator(List<DateObj<T>> dateObjects) {this.dateObjects = dateObjects;}
|
||||
|
||||
public Optional<DateObj<T>> mostRecent() {
|
||||
if (dateObjects.isEmpty()) return Optional.empty();
|
||||
dateObjects.sort(new DateHolderRecentComparator());
|
||||
return Optional.of(dateObjects.get(0));
|
||||
}
|
||||
}
|
@ -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<Long> 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<T> {
|
||||
public interface QueryCreator<T> {
|
||||
Query<Optional<TopListQueries.TopListEntry<T>>> apply(Integer number, Long timespan, @Untrusted Arguments parameters);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user