diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java index a340a5571..c926ab004 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java @@ -5,6 +5,7 @@ import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException; import main.java.com.djrapitops.plan.data.PlayerKill; import main.java.com.djrapitops.plan.data.Session; +import main.java.com.djrapitops.plan.data.time.GMTimes; 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; @@ -26,6 +27,7 @@ public class KillsTable extends UserIDTable { private final String columnSessionID = "session_id"; private final SessionsTable sessionsTable; + private String insertStatement; /** * @param db @@ -34,6 +36,16 @@ public class KillsTable extends UserIDTable { public KillsTable(SQLDB db, boolean usingMySQL) { super("plan_kills", db, usingMySQL); sessionsTable = db.getSessionsTable(); + insertStatement = "INSERT INTO " + tableName + " (" + + columnKillerUserID + ", " + + columnVictimUserID + ", " + + columnSessionID + ", " + + columnDate + ", " + + columnWeapon + + ") VALUES (" + + usersTable.statementSelectID + ", " + + usersTable.statementSelectID + ", " + + "?, ?, ?)"; } /** @@ -55,7 +67,7 @@ public class KillsTable extends UserIDTable { } @Override - public void removeUser(UUID uuid) throws SQLException{ + public void removeUser(UUID uuid) throws SQLException { PreparedStatement statement = null; try { statement = prepareStatement("DELETE FROM " + tableName + @@ -77,16 +89,7 @@ public class KillsTable extends UserIDTable { } PreparedStatement statement = null; try { - statement = prepareStatement("INSERT INTO " + tableName + " (" - + columnKillerUserID + ", " - + columnVictimUserID + ", " - + columnSessionID + ", " - + columnDate + ", " - + columnWeapon - + ") VALUES (" - + usersTable.statementSelectID + ", " - + usersTable.statementSelectID + ", " - + "?, ?, ?)"); + statement = prepareStatement(insertStatement); for (PlayerKill kill : playerKills) { UUID victim = kill.getVictim(); long date = kill.getTime(); @@ -183,11 +186,91 @@ public class KillsTable extends UserIDTable { } } - public void addKillsToSessions(Map>> map) { - // TODO + public void addKillsToSessions(Map>> map) throws SQLException { + Map> playerKillsBySessionID = getAllPlayerKillsBySessionID(); + for (UUID serverUUID : map.keySet()) { + for (List sessions : map.get(serverUUID).values()) { + for (Session session : sessions) { + List playerKills = playerKillsBySessionID.get(session.getSessionID()); + if (playerKills != null) { + session.setPlayerKills(playerKills); + } + } + } + } } - public void savePlayerKills(Map>> allSessions) { - // TODO + public void savePlayerKills(Map>> allSessions) throws SQLException { + if (Verify.isEmpty(allSessions)) { + return; + } + PreparedStatement statement = null; + try { + statement = prepareStatement(insertStatement); + String[] gms = GMTimes.getGMKeyArray(); + for (UUID serverUUID : allSessions.keySet()) { + for (Map.Entry> entry : allSessions.get(serverUUID).entrySet()) { + UUID uuid = entry.getKey(); + List sessions = entry.getValue(); + + for (Session session : sessions) { + int sessionID = session.getSessionID(); + for (PlayerKill kill : session.getPlayerKills()) { + UUID victim = kill.getVictim(); + long date = kill.getTime(); + String weapon = kill.getWeapon(); + statement.setString(1, uuid.toString()); + statement.setString(2, victim.toString()); + statement.setInt(3, sessionID); + statement.setLong(4, date); + statement.setString(5, weapon); + statement.addBatch(); + } + } + } + } + statement.executeBatch(); + commit(statement.getConnection()); + } finally { + close(statement); + } + } + + public Map> getAllPlayerKillsBySessionID() throws SQLException { + PreparedStatement statement = null; + ResultSet set = null; + try { + String usersIDColumn = usersTable + "." + usersTable.getColumnID(); + String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid"; + statement = prepareStatement("SELECT " + + columnSessionID + ", " + + columnDate + ", " + + columnWeapon + ", " + + usersUUIDColumn + + " FROM " + tableName + + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnVictimUserID); + + statement.setFetchSize(10000); + + set = statement.executeQuery(); + + Map> allPlayerKills = new HashMap<>(); + while (set.next()) { + int sessionID = set.getInt(columnSessionID); + + List playerKills = allPlayerKills.getOrDefault(sessionID, new ArrayList<>()); + + String uuidS = set.getString("victim_uuid"); + UUID victim = UUID.fromString(uuidS); + long date = set.getLong(columnDate); + String weapon = set.getString(columnWeapon); + playerKills.add(new PlayerKill(victim, weapon, date)); + + allPlayerKills.put(sessionID, playerKills); + } + return allPlayerKills; + } finally { + close(set, statement); + } } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java index 933f98ad6..34cc7eda3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java @@ -547,7 +547,7 @@ public class SessionsTable extends UserIDTable { } } - public Map>> getAllSessions(boolean includeExtraData) throws SQLException { + public Map>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException { PreparedStatement statement = null; ResultSet set = null; try { @@ -592,7 +592,7 @@ public class SessionsTable extends UserIDTable { sessionsByUser.put(uuid, sessions); map.put(serverUUID, sessionsByUser); } - if (includeExtraData) { + if (getKillsAndWorldTimes) { db.getKillsTable().addKillsToSessions(map); db.getWorldTimesTable().addWorldTimesToSessions(map); } @@ -603,7 +603,7 @@ public class SessionsTable extends UserIDTable { } } - public void insertSessions(Map>> allSessions, boolean containsExtraData) throws SQLException { + public void insertSessions(Map>> allSessions, boolean saveKillsAndWorldTimes) throws SQLException { if (Verify.isEmpty(allSessions)) { return; } @@ -632,7 +632,7 @@ public class SessionsTable extends UserIDTable { } finally { close(statement); } - if (containsExtraData) { + if (saveKillsAndWorldTimes) { Map>> savedSessions = getAllSessions(false); matchSessionIDs(allSessions, savedSessions); db.getKillsTable().savePlayerKills(allSessions); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java index 161e327a2..91f280837 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java @@ -284,7 +284,6 @@ public class WorldTimesTable extends UserIDTable { for (Map.Entry> entry : allSessions.get(serverUUID).entrySet()) { UUID uuid = entry.getKey(); List sessions = entry.getValue(); - for (Session session : sessions) { int sessionID = session.getSessionID(); for (Map.Entry worldTimesEntry : session.getWorldTimes().getWorldTimes().entrySet()) { @@ -293,7 +292,6 @@ public class WorldTimesTable extends UserIDTable { statement.setString(1, uuid.toString()); statement.setString(2, worldName); statement.setInt(3, sessionID); - statement.setLong(4, gmTimes.getTime(gms[0])); statement.setLong(5, gmTimes.getTime(gms[1])); statement.setLong(6, gmTimes.getTime(gms[2])); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java index b7ed9f9ab..483fd32f0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java @@ -72,7 +72,7 @@ public class BatchOperationTable extends Table { copyNicknames(toDB); copyTPS(toDB); copyWebUsers(toDB); - // TODO WorldTimes, Sessions, PlayerKills + copySessions(toDB); } public void copyActions(BatchOperationTable toDB) throws SQLException { @@ -150,4 +150,11 @@ public class BatchOperationTable extends Table { toTable.insertUsers(fromTable.getUsers()); toTable.updateKicked(fromTable.getAllTimesKicked()); } + + public void copySessions(BatchOperationTable toDB) throws SQLException { + if (toDB.equals(this)) { + return; + } + toDB.db.getSessionsTable().insertSessions(db.getSessionsTable().getAllSessions(true), true); + } } \ No newline at end of file