Loot Statistics/Checker/Code

//

```
/*jslint devel: true, browser: true, plusplus: true, white: true */
/*global $ */

(function () {
  'use strict';
  var sysop = false,
  main_function = function () {
    var current_tibia_version = '8.6',
    lootparser_versions_ex =
      {'8.6': {
        'changed': {
          'Rat': '9.63',
          'Spider': '9.63',
          'Poison Spider': '9.63',
          'Bug': '9.63',
          'Massive Fire Elemental': '9.6',
          'Ancient Scarab': '9.5',
          'Behemoth': '9.5',
          'Betrayed Wraith': '9.5',
          'Blightwalker': '9.5',
          'Crystal Spider': '9.5',
          'Dark Torturer': '9.5',
          'Defiler': '9.5',
          'Demon Skeleton': '9.5',
          'Draken Spellweaver': '9.5',
          'Eternal Guardian': '9.5',
          'Ghastly Dragon': '9.5',
          'Giant Spider': '9.5',
          'Gnarlhound': '9.5',
          'Hand of cursed Fate': '9.5',
          'Hellhound': '9.5',
          'Hydra': '9.5',
          'Ice Golem': '9.5',
          'Juggernaut': '9.5',
          'Killer Caiman': '9.5',
          'Lizard Zaogun': '9.5',
          'Lost Soul': '9.5',
          'Mammoth': '9.5',
          'Mutated Tiger': '9.5',
          'Phantasm': '9.5',
          'Quara Constrictor': '9.5',
          'Quara Constrictor Scout': '9.5',
          'Quara Hydromancer': '9.5',
          'Quara Hydromancer Scout': '9.5',
          'Quara Mantassin': '9.5',
          'Quara Mantassin Scout': '9.5',
          'Quara Pincher': '9.5',
          'Quara Predator Scout': '9.5',
          'Sea Serpent': '9.5',
          'Wailing Widow': '9.5',
          'Undead Dragon': '9.5',
          'Wyvern': '9.5',
          'Blazing Fire Elemental': '9.1',
          'Blistering Fire Elemental': '9.1',
          'Carrion Worm': '9.1',
          'Draken Abomination': '9.1',
          'Dwarf Guard': '9.1',
          'Earth Elemental': '9.1',
          'Fire Overlord': '9.1',
          'Hellfire Fighter': '9.1',
          'Jagged Earth Elemental': '9.1',
          'Muddy Earth Elemental': '9.1',
          'Stone Golem': '9.1',
          'Swamp Troll': '9.1',
          'Tarantula': '9.1',
          'Vampire': '9.1',
          'Witch': '9.1',
          'Worker Golem': '9.1',
        /*bosses*/
          'Ethershreck': '9.5',
          'Gorgo': '9.5',
          'Kerberos': '9.5',
          'Stonecracker': '9.5',
          'Deathbine': '9.2',
          'Esmeralda': '9.2',
          'Flameborn': '9.2',
          'Fleshcrawler': '9.2',
          'Hide': '9.2',
          'Leviathan': '9.2',
          'Ribstride': '9.2',
          'Shardhead': '9.2',
          'Sulphur Scuttler': '9.2',
          'The Bloodtusk': '9.2',
          'The Bloodweb': '9.2',
          'The Many': '9.2',
          'The Noxious Spawn': '9.2',
          'The Old Widow': '9.2',
          'The Snapper': '9.2',
          'Thul': '9.2'
        },
        'new': {
          'Young Troll': '9.63',
          'Abyssador': '9.6',
          'Deathstrike': '9.6',
          'Gnomevil': '9.6',
          'Vesperoth': '9.6',
          'Armadile': '9.6',
          'Cliff Strider': '9.6',
          'Crystalcrusher': '9.6',
          'Damaged Crystal Golem': '9.6',
          'Dragonling': '9.6',
          'Enraged Crystal Golem': '9.6',
          'Enslaved Dwarf': '9.6',
          'Hideous Fungus': '9.6',
          'Humongous Fungus': '9.6',
          'Humorless Fungus': '9.6',
          'Ironblight': '9.6',
          'Lava Golem': '9.6',
          'Lost Berserker': '9.6',
          'Magma Crawler': '9.6',
          'Modified Gnarlhound': '9.6',
          'Mushroom Sniffer': '9.6',
          'Orewalker': '9.6',
          'Stone Devourer': '9.6',
          'Vulcongra': '9.6',
          'Weeper': '9.6',
          'Wiggler': '9.6',
          'Bretzecutioner': '9.5',
          'Bruise Payne': '9.5',
          'Chopper': '9.5',
          'Fazzrah': '9.5',
          'Fleshslicer': '9.5',
          'Hemming': '9.5',
          'Maw': '9.5',
          'Mindmasher': '9.5',
          'Paiz the Pauperizer': '9.5',
          'Rotspit': '9.5',
          'Shadowstalker': '9.5',
          'Tormentor': '9.5',
          'Tromphonyte': '9.5',
          'Zanakeph': '9.5',
          'Jaul': '9.4',
          'Obujos': '9.4',
          'Tanjis': '9.4',
          'Calamary': '9.4',
          'Crawler': '9.4',
          'Deepling Guard': '9.4',
          'Deepling Spellsinger': '9.4',
          'Deepling Warrior': '9.4',
          'Deepling Worker': '9.4',
          'Fish (Creature)': '9.4',
          'Floor Blob': '9.4',
          'Hive Pore': '9.4',
          'Insectoid Worker': '9.4',
          'Jellyfish': '9.4',
          'Kollos': '9.4',
          'Ladybug': '9.4',
          'Lady Bug': '9.4',
          'Lesser Swarmer': '9.4',
          'Manta Ray': '9.4',
          'Northern Pike (Creature)': '9.4',
          'Slippery Northern Pike': '9.4',
          'Shark': '9.4',
          'Spidris': '9.4',
          'Spidris Elite': '9.4',
          'Spitter': '9.4',
          'Swarmer': '9.4',
          'Swarmer Hatchling': '9.4',
          'Waspoid': '9.4',
          'Askarak Demon': '9.1',
          'Askarak Lord': '9.1',
          'Askarak Prince': '9.1',
          'Bog Frog': '9.1',
          'Clay Guardian': '9.1',
          'Crystal Wolf': '9.1',
          'Death Priest': '9.1',
          'Deepling Scout': '9.1',
          'Desperate White Deer': '9.1',
          'Diamond Servant': '9.1',
          'Donkey': '9.1',
          'Dromedary': '9.1',
          'Elder Mummy': '9.1',
          'Elf Overseer': '9.1',
          'Energized Raging Mage': '9.1',
          'Enraged White Deer': '9.1',
          'Feverish Citizen': '9.1',
          'Filth Toad': '9.1',
          'Firestarter': '9.1',
          'Ghoulish Hyaena': '9.1',
          'Golden Servant': '9.1',
          'Grave Guard': '9.1',
          'Groam': '9.1',
          'Honour Guard': '9.1',
          'Horestis': '9.1',
          'Horse': '9.1',
          'Insectoid Scout': '9.1',
          'Iron Servant': '9.1',
          'Kraknaknork': '9.1',
          'Kraknaknork\'s Demon': '9.1',
          'Mad Mage': '9.1',
          'Raging Mage': '9.1',
          'Sacred Spider': '9.1',
          'Sandstone Scorpion': '9.1',
          'Shaburak Demon': '9.1',
          'Shaburak Lord': '9.1',
          'Shaburak Prince': '9.1',
          'Slug': '9.1',
          'Spider Queen': '9.1',
          'Starving Wolf': '9.1',
          'Thornfire Wolf': '9.1',
          'Tomb Servant': '9.1',
          'Weakened Demon': '9.1',
          'White Deer': '9.1',
          'Yielothax': '9.1'
        }
      } },
    lootparser_creature_special_names = {
      'Arkhothep': 'Arkhothep (Creature)', 'Armenius': 'Armenius (Creature)',
      'Avalanche': 'Avalanche (Creature)', 'Gamemaster': 'Gamemaster (Creature)',
      'Hacker': 'Hacker (Creature)', 'Pythius the Rotten': 'Pythius the Rotten (Creature)',
      'Slime': 'Slime (Creature)', 'Thief': 'Thief (Creature)',
      'Yalahari': 'Yalahari (Creature)', 'Fish': 'Fish (Creature)',
      'Northern Pike': 'Northern Pike (Creature)'
    },
      creature_items_name_change = {
        'Acolyte of the Cult': {'Book': 'Book (Orange)'},
        'Adept of the Cult': {'Book': 'Book (Orange)'},
        'Barbarian Brutetamer': {'Book': 'Book (Grey Small)'},
        'Blue Djinn': {'Book': 'Book (Blue)'},
        'Demodras': {'Book': 'Book (Gemmed)'},
        'Dragon Lord': {'Book': 'Book (Gemmed)'},
        'Enlightened of the Cult': {'Book': 'Book (Orange)'},
        'Frost Dragon': {'Book': 'Book (Gemmed)'},
        'Ghost': {'Book': 'Book (Orange)'},
        'Green Djinn': {'Book': 'Book (Green)'},
        'Novice of the Cult': {'Book': 'Book (Orange)'},
        'Orc Shaman': {'Book': 'Book (Grey Small)'},
        'Pirate Buccaneer': {'Treasure Map': 'Treasure Map (Pirate)'},
        'Pirate Cutthroat': {'Treasure Map': 'Treasure Map (Pirate)'},
        'Pirate Marauder': {'Treasure Map': 'Treasure Map (Pirate)'},
        'Priestess': {'Book': 'Book (Orange)'},
        'Chopper': {'Dung Ball': 'Dung Ball (Quest)'},
        'Fleshslicer': {'Dung Ball': 'Dung Ball (Quest)'},
        'Maw': {'Dung Ball': 'Dung Ball (Quest)'},
        'Mindmasher': {'Dung Ball': 'Dung Ball (Quest)'},
        'Rotspit': {'Dung Ball': 'Dung Ball (Quest)'},
        'Shadowstalker': {'Dung Ball': 'Dung Ball (Quest)'}
      },
    creature_items_name_paren = {},
    requests_to_do, total_requests, x, y,
    requests_to_do_minus = function () {
      $('#stats_doing1').html('Checking');
      $('#stats_doing2').html(String(Math.abs(requests_to_do - total_requests)) + '/' + String(total_requests));
      requests_to_do--;
      if (requests_to_do <= 0) {
        $('#stats_data').parents('div:first').find('input').removeAttr('disabled');
        $('#stats_doing2').html('Complete');
      }
    },
    check_all = function (filter_function) {
      var dook = function (c, r) {
        var i;
        if (lootparser_creature_special_names.hasOwnProperty(c)) { c = lootparser_creature_special_names[c]; }
        i = $('#stats_result').find('a[title="Loot Statistics:' + c + '"]').parent();
        if (!i.size()) { alert('couldn\'t find for remove\n' + '[title="Loot Statistics:' + c + '"]\n\n\n' + r); }
        else { i.remove(); }
      },
      doajax = function (id) {
        var tthis = $(this), t = $.trim($(this).text().replace('Loot Statistics:', '').replace(' ', '_'));
        $.ajax({cache: false, async: true, url: '/index.php?title=Loot_Statistics:' + t + '&action=raw', success: function (r) {
          if (r === '') { window.setTimeout(function () { doajax(id); }, 3000); }
          else {
            var iserror, creature = r.match(/name\s*=\s*([\.\w \(\)]*)/);
            creature = (creature === null ? false : $.trim(creature[1]).replace(/_/g, ' '));
            if (creature === false) { alert('problem with\n' + r); }
            else {
              iserror = filter_function(r, creature);
              if (iserror === false) {
                dook(creature, r);
              }
              else if (typeof iserror === 'object') {
                tthis.parent().append('<br />');
/*jslint unparam: true*/
                $.each(iserror, function (i, v) { tthis.parent().append(v + '<br />'); });
/*jslint unparam: false*/
              }
            }
            requests_to_do_minus();
          }
        }, error: function (a, b, c) {
          if (a.status !== 404) { alert(a.status + b + c); window.setTimeout(function () { doajax(id); }, 3000); }
          else { requests_to_do_minus(); }
        }
        });
      };
      $('#stats_data').parents('div:first').find('input').attr('disabled', 'disabled');
      $('#stats_result').html($('#stats_data').html());
      $('#stats_result').find('a.new').parent().remove();
      total_requests = requests_to_do = $('#stats_result').find('a').each(doajax).size();
    };

    for (x in creature_items_name_change) { if (creature_items_name_change.hasOwnProperty(x)) {
      for (y in creature_items_name_change[x]) { if (creature_items_name_change[x].hasOwnProperty(y)) {
        if (creature_items_name_change[x][y].match(/\(/) !== null) {
          if (!creature_items_name_paren.hasOwnProperty(x)) { creature_items_name_paren[x] = {}; }
          creature_items_name_paren[x][creature_items_name_change[x][y]] = 0;
        }
      } }
    } }

    $('#stats_data').closest('table').prev().remove();

    $('#stats_data').closest('table').before(
      $('<table>').append(
        $('<tr>').append(
          $('<td>').html('List pages that have wrong parameters or versions<br /><span style="font-size:xx-small;">Please note this only does some checks</span>'),
          $('<td>').append(
            $('<input />', {'value': 'Check', 'type': 'button'}).click(function () { check_all(function (r, creature) {
/*jslint regexp: true*/
              var tt, item, times, amount, total, x, y, ret = [], tmpt, tmps,
              tmp = r.match(/\{\{\s*Loot2[\s\|]*version\s*=\s*(?:\d+\.\d+)[\s\|]*[^}]*/g), tmpa = [],
              versions = r.match(/\{\{\s*Loot2[\s\|]*version\s*=\s*\d+\.\d+/g), versions_n = [], page_name, tmpv;
/*jslint regexp: false*/
              if (versions !== null) {
                for (x = 0; x < versions.length; x++) {
                  y = 0;
                  while (versions[x].substr(y - 1, 1).match(/\d|\./)) { y--; }
                  versions_n[x] = parseFloat(versions[x].substr(y), 10);
                }
/* possible incorrect version */
                page_name = lootparser_creature_special_names.hasOwnProperty(creature) ? lootparser_creature_special_names[creature] : creature;
                for (x = 0; x < versions_n.length; x++) { for (y = 0; y < versions_n.length; y++) {
                  if (x !== y && versions_n[x] === versions_n[y]) {
                    ret.push('Repeated template version:' + versions_n[x]);
                  }
                } }
                if (lootparser_versions_ex[current_tibia_version].changed.hasOwnProperty(page_name)) {
                  tmpv = parseFloat(lootparser_versions_ex[current_tibia_version].changed[page_name], 10);
                  for (x = 0; x < versions_n.length; x++) {
                    if (versions_n[x] > tmpv) { ret.push('Possible incorrect version:' + versions_n[x]); }
                  }
                }
                if (lootparser_versions_ex[current_tibia_version]['new'].hasOwnProperty(page_name)) {
                  tmpv = parseFloat(lootparser_versions_ex[current_tibia_version]['new'][page_name], 10);
                  for (x = 0; x < versions_n.length; x++) {
                    if (versions_n[x] !== tmpv) { ret.push('Possible incorrect version:' + versions_n[x]); }
                  }
                }
/* possible non existing version */
                for (x = 0; x < versions_n.length; x++) {
                  if (
                    versions_n[x] > current_tibia_version &&
                    !lootparser_versions_ex[current_tibia_version]['new'].hasOwnProperty(page_name) &&
                    !lootparser_versions_ex[current_tibia_version].changed.hasOwnProperty(page_name)
                  ) { ret.push('Possible non existing version:' + versions_n[x]); }
                }
/* repeated template version */
                for (x = 0; x < versions_n.length; x++) { for (y = 0; y < versions_n.length; y++) {
                  if (x !== y && versions_n[x] === versions_n[y]) {
                    ret.push('Repeated template version:' + versions_n[x]);
                  }
                } }
              }
/* wrong parameters */
              if (tmp !== null) { for (x = 0; x < tmp.length; x++) {
                tmpa[x] = tmp[x].split(/[\r\n|]+/);
                for (y = 0; y < tmpa[x].length; y++) {
                  tmpt = $.trim(tmpa[x][y]);
                  if (tmpt.indexOf(',') !== -1) {
/*jslint regexp: true*/
                    tt = tmpt.match(/(.*?\(.*?)\s*,/);
/*jslint regexp: false*/
                    if (tt !== null) {
                      item = tt[1];
                      if (
                        !creature_items_name_paren[creature].hasOwnProperty(item)
                      ) { ret.push(tmpt + ' - (non listed item with parenthesis)'); }
                    }

                    if (tmpt.match(/,/g).length !== 1 && tmpt.match(/,/g).length !== 3) { ret.push(tmpt + ' - (wrong number of parameters )'); }
                    else if (tmpt.match(/,/g).length === 3) {
/*jslint regexp: true*/
                      tt = tmpt.match(/(.*?)\s*,\s*times\s*:\s*(\d*)\s*,\s*amount\s*:\s*([\d\-]*)\s*,\s*total\s*:\s*(\d*)/);
/*jslint regexp: false*/
                      if (tt !== null) {
                        item = tt[1];
                        if (item === 'Empty') { ret.push(tmpt + ' - (wrong "Empty")'); }
                        else if ((tmps = item.match(/\s+(?:boots|legs)$/i)) !== null) {
                          ret.push(tmpt + ' - (wrong "' + tmps + '", non countable)');
                        }
                        else {
                          times = (parseInt(tt[2], 10) || 0);
                          amount = tt[3];
                          total = (parseInt(tt[4], 10) || 0);
                          if (times > total) { ret.push(tmpt + ' - (wrong "times" or "total")'); }
                          else if (amount.indexOf('-') === -1 && ((parseInt(amount, 10) || 0) * times) !== total) {
                            ret.push(tmpt + ' - (wrong "times" or "total")');
                          }
                          if (amount.indexOf('-') > -1 && times === total) {
                            ret.push(tmpt + ' - (wrong "times", "amount" or "total")');
                          }
                        }
                      }
                    }
                  }
                  else if (tmpt.indexOf('=') === -1 && tmpt.indexOf('Loot2') === -1 && tmpt !== '') {
                    ret.push(tmpt + ' - (missing parameters)');
                  }
                }
              } }

              ret = (ret.length === 0 ? false : ret);
              return ret;
            }); })
          )
        ),
        $('<tr>').append(
          $('<td>', {'id': 'stats_doing1'}).html(' '),
          $('<td>', {'id': 'stats_doing2'}).html(' ')
        )
      )
    );
  };
  $.ajax({
    url: '/api.php', type: 'GET', dataType: 'json',
    data : {
      'action': 'query', 'format': 'json',
      'meta': 'userinfo', 'uiprop': 'groups'
    },
    success: function (obj) {
      var x;
      if (typeof obj.query !== 'undefined' && typeof obj.query.userinfo !== 'undefined'
      && obj.query.userinfo.groups !== 'undefined') {
        for (x in obj.query.userinfo.groups) { if (obj.query.userinfo.groups.hasOwnProperty(x)) {
          if ((obj.query.userinfo.groups[x]) === 'sysop') { sysop = true; break; }
        } }
      }
      if (sysop) { main_function(); }
    }
  });
}());
//
```