APIv3/src/main/java/net/frozenorb/apiv3/collections/Server.java

85 lines
2.8 KiB
Java
Raw Normal View History

2016-02-08 02:51:02 +01:00
package net.frozenorb.apiv3.collections;
2016-02-06 02:58:49 +01:00
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import lombok.Getter;
2016-02-08 00:03:42 +01:00
import lombok.ToString;
2016-02-06 02:58:49 +01:00
import net.frozenorb.apiv3.APIv3;
2016-02-08 00:03:42 +01:00
import net.frozenorb.apiv3.utils.MongoUtils;
2016-02-06 02:58:49 +01:00
2016-02-08 00:03:42 +01:00
import java.time.Instant;
import java.util.*;
2016-02-06 02:58:49 +01:00
2016-02-08 00:03:42 +01:00
@ToString(exclude={ "secret" })
public final class Server {
public static final String COLLECTION_NAME = "server";
2016-02-06 02:58:49 +01:00
@Getter private String id;
2016-02-08 00:03:42 +01:00
@Getter private String bungeeId;
@Getter private String displayName;
@Getter private String secret;
@Getter private String group;
@Getter private String internalIp;
@Getter private Instant lastUpdate;
@Getter private double lastTps;
@Getter private List<UUID> players;
2016-02-08 00:09:54 +01:00
public static void findAllServers(Handler<AsyncResult<List<Server>>> callback) {
MongoUtils.findManyAndTransform(COLLECTION_NAME, new JsonObject(), Server::new, callback);
}
2016-02-08 00:03:42 +01:00
public static void findServerById(String id, Handler<AsyncResult<Server>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new JsonObject().put("_id", id), Server::new, callback);
}
public static void findServersByGroup(String groupId, Handler<AsyncResult<List<Server>>> callback) {
MongoUtils.findManyAndTransform(COLLECTION_NAME, new JsonObject().put("group", groupId), Server::new, callback);
}
private Server(JsonObject json) {
this.id = json.getString("_id");
this.bungeeId = json.getString("bungeeId");
this.displayName = json.getString("displayName");
this.secret = json.getString("secret");
this.group = json.getString("group");
this.internalIp = json.getString("internalIp");
this.lastUpdate = json.getInstant("lastUpdate");
this.lastTps = json.getDouble("lastTps");
this.players = new ArrayList<>();
for (Object uuidString : json.getJsonArray("players")) {
players.add(UUID.fromString((String) uuidString));
}
}
public void update() {
this.lastUpdate = Instant.now();
APIv3.getMongoClient().update(COLLECTION_NAME, new JsonObject().put("_id", id), MongoUtils.set(new JsonObject().put("lastUpdate", lastUpdate)), res -> {});
}
public JsonObject toLiteJson() {
JsonObject json = new JsonObject();
json.put("_id", id);
json.put("bungeeId", bungeeId);
json.put("displayName", displayName);
json.put("group", group);
json.put("internalIp", internalIp);
return json;
}
public JsonObject toFullJson() {
JsonObject json = toLiteJson();
json.put("lastUpdate", lastUpdate);
json.put("lastTps", lastTps);
json.put("players", new JsonArray(players));
2016-02-06 02:58:49 +01:00
2016-02-08 00:03:42 +01:00
return json;
2016-02-06 02:58:49 +01:00
}
}