Merge branch '4.0.0-BungeeCord-Support'

This commit is contained in:
Fuzzlemann 2017-08-21 18:27:11 +02:00
commit 9ccee9a9d6
6 changed files with 77 additions and 432 deletions

View File

@ -12,11 +12,10 @@ import java.util.Map;
*/
public class GMTimes extends TimeKeeper {
// TODO Make private once GMTimesTable is removed
public static final String SURVIVAL = "SURVIVAL";
public static final String CREATIVE = "CREATIVE";
public static final String ADVENTURE = "ADVENTURE";
public static final String SPECTATOR = "SPECTATOR";
private static final String SURVIVAL = "SURVIVAL";
private static final String CREATIVE = "CREATIVE";
private static final String ADVENTURE = "ADVENTURE";
private static final String SPECTATOR = "SPECTATOR";
public GMTimes(Map<String, Long> times, String lastState, long lastStateChange) {
super(times, lastState, lastStateChange);
@ -67,8 +66,8 @@ public class GMTimes extends TimeKeeper {
}
}
public void resetTimes(long playtime) {
resetState(SURVIVAL, playtime);
public void resetTimes(long time) {
resetState(SURVIVAL, time);
resetState(CREATIVE);
resetState(ADVENTURE);
resetState(SPECTATOR);

View File

@ -4,13 +4,12 @@ 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.
* Class that tracks the time spent in each World based on GMTimes.
*
* @author Rsl1122
* @since 3.6.0
* @since 4.0.0
*/
public class WorldTimes {
@ -31,16 +30,26 @@ public class WorldTimes {
addWorld(startingWorld, startingGM, MiscUtils.getTime());
}
public WorldTimes(Map<String, GMTimes> times, String lastWorld, String lastGM) {
/**
* Re-Creates an existing WorldTimes object for viewing.
*
* @param times Map of each World's GMTimes object.
*/
public WorldTimes(Map<String, GMTimes> times) {
worldTimes = times;
currentWorld = lastWorld;
currentGamemode = lastGM;
}
private void addWorld(String worldName, String gameMode, long changeTime) {
worldTimes.put(worldName, new GMTimes(gameMode, changeTime));
}
/**
* Updates the time status to match the new state.
*
* @param worldName World name of the world swapped to.
* @param gameMode GameMode name of the gm swapped to.
* @param changeTime Epoch ms the change occurred.
*/
public void updateState(String worldName, String gameMode, long changeTime) {
GMTimes currentGMTimes = worldTimes.get(currentWorld);
if (worldName.equals(currentWorld)) {
@ -59,20 +68,42 @@ public class WorldTimes {
currentGamemode = gameMode;
}
public Optional<Long> getWorldPlaytime(String world) {
/**
* Used to get a total playtime of a world.
*
* @param world World name being checked.
* @return total milliseconds spent in a world.
*/
public long getWorldPlaytime(String world) {
GMTimes gmTimes = worldTimes.get(world);
if (gmTimes != null) {
return Optional.of(gmTimes.getTotal());
return gmTimes.getTotal();
}
return Optional.empty();
return 0;
}
public Optional<GMTimes> getGMTimes(String world) {
public long getTotal() {
return worldTimes.values().stream()
.mapToLong(GMTimes::getTotal)
.sum();
}
/**
* Used for Quick access to time of each GameMode.
* <p>
* Should not be used for changing state,
* because if player has not played in the world,
* an empty GMTimes is given, with 0 as playtime
*
* @param world World name being checked.
* @return GMTimes object with play times of each GameMode.
*/
public GMTimes getGMTimes(String world) {
GMTimes gmTimes = worldTimes.get(world);
if (gmTimes != null) {
return Optional.of(gmTimes);
return gmTimes;
}
return Optional.empty();
return new GMTimes();
}
@Override
@ -88,11 +119,12 @@ public class WorldTimes {
return b.toString();
}
public String getCurrentWorld() {
return currentWorld;
}
public String getCurrentGamemode() {
return currentGamemode;
/**
* Used to get the Map for saving.
*
* @return Current time map.
*/
public Map<String, GMTimes> getWorldTimes() {
return worldTimes;
}
}

View File

@ -28,11 +28,6 @@ public abstract class Database {
*/
protected UsersTable usersTable;
/**
* Table representing plan_gamemodetimes in the database.
*/
protected GMTimesTable gmTimesTable;
/**
* Table representing plan_kills in the database.
*/
@ -294,15 +289,6 @@ public abstract class Database {
return sessionsTable;
}
/**
* Used to get the gm times table.
*
* @return Table representing plan_gamemodetimes
*/
public GMTimesTable getGmTimesTable() {
return gmTimesTable;
}
/**
* Used to get the kills table.
*

View File

@ -45,7 +45,6 @@ public abstract class SQLDB extends Database {
usingMySQL = getName().equals("MySQL");
usersTable = new UsersTable(this, usingMySQL);
gmTimesTable = new GMTimesTable(this, usingMySQL);
sessionsTable = new SessionsTable(this, usingMySQL);
killsTable = new KillsTable(this, usingMySQL);
ipsTable = new IPsTable(this, usingMySQL);
@ -226,7 +225,7 @@ public abstract class SQLDB extends Database {
*/
public Table[] getAllTables() {
return new Table[]{
usersTable, gmTimesTable, ipsTable,
usersTable, ipsTable,
nicknamesTable, sessionsTable, killsTable,
commandUseTable, tpsTable, worldTable,
worldTimesTable, securityTable};
@ -237,7 +236,7 @@ public abstract class SQLDB extends Database {
*/
public Table[] getAllTablesInRemoveOrder() {
return new Table[]{
gmTimesTable, ipsTable,
ipsTable,
nicknamesTable, sessionsTable, killsTable,
worldTimesTable, worldTable, usersTable,
commandUseTable, tpsTable};
@ -320,7 +319,6 @@ public abstract class SQLDB extends Database {
boolean success = userId != -1
&& ipsTable.removeUserIPs(userId)
&& nicknamesTable.removeUserNicknames(userId)
&& gmTimesTable.removeUserGMTimes(userId)
&& sessionsTable.removeUserSessions(userId)
&& killsTable.removeUserKillsAndVictims(userId)
&& worldTimesTable.removeUserWorldTimes(userId)
@ -369,9 +367,6 @@ public abstract class SQLDB extends Database {
List<InetAddress> ips = ipsTable.getIPAddresses(userId);
data.addIpAddresses(ips);
Map<String, Long> gmTimes = gmTimesTable.getGMTimes(userId);
data.getGmTimes().setTimes(gmTimes);
Map<String, Long> worldTimes = worldTimesTable.getWorldTimes(userId);
WorldTimes worldT = data.getWorldTimes();
// worldT.setTimes(worldTimes); //TODO
@ -419,7 +414,6 @@ public abstract class SQLDB extends Database {
Map<Integer, Set<InetAddress>> ipList = ipsTable.getIPList(ids);
Map<Integer, List<KillData>> playerKills = killsTable.getPlayerKills(ids, idUuidRel);
Map<Integer, List<SessionData>> sessionData = sessionsTable.getSessionData(ids);
Map<Integer, Map<String, Long>> gmTimes = gmTimesTable.getGMTimes(ids);
Map<Integer, Map<String, Long>> worldTimes = worldTimesTable.getWorldTimes(ids);
Log.debug("Database",
@ -431,7 +425,6 @@ public abstract class SQLDB extends Database {
" IPs: " + ipList.size(),
" Kills: " + playerKills.size(),
" Sessions: " + sessionData.size(),
" GM Times: " + gmTimes.size(),
" World Times: " + worldTimes.size()
);
@ -446,7 +439,6 @@ public abstract class SQLDB extends Database {
}
uData.addSessions(sessionData.get(id));
uData.setPlayerKills(playerKills.get(id));
uData.getGmTimes().setTimes(gmTimes.get(id));
WorldTimes worldT = uData.getWorldTimes();
// worldT.setTimes(worldTimes.get(id)); //TODO
// if (worldT.getLastStateChange() == 0) {
@ -526,7 +518,6 @@ public abstract class SQLDB extends Database {
ipsTable.saveIPList(ips);
killsTable.savePlayerKills(kills, uuids);
sessionsTable.saveSessionData(sessions);
gmTimesTable.saveGMTimes(gmTimes);
// TODO worldTable.saveWorlds(worldNames);
worldTimesTable.saveWorldTimes(worldTimes);
commit();
@ -561,7 +552,6 @@ public abstract class SQLDB extends Database {
nicknamesTable.saveNickList(userId, new HashSet<>(data.getNicknames()), data.getLastNick());
ipsTable.saveIPList(userId, new HashSet<>(data.getIps()));
killsTable.savePlayerKills(userId, new ArrayList<>(data.getPlayerKills()));
gmTimesTable.saveGMTimes(userId, data.getGmTimes().getTimes());
// TODO worldTable.saveWorlds(new HashSet<>(data.getWorldTimes().getTimes().keySet()));
// worldTimesTable.saveWorldTimes(userId, data.getWorldTimes().getTimes());
data.stopAccessing();

View File

@ -1,354 +0,0 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.database.Container;
import main.java.com.djrapitops.plan.database.DBUtils;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
/**
* @author Rsl1122
* @deprecated GM Times moved to WorldTable
*/
@Deprecated
public class GMTimesTable extends UserIDTable {
private final String columnSurvivalTime;
private final String columnCreativeTime;
private final String columnAdventureTime;
private final String columnSpectatorTime;
/**
* @param db
* @param usingMySQL
*/
public GMTimesTable(SQLDB db, boolean usingMySQL) {
super("plan_gamemodetimes", db, usingMySQL);
columnUserID = "user_id";
columnSurvivalTime = "SURVIVAL";
columnCreativeTime = "CREATIVE";
columnAdventureTime = "ADVENTURE";
columnSpectatorTime = "SPECTATOR";
}
/**
* @return
*/
@Override
public boolean createTable() {
UsersTable usersTable = db.getUsersTable();
try {
execute(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull()
.column(columnSurvivalTime, Sql.LONG).notNull()
.column(columnCreativeTime, Sql.LONG).notNull()
.column(columnAdventureTime, Sql.LONG).notNull()
.column(columnSpectatorTime, Sql.LONG).notNull()
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.toString()
);
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
}
}
/**
* @param userId
* @return
*/
public boolean removeUserGMTimes(int userId) {
return super.removeDataOf(userId);
}
/**
* @param userId
* @return
* @throws SQLException
*/
public Map<String, Long> getGMTimes(int userId) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT * FROM " + tableName + " WHERE (" + columnUserID + "=?)");
statement.setInt(1, userId);
set = statement.executeQuery();
HashMap<String, Long> times = new HashMap<>();
while (set.next()) {
times.put(GMTimes.SURVIVAL, set.getLong(columnSurvivalTime));
times.put(GMTimes.CREATIVE, set.getLong(columnCreativeTime));
times.put(GMTimes.ADVENTURE, set.getLong(columnAdventureTime));
times.put(GMTimes.SPECTATOR, set.getLong(columnSpectatorTime));
}
return times;
} finally {
close(set);
close(statement);
}
}
public Map<Integer, Map<String, Long>> getGMTimes(Collection<Integer> userIds) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
Map<Integer, Map<String, Long>> times = new HashMap<>();
try {
statement = prepareStatement("SELECT * FROM " + tableName);
set = statement.executeQuery();
while (set.next()) {
Map<String, Long> gmTimes = new HashMap<>();
int id = set.getInt(columnUserID);
if (!userIds.contains(id)) {
continue;
}
gmTimes.put(GMTimes.SURVIVAL, set.getLong(columnSurvivalTime));
gmTimes.put(GMTimes.CREATIVE, set.getLong(columnCreativeTime));
gmTimes.put(GMTimes.ADVENTURE, set.getLong(columnAdventureTime));
gmTimes.put(GMTimes.SPECTATOR, set.getLong(columnSpectatorTime));
times.put(id, gmTimes);
}
return times;
} finally {
close(set);
close(statement);
}
}
/**
* @param userId
* @param gamemodeTimes
* @throws SQLException
*/
public void saveGMTimes(int userId, Map<String, Long> gamemodeTimes) throws SQLException {
if (Verify.isEmpty(gamemodeTimes)) {
return;
}
PreparedStatement statement = null;
String[] gms = GMTimes.getGMKeyArray();
int update;
try {
statement = prepareStatement(
"UPDATE " + tableName + " SET "
+ columnSurvivalTime + "=?, "
+ columnCreativeTime + "=?, "
+ columnAdventureTime + "=?, "
+ columnSpectatorTime + "=? "
+ " WHERE (" + columnUserID + "=?)");
statement.setInt(5, userId);
for (int i = 0; i < gms.length; i++) {
Long time = gamemodeTimes.get(gms[i]);
statement.setLong(i + 1, time != null ? time : 0);
}
update = statement.executeUpdate();
} finally {
close(statement);
}
if (update == 0) {
addNewGMTimesRow(userId, gamemodeTimes);
}
}
private Set<Integer> getSavedIDs() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " + columnUserID + " FROM " + tableName);
set = statement.executeQuery();
Set<Integer> ids = new HashSet<>();
while (set.next()) {
ids.add(set.getInt(columnUserID));
}
return ids;
} finally {
close(set);
close(statement);
}
}
public void saveGMTimes(Map<Integer, Map<String, Long>> gamemodeTimes) throws SQLException {
if (Verify.isEmpty(gamemodeTimes)) {
return;
}
Set<Integer> savedIDs = getSavedIDs();
Map<Integer, GMTimes> gmTimes = new HashMap<>();
for (Map.Entry<Integer, Map<String, Long>> entrySet : gamemodeTimes.entrySet()) {
int userID = entrySet.getKey();
if (!savedIDs.contains(userID)) {
continue;
}
Map<String, Long> gmTimesMap = entrySet.getValue();
gmTimes.put(userID, new GMTimes(gmTimesMap));
}
List<List<Container<GMTimes>>> batches = DBUtils.splitIntoBatchesWithID(gmTimes);
batches.forEach(batch -> {
try {
saveGMTimesBatch(batch);
} catch (SQLException e) {
Log.toLog("GMTimesTable.saveGMTimes", e);
}
});
gamemodeTimes.keySet().removeAll(savedIDs);
addNewGMTimesRows(gamemodeTimes);
}
private void saveGMTimesBatch(List<Container<GMTimes>> batch) throws SQLException {
if (batch.isEmpty()) {
return;
}
String[] gms = GMTimes.getGMKeyArray();
Set<Integer> savedIDs = getSavedIDs();
PreparedStatement statement = null;
try {
statement = prepareStatement(
"UPDATE " + tableName + " SET "
+ columnSurvivalTime + "=?, "
+ columnCreativeTime + "=?, "
+ columnAdventureTime + "=?, "
+ columnSpectatorTime + "=? "
+ " WHERE (" + columnUserID + "=?)");
for (Container<GMTimes> data : batch) {
int id = data.getId();
if (!savedIDs.contains(id)) {
continue;
}
statement.setInt(5, id);
for (int i = 0; i < gms.length; i++) {
Map<String, Long> times = data.getObject().getTimes();
Long time = times.get(gms[i]);
statement.setLong(i + 1, time != null ? time : 0);
}
statement.addBatch();
}
statement.executeBatch();
} finally {
close(statement);
}
}
private void addNewGMTimesRows(Map<Integer, Map<String, Long>> gamemodeTimes) {
if (Verify.isEmpty(gamemodeTimes)) {
return;
}
Map<Integer, GMTimes> gmTimes = new HashMap<>();
for (Map.Entry<Integer, Map<String, Long>> entrySet : gamemodeTimes.entrySet()) {
int userID = entrySet.getKey();
Map<String, Long> gmTimesMap = entrySet.getValue();
gmTimes.put(userID, new GMTimes(gmTimesMap));
}
List<List<Container<GMTimes>>> batches = DBUtils.splitIntoBatchesWithID(gmTimes);
batches.forEach(batch -> {
try {
addNewGMTimesBatch(batch);
} catch (SQLException e) {
Log.toLog("GMTimesTable.addNewGMTimesRows", e);
}
});
}
private void addNewGMTimesBatch(List<Container<GMTimes>> batch) throws SQLException {
if (batch.isEmpty()) {
return;
}
String[] gms = GMTimes.getGMKeyArray();
PreparedStatement statement = null;
try {
statement = prepareStatement(
"INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnSurvivalTime + ", "
+ columnCreativeTime + ", "
+ columnAdventureTime + ", "
+ columnSpectatorTime
+ ") VALUES (?, ?, ?, ?, ?)");
for (Container<GMTimes> data : batch) {
statement.setInt(1, data.getId());
for (int i = 0; i < gms.length; i++) {
Map<String, Long> times = data.getObject().getTimes();
Long time = times.get(gms[i]);
statement.setLong(i + 2, time != null ? time : 0);
}
statement.addBatch();
}
statement.executeBatch();
} finally {
close(statement);
}
}
private void addNewGMTimesRow(int userId, Map<String, Long> gamemodeTimes) throws SQLException {
if (Verify.isEmpty(gamemodeTimes)) {
return;
}
PreparedStatement statement = null;
String[] gms = GMTimes.getGMKeyArray();
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnSurvivalTime + ", "
+ columnCreativeTime + ", "
+ columnAdventureTime + ", "
+ columnSpectatorTime
+ ") VALUES (?, ?, ?, ?, ?)");
statement.setInt(1, userId);
for (int i = 0; i < gms.length; i++) {
Long time = gamemodeTimes.get(gms[i]);
statement.setLong(i + 2, time != null ? time : 0);
}
statement.execute();
} finally {
close(statement);
}
}
}

View File

@ -6,7 +6,10 @@ import org.junit.Before;
import org.junit.Test;
import test.java.utils.RandomData;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@ -25,10 +28,7 @@ public class WorldTimesTest {
@Before
public void setUp() throws Exception {
test = new WorldTimes(worldOne, gms[0]);
Optional<GMTimes> gmTimes = test.getGMTimes(worldOne);
gmTimes.ifPresent(gmTimes1 ->
time = gmTimes1.getLastStateChange()
);
time = test.getGMTimes(worldOne).getLastStateChange();
System.out.println(test);
}
@ -37,8 +37,8 @@ public class WorldTimesTest {
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]));
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
}
@Test
@ -46,8 +46,8 @@ public class WorldTimesTest {
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]));
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
}
@Test
@ -56,13 +56,13 @@ public class WorldTimesTest {
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]));
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).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]));
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
assertEquals(1000L, test.getGMTimes(worldTwo).getTime(gms[2]));
}
@Test
@ -96,16 +96,8 @@ public class WorldTimesTest {
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();
}
long time1 = test.getWorldPlaytime(worldOne);
long time2 = test.getWorldPlaytime(worldTwo);
System.out.println(test);
// Tests World time calculation.
@ -118,7 +110,7 @@ public class WorldTimesTest {
public void testGMTrackingSingleWorld() {
long changeTime = time + 1000L;
long changeTime2 = changeTime + 1000L;
GMTimes gmTimes = test.getGMTimes(worldOne).get();
GMTimes gmTimes = test.getGMTimes(worldOne);
test.updateState(worldOne, "CREATIVE", changeTime);
assertEquals(1000L, gmTimes.getTime("SURVIVAL"));
assertEquals(0L, gmTimes.getTime("CREATIVE"));
@ -132,7 +124,7 @@ public class WorldTimesTest {
public void testGMTrackingTwoWorlds() {
long changeTime = time + 1000L;
long changeTime2 = time + 2000L;
GMTimes worldOneGMTimes = test.getGMTimes(worldOne).get();
GMTimes worldOneGMTimes = test.getGMTimes(worldOne);
test.updateState(worldOne, "CREATIVE", changeTime);
test.updateState(worldOne, "ADVENTURE", changeTime2);
assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL"));
@ -140,7 +132,7 @@ public class WorldTimesTest {
assertEquals(0L, worldOneGMTimes.getTime("ADVENTURE"));
test.updateState(worldTwo, "SURVIVAL", time + 3000L);
GMTimes worldTwoGMTimes = test.getGMTimes(worldTwo).get();
GMTimes worldTwoGMTimes = test.getGMTimes(worldTwo);
assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL"));
assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE"));
assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE"));