469 lines
16 KiB
Java
469 lines
16 KiB
Java
package eu.univento.commons.player;
|
|
|
|
import com.mongodb.CursorType;
|
|
import com.mongodb.client.FindIterable;
|
|
import com.mongodb.client.MongoCollection;
|
|
import eu.univento.commons.Commons;
|
|
import eu.univento.commons.database.MongoDB;
|
|
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.hacks.HackData;
|
|
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.profiles.Profile;
|
|
import eu.univento.commons.player.ranking.PlayerRanking;
|
|
import eu.univento.commons.player.settings.PlayerSettings;
|
|
import eu.univento.commons.player.warn.WarnData;
|
|
import eu.univento.commons.player.warn.WarnReason;
|
|
import org.bson.Document;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 0.1
|
|
*/
|
|
public class DatabasePlayer {
|
|
|
|
private UUID uuid;
|
|
|
|
private final PlayerSettings settings;
|
|
private final PlayerRanking ranking;
|
|
private final HackData hackData;
|
|
|
|
private final MongoCollection<Document> playerCollection;
|
|
private final MongoCollection<Document> friendCollection;
|
|
private final MongoCollection<Document> onlinePlayerCollection;
|
|
private final MongoCollection<Document> banCollection;
|
|
private final MongoCollection<Document> warnCollection;
|
|
private final MongoCollection<Document> kickCollection;
|
|
private final MongoCollection<Document> muteCollection;
|
|
private final MongoCollection<Document> profileCollection;
|
|
|
|
public DatabasePlayer(Commons commons, UUID uuid) {
|
|
this.uuid = uuid;
|
|
settings = new PlayerSettings(this);
|
|
ranking = new PlayerRanking(this);
|
|
hackData = new HackData(this);
|
|
|
|
MongoDB mongoDB = commons.getDatabaseManager().getMongoDB();
|
|
playerCollection = mongoDB.getDatabase().getCollection("players");
|
|
friendCollection = mongoDB.getDatabase().getCollection("friends");
|
|
onlinePlayerCollection = mongoDB.getDatabase().getCollection("online-players");
|
|
banCollection = mongoDB.getDatabase().getCollection("bans");
|
|
warnCollection = mongoDB.getDatabase().getCollection("warns");
|
|
kickCollection = mongoDB.getDatabase().getCollection("kicks");
|
|
muteCollection = mongoDB.getDatabase().getCollection("mutes");
|
|
profileCollection = mongoDB.getDatabase().getCollection("profiles");
|
|
}
|
|
|
|
public UUID getUuid() {
|
|
return uuid;
|
|
}
|
|
|
|
public MongoCollection<Document> getPlayerCollection() {
|
|
return playerCollection;
|
|
}
|
|
|
|
public MongoCollection<Document> getFriendCollection() {
|
|
return friendCollection;
|
|
}
|
|
|
|
public MongoCollection<Document> getOnlinePlayerCollection() {
|
|
return onlinePlayerCollection;
|
|
}
|
|
|
|
public MongoCollection<Document> getBanCollection() {
|
|
return banCollection;
|
|
}
|
|
|
|
public MongoCollection<Document> getWarnCollection() {
|
|
return warnCollection;
|
|
}
|
|
|
|
public MongoCollection<Document> getKickCollection() {
|
|
return kickCollection;
|
|
}
|
|
|
|
public MongoCollection<Document> getMuteCollection() {
|
|
return muteCollection;
|
|
}
|
|
|
|
public MongoCollection<Document> getProfileCollection() {
|
|
return profileCollection;
|
|
}
|
|
|
|
public PlayerRanking getPlayerRanking() {
|
|
return ranking;
|
|
}
|
|
|
|
public HackData getHackData() {
|
|
return hackData;
|
|
}
|
|
|
|
public Collection<Profile> getProfiles() {
|
|
ArrayList<String> list = getArrayListFromDatabase("profiles");
|
|
return list.stream().map(id -> new Profile(this, id)).collect(Collectors.toCollection(LinkedList::new));
|
|
}
|
|
|
|
public void ban(BanReason reason, UUID banner, String customMessage, String proof) {
|
|
Document doc = new Document("uuid", uuid.toString());
|
|
int i = 0;
|
|
if(getAllBans().size() > 1) {
|
|
for (BanData ban : getAllBans()) i =+ ban.getReason().getValue();
|
|
}
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.add(Calendar.DAY_OF_YEAR, reason.getValue() + i);
|
|
doc.put("date", calendar.getTime());
|
|
doc.put("reason", reason.name());
|
|
doc.put("banner", banner.toString());
|
|
doc.put("customMessage", customMessage);
|
|
doc.put("proof", proof);
|
|
|
|
banCollection.insertOne(doc);
|
|
}
|
|
|
|
public BanData getBan() {
|
|
FindIterable<Document> cursor = banCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
if(cursor.first() == null) return null;
|
|
Document doc = cursor.first();
|
|
return new BanData(BanReason.valueOf(doc.getString("reason")), UUID.fromString(doc.getString("banner")), doc.getString("customMessage"), doc.getDate("date"), doc.getString("proof"));
|
|
}
|
|
|
|
public boolean isBanned() {
|
|
return getBan() != null && !new Date().after(getBan().getUnbanDate());
|
|
}
|
|
|
|
public Collection<BanData> getAllBans() {
|
|
FindIterable<Document> cursor = banCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
Iterator<Document> iterator = cursor.iterator();
|
|
Collection<BanData> collection = new LinkedList<>();
|
|
while(iterator.hasNext()) {
|
|
Document doc = iterator.next();
|
|
collection.add(new BanData(BanReason.valueOf(doc.getString("reason")), UUID.fromString(doc.getString("banner")), doc.getString("customMessage"), doc.getDate("date"), doc.getString("proof")));
|
|
}
|
|
return collection;
|
|
}
|
|
|
|
public void warn(WarnReason reason, UUID warner, String proof) {
|
|
Document doc = new Document("uuid", uuid.toString());
|
|
doc.put("reason", reason.name());
|
|
doc.put("warner", warner.toString());
|
|
doc.put("date", new Date());
|
|
doc.put("proof", proof);
|
|
|
|
warnCollection.insertOne(doc);
|
|
}
|
|
|
|
public boolean isWarned() {
|
|
FindIterable<Document> cursor = warnCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
Document doc = cursor.first();
|
|
return doc != null && !new Date().after(doc.getDate("date"));
|
|
}
|
|
|
|
public Collection<WarnData> getAllWarns() {
|
|
FindIterable<Document> cursor = warnCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
Iterator<Document> iterator = cursor.iterator();
|
|
Collection<WarnData> collection = new LinkedList<>();
|
|
while(iterator.hasNext()) {
|
|
Document doc = iterator.next();
|
|
collection.add(new WarnData(WarnReason.valueOf(doc.getString("reason")), UUID.fromString(doc.getString("warner")), doc.getDate("date"), doc.getString("proof")));
|
|
}
|
|
return collection;
|
|
}
|
|
|
|
public void kick(UUID kicker, KickReason reason) {
|
|
Document doc = new Document("uuid", uuid.toString());
|
|
doc.put("reason", reason.name());
|
|
doc.put("kicker", kicker.toString());
|
|
doc.put("date", new Date());
|
|
|
|
kickCollection.insertOne(doc);
|
|
}
|
|
|
|
public Collection<KickData> getAllKicks() {
|
|
FindIterable<Document> cursor = kickCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
Iterator<Document> iterator = cursor.iterator();
|
|
LinkedList<KickData> collection = new LinkedList<>();
|
|
while(iterator.hasNext()) {
|
|
Document doc = iterator.next();
|
|
collection.add(new KickData(KickReason.valueOf(doc.getString("reason")), doc.getDate("date"), UUID.fromString(doc.getString("kicker"))));
|
|
}
|
|
return collection;
|
|
}
|
|
|
|
public void mute(UUID muter, MuteReason reason) {
|
|
int i = 0;
|
|
if(getAllBans().size() > 1) {
|
|
for (MuteData mute : getAllMutes()) i =+ mute.getReason().getValue();
|
|
}
|
|
Document doc = new Document("uuid", uuid.toString());
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.add(Calendar.DAY_OF_YEAR, reason.getValue() + i);
|
|
doc.put("reason", reason.name());
|
|
doc.put("muter", muter.toString());
|
|
doc.put("date", calendar.getTime());
|
|
|
|
muteCollection.insertOne(doc);
|
|
}
|
|
|
|
public boolean isMuted() {
|
|
FindIterable<Document> cursor = muteCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
Document doc = cursor.first();
|
|
return doc != null && !new Date().after(doc.getDate("date"));
|
|
}
|
|
|
|
public MuteData getMute() {
|
|
for(MuteData mute : getAllMutes()) {
|
|
if(new Date().before(mute.getDate())) {
|
|
return mute;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public Collection<MuteData> getAllMutes() {
|
|
FindIterable<Document> cursor = muteCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
Iterator<Document> iterator = cursor.iterator();
|
|
LinkedList<MuteData> collection = new LinkedList<>();
|
|
while(iterator.hasNext()) {
|
|
Document doc = iterator.next();
|
|
collection.add(new MuteData(MuteReason.valueOf(doc.getString("reason")), UUID.fromString(doc.getString("uuid")), UUID.fromString(doc.getString("muter")), doc.getDate("date")));
|
|
}
|
|
return collection;
|
|
}
|
|
|
|
public PlayerSettings getSettings() {
|
|
return settings;
|
|
}
|
|
|
|
public Rank getRank() {
|
|
return Rank.valueOf(getStringFromDatabase("rank"));
|
|
}
|
|
|
|
public void setRank(Rank rank) {
|
|
setInDatabase("rank", rank.name());
|
|
}
|
|
|
|
public boolean isAllowed(Rank rank) {
|
|
return getRank().getValue() >= rank.getValue();
|
|
}
|
|
|
|
public boolean hasPlayedBefore() {
|
|
FindIterable<Document> cursor = playerCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
Document obj = cursor.first();
|
|
return obj != null;
|
|
}
|
|
|
|
public void setTSID(String id) {
|
|
setInDatabase("tsid", id);
|
|
}
|
|
|
|
public void setCoins(int coins) {
|
|
setInDatabase("coins", coins);
|
|
}
|
|
|
|
public void setExperience(int experience) {
|
|
setInDatabase("experience", experience);
|
|
}
|
|
|
|
private void setFoundSecrets(ArrayList<String> secrets) {
|
|
setInDatabase("foundSecrets", secrets);
|
|
}
|
|
|
|
public void addSecret(String name) {
|
|
ArrayList<String> list = getFoundSecrets();
|
|
list.add(name);
|
|
setFoundSecrets(list);
|
|
}
|
|
|
|
private void setFoundEggs(ArrayList<String> eggs) {
|
|
setInDatabase("foundEggs", eggs);
|
|
}
|
|
|
|
public void addEgg(String name) {
|
|
ArrayList<String> list = getFoundEggs();
|
|
list.add(name);
|
|
setFoundEggs(list);
|
|
}
|
|
|
|
public Date getFirstLogin() {
|
|
return getDateFromDatabase("firstLogin");
|
|
}
|
|
|
|
public Date getLastLogin() {
|
|
return getDateFromDatabase("lastLogin");
|
|
}
|
|
|
|
public Date getLastOnline() {
|
|
return getDateFromDatabase("lastOnline");
|
|
}
|
|
|
|
public String getLastIP() {
|
|
return getStringFromDatabase("lastIP");
|
|
}
|
|
|
|
public String getTSID() {
|
|
return getStringFromDatabase("tsid");
|
|
}
|
|
|
|
public int getTimesJoined() {
|
|
return getIntegerFromDatabase("timesJoined");
|
|
}
|
|
|
|
public int getCoins() {
|
|
return getIntegerFromDatabase("coins");
|
|
}
|
|
|
|
public int getExperience() {
|
|
return getIntegerFromDatabase("experience");
|
|
}
|
|
|
|
public int getSecrets() {
|
|
return getFoundSecrets().size();
|
|
}
|
|
|
|
private ArrayList<String> getFoundSecrets() {
|
|
return getArrayListFromDatabase("foundSecrets");
|
|
}
|
|
|
|
public int getEggs() {
|
|
return getFoundEggs().size();
|
|
}
|
|
|
|
private ArrayList<String> getFoundEggs() {
|
|
return getArrayListFromDatabase("foundEggs");
|
|
}
|
|
|
|
private void setFriends(Collection<FriendData> friends) {
|
|
List<UUID> list = friends.stream().map(FriendData::getUuid).collect(Collectors.toCollection(LinkedList::new));
|
|
friendCollection.updateOne(new Document("uuid", uuid.toString()), new Document("$set", new Document("friends", list)));
|
|
}
|
|
|
|
public Collection<FriendData> getFriends() {
|
|
FindIterable<Document> cursor = friendCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
Document doc = cursor.first();
|
|
if (doc == null) return null;
|
|
|
|
LinkedList<UUID> list = (LinkedList<UUID>) doc.get("friends");
|
|
|
|
return list.stream().map(FriendData::new).collect(Collectors.toCollection(LinkedList::new));
|
|
}
|
|
|
|
public boolean isFriend(UUID uuid) {
|
|
for(FriendData friend : getFriends()) {
|
|
if(friend.getUuid() == uuid) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void addFriend(UUID uuid) {
|
|
Collection<FriendData> list = getFriends();
|
|
assert list != null;
|
|
list.add(new FriendData(uuid));
|
|
setFriends(list);
|
|
}
|
|
|
|
public void removeFriend(UUID uuid) {
|
|
Collection<FriendData> list = getFriends();
|
|
assert list != null;
|
|
list.remove(new FriendData(uuid));
|
|
setFriends(list);
|
|
}
|
|
|
|
public void addCoins(int coins) {
|
|
setCoins(getCoins() + coins);
|
|
}
|
|
|
|
public void substractCoins(int coins) {
|
|
setCoins(getCoins() - coins);
|
|
}
|
|
|
|
public String getMessage(String message) {
|
|
return new Language(getSettings().getLanguage()).getWord(message);
|
|
}
|
|
|
|
public boolean isOnline() {
|
|
FindIterable<Document> cursor = onlinePlayerCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
Document doc = cursor.first();
|
|
return doc != null;
|
|
}
|
|
|
|
public int getBoardID() {
|
|
return getIntegerFromDatabase("boardID");
|
|
}
|
|
|
|
public void setBoardID(int id) {
|
|
setInDatabase("boardID", id);
|
|
}
|
|
|
|
//database handling methods
|
|
public void setInDatabase(String name, Object obj) {
|
|
playerCollection.updateOne(new Document("uuid", uuid.toString()), new Document("$set", new Document(name, obj)));
|
|
}
|
|
|
|
public Object getObjectFromDatbase(String name) {
|
|
FindIterable<Document> cursor = playerCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
Document doc = cursor.first();
|
|
if (doc == null) return null;
|
|
|
|
return doc.get(name);
|
|
}
|
|
|
|
public int getIntegerFromDatabase(String name) {
|
|
FindIterable<Document> cursor = playerCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
Document doc = cursor.first();
|
|
if (doc == null) return 0;
|
|
|
|
return doc.getInteger(name);
|
|
}
|
|
|
|
public String getStringFromDatabase(String name) {
|
|
FindIterable<Document> cursor = playerCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
Document doc = cursor.first();
|
|
if (doc == null) return null;
|
|
|
|
return doc.getString(name);
|
|
}
|
|
|
|
public Date getDateFromDatabase(String name) {
|
|
FindIterable<Document> cursor = playerCollection.find(new Document("uuid", uuid.toString()));
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
Document doc = cursor.first();
|
|
if (doc == null) return null;
|
|
|
|
return doc.getDate(name);
|
|
}
|
|
|
|
public ArrayList<String> getArrayListFromDatabase(String name) {
|
|
return (ArrayList<String>) getObjectFromDatbase(name);
|
|
}
|
|
|
|
public Map<String, Object> getMapFromDatabase(String name) {
|
|
return (Map<String, Object>) getObjectFromDatbase(name);
|
|
}
|
|
} |