/* * Copyright (c) 2018 univento.eu - All rights reserved * You are not allowed to use, distribute or modify this code */ package eu.univento.commons.player.party; import eu.univento.commons.Commons; import eu.univento.commons.player.DatabasePlayer; import io.vertx.core.json.JsonObject; import java.util.LinkedList; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; /** * @author joethei * @version 1.0 */ public class PartyManager { public PartyManager() {} public CompletableFuture getParty(UUID id) { CompletableFuture future = new CompletableFuture<>(); List members = new LinkedList<>(); List invitations = new LinkedList<>(); Commons.getCommons().getDatabaseManager().getMongoDB().getClient().findOne("partys", new JsonObject().put("id", id), null, res -> { if(res.failed()) future.complete(null); res.result().getJsonArray("members").forEach(obj -> members.add(new DatabasePlayer(UUID.fromString(String.valueOf(obj))))); res.result().getJsonArray("invitations").forEach(obj -> invitations.add(new DatabasePlayer(UUID.fromString(String.valueOf(obj))))); future.complete(new Party(new DatabasePlayer(UUID.fromString(res.result().getString("owner"))), members, invitations)); }); return future; } public CompletableFuture getParty(DatabasePlayer player) { CompletableFuture future = new CompletableFuture<>(); Commons.getCommons().getDatabaseManager().getMongoDB().getClient().findOne("partys", new JsonObject().put("owner", player.getUuid().toString()), null, res -> { if(res.succeeded()) { getParty(UUID.fromString(res.result().getString("id"))).whenComplete((party, throwable) -> future.complete(party)); } }); return future; } public CompletableFuture isInParty(DatabasePlayer player) { CompletableFuture future = new CompletableFuture<>(); getParty(player).whenComplete((party, throwable) -> future.complete(party != null)); return future; } }