/* * Copyright (c) 2018 univento.eu - All rights reserved * You are not allowed to use, distribute or modify this code */ package eu.univento.commons.player; import eu.univento.commons.Commons; import eu.univento.commons.player.ban.BanData; import eu.univento.commons.player.ban.BanReason; import eu.univento.commons.player.currency.CurrencyType; import eu.univento.commons.player.kick.KickData; import eu.univento.commons.player.kick.KickReason; import eu.univento.commons.player.language.Language; import eu.univento.commons.player.mute.MuteData; import eu.univento.commons.player.mute.MuteReason; import eu.univento.commons.player.rank.Group; import eu.univento.commons.player.rank.Rank; import eu.univento.commons.player.ranking.Ranking; import eu.univento.commons.player.settings.PlayerSettings; import eu.univento.commons.player.user.UserInformation; import eu.univento.commons.player.warn.WarnData; import eu.univento.commons.player.warn.WarnReason; import io.vertx.core.json.JsonObject; import io.vertx.ext.mongo.MongoClient; import lombok.Getter; import java.time.Instant; import java.util.*; import java.util.concurrent.*; import java.util.logging.Level; /** * @author joethei * @version 1.5 */ @Getter public class DatabasePlayer { private UUID uuid; private String name; private JsonObject json; private Rank rank; private PlayerSettings settings; private Language language; private boolean muted; private ScheduledFuture updateFuture; public DatabasePlayer(UUID uuid) { this.uuid = uuid; } public DatabasePlayer(String name) { UserInformation.get(name).whenComplete((userInformation, throwable) -> new DatabasePlayer(UUID.fromString(userInformation.getUuid()))); } public void load(String host) { Commons.getCommons().getDatabaseManager().getMongoDB().getClient().find("players", new JsonObject().put("uuid", uuid.toString()), res -> { if(res.succeeded()) { if(res.result().isEmpty()) insert(host); }else { Commons.getCommons().getLogger().log(Level.SEVERE, res.cause().getMessage()); res.cause().printStackTrace(); } }); getRankAsync().whenComplete((rank1, throwable) -> rank = rank1); getSettingsAsync().whenComplete((playerSettings, throwable) -> settings = playerSettings); isMutedAsync().whenComplete((aBoolean, throwable) -> muted = aBoolean); getLanguageAsync().whenComplete((language1, throwable) -> language = language1); Commons.getCommons().getDatabaseManager().getRedis().getClient().get("Name:" + uuid.toString(), res -> { if(res.succeeded()) { name = res.result(); }else { UserInformation.get(uuid.toString()).whenComplete((userInformation, throwable) -> { name = userInformation.getUsername(); Commons.getCommons().getDatabaseManager().getRedis().getClient().set("Name:" + uuid.toString(), name, event -> { if(event.failed()) event.cause().printStackTrace(); }); }); } }); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Runnable updater = this::update; updateFuture = scheduler.scheduleAtFixedRate(updater, 5, 5, TimeUnit.MINUTES); } public void save() { updateFuture.cancel(true); } private void update() { Commons.getCommons().getDatabaseManager().getMongoDB().getClient().findOne("players", new JsonObject().put("uuid", uuid.toString()), null, res -> { if(res.failed()) res.cause().printStackTrace(); json = res.result(); }); } //TODO: add more stuff private void insert(String hostname) { JsonObject social = new JsonObject().put("tsID", "null").put("boardID", 0); JsonObject currency = new JsonObject().put("vents", 0); JsonObject generalSettings = new JsonObject(); JsonObject socialSettings = new JsonObject().put("partyRequests", true).put("friendRequests", true).put("friendJump", true).put("clanRequests", true).put("tsMove", true); JsonObject minigamesSettings = new JsonObject(); JsonObject settings = new JsonObject().put("general", generalSettings).put("social", socialSettings).put("minigames", minigamesSettings).put("nicked", false).put("language", Locale.ENGLISH.getLanguage()); JsonObject statistics = new JsonObject(); JsonObject friends = new JsonObject(); JsonObject history = new JsonObject().put(Instant.now().toString(), "PreIntro"); JsonObject items = new JsonObject(); JsonObject json = new JsonObject().put("uuid", uuid.toString()).put("lastName", name).put("rank", Rank.Player.name()).put("firstLogin", Instant.now()).put("lastLogin", Instant.now()).put("lastOnline", Instant.now()).put("onlineTime", 0L) .put("lastIP", hostname).put("social", social).put("currency", currency).put("settings", settings).put("statistics", statistics).put("friends", friends).put("history", history).put("items", items); Commons.getCommons().getDatabaseManager().getMongoDB().getClient().insert("players", json, res -> { if(res.failed()) { Commons.getCommons().getLogger().log(Level.SEVERE, res.cause().getMessage()); res.cause().printStackTrace(); } }); } public Language getLanguage() { return Language.getLanguage(getSettings().getLanguage()); } public CompletableFuture getLanguageAsync() { CompletableFuture future = new CompletableFuture<>(); getSettingsAsync().whenComplete((playerSettings, throwable) -> future.complete(Language.getLanguage(playerSettings.getLanguage()))); return future; } public CompletableFuture getSettingsAsync() { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase().whenComplete((entries, throwable) -> future.complete(new PlayerSettings(this, entries.getJsonObject("Settings")))); return future; } public CompletableFuture getRanking() { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase().whenComplete((entries, throwable) -> future.complete(Ranking.valueOf(entries.getString("ranking")))); return future; } public void ban(BanReason reason, UUID banner, String customMessage, String proof) { getAllBans().whenComplete((banData, throwable) -> { int i = 0; if (banData.size() > 1) { for (BanData ban : banData) { i = +ban.getReason().getValue(); } } Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, reason.getValue() + i); JsonObject obj = new JsonObject().put("uuid", uuid.toString()) .put("date", calendar.getTime()) .put("reason", reason.name()) .put("banner", banner.toString()) .put("customMessage", customMessage) .put("proof", proof); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.insert("bans", obj, res -> { if (res.failed()) res.cause().printStackTrace(); }); }); } public CompletableFuture getBan() { CompletableFuture future = new CompletableFuture<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.findOne("bans", new JsonObject().put("uuid", uuid.toString()), null, res -> { if(res.failed()) future.complete(null); if(res.result() == null) future.complete(null); JsonObject result = res.result(); if(result.isEmpty()) future.complete(null); BanData data = new BanData(BanReason.valueOf(result.getString("reason")), UUID.fromString(result.getString("banner")), result.getString("customMessage"), result.getInstant("date"), result.getString("proof")); future.complete(data); }); return future; } public CompletableFuture isBanned() { CompletableFuture future = new CompletableFuture<>(); Commons.getCommons().getDatabaseManager().getMongoDB().getClient().find("bans", new JsonObject().put("uuid", uuid.toString()), res -> { if(res.succeeded()) { getBan().whenComplete((banData, throwable) -> future.complete(banData != null && !Instant.now().isAfter(banData.getUnbanDate()))); }else future.complete(false); }); return future; } public CompletableFuture> getAllBans() { CompletableFuture> future = new CompletableFuture<>(); Collection data = new LinkedList<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.find("bans", new JsonObject().put("uuid", uuid.toString()), res -> { if (res.succeeded()) { for (JsonObject json : res.result()) { data.add(new BanData(BanReason.valueOf(json.getString("reason")), UUID.fromString(json.getString("banner")), json.getString("customMessage"), json.getInstant("date"), json.getString("proof"))); } future.complete(data); } }); return future; } public void warn(WarnReason reason, String warner, String proof) { JsonObject obj = new JsonObject().put("uuid", uuid.toString()).put("reason", reason.getName()).put("warner", warner).put("date", Instant.now()).put("proof", proof); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.insert("warns", obj, res -> { if (res.failed()) { try { throw res.cause(); } catch (Throwable throwable) { throwable.printStackTrace(); } } }); client.close(); } public CompletableFuture isWarned() { CompletableFuture future = new CompletableFuture<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.findOne("warns", new JsonObject().put("uuid", uuid.toString()), null, res -> { if (res.succeeded()) { JsonObject json = res.result(); future.complete(json != null && Instant.now().isAfter(json.getInstant("date"))); } }); client.close(); return future; } public CompletableFuture> getAllWarns() { CompletableFuture> future = new CompletableFuture<>(); Collection data = new LinkedList<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.find("warns", new JsonObject().put("uuid", uuid.toString()), res -> { if (res.succeeded()) { for (JsonObject json : res.result()) { data.add(new WarnData(WarnReason.valueOf(json.getString("reason")), UUID.fromString(json.getString("warner")), json.getInstant("date"), json.getString("proof"))); } future.complete(data); } }); return future; } public void kick(UUID kicker, KickReason reason) { JsonObject obj = new JsonObject().put("uuid", uuid.toString()).put("reason", reason.name()).put("kicker", kicker.toString()).put("date", Instant.now()); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.insert("kicks", obj, res -> { if (res.failed()) { try { throw res.cause(); } catch (Throwable throwable) { throwable.printStackTrace(); } } }); } public CompletableFuture> getAllKicks() { CompletableFuture> future = new CompletableFuture<>(); Collection data = new LinkedList<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.find("kicks", new JsonObject().put("uuid", uuid.toString()), res -> { if (res.succeeded()) { for (JsonObject json : res.result()) { data.add(new KickData(KickReason.valueOf(json.getString("reason")), json.getInstant("date"), UUID.fromString("kicker"))); } future.complete(data); } }); return future; } public void mute(UUID muter, MuteReason reason) { MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); getAllMutes().whenComplete((muteData, throwable) -> { int i = 0; if (muteData.size() > 1) { for (MuteData mute : muteData) i = +mute.getReason().getValue(); } Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, reason.getValue() + i); JsonObject obj = new JsonObject().put("uuid", uuid.toString()) .put("reason", reason.name()) .put("muter", muter.toString()) .put("date", calendar.getTime()); client.insert("mutes", obj, res -> { if (res.failed()) { try { throw res.cause(); } catch (Throwable throwable1) { throwable1.printStackTrace(); } } }); }); } public CompletableFuture isMutedAsync() { CompletableFuture future = new CompletableFuture<>(); getMute().whenComplete((muteData, throwable) -> future.complete(muteData != null && !Instant.now().isAfter(muteData.getDate()))); return future; } public CompletableFuture getMute() { CompletableFuture future = new CompletableFuture<>(); getAllMutes().whenComplete((muteData, throwable) -> { for (MuteData data : muteData) { if (Instant.now().isBefore(data.getDate())) future.complete(data); } }); return future; } public CompletableFuture> getAllMutes() { CompletableFuture> future = new CompletableFuture<>(); Collection data = new LinkedList<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.find("mutes", new JsonObject().put("uuid", uuid.toString()), res -> { if (res.succeeded()) { for (JsonObject json : res.result()) { data.add(new MuteData(MuteReason.valueOf(json.getString("reason")), UUID.fromString(json.getString("uuid")), UUID.fromString(json.getString("muter")), json.getInstant("date"))); } future.complete(data); } }); return future; } public CompletableFuture getRankAsync() { CompletableFuture future = new CompletableFuture<>(); getStringFromDatabase("rank").whenComplete((s, throwable) -> future.complete(Rank.valueOf(s))); return future; } public void setRank(Rank rank) { setInDatabase("rank", rank.name()); } public boolean isAllowed(Rank rank) { return getRank().getValue() >= rank.getValue(); } public CompletableFuture isAllowedAsync(Rank rank) { CompletableFuture future = new CompletableFuture<>(); getRankAsync().whenComplete((rank1, throwable) -> future.complete(rank1.getValue() >= rank.getValue())); return future; } public boolean isAllowed(Group group) { return getRank().getGroup() == group; } public CompletableFuture isAllowedAsync(Group group) { CompletableFuture future = new CompletableFuture<>(); getRankAsync().whenComplete((rank1, throwable) -> future.complete(rank1.getGroup() == group)); return future; } public boolean isTeamMember() { return isAllowed(Rank.Sound); } public CompletableFuture hasPlayedBefore() { CompletableFuture future = new CompletableFuture<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.findOne("players", new JsonObject().put("uuid", uuid.toString()), null, res -> future.complete(res.succeeded())); return future; } public void buy(CurrencyType type, int price) { getCurrency(type).whenComplete((integer, throwable) -> setCurrency(type, integer - price)); } private void setCurrency(CurrencyType type, int currency) { setInDatabase(type.getName(), currency); } public CompletableFuture getCurrency(CurrencyType type) { return getIntegerFromDatabase(type.getName()); } public void addCurrency(CurrencyType type, int amount) { getCurrency(type).whenComplete((integer, throwable) -> setCurrency(type, integer + amount)); } public void setTSID(String id) { setInDatabase("tsid", id); } public CompletableFuture getFirstLogin() { return getInstantFromDatabase("firstLogin"); } public CompletableFuture getLastLogin() { return getInstantFromDatabase("lastLogin"); } public CompletableFuture getLastOnline() { return getInstantFromDatabase("lastOnline"); } public CompletableFuture getLastIP() { return getStringFromDatabase("lastIP"); } public CompletableFuture getTSID() { return getStringFromDatabase("tsID"); } public CompletableFuture getTimesJoined() { return getIntegerFromDatabase("timesJoined"); } public CompletableFuture getOnlineTime() { return getLongFromDatabase("OnlineTime"); } public void setOnlineTime(long time) { setInDatabase("onlineTime", time); } public CompletableFuture getBoardID() { return getIntegerFromDatabase("boardID"); } public void setBoardID(int id) { setInDatabase("boardID", id); } public void setInDatabase(String name, Object obj) { MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.updateCollection("players", new JsonObject().put("uuid", uuid.toString()), new JsonObject().put("$set", new JsonObject().put(name, obj)), res -> { if(res.failed()) { res.cause().printStackTrace(); } }); update(); } private CompletableFuture getStringFromDatabase(String key) { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase().whenComplete((entries, throwable) -> future.complete(entries.getString(key))); return future; } private CompletableFuture getLongFromDatabase(String key) { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase().whenComplete((entries, throwable) -> future.complete(entries.getLong(key))); return future; } private CompletableFuture getIntegerFromDatabase(String key) { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase().whenComplete((entries, throwable) -> future.complete(entries.getInteger(key))); return future; } private CompletableFuture getBooleanFromDatabase(String key) { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase().whenComplete((entries, throwable) -> future.complete(entries.getBoolean(key))); return future; } public CompletableFuture getInstantFromDatabase(String key) { CompletableFuture future = new CompletableFuture<>(); getObjectFromDatabase().whenComplete((entries, throwable) -> future.complete(entries.getInstant(key))); return future; } public CompletableFuture getObjectFromDatabase() { CompletableFuture future = new CompletableFuture<>(); MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient(); client.findOne("players", new JsonObject().put("uuid", uuid.toString()), null, res -> future.complete(res.result())); return future; } }