emptydev / SE Forum - TOP Manager

// ==UserScript==
// @name         SE Forum - TOP Manager
// @namespace    169058880cdfb527a5b82c8240e36f01
// @version      0.3
// @description  Attack TOP Manager
// @author       Magnus Man
// @match        https://forum.sysemp.com/index.php?topic=*
// @grant        none
// @require      https://code.jquery.com/jquery-latest.js
// @downloadURL  https://openuserjs.org/install/emptydev/SE_Forum_-_TOP_Manager.user.js
// @updateURL    https://openuserjs.org/meta/emptydev/SE_Forum_-_TOP_Manager.meta.js
// @license      MIT
// @copyright 2020, emptydev (https://openuserjs.org/users/emptydev)
// ==/UserScript==

(function() {
    'use strict';
    function getNumber(value) {
        return parseInt(value.toString().replace(/\./g, '')) || 0;
    }

    var checkTop = $("<input />", { "type":"button"}).css({
        "background":"#c74646",
        "margin-left":"10px",
        "font-size":"11px"
    }).val("Verificar TOP");

    var clearTop = $("<input />", { "type":"button"}).css({
        "background":"#123271",
        "margin-left":"10px",
        "font-size":"11px"
    }).val("Limpar dados").on("click", function() {
        localStorage.removeItem("topsNaves");
        localStorage.removeItem("topsVeiculos");
    });

    function nf(amount, decimalCount = 0, decimal = ".", thousands = ".") {
        try {
            decimalCount = Math.abs(decimalCount);
            decimalCount = isNaN(decimalCount) ? 2 : decimalCount;

            const negativeSign = amount < 0 ? "-" : "";

            let i = parseInt(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)).toString();
            let j = (i.length > 3) ? i.length % 3 : 0;

            return amount == 0 ? '0' : negativeSign + (j ? i.substr(0, j) + thousands : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands) + (decimalCount ? decimal + Math.abs(amount - i).toFixed(decimalCount).slice(2) : "");
        } catch (e) {
            console.log("");
        }
    }

    function findByText(text) {
        var value = 0;

        $(".post .inner").find("span:contains('"+text+"')").each(function() {
            if($(this).closest(".bbc_standard_quote").find(":contains('Lucro Vagas não Incluídas')").length == 0) {
                value += getNumber($(this).nextAll("span.bbc_color:eq(0)").text());
            }
        });

        return value;
    }

    checkTop.on("click", function() {
        var veiculo = $(".post .inner").find(":contains('Veículo')").length > 0;
        var isTopSection = $(".navigate_section ul").find("li:contains('TOP')").length > 0;

        var topsNaves = JSON.parse(localStorage.getItem("topsNaves") || '{"top1":{"G":{"value":"","url":""},"GR":{"value":"","url":""},"D":{"value":"","url":""}},"top2":{"G":{"value":"","url":""},"GR":{"value":"","url":""},"D":{"value":"","url":""}},"top3":{"G":{"value":"","url":""},"GR":{"value":"","url":""},"D":{"value":"","url":""}}}');
        var topsNavesAC = JSON.parse(localStorage.getItem("topsNavesAC") || '{"top1":{"G":{"value":"","url":""},"GR":{"value":"","url":""},"D":{"value":"","url":""}},"top2":{"G":{"value":"","url":""},"GR":{"value":"","url":""},"D":{"value":"","url":""}},"top3":{"G":{"value":"","url":""},"GR":{"value":"","url":""},"D":{"value":"","url":""}}}');
        var topsVeiculos = JSON.parse(localStorage.getItem("topsVeiculos") || '{"top1":{"G":{"value":"","url":""},"D":{"value":"","url":""}},"top2":{"G":{"value":"","url":""},"D":{"value":"","url":""}},"top3":{"G":{"value":"","url":""},"D":{"value":"","url":""}}}');

        var url = window.location.href;
        url = url.substring(url.indexOf("?topic="), url.length);
        url = url.substring(0, url.indexOf(".") > -1 ? url.indexOf(".") : url.length);

        var isAc = $(".navigate_section").find("a:contains('Conjuntos')").length > 0;

        var newText = [];
        var message = "";
        if(!veiculo) {
            if(!isAc) {
                //var g = findByText("Lucro:");
                //var gr = findByText("Lucro + Destroços:");
                //var d = findByText("Perdas:");

                var reciclado = $(".post .inner").first().find("div.quoteheader:contains('Reciclagem')").length;

                //message += ("------------------- <b>LUCRO</b> -------------------") + "<br />";

                var g = 0;
                var gVagas = 0;
                $(".post .inner").first().find("span:contains('Lucro:')").each(function(i,e) {
                    let v = parseInt($(this).nextAll("span.bbc_color:eq(0)").text().replace(/[^1234567890-]/g, ''));
                    g += v;
                    if(i > 0) {
                        gVagas += v;
                    }
                    if(i == 1) {
                        //message += ("------------------- <b>VAGAS</b> -------------------") + "<br />";
                    }
                    //message += ("Lucro: " + nf(v)) + "<br />";
                });

                //message += ("------------------- <b>LUCRO TOTAL</b> -------------------") + "<br />";
                //message += ("Lucro: " + nf(g) + "<br />");
                //message += ("Lucro vagas: " + nf(gVagas)) + "<br /><br />";


                // message += ("------------------- <b>PERDAS</b> -------------------") + "<br />";
                var d = 0;
                var dVagas = 0;
                $(".post .inner").first().find("span:contains('Perdas:')").each(function(i,e) {
                    let v = parseInt($(this).nextAll("span.bbc_color:eq(0)").text().replace(/[^1234567890-]/g, ''));
                    d += v;
                    if(i > 0) {
                        dVagas += v;
                    }
                    if(i == 1) {
                        //message += ("------------------- <b>VAGAS</b> -------------------") + "<br />";
                    }
                    //message += ("Perdas: " + nf(v) +  "<br />");
                });
                //message += ("------------------- <b>PERDAS TOTAIS</b> -------------------") + "<br />";
                //message += "Perdas: " + nf(d) + "<br />"
                //message += ("Perdas vagas: " + nf(dVagas)) + "<br /><br />";

                // message += ("-------------------------------------------------") + "<br />";

                var gr = 0;
                var grVagas = 0;
                // message += ("------------------- <b>LUCRO + DESTROÇOS</b> -------------------") + "<br />";
                $(".post .inner").first().find("span:contains('Lucro + Destroços:')").each(function(i,e) {
                    let v = parseInt($(this).nextAll("span.bbc_color:eq(0)").text().replace(/[^1234567890-]/g, ''));
                    gr += v;
                    if(i > 0) {
                        grVagas += v;
                    }
                    if(i == 1) {
                        //message += ("------------------- <b>VAGAS</b> -------------------") + "<br />";
                    }
                    //message += ("Lucro + Destroços: " + nf(v) + "<br />");
                });
                //message += ("------------------- <b>LUCRO + DESTROÇOS TOTAIS</b> -------------------") + "<br />";
                //message += ("Lucro + Destroços: " + nf(gr)) + "<br />";
                //message += ("Lucro + Destroços vagas: " + nf(grVagas)) + "<br /><br />";

                var lucroNaoIncluido = 0;
                if($(".post .inner").first().find("span:contains('Lucro Vagas não Incluídas')").length > 0)
                    var lucroNaoIncluido = parseInt($($(".post .inner").first().find("span:contains('Lucro Vagas não Incluídas')")[0].childNodes).last().text().replace(/\D/g, ''));
                message += "<i><b>NOTA:</b> o Lucro Total pode ser superior ao valor do <b>G</b> ou <b>GR</b>. <br />Isto acontece se a recolha for superior aos 100%. <br />Apesar disto, o GR vai sempre representar o valor contemplando somente 100% da recolha.</i><br />";
                if(lucroNaoIncluido > 0) {
                    message += "<b style='color:#ff0000'>Este ataque contem vagas impróprias.</b><br />";
                }
                message += "--------------------------------------<br />";

                if(lucroNaoIncluido > 0) {
                    message += "G: "+nf(g)+" - " +nf(lucroNaoIncluido)+ " = <b style='color: #21AD5A'>" + nf(g-lucroNaoIncluido) + "</b><br />";
                } else {
                    message += "G: <b>"+nf(g)+"</b><br />";

                }
                message += "D: <b>"+nf(d)+"</b><br />";
                if(reciclado) {
                    if(lucroNaoIncluido > 0) {
                        message += "GR: "+nf(gr+gVagas)+" - " +nf(lucroNaoIncluido)+ " = <b style='color: #21AD5A'>" + nf((gr+gVagas)-lucroNaoIncluido) + "</b><br />";
                        gr = (gr+gVagas)-lucroNaoIncluido;
                    } else {
                        message += "GR: <b style='color: #21AD5A'>"+nf(gr+gVagas)+"</b><br />";
                        gr = gr+gVagas;
                    }
                } else {
                    gr = 0;
                    message += "<b style='color:#ff0000'>GR ilegível para top (não reciclado)</b><br />";
                }


                message += "--------------------------------------<br />";

                message += "Reciclado? " + (reciclado ? '<b>Sim</b>' : '<b>Não</b>') + "<br />";
                message += "Não incluír lucro: <b>" + nf(lucroNaoIncluido) + "</b> (vagas impróprias)<br />";
                message += "--------------------------------------<br />";


                var isTopG = false, isTopGR = false, isTopD = false;
                if(g >= topsNaves.top1.G.value) {
                    newText.push("TOP 1 G");

                    if(topsNaves.top1.G.url !== url) {
                        topsNaves.top3.G.value = topsNaves.top2.G.value;
                        topsNaves.top3.G.url = topsNaves.top2.G.url;

                        topsNaves.top2.G.value = topsNaves.top1.G.value;
                        topsNaves.top2.G.url = topsNaves.top1.G.url;

                        topsNaves.top1.G.value = g;
                        topsNaves.top1.G.url = url;
                    }

                    isTopG = true;
                } else if(g >= topsNaves.top2.G.value) {
                    newText.push("TOP 2 G");

                    if(topsNaves.top2.G.url !== url) {
                        topsNaves.top3.G.value = topsNaves.top2.G.value;
                        topsNaves.top3.G.url = topsNaves.top2.G.url;

                        topsNaves.top2.G.value = g;
                        topsNaves.top2.G.url = url;
                    }

                    isTopG = true;
                } else if(g >= topsNaves.top3.G.value) {
                    newText.push("TOP 3 G");

                    if(topsNaves.top3.G.url !== url) {
                        topsNaves.top3.G.value = g;
                        topsNaves.top3.G.url = url;
                    }

                    isTopG = true;
                }

                if(gr >= topsNaves.top1.GR.value) {
                    newText.push("TOP 1 GR");

                    if(topsNaves.top1.GR.url !== url) {
                        topsNaves.top3.GR.value = topsNaves.top2.GR.value;
                        topsNaves.top3.GR.url = topsNaves.top2.GR.url;

                        topsNaves.top2.GR.value = topsNaves.top1.GR.value;
                        topsNaves.top2.GR.url = topsNaves.top1.GR.url;

                        topsNaves.top1.GR.value = gr;
                        topsNaves.top1.GR.url = url;
                    }

                    isTopGR = true;
                } else if(gr >= topsNaves.top2.GR.value) {
                    newText.push("TOP 2 GR");

                    if(topsNaves.top2.GR.url !== url) {
                        topsNaves.top3.GR.value = topsNaves.top2.GR.value;
                        topsNaves.top3.GR.url = topsNaves.top2.GR.url;

                        topsNaves.top2.GR.value = gr;
                        topsNaves.top2.GR.url = url;
                    }

                    isTopGR = true;
                } else if(gr >= topsNaves.top3.GR.value) {
                    newText.push("TOP 3 GR");

                    if(topsNaves.top3.GR.url !== url) {
                        topsNaves.top3.GR.value = gr;
                        topsNaves.top3.GR.url = url;
                    }

                    isTopGR = true;
                }

                if(d >= topsNaves.top1.D.value) {
                    newText.push("TOP 1 D");

                    if(topsNaves.top1.D.url !== url) {
                        topsNaves.top3.D.value = topsNaves.top2.D.value;
                        topsNaves.top3.D.url = topsNaves.top2.D.url;

                        topsNaves.top2.D.value = topsNaves.top1.D.value;
                        topsNaves.top2.D.url = topsNaves.top1.D.url;

                        topsNaves.top1.D.value = d;
                        topsNaves.top1.D.url = url;

                    }

                    isTopD = true;
                } else if(d >= topsNaves.top2.D.value) {
                    newText.push("TOP 2 D");

                    if(topsNaves.top2.D.url !== url) {
                        topsNaves.top3.D.value = topsNaves.top2.D.value;
                        topsNaves.top3.D.url = topsNaves.top2.D.url;

                        topsNaves.top2.D.value = d;
                        topsNaves.top2.D.url = url;

                    }

                    isTopD = true;
                } else if(d >= topsNaves.top3.D.value) {
                    newText.push("TOP 3 D");

                    if(topsNaves.top3.D.url !== url) {
                        topsNaves.top3.D.value = d;
                        topsNaves.top3.D.url = url;
                    }

                    isTopD = true;
                }

                if(!isTopG && !isTopGR && !isTopD) {
                    alert("Arquivar - Não se enquandra em nenhum TOP.");
                }
            } else {
                var g = findByText("Lucro:");
                var gr = findByText("Lucro + Destroços:");
                var d = findByText("Perdas:");

                var isTopG = false, isTopGR = false, isTopD = false;
                if(g >= topsNavesAC.top1.G.value) {
                    newText.push("TOP 1 G");

                    if(topsNavesAC.top1.G.url !== url) {
                        topsNavesAC.top3.G.value = topsNavesAC.top2.G.value;
                        topsNavesAC.top3.G.url = topsNavesAC.top2.G.url;

                        topsNavesAC.top2.G.value = topsNavesAC.top1.G.value;
                        topsNavesAC.top2.G.url = topsNavesAC.top1.G.url;

                        topsNavesAC.top1.G.value = g;
                        topsNavesAC.top1.G.url = url;
                    }

                    isTopG = true;
                } else if(g >= topsNavesAC.top2.G.value) {
                    newText.push("TOP 2 G");

                    if(topsNavesAC.top2.G.url !== url) {
                        topsNavesAC.top3.G.value = topsNavesAC.top2.G.value;
                        topsNavesAC.top3.G.url = topsNavesAC.top2.G.url;

                        topsNavesAC.top2.G.value = g;
                        topsNavesAC.top2.G.url = url;
                    }

                    isTopG = true;
                } else if(g >= topsNavesAC.top3.G.value) {
                    newText.push("TOP 3 G");

                    if(topsNavesAC.top3.G.url !== url) {
                        topsNavesAC.top3.G.value = g;
                        topsNavesAC.top3.G.url = url;
                    }

                    isTopG = true;
                }

                if(gr >= topsNavesAC.top1.GR.value) {
                    newText.push("TOP 1 GR");

                    if(topsNavesAC.top1.GR.url !== url) {
                        topsNavesAC.top3.GR.value = topsNavesAC.top2.GR.value;
                        topsNavesAC.top3.GR.url = topsNavesAC.top2.GR.url;

                        topsNavesAC.top2.GR.value = topsNavesAC.top1.GR.value;
                        topsNavesAC.top2.GR.url = topsNavesAC.top1.GR.url;

                        topsNavesAC.top1.GR.value = gr;
                        topsNavesAC.top1.GR.url = url;
                    }

                    isTopGR = true;
                } else if(gr >= topsNavesAC.top2.GR.value) {
                    newText.push("TOP 2 GR");

                    if(topsNavesAC.top2.GR.url !== url) {
                        topsNavesAC.top3.GR.value = topsNavesAC.top2.GR.value;
                        topsNavesAC.top3.GR.url = topsNavesAC.top2.GR.url;

                        topsNavesAC.top2.GR.value = gr;
                        topsNavesAC.top2.GR.url = url;
                    }

                    isTopGR = true;
                } else if(gr >= topsNavesAC.top3.GR.value) {
                    newText.push("TOP 3 GR");

                    if(topsNavesAC.top3.GR.url !== url) {
                        topsNavesAC.top3.GR.value = gr;
                        topsNavesAC.top3.GR.url = url;
                    }

                    isTopGR = true;
                }

                if(d >= topsNavesAC.top1.D.value) {
                    newText.push("TOP 1 D");

                    if(topsNavesAC.top1.D.url !== url) {
                        topsNavesAC.top3.D.value = topsNavesAC.top2.D.value;
                        topsNavesAC.top3.D.url = topsNavesAC.top2.D.url;

                        topsNavesAC.top2.D.value = topsNavesAC.top1.D.value;
                        topsNavesAC.top2.D.url = topsNavesAC.top1.D.url;

                        topsNavesAC.top1.D.value = d;
                        topsNavesAC.top1.D.url = url;

                    }

                    isTopD = true;
                } else if(d >= topsNavesAC.top2.D.value) {
                    newText.push("TOP 2 D");

                    if(topsNavesAC.top2.D.url !== url) {
                        topsNavesAC.top3.D.value = topsNavesAC.top2.D.value;
                        topsNavesAC.top3.D.url = topsNavesAC.top2.D.url;

                        topsNavesAC.top2.D.value = d;
                        topsNavesAC.top2.D.url = url;

                    }

                    isTopD = true;
                } else if(d >= topsNavesAC.top3.D.value) {
                    newText.push("TOP 3 D");

                    if(topsNavesAC.top3.D.url !== url) {
                        topsNavesAC.top3.D.value = d;
                        topsNavesAC.top3.D.url = url;
                    }

                    isTopD = true;
                }

                if(!isTopG && !isTopGR && !isTopD) {
                    alert("Arquivar - Não se enquandra em nenhum TOP.");
                }
            }

        } else if(veiculo) {
            var g = findByText("Lucro:");
            var d = findByText("Perdas:");

            var isTopG = false, isTopD = false;
            if(g >= topsVeiculos.top1.G.value) {
                newText.push("TOP 1 G");

                if(topsVeiculos.top1.G.url !== url) {
                    topsVeiculos.top3.G.value = topsVeiculos.top2.G.value;
                    topsVeiculos.top3.G.url = topsVeiculos.top2.G.url;

                    topsVeiculos.top2.G.value = topsVeiculos.top1.G.value;
                    topsVeiculos.top2.G.url = topsVeiculos.top1.G.url;

                    topsVeiculos.top1.G.value = g;
                    topsVeiculos.top1.G.url = url;
                }

                isTopG = true;
            } else if(g >= topsVeiculos.top2.G.value) {
                newText.push("TOP 2 G");

                if(topsVeiculos.top2.G.url !== url) {
                    topsVeiculos.top3.G.value = topsVeiculos.top2.G.value;
                    topsVeiculos.top3.G.url = topsVeiculos.top2.G.url;

                    topsVeiculos.top2.G.value = g;
                    topsVeiculos.top2.G.url = url;
                }

                isTopG = true;
            } else if(g >= topsVeiculos.top3.G.value) {
                newText.push("TOP 3 G");

                if(topsVeiculos.top3.G.url !== url) {
                    topsVeiculos.top3.G.value = g;
                    topsVeiculos.top3.G.url = url;
                }

                isTopG = true;
            }

            if(d >= topsVeiculos.top1.D.value) {
                newText.push("TOP 1 D");

                if(topsVeiculos.top1.D.url !== url) {
                    topsVeiculos.top3.D.value = topsVeiculos.top2.D.value;
                    topsVeiculos.top3.D.url = topsVeiculos.top2.D.url;

                    topsVeiculos.top2.D.value = topsVeiculos.top1.D.value;
                    topsVeiculos.top2.D.url = topsVeiculos.top1.D.url;

                    topsVeiculos.top1.D.value = d;
                    topsVeiculos.top1.D.url = url;
                }

                isTopD = true;
            } else if(d >= topsVeiculos.top2.D.value) {
                newText.push("TOP 2 D");

                if(topsVeiculos.top2.D.url !== url) {
                    topsVeiculos.top3.D.value = topsVeiculos.top2.D.value;
                    topsVeiculos.top3.D.url = topsVeiculos.top2.D.url;

                    topsVeiculos.top2.D.value = d;
                    topsVeiculos.top2.D.url = url;
                }

                isTopD = true;
            } else if(d >= topsVeiculos.top3.D.value) {
                newText.push("TOP 3 D");

                if(topsVeiculos.top3.D.url !== url) {
                    topsVeiculos.top3.D.value = d;
                    topsVeiculos.top3.D.url = url;
                }

                isTopD = true;
            }

            if(!isTopG && !isTopD) {
                alert("Arquivar - Não se enquandra em nenhum TOP.");
            }
        }

        if(newText.length > 0) {
            message += "<br />Novo título: <input type='text' value='["+newText.join(" & ")+"] ' /><br /><br />";
            buildModal(message);
        }

        localStorage.setItem("topsNaves", JSON.stringify(topsNaves));
        localStorage.setItem("topsNavesAC", JSON.stringify(topsNavesAC));
        localStorage.setItem("topsVeiculos", JSON.stringify(topsVeiculos));
    });

    $(".pagesection .pagelinks").append(checkTop, clearTop);

    function buildModal(text, newTitle) {
        var modal = $("<div />").css({
            "position":"fixed",
            "top":"50%",
            "left":"10%",
            "transform":"translate(0, -50%)",
            "border":"2px solid #FFF",
            "background-color":"#010714",
            "padding":"10px"
        });

        var message = $("<div />").css({
            "width":"100%",
            "float":"left"
        }).html(text);

        var fechar = $("<input />").attr({
            "type":"button"
        }).css({
            "background":"#c74646",
            "margin-left":"10px",
            "font-size":"11px",
            "float":"right"
        }).val("Fechar").on("click", function() {
            modal.remove();
        });

        modal.append(message, fechar);

        $("body").append(modal);
    }

})();