+ asynchronous statistics cache for games

This commit is contained in:
Johannes Theiner 2017-06-04 23:54:32 +02:00
parent 5db8688a1e
commit 1de1adb47c
30 changed files with 217 additions and 104 deletions

View File

@ -44,16 +44,17 @@
<orderEntry type="library" name="Maven: org.mongodb:bson: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: 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.rabbitmq:amqp-client:3.6.5" 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.slf4j:slf4j-api:1.7.25" 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.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.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: com.google.guava:guava:19.0" level="project" />
<orderEntry type="library" name="Maven: com.github.ben-manes.caffeine:caffeine:2.5.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.1" 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: 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: 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: 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" />
<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: 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:simple:0.28" level="project" />
<orderEntry type="library" name="Maven: org.ejml:core:0.28" level="project" /> <orderEntry type="library" name="Maven: org.ejml:core:0.28" level="project" />

30
pom.xml
View File

@ -135,6 +135,7 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.14.8</version> <version>1.14.8</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.vertx</groupId> <groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId> <artifactId>vertx-core</artifactId>
@ -161,6 +162,16 @@
<version>3.4.1</version> <version>3.4.1</version>
</dependency> </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> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
@ -182,6 +193,13 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>19.0</version> <version>19.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.5.2</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
@ -192,6 +210,7 @@
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.5</version> <version>2.5</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
@ -202,16 +221,7 @@
<artifactId>TeamSpeak-3-Java-API</artifactId> <artifactId>TeamSpeak-3-Java-API</artifactId>
<version>v1.0.13</version> <version>v1.0.13</version>
</dependency> </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> <dependency>
<groupId>com.github.nsp</groupId> <groupId>com.github.nsp</groupId>
<artifactId>JSkills</artifactId> <artifactId>JSkills</artifactId>

View File

