Search in sources :

Example 1 with User

use of com.osu.api.ciyfhx.User in project MantaroBot by Mantaro.

the class OsuStatsCmd method osustats.

@Command
public static void osustats(CommandRegistry cr) {
    cr.register("osustats", new SimpleCommand(Category.GAMES) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            String noArgs = content.split(" ")[0];
            TextChannelGround.of(event).dropItemWithChance(4, 5);
            switch(noArgs) {
                case "best":
                    event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Retrieving information from osu! server...").queue(sentMessage -> {
                        Future<String> task = threadpool.submit(() -> best(content));
                        try {
                            sentMessage.editMessage(task.get(16, TimeUnit.SECONDS)).queue();
                        } catch (Exception e) {
                            if (e instanceof TimeoutException) {
                                task.cancel(true);
                                sentMessage.editMessage(EmoteReference.ERROR + "Request timeout. Maybe osu! API is slow?").queue();
                            } else
                                log.warn("Exception thrown while fetching data", e);
                        }
                    });
                    break;
                case "recent":
                    event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Retrieving information from server...").queue(sentMessage -> {
                        Future<String> task = threadpool.submit(() -> recent(content));
                        try {
                            sentMessage.editMessage(task.get(16, TimeUnit.SECONDS)).queue();
                        } catch (Exception e) {
                            if (e instanceof TimeoutException) {
                                task.cancel(true);
                                sentMessage.editMessage(EmoteReference.ERROR + "Request timeout. Maybe osu! API is slow?").queue();
                            } else
                                log.warn("Exception thrown while fetching data", e);
                        }
                    });
                    break;
                case "user":
                    event.getChannel().sendMessage(user(content)).queue();
                    break;
                default:
                    onError(event);
                    break;
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "osu! command").setDescription("**Retrieves information from the osu!API**.").addField("Usage", "`~>osustats best <player>` - **Retrieves best scores of the user specified in the specified gamemode**.\n" + "`~>osustats recent <player>` - **Retrieves recent scores of the user specified in the specified gamemode.**\n" + "`~>osustats user <player>` - **Retrieves information about a osu! player**.\n", false).addField("Parameters", "`player` - **The osu! player to look info for.**", false).build();
        }
    });
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Color(java.awt.Color) UserScore(com.osu.api.ciyfhx.UserScore) Mod(com.osu.api.ciyfhx.Mod) java.util.concurrent(java.util.concurrent) DecimalFormat(java.text.DecimalFormat) Module(net.kodehawa.mantarobot.modules.Module) HashMap(java.util.HashMap) Category(net.kodehawa.mantarobot.modules.commands.base.Category) User(com.osu.api.ciyfhx.User) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Slf4j(lombok.extern.slf4j.Slf4j) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONException(org.json.JSONException) List(java.util.List) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) Map(java.util.Map) MantaroData(net.kodehawa.mantarobot.data.MantaroData) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) OsuClient(com.osu.api.ciyfhx.OsuClient) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) OsuMod(net.kodehawa.mantarobot.commands.osu.OsuMod) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONException(org.json.JSONException) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 2 with User

use of com.osu.api.ciyfhx.User in project MantaroBot by Mantaro.

the class OsuStatsCmd method best.

private static String best(String content) {
    String finalResponse;
    try {
        long start = System.currentTimeMillis();
        String beheaded1 = content.replace("best ", "");
        String[] args = beheaded1.split(" ");
        map.put("m", 0);
        User hey = osuClient.getUser(args[0], map);
        List<UserScore> userBest = osuClient.getUserBest(hey, map);
        StringBuilder sb = new StringBuilder();
        List<String> best = new CopyOnWriteArrayList<>();
        int n1 = 0;
        DecimalFormat df = new DecimalFormat("####0.0");
        for (UserScore userScore : userBest) {
            if (n1 > 9)
                break;
            if (userScore.getEnabledMods().size() > 0) {
                List<Mod> mods = userScore.getEnabledMods();
                StringBuilder sb1 = new StringBuilder();
                mods.forEach(mod -> sb1.append(OsuMod.get(mod).getAbbreviation()));
                mods1 = " Mods: " + sb1.toString();
            }
            best.add(String.format("# %s -> %s\n | ###### |  [%spp] -> Rank: %s\n | (★%s) - %s | Date: %s -> Max Combo: %d\n", userScore.getBeatMap().getTitle().replace("'", ""), mods1, df.format(userScore.getPP()), userScore.getRank(), df.format(userScore.getBeatMap().getDifficultyRating()), userScore.getBeatMap().getCreator(), userScore.getDate(), userScore.getMaxCombo()));
            sb.append(best.get(n1));
            n1++;
        }
        long end = System.currentTimeMillis() - start;
        finalResponse = "```md\n" + sb.toString() + " \n<Response time: " + end + "ms>```";
    } catch (Exception e) {
        if (e instanceof JSONException)
            finalResponse = EmoteReference.ERROR + "No results found.";
        else {
            finalResponse = EmoteReference.ERROR + "Error while looking for results.";
            log.warn("Error retrieving results from osu!API", e);
        }
    }
    return finalResponse;
}
Also used : User(com.osu.api.ciyfhx.User) Mod(com.osu.api.ciyfhx.Mod) OsuMod(net.kodehawa.mantarobot.commands.osu.OsuMod) DecimalFormat(java.text.DecimalFormat) JSONException(org.json.JSONException) JSONException(org.json.JSONException) UserScore(com.osu.api.ciyfhx.UserScore)

