MediaWiki:CreatureStatistics.js

//  /*global $, ts_makeSortable*/

var ajax_load_bar = new Image; ajax_load_bar.src = "http://images3.wikia.nocookie.net/tibia/en/images/d/de/Ajax_Load_Bar.gif";

var creature_statistics_string = ''; var creature_statistics_obj = {}; var creature_statistics_basetimestamp, creature_statistics_token; var sysop = false;

var array_sort = function(inputArr, numeric, by_key, reverse, sub_key) { var tmp_arr={}, valArr=[], keyArr=[], keys=[], sorter, i, k, populateArr = []; if (typeof numeric === 'undefined') { numeric = false;} if (typeof by_key === 'undefined') { by_key = false;} if (typeof reverse === 'undefined') { reverse = false;} if (typeof sub_key === 'undefined') { sub_key = '';} var is_numeric = function(v) { v = parseFloat(String(v).replace('|', '')); return (typeof v === 'number' && !isNaN(v)); }; if (numeric) { sorter = function(a, b) { a = parseFloat(String(a).replace('|', )) || 0; b = parseFloat(String(b).replace('|', )) || 0; return (reverse ? b - a : a - b); }; } else { sorter = function(a, b) { var x = a, y = b, tmp; if (!is_numeric(a) && !is_numeric(b)) { tmp = (function(a,b){a=a.search(/[a-z]/);b=b.search(/[a-z]/);if((a!==0 && b!==0)||a===b){return 0;}if(a===0){return -1;}if(b===0){return 1;}}(x, y)); if (tmp !== 0) { return tmp*(reverse?-1:1); } if (a.search(/a[n]? /i) === 0) { a = a.substr(a.indexOf(' ')+1); } if (b.search(/a[n]? /i) === 0) { b = b.substr(b.indexOf(' ')+1); } if (a === b) { return 0; } if (a > b) { return (reverse ? -1 : 1); } return (reverse ? 1 : -1); }     else { a = parseFloat(a) || 0; b = parseFloat(a) || 0; return (reverse ? b - a : a - b); } }; }  if (by_key) { /*Make a list of key names*/ for (k in inputArr) { if (inputArr.hasOwnProperty(k)) { keys.push(k); } } keys.sort(sorter); /*Rebuild array with sorted key names*/ for (i = 0; i < keys.length; i++) { k = keys[i]; tmp_arr[k] = inputArr[k]; } for (i in tmp_arr) { if (tmp_arr.hasOwnProperty(i)) { populateArr[i] = tmp_arr[i]; } } } else { var bubbleSort = function(keyArr, inputArr, sub_key) { var i, j, tempValue, tempKeyVal, ret; for (i = inputArr.length-2; i >= 0; i--) { for (j = 0; j <= i; j++) { ret = (sub_key === '') ? sorter(inputArr[j+1], inputArr[j]) : sorter(inputArr[j+1][sub_key], inputArr[j][sub_key]); if (ret < 0) { tempValue = inputArr[j]; inputArr[j] = inputArr[j+1]; inputArr[j+1] = tempValue; tempKeyVal = keyArr[j]; keyArr[j] = keyArr[j+1]; keyArr[j+1] = tempKeyVal; }       }      }    }; /*Get key and value*/ for (k in inputArr) { if (inputArr.hasOwnProperty(k)) { valArr.push(inputArr[k]); keyArr.push(k); } } /*Sort our new temporary arrays*/ try { bubbleSort(keyArr, valArr, sub_key); } catch (e) { return false; } /*Repopulate the old array*/ for (i = 0; i < valArr.length; i++) { populateArr[keyArr[i]] = valArr[i]; } } return populateArr; }; var numcs = function(n) { n = String(n); while ((/\d{4}/).test(n)) { n = n.replace(/(\d{3},|\d{3}$)/, ',$1'); } return n; }; var creature_statistics_merge = function(data, sort_by_occurrences) { $('#creature_statistics_block p').html('Processing

'); if (typeof sort_by_occurrences === 'undefined') { sort_by_occurrences = false; }  var x, y, t = '', tmp, stats = {'heal':{}, 'damage':{}};  var action, origin, target, amount;  data = data.split(/(?:\r\n|\r|\n){1,10}/g);  for (x=0;x '; $('#creature_statistics_tables').append(           ' '+this_origin+' '+action+' '+(target==='-'?'self':target)+' ('+times+' times)'+' - ',            $(' ', {'style':'cursor:pointer;color:#0148c2;text-decoration:underline;'}).html('Show').click(creature_statistics_table_toggle),            tmp,            $(' ', {'style':'display:none;cursor:pointer;color:#0148c2;text-decoration:underline;'}).html('up').click(creature_statistics_table_up)          ); i++; } }     }    } }  });  if (i > 0) { $('table.creature_statistics').each(function { try{ts_makeSortable($(this)[0]);}catch(er){} }); }  else { $('#creature_statistics_tables').html(' Nothing to show with the current selection'); } creature_statistics_end; }; var creature_statistics_generator_init = function {  var action, origin;  var i = 0, options = '';  for (action in creature_statistics_obj) { if (creature_statistics_obj.hasOwnProperty(action)) {    for (origin in creature_statistics_obj[action]) { if (creature_statistics_obj[action].hasOwnProperty(origin)) {      options += ''+origin+' ';      i++;    } }  } }  $('#creature_statistics_generator').empty.append( $(' ', {'class':'wikitable'}).append($(' ').append( $(' ', {'style':'vertical-align:top;padding:5px 20px;'}).append(       $(' ', {'type':'checkbox', 'id':'creature_statistics_generator_ch', 'checked':'checked'}),        'Include heal ',        $(' ', {'type':'checkbox', 'id':'creature_statistics_generator_cd', 'checked':'checked'}),        'Include damage'      ), $(' ', {'style':'vertical-align:top;padding:5px 20px;'}).append(       'Creatures to include, use control+click to multi select ',        $('  ', {'style':'cursor:pointer;color:#0148c2;text-decoration:underline;'}).html('Select all')        .click(function { $('#creature_statistics_generator_sel').find('option').attr('selected', 'selected'); }),        ' ',        $(' ', {'id':'creature_statistics_generator_sel', 'multiple':'multiple', 'size':Math.min(i, 8)}).html(options)      ) )),   ' ',    $(' ', {'type':'button', 'value':'Generate tables'}).click(creature_statistics_generator_gen), ' ',   $('  ', {'id':'creature_statistics_tables'}) ); };

