Torn Extensions - Gym Torn Gain

calculates gym gain

Versione datata 08/02/2020. Vedi la nuova versione l'ultima versione.

// ==UserScript==
// @name         Torn Extensions - Gym Torn Gain
// @namespace    TornExtensions
// @version      1.3.0
// @description  calculates gym gain
// @author       Xradiation
// @match        https://www.torn.com/gym.php*
// @match        https://www.torn.com/preferences.php*
// @grant        none
// @run-at       document-idle
/* globals jQuery, $, waitForKeyElements */
// ==/UserScript==

(function() {
    'use strict';

    //EDIT AT OWN RISK!!!

    //minified vars
    var Gymlist2=[{Gym:"Premier Fitness",Energy:5,Str:2,Spe:2,Def:2,Dex:2},{Gym:"Average Joes",Energy:5,Str:2.4,Spe:2.4,Def:2.8,Dex:2.4},{Gym:"Woody's Workout",Energy:5,Str:2.8,Spe:3.2,Def:3,Dex:2.8},{Gym:"Beach Bods",Energy:5,Str:3.2,Spe:3.2,Def:3.2,Dex:"0"},{Gym:"Silver Gym",Energy:5,Str:3.4,Spe:3.6,Def:3.4,Dex:3.2},{Gym:"Pour Femme",Energy:5,Str:3.4,Spe:3.6,Def:3.6,Dex:3.8},{Gym:"Davies Den",Energy:5,Str:3.7,Spe:"0",Def:3.7,Dex:3.7},{Gym:"Global Gym",Energy:5,Str:4,Spe:4,Def:4,Dex:4},{Gym:"Knuckle Heads",Energy:10,Str:4.8,Spe:4.4,Def:4,Dex:4.2},{Gym:"Pioneer Fitness",Energy:10,Str:4.4,Spe:4.6,Def:4.8,Dex:4.4},{Gym:"Anabolic Anomalies",Energy:10,Str:5,Spe:4.6,Def:5.2,Dex:4.6},{Gym:"Core",Energy:10,Str:5,Spe:5.2,Def:5,Dex:5},{Gym:"Racing Fitness",Energy:10,Str:5,Spe:5.4,Def:4.8,Dex:5.2},{Gym:"Complete Cardio",Energy:10,Str:5.5,Spe:5.8,Def:5.5,Dex:5.2},{Gym:"Legs, Bums and Tums",Energy:10,Str:"0",Spe:5.6,Def:5.6,Dex:5.8},{Gym:"Deep Burn",Energy:10,Str:6,Spe:6,Def:6,Dex:6},{Gym:"Apollo Gym",Energy:10,Str:6,Spe:6.2,Def:6.4,Dex:6.2},{Gym:"Gun Shop",Energy:10,Str:6.6,Spe:6.4,Def:6.2,Dex:6.2},{Gym:"Force Training",Energy:10,Str:6.4,Spe:6.6,Def:6.4,Dex:6.8},{Gym:"Cha Cha's",Energy:10,Str:6.4,Spe:6.4,Def:6.8,Dex:7},{Gym:"Atlas",Energy:10,Str:7,Spe:6.4,Def:6.4,Dex:6.6},{Gym:"Last Round",Energy:10,Str:6.8,Spe:6.6,Def:7,Dex:6.6},{Gym:"The Edge",Energy:10,Str:6.8,Spe:7,Def:7,Dex:6.8},{Gym:"George's",Energy:10,Str:7.3,Spe:7.3,Def:7.3,Dex:7.3},{Gym:"Balboas Gym",Energy:25,Str:"0",Spe:"0",Def:7.5,Dex:7.5},{Gym:"Frontline Fitness",Energy:25,Str:7.5,Spe:7.5,Def:"0",Dex:"0"},{Gym:"Gym 3000",Energy:50,Str:8,Spe:"0",Def:"0",Dex:"0"},{Gym:"Mr. Isoyamas",Energy:50,Str:"0",Spe:"0",Def:8,Dex:"0"},{Gym:"Total Rebound",Energy:50,Str:"0",Spe:8,Def:"0",Dex:"0"},{Gym:"Elites",Energy:50,Str:"0",Spe:"0",Def:"0",Dex:8},{Gym:"Sports Science Lab",Energy:25,Str:9,Spe:9,Def:9,Dex:9}];
    var Gym,gymNumber,speed,strength,defense,dexterity,strength_modifier,defense_modifier,speed_modifier,dexterity_modifier,happy,energy,i,n,modifierSpe=1,modifierAll=1,modifierStr=1,modifierDex=1,modifierDef=1,a=3.480061091*Math.pow(10,-7),b=250,c=3.091619094*Math.pow(10,-6),d=6.82775184551527*Math.pow(10,-5),e=-.0301431777;
    var apiKey;
    if (localStorage.apiKey == null || localStorage.apiKey == 'undefined') {
        console.log('api is null');
        if (window.location.href.includes('preferences.php#tab=api')) checkExist();

        function checkExist() {
            if (!$("input.input___1n_f_").size() || $("input.input___1n_f_").val() == undefined || $("input.input___1n_f_").val() == '') {
                console.log('retry');
                console.log($("input.input___1n_f_").val());
                window.requestAnimationFrame(checkExist);
            }
            else {
                console.log($("input.input___1n_f_").val());
                (confirm('Allow Gym Torn Gain to use your apiKey?')) ? window.localStorage.apiKey = $('input.input___1n_f_').val(): '';
            }
        };
    }
    else if (window.location.href.includes('gym.php')) {
        apiKey = window.localStorage.apiKey;
        let tempID = ($('#gymroot').size()) ? '#gymroot' : '#mainContainer';
        $(tempID).prepend(`
<div id="customEstimate" class="tutorial-cont m-top10">
<div class="title-gray top-round" role="heading" aria-level="5">
<span>Custom Estimation</span>
</div>
<div class="tutorial-desc bottom-round cont-gray p10" tabindex="0">
<p>Happy: <input id="happy" type="number" class="input___2D0YE" required> Energy: <input type="number" id="energy" class="input___2D0YE" required></p>
<p>Gym: <select id="gyms">
<option>Premier Fitness</option><option>Average Joes</option><option>Woody's Workout</option><option>Beach Bods</option><option>Silver Gym</option><option>Pour Femme</option><option>Davies Den</option><option>Global Gym</option><option>Knuckle Heads</option><option>Pioneer Fitness</option><option>Anabolic Anomalies</option><option>Core</option><option>Racing Fitness</option><option>Complete Cardio</option><option>Legs Bums and Tums</option><option>Deep Burn</option><option>Apollo Gym</option><option>Gun Shop</option><option>Force Training</option><option>Cha Cha's</option><option>Atlas</option><option>Last Round</option><option>The Edge</option><option>George's</option><option>Balboas Gym</option><option>Frontline Fitness</option><option>Gym </option><option>Mr Isoyamas</option><option>Total Rebound</option><option>Elites</option><option>Sports Science Lab</option>
</select></p>
<p>speed: <input id="spe" type="number" class="input___2D0YE" required> str: <input id="str" type="number" class="input___2D0YE" required> dex: <input id="dex" type="number" class="input___2D0YE" required> def: <input id="def" type="number" class="input___2D0YE" required>
<p><input id="estimateButton" type ="submit" value="calculate"></p>
</div>
</div>`);
        $('#customEstimate').hide();
        $('#skip-to-content').css({
            color: 'green',
            cursor: 'pointer'
        });
        $('#skip-to-content').on('click', function() {
            $('#customEstimate').toggle("slide", {
                direction: "right"
            }, 500);
        });
        $('#estimateButton').on('click', function() {
            console.log('pressed');
            let energy = $('#energy').val();
            let happy = $('#happy').val();
            let gym = $('#gyms option:selected').text();
            console.log(energy);
            let gymThis = Gymlist2.filter(function(g) {
                return g.Gym == gym
            });
            let modifierStr = 1;
            let modifierSpe = 1;
            let modifierDef = 1;
            let modifierDex = 1;
            speed = $('#spe').val();
            defense = $('#def').val();
            dexterity = $('#dex').val();
            strength = $('#str').val();
            let GymDotsSpe = gymThis[0].Spe;
            let GymDotsDef = gymThis[0].Def;
            let GymDotsDex = gymThis[0].Dex;
            let GymDotsStr = gymThis[0].Str;
            var EnergyPerTrain = gymThis[0].Energy;
            let trains = parseInt(energy / EnergyPerTrain);
            console.log(trains);
            let gainSpe = (modifierSpe) * (GymDotsSpe) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (speed) + d * (happy + b) + e);
            let gainDef = (modifierDef) * (GymDotsDef) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (defense) + d * (happy + b) + e);
            let gainDex = (modifierDex) * (GymDotsDef) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (dexterity) + d * (happy + b) + e);
            let gainStr = (modifierStr) * (GymDotsStr) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (strength) + d * (happy + b) + e);
            console.table({
                Str: gainStr,
                Dex: gainDex,
                Def: gainDef,
                Spe: gainSpe
            });
            alert('check the console');
        });
        if (apiKey != null && localStorage.apiKey != 'undefined') statsEstimator();
    }

    function statsEstimator() {
        if (document.getElementById("gymroot")) {
            var urlStats = 'https://api.torn.com/user/?selections=battlestats,gym,bars,perks&key=' + apiKey;
            //api call
            fetch(urlStats).then(function(response) {
                response.json().then(function(data) {
                    console.log(data);
                    //apiRequest1
                    strength = data.strength;
                    defense = data.defense;
                    speed = data.speed;
                    dexterity = data.dexterity;
                    strength_modifier = data.strength_modifier;
                    defense_modifier = data.defense_modifier;
                    speed_modifier = data.speed_modifier;
                    dexterity_modifier = data.dexterity_modifier;
                    //apiRequest2
                    happy = data.happy.current;
                    energy = data.energy.current;
                    //apiRequest3
                    gymNumber = data.active_gym - 1;
                    Gym = Gymlist2[gymNumber].Gym;
                    //apiRequest4
                    var string;
                    if (data.hasOwnProperty('property_perks')) {
                        for (i = 0; i < data.property_perks.length; i++) {
                            string = data.property_perks[i];
                            if (string.includes('Gym gains')) {
                                n = parseFloat(data.property_perks[i].match(/\d+/)[0]);
                                n = (n / 100) + 1;
                                modifierAll *= n;
                            }
                        }
                    }
                    if (data.hasOwnProperty('education_perks')) {
                        for (i = 0; i < data.education_perks.length; i++) {
                            string = data.education_perks[i];
                            modifierAll *= (string.includes('1% Gym gains')) ? 1.01 : 1;
                            modifierDex *= (string.includes('Dexterity gym gains')) ? 1.01 : 1;
                            modifierDef *= (string.includes('Defense gym gains')) ? 1.01 : 1;
                            modifierSpe *= (string.includes('Speed gym gains')) ? 1.01 : 1;
                            modifierStr *= (string.includes('Strength gym gains')) ? 1.01 : 1;
                        }
                    }
                    if (data.hasOwnProperty('company_perks')) {
                        for (i = 0; i < data.company_perks.length; i++) {
                            string = data.company_perks[i];
                            modifierDex *= (string.includes('dexterity gym gains')) ? 1.1 : 1;
                            modifierDef *= (string.includes('defense gym gains')) ? 1.1 : 1;
                            modifierAll *= (string.includes('gym gains')) ? 1.03 : 1;
                        }
                    }
                    if (data.hasOwnProperty('book_perks')) {
                        for (i = 0; i < data.book_perks.length; i++) {
                            string = data.book_perks[i];
                            modifierAll *= (string.includes('all gym gains')) ? 1.2 : 1;
                            modifierStr *= (string.includes('Strength gym gains')) ? 1.3 : 1;
                            modifierDef *= (string.includes('Defense gym gains')) ? 1.3 : 1;
                            modifierSpe *= (string.includes('Speed gym gains')) ? 1.3 : 1;
                            modifierDex *= (string.includes('Dexterity gym gains')) ? 1.3 : 1;
                        }
                    }
                    if (data.hasOwnProperty('faction_perks')) {
                        for (i = 0; i < data.faction_perks.length; i++) {
                            string = data.faction_perks[i];
                            if (string.includes('strength gym gains')) {
                                n = parseFloat(data.faction_perks[i].match(/\d+/)[0]);
                                n = (n / 100) + 1;
                                modifierStr *= n;
                            }
                            else
                                if (string.includes('speed gym gains')) {
                                    n = parseFloat(data.faction_perks[i].match(/\d+/)[0]);
                                    n = (n / 100) + 1;
                                    modifierSpe *= n;
                                }
                            else
                                if (string.includes('defense gym gains')) {
                                    n = parseFloat(data.faction_perks[i].match(/\d+/)[0]);
                                    n = (n / 100) + 1;
                                    modifierDef *= n;
                                }
                            else
                                if (string.includes('dexterity gym gains by')) {
                                    n = parseFloat(data.faction_perks[i].match(/\d+/)[0]);
                                    n = (n / 100) + 1;
                                    modifierDex *= n;
                                }
                        }
                    }
                    modifierStr *= modifierAll;
                    modifierSpe *= modifierAll;
                    modifierDef *= modifierAll;
                    modifierDex *= modifierAll;
                    var GymDotsSpe = Gymlist2[gymNumber].Spe;
                    var GymDotsDef = Gymlist2[gymNumber].Def;
                    var GymDotsDex = Gymlist2[gymNumber].Dex;
                    var GymDotsStr = Gymlist2[gymNumber].Str;
                    var EnergyPerTrain = Gymlist2[gymNumber].Energy;
                    var trains = parseInt(energy / EnergyPerTrain);
                    var gainSpe = (modifierSpe) * (GymDotsSpe) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (speed) + d * (happy + b) + e);
                    var gainDef = (modifierDef) * (GymDotsDef) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (defense) + d * (happy + b) + e);
                    var gainDex = (modifierDex) * (GymDotsDef) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (dexterity) + d * (happy + b) + e);
                    var gainStr = (modifierStr) * (GymDotsStr) * (EnergyPerTrain) * ((a * Math.log(Math.abs(happy + b)) + c) * (strength) + d * (happy + b) + e);
                    var strText = '<div class="propertyTitle___36JU1 fontSize14___3E9pW"><h3 class="title___3FTtc">' + Math.round(gainStr * 100) / 100 + '</h3><span class="propertyValue___3oAOv">' + (Math.round(gainStr * 100 * trains) / 100) + '</span></div>';
                    var defText = '<div class="propertyTitle___36JU1 fontSize14___3E9pW"><h3 class="title___3FTtc">' + Math.round(gainDef * 100) / 100 + '</h3><span class="propertyValue___3oAOv">' + (Math.round(gainDef * 100 * trains) / 100) + '</span></div>';
                    var speText = '<div class="propertyTitle___36JU1 fontSize14___3E9pW"><h3 class="title___3FTtc">' + Math.round(gainSpe * 100) / 100 + '</h3><span class="propertyValue___3oAOv">' + (Math.round(gainSpe * 100 * trains) / 100) + '</span></div>';
                    var dexText = '<div class="propertyTitle___36JU1 fontSize14___3E9pW"><h3 class="title___3FTtc">' + Math.round(gainDex * 100) / 100 + '</h3><span class="propertyValue___3oAOv">' + (Math.round(gainDex * 100 * trains) / 100) + '</span></div>';
                    var array = [{
                        'speText': speText,
                        'value': GymDotsSpe
                    }, {
                        'defText': defText,
                        'value': GymDotsDef
                    }, {
                        'dexText': dexText,
                        'value': GymDotsDex
                    }, {
                        'strText': strText,
                        'value': GymDotsStr
                    }];
                    var largest = [{
                        'value': 0
                    }];
                    array.forEach(function(element) {
                        if (element.value > largest[0].value) {
                            largest = [element];
                        }
                        else if (element.value == largest[0].value) {
                            largest.push(element);
                        }
                    });
                    i = 0;
                    largest.forEach(function(e) {
                        var x = Object.keys(e)[0];
                        array[i][x] = e[x].replace('<div class="propertyTitle___36JU1 fontSize14___3E9pW">', '<div class="propertyTitle___36JU1 fontSize14___3E9pW" style="color:lightgreen;">');
                        i++;
                    });
                    $(array[3].strText).insertAfter(".strength___1GeGr .propertyTitle___36JU1");
                    $(array[1].defText).insertAfter(".defense___311kR .propertyTitle___36JU1");
                    $(array[0].speText).insertAfter(".speed___1o1b_ .propertyTitle___36JU1");
                    $(array[2].dexText).insertAfter(".dexterity___1YdUM .propertyTitle___36JU1");
                });
            });
        }
    }

})();