diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java index 87690c4..197e399 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java @@ -2,7 +2,6 @@ package net.frozenorb.apiv3.route.servers; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.mongodb.client.result.UpdateResult; import io.vertx.core.CompositeFuture; import io.vertx.core.Future; import io.vertx.core.Handler; @@ -15,7 +14,6 @@ import net.frozenorb.apiv3.actor.Actor; import net.frozenorb.apiv3.actor.ActorType; import net.frozenorb.apiv3.actor.actors.ServerActor; import net.frozenorb.apiv3.model.*; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.FutureCompatibilityCallback; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.PermissionUtils; @@ -99,43 +97,21 @@ public final class POSTServersHeartbeat implements Handler { Map> grants = result.result().result(1); Map> punishments = result.result().result(2); Map response = new HashMap<>(); - List loginInfoFutures = new LinkedList<>(); - for (Map.Entry userEntry : users.entrySet()) { + users.forEach((uuid, user) -> { Future> loginInfoFuture = Future.future(); - Map res2 = new HashMap<>(); - UUID uuid = userEntry.getKey(); - User user = userEntry.getValue(); + createLoginInfo(uuid, user, server, grants.get(uuid), punishments.get(uuid), Future.>future().setHandler((loginInfoResult) -> { + if (loginInfoResult.failed()) { + loginInfoFuture.fail(loginInfoFuture.cause()); + } else { + loginInfoFuture.complete(ImmutableMap.of(uuid.toString(), loginInfoResult.result())); + } + })); - if (user == null) { - String username = playerNames.get(uuid); - user = new User(uuid, username); - BlockingCallback nameCollisionCallback = new BlockingCallback<>(); - user.checkNameCollisions(nameCollisionCallback); - nameCollisionCallback.get(); - BlockingCallback insertCallback = new BlockingCallback<>(); - user.insert(insertCallback); - insertCallback.get(); - users.put(uuid, user); - } - - // Only save if needed - if (user.seenOnServer(server)) { - BlockingCallback saveCallback = new BlockingCallback<>(); - user.save(saveCallback); - saveCallback.get(); - } - - // TODO: Provide IPs for ip ban lookup (and ip intel) - BlockingCallback> loginInfo = new BlockingCallback<>(); - user.getLoginInfo(server, null, punishments.get(uuid), ImmutableList.of(), grants.get(uuid), loginInfo); - res2.put(uuid.toString(), loginInfo.get()); - - loginInfoFuture.complete(res2); loginInfoFutures.add(loginInfoFuture); - } + }); CompositeFuture.all(loginInfoFutures).setHandler((allLoginInfo) -> { for (int i = 0; i < allLoginInfo.result().size(); i++) { @@ -205,4 +181,55 @@ public final class POSTServersHeartbeat implements Handler { return result; } + private void createLoginInfo(UUID uuid, User user, Server server, List grants, List punishments, Future> callback) { + if (user == null) { + /*String username = playerNames.get(uuid); + user = new User(uuid, username); + BlockingCallback nameCollisionCallback = new BlockingCallback<>(); + user.checkNameCollisions(nameCollisionCallback); + nameCollisionCallback.get(); + BlockingCallback insertCallback = new BlockingCallback<>(); + user.insert(insertCallback); + insertCallback.get(); + users.put(uuid, user); + + // Only save if needed + if (user.seenOnServer(server)) { + BlockingCallback saveCallback = new BlockingCallback<>(); + user.save(saveCallback); + saveCallback.get(); + } + + // TODO: Provide IPs for ip ban lookup (and ip intel) + BlockingCallback> loginInfo = new BlockingCallback<>(); + user.getLoginInfo(server, null, punishments.get(uuid), ImmutableList.of(), grants.get(uuid), loginInfo); + res2.put(uuid.toString(), loginInfo.get());*/ + } else { + if (user.seenOnServer(server)) { + user.save((ignored, error) -> { + if (error != null) { + callback.fail(error); + return; + } + + user.getLoginInfo(server, null, punishments, ImmutableList.of(), grants, (loginInfo, error2) -> { + if (error2 != null) { + callback.fail(error2); + } else { + callback.complete(loginInfo); + } + }); + }); + } else { + user.getLoginInfo(server, null, punishments, ImmutableList.of(), grants, (loginInfo, error2) -> { + if (error2 != null) { + callback.fail(error2); + } else { + callback.complete(loginInfo); + } + }); + } + } + } + } \ No newline at end of file