Skip to content

Commit

Permalink
Add plan_server_players_online and plan_network_players_online placeh…
Browse files Browse the repository at this point in the history
…olders

Affects issues:
- Close #3693
  • Loading branch information
AuroraLS3 committed Nov 30, 2024
1 parent 7e46c6e commit 7c2c82e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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;
Expand All @@ -42,6 +42,10 @@ public T getValue() {
return value;
}

public void setValue(T value) {
this.value = value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +86,22 @@ public void register(

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

Expand Down Expand Up @@ -132,6 +150,14 @@ public void register(
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)))));

Expand Down Expand Up @@ -270,7 +296,7 @@ private <T> List<TopCategoryQuery<T>> createCategoryQueriesForAllTimespans(Strin
);
}

interface QueryCreator<T> {
public interface QueryCreator<T> {
Query<Optional<TopListQueries.TopListEntry<T>>> apply(Integer number, Long timespan, @Untrusted Arguments parameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static long now() {
return System.currentTimeMillis();
}

public static long fiveMinAgo() {
return now() - TimeUnit.MINUTES.toMillis(5L);
}

public static long dayAgo() {
return now() - TimeUnit.DAYS.toMillis(1L);
}
Expand Down

0 comments on commit 7c2c82e

Please sign in to comment.