User:SixBot/monobook.js

var sb = window.sb || {}; sb.templates = { 'item': { 'parameters': ["List", "GetValue", "newtradesystem", "name", "itemid", "flavortext", "implemented", "itemclass", "primarytype", "secondarytype", "attrib", "weight", "writable", "writechars", "droppedby", "value", "npcvalue", "npcprice", "buyfrom", "sellto", "notes", "levelrequired", "vocrequired", "hands", "type", "attack", "defense", "enchantable", "stackable", "npcvaluerook", "npcpricerook", "armor", "ingamestatus", "resist", "words", "lightcolor", "lightradius", "mlrequired", "volume", "consumable", "regenseconds", "sounds", "range", "atk_mod", "hit_mod", "defensemod", "edible", "destructible", "moveable", "elementattack", "enchanted", "mana", "damagetype", "damage", "duration"], },   'npc': { 'parameters': ["List", "GetValue", "name", "job", "job2", "location", "city", "buysell", "implemented", "sounds", "notes", "buys", "sells", "job3", "job4", "city2", "job5"], },   'creature': { 'parameters': ["List", "GetValue", "name", "implemented", "hp", "exp", "ratio", "summon", "convince", "illusionable", "creatureclass", "primarytype", "secondarytype", "isboss", "isarenaboss", "abilities", "maxdmg", "pushable", "pushobjects", "immunities", "physicalDmgMod", "holyDmgMod", "deathDmgMod", "fireDmgMod", "energyDmgMod", "iceDmgMod", "earthDmgMod", "drownDmgMod", "hpDrainDmgMod", "behaviour", "sounds", "location", "notes", "strategy", "loot", "article", "actualname", "plural", "walksthrough", "ignoresfields", "runstat", "behavior", "armor", "runsat", "walksaround", "senseinvis", "usedelements", "paraimmune"], } }; sb.getParameters = function (templateName, paramNames, callback) { var MAX_CONNECTIONS = 5, pArr = [], loaded = 0, count = paramNames.length, requested = 0, finished = false, i;   function load { var index = requested; if (index < count) { requested += 1; $.ajax({               url: ['', 'wiki', 'TibiaWiki:Data', templateName, paramNames[index] ].join("/") + "?action=raw",                type: 'GET',                dataType: 'json',                success: function (jso) {                    loaded += 1;                    pArr[index] = jso;                    load;                }            }); } else { if (requested === loaded) { if (finished) { // Already called callback; return false; } else { finished = true; callback(pArr); }           }        }    }    for (i = 0; i < MAX_CONNECTIONS; i += 1) { load; } }; (function (tw) {   var results = [];    var PATH_API = "/api.php";    var DIR_CONTENT = "/wiki/";    var NAMESPACE_MAIN = 0,        NAMESPACE_CATEGORY = 14,        NAMESPACE_LOOT_STATISTICS = 112;    var O_QUERY_GENERIC = {        'format': 'json',        'namespace': NAMESPACE_MAIN,        'action': 'query'    };    var O_QUERY_CATEGORY_MEMBERS = {        'generator': 'categorymembers',        'gaplimit': '100'    };    var O_QUERY_CONTENTS = {        'prop': 'revisions',        'rvprop': 'content'    };    function mixin(base) {        var i, len, prop, o;        for (i = 1, len = arguments.length; i < len; i += 1) {            o = arguments[i];            for (prop in o) {                if (o.hasOwnProperty(prop)) {                    base[prop] = o[prop];                }            }        }        return base;    }    function query(url, data, callback) {        $.ajax({ 'url': url, 'type': 'GET', 'dataType': 'json', 'data': mixin({}, O_QUERY_GENERIC, data), 'success': callback });   }    function queryCategory(category, ns, callback) {        var localResults, data = mixin({}, O_QUERY_CATEGORY_MEMBERS, O_QUERY_CONTENTS, { 'gcmtitle': 'Category:' + category, 'gcmnamespace': ns       });        localResults = results = [];        function queryContinue(o) {            var qPages, p;            // Record the items in the query            qPages = (o && o.query && o.query.pages); // Generator -> o.query.pages, List -> o.query.categorymembers.            for (p in qPages) {                if (qPages.hasOwnProperty(p)) {                    localResults.push(qPages[p]);                }            }            if (o && o["query-continue"] && o["query-continue"].categorymembers && o["query-continue"].categorymembers.gcmcontinue) {                query(PATH_API, mixin({}, data, {                    'gcmcontinue': o["query-continue"].categorymembers.gcmcontinue                }), queryContinue);            } else {                if (callback) {                    callback(localResults);                }            }        }        query(PATH_API, mixin({}, data), queryContinue); return localResults; }   tw.results = results; tw.query = query; tw.queryCategory = queryCategory; return tw; }(tw = (window.tw || {}))); (function (tw) { var categoryMap = { 'Item': 'Items', 'NPC': 'NPCs in Tibia', 'Object': 'Objects', 'Spell': 'Spells' };   var paramKeyMap = { 'Item': 'item', 'Creature': 'creature', 'NPC': 'npc' };   function getParameterNames(type) { var key = paramKeyMap[type]; return sb.templates[key].parameters; }   function getParameter(str, param, type) { var parameterNames = getParameterNames(type); var regex = new RegExp("\\|\\s*" + param +"\\s*=\\s*([\\w\\W]+?)((\\|\\s*(" + parameterNames.join("|") +")\\s*)|\\|?}}[^}]+$)"); var match = str.match(regex); return match && match[1]; }   function extractParameters(data, params, type) { var i, len, o = {}, rev, pid, paramValue; for (p = 0, plen = params.length; p < plen; p += 1) { param = params[p]; o[param] = {}; for (i = 0, len = data.length; i < len; i += 1) { pid = data[i].pageid; rev = data[i].revisions[0]['*']; paramValue = getParameter(rev, param, type); if (paramValue !== null) { o[param][pid] = paramValue; }           }        }        return o;    } function updateDataPages(type, data) { var parameters = sb.templates[paramKeyMap[type]].parameters, pi = 0, i, MAX_CONCURRENT_SUBMITS = 3; function next { if (parameters.length >= pi) { submit(pi); pi += 1; }       }        function submit(i) { var param = parameters[i], d = data[param], text = JSON.stringify(d); $.ajax({               url: '/wiki/' + 'TibiaWiki:Data/' + type + '/' + param + "?action=raw",                success: function (r) {                    console.log(text);                    if (text === r) {                        console.log("No change to:", param);                        return next;                    }                    $.ajax({ url: '/api.php', type: 'POST', data: { action: 'edit', title: 'TibiaWiki:Data/' + type + '/' + param, text: text, summary: 'Automatic data update.', bot: '1', format: 'json', token: mw.user.tokens.get('editToken') },                       success: next });               }            });        }        for (i = 0; i < MAX_CONCURRENT_SUBMITS; i += 1) { next; }   }    tw.data = { 'update': function { var i = 0, len = arguments.length, type; for (i; i < len; i += 1) { type = arguments[i]; if (categoryMap[type]) { tw.queryCategory(categoryMap[type], '', function (d) {                       updateDataPages(type, extractParameters(d, sb.templates[paramKeyMap[type]].parameters, type));                        console.log("Done");                    }); } else { console.log("Could not find category for type " + type); }           }        }    }; }(tw = tw || {}));