From 2f27625bcfe1b5dad0cd7e3eafd175cc634dcb8d Mon Sep 17 00:00:00 2001 From: KevDaDev <65958288+KevinDaGame@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:58:15 +0200 Subject: [PATCH] Implement advanced online players chart (#210) * Implement advanced online players chart * Implement test * Document function --- .../voxelsniper/SpigotVoxelSniper.java | 4 ++- .../bstats/ServerSizeCategoryCounter.kt | 35 +++++++++++++++++++ .../bstats/ServerSizeCategoryCounterTest.kt | 28 +++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounter.kt create mode 100644 VoxelSniperSpigot/src/test/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounterTest.kt diff --git a/VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/SpigotVoxelSniper.java b/VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/SpigotVoxelSniper.java index b22fd995..83eb055e 100644 --- a/VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/SpigotVoxelSniper.java +++ b/VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/SpigotVoxelSniper.java @@ -14,6 +14,7 @@ import com.github.kevindagame.voxelsniper.fileHandler.VoxelSniperConfiguration; import com.github.kevindagame.voxelsniper.integration.bstats.BrushUsageCounter; import com.github.kevindagame.voxelsniper.integration.bstats.BrushUsersCounter; +import com.github.kevindagame.voxelsniper.integration.bstats.ServerSizeCategoryCounter; import com.github.kevindagame.voxelsniper.integration.plotsquared.PlotSquaredIntegration; import com.github.kevindagame.voxelsniper.integration.worldguard.WorldGuardIntegration; import com.github.kevindagame.voxelsniper.material.SpigotMaterial; @@ -24,6 +25,7 @@ import com.github.kevindagame.voxelsniper.world.SpigotWorld; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bstats.bukkit.Metrics; +import org.bstats.charts.DrilldownPie; import org.bstats.charts.SimplePie; import org.bstats.charts.SingleLineChart; import org.bukkit.*; @@ -112,8 +114,8 @@ public void onEnable() { metrics.addCustomChart(new SimplePie("worldguard_integration", () -> WorldGuardIntegration.Companion.getEnabled() ? "enabled" : "disabled")); metrics.addCustomChart(new SimplePie("plotsquared_integration", () -> PlotSquaredIntegration.Companion.getEnabled() ? "enabled" : "disabled")); metrics.addCustomChart(new SingleLineChart("total_brush_uses_in_last_30_minutes", BrushUsageCounter::getTotalBrushUses)); -// metrics.addCustomChart(new Metrics.MultiLineChart("uses_per_brush", BrushUsageCounter::getUsagePerBrush)); metrics.addCustomChart(new SingleLineChart("total_snipers", BrushUsersCounter.Companion::getTotalBrushUses)); + metrics.addCustomChart(new DrilldownPie("server_size_category_counter", () -> ServerSizeCategoryCounter.INSTANCE.getData(getServer().getOnlinePlayers().size()))); // Do update check if (voxelSniperConfiguration.isUpdateCheckerEnabled()) { diff --git a/VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounter.kt b/VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounter.kt new file mode 100644 index 00000000..175c25f3 --- /dev/null +++ b/VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounter.kt @@ -0,0 +1,35 @@ +package com.github.kevindagame.voxelsniper.integration.bstats + +object ServerSizeCategoryCounter { + private val categories = listOf( + ServerSizeCategory("0", 0, 0), + ServerSizeCategory("1-10", 1, 10), + ServerSizeCategory("11-20", 11, 20), + ServerSizeCategory("21-50", 21, 50), + ServerSizeCategory("51-100", 51, 100), + ServerSizeCategory("101-200", 101, 200), + ServerSizeCategory("201-500", 201, 500), + ServerSizeCategory("501-1000", 501, 1000), + ServerSizeCategory("More than 1000", 1001, Int.MAX_VALUE), + + + ) + + /** + * Gets the data for the server size category chart. + * @param playerCount The amount of players online on the server. + * @return The data for the server size category chart. + * The data consists of a map with the label of the category, and a map with the exact value. The bstats docs do not explain the hardcoded 1. I assume this means that it's either 1 deep or a weight of 1 + */ + fun getData(playerCount: Int): Map> { + val map = mutableMapOf>() + val entries = mutableMapOf() + val category = categories.find { playerCount in it.min..it.max } + entries[playerCount.toString()] = 1 + map[category!!.label] = entries + + return map + } +} + +data class ServerSizeCategory(val label: String, val min: Int, val max: Int) \ No newline at end of file diff --git a/VoxelSniperSpigot/src/test/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounterTest.kt b/VoxelSniperSpigot/src/test/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounterTest.kt new file mode 100644 index 00000000..02b13e69 --- /dev/null +++ b/VoxelSniperSpigot/src/test/java/com/github/kevindagame/voxelsniper/integration/bstats/ServerSizeCategoryCounterTest.kt @@ -0,0 +1,28 @@ +package com.github.kevindagame.voxelsniper.integration.bstats + +import org.junit.Assert +import org.junit.Test + +class ServerSizeCategoryCounterTest { + + @Test + fun testGetData() { + val testData = listOf( + 0 to "0", + 5 to "1-10", + 15 to "11-20", + 30 to "21-50", + 75 to "51-100", + 150 to "101-200", + 300 to "201-500", + 750 to "501-1000", + 2000 to "More than 1000" + ) + + for ((playerCount, expectedCategory) in testData) { + val result = ServerSizeCategoryCounter.getData(playerCount) + val categoryMap = result[expectedCategory] + Assert.assertEquals(1, categoryMap?.get(playerCount.toString())) + } + } +} \ No newline at end of file