andrizz / TM Set routine and add bonus on skills

// ==UserScript==
// @name		    TM Set routine and add bonus on skills
// @version         0.1
// @description	    Adds the routine-bonus on skills in the player page. You can also select the routine from which to calculate the bonus (e.g. when the player gains routine through the sharing system)
// @author          Andrizz aka Banana aka Jimmy il Fenomeno (I made only a few small changes to the "NewMESklsRouCalcPlyr" script by Long Coast United)
// @downloadURL     https://openuserjs.org/install/andrizz/TM_Set_routine_and_add_bonus_on_skills.user.js
// @include		    http://trophymanager.com/players/*
// @include		    https://trophymanager.com/players/*
// @exclude		    http://trophymanager.com/players/
// @exclude		    http://trophymanager.com/players/compare
// @exclude		    http://trophymanager.com/players/compare/
// @exclude		    http://trophymanager.com/players/compare/*
// @exclude		    https://trophymanager.com/players/
// @exclude		    https://trophymanager.com/players/compare
// @exclude		    https://trophymanager.com/players/compare/
// @exclude		    https://trophymanager.com/players/compare/*
// @license         MIT
// ==/UserScript==

setTimeout(function() {
    // save routine into variable and add routine toggle button:
    var routineTbl = document.getElementsByClassName("float_left info_table zebra")[0];
    var tRows = routineTbl.getElementsByTagName("tr");
    var tCells = routineTbl.getElementsByTagName("td");
    var tRow, tCell, routine;
    for (var i = 0; i < tRows.length; i++) {
        tRow = tRows[i];
        tCell = tCells[i];
        if (tRow.innerHTML.includes("Routine")) {
            routine = tCell.innerHTML;
            if (routine.indexOf("div") != (-1)) {
                var divRatingR = routine.search(/<div.*/);
                routine = routine.substr(0,divRatingR).replace(/,/g, "");
            }
        tRow.firstChild.innerHTML = tRow.firstChild.innerHTML + "<span id='routineToggle' class='button'><span class='button_border' style='width: 90px; text-transform: none; padding: 0 5px 0 5px;'>Add rou. bonus</span></span>";
        tCell.innerHTML = tCell.innerHTML + "<div style='font-weight:normal'>Set rou: <input name='setROU' class='embossed' value='"+routine+"' size='1' maxlength='4' style='text-align: center;'><span id='skBonus'></span></div>" ;
        }
    }
    // add event to routine toggle button:
    document.getElementById("routineToggle").addEventListener("click", toggleRoutine, false);
    // save default documement object content to variable (only TR, so it does not conflict with other scripts):
    var positionCell = document.getElementsByClassName("favposition long")[0];
    if (positionCell.innerHTML.includes("gk")) var TR = 8; else TR = 7;
    var defaultSkillTbl = [];
    for (i = 0; i<TR; i++) {
        defaultSkillTbl[i] = document.getElementsByClassName("skill_table zebra")[0].rows[i].innerHTML;
    }
    // apply routine to skills:
    var skillTbl = document.getElementsByClassName("skill_table zebra")[0];
    var skillTR = document.getElementsByClassName("skill_table zebra")[0].rows;
    
    function applyRoutine() {
        //get routine from input box:
        routine = document.getElementsByName("setROU")[0].value;
        // calculate routine bonus and add to 'skBonus' box:
        var skBonus = (3/100) * (100-(100) * Math.pow(Math.E, -routine*0.035));
        var SkBns = document.getElementById("skBonus");
        SkBns.textContent = " +"+skBonus.toFixed(2);
        // calculate routine to skills:
        var tSkillCells = skillTbl.getElementsByClassName("align_center");
        var tSkillCell;
        // omit tSkillCells[2] which stands for stamina and is not affected by routine:
        for (var i = 0; i < tSkillCells.length; i++) {
            tSkillCell = tSkillCells[i];
            // omit tSkillCells[2] which stands for stamina and is not affected by routine:
            if (i === 2) {
                if (0 < parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 19) {
                    tSkillCell.innerHTML = parseInt(tSkillCell.innerHTML);
                } else {
                    tSkillCell.innerHTML = parseInt(tSkillCell.innerHTML.replace(/(^.+\D)(\d+)(\D.+$)/i, '$2'));
                }
                dyeStamina(tSkillCell);
            } else {
            if (0 < parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 19) {
                tSkillCell.innerHTML = (Math.round((parseInt(tSkillCell.innerHTML) + (3 / 100) * (100 - (100) * Math.exp(-routine * 0.035))) * 100) / 100);
            } else {
                tSkillCell.innerHTML = (Math.round((parseInt(tSkillCell.innerHTML.replace(/(^.+\D)(\d+)(\D.+$)/i, '$2')) + (3 / 100) * (100 - (100) * Math.exp(-routine * 0.035))) * 100) / 100);
            }
            dyeSkills(tSkillCell);
            }
        }
    }

    // colour skills depending on their value:
    function dyeStamina(tSkillCell) {
        if (20 <= parseInt(tSkillCell.innerHTML)) tSkillCell.style.color = "#FF4500";
        if (19 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 20) tSkillCell.style.color = "#FFA500";
        if (17 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 19) tSkillCell.style.color = "#FFD700";
        if (15 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 17) tSkillCell.style.color = "#FFFF00";
        if (5 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 10) tSkillCell.style.opacity = "0.75";
        if (1 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 5) tSkillCell.style.opacity = "0.5";
    }
    function dyeSkills(tSkillCell) {
        if (22 <= parseInt(tSkillCell.innerHTML)) tSkillCell.style.color = "#FF4500";
        if (21 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 22) tSkillCell.style.color = "#FFA500";
        if (19 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 21) tSkillCell.style.color = "#FFD700";
        if (15 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 19) tSkillCell.style.color = "#FFFF00";
        if (5 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 10) tSkillCell.style.opacity = "0.75";
        if (1 <= parseInt(tSkillCell.innerHTML) && parseInt(tSkillCell.innerHTML) < 5) tSkillCell.style.opacity = "0.5";
    }
    // add/remove routine to/from skills:
    function toggleRoutine() {
        if (document.getElementById("routineToggle").innerHTML.includes("Add rou. bonus")) {
            applyRoutine();
            document.getElementById("routineToggle").innerHTML = "<span class='button_border' style='width: 90px; text-transform: none; padding: 0 5px 0 5px;'>Remove bonus</span>";
        } else if (document.getElementById("routineToggle").innerHTML.includes("Remove")) {
            for (i = 0; i<TR; i++) {
                skillTR[i].innerHTML = defaultSkillTbl[i];
            }
            document.getElementById("routineToggle").innerHTML = "<span class='button_border' style='width: 90px; text-transform: none; padding: 0 5px 0 5px;'>Add rou. bonus</span>";
        } else {
            alert("CAUTION: The script may not work properly!");
        }
    }
}, 150);