Level/src/main/java/world/bentobox/level/objects/TopTenData.java

108 lines
2.9 KiB
Java
Raw Normal View History

package world.bentobox.level.objects;
2018-04-03 03:20:26 +02:00
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.Nullable;
2018-03-11 20:06:31 +01:00
import com.google.gson.annotations.Expose;
2018-08-01 18:49:43 +02:00
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
/**
* This class stores and sorts the top ten.
* @author tastybento
*
*/
@Table(name = "TopTenData")
2018-01-07 20:25:24 +01:00
public class TopTenData implements DataObject {
2018-08-26 06:15:45 +02:00
2018-05-26 04:59:44 +02:00
// UniqueId is the world name
2018-03-11 20:06:31 +01:00
@Expose
2018-05-26 04:59:44 +02:00
private String uniqueId = "";
2018-03-11 20:06:31 +01:00
@Expose
2018-04-03 03:20:26 +02:00
private Map<UUID, Long> topTen = new LinkedHashMap<>();
2018-03-11 20:06:31 +01:00
public Map<UUID, Long> getTopTen() {
2019-07-10 17:08:26 +02:00
// Remove any entries that have level values less than 1
//topTen.values().removeIf(l -> l < 1);
// make copy
Map<UUID, Long> snapTopTen = Collections.unmodifiableMap(topTen);
return snapTopTen.entrySet().stream()
.filter(l -> l.getValue() > 0)
2018-04-03 03:20:26 +02:00
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(10)
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
/**
* Get the level for the rank
* @param rank - rank
* @return level value or 0 if none.
*/
public long getTopTenLevel(int rank) {
Map<UUID, Long> tt = getTopTen();
2020-04-20 00:49:16 +02:00
return rank <= tt.size() ? (long)tt.values().toArray()[(rank-1)] : 0;
}
/**
* Get the UUID of the rank
* @param rank - rank
* @return UUID or null
*/
@Nullable
public UUID getTopTenUUID(int rank) {
Map<UUID, Long> tt = getTopTen();
2020-04-20 00:49:16 +02:00
return rank <= tt.size() ? (UUID)tt.keySet().toArray()[(rank-1)] : null;
}
2018-04-03 03:20:26 +02:00
public void setTopTen(Map<UUID, Long> topTen) {
this.topTen = topTen;
}
@Override
public String getUniqueId() {
// This is the world name
return uniqueId;
}
@Override
public void setUniqueId(String uniqueId) {
// This is the world name - make it always lowercase
this.uniqueId = uniqueId.toLowerCase();
}
/**
* Add level for this island owner or team leader, sort the top ten and limit to ten entries
* @param uuid - UUID of owner or team leader
* @param level - island level
*/
public void addLevel(UUID uuid, Long level) {
this.topTen.put(uuid, level);
}
2018-08-26 06:15:45 +02:00
/**
* Get the level for this UUID, or zero if the UUID is not found
2018-08-26 17:21:41 +02:00
* @param uuid - UUID to check
* @return island level
*/
public long getLevel(UUID uuid) {
if (topTen.containsKey(uuid))
return topTen.get(uuid);
return 0L;
}
/**
* Removes ownerUUID from the top ten
2018-08-26 17:21:41 +02:00
* @param ownerUUID - UUID to remove
*/
public void remove(UUID ownerUUID) {
2018-08-26 06:15:45 +02:00
this.topTen.remove(ownerUUID);
}
2018-08-26 06:15:45 +02:00
}