Search in sources :

Example 6 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class TestData method storeServers.

public static Transaction storeServers() {
    return new Transaction() {

        @Override
        protected void performOperations() {
            executeOther(new StoreServerInformationTransaction(new Server(serverUUID, "Server 1", "", TestConstants.VERSION)));
            executeOther(new StoreServerInformationTransaction(new Server(server2UUID, "Server 2", "", TestConstants.VERSION)));
            for (String worldName : serverWorldNames) {
                executeOther(new WorldNameStoreTransaction(serverUUID, worldName));
            }
            for (String worldName : server2WorldNames) {
                executeOther(new WorldNameStoreTransaction(server2UUID, worldName));
            }
        }
    };
}
Also used : Transaction(com.djrapitops.plan.storage.database.transactions.Transaction) StoreServerInformationTransaction(com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction) Server(com.djrapitops.plan.identification.Server) StoreServerInformationTransaction(com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction)

Example 7 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class ServerQueries method fetchServerMatchingIdentifier.

public static Query<Optional<Server>> fetchServerMatchingIdentifier(String identifier) {
    String sql = SELECT + '*' + FROM + ServerTable.TABLE_NAME + WHERE + "(LOWER(" + ServerTable.SERVER_UUID + ") LIKE LOWER(?)" + OR + "LOWER(" + ServerTable.NAME + ") LIKE LOWER(?)" + OR + ServerTable.ID + "=?" + OR + ServerTable.ID + "=?)" + AND + ServerTable.INSTALLED + "=?" + LIMIT + '1';
    return new QueryStatement<Optional<Server>>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setString(1, identifier);
            statement.setString(2, identifier);
            statement.setInt(3, NumberUtils.isParsable(identifier) ? Integer.parseInt(identifier) : -1);
            String id = identifier.startsWith("Server ") ? identifier.substring(7) : identifier;
            statement.setInt(4, NumberUtils.isParsable(id) ? Integer.parseInt(id) : -1);
            statement.setBoolean(5, true);
        }

        @Override
        public Optional<Server> processResults(ResultSet set) throws SQLException {
            if (set.next()) {
                return Optional.of(new Server(set.getInt(ServerTable.ID), ServerUUID.fromString(set.getString(ServerTable.SERVER_UUID)), set.getString(ServerTable.NAME), set.getString(ServerTable.WEB_ADDRESS), set.getBoolean(ServerTable.PROXY), set.getString(ServerTable.PLAN_VERSION)));
            }
            return Optional.empty();
        }
    };
}
Also used : Server(com.djrapitops.plan.identification.Server) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 8 with Server

use of com.djrapitops.plan.identification.Server 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("server_uuid"));
        Map<UUID, SortedMap<Long, FinishedSession>> serverSessions = byServer.computeIfAbsent(serverUUID, Maps::create);
        UUID playerUUID = UUID.fromString(set.getString(UsersTable.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)));
        extraData.put(JoinAddress.class, new JoinAddress(set.getString("join_address")));
        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) JoinAddress(com.djrapitops.plan.gathering.domain.event.JoinAddress) 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) JoinAddress(com.djrapitops.plan.gathering.domain.event.JoinAddress) 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 9 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class ExportScheduler method scheduleServerPageExport.

