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

70 lines
1.9 KiB
Java
Raw Normal View History

2016-02-12 02:40:06 +01:00
package net.frozenorb.apiv3.model;
2016-02-06 02:58:49 +01:00
import lombok.Getter;
2016-02-08 00:03:42 +01:00
import lombok.ToString;
2016-02-12 02:40:06 +01:00
import net.frozenorb.apiv3.accessor.Servers;
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 06:31:57 +01:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
2016-02-12 01:10:46 +01:00
import java.util.stream.Collectors;
2016-02-06 02:58:49 +01:00
2016-02-12 02:40:06 +01:00
@ToString
2016-02-12 06:31:57 +01:00
public final class Server extends BaseModel {
2016-02-08 00:03:42 +01:00
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;
2016-02-23 13:14:42 +01:00
@Getter private String ip;
2016-02-08 00:03:42 +01:00
@Getter private Instant lastUpdate;
@Getter private double lastTps;
@Getter private List<UUID> players;
2016-02-12 02:40:06 +01:00
public Server(Document json) {
2016-02-12 06:31:57 +01:00
super(Servers.COLLECTION_NAME);
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");
2016-02-23 13:14:42 +01:00
this.ip = json.getString("ip");
2016-02-12 02:40:06 +01:00
this.lastUpdate = (Instant) json.get("lastUpdate");
this.lastTps = ((Number) json.get("lastTps")).doubleValue();
2016-02-08 00:03:42 +01:00
this.players = new ArrayList<>();
2016-02-12 02:40:06 +01:00
for (Object uuidString : (Collection) json.get("players")) {
2016-02-08 00:03:42 +01:00
players.add(UUID.fromString((String) uuidString));
}
2016-02-12 06:31:57 +01:00
setId(id);
2016-02-08 00:03:42 +01:00
}
2016-02-12 01:10:46 +01:00
public Document toLiteJson() {
Document json = new Document();
2016-02-08 00:03:42 +01:00
2016-02-12 02:40:06 +01:00
json.put("id", id);
2016-02-08 00:03:42 +01:00
json.put("bungeeId", bungeeId);
json.put("displayName", displayName);
json.put("group", group);
2016-02-23 13:14:42 +01:00
json.put("ip", ip);
2016-02-08 00:03:42 +01:00
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
}
}