|
| 1 | +package net.javadiscord.javabot.systems.help.commands; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.image.BufferedImage; |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.time.LocalDate; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Comparator; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Map.Entry; |
| 15 | +import java.util.PriorityQueue; |
| 16 | + |
| 17 | +import javax.imageio.ImageIO; |
| 18 | + |
| 19 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 20 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 21 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
| 22 | +import net.dv8tion.jda.api.utils.FileUpload; |
| 23 | +import net.javadiscord.javabot.systems.help.dao.HelpTransactionRepository; |
| 24 | +import net.javadiscord.javabot.systems.help.dao.HelpTransactionRepository.MonthInYear; |
| 25 | +import net.javadiscord.javabot.systems.help.model.HelpAccount; |
| 26 | +import net.javadiscord.javabot.util.ExceptionLogger; |
| 27 | +import net.javadiscord.javabot.util.Pair; |
| 28 | +import net.javadiscord.javabot.util.Plotter; |
| 29 | +import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand; |
| 30 | + |
| 31 | +/** |
| 32 | + * Shows the distribution of help XP per user. |
| 33 | + */ |
| 34 | +public class HelpStatisticsSubcommand extends SlashCommand.Subcommand { |
| 35 | + |
| 36 | + private static final List<Pair<String, Color>> COLORS = List.of( |
| 37 | + new Pair<>("Red", Color.RED), new Pair<>("Blue", Color.BLUE), new Pair<>("Yellow", Color.YELLOW), |
| 38 | + new Pair<>("Green", Color.GREEN), new Pair<>("Cyan", Color.CYAN), new Pair<>("Magenta", Color.MAGENTA), |
| 39 | + new Pair<>("Orange", Color.ORANGE), new Pair<>("Pink", Color.PINK), new Pair<>("Light gray", Color.LIGHT_GRAY) |
| 40 | + ); |
| 41 | + |
| 42 | + private final HelpTransactionRepository transactionRepository; |
| 43 | + |
| 44 | + public HelpStatisticsSubcommand(HelpTransactionRepository transactionRepository) { |
| 45 | + this.transactionRepository = transactionRepository; |
| 46 | + setCommandData(new SubcommandData("stats", "Shows an general plot about help activity in this server")); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void execute(SlashCommandInteractionEvent event) { |
| 51 | + |
| 52 | + event.deferReply().queue(); |
| 53 | + |
| 54 | + List<Pair<MonthInYear,HelpAccount>> transactionWeights = transactionRepository.getTotalTransactionWeightByMonthAndUsers(LocalDate.now().withDayOfMonth(1).minusYears(1).atStartOfDay()); |
| 55 | + |
| 56 | + Map<Long, Pair<String, Color>> topUsersToColors = mapTopUsersToColors(transactionWeights); |
| 57 | + |
| 58 | + List<Pair<String, Plotter.Bar>> plotData = new ArrayList<>(); |
| 59 | + |
| 60 | + int i = 0; |
| 61 | + |
| 62 | + for(LocalDate position = LocalDate.now().minusYears(1); position.isBefore(LocalDate.now().plusDays(1)); position=position.plusMonths(1)) { |
| 63 | + List<Pair<Color,Double>> entriesForThisMonth = new ArrayList<>(); |
| 64 | + boolean correctMonth = true; |
| 65 | + while(i<transactionWeights.size() && correctMonth) { |
| 66 | + Pair<MonthInYear,HelpAccount> entry = transactionWeights.get(i); |
| 67 | + if(entry.first().month() == position.getMonthValue() && entry.first().year() == position.getYear()) { |
| 68 | + Long userId = entry.second().getUserId(); |
| 69 | + Color color = topUsersToColors.getOrDefault(userId, new Pair<String, Color>(null, Color.GRAY)).second(); |
| 70 | + entriesForThisMonth.add(new Pair<>(color, entry.second().getExperience())); |
| 71 | + i++; |
| 72 | + }else { |
| 73 | + correctMonth = false; |
| 74 | + } |
| 75 | + } |
| 76 | + plotData.add(new Pair<>(position.getMonth() + " " + position.getYear(), new Plotter.Bar(entriesForThisMonth))); |
| 77 | + } |
| 78 | + |
| 79 | + BufferedImage plot = new Plotter(plotData, "General helper statistics").plot(); |
| 80 | + try(ByteArrayOutputStream os = new ByteArrayOutputStream()){ |
| 81 | + ImageIO.write(plot, "png", os); |
| 82 | + FileUpload upload = FileUpload.fromData(os.toByteArray(), "image.png"); |
| 83 | + EmbedBuilder eb = new EmbedBuilder() |
| 84 | + .setTitle("Help XP distribution") |
| 85 | + .setDescription("This plot shows how much help XP have been awarded to different helpers.") |
| 86 | + .setImage("attachment://"+upload.getName()); |
| 87 | + for (Entry<Long, Pair<String, Color>> entry : topUsersToColors.entrySet()) { |
| 88 | + eb.addField(entry.getValue().first(), "<@"+entry.getKey()+">", true); |
| 89 | + } |
| 90 | + event.getHook().sendMessageEmbeds(eb |
| 91 | + .build()).addFiles(upload).queue(); |
| 92 | + } catch (IOException e) { |
| 93 | + ExceptionLogger.capture(e, "Cannot create XP plot"); |
| 94 | + event.getHook().sendMessage("An error occured.").queue(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private Map<Long, Pair<String, Color>> mapTopUsersToColors(List<Pair<MonthInYear,HelpAccount>> transactionWeights) { |
| 99 | + Map<Long, Double> totalByUser = new HashMap<>(); |
| 100 | + for (Pair<MonthInYear,HelpAccount> pair : transactionWeights) { |
| 101 | + totalByUser.merge(pair.second().getUserId(), pair.second().getExperience(), (a,b) -> a+b); |
| 102 | + } |
| 103 | + |
| 104 | + PriorityQueue<Pair<Long, Double>> topUserQueue = new PriorityQueue<>(Comparator.comparingDouble(Pair::second)); |
| 105 | + for (Entry<Long, Double> e : totalByUser.entrySet()) { |
| 106 | + topUserQueue.add(new Pair<>(e.getKey(), e.getValue())); |
| 107 | + if(topUserQueue.size()>COLORS.size()) { |
| 108 | + topUserQueue.remove(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + List<Long> topUsers = new ArrayList<>(); |
| 113 | + while(!topUserQueue.isEmpty()) { |
| 114 | + topUsers.add(topUserQueue.remove().first()); |
| 115 | + } |
| 116 | + Map<Long, Pair<String, Color>> topUsersToColors = new LinkedHashMap<>(); |
| 117 | + for(int i=0;i<topUsers.size();i++) { |
| 118 | + topUsersToColors.put(topUsers.get(topUsers.size() - i - 1), COLORS.get(i)); |
| 119 | + } |
| 120 | + return topUsersToColors; |
| 121 | + } |
| 122 | +} |
0 commit comments