Search in sources :

Example 1 with PlayerService

use of com.ebicep.warlords.database.repositories.player.PlayerService in project Warlords by ebicep.

the class DatabaseManager method init.

public static void init() {
    if (!enabled) {
        NPCManager.createGameNPC();
        return;
    }
    if (!LeaderboardManager.enabled) {
        NPCManager.createGameNPC();
    }
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
    try {
        playerService = context.getBean("playerService", PlayerService.class);
        gameService = context.getBean("gameService", GameService.class);
    } catch (Exception e) {
        playerService = null;
        gameService = null;
        NPCManager.createGameNPC();
        e.printStackTrace();
        return;
    }
    try {
        System.out.println("CACHES");
        for (String cacheName : MultipleCacheResolver.playersCacheManager.getCacheNames()) {
            System.out.println(Objects.requireNonNull(((CaffeineCache) MultipleCacheResolver.playersCacheManager.getCache(cacheName)).getNativeCache()).asMap());
            Objects.requireNonNull(MultipleCacheResolver.playersCacheManager.getCache(cacheName)).clear();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Loading all online players
    Bukkit.getOnlinePlayers().forEach(player -> {
        loadPlayer(player.getUniqueId(), PlayersCollections.LIFETIME, () -> {
            updateName(player.getUniqueId());
        });
    });
    // Loading last 5 games
    Warlords.newChain().asyncFirst(() -> gameService.getLastGames(10)).syncLast((games) -> {
        previousGames.addAll(games);
        LeaderboardManager.addHologramLeaderboards(UUID.randomUUID().toString(), true);
    }).execute();
    MongoCollection<Document> resetTimings = warlordsDatabase.getCollection("Reset_Timings");
    // checking weekly date, if over 10,000 minutes (10080 == 1 week) reset weekly
    Document weeklyDocumentInfo = resetTimings.find().filter(eq("time", "weekly")).first();
    Date current = new Date();
    if (weeklyDocumentInfo != null && weeklyDocumentInfo.get("last_reset") != null) {
        Date lastReset = weeklyDocumentInfo.getDate("last_reset");
        long timeDiff = current.getTime() - lastReset.getTime();
        System.out.println("Reset Time: " + timeDiff / 60000);
        if (timeDiff > 0 && timeDiff / (1000 * 60) > 10000) {
            Warlords.newSharedChain(UUID.randomUUID().toString()).delay(// to make sure leaderboards are cached
            20 * 10).async(() -> {
                // adding new document with top weekly players
                Document topPlayers = LeaderboardManager.getTopPlayersOnLeaderboard();
                MongoCollection<Document> weeklyLeaderboards = warlordsDatabase.getCollection("Weekly_Leaderboards");
                weeklyLeaderboards.insertOne(topPlayers);
                ExperienceManager.awardWeeklyExperience(topPlayers);
                // clearing weekly
                playerService.deleteAll(PlayersCollections.WEEKLY);
                // reloading boards
                LeaderboardManager.addHologramLeaderboards(UUID.randomUUID().toString(), false);
                // updating date to current
                resetTimings.updateOne(and(eq("time", "weekly"), eq("last_reset", lastReset)), new Document("$set", new Document("time", "weekly").append("last_reset", current)));
            }).sync(() -> Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[Warlords] Weekly player information reset")).execute();
        }
    }
    // checking daily date, if over 1,400 minutes (1440 == 1 day) reset daily
    Document dailyDocumentInfo = resetTimings.find().filter(eq("time", "daily")).first();
    if (dailyDocumentInfo != null && dailyDocumentInfo.get("last_reset") != null) {
        Date lastReset = dailyDocumentInfo.getDate("last_reset");
        long timeDiff = current.getTime() - lastReset.getTime();
        if (timeDiff > 0 && timeDiff / (1000 * 60) > 1400) {
            Warlords.newSharedChain(UUID.randomUUID().toString()).async(() -> {
                // clearing daily
                playerService.deleteAll(PlayersCollections.DAILY);
                // updating date to current
                resetTimings.updateOne(and(eq("time", "daily"), eq("last_reset", lastReset)), new Document("$set", new Document("time", "daily").append("last_reset", current)));
            }).sync(() -> Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[Warlords] Daily player information reset")).execute();
        }
    }
}
Also used : PlayerService(com.ebicep.warlords.database.repositories.player.PlayerService) Document(org.bson.Document) PlayerService(com.ebicep.warlords.database.repositories.player.PlayerService) java.util(java.util) ApplicationConfiguration(com.ebicep.warlords.database.configuration.ApplicationConfiguration) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) URL(java.net.URL) DatabaseGameBase.previousGames(com.ebicep.warlords.database.repositories.games.pojos.DatabaseGameBase.previousGames) MongoDatabase(com.mongodb.client.MongoDatabase) Player(org.bukkit.entity.Player) DatabaseGameBase(com.ebicep.warlords.database.repositories.games.pojos.DatabaseGameBase) DatabasePlayer(com.ebicep.warlords.database.repositories.player.pojos.general.DatabasePlayer) JSONArray(org.json.simple.JSONArray) AtomicReference(java.util.concurrent.atomic.AtomicReference) Filters.and(com.mongodb.client.model.Filters.and) ParseException(org.json.simple.parser.ParseException) JSONValue(org.json.simple.JSONValue) CaffeineCache(org.springframework.cache.caffeine.CaffeineCache) Filters.eq(com.mongodb.client.model.Filters.eq) Bukkit(org.bukkit.Bukkit) Warlords(com.ebicep.warlords.Warlords) IOException(java.io.IOException) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) PlayersCollections(com.ebicep.warlords.database.repositories.player.PlayersCollections) com.ebicep.warlords.player(com.ebicep.warlords.player) IOUtils(org.apache.commons.io.IOUtils) JSONObject(org.json.simple.JSONObject) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GamesCollections(com.ebicep.warlords.database.repositories.games.GamesCollections) NPCManager(com.ebicep.customentities.npc.NPCManager) LeaderboardManager(com.ebicep.warlords.database.leaderboards.LeaderboardManager) ChatColor(org.bukkit.ChatColor) GameService(com.ebicep.warlords.database.repositories.games.GameService) MultipleCacheResolver(com.ebicep.warlords.database.cache.MultipleCacheResolver) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GameService(com.ebicep.warlords.database.repositories.games.GameService) Document(org.bson.Document) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Aggregations

NPCManager (com.ebicep.customentities.npc.NPCManager)1 Warlords (com.ebicep.warlords.Warlords)1 MultipleCacheResolver (com.ebicep.warlords.database.cache.MultipleCacheResolver)1 ApplicationConfiguration (com.ebicep.warlords.database.configuration.ApplicationConfiguration)1 LeaderboardManager (com.ebicep.warlords.database.leaderboards.LeaderboardManager)1 GameService (com.ebicep.warlords.database.repositories.games.GameService)1 GamesCollections (com.ebicep.warlords.database.repositories.games.GamesCollections)1 DatabaseGameBase (com.ebicep.warlords.database.repositories.games.pojos.DatabaseGameBase)1 DatabaseGameBase.previousGames (com.ebicep.warlords.database.repositories.games.pojos.DatabaseGameBase.previousGames)1 PlayerService (com.ebicep.warlords.database.repositories.player.PlayerService)1 PlayersCollections (com.ebicep.warlords.database.repositories.player.PlayersCollections)1 DatabasePlayer (com.ebicep.warlords.database.repositories.player.pojos.general.DatabasePlayer)1 com.ebicep.warlords.player (com.ebicep.warlords.player)1 MongoClient (com.mongodb.client.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 Filters.and (com.mongodb.client.model.Filters.and)1 Filters.eq (com.mongodb.client.model.Filters.eq)1 IOException (java.io.IOException)1 URL (java.net.URL)1