From 0294dc4ec836b6ae4ebec015b8b19369657c3dcb Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 5 Aug 2018 11:22:42 +0300 Subject: [PATCH] Attempt to fix #656 #655 & wrote tests to ensure Session functionality --- .../plan/data/container/Session.java | 70 ++++++++++++++----- .../djrapitops/plan/data/time/TimeKeeper.java | 8 +-- .../djrapitops/plan/data/time/WorldTimes.java | 5 +- .../plan/data/container/SessionTest.java | 51 ++++++++++++++ .../plan/data/time/WorldTimesTest.java | 4 +- 5 files changed, 113 insertions(+), 25 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/container/Session.java b/Plan/src/main/java/com/djrapitops/plan/data/container/Session.java index 7b49ffc4c..a6ab9953a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/container/Session.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/container/Session.java @@ -16,6 +16,10 @@ import java.util.*; */ public class Session extends DataContainer implements DateHolder { + private long sessionStart; + private WorldTimes worldTimes; + private List playerKills; + private int mobKills; private int deaths; private long afkTime; @@ -29,18 +33,22 @@ public class Session extends DataContainer implements DateHolder { * @param gm Starting GameMode. */ public Session(UUID uuid, long sessionStart, String world, String gm) { + this.sessionStart = sessionStart; + worldTimes = new WorldTimes(world, gm, sessionStart); + playerKills = new ArrayList<>(); + mobKills = 0; deaths = 0; afkTime = 0; putRawData(SessionKeys.UUID, uuid); - putRawData(SessionKeys.START, sessionStart); - putRawData(SessionKeys.WORLD_TIMES, new WorldTimes(world, gm)); - putRawData(SessionKeys.PLAYER_KILLS, new ArrayList<>()); + putSupplier(SessionKeys.START, this::getSessionStart); + putSupplier(SessionKeys.WORLD_TIMES, this::getWorldTimes); + putSupplier(SessionKeys.PLAYER_KILLS, this::getPlayerKills); putRawData(SessionKeys.PLAYER_DEATHS, new ArrayList<>()); - putSupplier(SessionKeys.MOB_KILL_COUNT, () -> mobKills); - putSupplier(SessionKeys.DEATH_COUNT, () -> deaths); - putSupplier(SessionKeys.AFK_TIME, () -> afkTime); + putSupplier(SessionKeys.MOB_KILL_COUNT, this::getMobKills); + putSupplier(SessionKeys.DEATH_COUNT, this::getDeaths); + putSupplier(SessionKeys.AFK_TIME, this::getAfkTime); putSupplier(SessionKeys.PLAYER_KILL_COUNT, getUnsafe(SessionKeys.PLAYER_KILLS)::size); putSupplier(SessionKeys.LENGTH, () -> @@ -64,22 +72,26 @@ public class Session extends DataContainer implements DateHolder { * @param afkTime Time spent AFK during the session. */ public Session(int id, UUID uuid, UUID serverUUID, long sessionStart, long sessionEnd, int mobKills, int deaths, long afkTime) { - putRawData(SessionKeys.DB_ID, id); - putRawData(SessionKeys.UUID, uuid); - putRawData(SessionKeys.SERVER_UUID, serverUUID); - putRawData(SessionKeys.START, sessionStart); - putRawData(SessionKeys.END, sessionEnd); - putRawData(SessionKeys.WORLD_TIMES, new WorldTimes(new HashMap<>())); - putRawData(SessionKeys.PLAYER_KILLS, new ArrayList<>()); - putRawData(SessionKeys.PLAYER_DEATHS, new ArrayList<>()); - putSupplier(SessionKeys.MOB_KILL_COUNT, () -> mobKills); - putSupplier(SessionKeys.DEATH_COUNT, () -> deaths); - putSupplier(SessionKeys.AFK_TIME, () -> afkTime); + this.sessionStart = sessionStart; + worldTimes = new WorldTimes(new HashMap<>()); + playerKills = new ArrayList<>(); this.mobKills = mobKills; this.deaths = deaths; this.afkTime = afkTime; + putRawData(SessionKeys.DB_ID, id); + putRawData(SessionKeys.UUID, uuid); + putRawData(SessionKeys.SERVER_UUID, serverUUID); + putSupplier(SessionKeys.START, this::getSessionStart); + putRawData(SessionKeys.END, sessionEnd); + putSupplier(SessionKeys.WORLD_TIMES, this::getWorldTimes); + putSupplier(SessionKeys.PLAYER_KILLS, this::getPlayerKills); + putRawData(SessionKeys.PLAYER_DEATHS, new ArrayList<>()); + putSupplier(SessionKeys.MOB_KILL_COUNT, this::getMobKills); + putSupplier(SessionKeys.DEATH_COUNT, this::getDeaths); + putSupplier(SessionKeys.AFK_TIME, this::getAfkTime); + putSupplier(SessionKeys.PLAYER_KILL_COUNT, () -> getUnsafe(SessionKeys.PLAYER_KILLS).size()); putSupplier(SessionKeys.LENGTH, () -> getValue(SessionKeys.END).orElse(System.currentTimeMillis()) - getUnsafe(SessionKeys.START)); @@ -184,4 +196,28 @@ public class Session extends DataContainer implements DateHolder { public int hashCode() { return Objects.hash(super.hashCode(), mobKills, deaths, afkTime); } + + private long getSessionStart() { + return sessionStart; + } + + private WorldTimes getWorldTimes() { + return worldTimes; + } + + private List getPlayerKills() { + return playerKills; + } + + private int getMobKills() { + return mobKills; + } + + private int getDeaths() { + return deaths; + } + + private long getAfkTime() { + return afkTime; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java b/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java index 8fdd617e7..3a6b7583d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java @@ -72,19 +72,19 @@ public abstract class TimeKeeper { * Adds time to the last state while updating the status of other parameters. * * @param newState New State seen in. - * @param playTime Current Playtime. + * @param ms Epoch ms the change occurred. * @throws IllegalArgumentException If newState is null. */ - public void changeState(String newState, long playTime) { + public void changeState(String newState, long ms) { Verify.nullCheck(newState); if (state == null) { state = newState; } Long currentTime = times.getOrDefault(state, 0L); - long diff = playTime - lastStateChange; + long diff = ms - lastStateChange; times.put(state, currentTime + Math.abs(diff)); state = newState; - lastStateChange = playTime; + lastStateChange = ms; } protected void resetState(String state) { diff --git a/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java b/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java index 5edbb07c0..096387071 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java @@ -21,12 +21,13 @@ public class WorldTimes { * * @param startingWorld World to start the calculations at. * @param startingGM GameMode to start the calculations at. + * @param time Epoch ms the time calculation should start */ - public WorldTimes(String startingWorld, String startingGM) { + public WorldTimes(String startingWorld, String startingGM, long time) { worldTimes = new HashMap<>(); currentWorld = startingWorld; currentGamemode = startingGM; - addWorld(startingWorld, startingGM, System.currentTimeMillis()); + addWorld(startingWorld, startingGM, time); } /** diff --git a/Plan/src/test/java/com/djrapitops/plan/data/container/SessionTest.java b/Plan/src/test/java/com/djrapitops/plan/data/container/SessionTest.java index 9a99ae561..d58898680 100644 --- a/Plan/src/test/java/com/djrapitops/plan/data/container/SessionTest.java +++ b/Plan/src/test/java/com/djrapitops/plan/data/container/SessionTest.java @@ -1,7 +1,14 @@ package com.djrapitops.plan.data.container; import com.djrapitops.plan.data.store.keys.SessionKeys; +import com.djrapitops.plan.data.time.WorldTimes; import org.junit.Test; +import utilities.TestConstants; + +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.*; /** * Test for {@link Session} {@link com.djrapitops.plan.data.store.containers.DataContainer}. @@ -31,4 +38,48 @@ public class SessionTest { } } + @Test + public void killsAreAdded() { + Session session = new Session(null, System.currentTimeMillis(), "", ""); + + Optional> beforeOptional = session.getValue(SessionKeys.PLAYER_KILLS); + assertTrue(beforeOptional.isPresent()); + List before = beforeOptional.get(); + assertTrue(before.isEmpty()); + + session.playerKilled(new PlayerKill(TestConstants.PLAYER_TWO_UUID, "Weapon", System.currentTimeMillis())); + + Optional> afterOptional = session.getValue(SessionKeys.PLAYER_KILLS); + assertTrue(afterOptional.isPresent()); + List after = afterOptional.get(); + + assertFalse(after.isEmpty()); + assertEquals(before, after); + } + + @Test + public void killsAreAdded2() { + Session session = new Session(null, System.currentTimeMillis(), "", ""); + + session.playerKilled(new PlayerKill(TestConstants.PLAYER_TWO_UUID, "Weapon", System.currentTimeMillis())); + + Optional> afterOptional = session.getValue(SessionKeys.PLAYER_KILLS); + assertTrue(afterOptional.isPresent()); + List after = afterOptional.get(); + + assertFalse(after.isEmpty()); + } + + @Test + public void worldTimesWorks() { + long time = System.currentTimeMillis(); + Session session = new Session(null, time, "One", "Survival"); + session.changeState("Two", "Three", time + 5L); + + Optional optional = session.getValue(SessionKeys.WORLD_TIMES); + assertTrue(optional.isPresent()); + WorldTimes worldTimes = optional.get(); + + assertEquals(5L, worldTimes.getGMTimes("One").getTotal()); + } } \ No newline at end of file diff --git a/Plan/src/test/java/com/djrapitops/plan/data/time/WorldTimesTest.java b/Plan/src/test/java/com/djrapitops/plan/data/time/WorldTimesTest.java index 72c0dad0e..d85dca4c4 100644 --- a/Plan/src/test/java/com/djrapitops/plan/data/time/WorldTimesTest.java +++ b/Plan/src/test/java/com/djrapitops/plan/data/time/WorldTimesTest.java @@ -18,8 +18,8 @@ public class WorldTimesTest { private final String worldOne = "ONE"; private final String worldTwo = "TWO"; private final String[] gms = GMTimes.getGMKeyArray(); - private WorldTimes worldTimes = new WorldTimes(worldOne, gms[0]); - private long time = worldTimes.getGMTimes(worldOne).getLastStateChange(); + private long time = System.currentTimeMillis(); + private WorldTimes worldTimes = new WorldTimes(worldOne, gms[0], time); @Test public void testWorldChange() {