2016-04-08 13:12:31 +02:00
|
|
|
package net.frozenorb.apiv3.models;
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-05-09 05:18:55 +02:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2016-03-21 23:28:17 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
import net.frozenorb.apiv3.APIv3;
|
|
|
|
import org.mongodb.morphia.annotations.Entity;
|
|
|
|
import org.mongodb.morphia.annotations.Id;
|
2016-05-03 01:34:30 +02:00
|
|
|
import org.mongodb.morphia.annotations.Indexed;
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-05-20 22:19:13 +02:00
|
|
|
import java.util.*;
|
2016-05-09 05:18:55 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2016-04-27 02:46:34 +02:00
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
@Entity(value = "ranks", noClassnameStored = true)
|
|
|
|
public final class Rank {
|
|
|
|
|
2016-05-09 05:18:55 +02:00
|
|
|
private static Map<String, Rank> rankCache = null;
|
2016-05-20 22:19:13 +02:00
|
|
|
private static List<Rank> sortedRankCache = null;
|
2016-05-09 05:18:55 +02:00
|
|
|
private static long rankCacheUpdated = 0;
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter @Id private String id;
|
|
|
|
@Getter private int weight;
|
|
|
|
@Getter private String displayName;
|
|
|
|
@Getter private String gameColor;
|
|
|
|
@Getter private String websiteColor;
|
2016-05-03 01:34:30 +02:00
|
|
|
@Getter @Indexed private boolean staffRank;
|
2016-04-08 13:12:31 +02:00
|
|
|
|
|
|
|
public static Rank byId(String id) {
|
2016-05-09 05:18:55 +02:00
|
|
|
updateCacheIfNeeded();
|
|
|
|
return rankCache.get(id);
|
2016-04-08 13:12:31 +02:00
|
|
|
}
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-04-27 02:46:34 +02:00
|
|
|
public static List<Rank> values() {
|
2016-05-14 05:58:25 +02:00
|
|
|
updateCacheIfNeeded();
|
2016-05-20 22:19:13 +02:00
|
|
|
return ImmutableList.copyOf(sortedRankCache);
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
public Rank() {} // For Morphia
|
|
|
|
|
2016-04-08 13:12:31 +02:00
|
|
|
public Rank(String id, int weight, String displayName, String gameColor, String websiteColor, boolean staffRank) {
|
2016-03-21 23:28:17 +01:00
|
|
|
this.id = id;
|
|
|
|
this.weight = weight;
|
|
|
|
this.displayName = displayName;
|
|
|
|
this.gameColor = gameColor;
|
|
|
|
this.websiteColor = websiteColor;
|
2016-04-08 13:12:31 +02:00
|
|
|
this.staffRank = staffRank;
|
2016-03-21 23:28:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void delete() {
|
|
|
|
APIv3.getDatastore().delete(this);
|
|
|
|
}
|
|
|
|
|
2016-05-09 05:18:55 +02:00
|
|
|
private static void updateCacheIfNeeded() {
|
|
|
|
if (rankCache == null || (System.currentTimeMillis() - rankCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
|
|
|
Map<String, Rank> working = new HashMap<>();
|
2016-05-20 22:19:13 +02:00
|
|
|
List<Rank> workingSorted = new ArrayList<>();
|
2016-05-09 05:18:55 +02:00
|
|
|
|
|
|
|
for (Rank rank : APIv3.getDatastore().createQuery(Rank.class).order("weight").asList()) {
|
|
|
|
working.put(rank.getId(), rank);
|
2016-05-20 22:19:13 +02:00
|
|
|
workingSorted.add(rank);
|
2016-05-09 05:18:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rankCache = working;
|
2016-05-20 22:19:13 +02:00
|
|
|
sortedRankCache = workingSorted;
|
2016-05-09 05:18:55 +02:00
|
|
|
rankCacheUpdated = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
}
|