MediaWiki:Gadget-heroStats.js

From Predecessor Wiki
Jump to navigation Jump to search

Note: After publishing, 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 / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Credits to League of Legends Fandom wiki for code
// Modified from https://leagueoflegends.fandom.com/wiki/MediaWiki:Common.js/levelselect.js
mw.loader.using('site').then(function(){
    'use strict';
    var stats = [
        'physical_power',
        'attack_speed',
        'physical_armor',
        'magical_armor',
        'max_health',
        'max_mana',
        'health_regeneration',
        'mana_regeneration'
    ], MAX_LVL = 18, data = {};

    /*function toLvl(hero, index, lvl) {
        var base = data[hero][index].base,
            plus = data[hero][index].plus,
            value;
        value = plus * (lvl - 1) + base;
        return Math.round(value * 100) / 100;
    }*/
    
    function toLvl(hero, index, lvl) {
	    var base = data[hero][index].base,
	        plus = data[hero][index].plus,
	        value;
	    if (lvl === 1) {
	        value = base;
	    } else {
	        value = base;
	        for (var i = 2; i <= lvl; i++) {
	            var growthRate = 0.8 + (i - 2) * 0.025;
	            value += plus * growthRate;
	        }
	    }
	    return Math.round(value * 100) / 100;
	}

    function update() {
        var $this = $(this),
            hero = $this.attr('data-hero'),
            lvl = Number($this.val()),
            heroStats = data[hero];
    
        stats.forEach(function(stat, index) {
            var heroStat = heroStats[index],
                $base = heroStat.$base,
                $plus = heroStat.$plus,
                base = heroStat.base,
                plus = heroStat.plus;
    
            switch (lvl) {
                // Level N
                case -1:
                    if ($base.length || $plus.length) {
                        if (base === 0) {
                            $base.empty();
                            $plus.text(plus);
                        } else {
                            if (stat === 'attack_speed') {
                                $base.text(base + '%');
                                $plus.text(plus ? ' (+ ' + plus + '%)' : '');
                            } else {
                                $base.text(base);
                                $plus.text(plus ? ' (+ ' + plus + ')' : '');
                            }
                        }
                    }
                    break;
                // Level "1 - MAX_LVL"
                case 0:
                    if ($base.length || $plus.length) {
                        $base.text(plus ? '' : toLvl(hero, index, 1));
                        if (stat === 'attack_speed') {
                            $plus.text(
                                plus ?
                                    base + ' – ' + toLvl(hero, index, MAX_LVL) + '%' :
                                    ($base.length ? '' : base + '%')
                            );
                        } else {
                            $plus.text(
                                plus ?
                                    base + ' – ' + toLvl(hero, index, MAX_LVL) :
                                    ($base.length ? '' : base)
                            );
                        }
                    }
                    break;
                // Level dynamic
                default:
                    if ($plus.length) {
                        $base.text('');
                        if (stat === 'attack_speed') {
                            $plus.text(toLvl(hero, index, lvl) + '%');
                        } else {
                            $plus.text(toLvl(hero, index, lvl));
                        }
                    }
                    break;
            }
        });
    }

    // Note: change the trigger class if the new version is required
    function initEach() {
        var $this = $(this).addClass('template-levelselect-initialized'),
            $hero = $this.find('.template-levelselect-hero').eq(0),
            hero = $hero.text().trim(),
            $select = $('<select>', {
                'change': update,
                'data-hero': hero,
                'id': 'level_' + hero
            }).append(
                $('<option>', {
                    text: 'n',
                    value: -1
                }),
                $('<option>', {
                    selected: 'selected',
                    text: '1-' + MAX_LVL,
                    value: 0
                }),
                Array
                    .apply(null, Array(MAX_LVL))
                    .map(function(_, index) {
                        return $('<option>', {
                            text: index + 1,
                            value: index + 1
                        });
                    })
            );
        data[hero] = stats.map(function(stat) {
            var $base = $this.find('#' + stat + '_' + hero).eq(0),
                $plus = $this.find('#' + stat + '_' + hero + '_growth').eq(0);
            return {
                $base: $base,
                $plus: $plus,
                base: Number($base.text()) || 0,
                plus: Number($plus.text()) || 0
            };
        });
        $hero.html([
            $('<label>', {
                'for': 'level_' + hero,
                'text': 'Level: '
            }),
            $select
        ]);
        update.bind($select)();
    }

    function init($content) {
        $content
            .find('.template-levelselect:not(.template-levelselect-initialized)')
            .each(initEach);
    }

    mw.loader.using(['mediawiki.util']).then(function() {
        init(mw.util.$content);
        mw.hook('wikipage.content').add(init);
    });
});