More work on the heartbeat

This commit is contained in:
Colin McDonald 2016-06-24 23:50:51 -04:00
parent 1d21d70371
commit ef3f7345e5

View File

@ -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<RoutingContext> {
Map<UUID, List<Grant>> grants = result.result().result(1);
Map<UUID, List<Punishment>> punishments = result.result().result(2);
Map<String, Object> response = new HashMap<>();
List<Future> loginInfoFutures = new LinkedList<>();
for (Map.Entry<UUID, User> userEntry : users.entrySet()) {
users.forEach((uuid, user) -> {
Future<Map<String, Object>> loginInfoFuture = Future.future();
Map<String, Object> res2 = new HashMap<>();
UUID uuid = userEntry.getKey();
User user = userEntry.getValue();
createLoginInfo(uuid, user, server, grants.get(uuid), punishments.get(uuid), Future.<Map<String, Object>>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<Void> nameCollisionCallback = new BlockingCallback<>();
user.checkNameCollisions(nameCollisionCallback);
nameCollisionCallback.get();
BlockingCallback<Void> insertCallback = new BlockingCallback<>();
user.insert(insertCallback);
insertCallback.get();
users.put(uuid, user);
}
// Only save if needed
if (user.seenOnServer(server)) {
BlockingCallback<UpdateResult> saveCallback = new BlockingCallback<>();
user.save(saveCallback);
saveCallback.get();
}
// TODO: Provide IPs for ip ban lookup (and ip intel)
BlockingCallback<Map<String, Object>> 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<RoutingContext> {
return result;
}
private void createLoginInfo(UUID uuid, User user, Server server, List<Grant> grants, List<Punishment> punishments, Future<Map<String, Object>> callback) {
if (user == null) {
/*String username = playerNames.get(uuid);
user = new User(uuid, username);
BlockingCallback<Void> nameCollisionCallback = new BlockingCallback<>();
user.checkNameCollisions(nameCollisionCallback);
nameCollisionCallback.get();
BlockingCallback<Void> insertCallback = new BlockingCallback<>();
user.insert(insertCallback);
insertCallback.get();
users.put(uuid, user);
// Only save if needed
if (user.seenOnServer(server)) {
BlockingCallback<UpdateResult> saveCallback = new BlockingCallback<>();
user.save(saveCallback);
saveCallback.get();
}
// TODO: Provide IPs for ip ban lookup (and ip intel)
BlockingCallback<Map<String, Object>> 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);
}
});
}
}
}
}