From 437345cded0728b7975d845b4a1e6c652c5f9051 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 21 Aug 2017 17:55:49 +0300 Subject: [PATCH] Changes WorldTimes to work with GMTimes, Breaks the Database, Grathering & Analysis. --- .../com/djrapitops/plan/data/UserData.java | 2 +- .../plan/data/analysis/WorldPart.java | 6 +- .../plan/data/handling/PlaytimeHandling.java | 2 +- .../djrapitops/plan/data/time/WorldTimes.java | 86 +++++++-- .../plan/database/databases/SQLDB.java | 38 ++-- .../plan/database/tables/UsersTable.java | 72 +++---- .../plan/utilities/PlaceholderUtils.java | 5 +- .../plan/utilities/analysis/Analysis.java | 8 +- .../data/handling/info/LoginInfoTest.java | 2 +- .../data/handling/info/LogoutInfoTest.java | 2 +- .../data/handling/info/ReloadInfoTest.java | 2 +- .../plan/data/time/WorldTimesTest.java | 180 ++++++++++++++++++ .../plan/database/DatabaseTest.java | 8 +- 13 files changed, 321 insertions(+), 92 deletions(-) create mode 100644 Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java diff --git a/Plan/src/main/java/com/djrapitops/plan/data/UserData.java b/Plan/src/main/java/com/djrapitops/plan/data/UserData.java index 2497d2f24..9a4668cc8 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/UserData.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/UserData.java @@ -67,7 +67,7 @@ public class UserData { accessing = 0; this.gmTimes = new GMTimes(gm); - this.worldTimes = new WorldTimes(); +// TODO REMOVE this.worldTimes = new WorldTimes(); this.uuid = uuid; this.name = name; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java index 46687850f..431cb71c3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java @@ -1,8 +1,6 @@ package main.java.com.djrapitops.plan.data.analysis; -import main.java.com.djrapitops.plan.data.time.WorldTimes; import main.java.com.djrapitops.plan.ui.html.graphs.WorldPieCreator; -import main.java.com.djrapitops.plan.utilities.FormatUtils; import java.util.HashMap; import java.util.Map; @@ -31,8 +29,8 @@ public class WorldPart extends RawData { @Override protected void analyse() { - WorldTimes t = new WorldTimes(worldTimes); - addValue("worldTotal", FormatUtils.formatTimeAmount(t.getTotal())); +// TODO WorldTimes t = new WorldTimes(worldTimes); +// addValue("worldTotal", FormatUtils.formatTimeAmount(t.getTotal())); addValue("worldSeries", WorldPieCreator.createSeriesData(worldTimes)); } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/handling/PlaytimeHandling.java b/Plan/src/main/java/com/djrapitops/plan/data/handling/PlaytimeHandling.java index aa94f9ca0..f8c481fd4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/handling/PlaytimeHandling.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/handling/PlaytimeHandling.java @@ -27,6 +27,6 @@ public class PlaytimeHandling { } WorldTimes worldTimes = data.getWorldTimes(); - worldTimes.changeState(worldName, playTime); +// TODO worldTimes.changeState(worldName, playTime); } } 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 938ac8800..bdeb603eb 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 @@ -1,6 +1,10 @@ package main.java.com.djrapitops.plan.data.time; +import main.java.com.djrapitops.plan.utilities.MiscUtils; + +import java.util.HashMap; import java.util.Map; +import java.util.Optional; /** * TimeKeeper class that tracks the time spent in each World based on Playtime. @@ -8,37 +12,85 @@ import java.util.Map; * @author Rsl1122 * @since 3.6.0 */ -// TODO Change WorldTimes to use GMTimes per world. -public class WorldTimes extends TimeKeeper { +public class WorldTimes { - public WorldTimes(Map times, String lastState, long lastStateChange) { - super(times, lastState, lastStateChange); + private final Map worldTimes; + private String currentWorld; + private String currentGamemode; + + /** + * Creates a new Empty WorldTimes object. + * + * @param startingWorld World to start the calculations at. + * @param startingGM GameMode to start the calculations at. + */ + public WorldTimes(String startingWorld, String startingGM) { + worldTimes = new HashMap<>(); + currentWorld = startingWorld; + currentGamemode = startingGM; + addWorld(startingWorld, startingGM, MiscUtils.getTime()); } - public WorldTimes(String lastState, long lastStateChange) { - super(lastState, lastStateChange); + public WorldTimes(Map times, String lastWorld, String lastGM) { + worldTimes = times; + currentWorld = lastWorld; + currentGamemode = lastGM; } - public WorldTimes(String lastState) { - super(lastState); + private void addWorld(String worldName, String gameMode, long changeTime) { + worldTimes.put(worldName, new GMTimes(gameMode, changeTime)); } - public WorldTimes(Map times, long lastStateChange) { - super(times, null, lastStateChange); + public void updateState(String worldName, String gameMode, long changeTime) { + GMTimes currentGMTimes = worldTimes.get(currentWorld); + + GMTimes newGMTimes = worldTimes.get(worldName); + if (newGMTimes == null) { + addWorld(worldName, gameMode, currentGMTimes.getLastStateChange()); + } + currentGMTimes.changeState(gameMode, changeTime); + for (GMTimes gmTimes : worldTimes.values()) { + gmTimes.setLastStateChange(changeTime); + } + worldTimes.put(currentWorld, currentGMTimes); + currentWorld = worldName; + currentGamemode = gameMode; } - public WorldTimes(Map times) { - super(times); + public Optional getWorldPlaytime(String world) { + GMTimes gmTimes = worldTimes.get(world); + if (gmTimes != null) { + return Optional.of(gmTimes.getTotal()); + } + return Optional.empty(); } - public WorldTimes() { - super(); + public Optional getGMTimes(String world) { + GMTimes gmTimes = worldTimes.get(world); + if (gmTimes != null) { + return Optional.of(gmTimes); + } + return Optional.empty(); } @Override - public String getState() { - String state = super.getState(); - return state != null ? state : "Unknown"; + public String toString() { + StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n"); + for (Map.Entry entry : worldTimes.entrySet()) { + b.append("World '").append(entry.getKey()).append("':\n"); + GMTimes value = entry.getValue(); + b.append(" Total: ").append(value.getTotal()).append("\n"); + b.append(" ").append(value.toString()).append("\n"); + } + b.append("}"); + return b.toString(); } + public String getCurrentWorld() { + return currentWorld; + } + + public String getCurrentGamemode() { + return currentGamemode; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java index b15186a8b..f8ae773b8 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java @@ -374,10 +374,10 @@ public abstract class SQLDB extends Database { data.getGmTimes().setTimes(gmTimes); Map worldTimes = worldTimesTable.getWorldTimes(userId); WorldTimes worldT = data.getWorldTimes(); - worldT.setTimes(worldTimes); - if (worldT.getLastStateChange() == 0) { - worldT.setLastStateChange(data.getPlayTime()); - } +// worldT.setTimes(worldTimes); //TODO +// if (worldT.getLastStateChange() == 0) { +// worldT.setLastStateChange(data.getPlayTime()); +// } List sessions = sessionsTable.getSessionData(userId); data.addSessions(sessions); @@ -448,10 +448,10 @@ public abstract class SQLDB extends Database { uData.setPlayerKills(playerKills.get(id)); uData.getGmTimes().setTimes(gmTimes.get(id)); WorldTimes worldT = uData.getWorldTimes(); - worldT.setTimes(worldTimes.get(id)); - if (worldT.getLastStateChange() == 0) { - worldT.setLastStateChange(uData.getPlayTime()); - } +// worldT.setTimes(worldTimes.get(id)); //TODO +// if (worldT.getLastStateChange() == 0) { +// worldT.setLastStateChange(uData.getPlayTime()); +// } } Benchmark.stop("Database", "Get UserData for " + uuidsCol.size()); @@ -493,13 +493,13 @@ public abstract class SQLDB extends Database { Map> worldTimes = new HashMap<>(); // Put in data set - List worldNames = data.stream() - .map(UserData::getWorldTimes) - .map(WorldTimes::getTimes) - .map(Map::keySet) - .flatMap(Collection::stream) - .distinct() - .collect(Collectors.toList()); +// List worldNames = data.stream() //TODO +// .map(UserData::getWorldTimes) +// .map(WorldTimes::getTimes) +// .map(Map::keySet) +// .flatMap(Collection::stream) +// .distinct() +// .collect(Collectors.toList()); for (Map.Entry entrySet : userDatas.entrySet()) { UUID uuid = entrySet.getKey(); @@ -518,7 +518,7 @@ public abstract class SQLDB extends Database { kills.put(id, new ArrayList<>(uData.getPlayerKills())); sessions.put(id, new ArrayList<>(uData.getSessions())); gmTimes.put(id, new HashMap<>(uData.getGmTimes().getTimes())); - worldTimes.put(id, new HashMap<>(uData.getWorldTimes().getTimes())); +// TODO worldTimes.put(id, new HashMap<>(uData.getWorldTimes().getTimes())); } // Save @@ -527,7 +527,7 @@ public abstract class SQLDB extends Database { killsTable.savePlayerKills(kills, uuids); sessionsTable.saveSessionData(sessions); gmTimesTable.saveGMTimes(gmTimes); - worldTable.saveWorlds(worldNames); +// TODO worldTable.saveWorlds(worldNames); worldTimesTable.saveWorldTimes(worldTimes); commit(); userDatas.values().stream() @@ -562,8 +562,8 @@ public abstract class SQLDB extends Database { ipsTable.saveIPList(userId, new HashSet<>(data.getIps())); killsTable.savePlayerKills(userId, new ArrayList<>(data.getPlayerKills())); gmTimesTable.saveGMTimes(userId, data.getGmTimes().getTimes()); - worldTable.saveWorlds(new HashSet<>(data.getWorldTimes().getTimes().keySet())); - worldTimesTable.saveWorldTimes(userId, data.getWorldTimes().getTimes()); +// TODO worldTable.saveWorlds(new HashSet<>(data.getWorldTimes().getTimes().keySet())); +// worldTimesTable.saveWorldTimes(userId, data.getWorldTimes().getTimes()); data.stopAccessing(); commit(); Benchmark.stop("Database", "Save Single UserData"); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java index 593de0209..2ec7059cb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java @@ -370,13 +370,13 @@ public class UsersTable extends Table { data.setDeaths(set.getInt(columnDeaths)); data.setMobKills(set.getInt(columnMobKills)); WorldTimes worldTimes = data.getWorldTimes(); - worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); - String lastWorld = set.getString(columnLastWorld); - if (!"Unknown".equals(lastWorld)) { - worldTimes.setState(lastWorld); - } else { - worldTimes.setLastStateChange(playTime); - } +// TODO worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); +// String lastWorld = set.getString(columnLastWorld); +// if (!"Unknown".equals(lastWorld)) { +// worldTimes.setState(lastWorld); +// } else { +// worldTimes.setLastStateChange(playTime); +// } return data; } } finally { @@ -415,13 +415,13 @@ public class UsersTable extends Table { data.setDeaths(set.getInt(columnDeaths)); data.setMobKills(set.getInt(columnMobKills)); WorldTimes worldTimes = data.getWorldTimes(); - worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); - String lastWorld = set.getString(columnLastWorld); - if (!"Unknown".equals(lastWorld)) { - worldTimes.setState(lastWorld); - } else { - worldTimes.setLastStateChange(playTime); - } +// TODO worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); +// String lastWorld = set.getString(columnLastWorld); +// if (!"Unknown".equals(lastWorld)) { +// worldTimes.setState(lastWorld); +// } else { +// worldTimes.setLastStateChange(playTime); +// } datas.add(data); } } finally { @@ -454,13 +454,13 @@ public class UsersTable extends Table { data.setDeaths(set.getInt(columnDeaths)); data.setMobKills(set.getInt(columnMobKills)); WorldTimes worldTimes = data.getWorldTimes(); - worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); - String lastWorld = set.getString(columnLastWorld); - if (!"Unknown".equals(lastWorld)) { - worldTimes.setState(lastWorld); - } else { - worldTimes.setLastStateChange(playTime); - } +// TODO worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); +// String lastWorld = set.getString(columnLastWorld); +// if (!"Unknown".equals(lastWorld)) { +// worldTimes.setState(lastWorld); +// } else { +// worldTimes.setLastStateChange(playTime); +// } } } finally { close(set); @@ -497,13 +497,13 @@ public class UsersTable extends Table { gmTimes.setState(set.getString(columnLastGM)); gmTimes.setLastStateChange(set.getLong(columnLastGMSwapTime)); WorldTimes worldTimes = uData.getWorldTimes(); - worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); - String lastWorld = set.getString(columnLastWorld); - if (!"Unknown".equals(lastWorld)) { - worldTimes.setState(lastWorld); - } else { - worldTimes.setLastStateChange(playTime); - } +// TODO worldTimes.setLastStateChange(set.getLong(columnLastWorldSwapTime)); +// String lastWorld = set.getString(columnLastWorld); +// if (!"Unknown".equals(lastWorld)) { +// worldTimes.setState(lastWorld); +// } else { +// worldTimes.setLastStateChange(playTime); +// } } } finally { close(set); @@ -540,8 +540,8 @@ public class UsersTable extends Table { statement.setString(12, data.getName()); statement.setLong(13, data.getRegistered()); WorldTimes worldTimes = data.getWorldTimes(); - statement.setString(14, worldTimes.getState()); - statement.setLong(15, worldTimes.getLastStateChange()); +// TODO statement.setString(14, worldTimes.getState()); +// statement.setLong(15, worldTimes.getLastStateChange()); statement.setString(16, uuid.toString()); update = statement.executeUpdate(); } @@ -565,8 +565,8 @@ public class UsersTable extends Table { statement.setString(13, data.getName()); statement.setLong(14, data.getRegistered()); WorldTimes worldTimes = data.getWorldTimes(); - statement.setString(15, worldTimes.getState()); - statement.setLong(16, worldTimes.getLastStateChange()); +// TODO statement.setString(15, worldTimes.getState()); +// statement.setLong(16, worldTimes.getLastStateChange()); statement.execute(); } } finally { @@ -701,8 +701,8 @@ public class UsersTable extends Table { statement.setLong(14, uData.getRegistered()); WorldTimes worldTimes = uData.getWorldTimes(); - statement.setString(15, worldTimes.getState()); - statement.setLong(16, worldTimes.getLastStateChange()); +// TODO statement.setString(15, worldTimes.getState()); +// statement.setLong(16, worldTimes.getLastStateChange()); statement.addBatch(); } @@ -759,8 +759,8 @@ public class UsersTable extends Table { statement.setString(12, uData.getName()); statement.setLong(13, uData.getRegistered()); WorldTimes worldTimes = uData.getWorldTimes(); - statement.setString(14, worldTimes.getState()); - statement.setLong(15, worldTimes.getLastStateChange()); +// TODO statement.setString(14, worldTimes.getState()); +// statement.setLong(15, worldTimes.getLastStateChange()); statement.setString(16, uuid.toString()); statement.addBatch(); uData.stopAccessing(); diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/PlaceholderUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/PlaceholderUtils.java index 26da280a9..e12fe5aeb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/PlaceholderUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/PlaceholderUtils.java @@ -7,7 +7,6 @@ import main.java.com.djrapitops.plan.data.SessionData; import main.java.com.djrapitops.plan.data.UserData; import main.java.com.djrapitops.plan.data.time.WorldTimes; import main.java.com.djrapitops.plan.ui.html.graphs.PunchCardGraphCreator; -import main.java.com.djrapitops.plan.ui.html.graphs.WorldPieCreator; import java.io.Serializable; import java.util.*; @@ -79,8 +78,8 @@ public class PlaceholderUtils { Set sessions = new HashSet<>(data.getSessions()); replaceMap.put("punchCardSeries", PunchCardGraphCreator.createDataSeries(sessions)); WorldTimes worldTimes = data.getWorldTimes(); - replaceMap.put("worldSeries", WorldPieCreator.createSeriesData(worldTimes.getTimes())); - replaceMap.put("worldTotal", FormatUtils.formatTimeAmount(worldTimes.getTotal())); +// TODO replaceMap.put("worldSeries", WorldPieCreator.createSeriesData(worldTimes.getTimes())); +// replaceMap.put("worldTotal", FormatUtils.formatTimeAmount(worldTimes.getTotal())); //TODO Plugin Tab content Web API //TODO Player Plugin tab code. diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java index 4be514ada..ba89405bc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java @@ -277,10 +277,10 @@ public class Analysis { } } } - Map worldTimes = uData.getWorldTimes().getTimes(); - for (Map.Entry world : worldTimes.entrySet()) { - worldPart.addToWorld(world.getKey(), world.getValue()); - } +// TODO Map worldTimes = uData.getWorldTimes().getTimes(); +// for (Map.Entry world : worldTimes.entrySet()) { +// worldPart.addToWorld(world.getKey(), world.getValue()); +// } final long playTime = uData.getPlayTime(); playtime.addToPlaytime(playTime); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LoginInfoTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LoginInfoTest.java index 59b2dab5d..0f6d72859 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LoginInfoTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LoginInfoTest.java @@ -63,6 +63,6 @@ public class LoginInfoTest { String geo = data.getGeolocation(); assertTrue("Wrong location " + geo, geo.equals("United States")); assertEquals("CREATIVE", data.getGmTimes().getState()); - assertEquals("World", data.getWorldTimes().getState()); +// TODO assertEquals("World", data.getWorldTimes().getState()); } } diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LogoutInfoTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LogoutInfoTest.java index 2ff92c731..955f82aa0 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LogoutInfoTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/LogoutInfoTest.java @@ -49,7 +49,7 @@ public class LogoutInfoTest { assertTrue("Playtime wrong", data.getPlayTime() == 10L); assertTrue("Banned wrong", data.isBanned()); assertEquals("CREATIVE", data.getGmTimes().getState()); - assertEquals("World", data.getWorldTimes().getState()); +// TODO assertEquals("World", data.getWorldTimes().getState()); assertEquals(1, data.getSessions().size()); } } diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/ReloadInfoTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/ReloadInfoTest.java index 329e38ded..04bfc7108 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/ReloadInfoTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/handling/info/ReloadInfoTest.java @@ -63,6 +63,6 @@ public class ReloadInfoTest { assertEquals(nick, data.getLastNick()); assertEquals("United States", data.getGeolocation()); assertEquals("CREATIVE", data.getGmTimes().getState()); - assertEquals("World", data.getWorldTimes().getState()); +// TODO assertEquals("World", data.getWorldTimes().getState()); } } diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java new file mode 100644 index 000000000..52cff0aff --- /dev/null +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java @@ -0,0 +1,180 @@ +package test.java.main.java.com.djrapitops.plan.data.time; + +import main.java.com.djrapitops.plan.data.time.GMTimes; +import main.java.com.djrapitops.plan.data.time.WorldTimes; +import org.junit.Before; +import org.junit.Test; +import test.java.utils.RandomData; + +import java.util.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author Rsl1122 + */ +public class WorldTimesTest { + + private long time; + private WorldTimes test; + private final String worldOne = "ONE"; + private final String worldTwo = "TWO"; + + private final String[] gms = GMTimes.getGMKeyArray(); + + @Before + public void setUp() throws Exception { + test = new WorldTimes(worldOne, gms[0]); + Optional gmTimes = test.getGMTimes(worldOne); + gmTimes.ifPresent(gmTimes1 -> + time = gmTimes1.getLastStateChange() + ); + System.out.println(test); + } + + @Test + public void testWorldChange() { + long changeTime = time + 1000L; + test.updateState(worldTwo, gms[0], changeTime); + System.out.println(test); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); + assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); + } + + @Test + public void testGMChange() { + long changeTime = time + 1000L; + test.updateState(worldOne, gms[0], changeTime); + System.out.println(test); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); + assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); + } + + @Test + public void testBothTwiceChange() { + long changeTime = time + 1000L; + long changeTime2 = changeTime + 1000L; + test.updateState(worldTwo, gms[2], changeTime); + System.out.println(test); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); + assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); + test.updateState(worldOne, gms[1], changeTime2); + System.out.println(test); + assertEquals(1000L, (long) test.getWorldPlaytime(worldOne).get()); + assertEquals(1000L, test.getGMTimes(worldOne).get().getTime(gms[0])); + assertEquals(1000L, test.getGMTimes(worldTwo).get().getTime(gms[2])); + } + + @Test + public void testLotOfChanges() { + long amount = 1000L; + String[] worlds = new String[]{worldOne, worldTwo}; + + Map> testedW = new HashMap<>(); + testedW.put(worldOne, new ArrayList<>()); + testedW.put(worldTwo, new ArrayList<>()); + + String lastWorld = worldOne; + String lastGM = gms[0]; + for (int i = 1; i <= 50; i++) { + int wRndm = RandomData.randomInt(0, worlds.length); + int gmRndm = RandomData.randomInt(0, gms.length); + + String world = worlds[wRndm]; + String gm = gms[gmRndm]; + testedW.get(lastWorld).add(lastGM); + lastGM = gm; + lastWorld = world; + + long time = i * amount + this.time; + + printCurrentState(testedW, i, gm, world); + test.updateState(world, gm, time); + } + + long worldOneCount = testedW.get(worldOne).size(); + long worldTwoCount = testedW.get(worldTwo).size(); + long worldTimeOne = worldOneCount * amount; + long worldTimeTwo = worldTwoCount * amount; + + long time1 = 0L; + long time2 = 0L; + Optional worldPlaytime = test.getWorldPlaytime(worldOne); + if (worldPlaytime.isPresent()) { + time1 += worldPlaytime.get(); + } + Optional worldPlaytime2 = test.getWorldPlaytime(worldTwo); + if (worldPlaytime2.isPresent()) { + time2 += worldPlaytime2.get(); + } + System.out.println(test); + + // Tests World time calculation. + assertEquals(amount * 50, time1 + time2); + assertEquals(worldTimeOne, time1); + assertEquals(worldTimeTwo, time2); + + // Tests GM Time calculation + for (Map.Entry> entry : testedW.entrySet()) { + String world = entry.getKey(); + List gmList = entry.getValue(); + for (int i = 0; i < gms.length; i++) { + final String lookFor = gms[i]; + long gmCount = gmList.stream().filter(gmNum -> gmNum.equals(lookFor)).count(); + + Optional gmTimes = test.getGMTimes(world); + + if (gmTimes.isPresent()) { + long expected = gmCount * amount; + long actual = gmTimes.get().getTime(lookFor); + assertEquals(world + ": " + lookFor + ": " + expected + " Actual: " + actual, expected, actual); + } else { + fail("GM Times can't not be present."); + } + } + } + } + + private void printCurrentState(Map> testedW, int i, String gm, String world) { + int sizeW1 = testedW.get(worldOne).size(); + int sizeW2 = testedW.get(worldTwo).size(); + + StringBuilder b = new StringBuilder(""+i); + while (b.length() < 3) { + b.append(" "); + } + b.append(world).append(":").append(gm).append(": "); + while (b.length() < 18) { + b.append(" "); + } + b.append(sizeW1); + + while (b.length() < 21) { + b.append(" "); + } + + for (final String lookFor : gms) { + long count = testedW.get(worldOne).stream().filter(gmNum -> gmNum.equals(lookFor)).count(); + b.append(" ").append(count); + } + while (b.length() < 29) { + b.append(" "); + } + b.append(" |"); + for (final String lookFor : gms) { + long count = testedW.get(worldTwo).stream().filter(gmNum -> gmNum.equals(lookFor)).count(); + b.append(" ").append(count); + } + while (b.length() < 40) { + b.append(" "); + } + b.append(" ") + .append(sizeW2) + .append(" = ") + .append(sizeW1 + sizeW2); + System.out.println(b.toString()); + } + + // TODO Test where SessionData is ended, check if worldTimes & session length add up. +} \ No newline at end of file diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java index 623308492..0798e9cf9 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java @@ -224,9 +224,9 @@ public class DatabaseTest { gmTimes.setState("SURVIVAL"); gmTimes.setLastStateChange(10L); WorldTimes worldTimes = data.getWorldTimes(); - worldTimes.setTime("World", 20L); - worldTimes.setState("World"); - worldTimes.setLastStateChange(10L); +// TODO worldTimes.setTime("World", 20L); +// worldTimes.setState("World"); +// worldTimes.setLastStateChange(10L); db.saveUserData(data); data.addNickname("TestUpdateForSave"); db.saveUserData(data); @@ -261,7 +261,7 @@ public class DatabaseTest { db.init(); UserData data = MockUtils.mockUser(); data.getGmTimes().setAllGMTimes(5L, 10L, 15L, 20L); - data.getWorldTimes().setTime("World", 20L); +// TODO data.getWorldTimes().setTime("World", 20L); data.addIpAddress(InetAddress.getByName("185.64.113.61")); data.addSession(new SessionData(1286349L, 2342978L)); data.addNickname("TestNick");