mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-03-10 05:39:19 +01:00
Fixed AFK Tracker counting afk time for those with ignore permission when they use afk command
This commit is contained in:
parent
0851055d22
commit
7f153efcfa
@ -52,22 +52,26 @@ public class AFKTracker {
|
||||
}
|
||||
|
||||
public void usedAfkCommand(UUID uuid, long time) {
|
||||
Long lastMoved = lastMovement.getOrDefault(uuid, time);
|
||||
if (lastMoved == -1) {
|
||||
return;
|
||||
}
|
||||
usedAFKCommand.add(uuid);
|
||||
lastMovement.put(uuid, time - getAfkThreshold());
|
||||
}
|
||||
|
||||
public void performedAction(UUID uuid, long time) {
|
||||
public long performedAction(UUID uuid, long time) {
|
||||
Long lastMoved = lastMovement.getOrDefault(uuid, time);
|
||||
// Ignore afk permission
|
||||
if (lastMoved == -1) {
|
||||
return;
|
||||
return 0L;
|
||||
}
|
||||
lastMovement.put(uuid, time);
|
||||
|
||||
try {
|
||||
if (time - lastMoved < getAfkThreshold()) {
|
||||
// Threshold not crossed, no action required.
|
||||
return;
|
||||
return 0L;
|
||||
}
|
||||
|
||||
long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? getAfkThreshold() : 0;
|
||||
@ -75,15 +79,17 @@ public class AFKTracker {
|
||||
|
||||
SessionCache.getCachedSession(uuid)
|
||||
.ifPresent(session -> session.addAfkTime(timeAFK));
|
||||
return timeAFK;
|
||||
} finally {
|
||||
usedAFKCommand.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public void loggedOut(UUID uuid, long time) {
|
||||
performedAction(uuid, time);
|
||||
public long loggedOut(UUID uuid, long time) {
|
||||
long timeAFK = performedAction(uuid, time);
|
||||
lastMovement.remove(uuid);
|
||||
usedAFKCommand.remove(uuid);
|
||||
return timeAFK;
|
||||
}
|
||||
|
||||
public boolean isAfk(UUID uuid) {
|
||||
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.gathering.afk;
|
||||
|
||||
import com.djrapitops.plan.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.settings.config.paths.TimeSettings;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import utilities.TestConstants;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AFKTrackerTest {
|
||||
|
||||
private final UUID playerUUID = TestConstants.PLAYER_ONE_UUID;
|
||||
private AFKTracker underTest;
|
||||
private long afkThreshold;
|
||||
|
||||
@BeforeEach
|
||||
void setUpAfkTracker() {
|
||||
PlanConfig config = Mockito.mock(PlanConfig.class);
|
||||
afkThreshold = TimeUnit.MINUTES.toMillis(1L);
|
||||
when(config.get(TimeSettings.AFK_THRESHOLD)).thenReturn(afkThreshold);
|
||||
|
||||
underTest = new AFKTracker(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void afkThresholdMatches() {
|
||||
assertEquals(afkThreshold, underTest.getAfkThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void someoneIsAFKForAWhile() {
|
||||
underTest.performedAction(playerUUID, 0L);
|
||||
long afkTime = underTest.loggedOut(playerUUID, afkThreshold * 2);
|
||||
assertEquals(afkThreshold * 2, afkTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void someoneIsAFKForAWhileButHasIgnorePermission() {
|
||||
underTest.hasIgnorePermission(playerUUID);
|
||||
underTest.performedAction(playerUUID, 0L);
|
||||
long afkTime = underTest.loggedOut(playerUUID, afkThreshold * 2);
|
||||
assertEquals(0L, afkTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void someOneIsAfk() {
|
||||
underTest.performedAction(playerUUID, 0L);
|
||||
assertTrue(underTest.isAfk(playerUUID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void someOneIsNotEvenOnline() {
|
||||
assertFalse(underTest.isAfk(playerUUID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void someOneIsNotAfk() {
|
||||
underTest.performedAction(playerUUID, System.currentTimeMillis());
|
||||
assertFalse(underTest.isAfk(playerUUID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void someoneIsAFKForAwhileWithAfkCommand() {
|
||||
underTest.usedAfkCommand(playerUUID, 0L);
|
||||
long afkTime = underTest.loggedOut(playerUUID, afkThreshold * 2);
|
||||
assertEquals(afkThreshold * 2, afkTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void someoneIsAFKForAwhileWithAfkCommandButHasIgnorePermission() {
|
||||
underTest.hasIgnorePermission(playerUUID);
|
||||
underTest.usedAfkCommand(playerUUID, 0L);
|
||||
long afkTime = underTest.loggedOut(playerUUID, afkThreshold * 2);
|
||||
assertEquals(0L, afkTime);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user