MediaWiki:Bestiary/Code.js

// /*jslint devel: true, browser: true, white: true, indent: 2, plusplus: true, bitwise: true*/ /*global $, wgUserName, mw */ $(window).load(function {   //__NOWYSIWYG__    'use strict';    /*$('#bestiary_creature_img').html( '' );*/   var get_creature_name_from_img_src = function (src) {        var tokens, imgnamecomponents, name;        tokens = src.split(/\//);        /*         * All image links follow a similar pattern: tokens[7] contains image name.         * Assumption: there are no slashes in the image name.         * Assumption: there is only one extension separator (.) in the image name.         * Example: Ancient_Amulet.gif         */        imgnamecomponents = (tokens[7] || "").split(".");        /* First element of the components corresponds to the image file, without the extension. */        name = decodeURIComponent(imgnamecomponents[0].replace(/_/g, ' ').toLowerCase);        return name;    },    bestiary_get_link_for_name = function (creature) {    /* Filter out every image that doesn't match. We should be left with a jQuery object containing one element.    * If we aren't, just get the first one anyway. */   /* Wikia's Lazy Loading functionality requires us to check data-src. If data-src doesn't exist, it may have already been processed, so    use the src as a fallback. This solves a rare issue of (usually a single) image not loading correctly. */   var $creature = $('#bestiary_list_img img').filter(function {         var $this = $(this), dataSrc;         dataSrc = $this.attr('data-src') || $this.attr('src');         if(dataSrc !== null) {             return (get_creature_name_from_img_src(dataSrc) === creature.toLowerCase);         }}).first; return $creature.attr('data-src') || $creature.attr('src'); },   //Get data from DPL table when page is loaded. bestiary_creature_info = function { var ret = {}, x, p, h = $('#bestiary_list').html.replace(/ /gi, ).replace(/<\/p>/gi, ); while (h.search(/\s\s/) !== -1) { h = h.replace(/\s\s/g, ' '); }       h = h.replace(/\s/g, ' '); p = h.split('|'); for (x in p) { if (p.hasOwnProperty(x)) { p[x] = $.trim(p[x]); }       }        while (p[0] === '') { p = p.slice(1); }       for (x = 0; x < p.length; x) { ret[p[x].toLowerCase] = { ur: bestiary_get_link_for_name(p[x]), name: p[x++], hp: p[x++] || '?', exp: p[x++] || '?', armor: p[x++] || '?', speed: p[x++] || '?', bestiaryclass: p[x++].toLowerCase, bestiarylevel: p[x++].toLowerCase, occurrence: p[x++].toLowerCase, physicalDmgMod: (parseInt(p[x++], 10) || 100), earthDmgMod: (parseInt(p[x++], 10) || 100), fireDmgMod: (parseInt(p[x++], 10) || 100), energyDmgMod: (parseInt(p[x++], 10) || 100), iceDmgMod: (parseInt(p[x++], 10) || 100), deathDmgMod: (parseInt(p[x++], 10) || 100), holyDmgMod: (parseInt(p[x++], 10) || 100), healMod: (parseInt(p[x++], 10) || 100), location: p[x++], loot: p[x++] };       }        return ret; },   bestiary_level_imgs = {harmless: 'https://vignette.wikia.nocookie.net/tibia/images/a/ab/Bestiary_Level_Harmless.gif/revision/latest?cb=20180618025240&path-prefix=en', trivial: 'https://vignette.wikia.nocookie.net/tibia/images/d/d1/Bestiary_Level_Trivial.gif/revision/latest?cb=20171226161649&path-prefix=en', easy: 'https://vignette.wikia.nocookie.net/tibia/images/b/bf/Bestiary_Level_Easy.gif/revision/latest?cb=20171226161648&path-prefix=en', medium: 'https://vignette.wikia.nocookie.net/tibia/images/f/ff/Bestiary_Level_Medium.gif/revision/latest?cb=20171226161649&path-prefix=en', hard: 'https://vignette.wikia.nocookie.net/tibia/images/4/46/Bestiary_Level_Hard.gif/revision/latest?cb=20171226161648&path-prefix=en'}, bestiary_occurrence_imgs = {common: 'https://vignette.wikia.nocookie.net/tibia/images/2/2c/Bestiary_Occurrence_Common.gif/revision/latest?cb=20180618025920&path-prefix=en', uncommon: 'https://vignette.wikia.nocookie.net/tibia/images/2/2f/Bestiary_Occurrence_Uncommon.gif/revision/latest?cb=20180618025920&path-prefix=en', rare: 'https://vignette.wikia.nocookie.net/tibia/images/d/d5/Bestiary_Occurrence_Rare.gif/revision/latest?cb=20180618025921&path-prefix=en', veryrare: 'https://vignette.wikia.nocookie.net/tibia/images/0/08/Bestiary_Occurrence_Very_Rare.gif/revision/latest?cb=20180618025921&path-prefix=en'}, bestiary_populate_ui = function(creature) { if (!(creature.toLowerCase in bestiary_creature_info)) { return; }       var data = bestiary_creature_info[creature.toLowerCase], minKills = {harmless: 25, trivial: 250, easy: 500, medium: 1000, hard: 2500}, charmPts = {harmless: 1, trivial: 5, easy: 15, medium: 25, hard: 50}, charmPtsvr = {harmless: 5, trivial: 10, easy: 30, medium: 50, hard: 100}, dmgMods = ['physicalDmgMod', 'earthDmgMod', 'fireDmgMod', 'energyDmgMod', 'iceDmgMod','deathDmgMod', 'holyDmgMod', 'healMod'], itemcounter = {}, rareness = ['common', 'uncommon', 'semirare', 'rare', 'veryrare'], fpath = 'https://tibia.fandom.com/wiki/Special:FilePath?file='; $('#bestiary_creature_img').html(''); $('#bestiary_creature_name').html(data.name); $('#bestiary_creature_hp').html(data.hp); $('#bestiary_creature_exp').html(data.exp); $('#bestiary_creature_speed').html(data.speed); $('#bestiary_creature_armor').html(data.armor); //       $('#bestiary_creature_bestiarylevel').html(''); $('#bestiary_creature_occurrence').html(''); if (data.occurrence == "Very Rare") { $('#bestiary_creature_charms').html(charmPtsvr[data.bestiarylevel]); } else { $('#bestiary_creature_charms').html(charmPts[data.bestiarylevel]); }       //        $('#bestiary_kills2').html(minKills[data.bestiarylevel]);

