Search in sources :

Example 16 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class HttpHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    response.setStatus(200);
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Headers", "Authorization, *");
    if (request.getMethod().equals("OPTIONS")) {
        response.setStatus(204);
        baseRequest.setHandled(true);
        return;
    }
    String auth = request.getHeader("Authorization");
    String path = target.substring("/api".length());
    logger.info("target = " + target);
    logger.info("auth = " + auth);
    for (Method method : HttpHandler.class.getDeclaredMethods()) {
        Route a = method.getAnnotation(Route.class);
        if (a == null)
            continue;
        Matcher matcher = Pattern.compile(a.route()).matcher(path);
        if (matcher.find() && baseRequest.getMethod().equals(a.method())) {
            baseRequest.setHandled(true);
            if (a.needsCookie() && (auth == null || auth.isEmpty())) {
                response.getWriter().println(new JSONObject().put("error", "Missing authorization"));
                return;
            }
            List<String> l = new ArrayList<>(matcher.groupCount());
            for (int i = 1; i <= matcher.groupCount(); i++) {
                l.add(matcher.group(i));
            }
            try {
                if (l.size() > 0)
                    method.invoke(null, auth, request, response, l);
                else
                    method.invoke(null, auth, request, response);
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}
Also used : JSONObject(org.jsolf.JSONObject) Matcher(java.util.regex.Matcher) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 17 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class ResistanceCommand method process.

@Override
public void process(GuildCommandEvent e) {
    Translation type = e.getArguments().getTranslation("type");
    if (type.isFromType(Translation.Type.TYPE)) {
        JSONObject json = getTypeJSON();
        HashMap<String, Integer> map = new HashMap<>();
        StringBuilder b = new StringBuilder();
        JSONObject typejson = json.getJSONObject(type.getTranslation()).getJSONObject("damageTaken");
        typejson.keySet().forEach(str -> {
            int damageTaken = typejson.getInt(str);
            if (damageTaken > 1) {
                Translation t = (Translation) Translation.Type.TYPE.validate(str, Translation.Language.GERMAN, "default");
                if (t != null) {
                    if (damageTaken == 3)
                        b.append("**");
                    b.append(t.getTranslation());
                    if (damageTaken == 3)
                        b.append("**");
                    b.append("\n");
                }
            }
        });
        if (b.length() == 0) {
            e.reply("Es wurden keine Typen gefunden, die diesen Typ resistieren! (Das ist für normal ein Fehler, Flo wurde kontaktiert)");
            Command.sendToMe("ResistanceCommand ListSize 0");
            return;
        }
        e.reply("Folgende Typen resistiert " + type.getOtherLang() + ":\n" + b);
    } else {
        e.reply("Macht Flo noch irgendwann :)");
    }
}
Also used : JSONObject(org.jsolf.JSONObject) HashMap(java.util.HashMap)

Example 18 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class ShinyCommand method process.

@Override
public void process(GuildCommandEvent e) {
    String suffix;
    ArgumentManager args = e.getArguments();
    String monname = e.getArguments().getTranslation("mon").getTranslation();
    JSONObject mon = getDataJSON().getJSONObject(toSDName(monname));
    if (args.has("regform")) {
        String form = args.getText("regform");
        suffix = "-" + form.toLowerCase();
    } else {
        suffix = "";
    }
    if (args.has("form")) {
        String form = args.getText("form");
        if (!mon.has("otherFormes")) {
            e.reply(monname + " besitzt keine **" + form + "**-Form!");
            return;
        }
        JSONArray otherFormes = mon.getJSONArray("otherFormes");
        if (otherFormes.toList().stream().noneMatch(s -> ((String) s).toLowerCase().endsWith("-" + form.toLowerCase()))) {
            e.reply(monname + " besitzt keine **" + form + "**-Form!");
            return;
        }
        if (suffix.equals(""))
            suffix = "-";
        suffix += form.toLowerCase();
    }
    File f = new File("../Showdown/sspclient/sprites/gen5-shiny/" + monname.toLowerCase() + suffix + ".png");
    if (!f.exists()) {
        e.reply(mon + " hat keine " + args.getText("form") + "-Form!");
    }
    // if(!f.exists()) f = new File("../Showdown/sspclient/sprites/gen5-shiny/" + mon.split(";")[1].toLowerCase() + ".png");
    e.getChannel().sendFile(f).queue();
}
Also used : JSONObject(org.jsolf.JSONObject) JSONArray(org.jsolf.JSONArray) File(java.io.File)

Example 19 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class SmogonCommand method process.

@Override
public void process(GuildCommandEvent e) throws IOException {
    TextChannel tco = e.getChannel();
    Message m = e.getMessage();
    String msg = m.getContentDisplay();
    Member member = e.getMember();
    Document wiki;
    ArgumentManager args = e.getArguments();
    String name = args.getTranslation("mon").getTranslation();
    String form = args.has("form") ? "-" + args.getText("form").toLowerCase() : "";
    Document d = Jsoup.connect("https://www.smogon.com/dex/ss/pokemon/" + name.toLowerCase() + form + "/").get();
    JSONObject obj = new JSONObject(d.select("script").get(1).data().trim().substring("dexSettings = ".length())).getJSONArray("injectRpcs").getJSONArray(2).getJSONObject(1);
    if (obj.getJSONArray("strategies").length() == 0) {
        try {
            d = Jsoup.connect("https://www.smogon.com/dex/sm/pokemon/" + name.toLowerCase() + form + "/").get();
            obj = new JSONObject(d.select("script").get(1).data().trim().substring("dexSettings = ".length())).getJSONArray("injectRpcs").getJSONArray(2).getJSONObject(1);
            tco.sendMessage("Gen 7:").queue();
        } catch (Exception ex) {
            tco.sendMessage("Es gibt kein aktuelles Moveset für dieses Pokemon!").queue();
            return;
        }
    }
    JSONArray arr = obj.getJSONArray("strategies");
    SmogonSet smogon = new SmogonSet(arr);
    // noinspection ResultOfMethodCallIgnored
    e.reply(smogon.buildMessage(), ma -> ma.setActionRows(smogon.buildActionRows()), null, mes -> Command.smogonMenu.put(mes.getIdLong(), smogon), null);
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) Message(net.dv8tion.jda.api.entities.Message) JSONObject(org.jsolf.JSONObject) SmogonSet(de.tectoast.emolga.selectmenus.selectmenusaves.SmogonSet) JSONArray(org.jsolf.JSONArray) Document(org.jsoup.nodes.Document) Member(net.dv8tion.jda.api.entities.Member) IOException(java.io.IOException)

Example 20 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class WhatLearnCommand method process.

@Override
public void process(GuildCommandEvent e) {
    ArgumentManager args = e.getArguments();
    String mon = toSDName(args.getTranslation("mon").getTranslation() + args.getOrDefault("form", ""));
    String type = args.getText("type");
    int gen = args.getOrDefault("gen", 8);
    JSONObject learnset = getLearnsetJSON(getModByGuild(e)).getJSONObject(mon).getJSONObject("learnset");
    LinkedList<String> list = new LinkedList<>();
    HashMap<Integer, List<String>> levels = new HashMap<>();
    boolean b = banane(learnset, gen, type, levels, list);
    if (!b)
        banane(learnset, --gen, type, levels, list);
    String send;
    if (type.equals("L")) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i <= 100; i++) {
            if (levels.containsKey(i)) {
                levels.get(i).sort(null);
                str.append("L").append(i).append(": ").append(String.join(", ", levels.get(i))).append("\n");
            }
        }
        send = str.toString();
    } else {
        if (list.isEmpty()) {
            e.reply("Dieses Pokemon kann in Generation " + gen + " auf diese Art keine Attacken lernen!");
            return;
        }
        ArrayList<String> set = new ArrayList<>(new HashSet<>(list));
        set.sort(null);
        send = String.join("\n", set);
    }
    e.reply(new EmbedBuilder().setColor(Color.CYAN).setDescription(send).setTitle("Attacken").build());
    if (e.getMember().getIdLong() == 598199247124299776L)
        e.reply("Hier noch ein Keks für dich :3");
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) JSONObject(org.jsolf.JSONObject) List(java.util.List)

Aggregations

JSONObject (org.jsolf.JSONObject)72 TextChannel (net.dv8tion.jda.api.entities.TextChannel)23 Member (net.dv8tion.jda.api.entities.Member)20 Command (de.tectoast.emolga.commands.Command)19 JSONArray (org.jsolf.JSONArray)19 RequestBuilder (de.tectoast.emolga.utils.RequestBuilder)16 CommandCategory (de.tectoast.emolga.commands.CommandCategory)15 GuildCommandEvent (de.tectoast.emolga.commands.GuildCommandEvent)15 Draft (de.tectoast.emolga.utils.draft.Draft)14 Guild (net.dv8tion.jda.api.entities.Guild)13 DraftPokemon (de.tectoast.emolga.utils.draft.DraftPokemon)12 Collectors (java.util.stream.Collectors)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Tierlist (de.tectoast.emolga.utils.draft.Tierlist)10 Coord (de.tectoast.emolga.utils.records.Coord)9 Draft.getIndex (de.tectoast.emolga.utils.draft.Draft.getIndex)8 ResultSet (java.sql.ResultSet)8 java.util (java.util)8 ArrayList (java.util.ArrayList)7