509 lines
20 KiB
Java
509 lines
20 KiB
Java
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;
|
|
|
|
public DatabasePlayer(UUID uuid, String name) {
|
|
this.uuid = uuid;
|
|
this.name = name;
|
|
}
|
|
|
|
private PlayerSettings settings;
|
|
public PlayerSettings getSettings() {
|
|
if(settings == null) {
|
|
getSettingsFromDatabase().whenComplete((playerSettings, throwable) -> settings = playerSettings);
|
|
}
|
|
return settings;
|
|
}
|
|
|
|
public Language getLanguage() {
|
|
return Language.getLanguage(Locale.forLanguageTag(getSettings().getLanguage()));
|
|
}
|
|
|
|
public CompletableFuture<PlayerSettings> getSettingsFromDatabase() {
|
|
CompletableFuture<PlayerSettings> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("Settings").whenComplete((entries, throwable) -> {
|
|
future.complete(new PlayerSettings(this, entries));
|
|
});
|
|
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()) {
|
|
try {
|
|
throw res.cause();
|
|
} catch (Throwable throwable1) {
|
|
throwable1.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
client.close();
|
|
});
|
|
|
|
}
|
|
|
|
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 -> {
|
|
JsonObject result = res.result();
|
|
BanData data = new BanData(BanReason.valueOf(result.getString("reason")),
|
|
UUID.fromString(result.getString("banner")),
|
|
result.getString("customMessage"),
|
|
result.getInstant("date"),
|
|
result.getString("proof"));
|
|
client.close();
|
|
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")));
|
|
}
|
|
client.close();
|
|
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")));
|
|
}
|
|
client.close();
|
|
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")));
|
|
}
|
|
client.close();
|
|
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();
|
|
}
|
|
}
|
|
});
|
|
client.close();
|
|
});
|
|
}
|
|
|
|
public CompletableFuture<Boolean> isMuted() {
|
|
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")));
|
|
}
|
|
client.close();
|
|
future.complete(data);
|
|
}
|
|
});
|
|
return future;
|
|
}
|
|
|
|
public CompletableFuture<Rank> getRankFromDatabase() {
|
|
CompletableFuture<Rank> future = new CompletableFuture<>();
|
|
getObjectFromDatabase("rank").whenComplete((entries, throwable) ->
|
|
future.complete(Rank.valueOf(entries.getString("rank"))));
|
|
return future;
|
|
}
|
|
|
|
private Rank rank;
|
|
|
|
public Rank getRank() {
|
|
if (rank == null) {
|
|
getRankFromDatabase().whenComplete((rank1, throwable) -> rank = rank1);
|
|
}
|
|
return rank;
|
|
}
|
|
|
|
public void setRank(Rank rank) {
|
|
setInDatabase("rank", rank.name());
|
|
}
|
|
|
|
public boolean isAllowed(Rank rank) {
|
|
return getRank().getValue() >= rank.getValue();
|
|
}
|
|
|
|
public boolean isAllowed(Group group) {
|
|
return getRank().getGroup() == group;
|
|
}
|
|
|
|
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()), new JsonObject("uuid"), res -> {
|
|
client.close();
|
|
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))));
|
|
}
|
|
client.close();
|
|
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.findOneAndUpdate("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();
|
|
}
|
|
}
|
|
client.close();
|
|
});
|
|
}
|
|
|
|
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()), new JsonObject(name), res -> {
|
|
client.close();
|
|
future.complete(res.result());
|
|
});
|
|
return future;
|
|
}
|
|
} |