MediaWiki:WildCreatureStats.js

From ARK Wiki
Jump to navigation Jump to search

In other languages: DeutschEspañolFrançaisItaliano日本語PolskiPortuguês do BrasilРусскийไทย


Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
arkUsingArticles(['en:MediaWiki:DataFetch.js'], function() {
    var wildStatCalcDiv = null;
    var stats = null;
    var wildCreatureStats = null;
    var percentiles = null;
    var wildStatCalcStats = {
        'health': 'Health',
        'stamina': 'Stamina',
        'oxygen': 'Oxygen',
        'food': 'Food',
        'weight': 'Weight',
        'damage': 'Melee Damage'
    };

	var CACHE_EXPIRY_TIME = 72 * 60 * 60;
    var DATA_PAGE_NAME = 'en:Data:Wild creature stat values';
    var DATA_CACHE_ID = 1; // bump if stat data changes and you need it refreshed within the next hour

    if (wildStatCalcDiv = document.getElementById('wildStatCalc')) {
        // if a stat has x levelups, the stat is in the top percentiles[x] %. For 33 or more level, the probability is less than 0.01 %
        percentiles = Array(100.00, 100.00, 100.00, 100.00, 99.99, 99.97, 99.89, 99.67, 99.17, 98.13, 96.23, 93.09, 88.39, 81.93,
                            73.78, 64.28, 53.98, 43.58, 33.76, 25.06, 17.81, 12.11, 7.88, 4.91, 2.92, 1.67, 0.91, 0.48, 0.24, 0.12,
                            0.05, 0.02, 0.01);
        
        // TODO: these notes apply to data, move it into docs when we get an extension with formatting capabilities
        // NOTE: the increase is always given as percentage of the base-stat
        // NOTE: in this tool damage1 is (in most cases) 100 to represent 100% (as it is seen ingame)
        // NOTE: if a creature don't have oxygen (aquatics etc.), just leave these parameter out

        arkLoadDataPages([ DATA_PAGE_NAME ], DATA_CACHE_ID, CACHE_EXPIRY_TIME).then(function (results) {
            wildCreatureStats = results[DATA_PAGE_NAME];
            changeCreature(wildStatCalcDiv.innerHTML);
        });
    }

    function changeCreature(creature) {
        if (wildCreatureStats[creature] == null)
            creature = Object.keys(wildCreatureStats)[0];

        stats = wildCreatureStats[creature];
        var tableRows = '';
        for (var id in wildStatCalcStats) {
            if (stats[id + '1']) {
                var unit = '';
                if (id === 'damage')
                    unit = ' [%]';
                tableRows += '<tr><td><a href="/' + wildStatCalcStats[id] + '">' + wildStatCalcStats[id] + unit + '</a></td><td><input type="number" min="' + stats[id + '1'] + '" max="100000" maxlength="6" step="0.1" value="' + stats[id + '1'] + '" id="wildStatCalc' + id + 'input" style="width:5em"></td><td id="wildStatCalc' + id + '"></td></tr>';
            }
        }
        tableRows += '<tr><td><a href="/Speed">Speed [%]</a></td><td title="Wild creatures always and only have 100% Speed">100</td><td id="wildStatCalcSpeed"></td></tr>';
        var creatureSelect = '<select id="wildStatCalcSwitch">';
        for (var c in wildCreatureStats) {
            creatureSelect += '<option' + (c === creature ? ' selected' : '') + '>' + c + '</option>';
        }
        creatureSelect += '</select>';
        wildStatCalcDiv.innerHTML = '<table class="wikitable">' +
        '<tr><th colspan="3">' + creatureSelect + ' Level <input type="number" min="1" max="100000" maxlength="6" value="1" id="wildStatCalcLevelInput" style="width:5em"></th></tr>' +
        '<tr><th>Stat</th><th>Value</th><th>Levelups</th></tr>' +
        tableRows +
        '</table>';

        for (var id in wildStatCalcStats) {
            if (stats[id + '1']) {
                document.getElementById('wildStatCalc' + id + 'input').addEventListener('input', calcNatStats);
            }
        }

        var selectElement = document.getElementById('wildStatCalcSwitch');
        selectElement.addEventListener('change', function() {
            changeCreature(selectElement.value);
        });
        document.getElementById('wildStatCalcLevelInput').addEventListener('input', calcNatStats);
        calcNatStats();
    }

    function calcNatStats() {
        // calculate according level
        if (stats) {
            var totallevel = 1; // 1 is first level
            var levels = [];
            var stat = 0;
            var levelEl = null;
            var percentile = '';
            var determinedStatsNr = 0;
            for (var id in wildStatCalcStats) {
                if (stats[id + '1']) {
                    stat = document.getElementById('wildStatCalc' + id + 'input').value;
                    if (stats[id + '1'] && stats[id + 'Inc'] && stat >= stats[id + '1']) {
                        var level = Math.round((stat - stats[id + '1']) / (stats[id + 'Inc'] * stats[id + '1']));
                        levels[id] = level;
                        levelEl = document.getElementById('wildStatCalc' + id);
                        levelEl.innerHTML = level;
                        if (level > 32)
                        percentile = 0.01;
                        else
                        percentile = percentiles[level];
                        levelEl.title = 'This level is in the top ' + percentile + '% compared to all wild level 120 creatures.';
                        totallevel += level;
                        determinedStatsNr++;
                    }
                }
            }
            var levelInput = document.getElementById('wildStatCalcLevelInput').value;
            var speedPrefix = '';
            if (determinedStatsNr < 6)
                speedPrefix = '~';
            if (determinedStatsNr !== 7)
                document.getElementById('wildStatCalcSpeed').innerHTML = speedPrefix + Math.round((levelInput - totallevel) / (7 - determinedStatsNr));
            var hue = 0;
            for (var id in wildStatCalcStats) {
                if (stats[id + '1']) {
                    hue = 0;
                    if (totallevel > 1) {
                        hue = Math.round(120 * levels[id] / 28);
                    }
                    if (hue > 120)
                    hue = 120;
                    document.getElementById('wildStatCalc' + id).parentElement.style.backgroundColor = 'hsl(' + hue + ', 100%, 85%)';
                }
            }
        }
    }
});