diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java index b0702dfa0..4619f5556 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java @@ -493,6 +493,32 @@ public class SessionQueries { }; } + public static Query> playtimeOfPlayer(long after, long before, UUID playerUUID) { + String sql = SELECT + SessionsTable.SERVER_UUID + ",SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" + + FROM + SessionsTable.TABLE_NAME + + WHERE + SessionsTable.USER_UUID + "=?" + + AND + SessionsTable.SESSION_END + ">=?" + + AND + SessionsTable.SESSION_START + "<=?" + + GROUP_BY + SessionsTable.SERVER_UUID; + return new QueryStatement>(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, playerUUID.toString()); + statement.setLong(2, after); + statement.setLong(3, before); + } + + @Override + public Map processResults(ResultSet set) throws SQLException { + Map playtimeOfPlayer = new HashMap<>(); + while (set.next()) { + playtimeOfPlayer.put(UUID.fromString(set.getString(SessionsTable.SERVER_UUID)), set.getLong("playtime")); + } + return playtimeOfPlayer; + } + }; + } + public static Query playtime(long after, long before) { String sql = SELECT + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" + FROM + SessionsTable.TABLE_NAME + @@ -765,4 +791,23 @@ public class SessionQueries { } }; } + + public static Query lastSeen(UUID playerUUID, UUID serverUUID) { + String sql = SELECT + "MAX(" + SessionsTable.SESSION_END + ") as last_seen" + + FROM + SessionsTable.TABLE_NAME + + WHERE + SessionsTable.USER_UUID + "=?" + + AND + SessionsTable.SERVER_UUID + "=?"; + return new QueryStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, playerUUID.toString()); + statement.setString(2, serverUUID.toString()); + } + + @Override + public Long processResults(ResultSet set) throws SQLException { + return set.next() ? set.getLong("last_seen") : 0; + } + }; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/query/CommonQueriesImplementation.java b/Plan/common/src/main/java/com/djrapitops/plan/query/CommonQueriesImplementation.java index c08e867fc..f431a037d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/query/CommonQueriesImplementation.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/query/CommonQueriesImplementation.java @@ -16,12 +16,10 @@ */ package com.djrapitops.plan.query; -import com.djrapitops.plan.data.store.containers.PlayerContainer; -import com.djrapitops.plan.data.store.mutators.SessionsMutator; import com.djrapitops.plan.db.DBType; import com.djrapitops.plan.db.Database; -import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries; import com.djrapitops.plan.db.access.queries.objects.ServerQueries; +import com.djrapitops.plan.db.access.queries.objects.SessionQueries; import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries; import com.djrapitops.plan.db.access.queries.schema.H2SchemaQueries; import com.djrapitops.plan.db.access.queries.schema.MySQLSchemaQueries; @@ -41,21 +39,12 @@ public class CommonQueriesImplementation implements CommonQueries { @Override public long fetchPlaytime(UUID playerUUID, UUID serverUUID, long after, long before) { - // TODO Replace with single query later - PlayerContainer player = db.query(ContainerFetchQueries.fetchPlayerContainer(playerUUID)); - return SessionsMutator.forContainer(player) - .filterSessionsBetween(after, before) - .filterPlayedOnServer(serverUUID) - .toPlaytime(); + return db.query(SessionQueries.playtimeOfPlayer(after, before, playerUUID)).getOrDefault(serverUUID, 0L); } @Override public long fetchLastSeen(UUID playerUUID, UUID serverUUID) { - // TODO Replace with single query later - PlayerContainer player = db.query(ContainerFetchQueries.fetchPlayerContainer(playerUUID)); - return SessionsMutator.forContainer(player) - .filterPlayedOnServer(serverUUID) - .toLastSeen(); + return db.query(SessionQueries.lastSeen(playerUUID, serverUUID)); } @Override