Removed GMTimesTable

This commit is contained in:
Rsl1122 2017-08-21 19:19:33 +03:00
parent 7fd9960c8a
commit 67df66cc31
4 changed files with 8 additions and 387 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

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