mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-03-07 17:28:03 +08:00
Add AFK status placeholders
- %plan_player_is_afk%, output true/false - %plan_player_is_afk_badge%, output 'AFK'/'' Affects issues: - Close #2247
This commit is contained in:
parent
9c6085da86
commit
96e0cd6856
@ -17,10 +17,14 @@
|
|||||||
package com.djrapitops.plan.gathering.afk;
|
package com.djrapitops.plan.gathering.afk;
|
||||||
|
|
||||||
import com.djrapitops.plan.gathering.cache.SessionCache;
|
import com.djrapitops.plan.gathering.cache.SessionCache;
|
||||||
|
import com.djrapitops.plan.gathering.domain.ActiveSession;
|
||||||
import com.djrapitops.plan.settings.config.PlanConfig;
|
import com.djrapitops.plan.settings.config.PlanConfig;
|
||||||
import com.djrapitops.plan.settings.config.paths.TimeSettings;
|
import com.djrapitops.plan.settings.config.paths.TimeSettings;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps track how long player has been afk during a session
|
* Keeps track how long player has been afk during a session
|
||||||
@ -29,15 +33,15 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public class AFKTracker {
|
public class AFKTracker {
|
||||||
|
|
||||||
|
public static final long IGNORES_AFK = -1L;
|
||||||
|
|
||||||
private final Set<UUID> usedAFKCommand;
|
private final Set<UUID> usedAFKCommand;
|
||||||
private final Map<UUID, Long> lastMovement;
|
|
||||||
private final PlanConfig config;
|
private final PlanConfig config;
|
||||||
private Long afkThresholdMs;
|
private Long afkThresholdMs;
|
||||||
|
|
||||||
public AFKTracker(PlanConfig config) {
|
public AFKTracker(PlanConfig config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
usedAFKCommand = new HashSet<>();
|
usedAFKCommand = new HashSet<>();
|
||||||
lastMovement = new HashMap<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getAfkThreshold() {
|
public long getAfkThreshold() {
|
||||||
@ -47,26 +51,41 @@ public class AFKTracker {
|
|||||||
return afkThresholdMs;
|
return afkThresholdMs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void hasIgnorePermission(UUID uuid) {
|
public void hasIgnorePermission(UUID playerUUID) {
|
||||||
lastMovement.put(uuid, -1L);
|
storeLastMovement(playerUUID, IGNORES_AFK);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void usedAfkCommand(UUID uuid, long time) {
|
private void storeLastMovement(UUID playerUUID, long time) {
|
||||||
Long lastMoved = lastMovement.getOrDefault(uuid, time);
|
SessionCache.getCachedSession(playerUUID)
|
||||||
if (lastMoved == -1) {
|
.ifPresent(activeSession -> activeSession.setLastMovementForAfkCalculation(time));
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getLastMovement(UUID playerUUID, long time) {
|
||||||
|
return getLastMovement(playerUUID)
|
||||||
|
.orElse(time);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<Long> getLastMovement(UUID playerUUID) {
|
||||||
|
return SessionCache.getCachedSession(playerUUID)
|
||||||
|
.map(ActiveSession::getLastMovementForAfkCalculation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void usedAfkCommand(UUID playerUUID, long time) {
|
||||||
|
long lastMoved = getLastMovement(playerUUID, time);
|
||||||
|
if (lastMoved == IGNORES_AFK) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
usedAFKCommand.add(uuid);
|
usedAFKCommand.add(playerUUID);
|
||||||
lastMovement.put(uuid, time - getAfkThreshold());
|
storeLastMovement(playerUUID, time - getAfkThreshold());
|
||||||
}
|
}
|
||||||
|
|
||||||
public long performedAction(UUID uuid, long time) {
|
public long performedAction(UUID playerUUID, long time) {
|
||||||
Long lastMoved = lastMovement.getOrDefault(uuid, time);
|
long lastMoved = getLastMovement(playerUUID, time);
|
||||||
// Ignore afk permission
|
// Ignore afk permission
|
||||||
if (lastMoved == -1) {
|
if (lastMoved == IGNORES_AFK) {
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
lastMovement.put(uuid, time);
|
storeLastMovement(playerUUID, time);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (time - lastMoved < getAfkThreshold()) {
|
if (time - lastMoved < getAfkThreshold()) {
|
||||||
@ -74,31 +93,30 @@ public class AFKTracker {
|
|||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? getAfkThreshold() : 0;
|
long removeAfkCommandEffect = usedAFKCommand.contains(playerUUID) ? getAfkThreshold() : 0;
|
||||||
long timeAFK = time - lastMoved - removeAfkCommandEffect;
|
long timeAFK = time - lastMoved - removeAfkCommandEffect;
|
||||||
|
|
||||||
SessionCache.getCachedSession(uuid)
|
SessionCache.getCachedSession(playerUUID)
|
||||||
.ifPresent(session -> session.addAfkTime(timeAFK));
|
.ifPresent(session -> session.addAfkTime(timeAFK));
|
||||||
return timeAFK;
|
return timeAFK;
|
||||||
} finally {
|
} finally {
|
||||||
usedAFKCommand.remove(uuid);
|
usedAFKCommand.remove(playerUUID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long loggedOut(UUID uuid, long time) {
|
public long loggedOut(UUID uuid, long time) {
|
||||||
long timeAFK = performedAction(uuid, time);
|
long timeAFK = performedAction(uuid, time);
|
||||||
lastMovement.remove(uuid);
|
|
||||||
usedAFKCommand.remove(uuid);
|
usedAFKCommand.remove(uuid);
|
||||||
return timeAFK;
|
return timeAFK;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAfk(UUID uuid) {
|
public boolean isAfk(UUID playerUUID) {
|
||||||
long time = System.currentTimeMillis();
|
long time = System.currentTimeMillis();
|
||||||
|
|
||||||
Long lastMoved = lastMovement.get(uuid);
|
Optional<Long> lastMoved = getLastMovement(playerUUID);
|
||||||
if (lastMoved == null || lastMoved == -1) {
|
if (!lastMoved.isPresent() || lastMoved.get() == IGNORES_AFK) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return time - lastMoved > getAfkThreshold();
|
return time - lastMoved.get() > getAfkThreshold();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -32,6 +32,8 @@ public class ActiveSession {
|
|||||||
private final DataMap extraData;
|
private final DataMap extraData;
|
||||||
private long afkTime;
|
private long afkTime;
|
||||||
|
|
||||||
|
private long lastMovementForAfkCalculation;
|
||||||
|
|
||||||
public ActiveSession(UUID playerUUID, ServerUUID serverUUID, long start, String world, String gameMode) {
|
public ActiveSession(UUID playerUUID, ServerUUID serverUUID, long start, String world, String gameMode) {
|
||||||
this.playerUUID = playerUUID;
|
this.playerUUID = playerUUID;
|
||||||
this.serverUUID = serverUUID;
|
this.serverUUID = serverUUID;
|
||||||
@ -43,6 +45,8 @@ public class ActiveSession {
|
|||||||
extraData.put(MobKillCounter.class, new MobKillCounter());
|
extraData.put(MobKillCounter.class, new MobKillCounter());
|
||||||
extraData.put(DeathCounter.class, new DeathCounter());
|
extraData.put(DeathCounter.class, new DeathCounter());
|
||||||
extraData.put(PlayerKills.class, new PlayerKills());
|
extraData.put(PlayerKills.class, new PlayerKills());
|
||||||
|
|
||||||
|
lastMovementForAfkCalculation = start;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ActiveSession fromPlayerJoin(PlayerJoin join) {
|
public static ActiveSession fromPlayerJoin(PlayerJoin join) {
|
||||||
@ -147,5 +151,13 @@ public class ActiveSession {
|
|||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getLastMovementForAfkCalculation() {
|
||||||
|
return lastMovementForAfkCalculation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastMovementForAfkCalculation(long lastMovementForAfkCalculation) {
|
||||||
|
this.lastMovementForAfkCalculation = lastMovementForAfkCalculation;
|
||||||
|
}
|
||||||
|
|
||||||
public static class FirstSession {}
|
public static class FirstSession {}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.djrapitops.plan.placeholder;
|
package com.djrapitops.plan.placeholder;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.delivery.domain.container.PlayerContainer;
|
||||||
import com.djrapitops.plan.delivery.domain.keys.PlayerKeys;
|
import com.djrapitops.plan.delivery.domain.keys.PlayerKeys;
|
||||||
import com.djrapitops.plan.delivery.domain.mutators.PerServerMutator;
|
import com.djrapitops.plan.delivery.domain.mutators.PerServerMutator;
|
||||||
import com.djrapitops.plan.delivery.domain.mutators.PingMutator;
|
import com.djrapitops.plan.delivery.domain.mutators.PingMutator;
|
||||||
@ -23,6 +24,9 @@ import com.djrapitops.plan.delivery.domain.mutators.PlayerVersusMutator;
|
|||||||
import com.djrapitops.plan.delivery.domain.mutators.SessionsMutator;
|
import com.djrapitops.plan.delivery.domain.mutators.SessionsMutator;
|
||||||
import com.djrapitops.plan.delivery.formatting.Formatter;
|
import com.djrapitops.plan.delivery.formatting.Formatter;
|
||||||
import com.djrapitops.plan.delivery.formatting.Formatters;
|
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.PlayerKill;
|
import com.djrapitops.plan.gathering.domain.PlayerKill;
|
||||||
import com.djrapitops.plan.identification.Server;
|
import com.djrapitops.plan.identification.Server;
|
||||||
import com.djrapitops.plan.identification.ServerInfo;
|
import com.djrapitops.plan.identification.ServerInfo;
|
||||||
@ -71,6 +75,9 @@ public class PlayerPlaceHolders implements Placeholders {
|
|||||||
Formatter<Long> year = formatters.yearLong();
|
Formatter<Long> year = formatters.yearLong();
|
||||||
Formatter<Long> time = formatters.timeAmount();
|
Formatter<Long> time = formatters.timeAmount();
|
||||||
|
|
||||||
|
placeholders.register("player_is_afk", this::isAfk);
|
||||||
|
placeholders.register("player_is_afk_badge", player -> isAfk(player) ? "AFK" : "");
|
||||||
|
|
||||||
placeholders.register("player_banned",
|
placeholders.register("player_banned",
|
||||||
player -> player.getValue(PlayerKeys.BANNED)
|
player -> player.getValue(PlayerKeys.BANNED)
|
||||||
.orElse(Boolean.FALSE)
|
.orElse(Boolean.FALSE)
|
||||||
@ -159,10 +166,18 @@ public class PlayerPlaceHolders implements Placeholders {
|
|||||||
).getGroup()
|
).getGroup()
|
||||||
);
|
);
|
||||||
|
|
||||||
registerKillPlaceholders(placeholders, time);
|
registerKillPlaceholders(placeholders);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerKillPlaceholders(PlanPlaceholders placeholders, Formatter<Long> time) {
|
private boolean isAfk(PlayerContainer player) {
|
||||||
|
return SessionCache.getCachedSession(player.getUnsafe(PlayerKeys.UUID))
|
||||||
|
.map(ActiveSession::getLastMovementForAfkCalculation)
|
||||||
|
.filter(lastMovement -> lastMovement != AFKTracker.IGNORES_AFK
|
||||||
|
&& now() - lastMovement > config.get(TimeSettings.AFK_THRESHOLD))
|
||||||
|
.isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerKillPlaceholders(PlanPlaceholders placeholders) {
|
||||||
placeholders.register("player_player_caused_deaths",
|
placeholders.register("player_player_caused_deaths",
|
||||||
player -> PlayerVersusMutator.forContainer(player).toPlayerDeathCount()
|
player -> PlayerVersusMutator.forContainer(player).toPlayerDeathCount()
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user