var creature_statistics_init = function { $('#creature_statistics_main').empty.css({'text-align':'center','min-height':'100px'}).append(   $(' ', {'style':'width:100%;'}).append($(' ').append(      $(' ', {'style':'width:50%;', 'id':'creature_statistics_td_load'}).append( $(' ', {'cols':'20','rows':'5', 'style':'width:100%;margin-bottom:5px;', 'id':'creature_statistics_pt'}), $(' ', {'type':'button', 'id':'creature_statistics_p', 'value':'Process your pasted data'}), ' ',       '(Creature heal and damage statistics from ',        $('', {'href':'/wiki/Loot_Statistics'}).html('Loot Statistics').click(function{window.open($(this).attr('href'));}),        ')', '

OR

',       $(' ', {'type':'button', 'id':'creature_statistics_l', 'value':'Load data saved on tibiawiki'}) ),     $(' ', {'style':'width:50%;', 'id':'creature_statistics_td_sysop'}).append(' ')    )),    $('  ', {'id':'creature_statistics_generator', 'style':'text-align:left;'})  );

$('#creature_statistics_main').append(   $('  ', {id:'creature_statistics_block'})      .css({'display':'none', 'position':'absolute', 'top':'0px', 'left':'0px', 'width':'100%', 'height':'100%', 'background-color':'#666666', 'text-align':'center'})      .html(' ') ); };

var creature_statistics_upload = function { $('#creature_statistics_up').attr('disabled','disabled'); $('#creature_statistics_block p').html('Uploading

'); $('#creature_statistics_block').fadeTo(400, 0.8);  var upload_stats = function {    var errort = 'There was an error uploading, try again';    $.ajax({ url: '/api.php', type: 'POST', dataType:'json', data : { 'action':'edit', 'title':'TibiaWiki:Creature_Statistics/Statistics', 'format':'json', 'summary':'merging', 'text':creature_statistics_string, 'token':creature_statistics_token, 'basetimestamp':creature_statistics_basetimestamp, 'minor':'minor' },     error: function { creature_statistics_end(errort, 2); }, success: function(obj) { if (typeof obj.edit !== 'undefined' && typeof obj.edit.result !== 'undefined'       && obj.edit.result === 'Success') { creature_statistics_end('Stats uploaded.', 2); }       else { creature_statistics_end(errort, 2); } }   });  };  upload_stats; };

var creature_statistics_text_to_stats = function(text) { var tmp = creature_statistics_merge(text); creature_statistics_string = tmp[0]; creature_statistics_obj = tmp[1]; creature_statistics_generator_init; creature_statistics_end; $('#creature_statistics_td_load').empty.append(   $(' ', {'type':'button', 'value':'Go back'}).click(function { creature_statistics_init; })  ); if (sysop) { $('#creature_statistics_stats_size').val('Old: '+numcs(text.length)+' / Merged: '+numcs(creature_statistics_string.length)); $('#creature_statistics_up').removeAttr('disabled'); } };

var creature_statistics_load = function(from_teaxtarea) { $('#creature_statistics_l').attr('disabled','disabled'); $('#creature_statistics_block p').html('Downloading

'); $('#creature_statistics_block').fadeTo(400, 0.8);  if (typeof from_teaxtarea !== 'undefined') {    creature_statistics_text_to_stats($('#'+from_teaxtarea).val);    return;  }  if (sysop) {    $('#creature_statistics_td_sysop').append( 'Size: ', $(' ', {'id':'creature_statistics_stats_size', 'type':'text', 'size':'30', 'value':'', 'readonly':'readonly', 'style':'margin: 10px 0px'}), $(' ', {'type':'button', 'value':'Upload merged', 'id':'creature_statistics_up'}).click(function { creature_statistics_upload; }) ); }  var errort = 'There was a problem downloading, try again.';  var get_stats_contents = function {    $.ajax({ url: '/api.php', type: 'GET', dataType:'json', data : { 'action':'query', 'titles':'TibiaWiki:Creature_Statistics/Statistics', 'format':'json', 'rvprop':'content', 'prop':'info|revisions', 'intoken':'edit' },     error: function { creature_statistics_end(errort, 2); }, success: function(obj) { var x;       for (x in obj.query.pages) { if (obj.query.pages.hasOwnProperty(x)) { if (typeof obj.query.pages[x].missing !== 'undefined') { creature_statistics_end(errort, 2); } else { creature_statistics_text_to_stats($.trim(obj.query.pages[x].revisions[0]['*'])); }         break; } }     }    });  };  var get_edit_info = function {    $.ajax({ url: '/api.php', type: 'GET', dataType:'json', data : { 'action':'query', 'titles':'TibiaWiki:Creature_Statistics/Statistics', 'format':'json', 'prop':'info|revisions', 'intoken':'edit' },     error: function { creature_statistics_end(errort, 2); }, success: function(obj) { var x;       for (x in obj.query.pages) { if (obj.query.pages.hasOwnProperty(x)) { creature_statistics_token = obj.query.pages[x].edittoken; if (typeof obj.query.pages[x].missing !== 'undefined') { creature_statistics_basetimestamp = ''; } else { creature_statistics_basetimestamp = obj.query.pages[x].revisions[0].timestamp; get_stats_contents; } break; } }     }    });  };  get_edit_info; };

$('#creature_statistics_l').live('click', function { creature_statistics_load; }); $('#creature_statistics_p').live('click', function { creature_statistics_load('creature_statistics_pt'); });

(function{ $.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; } } }     }      creature_statistics_init; $('#creature_statistics_img').hide; $('#creature_statistics_main').show; } }); }); //