+ new way to get information about other servers in the network

~ refactoring
This commit is contained in:
Johannes Theiner 2017-06-04 13:36:38 +02:00
parent 3782ee31b5
commit 5db8688a1e
20 changed files with 176 additions and 141 deletions

View File

@ -1,7 +1,7 @@
image: maven:3-jdk-8
build:
script: "mvn clean install deploy --settings .gitlab/settings.xml"
script: "mvn clean --settings .gitlab/settings.xml"
artifacts:
name: "Commons"
paths:

View File

@ -11,6 +11,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: ai.api:libai:1.4.8" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.14.8" level="project" />
<orderEntry type="library" name="Maven: io.vertx:vertx-core:3.4.1" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.8.Final" level="project" />
@ -43,6 +44,7 @@
<orderEntry type="library" name="Maven: org.mongodb:bson:3.4.1" level="project" />
<orderEntry type="library" name="Maven: io.vertx:vertx-rabbitmq-client:3.4.1" level="project" />
<orderEntry type="library" name="Maven: com.rabbitmq:amqp-client:3.6.5" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.3.1" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
@ -53,6 +55,9 @@
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-core:2.6.2" level="project" />
<orderEntry type="library" name="Maven: com.github.nsp:JSkills:master-0.9.0-g8b333ec-15" level="project" />
<orderEntry type="library" name="Maven: org.ejml:simple:0.28" level="project" />
<orderEntry type="library" name="Maven: org.ejml:core:0.28" level="project" />
<orderEntry type="library" name="Maven: org.ejml:dense64:0.28" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.gitlab:java-gitlab-api:1.2.7" level="project" />

17
pom.xml
View File

@ -11,6 +11,10 @@
<description>Das Commons Modul stellt alles zur Verfügung was von allen anderen Modulen benötigt wird, wie etwa Datenbank Zugriffe</description>
<url>https://development.univento.eu/docs/Commons</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<organization>
<name>univentoEU</name>
<url>https://univento.eu</url>
@ -120,6 +124,12 @@
</build>
<dependencies>
<dependency>
<groupId>ai.api</groupId>
<artifactId>libai</artifactId>
<version>1.4.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@ -150,6 +160,13 @@
<artifactId>vertx-rabbitmq-client</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>

View File

@ -27,8 +27,8 @@ public class Commons {
commons = this;
vertx = Vertx.vertx();
configurationHandler = new ConfigurationHandler();
databaseManager = new DatabaseManager();
messagingManager = new MessagingManager();
databaseManager = new DatabaseManager();
loggingHandler = new LoggingHandler();
}

View File

@ -12,8 +12,6 @@ import lombok.Data;
@Data
public class DatabaseManager {
//TODO: add board sql back, when forum is online.
private MongoDB mongoDB;
private MySQL mySQL;
private Redis redis;

View File

@ -17,5 +17,4 @@ public class MongoDB {
client = MongoClient.createShared(Commons.getCommons().getVertx(),
new JsonObject().put("host", host).put("port", port).put("username", username).put("password", password).put("db_name", databaseName));
}
}

View File

