101 lines
3.3 KiB
Java
101 lines
3.3 KiB
Java
package net.frozenorb.apiv3.model;
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
import com.mongodb.async.client.MongoCollection;
|
|
import com.mongodb.client.result.DeleteResult;
|
|
import com.mongodb.client.result.UpdateResult;
|
|
import fr.javatic.mongo.jacksonCodec.Entity;
|
|
import fr.javatic.mongo.jacksonCodec.objectId.Id;
|
|
import lombok.Getter;
|
|
import net.frozenorb.apiv3.APIv3;
|
|
import net.frozenorb.apiv3.serialization.gson.ExcludeFromReplies;
|
|
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
|
import net.frozenorb.apiv3.util.SyncUtils;
|
|
import org.bson.Document;
|
|
|
|
import java.time.Instant;
|
|
import java.util.*;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@Entity
|
|
public final class Server {
|
|
|
|
private static final MongoCollection<Server> serversCollection = APIv3.getDatabase().getCollection("servers", Server.class);
|
|
|
|
private static Map<String, Server> serverCache = null;
|
|
private static List<Server> serverCacheAlt = null;
|
|
private static long serverCacheUpdated = 0;
|
|
|
|
@Getter @Id private String id;
|
|
@Getter private String displayName;
|
|
@Getter @ExcludeFromReplies String apiKey;
|
|
@Getter private String serverGroup;
|
|
@Getter private String serverIp;
|
|
@Getter private Instant lastUpdatedAt;
|
|
@Getter private double lastTps;
|
|
@Getter @ExcludeFromReplies private Set<UUID> players;
|
|
|
|
public static List<Server> findAll() {
|
|
updateCacheIfNeeded();
|
|
return serverCacheAlt;
|
|
}
|
|
|
|
public static Server findById(String id) {
|
|
updateCacheIfNeeded();
|
|
return serverCache.get(id);
|
|
}
|
|
|
|
public Server() {} // For Jackson
|
|
|
|
public Server(String id, String displayName, String apiKey, ServerGroup serverGroup, String serverIp) {
|
|
this.id = id;
|
|
this.displayName = displayName;
|
|
this.apiKey = apiKey;
|
|
this.serverGroup = serverGroup.getId();
|
|
this.serverIp = serverIp;
|
|
this.lastUpdatedAt = Instant.now();
|
|
this.lastTps = 0;
|
|
this.players = new HashSet<>();
|
|
}
|
|
|
|
private static void updateCacheIfNeeded() {
|
|
if (serverCache == null || (System.currentTimeMillis() - serverCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
|
Map<String, Server> working = new HashMap<>();
|
|
List<Server> workingAlt = new ArrayList<>();
|
|
|
|
for (Server server : SyncUtils.blockMulti(serversCollection.find())) {
|
|
working.put(server.getId(), server);
|
|
workingAlt.add(server);
|
|
}
|
|
|
|
serverCache = working;
|
|
serverCacheAlt = workingAlt;
|
|
serverCacheUpdated = System.currentTimeMillis();
|
|
}
|
|
}
|
|
|
|
public void receivedHeartbeat(double tps, Iterable<UUID> players) {
|
|
this.lastUpdatedAt = Instant.now();
|
|
this.lastTps = tps;
|
|
this.players = ImmutableSet.copyOf(players);
|
|
}
|
|
|
|
public void insert() {
|
|
BlockingCallback<Void> callback = new BlockingCallback<>();
|
|
serversCollection.insertOne(this, callback);
|
|
callback.get();
|
|
}
|
|
|
|
public void save() {
|
|
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
|
|
serversCollection.replaceOne(new Document("_id", id), this, callback);
|
|
callback.get();
|
|
}
|
|
|
|
public void delete() {
|
|
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
|
|
serversCollection.deleteOne(new Document("_id", id), callback);
|
|
callback.get();
|
|
}
|
|
|
|
} |