Search in sources :

Example 1 with Maps

use of com.djrapitops.plan.utilities.java.Maps in project Plan by plan-player-analytics.

the class LargeFetchQueries method fetchAllWorldNames.

/**
 * Query database for world names.
 *
 * @return Map: Server UUID - List of world names
 */
public static Query<Map<ServerUUID, Collection<String>>> fetchAllWorldNames() {
    String sql = SELECT + '*' + FROM + WorldTable.TABLE_NAME;
    return new QueryAllStatement<Map<ServerUUID, Collection<String>>>(sql, 1000) {

        @Override
        public Map<ServerUUID, Collection<String>> processResults(ResultSet set) throws SQLException {
            Map<ServerUUID, Collection<String>> worldMap = new HashMap<>();
            while (set.next()) {
                ServerUUID serverUUID = ServerUUID.fromString(set.getString(WorldTable.SERVER_UUID));
                Collection<String> worlds = worldMap.computeIfAbsent(serverUUID, Maps::createSet);
                worlds.add(set.getString(WorldTable.NAME));
            }
            return worldMap;
        }
    };
}
Also used : ServerUUID(com.djrapitops.plan.identification.ServerUUID) Maps(com.djrapitops.plan.utilities.java.Maps) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) Collection(java.util.Collection)

Example 2 with Maps

use of com.djrapitops.plan.utilities.java.Maps in project Plan by plan-player-analytics.

the class PlayersMutator method toActivityDataMap.

public TreeMap<Long, Map<String, Set<UUID>>> toActivityDataMap(long date, long msThreshold) {
    TreeMap<Long, Map<String, Set<UUID>>> activityData = new TreeMap<>();
    for (long time = date; time >= date - TimeAmount.MONTH.toMillis(2L); time -= TimeAmount.WEEK.toMillis(1L)) {
        Map<String, Set<UUID>> map = activityData.computeIfAbsent(time, Maps::create);
        if (!players.isEmpty()) {
            for (PlayerContainer player : players) {
                if (player.getValue(PlayerKeys.REGISTERED).orElse(0L) > time) {
                    continue;
                }
                ActivityIndex activityIndex = player.getActivityIndex(time, msThreshold);
                String activityGroup = activityIndex.getGroup();
                Set<UUID> uuids = map.computeIfAbsent(activityGroup, Maps::createSet);
                uuids.add(player.getUnsafe(PlayerKeys.UUID));
            }
        }
    }
    return activityData;
}
Also used : PlayerContainer(com.djrapitops.plan.delivery.domain.container.PlayerContainer) Maps(com.djrapitops.plan.utilities.java.Maps) ServerUUID(com.djrapitops.plan.identification.ServerUUID)

Example 3 with Maps

use of com.djrapitops.plan.utilities.java.Maps in project Plan by plan-player-analytics.

the class SessionQueries method extractDataFromSessionSelectStatement.

