added statistics appliance in craftattack
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package eu.mhsl.craftattack.spawn.craftattack.appliances.tooling.statistics;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.Main;
|
||||
import eu.mhsl.craftattack.spawn.core.api.server.HttpServer;
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Statistics extends Appliance {
|
||||
record StatisticsResponse(List<PlayerStatistics> playerStatistics) {
|
||||
record PlayerStatistics(String player, Map<MaterialStatistic, Integer> values) {
|
||||
}
|
||||
}
|
||||
|
||||
record MaterialStatistic(String name, String material) {
|
||||
}
|
||||
|
||||
record StatisticsRequest(List<MaterialStatistic> categories) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void httpApi(HttpServer.ApiBuilder apiBuilder) {
|
||||
apiBuilder.post(
|
||||
"getStatistics",
|
||||
StatisticsRequest.class,
|
||||
(statistics, request) -> {
|
||||
Main.instance().getLogger().info("API requested statistics");
|
||||
List<StatisticsResponse.PlayerStatistics> statisticsList = Arrays.stream(Bukkit.getOfflinePlayers())
|
||||
.parallel()
|
||||
.map(player -> new StatisticsResponse.PlayerStatistics(
|
||||
player.getUniqueId().toString(),
|
||||
statistics.categories().stream()
|
||||
.map(s -> new AbstractMap.SimpleEntry<>(s, s.material().isBlank()
|
||||
? player.getStatistic(Statistic.valueOf(s.name))
|
||||
: player.getStatistic(Statistic.valueOf(s.name), Material.valueOf(s.material()))
|
||||
))
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue
|
||||
))
|
||||
))
|
||||
.toList();
|
||||
return new StatisticsResponse(statisticsList);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user