Core/src/main/java/eu/univento/core/api/cloud/CloudApiWrapper.java

58 lines
2.0 KiB
Java

/*
* Copyright (c) 2018 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.core.api.cloud;
import de.dytanic.cloudnet.api.CloudAPI;
import de.dytanic.cloudnet.lib.server.ServerState;
import de.dytanic.cloudnet.lib.server.info.ServerInfo;
import eu.univento.commons.server.ServerType;
import eu.univento.core.Core;
import eu.univento.core.api.player.CustomPlayer;
import lombok.Getter;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
/**
* @author joethei
* @version 1.0
*/
public class CloudApiWrapper {
@Getter private CloudAPI cloudAPI;
public CloudApiWrapper() {
cloudAPI = CloudAPI.getInstance();
}
public CompletableFuture<String> getOptimalServer(CustomPlayer player, ServerType type) {
CompletableFuture<String> future = new CompletableFuture<>();
Collection<ServerInfo> servers = cloudAPI.getServers(type.getName());
Core.getCommons().getPartyManager().isInParty(player.getDatabasePlayer()).whenComplete(((bool, throwable) -> servers.forEach(serverInfo -> {
if (serverInfo.isOnline() && serverInfo.getServerState() == ServerState.LOBBY) {
if (bool) {
Core.getCommons().getPartyManager().getParty(player.getDatabasePlayer()).whenComplete((party, throwable1) -> {
if (serverInfo.getOnlineCount() < serverInfo.getMaxPlayers() - party.getMembers().size()) {
future.complete(serverInfo.getServiceId().getServerId());
}
});
} else {
if (serverInfo.getOnlineCount() < serverInfo.getMaxPlayers() - 1) {
future.complete(serverInfo.getServiceId().getServerId());
}
}
}
})));
return future;
}
public String getServerId() {
return cloudAPI.getServerId();
}
}