+ added async MongoDB

+ added Slack Integration
+ added HikariCP
+ better i18n Message Support
+ added player ranking
+ added Game stats
This commit is contained in:
Johannes Theiner 2016-10-10 13:21:12 +02:00 committed by BuildTools
parent 1fa03cb94a
commit 1badee9881
19 changed files with 230 additions and 180 deletions

View File

@ -12,12 +12,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.16.8" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.2.2" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-async:3.2.2" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.2.2" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:bson:3.2.2" level="project" />
<orderEntry type="library" name="Maven: org.mongodb.morphia:morphia:1.2.1" level="project" />
<orderEntry type="library" name="Maven: cglib:cglib-nodep:2.2.2" level="project" />
<orderEntry type="library" name="Maven: com.thoughtworks.proxytoys:proxytoys:1.0" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:2.4.5" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.16" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-all:4.0.36.Final" level="project" />
@ -26,10 +20,12 @@
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
<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: 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: joda-time:joda-time:2.9.4" level="project" />
<orderEntry type="library" name="Maven: com.github.theholywaffle:teamspeak3-api:1.0.14-SNAPSHOT" level="project" />
<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: net.gpedro.integrations.slack:slack-webhook:1.2.1" level="project" />
</component>
</module>

51
pom.xml
View File

@ -69,6 +69,17 @@
</plugins>
</build>
<repositories>
<repository>
<id>TeamSpeak-3-Java-API-mvn-repo</id>
<url>https://raw.githubusercontent.com/TheHolyWaffle/TeamSpeak-3-Java-API/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
@ -80,16 +91,6 @@
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mongodb.morphia</groupId>
<artifactId>morphia</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
@ -115,16 +116,6 @@
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
@ -140,5 +131,25 @@
<artifactId>joda-time</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.github.theholywaffle</groupId>
<artifactId>teamspeak3-api</artifactId>
<version>[1.0.0,2.0.0)</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>net.gpedro.integrations.slack</groupId>
<artifactId>slack-webhook</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
</project>

View File

@ -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() {

View File

@ -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<ServerAddress> addressList = new ArrayList<>();
addressList.add(new ServerAddress(host, port));
List<MongoCredential> 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();
}
}

View File

@ -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<ResultSet> 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;
}
}

View File

@ -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;
}
}

View File

@ -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");

View File

@ -4,6 +4,7 @@ package eu.univento.commons.helpers;
* @author joethei
* @version 0.1
*/
public class Location {
private final String world;

View File

@ -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() {
}
}

View File

@ -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<Document> playerCollection;
private final MongoCollection<Document> 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<Document> getPlayerCollection() {
return playerCollection;
}
public MongoCollection<Document> getFriendCollection() {
return friendCollection;
}
public MongoCollection<Document> getOnlinePlayerCollection() {
return onlinePlayerCollection;
}
public MongoCollection<Document> getBanCollection() {
return banCollection;
}
public MongoCollection<Document> getWarnCollection() {
return warnCollection;
}
public MongoCollection<Document> getKickCollection() {
return kickCollection;
}
public MongoCollection<Document> getMuteCollection() {
return muteCollection;
}
public MongoCollection<Document> getProfileCollection() {
return profileCollection;
}
public PlayerRanking getPlayerRanking() {
return ranking;
}
public HackData getHackData() {
return hackData;
}
public Collection<Profile> getProfiles() {
ArrayList<String> 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<Document> cursor = onlinePlayerCollection.find(new Document("uuid", uuid.toString()));
cursor.cursorType(CursorType.NonTailable);

View File

@ -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());
}
}

View File

@ -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;
}
}

View File

@ -15,6 +15,6 @@ public class PlayerRanking {
}
public Ranking getRanking() {
return Ranking.getRanking(player.getIntegerFromDatabase("ranking"));
return Ranking.getFromInt(player.getIntegerFromDatabase("ranking"));
}
}

View File

@ -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;

View File

@ -130,8 +130,8 @@ public class PlayerSettings {
}
private void setSetting(String name, Serializable setting) {
Map<String, Object> list = getSettings();
list.put(name, setting);
setSettings(list);
Map<String, Object> map = getSettings();
map.put(name, setting);
setSettings(map);
}
}

View File

@ -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<String, Object> 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<String, Object> stats) {
player.setInDatabase(type.getName() + "Stats", stats);
}
public void setStat(String name, Long value) {
Map<String, Object> map = stats;
map.put(name, value);
setStats(map);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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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