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

85 lines
2.9 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 lombok.Getter;
2016-02-08 00:03:42 +01:00
import lombok.ToString;
2016-02-12 01:10:46 +01:00
import net.frozenorb.apiv3.APIv3;
2016-02-08 02:54:27 +01:00
import net.frozenorb.apiv3.LiteFullJson;
2016-02-08 00:03:42 +01:00
import net.frozenorb.apiv3.utils.MongoUtils;
2016-02-12 01:10:46 +01:00
import org.bson.Document;
2016-02-06 02:58:49 +01:00
2016-02-08 00:03:42 +01:00
import java.time.Instant;
2016-02-12 01:10:46 +01:00
import java.util.*;
import java.util.stream.Collectors;
2016-02-06 02:58:49 +01:00
2016-02-08 00:03:42 +01:00
@ToString(exclude={ "secret" })
2016-02-08 02:54:27 +01:00
public final class Server implements LiteFullJson {
2016-02-08 00:03:42 +01:00
2016-02-12 01:10:46 +01:00
private 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-12 01:10:46 +01:00
public static void findAll(Handler<AsyncResult<Collection<Server>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Server::new, callback);
2016-02-08 00:09:54 +01:00
}
2016-02-08 02:55:20 +01:00
public static void findById(String id, Handler<AsyncResult<Server>> callback) {
2016-02-12 01:10:46 +01:00
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Server::new, callback);
2016-02-08 00:03:42 +01:00
}
2016-02-12 01:10:46 +01:00
public static void findByGroup(String groupId, Handler<AsyncResult<Collection<Server>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("group", groupId), Server::new, callback);
2016-02-08 00:03:42 +01:00
}
2016-02-12 01:10:46 +01:00
private Server(Document json) {
2016-02-08 00:03:42 +01:00
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");
2016-02-12 01:10:46 +01:00
this.lastUpdate = json.get("lastUpdate", Instant.class);
this.lastTps = json.get("lastTps", Number.class).doubleValue();
2016-02-08 00:03:42 +01:00
this.players = new ArrayList<>();
2016-02-12 01:10:46 +01:00
for (Object uuidString : json.get("players", Collection.class)) {
2016-02-08 00:03:42 +01:00
players.add(UUID.fromString((String) uuidString));
}
}
2016-02-12 01:10:46 +01:00
public void update() {
APIv3.getMongo().getCollection(COLLECTION_NAME).updateOne(new Document("_id", id), new Document("$set", new Document("lastUpdate", new Date())), (result, error) -> {});
}
public Document toLiteJson() {
Document json = new Document();
2016-02-08 00:03:42 +01:00
json.put("_id", id);
json.put("bungeeId", bungeeId);
json.put("displayName", displayName);
json.put("group", group);
json.put("internalIp", internalIp);
return json;
}
2016-02-12 01:10:46 +01:00
public Document toFullJson() {
Document json = toLiteJson();
2016-02-08 00:03:42 +01:00
2016-02-12 01:10:46 +01:00
json.put("lastUpdate", lastUpdate.toString());
2016-02-08 00:03:42 +01:00
json.put("lastTps", lastTps);
2016-02-12 01:10:46 +01:00
json.put("players", players.stream().map(UUID::toString).collect(Collectors.toList()));
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
}
}