Skip to content

Commit 2f27625

Browse files
authored
Implement advanced online players chart (#210)
* Implement advanced online players chart * Implement test * Document function
1 parent 7570ba8 commit 2f27625

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

VoxelSniperSpigot/src/main/java/com/github/kevindagame/voxelsniper/SpigotVoxelSniper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.github.kevindagame.voxelsniper.fileHandler.VoxelSniperConfiguration;
1515
import com.github.kevindagame.voxelsniper.integration.bstats.BrushUsageCounter;
1616
import com.github.kevindagame.voxelsniper.integration.bstats.BrushUsersCounter;
17+
import com.github.kevindagame.voxelsniper.integration.bstats.ServerSizeCategoryCounter;
1718
import com.github.kevindagame.voxelsniper.integration.plotsquared.PlotSquaredIntegration;
1819
import com.github.kevindagame.voxelsniper.integration.worldguard.WorldGuardIntegration;
1920
import com.github.kevindagame.voxelsniper.material.SpigotMaterial;
@@ -24,6 +25,7 @@
2425
import com.github.kevindagame.voxelsniper.world.SpigotWorld;
2526
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2627
import org.bstats.bukkit.Metrics;
28+
import org.bstats.charts.DrilldownPie;
2729
import org.bstats.charts.SimplePie;
2830
import org.bstats.charts.SingleLineChart;
2931
import org.bukkit.*;
@@ -112,8 +114,8 @@ public void onEnable() {
112114
metrics.addCustomChart(new SimplePie("worldguard_integration", () -> WorldGuardIntegration.Companion.getEnabled() ? "enabled" : "disabled"));
113115
metrics.addCustomChart(new SimplePie("plotsquared_integration", () -> PlotSquaredIntegration.Companion.getEnabled() ? "enabled" : "disabled"));
114116
metrics.addCustomChart(new SingleLineChart("total_brush_uses_in_last_30_minutes", BrushUsageCounter::getTotalBrushUses));
115-
// metrics.addCustomChart(new Metrics.MultiLineChart("uses_per_brush", BrushUsageCounter::getUsagePerBrush));
116117
metrics.addCustomChart(new SingleLineChart("total_snipers", BrushUsersCounter.Companion::getTotalBrushUses));
118+
metrics.addCustomChart(new DrilldownPie("server_size_category_counter", () -> ServerSizeCategoryCounter.INSTANCE.getData(getServer().getOnlinePlayers().size())));
117119

118120
// Do update check
119121
if (voxelSniperConfiguration.isUpdateCheckerEnabled()) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.kevindagame.voxelsniper.integration.bstats
2+
3+
object ServerSizeCategoryCounter {
4+
private val categories = listOf(
5+
ServerSizeCategory("0", 0, 0),
6+
ServerSizeCategory("1-10", 1, 10),
7+
ServerSizeCategory("11-20", 11, 20),
8+
ServerSizeCategory("21-50", 21, 50),
9+
ServerSizeCategory("51-100", 51, 100),
10+
ServerSizeCategory("101-200", 101, 200),
11+
ServerSizeCategory("201-500", 201, 500),
12+
ServerSizeCategory("501-1000", 501, 1000),
13+
ServerSizeCategory("More than 1000", 1001, Int.MAX_VALUE),
14+
15+
16+
)
17+
18+
/**
19+
* Gets the data for the server size category chart.
20+
* @param playerCount The amount of players online on the server.
21+
* @return The data for the server size category chart.
22+
* 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
23+
*/
24+
fun getData(playerCount: Int): Map<String, Map<String, Int>> {
25+
val map = mutableMapOf<String, Map<String, Int>>()
26+
val entries = mutableMapOf<String, Int>()
27+
val category = categories.find { playerCount in it.min..it.max }
28+
entries[playerCount.toString()] = 1
29+
map[category!!.label] = entries
30+
31+
return map
32+
}
33+
}
34+
35+
data class ServerSizeCategory(val label: String, val min: Int, val max: Int)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.kevindagame.voxelsniper.integration.bstats
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class ServerSizeCategoryCounterTest {
7+
8+
@Test
9+
fun testGetData() {
10+
val testData = listOf(
11+
0 to "0",
12+
5 to "1-10",
13+
15 to "11-20",
14+
30 to "21-50",
15+
75 to "51-100",
16+
150 to "101-200",
17+
300 to "201-500",
18+
750 to "501-1000",
19+
2000 to "More than 1000"
20+
)
21+
22+
for ((playerCount, expectedCategory) in testData) {
23+
val result = ServerSizeCategoryCounter.getData(playerCount)
24+
val categoryMap = result[expectedCategory]
25+
Assert.assertEquals(1, categoryMap?.get(playerCount.toString()))
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)