package net.frozenorb.apiv3.collections; 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; import lombok.ToString; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.utils.MongoUtils; import java.time.Instant; import java.util.*; @ToString(exclude={ "secret" }) public final class Server { public static final String COLLECTION_NAME = "server"; @Getter private String id; @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 players; public static void findAllServers(Handler>> callback) { MongoUtils.findManyAndTransform(COLLECTION_NAME, new JsonObject(), Server::new, callback); } public static void findServerById(String id, Handler> callback) { MongoUtils.findOneAndTransform(COLLECTION_NAME, new JsonObject().put("_id", id), Server::new, callback); } public static void findServersByGroup(String groupId, Handler>> 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)); return json; } }