Fixed AFK Tracker counting afk time for those with ignore permission when they use afk command

This commit is contained in:
Risto Lahtela 2021-03-20 17:26:34 +02:00
parent 0851055d22
commit 7f153efcfa
2 changed files with 109 additions and 5 deletions

View File

@ -52,22 +52,26 @@ public class AFKTracker {
} }
public void usedAfkCommand(UUID uuid, long time) { public void usedAfkCommand(UUID uuid, long time) {
Long lastMoved = lastMovement.getOrDefault(uuid, time);
if (lastMoved == -1) {
return;
}
usedAFKCommand.add(uuid); usedAFKCommand.add(uuid);
lastMovement.put(uuid, time - getAfkThreshold()); 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); Long lastMoved = lastMovement.getOrDefault(uuid, time);
// Ignore afk permission // Ignore afk permission
if (lastMoved == -1) { if (lastMoved == -1) {
return; return 0L;
} }
lastMovement.put(uuid, time); lastMovement.put(uuid, time);
try { try {
if (time - lastMoved < getAfkThreshold()) { if (time - lastMoved < getAfkThreshold()) {
// Threshold not crossed, no action required. // Threshold not crossed, no action required.
return; return 0L;
} }
long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? getAfkThreshold() : 0; long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? getAfkThreshold() : 0;
@ -75,15 +79,17 @@ public class AFKTracker {
SessionCache.getCachedSession(uuid) SessionCache.getCachedSession(uuid)
.ifPresent(session -> session.addAfkTime(timeAFK)); .ifPresent(session -> session.addAfkTime(timeAFK));
return timeAFK;
} finally { } finally {
usedAFKCommand.remove(uuid); usedAFKCommand.remove(uuid);
} }
} }
public void loggedOut(UUID uuid, long time) { public long loggedOut(UUID uuid, long time) {
performedAction(uuid, time); long timeAFK = performedAction(uuid, time);
lastMovement.remove(uuid); lastMovement.remove(uuid);
usedAFKCommand.remove(uuid); usedAFKCommand.remove(uuid);
return timeAFK;
} }
public boolean isAfk(UUID uuid) { public boolean isAfk(UUID uuid) {

View File

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