2016-06-16 16:29:33 +02:00
|
|
|
package net.frozenorb.apiv3.model;
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-05-09 05:18:55 +02:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2016-06-10 04:39:23 +02:00
|
|
|
import com.google.common.primitives.Ints;
|
|
|
|
import com.mongodb.async.client.MongoCollection;
|
|
|
|
import com.mongodb.client.result.DeleteResult;
|
2016-06-16 06:08:44 +02:00
|
|
|
import fr.javatic.mongo.jacksonCodec.Entity;
|
2016-06-12 22:36:11 +02:00
|
|
|
import fr.javatic.mongo.jacksonCodec.objectId.Id;
|
2016-03-21 23:28:17 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
import net.frozenorb.apiv3.APIv3;
|
2016-06-10 04:39:23 +02:00
|
|
|
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
2016-06-16 16:29:33 +02:00
|
|
|
import net.frozenorb.apiv3.util.SyncUtils;
|
2016-06-10 04:39:23 +02:00
|
|
|
import org.bson.Document;
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-06-01 20:23:37 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2016-05-09 05:18:55 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2016-04-27 02:46:34 +02:00
|
|
|
|
2016-06-16 06:08:44 +02:00
|
|
|
@Entity
|
2016-03-21 23:28:17 +01:00
|
|
|
public final class Rank {
|
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
private static final MongoCollection<Rank> ranksCollection = APIv3.getDatabase().getCollection("ranks", Rank.class);
|
|
|
|
|
2016-05-09 05:18:55 +02:00
|
|
|
private static Map<String, Rank> rankCache = null;
|
2016-05-28 06:29:03 +02:00
|
|
|
private static List<Rank> rankAltCache = 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-06-10 04:39:23 +02:00
|
|
|
@Getter private boolean staffRank;
|
2016-04-08 13:12:31 +02:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public static List<Rank> findAll() {
|
2016-05-09 05:18:55 +02:00
|
|
|
updateCacheIfNeeded();
|
2016-06-10 04:39:23 +02:00
|
|
|
return ImmutableList.copyOf(rankAltCache);
|
2016-04-08 13:12:31 +02:00
|
|
|
}
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public static Rank findById(String id) {
|
2016-05-14 05:58:25 +02:00
|
|
|
updateCacheIfNeeded();
|
2016-06-10 04:39:23 +02:00
|
|
|
return rankCache.get(id);
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 18:40:47 +02:00
|
|
|
public Rank() {} // For Jackson
|
2016-03-21 23:28:17 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-28 06:29:03 +02:00
|
|
|
List<Rank> workingAlt = new ArrayList<>();
|
2016-06-10 04:39:23 +02:00
|
|
|
List<Rank> allRanks = SyncUtils.blockMulti(ranksCollection.find());
|
|
|
|
allRanks.sort((a, b) -> Ints.compare(a.getWeight(), b.getWeight()));
|
2016-05-09 05:18:55 +02:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
for (Rank rank : allRanks) {
|
2016-05-09 05:18:55 +02:00
|
|
|
working.put(rank.getId(), rank);
|
2016-05-28 06:29:03 +02:00
|
|
|
workingAlt.add(rank);
|
2016-05-09 05:18:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rankCache = working;
|
2016-05-28 06:29:03 +02:00
|
|
|
rankAltCache = workingAlt;
|
2016-05-09 05:18:55 +02:00
|
|
|
rankCacheUpdated = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public void insert() {
|
|
|
|
BlockingCallback<Void> callback = new BlockingCallback<>();
|
|
|
|
ranksCollection.insertOne(this, callback);
|
|
|
|
callback.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete() {
|
|
|
|
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
|
|
|
|
ranksCollection.deleteOne(new Document("_id", id), callback);
|
|
|
|
callback.get();
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
}
|