$.each(dmgMods, function (i, v) {           var tmp = '#bestiary_creature_' + dmgMods[i];            var val = data[v];            if (val === 0) {                $(tmp).attr('class', 'bestiary_dmg_immune');            } else if (val < 100) {                $(tmp).attr('class', 'bestiary_dmg_strong');            } else if (val ==100) {                $(tmp).attr('class', 'bestiary_dmg_neutral');            } else if (val > 100) {                $(tmp).attr('class', 'bestiary_dmg_weak');            }             $(tmp).width(21 + val/5);        }); //       $('#bestiary_creature_locations').html(data.location); //       $.each(rareness, function(i, v) {            $('#bestiary_loot_' + v).empty;            itemcounter[v] = 0;        }); $('#bestiary_loot_common').empty; $('#bestiary_loot_uncommon').empty; $.each($($.parseHTML(data.loot)).find('li'), function(i, v) {           //Removes x symbol and  tag            var item, amount, name, rarity;            item = v.innerHTML.replace(/×\s/, ).replace(//, '/').replace(/<\/a>\s?/, '/').split('/');            amount = item[0];            name = item[1];            rarity = item[2].replace(/[\(\)\-\s]/g, );            if (rarity == 'always' || rarity === '') {                rarity = 'common';            }            $('#bestiary_loot_' + rarity).append(' ');            itemcounter[rarity]++;        }); $.each(rareness, function(i, v) {           if (itemcounter[v] === 0) {                $('#bestiary_loot_' + v).parent.remove;            } else {                var itemsleft = 14 - itemcounter[v];                for (i = 0; i < itemsleft; i++) {                $('#bestiary_loot_' + v).append(' ');                }            }        }); };   $('#bestiary_search').html(        ''    ); $('#bestiary_search_btn').click(function {       bestiary_populate_ui($('#bestiary_input').val.toLowerCase);    }); $('#bestiary_input').keypress(function(e) {       if (e.which == 13) { //Enter key            $('#bestiary_search_btn').click;        }    }); /*$('#bs_orc').click(function {       $('#bestiary_creature_img').html( '' );   });*/ });