+ added RabbitMQ integration

This commit is contained in:
Johannes Theiner 2017-04-13 15:56:55 +02:00
parent 9bc84f9d6e
commit a14b794f4e
18 changed files with 122 additions and 319 deletions

View File

@ -41,15 +41,13 @@
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-async:3.4.1" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.4.1" level="project" />
<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: 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" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.1" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.5" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.4" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.9" level="project" />
<orderEntry type="library" name="Maven: joda-time:joda-time:2.9.4" level="project" />
<orderEntry type="library" name="Maven: com.github.TheHolyWaffle:TeamSpeak-3-Java-API:v1.0.13" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.6.2" level="project" />

10
pom.xml
View File

@ -78,6 +78,11 @@
<artifactId>vertx-mongo-client</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rabbitmq-client</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
@ -103,11 +108,6 @@
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>

View File

@ -3,7 +3,7 @@ package eu.univento.commons;
import eu.univento.commons.configuration.ConfigurationHandler;
import eu.univento.commons.database.DatabaseManager;
import eu.univento.commons.logging.LoggingHandler;
import eu.univento.commons.security.SecurityHandler;
import eu.univento.commons.messaging.MessagingManager;
import io.vertx.core.Vertx;
import lombok.Getter;
@ -18,18 +18,18 @@ public class Commons {
private static Commons commons;
private DatabaseManager databaseManager;
private MessagingManager messagingManager;
private ConfigurationHandler configurationHandler;
private SecurityHandler securityHandler;
private LoggingHandler loggingHandler;
private Vertx vertx;
public Commons() {
commons = this;
configurationHandler = new ConfigurationHandler();
databaseManager = new DatabaseManager(this);
securityHandler = new SecurityHandler(this);
loggingHandler = new LoggingHandler();
vertx = Vertx.vertx();
configurationHandler = new ConfigurationHandler();
databaseManager = new DatabaseManager();
messagingManager = new MessagingManager();
loggingHandler = new LoggingHandler();
}
public void shutdown() {

View File

@ -18,10 +18,10 @@ public class DatabaseManager {
private MySQL mySQL;
private Redis redis;
public DatabaseManager(Commons commons) {
ConfigurationHandler config = commons.getConfigurationHandler();
public DatabaseManager() {
ConfigurationHandler config = Commons.getCommons().getConfigurationHandler();
mongoDB = new MongoDB(config.getString("MongoDB.Host"), config.getInteger("MongoDB.Port"), config.getString("MongoDB.Username"), config.getString("MongoDB.Password"), config.getString("MongoDB.Database"));
mySQL = new MySQL(config.getString("MySQL.Host"), config.getString("MySQL.Port"), config.getString("MySQL.Database"), config.getString("MySQL.Username"), config.getString("MySQL.Password"));
mySQL = new MySQL(config.getString("MySQL.Host"), config.getInteger("MySQL.Port"), config.getString("MySQL.Database"), config.getString("MySQL.Username"), config.getString("MySQL.Password"));
redis = new Redis(config.getString("Redis.Hostname"), config.getInteger("Redis.Port"), config.getString("Redis.Password"));
}

View File

@ -15,7 +15,7 @@ public class MongoDB {
public MongoDB(String host, int port, String username, String password, String databaseName) {
client = MongoClient.createShared(Commons.getCommons().getVertx(),
new JsonObject().put("host", host).put("port", port).put("username", username).put("password", password).put("authSource", databaseName));
new JsonObject().put("host", host).put("port", port).put("username", username).put("password", password).put("db_name", databaseName));
}
}

View File

@ -14,7 +14,7 @@ public class MySQL {
@Getter private AsyncSQLClient client;
public MySQL(String hostname, String port, String database, String username, String password) {
public MySQL(String hostname, int port, String database, String username, String password) {
client = MySQLClient.createShared(Commons.getCommons().getVertx(),
new JsonObject().put("host", hostname).put("port", port).put("username", username).put("password", password).put("database", database));
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.messaging;
import eu.univento.commons.Commons;
import eu.univento.commons.configuration.ConfigurationHandler;
import lombok.Getter;
/**
* @author joethei
* @version 0.1
*/
public class MessagingManager {
@Getter
private RabbitMQ rabbitMQ;
public MessagingManager() {
ConfigurationHandler config = Commons.getCommons().getConfigurationHandler();
rabbitMQ = new RabbitMQ(config.getString("RabbitMQ.Host"), config.getInteger("RabbitMQ.Port"),
config.getString("RabbitMQ.Username"), config.getString("RabbitMQ.Password"), config.getString("RabbitMQ.VirtualHost"));
}
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.messaging;
import eu.univento.commons.Commons;
import io.vertx.core.json.JsonObject;
import io.vertx.rabbitmq.RabbitMQClient;
import lombok.Getter;
/**
* @author joethei
* @version 0.1
*/
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);
client = RabbitMQClient.create(Commons.getCommons().getVertx(), config);
}
}

View File

@ -35,28 +35,35 @@ public class DatabasePlayer {
private UUID uuid;
private String name;
private Rank rank;
private PlayerSettings settings;
private boolean muted;
public DatabasePlayer(UUID uuid, String name) {
this.uuid = uuid;
this.name = name;
}
private PlayerSettings settings;
public PlayerSettings getSettings() {
if(settings == null) {
getSettingsFromDatabase().whenComplete((playerSettings, throwable) -> settings = playerSettings);
}
return settings;
public void load() {
getRankAsync().whenComplete((rank1, throwable) -> rank = rank1);
getSettingsAsync().whenComplete((playerSettings, throwable) -> settings = playerSettings);
isMutedAsync().whenComplete((aBoolean, throwable) -> muted = aBoolean);
}
public Language getLanguage() {
return Language.getLanguage(Locale.forLanguageTag(getSettings().getLanguage()));
}
public CompletableFuture<PlayerSettings> getSettingsFromDatabase() {
public CompletableFuture<Language> getLanguageAsync() {
CompletableFuture<Language> future = new CompletableFuture<>();
getSettingsAsync().whenComplete((playerSettings, throwable) -> future.complete(Language.getLanguage(Locale.forLanguageTag(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)));
return future;
}
@ -95,7 +102,6 @@ public class DatabasePlayer {
}
}
});
client.close();
});
}
@ -110,7 +116,6 @@ public class DatabasePlayer {
result.getString("customMessage"),
result.getInstant("date"),
result.getString("proof"));
client.close();
future.complete(data);
});
return future;
@ -135,7 +140,6 @@ public class DatabasePlayer {
json.getInstant("date"),
json.getString("proof")));
}
client.close();
future.complete(data);
}
});
@ -182,7 +186,6 @@ public class DatabasePlayer {
json.getInstant("date"),
json.getString("proof")));
}
client.close();
future.complete(data);
}
});
@ -214,7 +217,6 @@ public class DatabasePlayer {
json.getInstant("date"),
UUID.fromString("kicker")));
}
client.close();
future.complete(data);
}
});
@ -243,15 +245,12 @@ public class DatabasePlayer {
}
}
});
client.close();
});
}
public CompletableFuture<Boolean> isMuted() {
public CompletableFuture<Boolean> isMutedAsync() {
CompletableFuture<Boolean> future = new CompletableFuture<>();
getMute().whenComplete((muteData, throwable) -> {
future.complete(muteData != null && !Instant.now().isAfter(muteData.getDate()));
});
getMute().whenComplete((muteData, throwable) -> future.complete(muteData != null && !Instant.now().isAfter(muteData.getDate())));
return future;
}
@ -277,29 +276,26 @@ public class DatabasePlayer {
UUID.fromString(json.getString("muter")),
json.getInstant("date")));
}
client.close();
future.complete(data);
}
});
return future;
}
public CompletableFuture<Rank> getRankFromDatabase() {
public CompletableFuture<Rank> getRankAsync() {
CompletableFuture<Rank> future = new CompletableFuture<>();
getObjectFromDatabase("rank").whenComplete((entries, throwable) ->
future.complete(Rank.valueOf(entries.getString("rank"))));
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
client.findOne("players", new JsonObject().put("uuid", uuid.toString()), null, res -> {
if(res.succeeded()) {
future.complete(Rank.valueOf(res.result().getString("rank")));
}else{
res.cause().printStackTrace();
future.complete(Rank.Player);
}
});
return future;
}
private Rank rank;
public Rank getRank() {
if (rank == null) {
getRankFromDatabase().whenComplete((rank1, throwable) -> rank = rank1);
}
return rank;
}
public void setRank(Rank rank) {
setInDatabase("rank", rank.name());
}
@ -308,17 +304,26 @@ public class DatabasePlayer {
return getRank().getValue() >= rank.getValue();
}
public CompletableFuture<Boolean> isAllowedAsync(Rank rank) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
getRankAsync().whenComplete((rank1, throwable) -> future.complete(rank1.getValue() >= rank.getValue()));
return future;
}
public boolean isAllowed(Group group) {
return getRank().getGroup() == group;
}
public CompletableFuture<Boolean> isAllowedAsync(Group group) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
getRankAsync().whenComplete((rank1, throwable) -> future.complete(rank1.getGroup() == group));
return future;
}
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 -> {
client.close();
future.complete(res.succeeded());
});
client.findOne("players", new JsonObject().put("uuid", uuid.toString()), new JsonObject("uuid"), res -> future.complete(res.succeeded()));
return future;
}
@ -424,7 +429,6 @@ public class DatabasePlayer {
for (Object uuid : res.result().getJsonArray("friends")) {
data.add(new FriendData(UUID.fromString(String.valueOf(uuid))));
}
client.close();
future.complete(data);
}
});
@ -459,22 +463,16 @@ public class DatabasePlayer {
}
public void addCoins(int coins) {
getCoins().whenComplete((integer, throwable) -> {
setCoins(integer + coins);
});
getCoins().whenComplete((integer, throwable) -> setCoins(integer + coins));
}
public void substractCoins(int coins) {
getCoins().whenComplete((integer, throwable) -> {
setCoins(integer - coins);
});
getCoins().whenComplete((integer, throwable) -> setCoins(integer - coins));
}
public CompletableFuture<Integer> getBoardID() {
CompletableFuture<Integer> future = new CompletableFuture<>();
getObjectFromDatabase("boardID").whenComplete((entries, throwable) -> {
future.complete(entries.getInteger("boardID"));
});
getObjectFromDatabase("boardID").whenComplete((entries, throwable) -> future.complete(entries.getInteger("boardID")));
return future;
}
@ -493,17 +491,13 @@ public class DatabasePlayer {
throwable.printStackTrace();
}
}
client.close();
});
}
public CompletableFuture<JsonObject> getObjectFromDatabase(String name) {
CompletableFuture<JsonObject> future = new CompletableFuture<>();
MongoClient client = Commons.getCommons().getDatabaseManager().getMongoDB().getClient();
client.findOne("players", new JsonObject().put("uuid", uuid.toString()), new JsonObject(name), res -> {
client.close();
future.complete(res.result());
});
client.findOne("players", new JsonObject().put("uuid", uuid.toString()), null, res -> future.complete(res.result()));
return future;
}
}