Example 3 with User

use of com.osu.api.ciyfhx.User in project MantaroBot by Mantaro.

the class OsuStatsCmd method user.

private static MessageEmbed user(String content) {
    MessageEmbed finalMessage;
    try {
        long start = System.currentTimeMillis();
        String beheaded1 = content.replace("user ", "");
        String[] args = beheaded1.split(" ");
        map.put("m", 0);
        User osuClientUser = osuClient.getUser(args[0], map);
        //For accuracy
        DecimalFormat dfa = new DecimalFormat("####0.00");
        //For everything else
        DecimalFormat df = new DecimalFormat("####0");
        long end = System.currentTimeMillis() - start;
        EmbedBuilder builder = new EmbedBuilder();
        builder.setAuthor("osu! statistics for " + osuClientUser.getUsername(), "https://osu.ppy.sh/" + osuClientUser.getUserID(), "https://a.ppy.sh/" + osuClientUser.getUserID()).setColor(Color.GRAY).addField("Rank", "#" + df.format(osuClientUser.getPPRank()), true).addField(":flag_" + osuClientUser.getCountry().toLowerCase() + ": Country Rank", "#" + df.format(osuClientUser.getPPCountryRank()), true).addField("PP", df.format(osuClientUser.getPPRaw()) + "pp", true).addField("Accuracy", dfa.format(osuClientUser.getAccuracy()) + "%", true).addField("Level", df.format(osuClientUser.getLevel()), true).addField("Ranked Score", df.format(osuClientUser.getRankedScore()), true).addField("SS", df.format(osuClientUser.getCountRankSS()), true).addField("S", df.format(osuClientUser.getCountRankS()), true).addField("A", df.format(osuClientUser.getCountRankA()), true).setFooter("Response time: " + end + "ms.", null);
        finalMessage = builder.build();
    } catch (Exception e) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setTitle("Error.", null).setColor(Color.RED).addField("Description", "Error retrieving results or no results found. (" + e.getMessage() + ")", false);
        finalMessage = builder.build();
    }
    return finalMessage;
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) User(com.osu.api.ciyfhx.User) DecimalFormat(java.text.DecimalFormat) JSONException(org.json.JSONException)

Example 4 with User

use of com.osu.api.ciyfhx.User in project MantaroBot by Mantaro.

the class OsuStatsCmd method recent.

private static String recent(String content) {
    String finalMessage;
    try {
        long start = System.currentTimeMillis();
        String beheaded1 = content.replace("recent ", "");
        String[] args = beheaded1.split(" ");
        map.put("m", 0);
        User hey = osuClient.getUser(args[0], map);
        List<UserScore> userRecent = osuClient.getUserRecent(hey, map);
        StringBuilder sb = new StringBuilder();
        List<String> recent = new CopyOnWriteArrayList<>();
        int n1 = 0;
        DecimalFormat df = new DecimalFormat("####0.0");
        for (UserScore u : userRecent) {
            if (n1 > 9)
                break;
            n1++;
            if (u.getEnabledMods().size() > 0) {
                List<Mod> mods = u.getEnabledMods();
                StringBuilder sb1 = new StringBuilder();
                mods.forEach(mod -> sb1.append(OsuMod.get(mod).getAbbreviation()));
                mods1 = " Mods: " + sb1.toString();
            }
            recent.add(String.format("# %s -> %s\n | (★%s) - %s | Date: %s -> Max Combo: %d\n", u.getBeatMap().getTitle().replace("'", ""), mods1, df.format(u.getBeatMap().getDifficultyRating()), u.getBeatMap().getCreator(), u.getDate(), u.getMaxCombo()));
        }
        recent.forEach(sb::append);
        long end = System.currentTimeMillis() - start;
        finalMessage = "```md\n" + sb.toString() + " \n<Response time: " + end + "ms>```";
    } catch (Exception e) {
        if (e instanceof JSONException)
            finalMessage = EmoteReference.ERROR + "No results found.";
        else {
            finalMessage = EmoteReference.ERROR + "Error while looking for results.";
            log.warn("Error retrieving results from osu!API", e);
        }
    }
    return finalMessage;
}
Also used : User(com.osu.api.ciyfhx.User) Mod(com.osu.api.ciyfhx.Mod) OsuMod(net.kodehawa.mantarobot.commands.osu.OsuMod) DecimalFormat(java.text.DecimalFormat) JSONException(org.json.JSONException) JSONException(org.json.JSONException) UserScore(com.osu.api.ciyfhx.UserScore)

Aggregations

User (com.osu.api.ciyfhx.User)4 DecimalFormat (java.text.DecimalFormat)4 JSONException (org.json.JSONException)4 Mod (com.osu.api.ciyfhx.Mod)3 UserScore (com.osu.api.ciyfhx.UserScore)3 OsuMod (net.kodehawa.mantarobot.commands.osu.OsuMod)3 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)2 OsuClient (com.osu.api.ciyfhx.OsuClient)1 Color (java.awt.Color)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 java.util.concurrent (java.util.concurrent)1 Slf4j (lombok.extern.slf4j.Slf4j)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 TextChannelGround (net.kodehawa.mantarobot.commands.currency.TextChannelGround)1 MantaroData (net.kodehawa.mantarobot.data.MantaroData)1 Command (net.kodehawa.mantarobot.modules.Command)1 CommandRegistry (net.kodehawa.mantarobot.modules.CommandRegistry)1