use of com.ebicep.warlords.database.repositories.games.GameService 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();
}
}
}
Aggregations