Add plan_server_players_online and plan_network_players_online placeholders

Affects issues:
- Close #3693
This commit is contained in:
Aurora Lahtela 2024-11-30 13:50:04 +02:00
parent 7e46c6ef63
commit 7c2c82ed0e
4 changed files with 75 additions and 2 deletions

View File

@ -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;

View File

@ -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));
}
}

View File

@ -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);
}

View File

@ -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);
}