Log player versions (#22)
* add simplified versions to MinecraftVersions * simplify playerversion command * add playerversion logger * make requested changes
This commit is contained in:
parent
b880857703
commit
2068aebd52
|
@ -52,7 +52,7 @@ public class MiscCommands implements Commands {
|
|||
@Command(
|
||||
aliases = { "playerversion", "pv" },
|
||||
desc = "Shows statics on what version players online are using",
|
||||
flags = "a",
|
||||
flags = "ad",
|
||||
min = 0,
|
||||
max = 1
|
||||
)
|
||||
|
@ -60,24 +60,22 @@ public class MiscCommands implements Commands {
|
|||
public void listPlayerVersions(final CommandContext args, final CommandSender sender) throws CommandException {
|
||||
Audience audience = audiences.get(sender);
|
||||
if (args.hasFlag('a')) {
|
||||
Map<Integer, Integer> playerCountVersionMap = new HashMap<>();
|
||||
Stream<Player> players = userStore.stream();
|
||||
players.forEach(player -> {
|
||||
int version = player.getProtocolVersion();
|
||||
playerCountVersionMap.put(version, (playerCountVersionMap.containsKey(version) ? playerCountVersionMap.get(version) : 0) + 1);
|
||||
Map<String, Integer> playerCountVersionMap = new HashMap<>();
|
||||
userStore.stream().forEach(player -> {
|
||||
String version = MinecraftVersion.describeProtocol(player.getProtocolVersion(), args.hasFlag('d'));
|
||||
playerCountVersionMap.put(version, playerCountVersionMap.getOrDefault(version, 0) + 1);
|
||||
});
|
||||
playerCountVersionMap.size();
|
||||
|
||||
audience.sendMessage(new HeaderComponent(new Component(ChatColor.AQUA).translate("list.player.versions.title")));
|
||||
for (Map.Entry<Integer, Integer> entry: playerCountVersionMap.entrySet()) {
|
||||
for (Map.Entry<String, Integer> entry : playerCountVersionMap.entrySet()) {
|
||||
audience.sendMessage(new TranslatableComponent("list.player.versions.message." + (entry.getValue() == 1 ? "singular" : "plural"),
|
||||
ChatColor.AQUA + entry.getValue().toString(),
|
||||
ChatColor.AQUA + MinecraftVersion.describeProtocol(entry.getKey()),
|
||||
String.format("%.1f", 100 * entry.getValue() / (double)userStore.count()) + "%"));
|
||||
ChatColor.AQUA + entry.getKey(),
|
||||
String.format("%.1f", 100 * entry.getValue() / (double) userStore.count()) + "%"));
|
||||
}
|
||||
} else {
|
||||
Player player = CommandUtils.getPlayerOrSelf(args, sender, 0);
|
||||
audience.sendMessage(new TranslatableComponent("list.player.version.singular.message", new PlayerComponent(identityProvider.createIdentity(player)), ChatColor.AQUA + MinecraftVersion.describeProtocol(player.getProtocolVersion())));
|
||||
audience.sendMessage(new TranslatableComponent("list.player.version.singular.message", new PlayerComponent(identityProvider.createIdentity(player)), ChatColor.AQUA + MinecraftVersion.describeProtocol(player.getProtocolVersion(), false)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,8 +8,9 @@ import tc.oc.pgm.match.inject.MatchScoped;
|
|||
public class MatchAnalyticsManifest extends HybridManifest {
|
||||
@Override
|
||||
protected void configure() {
|
||||
new PluginFacetBinder(binder())
|
||||
.register(MatchPlayerReporter.class);
|
||||
final PluginFacetBinder facets = new PluginFacetBinder(binder());
|
||||
facets.register(MatchPlayerReporter.class);
|
||||
facets.register(PlayerVersionLogger.class);
|
||||
|
||||
bind(MatchTagger.class).in(MatchScoped.class);
|
||||
expose(MatchTagger.class);
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
package tc.oc.pgm.analytics;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import tc.oc.api.minecraft.users.UserStore;
|
||||
import tc.oc.commons.core.plugin.PluginFacet;
|
||||
import tc.oc.commons.core.scheduler.Scheduler;
|
||||
import tc.oc.commons.core.scheduler.Task;
|
||||
import tc.oc.minecraft.api.entity.Player;
|
||||
import tc.oc.minecraft.protocol.MinecraftVersion;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Singleton
|
||||
public class PlayerVersionLogger implements PluginFacet {
|
||||
|
||||
private final UserStore<Player> userStore;
|
||||
private final Scheduler scheduler;
|
||||
|
||||
private File playerVersionLogFile;
|
||||
private Task task;
|
||||
|
||||
@Inject PlayerVersionLogger(UserStore<Player> userStore, Scheduler scheduler) {
|
||||
this.userStore = userStore;
|
||||
this.scheduler = scheduler;
|
||||
|
||||
playerVersionLogFile = new File("player_versions.log");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
task = this.scheduler.createRepeatingTask(Duration.ofMinutes(15), Duration.ofMinutes(15), this::logPlayerVersions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
task.cancel();
|
||||
}
|
||||
|
||||
public void logPlayerVersions() {
|
||||
Map<String, Integer> playerCountVersionMap = new HashMap<>();
|
||||
IntStream.rangeClosed(7, 12).forEach(v -> playerCountVersionMap.put("1." + v, 0));
|
||||
userStore.stream().forEach(player -> {
|
||||
String version = MinecraftVersion.describeProtocol(player.getProtocolVersion(), true);
|
||||
playerCountVersionMap.put(version, playerCountVersionMap.getOrDefault(version, 0) + 1);
|
||||
});
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("['").append(new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date())).append("'");
|
||||
|
||||
playerCountVersionMap.entrySet().stream()
|
||||
.sorted(Comparator.comparingInt(e -> Integer.parseInt(e.getKey().split("\\.")[1])))
|
||||
.forEach( entry -> builder.append(", '").append(entry.getValue()).append("'"));
|
||||
builder.append("]\n");
|
||||
|
||||
try {
|
||||
FileUtils.writeStringToFile(playerVersionLogFile, builder.toString(), true);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -7,24 +7,28 @@ import com.google.common.collect.ImmutableMap;
|
|||
|
||||
public enum MinecraftVersion {
|
||||
|
||||
MINECRAFT_1_7_2(4, "1.7.2"),
|
||||
MINECRAFT_1_7_10(5, "1.7.10"),
|
||||
MINECRAFT_1_8(47, "1.8"),
|
||||
MINECRAFT_1_9(107, "1.9"),
|
||||
MINECRAFT_1_9_1(108, "1.9.1"),
|
||||
MINECRAFT_1_9_2(109, "1.9.2"),
|
||||
MINECRAFT_1_9_4(110, "1.9.4"),
|
||||
MINECRAFT_1_10(210, "1.10"),
|
||||
MINECRAFT_1_11(315, "1.11"),
|
||||
MINECRAFT_1_11_1(316, "1.11.1"),
|
||||
MINECRAFT_1_12(335, "1.12");
|
||||
MINECRAFT_1_7_2(4, 1, 7, 2),
|
||||
MINECRAFT_1_7_10(5, 1, 7, 10),
|
||||
MINECRAFT_1_8(47, 1, 8, 0),
|
||||
MINECRAFT_1_9(107, 1, 9, 0),
|
||||
MINECRAFT_1_9_1(108, 1, 9 , 1),
|
||||
MINECRAFT_1_9_2(109, 1, 9 , 2),
|
||||
MINECRAFT_1_9_4(110, 1, 9 , 4),
|
||||
MINECRAFT_1_10(210, 1, 10, 0),
|
||||
MINECRAFT_1_11(315, 1, 11, 0),
|
||||
MINECRAFT_1_11_1(316, 1, 11, 1),
|
||||
MINECRAFT_1_12(335, 1, 12, 0);
|
||||
|
||||
private final int protocol;
|
||||
private final String version;
|
||||
private final int major;
|
||||
private final int minor;
|
||||
private final int patch;
|
||||
|
||||
MinecraftVersion(int protocol, String version) {
|
||||
MinecraftVersion(int protocol, int major, int minor, int patch) {
|
||||
this.protocol = protocol;
|
||||
this.version = version;
|
||||
this.major = major;
|
||||
this.minor = minor;
|
||||
this.patch = patch;
|
||||
}
|
||||
|
||||
public int protocol() {
|
||||
|
@ -32,13 +36,21 @@ public enum MinecraftVersion {
|
|||
}
|
||||
|
||||
public String version() {
|
||||
return version;
|
||||
return major + "." + minor + (patch != 0 ? "." + patch : "");
|
||||
}
|
||||
|
||||
public String simplifiedVersion() {
|
||||
return major + "." + minor;
|
||||
}
|
||||
|
||||
public static String describeProtocol(int protocol) {
|
||||
return describeProtocol(protocol, false);
|
||||
}
|
||||
|
||||
public static String describeProtocol(int protocol, boolean simplified) {
|
||||
final MinecraftVersion mv = byProtocol(protocol);
|
||||
return mv != null ? mv.version()
|
||||
: "unknown." + protocol;
|
||||
return mv != null ? (simplified ? mv.simplifiedVersion() : mv.version())
|
||||
: "unknown." + protocol;
|
||||
}
|
||||
|
||||
public static @Nullable MinecraftVersion byProtocol(int protocol) {
|
||||
|
|
Loading…
Reference in New Issue