Search in sources :

Example 1 with Character

use of data.Character in project KaellyBot by Kaysoro.

the class WhoisCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Matcher m = getMatcher(message);
        m.find();
        Language lg = Translator.getLanguageFrom(message.getChannel());
        String pseudo = m.group(1).trim().toLowerCase();
        StringBuilder url;
        try {
            url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "character.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            ExceptionManager.manageException(e, message, this, lg);
            return false;
        }
        if (m.group(2) != null) {
            String serverName = m.group(2).trim().toLowerCase();
            List<ServerDofus> result = new ArrayList<>();
            for (ServerDofus server : ServerDofus.getServersDofus()) if (server.getName().toLowerCase().startsWith(serverName))
                result.add(server);
            if (result.size() == 1)
                url.append("&").append(forServer).append(result.get(0).getId());
            else {
                if (!result.isEmpty())
                    tooMuchServers.throwException(message, this, lg);
                else
                    notFoundServer.throwException(message, this, lg);
                return false;
            }
        }
        try {
            Document doc = JSoupManager.getDocument(url.toString());
            Elements elems = doc.getElementsByClass("ak-bg-odd");
            elems.addAll(doc.getElementsByClass("ak-bg-even"));
            if (!elems.isEmpty()) {
                // on boucle jusqu'à temps de trouver le bon personnage (ie le plus proche du nom donnée)
                List<String> result = new ArrayList<>();
                List<String> servers = new ArrayList<>();
                for (Element element : elems) if (pseudo.equals(element.child(1).text().trim().toLowerCase())) {
                    result.add(element.child(1).select("a").attr("href"));
                    servers.add(element.child(element.children().size() - 2).text());
                }
                if (result.size() == 1) {
                    Connection.Response response = JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + result.get(0));
                    if (!response.url().getPath().endsWith(Translator.getLabel(lg, "whois.request"))) {
                        Character characPage = Character.getCharacter(Translator.getLabel(lg, "game.url") + result.get(0), lg);
                        Message.sendEmbed(message.getChannel(), characPage.getEmbedObject(lg));
                    } else
                        characterTooOld.throwException(message, this, lg);
                } else if (result.size() > 1)
                    tooMuchCharacters.throwException(message, this, lg, servers);
                else
                    notFoundCharacter.throwException(message, this, lg);
            } else
                notFoundCharacter.throwException(message, this, lg);
        } catch (IOException e) {
            ExceptionManager.manageIOException(e, message, this, lg, characterPageInaccessible);
        } catch (Exception e) {
            ExceptionManager.manageException(e, message, this, lg);
        }
    }
    return false;
}
Also used : Character(data.Character) Matcher(java.util.regex.Matcher) ServerDofus(data.ServerDofus) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Connection(org.jsoup.Connection) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) Exception(java.lang.Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Language(enums.Language)

Aggregations

Character (data.Character)1 ServerDofus (data.ServerDofus)1 Language (enums.Language)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Exception (java.lang.Exception)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Connection (org.jsoup.Connection)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1