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-24 09:00:37 +02:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import com.mongodb.async.SingleResultCallback;
|
2016-06-10 04:39:23 +02:00
|
|
|
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 org.bson.Document;
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-06-01 20:23:37 +02:00
|
|
|
import java.util.HashMap;
|
2016-06-23 06:59:18 +02:00
|
|
|
import java.util.LinkedList;
|
2016-06-01 20:23:37 +02:00
|
|
|
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-06-24 09:00:37 +02:00
|
|
|
private static Map<String, Rank> rankIdCache = null;
|
|
|
|
private static List<Rank> rankCache = null;
|
2016-05-09 05:18:55 +02:00
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter @Id private String id;
|
2016-06-26 00:20:32 +02:00
|
|
|
@Getter private String inheritsFromId;
|
2016-03-21 23:28:17 +01:00
|
|
|
@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-06-27 01:19:34 +02:00
|
|
|
return ImmutableList.copyOf(rankCache);
|
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-06-24 09:00:37 +02:00
|
|
|
return rankIdCache.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static {
|
2016-06-26 00:41:44 +02:00
|
|
|
APIv3.getVertxInstance().setPeriodic(TimeUnit.MINUTES.toMillis(1), (id) -> updateCache());
|
2016-06-24 09:00:37 +02:00
|
|
|
}
|
|
|
|
|
2016-06-26 21:09:03 +02:00
|
|
|
public static void updateCache() {
|
2016-06-24 09:00:37 +02:00
|
|
|
ranksCollection.find().into(new LinkedList<>(), (ranks, error) -> {
|
|
|
|
if (error != null) {
|
|
|
|
error.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, Rank> working = new HashMap<>();
|
|
|
|
|
|
|
|
for (Rank rank : ranks) {
|
|
|
|
working.put(rank.getId(), rank);
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:19:34 +02:00
|
|
|
rankIdCache = working;
|
|
|
|
rankCache = ranks;
|
2016-06-24 09:00:37 +02:00
|
|
|
});
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 01:00:40 +02:00
|
|
|
private Rank() {} // For Jackson
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-06-26 00:20:32 +02:00
|
|
|
public Rank(String id, String inheritsFromId, int weight, String displayName, String gameColor, String websiteColor, boolean staffRank) {
|
2016-03-21 23:28:17 +01:00
|
|
|
this.id = id;
|
2016-06-26 00:20:32 +02:00
|
|
|
this.inheritsFromId = inheritsFromId;
|
2016-03-21 23:28:17 +01:00
|
|
|
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-06-24 09:00:37 +02:00
|
|
|
public void insert(SingleResultCallback<Void> callback) {
|
2016-06-25 05:54:34 +02:00
|
|
|
rankCache.add(this);
|
|
|
|
rankIdCache.put(id, this);
|
2016-06-10 04:39:23 +02:00
|
|
|
ranksCollection.insertOne(this, callback);
|
|
|
|
}
|
|
|
|
|
2016-06-24 09:00:37 +02:00
|
|
|
public void delete(SingleResultCallback<DeleteResult> callback) {
|
2016-06-25 05:54:34 +02:00
|
|
|
rankCache.remove(this);
|
|
|
|
rankIdCache.remove(id);
|
2016-06-10 04:39:23 +02:00
|
|
|
ranksCollection.deleteOne(new Document("_id", id), callback);
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
}
|