Skip to content

Commit

Permalink
Implement advanced online players chart (#210)
Browse files Browse the repository at this point in the history
* Implement advanced online players chart

* Implement test

* Document function
  • Loading branch information
KevinDaGame authored Jul 24, 2023
1 parent 7570ba8 commit 2f27625
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Map<String, Int>> {
val map = mutableMapOf<String, Map<String, Int>>()
val entries = mutableMapOf<String, Int>()
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)
Original file line number Diff line number Diff line change
@@ -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()))
}
}
}

0 comments on commit 2f27625

Please sign in to comment.