private void scheduleServerPageExport() {
    if (config.isFalse(ExportSettings.SERVER_PAGE))
        return;
    Collection<Server> servers = dbSystem.getDatabase().query(ServerQueries.fetchPlanServerInformationCollection());
    int serverCount = servers.size();
    if (serverCount == 0)
        return;
    long period = TimeAmount.toTicks(config.get(ExportSettings.EXPORT_PERIOD), TimeUnit.MILLISECONDS);
    long offset = period / serverCount;
    Optional<Server> proxy = servers.stream().filter(Server::isProxy).findFirst();
    proxy.ifPresent(mainServer -> runnableFactory.create(new ExportTask(exporter, same -> same.exportServerPage(mainServer), errorLogger)).runTaskTimerAsynchronously(TimeAmount.toTicks(1, TimeUnit.MINUTES), period));
    // Delay first server export if on a network.
    int offsetMultiplier = proxy.isPresent() ? 1 : 0;
    for (Server server : servers) {
        runnableFactory.create(new ExportTask(exporter, same -> {
            same.exportServerPage(server);
            same.exportServerJSON(server);
        }, errorLogger)).runTaskTimerAsynchronously(offset * offsetMultiplier, period);
        offsetMultiplier++;
    }
}
Also used : PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) ServerInfo(com.djrapitops.plan.identification.ServerInfo) ExportSettings(com.djrapitops.plan.settings.config.paths.ExportSettings) Collection(java.util.Collection) TimeAmount(net.playeranalytics.plugin.scheduling.TimeAmount) Singleton(javax.inject.Singleton) Database(com.djrapitops.plan.storage.database.Database) DBSystem(com.djrapitops.plan.storage.database.DBSystem) Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) Server(com.djrapitops.plan.identification.Server) ErrorLogger(com.djrapitops.plan.utilities.logging.ErrorLogger) RunnableFactory(net.playeranalytics.plugin.scheduling.RunnableFactory) Optional(java.util.Optional) PluginRunnable(net.playeranalytics.plugin.scheduling.PluginRunnable) ServerQueries(com.djrapitops.plan.storage.database.queries.objects.ServerQueries) Server(com.djrapitops.plan.identification.Server)

Example 10 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class DatabaseCommands method onUninstalled.

public void onUninstalled(CMDSender sender, Arguments arguments) {
    ensureDatabaseIsOpen();
    String identifier = arguments.concatenate(" ");
    Server server = dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(identifier)).orElseThrow(() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_SERVER_NOT_FOUND, identifier)));
    if (server.getUuid().equals(serverInfo.getServerUUID())) {
        throw new IllegalArgumentException(locale.getString(CommandLang.UNINSTALLING_SAME_SERVER));
    }
    dbSystem.getDatabase().executeTransaction(new SetServerAsUninstalledTransaction(server.getUuid()));
    sender.send(locale.getString(CommandLang.PROGRESS_SUCCESS));
    sender.send(locale.getString(CommandLang.DB_UNINSTALLED));
}
Also used : SetServerAsUninstalledTransaction(com.djrapitops.plan.storage.database.transactions.commands.SetServerAsUninstalledTransaction) Server(com.djrapitops.plan.identification.Server)

Aggregations

Server (com.djrapitops.plan.identification.Server)28 ServerUUID (com.djrapitops.plan.identification.ServerUUID)11 StoreServerInformationTransaction (com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction)9 Test (org.junit.jupiter.api.Test)7 PreparedStatement (java.sql.PreparedStatement)6 Database (com.djrapitops.plan.storage.database.Database)5 QueryStatement (com.djrapitops.plan.storage.database.queries.QueryStatement)5 ResultSet (java.sql.ResultSet)5 SessionsMutator (com.djrapitops.plan.delivery.domain.mutators.SessionsMutator)3 EnableException (com.djrapitops.plan.exceptions.EnableException)3 ServerInfo (com.djrapitops.plan.identification.ServerInfo)3 PlanConfig (com.djrapitops.plan.settings.config.PlanConfig)3 DBSystem (com.djrapitops.plan.storage.database.DBSystem)3 RemoveEverythingTransaction (com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction)3 SetServerAsUninstalledTransaction (com.djrapitops.plan.storage.database.transactions.commands.SetServerAsUninstalledTransaction)3 User (com.djrapitops.plan.delivery.domain.auth.User)2 ExtensionPlayerDataQuery (com.djrapitops.plan.extension.implementation.storage.queries.ExtensionPlayerDataQuery)2 Locale (com.djrapitops.plan.settings.locale.Locale)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2