Add session duration placeholders

- %plan_player_current_session_length%: The length of active session (Online time)
- %plan_player_current_session_length_raw%: unformatted ms
- %plan_player_latest_session_length%: The length of active session or previous session if not online
- %plan_player_latest_session_length_raw%: unformatted ms
- %plan_player_previous_session_length%: The length of previous non-active session even if online
- %plan_player_previous_session_length_raw%: unformatted ms

Affects issues:
- Close #2211
This commit is contained in:
Aurora Lahtela 2022-05-28 08:37:13 +03:00
parent ee1a69cb0c
commit 79ff7d599f
2 changed files with 55 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.settings.config.WorldAliasSettings;
import com.djrapitops.plan.utilities.analysis.Median;
import com.djrapitops.plan.utilities.comparators.DateHolderOldestComparator;
import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator;
import com.djrapitops.plan.utilities.java.Lists;
import java.util.*;
@ -304,4 +305,22 @@ public class SessionsMutator {
return sessionMap;
});
}
public Optional<FinishedSession> latestSession() {
List<FinishedSession> orderedSessions = sort(new DateHolderRecentComparator()).all();
return orderedSessions.isEmpty() ? Optional.empty() : Optional.of(orderedSessions.get(0));
}
public Optional<FinishedSession> previousSession() {
List<FinishedSession> orderedSessions = sort(new DateHolderRecentComparator()).all();
for (FinishedSession session : orderedSessions) {
if (session.getExtraData(ActiveSession.class).isPresent()) {
continue;
}
// First non-active session is previous one.
return Optional.of(session);
}
return Optional.empty();
}
}

View File

@ -27,6 +27,7 @@ import com.djrapitops.plan.delivery.formatting.Formatters;
import com.djrapitops.plan.gathering.afk.AFKTracker;
import com.djrapitops.plan.gathering.cache.SessionCache;
import com.djrapitops.plan.gathering.domain.ActiveSession;
import com.djrapitops.plan.gathering.domain.FinishedSession;
import com.djrapitops.plan.gathering.domain.PlayerKill;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
@ -35,9 +36,11 @@ import com.djrapitops.plan.settings.config.paths.TimeSettings;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.utilities.Predicates;
import org.jetbrains.annotations.NotNull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Optional;
import static com.djrapitops.plan.utilities.MiscUtils.*;
@ -346,5 +349,38 @@ public class PlayerPlaceHolders implements Placeholders {
.filterPlayedOnServer(serverInfo.getServerUUID())
.toPlaytime()
);
placeholders.register("player_current_session_length",
player -> time.apply(getActiveSessionLength(player).orElse(-1L)));
placeholders.register("player_current_session_length_raw",
player -> getActiveSessionLength(player).orElse(0L));
placeholders.register("player_latest_session_length",
player -> time.apply(getActiveSessionLength(player)
.orElseGet(() -> SessionsMutator.forContainer(player).latestSession()
.map(FinishedSession::getLength)
.orElse(-1L))));
placeholders.register("player_latest_session_length_raw",
player -> getActiveSessionLength(player)
.orElseGet(() -> SessionsMutator.forContainer(player).latestSession()
.map(FinishedSession::getLength)
.orElse(0L)));
placeholders.register("player_previous_session_length",
player -> time.apply(SessionsMutator.forContainer(player).previousSession()
.map(FinishedSession::getLength)
.orElse(-1L)));
placeholders.register("player_previous_session_length_raw",
player -> SessionsMutator.forContainer(player).previousSession()
.map(FinishedSession::getLength)
.orElse(0L));
}
@NotNull
private Optional<Long> getActiveSessionLength(PlayerContainer player) {
SessionCache.refreshActiveSessionsState();
return SessionCache.getCachedSession(player.getUnsafe(PlayerKeys.UUID))
.map(ActiveSession::toFinishedSessionFromStillActive)
.map(FinishedSession::getLength);
}
}