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 lombok.Data; import org.bson.Document; import java.util.*; import java.util.stream.Collectors; /** * @author joethei * @version 0.1 */ @Data public class DatabasePlayer { private UUID uuid; private final PlayerSettings settings; private final PlayerRanking ranking; private final HackData hackData; private final Language language; private final MongoCollection playerCollection; private final MongoCollection friendCollection; private final MongoCollection onlinePlayerCollection; private final MongoCollection banCollection; private final MongoCollection warnCollection; private final MongoCollection kickCollection; private final MongoCollection muteCollection; private final MongoCollection profileCollection; public DatabasePlayer(Commons commons, UUID uuid) { this.uuid = uuid; settings = new PlayerSettings(this); ranking = new PlayerRanking(this); hackData = new HackData(this); language = new Language(getSettings().getLanguage()); 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 Collection getProfiles() { ArrayList 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 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 getAllBans() { FindIterable cursor = banCollection.find(new Document("uuid", uuid.toString())); cursor.cursorType(CursorType.NonTailable); Iterator iterator = cursor.iterator(); Collection 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 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 getAllWarns() { FindIterable cursor = warnCollection.find(new Document("uuid", uuid.toString())); cursor.cursorType(CursorType.NonTailable); Iterator iterator = cursor.iterator(); Collection 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 getAllKicks() { FindIterable cursor = kickCollection.find(new Document("uuid", uuid.toString())); cursor.cursorType(CursorType.NonTailable); Iterator iterator = cursor.iterator(); LinkedList 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 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 getAllMutes() { FindIterable cursor = muteCollection.find(new Document("uuid", uuid.toString())); cursor.cursorType(CursorType.NonTailable); Iterator iterator = cursor.iterator(); LinkedList 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 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 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 secrets) { setInDatabase("foundSecrets", secrets); } public void addSecret(String name) { ArrayList list = getFoundSecrets(); list.add(name); setFoundSecrets(list); } private void setFoundEggs(ArrayList eggs) { setInDatabase("foundEggs", eggs); } public void addEgg(String name) { ArrayList 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 getFoundSecrets() { return getArrayListFromDatabase("foundSecrets"); } public int getEggs() { return getFoundEggs().size(); } private ArrayList getFoundEggs() { return getArrayListFromDatabase("foundEggs"); } private void setFriends(Collection friends) { List 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 getFriends() { FindIterable cursor = friendCollection.find(new Document("uuid", uuid.toString())); cursor.cursorType(CursorType.NonTailable); Document doc = cursor.first(); if (doc == null) return null; LinkedList list = (LinkedList) 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 list = getFriends(); assert list != null; list.add(new FriendData(uuid)); setFriends(list); } public void removeFriend(UUID uuid) { Collection 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 boolean isOnline() { FindIterable 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 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 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 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 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 getArrayListFromDatabase(String name) { return (ArrayList) getObjectFromDatbase(name); } public Map getMapFromDatabase(String name) { return (Map) getObjectFromDatbase(name); } }