diff --git a/Plan/src/main/java/com/djrapitops/plan/data/PlayerProfile.java b/Plan/src/main/java/com/djrapitops/plan/data/PlayerProfile.java index 9160e0cc9..0449b8eae 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/PlayerProfile.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/PlayerProfile.java @@ -36,6 +36,7 @@ public class PlayerProfile implements OfflinePlayer { private Map registeredMap; private Set bannedOnServers; private Set oppedOnServers; + private int timesKicked; // Activity related information private Map> sessions; @@ -310,6 +311,10 @@ public class PlayerProfile implements OfflinePlayer { this.sessions.put(serverUUID, sessions); } + public void setSessions(Map> sessions) { + this.sessions.putAll(sessions); + } + public void addActiveSession(Session activeSession) { UUID serverUUID = MiscUtils.getIPlan().getServerUuid(); List sessions = getSessions(serverUUID); @@ -355,8 +360,16 @@ public class PlayerProfile implements OfflinePlayer { this.geoInformation = geoInformation; } + public void setTimesKicked(int timesKicked) { + this.timesKicked = timesKicked; + } + // Default Getters + public int getTimesKicked() { + return timesKicked; + } + public List getNicknames() { return nicknames; } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/Database.java b/Plan/src/main/java/com/djrapitops/plan/database/Database.java index 46fd3aba4..5c71bf29c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/Database.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/Database.java @@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; +import main.java.com.djrapitops.plan.data.PlayerProfile; import main.java.com.djrapitops.plan.database.tables.*; import org.apache.commons.lang3.StringUtils; @@ -265,4 +266,6 @@ public abstract class Database { public boolean isUsingMySQL() { return "mysql".equals(getConfigName()); } + + public abstract PlayerProfile getPlayerProfile(UUID uuid) throws SQLException; } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java index 46701e67f..8ae546dc1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java @@ -8,6 +8,9 @@ import com.djrapitops.plugin.task.ITask; import com.djrapitops.plugin.task.RunnableFactory; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; +import main.java.com.djrapitops.plan.data.PlayerProfile; +import main.java.com.djrapitops.plan.data.Session; +import main.java.com.djrapitops.plan.data.UserInfo; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.tables.*; import main.java.com.djrapitops.plan.database.tables.move.Version8TransferTable; @@ -15,6 +18,9 @@ import org.apache.commons.dbcp2.BasicDataSource; import java.sql.Connection; import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.UUID; /** @@ -156,7 +162,7 @@ public abstract class SQLDB extends Database { } /** - * Get all tables in a good order. + * Get all tables in a create order. * * @return Table array. */ @@ -222,6 +228,53 @@ public abstract class SQLDB extends Database { return versionTable.isNewDatabase(); } + @Override + public PlayerProfile getPlayerProfile(UUID uuid) throws SQLException { + if (!wasSeenBefore(uuid)) { + return null; + } + + String playerName = usersTable.getPlayerName(uuid); + Optional registerDate = usersTable.getRegisterDate(uuid); + + if (!registerDate.isPresent()) { + throw new IllegalStateException("User has been saved with null register date to a NOT NULL column"); + } + + PlayerProfile profile = new PlayerProfile(uuid, playerName, registerDate.get()); + profile.setTimesKicked(usersTable.getTimesKicked(uuid)); + + Map userInfo = userInfoTable.getAllUserInfo(uuid); + addUserInfoToProfile(profile, userInfo); + + profile.setActions(actionsTable.getActions(uuid)); + profile.setNicknames(nicknamesTable.getAllNicknames(uuid)); + + // TODO Add IPs as GeoInfo, requires IPTable changes + + Map> sessions = sessionsTable.getSessions(uuid); + profile.setSessions(sessions); + profile.setTotalWorldTimes(worldTimesTable.getWorldTimesOfUser(uuid)); + + + return null; + } + + private void addUserInfoToProfile(PlayerProfile profile, Map userInfo) { + for (Map.Entry entry : userInfo.entrySet()) { + UUID serverUUID = entry.getKey(); + UserInfo info = entry.getValue(); + + profile.setRegistered(serverUUID, info.getRegistered()); + if (info.isBanned()) { + profile.bannedOnServer(serverUUID); + } + if (info.isOpped()) { + profile.oppedOnServer(serverUUID); + } + } + } + @Override public boolean wasSeenBefore(UUID uuid) { if (uuid == null) { diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java index 257a8cd51..907402785 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java @@ -193,7 +193,7 @@ public class ServerTable extends Table { }); } - public Map getServerNames() throws SQLException { + public Map getServerNamesByID() throws SQLException { String sql = Select.from(tableName, columnServerID, columnServerName) .toString(); @@ -211,6 +211,42 @@ public class ServerTable extends Table { }); } + public Map getServerNames() throws SQLException { + String sql = Select.from(tableName, + columnServerUUID, columnServerName) + .toString(); + + return query(new QueryAllStatement>(sql) { + @Override + public Map processResults(ResultSet set) throws SQLException { + Map names = new HashMap<>(); + while (set.next()) { + UUID serverUUID = UUID.fromString(set.getString(columnServerUUID)); + names.put(serverUUID, set.getString(columnServerName)); + } + return names; + } + }); + } + + public Map getServerUuids() throws SQLException { + String sql = Select.from(tableName, + columnServerID, columnServerUUID) + .toString(); + + return query(new QueryAllStatement>(sql) { + @Override + public Map processResults(ResultSet set) throws SQLException { + Map uuids = new HashMap<>(); + while (set.next()) { + int id = set.getInt(columnServerID); + uuids.put(id, UUID.fromString(set.getString(columnServerUUID))); + } + return uuids; + } + }); + } + /** * Used to get BungeeCord WebServer info if present. * 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 aeb7e2dda..55658f601 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 @@ -151,44 +151,44 @@ public class SessionsTable extends UserIDTable { * @return Map with Sessions that don't contain Kills or WorldTimes. * @throws SQLException DB Error */ - private Map> getSessionInformation(UUID uuid) throws SQLException { - Map serverNames = serverTable.getServerNames(); + private Map> getSessionInformation(UUID uuid) throws SQLException { + Map serverUUIDs = serverTable.getServerUuids(); String sql = Select.from(tableName, "*") .where(columnUserID + "=" + usersTable.statementSelectID) .toString(); - return query(new QueryStatement>>(sql, 10000) { + return query(new QueryStatement>>(sql, 10000) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, uuid.toString()); } @Override - public Map> processResults(ResultSet set) throws SQLException { - Map> sessionsByServer = new HashMap<>(); + public Map> processResults(ResultSet set) throws SQLException { + Map> sessionsByServer = new HashMap<>(); while (set.next()) { int id = set.getInt(columnID); long start = set.getLong(columnSessionStart); long end = set.getLong(columnSessionEnd); - String serverName = serverNames.get(set.getInt(columnServerID)); + UUID serverUUID = serverUUIDs.get(set.getInt(columnServerID)); - if (serverName == null) { + if (serverUUID == null) { throw new IllegalStateException("Server not present"); } int deaths = set.getInt(columnDeaths); int mobKills = set.getInt(columnMobKills); - List sessions = sessionsByServer.getOrDefault(serverName, new ArrayList<>()); + List sessions = sessionsByServer.getOrDefault(serverUUID, new ArrayList<>()); sessions.add(new Session(id, start, end, mobKills, deaths)); - sessionsByServer.put(serverName, sessions); + sessionsByServer.put(serverUUID, sessions); } return sessionsByServer; } }); } - public Map> getSessions(UUID uuid) throws SQLException { - Map> sessions = getSessionInformation(uuid); + public Map> getSessions(UUID uuid) throws SQLException { + Map> sessions = getSessionInformation(uuid); Map allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toMap(Session::getSessionID, Function.identity())); db.getKillsTable().addKillsToSessions(uuid, allSessions); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/UserInfoTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/UserInfoTable.java index d37d05f66..d50137dd3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/UserInfoTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/UserInfoTable.java @@ -121,39 +121,42 @@ public class UserInfoTable extends UserIDTable { } public UserInfo getUserInfo(UUID uuid) throws SQLException { - return getUserInfo(uuid, Plan.getServerUUID()); + return getAllUserInfo(uuid).get(MiscUtils.getIPlan().getServerUuid()); } - public UserInfo getUserInfo(UUID uuid, UUID serverUUID) throws SQLException { + public Map getAllUserInfo(UUID uuid) throws SQLException { String usersIDColumn = usersTable + "." + usersTable.getColumnID(); - String usersNameColumn = usersTable + "." + usersTable.getColumnName() + " as name"; + String serverIDColumn = serverTable + "." + serverTable.getColumnID(); + String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid"; String sql = "SELECT " + tableName + "." + columnRegistered + ", " + - columnOP + ", " + columnBanned + ", " + - usersNameColumn + + columnOP + ", " + + serverUUIDColumn + " FROM " + tableName + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + - " WHERE " + columnUserID + "=" + usersTable.statementSelectID + - " AND " + columnServerID + "=" + serverTable.statementSelectServerID; + " JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID + + " WHERE " + columnUserID + "=" + usersTable.statementSelectID; - return query(new QueryStatement(sql) { + return query(new QueryStatement>(sql) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, uuid.toString()); - statement.setString(2, serverUUID.toString()); } @Override - public UserInfo processResults(ResultSet set) throws SQLException { - if (set.next()) { + public Map processResults(ResultSet set) throws SQLException { + Map map = new HashMap<>(); + while (set.next()) { long registered = set.getLong(columnRegistered); boolean opped = set.getBoolean(columnOP); boolean banned = set.getBoolean(columnBanned); String name = set.getString("name"); - return new UserInfo(uuid, name, registered, opped, banned); + + UUID serverUUID = UUID.fromString(set.getString("s_uuid")); + map.put(serverUUID, new UserInfo(uuid, name, registered, opped, banned)); } - return null; + return map; } }); } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/Html.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/Html.java index b0ecfcfbc..905a40459 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/Html.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/Html.java @@ -29,6 +29,7 @@ public enum Html { COLOR_F(""), // FONT_AWESOME_ICON(""), + FA_COLORED_ICON(""), SPAN("${0}"), BUTTON("${1}"), BUTTON_CLASS("class=\"button\""),