Begun working on PlayerProfile get method to database

This commit is contained in:
Rsl1122 2017-11-20 14:42:06 +02:00
parent 94d8746c9a
commit b729f00a12
7 changed files with 135 additions and 26 deletions

View File

@ -36,6 +36,7 @@ public class PlayerProfile implements OfflinePlayer {
private Map<UUID, Long> registeredMap;
private Set<UUID> bannedOnServers;
private Set<UUID> oppedOnServers;
private int timesKicked;
// Activity related information
private Map<UUID, List<Session>> sessions;
@ -310,6 +311,10 @@ public class PlayerProfile implements OfflinePlayer {
this.sessions.put(serverUUID, sessions);
}
public void setSessions(Map<UUID, List<Session>> sessions) {
this.sessions.putAll(sessions);
}
public void addActiveSession(Session activeSession) {
UUID serverUUID = MiscUtils.getIPlan().getServerUuid();
List<Session> 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<String> getNicknames() {
return nicknames;
}

View File

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

View File

@ -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<Long> 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<UUID, UserInfo> 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<UUID, List<Session>> sessions = sessionsTable.getSessions(uuid);
profile.setSessions(sessions);
profile.setTotalWorldTimes(worldTimesTable.getWorldTimesOfUser(uuid));
return null;
}
private void addUserInfoToProfile(PlayerProfile profile, Map<UUID, UserInfo> userInfo) {
for (Map.Entry<UUID, UserInfo> 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) {

View File

@ -193,7 +193,7 @@ public class ServerTable extends Table {
});
}
public Map<Integer, String> getServerNames() throws SQLException {
public Map<Integer, String> getServerNamesByID() throws SQLException {
String sql = Select.from(tableName,
columnServerID, columnServerName)
.toString();
@ -211,6 +211,42 @@ public class ServerTable extends Table {
});
}
public Map<UUID, String> getServerNames() throws SQLException {
String sql = Select.from(tableName,
columnServerUUID, columnServerName)
.toString();
return query(new QueryAllStatement<Map<UUID, String>>(sql) {
@Override
public Map<UUID, String> processResults(ResultSet set) throws SQLException {
Map<UUID, String> names = new HashMap<>();
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString(columnServerUUID));
names.put(serverUUID, set.getString(columnServerName));
}
return names;
}
});
}
public Map<Integer, UUID> getServerUuids() throws SQLException {
String sql = Select.from(tableName,
columnServerID, columnServerUUID)
.toString();
return query(new QueryAllStatement<Map<Integer, UUID>>(sql) {
@Override
public Map<Integer, UUID> processResults(ResultSet set) throws SQLException {
Map<Integer, UUID> 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.
*

View File

@ -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<String, List<Session>> getSessionInformation(UUID uuid) throws SQLException {
Map<Integer, String> serverNames = serverTable.getServerNames();
private Map<UUID, List<Session>> getSessionInformation(UUID uuid) throws SQLException {
Map<Integer, UUID> serverUUIDs = serverTable.getServerUuids();
String sql = Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString();
return query(new QueryStatement<Map<String, List<Session>>>(sql, 10000) {
return query(new QueryStatement<Map<UUID, List<Session>>>(sql, 10000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public Map<String, List<Session>> processResults(ResultSet set) throws SQLException {
Map<String, List<Session>> sessionsByServer = new HashMap<>();
public Map<UUID, List<Session>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<Session>> 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<Session> sessions = sessionsByServer.getOrDefault(serverName, new ArrayList<>());
List<Session> 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<String, List<Session>> getSessions(UUID uuid) throws SQLException {
Map<String, List<Session>> sessions = getSessionInformation(uuid);
public Map<UUID, List<Session>> getSessions(UUID uuid) throws SQLException {
Map<UUID, List<Session>> sessions = getSessionInformation(uuid);
Map<Integer, Session> allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toMap(Session::getSessionID, Function.identity()));
db.getKillsTable().addKillsToSessions(uuid, allSessions);

View File

@ -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<UUID, UserInfo> 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<UserInfo>(sql) {
return query(new QueryStatement<Map<UUID, UserInfo>>(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<UUID, UserInfo> processResults(ResultSet set) throws SQLException {
Map<UUID, UserInfo> 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;
}
});
}

View File

@ -29,6 +29,7 @@ public enum Html {
COLOR_F("<span class=\"white\">"),
//
FONT_AWESOME_ICON("<i class=\"fa fa-${0}\"></i>"),
FA_COLORED_ICON("<i class=\"col-${0} fa fa-${1}\"></i>"),
SPAN("${0}</span>"),
BUTTON("<a class=\"button\" href=\"${0}\">${1}</a>"),
BUTTON_CLASS("class=\"button\""),