View File

@ -1,68 +0,0 @@
package eu.univento.commons.player.profiles;
import com.mongodb.CursorType;
import com.mongodb.client.FindIterable;
import eu.univento.commons.player.DatabasePlayer;
import org.bson.Document;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.stream.Collectors;
/**
* @author joethei
* @version 0.1
*/
public class Profile {
private DatabasePlayer player;
private String id;
private String name;
private Date date;
private Save save;
private Collection<StoryItem> items;
public Profile(DatabasePlayer player, String id) {
this.player = player;
this.id = id;
FindIterable<Document> cursor = player.getProfileCollection().find(new Document("_id", id));
cursor.cursorType(CursorType.NonTailable);
Document doc = cursor.first();
this.name = doc.getString("name");
this.save = Save.valueOf(doc.getString("save"));
this.date = doc.getDate("date");
Collection<String> list = (Collection<String>) doc.get("items");
items = new LinkedList<>();
items.addAll(list.stream().map(StoryItem::valueOf).collect(Collectors.toList()));
}
public DatabasePlayer getPlayer() {
return player;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Save getSave() {
return save;
}
public Date getDate() {
return date;
}
public Collection<StoryItem> getItems() {
return items;
}
public String getURL() {
return "https://players.univento.eu/" + player.getUuid() + "/profile/" + id;
}
}

View File

@ -1,38 +0,0 @@
package eu.univento.commons.player.profiles;
/**
* @author joethei
* @version 0.1
*/
public enum Save {
START("Starting", 2.0D, 0.0D, 6.0D);
private String name;
private double x;
private double y;
private double z;
Save(String name, double x, double y, double z) {
this.name = name;
this.x = x;
this.y = y;
this.z = z;
}
public String getName() {
return name;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
}

View File

@ -1,33 +0,0 @@
package eu.univento.commons.player.profiles;
/**
* @author joethei
* @version 0.1
*/
public enum StoryItem {
START("Item.Stone", "STONE", 3);
private String name;
private String material;
private int durability;
StoryItem(String name, String material, int durability) {
this.name = name;
this.material = material;
this.durability = durability;
}
public String getName() {
return name;
}
public String getMaterial() {
return material;
}
public int getDurability() {
return durability;
}
}

View File

@ -1,46 +0,0 @@
package eu.univento.commons.security;
import eu.univento.commons.Commons;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.function.Consumer;
/**
* @author joethei
* @version 0.1
*/
public class SecurityHandler {
private Commons commons;
private ArrayList<String> cache = new ArrayList<>();
//TODO: add real security
public SecurityHandler(Commons commons) {
this.commons = commons;
/*
load(resultSet -> {
try {
while (resultSet.next()) {
cache.add(resultSet.getString("ip"));
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
});
*/
}
public boolean isValidServer(String ip) {
//return cache.contains(ip);
return true;
}
private void load(Consumer<ResultSet> consumer) {
commons.getDatabaseManager().getAsyncMySQL().query("SELECT * FROM Servers;", consumer);
}
}

View File

@ -21,3 +21,8 @@ TS3.ID = 1
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.Username = univento
RabbitMQ.Password = tYRHXZNm4exW@QB6rMm72pHnafrT8wQ5ebGPEetJDBVU2mj25kTVXuYeQavb#EYr
RabbitMQ.VirtualHost = univento

View File

@ -1,4 +0,0 @@
Prefix = undefined
Tab_Prefix = §8• §6U N I V E N T O §8•
Not_a_player = You are not a valid player
No_perms =

View File

@ -1,56 +0,0 @@
Pack.failed_download = Das Pack konnte nicht geladen werden
Pack.declined = Du hast das Pack abgelehnt, du wirst nicht das volle Spielerlebnis erhalten
System.unknown_error = Es ist ein unbekannter Fehler aufgetreten
System.critical_error = Es ist leider ein kritischer Fehler aufgetreten
Server.restart = Der Server wurde neugestartet
Server.stop = Der Server wurde ausgeschaltet
Server.reconnect = Bitte verbinde dich neu
Server.full = Dieser Server ist bereits voll
Server.ip = IP: play.univento.eu
Server.ts_ip = TeamSpeak IP: ts.univento.eu
Server.board_ip = Foren Adresse: forum.univento.eu
Command.prefix = univento.eu Default Command Prefix
Command.not_found = Dieser Befehl konnte nicht gefunden werden
Command.not_online = Der Spieler $player ist nicht online
Command.no_perms = Um diesen Befehl zu benutzen hast du keine Rechte
Command.Build.usage = Nutze /build
Command.Build.toogle_on = Du hast den Build Modus aktiviert
Command.Build.toogle_off = Du hast den Build Modus deaktiviert
Command.TS.usage = Nutze /ts
Command.TS.already_verified = Du wurdest bereits verifiziert
Command.TS.not_online = Du bist nicht mit dem TeamSpeak Server verbunden
Command.TS.verified = Du wurdest verifiziert
Command.Fix.usage = Nutze /fix <Spieler>
Command.Fix.own = Du hast dich selbst gefixt
Command.Fix.other = Du hast $player gefixt
Command.Fix.by_other = Du wurdest von $player gefixt
Shop.Lobby.Main = Lobby Shop
Shop.Game.Maya.Main = Maya Game Shop
Lobby.Item.Navigator = Navigator
Lobby.Item.LobbySwitcher = LobbySwitcher
Lobby.Item.Adventure = Adventure Kram
Lobby.Item.FunChest = FunKiste
Lobby.Item.Youtuber = Youtuber Zeugs
Lobby.Item.Profile = Profil
Lobby.Item.YT.Nick = Nicken
Lobby.Item.YT.SilentLobby = SilentLobby
Lobby.Item.YT.Forcefield = Forcefield
Lobby.Item.YT.Extras = Extras
Lobby.Item.YT.Back = Zurück
Lobby.Menu.Nav.Spawn = Spawn
Lobby.Menu.Nav.Strive = Strive
Lobby.Menu.Nav.Hustle = Hustle
Lobby.Menu.Nav.Build = Bau Server
Lobby.Menu.Profile = Dein Profil
Lobby.Menu.other_Profile = Profil von $player
Lobby.Menu.Settings = Einstellungen
Lobby.Menu.Language = Sprache einstellen

View File

@ -6,7 +6,6 @@
package eu.univento.commons;
import eu.univento.commons.database.DatabaseManager;
import eu.univento.commons.security.SecurityHandler;
import org.junit.Test;
import static org.junit.Assert.assertFalse;