private static List<FinishedSession> extractDataFromSessionSelectStatement(ResultSet set) throws SQLException {
    // Server UUID - Player UUID - Session Start - Session
    Map<ServerUUID, Map<UUID, SortedMap<Long, FinishedSession>>> byServer = new HashMap<>();
    // Utilities
    String[] gms = GMTimes.getGMKeyArray();
    Comparator<DateHolder> mostRecentFirst = new DateHolderRecentComparator();
    // Descending order, most recent first.
    Comparator<Long> longRecentComparator = (one, two) -> Long.compare(two, one);
    while (set.next()) {
        ServerUUID serverUUID = ServerUUID.fromString(set.getString(SessionsTable.SERVER_UUID));
        Map<UUID, SortedMap<Long, FinishedSession>> serverSessions = byServer.computeIfAbsent(serverUUID, Maps::create);
        UUID playerUUID = UUID.fromString(set.getString(SessionsTable.USER_UUID));
        SortedMap<Long, FinishedSession> playerSessions = serverSessions.computeIfAbsent(playerUUID, key -> new TreeMap<>(longRecentComparator));
        long sessionStart = set.getLong(SessionsTable.SESSION_START);
        // id, uuid, serverUUID, sessionStart, sessionEnd, mobKills, deaths, afkTime
        FinishedSession session = playerSessions.getOrDefault(sessionStart, new FinishedSession(playerUUID, serverUUID, sessionStart, set.getLong(SessionsTable.SESSION_END), set.getLong(SessionsTable.AFK_TIME), new DataMap()));
        DataMap extraData = session.getExtraData();
        extraData.put(FinishedSession.Id.class, new FinishedSession.Id(set.getInt(SessionsTable.ID)));
        extraData.put(MobKillCounter.class, new MobKillCounter(set.getInt(SessionsTable.MOB_KILLS)));
        extraData.put(DeathCounter.class, new DeathCounter(set.getInt(SessionsTable.DEATHS)));
        Optional<WorldTimes> existingWorldTimes = extraData.get(WorldTimes.class);
        Optional<PlayerKills> existingPlayerKills = extraData.get(PlayerKills.class);
        WorldTimes worldTimes = existingWorldTimes.orElseGet(WorldTimes::new);
        String worldName = set.getString(WorldTable.NAME);
        if (!worldTimes.contains(worldName)) {
            Map<String, Long> gmMap = new HashMap<>();
            gmMap.put(gms[0], set.getLong(WorldTimesTable.SURVIVAL));
            gmMap.put(gms[1], set.getLong(WorldTimesTable.CREATIVE));
            gmMap.put(gms[2], set.getLong(WorldTimesTable.ADVENTURE));
            gmMap.put(gms[3], set.getLong(WorldTimesTable.SPECTATOR));
            GMTimes gmTimes = new GMTimes(gmMap);
            worldTimes.setGMTimesForWorld(worldName, gmTimes);
        }
        if (!existingWorldTimes.isPresent())
            extraData.put(WorldTimes.class, worldTimes);
        ServerName serverName = new ServerName(Server.getIdentifiableName(set.getString("server_name"), set.getInt("server_id")));
        extraData.put(ServerName.class, serverName);
        PlayerKills playerKills = existingPlayerKills.orElseGet(PlayerKills::new);
        String victimName = set.getString("victim_name");
        if (victimName != null) {
            PlayerKill.Killer killer = new PlayerKill.Killer(UUID.fromString(set.getString(KillsTable.KILLER_UUID)), set.getString("killer_name"));
            PlayerKill.Victim victim = new PlayerKill.Victim(UUID.fromString(set.getString(KillsTable.VICTIM_UUID)), victimName);
            ServerIdentifier serverIdentifier = new ServerIdentifier(serverUUID, serverName);
            String weapon = set.getString(KillsTable.WEAPON);
            long date = set.getLong(KillsTable.DATE);
            PlayerKill newKill = new PlayerKill(killer, victim, serverIdentifier, weapon, date);
            if (!playerKills.contains(newKill)) {
                playerKills.add(newKill);
            }
        }
        if (!existingPlayerKills.isPresent())
            extraData.put(PlayerKills.class, playerKills);
        extraData.put(PlayerName.class, new PlayerName(set.getString("name")));
        session.setAsFirstSessionIfMatches(set.getLong("registered"));
        playerSessions.put(sessionStart, session);
    }
    return byServer.values().stream().map(Map::values).flatMap(Collection::stream).map(SortedMap::values).flatMap(Collection::stream).sorted(// Disorder arises
    mostRecentFirst).collect(Collectors.toList());
}
Also used : SessionsMutator(com.djrapitops.plan.delivery.domain.mutators.SessionsMutator) java.util(java.util) ServerName(com.djrapitops.plan.delivery.domain.ServerName) com.djrapitops.plan.gathering.domain(com.djrapitops.plan.gathering.domain) PlayerName(com.djrapitops.plan.delivery.domain.PlayerName) ServerUUID(com.djrapitops.plan.identification.ServerUUID) TextStringBuilder(org.apache.commons.text.TextStringBuilder) Query(com.djrapitops.plan.storage.database.queries.Query) QueryAllStatement(com.djrapitops.plan.storage.database.queries.QueryAllStatement) com.djrapitops.plan.storage.database.sql.tables(com.djrapitops.plan.storage.database.sql.tables) PreparedStatement(java.sql.PreparedStatement) Sql(com.djrapitops.plan.storage.database.sql.building.Sql) Collectors(java.util.stream.Collectors) SQLException(java.sql.SQLException) DateHolder(com.djrapitops.plan.delivery.domain.DateHolder) Server(com.djrapitops.plan.identification.Server) ResultSet(java.sql.ResultSet) ServerIdentifier(com.djrapitops.plan.delivery.domain.ServerIdentifier) DateHolderRecentComparator(com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator) Maps(com.djrapitops.plan.utilities.java.Maps) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement) Maps(com.djrapitops.plan.utilities.java.Maps) PlayerName(com.djrapitops.plan.delivery.domain.PlayerName) ServerUUID(com.djrapitops.plan.identification.ServerUUID) ServerIdentifier(com.djrapitops.plan.delivery.domain.ServerIdentifier) ServerUUID(com.djrapitops.plan.identification.ServerUUID) DateHolder(com.djrapitops.plan.delivery.domain.DateHolder) ServerName(com.djrapitops.plan.delivery.domain.ServerName) DateHolderRecentComparator(com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator)