@ -1,10 +1,13 @@
package eu.univento.commons.helpers;
import lombok.Getter;
/**
* @author joethei
* @version 1.0
*/
@Getter
public class Location {
private final String world;
@ -22,28 +25,4 @@ public class Location {
this.pitch = pitch;
this.yaw = yaw;
}
public String getWorld() {
return world;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public float getPitch() {
return pitch;
}
public float getYaw() {
return yaw;
}
}

View File

@ -1,7 +1,7 @@
package eu.univento.commons.logging;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import eu.univento.commons.messaging.MessageHandler;
import eu.univento.commons.server.ServerType;
/**
* @author joethei
@ -10,36 +10,9 @@ import org.apache.logging.log4j.Logger;
public class LoggingHandler {
public Logger getCommons() {
return LogManager.getLogger("Commons");
}
public LoggingHandler() {}
public Logger getCore() {
return LogManager.getLogger("Core");
}
public Logger getBungeeCore() {
return LogManager.getLogger("BungeeCore");
}
public Logger getLobby() {
return LogManager.getLogger("Lobby");
}
public Logger getTrashGames() {
return LogManager.getLogger("TrashGames");
}
public Logger getTeamVento() {
return LogManager.getLogger("TeamVento");
}
public Logger getCloudCommons() {return LogManager.getLogger("CloudCommons");}
public Logger getCloudDaemon() {return LogManager.getLogger("CloudDaemon");}
public Logger getCloudMaster() {return LogManager.getLogger("CloudMaster");}
public LoggingHandler() {
public void log(ServerType type, String message) {
MessageHandler.sendMessage("log." + type.getName(), message);
}
}

View File

@ -22,23 +22,23 @@ public class MessageHandler {
public static void registerListener(String queue, MessageEvent event) {
events.add(event);
Commons.getCommons().getMessagingManager().getRabbitMQ().getClient().basicConsume(queue, "univento." + queue, res -> {
if(!res.succeeded()) res.cause().printStackTrace();
Commons.getCommons().getVertx().eventBus().consumer(queue, msg -> {
JsonObject json = (JsonObject) msg.body();
event.onMessageReceived(queue, json.getString("body"));
});
Commons.getCommons().getVertx().eventBus().consumer(queue, msg -> {
JsonObject json = (JsonObject) msg.body();
event.onMessageReceived(queue, json.getString("body"));
});
Commons.getCommons().getMessagingManager().start().whenComplete((rabbitMQ, throwable) -> rabbitMQ.getClient().basicConsume(queue, queue, res -> {
if(!res.succeeded()) res.cause().printStackTrace();
}));
}
public static void sendMessage(String queue, String message) {
Commons.getCommons().getMessagingManager().getRabbitMQ().getClient().basicPublish("", queue, new JsonObject().put("body", message), res -> {
Commons.getCommons().getMessagingManager().start().whenComplete((rabbitMQ, throwable) -> rabbitMQ.getClient().basicPublish("", queue, new JsonObject().put("body", message), res -> {
if(res.succeeded()) {
for(MessageEvent event : events) {
event.onMessageSend(queue, message);
}
}else res.cause().printStackTrace();
});
}));
}
}

View File

@ -7,7 +7,8 @@ package eu.univento.commons.messaging;
import eu.univento.commons.Commons;
import eu.univento.commons.configuration.ConfigurationHandler;
import lombok.Getter;
import java.util.concurrent.CompletableFuture;
/**
* @author joethei
@ -15,7 +16,6 @@ import lombok.Getter;
*/
public class MessagingManager {
@Getter
private RabbitMQ rabbitMQ;
public MessagingManager() {
@ -23,4 +23,17 @@ public class MessagingManager {
rabbitMQ = new RabbitMQ(config.getString("RabbitMQ.Host"), config.getInteger("RabbitMQ.Port"),
config.getString("RabbitMQ.Username"), config.getString("RabbitMQ.Password"), config.getString("RabbitMQ.VirtualHost"));
}
public CompletableFuture<RabbitMQ> start() {
CompletableFuture<RabbitMQ> future = new CompletableFuture<>();
if(rabbitMQ.getClient().isConnected()) future.complete(rabbitMQ);
rabbitMQ.getClient().start(res -> {
if(res.failed()) res.cause().printStackTrace();
else future.complete(rabbitMQ);
});
return future;
}
}

View File

@ -19,8 +19,8 @@ public class RabbitMQ {
@Getter private RabbitMQClient client;
public RabbitMQ(String host, int port, String user, String password, String virtualHost) {
JsonObject config = new JsonObject().put("host", host).put("port", port).put("user", user).put("password", password).put("virtualHost", virtualHost).put("connectionTimeout", 60)
.put("requestedHeartbeat", 60).put("handshakeTimeout", 60).put("requestedChannelMax", 5).put("networkRecoveryInterval", 5).put("automaticRecoveryEnabled", true);
JsonObject config = new JsonObject().put("host", host).put("port", port).put("user", user).put("password", password).put("virtualHost", virtualHost).put("connectionTimeout", 600)
.put("requestedHeartbeat", 60).put("handshakeTimeout", 600).put("requestedChannelMax", 5).put("networkRecoveryInterval", 5).put("automaticRecoveryEnabled", true);
client = RabbitMQClient.create(Commons.getCommons().getVertx(), config);
}
}

View File

@ -37,6 +37,7 @@ public class DatabasePlayer {
private Rank rank;
private PlayerSettings settings;
private Language language;
private boolean muted;
public DatabasePlayer(UUID uuid, String name) {
@ -48,22 +49,24 @@ public class DatabasePlayer {
getRankAsync().whenComplete((rank1, throwable) -> rank = rank1);
getSettingsAsync().whenComplete((playerSettings, throwable) -> settings = playerSettings);
isMutedAsync().whenComplete((aBoolean, throwable) -> muted = aBoolean);
getLanguageAsync().whenComplete((language1, throwable) -> language = language1);
}
public Language getLanguage() {
return Language.getLanguage(Locale.forLanguageTag(getSettings().getLanguage()));
return Language.getLanguage(getSettings().getLanguage());
}
public CompletableFuture<Language> getLanguageAsync() {
CompletableFuture<Language> future = new CompletableFuture<>();
getSettingsAsync().whenComplete((playerSettings, throwable) -> future.complete(Language.getLanguage(Locale.forLanguageTag(playerSettings.getLanguage()))));
getSettingsAsync().whenComplete((playerSettings, throwable) -> future.complete(Language.getLanguage(playerSettings.getLanguage())));
return future;
}
public CompletableFuture<PlayerSettings> getSettingsAsync() {
CompletableFuture<PlayerSettings> future = new CompletableFuture<>();
getObjectFromDatabase("Settings").whenComplete((entries, throwable) -> future.complete(new PlayerSettings(this, entries)));
getObjectFromDatabase("Settings").whenComplete((entries, throwable) ->
future.complete(new PlayerSettings(this, entries.getJsonObject("Settings"))));
return future;
}
@ -94,13 +97,7 @@ public class DatabasePlayer {
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
client.insert("bans", obj, res -> {
if (res.failed()) {
try {
throw res.cause();
} catch (Throwable throwable1) {
throwable1.printStackTrace();
}
}
if (res.failed()) res.cause().printStackTrace();
});
});
@ -110,7 +107,9 @@ public class DatabasePlayer {
CompletableFuture<BanData> future = new CompletableFuture<>();
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
client.findOne("bans", new JsonObject().put("uuid", uuid.toString()), null, res -> {
if(res.failed()) future.complete(null);
JsonObject result = res.result();
if(result.isEmpty()) future.complete(null);
BanData data = new BanData(BanReason.valueOf(result.getString("reason")),
UUID.fromString(result.getString("banner")),
result.getString("customMessage"),
@ -323,7 +322,7 @@ public class DatabasePlayer {
public CompletableFuture<Boolean> hasPlayedBefore() {
CompletableFuture<Boolean> future = new CompletableFuture<>();
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
client.findOne("players", new JsonObject().put("uuid", uuid.toString()), new JsonObject("uuid"), res -> future.complete(res.succeeded()));
client.findOne("players", new JsonObject().put("uuid", uuid.toString()), null, res -> future.complete(res.succeeded()));
return future;
}
@ -342,65 +341,49 @@ public class DatabasePlayer {
public CompletableFuture<Instant> getFirstLogin() {
CompletableFuture<Instant> future = new CompletableFuture<>();
getObjectFromDatabase("firstLogin").whenComplete((entries, throwable) -> {
future.complete(entries.getInstant("firstLogin"));
});
getObjectFromDatabase("firstLogin").whenComplete((entries, throwable) -> future.complete(entries.getInstant("firstLogin")));
return future;
}
public CompletableFuture<Instant> getLastLogin() {
CompletableFuture<Instant> future = new CompletableFuture<>();
getObjectFromDatabase("lastLogin").whenComplete((entries, throwable) -> {
future.complete(entries.getInstant("lastLogin"));
});
getObjectFromDatabase("lastLogin").whenComplete((entries, throwable) -> future.complete(entries.getInstant("lastLogin")));
return future;
}
public CompletableFuture<Instant> getLastOnline() {
CompletableFuture<Instant> future = new CompletableFuture<>();
getObjectFromDatabase("lastOnline").whenComplete((entries, throwable) -> {
future.complete(entries.getInstant("lastOnline"));
});
getObjectFromDatabase("lastOnline").whenComplete((entries, throwable) -> future.complete(entries.getInstant("lastOnline")));
return future;
}
public CompletableFuture<String> getLastIP() {
CompletableFuture<String> future = new CompletableFuture<>();
getObjectFromDatabase("lastIP").whenComplete((entries, throwable) -> {
future.complete(entries.getString("lastIP"));
});
getObjectFromDatabase("lastIP").whenComplete((entries, throwable) -> future.complete(entries.getString("lastIP")));
return future;
}
public CompletableFuture<String> getTSID() {
CompletableFuture<String> future = new CompletableFuture<>();
getObjectFromDatabase("tsid").whenComplete((entries, throwable) -> {
future.complete(entries.getString("tsid"));
});
getObjectFromDatabase("tsid").whenComplete((entries, throwable) -> future.complete(entries.getString("tsid")));
return future;
}
public CompletableFuture<Integer> getTimesJoined() {
CompletableFuture<Integer> future = new CompletableFuture<>();
getObjectFromDatabase("timesJoined").whenComplete((entries, throwable) -> {
future.complete(entries.getInteger("TimesJoined"));
});
getObjectFromDatabase("timesJoined").whenComplete((entries, throwable) -> future.complete(entries.getInteger("TimesJoined")));
return future;
}
public CompletableFuture<Integer> getCoins() {
CompletableFuture<Integer> future = new CompletableFuture<>();
getObjectFromDatabase("coins").whenComplete((entries, throwable) -> {
future.complete(entries.getInteger("coins"));
});
getObjectFromDatabase("coins").whenComplete((entries, throwable) -> future.complete(entries.getInteger("coins")));
return future;
}
public CompletableFuture<Integer> getExperience() {
CompletableFuture<Integer> future = new CompletableFuture<>();
getObjectFromDatabase("experience").whenComplete((entries, throwable) -> {
future.complete(entries.getInteger("experience"));
});
getObjectFromDatabase("experience").whenComplete((entries, throwable) -> future.complete(entries.getInteger("experience")));
return future;
}
@ -482,7 +465,7 @@ public class DatabasePlayer {
public void setInDatabase(String name, Object obj) {
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
client.findOneAndUpdate("players", new JsonObject().put("uuid", uuid.toString()),
client.update("players", new JsonObject().put("uuid", uuid.toString()),
new JsonObject().put("$set", new JsonObject().put(name, obj)), res -> {
if (res.failed()) {
try {

View File

@ -37,11 +37,11 @@ public class Language {
Commons.getCommons().getDatabaseManager().getMySQL().getClient().getConnection(res -> {
if(res.succeeded()) {
SQLConnection connection = res.result();
connection.query("SELECT identifier, " + locale.getISO3Language() + " FROM translations;", result -> {
connection.query("SELECT identifier, " + locale.getLanguage() + " FROM translations;", result -> {
if(result.succeeded()) {
ResultSet rs = result.result();
for(JsonObject json : rs.getRows()) {
translations.put(json.getString("translations"), json.getString(locale.getISO3Language()));
translations.put(json.getString("identifier"), json.getString(locale.getLanguage()));
}
}else result.cause().printStackTrace();
@ -50,9 +50,9 @@ public class Language {
});
}
public static Language getLanguage(Locale locale) {
public static Language getLanguage(String locale) {
for(Language language : languages) {
if(language.getLocale().equals(locale)) return language;
if(language.getLocale().getLanguage().equals(locale)) return language;
}
return null;
}
@ -61,7 +61,7 @@ public class Language {
return getWord(constant.getName());
}
public String getWord(String constant) {
return translations.get(constant);
public String getWord(String identifier) {
return translations.get(identifier);
}
}

View File

@ -69,8 +69,6 @@ public class PlayerSettings {
return json.getBoolean("nicked");
}
public void setPlayerVisibility(String visibility) {
json.put("playerVisibility", visibility);
update();

View File

@ -1,6 +1,7 @@
package eu.univento.commons.player.uuid;
import eu.univento.commons.Commons;
import eu.univento.commons.server.ServerType;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@ -27,7 +28,7 @@ public class NameFetcher {
String uniqueId = (String) response.get("uuid");
if (uniqueId.length() == 0)
Commons.getCommons().getLoggingHandler().getCommons().error("A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?");
Commons.getCommons().getLoggingHandler().log(ServerType.getServerType() ,"A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?");
return (String) response.get("username");
} catch (IOException |ParseException e) {
@ -42,17 +43,17 @@ public class NameFetcher {
String name = (String) response.get("name");
if (name == null)
Commons.getCommons().getLoggingHandler().getCommons().error("A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?");
Commons.getCommons().getLoggingHandler().log(ServerType.getServerType(),"A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?");
String cause = (String) response.get("cause");
String errorMessage = (String) response.get("errorMessage");
if (cause != null && cause.length() > 0) {
Commons.getCommons().getLoggingHandler().getCommons().error(errorMessage);
Commons.getCommons().getLoggingHandler().log(ServerType.getServerType(), errorMessage);
}
return name;
} catch (IOException | ParseException e) {
e.printStackTrace();
Commons.getCommons().getLoggingHandler().getCommons().error("A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?");
Commons.getCommons().getLoggingHandler().log(ServerType.getServerType(), "A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?");
}
return null;
}

View File

@ -0,0 +1,46 @@
package eu.univento.commons.server;
import eu.univento.commons.event.MessageEvent;
import eu.univento.commons.messaging.MessageHandler;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
/**
* @author joethei
* @version 1.0
*/
@AllArgsConstructor
@Getter
public class ServerInfo {
public static CompletableFuture<ServerInfo> getServerInfo(String server) {
CompletableFuture<ServerInfo> future = new CompletableFuture<>();
UUID uuid = UUID.randomUUID();
MessageHandler.sendMessage("cloud.servers", uuid.toString() + "|INFO|" + server);
MessageHandler.registerListener("cloud.servers", new MessageEvent() {
@Override
public void onMessageReceived(String queue, String message) {
String[] info = message.split(Pattern.quote("|"));
if(info[0].equals(uuid.toString())) {
future.complete(new ServerInfo(info[1], ServerType.valueOf(info[2]), Integer.getInteger(info[3])));
}
}
@Override
public void onMessageSend(String queue, String message) {
}
});
return future;
}
private String name;
private ServerType type;
private int players;
}

View File

@ -1,16 +1,24 @@
package eu.univento.commons.server;
import lombok.Getter;
import lombok.Setter;
/**
* @author joethei
* @version 0.1
* @version 1.0
*/
@Getter
public enum ServerType {
LOBBY(0, "Lobby", "§6Lobby"),
SECRET_GAME_FREE4ALL(1, "Free4All", "§6Free4All"),
SECRET_GAME_TRASHGAMES(2, "TrashGames", "§6TrashGames"),
GAME_WOOLGET(3, "WoolGet", "§6WoolGet"),
GAME_MAYA(4, "Maya", "§6Maya");
NONE(0, "None", "None"),
PROXY(1, "Proxy", "§6Proxy"),
LOBBY(2, "Lobby", "§6Lobby"),
TEAM_BUILD(3, "TeamVento", "§6TeamVento"),
SECRET_GAME_FREE4ALL(4, "Free4All", "§6Free4All"),
SECRET_GAME_TRASHGAMES(5, "TrashGames", "§6TrashGames"),
GAME_WOOLGET(6, "WoolGet", "§6WoolGet"),
GAME_MAYA(7, "Maya", "§6Maya"),
GAME_IMPACT(8, "Impact", "§6Impact");
private int id;
private String name;
@ -21,16 +29,5 @@ public enum ServerType {
this.name = name;
this.prefix = prefix;
}
public String getName() {
return name;
}
public String getPrefix() {
return prefix;
}
public int getID() {
return id;
}
@Getter @Setter private static ServerType serverType;
}

12
src/main/main.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="target" level="project" />
</component>
</module>

View File

@ -22,9 +22,10 @@ TS3.Username = serveradmin
TS3.Password = Wb69cypb499CTP4vtaJPKpKZELUbPD
Slack.hook_url = https://hooks.slack.com/services/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
RabbitMQ.Host = vps.joethei.de
RabbitMQ.Port = 5672
RabbitMQ.Port = 5673
RabbitMQ.Username = univento
RabbitMQ.Password = tYRHXZNm4exW@QB6rMm72pHnafrT8wQ5ebGPEetJDBVU2mj25kTVXuYeQavb#EYr
RabbitMQ.VirtualHost = univento
Gitlab.Host = https://gitlab.com/
Gitlab.token = add token here
api.ai.token = e1c97924577a4743800b3fefb16dd527

13
src/test/test.iml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="target" level="project" />
<orderEntry type="module" module-name="main" />
</component>
</module>