Changes WorldTimes to work with GMTimes, Breaks the Database, Grathering & Analysis.

This commit is contained in:
Rsl1122 2017-08-21 17:55:49 +03:00
parent 7245a02c2f
commit 437345cded
13 changed files with 321 additions and 92 deletions

View File

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

View File

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

View File

@ -27,6 +27,6 @@ public class PlaytimeHandling {
}
WorldTimes worldTimes = data.getWorldTimes();
worldTimes.changeState(worldName, playTime);
// TODO worldTimes.changeState(worldName, playTime);
}
}

View File

@ -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<String, Long> times, String lastState, long lastStateChange) {
super(times, lastState, lastStateChange);
private final Map<String, GMTimes> 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<String, GMTimes> 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<String, Long> 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<String, Long> times) {
super(times);
public Optional<Long> getWorldPlaytime(String world) {
GMTimes gmTimes = worldTimes.get(world);
if (gmTimes != null) {
return Optional.of(gmTimes.getTotal());
}
return Optional.empty();
}
public WorldTimes() {
super();
public Optional<GMTimes> 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<String, GMTimes> 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;
}
}

View File

@ -374,10 +374,10 @@ public abstract class SQLDB extends Database {
data.getGmTimes().setTimes(gmTimes);
Map<String, Long> 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<SessionData> 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<Integer, Map<String, Long>> worldTimes = new HashMap<>();
// Put in data set
List<String> worldNames = data.stream()
.map(UserData::getWorldTimes)
.map(WorldTimes::getTimes)
.map(Map::keySet)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
// List<String> worldNames = data.stream() //TODO
// .map(UserData::getWorldTimes)
// .map(WorldTimes::getTimes)
// .map(Map::keySet)
// .flatMap(Collection::stream)
// .distinct()
// .collect(Collectors.toList());
for (Map.Entry<UUID, UserData> 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");

View File

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

View File

@ -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<SessionData> 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.

View File

@ -277,10 +277,10 @@ public class Analysis {
}
}
}
Map<String, Long> worldTimes = uData.getWorldTimes().getTimes();
for (Map.Entry<String, Long> world : worldTimes.entrySet()) {
worldPart.addToWorld(world.getKey(), world.getValue());
}
// TODO Map<String, Long> worldTimes = uData.getWorldTimes().getTimes();
// for (Map.Entry<String, Long> world : worldTimes.entrySet()) {
// worldPart.addToWorld(world.getKey(), world.getValue());
// }
final long playTime = uData.getPlayTime();
playtime.addToPlaytime(playTime);

View File

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

View File

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

View File

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

View File

@ -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> 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<String, List<String>> 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<Long> worldPlaytime = test.getWorldPlaytime(worldOne);
if (worldPlaytime.isPresent()) {
time1 += worldPlaytime.get();
}
Optional<Long> 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<String, List<String>> entry : testedW.entrySet()) {
String world = entry.getKey();
List<String> 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> 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<String, List<String>> 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.
}

View File

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