2021-11-21 14:36:32 +00:00
|
|
|
package rip.tilly.bedwars.player;
|
|
|
|
|
|
|
|
import com.mongodb.client.MongoCursor;
|
|
|
|
import com.mongodb.client.model.Filters;
|
|
|
|
import com.mongodb.client.model.UpdateOptions;
|
|
|
|
import lombok.Getter;
|
|
|
|
import org.bson.Document;
|
|
|
|
import rip.tilly.bedwars.BedWars;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Lucanius
|
|
|
|
* Project: BedWars
|
|
|
|
*/
|
|
|
|
public class PlayerDataManager {
|
|
|
|
|
|
|
|
@Getter private final Map<UUID, PlayerData> players = new HashMap<>();
|
|
|
|
|
|
|
|
private final BedWars plugin = BedWars.getInstance();
|
|
|
|
|
|
|
|
public PlayerData getOrCreate(UUID uniqueId) {
|
|
|
|
return this.players.computeIfAbsent(uniqueId, PlayerData::new);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerData getPlayerData(UUID uniqueId) {
|
|
|
|
return this.players.getOrDefault(uniqueId, new PlayerData(uniqueId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<PlayerData> getAllPlayers() {
|
|
|
|
return this.players.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadPlayerData(PlayerData playerData) {
|
|
|
|
Document document = this.plugin.getMongoManager().getPlayers().find(Filters.eq("uniqueId", playerData.getUniqueId().toString())).first();
|
|
|
|
|
|
|
|
if (document != null) {
|
|
|
|
playerData.setKills(document.getInteger("kills"));
|
|
|
|
playerData.setDeaths(document.getInteger("deaths"));
|
2021-11-21 15:12:16 +00:00
|
|
|
playerData.setXp(document.getDouble("xp"));
|
2021-11-21 14:36:32 +00:00
|
|
|
playerData.setLevel(document.getInteger("level"));
|
|
|
|
playerData.setWins(document.getInteger("wins"));
|
|
|
|
playerData.setLosses(document.getInteger("losses"));
|
|
|
|
playerData.setGamesPlayed(document.getInteger("gamesPlayed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
playerData.setLoaded(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void savePlayerData(PlayerData playerData) {
|
|
|
|
Document document = new Document();
|
|
|
|
|
|
|
|
document.put("uniqueId", playerData.getUniqueId().toString());
|
|
|
|
|
|
|
|
document.put("kills", playerData.getKills());
|
|
|
|
document.put("deaths", playerData.getDeaths());
|
|
|
|
document.put("xp", playerData.getXp());
|
|
|
|
document.put("level", playerData.getLevel());
|
|
|
|
document.put("wins", playerData.getWins());
|
|
|
|
document.put("losses", playerData.getLosses());
|
|
|
|
document.put("gamesPlayed", playerData.getGamesPlayed());
|
|
|
|
|
|
|
|
this.plugin.getMongoManager().getPlayers().replaceOne(Filters.eq("uniqueId", playerData.getUniqueId().toString()), document, new UpdateOptions().upsert(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deletePlayer(UUID uniqueId) {
|
|
|
|
this.savePlayerData(getPlayerData(uniqueId));
|
|
|
|
this.getPlayers().remove(uniqueId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MongoCursor<Document> getPlayersSorted(String stat, int limit) {
|
|
|
|
final Document document = new Document();
|
|
|
|
document.put(stat, -1);
|
|
|
|
|
|
|
|
return this.plugin.getMongoManager().getPlayers().find().sort(document).limit(limit).iterator();
|
|
|
|
}
|
|
|
|
}
|