KillsTable

This commit is contained in:
Rsl1122 2017-08-23 11:29:33 +03:00
parent c6fbb12d2d
commit 1c49dfbdfd
5 changed files with 76 additions and 160 deletions

View File

@ -277,9 +277,9 @@ public abstract class SQLDB extends Database {
boolean success = userId != -1
&& ipsTable.removeUserIPs(userId)
&& nicknamesTable.removeUserNicknames(userId)
&& sessionsTable.removeUserSessions(userId)
&& killsTable.removeUserKillsAndVictims(userId)
&& worldTimesTable.removeUserWorldTimes(userId)
&& sessionsTable.removeUserSessions(userId)
&& usersTable.removeUser(uuid);
if (success) {
commit();

View File

@ -7,31 +7,36 @@ import main.java.com.djrapitops.plan.data.Session;
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 main.java.com.djrapitops.plan.utilities.Benchmark;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author Rsl1122
*/
public class KillsTable extends Table {
public class KillsTable extends UserIDTable {
private final String columnKillerUserID = "killer_id";
private final String columnVictimUserID = "victim_id";
private final String columnWeapon = "weapon";
private final String columnDate = "date";
private final String columnServerID = "server_id"; //TODO
private final String columnSessionID = "session_id"; //TODO
private final SessionsTable sessionsTable;
/**
* @param db
* @param usingMySQL
*/
public KillsTable(SQLDB db, boolean usingMySQL) {
super("plan_kills", db, usingMySQL);
sessionsTable = db.getSessionsTable();
}
/**
@ -39,15 +44,16 @@ public class KillsTable extends Table {
*/
@Override
public boolean createTable() {
UsersTable usersTable = db.getUsersTable();
try {
execute(TableSqlParser.createTable(tableName)
.column(columnKillerUserID, Sql.INT).notNull()
.column(columnVictimUserID, Sql.INT).notNull()
.column(columnWeapon, Sql.varchar(30)).notNull()
.column(columnDate, Sql.LONG).notNull()
.column(columnSessionID, Sql.LONG).notNull()
.foreignKey(columnKillerUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnVictimUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnSessionID, sessionsTable.getTableName(), sessionsTable.getColumnID())
.toString()
);
return true;
@ -77,164 +83,70 @@ public class KillsTable extends Table {
}
}
/**
* @param userId
* @return
* @throws SQLException
*/
public List<KillData> getPlayerKills(int userId) throws SQLException {
UsersTable usersTable = db.getUsersTable();
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT * FROM " + tableName + " WHERE (" + columnKillerUserID + "=?)");
statement.setInt(1, userId);
set = statement.executeQuery();
List<KillData> killData = new ArrayList<>();
while (set.next()) {
UUID victimUUID = null; // TODO Victim UUID Retrieval
killData.add(new KillData(victimUUID, set.getString(columnWeapon), set.getLong(columnDate)));
}
return killData;
} finally {
close(set);
close(statement);
}
}
/**
* @param userId
* @param kills
* @throws SQLException
*/
public void savePlayerKills(int userId, List<KillData> kills) throws SQLException {
if (Verify.isEmpty(kills)) {
public void savePlayerKills(UUID uuid, long sessionID, List<KillData> playerKills) throws SQLException {
if (Verify.isEmpty(playerKills)) {
return;
}
Benchmark.start("Save Kills");
kills.removeAll(getPlayerKills(userId));
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnKillerUserID + ", "
+ columnVictimUserID + ", "
+ columnWeapon + ", "
+ columnDate
+ ") VALUES (?, ?, ?, ?)");
boolean commitRequired = false;
for (KillData kill : kills) {
if (kill == null) {
continue;
}
statement.setInt(1, userId);
statement.setInt(2, -1); // TODO Victim ID Retrieval
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getTime());
+ columnSessionID + ", "
+ columnDate + ", "
+ columnWeapon
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ usersTable.statementSelectID + ", "
+ "?, ?, ?)");
for (KillData kill : playerKills) {
UUID victim = kill.getVictim();
long date = kill.getTime();
String weapon = kill.getWeapon();
statement.setString(1, uuid.toString());
statement.setString(2, victim.toString());
statement.setLong(3, sessionID);
statement.setLong(4, date);
statement.setString(5, weapon);
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
close(statement);
Benchmark.stop("Database", "Save Kills");
}
}
/**
* @param ids
* @param uuids
* @return
* @throws SQLException
*/
public Map<Integer, List<KillData>> getPlayerKills(Collection<Integer> ids, Map<Integer, UUID> uuids) throws SQLException {
if (ids == null || ids.isEmpty()) {
return new HashMap<>();
}
Benchmark.start("Get Kills multiple");
public void addKillsToSessions(UUID uuid, List<Session> allSessions) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
Map<Long, Session> sessions = allSessions.stream().collect(Collectors.toMap(Session::getSessionID, Function.identity()));
try {
Map<Integer, List<KillData>> kills = new HashMap<>();
statement = prepareStatement("SELECT * FROM " + tableName);
String usersTableID = usersTable + "." + usersTable.getColumnID();
String usersTableUUID = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
statement = prepareStatement("SELECT " +
columnSessionID + ", " +
columnDate + ", " +
columnWeapon + ", " +
usersTableUUID + ", " +
" FROM " + tableName +
" WHERE " + columnKillerUserID + "=" + usersTable.statementSelectID +
" JOIN " + usersTable + " on " + usersTableID + "=" + columnVictimUserID); // Might not work TODO TEST
statement.setString(1, uuid.toString());
set = statement.executeQuery();
for (Integer id : ids) {
kills.put(id, new ArrayList<>());
}
while (set.next()) {
int killerID = set.getInt(columnKillerUserID);
if (!ids.contains(killerID)) {
long sessionID = set.getLong(columnSessionID);
Session session = sessions.get(sessionID);
if (session == null) {
continue;
}
UUID victimUUID = null; // TODO Victim UUID Retrieval
kills.get(killerID).add(new KillData(victimUUID, set.getString(columnWeapon), set.getLong(columnDate)));
}
return kills;
} finally {
close(set);
close(statement);
Benchmark.stop("Database", "Get Kills multiple");
}
}
/**
* @param kills
* @param uuids
* @throws SQLException
*/
public void savePlayerKills(Map<Integer, List<KillData>> kills, Map<Integer, UUID> uuids) throws SQLException {
if (Verify.isEmpty(kills)) {
return;
}
Benchmark.start("Save Kills multiple");
Map<Integer, List<KillData>> saved = getPlayerKills(kills.keySet(), uuids);
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnKillerUserID + ", "
+ columnVictimUserID + ", "
+ columnWeapon + ", "
+ columnDate
+ ") VALUES (?, ?, ?, ?)");
boolean commitRequired = false;
for (Map.Entry<Integer, List<KillData>> entrySet : kills.entrySet()) {
Integer id = entrySet.getKey();
List<KillData> playerKills = entrySet.getValue();
playerKills.removeIf(Objects::isNull);
List<KillData> s = saved.get(id);
if (s != null) {
playerKills.removeAll(s);
}
for (KillData kill : playerKills) {
statement.setInt(1, id);
statement.setInt(2, -1); // TODO Victim ID Retrieval
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getTime());
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
String uuidS = set.getString("victim_uuid");
UUID victim = UUID.fromString(uuidS);
long date = set.getLong(columnDate);
String weapon = set.getString(columnWeapon);
session.getPlayerKills().add(new KillData(victim, weapon, date));
}
} finally {
close(statement);
Benchmark.stop("Database", "Save Kills multiple");
close(set, statement);
}
}
public void savePlayerKills(UUID uuid, List<KillData> playerKills) {
// TODO savePlayerKills
}
public void addKillsToSessions(List<Session> allSessions) {
// TODO addKillsToSessions
}
}

View File

@ -19,7 +19,7 @@ import java.util.stream.Collectors;
*/
public class SessionsTable extends UserIDTable {
private final String columnSessionID = "id";
private final String columnID = "id";
private final String columnSessionStart = "session_start";
private final String columnSessionEnd = "session_end";
private final String columnServerID = "server_id";
@ -46,7 +46,7 @@ public class SessionsTable extends UserIDTable {
String serverTableName = serverTable.getTableName();
String serverTableID = serverTable.getColumnID();
String sql = TableSqlParser.createTable(this.tableName)
.primaryKeyIDColumn(usingMySQL, columnSessionID, Sql.LONG)
.primaryKeyIDColumn(usingMySQL, columnID, Sql.LONG)
.column(columnUserID, Sql.INT).notNull()
.column(columnServerID, Sql.INT).notNull()
.column(columnSessionStart, Sql.LONG).notNull()
@ -55,7 +55,7 @@ public class SessionsTable extends UserIDTable {
.column(columnDeaths, Sql.INT).notNull()
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnServerID, serverTableName, serverTableID)
.primaryKey(usingMySQL, columnSessionID)
.primaryKey(usingMySQL, columnID)
.toString();
System.out.println(sql);
execute(sql);
@ -92,9 +92,8 @@ public class SessionsTable extends UserIDTable {
if (sessionID == -1) {
throw new IllegalStateException("Session was not Saved!");
}
session.setSessionID(sessionID);
db.getWorldTimesTable().saveWorldTimes(session.getWorldTimes());
db.getKillsTable().savePlayerKills(uuid, session.getPlayerKills());
db.getWorldTimesTable().saveWorldTimes(sessionID, session.getWorldTimes());
db.getKillsTable().savePlayerKills(uuid, sessionID, session.getPlayerKills());
}
/**
@ -117,7 +116,7 @@ public class SessionsTable extends UserIDTable {
+ columnMobKills + ", "
+ columnServerID
+ ") VALUES ("
+ columnUserID + "=" + usersTable.statementSelectID + ", "
+ usersTable.statementSelectID + ", "
+ "?, ?, ?, ?, "
+ serverTable.statementSelectServerID + ")");
statement.setString(1, uuid.toString());
@ -145,7 +144,7 @@ public class SessionsTable extends UserIDTable {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " + columnSessionID + " FROM " + tableName +
statement = prepareStatement("SELECT " + columnID + " FROM " + tableName +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnSessionStart + "=?" +
" AND " + columnSessionEnd + "=?");
@ -154,7 +153,7 @@ public class SessionsTable extends UserIDTable {
statement.setLong(3, session.getSessionEnd());
set = statement.executeQuery();
if (set.next()) {
return set.getLong(columnSessionID);
return set.getLong(columnID);
}
return -1L;
} finally {
@ -184,7 +183,7 @@ public class SessionsTable extends UserIDTable {
statement.setString(1, uuid.toString());
set = statement.executeQuery();
while (set.next()) {
long id = set.getLong(columnSessionID);
long id = set.getLong(columnID);
long start = set.getLong(columnSessionStart);
long end = set.getLong(columnSessionEnd);
String serverName = serverNames.get(set.getInt(columnServerID));
@ -203,8 +202,8 @@ public class SessionsTable extends UserIDTable {
public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException {
Map<String, List<Session>> sessions = getSessionInformation(uuid);
List<Session> allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
db.getKillsTable().addKillsToSessions(allSessions);
db.getWorldTimesTable().addWorldTimesToSessions(allSessions);
db.getKillsTable().addKillsToSessions(uuid, allSessions);
db.getWorldTimesTable().addWorldTimesToSessions(uuid, allSessions);
return sessions;
}
@ -397,7 +396,7 @@ public class SessionsTable extends UserIDTable {
ResultSet set = null;
try {
statement = prepareStatement("SELECT" +
" (COUNT(" + columnSessionID + ") - SUM(" + columnSessionStart + ")) as logintimes" +
" (COUNT(" + columnID + ") - SUM(" + columnSessionStart + ")) as logintimes" +
" FROM " + tableName +
" WHERE " + columnSessionStart + ">?" +
" AND " + columnUserID + "=" + usersTable.statementSelectID +
@ -414,4 +413,8 @@ public class SessionsTable extends UserIDTable {
close(set, statement);
}
}
public String getColumnID() {
return columnID;
}
}

View File

@ -377,4 +377,8 @@ public class UsersTable extends Table {
// TODO Rewrite method for new UserData objects.
return new ArrayList<>();
}
public String getColumnUUID() {
return columnUUID;
}
}

View File

@ -11,10 +11,7 @@ import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Table class representing database table plan_world_times.
@ -316,11 +313,11 @@ public class WorldTimesTable extends UserIDTable {
}
}
public void saveWorldTimes(WorldTimes worldTimes) {
public void saveWorldTimes(long sessionID, WorldTimes worldTimes) {
// TODO saveWorldTimes (INSERT)
}
public void addWorldTimesToSessions(List<Session> allSessions) {
public void addWorldTimesToSessions(UUID uuid, List<Session> allSessions) {
// TODO saveWorldTimes
}
}