From e10a8d652d47ad452bc8f2551a77a35930224f96 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Wed, 16 Jan 2019 19:49:05 +0100 Subject: [PATCH] Add //rank command --- .../net/Broken/Commands/Over18/Madame.java | 2 +- src/main/java/net/Broken/Commands/Rank.java | 30 ++++++++++++ .../UserManager/Stats/UserStatsUtils.java | 46 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/Broken/Commands/Rank.java diff --git a/src/main/java/net/Broken/Commands/Over18/Madame.java b/src/main/java/net/Broken/Commands/Over18/Madame.java index e31894c..e9d6ecc 100644 --- a/src/main/java/net/Broken/Commands/Over18/Madame.java +++ b/src/main/java/net/Broken/Commands/Over18/Madame.java @@ -17,7 +17,7 @@ import java.io.IOException; /** * Madame command that return random picture from dites.bonjourmadame.fr */ -//@NoDev +@NoDev public class Madame extends NumberedCommande { Logger logger = LogManager.getLogger(); MessageReceivedEvent event; diff --git a/src/main/java/net/Broken/Commands/Rank.java b/src/main/java/net/Broken/Commands/Rank.java new file mode 100644 index 0000000..eba463d --- /dev/null +++ b/src/main/java/net/Broken/Commands/Rank.java @@ -0,0 +1,30 @@ +package net.Broken.Commands; + +import net.Broken.Commande; +import net.Broken.Tools.UserManager.Stats.UserStatsUtils; +import net.dv8tion.jda.core.entities.MessageEmbed; +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; + +public class Rank implements Commande { + @Override + public void action(String[] args, MessageReceivedEvent event) { + UserStatsUtils userStats = UserStatsUtils.getINSTANCE(); + MessageEmbed msg = userStats.getRankMessage(event.getMember()); + event.getTextChannel().sendMessage(msg).queue(); + } + + @Override + public boolean isPrivateUsable() { + return false; + } + + @Override + public boolean isAdminCmd() { + return false; + } + + @Override + public boolean isNSFW() { + return false; + } +} diff --git a/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java b/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java index f834673..9d194fe 100644 --- a/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java +++ b/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java @@ -6,16 +6,20 @@ import net.Broken.DB.Repository.UserRepository; import net.Broken.DB.Repository.UserStatsRepository; import net.Broken.MainBot; import net.Broken.SpringContext; +import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; import net.Broken.Tools.UserManager.UserUtils; +import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Member; +import net.dv8tion.jda.core.entities.MessageEmbed; import net.dv8tion.jda.core.entities.User; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.security.crypto.password.PasswordEncoder; +import java.awt.*; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; @@ -204,6 +208,48 @@ public class UserStatsUtils { } + + public MessageEmbed getRankMessage(Member member){ + UserStats userStats = getGuildUserStats(member); + GuildStatsPack pack = getStatPack(userStats.getUser(), member.getGuild().getId()); + StringBuilder stringBuilder = new StringBuilder(); + int i = 1; + for(GuildStats stats : pack.ranking){ + if( i >= 6){ + break; + } + stringBuilder.append(i).append(". ").append(stats.userName).append(" with ").append(stats.total).append(" points!").append("\n"); + i++; + + } + + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setColor(Color.yellow); + embedBuilder.setTitle(member.getGuild().getName() + " Ranking"); + embedBuilder.addField("Top 5:", stringBuilder.toString(), false); + String rank; + switch (pack.selfStats.rank){ + case 1: + rank = "1st"; + break; + case 2: + rank = "2nd"; + break; + case 3: + rank = "3rd"; + break; + default: + rank = pack.selfStats.rank + "th"; + break; + } + + + embedBuilder.addField("Your stats:", rank + " with " + pack.selfStats.total + " points", false); + embedBuilder.addField("More stats:", "https://" + MainBot.url+"/rank", false); + return EmbedMessageUtils.buildStandar(embedBuilder); + + } + public static class VoicePresenceCounter extends Thread{ private Member member; public VoicePresenceCounter(Member member){