@ -1,9 +1,16 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons; package eu.univento.commons;
import eu.univento.commons.configuration.ConfigurationHandler; import eu.univento.commons.configuration.ConfigurationHandler;
import eu.univento.commons.database.DatabaseManager; import eu.univento.commons.database.DatabaseManager;
import eu.univento.commons.logging.LoggingHandler; import eu.univento.commons.logging.LoggingHandler;
import eu.univento.commons.messaging.MessageHandler;
import eu.univento.commons.messaging.MessagingManager; import eu.univento.commons.messaging.MessagingManager;
import eu.univento.commons.player.statistics.GameStatistics;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import lombok.Getter; import lombok.Getter;
@ -22,6 +29,7 @@ public class Commons {
private ConfigurationHandler configurationHandler; private ConfigurationHandler configurationHandler;
private LoggingHandler loggingHandler; private LoggingHandler loggingHandler;
private Vertx vertx; private Vertx vertx;
private GameStatistics gameStatistics;
public Commons() { public Commons() {
commons = this; commons = this;
@ -30,6 +38,9 @@ public class Commons {
messagingManager = new MessagingManager(); messagingManager = new MessagingManager();
databaseManager = new DatabaseManager(); databaseManager = new DatabaseManager();
loggingHandler = new LoggingHandler(); loggingHandler = new LoggingHandler();
gameStatistics = new GameStatistics();
Thread.setDefaultUncaughtExceptionHandler((t, e) -> MessageHandler.sendMessage("log.Exceptions", e.getMessage()));
} }
public void shutdown() { public void shutdown() {

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.configuration; package eu.univento.commons.configuration;
import java.io.IOException; import java.io.IOException;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.database; package eu.univento.commons.database;
import eu.univento.commons.Commons; import eu.univento.commons.Commons;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.database; package eu.univento.commons.database;
import eu.univento.commons.Commons; import eu.univento.commons.Commons;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.database; package eu.univento.commons.database;
import eu.univento.commons.Commons; import eu.univento.commons.Commons;

View File

@ -7,7 +7,7 @@ package eu.univento.commons.event;
/** /**
* @author joethei * @author joethei
* @version 0.1 * @version 1.0
*/ */
public interface MessageEvent { public interface MessageEvent {

View File

@ -1,5 +1,11 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.helpers; package eu.univento.commons.helpers;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
/** /**
@ -8,6 +14,7 @@ import lombok.Getter;
*/ */
@Getter @Getter
@AllArgsConstructor
public class Location { public class Location {
private final String world; private final String world;
@ -16,13 +23,4 @@ public class Location {
private final double z; private final double z;
private final float pitch; private final float pitch;
private final float yaw; private final float yaw;
public Location(String world, double x, double y, double z, float pitch, float yaw) {
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.pitch = pitch;
this.yaw = yaw;
}
} }

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.logging; package eu.univento.commons.logging;
import eu.univento.commons.messaging.MessageHandler; import eu.univento.commons.messaging.MessageHandler;
@ -12,7 +17,7 @@ public class LoggingHandler {
public LoggingHandler() {} public LoggingHandler() {}
public void log(ServerType type, String message) { public void log(String message) {
MessageHandler.sendMessage("log." + type.getName(), message); MessageHandler.sendMessage("log." + ServerType.getServerType().getName(), message);
} }
} }

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player; package eu.univento.commons.player;
import eu.univento.commons.Commons; import eu.univento.commons.Commons;
@ -75,7 +80,6 @@ public class DatabasePlayer {
getObjectFromDatabase("ranking").whenComplete((entries, throwable) -> getObjectFromDatabase("ranking").whenComplete((entries, throwable) ->
future.complete(Ranking.valueOf(entries.getString("ranking")))); future.complete(Ranking.valueOf(entries.getString("ranking"))));
return future; return future;
} }
public void ban(BanReason reason, UUID banner, String customMessage, String proof) { public void ban(BanReason reason, UUID banner, String customMessage, String proof) {

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.ban; package eu.univento.commons.player.ban;
import eu.univento.commons.player.uuid.NameFetcher; import eu.univento.commons.player.uuid.NameFetcher;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.ban; package eu.univento.commons.player.ban;
/** /**

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.friend; package eu.univento.commons.player.friend;
import eu.univento.commons.player.uuid.NameFetcher; import eu.univento.commons.player.uuid.NameFetcher;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.kick; package eu.univento.commons.player.kick;
import eu.univento.commons.player.uuid.NameFetcher; import eu.univento.commons.player.uuid.NameFetcher;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.kick; package eu.univento.commons.player.kick;
/** /**

View File

@ -17,7 +17,7 @@ import java.util.Locale;
/** /**
* @author joethei * @author joethei
* @version 0.1 * @version 1.5
*/ */
public class Language { public class Language {

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.language; package eu.univento.commons.player.language;
import lombok.Getter; import lombok.Getter;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.mute; package eu.univento.commons.player.mute;
import eu.univento.commons.player.uuid.NameFetcher; import eu.univento.commons.player.uuid.NameFetcher;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.mute; package eu.univento.commons.player.mute;
/** /**

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.ranking; package eu.univento.commons.player.ranking;
/** /**

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.settings; package eu.univento.commons.player.settings;
import eu.univento.commons.player.DatabasePlayer; import eu.univento.commons.player.DatabasePlayer;

View File

@ -1,15 +0,0 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.statistics;
/**
* @author joethei
* @version 0.1
*/
public interface GameStat {
//@Getter ServerType type = null;
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.statistics;
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import eu.univento.commons.Commons;
import eu.univento.commons.player.DatabasePlayer;
import io.vertx.core.json.JsonObject;
import lombok.Getter;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* @author joethei
* @version 1.0
*/
@Getter
public class GameStatistics {
private AsyncLoadingCache<DatabasePlayer, ImpactStatistics> impact;
public GameStatistics() {
impact = Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(5, TimeUnit.MINUTES).buildAsync((player, executor) -> {
CompletableFuture<ImpactStatistics> future = new CompletableFuture<>();
Commons.getCommons().getDatabaseManager().getMySQL().getClient().getConnection(res -> {
if(res.failed()) res.cause().printStackTrace();
res.result().query("SELECT * FROM Stats_Impact WHERE UUID='" + player.getUuid() + "';", result -> {
if(result.failed()) result.cause().printStackTrace();
JsonObject json = result.result().getNext().toJson();
future.complete(new ImpactStatistics(json.getInteger("gamesPlayed"), json.getInteger("gamesWon"), json.getInteger("gamesLost"), json.getInteger("kills"), json.getInteger("deaths")));
});
});
return future;
});
}
}

View File

@ -1,41 +0,0 @@
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;
player.getObjectFromDatabase(type.getName()).whenComplete((entries, throwable) -> {
//TODO: add stuff here or change the entire stats system
});
}
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,25 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.player.statistics;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author joethei
* @version 1.0
*/
@Getter
@AllArgsConstructor
public class ImpactStatistics {
private int gamesPlayed;
private int gamesWon;
private int gamesLost;
private int kills;
private int deaths;
}

View File

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

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.server; package eu.univento.commons.server;
import eu.univento.commons.event.MessageEvent; import eu.univento.commons.event.MessageEvent;
@ -18,6 +23,10 @@ import java.util.regex.Pattern;
@Getter @Getter
public class ServerInfo { public class ServerInfo {
private String name;
private ServerType type;
private int players;
public static CompletableFuture<ServerInfo> getServerInfo(String server) { public static CompletableFuture<ServerInfo> getServerInfo(String server) {
CompletableFuture<ServerInfo> future = new CompletableFuture<>(); CompletableFuture<ServerInfo> future = new CompletableFuture<>();
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
@ -39,8 +48,4 @@ public class ServerInfo {
return future; return future;
} }
private String name;
private ServerType type;
private int players;
} }

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.commons.server; package eu.univento.commons.server;
import lombok.Getter; import lombok.Getter;
@ -29,5 +34,10 @@ public enum ServerType {
this.name = name; this.name = name;
this.prefix = prefix; this.prefix = prefix;
} }
@Getter @Setter private static ServerType serverType; @Setter private static ServerType serverType;
public static ServerType getServerType() {
if(serverType == null) return ServerType.NONE;
return serverType;
}
} }

View File

@ -1,12 +0,0 @@
<?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>