Example 4 with Maps

use of com.djrapitops.plan.utilities.java.Maps in project Plan by plan-player-analytics.

the class NicknameQueries method fetchAllNicknameData.

/**
 * Query database for all nickname data.
 *
 * @return Multimap: Server UUID - (Player UUID - List of nicknames)
 */
public static Query<Map<ServerUUID, Map<UUID, List<Nickname>>>> fetchAllNicknameData() {
    String sql = SELECT + NicknamesTable.NICKNAME + ',' + NicknamesTable.LAST_USED + ',' + NicknamesTable.USER_UUID + ',' + NicknamesTable.SERVER_UUID + FROM + NicknamesTable.TABLE_NAME;
    return new QueryAllStatement<Map<ServerUUID, Map<UUID, List<Nickname>>>>(sql, 5000) {

        @Override
        public Map<ServerUUID, Map<UUID, List<Nickname>>> processResults(ResultSet set) throws SQLException {
            Map<ServerUUID, Map<UUID, List<Nickname>>> map = new HashMap<>();
            while (set.next()) {
                ServerUUID serverUUID = ServerUUID.fromString(set.getString(NicknamesTable.SERVER_UUID));
                UUID uuid = UUID.fromString(set.getString(NicknamesTable.USER_UUID));
                Map<UUID, List<Nickname>> serverMap = map.computeIfAbsent(serverUUID, Maps::create);
                List<Nickname> nicknames = serverMap.computeIfAbsent(uuid, Lists::create);
                nicknames.add(new Nickname(set.getString(NicknamesTable.NICKNAME), set.getLong(NicknamesTable.LAST_USED), serverUUID));
            }
            return map;
        }
    };
}
Also used : ServerUUID(com.djrapitops.plan.identification.ServerUUID) QueryAllStatement(com.djrapitops.plan.storage.database.queries.QueryAllStatement) Maps(com.djrapitops.plan.utilities.java.Maps) Lists(com.djrapitops.plan.utilities.java.Lists) ResultSet(java.sql.ResultSet) ServerUUID(com.djrapitops.plan.identification.ServerUUID) Nickname(com.djrapitops.plan.delivery.domain.Nickname)

Aggregations

ServerUUID (com.djrapitops.plan.identification.ServerUUID)4 Maps (com.djrapitops.plan.utilities.java.Maps)4 ResultSet (java.sql.ResultSet)3 QueryAllStatement (com.djrapitops.plan.storage.database.queries.QueryAllStatement)2 DateHolder (com.djrapitops.plan.delivery.domain.DateHolder)1 Nickname (com.djrapitops.plan.delivery.domain.Nickname)1 PlayerName (com.djrapitops.plan.delivery.domain.PlayerName)1 ServerIdentifier (com.djrapitops.plan.delivery.domain.ServerIdentifier)1 ServerName (com.djrapitops.plan.delivery.domain.ServerName)1 PlayerContainer (com.djrapitops.plan.delivery.domain.container.PlayerContainer)1 SessionsMutator (com.djrapitops.plan.delivery.domain.mutators.SessionsMutator)1 com.djrapitops.plan.gathering.domain (com.djrapitops.plan.gathering.domain)1 Server (com.djrapitops.plan.identification.Server)1 Query (com.djrapitops.plan.storage.database.queries.Query)1 QueryStatement (com.djrapitops.plan.storage.database.queries.QueryStatement)1 Sql (com.djrapitops.plan.storage.database.sql.building.Sql)1 com.djrapitops.plan.storage.database.sql.tables (com.djrapitops.plan.storage.database.sql.tables)1 DateHolderRecentComparator (com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator)1 Lists (com.djrapitops.plan.utilities.java.Lists)1 PreparedStatement (java.sql.PreparedStatement)1