Attempt to fix #656 #655 & wrote tests to ensure Session functionality

This commit is contained in:
Rsl1122 2018-08-05 11:22:42 +03:00
parent 0b6d6cb687
commit 0294dc4ec8
5 changed files with 113 additions and 25 deletions

View File

@ -16,6 +16,10 @@ import java.util.*;
*/
public class Session extends DataContainer implements DateHolder {
private long sessionStart;
private WorldTimes worldTimes;
private List<PlayerKill> 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<PlayerKill> getPlayerKills() {
return playerKills;
}
private int getMobKills() {
return mobKills;
}
private int getDeaths() {
return deaths;
}
private long getAfkTime() {
return afkTime;
}
}

View File

@ -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) {

View File

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

View File

@ -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<List<PlayerKill>> beforeOptional = session.getValue(SessionKeys.PLAYER_KILLS);
assertTrue(beforeOptional.isPresent());
List<PlayerKill> before = beforeOptional.get();
assertTrue(before.isEmpty());
session.playerKilled(new PlayerKill(TestConstants.PLAYER_TWO_UUID, "Weapon", System.currentTimeMillis()));
Optional<List<PlayerKill>> afterOptional = session.getValue(SessionKeys.PLAYER_KILLS);
assertTrue(afterOptional.isPresent());
List<PlayerKill> 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<List<PlayerKill>> afterOptional = session.getValue(SessionKeys.PLAYER_KILLS);
assertTrue(afterOptional.isPresent());
List<PlayerKill> 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<WorldTimes> optional = session.getValue(SessionKeys.WORLD_TIMES);
assertTrue(optional.isPresent());
WorldTimes worldTimes = optional.get();
assertEquals(5L, worldTimes.getGMTimes("One").getTotal());
}
}

View File

@ -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() {