From 1badee98817134d83adffc7cdbbef169b70019e6 Mon Sep 17 00:00:00 2001 From: joethei Date: Mon, 10 Oct 2016 13:21:12 +0200 Subject: [PATCH] + added async MongoDB + added Slack Integration + added HikariCP + better i18n Message Support + added player ranking + added Game stats --- Commons.iml | 12 ++-- pom.xml | 51 ++++++++++------- .../java/eu/univento/commons/Commons.java | 21 ++----- .../commons/database/AsyncMongoDB.java | 55 +++--------------- .../univento/commons/database/AsyncMySQL.java | 15 +++-- .../commons/database/DatabaseManager.java | 28 ++------- .../eu/univento/commons/database/MySQL.java | 10 +++- .../eu/univento/commons/helpers/Location.java | 1 + .../commons/logging/LoggingHandler.java | 7 +++ .../commons/player/DatabasePlayer.java | 57 ++----------------- .../commons/player/language/Language.java | 4 ++ .../player/language/MessageConstant.java | 39 +++++++++++++ .../commons/player/ranking/PlayerRanking.java | 2 +- .../commons/player/ranking/Ranking.java | 2 +- .../player/settings/PlayerSettings.java | 6 +- .../commons/player/statistics/GameStats.java | 41 +++++++++++++ .../univento/commons/server/ServerType.java | 34 +++++++++++ .../commons/web/slack/SlackHandler.java | 19 +++++++ src/main/resources/config.properties | 6 ++ 19 files changed, 230 insertions(+), 180 deletions(-) create mode 100644 src/main/java/eu/univento/commons/player/language/MessageConstant.java create mode 100644 src/main/java/eu/univento/commons/player/statistics/GameStats.java create mode 100644 src/main/java/eu/univento/commons/server/ServerType.java create mode 100644 src/main/java/eu/univento/commons/web/slack/SlackHandler.java diff --git a/Commons.iml b/Commons.iml index 16f4f79..1ac400d 100644 --- a/Commons.iml +++ b/Commons.iml @@ -12,12 +12,6 @@ - - - - - - @@ -26,10 +20,12 @@ - - + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4039e5c..2863357 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,17 @@ + + + TeamSpeak-3-Java-API-mvn-repo + https://raw.githubusercontent.com/TheHolyWaffle/TeamSpeak-3-Java-API/mvn-repo/ + + true + always + + + + org.projectlombok @@ -80,16 +91,6 @@ mongo-java-driver 3.2.2 - - org.mongodb - mongodb-driver-async - 3.2.2 - - - org.mongodb.morphia - morphia - 1.2.1 - com.zaxxer HikariCP @@ -115,16 +116,6 @@ guava 19.0 - - org.apache.logging.log4j - log4j-api - 2.6.2 - - - org.apache.logging.log4j - log4j-core - 2.6.2 - org.apache.commons commons-lang3 @@ -140,5 +131,25 @@ joda-time 2.9.4 + + com.github.theholywaffle + teamspeak3-api + [1.0.0,2.0.0) + + + org.apache.logging.log4j + log4j-api + 2.6.2 + + + org.apache.logging.log4j + log4j-core + 2.6.2 + + + net.gpedro.integrations.slack + slack-webhook + 1.2.1 + \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/Commons.java b/src/main/java/eu/univento/commons/Commons.java index 665e9f3..be57a34 100644 --- a/src/main/java/eu/univento/commons/Commons.java +++ b/src/main/java/eu/univento/commons/Commons.java @@ -4,38 +4,29 @@ 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.web.slack.SlackHandler; +import lombok.Getter; /** * @author joethei * @version 0.1 */ + +@Getter public class Commons { private DatabaseManager databaseManager; - public DatabaseManager getDatabaseManager() { - return databaseManager; - } - private ConfigurationHandler configurationHandler; - public ConfigurationHandler getConfigurationHandler() { - return configurationHandler; - } - private SecurityHandler securityHandler; - public SecurityHandler getSecurityHandler() { - return securityHandler; - } - private LoggingHandler loggingHandler; - public LoggingHandler getLoggingHandler() { - return loggingHandler; - } + private SlackHandler slackHandler; public Commons() { configurationHandler = new ConfigurationHandler(); databaseManager = new DatabaseManager(this); securityHandler = new SecurityHandler(this); loggingHandler = new LoggingHandler(); + slackHandler = new SlackHandler(configurationHandler.getString("Slack.hook_url")); } public void shutdown() { diff --git a/src/main/java/eu/univento/commons/database/AsyncMongoDB.java b/src/main/java/eu/univento/commons/database/AsyncMongoDB.java index ed63ee7..b944580 100644 --- a/src/main/java/eu/univento/commons/database/AsyncMongoDB.java +++ b/src/main/java/eu/univento/commons/database/AsyncMongoDB.java @@ -1,15 +1,9 @@ package eu.univento.commons.database; -import com.mongodb.MongoCredential; -import com.mongodb.ServerAddress; -import com.mongodb.async.client.MongoClient; -import com.mongodb.async.client.MongoClientSettings; -import com.mongodb.async.client.MongoClients; -import com.mongodb.async.client.MongoDatabase; -import com.mongodb.connection.ClusterSettings; +import lombok.Getter; -import java.util.ArrayList; -import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * @author joethei @@ -17,45 +11,14 @@ import java.util.List; */ public class AsyncMongoDB { - private final MongoClient client; - private MongoDatabase database; + private ExecutorService executor; + @Getter + private MongoDB mongoDB; - private final String host; - private final int port; - private final String username; - private final String password; - private final String databaseName; - - public AsyncMongoDB(String host, int port, String username, String password, String databaseName) { - this.host = host; - this.port = port; - this.username = username; - this.password = password; - this.databaseName = databaseName; - - List addressList = new ArrayList<>(); - addressList.add(new ServerAddress(host, port)); - List credentials = new ArrayList<>(); - credentials.add(MongoCredential.createCredential(username, databaseName, password.toCharArray())); - ClusterSettings clusterSettings = ClusterSettings.builder().hosts(addressList).build(); - MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).credentialList(credentials).build(); - client = MongoClients.create(settings); + public AsyncMongoDB(String host, int port, String user, String password, String database) { + mongoDB = new MongoDB(host, port, user, password, database); + executor = Executors.newCachedThreadPool(); } - private MongoClient getClient() { - if(client == null) - new AsyncMongoDB(host, port, username, password, databaseName); - return client; - } - public MongoDatabase getDatabase() { - if(database == null) - database = getClient().getDatabase(databaseName); - return database; - } - - public void closeConnection() { - if(client != null) - client.close(); - } } \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/database/AsyncMySQL.java b/src/main/java/eu/univento/commons/database/AsyncMySQL.java index 5c1c9e7..2d550ae 100644 --- a/src/main/java/eu/univento/commons/database/AsyncMySQL.java +++ b/src/main/java/eu/univento/commons/database/AsyncMySQL.java @@ -1,5 +1,7 @@ package eu.univento.commons.database; +import lombok.Getter; + import java.sql.ResultSet; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -12,11 +14,12 @@ import java.util.function.Consumer; public class AsyncMySQL { private ExecutorService executor; - private MySQL sql; + @Getter + private MySQL mySQL; public AsyncMySQL(String host, String port, String user, String password, String database) { try { - sql = new MySQL(host, port, user, password, database); + mySQL = new MySQL(host, port, user, password, database); executor = Executors.newCachedThreadPool(); } catch (Exception e) { e.printStackTrace(); @@ -24,19 +27,15 @@ public class AsyncMySQL { } public void update(String update) { - executor.execute(() -> sql.query(update)); + executor.execute(() -> mySQL.query(update)); } public void query(String query, Consumer consumer) { executor.execute(() -> { ResultSet result = null; - result = sql.query(query); + result = mySQL.query(query); ResultSet finalResult = result; consumer.accept(finalResult); }); } - - public MySQL getMySQL() { - return sql; - } } \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/database/DatabaseManager.java b/src/main/java/eu/univento/commons/database/DatabaseManager.java index a61594d..7740e8f 100644 --- a/src/main/java/eu/univento/commons/database/DatabaseManager.java +++ b/src/main/java/eu/univento/commons/database/DatabaseManager.java @@ -2,11 +2,14 @@ package eu.univento.commons.database; import eu.univento.commons.Commons; import eu.univento.commons.configuration.ConfigurationHandler; +import lombok.Data; /** * @author joethei * @version 0.1 */ + +@Data public class DatabaseManager { private MongoDB mongoDB; @@ -22,35 +25,14 @@ public class DatabaseManager { asyncMongoDB = new AsyncMongoDB(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")); asyncMySQL = new AsyncMySQL(config.getString("MySQL.Host"), config.getString("MySQL.Port"), config.getString("MySQL.Database"), config.getString("MySQL.Username"), config.getString("MySQL.Password")); - boardSQL = new AsyncMySQL("univento.eu", "3306", "forum", "forum", "2Ogxk6$5Yvsr4*24"); - + boardSQL = new AsyncMySQL(config.getString("BoardSQL.Host"), config.getString("BoardSQL.Port"), config.getString("BoardSQL.Database"), config.getString("BoardSQL.Username"), config.getString("BoardSQL.Password")); } public void closeConnections() { mongoDB.closeConnection(); - asyncMongoDB.closeConnection(); + asyncMongoDB.getMongoDB().closeConnection(); mySQL.close(); asyncMySQL.getMySQL().close(); boardSQL.getMySQL().close(); } - - public MongoDB getMongoDB() { - return mongoDB; - } - - public AsyncMongoDB getAsyncMongoDB() { - return asyncMongoDB; - } - - public MySQL getMySQL() { - return mySQL; - } - - public AsyncMySQL getAsyncMySQL() { - return asyncMySQL; - } - - public AsyncMySQL getBoardSQL() { - return boardSQL; - } } \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/database/MySQL.java b/src/main/java/eu/univento/commons/database/MySQL.java index ba75b88..a85a665 100644 --- a/src/main/java/eu/univento/commons/database/MySQL.java +++ b/src/main/java/eu/univento/commons/database/MySQL.java @@ -18,9 +18,13 @@ public class MySQL { public MySQL(String hostname, String port, String database, String username, String password) { HikariConfig config = new HikariConfig(); - config.setJdbcUrl("jdbc:mysql://" + hostname + ":" + port + "/" + database); - config.setUsername(username); - config.setPassword(password); + config.setMaximumPoolSize(10); + config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); + config.addDataSourceProperty("serverName", hostname); + config.addDataSourceProperty("port", port); + config.addDataSourceProperty("databaseName", database); + config.addDataSourceProperty("user", username); + config.addDataSourceProperty("password", password); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); diff --git a/src/main/java/eu/univento/commons/helpers/Location.java b/src/main/java/eu/univento/commons/helpers/Location.java index a09c2e6..65bea0a 100644 --- a/src/main/java/eu/univento/commons/helpers/Location.java +++ b/src/main/java/eu/univento/commons/helpers/Location.java @@ -4,6 +4,7 @@ package eu.univento.commons.helpers; * @author joethei * @version 0.1 */ + public class Location { private final String world; diff --git a/src/main/java/eu/univento/commons/logging/LoggingHandler.java b/src/main/java/eu/univento/commons/logging/LoggingHandler.java index ec5e171..ca6286d 100644 --- a/src/main/java/eu/univento/commons/logging/LoggingHandler.java +++ b/src/main/java/eu/univento/commons/logging/LoggingHandler.java @@ -7,6 +7,7 @@ import org.apache.logging.log4j.Logger; * @author joethei * @version 0.1 */ + public class LoggingHandler { public Logger getCommons() { @@ -33,6 +34,12 @@ public class LoggingHandler { return LogManager.getLogger("univento TeamVento"); } + public Logger getCloudCommons() {return LogManager.getLogger("univento CloudCommons");} + + public Logger getCloudDaemon() {return LogManager.getLogger("univento CloudDaemon");} + + public Logger getCloudMaster() {return LogManager.getLogger("univento CloudMaster");} + public LoggingHandler() { } } \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/player/DatabasePlayer.java b/src/main/java/eu/univento/commons/player/DatabasePlayer.java index 7b762c9..6ad5fb5 100644 --- a/src/main/java/eu/univento/commons/player/DatabasePlayer.java +++ b/src/main/java/eu/univento/commons/player/DatabasePlayer.java @@ -19,6 +19,7 @@ import eu.univento.commons.player.ranking.PlayerRanking; import eu.univento.commons.player.settings.PlayerSettings; import eu.univento.commons.player.warn.WarnData; import eu.univento.commons.player.warn.WarnReason; +import lombok.Data; import org.bson.Document; import java.util.*; @@ -28,6 +29,8 @@ import java.util.stream.Collectors; * @author joethei * @version 0.1 */ + +@Data public class DatabasePlayer { private UUID uuid; @@ -35,6 +38,7 @@ public class DatabasePlayer { private final PlayerSettings settings; private final PlayerRanking ranking; private final HackData hackData; + private final Language language; private final MongoCollection playerCollection; private final MongoCollection friendCollection; @@ -50,6 +54,7 @@ public class DatabasePlayer { settings = new PlayerSettings(this); ranking = new PlayerRanking(this); hackData = new HackData(this); + language = new Language(getSettings().getLanguage()); MongoDB mongoDB = commons.getDatabaseManager().getMongoDB(); playerCollection = mongoDB.getDatabase().getCollection("players"); @@ -62,50 +67,6 @@ public class DatabasePlayer { profileCollection = mongoDB.getDatabase().getCollection("profiles"); } - public UUID getUuid() { - return uuid; - } - - public MongoCollection getPlayerCollection() { - return playerCollection; - } - - public MongoCollection getFriendCollection() { - return friendCollection; - } - - public MongoCollection getOnlinePlayerCollection() { - return onlinePlayerCollection; - } - - public MongoCollection getBanCollection() { - return banCollection; - } - - public MongoCollection getWarnCollection() { - return warnCollection; - } - - public MongoCollection getKickCollection() { - return kickCollection; - } - - public MongoCollection getMuteCollection() { - return muteCollection; - } - - public MongoCollection getProfileCollection() { - return profileCollection; - } - - public PlayerRanking getPlayerRanking() { - return ranking; - } - - public HackData getHackData() { - return hackData; - } - public Collection getProfiles() { ArrayList list = getArrayListFromDatabase("profiles"); return list.stream().map(id -> new Profile(this, id)).collect(Collectors.toCollection(LinkedList::new)); @@ -245,10 +206,6 @@ public class DatabasePlayer { return collection; } - public PlayerSettings getSettings() { - return settings; - } - public Rank getRank() { return Rank.valueOf(getStringFromDatabase("rank")); } @@ -395,10 +352,6 @@ public class DatabasePlayer { setCoins(getCoins() - coins); } - public String getMessage(String message) { - return new Language(getSettings().getLanguage()).getWord(message); - } - public boolean isOnline() { FindIterable cursor = onlinePlayerCollection.find(new Document("uuid", uuid.toString())); cursor.cursorType(CursorType.NonTailable); diff --git a/src/main/java/eu/univento/commons/player/language/Language.java b/src/main/java/eu/univento/commons/player/language/Language.java index d8e038f..015eaf8 100644 --- a/src/main/java/eu/univento/commons/player/language/Language.java +++ b/src/main/java/eu/univento/commons/player/language/Language.java @@ -24,4 +24,8 @@ public class Language { public String getWord(String keyword) { return translation.getString(keyword); } + + public String getMessage(MessageConstant constant) { + return translation.getString(constant.getName()); + } } \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/player/language/MessageConstant.java b/src/main/java/eu/univento/commons/player/language/MessageConstant.java new file mode 100644 index 0000000..18b548e --- /dev/null +++ b/src/main/java/eu/univento/commons/player/language/MessageConstant.java @@ -0,0 +1,39 @@ +package eu.univento.commons.player.language; + +/** + * @author joethei + * @version 0.1 + */ +public enum MessageConstant { + + SYSTEM_UNKOWN_ERROR("System.unknown_error"), + SYSTEM_CRITICAL_ERROR("System.critical_error"), + + SERVER_RESTART("Server.restart"), + SERVER_STOP("Server.stop"), + SERVER_RECONNECT("Server.reconnect"), + SERVER_FULL("Server.full"), + SERVER_IP("Server.ip"), + SERVER_TS_IP("Server.ts_ip"), + SERVER_BOARD_IP("Server.board_ip"), + + COMMAND_PREFIX("Command.prefix"), + COMMAND_NOT_FOUND("Command.not_found"), + COMMAND_NOT_ONLINE("Command.not_online"), + COMMAND_NO_PERMS("Command.no_perms"), + + PACK_FAILED_DOWNLOAD("Pack.failed_download"), + PACK_DECLINED("Pack.declined"), + + GAME_SPECTATE_MENU("Game.Menu.SpectatorMenu"); + + private String name; + + MessageConstant(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/player/ranking/PlayerRanking.java b/src/main/java/eu/univento/commons/player/ranking/PlayerRanking.java index 53467d2..44fc739 100644 --- a/src/main/java/eu/univento/commons/player/ranking/PlayerRanking.java +++ b/src/main/java/eu/univento/commons/player/ranking/PlayerRanking.java @@ -15,6 +15,6 @@ public class PlayerRanking { } public Ranking getRanking() { - return Ranking.getRanking(player.getIntegerFromDatabase("ranking")); + return Ranking.getFromInt(player.getIntegerFromDatabase("ranking")); } } \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/player/ranking/Ranking.java b/src/main/java/eu/univento/commons/player/ranking/Ranking.java index 7e321ae..9c38a62 100644 --- a/src/main/java/eu/univento/commons/player/ranking/Ranking.java +++ b/src/main/java/eu/univento/commons/player/ranking/Ranking.java @@ -30,7 +30,7 @@ public enum Ranking { return max; } - public static Ranking getRanking(int ranking) { + public static Ranking getFromInt(int ranking) { for(Ranking rank : values()) { if(rank.getMax() > ranking && rank.getMin() < ranking) { return rank; diff --git a/src/main/java/eu/univento/commons/player/settings/PlayerSettings.java b/src/main/java/eu/univento/commons/player/settings/PlayerSettings.java index 7ef3c44..bb3a71d 100644 --- a/src/main/java/eu/univento/commons/player/settings/PlayerSettings.java +++ b/src/main/java/eu/univento/commons/player/settings/PlayerSettings.java @@ -130,8 +130,8 @@ public class PlayerSettings { } private void setSetting(String name, Serializable setting) { - Map list = getSettings(); - list.put(name, setting); - setSettings(list); + Map map = getSettings(); + map.put(name, setting); + setSettings(map); } } \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/player/statistics/GameStats.java b/src/main/java/eu/univento/commons/player/statistics/GameStats.java new file mode 100644 index 0000000..e09eadd --- /dev/null +++ b/src/main/java/eu/univento/commons/player/statistics/GameStats.java @@ -0,0 +1,41 @@ +package eu.univento.commons.player.statistics; + +import eu.univento.commons.player.DatabasePlayer; +import eu.univento.commons.server.ServerType; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author joethei + * @version 0.1 + */ + +public class GameStats { + + + public ServerType type; + private DatabasePlayer player; + private Map stats = new HashMap<>(); + + public GameStats(ServerType type, DatabasePlayer player) { + this.type = type; + this.player = player; + stats = player.getMapFromDatabase(type.getName()); + + } + + public Long getStat(String name) { + return (Long) stats.get(name); + } + + private void setStats(Map stats) { + player.setInDatabase(type.getName() + "Stats", stats); + } + + public void setStat(String name, Long value) { + Map map = stats; + map.put(name, value); + setStats(map); + } +} \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/server/ServerType.java b/src/main/java/eu/univento/commons/server/ServerType.java new file mode 100644 index 0000000..613d230 --- /dev/null +++ b/src/main/java/eu/univento/commons/server/ServerType.java @@ -0,0 +1,34 @@ +package eu.univento.commons.server; + +/** + * @author joethei + * @version 0.1 + */ +public enum ServerType { + LOBBY(0, "Lobby", "§6Lobby"), + SECRET_GAME_FREE4ALL(1, "Free4All", "§6Free4All"), + SECRET_GAME_TRASHGAMES(2, "TrashGames", "§6TrashGames"), + GAME_WOOLGET(3, "WoolGet", "§6WoolGet"); + + private int id; + private String name; + private String prefix; + + ServerType(int id, String name, String prefix) { + this.id = id; + this.name = name; + this.prefix = prefix; + } + + public String getName() { + return name; + } + + public String getPrefix() { + return prefix; + } + + public int getID() { + return id; + } +} \ No newline at end of file diff --git a/src/main/java/eu/univento/commons/web/slack/SlackHandler.java b/src/main/java/eu/univento/commons/web/slack/SlackHandler.java new file mode 100644 index 0000000..55edceb --- /dev/null +++ b/src/main/java/eu/univento/commons/web/slack/SlackHandler.java @@ -0,0 +1,19 @@ +package eu.univento.commons.web.slack; + +import lombok.Getter; +import net.gpedro.integrations.slack.SlackApi; + +/** + * @author joethei + * @version 0.1 + */ + +public class SlackHandler { + + @Getter + private SlackApi slackAPI; + + public SlackHandler(String hook) { + slackAPI = new SlackApi(hook); + } +} \ No newline at end of file diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties index c748b66..1ebc745 100644 --- a/src/main/resources/config.properties +++ b/src/main/resources/config.properties @@ -8,3 +8,9 @@ MongoDB.Port = 27017 MongoDB.Username = admin MongoDB.Password = ++JT1415++ MongoDB.Database = admin +BoardSQL.Host = univento.eu +BoardSQL.Port = 3306 +BoardSQL.Username = forum +BoardSQL.Password = 2Ogxk6$5Yvsr4*24 +BoardSQL.Database = forum +Slack.hook_url = https://hooks.slack.com/services/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 \ No newline at end of file