490 lines
20 KiB
Java
490 lines
20 KiB
Java
/*
|
|
* Copyright (c) 2017 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.friend.FriendData;
|
|
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.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.CompletableFuture;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 1.5
|
|
*/
|
|
|
|
@Getter
|
|
public class DatabasePlayer {
|
|
|
|
private UUID uuid;
|
|
private String name;
|
|
|
|
private Rank rank;
|
|
private PlayerSettings settings;
|
|
private Language language;
|
|
private boolean muted;
|
|
|
|
public DatabasePlayer(UUID uuid, String name) {
|
|
this.uuid = uuid;
|
|
this.name = name;
|
|
}
|
|
|
|
public void load() {
|
|
getRankAsync().whenComplete((rank1, throwable) -> rank = rank1);
|
|
getSettingsAsync().whenComplete((playerSettings, throwable) -> settings = playerSettings);
|
|
isMutedAsync().whenComplete((aBoolean, throwable) -> muted = aBoolean);
|
|
getLanguageAsync().whenComplete((language1, throwable) -> language = language1);
|
|
}
|
|
|
|
public Language getLanguage() {
|
|
return Language.getLanguage(getSettings().getLanguage());
|
|
}
|
|
|
|
public CompletableFuture<Language> getLanguageAsync() {
|
|
CompletableFuture<Language> future = new CompletableFuture<>();
|
|
getSettingsAsync().whenComplete((playerSettings, throwable) -> future.complete(Language.getLanguage(playerSettings.getLanguage())));
|
|
return future;
|
|
}
|
|
|
|
|
|
public CompletableFuture<PlayerSettings> getSettingsAsync() {
|
|
CompletableFuture<PlayerSettings> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("Settings").whenComplete((entries, throwable) ->
|
|
future.complete(new PlayerSettings(this, entries.getJsonObject("Settings"))));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Ranking> getRanking() {
|
|
CompletableFuture<Ranking> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("ranking").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<BanData> getBan() {
|
|
CompletableFuture<BanData> 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);
|
|
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<Boolean> isBanned() {
|
|
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
|
getBan().whenComplete((banData, throwable) -> future.complete(banData != null && !Instant.now().isAfter(banData.getUnbanDate())));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Collection<BanData>> getAllBans() {
|
|
CompletableFuture<Collection<BanData>> future = new CompletableFuture<>();
|
|
Collection<BanData> 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<Boolean> isWarned() {
|
|
CompletableFuture<Boolean> 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<Collection<WarnData>> getAllWarns() {
|
|
CompletableFuture<Collection<WarnData>> future = new CompletableFuture<>();
|
|
Collection<WarnData> 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<Collection<KickData>> getAllKicks() {
|
|
CompletableFuture<Collection<KickData>> future = new CompletableFuture<>();
|
|
Collection<KickData> 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<Boolean> isMutedAsync() {
|
|
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
|
getMute().whenComplete((muteData, throwable) -> future.complete(muteData != null && !Instant.now().isAfter(muteData.getDate())));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<MuteData> getMute() {
|
|
CompletableFuture<MuteData> future = new CompletableFuture<>();
|
|
getAllMutes().whenComplete((muteData, throwable) -> {
|
|
for (MuteData data : muteData) {
|
|
if (Instant.now().isBefore(data.getDate())) future.complete(data);
|
|
}
|
|
});
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Collection<MuteData>> getAllMutes() {
|
|
CompletableFuture<Collection<MuteData>> future = new CompletableFuture<>();
|
|
Collection<MuteData> 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<Rank> getRankAsync() {
|
|
CompletableFuture<Rank> future = new CompletableFuture<>();
|
|
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
|
|
client.findOne("players", new JsonObject().put("uuid", uuid.toString()), null, res -> {
|
|
if(res.succeeded()) {
|
|
future.complete(Rank.valueOf(res.result().getString("rank")));
|
|
}else{
|
|
res.cause().printStackTrace();
|
|
future.complete(Rank.Player);
|
|
}
|
|
});
|
|
return future;
|
|
}
|
|
|
|
public void setRank(Rank rank) {
|
|
setInDatabase("rank", rank.name());
|
|
}
|
|
|
|
public boolean isAllowed(Rank rank) {
|
|
return getRank().getValue() >= rank.getValue();
|
|
}
|
|
|
|
public CompletableFuture<Boolean> isAllowedAsync(Rank rank) {
|
|
CompletableFuture<Boolean> 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<Boolean> isAllowedAsync(Group group) {
|
|
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
|
getRankAsync().whenComplete((rank1, throwable) -> future.complete(rank1.getGroup() == group));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Boolean> hasPlayedBefore() {
|
|
CompletableFuture<Boolean> 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 setTSID(String id) {
|
|
setInDatabase("tsid", id);
|
|
}
|
|
|
|
public void setCoins(int coins) {
|
|
setInDatabase("coins", coins);
|
|
}
|
|
|
|
public void setExperience(int experience) {
|
|
setInDatabase("experience", experience);
|
|
}
|
|
|
|
public CompletableFuture<Instant> getFirstLogin() {
|
|
CompletableFuture<Instant> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("firstLogin").whenComplete((entries, throwable) -> future.complete(entries.getInstant("firstLogin")));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Instant> getLastLogin() {
|
|
CompletableFuture<Instant> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("lastLogin").whenComplete((entries, throwable) -> future.complete(entries.getInstant("lastLogin")));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Instant> getLastOnline() {
|
|
CompletableFuture<Instant> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("lastOnline").whenComplete((entries, throwable) -> future.complete(entries.getInstant("lastOnline")));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<String> getLastIP() {
|
|
CompletableFuture<String> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("lastIP").whenComplete((entries, throwable) -> future.complete(entries.getString("lastIP")));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<String> getTSID() {
|
|
CompletableFuture<String> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("tsid").whenComplete((entries, throwable) -> future.complete(entries.getString("tsid")));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Integer> getTimesJoined() {
|
|
CompletableFuture<Integer> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("timesJoined").whenComplete((entries, throwable) -> future.complete(entries.getInteger("TimesJoined")));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Integer> getCoins() {
|
|
CompletableFuture<Integer> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("coins").whenComplete((entries, throwable) -> future.complete(entries.getInteger("coins")));
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Integer> getExperience() {
|
|
CompletableFuture<Integer> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("experience").whenComplete((entries, throwable) -> future.complete(entries.getInteger("experience")));
|
|
return future;
|
|
}
|
|
|
|
private void setFriends(Collection<FriendData> friends) {
|
|
List<UUID> list = friends.stream().map(FriendData::getUuid).collect(Collectors.toCollection(LinkedList::new));
|
|
|
|
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
|
|
client.findOneAndUpdate("friends", new JsonObject().put("uuid", uuid.toString()),
|
|
new JsonObject().put("$set", new JsonObject().put("friends", list)), res -> {
|
|
if (res.failed()) {
|
|
try {
|
|
throw res.cause();
|
|
} catch (Throwable throwable) {
|
|
throwable.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public CompletableFuture<Collection<FriendData>> getFriends() {
|
|
CompletableFuture<Collection<FriendData>> future = new CompletableFuture<>();
|
|
Collection<FriendData> data = new LinkedList<>();
|
|
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
|
|
client.findOne("friends", new JsonObject().put("uuid", uuid.toString()), null, res -> {
|
|
if (res.succeeded()) {
|
|
for (Object uuid : res.result().getJsonArray("friends")) {
|
|
data.add(new FriendData(UUID.fromString(String.valueOf(uuid))));
|
|
}
|
|
future.complete(data);
|
|
}
|
|
});
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Boolean> isFriend(UUID uuid) {
|
|
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
|
getFriends().whenComplete((friendData, throwable) -> {
|
|
for (FriendData data : friendData) {
|
|
if (data.getUuid() == uuid) future.complete(true);
|
|
}
|
|
future.complete(false);
|
|
});
|
|
|
|
return future;
|
|
}
|
|
|
|
public void addFriend(UUID uuid) {
|
|
getFriends().whenComplete((friendData, throwable) -> {
|
|
friendData.add(new FriendData(uuid));
|
|
setFriends(friendData);
|
|
});
|
|
}
|
|
|
|
public void removeFriend(UUID uuid) {
|
|
getFriends().whenComplete((friendData, throwable) -> {
|
|
friendData.remove(new FriendData(uuid));
|
|
setFriends(friendData);
|
|
});
|
|
|
|
}
|
|
|
|
public void addCoins(int coins) {
|
|
getCoins().whenComplete((integer, throwable) -> setCoins(integer + coins));
|
|
}
|
|
|
|
public void substractCoins(int coins) {
|
|
getCoins().whenComplete((integer, throwable) -> setCoins(integer - coins));
|
|
}
|
|
|
|
public CompletableFuture<Integer> getBoardID() {
|
|
CompletableFuture<Integer> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("boardID").whenComplete((entries, throwable) -> future.complete(entries.getInteger("boardID")));
|
|
return future;
|
|
}
|
|
|
|
public void setBoardID(int id) {
|
|
setInDatabase("boardID", id);
|
|
}
|
|
|
|
public void setInDatabase(String name, Object obj) {
|
|
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
|
|
client.update("players", new JsonObject().put("uuid", uuid.toString()),
|
|
new JsonObject().put("$set", new JsonObject().put(name, obj)), res -> {
|
|
if (res.failed()) {
|
|
try {
|
|
throw res.cause();
|
|
} catch (Throwable throwable) {
|
|
throwable.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public CompletableFuture<JsonObject> getObjectFromDatabase(String name) {
|
|
CompletableFuture<JsonObject> 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;
|
|
}
|
|
} |